Merge tag 'v4.4-rc8'
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_base_kernel.h
1 /*
2  *
3  * (C) COPYRIGHT 2010-2015 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
22  * Base structures shared with the kernel.
23  */
24
25 #ifndef _BASE_KERNEL_H_
26 #define _BASE_KERNEL_H_
27
28 #ifndef __user
29 #define __user
30 #endif
31
32 /* Support UK6 IOCTLS */
33 #define BASE_LEGACY_UK6_SUPPORT 1
34
35 /* Support UK7 IOCTLS */
36 /* NB: To support UK6 we also need to support UK7 */
37 #define BASE_LEGACY_UK7_SUPPORT 1
38
39 typedef u64 base_mem_handle;
40
41 #include "mali_base_mem_priv.h"
42 #include "mali_kbase_profiling_gator_api.h"
43
44 /*
45  * Dependency stuff, keep it private for now. May want to expose it if
46  * we decide to make the number of semaphores a configurable
47  * option.
48  */
49 #define BASE_JD_ATOM_COUNT              256
50
51 #define BASEP_JD_SEM_PER_WORD_LOG2      5
52 #define BASEP_JD_SEM_PER_WORD           (1 << BASEP_JD_SEM_PER_WORD_LOG2)
53 #define BASEP_JD_SEM_WORD_NR(x)         ((x) >> BASEP_JD_SEM_PER_WORD_LOG2)
54 #define BASEP_JD_SEM_MASK_IN_WORD(x)    (1 << ((x) & (BASEP_JD_SEM_PER_WORD - 1)))
55 #define BASEP_JD_SEM_ARRAY_SIZE         BASEP_JD_SEM_WORD_NR(BASE_JD_ATOM_COUNT)
56
57 #define BASE_GPU_NUM_TEXTURE_FEATURES_REGISTERS 3
58
59 #define BASE_MAX_COHERENT_GROUPS 16
60
61 #if defined CDBG_ASSERT
62 #define LOCAL_ASSERT CDBG_ASSERT
63 #elif defined KBASE_DEBUG_ASSERT
64 #define LOCAL_ASSERT KBASE_DEBUG_ASSERT
65 #else
66 #error assert macro not defined!
67 #endif
68
69 #if defined PAGE_MASK
70 #define LOCAL_PAGE_LSB ~PAGE_MASK
71 #else
72 #include <osu/mali_osu.h>
73
74 #if defined OSU_CONFIG_CPU_PAGE_SIZE_LOG2
75 #define LOCAL_PAGE_LSB ((1ul << OSU_CONFIG_CPU_PAGE_SIZE_LOG2) - 1)
76 #else
77 #error Failed to find page size
78 #endif
79 #endif
80
81 /** 32/64-bit neutral way to represent pointers */
82 typedef union kbase_pointer {
83         void __user *value;       /**< client should store their pointers here */
84         u32 compat_value; /**< 64-bit kernels should fetch value here when handling 32-bit clients */
85         u64 sizer;        /**< Force 64-bit storage for all clients regardless */
86 } kbase_pointer;
87
88 /**
89  * @addtogroup base_user_api User-side Base APIs
90  * @{
91  */
92
93 /**
94  * @addtogroup base_user_api_memory User-side Base Memory APIs
95  * @{
96  */
97
98 /**
99  * @brief Memory allocation, access/hint flags
100  *
101  * A combination of MEM_PROT/MEM_HINT flags must be passed to each allocator
102  * in order to determine the best cache policy. Some combinations are
103  * of course invalid (eg @c MEM_PROT_CPU_WR | @c MEM_HINT_CPU_RD),
104  * which defines a @a write-only region on the CPU side, which is
105  * heavily read by the CPU...
106  * Other flags are only meaningful to a particular allocator.
107  * More flags can be added to this list, as long as they don't clash
108  * (see ::BASE_MEM_FLAGS_NR_BITS for the number of the first free bit).
109  */
110 typedef u32 base_mem_alloc_flags;
111
112 /**
113  * @brief Memory allocation, access/hint flags
114  *
115  * See ::base_mem_alloc_flags.
116  *
117  */
118 enum {
119 /* IN */
120         BASE_MEM_PROT_CPU_RD = (1U << 0),      /**< Read access CPU side */
121         BASE_MEM_PROT_CPU_WR = (1U << 1),      /**< Write access CPU side */
122         BASE_MEM_PROT_GPU_RD = (1U << 2),      /**< Read access GPU side */
123         BASE_MEM_PROT_GPU_WR = (1U << 3),      /**< Write access GPU side */
124         BASE_MEM_PROT_GPU_EX = (1U << 4),      /**< Execute allowed on the GPU
125                                                     side */
126
127         /* BASE_MEM_HINT flags have been removed, but their values are reserved
128          * for backwards compatibility with older user-space drivers. The values
129          * can be re-used once support for r5p0 user-space drivers is removed,
130          * presumably in r7p0.
131          *
132          * RESERVED: (1U << 5)
133          * RESERVED: (1U << 6)
134          * RESERVED: (1U << 7)
135          * RESERVED: (1U << 8)
136          */
137
138         BASE_MEM_GROW_ON_GPF = (1U << 9),      /**< Grow backing store on GPU
139                                                     Page Fault */
140
141         BASE_MEM_COHERENT_SYSTEM = (1U << 10), /**< Page coherence Outer
142                                                     shareable, if available */
143         BASE_MEM_COHERENT_LOCAL = (1U << 11),  /**< Page coherence Inner
144                                                     shareable */
145         BASE_MEM_CACHED_CPU = (1U << 12),      /**< Should be cached on the
146                                                     CPU */
147
148 /* IN/OUT */
149         BASE_MEM_SAME_VA = (1U << 13), /**< Must have same VA on both the GPU
150                                             and the CPU */
151 /* OUT */
152         BASE_MEM_NEED_MMAP = (1U << 14), /**< Must call mmap to aquire a GPU
153                                              address for the alloc */
154
155 /* IN */
156         BASE_MEM_COHERENT_SYSTEM_REQUIRED = (1U << 15), /**< Page coherence
157                                                         Outer shareable, required. */
158         BASE_MEM_SECURE = (1U << 16)           /**< Secure memory */
159
160 };
161
162 /**
163  * @brief Number of bits used as flags for base memory management
164  *
165  * Must be kept in sync with the ::base_mem_alloc_flags flags
166  */
167 #define BASE_MEM_FLAGS_NR_BITS 17
168
169 /**
170   * A mask for all output bits, excluding IN/OUT bits.
171   */
172 #define BASE_MEM_FLAGS_OUTPUT_MASK BASE_MEM_NEED_MMAP
173
174 /**
175   * A mask for all input bits, including IN/OUT bits.
176   */
177 #define BASE_MEM_FLAGS_INPUT_MASK \
178         (((1 << BASE_MEM_FLAGS_NR_BITS) - 1) & ~BASE_MEM_FLAGS_OUTPUT_MASK)
179
180
181 /**
182  * @brief Memory types supported by @a base_mem_import
183  *
184  * Each type defines what the supported handle type is.
185  *
186  * If any new type is added here ARM must be contacted
187  * to allocate a numeric value for it.
188  * Do not just add a new type without synchronizing with ARM
189  * as future releases from ARM might include other new types
190  * which could clash with your custom types.
191  */
192 typedef enum base_mem_import_type {
193         BASE_MEM_IMPORT_TYPE_INVALID = 0,
194         /** UMP import. Handle type is ump_secure_id. */
195         BASE_MEM_IMPORT_TYPE_UMP = 1,
196         /** UMM import. Handle type is a file descriptor (int) */
197         BASE_MEM_IMPORT_TYPE_UMM = 2
198 } base_mem_import_type;
199
200 /* legacy API wrappers */
201 #define base_tmem_import_type          base_mem_import_type
202 #define BASE_TMEM_IMPORT_TYPE_INVALID  BASE_MEM_IMPORT_TYPE_INVALID
203 #define BASE_TMEM_IMPORT_TYPE_UMP      BASE_MEM_IMPORT_TYPE_UMP
204 #define BASE_TMEM_IMPORT_TYPE_UMM      BASE_MEM_IMPORT_TYPE_UMM
205
206 /**
207  * @brief Invalid memory handle type.
208  * Return value from functions returning @a base_mem_handle on error.
209  */
210 #define BASE_MEM_INVALID_HANDLE                (0ull  << 12)
211 #define BASE_MEM_MMU_DUMP_HANDLE               (1ull  << 12)
212 #define BASE_MEM_TRACE_BUFFER_HANDLE           (2ull  << 12)
213 #define BASE_MEM_MAP_TRACKING_HANDLE           (3ull  << 12)
214 #define BASE_MEM_WRITE_ALLOC_PAGES_HANDLE      (4ull  << 12)
215 /* reserved handles ..-64<<PAGE_SHIFT> for future special handles */
216 #define BASE_MEM_COOKIE_BASE                   (64ul  << 12)
217 #define BASE_MEM_FIRST_FREE_ADDRESS            ((BITS_PER_LONG << 12) + \
218                                                 BASE_MEM_COOKIE_BASE)
219
220 /* Bit mask of cookies used for for memory allocation setup */
221 #define KBASE_COOKIE_MASK  ~1UL /* bit 0 is reserved */
222
223
224 /**
225  * @brief Result codes of changing the size of the backing store allocated to a tmem region
226  */
227 typedef enum base_backing_threshold_status {
228         BASE_BACKING_THRESHOLD_OK = 0,                      /**< Resize successful */
229         BASE_BACKING_THRESHOLD_ERROR_NOT_GROWABLE = -1,     /**< Not a growable tmem object */
230         BASE_BACKING_THRESHOLD_ERROR_OOM = -2,              /**< Increase failed due to an out-of-memory condition */
231         BASE_BACKING_THRESHOLD_ERROR_MAPPED = -3,           /**< Resize attempted on buffer while it was mapped, which is not permitted */
232         BASE_BACKING_THRESHOLD_ERROR_INVALID_ARGUMENTS = -4 /**< Invalid arguments (not tmem, illegal size request, etc.) */
233 } base_backing_threshold_status;
234
235 /**
236  * @addtogroup base_user_api_memory_defered User-side Base Defered Memory Coherency APIs
237  * @{
238  */
239
240 /**
241  * @brief a basic memory operation (sync-set).
242  *
243  * The content of this structure is private, and should only be used
244  * by the accessors.
245  */
246 typedef struct base_syncset {
247         struct basep_syncset basep_sset;
248 } base_syncset;
249
250 /** @} end group base_user_api_memory_defered */
251
252 /**
253  * Handle to represent imported memory object.
254  * Simple opague handle to imported memory, can't be used
255  * with anything but base_external_resource_init to bind to an atom.
256  */
257 typedef struct base_import_handle {
258         struct {
259                 u64 handle;
260         } basep;
261 } base_import_handle;
262
263 /** @} end group base_user_api_memory */
264
265 /**
266  * @addtogroup base_user_api_job_dispatch User-side Base Job Dispatcher APIs
267  * @{
268  */
269
270 typedef int platform_fence_type;
271 #define INVALID_PLATFORM_FENCE ((platform_fence_type)-1)
272
273 /**
274  * Base stream handle.
275  *
276  * References an underlying base stream object.
277  */
278 typedef struct base_stream {
279         struct {
280                 int fd;
281         } basep;
282 } base_stream;
283
284 /**
285  * Base fence handle.
286  *
287  * References an underlying base fence object.
288  */
289 typedef struct base_fence {
290         struct {
291                 int fd;
292                 int stream_fd;
293         } basep;
294 } base_fence;
295
296 /**
297  * @brief Per-job data
298  *
299  * This structure is used to store per-job data, and is completly unused
300  * by the Base driver. It can be used to store things such as callback
301  * function pointer, data to handle job completion. It is guaranteed to be
302  * untouched by the Base driver.
303  */
304 typedef struct base_jd_udata {
305         u64 blob[2];     /**< per-job data array */
306 } base_jd_udata;
307
308 /**
309  * @brief Memory aliasing info
310  *
311  * Describes a memory handle to be aliased.
312  * A subset of the handle can be chosen for aliasing, given an offset and a
313  * length.
314  * A special handle BASE_MEM_WRITE_ALLOC_PAGES_HANDLE is used to represent a
315  * region where a special page is mapped with a write-alloc cache setup,
316  * typically used when the write result of the GPU isn't needed, but the GPU
317  * must write anyway.
318  *
319  * Offset and length are specified in pages.
320  * Offset must be within the size of the handle.
321  * Offset+length must not overrun the size of the handle.
322  *
323  * @handle Handle to alias, can be BASE_MEM_WRITE_ALLOC_PAGES_HANDLE
324  * @offset Offset within the handle to start aliasing from, in pages.
325  *         Not used with BASE_MEM_WRITE_ALLOC_PAGES_HANDLE.
326  * @length Length to alias, in pages. For BASE_MEM_WRITE_ALLOC_PAGES_HANDLE
327  *         specifies the number of times the special page is needed.
328  */
329 struct base_mem_aliasing_info {
330         base_mem_handle handle;
331         u64 offset;
332         u64 length;
333 };
334
335 /**
336  * @brief Job dependency type.
337  *
338  * A flags field will be inserted into the atom structure to specify whether a dependency is a data or
339  * ordering dependency (by putting it before/after 'core_req' in the structure it should be possible to add without
340  * changing the structure size).
341  * When the flag is set for a particular dependency to signal that it is an ordering only dependency then
342  * errors will not be propagated.
343  */
344 typedef u8 base_jd_dep_type;
345
346
347 #define BASE_JD_DEP_TYPE_INVALID  (0)       /**< Invalid dependency */
348 #define BASE_JD_DEP_TYPE_DATA     (1U << 0) /**< Data dependency */
349 #define BASE_JD_DEP_TYPE_ORDER    (1U << 1) /**< Order dependency */
350
351 /**
352  * @brief Job chain hardware requirements.
353  *
354  * A job chain must specify what GPU features it needs to allow the
355  * driver to schedule the job correctly.  By not specifying the
356  * correct settings can/will cause an early job termination.  Multiple
357  * values can be ORed together to specify multiple requirements.
358  * Special case is ::BASE_JD_REQ_DEP, which is used to express complex
359  * dependencies, and that doesn't execute anything on the hardware.
360  */
361 typedef u16 base_jd_core_req;
362
363 /* Requirements that come from the HW */
364 #define BASE_JD_REQ_DEP 0           /**< No requirement, dependency only */
365 #define BASE_JD_REQ_FS  (1U << 0)   /**< Requires fragment shaders */
366 /**
367  * Requires compute shaders
368  * This covers any of the following Midgard Job types:
369  * - Vertex Shader Job
370  * - Geometry Shader Job
371  * - An actual Compute Shader Job
372  *
373  * Compare this with @ref BASE_JD_REQ_ONLY_COMPUTE, which specifies that the
374  * job is specifically just the "Compute Shader" job type, and not the "Vertex
375  * Shader" nor the "Geometry Shader" job type.
376  */
377 #define BASE_JD_REQ_CS  (1U << 1)
378 #define BASE_JD_REQ_T   (1U << 2)   /**< Requires tiling */
379 #define BASE_JD_REQ_CF  (1U << 3)   /**< Requires cache flushes */
380 #define BASE_JD_REQ_V   (1U << 4)   /**< Requires value writeback */
381
382 /* SW-only requirements - the HW does not expose these as part of the job slot capabilities */
383
384 /* Requires fragment job with AFBC encoding */
385 #define BASE_JD_REQ_FS_AFBC  (1U << 13)
386
387 /**
388  * SW Only requirement: the job chain requires a coherent core group. We don't
389  * mind which coherent core group is used.
390  */
391 #define BASE_JD_REQ_COHERENT_GROUP  (1U << 6)
392
393 /**
394  * SW Only requirement: The performance counters should be enabled only when
395  * they are needed, to reduce power consumption.
396  */
397
398 #define BASE_JD_REQ_PERMON               (1U << 7)
399
400 /**
401  * SW Only requirement: External resources are referenced by this atom.
402  * When external resources are referenced no syncsets can be bundled with the atom
403  * but should instead be part of a NULL jobs inserted into the dependency tree.
404  * The first pre_dep object must be configured for the external resouces to use,
405  * the second pre_dep object can be used to create other dependencies.
406  */
407 #define BASE_JD_REQ_EXTERNAL_RESOURCES   (1U << 8)
408
409 /**
410  * SW Only requirement: Software defined job. Jobs with this bit set will not be submitted
411  * to the hardware but will cause some action to happen within the driver
412  */
413 #define BASE_JD_REQ_SOFT_JOB        (1U << 9)
414
415 #define BASE_JD_REQ_SOFT_DUMP_CPU_GPU_TIME      (BASE_JD_REQ_SOFT_JOB | 0x1)
416 #define BASE_JD_REQ_SOFT_FENCE_TRIGGER          (BASE_JD_REQ_SOFT_JOB | 0x2)
417 #define BASE_JD_REQ_SOFT_FENCE_WAIT             (BASE_JD_REQ_SOFT_JOB | 0x3)
418
419 /**
420  * SW Only requirement : Replay job.
421  *
422  * If the preceeding job fails, the replay job will cause the jobs specified in
423  * the list of base_jd_replay_payload pointed to by the jc pointer to be
424  * replayed.
425  *
426  * A replay job will only cause jobs to be replayed up to BASEP_JD_REPLAY_LIMIT
427  * times. If a job fails more than BASEP_JD_REPLAY_LIMIT times then the replay
428  * job is failed, as well as any following dependencies.
429  *
430  * The replayed jobs will require a number of atom IDs. If there are not enough
431  * free atom IDs then the replay job will fail.
432  *
433  * If the preceeding job does not fail, then the replay job is returned as
434  * completed.
435  *
436  * The replayed jobs will never be returned to userspace. The preceeding failed
437  * job will be returned to userspace as failed; the status of this job should
438  * be ignored. Completion should be determined by the status of the replay soft
439  * job.
440  *
441  * In order for the jobs to be replayed, the job headers will have to be
442  * modified. The Status field will be reset to NOT_STARTED. If the Job Type
443  * field indicates a Vertex Shader Job then it will be changed to Null Job.
444  *
445  * The replayed jobs have the following assumptions :
446  *
447  * - No external resources. Any required external resources will be held by the
448  *   replay atom.
449  * - Pre-dependencies are created based on job order.
450  * - Atom numbers are automatically assigned.
451  * - device_nr is set to 0. This is not relevant as
452  *   BASE_JD_REQ_SPECIFIC_COHERENT_GROUP should not be set.
453  * - Priority is inherited from the replay job.
454  */
455 #define BASE_JD_REQ_SOFT_REPLAY                 (BASE_JD_REQ_SOFT_JOB | 0x4)
456
457 /**
458  * HW Requirement: Requires Compute shaders (but not Vertex or Geometry Shaders)
459  *
460  * This indicates that the Job Chain contains Midgard Jobs of the 'Compute Shaders' type.
461  *
462  * In contrast to @ref BASE_JD_REQ_CS, this does \b not indicate that the Job
463  * Chain contains 'Geometry Shader' or 'Vertex Shader' jobs.
464  *
465  * @note This is a more flexible variant of the @ref BASE_CONTEXT_HINT_ONLY_COMPUTE flag,
466  * allowing specific jobs to be marked as 'Only Compute' instead of the entire context
467  */
468 #define BASE_JD_REQ_ONLY_COMPUTE    (1U << 10)
469
470 /**
471  * HW Requirement: Use the base_jd_atom::device_nr field to specify a
472  * particular core group
473  *
474  * If both BASE_JD_REQ_COHERENT_GROUP and this flag are set, this flag takes priority
475  *
476  * This is only guaranteed to work for BASE_JD_REQ_ONLY_COMPUTE atoms.
477  *
478  * If the core availability policy is keeping the required core group turned off, then
479  * the job will fail with a BASE_JD_EVENT_PM_EVENT error code.
480  */
481 #define BASE_JD_REQ_SPECIFIC_COHERENT_GROUP (1U << 11)
482
483 /**
484  * SW Flag: If this bit is set then the successful completion of this atom
485  * will not cause an event to be sent to userspace
486  */
487 #define BASE_JD_REQ_EVENT_ONLY_ON_FAILURE   (1U << 12)
488
489 /**
490  * SW Flag: If this bit is set then completion of this atom will not cause an
491  * event to be sent to userspace, whether successful or not.
492  */
493 #define BASEP_JD_REQ_EVENT_NEVER (1U << 14)
494
495 /**
496 * These requirement bits are currently unused in base_jd_core_req (currently a u16)
497 */
498
499 #define BASEP_JD_REQ_RESERVED_BIT5 (1U << 5)
500 #define BASEP_JD_REQ_RESERVED_BIT15 (1U << 15)
501
502 /**
503 * Mask of all the currently unused requirement bits in base_jd_core_req.
504 */
505
506 #define BASEP_JD_REQ_RESERVED (BASEP_JD_REQ_RESERVED_BIT5 | \
507                                 BASEP_JD_REQ_RESERVED_BIT15)
508
509 /**
510  * Mask of all bits in base_jd_core_req that control the type of the atom.
511  *
512  * This allows dependency only atoms to have flags set
513  */
514 #define BASEP_JD_REQ_ATOM_TYPE (~(BASEP_JD_REQ_RESERVED | BASE_JD_REQ_EVENT_ONLY_ON_FAILURE |\
515                                 BASE_JD_REQ_EXTERNAL_RESOURCES | BASEP_JD_REQ_EVENT_NEVER))
516
517 /**
518  * @brief States to model state machine processed by kbasep_js_job_check_ref_cores(), which
519  * handles retaining cores for power management and affinity management.
520  *
521  * The state @ref KBASE_ATOM_COREREF_STATE_RECHECK_AFFINITY prevents an attack
522  * where lots of atoms could be submitted before powerup, and each has an
523  * affinity chosen that causes other atoms to have an affinity
524  * violation. Whilst the affinity was not causing violations at the time it
525  * was chosen, it could cause violations thereafter. For example, 1000 jobs
526  * could have had their affinity chosen during the powerup time, so any of
527  * those 1000 jobs could cause an affinity violation later on.
528  *
529  * The attack would otherwise occur because other atoms/contexts have to wait for:
530  * -# the currently running atoms (which are causing the violation) to
531  * finish
532  * -# and, the atoms that had their affinity chosen during powerup to
533  * finish. These are run preferrentially because they don't cause a
534  * violation, but instead continue to cause the violation in others.
535  * -# or, the attacker is scheduled out (which might not happen for just 2
536  * contexts)
537  *
538  * By re-choosing the affinity (which is designed to avoid violations at the
539  * time it's chosen), we break condition (2) of the wait, which minimizes the
540  * problem to just waiting for current jobs to finish (which can be bounded if
541  * the Job Scheduling Policy has a timer).
542  */
543 enum kbase_atom_coreref_state {
544         /** Starting state: No affinity chosen, and cores must be requested. kbase_jd_atom::affinity==0 */
545         KBASE_ATOM_COREREF_STATE_NO_CORES_REQUESTED,
546         /** Cores requested, but waiting for them to be powered. Requested cores given by kbase_jd_atom::affinity */
547         KBASE_ATOM_COREREF_STATE_WAITING_FOR_REQUESTED_CORES,
548         /** Cores given by kbase_jd_atom::affinity are powered, but affinity might be out-of-date, so must recheck */
549         KBASE_ATOM_COREREF_STATE_RECHECK_AFFINITY,
550         /** Cores given by kbase_jd_atom::affinity are powered, and affinity is up-to-date, but must check for violations */
551         KBASE_ATOM_COREREF_STATE_CHECK_AFFINITY_VIOLATIONS,
552         /** Cores are powered, kbase_jd_atom::affinity up-to-date, no affinity violations: atom can be submitted to HW */
553         KBASE_ATOM_COREREF_STATE_READY
554 };
555
556 /*
557  * Base Atom priority
558  *
559  * Only certain priority levels are actually implemented, as specified by the
560  * BASE_JD_PRIO_<...> definitions below. It is undefined to use a priority
561  * level that is not one of those defined below.
562  *
563  * Priority levels only affect scheduling between atoms of the same type within
564  * a base context, and only after the atoms have had dependencies resolved.
565  * Fragment atoms does not affect non-frament atoms with lower priorities, and
566  * the other way around. For example, a low priority atom that has had its
567  * dependencies resolved might run before a higher priority atom that has not
568  * had its dependencies resolved.
569  *
570  * The scheduling between base contexts/processes and between atoms from
571  * different base contexts/processes is unaffected by atom priority.
572  *
573  * The atoms are scheduled as follows with respect to their priorities:
574  * - Let atoms 'X' and 'Y' be for the same job slot who have dependencies
575  *   resolved, and atom 'X' has a higher priority than atom 'Y'
576  * - If atom 'Y' is currently running on the HW, then it is interrupted to
577  *   allow atom 'X' to run soon after
578  * - If instead neither atom 'Y' nor atom 'X' are running, then when choosing
579  *   the next atom to run, atom 'X' will always be chosen instead of atom 'Y'
580  * - Any two atoms that have the same priority could run in any order with
581  *   respect to each other. That is, there is no ordering constraint between
582  *   atoms of the same priority.
583  */
584 typedef u8 base_jd_prio;
585
586 /* Medium atom priority. This is a priority higher than BASE_JD_PRIO_LOW */
587 #define BASE_JD_PRIO_MEDIUM  ((base_jd_prio)0)
588 /* High atom priority. This is a priority higher than BASE_JD_PRIO_MEDIUM and
589  * BASE_JD_PRIO_LOW */
590 #define BASE_JD_PRIO_HIGH    ((base_jd_prio)1)
591 /* Low atom priority. */
592 #define BASE_JD_PRIO_LOW     ((base_jd_prio)2)
593
594 /* Count of the number of priority levels. This itself is not a valid
595  * base_jd_prio setting */
596 #define BASE_JD_NR_PRIO_LEVELS 3
597
598 enum kbase_jd_atom_state {
599         /** Atom is not used */
600         KBASE_JD_ATOM_STATE_UNUSED,
601         /** Atom is queued in JD */
602         KBASE_JD_ATOM_STATE_QUEUED,
603         /** Atom has been given to JS (is runnable/running) */
604         KBASE_JD_ATOM_STATE_IN_JS,
605         /** Atom has been completed, but not yet handed back to job dispatcher
606          *  for dependency resolution */
607         KBASE_JD_ATOM_STATE_HW_COMPLETED,
608         /** Atom has been completed, but not yet handed back to userspace */
609         KBASE_JD_ATOM_STATE_COMPLETED
610 };
611
612 typedef u8 base_atom_id; /**< Type big enough to store an atom number in */
613
614 struct base_dependency {
615         base_atom_id  atom_id;               /**< An atom number */
616         base_jd_dep_type dependency_type;    /**< Dependency type */
617 };
618
619 typedef struct base_jd_atom_v2 {
620         u64 jc;                     /**< job-chain GPU address */
621         struct base_jd_udata udata;                 /**< user data */
622         kbase_pointer extres_list;          /**< list of external resources */
623         u16 nr_extres;                      /**< nr of external resources */
624         base_jd_core_req core_req;          /**< core requirements */
625         const struct base_dependency pre_dep[2]; /**< pre-dependencies, one need to use SETTER function to assign this field,
626         this is done in order to reduce possibility of improper assigment of a dependency field */
627         base_atom_id atom_number;           /**< unique number to identify the atom */
628         base_jd_prio prio;                  /**< Atom priority. Refer to @ref base_jd_prio for more details */
629         u8 device_nr;                       /**< coregroup when BASE_JD_REQ_SPECIFIC_COHERENT_GROUP specified */
630         u8 padding[5];
631 } base_jd_atom_v2;
632
633 #ifdef BASE_LEGACY_UK6_SUPPORT
634 struct base_jd_atom_v2_uk6 {
635         u64 jc;                     /**< job-chain GPU address */
636         struct base_jd_udata udata;                 /**< user data */
637         kbase_pointer extres_list;          /**< list of external resources */
638         u16 nr_extres;                      /**< nr of external resources */
639         base_jd_core_req core_req;          /**< core requirements */
640         base_atom_id pre_dep[2]; /**< pre-dependencies */
641         base_atom_id atom_number;           /**< unique number to identify the atom */
642         base_jd_prio prio;                  /**< priority - smaller is higher priority */
643         u8 device_nr;                       /**< coregroup when BASE_JD_REQ_SPECIFIC_COHERENT_GROUP specified */
644         u8 padding[7];
645 };
646 #endif
647
648 typedef enum base_external_resource_access {
649         BASE_EXT_RES_ACCESS_SHARED,
650         BASE_EXT_RES_ACCESS_EXCLUSIVE
651 } base_external_resource_access;
652
653 typedef struct base_external_resource {
654         u64 ext_resource;
655 } base_external_resource;
656
657 /**
658  * @brief Setter for a dependency structure
659  *
660  * @param[in] dep          The kbase jd atom dependency to be initialized.
661  * @param     id           The atom_id to be assigned.
662  * @param     dep_type     The dep_type to be assigned.
663  *
664  */
665 static inline void base_jd_atom_dep_set(const struct base_dependency *const_dep, base_atom_id id, base_jd_dep_type dep_type)
666 {
667         struct base_dependency *dep;
668
669         LOCAL_ASSERT(const_dep != NULL);
670         /* make sure we don't set not allowed combinations of atom_id/dependency_type */
671         LOCAL_ASSERT((id == 0 && dep_type == BASE_JD_DEP_TYPE_INVALID) ||
672                         (id > 0 && dep_type != BASE_JD_DEP_TYPE_INVALID));
673
674         dep = (struct base_dependency *)const_dep;
675
676         dep->atom_id = id;
677         dep->dependency_type = dep_type;
678 }
679
680 /**
681  * @brief Make a copy of a dependency structure
682  *
683  * @param[in,out] dep          The kbase jd atom dependency to be written.
684  * @param[in]     from         The dependency to make a copy from.
685  *
686  */
687 static inline void base_jd_atom_dep_copy(const struct base_dependency *const_dep, const struct base_dependency *from)
688 {
689         LOCAL_ASSERT(const_dep != NULL);
690
691         base_jd_atom_dep_set(const_dep, from->atom_id, from->dependency_type);
692 }
693
694 /**
695  * @brief Soft-atom fence trigger setup.
696  *
697  * Sets up an atom to be a SW-only atom signaling a fence
698  * when it reaches the run state.
699  *
700  * Using the existing base dependency system the fence can
701  * be set to trigger when a GPU job has finished.
702  *
703  * The base fence object must not be terminated until the atom
704  * has been submitted to @a base_jd_submit_bag and @a base_jd_submit_bag has returned.
705  *
706  * @a fence must be a valid fence set up with @a base_fence_init.
707  * Calling this function with a uninitialized fence results in undefined behavior.
708  *
709  * @param[out] atom A pre-allocated atom to configure as a fence trigger SW atom
710  * @param[in] fence The base fence object to trigger.
711  */
712 static inline void base_jd_fence_trigger_setup_v2(struct base_jd_atom_v2 *atom, struct base_fence *fence)
713 {
714         LOCAL_ASSERT(atom);
715         LOCAL_ASSERT(fence);
716         LOCAL_ASSERT(fence->basep.fd == INVALID_PLATFORM_FENCE);
717         LOCAL_ASSERT(fence->basep.stream_fd >= 0);
718         atom->jc = (uintptr_t) fence;
719         atom->core_req = BASE_JD_REQ_SOFT_FENCE_TRIGGER;
720 }
721
722 /**
723  * @brief Soft-atom fence wait setup.
724  *
725  * Sets up an atom to be a SW-only atom waiting on a fence.
726  * When the fence becomes triggered the atom becomes runnable
727  * and completes immediately.
728  *
729  * Using the existing base dependency system the fence can
730  * be set to block a GPU job until it has been triggered.
731  *
732  * The base fence object must not be terminated until the atom
733  * has been submitted to @a base_jd_submit_bag and @a base_jd_submit_bag has returned.
734  *
735  * @a fence must be a valid fence set up with @a base_fence_init or @a base_fence_import.
736  * Calling this function with a uninitialized fence results in undefined behavior.
737  *
738  * @param[out] atom A pre-allocated atom to configure as a fence wait SW atom
739  * @param[in] fence The base fence object to wait on
740  */
741 static inline void base_jd_fence_wait_setup_v2(struct base_jd_atom_v2 *atom, struct base_fence *fence)
742 {
743         LOCAL_ASSERT(atom);
744         LOCAL_ASSERT(fence);
745         LOCAL_ASSERT(fence->basep.fd >= 0);
746         atom->jc = (uintptr_t) fence;
747         atom->core_req = BASE_JD_REQ_SOFT_FENCE_WAIT;
748 }
749
750 /**
751  * @brief External resource info initialization.
752  *
753  * Sets up a external resource object to reference
754  * a memory allocation and the type of access requested.
755  *
756  * @param[in] res     The resource object to initialize
757  * @param     handle  The handle to the imported memory object
758  * @param     access  The type of access requested
759  */
760 static inline void base_external_resource_init(struct base_external_resource *res, struct base_import_handle handle, base_external_resource_access access)
761 {
762         u64 address;
763
764         address = handle.basep.handle;
765
766         LOCAL_ASSERT(res != NULL);
767         LOCAL_ASSERT(0 == (address & LOCAL_PAGE_LSB));
768         LOCAL_ASSERT(access == BASE_EXT_RES_ACCESS_SHARED || access == BASE_EXT_RES_ACCESS_EXCLUSIVE);
769
770         res->ext_resource = address | (access & LOCAL_PAGE_LSB);
771 }
772
773 /**
774  * @brief Job chain event code bits
775  * Defines the bits used to create ::base_jd_event_code
776  */
777 enum {
778         BASE_JD_SW_EVENT_KERNEL = (1u << 15), /**< Kernel side event */
779         BASE_JD_SW_EVENT = (1u << 14), /**< SW defined event */
780         BASE_JD_SW_EVENT_SUCCESS = (1u << 13), /**< Event idicates success (SW events only) */
781         BASE_JD_SW_EVENT_JOB = (0u << 11), /**< Job related event */
782         BASE_JD_SW_EVENT_BAG = (1u << 11), /**< Bag related event */
783         BASE_JD_SW_EVENT_INFO = (2u << 11), /**< Misc/info event */
784         BASE_JD_SW_EVENT_RESERVED = (3u << 11), /**< Reserved event type */
785         BASE_JD_SW_EVENT_TYPE_MASK = (3u << 11)     /**< Mask to extract the type from an event code */
786 };
787
788 /**
789  * @brief Job chain event codes
790  *
791  * HW and low-level SW events are represented by event codes.
792  * The status of jobs which succeeded are also represented by
793  * an event code (see ::BASE_JD_EVENT_DONE).
794  * Events are usually reported as part of a ::base_jd_event.
795  *
796  * The event codes are encoded in the following way:
797  * @li 10:0  - subtype
798  * @li 12:11 - type
799  * @li 13    - SW success (only valid if the SW bit is set)
800  * @li 14    - SW event (HW event if not set)
801  * @li 15    - Kernel event (should never be seen in userspace)
802  *
803  * Events are split up into ranges as follows:
804  * - BASE_JD_EVENT_RANGE_\<description\>_START
805  * - BASE_JD_EVENT_RANGE_\<description\>_END
806  *
807  * \a code is in \<description\>'s range when:
808  * - <tt>BASE_JD_EVENT_RANGE_\<description\>_START <= code < BASE_JD_EVENT_RANGE_\<description\>_END </tt>
809  *
810  * Ranges can be asserted for adjacency by testing that the END of the previous
811  * is equal to the START of the next. This is useful for optimizing some tests
812  * for range.
813  *
814  * A limitation is that the last member of this enum must explicitly be handled
815  * (with an assert-unreachable statement) in switch statements that use
816  * variables of this type. Otherwise, the compiler warns that we have not
817  * handled that enum value.
818  */
819 typedef enum base_jd_event_code {
820         /* HW defined exceptions */
821
822         /** Start of HW Non-fault status codes
823          *
824          * @note Obscurely, BASE_JD_EVENT_TERMINATED indicates a real fault,
825          * because the job was hard-stopped
826          */
827         BASE_JD_EVENT_RANGE_HW_NONFAULT_START = 0,
828
829         /* non-fatal exceptions */
830         BASE_JD_EVENT_NOT_STARTED = 0x00, /**< Can't be seen by userspace, treated as 'previous job done' */
831         BASE_JD_EVENT_DONE = 0x01,
832         BASE_JD_EVENT_STOPPED = 0x03,     /**< Can't be seen by userspace, becomes TERMINATED, DONE or JOB_CANCELLED */
833         BASE_JD_EVENT_TERMINATED = 0x04,  /**< This is actually a fault status code - the job was hard stopped */
834         BASE_JD_EVENT_ACTIVE = 0x08,      /**< Can't be seen by userspace, jobs only returned on complete/fail/cancel */
835
836         /** End of HW Non-fault status codes
837          *
838          * @note Obscurely, BASE_JD_EVENT_TERMINATED indicates a real fault,
839          * because the job was hard-stopped
840          */
841         BASE_JD_EVENT_RANGE_HW_NONFAULT_END = 0x40,
842
843         /** Start of HW fault and SW Error status codes */
844         BASE_JD_EVENT_RANGE_HW_FAULT_OR_SW_ERROR_START = 0x40,
845
846         /* job exceptions */
847         BASE_JD_EVENT_JOB_CONFIG_FAULT = 0x40,
848         BASE_JD_EVENT_JOB_POWER_FAULT = 0x41,
849         BASE_JD_EVENT_JOB_READ_FAULT = 0x42,
850         BASE_JD_EVENT_JOB_WRITE_FAULT = 0x43,
851         BASE_JD_EVENT_JOB_AFFINITY_FAULT = 0x44,
852         BASE_JD_EVENT_JOB_BUS_FAULT = 0x48,
853         BASE_JD_EVENT_INSTR_INVALID_PC = 0x50,
854         BASE_JD_EVENT_INSTR_INVALID_ENC = 0x51,
855         BASE_JD_EVENT_INSTR_TYPE_MISMATCH = 0x52,
856         BASE_JD_EVENT_INSTR_OPERAND_FAULT = 0x53,
857         BASE_JD_EVENT_INSTR_TLS_FAULT = 0x54,
858         BASE_JD_EVENT_INSTR_BARRIER_FAULT = 0x55,
859         BASE_JD_EVENT_INSTR_ALIGN_FAULT = 0x56,
860         BASE_JD_EVENT_DATA_INVALID_FAULT = 0x58,
861         BASE_JD_EVENT_TILE_RANGE_FAULT = 0x59,
862         BASE_JD_EVENT_STATE_FAULT = 0x5A,
863         BASE_JD_EVENT_OUT_OF_MEMORY = 0x60,
864         BASE_JD_EVENT_UNKNOWN = 0x7F,
865
866         /* GPU exceptions */
867         BASE_JD_EVENT_DELAYED_BUS_FAULT = 0x80,
868         BASE_JD_EVENT_SHAREABILITY_FAULT = 0x88,
869
870         /* MMU exceptions */
871         BASE_JD_EVENT_TRANSLATION_FAULT_LEVEL1 = 0xC1,
872         BASE_JD_EVENT_TRANSLATION_FAULT_LEVEL2 = 0xC2,
873         BASE_JD_EVENT_TRANSLATION_FAULT_LEVEL3 = 0xC3,
874         BASE_JD_EVENT_TRANSLATION_FAULT_LEVEL4 = 0xC4,
875         BASE_JD_EVENT_PERMISSION_FAULT = 0xC8,
876         BASE_JD_EVENT_TRANSTAB_BUS_FAULT_LEVEL1 = 0xD1,
877         BASE_JD_EVENT_TRANSTAB_BUS_FAULT_LEVEL2 = 0xD2,
878         BASE_JD_EVENT_TRANSTAB_BUS_FAULT_LEVEL3 = 0xD3,
879         BASE_JD_EVENT_TRANSTAB_BUS_FAULT_LEVEL4 = 0xD4,
880         BASE_JD_EVENT_ACCESS_FLAG = 0xD8,
881
882         /* SW defined exceptions */
883         BASE_JD_EVENT_MEM_GROWTH_FAILED = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_JOB | 0x000,
884         BASE_JD_EVENT_TIMED_OUT         = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_JOB | 0x001,
885         BASE_JD_EVENT_JOB_CANCELLED     = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_JOB | 0x002,
886         BASE_JD_EVENT_JOB_INVALID       = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_JOB | 0x003,
887         BASE_JD_EVENT_PM_EVENT          = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_JOB | 0x004,
888         BASE_JD_EVENT_FORCE_REPLAY      = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_JOB | 0x005,
889
890         BASE_JD_EVENT_BAG_INVALID       = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_BAG | 0x003,
891
892         /** End of HW fault and SW Error status codes */
893         BASE_JD_EVENT_RANGE_HW_FAULT_OR_SW_ERROR_END = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_RESERVED | 0x3FF,
894
895         /** Start of SW Success status codes */
896         BASE_JD_EVENT_RANGE_SW_SUCCESS_START = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_SUCCESS | 0x000,
897
898         BASE_JD_EVENT_PROGRESS_REPORT = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_SUCCESS | BASE_JD_SW_EVENT_JOB | 0x000,
899         BASE_JD_EVENT_BAG_DONE = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_SUCCESS | BASE_JD_SW_EVENT_BAG | 0x000,
900         BASE_JD_EVENT_DRV_TERMINATED = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_SUCCESS | BASE_JD_SW_EVENT_INFO | 0x000,
901
902         /** End of SW Success status codes */
903         BASE_JD_EVENT_RANGE_SW_SUCCESS_END = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_SUCCESS | BASE_JD_SW_EVENT_RESERVED | 0x3FF,
904
905         /** Start of Kernel-only status codes. Such codes are never returned to user-space */
906         BASE_JD_EVENT_RANGE_KERNEL_ONLY_START = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_KERNEL | 0x000,
907         BASE_JD_EVENT_REMOVED_FROM_NEXT = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_KERNEL | BASE_JD_SW_EVENT_JOB | 0x000,
908
909         /** End of Kernel-only status codes. */
910         BASE_JD_EVENT_RANGE_KERNEL_ONLY_END = BASE_JD_SW_EVENT | BASE_JD_SW_EVENT_KERNEL | BASE_JD_SW_EVENT_RESERVED | 0x3FF
911 } base_jd_event_code;
912
913 /**
914  * @brief Event reporting structure
915  *
916  * This structure is used by the kernel driver to report information
917  * about GPU events. The can either be HW-specific events or low-level
918  * SW events, such as job-chain completion.
919  *
920  * The event code contains an event type field which can be extracted
921  * by ANDing with ::BASE_JD_SW_EVENT_TYPE_MASK.
922  *
923  * Based on the event type base_jd_event::data holds:
924  * @li ::BASE_JD_SW_EVENT_JOB : the offset in the ring-buffer for the completed
925  * job-chain
926  * @li ::BASE_JD_SW_EVENT_BAG : The address of the ::base_jd_bag that has
927  * been completed (ie all contained job-chains have been completed).
928  * @li ::BASE_JD_SW_EVENT_INFO : base_jd_event::data not used
929  */
930 typedef struct base_jd_event_v2 {
931         base_jd_event_code event_code;  /**< event code */
932         base_atom_id atom_number;       /**< the atom number that has completed */
933         struct base_jd_udata udata;     /**< user data */
934 } base_jd_event_v2;
935
936 /**
937  * Padding required to ensure that the @ref struct base_dump_cpu_gpu_counters structure fills
938  * a full cache line.
939  */
940
941 #define BASE_CPU_GPU_CACHE_LINE_PADDING (36)
942
943
944 /**
945  * @brief Structure for BASE_JD_REQ_SOFT_DUMP_CPU_GPU_COUNTERS jobs.
946  *
947  * This structure is stored into the memory pointed to by the @c jc field of @ref base_jd_atom.
948  *
949  * This structure must be padded to ensure that it will occupy whole cache lines. This is to avoid
950  * cases where access to pages containing the structure is shared between cached and un-cached
951  * memory regions, which would cause memory corruption.  Here we set the structure size to be 64 bytes
952  * which is the cache line for ARM A15 processors.
953  */
954
955 typedef struct base_dump_cpu_gpu_counters {
956         u64 system_time;
957         u64 cycle_counter;
958         u64 sec;
959         u32 usec;
960         u8 padding[BASE_CPU_GPU_CACHE_LINE_PADDING];
961 } base_dump_cpu_gpu_counters;
962
963
964
965 /** @} end group base_user_api_job_dispatch */
966
967 #ifdef __KERNEL__
968 /*
969  * The following typedefs should be removed when a midg types header is added.
970  * See MIDCOM-1657 for details.
971  */
972 typedef u32 gpu_product_id;
973 typedef u32 gpu_cache_features;
974 typedef u32 gpu_tiler_features;
975 typedef u32 gpu_mem_features;
976 typedef u32 gpu_mmu_features;
977 typedef u32 gpu_js_features;
978 typedef u32 gpu_as_present;
979 typedef u32 gpu_js_present;
980
981 #define GPU_MAX_JOB_SLOTS 16
982
983 #else
984 #include <gpu/mali_gpu_registers.h>
985 #include <gpu/mali_gpu_props.h>
986 #endif
987
988 /**
989  * @page page_base_user_api_gpuprops User-side Base GPU Property Query API
990  *
991  * The User-side Base GPU Property Query API encapsulates two
992  * sub-modules:
993  *
994  * - @ref base_user_api_gpuprops_dyn "Dynamic GPU Properties"
995  * - @ref base_plat_config_gpuprops "Base Platform Config GPU Properties"
996  *
997  * There is a related third module outside of Base, which is owned by the MIDG
998  * module:
999  * - @ref gpu_props_static "Midgard Compile-time GPU Properties"
1000  *
1001  * Base only deals with properties that vary between different Midgard
1002  * implementations - the Dynamic GPU properties and the Platform Config
1003  * properties.
1004  *
1005  * For properties that are constant for the Midgard Architecture, refer to the
1006  * MIDG module. However, we will discuss their relevance here <b>just to
1007  * provide background information.</b>
1008  *
1009  * @section sec_base_user_api_gpuprops_about About the GPU Properties in Base and MIDG modules
1010  *
1011  * The compile-time properties (Platform Config, Midgard Compile-time
1012  * properties) are exposed as pre-processor macros.
1013  *
1014  * Complementing the compile-time properties are the Dynamic GPU
1015  * Properties, which act as a conduit for the Midgard Configuration
1016  * Discovery.
1017  *
1018  * In general, the dynamic properties are present to verify that the platform
1019  * has been configured correctly with the right set of Platform Config
1020  * Compile-time Properties.
1021  *
1022  * As a consistant guide across the entire DDK, the choice for dynamic or
1023  * compile-time should consider the following, in order:
1024  * -# Can the code be written so that it doesn't need to know the
1025  * implementation limits at all?
1026  * -# If you need the limits, get the information from the Dynamic Property
1027  * lookup. This should be done once as you fetch the context, and then cached
1028  * as part of the context data structure, so it's cheap to access.
1029  * -# If there's a clear and arguable inefficiency in using Dynamic Properties,
1030  * then use a Compile-Time Property (Platform Config, or Midgard Compile-time
1031  * property). Examples of where this might be sensible follow:
1032  *  - Part of a critical inner-loop
1033  *  - Frequent re-use throughout the driver, causing significant extra load
1034  * instructions or control flow that would be worthwhile optimizing out.
1035  *
1036  * We cannot provide an exhaustive set of examples, neither can we provide a
1037  * rule for every possible situation. Use common sense, and think about: what
1038  * the rest of the driver will be doing; how the compiler might represent the
1039  * value if it is a compile-time constant; whether an OEM shipping multiple
1040  * devices would benefit much more from a single DDK binary, instead of
1041  * insignificant micro-optimizations.
1042  *
1043  * @section sec_base_user_api_gpuprops_dyn Dynamic GPU Properties
1044  *
1045  * Dynamic GPU properties are presented in two sets:
1046  * -# the commonly used properties in @ref base_gpu_props, which have been
1047  * unpacked from GPU register bitfields.
1048  * -# The full set of raw, unprocessed properties in @ref gpu_raw_gpu_props
1049  * (also a member of @ref base_gpu_props). All of these are presented in
1050  * the packed form, as presented by the GPU  registers themselves.
1051  *
1052  * @usecase The raw properties in @ref gpu_raw_gpu_props are necessary to
1053  * allow a user of the Mali Tools (e.g. PAT) to determine "Why is this device
1054  * behaving differently?". In this case, all information about the
1055  * configuration is potentially useful, but it <b>does not need to be processed
1056  * by the driver</b>. Instead, the raw registers can be processed by the Mali
1057  * Tools software on the host PC.
1058  *
1059  * The properties returned extend the Midgard Configuration Discovery
1060  * registers. For example, GPU clock speed is not specified in the Midgard
1061  * Architecture, but is <b>necessary for OpenCL's clGetDeviceInfo() function</b>.
1062  *
1063  * The GPU properties are obtained by a call to
1064  * _mali_base_get_gpu_props(). This simply returns a pointer to a const
1065  * base_gpu_props structure. It is constant for the life of a base
1066  * context. Multiple calls to _mali_base_get_gpu_props() to a base context
1067  * return the same pointer to a constant structure. This avoids cache pollution
1068  * of the common data.
1069  *
1070  * This pointer must not be freed, because it does not point to the start of a
1071  * region allocated by the memory allocator; instead, just close the @ref
1072  * base_context.
1073  *
1074  *
1075  * @section sec_base_user_api_gpuprops_config Platform Config Compile-time Properties
1076  *
1077  * The Platform Config File sets up gpu properties that are specific to a
1078  * certain platform. Properties that are 'Implementation Defined' in the
1079  * Midgard Architecture spec are placed here.
1080  *
1081  * @note Reference configurations are provided for Midgard Implementations, such as
1082  * the Mali-T600 family. The customer need not repeat this information, and can select one of
1083  * these reference configurations. For example, VA_BITS, PA_BITS and the
1084  * maximum number of samples per pixel might vary between Midgard Implementations, but
1085  * \b not for platforms using the Mali-T604. This information is placed in
1086  * the reference configuration files.
1087  *
1088  * The System Integrator creates the following structure:
1089  * - platform_XYZ
1090  * - platform_XYZ/plat
1091  * - platform_XYZ/plat/plat_config.h
1092  *
1093  * They then edit plat_config.h, using the example plat_config.h files as a
1094  * guide.
1095  *
1096  * At the very least, the customer must set @ref CONFIG_GPU_CORE_TYPE, and will
1097  * receive a helpful \#error message if they do not do this correctly. This
1098  * selects the Reference Configuration for the Midgard Implementation. The rationale
1099  * behind this decision (against asking the customer to write \#include
1100  * <gpus/mali_t600.h> in their plat_config.h) is as follows:
1101  * - This mechanism 'looks' like a regular config file (such as Linux's
1102  * .config)
1103  * - It is difficult to get wrong in a way that will produce strange build
1104  * errors:
1105  *  - They need not know where the mali_t600.h, other_midg_gpu.h etc. files are stored - and
1106  *  so they won't accidentally pick another file with 'mali_t600' in its name
1107  *  - When the build doesn't work, the System Integrator may think the DDK is
1108  *  doesn't work, and attempt to fix it themselves:
1109  *   - For the @ref CONFIG_GPU_CORE_TYPE mechanism, the only way to get past the
1110  *   error is to set @ref CONFIG_GPU_CORE_TYPE, and this is what the \#error tells
1111  *   you.
1112  *   - For a \#include mechanism, checks must still be made elsewhere, which the
1113  *   System Integrator may try working around by setting \#defines (such as
1114  *   VA_BITS) themselves in their plat_config.h. In the  worst case, they may
1115  *   set the prevention-mechanism \#define of
1116  *   "A_CORRECT_MIDGARD_CORE_WAS_CHOSEN".
1117  *   - In this case, they would believe they are on the right track, because
1118  *   the build progresses with their fix, but with errors elsewhere.
1119  *
1120  * However, there is nothing to prevent the customer using \#include to organize
1121  * their own configurations files hierarchically.
1122  *
1123  * The mechanism for the header file processing is as follows:
1124  *
1125  * @dot
1126    digraph plat_config_mechanism {
1127            rankdir=BT
1128            size="6,6"
1129
1130        "mali_base.h";
1131            "gpu/mali_gpu.h";
1132
1133            node [ shape=box ];
1134            {
1135                rank = same; ordering = out;
1136
1137                    "gpu/mali_gpu_props.h";
1138                    "base/midg_gpus/mali_t600.h";
1139                    "base/midg_gpus/other_midg_gpu.h";
1140            }
1141            { rank = same; "plat/plat_config.h"; }
1142            {
1143                rank = same;
1144                    "gpu/mali_gpu.h" [ shape=box ];
1145                    gpu_chooser [ label="" style="invisible" width=0 height=0 fixedsize=true ];
1146                    select_gpu [ label="Mali-T600 | Other\n(select_gpu.h)" shape=polygon,sides=4,distortion=0.25 width=3.3 height=0.99 fixedsize=true ] ;
1147            }
1148            node [ shape=box ];
1149            { rank = same; "plat/plat_config.h"; }
1150            { rank = same; "mali_base.h"; }
1151
1152            "mali_base.h" -> "gpu/mali_gpu.h" -> "gpu/mali_gpu_props.h";
1153            "mali_base.h" -> "plat/plat_config.h" ;
1154            "mali_base.h" -> select_gpu ;
1155
1156            "plat/plat_config.h" -> gpu_chooser [style="dotted,bold" dir=none weight=4] ;
1157            gpu_chooser -> select_gpu [style="dotted,bold"] ;
1158
1159            select_gpu -> "base/midg_gpus/mali_t600.h" ;
1160            select_gpu -> "base/midg_gpus/other_midg_gpu.h" ;
1161    }
1162    @enddot
1163  *
1164  *
1165  * @section sec_base_user_api_gpuprops_kernel Kernel Operation
1166  *
1167  * During Base Context Create time, user-side makes a single kernel call:
1168  * - A call to fill user memory with GPU information structures
1169  *
1170  * The kernel-side will fill the provided the entire processed @ref base_gpu_props
1171  * structure, because this information is required in both
1172  * user and kernel side; it does not make sense to decode it twice.
1173  *
1174  * Coherency groups must be derived from the bitmasks, but this can be done
1175  * kernel side, and just once at kernel startup: Coherency groups must already
1176  * be known kernel-side, to support chains that specify a 'Only Coherent Group'
1177  * SW requirement, or 'Only Coherent Group with Tiler' SW requirement.
1178  *
1179  * @section sec_base_user_api_gpuprops_cocalc Coherency Group calculation
1180  * Creation of the coherent group data is done at device-driver startup, and so
1181  * is one-time. This will most likely involve a loop with CLZ, shifting, and
1182  * bit clearing on the L2_PRESENT mask, depending on whether the
1183  * system is L2 Coherent. The number of shader cores is done by a
1184  * population count, since faulty cores may be disabled during production,
1185  * producing a non-contiguous mask.
1186  *
1187  * The memory requirements for this algoirthm can be determined either by a u64
1188  * population count on the L2_PRESENT mask (a LUT helper already is
1189  * requried for the above), or simple assumption that there can be no more than
1190  * 16 coherent groups, since core groups are typically 4 cores.
1191  */
1192
1193 /**
1194  * @addtogroup base_user_api_gpuprops User-side Base GPU Property Query APIs
1195  * @{
1196  */
1197
1198 /**
1199  * @addtogroup base_user_api_gpuprops_dyn Dynamic HW Properties
1200  * @{
1201  */
1202
1203 #define BASE_GPU_NUM_TEXTURE_FEATURES_REGISTERS 3
1204
1205 #define BASE_MAX_COHERENT_GROUPS 16
1206
1207 struct mali_base_gpu_core_props {
1208         /**
1209          * Product specific value.
1210          */
1211         gpu_product_id product_id;
1212
1213         /**
1214          * Status of the GPU release.
1215      * No defined values, but starts at 0 and increases by one for each release
1216      * status (alpha, beta, EAC, etc.).
1217      * 4 bit values (0-15).
1218          */
1219         u16 version_status;
1220
1221         /**
1222          * Minor release number of the GPU. "P" part of an "RnPn" release number.
1223      * 8 bit values (0-255).
1224          */
1225         u16 minor_revision;
1226
1227         /**
1228          * Major release number of the GPU. "R" part of an "RnPn" release number.
1229      * 4 bit values (0-15).
1230          */
1231         u16 major_revision;
1232
1233         u16 padding;
1234
1235         /**
1236          * @usecase GPU clock speed is not specified in the Midgard Architecture, but is
1237          * <b>necessary for OpenCL's clGetDeviceInfo() function</b>.
1238          */
1239         u32 gpu_speed_mhz;
1240
1241         /**
1242          * @usecase GPU clock max/min speed is required for computing best/worst case
1243          * in tasks as job scheduling ant irq_throttling. (It is not specified in the
1244          *  Midgard Architecture).
1245          */
1246         u32 gpu_freq_khz_max;
1247         u32 gpu_freq_khz_min;
1248
1249         /**
1250          * Size of the shader program counter, in bits.
1251          */
1252         u32 log2_program_counter_size;
1253
1254         /**
1255          * TEXTURE_FEATURES_x registers, as exposed by the GPU. This is a
1256          * bitpattern where a set bit indicates that the format is supported.
1257          *
1258          * Before using a texture format, it is recommended that the corresponding
1259          * bit be checked.
1260          */
1261         u32 texture_features[BASE_GPU_NUM_TEXTURE_FEATURES_REGISTERS];
1262
1263         /**
1264          * Theoretical maximum memory available to the GPU. It is unlikely that a
1265          * client will be able to allocate all of this memory for their own
1266          * purposes, but this at least provides an upper bound on the memory
1267          * available to the GPU.
1268          *
1269          * This is required for OpenCL's clGetDeviceInfo() call when
1270          * CL_DEVICE_GLOBAL_MEM_SIZE is requested, for OpenCL GPU devices. The
1271          * client will not be expecting to allocate anywhere near this value.
1272          */
1273         u64 gpu_available_memory_size;
1274 };
1275
1276 /**
1277  *
1278  * More information is possible - but associativity and bus width are not
1279  * required by upper-level apis.
1280  */
1281 struct mali_base_gpu_l2_cache_props {
1282         u8 log2_line_size;
1283         u8 log2_cache_size;
1284         u8 num_l2_slices; /* Number of L2C slices. 1 or higher */
1285         u8 padding[5];
1286 };
1287
1288 struct mali_base_gpu_tiler_props {
1289         u32 bin_size_bytes;     /* Max is 4*2^15 */
1290         u32 max_active_levels;  /* Max is 2^15 */
1291 };
1292
1293 /**
1294  * GPU threading system details.
1295  */
1296 struct mali_base_gpu_thread_props {
1297         u32 max_threads;            /* Max. number of threads per core */
1298         u32 max_workgroup_size;     /* Max. number of threads per workgroup */
1299         u32 max_barrier_size;       /* Max. number of threads that can synchronize on a simple barrier */
1300         u16 max_registers;          /* Total size [1..65535] of the register file available per core. */
1301         u8  max_task_queue;         /* Max. tasks [1..255] which may be sent to a core before it becomes blocked. */
1302         u8  max_thread_group_split; /* Max. allowed value [1..15] of the Thread Group Split field. */
1303         u8  impl_tech;              /* 0 = Not specified, 1 = Silicon, 2 = FPGA, 3 = SW Model/Emulation */
1304         u8  padding[7];
1305 };
1306
1307 /**
1308  * @brief descriptor for a coherent group
1309  *
1310  * \c core_mask exposes all cores in that coherent group, and \c num_cores
1311  * provides a cached population-count for that mask.
1312  *
1313  * @note Whilst all cores are exposed in the mask, not all may be available to
1314  * the application, depending on the Kernel Power policy.
1315  *
1316  * @note if u64s must be 8-byte aligned, then this structure has 32-bits of wastage.
1317  */
1318 struct mali_base_gpu_coherent_group {
1319         u64 core_mask;         /**< Core restriction mask required for the group */
1320         u16 num_cores;         /**< Number of cores in the group */
1321         u16 padding[3];
1322 };
1323
1324 /**
1325  * @brief Coherency group information
1326  *
1327  * Note that the sizes of the members could be reduced. However, the \c group
1328  * member might be 8-byte aligned to ensure the u64 core_mask is 8-byte
1329  * aligned, thus leading to wastage if the other members sizes were reduced.
1330  *
1331  * The groups are sorted by core mask. The core masks are non-repeating and do
1332  * not intersect.
1333  */
1334 struct mali_base_gpu_coherent_group_info {
1335         u32 num_groups;
1336
1337         /**
1338          * Number of core groups (coherent or not) in the GPU. Equivalent to the number of L2 Caches.
1339          *
1340          * The GPU Counter dumping writes 2048 bytes per core group, regardless of
1341          * whether the core groups are coherent or not. Hence this member is needed
1342          * to calculate how much memory is required for dumping.
1343          *
1344          * @note Do not use it to work out how many valid elements are in the
1345          * group[] member. Use num_groups instead.
1346          */
1347         u32 num_core_groups;
1348
1349         /**
1350          * Coherency features of the memory, accessed by @ref gpu_mem_features
1351          * methods
1352          */
1353         gpu_mem_features coherency;
1354
1355         u32 padding;
1356
1357         /**
1358          * Descriptors of coherent groups
1359          */
1360         struct mali_base_gpu_coherent_group group[BASE_MAX_COHERENT_GROUPS];
1361 };
1362
1363 /**
1364  * A complete description of the GPU's Hardware Configuration Discovery
1365  * registers.
1366  *
1367  * The information is presented inefficiently for access. For frequent access,
1368  * the values should be better expressed in an unpacked form in the
1369  * base_gpu_props structure.
1370  *
1371  * @usecase The raw properties in @ref gpu_raw_gpu_props are necessary to
1372  * allow a user of the Mali Tools (e.g. PAT) to determine "Why is this device
1373  * behaving differently?". In this case, all information about the
1374  * configuration is potentially useful, but it <b>does not need to be processed
1375  * by the driver</b>. Instead, the raw registers can be processed by the Mali
1376  * Tools software on the host PC.
1377  *
1378  */
1379 struct gpu_raw_gpu_props {
1380         u64 shader_present;
1381         u64 tiler_present;
1382         u64 l2_present;
1383         u64 unused_1; /* keep for backward compatibility */
1384
1385         gpu_cache_features l2_features;
1386         u32 suspend_size; /* API 8.2+ */
1387         gpu_mem_features mem_features;
1388         gpu_mmu_features mmu_features;
1389
1390         gpu_as_present as_present;
1391
1392         u32 js_present;
1393         gpu_js_features js_features[GPU_MAX_JOB_SLOTS];
1394         gpu_tiler_features tiler_features;
1395         u32 texture_features[3];
1396
1397         u32 gpu_id;
1398
1399         u32 thread_max_threads;
1400         u32 thread_max_workgroup_size;
1401         u32 thread_max_barrier_size;
1402         u32 thread_features;
1403
1404         u32 coherency_features;
1405 };
1406
1407 /**
1408  * Return structure for _mali_base_get_gpu_props().
1409  *
1410  * NOTE: the raw_props member in this datastructure contains the register
1411  * values from which the value of the other members are derived. The derived
1412  * members exist to allow for efficient access and/or shielding the details
1413  * of the layout of the registers.
1414  *
1415  */
1416 typedef struct mali_base_gpu_props {
1417         struct mali_base_gpu_core_props core_props;
1418         struct mali_base_gpu_l2_cache_props l2_props;
1419         u64 unused_1; /* keep for backwards compatibility */
1420         struct mali_base_gpu_tiler_props tiler_props;
1421         struct mali_base_gpu_thread_props thread_props;
1422
1423         /** This member is large, likely to be 128 bytes */
1424         struct gpu_raw_gpu_props raw_props;
1425
1426         /** This must be last member of the structure */
1427         struct mali_base_gpu_coherent_group_info coherency_info;
1428 } base_gpu_props;
1429
1430 /** @} end group base_user_api_gpuprops_dyn */
1431
1432 /** @} end group base_user_api_gpuprops */
1433
1434 /**
1435  * @addtogroup base_user_api_core User-side Base core APIs
1436  * @{
1437  */
1438
1439 /**
1440  * \enum base_context_create_flags
1441  *
1442  * Flags to pass to ::base_context_init.
1443  * Flags can be ORed together to enable multiple things.
1444  *
1445  * These share the same space as @ref basep_context_private_flags, and so must
1446  * not collide with them.
1447  */
1448 enum base_context_create_flags {
1449         /** No flags set */
1450         BASE_CONTEXT_CREATE_FLAG_NONE = 0,
1451
1452         /** Base context is embedded in a cctx object (flag used for CINSTR software counter macros) */
1453         BASE_CONTEXT_CCTX_EMBEDDED = (1u << 0),
1454
1455         /** Base context is a 'System Monitor' context for Hardware counters.
1456          *
1457          * One important side effect of this is that job submission is disabled. */
1458         BASE_CONTEXT_SYSTEM_MONITOR_SUBMIT_DISABLED = (1u << 1),
1459
1460         /** Base context flag indicating a 'hint' that this context uses Compute
1461          * Jobs only.
1462          *
1463          * Specifially, this means that it only sends atoms that <b>do not</b>
1464          * contain the following @ref base_jd_core_req :
1465          * - BASE_JD_REQ_FS
1466          * - BASE_JD_REQ_T
1467          *
1468          * Violation of these requirements will cause the Job-Chains to be rejected.
1469          *
1470          * In addition, it is inadvisable for the atom's Job-Chains to contain Jobs
1471          * of the following @ref gpu_job_type (whilst it may work now, it may not
1472          * work in future) :
1473          * - @ref GPU_JOB_VERTEX
1474          * - @ref GPU_JOB_GEOMETRY
1475          *
1476          * @note An alternative to using this is to specify the BASE_JD_REQ_ONLY_COMPUTE
1477          * requirement in atoms.
1478          */
1479         BASE_CONTEXT_HINT_ONLY_COMPUTE = (1u << 2)
1480 };
1481
1482 /**
1483  * Bitpattern describing the ::base_context_create_flags that can be passed to base_context_init()
1484  */
1485 #define BASE_CONTEXT_CREATE_ALLOWED_FLAGS \
1486         (((u32)BASE_CONTEXT_CCTX_EMBEDDED) | \
1487           ((u32)BASE_CONTEXT_SYSTEM_MONITOR_SUBMIT_DISABLED) | \
1488           ((u32)BASE_CONTEXT_HINT_ONLY_COMPUTE))
1489
1490 /**
1491  * Bitpattern describing the ::base_context_create_flags that can be passed to the kernel
1492  */
1493 #define BASE_CONTEXT_CREATE_KERNEL_FLAGS \
1494         (((u32)BASE_CONTEXT_SYSTEM_MONITOR_SUBMIT_DISABLED) | \
1495           ((u32)BASE_CONTEXT_HINT_ONLY_COMPUTE))
1496
1497 /**
1498  * Private flags used on the base context
1499  *
1500  * These start at bit 31, and run down to zero.
1501  *
1502  * They share the same space as @ref base_context_create_flags, and so must
1503  * not collide with them.
1504  */
1505 enum basep_context_private_flags {
1506         /** Private flag tracking whether job descriptor dumping is disabled */
1507         BASEP_CONTEXT_FLAG_JOB_DUMP_DISABLED = (1 << 31)
1508 };
1509
1510 /** @} end group base_user_api_core */
1511
1512 /** @} end group base_user_api */
1513
1514 /**
1515  * @addtogroup base_plat_config_gpuprops Base Platform Config GPU Properties
1516  * @{
1517  *
1518  * C Pre-processor macros are exposed here to do with Platform
1519  * Config.
1520  *
1521  * These include:
1522  * - GPU Properties that are constant on a particular Midgard Family
1523  * Implementation e.g. Maximum samples per pixel on Mali-T600.
1524  * - General platform config for the GPU, such as the GPU major and minor
1525  * revison.
1526  */
1527
1528 /** @} end group base_plat_config_gpuprops */
1529
1530 /**
1531  * @addtogroup base_api Base APIs
1532  * @{
1533  */
1534
1535 /**
1536  * @brief The payload for a replay job. This must be in GPU memory.
1537  */
1538 typedef struct base_jd_replay_payload {
1539         /**
1540          * Pointer to the first entry in the base_jd_replay_jc list.  These
1541          * will be replayed in @b reverse order (so that extra ones can be added
1542          * to the head in future soft jobs without affecting this soft job)
1543          */
1544         u64 tiler_jc_list;
1545
1546         /**
1547          * Pointer to the fragment job chain.
1548          */
1549         u64 fragment_jc;
1550
1551         /**
1552          * Pointer to the tiler heap free FBD field to be modified.
1553          */
1554         u64 tiler_heap_free;
1555
1556         /**
1557          * Hierarchy mask for the replayed fragment jobs. May be zero.
1558          */
1559         u16 fragment_hierarchy_mask;
1560
1561         /**
1562          * Hierarchy mask for the replayed tiler jobs. May be zero.
1563          */
1564         u16 tiler_hierarchy_mask;
1565
1566         /**
1567          * Default weight to be used for hierarchy levels not in the original
1568          * mask.
1569          */
1570         u32 hierarchy_default_weight;
1571
1572         /**
1573          * Core requirements for the tiler job chain
1574          */
1575         base_jd_core_req tiler_core_req;
1576
1577         /**
1578          * Core requirements for the fragment job chain
1579          */
1580         base_jd_core_req fragment_core_req;
1581
1582         u8 padding[4];
1583 } base_jd_replay_payload;
1584
1585 /**
1586  * @brief An entry in the linked list of job chains to be replayed. This must
1587  *        be in GPU memory.
1588  */
1589 typedef struct base_jd_replay_jc {
1590         /**
1591          * Pointer to next entry in the list. A setting of NULL indicates the
1592          * end of the list.
1593          */
1594         u64 next;
1595
1596         /**
1597          * Pointer to the job chain.
1598          */
1599         u64 jc;
1600
1601 } base_jd_replay_jc;
1602
1603 /* Maximum number of jobs allowed in a fragment chain in the payload of a
1604  * replay job */
1605 #define BASE_JD_REPLAY_F_CHAIN_JOB_LIMIT 256
1606
1607 /** @} end group base_api */
1608
1609 typedef struct base_profiling_controls {
1610         u32 profiling_controls[FBDUMP_CONTROL_MAX];
1611 } base_profiling_controls;
1612
1613 #endif                          /* _BASE_KERNEL_H_ */