MALI: rockchip: upgrade midgard DDK to r14p0-01rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_js_defs.h
1 /*
2  *
3  * (C) COPYRIGHT 2011-2016 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18
19
20 /**
21  * @file mali_kbase_js.h
22  * Job Scheduler Type Definitions
23  */
24
25 #ifndef _KBASE_JS_DEFS_H_
26 #define _KBASE_JS_DEFS_H_
27
28 /**
29  * @addtogroup base_api
30  * @{
31  */
32
33 /**
34  * @addtogroup base_kbase_api
35  * @{
36  */
37
38 /**
39  * @addtogroup kbase_js
40  * @{
41  */
42 /* Forward decls */
43 struct kbase_device;
44 struct kbase_jd_atom;
45
46
47 typedef u32 kbase_context_flags;
48
49 struct kbasep_atom_req {
50         base_jd_core_req core_req;
51         kbase_context_flags ctx_req;
52         u32 device_nr;
53 };
54
55 #include "mali_kbase_js_policy_cfs.h"
56
57 /* Wrapper Interface - doxygen is elsewhere */
58 union kbasep_js_policy {
59         struct kbasep_js_policy_cfs cfs;
60 };
61
62 /* Wrapper Interface - doxygen is elsewhere */
63 union kbasep_js_policy_ctx_info {
64         struct kbasep_js_policy_cfs_ctx cfs;
65 };
66
67 /* Wrapper Interface - doxygen is elsewhere */
68 union kbasep_js_policy_job_info {
69         struct kbasep_js_policy_cfs_job cfs;
70 };
71
72
73 /** Callback function run on all of a context's jobs registered with the Job
74  * Scheduler */
75 typedef void (*kbasep_js_policy_ctx_job_cb)(struct kbase_device *kbdev, struct kbase_jd_atom *katom);
76
77 /**
78  * @brief Maximum number of jobs that can be submitted to a job slot whilst
79  * inside the IRQ handler.
80  *
81  * This is important because GPU NULL jobs can complete whilst the IRQ handler
82  * is running. Otherwise, it potentially allows an unlimited number of GPU NULL
83  * jobs to be submitted inside the IRQ handler, which increases IRQ latency.
84  */
85 #define KBASE_JS_MAX_JOB_SUBMIT_PER_SLOT_PER_IRQ 2
86
87 /**
88  * @brief the IRQ_THROTTLE time in microseconds
89  *
90  * This will be converted via the GPU's clock frequency into a cycle-count.
91  *
92  * @note we can make an estimate of the GPU's frequency by periodically
93  * sampling its CYCLE_COUNT register
94  */
95 #define KBASE_JS_IRQ_THROTTLE_TIME_US 20
96
97 /**
98  * @brief Context attributes
99  *
100  * Each context attribute can be thought of as a boolean value that caches some
101  * state information about either the runpool, or the context:
102  * - In the case of the runpool, it is a cache of "Do any contexts owned by
103  * the runpool have attribute X?"
104  * - In the case of a context, it is a cache of "Do any atoms owned by the
105  * context have attribute X?"
106  *
107  * The boolean value of the context attributes often affect scheduling
108  * decisions, such as affinities to use and job slots to use.
109  *
110  * To accomodate changes of state in the context, each attribute is refcounted
111  * in the context, and in the runpool for all running contexts. Specifically:
112  * - The runpool holds a refcount of how many contexts in the runpool have this
113  * attribute.
114  * - The context holds a refcount of how many atoms have this attribute.
115  */
116 enum kbasep_js_ctx_attr {
117         /** Attribute indicating a context that contains Compute jobs. That is,
118          * the context has jobs of type @ref BASE_JD_REQ_ONLY_COMPUTE
119          *
120          * @note A context can be both 'Compute' and 'Non Compute' if it contains
121          * both types of jobs.
122          */
123         KBASEP_JS_CTX_ATTR_COMPUTE,
124
125         /** Attribute indicating a context that contains Non-Compute jobs. That is,
126          * the context has some jobs that are \b not of type @ref
127          * BASE_JD_REQ_ONLY_COMPUTE. The context usually has
128          * BASE_CONTEXT_HINT_COMPUTE \b clear, but this depends on the HW
129          * workarounds in use in the Job Scheduling Policy.
130          *
131          * @note A context can be both 'Compute' and 'Non Compute' if it contains
132          * both types of jobs.
133          */
134         KBASEP_JS_CTX_ATTR_NON_COMPUTE,
135
136         /** Attribute indicating that a context contains compute-job atoms that
137          * aren't restricted to a coherent group, and can run on all cores.
138          *
139          * Specifically, this is when the atom's \a core_req satisfy:
140          * - (\a core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_ONLY_COMPUTE | BASE_JD_REQ_T) // uses slot 1 or slot 2
141          * - && !(\a core_req & BASE_JD_REQ_COHERENT_GROUP) // not restricted to coherent groups
142          *
143          * Such atoms could be blocked from running if one of the coherent groups
144          * is being used by another job slot, so tracking this context attribute
145          * allows us to prevent such situations.
146          *
147          * @note This doesn't take into account the 1-coregroup case, where all
148          * compute atoms would effectively be able to run on 'all cores', but
149          * contexts will still not always get marked with this attribute. Instead,
150          * it is the caller's responsibility to take into account the number of
151          * coregroups when interpreting this attribute.
152          *
153          * @note Whilst Tiler atoms are normally combined with
154          * BASE_JD_REQ_COHERENT_GROUP, it is possible to send such atoms without
155          * BASE_JD_REQ_COHERENT_GROUP set. This is an unlikely case, but it's easy
156          * enough to handle anyway.
157          */
158         KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES,
159
160         /** Must be the last in the enum */
161         KBASEP_JS_CTX_ATTR_COUNT
162 };
163
164 enum {
165         /** Bit indicating that new atom should be started because this atom completed */
166         KBASE_JS_ATOM_DONE_START_NEW_ATOMS = (1u << 0),
167         /** Bit indicating that the atom was evicted from the JS_NEXT registers */
168         KBASE_JS_ATOM_DONE_EVICTED_FROM_NEXT = (1u << 1)
169 };
170
171 /** Combination of KBASE_JS_ATOM_DONE_<...> bits */
172 typedef u32 kbasep_js_atom_done_code;
173
174 /**
175  * Data used by the scheduler that is unique for each Address Space.
176  *
177  * This is used in IRQ context and hwaccess_lock must be held whilst accessing
178  * this data (inculding reads and atomic decisions based on the read).
179  */
180 struct kbasep_js_per_as_data {
181         /**
182          * Ref count of whether this AS is busy, and must not be scheduled out
183          *
184          * When jobs are running this is always positive. However, it can still be
185          * positive when no jobs are running. If all you need is a heuristic to
186          * tell you whether jobs might be running, this should be sufficient.
187          */
188         int as_busy_refcount;
189
190         /** Pointer to the current context on this address space, or NULL for no context */
191         struct kbase_context *kctx;
192 };
193
194 /**
195  * @brief KBase Device Data Job Scheduler sub-structure
196  *
197  * This encapsulates the current context of the Job Scheduler on a particular
198  * device. This context is global to the device, and is not tied to any
199  * particular struct kbase_context running on the device.
200  *
201  * nr_contexts_running and as_free are optimized for packing together (by making
202  * them smaller types than u32). The operations on them should rarely involve
203  * masking. The use of signed types for arithmetic indicates to the compiler that
204  * the value will not rollover (which would be undefined behavior), and so under
205  * the Total License model, it is free to make optimizations based on that (i.e.
206  * to remove masking).
207  */
208 struct kbasep_js_device_data {
209         /* Sub-structure to collect together Job Scheduling data used in IRQ
210          * context. The hwaccess_lock must be held when accessing. */
211         struct runpool_irq {
212                 /** Bitvector indicating whether a currently scheduled context is allowed to submit jobs.
213                  * When bit 'N' is set in this, it indicates whether the context bound to address space
214                  * 'N' (per_as_data[N].kctx) is allowed to submit jobs.
215                  *
216                  * It is placed here because it's much more memory efficient than having a u8 in
217                  * struct kbasep_js_per_as_data to store this flag  */
218                 u16 submit_allowed;
219
220                 /** Context Attributes:
221                  * Each is large enough to hold a refcount of the number of contexts
222                  * that can fit into the runpool. This is currently BASE_MAX_NR_AS
223                  *
224                  * Note that when BASE_MAX_NR_AS==16 we need 5 bits (not 4) to store
225                  * the refcount. Hence, it's not worthwhile reducing this to
226                  * bit-manipulation on u32s to save space (where in contrast, 4 bit
227                  * sub-fields would be easy to do and would save space).
228                  *
229                  * Whilst this must not become negative, the sign bit is used for:
230                  * - error detection in debug builds
231                  * - Optimization: it is undefined for a signed int to overflow, and so
232                  * the compiler can optimize for that never happening (thus, no masking
233                  * is required on updating the variable) */
234                 s8 ctx_attr_ref_count[KBASEP_JS_CTX_ATTR_COUNT];
235
236                 /** Data that is unique for each AS */
237                 struct kbasep_js_per_as_data per_as_data[BASE_MAX_NR_AS];
238
239                 /*
240                  * Affinity management and tracking
241                  */
242                 /** Bitvector to aid affinity checking. Element 'n' bit 'i' indicates
243                  * that slot 'n' is using core i (i.e. slot_affinity_refcount[n][i] > 0) */
244                 u64 slot_affinities[BASE_JM_MAX_NR_SLOTS];
245                 /** Refcount for each core owned by each slot. Used to generate the
246                  * slot_affinities array of bitvectors
247                  *
248                  * The value of the refcount will not exceed BASE_JM_SUBMIT_SLOTS,
249                  * because it is refcounted only when a job is definitely about to be
250                  * submitted to a slot, and is de-refcounted immediately after a job
251                  * finishes */
252                 s8 slot_affinity_refcount[BASE_JM_MAX_NR_SLOTS][64];
253         } runpool_irq;
254
255         /**
256          * Run Pool mutex, for managing contexts within the runpool.
257          * Unless otherwise specified, you must hold this lock whilst accessing any
258          * members that follow
259          *
260          * In addition, this is used to access:
261          * - the kbasep_js_kctx_info::runpool substructure
262          */
263         struct mutex runpool_mutex;
264
265         /**
266          * Queue Lock, used to access the Policy's queue of contexts independently
267          * of the Run Pool.
268          *
269          * Of course, you don't need the Run Pool lock to access this.
270          */
271         struct mutex queue_mutex;
272
273         /**
274          * Scheduling semaphore. This must be held when calling
275          * kbase_jm_kick()
276          */
277         struct semaphore schedule_sem;
278
279         /**
280          * List of contexts that can currently be pulled from
281          */
282         struct list_head ctx_list_pullable[BASE_JM_MAX_NR_SLOTS];
283         /**
284          * List of contexts that can not currently be pulled from, but have
285          * jobs currently running.
286          */
287         struct list_head ctx_list_unpullable[BASE_JM_MAX_NR_SLOTS];
288
289         u16 as_free;                            /**< Bitpattern of free Address Spaces */
290
291         /** Number of currently scheduled user contexts (excluding ones that are not submitting jobs) */
292         s8 nr_user_contexts_running;
293         /** Number of currently scheduled contexts (including ones that are not submitting jobs) */
294         s8 nr_all_contexts_running;
295
296         /**
297          * Policy-specific information.
298          *
299          * Refer to the structure defined by the current policy to determine which
300          * locks must be held when accessing this.
301          */
302         union kbasep_js_policy policy;
303
304         /** Core Requirements to match up with base_js_atom's core_req memeber
305          * @note This is a write-once member, and so no locking is required to read */
306         base_jd_core_req js_reqs[BASE_JM_MAX_NR_SLOTS];
307
308         u32 scheduling_period_ns;    /*< Value for JS_SCHEDULING_PERIOD_NS */
309         u32 soft_stop_ticks;         /*< Value for JS_SOFT_STOP_TICKS */
310         u32 soft_stop_ticks_cl;      /*< Value for JS_SOFT_STOP_TICKS_CL */
311         u32 hard_stop_ticks_ss;      /*< Value for JS_HARD_STOP_TICKS_SS */
312         u32 hard_stop_ticks_cl;      /*< Value for JS_HARD_STOP_TICKS_CL */
313         u32 hard_stop_ticks_dumping; /*< Value for JS_HARD_STOP_TICKS_DUMPING */
314         u32 gpu_reset_ticks_ss;      /*< Value for JS_RESET_TICKS_SS */
315         u32 gpu_reset_ticks_cl;      /*< Value for JS_RESET_TICKS_CL */
316         u32 gpu_reset_ticks_dumping; /*< Value for JS_RESET_TICKS_DUMPING */
317         u32 ctx_timeslice_ns;            /**< Value for JS_CTX_TIMESLICE_NS */
318         u32 cfs_ctx_runtime_init_slices; /**< Value for DEFAULT_JS_CFS_CTX_RUNTIME_INIT_SLICES */
319         u32 cfs_ctx_runtime_min_slices;  /**< Value for  DEFAULT_JS_CFS_CTX_RUNTIME_MIN_SLICES */
320
321         /**< Value for JS_SOFT_JOB_TIMEOUT */
322         atomic_t soft_job_timeout_ms;
323
324         /** List of suspended soft jobs */
325         struct list_head suspended_soft_jobs_list;
326
327 #ifdef CONFIG_MALI_DEBUG
328         /* Support soft-stop on a single context */
329         bool softstop_always;
330 #endif                          /* CONFIG_MALI_DEBUG */
331
332         /** The initalized-flag is placed at the end, to avoid cache-pollution (we should
333          * only be using this during init/term paths).
334          * @note This is a write-once member, and so no locking is required to read */
335         int init_status;
336
337         /* Number of contexts that can currently be pulled from */
338         u32 nr_contexts_pullable;
339
340         /* Number of contexts that can either be pulled from or are currently
341          * running */
342         atomic_t nr_contexts_runnable;
343 };
344
345 /**
346  * @brief KBase Context Job Scheduling information structure
347  *
348  * This is a substructure in the struct kbase_context that encapsulates all the
349  * scheduling information.
350  */
351 struct kbasep_js_kctx_info {
352         /**
353          * Runpool substructure. This must only be accessed whilst the Run Pool
354          * mutex ( kbasep_js_device_data::runpool_mutex ) is held.
355          *
356          * In addition, the hwaccess_lock may need to be held for certain
357          * sub-members.
358          *
359          * @note some of the members could be moved into struct kbasep_js_device_data for
360          * improved d-cache/tlb efficiency.
361          */
362         struct {
363                 union kbasep_js_policy_ctx_info policy_ctx;     /**< Policy-specific context */
364         } runpool;
365
366         /**
367          * Job Scheduler Context information sub-structure. These members are
368          * accessed regardless of whether the context is:
369          * - In the Policy's Run Pool
370          * - In the Policy's Queue
371          * - Not queued nor in the Run Pool.
372          *
373          * You must obtain the jsctx_mutex before accessing any other members of
374          * this substructure.
375          *
376          * You may not access any of these members from IRQ context.
377          */
378         struct kbase_jsctx {
379                 struct mutex jsctx_mutex;                   /**< Job Scheduler Context lock */
380
381                 /** Number of jobs <b>ready to run</b> - does \em not include the jobs waiting in
382                  * the dispatcher, and dependency-only jobs. See kbase_jd_context::job_nr
383                  * for such jobs*/
384                 u32 nr_jobs;
385
386                 /** Context Attributes:
387                  * Each is large enough to hold a refcount of the number of atoms on
388                  * the context. **/
389                 u32 ctx_attr_ref_count[KBASEP_JS_CTX_ATTR_COUNT];
390
391                 /**
392                  * Wait queue to wait for KCTX_SHEDULED flag state changes.
393                  * */
394                 wait_queue_head_t is_scheduled_wait;
395
396                 /** Link implementing JS queues. Context can be present on one
397                  * list per job slot
398                  */
399                 struct list_head ctx_list_entry[BASE_JM_MAX_NR_SLOTS];
400         } ctx;
401
402         /* The initalized-flag is placed at the end, to avoid cache-pollution (we should
403          * only be using this during init/term paths) */
404         int init_status;
405 };
406
407 /** Subset of atom state that can be available after jd_done_nolock() is called
408  * on that atom. A copy must be taken via kbasep_js_atom_retained_state_copy(),
409  * because the original atom could disappear. */
410 struct kbasep_js_atom_retained_state {
411         /** Event code - to determine whether the atom has finished */
412         enum base_jd_event_code event_code;
413         /** core requirements */
414         base_jd_core_req core_req;
415         /* priority */
416         int sched_priority;
417         /** Job Slot to retry submitting to if submission from IRQ handler failed */
418         int retry_submit_on_slot;
419         /* Core group atom was executed on */
420         u32 device_nr;
421
422 };
423
424 /**
425  * Value signifying 'no retry on a slot required' for:
426  * - kbase_js_atom_retained_state::retry_submit_on_slot
427  * - kbase_jd_atom::retry_submit_on_slot
428  */
429 #define KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID (-1)
430
431 /**
432  * base_jd_core_req value signifying 'invalid' for a kbase_jd_atom_retained_state.
433  *
434  * @see kbase_atom_retained_state_is_valid()
435  */
436 #define KBASEP_JS_ATOM_RETAINED_STATE_CORE_REQ_INVALID BASE_JD_REQ_DEP
437
438 /**
439  * @brief The JS timer resolution, in microseconds
440  *
441  * Any non-zero difference in time will be at least this size.
442  */
443 #define KBASEP_JS_TICK_RESOLUTION_US 1
444
445 /*
446  * Internal atom priority defines for kbase_jd_atom::sched_prio
447  */
448 enum {
449         KBASE_JS_ATOM_SCHED_PRIO_HIGH = 0,
450         KBASE_JS_ATOM_SCHED_PRIO_MED,
451         KBASE_JS_ATOM_SCHED_PRIO_LOW,
452         KBASE_JS_ATOM_SCHED_PRIO_COUNT,
453 };
454
455 /* Invalid priority for kbase_jd_atom::sched_prio */
456 #define KBASE_JS_ATOM_SCHED_PRIO_INVALID -1
457
458 /* Default priority in the case of contexts with no atoms, or being lenient
459  * about invalid priorities from userspace */
460 #define KBASE_JS_ATOM_SCHED_PRIO_DEFAULT KBASE_JS_ATOM_SCHED_PRIO_MED
461
462           /** @} *//* end group kbase_js */
463           /** @} *//* end group base_kbase_api */
464           /** @} *//* end group base_api */
465
466 #endif                          /* _KBASE_JS_DEFS_H_ */