b6d28fea9987331d4070f3bd6fa73ae468bd1bce
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase.h
1 /*
2  *
3  * (C) COPYRIGHT 2010-2016 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 #ifndef _KBASE_H_
21 #define _KBASE_H_
22
23 #include <mali_malisw.h>
24
25 #include <mali_kbase_debug.h>
26
27 #include <asm/page.h>
28
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>
43
44 #include "mali_base_kernel.h"
45 #include <mali_kbase_uku.h>
46 #include <mali_kbase_linux.h>
47
48 #include "mali_kbase_strings.h"
49 #include "mali_kbase_pm.h"
50 #include "mali_kbase_mem_lowlevel.h"
51 #include "mali_kbase_defs.h"
52 #include "mali_kbase_trace_timeline.h"
53 #include "mali_kbase_js.h"
54 #include "mali_kbase_mem.h"
55 #include "mali_kbase_utility.h"
56 #include "mali_kbase_gpu_memory_debugfs.h"
57 #include "mali_kbase_mem_profile_debugfs.h"
58 #include "mali_kbase_debug_job_fault.h"
59 #include "mali_kbase_jd_debugfs.h"
60 #include "mali_kbase_gpuprops.h"
61 #include "mali_kbase_jm.h"
62 #include "mali_kbase_vinstr.h"
63 #include "mali_kbase_ipa.h"
64 #ifdef CONFIG_GPU_TRACEPOINTS
65 #include <trace/events/gpu.h>
66 #endif
67 /**
68  * @page page_base_kernel_main Kernel-side Base (KBase) APIs
69  *
70  * The Kernel-side Base (KBase) APIs are divided up as follows:
71  * - @subpage page_kbase_js_policy
72  */
73
74 /**
75  * @defgroup base_kbase_api Kernel-side Base (KBase) APIs
76  */
77
78 struct kbase_device *kbase_device_alloc(void);
79 /*
80 * note: configuration attributes member of kbdev needs to have
81 * been setup before calling kbase_device_init
82 */
83
84 /*
85 * API to acquire device list semaphore and return pointer
86 * to the device list head
87 */
88 const struct list_head *kbase_dev_list_get(void);
89 /* API to release the device list semaphore */
90 void kbase_dev_list_put(const struct list_head *dev_list);
91
92 int kbase_device_init(struct kbase_device * const kbdev);
93 void kbase_device_term(struct kbase_device *kbdev);
94 void kbase_device_free(struct kbase_device *kbdev);
95 int kbase_device_has_feature(struct kbase_device *kbdev, u32 feature);
96
97 /* Needed for gator integration and for reporting vsync information */
98 struct kbase_device *kbase_find_device(int minor);
99 void kbase_release_device(struct kbase_device *kbdev);
100
101 void kbase_set_profiling_control(struct kbase_device *kbdev, u32 control, u32 value);
102
103 u32 kbase_get_profiling_control(struct kbase_device *kbdev, u32 control);
104
105 struct kbase_context *
106 kbase_create_context(struct kbase_device *kbdev, bool is_compat);
107 void kbase_destroy_context(struct kbase_context *kctx);
108 int kbase_context_set_create_flags(struct kbase_context *kctx, u32 flags);
109
110 int kbase_jd_init(struct kbase_context *kctx);
111 void kbase_jd_exit(struct kbase_context *kctx);
112 #ifdef BASE_LEGACY_UK6_SUPPORT
113 int kbase_jd_submit(struct kbase_context *kctx,
114                 const struct kbase_uk_job_submit *submit_data,
115                 int uk6_atom);
116 #else
117 int kbase_jd_submit(struct kbase_context *kctx,
118                 const struct kbase_uk_job_submit *submit_data);
119 #endif
120
121 /**
122  * kbase_jd_done_worker - Handle a job completion
123  * @data: a &struct work_struct
124  *
125  * This function requeues the job from the runpool (if it was soft-stopped or
126  * removed from NEXT registers).
127  *
128  * Removes it from the system if it finished/failed/was cancelled.
129  *
130  * Resolves dependencies to add dependent jobs to the context, potentially
131  * starting them if necessary (which may add more references to the context)
132  *
133  * Releases the reference to the context from the no-longer-running job.
134  *
135  * Handles retrying submission outside of IRQ context if it failed from within
136  * IRQ context.
137  */
138 void kbase_jd_done_worker(struct work_struct *data);
139
140 void kbase_jd_done(struct kbase_jd_atom *katom, int slot_nr, ktime_t *end_timestamp,
141                 kbasep_js_atom_done_code done_code);
142 void kbase_jd_cancel(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);
150 void kbase_jd_dep_clear_locked(struct kbase_jd_atom *katom);
151
152 void kbase_job_done(struct kbase_device *kbdev, u32 done);
153
154 void kbase_gpu_cacheclean(struct kbase_device *kbdev,
155                                         struct kbase_jd_atom *katom);
156 /**
157  * kbase_job_slot_ctx_priority_check_locked(): - Check for lower priority atoms
158  *                                               and soft stop them
159  * @kctx: Pointer to context to check.
160  * @katom: Pointer to priority atom.
161  *
162  * Atoms from @kctx on the same job slot as @katom, which have lower priority
163  * than @katom will be soft stopped and put back in the queue, so that atoms
164  * with higher priority can run.
165  *
166  * The js_data.runpool_irq.lock must be held when calling this function.
167  */
168 void kbase_job_slot_ctx_priority_check_locked(struct kbase_context *kctx,
169                                 struct kbase_jd_atom *katom);
170
171 void kbase_job_slot_softstop(struct kbase_device *kbdev, int js,
172                 struct kbase_jd_atom *target_katom);
173 void kbase_job_slot_softstop_swflags(struct kbase_device *kbdev, int js,
174                 struct kbase_jd_atom *target_katom, u32 sw_flags);
175 void kbase_job_slot_hardstop(struct kbase_context *kctx, int js,
176                 struct kbase_jd_atom *target_katom);
177 void kbase_job_check_enter_disjoint(struct kbase_device *kbdev, u32 action,
178                 base_jd_core_req core_reqs, struct kbase_jd_atom *target_katom);
179 void kbase_job_check_leave_disjoint(struct kbase_device *kbdev,
180                 struct kbase_jd_atom *target_katom);
181
182 void kbase_event_post(struct kbase_context *ctx, struct kbase_jd_atom *event);
183 int kbase_event_dequeue(struct kbase_context *ctx, struct base_jd_event_v2 *uevent);
184 int kbase_event_pending(struct kbase_context *ctx);
185 int kbase_event_init(struct kbase_context *kctx);
186 void kbase_event_close(struct kbase_context *kctx);
187 void kbase_event_cleanup(struct kbase_context *kctx);
188 void kbase_event_wakeup(struct kbase_context *kctx);
189
190 int kbase_process_soft_job(struct kbase_jd_atom *katom);
191 int kbase_prepare_soft_job(struct kbase_jd_atom *katom);
192 void kbase_finish_soft_job(struct kbase_jd_atom *katom);
193 void kbase_cancel_soft_job(struct kbase_jd_atom *katom);
194 void kbase_resume_suspended_soft_jobs(struct kbase_device *kbdev);
195 void kbasep_add_waiting_soft_job(struct kbase_jd_atom *katom);
196 void kbasep_remove_waiting_soft_job(struct kbase_jd_atom *katom);
197 int kbase_soft_event_update(struct kbase_context *kctx,
198                             u64 event,
199                             unsigned char new_status);
200
201 bool kbase_replay_process(struct kbase_jd_atom *katom);
202
203 void kbasep_soft_job_timeout_worker(unsigned long data);
204 void kbasep_complete_triggered_soft_events(struct kbase_context *kctx, u64 evt);
205
206 /* api used internally for register access. Contains validation and tracing */
207 void kbase_device_trace_register_access(struct kbase_context *kctx, enum kbase_reg_access_type type, u16 reg_offset, u32 reg_value);
208 int kbase_device_trace_buffer_install(
209                 struct kbase_context *kctx, u32 *tb, size_t size);
210 void kbase_device_trace_buffer_uninstall(struct kbase_context *kctx);
211
212 /* api to be ported per OS, only need to do the raw register access */
213 void kbase_os_reg_write(struct kbase_device *kbdev, u16 offset, u32 value);
214 u32 kbase_os_reg_read(struct kbase_device *kbdev, u16 offset);
215
216 void kbasep_as_do_poke(struct work_struct *work);
217
218 /** Returns the name associated with a Mali exception code
219  *
220  * This function is called from the interrupt handler when a GPU fault occurs.
221  * It reports the details of the fault using KBASE_DEBUG_PRINT_WARN.
222  *
223  * @param[in] kbdev     The kbase device that the GPU fault occurred from.
224  * @param[in] exception_code  exception code
225  * @return name associated with the exception code
226  */
227 const char *kbase_exception_name(struct kbase_device *kbdev,
228                 u32 exception_code);
229
230 /**
231  * Check whether a system suspend is in progress, or has already been suspended
232  *
233  * The caller should ensure that either kbdev->pm.active_count_lock is held, or
234  * a dmb was executed recently (to ensure the value is most
235  * up-to-date). However, without a lock the value could change afterwards.
236  *
237  * @return false if a suspend is not in progress
238  * @return !=false otherwise
239  */
240 static inline bool kbase_pm_is_suspending(struct kbase_device *kbdev)
241 {
242         return kbdev->pm.suspending;
243 }
244
245 /**
246  * Return the atom's ID, as was originally supplied by userspace in
247  * base_jd_atom_v2::atom_number
248  */
249 static inline int kbase_jd_atom_id(struct kbase_context *kctx, struct kbase_jd_atom *katom)
250 {
251         int result;
252
253         KBASE_DEBUG_ASSERT(kctx);
254         KBASE_DEBUG_ASSERT(katom);
255         KBASE_DEBUG_ASSERT(katom->kctx == kctx);
256
257         result = katom - &kctx->jctx.atoms[0];
258         KBASE_DEBUG_ASSERT(result >= 0 && result <= BASE_JD_ATOM_COUNT);
259         return result;
260 }
261
262 /**
263  * kbase_jd_atom_from_id - Return the atom structure for the given atom ID
264  * @kctx: Context pointer
265  * @id:   ID of atom to retrieve
266  *
267  * Return: Pointer to struct kbase_jd_atom associated with the supplied ID
268  */
269 static inline struct kbase_jd_atom *kbase_jd_atom_from_id(
270                 struct kbase_context *kctx, int id)
271 {
272         return &kctx->jctx.atoms[id];
273 }
274
275 /**
276  * Initialize the disjoint state
277  *
278  * The disjoint event count and state are both set to zero.
279  *
280  * Disjoint functions usage:
281  *
282  * The disjoint event count should be incremented whenever a disjoint event occurs.
283  *
284  * There are several cases which are regarded as disjoint behavior. Rather than just increment
285  * the counter during disjoint events we also increment the counter when jobs may be affected
286  * by what the GPU is currently doing. To facilitate this we have the concept of disjoint state.
287  *
288  * Disjoint state is entered during GPU reset and for the entire time that an atom is replaying
289  * (as part of the replay workaround). Increasing the disjoint state also increases the count of
290  * disjoint events.
291  *
292  * The disjoint state is then used to increase the count of disjoint events during job submission
293  * and job completion. Any atom submitted or completed while the disjoint state is greater than
294  * zero is regarded as a disjoint event.
295  *
296  * The disjoint event counter is also incremented immediately whenever a job is soft stopped
297  * and during context creation.
298  *
299  * @param kbdev The kbase device
300  */
301 void kbase_disjoint_init(struct kbase_device *kbdev);
302
303 /**
304  * Increase the count of disjoint events
305  * called when a disjoint event has happened
306  *
307  * @param kbdev The kbase device
308  */
309 void kbase_disjoint_event(struct kbase_device *kbdev);
310
311 /**
312  * Increase the count of disjoint events only if the GPU is in a disjoint state
313  *
314  * This should be called when something happens which could be disjoint if the GPU
315  * is in a disjoint state. The state refcount keeps track of this.
316  *
317  * @param kbdev The kbase device
318  */
319 void kbase_disjoint_event_potential(struct kbase_device *kbdev);
320
321 /**
322  * Returns the count of disjoint events
323  *
324  * @param kbdev The kbase device
325  * @return the count of disjoint events
326  */
327 u32 kbase_disjoint_event_get(struct kbase_device *kbdev);
328
329 /**
330  * Increment the refcount state indicating that the GPU is in a disjoint state.
331  *
332  * Also Increment the disjoint event count (calls @ref kbase_disjoint_event)
333  * eventually after the disjoint state has completed @ref kbase_disjoint_state_down
334  * should be called
335  *
336  * @param kbdev The kbase device
337  */
338 void kbase_disjoint_state_up(struct kbase_device *kbdev);
339
340 /**
341  * Decrement the refcount state
342  *
343  * Also Increment the disjoint event count (calls @ref kbase_disjoint_event)
344  *
345  * Called after @ref kbase_disjoint_state_up once the disjoint state is over
346  *
347  * @param kbdev The kbase device
348  */
349 void kbase_disjoint_state_down(struct kbase_device *kbdev);
350
351 /**
352  * If a job is soft stopped and the number of contexts is >= this value
353  * it is reported as a disjoint event
354  */
355 #define KBASE_DISJOINT_STATE_INTERLEAVED_CONTEXT_COUNT_THRESHOLD 2
356
357 #if !defined(UINT64_MAX)
358         #define UINT64_MAX ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
359 #endif
360
361 #if KBASE_TRACE_ENABLE
362 void kbasep_trace_debugfs_init(struct kbase_device *kbdev);
363
364 #ifndef CONFIG_MALI_SYSTEM_TRACE
365 /** Add trace values about a job-slot
366  *
367  * @note Any functions called through this macro will still be evaluated in
368  * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
369  * functions called to get the parameters supplied to this macro must:
370  * - be static or static inline
371  * - must just return 0 and have no other statements present in the body.
372  */
373 #define KBASE_TRACE_ADD_SLOT(kbdev, code, ctx, katom, gpu_addr, jobslot) \
374         kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
375                         KBASE_TRACE_FLAG_JOBSLOT, 0, jobslot, 0)
376
377 /** Add trace values about a job-slot, with info
378  *
379  * @note Any functions called through this macro will still be evaluated in
380  * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
381  * functions called to get the parameters supplied to this macro must:
382  * - be static or static inline
383  * - must just return 0 and have no other statements present in the body.
384  */
385 #define KBASE_TRACE_ADD_SLOT_INFO(kbdev, code, ctx, katom, gpu_addr, jobslot, info_val) \
386         kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
387                         KBASE_TRACE_FLAG_JOBSLOT, 0, jobslot, info_val)
388
389 /** Add trace values about a ctx refcount
390  *
391  * @note Any functions called through this macro will still be evaluated in
392  * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
393  * functions called to get the parameters supplied to this macro must:
394  * - be static or static inline
395  * - must just return 0 and have no other statements present in the body.
396  */
397 #define KBASE_TRACE_ADD_REFCOUNT(kbdev, code, ctx, katom, gpu_addr, refcount) \
398         kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
399                         KBASE_TRACE_FLAG_REFCOUNT, refcount, 0, 0)
400 /** Add trace values about a ctx refcount, and info
401  *
402  * @note Any functions called through this macro will still be evaluated in
403  * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
404  * functions called to get the parameters supplied to this macro must:
405  * - be static or static inline
406  * - must just return 0 and have no other statements present in the body.
407  */
408 #define KBASE_TRACE_ADD_REFCOUNT_INFO(kbdev, code, ctx, katom, gpu_addr, refcount, info_val) \
409         kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
410                         KBASE_TRACE_FLAG_REFCOUNT, refcount, 0, info_val)
411
412 /** Add trace values (no slot or refcount)
413  *
414  * @note Any functions called through this macro will still be evaluated in
415  * Release builds (CONFIG_MALI_DEBUG not defined). Therefore, when KBASE_TRACE_ENABLE == 0 any
416  * functions called to get the parameters supplied to this macro must:
417  * - be static or static inline
418  * - must just return 0 and have no other statements present in the body.
419  */
420 #define KBASE_TRACE_ADD(kbdev, code, ctx, katom, gpu_addr, info_val)     \
421         kbasep_trace_add(kbdev, KBASE_TRACE_CODE(code), ctx, katom, gpu_addr, \
422                         0, 0, 0, info_val)
423
424 /** Clear the trace */
425 #define KBASE_TRACE_CLEAR(kbdev) \
426         kbasep_trace_clear(kbdev)
427
428 /** Dump the slot trace */
429 #define KBASE_TRACE_DUMP(kbdev) \
430         kbasep_trace_dump(kbdev)
431
432 /** PRIVATE - do not use directly. Use KBASE_TRACE_ADD() instead */
433 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);
434 /** PRIVATE - do not use directly. Use KBASE_TRACE_CLEAR() instead */
435 void kbasep_trace_clear(struct kbase_device *kbdev);
436 #else /* #ifndef CONFIG_MALI_SYSTEM_TRACE */
437 /* Dispatch kbase trace events as system trace events */
438 #include <mali_linux_kbase_trace.h>
439 #define KBASE_TRACE_ADD_SLOT(kbdev, code, ctx, katom, gpu_addr, jobslot)\
440         trace_mali_##code(jobslot, 0)
441
442 #define KBASE_TRACE_ADD_SLOT_INFO(kbdev, code, ctx, katom, gpu_addr, jobslot, info_val)\
443         trace_mali_##code(jobslot, info_val)
444
445 #define KBASE_TRACE_ADD_REFCOUNT(kbdev, code, ctx, katom, gpu_addr, refcount)\
446         trace_mali_##code(refcount, 0)
447
448 #define KBASE_TRACE_ADD_REFCOUNT_INFO(kbdev, code, ctx, katom, gpu_addr, refcount, info_val)\
449         trace_mali_##code(refcount, info_val)
450
451 #define KBASE_TRACE_ADD(kbdev, code, ctx, katom, gpu_addr, info_val)\
452         trace_mali_##code(gpu_addr, info_val)
453
454 #define KBASE_TRACE_CLEAR(kbdev)\
455         do {\
456                 CSTD_UNUSED(kbdev);\
457                 CSTD_NOP(0);\
458         } while (0)
459 #define KBASE_TRACE_DUMP(kbdev)\
460         do {\
461                 CSTD_UNUSED(kbdev);\
462                 CSTD_NOP(0);\
463         } while (0)
464
465 #endif /* #ifndef CONFIG_MALI_SYSTEM_TRACE */
466 #else
467 #define KBASE_TRACE_ADD_SLOT(kbdev, code, ctx, katom, gpu_addr, jobslot)\
468         do {\
469                 CSTD_UNUSED(kbdev);\
470                 CSTD_NOP(code);\
471                 CSTD_UNUSED(ctx);\
472                 CSTD_UNUSED(katom);\
473                 CSTD_UNUSED(gpu_addr);\
474                 CSTD_UNUSED(jobslot);\
475         } while (0)
476
477 #define KBASE_TRACE_ADD_SLOT_INFO(kbdev, code, ctx, katom, gpu_addr, jobslot, info_val)\
478         do {\
479                 CSTD_UNUSED(kbdev);\
480                 CSTD_NOP(code);\
481                 CSTD_UNUSED(ctx);\
482                 CSTD_UNUSED(katom);\
483                 CSTD_UNUSED(gpu_addr);\
484                 CSTD_UNUSED(jobslot);\
485                 CSTD_UNUSED(info_val);\
486                 CSTD_NOP(0);\
487         } while (0)
488
489 #define KBASE_TRACE_ADD_REFCOUNT(kbdev, code, ctx, katom, gpu_addr, refcount)\
490         do {\
491                 CSTD_UNUSED(kbdev);\
492                 CSTD_NOP(code);\
493                 CSTD_UNUSED(ctx);\
494                 CSTD_UNUSED(katom);\
495                 CSTD_UNUSED(gpu_addr);\
496                 CSTD_UNUSED(refcount);\
497                 CSTD_NOP(0);\
498         } while (0)
499
500 #define KBASE_TRACE_ADD_REFCOUNT_INFO(kbdev, code, ctx, katom, gpu_addr, refcount, info_val)\
501         do {\
502                 CSTD_UNUSED(kbdev);\
503                 CSTD_NOP(code);\
504                 CSTD_UNUSED(ctx);\
505                 CSTD_UNUSED(katom);\
506                 CSTD_UNUSED(gpu_addr);\
507                 CSTD_UNUSED(info_val);\
508                 CSTD_NOP(0);\
509         } while (0)
510
511 #define KBASE_TRACE_ADD(kbdev, code, subcode, ctx, katom, val)\
512         do {\
513                 CSTD_UNUSED(kbdev);\
514                 CSTD_NOP(code);\
515                 CSTD_UNUSED(subcode);\
516                 CSTD_UNUSED(ctx);\
517                 CSTD_UNUSED(katom);\
518                 CSTD_UNUSED(val);\
519                 CSTD_NOP(0);\
520         } while (0)
521
522 #define KBASE_TRACE_CLEAR(kbdev)\
523         do {\
524                 CSTD_UNUSED(kbdev);\
525                 CSTD_NOP(0);\
526         } while (0)
527 #define KBASE_TRACE_DUMP(kbdev)\
528         do {\
529                 CSTD_UNUSED(kbdev);\
530                 CSTD_NOP(0);\
531         } while (0)
532 #endif /* KBASE_TRACE_ENABLE */
533 /** PRIVATE - do not use directly. Use KBASE_TRACE_DUMP() instead */
534 void kbasep_trace_dump(struct kbase_device *kbdev);
535
536 #ifdef CONFIG_MALI_DEBUG
537 /**
538  * kbase_set_driver_inactive - Force driver to go inactive
539  * @kbdev:    Device pointer
540  * @inactive: true if driver should go inactive, false otherwise
541  *
542  * Forcing the driver inactive will cause all future IOCTLs to wait until the
543  * driver is made active again. This is intended solely for the use of tests
544  * which require that no jobs are running while the test executes.
545  */
546 void kbase_set_driver_inactive(struct kbase_device *kbdev, bool inactive);
547 #endif /* CONFIG_MALI_DEBUG */
548
549 #endif