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
get_queue_by_qid(struct process_queue_manager * pqm,unsigned int qid)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
find_available_queue_slot(struct process_queue_manager * pqm,unsigned int * qid)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 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
58
59 pr_debug("kfd: the new slot id %lu\n", found);
60
61 if (found >= KFD_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
pqm_init(struct process_queue_manager * pqm,struct kfd_process * p)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(KFD_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
pqm_uninit(struct process_queue_manager * pqm)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
create_cp_queue(struct process_queue_manager * pqm,struct kfd_dev * dev,struct queue ** q,struct queue_properties * q_properties,struct file * f,unsigned int qid)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
132 retval = init_queue(q, *q_properties);
133 if (retval != 0)
134 goto err_init_queue;
135
136 (*q)->device = dev;
137 (*q)->process = pqm->process;
138
139 pr_debug("kfd: PQM After init queue");
140
141 return retval;
142
143 err_init_queue:
144 return retval;
145 }
146
pqm_create_queue(struct process_queue_manager * pqm,struct kfd_dev * dev,struct file * f,struct queue_properties * properties,unsigned int flags,enum kfd_queue_type type,unsigned int * qid)147 int pqm_create_queue(struct process_queue_manager *pqm,
148 struct kfd_dev *dev,
149 struct file *f,
150 struct queue_properties *properties,
151 unsigned int flags,
152 enum kfd_queue_type type,
153 unsigned int *qid)
154 {
155 int retval;
156 struct kfd_process_device *pdd;
157 struct queue_properties q_properties;
158 struct queue *q;
159 struct process_queue_node *pqn;
160 struct kernel_queue *kq;
161 int num_queues = 0;
162 struct queue *cur;
163
164 BUG_ON(!pqm || !dev || !properties || !qid);
165
166 memset(&q_properties, 0, sizeof(struct queue_properties));
167 memcpy(&q_properties, properties, sizeof(struct queue_properties));
168 q = NULL;
169 kq = NULL;
170
171 pdd = kfd_get_process_device_data(dev, pqm->process);
172 if (!pdd) {
173 pr_err("Process device data doesn't exist\n");
174 return -1;
175 }
176
177 /*
178 * for debug process, verify that it is within the static queues limit
179 * currently limit is set to half of the total avail HQD slots
180 * If we are just about to create DIQ, the is_debug flag is not set yet
181 * Hence we also check the type as well
182 */
183 if ((pdd->qpd.is_debug) ||
184 (type == KFD_QUEUE_TYPE_DIQ)) {
185 list_for_each_entry(cur, &pdd->qpd.queues_list, list)
186 num_queues++;
187 if (num_queues >= dev->device_info->max_no_of_hqd/2)
188 return (-ENOSPC);
189 }
190
191 retval = find_available_queue_slot(pqm, qid);
192 if (retval != 0)
193 return retval;
194
195 if (list_empty(&pqm->queues)) {
196 pdd->qpd.pqm = pqm;
197 dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
198 }
199
200 pqn = kzalloc(sizeof(struct process_queue_node), GFP_KERNEL);
201 if (!pqn) {
202 retval = -ENOMEM;
203 goto err_allocate_pqn;
204 }
205
206 switch (type) {
207 case KFD_QUEUE_TYPE_SDMA:
208 if (dev->dqm->queue_count >=
209 CIK_SDMA_QUEUES_PER_ENGINE * CIK_SDMA_ENGINE_NUM) {
210 pr_err("Over-subscription is not allowed for SDMA.\n");
211 retval = -EPERM;
212 goto err_create_queue;
213 }
214
215 retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
216 if (retval != 0)
217 goto err_create_queue;
218 pqn->q = q;
219 pqn->kq = NULL;
220 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd,
221 &q->properties.vmid);
222 pr_debug("DQM returned %d for create_queue\n", retval);
223 print_queue(q);
224 break;
225
226 case KFD_QUEUE_TYPE_COMPUTE:
227 /* check if there is over subscription */
228 if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
229 ((dev->dqm->processes_count >= VMID_PER_DEVICE) ||
230 (dev->dqm->queue_count >= PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE))) {
231 pr_err("kfd: over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
232 retval = -EPERM;
233 goto err_create_queue;
234 }
235
236 retval = create_cp_queue(pqm, dev, &q, &q_properties, f, *qid);
237 if (retval != 0)
238 goto err_create_queue;
239 pqn->q = q;
240 pqn->kq = NULL;
241 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd,
242 &q->properties.vmid);
243 pr_debug("DQM returned %d for create_queue\n", retval);
244 print_queue(q);
245 break;
246 case KFD_QUEUE_TYPE_DIQ:
247 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
248 if (kq == NULL) {
249 retval = -ENOMEM;
250 goto err_create_queue;
251 }
252 kq->queue->properties.queue_id = *qid;
253 pqn->kq = kq;
254 pqn->q = NULL;
255 retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
256 kq, &pdd->qpd);
257 break;
258 default:
259 BUG();
260 break;
261 }
262
263 if (retval != 0) {
264 pr_debug("Error dqm create queue\n");
265 goto err_create_queue;
266 }
267
268 pr_debug("kfd: PQM After DQM create queue\n");
269
270 list_add(&pqn->process_queue_list, &pqm->queues);
271
272 if (q) {
273 *properties = q->properties;
274 pr_debug("kfd: PQM done creating queue\n");
275 print_queue_properties(properties);
276 }
277
278 return retval;
279
280 err_create_queue:
281 kfree(pqn);
282 err_allocate_pqn:
283 /* check if queues list is empty unregister process from device */
284 clear_bit(*qid, pqm->queue_slot_bitmap);
285 if (list_empty(&pqm->queues))
286 dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
287 return retval;
288 }
289
pqm_destroy_queue(struct process_queue_manager * pqm,unsigned int qid)290 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
291 {
292 struct process_queue_node *pqn;
293 struct kfd_process_device *pdd;
294 struct device_queue_manager *dqm;
295 struct kfd_dev *dev;
296 int retval;
297
298 dqm = NULL;
299
300 BUG_ON(!pqm);
301 retval = 0;
302
303 pr_debug("kfd: In Func %s\n", __func__);
304
305 pqn = get_queue_by_qid(pqm, qid);
306 if (pqn == NULL) {
307 pr_err("kfd: queue id does not match any known queue\n");
308 return -EINVAL;
309 }
310
311 dev = NULL;
312 if (pqn->kq)
313 dev = pqn->kq->dev;
314 if (pqn->q)
315 dev = pqn->q->device;
316 BUG_ON(!dev);
317
318 pdd = kfd_get_process_device_data(dev, pqm->process);
319 if (!pdd) {
320 pr_err("Process device data doesn't exist\n");
321 return -1;
322 }
323
324 if (pqn->kq) {
325 /* destroy kernel queue (DIQ) */
326 dqm = pqn->kq->dev->dqm;
327 dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
328 kernel_queue_uninit(pqn->kq);
329 }
330
331 if (pqn->q) {
332 dqm = pqn->q->device->dqm;
333 retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
334 if (retval != 0)
335 return retval;
336
337 uninit_queue(pqn->q);
338 }
339
340 list_del(&pqn->process_queue_list);
341 kfree(pqn);
342 clear_bit(qid, pqm->queue_slot_bitmap);
343
344 if (list_empty(&pqm->queues))
345 dqm->ops.unregister_process(dqm, &pdd->qpd);
346
347 return retval;
348 }
349
pqm_update_queue(struct process_queue_manager * pqm,unsigned int qid,struct queue_properties * p)350 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
351 struct queue_properties *p)
352 {
353 int retval;
354 struct process_queue_node *pqn;
355
356 BUG_ON(!pqm);
357
358 pqn = get_queue_by_qid(pqm, qid);
359 if (!pqn) {
360 pr_debug("amdkfd: No queue %d exists for update operation\n",
361 qid);
362 return -EFAULT;
363 }
364
365 pqn->q->properties.queue_address = p->queue_address;
366 pqn->q->properties.queue_size = p->queue_size;
367 pqn->q->properties.queue_percent = p->queue_percent;
368 pqn->q->properties.priority = p->priority;
369
370 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
371 pqn->q);
372 if (retval != 0)
373 return retval;
374
375 return 0;
376 }
377
pqm_get_kernel_queue(struct process_queue_manager * pqm,unsigned int qid)378 struct kernel_queue *pqm_get_kernel_queue(
379 struct process_queue_manager *pqm,
380 unsigned int qid)
381 {
382 struct process_queue_node *pqn;
383
384 BUG_ON(!pqm);
385
386 pqn = get_queue_by_qid(pqm, qid);
387 if (pqn && pqn->kq)
388 return pqn->kq;
389
390 return NULL;
391 }
392
393
394