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