344a1f16de8a0073f903072a386e0eb3ef2ffa33
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_context.c
1 /*
2  *
3  * (C) COPYRIGHT 2010-2016 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18
19
20 /*
21  * Base kernel context APIs
22  */
23
24 #include <mali_kbase.h>
25 #include <mali_midg_regmap.h>
26 #include <mali_kbase_mem_linux.h>
27
28 /**
29  * kbase_create_context() - Create a kernel base context.
30  * @kbdev: Kbase device
31  * @is_compat: Force creation of a 32-bit context
32  *
33  * Allocate and init a kernel base context.
34  *
35  * Return: new kbase context
36  */
37 struct kbase_context *
38 kbase_create_context(struct kbase_device *kbdev, bool is_compat)
39 {
40         struct kbase_context *kctx;
41         int err;
42
43         KBASE_DEBUG_ASSERT(kbdev != NULL);
44
45         /* zero-inited as lot of code assume it's zero'ed out on create */
46         kctx = vzalloc(sizeof(*kctx));
47
48         if (!kctx)
49                 goto out;
50
51         /* creating a context is considered a disjoint event */
52         kbase_disjoint_event(kbdev);
53
54         kctx->kbdev = kbdev;
55         kctx->as_nr = KBASEP_AS_NR_INVALID;
56         kctx->is_compat = is_compat;
57 #ifdef CONFIG_MALI_TRACE_TIMELINE
58         kctx->timeline.owner_tgid = task_tgid_nr(current);
59 #endif
60         atomic_set(&kctx->setup_complete, 0);
61         atomic_set(&kctx->setup_in_progress, 0);
62         kctx->infinite_cache_active = 0;
63         spin_lock_init(&kctx->mm_update_lock);
64         kctx->process_mm = NULL;
65         atomic_set(&kctx->nonmapped_pages, 0);
66         kctx->slots_pullable = 0;
67         kctx->tgid = current->tgid;
68         kctx->pid = current->pid;
69
70         err = kbase_mem_pool_init(&kctx->mem_pool,
71                         kbdev->mem_pool_max_size_default,
72                         kctx->kbdev, &kbdev->mem_pool);
73         if (err)
74                 goto free_kctx;
75
76         err = kbase_mem_evictable_init(kctx);
77         if (err)
78                 goto free_pool;
79
80         atomic_set(&kctx->used_pages, 0);
81
82         err = kbase_jd_init(kctx);
83         if (err)
84                 goto deinit_evictable;
85
86         err = kbasep_js_kctx_init(kctx);
87         if (err)
88                 goto free_jd;   /* safe to call kbasep_js_kctx_term  in this case */
89
90         err = kbase_event_init(kctx);
91         if (err)
92                 goto free_jd;
93
94         atomic_set(&kctx->drain_pending, 0);
95
96         mutex_init(&kctx->reg_lock);
97
98         INIT_LIST_HEAD(&kctx->waiting_soft_jobs);
99         spin_lock_init(&kctx->waiting_soft_jobs_lock);
100 #ifdef CONFIG_KDS
101         INIT_LIST_HEAD(&kctx->waiting_kds_resource);
102 #endif
103         err = kbase_dma_fence_init(kctx);
104         if (err)
105                 goto free_event;
106
107         err = kbase_mmu_init(kctx);
108         if (err)
109                 goto term_dma_fence;
110
111         kctx->pgd = kbase_mmu_alloc_pgd(kctx);
112         if (!kctx->pgd)
113                 goto free_mmu;
114
115         kctx->aliasing_sink_page = kbase_mem_pool_alloc(&kctx->mem_pool);
116         if (!kctx->aliasing_sink_page)
117                 goto no_sink_page;
118
119         init_waitqueue_head(&kctx->event_queue);
120
121         kctx->cookies = KBASE_COOKIE_MASK;
122
123         /* Make sure page 0 is not used... */
124         err = kbase_region_tracker_init(kctx);
125         if (err)
126                 goto no_region_tracker;
127
128         err = kbase_sticky_resource_init(kctx);
129         if (err)
130                 goto no_sticky;
131
132         err = kbase_jit_init(kctx);
133         if (err)
134                 goto no_jit;
135 #ifdef CONFIG_GPU_TRACEPOINTS
136         atomic_set(&kctx->jctx.work_id, 0);
137 #endif
138 #ifdef CONFIG_MALI_TRACE_TIMELINE
139         atomic_set(&kctx->timeline.jd_atoms_in_flight, 0);
140 #endif
141
142         kctx->id = atomic_add_return(1, &(kbdev->ctx_num)) - 1;
143
144         mutex_init(&kctx->vinstr_cli_lock);
145
146         setup_timer(&kctx->soft_job_timeout,
147                     kbasep_soft_job_timeout_worker,
148                     (uintptr_t)kctx);
149
150         return kctx;
151
152 no_jit:
153         kbase_gpu_vm_lock(kctx);
154         kbase_sticky_resource_term(kctx);
155         kbase_gpu_vm_unlock(kctx);
156 no_sticky:
157         kbase_region_tracker_term(kctx);
158 no_region_tracker:
159         kbase_mem_pool_free(&kctx->mem_pool, kctx->aliasing_sink_page, false);
160 no_sink_page:
161         /* VM lock needed for the call to kbase_mmu_free_pgd */
162         kbase_gpu_vm_lock(kctx);
163         kbase_mmu_free_pgd(kctx);
164         kbase_gpu_vm_unlock(kctx);
165 free_mmu:
166         kbase_mmu_term(kctx);
167 term_dma_fence:
168         kbase_dma_fence_term(kctx);
169 free_event:
170         kbase_event_cleanup(kctx);
171 free_jd:
172         /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
173         kbasep_js_kctx_term(kctx);
174         kbase_jd_exit(kctx);
175 deinit_evictable:
176         kbase_mem_evictable_deinit(kctx);
177 free_pool:
178         kbase_mem_pool_term(&kctx->mem_pool);
179 free_kctx:
180         vfree(kctx);
181 out:
182         return NULL;
183 }
184 KBASE_EXPORT_SYMBOL(kbase_create_context);
185
186 static void kbase_reg_pending_dtor(struct kbase_va_region *reg)
187 {
188         dev_dbg(reg->kctx->kbdev->dev, "Freeing pending unmapped region\n");
189         kbase_mem_phy_alloc_put(reg->cpu_alloc);
190         kbase_mem_phy_alloc_put(reg->gpu_alloc);
191         kfree(reg);
192 }
193
194 /**
195  * kbase_destroy_context - Destroy a kernel base context.
196  * @kctx: Context to destroy
197  *
198  * Calls kbase_destroy_os_context() to free OS specific structures.
199  * Will release all outstanding regions.
200  */
201 void kbase_destroy_context(struct kbase_context *kctx)
202 {
203         struct kbase_device *kbdev;
204         int pages;
205         unsigned long pending_regions_to_clean;
206
207         KBASE_DEBUG_ASSERT(NULL != kctx);
208
209         kbdev = kctx->kbdev;
210         KBASE_DEBUG_ASSERT(NULL != kbdev);
211
212         KBASE_TRACE_ADD(kbdev, CORE_CTX_DESTROY, kctx, NULL, 0u, 0u);
213
214         /* Ensure the core is powered up for the destroy process */
215         /* A suspend won't happen here, because we're in a syscall from a userspace
216          * thread. */
217         kbase_pm_context_active(kbdev);
218
219         kbase_jd_zap_context(kctx);
220         kbase_event_cleanup(kctx);
221
222         /*
223          * JIT must be terminated before the code below as it must be called
224          * without the region lock being held.
225          * The code above ensures no new JIT allocations can be made by
226          * by the time we get to this point of context tear down.
227          */
228         kbase_jit_term(kctx);
229
230         kbase_gpu_vm_lock(kctx);
231
232         kbase_sticky_resource_term(kctx);
233
234         /* MMU is disabled as part of scheduling out the context */
235         kbase_mmu_free_pgd(kctx);
236
237         /* drop the aliasing sink page now that it can't be mapped anymore */
238         kbase_mem_pool_free(&kctx->mem_pool, kctx->aliasing_sink_page, false);
239
240         /* free pending region setups */
241         pending_regions_to_clean = (~kctx->cookies) & KBASE_COOKIE_MASK;
242         while (pending_regions_to_clean) {
243                 unsigned int cookie = __ffs(pending_regions_to_clean);
244
245                 BUG_ON(!kctx->pending_regions[cookie]);
246
247                 kbase_reg_pending_dtor(kctx->pending_regions[cookie]);
248
249                 kctx->pending_regions[cookie] = NULL;
250                 pending_regions_to_clean &= ~(1UL << cookie);
251         }
252
253         kbase_region_tracker_term(kctx);
254         kbase_gpu_vm_unlock(kctx);
255
256         /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
257         kbasep_js_kctx_term(kctx);
258
259         kbase_jd_exit(kctx);
260
261         kbase_pm_context_idle(kbdev);
262
263         kbase_dma_fence_term(kctx);
264
265         kbase_mmu_term(kctx);
266
267         pages = atomic_read(&kctx->used_pages);
268         if (pages != 0)
269                 dev_warn(kbdev->dev, "%s: %d pages in use!\n", __func__, pages);
270
271         kbase_mem_evictable_deinit(kctx);
272         kbase_mem_pool_term(&kctx->mem_pool);
273         WARN_ON(atomic_read(&kctx->nonmapped_pages) != 0);
274
275         vfree(kctx);
276 }
277 KBASE_EXPORT_SYMBOL(kbase_destroy_context);
278
279 /**
280  * kbase_context_set_create_flags - Set creation flags on a context
281  * @kctx: Kbase context
282  * @flags: Flags to set
283  *
284  * Return: 0 on success
285  */
286 int kbase_context_set_create_flags(struct kbase_context *kctx, u32 flags)
287 {
288         int err = 0;
289         struct kbasep_js_kctx_info *js_kctx_info;
290         unsigned long irq_flags;
291
292         KBASE_DEBUG_ASSERT(NULL != kctx);
293
294         js_kctx_info = &kctx->jctx.sched_info;
295
296         /* Validate flags */
297         if (flags != (flags & BASE_CONTEXT_CREATE_KERNEL_FLAGS)) {
298                 err = -EINVAL;
299                 goto out;
300         }
301
302         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
303         spin_lock_irqsave(&kctx->kbdev->js_data.runpool_irq.lock, irq_flags);
304
305         /* Translate the flags */
306         if ((flags & BASE_CONTEXT_SYSTEM_MONITOR_SUBMIT_DISABLED) == 0)
307                 js_kctx_info->ctx.flags &= ~((u32) KBASE_CTX_FLAG_SUBMIT_DISABLED);
308
309         /* Latch the initial attributes into the Job Scheduler */
310         kbasep_js_ctx_attr_set_initial_attrs(kctx->kbdev, kctx);
311
312         spin_unlock_irqrestore(&kctx->kbdev->js_data.runpool_irq.lock,
313                         irq_flags);
314         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
315  out:
316         return err;
317 }
318 KBASE_EXPORT_SYMBOL(kbase_context_set_create_flags);