summaryrefslogtreecommitdiff
path: root/mali_kbase/mali_kbase_ctx_sched.h
blob: fd1b82471d26f6bb39b0b664dbb4332b31cb2898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
/*
 *
 * (C) COPYRIGHT 2017-2023 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */

#ifndef _KBASE_CTX_SCHED_H_
#define _KBASE_CTX_SCHED_H_

#include <linux/types.h>

struct kbase_context;
struct kbase_device;

/**
 * DOC: The Context Scheduler manages address space assignment and reference
 * counting to kbase_context. The interface has been designed to minimise
 * interactions between the Job Scheduler and Power Management/MMU to support
 * the existing Job Scheduler interface.
 *
 * The initial implementation of the Context Scheduler does not schedule
 * contexts. Instead it relies on the Job Scheduler to make decisions of
 * when to schedule/evict contexts if address spaces are starved. In the
 * future, once an interface between the CS and JS has been devised to
 * provide enough information about how each context is consuming GPU resources,
 * those decisions can be made in the CS itself, thereby reducing duplicated
 * code.
 */

/**
 * kbase_ctx_sched_init() - Initialise the context scheduler
 * @kbdev: The device for which the context scheduler needs to be initialised
 *
 * This must be called during device initialisation. The number of hardware
 * address spaces must already be established before calling this function.
 *
 * Return: 0 for success, otherwise failure
 */
int kbase_ctx_sched_init(struct kbase_device *kbdev);

/**
 * kbase_ctx_sched_term - Terminate the context scheduler
 * @kbdev: The device for which the context scheduler needs to be terminated
 *
 * This must be called during device termination after all contexts have been
 * destroyed.
 */
void kbase_ctx_sched_term(struct kbase_device *kbdev);

/**
 * kbase_ctx_sched_init_ctx - Initialize per-context data fields for scheduling
 * @kctx: The context to initialize
 *
 * This must be called during context initialization before any other context
 * scheduling functions are called on @kctx
 *
 * Return: 0
 */
int kbase_ctx_sched_init_ctx(struct kbase_context *kctx);

/**
 * kbase_ctx_sched_retain_ctx - Retain a reference to the @ref kbase_context
 * @kctx: The context to which to retain a reference
 *
 * This function should be called whenever an address space should be assigned
 * to a context and programmed onto the MMU. It should typically be called
 * when jobs are ready to be submitted to the GPU.
 *
 * It can be called as many times as necessary. The address space will be
 * assigned to the context for as long as there is a reference to said context.
 *
 * The kbase_device::mmu_hw_mutex and kbase_device::hwaccess_lock locks must be
 * held whilst calling this function.
 *
 * Return: The address space that the context has been assigned to or
 *         KBASEP_AS_NR_INVALID if no address space was available.
 */
int kbase_ctx_sched_retain_ctx(struct kbase_context *kctx);

/**
 * kbase_ctx_sched_retain_ctx_refcount - Retain a reference to the @ref kbase_context
 * @kctx: The context to which to retain a reference
 *
 * This function only retains a reference to the context. It must be called
 * only when the context already has a reference.
 *
 * This is typically called inside an atomic session where we know the context
 * is already scheduled in but want to take an extra reference to ensure that
 * it doesn't get descheduled.
 *
 * The kbase_device::hwaccess_lock must be held whilst calling this function
 */
void kbase_ctx_sched_retain_ctx_refcount(struct kbase_context *kctx);

/**
 * kbase_ctx_sched_release_ctx - Release a reference to the @ref kbase_context
 * @kctx: The context from which to release a reference
 *
 * This function should be called whenever an address space could be unassigned
 * from a context. When there are no more references to said context, the
 * address space previously assigned to this context shall be reassigned to
 * other contexts as needed.
 *
 * The kbase_device::hwaccess_lock must be held whilst calling this function
 */
void kbase_ctx_sched_release_ctx(struct kbase_context *kctx);

/**
 * kbase_ctx_sched_remove_ctx - Unassign previously assigned address space
 * @kctx: The context to be removed
 *
 * This function should be called when a context is being destroyed. The
 * context must no longer have any reference. If it has been assigned an
 * address space before then the AS will be unprogrammed.
 */
void kbase_ctx_sched_remove_ctx(struct kbase_context *kctx);

/**
 * kbase_ctx_sched_restore_all_as - Reprogram all address spaces
 * @kbdev: The device for which address spaces to be reprogrammed
 *
 * This function shall reprogram all address spaces previously assigned to
 * contexts. It can be used after the GPU is reset.
 *
 * The kbase_device::mmu_hw_mutex and kbase_device::hwaccess_lock locks must be
 * held whilst calling this function.
 */
