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