3 * (C) COPYRIGHT 2010-2015 ARM Limited. All rights reserved.
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
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.
21 * Base kernel context APIs
24 #include <mali_kbase.h>
25 #include <mali_midg_regmap.h>
26 #include <mali_kbase_instr.h>
28 #define MEMPOOL_PAGES 16384
32 * kbase_create_context() - Create a kernel base context.
33 * @kbdev: Kbase device
34 * @is_compat: Force creation of a 32-bit context
36 * Allocate and init a kernel base context.
38 * Return: new kbase context
40 struct kbase_context *
41 kbase_create_context(struct kbase_device *kbdev, bool is_compat)
43 struct kbase_context *kctx;
46 KBASE_DEBUG_ASSERT(kbdev != NULL);
48 /* zero-inited as lot of code assume it's zero'ed out on create */
49 kctx = vzalloc(sizeof(*kctx));
54 /* creating a context is considered a disjoint event */
55 kbase_disjoint_event(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);
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;
71 if (kbase_mem_allocator_init(&kctx->osalloc, MEMPOOL_PAGES, kctx->kbdev) != 0)
74 kctx->pgd_allocator = &kctx->osalloc;
75 atomic_set(&kctx->used_pages, 0);
77 if (kbase_jd_init(kctx))
80 mali_err = kbasep_js_kctx_init(kctx);
82 goto free_jd; /* safe to call kbasep_js_kctx_term in this case */
84 mali_err = kbase_event_init(kctx);
88 mutex_init(&kctx->reg_lock);
90 INIT_LIST_HEAD(&kctx->waiting_soft_jobs);
92 INIT_LIST_HEAD(&kctx->waiting_kds_resource);
95 mali_err = kbase_mmu_init(kctx);
99 kctx->pgd = kbase_mmu_alloc_pgd(kctx);
103 if (kbase_mem_allocator_alloc(&kctx->osalloc, 1, &kctx->aliasing_sink_page) != 0)
106 kctx->tgid = current->tgid;
107 kctx->pid = current->pid;
108 init_waitqueue_head(&kctx->event_queue);
110 kctx->cookies = KBASE_COOKIE_MASK;
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);
118 #ifdef CONFIG_MALI_TRACE_TIMELINE
119 atomic_set(&kctx->timeline.jd_atoms_in_flight, 0);
122 kctx->id = atomic_add_return(1, &(kbdev->ctx_num)) - 1;
124 mutex_init(&kctx->vinstr_cli_lock);
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);
136 kbase_mmu_term(kctx);
138 kbase_event_cleanup(kctx);
140 /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
141 kbasep_js_kctx_term(kctx);
144 kbase_mem_allocator_term(&kctx->osalloc);
150 KBASE_EXPORT_SYMBOL(kbase_create_context);
152 static void kbase_reg_pending_dtor(struct kbase_va_region *reg)
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);
161 * kbase_destroy_context - Destroy a kernel base context.
162 * @kctx: Context to destroy
164 * Calls kbase_destroy_os_context() to free OS specific structures.
165 * Will release all outstanding regions.
167 void kbase_destroy_context(struct kbase_context *kctx)
169 struct kbase_device *kbdev;
171 unsigned long pending_regions_to_clean;
173 KBASE_DEBUG_ASSERT(NULL != kctx);
176 KBASE_DEBUG_ASSERT(NULL != kbdev);
178 KBASE_TRACE_ADD(kbdev, CORE_CTX_DESTROY, kctx, NULL, 0u, 0u);
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
183 kbase_pm_context_active(kbdev);
185 kbase_jd_zap_context(kctx);
186 kbase_event_cleanup(kctx);
188 kbase_gpu_vm_lock(kctx);
190 /* MMU is disabled as part of scheduling out the context */
191 kbase_mmu_free_pgd(kctx);
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);
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);
201 BUG_ON(!kctx->pending_regions[cookie]);
203 kbase_reg_pending_dtor(kctx->pending_regions[cookie]);
205 kctx->pending_regions[cookie] = NULL;
206 pending_regions_to_clean &= ~(1UL << cookie);
209 kbase_region_tracker_term(kctx);
210 kbase_gpu_vm_unlock(kctx);
212 /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
213 kbasep_js_kctx_term(kctx);
217 kbase_pm_context_idle(kbdev);
219 kbase_mmu_term(kctx);
221 pages = atomic_read(&kctx->used_pages);
223 dev_warn(kbdev->dev, "%s: %d pages in use!\n", __func__, pages);
225 kbase_mem_allocator_term(&kctx->osalloc);
226 WARN_ON(atomic_read(&kctx->nonmapped_pages) != 0);
230 KBASE_EXPORT_SYMBOL(kbase_destroy_context);
233 * kbase_context_set_create_flags - Set creation flags on a context
234 * @kctx: Kbase context
235 * @flags: Flags to set
237 * Return: 0 on success
239 int kbase_context_set_create_flags(struct kbase_context *kctx, u32 flags)
242 struct kbasep_js_kctx_info *js_kctx_info;
243 unsigned long irq_flags;
245 KBASE_DEBUG_ASSERT(NULL != kctx);
247 js_kctx_info = &kctx->jctx.sched_info;
250 if (flags != (flags & BASE_CONTEXT_CREATE_KERNEL_FLAGS)) {
255 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
256 spin_lock_irqsave(&kctx->kbdev->js_data.runpool_irq.lock, irq_flags);
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);
262 if ((flags & BASE_CONTEXT_HINT_ONLY_COMPUTE) != 0)
263 js_kctx_info->ctx.flags |= (u32) KBASE_CTX_FLAG_HINT_ONLY_COMPUTE;
265 /* Latch the initial attributes into the Job Scheduler */
266 kbasep_js_ctx_attr_set_initial_attrs(kctx->kbdev, kctx);
268 spin_unlock_irqrestore(&kctx->kbdev->js_data.runpool_irq.lock,
270 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
274 KBASE_EXPORT_SYMBOL(kbase_context_set_create_flags);