rk: ion: resolve build err
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / t6xx / kbase / src / common / mali_kbase_context.c
1 /*
2  *
3  * (C) COPYRIGHT 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  * @file mali_kbase_context.c
22  * Base kernel context APIs
23  */
24
25 #include <kbase/src/common/mali_kbase.h>
26 #include <kbase/src/common/mali_midg_regmap.h>
27
28 #define MEMPOOL_PAGES 16384
29
30 /**
31  * @brief Create a kernel base context.
32  *
33  * Allocate and init a kernel base context. Calls
34  * kbase_create_os_context() to setup OS specific structures.
35  */
36 kbase_context *kbase_create_context(kbase_device *kbdev)
37 {
38         kbase_context *kctx;
39         mali_error mali_err;
40
41         KBASE_DEBUG_ASSERT(kbdev != NULL);
42
43         /* zero-inited as lot of code assume it's zero'ed out on create */
44         kctx = vzalloc(sizeof(*kctx));
45
46         if (!kctx)
47                 goto out;
48
49         kctx->kbdev = kbdev;
50         kctx->as_nr = KBASEP_AS_NR_INVALID;
51 #ifdef CONFIG_MALI_TRACE_TIMELINE
52         kctx->timeline.owner_tgid = task_tgid_nr(current);
53 #endif
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);
60
61         if (MALI_ERROR_NONE != kbase_mem_allocator_init(&kctx->osalloc, MEMPOOL_PAGES))
62                 goto free_kctx;
63
64         kctx->pgd_allocator = &kctx->osalloc;
65         if (kbase_mem_usage_init(&kctx->usage, kctx->kbdev->memdev.per_process_memory_limit >> PAGE_SHIFT))
66                 goto free_allocator;
67
68
69         if (kbase_jd_init(kctx))
70                 goto free_memctx;
71
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 */
75
76         mali_err = kbase_event_init(kctx);
77         if (MALI_ERROR_NONE != mali_err)
78                 goto free_jd;
79
80         mutex_init(&kctx->reg_lock);
81
82         INIT_LIST_HEAD(&kctx->waiting_soft_jobs);
83 #ifdef CONFIG_KDS
84         INIT_LIST_HEAD(&kctx->waiting_kds_resource);
85 #endif
86
87         mali_err = kbase_mmu_init(kctx);
88         if (MALI_ERROR_NONE != mali_err)
89                 goto free_event;
90
91         kctx->pgd = kbase_mmu_alloc_pgd(kctx);
92         if (!kctx->pgd)
93                 goto free_mmu;
94
95         if (kbase_create_os_context(&kctx->osctx))
96                 goto free_pgd;
97
98         /* Make sure page 0 is not used... */
99         if (kbase_region_tracker_init(kctx))
100                 goto free_osctx;
101 #ifdef CONFIG_GPU_TRACEPOINTS
102         atomic_set(&kctx->jctx.work_id, 0);
103 #endif
104 #ifdef CONFIG_MALI_TRACE_TIMELINE
105         atomic_set(&kctx->timeline.jd_atoms_in_flight, 0);
106 #endif
107
108         return kctx;
109
110  free_osctx:
111         kbase_destroy_os_context(&kctx->osctx);
112  free_pgd:
113         kbase_mmu_free_pgd(kctx);
114  free_mmu:
115         kbase_mmu_term(kctx);
116  free_event:
117         kbase_event_cleanup(kctx);
118  free_jd:
119         /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
120         kbasep_js_kctx_term(kctx);
121         kbase_jd_exit(kctx);
122  free_memctx:
123         kbase_mem_usage_term(&kctx->usage);
124  free_allocator:
125         kbase_mem_allocator_term(&kctx->osalloc);
126  free_kctx:
127     vfree(kctx);
128  out:
129         return NULL;
130
131 }
132 KBASE_EXPORT_SYMBOL(kbase_create_context)
133
134 /**
135  * @brief Destroy a kernel base context.
136  *
137  * Destroy a kernel base context. Calls kbase_destroy_os_context() to
138  * free OS specific structures. Will release all outstanding regions.
139  */
140 void kbase_destroy_context(kbase_context *kctx)
141 {
142         kbase_device *kbdev;
143
144         KBASE_DEBUG_ASSERT(NULL != kctx);
145
146         kbdev = kctx->kbdev;
147         KBASE_DEBUG_ASSERT(NULL != kbdev);
148
149         KBASE_TRACE_ADD(kbdev, CORE_CTX_DESTROY, kctx, NULL, 0u, 0u);
150
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
153          * thread. */
154         kbase_pm_context_active(kbdev);
155
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);
161         }
162
163         kbase_jd_zap_context(kctx);
164         kbase_event_cleanup(kctx);
165
166         kbase_gpu_vm_lock(kctx);
167
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);
173
174         /* Safe to call this one even when didn't initialize (assuming kctx was sufficiently zeroed) */
175         kbasep_js_kctx_term(kctx);
176
177         kbase_jd_exit(kctx);
178
179         kbase_pm_context_idle(kbdev);
180
181         kbase_mmu_term(kctx);
182
183         kbase_mem_usage_term(&kctx->usage);
184
185         if (kctx->keep_gpu_powered) {
186                 atomic_dec(&kbdev->keep_gpu_powered_count);
187                 kbase_pm_context_idle(kbdev);
188         }
189
190         kbase_mem_allocator_term(&kctx->osalloc);
191         WARN_ON(atomic_read(&kctx->nonmapped_pages) != 0);
192         vfree(kctx);
193 }
194 KBASE_EXPORT_SYMBOL(kbase_destroy_context)
195
196 /**
197  * Set creation flags on a context
198  */
199 mali_error kbase_context_set_create_flags(kbase_context *kctx, u32 flags)
200 {
201         mali_error err = MALI_ERROR_NONE;
202         kbasep_js_kctx_info *js_kctx_info;
203         KBASE_DEBUG_ASSERT(NULL != kctx);
204
205         js_kctx_info = &kctx->jctx.sched_info;
206
207         /* Validate flags */
208         if (flags != (flags & BASE_CONTEXT_CREATE_KERNEL_FLAGS)) {
209                 err = MALI_ERROR_FUNCTION_FAILED;
210                 goto out;
211         }
212
213         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
214
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);
218
219         if ((flags & BASE_CONTEXT_HINT_ONLY_COMPUTE) != 0)
220                 js_kctx_info->ctx.flags |= (u32) KBASE_CTX_FLAG_HINT_ONLY_COMPUTE;
221
222         /* Latch the initial attributes into the Job Scheduler */
223         kbasep_js_ctx_attr_set_initial_attrs(kctx->kbdev, kctx);
224
225         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
226  out:
227         return err;
228 }
229 KBASE_EXPORT_SYMBOL(kbase_context_set_create_flags)