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 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
find_available_queue_slot(struct process_queue_manager * pqm,unsigned int * qid)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
pqm_init(struct process_queue_manager * pqm,struct kfd_process * p)66 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
67 {
68 INIT_LIST_HEAD(&pqm->queues);
69 pqm->queue_slot_bitmap =
70 kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
71 BITS_PER_BYTE), GFP_KERNEL);
72 if (!pqm->queue_slot_bitmap)
73 return -ENOMEM;
74 pqm->process = p;
75
76 return 0;
77 }
78
pqm_uninit(struct process_queue_manager * pqm)79 void pqm_uninit(struct process_queue_manager *pqm)
80 {
81 int retval;
82 struct process_queue_node *pqn, *next;
83
84 list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
85 retval = pqm_destroy_queue(
86 pqm,
87 (pqn->q != NULL) ?
88 pqn->q->properties.queue_id :
89 pqn->kq->queue->properties.queue_id);
90
91 if (retval != 0) {
92 pr_err("failed to destroy queue\n");
93 return;
94 }
95 }
96 kfree(pqm->queue_slot_bitmap);
97 pqm->queue_slot_bitmap = NULL;
98 }
99
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)100 static int create_cp_queue(struct process_queue_manager *pqm,
101 struct kfd_dev *dev, struct queue **q,
102 struct queue_properties *q_properties,
103 struct file *f, unsigned int qid)
104 {
105 int retval;
106
107 /* Doorbell initialized in user space*/
108 q_properties->doorbell_ptr = NULL;
109
110 q_properties->doorbell_off =
111 kfd_queue_id_to_doorbell(dev, pqm->process, qid);
112
113 /* let DQM handle it*/
114 q_properties->vmid = 0;
115 q_properties->queue_id = qid;
116
117 retval = init_queue(q, q_properties);
118 if (retval != 0)
119 return retval;
120
121 (*q)->device = dev;
122 (*q)->process = pqm->process;
123
124 pr_debug("PQM After init queue");
125
126 return retval;
127 }
128
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)129 int pqm_create_queue(struct process_queue_manager *pqm,
130 struct kfd_dev *dev,
131 struct file *f,
132 struct queue_properties *properties,
133 unsigned int flags,
134 enum kfd_queue_type type,
135 unsigned int *qid)
136 {
137 int retval;
138 struct kfd_process_device *pdd;
139 struct queue_properties q_properties;
140 struct queue *q;
141 struct process_queue_node *pqn;
142 struct kernel_queue *kq;
143 int num_queues = 0;
144 struct queue *cur;
145
146 memcpy(&q_properties, properties, sizeof(struct queue_properties));
147 q = NULL;
148 kq = NULL;
149
150 pdd = kfd_get_process_device_data(dev, pqm->process);
151 if (!pdd) {
152 pr_err("Process device data doesn't exist\n");
153 return -1;
154 }
155
156 /*
157 * for debug process, verify that it is within the static queues limit
158 * currently limit is set to half of the total avail HQD slots
159 * If we are just about to create DIQ, the is_debug flag is not set yet
160 * Hence we also check the type as well
161 */
162 if ((pdd->qpd.is_debug) ||
163 (type == KFD_QUEUE_TYPE_DIQ)) {
164 list_for_each_entry(cur, &pdd->qpd.queues_list, list)
165 num_queues++;
166 if (num_queues >= dev->device_info->max_no_of_hqd/2)
167 return -ENOSPC;
168 }
169
170 retval = find_available_queue_slot(pqm, qid);
171 if (retval != 0)
172 return retval;
173
174 if (list_empty(&pqm->queues)) {
175 pdd->qpd.pqm = pqm;
176 dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
177 }
178
179 pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
180 if (!pqn) {
181 retval = -ENOMEM;
182 goto err_allocate_pqn;
183 }
184
185 switch (type) {
186 case KFD_QUEUE_TYPE_SDMA:
187 if (dev->dqm->queue_count >=
188 CIK_SDMA_QUEUES_PER_ENGINE * CIK_SDMA_ENGINE_NUM) {
189 pr_err("Over-subscription is not allowed for SDMA.\n");
190 retval = -EPERM;
191 goto err_create_queue;
192 }
193
194 retval = create_cp_queue(pqm, dev, &q, properties, f, *qid);
195 if (retval != 0)
196 goto err_create_queue;
197 pqn->q = q;
198 pqn->kq = NULL;
199 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd,
200 &q->properties.vmid);
201 pr_debug("DQM returned %d for create_queue\n", retval);
202 print_queue(q);
203 break;
204
205 case KFD_QUEUE_TYPE_COMPUTE:
206 /* check if there is over subscription */
207 if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
208 ((dev->dqm->processes_count >= VMID_PER_DEVICE) ||
209 (dev->dqm->queue_count >= get_queues_num(dev->dqm)))) {
210 pr_err("Over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
211 retval = -EPERM;
212 goto err_create_queue;
213 }
214
215 retval = create_cp_queue(pqm, dev, &q, &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 case KFD_QUEUE_TYPE_DIQ:
226 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
227 if (!kq) {
228 retval = -ENOMEM;
229 goto err_create_queue;
230 }
231 kq->queue->properties.queue_id = *qid;
232 pqn->kq = kq;
233 pqn->q = NULL;
234 retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
235 kq, &pdd->qpd);
236 break;
237 default:
238 WARN(1, "Invalid queue type %d", type);
239 retval = -EINVAL;
240 }
241
242 if (retval != 0) {
243 pr_err("DQM create queue failed\n");
244 goto err_create_queue;
245 }
246
247 pr_debug("PQM After DQM create queue\n");
248
249 list_add(&pqn->process_queue_list, &pqm->queues);
250
251 if (q) {
252 *properties = q->properties;
253 pr_debug("PQM done creating queue\n");
254 print_queue_properties(properties);
255 }
256
257 return retval;
258
259 err_create_queue:
260 kfree(pqn);
261 err_allocate_pqn:
262 /* check if queues list is empty unregister process from device */
263 clear_bit(*qid, pqm->queue_slot_bitmap);
264 if (list_empty(&pqm->queues))
265 dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
266 return retval;
267 }
268
pqm_destroy_queue(struct process_queue_manager * pqm,unsigned int qid)269 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
270 {
271 struct process_queue_node *pqn;
272 struct kfd_process_device *pdd;
273 struct device_queue_manager *dqm;
274 struct kfd_dev *dev;
275 int retval;
276
277 dqm = NULL;
278
279 retval = 0;
280
281 pqn = get_queue_by_qid(pqm, qid);
282 if (!pqn) {
283 pr_err("Queue id does not match any known queue\n");
284 return -EINVAL;
285 }
286
287 dev = NULL;
288 if (pqn->kq)
289 dev = pqn->kq->dev;
290 if (pqn->q)
291 dev = pqn->q->device;
292 if (WARN_ON(!dev))
293 return -ENODEV;
294
295 pdd = kfd_get_process_device_data(dev, pqm->process);
296 if (!pdd) {
297 pr_err("Process device data doesn't exist\n");
298 return -1;
299 }
300
301 if (pqn->kq) {
302 /* destroy kernel queue (DIQ) */
303 dqm = pqn->kq->dev->dqm;
304 dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
305 kernel_queue_uninit(pqn->kq);
306 }
307
308 if (pqn->q) {
309 dqm = pqn->q->device->dqm;
310 retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
311 if (retval != 0)
312 return retval;
313
314 uninit_queue(pqn->q);
315 }
316
317 list_del(&pqn->process_queue_list);
318 kfree(pqn);
319 clear_bit(qid, pqm->queue_slot_bitmap);
320
321 if (list_empty(&pqm->queues))
322 dqm->ops.unregister_process(dqm, &pdd->qpd);
323
324 return retval;
325 }
326
pqm_update_queue(struct process_queue_manager * pqm,unsigned int qid,struct queue_properties * p)327 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
328 struct queue_properties *p)
329 {
330 int retval;
331 struct process_queue_node *pqn;
332
333 pqn = get_queue_by_qid(pqm, qid);
334 if (!pqn) {
335 pr_debug("No queue %d exists for update operation\n", qid);
336 return -EFAULT;
337 }
338
339 pqn->q->properties.queue_address = p->queue_address;
340 pqn->q->properties.queue_size = p->queue_size;
341 pqn->q->properties.queue_percent = p->queue_percent;
342 pqn->q->properties.priority = p->priority;
343
344 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
345 pqn->q);
346 if (retval != 0)
347 return retval;
348
349 return 0;
350 }
351
pqm_get_kernel_queue(struct process_queue_manager * pqm,unsigned int qid)352 struct kernel_queue *pqm_get_kernel_queue(
353 struct process_queue_manager *pqm,
354 unsigned int qid)
355 {
356 struct process_queue_node *pqn;
357
358 pqn = get_queue_by_qid(pqm, qid);
359 if (pqn && pqn->kq)
360 return pqn->kq;
361
362 return NULL;
363 }
364
365
366