Commit | Line | Data |
---|---|---|
45102048 BG |
1 | /* |
2 | * Copyright 2014 Advanced Micro Devices, Inc. | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
20 | * OTHER DEALINGS IN THE SOFTWARE. | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/slab.h> | |
25 | #include <linux/list.h> | |
26 | #include "kfd_device_queue_manager.h" | |
27 | #include "kfd_priv.h" | |
28 | #include "kfd_kernel_queue.h" | |
29 | ||
30 | static inline struct process_queue_node *get_queue_by_qid( | |
31 | struct process_queue_manager *pqm, unsigned int qid) | |
32 | { | |
33 | struct process_queue_node *pqn; | |
34 | ||
35 | BUG_ON(!pqm); | |
36 | ||
37 | list_for_each_entry(pqn, &pqm->queues, process_queue_list) { | |
38 | if (pqn->q && pqn->q->properties.queue_id == qid) | |
39 | return pqn; | |
40 | if (pqn->kq && pqn->kq->queue->properties.queue_id == qid) | |
41 | return pqn; | |
42 | } | |
43 | ||
44 | return NULL; | |
45 | } | |
46 | ||
47 | static int find_available_queue_slot(struct process_queue_manager *pqm, | |
48 | unsigned int *qid) | |
49 | { | |
50 | unsigned long found; | |
51 | ||
52 | BUG_ON(!pqm || !qid); | |
53 | ||
54 | pr_debug("kfd: in %s\n", __func__); | |
55 | ||
56 | found = find_first_zero_bit(pqm->queue_slot_bitmap, | |
57 | max_num_of_queues_per_process); | |
58 | ||
59 | pr_debug("kfd: the new slot id %lu\n", found); | |
60 | ||
61 | if (found >= max_num_of_queues_per_process) { | |
62 | pr_info("amdkfd: Can not open more queues for process with pasid %d\n", | |
63 | pqm->process->pasid); | |
64 | return -ENOMEM; | |
65 | } | |
66 | ||
67 | set_bit(found, pqm->queue_slot_bitmap); | |
68 | *qid = found; | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p) | |
74 | { | |
75 | BUG_ON(!pqm); | |
76 | ||
77 | INIT_LIST_HEAD(&pqm->queues); | |
78 | pqm->queue_slot_bitmap = | |
79 | kzalloc(DIV_ROUND_UP(max_num_of_queues_per_process, | |
80 | BITS_PER_BYTE), GFP_KERNEL); | |
81 | if (pqm->queue_slot_bitmap == NULL) | |
82 | return -ENOMEM; | |
83 | pqm->process = p; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | void pqm_uninit(struct process_queue_manager *pqm) | |
89 | { | |
90 | int retval; | |
91 | struct process_queue_node *pqn, *next; | |
92 | ||
93 | BUG_ON(!pqm); | |
94 | ||
95 | pr_debug("In func %s\n", __func__); | |
96 | ||
97 | list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) { | |
98 | retval = pqm_destroy_queue( | |
99 | pqm, | |
100 | (pqn->q != NULL) ? | |
101 | pqn->q->properties.queue_id : | |
102 | pqn->kq->queue->properties.queue_id); | |
103 | ||
104 | if (retval != 0) { | |
105 | pr_err("kfd: failed to destroy queue\n"); | |
106 | return; | |
107 | } | |
108 | } | |
109 | kfree(pqm->queue_slot_bitmap); | |
110 | pqm->queue_slot_bitmap = NULL; | |
111 | } | |
112 | ||
113 | static int create_cp_queue(struct process_queue_manager *pqm, | |
114 | struct kfd_dev *dev, struct queue **q, | |
115 | struct queue_properties *q_properties, | |
116 | struct file *f, unsigned int qid) | |
117 | { | |
118 | int retval; | |
119 | ||
120 | retval = 0; | |
121 | ||
122 | /* Doorbell initialized in user space*/ | |
123 | q_properties->doorbell_ptr = NULL; | |
124 | ||
125 | q_properties->doorbell_off = | |
126 | kfd_queue_id_to_doorbell(dev, pqm->process, qid); | |
127 | ||
128 | /* let DQM handle it*/ | |
129 | q_properties->vmid = 0; | |
130 | q_properties->queue_id = qid; | |
131 | q_properties->type = KFD_QUEUE_TYPE_COMPUTE; | |
132 | ||
133 | retval = init_queue(q, *q_properties); | |
134 | if (retval != 0) | |
135 | goto err_init_queue; | |
136 | ||
137 | (*q)->device = dev; | |
138 | (*q)->process = pqm->process; | |
139 | ||
140 | pr_debug("kfd: PQM After init queue"); | |
141 | ||
142 | return retval; | |
143 | ||
144 | err_init_queue: | |
145 | return retval; | |
146 | } | |
147 | ||
148 | int pqm_create_queue(struct process_queue_manager *pqm, | |
149 | struct kfd_dev *dev, | |
150 | struct file *f, | |
151 | struct queue_properties *properties, | |
152 | unsigned int flags, | |
153 | enum kfd_queue_type type, | |
154 | unsigned int *qid) | |
155 | { | |
156 | int retval; | |
157 | struct kfd_process_device *pdd; | |
158 | struct queue_properties q_properties; | |
159 | struct queue *q; | |
160 | struct process_queue_node *pqn; | |
161 | struct kernel_queue *kq; | |
162 | ||
163 | BUG_ON(!pqm || !dev || !properties || !qid); | |
164 | ||
165 | memset(&q_properties, 0, sizeof(struct queue_properties)); | |
166 | memcpy(&q_properties, properties, sizeof(struct queue_properties)); | |
167 | q = NULL; | |
168 | kq = NULL; | |
169 | ||
170 | pdd = kfd_get_process_device_data(dev, pqm->process, 1); | |
171 | BUG_ON(!pdd); | |
172 | ||
173 | retval = find_available_queue_slot(pqm, qid); | |
174 | if (retval != 0) | |
175 | return retval; | |
176 | ||
177 | if (list_empty(&pqm->queues)) { | |
178 | pdd->qpd.pqm = pqm; | |
179 | dev->dqm->register_process(dev->dqm, &pdd->qpd); | |
180 | } | |
181 | ||
182 | pqn = kzalloc(sizeof(struct process_queue_node), GFP_KERNEL); | |
183 | if (!pqn) { | |
184 | retval = -ENOMEM; | |
185 | goto err_allocate_pqn; | |
186 | } | |
187 | ||
188 | switch (type) { | |
189 | case KFD_QUEUE_TYPE_COMPUTE: | |
190 | /* check if there is over subscription */ | |
191 | if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) && | |
192 | ((dev->dqm->processes_count >= VMID_PER_DEVICE) || | |
193 | (dev->dqm->queue_count >= PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE))) { | |
194 | pr_err("kfd: over-subscription is not allowed in radeon_kfd.sched_policy == 1\n"); | |
195 | retval = -EPERM; | |
196 | goto err_create_queue; | |
197 | } | |
198 | ||
199 | retval = create_cp_queue(pqm, dev, &q, &q_properties, f, *qid); | |
200 | if (retval != 0) | |
201 | goto err_create_queue; | |
202 | pqn->q = q; | |
203 | pqn->kq = NULL; | |
204 | retval = dev->dqm->create_queue(dev->dqm, q, &pdd->qpd, | |
205 | &q->properties.vmid); | |
206 | print_queue(q); | |
207 | break; | |
208 | case KFD_QUEUE_TYPE_DIQ: | |
209 | kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ); | |
210 | if (kq == NULL) { | |
e048a0b2 | 211 | retval = -ENOMEM; |
45102048 BG |
212 | goto err_create_queue; |
213 | } | |
214 | kq->queue->properties.queue_id = *qid; | |
215 | pqn->kq = kq; | |
216 | pqn->q = NULL; | |
217 | retval = dev->dqm->create_kernel_queue(dev->dqm, kq, &pdd->qpd); | |
218 | break; | |
219 | default: | |
220 | BUG(); | |
221 | break; | |
222 | } | |
223 | ||
224 | if (retval != 0) { | |
225 | pr_err("kfd: error dqm create queue\n"); | |
226 | goto err_create_queue; | |
227 | } | |
228 | ||
229 | pr_debug("kfd: PQM After DQM create queue\n"); | |
230 | ||
231 | list_add(&pqn->process_queue_list, &pqm->queues); | |
232 | ||
233 | if (q) { | |
234 | *properties = q->properties; | |
235 | pr_debug("kfd: PQM done creating queue\n"); | |
236 | print_queue_properties(properties); | |
237 | } | |
238 | ||
239 | return retval; | |
240 | ||
241 | err_create_queue: | |
242 | kfree(pqn); | |
243 | err_allocate_pqn: | |
244 | clear_bit(*qid, pqm->queue_slot_bitmap); | |
245 | return retval; | |
246 | } | |
247 | ||
248 | int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid) | |
249 | { | |
250 | struct process_queue_node *pqn; | |
251 | struct kfd_process_device *pdd; | |
252 | struct device_queue_manager *dqm; | |
253 | struct kfd_dev *dev; | |
254 | int retval; | |
255 | ||
256 | dqm = NULL; | |
257 | ||
258 | BUG_ON(!pqm); | |
259 | retval = 0; | |
260 | ||
261 | pr_debug("kfd: In Func %s\n", __func__); | |
262 | ||
263 | pqn = get_queue_by_qid(pqm, qid); | |
264 | if (pqn == NULL) { | |
265 | pr_err("kfd: queue id does not match any known queue\n"); | |
266 | return -EINVAL; | |
267 | } | |
268 | ||
269 | dev = NULL; | |
270 | if (pqn->kq) | |
271 | dev = pqn->kq->dev; | |
272 | if (pqn->q) | |
273 | dev = pqn->q->device; | |
274 | BUG_ON(!dev); | |
275 | ||
276 | pdd = kfd_get_process_device_data(dev, pqm->process, 1); | |
277 | BUG_ON(!pdd); | |
278 | ||
279 | if (pqn->kq) { | |
280 | /* destroy kernel queue (DIQ) */ | |
281 | dqm = pqn->kq->dev->dqm; | |
282 | dqm->destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd); | |
283 | kernel_queue_uninit(pqn->kq); | |
284 | } | |
285 | ||
286 | if (pqn->q) { | |
287 | dqm = pqn->q->device->dqm; | |
288 | retval = dqm->destroy_queue(dqm, &pdd->qpd, pqn->q); | |
289 | if (retval != 0) | |
290 | return retval; | |
291 | ||
292 | uninit_queue(pqn->q); | |
293 | } | |
294 | ||
295 | list_del(&pqn->process_queue_list); | |
296 | kfree(pqn); | |
297 | clear_bit(qid, pqm->queue_slot_bitmap); | |
298 | ||
299 | if (list_empty(&pqm->queues)) | |
300 | dqm->unregister_process(dqm, &pdd->qpd); | |
301 | ||
302 | return retval; | |
303 | } | |
304 | ||
305 | int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid, | |
306 | struct queue_properties *p) | |
307 | { | |
308 | int retval; | |
309 | struct process_queue_node *pqn; | |
310 | ||
311 | BUG_ON(!pqm); | |
312 | ||
313 | pqn = get_queue_by_qid(pqm, qid); | |
314 | BUG_ON(!pqn); | |
315 | ||
316 | pqn->q->properties.queue_address = p->queue_address; | |
317 | pqn->q->properties.queue_size = p->queue_size; | |
318 | pqn->q->properties.queue_percent = p->queue_percent; | |
319 | pqn->q->properties.priority = p->priority; | |
320 | ||
321 | retval = pqn->q->device->dqm->update_queue(pqn->q->device->dqm, pqn->q); | |
322 | if (retval != 0) | |
323 | return retval; | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
7347a6cb | 328 | static __attribute__((unused)) struct kernel_queue *pqm_get_kernel_queue( |
329 | struct process_queue_manager *pqm, | |
45102048 BG |
330 | unsigned int qid) |
331 | { | |
332 | struct process_queue_node *pqn; | |
333 | ||
334 | BUG_ON(!pqm); | |
335 | ||
336 | pqn = get_queue_by_qid(pqm, qid); | |
337 | if (pqn && pqn->kq) | |
338 | return pqn->kq; | |
339 | ||
340 | return NULL; | |
341 | } | |
342 | ||
343 |