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