3 * (C) COPYRIGHT 2011-2016 ARM Limited. All rights reserved.
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
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.
21 * @file mali_kbase_js.h
22 * Job Scheduler Type Definitions
25 #ifndef _KBASE_JS_DEFS_H_
26 #define _KBASE_JS_DEFS_H_
29 * @addtogroup base_api
34 * @addtogroup base_kbase_api
39 * @addtogroup kbase_js
47 typedef u32 kbase_context_flags;
49 struct kbasep_atom_req {
50 base_jd_core_req core_req;
51 kbase_context_flags ctx_req;
55 #include "mali_kbase_js_policy_cfs.h"
57 /* Wrapper Interface - doxygen is elsewhere */
58 union kbasep_js_policy {
59 struct kbasep_js_policy_cfs cfs;
62 /* Wrapper Interface - doxygen is elsewhere */
63 union kbasep_js_policy_ctx_info {
64 struct kbasep_js_policy_cfs_ctx cfs;
67 /* Wrapper Interface - doxygen is elsewhere */
68 union kbasep_js_policy_job_info {
69 struct kbasep_js_policy_cfs_job cfs;
73 /** Callback function run on all of a context's jobs registered with the Job
75 typedef void (*kbasep_js_policy_ctx_job_cb)(struct kbase_device *kbdev, struct kbase_jd_atom *katom);
78 * @brief Maximum number of jobs that can be submitted to a job slot whilst
79 * inside the IRQ handler.
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.
85 #define KBASE_JS_MAX_JOB_SUBMIT_PER_SLOT_PER_IRQ 2
88 * @brief the IRQ_THROTTLE time in microseconds
90 * This will be converted via the GPU's clock frequency into a cycle-count.
92 * @note we can make an estimate of the GPU's frequency by periodically
93 * sampling its CYCLE_COUNT register
95 #define KBASE_JS_IRQ_THROTTLE_TIME_US 20
98 * @brief Context attributes
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?"
107 * The boolean value of the context attributes often affect scheduling
108 * decisions, such as affinities to use and job slots to use.
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
114 * - The context holds a refcount of how many atoms have this attribute.
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
120 * @note A context can be both 'Compute' and 'Non Compute' if it contains
121 * both types of jobs.
123 KBASEP_JS_CTX_ATTR_COMPUTE,
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.
131 * @note A context can be both 'Compute' and 'Non Compute' if it contains
132 * both types of jobs.
134 KBASEP_JS_CTX_ATTR_NON_COMPUTE,
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.
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
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.
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.
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.
158 KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES,
160 /** Must be the last in the enum */
161 KBASEP_JS_CTX_ATTR_COUNT
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)
171 /** Combination of KBASE_JS_ATOM_DONE_<...> bits */
172 typedef u32 kbasep_js_atom_done_code;
175 * Data used by the scheduler that is unique for each Address Space.
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).
180 struct kbasep_js_per_as_data {
182 * Ref count of whether this AS is busy, and must not be scheduled out
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.
188 int as_busy_refcount;
190 /** Pointer to the current context on this address space, or NULL for no context */
191 struct kbase_context *kctx;
195 * @brief KBase Device Data Job Scheduler sub-structure
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.
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).
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. */
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.
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 */
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
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).
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];
236 /** Data that is unique for each AS */
237 struct kbasep_js_per_as_data per_as_data[BASE_MAX_NR_AS];
240 * Affinity management and tracking
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
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
252 s8 slot_affinity_refcount[BASE_JM_MAX_NR_SLOTS][64];
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
260 * In addition, this is used to access:
261 * - the kbasep_js_kctx_info::runpool substructure
263 struct mutex runpool_mutex;
266 * Queue Lock, used to access the Policy's queue of contexts independently
269 * Of course, you don't need the Run Pool lock to access this.
271 struct mutex queue_mutex;
274 * Scheduling semaphore. This must be held when calling
277 struct semaphore schedule_sem;
280 * List of contexts that can currently be pulled from
282 struct list_head ctx_list_pullable[BASE_JM_MAX_NR_SLOTS];
284 * List of contexts that can not currently be pulled from, but have
285 * jobs currently running.
287 struct list_head ctx_list_unpullable[BASE_JM_MAX_NR_SLOTS];
289 u16 as_free; /**< Bitpattern of free Address Spaces */
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;
297 * Policy-specific information.
299 * Refer to the structure defined by the current policy to determine which
300 * locks must be held when accessing this.
302 union kbasep_js_policy policy;
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];
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 */
321 /**< Value for JS_SOFT_JOB_TIMEOUT */
322 atomic_t soft_job_timeout_ms;
324 /** List of suspended soft jobs */
325 struct list_head suspended_soft_jobs_list;
327 #ifdef CONFIG_MALI_DEBUG
328 /* Support soft-stop on a single context */
329 bool softstop_always;
330 #endif /* CONFIG_MALI_DEBUG */
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 */
337 /* Number of contexts that can currently be pulled from */
338 u32 nr_contexts_pullable;
340 /* Number of contexts that can either be pulled from or are currently
342 atomic_t nr_contexts_runnable;
346 * @brief KBase Context Job Scheduling information structure
348 * This is a substructure in the struct kbase_context that encapsulates all the
349 * scheduling information.
351 struct kbasep_js_kctx_info {
353 * Runpool substructure. This must only be accessed whilst the Run Pool
354 * mutex ( kbasep_js_device_data::runpool_mutex ) is held.
356 * In addition, the hwaccess_lock may need to be held for certain
359 * @note some of the members could be moved into struct kbasep_js_device_data for
360 * improved d-cache/tlb efficiency.
363 union kbasep_js_policy_ctx_info policy_ctx; /**< Policy-specific context */
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.
373 * You must obtain the jsctx_mutex before accessing any other members of
376 * You may not access any of these members from IRQ context.
379 struct mutex jsctx_mutex; /**< Job Scheduler Context lock */
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
386 /** Context Attributes:
387 * Each is large enough to hold a refcount of the number of atoms on
389 u32 ctx_attr_ref_count[KBASEP_JS_CTX_ATTR_COUNT];
392 * Wait queue to wait for KCTX_SHEDULED flag state changes.
394 wait_queue_head_t is_scheduled_wait;
396 /** Link implementing JS queues. Context can be present on one
399 struct list_head ctx_list_entry[BASE_JM_MAX_NR_SLOTS];
402 /* The initalized-flag is placed at the end, to avoid cache-pollution (we should
403 * only be using this during init/term paths) */
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;
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 */
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
429 #define KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID (-1)
432 * base_jd_core_req value signifying 'invalid' for a kbase_jd_atom_retained_state.
434 * @see kbase_atom_retained_state_is_valid()
436 #define KBASEP_JS_ATOM_RETAINED_STATE_CORE_REQ_INVALID BASE_JD_REQ_DEP
439 * @brief The JS timer resolution, in microseconds
441 * Any non-zero difference in time will be at least this size.
443 #define KBASEP_JS_TICK_RESOLUTION_US 1
446 * Internal atom priority defines for kbase_jd_atom::sched_prio
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,
455 /* Invalid priority for kbase_jd_atom::sched_prio */
456 #define KBASE_JS_ATOM_SCHED_PRIO_INVALID -1
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
462 /** @} *//* end group kbase_js */
463 /** @} *//* end group base_kbase_api */
464 /** @} *//* end group base_api */
466 #endif /* _KBASE_JS_DEFS_H_ */