Merge branch 'android-4.4' of https://android.googlesource.com/kernel/common
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_context.c
1 /*
2  *
3  * (C) COPYRIGHT 2010-2015 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
28 #define MEMPOOL_PAGES 16384
29
30
31 /**
32  * kbase_create_context() - Create a kernel base context.
33  * @kbdev: Kbase device
34  * @is_compat: Force creation of a 32-bit context
35  *
36  * Allocate and init a kernel base context.
37  *
38  * Return: new kbase context
39  */
40 struct kbase_context *
41 kbase_create_context(struct kbase_device *kbdev, bool is_compat)
42 {
43         struct kbase_context *kctx;
44         int mali_err;
45
46         KBASE_DEBUG_ASSERT(kbdev != NULL);
47
48         /* zero-inited as lot of code assume it's zero'ed out on create */
49         kctx = vzalloc(sizeof(*kctx));
50
51         if (!kctx)
52                 goto out;
53
54         /* creating a context is considered a disjoint event */
55         kbase_disjoint_event(kbdev);
56
57         kctx->kbdev = kbdev;
58         kctx->as_nr = KBASEP_AS_NR_INVALID;
59         kctx->is_compat = is_compat;
60 #ifdef CONFIG_MALI_TRACE_TIMELINE
61         kctx->timeline.owner_tgid = task_tgid_nr(current);
62 #endif
63         atomic_set(&kctx->setup_complete, 0);
64         atomic_set(&kctx->setup_in_progress, 0);
65         kctx->infinite_cache_active = 0;
66         spin_lock_init(&kctx->mm_update_lock);
67         kctx->process_mm = NULL;
68         atomic_set(&kctx->nonmapped_pages, 0);
69         kctx->slots_pullable = 0;
70
71         if (kbase_mem_allocator_init(&kctx->osalloc, MEMPOOL_PAGES, kctx->kbdev) != 0)
72                 goto free_kctx;
73
74         kctx->pgd_allocator = &kctx->osalloc;
75         atomic_set(&kctx->used_pages, 0);
76
77         if (kbase_jd_init(kctx))
78                 goto free_allocator;
79
80         mali_err = kbasep_js_kctx_init(kctx);
81         if (mali_err)
82                 goto free_jd;   /* safe to call kbasep_js_kctx_term  in this case */
83
84         mali_err = kbase_event_init(kctx);
85         if (mali_err)
86                 goto free_jd;
87
88         mutex_init(&kctx->reg_lock);
89
90         INIT_LIST_HEAD(&kctx->waiting_soft_jobs);
91 #ifdef CONFIG_KDS
92         INIT_LIST_HEAD(&kctx->waiting_kds_resource);
93 #endif
94
95         mali_err = kbase_mmu_init(kctx);
96         if (mali_err)
97                 goto free_event;
98
99         kctx->pgd = kbase_mmu_alloc_pgd(kctx);
100         if (!kctx->pgd)
101                 goto free_mmu;
102
103         if (kbase_mem_allocator_alloc(&kctx->osalloc, 1, &kctx->aliasing_sink_page) != 0)
104                 goto no_sink_page;
105
106         kctx->tgid = current->tgid;
107         kctx->pid = current->pid;
108         init_waitqueue_head(&kctx->event_queue);
109
110         kctx->cookies = KBASE_COOKIE_MASK;
111
112         /* Make sure page 0 is not used... */
113         if (kbase_region_tracker_init(kctx))
114                 goto no_region_tracker;
115 #ifdef CONFIG_GPU_TRACEPOINTS
116         atomic_set(&kctx->jctx.work_id, 0);
117 #endif
118 #ifdef CONFIG_MALI_TRACE_TIMELINE
119         atomic_set(&kctx->timeline.jd_atoms_in_flight, 0);
120 #endif
121
122         kctx->id = atomic_add_return(1, &(kbdev->ctx_num)) - 1;
123
124         mutex_init(&kctx->vinstr_cli_lock);
125
126         return kctx;
127
128 no_region_tracker:
129 no_sink_page:
130         kbase_mem_allocator_free(&kctx->osalloc, 1, &kctx->aliasing_sink_page, 0);
131         /* VM lock needed for the call to kbase_mmu_free_pgd */
132         kbase_gpu_vm_lock(kctx);
133         kbase_mmu_free_pgd(kctx);
134         kbase_gpu_vm_unlock(kctx);
135 free_mmu:
136         kbase_mmu_term(kctx);
137 free_event:
138         kbase_event_cleanup(kctx);
139 free_jd:
140         /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
141         kbasep_js_kctx_term(kctx);
142         kbase_jd_exit(kctx);
143 free_allocator:
144         kbase_mem_allocator_term(&kctx->osalloc);
145 free_kctx:
146         vfree(kctx);
147 out:
148         return NULL;
149 }
150 KBASE_EXPORT_SYMBOL(kbase_create_context);
151
152 static void kbase_reg_pending_dtor(struct kbase_va_region *reg)
153 {
154         dev_dbg(reg->kctx->kbdev->dev, "Freeing pending unmapped region\n");
155         kbase_mem_phy_alloc_put(reg->cpu_alloc);
156         kbase_mem_phy_alloc_put(reg->gpu_alloc);
157         kfree(reg);
158 }
159
160 /**
161  * kbase_destroy_context - Destroy a kernel base context.
162  * @kctx: Context to destroy
163  *
164  * Calls kbase_destroy_os_context() to free OS specific structures.
165  * Will release all outstanding regions.
166  */
167 void kbase_destroy_context(struct kbase_context *kctx)
168 {
169         struct kbase_device *kbdev;
170         int pages;
171         unsigned long pending_regions_to_clean;
172
173         KBASE_DEBUG_ASSERT(NULL != kctx);
174
175         kbdev = kctx->kbdev;
176         KBASE_DEBUG_ASSERT(NULL != kbdev);
177
178         KBASE_TRACE_ADD(kbdev, CORE_CTX_DESTROY, kctx, NULL, 0u, 0u);
179
180         /* Ensure the core is powered up for the destroy process */
181         /* A suspend won't happen here, because we're in a syscall from a userspace
182          * thread. */
183         kbase_pm_context_active(kbdev);
184
185         kbase_jd_zap_context(kctx);
186         kbase_event_cleanup(kctx);
187
188         kbase_gpu_vm_lock(kctx);
189
190         /* MMU is disabled as part of scheduling out the context */
191         kbase_mmu_free_pgd(kctx);
192
193         /* drop the aliasing sink page now that it can't be mapped anymore */
194         kbase_mem_allocator_free(&kctx->osalloc, 1, &kctx->aliasing_sink_page, 0);
195
196         /* free pending region setups */
197         pending_regions_to_clean = (~kctx->cookies) & KBASE_COOKIE_MASK;
198         while (pending_regions_to_clean) {
199                 unsigned int cookie = __ffs(pending_regions_to_clean);
200
201                 BUG_ON(!kctx->pending_regions[cookie]);
202
203                 kbase_reg_pending_dtor(kctx->pending_regions[cookie]);
204
205                 kctx->pending_regions[cookie] = NULL;
206                 pending_regions_to_clean &= ~(1UL << cookie);
207         }
208
209         kbase_region_tracker_term(kctx);
210         kbase_gpu_vm_unlock(kctx);
211
212         /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
213         kbasep_js_kctx_term(kctx);
214
215         kbase_jd_exit(kctx);
216
217         kbase_pm_context_idle(kbdev);
218
219         kbase_mmu_term(kctx);
220
221         pages = atomic_read(&kctx->used_pages);
222         if (pages != 0)
223                 dev_warn(kbdev->dev, "%s: %d pages in use!\n", __func__, pages);
224
225         kbase_mem_allocator_term(&kctx->osalloc);
226         WARN_ON(atomic_read(&kctx->nonmapped_pages) != 0);
227
228         vfree(kctx);
229 }
230 KBASE_EXPORT_SYMBOL(kbase_destroy_context);
231
232 /**
233  * kbase_context_set_create_flags - Set creation flags on a context
234  * @kctx: Kbase context
235  * @flags: Flags to set
236  *
237  * Return: 0 on success
238  */
239 int kbase_context_set_create_flags(struct kbase_context *kctx, u32 flags)
240 {
241         int err = 0;
242         struct kbasep_js_kctx_info *js_kctx_info;
243         unsigned long irq_flags;
244
245         KBASE_DEBUG_ASSERT(NULL != kctx);
246
247         js_kctx_info = &kctx->jctx.sched_info;
248
249         /* Validate flags */
250         if (flags != (flags & BASE_CONTEXT_CREATE_KERNEL_FLAGS)) {
251                 err = -EINVAL;
252                 goto out;
253         }
254
255         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
256         spin_lock_irqsave(&kctx->kbdev->js_data.runpool_irq.lock, irq_flags);
257
258         /* Translate the flags */
259         if ((flags & BASE_CONTEXT_SYSTEM_MONITOR_SUBMIT_DISABLED) == 0)
260                 js_kctx_info->ctx.flags &= ~((u32) KBASE_CTX_FLAG_SUBMIT_DISABLED);
261
262         if ((flags & BASE_CONTEXT_HINT_ONLY_COMPUTE) != 0)
263                 js_kctx_info->ctx.flags |= (u32) KBASE_CTX_FLAG_HINT_ONLY_COMPUTE;
264
265         /* Latch the initial attributes into the Job Scheduler */
266         kbasep_js_ctx_attr_set_initial_attrs(kctx->kbdev, kctx);
267
268         spin_unlock_irqrestore(&kctx->kbdev->js_data.runpool_irq.lock,
269                         irq_flags);
270         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
271  out:
272         return err;
273 }
274 KBASE_EXPORT_SYMBOL(kbase_context_set_create_flags);