1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include "kfd_device_queue_manager.h"
28 #include "kfd_kernel_queue.h"
31 static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
32 unsigned int buffer_size_bytes)
34 unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
36 WARN((temp * sizeof(uint32_t)) > buffer_size_bytes,
37 "Runlist IB overflow");
41 static void pm_calc_rlib_size(struct packet_manager *pm,
42 unsigned int *rlib_size,
43 bool *over_subscription)
45 unsigned int process_count, queue_count, compute_queue_count, gws_queue_count;
46 unsigned int map_queue_size;
47 unsigned int max_proc_per_quantum = 1;
48 struct kfd_node *node = pm->dqm->dev;
49 struct device *dev = node->adev->dev;
51 process_count = pm->dqm->processes_count;
52 queue_count = pm->dqm->active_queue_count;
53 compute_queue_count = pm->dqm->active_cp_queue_count;
54 gws_queue_count = pm->dqm->gws_queue_count;
56 /* check if there is over subscription
57 * Note: the arbitration between the number of VMIDs and
58 * hws_max_conc_proc has been done in
59 * kgd2kfd_device_init().
61 *over_subscription = false;
63 if (node->max_proc_per_quantum > 1)
64 max_proc_per_quantum = node->max_proc_per_quantum;
66 if ((process_count > max_proc_per_quantum) ||
67 compute_queue_count > get_cp_queues_num(pm->dqm) ||
68 gws_queue_count > 1) {
69 *over_subscription = true;
70 dev_dbg(dev, "Over subscribed runlist\n");
73 map_queue_size = pm->pmf->map_queues_size;
74 /* calculate run list ib allocation size */
75 *rlib_size = process_count * pm->pmf->map_process_size +
76 queue_count * map_queue_size;
79 * Increase the allocation size in case we need a chained run list
80 * when over subscription
82 if (*over_subscription)
83 *rlib_size += pm->pmf->runlist_size;
85 dev_dbg(dev, "runlist ib size %d\n", *rlib_size);
88 static int pm_allocate_runlist_ib(struct packet_manager *pm,
89 unsigned int **rl_buffer,
90 uint64_t *rl_gpu_buffer,
91 unsigned int *rl_buffer_size,
92 bool *is_over_subscription)
94 struct kfd_node *node = pm->dqm->dev;
95 struct device *dev = node->adev->dev;
98 if (WARN_ON(pm->allocated))
101 pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
103 mutex_lock(&pm->lock);
105 retval = kfd_gtt_sa_allocate(node, *rl_buffer_size, &pm->ib_buffer_obj);
108 dev_err(dev, "Failed to allocate runlist IB\n");
112 *(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
113 *rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
115 memset(*rl_buffer, 0, *rl_buffer_size);
116 pm->allocated = true;
119 mutex_unlock(&pm->lock);
123 static int pm_create_runlist_ib(struct packet_manager *pm,
124 struct list_head *queues,
125 uint64_t *rl_gpu_addr,
126 size_t *rl_size_bytes)
128 unsigned int alloc_size_bytes;
129 unsigned int *rl_buffer, rl_wptr, i;
130 struct kfd_node *node = pm->dqm->dev;
131 struct device *dev = node->adev->dev;
132 int retval, processes_mapped;
133 struct device_process_node *cur;
134 struct qcm_process_device *qpd;
136 struct kernel_queue *kq;
137 bool is_over_subscription;
139 rl_wptr = retval = processes_mapped = 0;
141 retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
142 &alloc_size_bytes, &is_over_subscription);
146 *rl_size_bytes = alloc_size_bytes;
147 pm->ib_size_bytes = alloc_size_bytes;
149 dev_dbg(dev, "Building runlist ib process count: %d queues count %d\n",
150 pm->dqm->processes_count, pm->dqm->active_queue_count);
152 /* build the run list ib packet */
153 list_for_each_entry(cur, queues, list) {
155 /* build map process packet */
156 if (processes_mapped >= pm->dqm->processes_count) {
157 dev_dbg(dev, "Not enough space left in runlist IB\n");
162 retval = pm->pmf->map_process(pm, &rl_buffer[rl_wptr], qpd);
167 inc_wptr(&rl_wptr, pm->pmf->map_process_size,
170 list_for_each_entry(kq, &qpd->priv_queue_list, list) {
171 if (!kq->queue->properties.is_active)
175 "static_queue, mapping kernel q %d, is debug status %d\n",
176 kq->queue->queue, qpd->is_debug);
178 retval = pm->pmf->map_queues(pm,
186 pm->pmf->map_queues_size,
190 list_for_each_entry(q, &qpd->queues_list, list) {
191 if (!q->properties.is_active)
195 "static_queue, mapping user queue %d, is debug status %d\n",
196 q->queue, qpd->is_debug);
198 retval = pm->pmf->map_queues(pm,
207 pm->pmf->map_queues_size,
212 dev_dbg(dev, "Finished map process and queues to runlist\n");
214 if (is_over_subscription) {
215 if (!pm->is_over_subscription)
218 "Runlist is getting oversubscribed. Expect reduced ROCm performance.\n");
219 retval = pm->pmf->runlist(pm, &rl_buffer[rl_wptr],
221 alloc_size_bytes / sizeof(uint32_t),
224 pm->is_over_subscription = is_over_subscription;
226 for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
227 pr_debug("0x%2X ", rl_buffer[i]);
233 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
235 switch (dqm->dev->adev->asic_type) {
238 /* PM4 packet structures on CIK are the same as on VI */
246 pm->pmf = &kfd_vi_pm_funcs;
249 if (KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 2) ||
250 KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 3) ||
251 KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 4))
252 pm->pmf = &kfd_aldebaran_pm_funcs;
253 else if (KFD_GC_VERSION(dqm->dev) >= IP_VERSION(9, 0, 1))
254 pm->pmf = &kfd_v9_pm_funcs;
256 WARN(1, "Unexpected ASIC family %u",
257 dqm->dev->adev->asic_type);
263 mutex_init(&pm->lock);
264 pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
265 if (!pm->priv_queue) {
266 mutex_destroy(&pm->lock);
269 pm->allocated = false;
274 void pm_uninit(struct packet_manager *pm)
276 mutex_destroy(&pm->lock);
277 kernel_queue_uninit(pm->priv_queue);
278 pm->priv_queue = NULL;
281 int pm_send_set_resources(struct packet_manager *pm,
282 struct scheduling_resources *res)
284 struct kfd_node *node = pm->dqm->dev;
285 struct device *dev = node->adev->dev;
286 uint32_t *buffer, size;
289 size = pm->pmf->set_resources_size;
290 mutex_lock(&pm->lock);
291 kq_acquire_packet_buffer(pm->priv_queue,
292 size / sizeof(uint32_t),
293 (unsigned int **)&buffer);
295 dev_err(dev, "Failed to allocate buffer on kernel queue\n");
300 retval = pm->pmf->set_resources(pm, buffer, res);
302 retval = kq_submit_packet(pm->priv_queue);
304 kq_rollback_packet(pm->priv_queue);
307 mutex_unlock(&pm->lock);
312 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
314 uint64_t rl_gpu_ib_addr;
316 size_t rl_ib_size, packet_size_dwords;
319 retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
322 goto fail_create_runlist_ib;
324 pr_debug("runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
326 packet_size_dwords = pm->pmf->runlist_size / sizeof(uint32_t);
327 mutex_lock(&pm->lock);
329 retval = kq_acquire_packet_buffer(pm->priv_queue,
330 packet_size_dwords, &rl_buffer);
332 goto fail_acquire_packet_buffer;
334 retval = pm->pmf->runlist(pm, rl_buffer, rl_gpu_ib_addr,
335 rl_ib_size / sizeof(uint32_t), false);
337 goto fail_create_runlist;
339 retval = kq_submit_packet(pm->priv_queue);
341 mutex_unlock(&pm->lock);
346 kq_rollback_packet(pm->priv_queue);
347 fail_acquire_packet_buffer:
348 mutex_unlock(&pm->lock);
349 fail_create_runlist_ib:
354 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
355 uint64_t fence_value)
357 struct kfd_node *node = pm->dqm->dev;
358 struct device *dev = node->adev->dev;
359 uint32_t *buffer, size;
362 if (WARN_ON(!fence_address))
365 size = pm->pmf->query_status_size;
366 mutex_lock(&pm->lock);
367 kq_acquire_packet_buffer(pm->priv_queue,
368 size / sizeof(uint32_t), (unsigned int **)&buffer);
370 dev_err(dev, "Failed to allocate buffer on kernel queue\n");
375 retval = pm->pmf->query_status(pm, buffer, fence_address, fence_value);
377 retval = kq_submit_packet(pm->priv_queue);
379 kq_rollback_packet(pm->priv_queue);
382 mutex_unlock(&pm->lock);
386 int pm_update_grace_period(struct packet_manager *pm, uint32_t grace_period)
388 struct kfd_node *node = pm->dqm->dev;
389 struct device *dev = node->adev->dev;
391 uint32_t *buffer, size;
393 size = pm->pmf->set_grace_period_size;
395 mutex_lock(&pm->lock);
398 kq_acquire_packet_buffer(pm->priv_queue,
399 size / sizeof(uint32_t),
400 (unsigned int **)&buffer);
404 "Failed to allocate buffer on kernel queue\n");
409 retval = pm->pmf->set_grace_period(pm, buffer, grace_period);
411 retval = kq_submit_packet(pm->priv_queue);
413 kq_rollback_packet(pm->priv_queue);
417 mutex_unlock(&pm->lock);
421 int pm_send_unmap_queue(struct packet_manager *pm,
422 enum kfd_unmap_queues_filter filter,
423 uint32_t filter_param, bool reset)
425 struct kfd_node *node = pm->dqm->dev;
426 struct device *dev = node->adev->dev;
427 uint32_t *buffer, size;
430 size = pm->pmf->unmap_queues_size;
431 mutex_lock(&pm->lock);
432 kq_acquire_packet_buffer(pm->priv_queue,
433 size / sizeof(uint32_t), (unsigned int **)&buffer);
435 dev_err(dev, "Failed to allocate buffer on kernel queue\n");
440 retval = pm->pmf->unmap_queues(pm, buffer, filter, filter_param, reset);
442 retval = kq_submit_packet(pm->priv_queue);
444 kq_rollback_packet(pm->priv_queue);
447 mutex_unlock(&pm->lock);
451 void pm_release_ib(struct packet_manager *pm)
453 mutex_lock(&pm->lock);
455 kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj);
456 pm->allocated = false;
458 mutex_unlock(&pm->lock);
461 #if defined(CONFIG_DEBUG_FS)
463 int pm_debugfs_runlist(struct seq_file *m, void *data)
465 struct packet_manager *pm = data;
467 mutex_lock(&pm->lock);
469 if (!pm->allocated) {
470 seq_puts(m, " No active runlist\n");
474 seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 32, 4,
475 pm->ib_buffer_obj->cpu_ptr, pm->ib_size_bytes, false);
478 mutex_unlock(&pm->lock);
482 int pm_debugfs_hang_hws(struct packet_manager *pm)
484 struct kfd_node *node = pm->dqm->dev;
485 struct device *dev = node->adev->dev;
486 uint32_t *buffer, size;
492 size = pm->pmf->query_status_size;
493 mutex_lock(&pm->lock);
494 kq_acquire_packet_buffer(pm->priv_queue,
495 size / sizeof(uint32_t), (unsigned int **)&buffer);
497 dev_err(dev, "Failed to allocate buffer on kernel queue\n");
501 memset(buffer, 0x55, size);
502 kq_submit_packet(pm->priv_queue);
504 dev_info(dev, "Submitting %x %x %x %x %x %x %x to HIQ to hang the HWS.",
505 buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],
506 buffer[5], buffer[6]);
508 mutex_unlock(&pm->lock);