3 * (C) COPYRIGHT 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 * @file mali_kbase_context.c
22 * Base kernel context APIs
25 #include <kbase/src/common/mali_kbase.h>
26 #include <kbase/src/common/mali_midg_regmap.h>
28 #define MEMPOOL_PAGES 16384
31 * @brief Create a kernel base context.
33 * Allocate and init a kernel base context. Calls
34 * kbase_create_os_context() to setup OS specific structures.
36 kbase_context *kbase_create_context(kbase_device *kbdev)
41 KBASE_DEBUG_ASSERT(kbdev != NULL);
43 /* zero-inited as lot of code assume it's zero'ed out on create */
44 kctx = vzalloc(sizeof(*kctx));
50 kctx->as_nr = KBASEP_AS_NR_INVALID;
51 #ifdef CONFIG_MALI_TRACE_TIMELINE
52 kctx->timeline.owner_tgid = task_tgid_nr(current);
54 atomic_set(&kctx->setup_complete, 0);
55 atomic_set(&kctx->setup_in_progress, 0);
56 kctx->keep_gpu_powered = MALI_FALSE;
57 spin_lock_init(&kctx->mm_update_lock);
58 kctx->process_mm = NULL;
59 atomic_set(&kctx->nonmapped_pages, 0);
61 if (MALI_ERROR_NONE != kbase_mem_allocator_init(&kctx->osalloc, MEMPOOL_PAGES))
64 kctx->pgd_allocator = &kctx->osalloc;
65 if (kbase_mem_usage_init(&kctx->usage, kctx->kbdev->memdev.per_process_memory_limit >> PAGE_SHIFT))
69 if (kbase_jd_init(kctx))
72 mali_err = kbasep_js_kctx_init(kctx);
73 if (MALI_ERROR_NONE != mali_err)
74 goto free_jd; /* safe to call kbasep_js_kctx_term in this case */
76 mali_err = kbase_event_init(kctx);
77 if (MALI_ERROR_NONE != mali_err)
80 mutex_init(&kctx->reg_lock);
82 INIT_LIST_HEAD(&kctx->waiting_soft_jobs);
84 INIT_LIST_HEAD(&kctx->waiting_kds_resource);
87 mali_err = kbase_mmu_init(kctx);
88 if (MALI_ERROR_NONE != mali_err)
91 kctx->pgd = kbase_mmu_alloc_pgd(kctx);
95 if (kbase_create_os_context(&kctx->osctx))
98 /* Make sure page 0 is not used... */
99 if (kbase_region_tracker_init(kctx))
101 #ifdef CONFIG_GPU_TRACEPOINTS
102 atomic_set(&kctx->jctx.work_id, 0);
104 #ifdef CONFIG_MALI_TRACE_TIMELINE
105 atomic_set(&kctx->timeline.jd_atoms_in_flight, 0);
111 kbase_destroy_os_context(&kctx->osctx);
113 kbase_mmu_free_pgd(kctx);
115 kbase_mmu_term(kctx);
117 kbase_event_cleanup(kctx);
119 /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
120 kbasep_js_kctx_term(kctx);
123 kbase_mem_usage_term(&kctx->usage);
125 kbase_mem_allocator_term(&kctx->osalloc);
132 KBASE_EXPORT_SYMBOL(kbase_create_context)
135 * @brief Destroy a kernel base context.
137 * Destroy a kernel base context. Calls kbase_destroy_os_context() to
138 * free OS specific structures. Will release all outstanding regions.
140 void kbase_destroy_context(kbase_context *kctx)
144 KBASE_DEBUG_ASSERT(NULL != kctx);
147 KBASE_DEBUG_ASSERT(NULL != kbdev);
149 KBASE_TRACE_ADD(kbdev, CORE_CTX_DESTROY, kctx, NULL, 0u, 0u);
151 /* Ensure the core is powered up for the destroy process */
152 /* A suspend won't happen here, because we're in a syscall from a userspace
154 kbase_pm_context_active(kbdev);
156 if (kbdev->hwcnt.kctx == kctx) {
157 /* disable the use of the hw counters if the app didn't use the API correctly or crashed */
158 KBASE_TRACE_ADD(kbdev, CORE_CTX_HWINSTR_TERM, kctx, NULL, 0u, 0u);
159 KBASE_DEBUG_PRINT_WARN(KBASE_CTX, "The privileged process asking for instrumentation forgot to disable it " "before exiting. Will end instrumentation for them");
160 kbase_instr_hwcnt_disable(kctx);
163 kbase_jd_zap_context(kctx);
164 kbase_event_cleanup(kctx);
166 kbase_gpu_vm_lock(kctx);
168 /* MMU is disabled as part of scheduling out the context */
169 kbase_mmu_free_pgd(kctx);
170 kbase_region_tracker_term(kctx);
171 kbase_destroy_os_context(&kctx->osctx);
172 kbase_gpu_vm_unlock(kctx);
174 /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
175 kbasep_js_kctx_term(kctx);
179 kbase_pm_context_idle(kbdev);
181 kbase_mmu_term(kctx);
183 kbase_mem_usage_term(&kctx->usage);
185 if (kctx->keep_gpu_powered) {
186 atomic_dec(&kbdev->keep_gpu_powered_count);
187 kbase_pm_context_idle(kbdev);
190 kbase_mem_allocator_term(&kctx->osalloc);
191 WARN_ON(atomic_read(&kctx->nonmapped_pages) != 0);
194 KBASE_EXPORT_SYMBOL(kbase_destroy_context)
197 * Set creation flags on a context
199 mali_error kbase_context_set_create_flags(kbase_context *kctx, u32 flags)
201 mali_error err = MALI_ERROR_NONE;
202 kbasep_js_kctx_info *js_kctx_info;
203 KBASE_DEBUG_ASSERT(NULL != kctx);
205 js_kctx_info = &kctx->jctx.sched_info;
208 if (flags != (flags & BASE_CONTEXT_CREATE_KERNEL_FLAGS)) {
209 err = MALI_ERROR_FUNCTION_FAILED;
213 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
215 /* Translate the flags */
216 if ((flags & BASE_CONTEXT_SYSTEM_MONITOR_SUBMIT_DISABLED) == 0)
217 js_kctx_info->ctx.flags &= ~((u32) KBASE_CTX_FLAG_SUBMIT_DISABLED);
219 if ((flags & BASE_CONTEXT_HINT_ONLY_COMPUTE) != 0)
220 js_kctx_info->ctx.flags |= (u32) KBASE_CTX_FLAG_HINT_ONLY_COMPUTE;
222 /* Latch the initial attributes into the Job Scheduler */
223 kbasep_js_ctx_attr_set_initial_attrs(kctx->kbdev, kctx);
225 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
229 KBASE_EXPORT_SYMBOL(kbase_context_set_create_flags)