]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
Merge drm-fixes-for-v4.17-rc6-urgent into drm-next
[linux.git] / drivers / gpu / drm / amd / amdkfd / kfd_process_queue_manager.c
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         list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
36                 if ((pqn->q && pqn->q->properties.queue_id == qid) ||
37                     (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
38                         return pqn;
39         }
40
41         return NULL;
42 }
43
44 static int find_available_queue_slot(struct process_queue_manager *pqm,
45                                         unsigned int *qid)
46 {
47         unsigned long found;
48
49         found = find_first_zero_bit(pqm->queue_slot_bitmap,
50                         KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
51
52         pr_debug("The new slot id %lu\n", found);
53
54         if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
55                 pr_info("Cannot open more queues for process with pasid %d\n",
56                                 pqm->process->pasid);
57                 return -ENOMEM;
58         }
59
60         set_bit(found, pqm->queue_slot_bitmap);
61         *qid = found;
62
63         return 0;
64 }
65
66 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
67 {
68         struct kfd_dev *dev = pdd->dev;
69
70         if (pdd->already_dequeued)
71                 return;
72
73         dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
74         pdd->already_dequeued = true;
75 }
76
77 void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
78 {
79         struct kfd_process_device *pdd;
80
81         list_for_each_entry(pdd, &p->per_device_data, per_device_list)
82                 kfd_process_dequeue_from_device(pdd);
83 }
84
85 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
86 {
87         INIT_LIST_HEAD(&pqm->queues);
88         pqm->queue_slot_bitmap =
89                         kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
90                                         BITS_PER_BYTE), GFP_KERNEL);
91         if (!pqm->queue_slot_bitmap)
92                 return -ENOMEM;
93         pqm->process = p;
94
95         return 0;
96 }
97
98 void pqm_uninit(struct process_queue_manager *pqm)
99 {
100         struct process_queue_node *pqn, *next;
101
102         list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
103                 uninit_queue(pqn->q);
104                 list_del(&pqn->process_queue_list);
105                 kfree(pqn);
106         }
107
108         kfree(pqm->queue_slot_bitmap);
109         pqm->queue_slot_bitmap = NULL;
110 }
111
112 static int create_cp_queue(struct process_queue_manager *pqm,
113                                 struct kfd_dev *dev, struct queue **q,
114                                 struct queue_properties *q_properties,
115                                 struct file *f, unsigned int qid)
116 {
117         int retval;
118
119         /* Doorbell initialized in user space*/
120         q_properties->doorbell_ptr = NULL;
121
122         /* let DQM handle it*/
123         q_properties->vmid = 0;
124         q_properties->queue_id = qid;
125
126         retval = init_queue(q, q_properties);
127         if (retval != 0)
128                 return retval;
129
130         (*q)->device = dev;
131         (*q)->process = pqm->process;
132
133         pr_debug("PQM After init queue");
134
135         return retval;
136 }
137
138 int pqm_create_queue(struct process_queue_manager *pqm,
139                             struct kfd_dev *dev,
140                             struct file *f,
141                             struct queue_properties *properties,
142                             unsigned int *qid)
143 {
144         int retval;
145         struct kfd_process_device *pdd;
146         struct queue *q;
147         struct process_queue_node *pqn;
148         struct kernel_queue *kq;
149         enum kfd_queue_type type = properties->type;
150         unsigned int max_queues = 127; /* HWS limit */
151
152         q = NULL;
153         kq = NULL;
154
155         pdd = kfd_get_process_device_data(dev, pqm->process);
156         if (!pdd) {
157                 pr_err("Process device data doesn't exist\n");
158                 return -1;
159         }
160
161         /*
162          * for debug process, verify that it is within the static queues limit
163          * currently limit is set to half of the total avail HQD slots
164          * If we are just about to create DIQ, the is_debug flag is not set yet
165          * Hence we also check the type as well
166          */
167         if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
168                 max_queues = dev->device_info->max_no_of_hqd/2;
169
170         if (pdd->qpd.queue_count >= max_queues)
171                 return -ENOSPC;
172
173         retval = find_available_queue_slot(pqm, qid);
174         if (retval != 0)
175                 return retval;
176
177         if (list_empty(&pdd->qpd.queues_list) &&
178             list_empty(&pdd->qpd.priv_queue_list))
179                 dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
180
181         pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
182         if (!pqn) {
183                 retval = -ENOMEM;
184                 goto err_allocate_pqn;
185         }
186
187         switch (type) {
188         case KFD_QUEUE_TYPE_SDMA:
189                 if (dev->dqm->queue_count >=
190                         CIK_SDMA_QUEUES_PER_ENGINE * CIK_SDMA_ENGINE_NUM) {
191                         pr_err("Over-subscription is not allowed for SDMA.\n");
192                         retval = -EPERM;
193                         goto err_create_queue;
194                 }
195
196                 retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
197                 if (retval != 0)
198                         goto err_create_queue;
199                 pqn->q = q;
200                 pqn->kq = NULL;
201                 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
202                 pr_debug("DQM returned %d for create_queue\n", retval);
203                 print_queue(q);
204                 break;
205
206         case KFD_QUEUE_TYPE_COMPUTE:
207                 /* check if there is over subscription */
208                 if ((dev->dqm->sched_policy ==
209                      KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
210                 ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
211                 (dev->dqm->queue_count >= get_queues_num(dev->dqm)))) {
212                         pr_err("Over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
213                         retval = -EPERM;
214                         goto err_create_queue;
215                 }
216
217                 retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
218                 if (retval != 0)
219                         goto err_create_queue;
220                 pqn->q = q;
221                 pqn->kq = NULL;
222                 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
223                 pr_debug("DQM returned %d for create_queue\n", retval);
224                 print_queue(q);
225                 break;
226         case KFD_QUEUE_TYPE_DIQ:
227                 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
228                 if (!kq) {
229                         retval = -ENOMEM;
230                         goto err_create_queue;
231                 }
232                 kq->queue->properties.queue_id = *qid;
233                 pqn->kq = kq;
234                 pqn->q = NULL;
235                 retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
236                                                         kq, &pdd->qpd);
237                 break;
238         default:
239                 WARN(1, "Invalid queue type %d", type);
240                 retval = -EINVAL;
241         }
242
243         if (retval != 0) {
244                 pr_err("Pasid %d DQM create queue %d failed. ret %d\n",
245                         pqm->process->pasid, type, retval);
246                 goto err_create_queue;
247         }
248
249         if (q)
250                 /* Return the doorbell offset within the doorbell page
251                  * to the caller so it can be passed up to user mode
252                  * (in bytes).
253                  */
254                 properties->doorbell_off =
255                         (q->properties.doorbell_off * sizeof(uint32_t)) &
256                         (kfd_doorbell_process_slice(dev) - 1);
257
258         pr_debug("PQM After DQM create queue\n");
259
260         list_add(&pqn->process_queue_list, &pqm->queues);
261
262         if (q) {
263                 pr_debug("PQM done creating queue\n");
264                 print_queue_properties(&q->properties);
265         }
266
267         return retval;
268
269 err_create_queue:
270         kfree(pqn);
271 err_allocate_pqn:
272         /* check if queues list is empty unregister process from device */
273         clear_bit(*qid, pqm->queue_slot_bitmap);
274         if (list_empty(&pdd->qpd.queues_list) &&
275             list_empty(&pdd->qpd.priv_queue_list))
276                 dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
277         return retval;
278 }
279
280 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
281 {
282         struct process_queue_node *pqn;
283         struct kfd_process_device *pdd;
284         struct device_queue_manager *dqm;
285         struct kfd_dev *dev;
286         int retval;
287
288         dqm = NULL;
289
290         retval = 0;
291
292         pqn = get_queue_by_qid(pqm, qid);
293         if (!pqn) {
294                 pr_err("Queue id does not match any known queue\n");
295                 return -EINVAL;
296         }
297
298         dev = NULL;
299         if (pqn->kq)
300                 dev = pqn->kq->dev;
301         if (pqn->q)
302                 dev = pqn->q->device;
303         if (WARN_ON(!dev))
304                 return -ENODEV;
305
306         pdd = kfd_get_process_device_data(dev, pqm->process);
307         if (!pdd) {
308                 pr_err("Process device data doesn't exist\n");
309                 return -1;
310         }
311
312         if (pqn->kq) {
313                 /* destroy kernel queue (DIQ) */
314                 dqm = pqn->kq->dev->dqm;
315                 dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
316                 kernel_queue_uninit(pqn->kq);
317         }
318
319         if (pqn->q) {
320                 dqm = pqn->q->device->dqm;
321                 retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
322                 if (retval) {
323                         pr_err("Pasid %d destroy queue %d failed, ret %d\n",
324                                 pqm->process->pasid,
325                                 pqn->q->properties.queue_id, retval);
326                         if (retval != -ETIME)
327                                 goto err_destroy_queue;
328                 }
329                 uninit_queue(pqn->q);
330         }
331
332         list_del(&pqn->process_queue_list);
333         kfree(pqn);
334         clear_bit(qid, pqm->queue_slot_bitmap);
335
336         if (list_empty(&pdd->qpd.queues_list) &&
337             list_empty(&pdd->qpd.priv_queue_list))
338                 dqm->ops.unregister_process(dqm, &pdd->qpd);
339
340 err_destroy_queue:
341         return retval;
342 }
343
344 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
345                         struct queue_properties *p)
346 {
347         int retval;
348         struct process_queue_node *pqn;
349
350         pqn = get_queue_by_qid(pqm, qid);
351         if (!pqn) {
352                 pr_debug("No queue %d exists for update operation\n", qid);
353                 return -EFAULT;
354         }
355
356         pqn->q->properties.queue_address = p->queue_address;
357         pqn->q->properties.queue_size = p->queue_size;
358         pqn->q->properties.queue_percent = p->queue_percent;
359         pqn->q->properties.priority = p->priority;
360
361         retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
362                                                         pqn->q);
363         if (retval != 0)
364                 return retval;
365
366         return 0;
367 }
368
369 struct kernel_queue *pqm_get_kernel_queue(
370                                         struct process_queue_manager *pqm,
371                                         unsigned int qid)
372 {
373         struct process_queue_node *pqn;
374
375         pqn = get_queue_by_qid(pqm, qid);
376         if (pqn && pqn->kq)
377                 return pqn->kq;
378
379         return NULL;
380 }
381
382 #if defined(CONFIG_DEBUG_FS)
383
384 int pqm_debugfs_mqds(struct seq_file *m, void *data)
385 {
386         struct process_queue_manager *pqm = data;
387         struct process_queue_node *pqn;
388         struct queue *q;
389         enum KFD_MQD_TYPE mqd_type;
390         struct mqd_manager *mqd_manager;
391         int r = 0;
392
393         list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
394                 if (pqn->q) {
395                         q = pqn->q;
396                         switch (q->properties.type) {
397                         case KFD_QUEUE_TYPE_SDMA:
398                                 seq_printf(m, "  SDMA queue on device %x\n",
399                                            q->device->id);
400                                 mqd_type = KFD_MQD_TYPE_SDMA;
401                                 break;
402                         case KFD_QUEUE_TYPE_COMPUTE:
403                                 seq_printf(m, "  Compute queue on device %x\n",
404                                            q->device->id);
405                                 mqd_type = KFD_MQD_TYPE_CP;
406                                 break;
407                         default:
408                                 seq_printf(m,
409                                 "  Bad user queue type %d on device %x\n",
410                                            q->properties.type, q->device->id);
411                                 continue;
412                         }
413                         mqd_manager = q->device->dqm->ops.get_mqd_manager(
414                                 q->device->dqm, mqd_type);
415                 } else if (pqn->kq) {
416                         q = pqn->kq->queue;
417                         mqd_manager = pqn->kq->mqd;
418                         switch (q->properties.type) {
419                         case KFD_QUEUE_TYPE_DIQ:
420                                 seq_printf(m, "  DIQ on device %x\n",
421                                            pqn->kq->dev->id);
422                                 mqd_type = KFD_MQD_TYPE_HIQ;
423                                 break;
424                         default:
425                                 seq_printf(m,
426                                 "  Bad kernel queue type %d on device %x\n",
427                                            q->properties.type,
428                                            pqn->kq->dev->id);
429                                 continue;
430                         }
431                 } else {
432                         seq_printf(m,
433                 "  Weird: Queue node with neither kernel nor user queue\n");
434                         continue;
435                 }
436
437                 r = mqd_manager->debugfs_show_mqd(m, q->mqd);
438                 if (r != 0)
439                         break;
440         }
441
442         return r;
443 }
444
445 #endif
This page took 0.061359 seconds and 4 git commands to generate.