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.
23 #include <mali_malisw.h>
25 #include <mali_kbase_debug.h>
29 #include <linux/atomic.h>
30 #include <linux/highmem.h>
31 #include <linux/hrtimer.h>
32 #include <linux/ktime.h>
33 #include <linux/list.h>
34 #include <linux/mm_types.h>
35 #include <linux/mutex.h>
36 #include <linux/rwsem.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
40 #include <linux/vmalloc.h>
41 #include <linux/wait.h>
42 #include <linux/workqueue.h>
44 #include "mali_base_kernel.h"
45 #include <mali_kbase_uku.h>
46 #include <mali_kbase_linux.h>
48 #include "mali_kbase_pm.h"
49 #include "mali_kbase_mem_lowlevel.h"
50 #include "mali_kbase_defs.h"
51 #include "mali_kbase_trace_timeline.h"
52 #include "mali_kbase_js.h"
53 #include "mali_kbase_mem.h"
54 #include "mali_kbase_utility.h"
55 #include "mali_kbase_gpu_memory_debugfs.h"
56 #include "mali_kbase_mem_profile_debugfs.h"
57 #include "mali_kbase_debug_job_fault.h"
58 #include "mali_kbase_jd_debugfs.h"
59 #include "mali_kbase_gpuprops.h"
60 #include "mali_kbase_jm.h"
61 #include "mali_kbase_vinstr.h"
62 #include "mali_kbase_ipa.h"
63 #ifdef CONFIG_GPU_TRACEPOINTS
64 #include <trace/events/gpu.h>
67 * @page page_base_kernel_main Kernel-side Base (KBase) APIs
69 * The Kernel-side Base (KBase) APIs are divided up as follows:
70 * - @subpage page_kbase_js_policy
74 * @defgroup base_kbase_api Kernel-side Base (KBase) APIs
77 struct kbase_device *kbase_device_alloc(void);
79 * note: configuration attributes member of kbdev needs to have
80 * been setup before calling kbase_device_init
84 * API to acquire device list semaphore and return pointer
85 * to the device list head
87 const struct list_head *kbase_dev_list_get(void);
88 /* API to release the device list semaphore */
89 void kbase_dev_list_put(const struct list_head *dev_list);
91 int kbase_device_init(struct kbase_device * const kbdev);
92 void kbase_device_term(struct kbase_device *kbdev);
93 void kbase_device_free(struct kbase_device *kbdev);
94 int kbase_device_has_feature(struct kbase_device *kbdev, u32 feature);
96 /* Needed for gator integration and for reporting vsync information */
97 struct kbase_device *kbase_find_device(int minor);
98 void kbase_release_device(struct kbase_device *kbdev);
100 void kbase_set_profiling_control(struct kbase_device *kbdev, u32 control, u32 value);
102 u32 kbase_get_profiling_control(struct kbase_device *kbdev, u32 control);
104 struct kbase_context *
105 kbase_create_context(struct kbase_device *kbdev, bool is_compat);
106 void kbase_destroy_context(struct kbase_context *kctx);
107 int kbase_context_set_create_flags(struct kbase_context *kctx, u32 flags);
109 int kbase_jd_init(struct kbase_context *kctx);
110 void kbase_jd_exit(struct kbase_context *kctx);
111 #ifdef BASE_LEGACY_UK6_SUPPORT
112 int kbase_jd_submit(struct kbase_context *kctx,
113 const struct kbase_uk_job_submit *submit_data,
116 int kbase_jd_submit(struct kbase_context *kctx,
117 const struct kbase_uk_job_submit *submit_data);
121 * kbase_jd_done_worker - Handle a job completion
122 * @data: a &struct work_struct
124 * This function requeues the job from the runpool (if it was soft-stopped or
125 * removed from NEXT registers).
127 * Removes it from the system if it finished/failed/was cancelled.
129 * Resolves dependencies to add dependent jobs to the context, potentially
130 * starting them if necessary (which may add more references to the context)
132 * Releases the reference to the context from the no-longer-running job.
134 * Handles retrying submission outside of IRQ context if it failed from within
137 void kbase_jd_done_worker(struct work_struct *data);
139 void kbase_jd_done(struct kbase_jd_atom *katom, int slot_nr, ktime_t *end_timestamp,
140 kbasep_js_atom_done_code done_code);
141 void kbase_jd_cancel(struct kbase_device *kbdev, struct kbase_jd_atom *katom);
142 void kbase_jd_evict(struct kbase_device *kbdev, struct kbase_jd_atom *katom);
143 void kbase_jd_zap_context(struct kbase_context *kctx);
144 bool jd_done_nolock(struct kbase_jd_atom *katom,
145 struct list_head *completed_jobs_ctx);
146 void kbase_jd_free_external_resources(struct kbase_jd_atom *katom);
147 bool jd_submit_atom(struct kbase_context *kctx,
148 const struct base_jd_atom_v2 *user_atom,
149 struct kbase_jd_atom *katom);
151 void kbase_job_done(struct kbase_device *kbdev, u32 done);
153 void kbase_gpu_cacheclean(struct kbase_device *kbdev,
154 struct kbase_jd_atom *katom);
156 * kbase_job_slot_ctx_priority_check_locked(): - Check for lower priority atoms
158 * @kctx: Pointer to context to check.
159 * @katom: Pointer to priority atom.
161 * Atoms from @kctx on the same job slot as @katom, which have lower priority
162 * than @katom will be soft stopped and put back in the queue, so that atoms
163 * with higher priority can run.
165 * The js_data.runpool_irq.lock must be held when calling this function.
167 void kbase_job_slot_ctx_priority_check_locked(struct kbase_context *kctx,
168 struct kbase_jd_atom *katom);
170 void kbase_job_slot_softstop(struct kbase_device *kbdev, int js,
171 struct kbase_jd_atom *target_katom);
172 void kbase_job_slot_softstop_swflags(struct kbase_device *kbdev, int js,
173 struct kbase_jd_atom *target_katom, u32 sw_flags);
174 void kbase_job_slot_hardstop(struct kbase_context *kctx, int js,
175 struct kbase_jd_atom *target_katom);
176 void kbase_job_check_enter_disjoint(struct kbase_device *kbdev, u32 action,
177 u16 core_reqs, struct kbase_jd_atom *target_katom);
178 void kbase_job_check_leave_disjoint(struct kbase_device *kbdev,
179 struct kbase_jd_atom *target_katom);
181 void kbase_event_post(struct kbase_context *ctx, struct kbase_jd_atom *event);
182 int kbase_event_dequeue(struct kbase_context *ctx, struct base_jd_event_v2 *uevent);
183 int kbase_event_pending(struct kbase_context *ctx);
184 int kbase_event_init(struct kbase_context *kctx);
185 void kbase_event_close(struct kbase_context *kctx);
186 void kbase_event_cleanup(struct kbase_context *kctx);
187 void kbase_event_wakeup(struct kbase_context *kctx);
189 int kbase_process_soft_job(struct kbase_jd_atom *katom);
190 int kbase_prepare_soft_job(struct kbase_jd_atom *katom);
191 void kbase_finish_soft_job(struct kbase_jd_atom *katom);
192 void kbase_cancel_soft_job(struct kbase_jd_atom *katom);
193 void kbase_resume_suspended_soft_jobs(struct kbase_device *kbdev);
195 bool kbase_replay_process(struct kbase_jd_atom *katom);
197 /* api used internally for register access. Contains validation and tracing */
198 void kbase_device_trace_register_access(struct kbase_context *kctx, enum kbase_reg_access_type type, u16 reg_offset, u32 reg_value);
199 int kbase_device_trace_buffer_install(
200 struct kbase_context *kctx, u32 *tb, size_t size);
201 void kbase_device_trace_buffer_uninstall(struct kbase_context *kctx);
203 /* api to be ported per OS, only need to do the raw register access */
204 void kbase_os_reg_write(struct kbase_device *kbdev, u16 offset, u32 value);
205 u32 kbase_os_reg_read(struct kbase_device *kbdev, u16 offset);
208 void kbasep_as_do_poke(struct work_struct *work);
210 /** Returns the name associated with a Mali exception code
212 * This function is called from the interrupt handler when a GPU fault occurs.
213 * It reports the details of the fault using KBASE_DEBUG_PRINT_WARN.
215 * @param[in] kbdev The kbase device that the GPU fault occurred from.
216 * @param[in] exception_code exception code
217 * @return name associated with the exception code
219 const char *kbase_exception_name(struct kbase_device *kbdev,
223 * Check whether a system suspend is in progress, or has already been suspended
225 * The caller should ensure that either kbdev->pm.active_count_lock is held, or
226 * a dmb was executed recently (to ensure the value is most
227 * up-to-date). However, without a lock the value could change afterwards.
229 * @return false if a suspend is not in progress
230 * @return !=false otherwise
232 static inline bool kbase_pm_is_suspending(struct kbase_device *kbdev)
234 return kbdev->pm.suspending;
238 * Return the atom's ID, as was originally supplied by userspace in
239 * base_jd_atom_v2::atom_number
241 static inline int kbase_jd_atom_id(struct kbase_context *kctx, struct kbase_jd_atom *katom)
245 KBASE_DEBUG_ASSERT(kctx);
246 KBASE_DEBUG_ASSERT(katom);
247 KBASE_DEBUG_ASSERT(katom->kctx == kctx);
249 result = katom - &kctx->jctx.atoms[0];
250 KBASE_DEBUG_ASSERT(result >= 0 && result <= BASE_JD_ATOM_COUNT);
255 * kbase_jd_atom_from_id - Return the atom structure for the given atom ID
256 * @kctx: Context pointer
257 * @id: ID of atom to retrieve
259 * Return: Pointer to struct kbase_jd_atom associated with the supplied ID
261 static inline struct kbase_jd_atom *kbase_jd_atom_from_id(
262 struct kbase_context *kctx, int id)
264 return &kctx->jctx.atoms[id];
268 * Initialize the disjoint state
270 * The disjoint event count and state are both set to zero.
272 * Disjoint functions usage:
274 * The disjoint event count should be incremented whenever a disjoint event occurs.
276 * There are several cases which are regarded as disjoint behavior. Rather than just increment
277 * the counter during disjoint events we also increment the counter when jobs may be affected
278 * by what the GPU is currently doing. To facilitate this we have the concept of disjoint state.
280 * Disjoint state is entered during GPU reset and for the entire time that an atom is replaying
281 * (as part of the replay workaround). Increasing the disjoint state also increases the count of
284 * The disjoint state is then used to increase the count of disjoint events during job submission
285 * and job completion. Any atom submitted or completed while the disjoint state is greater than
286 * zero is regarded as a disjoint event.
288 * The disjoint event counter is also incremented immediately whenever a job is soft stopped
289 * and during context creation.
291 * @param kbdev The kbase device
293 void kbase_disjoint_init(struct kbase_device *kbdev);
296 * Increase the count of disjoint events
297 * called when a disjoint event has happened
299 * @param kbdev The kbase device
301 void kbase_disjoint_event(struct kbase_device *kbdev);
304 * Increase the count of disjoint events only if the GPU is in a disjoint state
306 * This should be called when something happens which could be disjoint if the GPU
307 * is in a disjoint state. The state refcount keeps track of this.
309 * @param kbdev The kbase device
311 void kbase_disjoint_event_potential(struct kbase_device *kbdev);
314 * Returns the count of disjoint events
316 * @param kbdev The kbase device
317 * @return the count of disjoint events
319 u32 kbase_disjoint_event_get(struct kbase_device *kbdev);
322 * Increment the refcount state indicating that the GPU is in a disjoint state.
324 * Also Increment the disjoint event count (calls @ref kbase_disjoint_event)
325 * eventually after the disjoint state has completed @ref kbase_disjoint_state_down
328 * @param kbdev The kbase device
330 void kbase_disjoint_state_up(struct kbase_device *kbdev);
333 * Decrement the refcount state
335 * Also Increment the disjoint event count (calls @ref kbase_disjoint_event)
337 * Called after @ref kbase_disjoint_state_up once the disjoint state is over
339 * @param kbdev The kbase device
341 void kbase_disjoint_state_down(struct kbase_device *kbdev);
344 * If a job is soft stopped and the number of contexts is >= this value
345 * it is reported as a disjoint event
347 #define KBASE_DISJOINT_STATE_INTERLEAVED_CONTEXT_COUNT_THRESHOLD 2
349 #if !defined(UINT64_MAX)
350 #define UINT64_MAX ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
353 #if KBASE_TRACE_ENABLE
354 void kbasep_trace_debugfs_init(struct kbase_device *kbdev);
356 #ifndef CONFIG_MALI_SYSTEM_TRACE
357 /** Add trace values about a job-slot
359 * @note Any functions called through this macro will still be evaluated in
360 * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
361 * functions called to get the parameters supplied to this macro must:
362 * - be static or static inline
363 * - must just return 0 and have no other statements present in the body.
365 #define KBASE_TRACE_ADD_SLOT(kbdev, code, ctx, katom, gpu_addr, jobslot) \
366 kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
367 KBASE_TRACE_FLAG_JOBSLOT, 0, jobslot, 0)
369 /** Add trace values about a job-slot, with info
371 * @note Any functions called through this macro will still be evaluated in
372 * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
373 * functions called to get the parameters supplied to this macro must:
374 * - be static or static inline
375 * - must just return 0 and have no other statements present in the body.
377 #define KBASE_TRACE_ADD_SLOT_INFO(kbdev, code, ctx, katom, gpu_addr, jobslot, info_val) \
378 kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
379 KBASE_TRACE_FLAG_JOBSLOT, 0, jobslot, info_val)
381 /** Add trace values about a ctx refcount
383 * @note Any functions called through this macro will still be evaluated in
384 * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
385 * functions called to get the parameters supplied to this macro must:
386 * - be static or static inline
387 * - must just return 0 and have no other statements present in the body.
389 #define KBASE_TRACE_ADD_REFCOUNT(kbdev, code, ctx, katom, gpu_addr, refcount) \
390 kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
391 KBASE_TRACE_FLAG_REFCOUNT, refcount, 0, 0)
392 /** Add trace values about a ctx refcount, and info
394 * @note Any functions called through this macro will still be evaluated in
395 * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
396 * functions called to get the parameters supplied to this macro must:
397 * - be static or static inline
398 * - must just return 0 and have no other statements present in the body.
400 #define KBASE_TRACE_ADD_REFCOUNT_INFO(kbdev, code, ctx, katom, gpu_addr, refcount, info_val) \
401 kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
402 KBASE_TRACE_FLAG_REFCOUNT, refcount, 0, info_val)
404 /** Add trace values (no slot or refcount)
406 * @note Any functions called through this macro will still be evaluated in
407 * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
408 * functions called to get the parameters supplied to this macro must:
409 * - be static or static inline
410 * - must just return 0 and have no other statements present in the body.
412 #define KBASE_TRACE_ADD(kbdev, code, ctx, katom, gpu_addr, info_val) \
413 kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
416 /** Clear the trace */
417 #define KBASE_TRACE_CLEAR(kbdev) \
418 kbasep_trace_clear(kbdev)
420 /** Dump the slot trace */
421 #define KBASE_TRACE_DUMP(kbdev) \
422 kbasep_trace_dump(kbdev)
424 /** PRIVATE - do not use directly. Use KBASE_TRACE_ADD() instead */
425 void kbasep_trace_add(struct kbase_device *kbdev, enum kbase_trace_code code, void *ctx, struct kbase_jd_atom *katom, u64 gpu_addr, u8 flags, int refcount, int jobslot, unsigned long info_val);
426 /** PRIVATE - do not use directly. Use KBASE_TRACE_CLEAR() instead */
427 void kbasep_trace_clear(struct kbase_device *kbdev);
428 #else /* #ifndef CONFIG_MALI_SYSTEM_TRACE */
429 /* Dispatch kbase trace events as system trace events */
430 #include <mali_linux_kbase_trace.h>
431 #define KBASE_TRACE_ADD_SLOT(kbdev, code, ctx, katom, gpu_addr, jobslot)\
432 trace_mali_##code(jobslot, 0)
434 #define KBASE_TRACE_ADD_SLOT_INFO(kbdev, code, ctx, katom, gpu_addr, jobslot, info_val)\
435 trace_mali_##code(jobslot, info_val)
437 #define KBASE_TRACE_ADD_REFCOUNT(kbdev, code, ctx, katom, gpu_addr, refcount)\
438 trace_mali_##code(refcount, 0)
440 #define KBASE_TRACE_ADD_REFCOUNT_INFO(kbdev, code, ctx, katom, gpu_addr, refcount, info_val)\
441 trace_mali_##code(refcount, info_val)
443 #define KBASE_TRACE_ADD(kbdev, code, ctx, katom, gpu_addr, info_val)\
444 trace_mali_##code(gpu_addr, info_val)
446 #define KBASE_TRACE_CLEAR(kbdev)\
451 #define KBASE_TRACE_DUMP(kbdev)\
457 #endif /* #ifndef CONFIG_MALI_SYSTEM_TRACE */
459 #define KBASE_TRACE_ADD_SLOT(kbdev, code, ctx, katom, gpu_addr, jobslot)\
465 CSTD_UNUSED(gpu_addr);\
466 CSTD_UNUSED(jobslot);\
469 #define KBASE_TRACE_ADD_SLOT_INFO(kbdev, code, ctx, katom, gpu_addr, jobslot, info_val)\
475 CSTD_UNUSED(gpu_addr);\
476 CSTD_UNUSED(jobslot);\
477 CSTD_UNUSED(info_val);\
481 #define KBASE_TRACE_ADD_REFCOUNT(kbdev, code, ctx, katom, gpu_addr, refcount)\
487 CSTD_UNUSED(gpu_addr);\
488 CSTD_UNUSED(refcount);\
492 #define KBASE_TRACE_ADD_REFCOUNT_INFO(kbdev, code, ctx, katom, gpu_addr, refcount, info_val)\
498 CSTD_UNUSED(gpu_addr);\
499 CSTD_UNUSED(info_val);\
503 #define KBASE_TRACE_ADD(kbdev, code, subcode, ctx, katom, val)\
507 CSTD_UNUSED(subcode);\
514 #define KBASE_TRACE_CLEAR(kbdev)\
519 #define KBASE_TRACE_DUMP(kbdev)\
524 #endif /* KBASE_TRACE_ENABLE */
525 /** PRIVATE - do not use directly. Use KBASE_TRACE_DUMP() instead */
526 void kbasep_trace_dump(struct kbase_device *kbdev);
528 #ifdef CONFIG_MALI_DEBUG
530 * kbase_set_driver_inactive - Force driver to go inactive
531 * @kbdev: Device pointer
532 * @inactive: true if driver should go inactive, false otherwise
534 * Forcing the driver inactive will cause all future IOCTLs to wait until the
535 * driver is made active again. This is intended solely for the use of tests
536 * which require that no jobs are running while the test executes.
538 void kbase_set_driver_inactive(struct kbase_device *kbdev, bool inactive);
539 #endif /* CONFIG_MALI_DEBUG */