xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_packet_manager.c (revision ed4543328f7108e1047b83b96ca7f7208747d930)
1d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT
2241f24f8SBen Goz /*
3d87f36a0SRajneesh Bhardwaj  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4241f24f8SBen Goz  *
5241f24f8SBen Goz  * Permission is hereby granted, free of charge, to any person obtaining a
6241f24f8SBen Goz  * copy of this software and associated documentation files (the "Software"),
7241f24f8SBen Goz  * to deal in the Software without restriction, including without limitation
8241f24f8SBen Goz  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9241f24f8SBen Goz  * and/or sell copies of the Software, and to permit persons to whom the
10241f24f8SBen Goz  * Software is furnished to do so, subject to the following conditions:
11241f24f8SBen Goz  *
12241f24f8SBen Goz  * The above copyright notice and this permission notice shall be included in
13241f24f8SBen Goz  * all copies or substantial portions of the Software.
14241f24f8SBen Goz  *
15241f24f8SBen Goz  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16241f24f8SBen Goz  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17241f24f8SBen Goz  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18241f24f8SBen Goz  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19241f24f8SBen Goz  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20241f24f8SBen Goz  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21241f24f8SBen Goz  * OTHER DEALINGS IN THE SOFTWARE.
22241f24f8SBen Goz  *
23241f24f8SBen Goz  */
24241f24f8SBen Goz 
25241f24f8SBen Goz #include <linux/slab.h>
26241f24f8SBen Goz #include <linux/mutex.h>
27241f24f8SBen Goz #include "kfd_device_queue_manager.h"
28241f24f8SBen Goz #include "kfd_kernel_queue.h"
29241f24f8SBen Goz #include "kfd_priv.h"
30241f24f8SBen Goz 
inc_wptr(unsigned int * wptr,unsigned int increment_bytes,unsigned int buffer_size_bytes)31241f24f8SBen Goz static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
32241f24f8SBen Goz 				unsigned int buffer_size_bytes)
33241f24f8SBen Goz {
34241f24f8SBen Goz 	unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
35241f24f8SBen Goz 
3632fa8219SFelix Kuehling 	WARN((temp * sizeof(uint32_t)) > buffer_size_bytes,
3732fa8219SFelix Kuehling 	     "Runlist IB overflow");
38241f24f8SBen Goz 	*wptr = temp;
39241f24f8SBen Goz }
40241f24f8SBen Goz 
pm_calc_rlib_size(struct packet_manager * pm,unsigned int * rlib_size,bool * over_subscription)41241f24f8SBen Goz static void pm_calc_rlib_size(struct packet_manager *pm,
42241f24f8SBen Goz 				unsigned int *rlib_size,
43241f24f8SBen Goz 				bool *over_subscription)
44241f24f8SBen Goz {
45b8020b03SJoseph Greathouse 	unsigned int process_count, queue_count, compute_queue_count, gws_queue_count;
46e1940fa4SBen Goz 	unsigned int map_queue_size;
47a99c6d4fSFelix Kuehling 	unsigned int max_proc_per_quantum = 1;
48*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
49*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
50241f24f8SBen Goz 
51241f24f8SBen Goz 	process_count = pm->dqm->processes_count;
5281b820b3SYong Zhao 	queue_count = pm->dqm->active_queue_count;
53b42902f4SYong Zhao 	compute_queue_count = pm->dqm->active_cp_queue_count;
54b8020b03SJoseph Greathouse 	gws_queue_count = pm->dqm->gws_queue_count;
55241f24f8SBen Goz 
56a99c6d4fSFelix Kuehling 	/* check if there is over subscription
57a99c6d4fSFelix Kuehling 	 * Note: the arbitration between the number of VMIDs and
58a99c6d4fSFelix Kuehling 	 * hws_max_conc_proc has been done in
59a99c6d4fSFelix Kuehling 	 * kgd2kfd_device_init().
60a99c6d4fSFelix Kuehling 	 */
61241f24f8SBen Goz 	*over_subscription = false;
62a99c6d4fSFelix Kuehling 
63*4312b60fSLijo Lazar 	if (node->max_proc_per_quantum > 1)
64*4312b60fSLijo Lazar 		max_proc_per_quantum = node->max_proc_per_quantum;
65a99c6d4fSFelix Kuehling 
66a99c6d4fSFelix Kuehling 	if ((process_count > max_proc_per_quantum) ||
67b8020b03SJoseph Greathouse 	    compute_queue_count > get_cp_queues_num(pm->dqm) ||
68b8020b03SJoseph Greathouse 	    gws_queue_count > 1) {
69241f24f8SBen Goz 		*over_subscription = true;
70*4312b60fSLijo Lazar 		dev_dbg(dev, "Over subscribed runlist\n");
71241f24f8SBen Goz 	}
72241f24f8SBen Goz 
73f6e27ff1SFelix Kuehling 	map_queue_size = pm->pmf->map_queues_size;
74241f24f8SBen Goz 	/* calculate run list ib allocation size */
75f6e27ff1SFelix Kuehling 	*rlib_size = process_count * pm->pmf->map_process_size +
76e1940fa4SBen Goz 		     queue_count * map_queue_size;
77241f24f8SBen Goz 
78241f24f8SBen Goz 	/*
79241f24f8SBen Goz 	 * Increase the allocation size in case we need a chained run list
80241f24f8SBen Goz 	 * when over subscription
81241f24f8SBen Goz 	 */
82241f24f8SBen Goz 	if (*over_subscription)
83f6e27ff1SFelix Kuehling 		*rlib_size += pm->pmf->runlist_size;
84241f24f8SBen Goz 
85*4312b60fSLijo Lazar 	dev_dbg(dev, "runlist ib size %d\n", *rlib_size);
86241f24f8SBen Goz }
87241f24f8SBen Goz 
pm_allocate_runlist_ib(struct packet_manager * pm,unsigned int ** rl_buffer,uint64_t * rl_gpu_buffer,unsigned int * rl_buffer_size,bool * is_over_subscription)88241f24f8SBen Goz static int pm_allocate_runlist_ib(struct packet_manager *pm,
89241f24f8SBen Goz 				unsigned int **rl_buffer,
90241f24f8SBen Goz 				uint64_t *rl_gpu_buffer,
91241f24f8SBen Goz 				unsigned int *rl_buffer_size,
92241f24f8SBen Goz 				bool *is_over_subscription)
93241f24f8SBen Goz {
94*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
95*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
96241f24f8SBen Goz 	int retval;
97241f24f8SBen Goz 
9832fa8219SFelix Kuehling 	if (WARN_ON(pm->allocated))
9932fa8219SFelix Kuehling 		return -EINVAL;
100241f24f8SBen Goz 
101241f24f8SBen Goz 	pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
102241f24f8SBen Goz 
103bfdcbfd2SBen Goz 	mutex_lock(&pm->lock);
104bfdcbfd2SBen Goz 
105*4312b60fSLijo Lazar 	retval = kfd_gtt_sa_allocate(node, *rl_buffer_size, &pm->ib_buffer_obj);
106241f24f8SBen Goz 
1074eacc26bSKent Russell 	if (retval) {
108*4312b60fSLijo Lazar 		dev_err(dev, "Failed to allocate runlist IB\n");
109bfdcbfd2SBen Goz 		goto out;
110241f24f8SBen Goz 	}
111241f24f8SBen Goz 
112241f24f8SBen Goz 	*(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
113241f24f8SBen Goz 	*rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
114241f24f8SBen Goz 
115241f24f8SBen Goz 	memset(*rl_buffer, 0, *rl_buffer_size);
116241f24f8SBen Goz 	pm->allocated = true;
117bfdcbfd2SBen Goz 
118bfdcbfd2SBen Goz out:
119bfdcbfd2SBen Goz 	mutex_unlock(&pm->lock);
120241f24f8SBen Goz 	return retval;
121241f24f8SBen Goz }
122241f24f8SBen Goz 
pm_create_runlist_ib(struct packet_manager * pm,struct list_head * queues,uint64_t * rl_gpu_addr,size_t * rl_size_bytes)123241f24f8SBen Goz static int pm_create_runlist_ib(struct packet_manager *pm,
124241f24f8SBen Goz 				struct list_head *queues,
125241f24f8SBen Goz 				uint64_t *rl_gpu_addr,
126241f24f8SBen Goz 				size_t *rl_size_bytes)
127241f24f8SBen Goz {
128241f24f8SBen Goz 	unsigned int alloc_size_bytes;
129241f24f8SBen Goz 	unsigned int *rl_buffer, rl_wptr, i;
130*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
131*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
132c3c5cc9aSJonathan Kim 	int retval, processes_mapped;
133241f24f8SBen Goz 	struct device_process_node *cur;
134241f24f8SBen Goz 	struct qcm_process_device *qpd;
135241f24f8SBen Goz 	struct queue *q;
136241f24f8SBen Goz 	struct kernel_queue *kq;
137241f24f8SBen Goz 	bool is_over_subscription;
138241f24f8SBen Goz 
139c3c5cc9aSJonathan Kim 	rl_wptr = retval = processes_mapped = 0;
140241f24f8SBen Goz 
141241f24f8SBen Goz 	retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
142241f24f8SBen Goz 				&alloc_size_bytes, &is_over_subscription);
1434eacc26bSKent Russell 	if (retval)
144241f24f8SBen Goz 		return retval;
145241f24f8SBen Goz 
146241f24f8SBen Goz 	*rl_size_bytes = alloc_size_bytes;
147851a645eSFelix Kuehling 	pm->ib_size_bytes = alloc_size_bytes;
148241f24f8SBen Goz 
149*4312b60fSLijo Lazar 	dev_dbg(dev, "Building runlist ib process count: %d queues count %d\n",
15081b820b3SYong Zhao 		pm->dqm->processes_count, pm->dqm->active_queue_count);
151241f24f8SBen Goz 
152241f24f8SBen Goz 	/* build the run list ib packet */
153241f24f8SBen Goz 	list_for_each_entry(cur, queues, list) {
154241f24f8SBen Goz 		qpd = cur->qpd;
155241f24f8SBen Goz 		/* build map process packet */
156c3c5cc9aSJonathan Kim 		if (processes_mapped >= pm->dqm->processes_count) {
157*4312b60fSLijo Lazar 			dev_dbg(dev, "Not enough space left in runlist IB\n");
158241f24f8SBen Goz 			pm_release_ib(pm);
159241f24f8SBen Goz 			return -ENOMEM;
160241f24f8SBen Goz 		}
161992839adSYair Shachar 
162f6e27ff1SFelix Kuehling 		retval = pm->pmf->map_process(pm, &rl_buffer[rl_wptr], qpd);
1634eacc26bSKent Russell 		if (retval)
164241f24f8SBen Goz 			return retval;
165992839adSYair Shachar 
166c3c5cc9aSJonathan Kim 		processes_mapped++;
167f6e27ff1SFelix Kuehling 		inc_wptr(&rl_wptr, pm->pmf->map_process_size,
168241f24f8SBen Goz 				alloc_size_bytes);
169241f24f8SBen Goz 
170241f24f8SBen Goz 		list_for_each_entry(kq, &qpd->priv_queue_list, list) {
171991ca8eeSEdward O'Callaghan 			if (!kq->queue->properties.is_active)
172241f24f8SBen Goz 				continue;
173992839adSYair Shachar 
174*4312b60fSLijo Lazar 			dev_dbg(dev,
175*4312b60fSLijo Lazar 				"static_queue, mapping kernel q %d, is debug status %d\n",
176992839adSYair Shachar 				kq->queue->queue, qpd->is_debug);
177992839adSYair Shachar 
178f6e27ff1SFelix Kuehling 			retval = pm->pmf->map_queues(pm,
179d7b8f73eSBen Goz 						&rl_buffer[rl_wptr],
180d7b8f73eSBen Goz 						kq->queue,
181d7b8f73eSBen Goz 						qpd->is_debug);
1824eacc26bSKent Russell 			if (retval)
183241f24f8SBen Goz 				return retval;
184992839adSYair Shachar 
185992839adSYair Shachar 			inc_wptr(&rl_wptr,
186f6e27ff1SFelix Kuehling 				pm->pmf->map_queues_size,
187241f24f8SBen Goz 				alloc_size_bytes);
188241f24f8SBen Goz 		}
189241f24f8SBen Goz 
190241f24f8SBen Goz 		list_for_each_entry(q, &qpd->queues_list, list) {
191991ca8eeSEdward O'Callaghan 			if (!q->properties.is_active)
192241f24f8SBen Goz 				continue;
193992839adSYair Shachar 
194*4312b60fSLijo Lazar 			dev_dbg(dev,
195*4312b60fSLijo Lazar 				"static_queue, mapping user queue %d, is debug status %d\n",
196992839adSYair Shachar 				q->queue, qpd->is_debug);
197992839adSYair Shachar 
198f6e27ff1SFelix Kuehling 			retval = pm->pmf->map_queues(pm,
199d7b8f73eSBen Goz 						&rl_buffer[rl_wptr],
200d7b8f73eSBen Goz 						q,
201d7b8f73eSBen Goz 						qpd->is_debug);
202992839adSYair Shachar 
2034eacc26bSKent Russell 			if (retval)
204241f24f8SBen Goz 				return retval;
205992839adSYair Shachar 
206992839adSYair Shachar 			inc_wptr(&rl_wptr,
207f6e27ff1SFelix Kuehling 				pm->pmf->map_queues_size,
208241f24f8SBen Goz 				alloc_size_bytes);
209241f24f8SBen Goz 		}
210241f24f8SBen Goz 	}
211241f24f8SBen Goz 
212*4312b60fSLijo Lazar 	dev_dbg(dev, "Finished map process and queues to runlist\n");
213241f24f8SBen Goz 
214819ec5acSFelix Kuehling 	if (is_over_subscription) {
215819ec5acSFelix Kuehling 		if (!pm->is_over_subscription)
216*4312b60fSLijo Lazar 			dev_warn(
217*4312b60fSLijo Lazar 				dev,
218*4312b60fSLijo Lazar 				"Runlist is getting oversubscribed. Expect reduced ROCm performance.\n");
219f6e27ff1SFelix Kuehling 		retval = pm->pmf->runlist(pm, &rl_buffer[rl_wptr],
22032fa8219SFelix Kuehling 					*rl_gpu_addr,
22132fa8219SFelix Kuehling 					alloc_size_bytes / sizeof(uint32_t),
22232fa8219SFelix Kuehling 					true);
223819ec5acSFelix Kuehling 	}
224819ec5acSFelix Kuehling 	pm->is_over_subscription = is_over_subscription;
225241f24f8SBen Goz 
226241f24f8SBen Goz 	for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
227241f24f8SBen Goz 		pr_debug("0x%2X ", rl_buffer[i]);
228241f24f8SBen Goz 	pr_debug("\n");
229241f24f8SBen Goz 
23032fa8219SFelix Kuehling 	return retval;
231241f24f8SBen Goz }
232241f24f8SBen Goz 
pm_init(struct packet_manager * pm,struct device_queue_manager * dqm)233241f24f8SBen Goz int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
234241f24f8SBen Goz {
2357eb0502aSGraham Sider 	switch (dqm->dev->adev->asic_type) {
236f6e27ff1SFelix Kuehling 	case CHIP_KAVERI:
237f6e27ff1SFelix Kuehling 	case CHIP_HAWAII:
238f6e27ff1SFelix Kuehling 		/* PM4 packet structures on CIK are the same as on VI */
239f6e27ff1SFelix Kuehling 	case CHIP_CARRIZO:
240f6e27ff1SFelix Kuehling 	case CHIP_TONGA:
241f6e27ff1SFelix Kuehling 	case CHIP_FIJI:
242f6e27ff1SFelix Kuehling 	case CHIP_POLARIS10:
243f6e27ff1SFelix Kuehling 	case CHIP_POLARIS11:
244846a44d7SGang Ba 	case CHIP_POLARIS12:
245ed81cd6eSKent Russell 	case CHIP_VEGAM:
246f6e27ff1SFelix Kuehling 		pm->pmf = &kfd_vi_pm_funcs;
247f6e27ff1SFelix Kuehling 		break;
248f6e27ff1SFelix Kuehling 	default:
249828d9a87SHawking Zhang 		if (KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 2) ||
250828d9a87SHawking Zhang 		    KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 3))
251e4804a39SGraham Sider 			pm->pmf = &kfd_aldebaran_pm_funcs;
252e4804a39SGraham Sider 		else if (KFD_GC_VERSION(dqm->dev) >= IP_VERSION(9, 0, 1))
253e4804a39SGraham Sider 			pm->pmf = &kfd_v9_pm_funcs;
254e4804a39SGraham Sider 		else {
255f6e27ff1SFelix Kuehling 			WARN(1, "Unexpected ASIC family %u",
2567eb0502aSGraham Sider 			     dqm->dev->adev->asic_type);
257f6e27ff1SFelix Kuehling 			return -EINVAL;
258f6e27ff1SFelix Kuehling 		}
259e4804a39SGraham Sider 	}
260f6e27ff1SFelix Kuehling 
261241f24f8SBen Goz 	pm->dqm = dqm;
262241f24f8SBen Goz 	mutex_init(&pm->lock);
263241f24f8SBen Goz 	pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
2644eacc26bSKent Russell 	if (!pm->priv_queue) {
265241f24f8SBen Goz 		mutex_destroy(&pm->lock);
266241f24f8SBen Goz 		return -ENOMEM;
267241f24f8SBen Goz 	}
268241f24f8SBen Goz 	pm->allocated = false;
269241f24f8SBen Goz 
270241f24f8SBen Goz 	return 0;
271241f24f8SBen Goz }
272241f24f8SBen Goz 
pm_uninit(struct packet_manager * pm,bool hanging)273c2a77fdeSFelix Kuehling void pm_uninit(struct packet_manager *pm, bool hanging)
274241f24f8SBen Goz {
275241f24f8SBen Goz 	mutex_destroy(&pm->lock);
276c2a77fdeSFelix Kuehling 	kernel_queue_uninit(pm->priv_queue, hanging);
27778ccea9fSOak Zeng 	pm->priv_queue = NULL;
278241f24f8SBen Goz }
279241f24f8SBen Goz 
pm_send_set_resources(struct packet_manager * pm,struct scheduling_resources * res)280241f24f8SBen Goz int pm_send_set_resources(struct packet_manager *pm,
281241f24f8SBen Goz 				struct scheduling_resources *res)
282241f24f8SBen Goz {
283*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
284*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
285f6e27ff1SFelix Kuehling 	uint32_t *buffer, size;
286ab7c1648SKent Russell 	int retval = 0;
287241f24f8SBen Goz 
288f6e27ff1SFelix Kuehling 	size = pm->pmf->set_resources_size;
289241f24f8SBen Goz 	mutex_lock(&pm->lock);
290a5a4d68cSYong Zhao 	kq_acquire_packet_buffer(pm->priv_queue,
291f6e27ff1SFelix Kuehling 					size / sizeof(uint32_t),
292f6e27ff1SFelix Kuehling 					(unsigned int **)&buffer);
293f6e27ff1SFelix Kuehling 	if (!buffer) {
294*4312b60fSLijo Lazar 		dev_err(dev, "Failed to allocate buffer on kernel queue\n");
295ab7c1648SKent Russell 		retval = -ENOMEM;
296ab7c1648SKent Russell 		goto out;
297241f24f8SBen Goz 	}
298241f24f8SBen Goz 
299f6e27ff1SFelix Kuehling 	retval = pm->pmf->set_resources(pm, buffer, res);
300f6e27ff1SFelix Kuehling 	if (!retval)
301a5a4d68cSYong Zhao 		kq_submit_packet(pm->priv_queue);
302f6e27ff1SFelix Kuehling 	else
303a5a4d68cSYong Zhao 		kq_rollback_packet(pm->priv_queue);
304241f24f8SBen Goz 
305ab7c1648SKent Russell out:
306241f24f8SBen Goz 	mutex_unlock(&pm->lock);
307241f24f8SBen Goz 
308ab7c1648SKent Russell 	return retval;
309241f24f8SBen Goz }
310241f24f8SBen Goz 
pm_send_runlist(struct packet_manager * pm,struct list_head * dqm_queues)311241f24f8SBen Goz int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
312241f24f8SBen Goz {
313241f24f8SBen Goz 	uint64_t rl_gpu_ib_addr;
314241f24f8SBen Goz 	uint32_t *rl_buffer;
315241f24f8SBen Goz 	size_t rl_ib_size, packet_size_dwords;
316241f24f8SBen Goz 	int retval;
317241f24f8SBen Goz 
318241f24f8SBen Goz 	retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
319241f24f8SBen Goz 					&rl_ib_size);
3204eacc26bSKent Russell 	if (retval)
321241f24f8SBen Goz 		goto fail_create_runlist_ib;
322241f24f8SBen Goz 
32379775b62SKent Russell 	pr_debug("runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
324241f24f8SBen Goz 
325f6e27ff1SFelix Kuehling 	packet_size_dwords = pm->pmf->runlist_size / sizeof(uint32_t);
326241f24f8SBen Goz 	mutex_lock(&pm->lock);
327241f24f8SBen Goz 
328a5a4d68cSYong Zhao 	retval = kq_acquire_packet_buffer(pm->priv_queue,
329241f24f8SBen Goz 					packet_size_dwords, &rl_buffer);
3304eacc26bSKent Russell 	if (retval)
331241f24f8SBen Goz 		goto fail_acquire_packet_buffer;
332241f24f8SBen Goz 
333f6e27ff1SFelix Kuehling 	retval = pm->pmf->runlist(pm, rl_buffer, rl_gpu_ib_addr,
334241f24f8SBen Goz 					rl_ib_size / sizeof(uint32_t), false);
3354eacc26bSKent Russell 	if (retval)
336241f24f8SBen Goz 		goto fail_create_runlist;
337241f24f8SBen Goz 
338a5a4d68cSYong Zhao 	kq_submit_packet(pm->priv_queue);
339241f24f8SBen Goz 
340241f24f8SBen Goz 	mutex_unlock(&pm->lock);
341241f24f8SBen Goz 
342241f24f8SBen Goz 	return retval;
343241f24f8SBen Goz 
344241f24f8SBen Goz fail_create_runlist:
345a5a4d68cSYong Zhao 	kq_rollback_packet(pm->priv_queue);
346241f24f8SBen Goz fail_acquire_packet_buffer:
347241f24f8SBen Goz 	mutex_unlock(&pm->lock);
348241f24f8SBen Goz fail_create_runlist_ib:
349241f24f8SBen Goz 	pm_release_ib(pm);
350241f24f8SBen Goz 	return retval;
351241f24f8SBen Goz }
352241f24f8SBen Goz 
pm_send_query_status(struct packet_manager * pm,uint64_t fence_address,uint64_t fence_value)353241f24f8SBen Goz int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
354b010affeSQu Huang 			uint64_t fence_value)
355241f24f8SBen Goz {
356*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
357*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
358f6e27ff1SFelix Kuehling 	uint32_t *buffer, size;
359f6e27ff1SFelix Kuehling 	int retval = 0;
360241f24f8SBen Goz 
36132fa8219SFelix Kuehling 	if (WARN_ON(!fence_address))
36232fa8219SFelix Kuehling 		return -EFAULT;
363241f24f8SBen Goz 
364f6e27ff1SFelix Kuehling 	size = pm->pmf->query_status_size;
365241f24f8SBen Goz 	mutex_lock(&pm->lock);
366a5a4d68cSYong Zhao 	kq_acquire_packet_buffer(pm->priv_queue,
367f6e27ff1SFelix Kuehling 			size / sizeof(uint32_t), (unsigned int **)&buffer);
368f6e27ff1SFelix Kuehling 	if (!buffer) {
369*4312b60fSLijo Lazar 		dev_err(dev, "Failed to allocate buffer on kernel queue\n");
370f6e27ff1SFelix Kuehling 		retval = -ENOMEM;
371f6e27ff1SFelix Kuehling 		goto out;
372f6e27ff1SFelix Kuehling 	}
373241f24f8SBen Goz 
374f6e27ff1SFelix Kuehling 	retval = pm->pmf->query_status(pm, buffer, fence_address, fence_value);
375f6e27ff1SFelix Kuehling 	if (!retval)
376a5a4d68cSYong Zhao 		kq_submit_packet(pm->priv_queue);
377f6e27ff1SFelix Kuehling 	else
378a5a4d68cSYong Zhao 		kq_rollback_packet(pm->priv_queue);
379241f24f8SBen Goz 
380f6e27ff1SFelix Kuehling out:
381241f24f8SBen Goz 	mutex_unlock(&pm->lock);
382241f24f8SBen Goz 	return retval;
383241f24f8SBen Goz }
384241f24f8SBen Goz 
pm_update_grace_period(struct packet_manager * pm,uint32_t grace_period)3857cee6a68SJonathan Kim int pm_update_grace_period(struct packet_manager *pm, uint32_t grace_period)
3867cee6a68SJonathan Kim {
387*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
388*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
3897cee6a68SJonathan Kim 	int retval = 0;
3907cee6a68SJonathan Kim 	uint32_t *buffer, size;
3917cee6a68SJonathan Kim 
3927cee6a68SJonathan Kim 	size = pm->pmf->set_grace_period_size;
3937cee6a68SJonathan Kim 
3947cee6a68SJonathan Kim 	mutex_lock(&pm->lock);
3957cee6a68SJonathan Kim 
3967cee6a68SJonathan Kim 	if (size) {
3977cee6a68SJonathan Kim 		kq_acquire_packet_buffer(pm->priv_queue,
3987cee6a68SJonathan Kim 			size / sizeof(uint32_t),
3997cee6a68SJonathan Kim 			(unsigned int **)&buffer);
4007cee6a68SJonathan Kim 
4017cee6a68SJonathan Kim 		if (!buffer) {
402*4312b60fSLijo Lazar 			dev_err(dev,
403*4312b60fSLijo Lazar 				"Failed to allocate buffer on kernel queue\n");
4047cee6a68SJonathan Kim 			retval = -ENOMEM;
4057cee6a68SJonathan Kim 			goto out;
4067cee6a68SJonathan Kim 		}
4077cee6a68SJonathan Kim 
4087cee6a68SJonathan Kim 		retval = pm->pmf->set_grace_period(pm, buffer, grace_period);
4097cee6a68SJonathan Kim 		if (!retval)
4107cee6a68SJonathan Kim 			kq_submit_packet(pm->priv_queue);
4117cee6a68SJonathan Kim 		else
4127cee6a68SJonathan Kim 			kq_rollback_packet(pm->priv_queue);
4137cee6a68SJonathan Kim 	}
4147cee6a68SJonathan Kim 
4157cee6a68SJonathan Kim out:
4167cee6a68SJonathan Kim 	mutex_unlock(&pm->lock);
4177cee6a68SJonathan Kim 	return retval;
4187cee6a68SJonathan Kim }
4197cee6a68SJonathan Kim 
pm_send_unmap_queue(struct packet_manager * pm,enum kfd_unmap_queues_filter filter,uint32_t filter_param,bool reset)420d2cb0b21SJonathan Kim int pm_send_unmap_queue(struct packet_manager *pm,
4217da2bcf8SYong Zhao 			enum kfd_unmap_queues_filter filter,
422d2cb0b21SJonathan Kim 			uint32_t filter_param, bool reset)
423241f24f8SBen Goz {
424*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
425*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
426f6e27ff1SFelix Kuehling 	uint32_t *buffer, size;
427f6e27ff1SFelix Kuehling 	int retval = 0;
428241f24f8SBen Goz 
429f6e27ff1SFelix Kuehling 	size = pm->pmf->unmap_queues_size;
430241f24f8SBen Goz 	mutex_lock(&pm->lock);
431a5a4d68cSYong Zhao 	kq_acquire_packet_buffer(pm->priv_queue,
432f6e27ff1SFelix Kuehling 			size / sizeof(uint32_t), (unsigned int **)&buffer);
433f6e27ff1SFelix Kuehling 	if (!buffer) {
434*4312b60fSLijo Lazar 		dev_err(dev, "Failed to allocate buffer on kernel queue\n");
435f6e27ff1SFelix Kuehling 		retval = -ENOMEM;
436f6e27ff1SFelix Kuehling 		goto out;
437241f24f8SBen Goz 	}
438241f24f8SBen Goz 
439d2cb0b21SJonathan Kim 	retval = pm->pmf->unmap_queues(pm, buffer, filter, filter_param, reset);
440f6e27ff1SFelix Kuehling 	if (!retval)
441a5a4d68cSYong Zhao 		kq_submit_packet(pm->priv_queue);
442f6e27ff1SFelix Kuehling 	else
443a5a4d68cSYong Zhao 		kq_rollback_packet(pm->priv_queue);
444f6e27ff1SFelix Kuehling 
445f6e27ff1SFelix Kuehling out:
446241f24f8SBen Goz 	mutex_unlock(&pm->lock);
447241f24f8SBen Goz 	return retval;
448241f24f8SBen Goz }
449241f24f8SBen Goz 
pm_release_ib(struct packet_manager * pm)450241f24f8SBen Goz void pm_release_ib(struct packet_manager *pm)
451241f24f8SBen Goz {
452241f24f8SBen Goz 	mutex_lock(&pm->lock);
453241f24f8SBen Goz 	if (pm->allocated) {
454a86aa3caSOded Gabbay 		kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj);
455241f24f8SBen Goz 		pm->allocated = false;
456241f24f8SBen Goz 	}
457241f24f8SBen Goz 	mutex_unlock(&pm->lock);
458241f24f8SBen Goz }
459851a645eSFelix Kuehling 
460851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
461851a645eSFelix Kuehling 
pm_debugfs_runlist(struct seq_file * m,void * data)462851a645eSFelix Kuehling int pm_debugfs_runlist(struct seq_file *m, void *data)
463851a645eSFelix Kuehling {
464851a645eSFelix Kuehling 	struct packet_manager *pm = data;
465851a645eSFelix Kuehling 
466851a645eSFelix Kuehling 	mutex_lock(&pm->lock);
467851a645eSFelix Kuehling 
468851a645eSFelix Kuehling 	if (!pm->allocated) {
469851a645eSFelix Kuehling 		seq_puts(m, "  No active runlist\n");
470851a645eSFelix Kuehling 		goto out;
471851a645eSFelix Kuehling 	}
472851a645eSFelix Kuehling 
473851a645eSFelix Kuehling 	seq_hex_dump(m, "  ", DUMP_PREFIX_OFFSET, 32, 4,
474851a645eSFelix Kuehling 		     pm->ib_buffer_obj->cpu_ptr, pm->ib_size_bytes, false);
475851a645eSFelix Kuehling 
476851a645eSFelix Kuehling out:
477851a645eSFelix Kuehling 	mutex_unlock(&pm->lock);
478851a645eSFelix Kuehling 	return 0;
479851a645eSFelix Kuehling }
480851a645eSFelix Kuehling 
pm_debugfs_hang_hws(struct packet_manager * pm)481a29ec470SShaoyun Liu int pm_debugfs_hang_hws(struct packet_manager *pm)
482a29ec470SShaoyun Liu {
483*4312b60fSLijo Lazar 	struct kfd_node *node = pm->dqm->dev;
484*4312b60fSLijo Lazar 	struct device *dev = node->adev->dev;
485a29ec470SShaoyun Liu 	uint32_t *buffer, size;
486a29ec470SShaoyun Liu 	int r = 0;
487a29ec470SShaoyun Liu 
4884f942aaeSOak Zeng 	if (!pm->priv_queue)
4894f942aaeSOak Zeng 		return -EAGAIN;
4904f942aaeSOak Zeng 
491a29ec470SShaoyun Liu 	size = pm->pmf->query_status_size;
492a29ec470SShaoyun Liu 	mutex_lock(&pm->lock);
493a5a4d68cSYong Zhao 	kq_acquire_packet_buffer(pm->priv_queue,
494a29ec470SShaoyun Liu 			size / sizeof(uint32_t), (unsigned int **)&buffer);
495a29ec470SShaoyun Liu 	if (!buffer) {
496*4312b60fSLijo Lazar 		dev_err(dev, "Failed to allocate buffer on kernel queue\n");
497a29ec470SShaoyun Liu 		r = -ENOMEM;
498a29ec470SShaoyun Liu 		goto out;
499a29ec470SShaoyun Liu 	}
500a29ec470SShaoyun Liu 	memset(buffer, 0x55, size);
501a5a4d68cSYong Zhao 	kq_submit_packet(pm->priv_queue);
502a29ec470SShaoyun Liu 
503*4312b60fSLijo Lazar 	dev_info(dev, "Submitting %x %x %x %x %x %x %x to HIQ to hang the HWS.",
504*4312b60fSLijo Lazar 		 buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],
505*4312b60fSLijo Lazar 		 buffer[5], buffer[6]);
506a29ec470SShaoyun Liu out:
507a29ec470SShaoyun Liu 	mutex_unlock(&pm->lock);
508a29ec470SShaoyun Liu 	return r;
509a29ec470SShaoyun Liu }
510a29ec470SShaoyun Liu 
511a29ec470SShaoyun Liu 
512851a645eSFelix Kuehling #endif
513