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/mutex.h>
26 #include "kfd_device_queue_manager.h"
27 #include "kfd_kernel_queue.h"
28 #include "kfd_priv.h"
29 
30 static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
31 				unsigned int buffer_size_bytes)
32 {
33 	unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
34 
35 	WARN((temp * sizeof(uint32_t)) > buffer_size_bytes,
36 	     "Runlist IB overflow");
37 	*wptr = temp;
38 }
39 
40 static void pm_calc_rlib_size(struct packet_manager *pm,
41 				unsigned int *rlib_size,
42 				bool *over_subscription)
43 {
44 	unsigned int process_count, queue_count, compute_queue_count;
45 	unsigned int map_queue_size;
46 	unsigned int max_proc_per_quantum = 1;
47 	struct kfd_dev *dev = pm->dqm->dev;
48 
49 	process_count = pm->dqm->processes_count;
50 	queue_count = pm->dqm->queue_count;
51 	compute_queue_count = queue_count - pm->dqm->sdma_queue_count -
52 				pm->dqm->xgmi_sdma_queue_count;
53 
54 	/* check if there is over subscription
55 	 * Note: the arbitration between the number of VMIDs and
56 	 * hws_max_conc_proc has been done in
57 	 * kgd2kfd_device_init().
58 	 */
59 	*over_subscription = false;
60 
61 	if (dev->max_proc_per_quantum > 1)
62 		max_proc_per_quantum = dev->max_proc_per_quantum;
63 
64 	if ((process_count > max_proc_per_quantum) ||
65 	    compute_queue_count > get_queues_num(pm->dqm)) {
66 		*over_subscription = true;
67 		pr_debug("Over subscribed runlist\n");
68 	}
69 
70 	map_queue_size = pm->pmf->map_queues_size;
71 	/* calculate run list ib allocation size */
72 	*rlib_size = process_count * pm->pmf->map_process_size +
73 		     queue_count * map_queue_size;
74 
75 	/*
76 	 * Increase the allocation size in case we need a chained run list
77 	 * when over subscription
78 	 */
79 	if (*over_subscription)
80 		*rlib_size += pm->pmf->runlist_size;
81 
82 	pr_debug("runlist ib size %d\n", *rlib_size);
83 }
84 
85 static int pm_allocate_runlist_ib(struct packet_manager *pm,
86 				unsigned int **rl_buffer,
87 				uint64_t *rl_gpu_buffer,
88 				unsigned int *rl_buffer_size,
89 				bool *is_over_subscription)
90 {
91 	int retval;
92 
93 	if (WARN_ON(pm->allocated))
94 		return -EINVAL;
95 
96 	pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
97 
98 	mutex_lock(&pm->lock);
99 
100 	retval = kfd_gtt_sa_allocate(pm->dqm->dev, *rl_buffer_size,
101 					&pm->ib_buffer_obj);
102 
103 	if (retval) {
104 		pr_err("Failed to allocate runlist IB\n");
105 		goto out;
106 	}
107 
108 	*(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
109 	*rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
110 
111 	memset(*rl_buffer, 0, *rl_buffer_size);
112 	pm->allocated = true;
113 
114 out:
115 	mutex_unlock(&pm->lock);
116 	return retval;
117 }
118 
119 static int pm_create_runlist_ib(struct packet_manager *pm,
120 				struct list_head *queues,
121 				uint64_t *rl_gpu_addr,
122 				size_t *rl_size_bytes)
123 {
124 	unsigned int alloc_size_bytes;
125 	unsigned int *rl_buffer, rl_wptr, i;
126 	int retval, proccesses_mapped;
127 	struct device_process_node *cur;
128 	struct qcm_process_device *qpd;
129 	struct queue *q;
130 	struct kernel_queue *kq;
131 	bool is_over_subscription;
132 
133 	rl_wptr = retval = proccesses_mapped = 0;
134 
135 	retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
136 				&alloc_size_bytes, &is_over_subscription);
137 	if (retval)
138 		return retval;
139 
140 	*rl_size_bytes = alloc_size_bytes;
141 	pm->ib_size_bytes = alloc_size_bytes;
142 
143 	pr_debug("Building runlist ib process count: %d queues count %d\n",
144 		pm->dqm->processes_count, pm->dqm->queue_count);
145 
146 	/* build the run list ib packet */
147 	list_for_each_entry(cur, queues, list) {
148 		qpd = cur->qpd;
149 		/* build map process packet */
150 		if (proccesses_mapped >= pm->dqm->processes_count) {
151 			pr_debug("Not enough space left in runlist IB\n");
152 			pm_release_ib(pm);
153 			return -ENOMEM;
154 		}
155 
156 		retval = pm->pmf->map_process(pm, &rl_buffer[rl_wptr], qpd);
157 		if (retval)
158 			return retval;
159 
160 		proccesses_mapped++;
161 		inc_wptr(&rl_wptr, pm->pmf->map_process_size,
162 				alloc_size_bytes);
163 
164 		list_for_each_entry(kq, &qpd->priv_queue_list, list) {
165 			if (!kq->queue->properties.is_active)
166 				continue;
167 
168 			pr_debug("static_queue, mapping kernel q %d, is debug status %d\n",
169 				kq->queue->queue, qpd->is_debug);
170 
171 			retval = pm->pmf->map_queues(pm,
172 						&rl_buffer[rl_wptr],
173 						kq->queue,
174 						qpd->is_debug);
175 			if (retval)
176 				return retval;
177 
178 			inc_wptr(&rl_wptr,
179 				pm->pmf->map_queues_size,
180 				alloc_size_bytes);
181 		}
182 
183 		list_for_each_entry(q, &qpd->queues_list, list) {
184 			if (!q->properties.is_active)
185 				continue;
186 
187 			pr_debug("static_queue, mapping user queue %d, is debug status %d\n",
188 				q->queue, qpd->is_debug);
189 
190 			retval = pm->pmf->map_queues(pm,
191 						&rl_buffer[rl_wptr],
192 						q,
193 						qpd->is_debug);
194 
195 			if (retval)
196 				return retval;
197 
198 			inc_wptr(&rl_wptr,
199 				pm->pmf->map_queues_size,
200 				alloc_size_bytes);
201 		}
202 	}
203 
204 	pr_debug("Finished map process and queues to runlist\n");
205 
206 	if (is_over_subscription) {
207 		if (!pm->is_over_subscription)
208 			pr_warn("Runlist is getting oversubscribed. Expect reduced ROCm performance.\n");
209 		retval = pm->pmf->runlist(pm, &rl_buffer[rl_wptr],
210 					*rl_gpu_addr,
211 					alloc_size_bytes / sizeof(uint32_t),
212 					true);
213 	}
214 	pm->is_over_subscription = is_over_subscription;
215 
216 	for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
217 		pr_debug("0x%2X ", rl_buffer[i]);
218 	pr_debug("\n");
219 
220 	return retval;
221 }
222 
223 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
224 {
225 	switch (dqm->dev->device_info->asic_family) {
226 	case CHIP_KAVERI:
227 	case CHIP_HAWAII:
228 		/* PM4 packet structures on CIK are the same as on VI */
229 	case CHIP_CARRIZO:
230 	case CHIP_TONGA:
231 	case CHIP_FIJI:
232 	case CHIP_POLARIS10:
233 	case CHIP_POLARIS11:
234 	case CHIP_POLARIS12:
235 	case CHIP_VEGAM:
236 		pm->pmf = &kfd_vi_pm_funcs;
237 		break;
238 	case CHIP_VEGA10:
239 	case CHIP_VEGA12:
240 	case CHIP_VEGA20:
241 	case CHIP_RAVEN:
242 		pm->pmf = &kfd_v9_pm_funcs;
243 		break;
244 	case CHIP_NAVI10:
245 		pm->pmf = &kfd_v10_pm_funcs;
246 		break;
247 	default:
248 		WARN(1, "Unexpected ASIC family %u",
249 		     dqm->dev->device_info->asic_family);
250 		return -EINVAL;
251 	}
252 
253 	pm->dqm = dqm;
254 	mutex_init(&pm->lock);
255 	pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
256 	if (!pm->priv_queue) {
257 		mutex_destroy(&pm->lock);
258 		return -ENOMEM;
259 	}
260 	pm->allocated = false;
261 
262 	return 0;
263 }
264 
265 void pm_uninit(struct packet_manager *pm)
266 {
267 	mutex_destroy(&pm->lock);
268 	kernel_queue_uninit(pm->priv_queue);
269 }
270 
271 int pm_send_set_resources(struct packet_manager *pm,
272 				struct scheduling_resources *res)
273 {
274 	uint32_t *buffer, size;
275 	int retval = 0;
276 
277 	size = pm->pmf->set_resources_size;
278 	mutex_lock(&pm->lock);
279 	pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
280 					size / sizeof(uint32_t),
281 					(unsigned int **)&buffer);
282 	if (!buffer) {
283 		pr_err("Failed to allocate buffer on kernel queue\n");
284 		retval = -ENOMEM;
285 		goto out;
286 	}
287 
288 	retval = pm->pmf->set_resources(pm, buffer, res);
289 	if (!retval)
290 		pm->priv_queue->ops.submit_packet(pm->priv_queue);
291 	else
292 		pm->priv_queue->ops.rollback_packet(pm->priv_queue);
293 
294 out:
295 	mutex_unlock(&pm->lock);
296 
297 	return retval;
298 }
299 
300 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
301 {
302 	uint64_t rl_gpu_ib_addr;
303 	uint32_t *rl_buffer;
304 	size_t rl_ib_size, packet_size_dwords;
305 	int retval;
306 
307 	retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
308 					&rl_ib_size);
309 	if (retval)
310 		goto fail_create_runlist_ib;
311 
312 	pr_debug("runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
313 
314 	packet_size_dwords = pm->pmf->runlist_size / sizeof(uint32_t);
315 	mutex_lock(&pm->lock);
316 
317 	retval = pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
318 					packet_size_dwords, &rl_buffer);
319 	if (retval)
320 		goto fail_acquire_packet_buffer;
321 
322 	retval = pm->pmf->runlist(pm, rl_buffer, rl_gpu_ib_addr,
323 					rl_ib_size / sizeof(uint32_t), false);
324 	if (retval)
325 		goto fail_create_runlist;
326 
327 	pm->priv_queue->ops.submit_packet(pm->priv_queue);
328 
329 	mutex_unlock(&pm->lock);
330 
331 	return retval;
332 
333 fail_create_runlist:
334 	pm->priv_queue->ops.rollback_packet(pm->priv_queue);
335 fail_acquire_packet_buffer:
336 	mutex_unlock(&pm->lock);
337 fail_create_runlist_ib:
338 	pm_release_ib(pm);
339 	return retval;
340 }
341 
342 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
343 			uint32_t fence_value)
344 {
345 	uint32_t *buffer, size;
346 	int retval = 0;
347 
348 	if (WARN_ON(!fence_address))
349 		return -EFAULT;
350 
351 	size = pm->pmf->query_status_size;
352 	mutex_lock(&pm->lock);
353 	pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
354 			size / sizeof(uint32_t), (unsigned int **)&buffer);
355 	if (!buffer) {
356 		pr_err("Failed to allocate buffer on kernel queue\n");
357 		retval = -ENOMEM;
358 		goto out;
359 	}
360 
361 	retval = pm->pmf->query_status(pm, buffer, fence_address, fence_value);
362 	if (!retval)
363 		pm->priv_queue->ops.submit_packet(pm->priv_queue);
364 	else
365 		pm->priv_queue->ops.rollback_packet(pm->priv_queue);
366 
367 out:
368 	mutex_unlock(&pm->lock);
369 	return retval;
370 }
371 
372 int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
373 			enum kfd_unmap_queues_filter filter,
374 			uint32_t filter_param, bool reset,
375 			unsigned int sdma_engine)
376 {
377 	uint32_t *buffer, size;
378 	int retval = 0;
379 
380 	size = pm->pmf->unmap_queues_size;
381 	mutex_lock(&pm->lock);
382 	pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
383 			size / sizeof(uint32_t), (unsigned int **)&buffer);
384 	if (!buffer) {
385 		pr_err("Failed to allocate buffer on kernel queue\n");
386 		retval = -ENOMEM;
387 		goto out;
388 	}
389 
390 	retval = pm->pmf->unmap_queues(pm, buffer, type, filter, filter_param,
391 				       reset, sdma_engine);
392 	if (!retval)
393 		pm->priv_queue->ops.submit_packet(pm->priv_queue);
394 	else
395 		pm->priv_queue->ops.rollback_packet(pm->priv_queue);
396 
397 out:
398 	mutex_unlock(&pm->lock);
399 	return retval;
400 }
401 
402 void pm_release_ib(struct packet_manager *pm)
403 {
404 	mutex_lock(&pm->lock);
405 	if (pm->allocated) {
406 		kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj);
407 		pm->allocated = false;
408 	}
409 	mutex_unlock(&pm->lock);
410 }
411 
412 #if defined(CONFIG_DEBUG_FS)
413 
414 int pm_debugfs_runlist(struct seq_file *m, void *data)
415 {
416 	struct packet_manager *pm = data;
417 
418 	mutex_lock(&pm->lock);
419 
420 	if (!pm->allocated) {
421 		seq_puts(m, "  No active runlist\n");
422 		goto out;
423 	}
424 
425 	seq_hex_dump(m, "  ", DUMP_PREFIX_OFFSET, 32, 4,
426 		     pm->ib_buffer_obj->cpu_ptr, pm->ib_size_bytes, false);
427 
428 out:
429 	mutex_unlock(&pm->lock);
430 	return 0;
431 }
432 
433 int pm_debugfs_hang_hws(struct packet_manager *pm)
434 {
435 	uint32_t *buffer, size;
436 	int r = 0;
437 
438 	size = pm->pmf->query_status_size;
439 	mutex_lock(&pm->lock);
440 	pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
441 			size / sizeof(uint32_t), (unsigned int **)&buffer);
442 	if (!buffer) {
443 		pr_err("Failed to allocate buffer on kernel queue\n");
444 		r = -ENOMEM;
445 		goto out;
446 	}
447 	memset(buffer, 0x55, size);
448 	pm->priv_queue->ops.submit_packet(pm->priv_queue);
449 
450 	pr_info("Submitting %x %x %x %x %x %x %x to HIQ to hang the HWS.",
451 		buffer[0], buffer[1], buffer[2], buffer[3],
452 		buffer[4], buffer[5], buffer[6]);
453 out:
454 	mutex_unlock(&pm->lock);
455 	return r;
456 }
457 
458 
459 #endif
460