void kbase_ctx_sched_restore_all_as(struct kbase_device *kbdev);

/**
 * kbase_ctx_sched_as_to_ctx_refcount - Lookup a context based on its current
 * address space and ensure that is stays scheduled in
 * @kbdev: The device for which the returned context must belong
 * @as_nr: address space assigned to the context of interest
 *
 * The context is refcounted as being busy to prevent it from scheduling
 * out. It must be released with kbase_ctx_sched_release_ctx() when it is no
 * longer required to stay scheduled in.
 *
 * This function can safely be called from IRQ context.
 *
 * The following locking conditions are made on the caller:
 * * it must not hold the kbase_device::hwaccess_lock, because it will be used
 *   internally.
 *
 * Return: a valid struct kbase_context on success, which has been refcounted
 * as being busy or return NULL on failure, indicating that no context was found
 * in as_nr.
 */
struct kbase_context *kbase_ctx_sched_as_to_ctx_refcount(struct kbase_device *kbdev, size_t as_nr);

/**
 * kbase_ctx_sched_as_to_ctx - Lookup a context based on its current address
 * space
 * @kbdev: The device for which the returned context must belong
 * @as_nr: address space assigned to the context of interest
 *
 * Return: a valid struct kbase_context on success or NULL on failure,
 * indicating that no context was found in as_nr.
 */
struct kbase_context *kbase_ctx_sched_as_to_ctx(struct kbase_device *kbdev, size_t as_nr);

/**
 * kbase_ctx_sched_as_to_ctx_nolock - Lookup a context based on its current
 * address space.
 * @kbdev: The device for which the returned context must belong
 * @as_nr: address space assigned to the context of interest
 *
 * The following lock must be held by the caller:
 * * kbase_device::hwaccess_lock
 *
 * Return: a valid struct kbase_context on success or NULL on failure,
 * indicating that no context was found in as_nr.
 */
struct kbase_context *kbase_ctx_sched_as_to_ctx_nolock(struct kbase_device *kbdev, size_t as_nr);

/**
 * kbase_ctx_sched_inc_refcount_nolock - Refcount a context as being busy,
 * preventing it from being scheduled out.
 * @kctx: Context to be refcounted
 *
 * The following locks must be held by the caller:
 * &kbase_device.mmu_hw_mutex
 * &kbase_device.hwaccess_lock
 *
 * Return: true if refcount succeeded, and the context will not be scheduled
 * out, false if the refcount failed (because the context is being/has been
 * scheduled out).
 */
bool kbase_ctx_sched_inc_refcount_nolock(struct kbase_context *kctx);

/**
 * kbase_ctx_sched_inc_refcount - Refcount a context as being busy, preventing
 * it from being scheduled out.
 * @kctx: Context to be refcounted
 *
 * The following locking conditions are made on the caller:
 * * it must not hold kbase_device::mmu_hw_mutex and
 *   kbase_device::hwaccess_lock, because they will be used internally.
 *
 * Return: true if refcount succeeded, and the context will not be scheduled
 * out, false if the refcount failed (because the context is being/has been
 * scheduled out).
 */
bool kbase_ctx_sched_inc_refcount(struct kbase_context *kctx);

/**
 * kbase_ctx_sched_release_ctx_lock - Release a reference count of a context
 * @kctx: Context for which refcount should be decreased
 *
 * Effectivelly, this is a wrapper for kbase_ctx_sched_release_ctx, but
 * kbase_device::hwaccess_lock is required NOT to be locked.
 */
void kbase_ctx_sched_release_ctx_lock(struct kbase_context *kctx);

#if MALI_USE_CSF
/**
 * kbase_ctx_sched_inc_refcount_if_as_valid - Refcount the context if it has GPU
 *                                            address space slot assigned to it.
 *
 * @kctx: Context to be refcounted
 *
 * This function takes a reference on the context if it has a GPU address space
 * slot assigned to it. The address space slot will not be available for
 * re-assignment until the reference is released.
 *
 * Return: true if refcount succeeded and the address space slot will not be
 * reassigned, false if the refcount failed (because the address space slot
 * was not assigned).
 */
bool kbase_ctx_sched_inc_refcount_if_as_valid(struct kbase_context *kctx);
#endif

#endif /* _KBASE_CTX_SCHED_H_ */