2 * Copyright 2022 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include "amdgpu_xcp.h"
25 #include "amdgpu_drv.h"
27 #include <drm/drm_drv.h>
29 static int __amdgpu_xcp_run(struct amdgpu_xcp_mgr *xcp_mgr,
30 struct amdgpu_xcp_ip *xcp_ip, int xcp_state)
32 int (*run_func)(void *handle, uint32_t inst_mask);
35 if (!xcp_ip || !xcp_ip->valid || !xcp_ip->ip_funcs)
41 case AMDGPU_XCP_PREPARE_SUSPEND:
42 run_func = xcp_ip->ip_funcs->prepare_suspend;
44 case AMDGPU_XCP_SUSPEND:
45 run_func = xcp_ip->ip_funcs->suspend;
47 case AMDGPU_XCP_PREPARE_RESUME:
48 run_func = xcp_ip->ip_funcs->prepare_resume;
50 case AMDGPU_XCP_RESUME:
51 run_func = xcp_ip->ip_funcs->resume;
56 ret = run_func(xcp_mgr->adev, xcp_ip->inst_mask);
61 static int amdgpu_xcp_run_transition(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
64 struct amdgpu_xcp_ip *xcp_ip;
65 struct amdgpu_xcp *xcp;
68 if (xcp_id > MAX_XCP || !xcp_mgr->xcp[xcp_id].valid)
71 xcp = &xcp_mgr->xcp[xcp_id];
72 for (i = 0; i < AMDGPU_XCP_MAX_BLOCKS; ++i) {
74 ret = __amdgpu_xcp_run(xcp_mgr, xcp_ip, state);
82 int amdgpu_xcp_prepare_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
84 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
85 AMDGPU_XCP_PREPARE_SUSPEND);
88 int amdgpu_xcp_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
90 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_SUSPEND);
93 int amdgpu_xcp_prepare_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
95 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
96 AMDGPU_XCP_PREPARE_RESUME);
99 int amdgpu_xcp_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
101 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_RESUME);
104 static void __amdgpu_xcp_add_block(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
105 struct amdgpu_xcp_ip *ip)
107 struct amdgpu_xcp *xcp;
112 xcp = &xcp_mgr->xcp[xcp_id];
113 xcp->ip[ip->ip_id] = *ip;
114 xcp->ip[ip->ip_id].valid = true;
119 int amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps, int mode)
121 struct amdgpu_device *adev = xcp_mgr->adev;
122 struct amdgpu_xcp_ip ip;
126 if (!num_xcps || num_xcps > MAX_XCP)
129 xcp_mgr->mode = mode;
131 for (i = 0; i < MAX_XCP; ++i)
132 xcp_mgr->xcp[i].valid = false;
134 for (i = 0; i < num_xcps; ++i) {
135 for (j = AMDGPU_XCP_GFXHUB; j < AMDGPU_XCP_MAX_BLOCKS; ++j) {
136 ret = xcp_mgr->funcs->get_ip_details(xcp_mgr, i, j,
141 __amdgpu_xcp_add_block(xcp_mgr, i, &ip);
144 xcp_mgr->xcp[i].id = i;
146 if (xcp_mgr->funcs->get_xcp_mem_id) {
147 ret = xcp_mgr->funcs->get_xcp_mem_id(
148 xcp_mgr, &xcp_mgr->xcp[i], &mem_id);
152 xcp_mgr->xcp[i].mem_id = mem_id;
156 xcp_mgr->num_xcps = num_xcps;
157 amdgpu_xcp_update_partition_sched_list(adev);
159 xcp_mgr->num_xcp_per_mem_partition = num_xcps / xcp_mgr->adev->gmc.num_mem_partitions;
163 int amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, int mode)
165 int ret, curr_mode, num_xcps = 0;
167 if (!xcp_mgr || mode == AMDGPU_XCP_MODE_NONE)
170 if (xcp_mgr->mode == mode)
173 if (!xcp_mgr->funcs || !xcp_mgr->funcs->switch_partition_mode)
176 mutex_lock(&xcp_mgr->xcp_lock);
178 curr_mode = xcp_mgr->mode;
179 /* State set to transient mode */
180 xcp_mgr->mode = AMDGPU_XCP_MODE_TRANS;
182 ret = xcp_mgr->funcs->switch_partition_mode(xcp_mgr, mode, &num_xcps);
185 /* Failed, get whatever mode it's at now */
186 if (xcp_mgr->funcs->query_partition_mode)
187 xcp_mgr->mode = amdgpu_xcp_query_partition_mode(
188 xcp_mgr, AMDGPU_XCP_FL_LOCKED);
190 xcp_mgr->mode = curr_mode;
196 mutex_unlock(&xcp_mgr->xcp_lock);
201 int amdgpu_xcp_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
205 if (xcp_mgr->mode == AMDGPU_XCP_MODE_NONE)
206 return xcp_mgr->mode;
208 if (!xcp_mgr->funcs || !xcp_mgr->funcs->query_partition_mode)
209 return xcp_mgr->mode;
211 if (!(flags & AMDGPU_XCP_FL_LOCKED))
212 mutex_lock(&xcp_mgr->xcp_lock);
213 mode = xcp_mgr->funcs->query_partition_mode(xcp_mgr);
214 if (xcp_mgr->mode != AMDGPU_XCP_MODE_TRANS && mode != xcp_mgr->mode)
217 "Cached partition mode %d not matching with device mode %d",
218 xcp_mgr->mode, mode);
220 if (!(flags & AMDGPU_XCP_FL_LOCKED))
221 mutex_unlock(&xcp_mgr->xcp_lock);
226 static int amdgpu_xcp_dev_alloc(struct amdgpu_device *adev)
228 struct drm_device *p_ddev;
229 struct pci_dev *pdev;
230 struct drm_device *ddev;
234 ddev = adev_to_drm(adev);
236 for (i = 0; i < MAX_XCP; i++) {
237 p_ddev = drm_dev_alloc(&amdgpu_partition_driver,
238 &pci_upstream_bridge(pdev)->dev);
240 return PTR_ERR(p_ddev);
242 /* Redirect all IOCTLs to the primary device */
243 p_ddev->render->dev = ddev;
244 p_ddev->vma_offset_manager = ddev->vma_offset_manager;
245 adev->xcp_mgr->xcp[i].ddev = p_ddev;
251 int amdgpu_xcp_mgr_init(struct amdgpu_device *adev, int init_mode,
253 struct amdgpu_xcp_mgr_funcs *xcp_funcs)
255 struct amdgpu_xcp_mgr *xcp_mgr;
257 if (!xcp_funcs || !xcp_funcs->switch_partition_mode ||
258 !xcp_funcs->get_ip_details)
261 xcp_mgr = kzalloc(sizeof(*xcp_mgr), GFP_KERNEL);
266 xcp_mgr->adev = adev;
267 xcp_mgr->funcs = xcp_funcs;
268 xcp_mgr->mode = init_mode;
269 mutex_init(&xcp_mgr->xcp_lock);
271 if (init_mode != AMDGPU_XCP_MODE_NONE)
272 amdgpu_xcp_init(xcp_mgr, init_num_xcps, init_mode);
274 adev->xcp_mgr = xcp_mgr;
276 return amdgpu_xcp_dev_alloc(adev);
279 int amdgpu_xcp_get_partition(struct amdgpu_xcp_mgr *xcp_mgr,
280 enum AMDGPU_XCP_IP_BLOCK ip, int instance)
282 struct amdgpu_xcp *xcp;
285 if (ip >= AMDGPU_XCP_MAX_BLOCKS)
288 for (i = 0; i < xcp_mgr->num_xcps; ++i) {
289 xcp = &xcp_mgr->xcp[i];
290 if ((xcp->valid) && (xcp->ip[ip].valid) &&
291 (xcp->ip[ip].inst_mask & BIT(instance)))
301 int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp,
302 enum AMDGPU_XCP_IP_BLOCK ip,
305 if (!xcp->valid || !inst_mask || !(xcp->ip[ip].valid))
308 *inst_mask = xcp->ip[ip].inst_mask;
313 int amdgpu_xcp_dev_register(struct amdgpu_device *adev,
314 const struct pci_device_id *ent)
321 for (i = 0; i < MAX_XCP; i++) {
322 ret = drm_dev_register(adev->xcp_mgr->xcp[i].ddev, ent->driver_data);
330 void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)
337 for (i = 0; i < MAX_XCP; i++)
338 drm_dev_unplug(adev->xcp_mgr->xcp[i].ddev);
341 int amdgpu_xcp_open_device(struct amdgpu_device *adev,
342 struct amdgpu_fpriv *fpriv,
343 struct drm_file *file_priv)
351 for (i = 0; i < MAX_XCP; ++i) {
352 if (!adev->xcp_mgr->xcp[i].ddev)
355 if (file_priv->minor == adev->xcp_mgr->xcp[i].ddev->render) {
356 if (adev->xcp_mgr->xcp[i].valid == FALSE) {
357 dev_err(adev->dev, "renderD%d partition %d not valid!",
358 file_priv->minor->index, i);
361 dev_dbg(adev->dev, "renderD%d partition %d openned!",
362 file_priv->minor->index, i);
368 fpriv->vm.mem_id = fpriv->xcp_id == ~0 ? -1 :
369 adev->xcp_mgr->xcp[fpriv->xcp_id].mem_id;
373 void amdgpu_xcp_release_sched(struct amdgpu_device *adev,
374 struct amdgpu_ctx_entity *entity)
376 struct drm_gpu_scheduler *sched;
377 struct amdgpu_ring *ring;
382 sched = entity->entity.rq->sched;
384 ring = to_amdgpu_ring(entity->entity.rq->sched);
385 atomic_dec(&adev->xcp_mgr->xcp[ring->xcp_id].ref_cnt);