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"
26 static int __amdgpu_xcp_run(struct amdgpu_xcp_mgr *xcp_mgr,
27 struct amdgpu_xcp_ip *xcp_ip, int xcp_state)
29 int (*run_func)(void *handle, uint32_t inst_mask);
32 if (!xcp_ip || !xcp_ip->valid || !xcp_ip->ip_funcs)
38 case AMDGPU_XCP_PREPARE_SUSPEND:
39 run_func = xcp_ip->ip_funcs->prepare_suspend;
41 case AMDGPU_XCP_SUSPEND:
42 run_func = xcp_ip->ip_funcs->suspend;
44 case AMDGPU_XCP_PREPARE_RESUME:
45 run_func = xcp_ip->ip_funcs->prepare_resume;
47 case AMDGPU_XCP_RESUME:
48 run_func = xcp_ip->ip_funcs->resume;
53 ret = run_func(xcp_mgr->adev, xcp_ip->inst_mask);
58 static int amdgpu_xcp_run_transition(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
61 struct amdgpu_xcp_ip *xcp_ip;
62 struct amdgpu_xcp *xcp;
65 if (xcp_id > MAX_XCP || !xcp_mgr->xcp[xcp_id].valid)
68 xcp = &xcp_mgr->xcp[xcp_id];
69 for (i = 0; i < AMDGPU_XCP_MAX_BLOCKS; ++i) {
71 ret = __amdgpu_xcp_run(xcp_mgr, xcp_ip, state);
79 int amdgpu_xcp_prepare_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
81 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
82 AMDGPU_XCP_PREPARE_SUSPEND);
85 int amdgpu_xcp_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
87 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_SUSPEND);
90 int amdgpu_xcp_prepare_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
92 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
93 AMDGPU_XCP_PREPARE_RESUME);
96 int amdgpu_xcp_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
98 return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_RESUME);
101 static void __amdgpu_xcp_add_block(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
102 struct amdgpu_xcp_ip *ip)
104 struct amdgpu_xcp *xcp;
109 xcp = &xcp_mgr->xcp[xcp_id];
110 xcp->ip[ip->ip_id] = *ip;
111 xcp->ip[ip->ip_id].valid = true;
116 static int __amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps)
118 struct amdgpu_xcp_ip ip;
121 for (i = 0; i < MAX_XCP; ++i)
122 xcp_mgr->xcp[i].valid = false;
124 for (i = 0; i < num_xcps; ++i) {
125 for (j = AMDGPU_XCP_GFXHUB; j < AMDGPU_XCP_MAX_BLOCKS; ++j) {
126 ret = xcp_mgr->funcs->get_ip_details(xcp_mgr, i, j,
131 __amdgpu_xcp_add_block(xcp_mgr, i, &ip);
135 xcp_mgr->num_xcps = num_xcps;
140 int amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, int mode)
142 int ret, curr_mode, num_xcps = 0;
144 if (!xcp_mgr || mode == AMDGPU_XCP_MODE_NONE)
147 if (xcp_mgr->mode == mode)
150 if (!xcp_mgr->funcs || !xcp_mgr->funcs->switch_partition_mode)
153 mutex_lock(&xcp_mgr->xcp_lock);
155 curr_mode = xcp_mgr->mode;
156 /* State set to transient mode */
157 xcp_mgr->mode = AMDGPU_XCP_MODE_TRANS;
159 ret = xcp_mgr->funcs->switch_partition_mode(xcp_mgr, mode, &num_xcps);
162 /* Failed, get whatever mode it's at now */
163 if (xcp_mgr->funcs->query_partition_mode)
164 xcp_mgr->mode = amdgpu_xcp_query_partition_mode(
165 xcp_mgr, AMDGPU_XCP_FL_LOCKED);
167 xcp_mgr->mode = curr_mode;
172 if (!num_xcps || num_xcps > MAX_XCP) {
177 xcp_mgr->mode = mode;
178 __amdgpu_xcp_init(xcp_mgr, num_xcps);
180 mutex_unlock(&xcp_mgr->xcp_lock);
185 int amdgpu_xcp_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
189 if (xcp_mgr->mode == AMDGPU_XCP_MODE_NONE)
190 return xcp_mgr->mode;
192 if (!xcp_mgr->funcs || !xcp_mgr->funcs->query_partition_mode)
193 return xcp_mgr->mode;
195 if (!(flags & AMDGPU_XCP_FL_LOCKED))
196 mutex_lock(&xcp_mgr->xcp_lock);
197 mode = xcp_mgr->funcs->query_partition_mode(xcp_mgr);
198 if (xcp_mgr->mode != AMDGPU_XCP_MODE_TRANS && mode != xcp_mgr->mode)
201 "Cached partition mode %d not matching with device mode %d",
202 xcp_mgr->mode, mode);
204 if (!(flags & AMDGPU_XCP_FL_LOCKED))
205 mutex_unlock(&xcp_mgr->xcp_lock);
210 int amdgpu_xcp_mgr_init(struct amdgpu_device *adev, int init_mode,
212 struct amdgpu_xcp_mgr_funcs *xcp_funcs)
214 struct amdgpu_xcp_mgr *xcp_mgr;
216 if (!xcp_funcs || !xcp_funcs->switch_partition_mode ||
217 !xcp_funcs->get_ip_details)
220 xcp_mgr = kzalloc(sizeof(*xcp_mgr), GFP_KERNEL);
225 xcp_mgr->adev = adev;
226 xcp_mgr->funcs = xcp_funcs;
227 xcp_mgr->mode = init_mode;
228 mutex_init(&xcp_mgr->xcp_lock);
230 if (init_mode != AMDGPU_XCP_MODE_NONE)
231 __amdgpu_xcp_init(xcp_mgr, init_num_xcps);
233 adev->xcp_mgr = xcp_mgr;
238 int amdgpu_xcp_get_partition(struct amdgpu_xcp_mgr *xcp_mgr,
239 enum AMDGPU_XCP_IP_BLOCK ip, int instance)
241 struct amdgpu_xcp *xcp;
244 if (ip >= AMDGPU_XCP_MAX_BLOCKS)
247 for (i = 0; i < xcp_mgr->num_xcps; ++i) {
248 xcp = &xcp_mgr->xcp[i];
249 if ((xcp->valid) && (xcp->ip[ip].valid) &&
250 (xcp->ip[ip].inst_mask & BIT(instance)))
260 int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp,
261 enum AMDGPU_XCP_IP_BLOCK ip,
264 if (!xcp->valid || !inst_mask || !(xcp->ip[ip].valid))
267 *inst_mask = xcp->ip[ip].inst_mask;