xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_xcp.c (revision f005ef32267c16bdb134f0e9fa4477dbe07c263a)
1*f005ef32Sjsg /*
2*f005ef32Sjsg  * Copyright 2022 Advanced Micro Devices, Inc.
3*f005ef32Sjsg  *
4*f005ef32Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
5*f005ef32Sjsg  * copy of this software and associated documentation files (the "Software"),
6*f005ef32Sjsg  * to deal in the Software without restriction, including without limitation
7*f005ef32Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*f005ef32Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
9*f005ef32Sjsg  * Software is furnished to do so, subject to the following conditions:
10*f005ef32Sjsg  *
11*f005ef32Sjsg  * The above copyright notice and this permission notice shall be included in
12*f005ef32Sjsg  * all copies or substantial portions of the Software.
13*f005ef32Sjsg  *
14*f005ef32Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*f005ef32Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*f005ef32Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*f005ef32Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*f005ef32Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*f005ef32Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*f005ef32Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
21*f005ef32Sjsg  *
22*f005ef32Sjsg  */
23*f005ef32Sjsg #include "amdgpu.h"
24*f005ef32Sjsg #include "amdgpu_xcp.h"
25*f005ef32Sjsg #include "amdgpu_drv.h"
26*f005ef32Sjsg 
27*f005ef32Sjsg #include <drm/drm_drv.h>
28*f005ef32Sjsg #include "../amdxcp/amdgpu_xcp_drv.h"
29*f005ef32Sjsg 
__amdgpu_xcp_run(struct amdgpu_xcp_mgr * xcp_mgr,struct amdgpu_xcp_ip * xcp_ip,int xcp_state)30*f005ef32Sjsg static int __amdgpu_xcp_run(struct amdgpu_xcp_mgr *xcp_mgr,
31*f005ef32Sjsg 			    struct amdgpu_xcp_ip *xcp_ip, int xcp_state)
32*f005ef32Sjsg {
33*f005ef32Sjsg 	int (*run_func)(void *handle, uint32_t inst_mask);
34*f005ef32Sjsg 	int ret = 0;
35*f005ef32Sjsg 
36*f005ef32Sjsg 	if (!xcp_ip || !xcp_ip->valid || !xcp_ip->ip_funcs)
37*f005ef32Sjsg 		return 0;
38*f005ef32Sjsg 
39*f005ef32Sjsg 	run_func = NULL;
40*f005ef32Sjsg 
41*f005ef32Sjsg 	switch (xcp_state) {
42*f005ef32Sjsg 	case AMDGPU_XCP_PREPARE_SUSPEND:
43*f005ef32Sjsg 		run_func = xcp_ip->ip_funcs->prepare_suspend;
44*f005ef32Sjsg 		break;
45*f005ef32Sjsg 	case AMDGPU_XCP_SUSPEND:
46*f005ef32Sjsg 		run_func = xcp_ip->ip_funcs->suspend;
47*f005ef32Sjsg 		break;
48*f005ef32Sjsg 	case AMDGPU_XCP_PREPARE_RESUME:
49*f005ef32Sjsg 		run_func = xcp_ip->ip_funcs->prepare_resume;
50*f005ef32Sjsg 		break;
51*f005ef32Sjsg 	case AMDGPU_XCP_RESUME:
52*f005ef32Sjsg 		run_func = xcp_ip->ip_funcs->resume;
53*f005ef32Sjsg 		break;
54*f005ef32Sjsg 	}
55*f005ef32Sjsg 
56*f005ef32Sjsg 	if (run_func)
57*f005ef32Sjsg 		ret = run_func(xcp_mgr->adev, xcp_ip->inst_mask);
58*f005ef32Sjsg 
59*f005ef32Sjsg 	return ret;
60*f005ef32Sjsg }
61*f005ef32Sjsg 
amdgpu_xcp_run_transition(struct amdgpu_xcp_mgr * xcp_mgr,int xcp_id,int state)62*f005ef32Sjsg static int amdgpu_xcp_run_transition(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
63*f005ef32Sjsg 				     int state)
64*f005ef32Sjsg {
65*f005ef32Sjsg 	struct amdgpu_xcp_ip *xcp_ip;
66*f005ef32Sjsg 	struct amdgpu_xcp *xcp;
67*f005ef32Sjsg 	int i, ret;
68*f005ef32Sjsg 
69*f005ef32Sjsg 	if (xcp_id >= MAX_XCP || !xcp_mgr->xcp[xcp_id].valid)
70*f005ef32Sjsg 		return -EINVAL;
71*f005ef32Sjsg 
72*f005ef32Sjsg 	xcp = &xcp_mgr->xcp[xcp_id];
73*f005ef32Sjsg 	for (i = 0; i < AMDGPU_XCP_MAX_BLOCKS; ++i) {
74*f005ef32Sjsg 		xcp_ip = &xcp->ip[i];
75*f005ef32Sjsg 		ret = __amdgpu_xcp_run(xcp_mgr, xcp_ip, state);
76*f005ef32Sjsg 		if (ret)
77*f005ef32Sjsg 			break;
78*f005ef32Sjsg 	}
79*f005ef32Sjsg 
80*f005ef32Sjsg 	return ret;
81*f005ef32Sjsg }
82*f005ef32Sjsg 
amdgpu_xcp_prepare_suspend(struct amdgpu_xcp_mgr * xcp_mgr,int xcp_id)83*f005ef32Sjsg int amdgpu_xcp_prepare_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
84*f005ef32Sjsg {
85*f005ef32Sjsg 	return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
86*f005ef32Sjsg 					 AMDGPU_XCP_PREPARE_SUSPEND);
87*f005ef32Sjsg }
88*f005ef32Sjsg 
amdgpu_xcp_suspend(struct amdgpu_xcp_mgr * xcp_mgr,int xcp_id)89*f005ef32Sjsg int amdgpu_xcp_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
90*f005ef32Sjsg {
91*f005ef32Sjsg 	return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_SUSPEND);
92*f005ef32Sjsg }
93*f005ef32Sjsg 
amdgpu_xcp_prepare_resume(struct amdgpu_xcp_mgr * xcp_mgr,int xcp_id)94*f005ef32Sjsg int amdgpu_xcp_prepare_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
95*f005ef32Sjsg {
96*f005ef32Sjsg 	return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
97*f005ef32Sjsg 					 AMDGPU_XCP_PREPARE_RESUME);
98*f005ef32Sjsg }
99*f005ef32Sjsg 
amdgpu_xcp_resume(struct amdgpu_xcp_mgr * xcp_mgr,int xcp_id)100*f005ef32Sjsg int amdgpu_xcp_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
101*f005ef32Sjsg {
102*f005ef32Sjsg 	return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_RESUME);
103*f005ef32Sjsg }
104*f005ef32Sjsg 
__amdgpu_xcp_add_block(struct amdgpu_xcp_mgr * xcp_mgr,int xcp_id,struct amdgpu_xcp_ip * ip)105*f005ef32Sjsg static void __amdgpu_xcp_add_block(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
106*f005ef32Sjsg 				   struct amdgpu_xcp_ip *ip)
107*f005ef32Sjsg {
108*f005ef32Sjsg 	struct amdgpu_xcp *xcp;
109*f005ef32Sjsg 
110*f005ef32Sjsg 	if (!ip)
111*f005ef32Sjsg 		return;
112*f005ef32Sjsg 
113*f005ef32Sjsg 	xcp = &xcp_mgr->xcp[xcp_id];
114*f005ef32Sjsg 	xcp->ip[ip->ip_id] = *ip;
115*f005ef32Sjsg 	xcp->ip[ip->ip_id].valid = true;
116*f005ef32Sjsg 
117*f005ef32Sjsg 	xcp->valid = true;
118*f005ef32Sjsg }
119*f005ef32Sjsg 
amdgpu_xcp_init(struct amdgpu_xcp_mgr * xcp_mgr,int num_xcps,int mode)120*f005ef32Sjsg int amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps, int mode)
121*f005ef32Sjsg {
122*f005ef32Sjsg 	struct amdgpu_device *adev = xcp_mgr->adev;
123*f005ef32Sjsg 	struct amdgpu_xcp_ip ip;
124*f005ef32Sjsg 	uint8_t mem_id;
125*f005ef32Sjsg 	int i, j, ret;
126*f005ef32Sjsg 
127*f005ef32Sjsg 	if (!num_xcps || num_xcps > MAX_XCP)
128*f005ef32Sjsg 		return -EINVAL;
129*f005ef32Sjsg 
130*f005ef32Sjsg 	xcp_mgr->mode = mode;
131*f005ef32Sjsg 
132*f005ef32Sjsg 	for (i = 0; i < MAX_XCP; ++i)
133*f005ef32Sjsg 		xcp_mgr->xcp[i].valid = false;
134*f005ef32Sjsg 
135*f005ef32Sjsg 	/* This is needed for figuring out memory id of xcp */
136*f005ef32Sjsg 	xcp_mgr->num_xcp_per_mem_partition = num_xcps / xcp_mgr->adev->gmc.num_mem_partitions;
137*f005ef32Sjsg 
138*f005ef32Sjsg 	for (i = 0; i < num_xcps; ++i) {
139*f005ef32Sjsg 		for (j = AMDGPU_XCP_GFXHUB; j < AMDGPU_XCP_MAX_BLOCKS; ++j) {
140*f005ef32Sjsg 			ret = xcp_mgr->funcs->get_ip_details(xcp_mgr, i, j,
141*f005ef32Sjsg 							     &ip);
142*f005ef32Sjsg 			if (ret)
143*f005ef32Sjsg 				continue;
144*f005ef32Sjsg 
145*f005ef32Sjsg 			__amdgpu_xcp_add_block(xcp_mgr, i, &ip);
146*f005ef32Sjsg 		}
147*f005ef32Sjsg 
148*f005ef32Sjsg 		xcp_mgr->xcp[i].id = i;
149*f005ef32Sjsg 
150*f005ef32Sjsg 		if (xcp_mgr->funcs->get_xcp_mem_id) {
151*f005ef32Sjsg 			ret = xcp_mgr->funcs->get_xcp_mem_id(
152*f005ef32Sjsg 				xcp_mgr, &xcp_mgr->xcp[i], &mem_id);
153*f005ef32Sjsg 			if (ret)
154*f005ef32Sjsg 				continue;
155*f005ef32Sjsg 			else
156*f005ef32Sjsg 				xcp_mgr->xcp[i].mem_id = mem_id;
157*f005ef32Sjsg 		}
158*f005ef32Sjsg 	}
159*f005ef32Sjsg 
160*f005ef32Sjsg 	xcp_mgr->num_xcps = num_xcps;
161*f005ef32Sjsg 	amdgpu_xcp_update_partition_sched_list(adev);
162*f005ef32Sjsg 
163*f005ef32Sjsg 	return 0;
164*f005ef32Sjsg }
165*f005ef32Sjsg 
amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr * xcp_mgr,int mode)166*f005ef32Sjsg int amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, int mode)
167*f005ef32Sjsg {
168*f005ef32Sjsg 	int ret, curr_mode, num_xcps = 0;
169*f005ef32Sjsg 
170*f005ef32Sjsg 	if (!xcp_mgr || mode == AMDGPU_XCP_MODE_NONE)
171*f005ef32Sjsg 		return -EINVAL;
172*f005ef32Sjsg 
173*f005ef32Sjsg 	if (xcp_mgr->mode == mode)
174*f005ef32Sjsg 		return 0;
175*f005ef32Sjsg 
176*f005ef32Sjsg 	if (!xcp_mgr->funcs || !xcp_mgr->funcs->switch_partition_mode)
177*f005ef32Sjsg 		return 0;
178*f005ef32Sjsg 
179*f005ef32Sjsg 	mutex_lock(&xcp_mgr->xcp_lock);
180*f005ef32Sjsg 
181*f005ef32Sjsg 	curr_mode = xcp_mgr->mode;
182*f005ef32Sjsg 	/* State set to transient mode */
183*f005ef32Sjsg 	xcp_mgr->mode = AMDGPU_XCP_MODE_TRANS;
184*f005ef32Sjsg 
185*f005ef32Sjsg 	ret = xcp_mgr->funcs->switch_partition_mode(xcp_mgr, mode, &num_xcps);
186*f005ef32Sjsg 
187*f005ef32Sjsg 	if (ret) {
188*f005ef32Sjsg 		/* Failed, get whatever mode it's at now */
189*f005ef32Sjsg 		if (xcp_mgr->funcs->query_partition_mode)
190*f005ef32Sjsg 			xcp_mgr->mode = amdgpu_xcp_query_partition_mode(
191*f005ef32Sjsg 				xcp_mgr, AMDGPU_XCP_FL_LOCKED);
192*f005ef32Sjsg 		else
193*f005ef32Sjsg 			xcp_mgr->mode = curr_mode;
194*f005ef32Sjsg 
195*f005ef32Sjsg 		goto out;
196*f005ef32Sjsg 	}
197*f005ef32Sjsg 
198*f005ef32Sjsg out:
199*f005ef32Sjsg 	mutex_unlock(&xcp_mgr->xcp_lock);
200*f005ef32Sjsg 
201*f005ef32Sjsg 	return ret;
202*f005ef32Sjsg }
203*f005ef32Sjsg 
amdgpu_xcp_query_partition_mode(struct amdgpu_xcp_mgr * xcp_mgr,u32 flags)204*f005ef32Sjsg int amdgpu_xcp_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
205*f005ef32Sjsg {
206*f005ef32Sjsg 	int mode;
207*f005ef32Sjsg 
208*f005ef32Sjsg 	if (xcp_mgr->mode == AMDGPU_XCP_MODE_NONE)
209*f005ef32Sjsg 		return xcp_mgr->mode;
210*f005ef32Sjsg 
211*f005ef32Sjsg 	if (!xcp_mgr->funcs || !xcp_mgr->funcs->query_partition_mode)
212*f005ef32Sjsg 		return xcp_mgr->mode;
213*f005ef32Sjsg 
214*f005ef32Sjsg 	if (!(flags & AMDGPU_XCP_FL_LOCKED))
215*f005ef32Sjsg 		mutex_lock(&xcp_mgr->xcp_lock);
216*f005ef32Sjsg 	mode = xcp_mgr->funcs->query_partition_mode(xcp_mgr);
217*f005ef32Sjsg 	if (xcp_mgr->mode != AMDGPU_XCP_MODE_TRANS && mode != xcp_mgr->mode)
218*f005ef32Sjsg 		dev_WARN(
219*f005ef32Sjsg 			xcp_mgr->adev->dev,
220*f005ef32Sjsg 			"Cached partition mode %d not matching with device mode %d",
221*f005ef32Sjsg 			xcp_mgr->mode, mode);
222*f005ef32Sjsg 
223*f005ef32Sjsg 	if (!(flags & AMDGPU_XCP_FL_LOCKED))
224*f005ef32Sjsg 		mutex_unlock(&xcp_mgr->xcp_lock);
225*f005ef32Sjsg 
226*f005ef32Sjsg 	return mode;
227*f005ef32Sjsg }
228*f005ef32Sjsg 
amdgpu_xcp_dev_alloc(struct amdgpu_device * adev)229*f005ef32Sjsg static int amdgpu_xcp_dev_alloc(struct amdgpu_device *adev)
230*f005ef32Sjsg {
231*f005ef32Sjsg 	struct drm_device *p_ddev;
232*f005ef32Sjsg 	struct drm_device *ddev;
233*f005ef32Sjsg 	int i, ret;
234*f005ef32Sjsg 
235*f005ef32Sjsg 	ddev = adev_to_drm(adev);
236*f005ef32Sjsg 
237*f005ef32Sjsg 	/* xcp #0 shares drm device setting with adev */
238*f005ef32Sjsg 	adev->xcp_mgr->xcp->ddev = ddev;
239*f005ef32Sjsg 
240*f005ef32Sjsg 	for (i = 1; i < MAX_XCP; i++) {
241*f005ef32Sjsg 		ret = amdgpu_xcp_drm_dev_alloc(&p_ddev);
242*f005ef32Sjsg 		if (ret == -ENOSPC) {
243*f005ef32Sjsg 			dev_warn(adev->dev,
244*f005ef32Sjsg 			"Skip xcp node #%d when out of drm node resource.", i);
245*f005ef32Sjsg 			return 0;
246*f005ef32Sjsg 		} else if (ret) {
247*f005ef32Sjsg 			return ret;
248*f005ef32Sjsg 		}
249*f005ef32Sjsg 
250*f005ef32Sjsg 		/* Redirect all IOCTLs to the primary device */
251*f005ef32Sjsg 		adev->xcp_mgr->xcp[i].rdev = p_ddev->render->dev;
252*f005ef32Sjsg 		adev->xcp_mgr->xcp[i].pdev = p_ddev->primary->dev;
253*f005ef32Sjsg 		adev->xcp_mgr->xcp[i].driver = (struct drm_driver *)p_ddev->driver;
254*f005ef32Sjsg 		adev->xcp_mgr->xcp[i].vma_offset_manager = p_ddev->vma_offset_manager;
255*f005ef32Sjsg 		p_ddev->render->dev = ddev;
256*f005ef32Sjsg 		p_ddev->primary->dev = ddev;
257*f005ef32Sjsg 		p_ddev->vma_offset_manager = ddev->vma_offset_manager;
258*f005ef32Sjsg 		p_ddev->driver = &amdgpu_partition_driver;
259*f005ef32Sjsg 		adev->xcp_mgr->xcp[i].ddev = p_ddev;
260*f005ef32Sjsg 	}
261*f005ef32Sjsg 
262*f005ef32Sjsg 	return 0;
263*f005ef32Sjsg }
264*f005ef32Sjsg 
amdgpu_xcp_mgr_init(struct amdgpu_device * adev,int init_mode,int init_num_xcps,struct amdgpu_xcp_mgr_funcs * xcp_funcs)265*f005ef32Sjsg int amdgpu_xcp_mgr_init(struct amdgpu_device *adev, int init_mode,
266*f005ef32Sjsg 			int init_num_xcps,
267*f005ef32Sjsg 			struct amdgpu_xcp_mgr_funcs *xcp_funcs)
268*f005ef32Sjsg {
269*f005ef32Sjsg 	struct amdgpu_xcp_mgr *xcp_mgr;
270*f005ef32Sjsg 
271*f005ef32Sjsg 	if (!xcp_funcs || !xcp_funcs->switch_partition_mode ||
272*f005ef32Sjsg 	    !xcp_funcs->get_ip_details)
273*f005ef32Sjsg 		return -EINVAL;
274*f005ef32Sjsg 
275*f005ef32Sjsg 	xcp_mgr = kzalloc(sizeof(*xcp_mgr), GFP_KERNEL);
276*f005ef32Sjsg 
277*f005ef32Sjsg 	if (!xcp_mgr)
278*f005ef32Sjsg 		return -ENOMEM;
279*f005ef32Sjsg 
280*f005ef32Sjsg 	xcp_mgr->adev = adev;
281*f005ef32Sjsg 	xcp_mgr->funcs = xcp_funcs;
282*f005ef32Sjsg 	xcp_mgr->mode = init_mode;
283*f005ef32Sjsg 	rw_init(&xcp_mgr->xcp_lock, "xcp");
284*f005ef32Sjsg 
285*f005ef32Sjsg 	if (init_mode != AMDGPU_XCP_MODE_NONE)
286*f005ef32Sjsg 		amdgpu_xcp_init(xcp_mgr, init_num_xcps, init_mode);
287*f005ef32Sjsg 
288*f005ef32Sjsg 	adev->xcp_mgr = xcp_mgr;
289*f005ef32Sjsg 
290*f005ef32Sjsg 	return amdgpu_xcp_dev_alloc(adev);
291*f005ef32Sjsg }
292*f005ef32Sjsg 
amdgpu_xcp_get_partition(struct amdgpu_xcp_mgr * xcp_mgr,enum AMDGPU_XCP_IP_BLOCK ip,int instance)293*f005ef32Sjsg int amdgpu_xcp_get_partition(struct amdgpu_xcp_mgr *xcp_mgr,
294*f005ef32Sjsg 			     enum AMDGPU_XCP_IP_BLOCK ip, int instance)
295*f005ef32Sjsg {
296*f005ef32Sjsg 	struct amdgpu_xcp *xcp;
297*f005ef32Sjsg 	int i, id_mask = 0;
298*f005ef32Sjsg 
299*f005ef32Sjsg 	if (ip >= AMDGPU_XCP_MAX_BLOCKS)
300*f005ef32Sjsg 		return -EINVAL;
301*f005ef32Sjsg 
302*f005ef32Sjsg 	for (i = 0; i < xcp_mgr->num_xcps; ++i) {
303*f005ef32Sjsg 		xcp = &xcp_mgr->xcp[i];
304*f005ef32Sjsg 		if ((xcp->valid) && (xcp->ip[ip].valid) &&
305*f005ef32Sjsg 		    (xcp->ip[ip].inst_mask & BIT(instance)))
306*f005ef32Sjsg 			id_mask |= BIT(i);
307*f005ef32Sjsg 	}
308*f005ef32Sjsg 
309*f005ef32Sjsg 	if (!id_mask)
310*f005ef32Sjsg 		id_mask = -ENXIO;
311*f005ef32Sjsg 
312*f005ef32Sjsg 	return id_mask;
313*f005ef32Sjsg }
314*f005ef32Sjsg 
amdgpu_xcp_get_inst_details(struct amdgpu_xcp * xcp,enum AMDGPU_XCP_IP_BLOCK ip,uint32_t * inst_mask)315*f005ef32Sjsg int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp,
316*f005ef32Sjsg 				enum AMDGPU_XCP_IP_BLOCK ip,
317*f005ef32Sjsg 				uint32_t *inst_mask)
318*f005ef32Sjsg {
319*f005ef32Sjsg 	if (!xcp->valid || !inst_mask || !(xcp->ip[ip].valid))
320*f005ef32Sjsg 		return -EINVAL;
321*f005ef32Sjsg 
322*f005ef32Sjsg 	*inst_mask = xcp->ip[ip].inst_mask;
323*f005ef32Sjsg 
324*f005ef32Sjsg 	return 0;
325*f005ef32Sjsg }
326*f005ef32Sjsg 
amdgpu_xcp_dev_register(struct amdgpu_device * adev,const struct pci_device_id * ent)327*f005ef32Sjsg int amdgpu_xcp_dev_register(struct amdgpu_device *adev,
328*f005ef32Sjsg 			const struct pci_device_id *ent)
329*f005ef32Sjsg {
330*f005ef32Sjsg 	int i, ret;
331*f005ef32Sjsg 
332*f005ef32Sjsg 	if (!adev->xcp_mgr)
333*f005ef32Sjsg 		return 0;
334*f005ef32Sjsg 
335*f005ef32Sjsg 	for (i = 1; i < MAX_XCP; i++) {
336*f005ef32Sjsg 		if (!adev->xcp_mgr->xcp[i].ddev)
337*f005ef32Sjsg 			break;
338*f005ef32Sjsg 
339*f005ef32Sjsg 		ret = drm_dev_register(adev->xcp_mgr->xcp[i].ddev, ent->driver_data);
340*f005ef32Sjsg 		if (ret)
341*f005ef32Sjsg 			return ret;
342*f005ef32Sjsg 	}
343*f005ef32Sjsg 
344*f005ef32Sjsg 	return 0;
345*f005ef32Sjsg }
346*f005ef32Sjsg 
amdgpu_xcp_dev_unplug(struct amdgpu_device * adev)347*f005ef32Sjsg void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)
348*f005ef32Sjsg {
349*f005ef32Sjsg 	struct drm_device *p_ddev;
350*f005ef32Sjsg 	int i;
351*f005ef32Sjsg 
352*f005ef32Sjsg 	if (!adev->xcp_mgr)
353*f005ef32Sjsg 		return;
354*f005ef32Sjsg 
355*f005ef32Sjsg 	for (i = 1; i < MAX_XCP; i++) {
356*f005ef32Sjsg 		if (!adev->xcp_mgr->xcp[i].ddev)
357*f005ef32Sjsg 			break;
358*f005ef32Sjsg 
359*f005ef32Sjsg 		p_ddev = adev->xcp_mgr->xcp[i].ddev;
360*f005ef32Sjsg 		drm_dev_unplug(p_ddev);
361*f005ef32Sjsg 		p_ddev->render->dev = adev->xcp_mgr->xcp[i].rdev;
362*f005ef32Sjsg 		p_ddev->primary->dev = adev->xcp_mgr->xcp[i].pdev;
363*f005ef32Sjsg 		p_ddev->driver =  adev->xcp_mgr->xcp[i].driver;
364*f005ef32Sjsg 		p_ddev->vma_offset_manager = adev->xcp_mgr->xcp[i].vma_offset_manager;
365*f005ef32Sjsg 	}
366*f005ef32Sjsg }
367*f005ef32Sjsg 
amdgpu_xcp_open_device(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,struct drm_file * file_priv)368*f005ef32Sjsg int amdgpu_xcp_open_device(struct amdgpu_device *adev,
369*f005ef32Sjsg 			   struct amdgpu_fpriv *fpriv,
370*f005ef32Sjsg 			   struct drm_file *file_priv)
371*f005ef32Sjsg {
372*f005ef32Sjsg 	int i;
373*f005ef32Sjsg 
374*f005ef32Sjsg 	if (!adev->xcp_mgr)
375*f005ef32Sjsg 		return 0;
376*f005ef32Sjsg 
377*f005ef32Sjsg 	fpriv->xcp_id = AMDGPU_XCP_NO_PARTITION;
378*f005ef32Sjsg 	for (i = 0; i < MAX_XCP; ++i) {
379*f005ef32Sjsg 		if (!adev->xcp_mgr->xcp[i].ddev)
380*f005ef32Sjsg 			break;
381*f005ef32Sjsg 
382*f005ef32Sjsg 		if (file_priv->minor == adev->xcp_mgr->xcp[i].ddev->render) {
383*f005ef32Sjsg 			if (adev->xcp_mgr->xcp[i].valid == FALSE) {
384*f005ef32Sjsg 				dev_err(adev->dev, "renderD%d partition %d not valid!",
385*f005ef32Sjsg 						file_priv->minor->index, i);
386*f005ef32Sjsg 				return -ENOENT;
387*f005ef32Sjsg 			}
388*f005ef32Sjsg 			dev_dbg(adev->dev, "renderD%d partition %d opened!",
389*f005ef32Sjsg 					file_priv->minor->index, i);
390*f005ef32Sjsg 			fpriv->xcp_id = i;
391*f005ef32Sjsg 			break;
392*f005ef32Sjsg 		}
393*f005ef32Sjsg 	}
394*f005ef32Sjsg 
395*f005ef32Sjsg 	fpriv->vm.mem_id = fpriv->xcp_id == AMDGPU_XCP_NO_PARTITION ? -1 :
396*f005ef32Sjsg 				adev->xcp_mgr->xcp[fpriv->xcp_id].mem_id;
397*f005ef32Sjsg 	return 0;
398*f005ef32Sjsg }
399*f005ef32Sjsg 
amdgpu_xcp_release_sched(struct amdgpu_device * adev,struct amdgpu_ctx_entity * entity)400*f005ef32Sjsg void amdgpu_xcp_release_sched(struct amdgpu_device *adev,
401*f005ef32Sjsg 				  struct amdgpu_ctx_entity *entity)
402*f005ef32Sjsg {
403*f005ef32Sjsg 	struct drm_gpu_scheduler *sched;
404*f005ef32Sjsg 	struct amdgpu_ring *ring;
405*f005ef32Sjsg 
406*f005ef32Sjsg 	if (!adev->xcp_mgr)
407*f005ef32Sjsg 		return;
408*f005ef32Sjsg 
409*f005ef32Sjsg 	sched = entity->entity.rq->sched;
410*f005ef32Sjsg 	if (sched->ready) {
411*f005ef32Sjsg 		ring = to_amdgpu_ring(entity->entity.rq->sched);
412*f005ef32Sjsg 		atomic_dec(&adev->xcp_mgr->xcp[ring->xcp_id].ref_cnt);
413*f005ef32Sjsg 	}
414*f005ef32Sjsg }
415*f005ef32Sjsg 
416