4bb8c2c7aec23176a4208809a58a087fe4936025
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_defs.h
1 /*
2  *
3  * (C) COPYRIGHT 2011-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 /**
21  * @file mali_kbase_defs.h
22  *
23  * Defintions (types, defines, etcs) common to Kbase. They are placed here to
24  * allow the hierarchy of header files to work.
25  */
26
27 #ifndef _KBASE_DEFS_H_
28 #define _KBASE_DEFS_H_
29
30 #include <mali_kbase_config.h>
31 #include <mali_base_hwconfig_features.h>
32 #include <mali_base_hwconfig_issues.h>
33 #include <mali_kbase_mem_lowlevel.h>
34 #include <mali_kbase_mmu_hw.h>
35 #include <mali_kbase_mmu_mode.h>
36 #include <mali_kbase_instr_defs.h>
37
38 #include <linux/atomic.h>
39 #include <linux/mempool.h>
40 #include <linux/slab.h>
41 #include <linux/file.h>
42
43 #ifdef CONFIG_MALI_FPGA_BUS_LOGGER
44 #include <linux/bus_logger.h>
45 #endif
46
47
48 #ifdef CONFIG_KDS
49 #include <linux/kds.h>
50 #endif                          /* CONFIG_KDS */
51
52 #ifdef CONFIG_SYNC
53 #include "sync.h"
54 #endif                          /* CONFIG_SYNC */
55
56 #include "mali_kbase_dma_fence.h"
57
58 #ifdef CONFIG_DEBUG_FS
59 #include <linux/debugfs.h>
60 #endif                          /* CONFIG_DEBUG_FS */
61
62 #ifdef CONFIG_PM_DEVFREQ
63 #include <linux/devfreq.h>
64 #endif /* CONFIG_DEVFREQ */
65
66 #include <linux/clk.h>
67 #include <linux/regulator/consumer.h>
68
69 #if defined(CONFIG_PM)
70 #define KBASE_PM_RUNTIME 1
71 #endif
72
73 /** Enable SW tracing when set */
74 #ifdef CONFIG_MALI_MIDGARD_ENABLE_TRACE
75 #define KBASE_TRACE_ENABLE 1
76 #endif
77
78 #ifndef KBASE_TRACE_ENABLE
79 #ifdef CONFIG_MALI_DEBUG
80 #define KBASE_TRACE_ENABLE 1
81 #else
82 #define KBASE_TRACE_ENABLE 0
83 #endif                          /* CONFIG_MALI_DEBUG */
84 #endif                          /* KBASE_TRACE_ENABLE */
85
86 /** Dump Job slot trace on error (only active if KBASE_TRACE_ENABLE != 0) */
87 #define KBASE_TRACE_DUMP_ON_JOB_SLOT_ERROR 1
88
89 /**
90  * Number of milliseconds before resetting the GPU when a job cannot be "zapped" from the hardware.
91  * Note that the time is actually ZAP_TIMEOUT+SOFT_STOP_RESET_TIMEOUT between the context zap starting and the GPU
92  * actually being reset to give other contexts time for their jobs to be soft-stopped and removed from the hardware
93  * before resetting.
94  */
95 #define ZAP_TIMEOUT             1000
96
97 /** Number of milliseconds before we time out on a GPU soft/hard reset */
98 #define RESET_TIMEOUT           500
99
100 /**
101  * Prevent soft-stops from occuring in scheduling situations
102  *
103  * This is not due to HW issues, but when scheduling is desired to be more predictable.
104  *
105  * Therefore, soft stop may still be disabled due to HW issues.
106  *
107  * @note Soft stop will still be used for non-scheduling purposes e.g. when terminating a context.
108  *
109  * @note if not in use, define this value to 0 instead of \#undef'ing it
110  */
111 #define KBASE_DISABLE_SCHEDULING_SOFT_STOPS 0
112
113 /**
114  * Prevent hard-stops from occuring in scheduling situations
115  *
116  * This is not due to HW issues, but when scheduling is desired to be more predictable.
117  *
118  * @note Hard stop will still be used for non-scheduling purposes e.g. when terminating a context.
119  *
120  * @note if not in use, define this value to 0 instead of \#undef'ing it
121  */
122 #define KBASE_DISABLE_SCHEDULING_HARD_STOPS 0
123
124 /**
125  * The maximum number of Job Slots to support in the Hardware.
126  *
127  * You can optimize this down if your target devices will only ever support a
128  * small number of job slots.
129  */
130 #define BASE_JM_MAX_NR_SLOTS        3
131
132 /**
133  * The maximum number of Address Spaces to support in the Hardware.
134  *
135  * You can optimize this down if your target devices will only ever support a
136  * small number of Address Spaces
137  */
138 #define BASE_MAX_NR_AS              16
139
140 /* mmu */
141 #define MIDGARD_MMU_VA_BITS 48
142
143 #if MIDGARD_MMU_VA_BITS > 39
144 #define MIDGARD_MMU_TOPLEVEL    0
145 #else
146 #define MIDGARD_MMU_TOPLEVEL    1
147 #endif
148
149 #define MIDGARD_MMU_BOTTOMLEVEL 3
150
151 #define GROWABLE_FLAGS_REQUIRED (KBASE_REG_PF_GROW | KBASE_REG_GPU_WR)
152
153 /** setting in kbase_context::as_nr that indicates it's invalid */
154 #define KBASEP_AS_NR_INVALID     (-1)
155
156 #define KBASE_LOCK_REGION_MAX_SIZE (63)
157 #define KBASE_LOCK_REGION_MIN_SIZE (11)
158
159 #define KBASE_TRACE_SIZE_LOG2 8 /* 256 entries */
160 #define KBASE_TRACE_SIZE (1 << KBASE_TRACE_SIZE_LOG2)
161 #define KBASE_TRACE_MASK ((1 << KBASE_TRACE_SIZE_LOG2)-1)
162
163 #include "mali_kbase_js_defs.h"
164 #include "mali_kbase_hwaccess_defs.h"
165
166 #define KBASEP_FORCE_REPLAY_DISABLED 0
167
168 /* Maximum force replay limit when randomization is enabled */
169 #define KBASEP_FORCE_REPLAY_RANDOM_LIMIT 16
170
171 /** Atom has been previously soft-stoppped */
172 #define KBASE_KATOM_FLAG_BEEN_SOFT_STOPPPED (1<<1)
173 /** Atom has been previously retried to execute */
174 #define KBASE_KATOM_FLAGS_RERUN (1<<2)
175 #define KBASE_KATOM_FLAGS_JOBCHAIN (1<<3)
176 /** Atom has been previously hard-stopped. */
177 #define KBASE_KATOM_FLAG_BEEN_HARD_STOPPED (1<<4)
178 /** Atom has caused us to enter disjoint state */
179 #define KBASE_KATOM_FLAG_IN_DISJOINT (1<<5)
180 /* Atom blocked on cross-slot dependency */
181 #define KBASE_KATOM_FLAG_X_DEP_BLOCKED (1<<7)
182 /* Atom has fail dependency on cross-slot dependency */
183 #define KBASE_KATOM_FLAG_FAIL_BLOCKER (1<<8)
184 /* Atom is currently in the list of atoms blocked on cross-slot dependencies */
185 #define KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST (1<<9)
186 /* Atom is currently holding a context reference */
187 #define KBASE_KATOM_FLAG_HOLDING_CTX_REF (1<<10)
188 /* Atom requires GPU to be in protected mode */
189 #define KBASE_KATOM_FLAG_PROTECTED (1<<11)
190 /* Atom has been stored in runnable_tree */
191 #define KBASE_KATOM_FLAG_JSCTX_IN_TREE (1<<12)
192
193 /* SW related flags about types of JS_COMMAND action
194  * NOTE: These must be masked off by JS_COMMAND_MASK */
195
196 /** This command causes a disjoint event */
197 #define JS_COMMAND_SW_CAUSES_DISJOINT 0x100
198
199 /** Bitmask of all SW related flags */
200 #define JS_COMMAND_SW_BITS  (JS_COMMAND_SW_CAUSES_DISJOINT)
201
202 #if (JS_COMMAND_SW_BITS & JS_COMMAND_MASK)
203 #error JS_COMMAND_SW_BITS not masked off by JS_COMMAND_MASK. Must update JS_COMMAND_SW_<..> bitmasks
204 #endif
205
206 /** Soft-stop command that causes a Disjoint event. This of course isn't
207  *  entirely masked off by JS_COMMAND_MASK */
208 #define JS_COMMAND_SOFT_STOP_WITH_SW_DISJOINT \
209                 (JS_COMMAND_SW_CAUSES_DISJOINT | JS_COMMAND_SOFT_STOP)
210
211 #define KBASEP_ATOM_ID_INVALID BASE_JD_ATOM_COUNT
212
213 #ifdef CONFIG_DEBUG_FS
214 struct base_job_fault_event {
215
216         u32 event_code;
217         struct kbase_jd_atom *katom;
218         struct work_struct job_fault_work;
219         struct list_head head;
220         int reg_offset;
221 };
222
223 #endif
224
225 struct kbase_jd_atom_dependency {
226         struct kbase_jd_atom *atom;
227         u8 dep_type;
228 };
229
230 /**
231  * @brief The function retrieves a read-only reference to the atom field from
232  * the  kbase_jd_atom_dependency structure
233  *
234  * @param[in] dep kbase jd atom dependency.
235  *
236  * @return readonly reference to dependent ATOM.
237  */
238 static inline const struct kbase_jd_atom * kbase_jd_katom_dep_atom(const struct kbase_jd_atom_dependency *dep)
239 {
240         LOCAL_ASSERT(dep != NULL);
241
242         return (const struct kbase_jd_atom *)(dep->atom);
243 }
244
245 /**
246  * @brief The function retrieves a read-only reference to the dependency type field from
247  * the  kbase_jd_atom_dependency structure
248  *
249  * @param[in] dep kbase jd atom dependency.
250  *
251  * @return A dependency type value.
252  */
253 static inline u8 kbase_jd_katom_dep_type(const struct kbase_jd_atom_dependency *dep)
254 {
255         LOCAL_ASSERT(dep != NULL);
256
257         return dep->dep_type;
258 }
259
260 /**
261  * @brief Setter macro for dep_atom array entry in kbase_jd_atom
262  *
263  * @param[in] dep    The kbase jd atom dependency.
264  * @param[in] a      The ATOM to be set as a dependency.
265  * @param     type   The ATOM dependency type to be set.
266  *
267  */
268 static inline void kbase_jd_katom_dep_set(const struct kbase_jd_atom_dependency *const_dep,
269                 struct kbase_jd_atom *a, u8 type)
270 {
271         struct kbase_jd_atom_dependency *dep;
272
273         LOCAL_ASSERT(const_dep != NULL);
274
275         dep = (struct kbase_jd_atom_dependency *)const_dep;
276
277         dep->atom = a;
278         dep->dep_type = type;
279 }
280
281 /**
282  * @brief Setter macro for dep_atom array entry in kbase_jd_atom
283  *
284  * @param[in] dep    The kbase jd atom dependency to be cleared.
285  *
286  */
287 static inline void kbase_jd_katom_dep_clear(const struct kbase_jd_atom_dependency *const_dep)
288 {
289         struct kbase_jd_atom_dependency *dep;
290
291         LOCAL_ASSERT(const_dep != NULL);
292
293         dep = (struct kbase_jd_atom_dependency *)const_dep;
294
295         dep->atom = NULL;
296         dep->dep_type = BASE_JD_DEP_TYPE_INVALID;
297 }
298
299 enum kbase_atom_gpu_rb_state {
300         /* Atom is not currently present in slot ringbuffer */
301         KBASE_ATOM_GPU_RB_NOT_IN_SLOT_RB,
302         /* Atom is in slot ringbuffer but is blocked on a previous atom */
303         KBASE_ATOM_GPU_RB_WAITING_BLOCKED,
304         /* Atom is in slot ringbuffer but is waiting for proected mode exit */
305         KBASE_ATOM_GPU_RB_WAITING_PROTECTED_MODE_EXIT,
306         /* Atom is in slot ringbuffer but is waiting for cores to become
307          * available */
308         KBASE_ATOM_GPU_RB_WAITING_FOR_CORE_AVAILABLE,
309         /* Atom is in slot ringbuffer but is blocked on affinity */
310         KBASE_ATOM_GPU_RB_WAITING_AFFINITY,
311         /* Atom is in slot ringbuffer but is waiting for protected mode entry */
312         KBASE_ATOM_GPU_RB_WAITING_PROTECTED_MODE_ENTRY,
313         /* Atom is in slot ringbuffer and ready to run */
314         KBASE_ATOM_GPU_RB_READY,
315         /* Atom is in slot ringbuffer and has been submitted to the GPU */
316         KBASE_ATOM_GPU_RB_SUBMITTED,
317         /* Atom must be returned to JS as soon as it reaches the head of the
318          * ringbuffer due to a previous failure */
319         KBASE_ATOM_GPU_RB_RETURN_TO_JS
320 };
321
322 enum kbase_atom_exit_protected_state {
323         /*
324          * Starting state:
325          * Check if a transition out of protected mode is required.
326          */
327         KBASE_ATOM_EXIT_PROTECTED_CHECK,
328         /* Wait for the L2 to become idle in preparation for the reset. */
329         KBASE_ATOM_EXIT_PROTECTED_IDLE_L2,
330         /* Issue the protected reset. */
331         KBASE_ATOM_EXIT_PROTECTED_RESET,
332         /*
333          * End state;
334          * Wait for the reset to complete.
335          */
336         KBASE_ATOM_EXIT_PROTECTED_RESET_WAIT,
337 };
338
339 struct kbase_ext_res {
340         u64 gpu_address;
341         struct kbase_mem_phy_alloc *alloc;
342 };
343
344 struct kbase_jd_atom {
345         struct work_struct work;
346         ktime_t start_timestamp;
347         u64 time_spent_us; /**< Total time spent on the GPU in microseconds */
348
349         struct base_jd_udata udata;
350         struct kbase_context *kctx;
351
352         struct list_head dep_head[2];
353         struct list_head dep_item[2];
354         const struct kbase_jd_atom_dependency dep[2];
355         /* List head used during job dispatch job_done processing - as
356          * dependencies may not be entirely resolved at this point, we need to
357          * use a separate list head. */
358         struct list_head jd_item;
359         /* true if atom's jd_item is currently on a list. Prevents atom being
360          * processed twice. */
361         bool in_jd_list;
362
363         u16 nr_extres;
364         struct kbase_ext_res *extres;
365
366         u32 device_nr;
367         u64 affinity;
368         u64 jc;
369         enum kbase_atom_coreref_state coreref_state;
370 #ifdef CONFIG_KDS
371         struct list_head node;
372         struct kds_resource_set *kds_rset;
373         bool kds_dep_satisfied;
374 #endif                          /* CONFIG_KDS */
375 #ifdef CONFIG_SYNC
376         struct sync_fence *fence;
377         struct sync_fence_waiter sync_waiter;
378 #endif                          /* CONFIG_SYNC */
379 #ifdef CONFIG_MALI_DMA_FENCE
380         struct {
381                 /* This points to the dma-buf fence for this atom. If this is
382                  * NULL then there is no fence for this atom and the other
383                  * fields related to dma_fence may have invalid data.
384                  *
385                  * The context and seqno fields contain the details for this
386                  * fence.
387                  *
388                  * This fence is signaled when the katom is completed,
389                  * regardless of the event_code of the katom (signal also on
390                  * failure).
391                  */
392                 struct fence *fence;
393                 /* The dma-buf fence context number for this atom. A unique
394                  * context number is allocated to each katom in the context on
395                  * context creation.
396                  */
397                 unsigned int context;
398                 /* The dma-buf fence sequence number for this atom. This is
399                  * increased every time this katom uses dma-buf fence.
400                  */
401                 atomic_t seqno;
402                 /* This contains a list of all callbacks set up to wait on
403                  * other fences.  This atom must be held back from JS until all
404                  * these callbacks have been called and dep_count have reached
405                  * 0. The initial value of dep_count must be equal to the
406                  * number of callbacks on this list.
407                  *
408                  * This list is protected by jctx.lock. Callbacks are added to
409                  * this list when the atom is built and the wait are set up.
410                  * All the callbacks then stay on the list until all callbacks
411                  * have been called and the atom is queued, or cancelled, and
412                  * then all callbacks are taken off the list and freed.
413                  */
414                 struct list_head callbacks;
415                 /* Atomic counter of number of outstandind dma-buf fence
416                  * dependencies for this atom. When dep_count reaches 0 the
417                  * atom may be queued.
418                  *
419                  * The special value "-1" may only be set after the count
420                  * reaches 0, while holding jctx.lock. This indicates that the
421                  * atom has been handled, either queued in JS or cancelled.
422                  *
423                  * If anyone but the dma-fence worker sets this to -1 they must
424                  * ensure that any potentially queued worker must have
425                  * completed before allowing the atom to be marked as unused.
426                  * This can be done by flushing the fence work queue:
427                  * kctx->dma_fence.wq.
428                  */
429                 atomic_t dep_count;
430         } dma_fence;
431 #endif /* CONFIG_MALI_DMA_FENCE */
432
433         /* Note: refer to kbasep_js_atom_retained_state, which will take a copy of some of the following members */
434         enum base_jd_event_code event_code;
435         base_jd_core_req core_req;          /**< core requirements */
436         /** Job Slot to retry submitting to if submission from IRQ handler failed
437          *
438          * NOTE: see if this can be unified into the another member e.g. the event */
439         int retry_submit_on_slot;
440
441         union kbasep_js_policy_job_info sched_info;
442         /* JS atom priority with respect to other atoms on its kctx. */
443         int sched_priority;
444
445         int poking;             /* BASE_HW_ISSUE_8316 */
446
447         wait_queue_head_t completed;
448         enum kbase_jd_atom_state status;
449 #ifdef CONFIG_GPU_TRACEPOINTS
450         int work_id;
451 #endif
452         /* Assigned after atom is completed. Used to check whether PRLAM-10676 workaround should be applied */
453         int slot_nr;
454
455         u32 atom_flags;
456
457         /* Number of times this atom has been retried. Used by replay soft job.
458          */
459         int retry_count;
460
461         enum kbase_atom_gpu_rb_state gpu_rb_state;
462
463         u64 need_cache_flush_cores_retained;
464
465         atomic_t blocked;
466
467         /* Pointer to atom that this atom has same-slot dependency on */
468         struct kbase_jd_atom *pre_dep;
469         /* Pointer to atom that has same-slot dependency on this atom */
470         struct kbase_jd_atom *post_dep;
471
472         /* Pointer to atom that this atom has cross-slot dependency on */
473         struct kbase_jd_atom *x_pre_dep;
474         /* Pointer to atom that has cross-slot dependency on this atom */
475         struct kbase_jd_atom *x_post_dep;
476
477         /* The GPU's flush count recorded at the time of submission, used for
478          * the cache flush optimisation */
479         u32 flush_id;
480
481         struct kbase_jd_atom_backend backend;
482 #ifdef CONFIG_DEBUG_FS
483         struct base_job_fault_event fault_event;
484 #endif
485
486         /* List head used for two different purposes:
487          *  1. Overflow list for JS ring buffers. If an atom is ready to run,
488          *     but there is no room in the JS ring buffer, then the atom is put
489          *     on the ring buffer's overflow list using this list node.
490          *  2. List of waiting soft jobs.
491          */
492         struct list_head queue;
493
494         struct kbase_va_region *jit_addr_reg;
495
496         /* If non-zero, this indicates that the atom will fail with the set
497          * event_code when the atom is processed. */
498         enum base_jd_event_code will_fail_event_code;
499
500         enum kbase_atom_exit_protected_state exit_protected_state;
501
502         struct rb_node runnable_tree_node;
503
504         /* 'Age' of atom relative to other atoms in the context. */
505         u32 age;
506 };
507
508 static inline bool kbase_jd_katom_is_protected(const struct kbase_jd_atom *katom)
509 {
510         return (bool)(katom->atom_flags & KBASE_KATOM_FLAG_PROTECTED);
511 }
512
513 /*
514  * Theory of operations:
515  *
516  * Atom objects are statically allocated within the context structure.
517  *
518  * Each atom is the head of two lists, one for the "left" set of dependencies, one for the "right" set.
519  */
520
521 #define KBASE_JD_DEP_QUEUE_SIZE 256
522
523 struct kbase_jd_context {
524         struct mutex lock;
525         struct kbasep_js_kctx_info sched_info;
526         struct kbase_jd_atom atoms[BASE_JD_ATOM_COUNT];
527
528         /** Tracks all job-dispatch jobs.  This includes those not tracked by
529          * the scheduler: 'not ready to run' and 'dependency-only' jobs. */
530         u32 job_nr;
531
532         /** Waitq that reflects whether there are no jobs (including SW-only
533          * dependency jobs). This is set when no jobs are present on the ctx,
534          * and clear when there are jobs.
535          *
536          * @note: Job Dispatcher knows about more jobs than the Job Scheduler:
537          * the Job Scheduler is unaware of jobs that are blocked on dependencies,
538          * and SW-only dependency jobs.
539          *
540          * This waitq can be waited upon to find out when the context jobs are all
541          * done/cancelled (including those that might've been blocked on
542          * dependencies) - and so, whether it can be terminated. However, it should
543          * only be terminated once it is neither present in the policy-queue (see
544          * kbasep_js_policy_try_evict_ctx() ) nor the run-pool (see
545          * kbasep_js_kctx_info::ctx::is_scheduled).
546          *
547          * Since the waitq is only set under kbase_jd_context::lock,
548          * the waiter should also briefly obtain and drop kbase_jd_context::lock to
549          * guarentee that the setter has completed its work on the kbase_context
550          *
551          * This must be updated atomically with:
552          * - kbase_jd_context::job_nr */
553         wait_queue_head_t zero_jobs_wait;
554
555         /** Job Done workqueue. */
556         struct workqueue_struct *job_done_wq;
557
558         spinlock_t tb_lock;
559         u32 *tb;
560         size_t tb_wrap_offset;
561
562 #ifdef CONFIG_KDS
563         struct kds_callback kds_cb;
564 #endif                          /* CONFIG_KDS */
565 #ifdef CONFIG_GPU_TRACEPOINTS
566         atomic_t work_id;
567 #endif
568 };
569
570 struct kbase_device_info {
571         u32 features;
572 };
573
574 /** Poking state for BASE_HW_ISSUE_8316  */
575 enum {
576         KBASE_AS_POKE_STATE_IN_FLIGHT     = 1<<0,
577         KBASE_AS_POKE_STATE_KILLING_POKE  = 1<<1
578 };
579
580 /** Poking state for BASE_HW_ISSUE_8316  */
581 typedef u32 kbase_as_poke_state;
582
583 struct kbase_mmu_setup {
584         u64     transtab;
585         u64     memattr;
586         u64     transcfg;
587 };
588
589 /**
590  * Important: Our code makes assumptions that a struct kbase_as structure is always at
591  * kbase_device->as[number]. This is used to recover the containing
592  * struct kbase_device from a struct kbase_as structure.
593  *
594  * Therefore, struct kbase_as structures must not be allocated anywhere else.
595  */
596 struct kbase_as {
597         int number;
598
599         struct workqueue_struct *pf_wq;
600         struct work_struct work_pagefault;
601         struct work_struct work_busfault;
602         enum kbase_mmu_fault_type fault_type;
603         u32 fault_status;
604         u64 fault_addr;
605         u64 fault_extra_addr;
606         struct mutex transaction_mutex;
607
608         struct kbase_mmu_setup current_setup;
609
610         /* BASE_HW_ISSUE_8316  */
611         struct workqueue_struct *poke_wq;
612         struct work_struct poke_work;
613         /** Protected by kbasep_js_device_data::runpool_irq::lock */
614         int poke_refcount;
615         /** Protected by kbasep_js_device_data::runpool_irq::lock */
616         kbase_as_poke_state poke_state;
617         struct hrtimer poke_timer;
618 };
619
620 static inline int kbase_as_has_bus_fault(struct kbase_as *as)
621 {
622         return as->fault_type == KBASE_MMU_FAULT_TYPE_BUS;
623 }
624
625 static inline int kbase_as_has_page_fault(struct kbase_as *as)
626 {
627         return as->fault_type == KBASE_MMU_FAULT_TYPE_PAGE;
628 }
629
630 struct kbasep_mem_device {
631         atomic_t used_pages;   /* Tracks usage of OS shared memory. Updated
632                                    when OS memory is allocated/freed. */
633
634 };
635
636 #define KBASE_TRACE_CODE(X) KBASE_TRACE_CODE_ ## X
637
638 enum kbase_trace_code {
639         /* IMPORTANT: USE OF SPECIAL #INCLUDE OF NON-STANDARD HEADER FILE
640          * THIS MUST BE USED AT THE START OF THE ENUM */
641 #define KBASE_TRACE_CODE_MAKE_CODE(X) KBASE_TRACE_CODE(X)
642 #include "mali_kbase_trace_defs.h"
643 #undef  KBASE_TRACE_CODE_MAKE_CODE
644         /* Comma on its own, to extend the list */
645         ,
646         /* Must be the last in the enum */
647         KBASE_TRACE_CODE_COUNT
648 };
649
650 #define KBASE_TRACE_FLAG_REFCOUNT (((u8)1) << 0)
651 #define KBASE_TRACE_FLAG_JOBSLOT  (((u8)1) << 1)
652
653 struct kbase_trace {
654         struct timespec timestamp;
655         u32 thread_id;
656         u32 cpu;
657         void *ctx;
658         bool katom;
659         int atom_number;
660         u64 atom_udata[2];
661         u64 gpu_addr;
662         unsigned long info_val;
663         u8 code;
664         u8 jobslot;
665         u8 refcount;
666         u8 flags;
667 };
668
669 /** Event IDs for the power management framework.
670  *
671  * Any of these events might be missed, so they should not be relied upon to
672  * find the precise state of the GPU at a particular time in the
673  * trace. Overall, we should get a high percentage of these events for
674  * statisical purposes, and so a few missing should not be a problem */
675 enum kbase_timeline_pm_event {
676         /* helper for tests */
677         KBASEP_TIMELINE_PM_EVENT_FIRST,
678
679         /** Event reserved for backwards compatibility with 'init' events */
680         KBASE_TIMELINE_PM_EVENT_RESERVED_0 = KBASEP_TIMELINE_PM_EVENT_FIRST,
681
682         /** The power state of the device has changed.
683          *
684          * Specifically, the device has reached a desired or available state.
685          */
686         KBASE_TIMELINE_PM_EVENT_GPU_STATE_CHANGED,
687
688         /** The GPU is becoming active.
689          *
690          * This event is sent when the first context is about to use the GPU.
691          */
692         KBASE_TIMELINE_PM_EVENT_GPU_ACTIVE,
693
694         /** The GPU is becoming idle.
695          *
696          * This event is sent when the last context has finished using the GPU.
697          */
698         KBASE_TIMELINE_PM_EVENT_GPU_IDLE,
699
700         /** Event reserved for backwards compatibility with 'policy_change'
701          * events */
702         KBASE_TIMELINE_PM_EVENT_RESERVED_4,
703
704         /** Event reserved for backwards compatibility with 'system_suspend'
705          * events */
706         KBASE_TIMELINE_PM_EVENT_RESERVED_5,
707
708         /** Event reserved for backwards compatibility with 'system_resume'
709          * events */
710         KBASE_TIMELINE_PM_EVENT_RESERVED_6,
711
712         /** The job scheduler is requesting to power up/down cores.
713          *
714          * This event is sent when:
715          * - powered down cores are needed to complete a job
716          * - powered up cores are not needed anymore
717          */
718         KBASE_TIMELINE_PM_EVENT_CHANGE_GPU_STATE,
719
720         KBASEP_TIMELINE_PM_EVENT_LAST = KBASE_TIMELINE_PM_EVENT_CHANGE_GPU_STATE,
721 };
722
723 #ifdef CONFIG_MALI_TRACE_TIMELINE
724 struct kbase_trace_kctx_timeline {
725         atomic_t jd_atoms_in_flight;
726         u32 owner_tgid;
727 };
728
729 struct kbase_trace_kbdev_timeline {
730         /* Note: strictly speaking, not needed, because it's in sync with
731          * kbase_device::jm_slots[]::submitted_nr
732          *
733          * But it's kept as an example of how to add global timeline tracking
734          * information
735          *
736          * The caller must hold kbasep_js_device_data::runpool_irq::lock when
737          * accessing this */
738         u8 slot_atoms_submitted[BASE_JM_MAX_NR_SLOTS];
739
740         /* Last UID for each PM event */
741         atomic_t pm_event_uid[KBASEP_TIMELINE_PM_EVENT_LAST+1];
742         /* Counter for generating PM event UIDs */
743         atomic_t pm_event_uid_counter;
744         /*
745          * L2 transition state - true indicates that the transition is ongoing
746          * Expected to be protected by pm.power_change_lock */
747         bool l2_transitioning;
748 };
749 #endif /* CONFIG_MALI_TRACE_TIMELINE */
750
751
752 struct kbasep_kctx_list_element {
753         struct list_head link;
754         struct kbase_context *kctx;
755 };
756
757 /**
758  * Data stored per device for power management.
759  *
760  * This structure contains data for the power management framework. There is one
761  * instance of this structure per device in the system.
762  */
763 struct kbase_pm_device_data {
764         /**
765          * The lock protecting Power Management structures accessed outside of
766          * IRQ.
767          *
768          * This lock must also be held whenever the GPU is being powered on or
769          * off.
770          */
771         struct mutex lock;
772
773         /** The reference count of active contexts on this device. */
774         int active_count;
775         /** Flag indicating suspending/suspended */
776         bool suspending;
777         /* Wait queue set when active_count == 0 */
778         wait_queue_head_t zero_active_count_wait;
779
780         /**
781          * Bit masks identifying the available shader cores that are specified
782          * via sysfs. One mask per job slot.
783          */
784         u64 debug_core_mask[BASE_JM_MAX_NR_SLOTS];
785         u64 debug_core_mask_all;
786
787         /**
788          * Lock protecting the power state of the device.
789          *
790          * This lock must be held when accessing the shader_available_bitmap,
791          * tiler_available_bitmap, l2_available_bitmap, shader_inuse_bitmap and
792          * tiler_inuse_bitmap fields of kbase_device, and the ca_in_transition
793          * and shader_poweroff_pending fields of kbase_pm_device_data. It is
794          * also held when the hardware power registers are being written to, to
795          * ensure that two threads do not conflict over the power transitions
796          * that the hardware should make.
797          */
798         spinlock_t power_change_lock;
799
800         /**
801          * Callback for initializing the runtime power management.
802          *
803          * @param kbdev The kbase device
804          *
805          * @return 0 on success, else error code
806          */
807          int (*callback_power_runtime_init)(struct kbase_device *kbdev);
808
809         /**
810          * Callback for terminating the runtime power management.
811          *
812          * @param kbdev The kbase device
813          */
814         void (*callback_power_runtime_term)(struct kbase_device *kbdev);
815
816         /* Time in milliseconds between each dvfs sample */
817         u32 dvfs_period;
818
819         /* Period of GPU poweroff timer */
820         ktime_t gpu_poweroff_time;
821
822         /* Number of ticks of GPU poweroff timer before shader is powered off */
823         int poweroff_shader_ticks;
824
825         /* Number of ticks of GPU poweroff timer before GPU is powered off */
826         int poweroff_gpu_ticks;
827
828         struct kbase_pm_backend_data backend;
829 };
830
831 /**
832  * struct kbase_protected_ops - Platform specific functions for GPU protected
833  * mode operations
834  * @protected_mode_enter: Callback to enter protected mode on the GPU
835  * @protected_mode_reset: Callback to reset the GPU and exit protected mode.
836  * @protected_mode_supported: Callback to check if protected mode is supported.
837  */
838 struct kbase_protected_ops {
839         /**
840          * protected_mode_enter() - Enter protected mode on the GPU
841          * @kbdev:      The kbase device
842          *
843          * Return: 0 on success, non-zero on error
844          */
845         int (*protected_mode_enter)(struct kbase_device *kbdev);
846
847         /**
848          * protected_mode_reset() - Reset the GPU and exit protected mode
849          * @kbdev:      The kbase device
850          *
851          * Return: 0 on success, non-zero on error
852          */
853         int (*protected_mode_reset)(struct kbase_device *kbdev);
854
855         /**
856          * protected_mode_supported() - Check if protected mode is supported
857          * @kbdev:      The kbase device
858          *
859          * Return: 0 on success, non-zero on error
860          */
861         bool (*protected_mode_supported)(struct kbase_device *kbdev);
862 };
863
864
865 /**
866  * struct kbase_mem_pool - Page based memory pool for kctx/kbdev
867  * @kbdev:     Kbase device where memory is used
868  * @cur_size:  Number of free pages currently in the pool (may exceed @max_size
869  *             in some corner cases)
870  * @max_size:  Maximum number of free pages in the pool
871  * @pool_lock: Lock protecting the pool - must be held when modifying @cur_size
872  *             and @page_list
873  * @page_list: List of free pages in the pool
874  * @reclaim:   Shrinker for kernel reclaim of free pages
875  * @next_pool: Pointer to next pool where pages can be allocated when this pool
876  *             is empty. Pages will spill over to the next pool when this pool
877  *             is full. Can be NULL if there is no next pool.
878  */
879 struct kbase_mem_pool {
880         struct kbase_device *kbdev;
881         size_t              cur_size;
882         size_t              max_size;
883         spinlock_t          pool_lock;
884         struct list_head    page_list;
885         struct shrinker     reclaim;
886
887         struct kbase_mem_pool *next_pool;
888 };
889
890
891 #define DEVNAME_SIZE    16
892
893 struct kbase_device {
894         s8 slot_submit_count_irq[BASE_JM_MAX_NR_SLOTS];
895
896         u32 hw_quirks_sc;
897         u32 hw_quirks_tiler;
898         u32 hw_quirks_mmu;
899         u32 hw_quirks_jm;
900
901         struct list_head entry;
902         struct device *dev;
903         unsigned int kbase_group_error;
904         struct miscdevice mdev;
905         u64 reg_start;
906         size_t reg_size;
907         void __iomem *reg;
908
909         struct {
910                 int irq;
911                 int flags;
912         } irqs[3];
913
914         struct clk *clock;
915 #ifdef CONFIG_REGULATOR
916         struct regulator *regulator;
917 #endif
918         char devname[DEVNAME_SIZE];
919
920 #ifdef CONFIG_MALI_NO_MALI
921         void *model;
922         struct kmem_cache *irq_slab;
923         struct workqueue_struct *irq_workq;
924         atomic_t serving_job_irq;
925         atomic_t serving_gpu_irq;
926         atomic_t serving_mmu_irq;
927         spinlock_t reg_op_lock;
928 #endif  /* CONFIG_MALI_NO_MALI */
929
930         struct kbase_pm_device_data pm;
931         struct kbasep_js_device_data js_data;
932         struct kbase_mem_pool mem_pool;
933         struct kbasep_mem_device memdev;
934         struct kbase_mmu_mode const *mmu_mode;
935
936         struct kbase_as as[BASE_MAX_NR_AS];
937
938         spinlock_t mmu_mask_change;
939
940         struct kbase_gpu_props gpu_props;
941
942         /** List of SW workarounds for HW issues */
943         unsigned long hw_issues_mask[(BASE_HW_ISSUE_END + BITS_PER_LONG - 1) / BITS_PER_LONG];
944         /** List of features available */
945         unsigned long hw_features_mask[(BASE_HW_FEATURE_END + BITS_PER_LONG - 1) / BITS_PER_LONG];
946
947         /* Bitmaps of cores that are currently in use (running jobs).
948          * These should be kept up to date by the job scheduler.
949          *
950          * pm.power_change_lock should be held when accessing these members.
951          *
952          * kbase_pm_check_transitions_nolock() should be called when bits are
953          * cleared to update the power management system and allow transitions to
954          * occur. */
955         u64 shader_inuse_bitmap;
956
957         /* Refcount for cores in use */
958         u32 shader_inuse_cnt[64];
959
960         /* Bitmaps of cores the JS needs for jobs ready to run */
961         u64 shader_needed_bitmap;
962
963         /* Refcount for cores needed */
964         u32 shader_needed_cnt[64];
965
966         u32 tiler_inuse_cnt;
967
968         u32 tiler_needed_cnt;
969
970         /* struct for keeping track of the disjoint information
971          *
972          * The state  is > 0 if the GPU is in a disjoint state. Otherwise 0
973          * The count is the number of disjoint events that have occurred on the GPU
974          */
975         struct {
976                 atomic_t count;
977                 atomic_t state;
978         } disjoint_event;
979
980         /* Refcount for tracking users of the l2 cache, e.g. when using hardware counter instrumentation. */
981         u32 l2_users_count;
982
983         /* Bitmaps of cores that are currently available (powered up and the power policy is happy for jobs to be
984          * submitted to these cores. These are updated by the power management code. The job scheduler should avoid
985          * submitting new jobs to any cores that are not marked as available.
986          *
987          * pm.power_change_lock should be held when accessing these members.
988          */
989         u64 shader_available_bitmap;
990         u64 tiler_available_bitmap;
991         u64 l2_available_bitmap;
992
993         u64 shader_ready_bitmap;
994         u64 shader_transitioning_bitmap;
995
996         s8 nr_hw_address_spaces;                          /**< Number of address spaces in the GPU (constant after driver initialisation) */
997         s8 nr_user_address_spaces;                        /**< Number of address spaces available to user contexts */
998
999         /* Structure used for instrumentation and HW counters dumping */
1000         struct kbase_hwcnt {
1001                 /* The lock should be used when accessing any of the following members */
1002                 spinlock_t lock;
1003
1004                 struct kbase_context *kctx;
1005                 u64 addr;
1006
1007                 struct kbase_instr_backend backend;
1008         } hwcnt;
1009
1010         struct kbase_vinstr_context *vinstr_ctx;
1011
1012         /*value to be written to the irq_throttle register each time an irq is served */
1013         atomic_t irq_throttle_cycles;
1014
1015 #if KBASE_TRACE_ENABLE
1016         spinlock_t              trace_lock;
1017         u16                     trace_first_out;
1018         u16                     trace_next_in;
1019         struct kbase_trace            *trace_rbuf;
1020 #endif
1021
1022         u32 reset_timeout_ms;
1023
1024         struct mutex cacheclean_lock;
1025
1026         /* Platform specific private data to be accessed by mali_kbase_config_xxx.c only */
1027         void *platform_context;
1028
1029         /* List of kbase_contexts created */
1030         struct list_head        kctx_list;
1031         struct mutex            kctx_list_lock;
1032
1033 #ifdef CONFIG_PM_DEVFREQ
1034         struct devfreq_dev_profile devfreq_profile;
1035         struct devfreq *devfreq;
1036         unsigned long current_freq;
1037         unsigned long current_voltage;
1038 #ifdef CONFIG_DEVFREQ_THERMAL
1039 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
1040         struct devfreq_cooling_device *devfreq_cooling;
1041 #else
1042         struct thermal_cooling_device *devfreq_cooling;
1043 #endif
1044 #endif
1045 #endif
1046
1047         struct kbase_ipa_context *ipa_ctx;
1048
1049 #ifdef CONFIG_MALI_TRACE_TIMELINE
1050         struct kbase_trace_kbdev_timeline timeline;
1051 #endif
1052
1053         /*
1054          * Control for enabling job dump on failure, set when control debugfs
1055          * is opened.
1056          */
1057         bool job_fault_debug;
1058
1059 #ifdef CONFIG_DEBUG_FS
1060         /* directory for debugfs entries */
1061         struct dentry *mali_debugfs_directory;
1062         /* Root directory for per context entry */
1063         struct dentry *debugfs_ctx_directory;
1064
1065 #ifdef CONFIG_MALI_DEBUG
1066         /* bit for each as, set if there is new data to report */
1067         u64 debugfs_as_read_bitmap;
1068 #endif /* CONFIG_MALI_DEBUG */
1069
1070         /* failed job dump, used for separate debug process */
1071         wait_queue_head_t job_fault_wq;
1072         wait_queue_head_t job_fault_resume_wq;
1073         struct workqueue_struct *job_fault_resume_workq;
1074         struct list_head job_fault_event_list;
1075         spinlock_t job_fault_event_lock;
1076         struct kbase_context *kctx_fault;
1077
1078 #if !MALI_CUSTOMER_RELEASE
1079         /* Per-device data for register dumping interface */
1080         struct {
1081                 u16 reg_offset; /* Offset of a GPU_CONTROL register to be
1082                                    dumped upon request */
1083         } regs_dump_debugfs_data;
1084 #endif /* !MALI_CUSTOMER_RELEASE */
1085 #endif /* CONFIG_DEBUG_FS */
1086
1087         /* fbdump profiling controls set by gator */
1088         u32 kbase_profiling_controls[FBDUMP_CONTROL_MAX];
1089
1090
1091 #if MALI_CUSTOMER_RELEASE == 0
1092         /* Number of jobs that are run before a job is forced to fail and
1093          * replay. May be KBASEP_FORCE_REPLAY_DISABLED, to disable forced
1094          * failures. */
1095         int force_replay_limit;
1096         /* Count of jobs between forced failures. Incremented on each job. A
1097          * job is forced to fail once this is greater than or equal to
1098          * force_replay_limit. */
1099         int force_replay_count;
1100         /* Core requirement for jobs to be failed and replayed. May be zero. */
1101         base_jd_core_req force_replay_core_req;
1102         /* true if force_replay_limit should be randomized. The random
1103          * value will be in the range of 1 - KBASEP_FORCE_REPLAY_RANDOM_LIMIT.
1104          */
1105         bool force_replay_random;
1106 #endif
1107
1108         /* Total number of created contexts */
1109         atomic_t ctx_num;
1110
1111         struct kbase_hwaccess_data hwaccess;
1112
1113         /* Count of page/bus faults waiting for workqueues to process */
1114         atomic_t faults_pending;
1115
1116         /* true if GPU is powered off or power off operation is in progress */
1117         bool poweroff_pending;
1118
1119
1120         /* defaults for new context created for this device */
1121 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1122         bool infinite_cache_active_default;
1123 #else
1124         u32 infinite_cache_active_default;
1125 #endif
1126         size_t mem_pool_max_size_default;
1127
1128         /* system coherency mode  */
1129         u32 system_coherency;
1130         /* Flag to track when cci snoops have been enabled on the interface */
1131         bool cci_snoop_enabled;
1132
1133         /* SMC function IDs to call into Trusted firmware to enable/disable
1134          * cache snooping. Value of 0 indicates that they are not used
1135          */
1136         u32 snoop_enable_smc;
1137         u32 snoop_disable_smc;
1138
1139         /* Protected operations */
1140         struct kbase_protected_ops *protected_ops;
1141
1142         /*
1143          * true when GPU is put into protected mode
1144          */
1145         bool protected_mode;
1146
1147         /*
1148          * true when GPU is transitioning into or out of protected mode
1149          */
1150         bool protected_mode_transition;
1151
1152         /*
1153          * true if protected mode is supported
1154          */
1155         bool protected_mode_support;
1156
1157
1158 #ifdef CONFIG_MALI_DEBUG
1159         wait_queue_head_t driver_inactive_wait;
1160         bool driver_inactive;
1161 #endif /* CONFIG_MALI_DEBUG */
1162
1163 #ifdef CONFIG_MALI_FPGA_BUS_LOGGER
1164         /*
1165          * Bus logger integration.
1166          */
1167         struct bus_logger_client *buslogger;
1168 #endif
1169         /* Boolean indicating if an IRQ flush during reset is in progress. */
1170         bool irq_reset_flush;
1171
1172         /* list of inited sub systems. Used during terminate/error recovery */
1173         u32 inited_subsys;
1174 };
1175
1176 /**
1177  * struct jsctx_queue - JS context atom queue
1178  * @runnable_tree: Root of RB-tree containing currently runnable atoms on this
1179  *                 job slot.
1180  * @x_dep_head:    Head item of the linked list of atoms blocked on cross-slot
1181  *                 dependencies. Atoms on this list will be moved to the
1182  *                 runnable_tree when the blocking atom completes.
1183  *
1184  * runpool_irq.lock must be held when accessing this structure.
1185  */
1186 struct jsctx_queue {
1187         struct rb_root runnable_tree;
1188         struct list_head x_dep_head;
1189 };
1190
1191
1192 #define KBASE_API_VERSION(major, minor) ((((major) & 0xFFF) << 20)  | \
1193                                          (((minor) & 0xFFF) << 8) | \
1194                                          ((0 & 0xFF) << 0))
1195
1196 struct kbase_context {
1197         struct file *filp;
1198         struct kbase_device *kbdev;
1199         int id; /* System wide unique id */
1200         unsigned long api_version;
1201         phys_addr_t pgd;
1202         struct list_head event_list;
1203         struct list_head event_coalesce_list;
1204         struct mutex event_mutex;
1205         atomic_t event_closed;
1206         struct workqueue_struct *event_workq;
1207         atomic_t event_count;
1208         int event_coalesce_count;
1209
1210         bool is_compat;
1211
1212         atomic_t                setup_complete;
1213         atomic_t                setup_in_progress;
1214
1215         u64 *mmu_teardown_pages;
1216
1217         struct page *aliasing_sink_page;
1218
1219         struct mutex            mmu_lock;
1220         struct mutex            reg_lock; /* To be converted to a rwlock? */
1221         struct rb_root          reg_rbtree; /* Red-Black tree of GPU regions (live regions) */
1222
1223         unsigned long    cookies;
1224         struct kbase_va_region *pending_regions[BITS_PER_LONG];
1225
1226         wait_queue_head_t event_queue;
1227         pid_t tgid;
1228         pid_t pid;
1229
1230         struct kbase_jd_context jctx;
1231         atomic_t used_pages;
1232         atomic_t         nonmapped_pages;
1233
1234         struct kbase_mem_pool mem_pool;
1235
1236         struct shrinker         reclaim;
1237         struct list_head        evict_list;
1238         struct mutex            evict_lock;
1239
1240         struct list_head waiting_soft_jobs;
1241         spinlock_t waiting_soft_jobs_lock;
1242 #ifdef CONFIG_KDS
1243         struct list_head waiting_kds_resource;
1244 #endif
1245 #ifdef CONFIG_MALI_DMA_FENCE
1246         struct {
1247                 struct list_head waiting_resource;
1248                 struct workqueue_struct *wq;
1249         } dma_fence;
1250 #endif /* CONFIG_MALI_DMA_FENCE */
1251         /** This is effectively part of the Run Pool, because it only has a valid
1252          * setting (!=KBASEP_AS_NR_INVALID) whilst the context is scheduled in
1253          *
1254          * The kbasep_js_device_data::runpool_irq::lock must be held whilst accessing
1255          * this.
1256          *
1257          * If the context relating to this as_nr is required, you must use
1258          * kbasep_js_runpool_retain_ctx() to ensure that the context doesn't disappear
1259          * whilst you're using it. Alternatively, just hold the kbasep_js_device_data::runpool_irq::lock
1260          * to ensure the context doesn't disappear (but this has restrictions on what other locks
1261          * you can take whilst doing this) */
1262         int as_nr;
1263
1264         /* NOTE:
1265          *
1266          * Flags are in jctx.sched_info.ctx.flags
1267          * Mutable flags *must* be accessed under jctx.sched_info.ctx.jsctx_mutex
1268          *
1269          * All other flags must be added there */
1270         spinlock_t         mm_update_lock;
1271         struct mm_struct *process_mm;
1272         /* End of the SAME_VA zone */
1273         u64 same_va_end;
1274
1275 #ifdef CONFIG_MALI_TRACE_TIMELINE
1276         struct kbase_trace_kctx_timeline timeline;
1277 #endif
1278 #ifdef CONFIG_DEBUG_FS
1279         /* Content of mem_profile file */
1280         char *mem_profile_data;
1281         /* Size of @c mem_profile_data */
1282         size_t mem_profile_size;
1283         /* Mutex guarding memory profile state */
1284         struct mutex mem_profile_lock;
1285         /* Memory profile file created */
1286         bool mem_profile_initialized;
1287         struct dentry *kctx_dentry;
1288
1289         /* for job fault debug */
1290         unsigned int *reg_dump;
1291         atomic_t job_fault_count;
1292         /* This list will keep the following atoms during the dump
1293          * in the same context
1294          */
1295         struct list_head job_fault_resume_event_list;
1296
1297 #endif /* CONFIG_DEBUG_FS */
1298
1299         struct jsctx_queue jsctx_queue
1300                 [KBASE_JS_ATOM_SCHED_PRIO_COUNT][BASE_JM_MAX_NR_SLOTS];
1301
1302         /* Number of atoms currently pulled from this context */
1303         atomic_t atoms_pulled;
1304         /* Number of atoms currently pulled from this context, per slot */
1305         atomic_t atoms_pulled_slot[BASE_JM_MAX_NR_SLOTS];
1306         /* true if last kick() caused atoms to be pulled from this context */
1307         bool pulled;
1308         /* true if infinite cache is to be enabled for new allocations. Existing
1309          * allocations will not change. bool stored as a u32 per Linux API */
1310 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1311         bool infinite_cache_active;
1312 #else
1313         u32 infinite_cache_active;
1314 #endif
1315         /* Bitmask of slots that can be pulled from */
1316         u32 slots_pullable;
1317
1318         /* Backend specific data */
1319         struct kbase_context_backend backend;
1320
1321         /* Work structure used for deferred ASID assignment */
1322         struct work_struct work;
1323
1324         /* Only one userspace vinstr client per kbase context */
1325         struct kbase_vinstr_client *vinstr_cli;
1326         struct mutex vinstr_cli_lock;
1327
1328         /* Must hold queue_mutex when accessing */
1329         bool ctx_active;
1330
1331         /* List of completed jobs waiting for events to be posted */
1332         struct list_head completed_jobs;
1333         /* Number of work items currently pending on job_done_wq */
1334         atomic_t work_count;
1335
1336         /* true if context is counted in kbdev->js_data.nr_contexts_runnable */
1337         bool ctx_runnable_ref;
1338
1339         /* Waiting soft-jobs will fail when this timer expires */
1340         struct timer_list soft_job_timeout;
1341
1342         /* JIT allocation management */
1343         struct kbase_va_region *jit_alloc[256];
1344         struct list_head jit_active_head;
1345         struct list_head jit_pool_head;
1346         struct list_head jit_destroy_head;
1347         struct mutex jit_lock;
1348         struct work_struct jit_work;
1349
1350         /* External sticky resource management */
1351         struct list_head ext_res_meta_head;
1352
1353         /* Used to record that a drain was requested from atomic context */
1354         atomic_t drain_pending;
1355
1356         /* Current age count, used to determine age for newly submitted atoms */
1357         u32 age_count;
1358 };
1359
1360 /**
1361  * struct kbase_ctx_ext_res_meta - Structure which binds an external resource
1362  *                                 to a @kbase_context.
1363  * @ext_res_node:                  List head for adding the metadata to a
1364  *                                 @kbase_context.
1365  * @alloc:                         The physical memory allocation structure
1366  *                                 which is mapped.
1367  * @gpu_addr:                      The GPU virtual address the resource is
1368  *                                 mapped to.
1369  *
1370  * External resources can be mapped into multiple contexts as well as the same
1371  * context multiple times.
1372  * As kbase_va_region itself isn't refcounted we can't attach our extra
1373  * information to it as it could be removed under our feet leaving external
1374  * resources pinned.
1375  * This metadata structure binds a single external resource to a single
1376  * context, ensuring that per context mapping is tracked separately so it can
1377  * be overridden when needed and abuses by the application (freeing the resource
1378  * multiple times) don't effect the refcount of the physical allocation.
1379  */
1380 struct kbase_ctx_ext_res_meta {
1381         struct list_head ext_res_node;
1382         struct kbase_mem_phy_alloc *alloc;
1383         u64 gpu_addr;
1384 };
1385
1386 enum kbase_reg_access_type {
1387         REG_READ,
1388         REG_WRITE
1389 };
1390
1391 enum kbase_share_attr_bits {
1392         /* (1ULL << 8) bit is reserved */
1393         SHARE_BOTH_BITS = (2ULL << 8),  /* inner and outer shareable coherency */
1394         SHARE_INNER_BITS = (3ULL << 8)  /* inner shareable coherency */
1395 };
1396
1397 /**
1398  * kbase_device_is_cpu_coherent - Returns if the device is CPU coherent.
1399  * @kbdev: kbase device
1400  *
1401  * Return: true if the device access are coherent, false if not.
1402  */
1403 static inline bool kbase_device_is_cpu_coherent(struct kbase_device *kbdev)
1404 {
1405         if ((kbdev->system_coherency == COHERENCY_ACE_LITE) ||
1406                         (kbdev->system_coherency == COHERENCY_ACE))
1407                 return true;
1408
1409         return false;
1410 }
1411
1412 /* Conversion helpers for setting up high resolution timers */
1413 #define HR_TIMER_DELAY_MSEC(x) (ns_to_ktime(((u64)(x))*1000000U))
1414 #define HR_TIMER_DELAY_NSEC(x) (ns_to_ktime(x))
1415
1416 /* Maximum number of loops polling the GPU for a cache flush before we assume it must have completed */
1417 #define KBASE_CLEAN_CACHE_MAX_LOOPS     100000
1418 /* Maximum number of loops polling the GPU for an AS command to complete before we assume the GPU has hung */
1419 #define KBASE_AS_INACTIVE_MAX_LOOPS     100000
1420
1421 /* Maximum number of times a job can be replayed */
1422 #define BASEP_JD_REPLAY_LIMIT 15
1423
1424 /* JobDescriptorHeader - taken from the architecture specifications, the layout
1425  * is currently identical for all GPU archs. */
1426 struct job_descriptor_header {
1427         u32 exception_status;
1428         u32 first_incomplete_task;
1429         u64 fault_pointer;
1430         u8 job_descriptor_size : 1;
1431         u8 job_type : 7;
1432         u8 job_barrier : 1;
1433         u8 _reserved_01 : 1;
1434         u8 _reserved_1 : 1;
1435         u8 _reserved_02 : 1;
1436         u8 _reserved_03 : 1;
1437         u8 _reserved_2 : 1;
1438         u8 _reserved_04 : 1;
1439         u8 _reserved_05 : 1;
1440         u16 job_index;
1441         u16 job_dependency_index_1;
1442         u16 job_dependency_index_2;
1443         union {
1444                 u64 _64;
1445                 u32 _32;
1446         } next_job;
1447 };
1448
1449 #endif                          /* _KBASE_DEFS_H_ */