• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/slab.h>
26 #include <linux/list.h>
27 #include "kfd_device_queue_manager.h"
28 #include "kfd_priv.h"
29 #include "kfd_kernel_queue.h"
30 #include "amdgpu_amdkfd.h"
31 #include "amdgpu_reset.h"
32 
get_queue_by_qid(struct process_queue_manager * pqm,unsigned int qid)33 static inline struct process_queue_node *get_queue_by_qid(
34 			struct process_queue_manager *pqm, unsigned int qid)
35 {
36 	struct process_queue_node *pqn;
37 
38 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
39 		if ((pqn->q && pqn->q->properties.queue_id == qid) ||
40 		    (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
41 			return pqn;
42 	}
43 
44 	return NULL;
45 }
46 
assign_queue_slot_by_qid(struct process_queue_manager * pqm,unsigned int qid)47 static int assign_queue_slot_by_qid(struct process_queue_manager *pqm,
48 				    unsigned int qid)
49 {
50 	if (qid >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
51 		return -EINVAL;
52 
53 	if (__test_and_set_bit(qid, pqm->queue_slot_bitmap)) {
54 		pr_err("Cannot create new queue because requested qid(%u) is in use\n", qid);
55 		return -ENOSPC;
56 	}
57 
58 	return 0;
59 }
60 
find_available_queue_slot(struct process_queue_manager * pqm,unsigned int * qid)61 static int find_available_queue_slot(struct process_queue_manager *pqm,
62 					unsigned int *qid)
63 {
64 	unsigned long found;
65 
66 	found = find_first_zero_bit(pqm->queue_slot_bitmap,
67 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
68 
69 	pr_debug("The new slot id %lu\n", found);
70 
71 	if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
72 		pr_info("Cannot open more queues for process with pasid 0x%x\n",
73 				pqm->process->pasid);
74 		return -ENOMEM;
75 	}
76 
77 	set_bit(found, pqm->queue_slot_bitmap);
78 	*qid = found;
79 
80 	return 0;
81 }
82 
kfd_process_dequeue_from_device(struct kfd_process_device * pdd)83 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
84 {
85 	struct kfd_node *dev = pdd->dev;
86 
87 	if (pdd->already_dequeued)
88 		return;
89 	/* The MES context flush needs to filter out the case which the
90 	 * KFD process is created without setting up the MES context and
91 	 * queue for creating a compute queue.
92 	 */
93 	dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
94 	if (dev->kfd->shared_resources.enable_mes && !!pdd->proc_ctx_gpu_addr &&
95 	    down_read_trylock(&dev->adev->reset_domain->sem)) {
96 		amdgpu_mes_flush_shader_debugger(dev->adev,
97 						 pdd->proc_ctx_gpu_addr);
98 		up_read(&dev->adev->reset_domain->sem);
99 	}
100 	pdd->already_dequeued = true;
101 }
102 
pqm_set_gws(struct process_queue_manager * pqm,unsigned int qid,void * gws)103 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
104 			void *gws)
105 {
106 	struct mqd_update_info minfo = {0};
107 	struct kfd_node *dev = NULL;
108 	struct process_queue_node *pqn;
109 	struct kfd_process_device *pdd;
110 	struct kgd_mem *mem = NULL;
111 	int ret;
112 
113 	pqn = get_queue_by_qid(pqm, qid);
114 	if (!pqn) {
115 		pr_err("Queue id does not match any known queue\n");
116 		return -EINVAL;
117 	}
118 
119 	if (pqn->q)
120 		dev = pqn->q->device;
121 	if (WARN_ON(!dev))
122 		return -ENODEV;
123 
124 	pdd = kfd_get_process_device_data(dev, pqm->process);
125 	if (!pdd) {
126 		pr_err("Process device data doesn't exist\n");
127 		return -EINVAL;
128 	}
129 
130 	/* Only allow one queue per process can have GWS assigned */
131 	if (gws && pdd->qpd.num_gws)
132 		return -EBUSY;
133 
134 	if (!gws && pdd->qpd.num_gws == 0)
135 		return -EINVAL;
136 
137 	if (KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 3) &&
138 	    KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 4) &&
139 	    !dev->kfd->shared_resources.enable_mes) {
140 		if (gws)
141 			ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info,
142 				gws, &mem);
143 		else
144 			ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info,
145 				pqn->q->gws);
146 		if (unlikely(ret))
147 			return ret;
148 		pqn->q->gws = mem;
149 	} else {
150 		/*
151 		 * Intentionally set GWS to a non-NULL value
152 		 * for devices that do not use GWS for global wave
153 		 * synchronization but require the formality
154 		 * of setting GWS for cooperative groups.
155 		 */
156 		pqn->q->gws = gws ? ERR_PTR(-ENOMEM) : NULL;
157 	}
158 
159 	pdd->qpd.num_gws = gws ? dev->adev->gds.gws_size : 0;
160 	minfo.update_flag = gws ? UPDATE_FLAG_IS_GWS : 0;
161 
162 	return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
163 							pqn->q, &minfo);
164 }
165 
kfd_process_dequeue_from_all_devices(struct kfd_process * p)166 void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
167 {
168 	int i;
169 
170 	for (i = 0; i < p->n_pdds; i++)
171 		kfd_process_dequeue_from_device(p->pdds[i]);
172 }
173 
pqm_init(struct process_queue_manager * pqm,struct kfd_process * p)174 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
175 {
176 	INIT_LIST_HEAD(&pqm->queues);
177 	pqm->queue_slot_bitmap = bitmap_zalloc(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
178 					       GFP_KERNEL);
179 	if (!pqm->queue_slot_bitmap)
180 		return -ENOMEM;
181 	pqm->process = p;
182 
183 	return 0;
184 }
185 
pqm_clean_queue_resource(struct process_queue_manager * pqm,struct process_queue_node * pqn)186 static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
187 				     struct process_queue_node *pqn)
188 {
189 	struct kfd_node *dev;
190 	struct kfd_process_device *pdd;
191 
192 	dev = pqn->q->device;
193 
194 	pdd = kfd_get_process_device_data(dev, pqm->process);
195 	if (!pdd) {
196 		pr_err("Process device data doesn't exist\n");
197 		return;
198 	}
199 
200 	if (pqn->q->gws) {
201 		if (KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 3) &&
202 		    KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 4) &&
203 		    !dev->kfd->shared_resources.enable_mes)
204 			amdgpu_amdkfd_remove_gws_from_process(
205 				pqm->process->kgd_process_info, pqn->q->gws);
206 		pdd->qpd.num_gws = 0;
207 	}
208 
209 	if (dev->kfd->shared_resources.enable_mes) {
210 		amdgpu_amdkfd_free_gtt_mem(dev->adev, &pqn->q->gang_ctx_bo);
211 		amdgpu_amdkfd_free_gtt_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
212 	}
213 }
214 
pqm_uninit(struct process_queue_manager * pqm)215 void pqm_uninit(struct process_queue_manager *pqm)
216 {
217 	struct process_queue_node *pqn, *next;
218 
219 	list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
220 		if (pqn->q) {
221 			struct kfd_process_device *pdd = kfd_get_process_device_data(pqn->q->device,
222 										     pqm->process);
223 			if (pdd) {
224 				kfd_queue_unref_bo_vas(pdd, &pqn->q->properties);
225 				kfd_queue_release_buffers(pdd, &pqn->q->properties);
226 			} else {
227 				WARN_ON(!pdd);
228 			}
229 			pqm_clean_queue_resource(pqm, pqn);
230 		}
231 
232 		kfd_procfs_del_queue(pqn->q);
233 		uninit_queue(pqn->q);
234 		list_del(&pqn->process_queue_list);
235 		kfree(pqn);
236 	}
237 
238 	bitmap_free(pqm->queue_slot_bitmap);
239 	pqm->queue_slot_bitmap = NULL;
240 }
241 
init_user_queue(struct process_queue_manager * pqm,struct kfd_node * dev,struct queue ** q,struct queue_properties * q_properties,struct file * f,unsigned int qid)242 static int init_user_queue(struct process_queue_manager *pqm,
243 				struct kfd_node *dev, struct queue **q,
244 				struct queue_properties *q_properties,
245 				struct file *f, unsigned int qid)
246 {
247 	int retval;
248 
249 	/* Doorbell initialized in user space*/
250 	q_properties->doorbell_ptr = NULL;
251 	q_properties->exception_status = KFD_EC_MASK(EC_QUEUE_NEW);
252 
253 	/* let DQM handle it*/
254 	q_properties->vmid = 0;
255 	q_properties->queue_id = qid;
256 
257 	retval = init_queue(q, q_properties);
258 	if (retval != 0)
259 		return retval;
260 
261 	(*q)->device = dev;
262 	(*q)->process = pqm->process;
263 
264 	if (dev->kfd->shared_resources.enable_mes) {
265 		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
266 						AMDGPU_MES_GANG_CTX_SIZE,
267 						&(*q)->gang_ctx_bo,
268 						&(*q)->gang_ctx_gpu_addr,
269 						&(*q)->gang_ctx_cpu_ptr,
270 						false);
271 		if (retval) {
272 			pr_err("failed to allocate gang context bo\n");
273 			goto cleanup;
274 		}
275 		memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
276 
277 		/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
278 		 * on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
279 		 */
280 		if (((dev->adev->mes.sched_version & AMDGPU_MES_API_VERSION_MASK)
281 		    >> AMDGPU_MES_API_VERSION_SHIFT) >= 2) {
282 			if (dev->adev != amdgpu_ttm_adev(q_properties->wptr_bo->tbo.bdev)) {
283 				pr_err("Queue memory allocated to wrong device\n");
284 				retval = -EINVAL;
285 				goto free_gang_ctx_bo;
286 			}
287 
288 			retval = amdgpu_amdkfd_map_gtt_bo_to_gart(q_properties->wptr_bo,
289 								  &(*q)->wptr_bo_gart);
290 			if (retval) {
291 				pr_err("Failed to map wptr bo to GART\n");
292 				goto free_gang_ctx_bo;
293 			}
294 		}
295 	}
296 
297 	pr_debug("PQM After init queue");
298 	return 0;
299 
300 free_gang_ctx_bo:
301 	amdgpu_amdkfd_free_gtt_mem(dev->adev, &(*q)->gang_ctx_bo);
302 cleanup:
303 	uninit_queue(*q);
304 	*q = NULL;
305 	return retval;
306 }
307 
pqm_create_queue(struct process_queue_manager * pqm,struct kfd_node * dev,struct file * f,struct queue_properties * properties,unsigned int * qid,const struct kfd_criu_queue_priv_data * q_data,const void * restore_mqd,const void * restore_ctl_stack,uint32_t * p_doorbell_offset_in_process)308 int pqm_create_queue(struct process_queue_manager *pqm,
309 			    struct kfd_node *dev,
310 			    struct file *f,
311 			    struct queue_properties *properties,
312 			    unsigned int *qid,
313 			    const struct kfd_criu_queue_priv_data *q_data,
314 			    const void *restore_mqd,
315 			    const void *restore_ctl_stack,
316 			    uint32_t *p_doorbell_offset_in_process)
317 {
318 	int retval;
319 	struct kfd_process_device *pdd;
320 	struct queue *q;
321 	struct process_queue_node *pqn;
322 	struct kernel_queue *kq;
323 	enum kfd_queue_type type = properties->type;
324 	unsigned int max_queues = 127; /* HWS limit */
325 
326 	/*
327 	 * On GFX 9.4.3, increase the number of queues that
328 	 * can be created to 255. No HWS limit on GFX 9.4.3.
329 	 */
330 	if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3) ||
331 	    KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4))
332 		max_queues = 255;
333 
334 	q = NULL;
335 	kq = NULL;
336 
337 	pdd = kfd_get_process_device_data(dev, pqm->process);
338 	if (!pdd) {
339 		pr_err("Process device data doesn't exist\n");
340 		return -1;
341 	}
342 
343 	/*
344 	 * for debug process, verify that it is within the static queues limit
345 	 * currently limit is set to half of the total avail HQD slots
346 	 * If we are just about to create DIQ, the is_debug flag is not set yet
347 	 * Hence we also check the type as well
348 	 */
349 	if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
350 		max_queues = dev->kfd->device_info.max_no_of_hqd/2;
351 
352 	if (pdd->qpd.queue_count >= max_queues)
353 		return -ENOSPC;
354 
355 	if (q_data) {
356 		retval = assign_queue_slot_by_qid(pqm, q_data->q_id);
357 		*qid = q_data->q_id;
358 	} else
359 		retval = find_available_queue_slot(pqm, qid);
360 
361 	if (retval != 0)
362 		return retval;
363 
364 	/* Register process if this is the first queue */
365 	if (list_empty(&pdd->qpd.queues_list) &&
366 	    list_empty(&pdd->qpd.priv_queue_list))
367 		dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
368 
369 	/* Allocate proc_ctx_bo only if MES is enabled and this is the first queue */
370 	if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) {
371 		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
372 						     AMDGPU_MES_PROC_CTX_SIZE,
373 						     &pdd->proc_ctx_bo,
374 						     &pdd->proc_ctx_gpu_addr,
375 						     &pdd->proc_ctx_cpu_ptr,
376 						     false);
377 		if (retval) {
378 			dev_err(dev->adev->dev, "failed to allocate process context bo\n");
379 			return retval;
380 		}
381 		memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
382 	}
383 
384 	pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
385 	if (!pqn) {
386 		retval = -ENOMEM;
387 		goto err_allocate_pqn;
388 	}
389 
390 	switch (type) {
391 	case KFD_QUEUE_TYPE_SDMA:
392 	case KFD_QUEUE_TYPE_SDMA_XGMI:
393 	case KFD_QUEUE_TYPE_SDMA_BY_ENG_ID:
394 		/* SDMA queues are always allocated statically no matter
395 		 * which scheduler mode is used. We also do not need to
396 		 * check whether a SDMA queue can be allocated here, because
397 		 * allocate_sdma_queue() in create_queue() has the
398 		 * corresponding check logic.
399 		 */
400 		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
401 		if (retval != 0)
402 			goto err_create_queue;
403 		pqn->q = q;
404 		pqn->kq = NULL;
405 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
406 						    restore_mqd, restore_ctl_stack);
407 		print_queue(q);
408 		break;
409 
410 	case KFD_QUEUE_TYPE_COMPUTE:
411 		/* check if there is over subscription */
412 		if ((dev->dqm->sched_policy ==
413 		     KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
414 		((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
415 		(dev->dqm->active_queue_count >= get_cp_queues_num(dev->dqm)))) {
416 			pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n");
417 			retval = -EPERM;
418 			goto err_create_queue;
419 		}
420 
421 		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
422 		if (retval != 0)
423 			goto err_create_queue;
424 		pqn->q = q;
425 		pqn->kq = NULL;
426 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data,
427 						    restore_mqd, restore_ctl_stack);
428 		print_queue(q);
429 		break;
430 	case KFD_QUEUE_TYPE_DIQ:
431 		kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
432 		if (!kq) {
433 			retval = -ENOMEM;
434 			goto err_create_queue;
435 		}
436 		kq->queue->properties.queue_id = *qid;
437 		pqn->kq = kq;
438 		pqn->q = NULL;
439 		retval = kfd_process_drain_interrupts(pdd);
440 		if (retval)
441 			break;
442 
443 		retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
444 							kq, &pdd->qpd);
445 		break;
446 	default:
447 		WARN(1, "Invalid queue type %d", type);
448 		retval = -EINVAL;
449 	}
450 
451 	if (retval != 0) {
452 		pr_err("Pasid 0x%x DQM create queue type %d failed. ret %d\n",
453 			pqm->process->pasid, type, retval);
454 		goto err_create_queue;
455 	}
456 
457 	if (q && p_doorbell_offset_in_process) {
458 		/* Return the doorbell offset within the doorbell page
459 		 * to the caller so it can be passed up to user mode
460 		 * (in bytes).
461 		 * relative doorbell index = Absolute doorbell index -
462 		 * absolute index of first doorbell in the page.
463 		 */
464 		uint32_t first_db_index = amdgpu_doorbell_index_on_bar(pdd->dev->adev,
465 								       pdd->qpd.proc_doorbells,
466 								       0,
467 								       pdd->dev->kfd->device_info.doorbell_size);
468 
469 		*p_doorbell_offset_in_process = (q->properties.doorbell_off
470 						- first_db_index) * sizeof(uint32_t);
471 	}
472 
473 	pr_debug("PQM After DQM create queue\n");
474 
475 	list_add(&pqn->process_queue_list, &pqm->queues);
476 
477 	if (q) {
478 		pr_debug("PQM done creating queue\n");
479 		kfd_procfs_add_queue(q);
480 		print_queue_properties(&q->properties);
481 	}
482 
483 	return retval;
484 
485 err_create_queue:
486 	uninit_queue(q);
487 	if (kq)
488 		kernel_queue_uninit(kq);
489 	kfree(pqn);
490 err_allocate_pqn:
491 	/* check if queues list is empty unregister process from device */
492 	clear_bit(*qid, pqm->queue_slot_bitmap);
493 	if (list_empty(&pdd->qpd.queues_list) &&
494 	    list_empty(&pdd->qpd.priv_queue_list))
495 		dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
496 	return retval;
497 }
498 
pqm_destroy_queue(struct process_queue_manager * pqm,unsigned int qid)499 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
500 {
501 	struct process_queue_node *pqn;
502 	struct kfd_process_device *pdd;
503 	struct device_queue_manager *dqm;
504 	struct kfd_node *dev;
505 	int retval;
506 
507 	dqm = NULL;
508 
509 	retval = 0;
510 
511 	pqn = get_queue_by_qid(pqm, qid);
512 	if (!pqn) {
513 		pr_err("Queue id does not match any known queue\n");
514 		return -EINVAL;
515 	}
516 
517 	dev = NULL;
518 	if (pqn->kq)
519 		dev = pqn->kq->dev;
520 	if (pqn->q)
521 		dev = pqn->q->device;
522 	if (WARN_ON(!dev))
523 		return -ENODEV;
524 
525 	pdd = kfd_get_process_device_data(dev, pqm->process);
526 	if (!pdd) {
527 		pr_err("Process device data doesn't exist\n");
528 		return -1;
529 	}
530 
531 	if (pqn->kq) {
532 		/* destroy kernel queue (DIQ) */
533 		dqm = pqn->kq->dev->dqm;
534 		dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
535 		kernel_queue_uninit(pqn->kq);
536 	}
537 
538 	if (pqn->q) {
539 		retval = kfd_queue_unref_bo_vas(pdd, &pqn->q->properties);
540 		if (retval)
541 			goto err_destroy_queue;
542 
543 		dqm = pqn->q->device->dqm;
544 		retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
545 		if (retval) {
546 			pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
547 				pqm->process->pasid,
548 				pqn->q->properties.queue_id, retval);
549 			if (retval != -ETIME && retval != -EIO)
550 				goto err_destroy_queue;
551 		}
552 		kfd_procfs_del_queue(pqn->q);
553 		kfd_queue_release_buffers(pdd, &pqn->q->properties);
554 		pqm_clean_queue_resource(pqm, pqn);
555 		uninit_queue(pqn->q);
556 	}
557 
558 	list_del(&pqn->process_queue_list);
559 	kfree(pqn);
560 	clear_bit(qid, pqm->queue_slot_bitmap);
561 
562 	if (list_empty(&pdd->qpd.queues_list) &&
563 	    list_empty(&pdd->qpd.priv_queue_list))
564 		dqm->ops.unregister_process(dqm, &pdd->qpd);
565 
566 err_destroy_queue:
567 	return retval;
568 }
569 
pqm_update_queue_properties(struct process_queue_manager * pqm,unsigned int qid,struct queue_properties * p)570 int pqm_update_queue_properties(struct process_queue_manager *pqm,
571 				unsigned int qid, struct queue_properties *p)
572 {
573 	int retval;
574 	struct process_queue_node *pqn;
575 
576 	pqn = get_queue_by_qid(pqm, qid);
577 	if (!pqn || !pqn->q) {
578 		pr_debug("No queue %d exists for update operation\n", qid);
579 		return -EFAULT;
580 	}
581 
582 	/*
583 	 * Update with NULL ring address is used to disable the queue
584 	 */
585 	if (p->queue_address && p->queue_size) {
586 		struct kfd_process_device *pdd;
587 		struct amdgpu_vm *vm;
588 		struct queue *q = pqn->q;
589 		int err;
590 
591 		pdd = kfd_get_process_device_data(q->device, q->process);
592 		if (!pdd)
593 			return -ENODEV;
594 		vm = drm_priv_to_vm(pdd->drm_priv);
595 		err = amdgpu_bo_reserve(vm->root.bo, false);
596 		if (err)
597 			return err;
598 
599 		if (kfd_queue_buffer_get(vm, (void *)p->queue_address, &p->ring_bo,
600 					 p->queue_size)) {
601 			pr_debug("ring buf 0x%llx size 0x%llx not mapped on GPU\n",
602 				 p->queue_address, p->queue_size);
603 			return -EFAULT;
604 		}
605 
606 		kfd_queue_unref_bo_va(vm, &pqn->q->properties.ring_bo);
607 		kfd_queue_buffer_put(&pqn->q->properties.ring_bo);
608 		amdgpu_bo_unreserve(vm->root.bo);
609 
610 		pqn->q->properties.ring_bo = p->ring_bo;
611 	}
612 
613 	pqn->q->properties.queue_address = p->queue_address;
614 	pqn->q->properties.queue_size = p->queue_size;
615 	pqn->q->properties.queue_percent = p->queue_percent;
616 	pqn->q->properties.priority = p->priority;
617 	pqn->q->properties.pm4_target_xcc = p->pm4_target_xcc;
618 
619 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
620 							pqn->q, NULL);
621 	if (retval != 0)
622 		return retval;
623 
624 	return 0;
625 }
626 
pqm_update_mqd(struct process_queue_manager * pqm,unsigned int qid,struct mqd_update_info * minfo)627 int pqm_update_mqd(struct process_queue_manager *pqm,
628 				unsigned int qid, struct mqd_update_info *minfo)
629 {
630 	int retval;
631 	struct process_queue_node *pqn;
632 
633 	pqn = get_queue_by_qid(pqm, qid);
634 	if (!pqn) {
635 		pr_debug("No queue %d exists for update operation\n", qid);
636 		return -EFAULT;
637 	}
638 
639 	/* CUs are masked for debugger requirements so deny user mask  */
640 	if (pqn->q->properties.is_dbg_wa && minfo && minfo->cu_mask.ptr)
641 		return -EBUSY;
642 
643 	/* ASICs that have WGPs must enforce pairwise enabled mask checks. */
644 	if (minfo && minfo->cu_mask.ptr &&
645 			KFD_GC_VERSION(pqn->q->device) >= IP_VERSION(10, 0, 0)) {
646 		int i;
647 
648 		for (i = 0; i < minfo->cu_mask.count; i += 2) {
649 			uint32_t cu_pair = (minfo->cu_mask.ptr[i / 32] >> (i % 32)) & 0x3;
650 
651 			if (cu_pair && cu_pair != 0x3) {
652 				pr_debug("CUs must be adjacent pairwise enabled.\n");
653 				return -EINVAL;
654 			}
655 		}
656 	}
657 
658 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
659 							pqn->q, minfo);
660 	if (retval != 0)
661 		return retval;
662 
663 	if (minfo && minfo->cu_mask.ptr)
664 		pqn->q->properties.is_user_cu_masked = true;
665 
666 	return 0;
667 }
668 
pqm_get_kernel_queue(struct process_queue_manager * pqm,unsigned int qid)669 struct kernel_queue *pqm_get_kernel_queue(
670 					struct process_queue_manager *pqm,
671 					unsigned int qid)
672 {
673 	struct process_queue_node *pqn;
674 
675 	pqn = get_queue_by_qid(pqm, qid);
676 	if (pqn && pqn->kq)
677 		return pqn->kq;
678 
679 	return NULL;
680 }
681 
pqm_get_user_queue(struct process_queue_manager * pqm,unsigned int qid)682 struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
683 					unsigned int qid)
684 {
685 	struct process_queue_node *pqn;
686 
687 	pqn = get_queue_by_qid(pqm, qid);
688 	return pqn ? pqn->q : NULL;
689 }
690 
pqm_get_wave_state(struct process_queue_manager * pqm,unsigned int qid,void __user * ctl_stack,u32 * ctl_stack_used_size,u32 * save_area_used_size)691 int pqm_get_wave_state(struct process_queue_manager *pqm,
692 		       unsigned int qid,
693 		       void __user *ctl_stack,
694 		       u32 *ctl_stack_used_size,
695 		       u32 *save_area_used_size)
696 {
697 	struct process_queue_node *pqn;
698 
699 	pqn = get_queue_by_qid(pqm, qid);
700 	if (!pqn) {
701 		pr_debug("amdkfd: No queue %d exists for operation\n",
702 			 qid);
703 		return -EFAULT;
704 	}
705 
706 	return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
707 						       pqn->q,
708 						       ctl_stack,
709 						       ctl_stack_used_size,
710 						       save_area_used_size);
711 }
712 
pqm_get_queue_snapshot(struct process_queue_manager * pqm,uint64_t exception_clear_mask,void __user * buf,int * num_qss_entries,uint32_t * entry_size)713 int pqm_get_queue_snapshot(struct process_queue_manager *pqm,
714 			   uint64_t exception_clear_mask,
715 			   void __user *buf,
716 			   int *num_qss_entries,
717 			   uint32_t *entry_size)
718 {
719 	struct process_queue_node *pqn;
720 	struct kfd_queue_snapshot_entry src;
721 	uint32_t tmp_entry_size = *entry_size, tmp_qss_entries = *num_qss_entries;
722 	int r = 0;
723 
724 	*num_qss_entries = 0;
725 	if (!(*entry_size))
726 		return -EINVAL;
727 
728 	*entry_size = min_t(size_t, *entry_size, sizeof(struct kfd_queue_snapshot_entry));
729 	mutex_lock(&pqm->process->event_mutex);
730 
731 	memset(&src, 0, sizeof(src));
732 
733 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
734 		if (!pqn->q)
735 			continue;
736 
737 		if (*num_qss_entries < tmp_qss_entries) {
738 			set_queue_snapshot_entry(pqn->q, exception_clear_mask, &src);
739 
740 			if (copy_to_user(buf, &src, *entry_size)) {
741 				r = -EFAULT;
742 				break;
743 			}
744 			buf += tmp_entry_size;
745 		}
746 		*num_qss_entries += 1;
747 	}
748 
749 	mutex_unlock(&pqm->process->event_mutex);
750 	return r;
751 }
752 
get_queue_data_sizes(struct kfd_process_device * pdd,struct queue * q,uint32_t * mqd_size,uint32_t * ctl_stack_size)753 static int get_queue_data_sizes(struct kfd_process_device *pdd,
754 				struct queue *q,
755 				uint32_t *mqd_size,
756 				uint32_t *ctl_stack_size)
757 {
758 	int ret;
759 
760 	ret = pqm_get_queue_checkpoint_info(&pdd->process->pqm,
761 					    q->properties.queue_id,
762 					    mqd_size,
763 					    ctl_stack_size);
764 	if (ret)
765 		pr_err("Failed to get queue dump info (%d)\n", ret);
766 
767 	return ret;
768 }
769 
kfd_process_get_queue_info(struct kfd_process * p,uint32_t * num_queues,uint64_t * priv_data_sizes)770 int kfd_process_get_queue_info(struct kfd_process *p,
771 			       uint32_t *num_queues,
772 			       uint64_t *priv_data_sizes)
773 {
774 	uint32_t extra_data_sizes = 0;
775 	struct queue *q;
776 	int i;
777 	int ret;
778 
779 	*num_queues = 0;
780 
781 	/* Run over all PDDs of the process */
782 	for (i = 0; i < p->n_pdds; i++) {
783 		struct kfd_process_device *pdd = p->pdds[i];
784 
785 		list_for_each_entry(q, &pdd->qpd.queues_list, list) {
786 			if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
787 				q->properties.type == KFD_QUEUE_TYPE_SDMA ||
788 				q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
789 				uint32_t mqd_size, ctl_stack_size;
790 
791 				*num_queues = *num_queues + 1;
792 
793 				ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
794 				if (ret)
795 					return ret;
796 
797 				extra_data_sizes += mqd_size + ctl_stack_size;
798 			} else {
799 				pr_err("Unsupported queue type (%d)\n", q->properties.type);
800 				return -EOPNOTSUPP;
801 			}
802 		}
803 	}
804 	*priv_data_sizes = extra_data_sizes +
805 				(*num_queues * sizeof(struct kfd_criu_queue_priv_data));
806 
807 	return 0;
808 }
809 
pqm_checkpoint_mqd(struct process_queue_manager * pqm,unsigned int qid,void * mqd,void * ctl_stack)810 static int pqm_checkpoint_mqd(struct process_queue_manager *pqm,
811 			      unsigned int qid,
812 			      void *mqd,
813 			      void *ctl_stack)
814 {
815 	struct process_queue_node *pqn;
816 
817 	pqn = get_queue_by_qid(pqm, qid);
818 	if (!pqn) {
819 		pr_debug("amdkfd: No queue %d exists for operation\n", qid);
820 		return -EFAULT;
821 	}
822 
823 	if (!pqn->q->device->dqm->ops.checkpoint_mqd) {
824 		pr_err("amdkfd: queue dumping not supported on this device\n");
825 		return -EOPNOTSUPP;
826 	}
827 
828 	return pqn->q->device->dqm->ops.checkpoint_mqd(pqn->q->device->dqm,
829 						       pqn->q, mqd, ctl_stack);
830 }
831 
criu_checkpoint_queue(struct kfd_process_device * pdd,struct queue * q,struct kfd_criu_queue_priv_data * q_data)832 static int criu_checkpoint_queue(struct kfd_process_device *pdd,
833 			   struct queue *q,
834 			   struct kfd_criu_queue_priv_data *q_data)
835 {
836 	uint8_t *mqd, *ctl_stack;
837 	int ret;
838 
839 	mqd = (void *)(q_data + 1);
840 	ctl_stack = mqd + q_data->mqd_size;
841 
842 	q_data->gpu_id = pdd->user_gpu_id;
843 	q_data->type = q->properties.type;
844 	q_data->format = q->properties.format;
845 	q_data->q_id =  q->properties.queue_id;
846 	q_data->q_address = q->properties.queue_address;
847 	q_data->q_size = q->properties.queue_size;
848 	q_data->priority = q->properties.priority;
849 	q_data->q_percent = q->properties.queue_percent;
850 	q_data->read_ptr_addr = (uint64_t)q->properties.read_ptr;
851 	q_data->write_ptr_addr = (uint64_t)q->properties.write_ptr;
852 	q_data->doorbell_id = q->doorbell_id;
853 
854 	q_data->sdma_id = q->sdma_id;
855 
856 	q_data->eop_ring_buffer_address =
857 		q->properties.eop_ring_buffer_address;
858 
859 	q_data->eop_ring_buffer_size = q->properties.eop_ring_buffer_size;
860 
861 	q_data->ctx_save_restore_area_address =
862 		q->properties.ctx_save_restore_area_address;
863 
864 	q_data->ctx_save_restore_area_size =
865 		q->properties.ctx_save_restore_area_size;
866 
867 	q_data->gws = !!q->gws;
868 
869 	ret = pqm_checkpoint_mqd(&pdd->process->pqm, q->properties.queue_id, mqd, ctl_stack);
870 	if (ret) {
871 		pr_err("Failed checkpoint queue_mqd (%d)\n", ret);
872 		return ret;
873 	}
874 
875 	pr_debug("Dumping Queue: gpu_id:%x queue_id:%u\n", q_data->gpu_id, q_data->q_id);
876 	return ret;
877 }
878 
criu_checkpoint_queues_device(struct kfd_process_device * pdd,uint8_t __user * user_priv,unsigned int * q_index,uint64_t * queues_priv_data_offset)879 static int criu_checkpoint_queues_device(struct kfd_process_device *pdd,
880 				   uint8_t __user *user_priv,
881 				   unsigned int *q_index,
882 				   uint64_t *queues_priv_data_offset)
883 {
884 	unsigned int q_private_data_size = 0;
885 	uint8_t *q_private_data = NULL; /* Local buffer to store individual queue private data */
886 	struct queue *q;
887 	int ret = 0;
888 
889 	list_for_each_entry(q, &pdd->qpd.queues_list, list) {
890 		struct kfd_criu_queue_priv_data *q_data;
891 		uint64_t q_data_size;
892 		uint32_t mqd_size;
893 		uint32_t ctl_stack_size;
894 
895 		if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE &&
896 			q->properties.type != KFD_QUEUE_TYPE_SDMA &&
897 			q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI) {
898 
899 			pr_err("Unsupported queue type (%d)\n", q->properties.type);
900 			ret = -EOPNOTSUPP;
901 			break;
902 		}
903 
904 		ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size);
905 		if (ret)
906 			break;
907 
908 		q_data_size = sizeof(*q_data) + mqd_size + ctl_stack_size;
909 
910 		/* Increase local buffer space if needed */
911 		if (q_private_data_size < q_data_size) {
912 			kfree(q_private_data);
913 
914 			q_private_data = kzalloc(q_data_size, GFP_KERNEL);
915 			if (!q_private_data) {
916 				ret = -ENOMEM;
917 				break;
918 			}
919 			q_private_data_size = q_data_size;
920 		}
921 
922 		q_data = (struct kfd_criu_queue_priv_data *)q_private_data;
923 
924 		/* data stored in this order: priv_data, mqd, ctl_stack */
925 		q_data->mqd_size = mqd_size;
926 		q_data->ctl_stack_size = ctl_stack_size;
927 
928 		ret = criu_checkpoint_queue(pdd, q, q_data);
929 		if (ret)
930 			break;
931 
932 		q_data->object_type = KFD_CRIU_OBJECT_TYPE_QUEUE;
933 
934 		ret = copy_to_user(user_priv + *queues_priv_data_offset,
935 				q_data, q_data_size);
936 		if (ret) {
937 			ret = -EFAULT;
938 			break;
939 		}
940 		*queues_priv_data_offset += q_data_size;
941 		*q_index = *q_index + 1;
942 	}
943 
944 	kfree(q_private_data);
945 
946 	return ret;
947 }
948 
kfd_criu_checkpoint_queues(struct kfd_process * p,uint8_t __user * user_priv_data,uint64_t * priv_data_offset)949 int kfd_criu_checkpoint_queues(struct kfd_process *p,
950 			 uint8_t __user *user_priv_data,
951 			 uint64_t *priv_data_offset)
952 {
953 	int ret = 0, pdd_index, q_index = 0;
954 
955 	for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) {
956 		struct kfd_process_device *pdd = p->pdds[pdd_index];
957 
958 		/*
959 		 * criu_checkpoint_queues_device will copy data to user and update q_index and
960 		 * queues_priv_data_offset
961 		 */
962 		ret = criu_checkpoint_queues_device(pdd, user_priv_data, &q_index,
963 					      priv_data_offset);
964 
965 		if (ret)
966 			break;
967 	}
968 
969 	return ret;
970 }
971 
set_queue_properties_from_criu(struct queue_properties * qp,struct kfd_criu_queue_priv_data * q_data)972 static void set_queue_properties_from_criu(struct queue_properties *qp,
973 					  struct kfd_criu_queue_priv_data *q_data)
974 {
975 	qp->is_interop = false;
976 	qp->queue_percent = q_data->q_percent;
977 	qp->priority = q_data->priority;
978 	qp->queue_address = q_data->q_address;
979 	qp->queue_size = q_data->q_size;
980 	qp->read_ptr = (uint32_t *) q_data->read_ptr_addr;
981 	qp->write_ptr = (uint32_t *) q_data->write_ptr_addr;
982 	qp->eop_ring_buffer_address = q_data->eop_ring_buffer_address;
983 	qp->eop_ring_buffer_size = q_data->eop_ring_buffer_size;
984 	qp->ctx_save_restore_area_address = q_data->ctx_save_restore_area_address;
985 	qp->ctx_save_restore_area_size = q_data->ctx_save_restore_area_size;
986 	qp->ctl_stack_size = q_data->ctl_stack_size;
987 	qp->type = q_data->type;
988 	qp->format = q_data->format;
989 }
990 
kfd_criu_restore_queue(struct kfd_process * p,uint8_t __user * user_priv_ptr,uint64_t * priv_data_offset,uint64_t max_priv_data_size)991 int kfd_criu_restore_queue(struct kfd_process *p,
992 			   uint8_t __user *user_priv_ptr,
993 			   uint64_t *priv_data_offset,
994 			   uint64_t max_priv_data_size)
995 {
996 	uint8_t *mqd, *ctl_stack, *q_extra_data = NULL;
997 	struct kfd_criu_queue_priv_data *q_data;
998 	struct kfd_process_device *pdd;
999 	uint64_t q_extra_data_size;
1000 	struct queue_properties qp;
1001 	unsigned int queue_id;
1002 	int ret = 0;
1003 
1004 	if (*priv_data_offset + sizeof(*q_data) > max_priv_data_size)
1005 		return -EINVAL;
1006 
1007 	q_data = kmalloc(sizeof(*q_data), GFP_KERNEL);
1008 	if (!q_data)
1009 		return -ENOMEM;
1010 
1011 	ret = copy_from_user(q_data, user_priv_ptr + *priv_data_offset, sizeof(*q_data));
1012 	if (ret) {
1013 		ret = -EFAULT;
1014 		goto exit;
1015 	}
1016 
1017 	*priv_data_offset += sizeof(*q_data);
1018 	q_extra_data_size = (uint64_t)q_data->ctl_stack_size + q_data->mqd_size;
1019 
1020 	if (*priv_data_offset + q_extra_data_size > max_priv_data_size) {
1021 		ret = -EINVAL;
1022 		goto exit;
1023 	}
1024 
1025 	q_extra_data = kmalloc(q_extra_data_size, GFP_KERNEL);
1026 	if (!q_extra_data) {
1027 		ret = -ENOMEM;
1028 		goto exit;
1029 	}
1030 
1031 	ret = copy_from_user(q_extra_data, user_priv_ptr + *priv_data_offset, q_extra_data_size);
1032 	if (ret) {
1033 		ret = -EFAULT;
1034 		goto exit;
1035 	}
1036 
1037 	*priv_data_offset += q_extra_data_size;
1038 
1039 	pdd = kfd_process_device_data_by_id(p, q_data->gpu_id);
1040 	if (!pdd) {
1041 		pr_err("Failed to get pdd\n");
1042 		ret = -EINVAL;
1043 		goto exit;
1044 	}
1045 
1046 	/* data stored in this order: mqd, ctl_stack */
1047 	mqd = q_extra_data;
1048 	ctl_stack = mqd + q_data->mqd_size;
1049 
1050 	memset(&qp, 0, sizeof(qp));
1051 	set_queue_properties_from_criu(&qp, q_data);
1052 
1053 	print_queue_properties(&qp);
1054 
1055 	ret = pqm_create_queue(&p->pqm, pdd->dev, NULL, &qp, &queue_id, q_data, mqd, ctl_stack,
1056 				NULL);
1057 	if (ret) {
1058 		pr_err("Failed to create new queue err:%d\n", ret);
1059 		goto exit;
1060 	}
1061 
1062 	if (q_data->gws)
1063 		ret = pqm_set_gws(&p->pqm, q_data->q_id, pdd->dev->gws);
1064 
1065 exit:
1066 	if (ret)
1067 		pr_err("Failed to restore queue (%d)\n", ret);
1068 	else
1069 		pr_debug("Queue id %d was restored successfully\n", queue_id);
1070 
1071 	kfree(q_data);
1072 	kfree(q_extra_data);
1073 
1074 	return ret;
1075 }
1076 
pqm_get_queue_checkpoint_info(struct process_queue_manager * pqm,unsigned int qid,uint32_t * mqd_size,uint32_t * ctl_stack_size)1077 int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm,
1078 				  unsigned int qid,
1079 				  uint32_t *mqd_size,
1080 				  uint32_t *ctl_stack_size)
1081 {
1082 	struct process_queue_node *pqn;
1083 
1084 	pqn = get_queue_by_qid(pqm, qid);
1085 	if (!pqn) {
1086 		pr_debug("amdkfd: No queue %d exists for operation\n", qid);
1087 		return -EFAULT;
1088 	}
1089 
1090 	if (!pqn->q->device->dqm->ops.get_queue_checkpoint_info) {
1091 		pr_err("amdkfd: queue dumping not supported on this device\n");
1092 		return -EOPNOTSUPP;
1093 	}
1094 
1095 	pqn->q->device->dqm->ops.get_queue_checkpoint_info(pqn->q->device->dqm,
1096 						       pqn->q, mqd_size,
1097 						       ctl_stack_size);
1098 	return 0;
1099 }
1100 
1101 #if defined(CONFIG_DEBUG_FS)
1102 
pqm_debugfs_mqds(struct seq_file * m,void * data)1103 int pqm_debugfs_mqds(struct seq_file *m, void *data)
1104 {
1105 	struct process_queue_manager *pqm = data;
1106 	struct process_queue_node *pqn;
1107 	struct queue *q;
1108 	enum KFD_MQD_TYPE mqd_type;
1109 	struct mqd_manager *mqd_mgr;
1110 	int r = 0, xcc, num_xccs = 1;
1111 	void *mqd;
1112 	uint64_t size = 0;
1113 
1114 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
1115 		if (pqn->q) {
1116 			q = pqn->q;
1117 			switch (q->properties.type) {
1118 			case KFD_QUEUE_TYPE_SDMA:
1119 			case KFD_QUEUE_TYPE_SDMA_XGMI:
1120 				seq_printf(m, "  SDMA queue on device %x\n",
1121 					   q->device->id);
1122 				mqd_type = KFD_MQD_TYPE_SDMA;
1123 				break;
1124 			case KFD_QUEUE_TYPE_COMPUTE:
1125 				seq_printf(m, "  Compute queue on device %x\n",
1126 					   q->device->id);
1127 				mqd_type = KFD_MQD_TYPE_CP;
1128 				num_xccs = NUM_XCC(q->device->xcc_mask);
1129 				break;
1130 			default:
1131 				seq_printf(m,
1132 				"  Bad user queue type %d on device %x\n",
1133 					   q->properties.type, q->device->id);
1134 				continue;
1135 			}
1136 			mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type];
1137 			size = mqd_mgr->mqd_stride(mqd_mgr,
1138 							&q->properties);
1139 		} else if (pqn->kq) {
1140 			q = pqn->kq->queue;
1141 			mqd_mgr = pqn->kq->mqd_mgr;
1142 			switch (q->properties.type) {
1143 			case KFD_QUEUE_TYPE_DIQ:
1144 				seq_printf(m, "  DIQ on device %x\n",
1145 					   pqn->kq->dev->id);
1146 				break;
1147 			default:
1148 				seq_printf(m,
1149 				"  Bad kernel queue type %d on device %x\n",
1150 					   q->properties.type,
1151 					   pqn->kq->dev->id);
1152 				continue;
1153 			}
1154 		} else {
1155 			seq_printf(m,
1156 		"  Weird: Queue node with neither kernel nor user queue\n");
1157 			continue;
1158 		}
1159 
1160 		for (xcc = 0; xcc < num_xccs; xcc++) {
1161 			mqd = q->mqd + size * xcc;
1162 			r = mqd_mgr->debugfs_show_mqd(m, mqd);
1163 			if (r != 0)
1164 				break;
1165 		}
1166 	}
1167 
1168 	return r;
1169 }
1170 
1171 #endif
1172