xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 /*
2  * Copyright 2025 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  */
23 
24 #include "amdgpu.h"
25 #include "amdgpu_ip.h"
26 
27 u32 amdgpu_ip_map_logical_to_dev_mask(struct amdgpu_ip_map_info *ip_map,
28 				      enum amd_hw_ip_block_type block,
29 				      u32 mask)
30 {
31 	unsigned int log_inst, dev_inst;
32 	u32 dev_mask = 0;
33 
34 	while (mask) {
35 		log_inst = ffs(mask) - 1;
36 		dev_inst = ip_map->dev_inst[block][log_inst];
37 		dev_mask |= (1 << dev_inst);
38 		mask &= ~(1 << log_inst);
39 	}
40 
41 	return dev_mask;
42 }
43 
44 void amdgpu_ip_map_init(struct amdgpu_device *adev)
45 {
46 	int i, j;
47 
48 	/* Assume 'logical instance == physical instance' unless overridden. */
49 	for (i = 0; i < MAX_HWIP; i++) {
50 		for (j = 0; j < HWIP_MAX_INSTANCE; j++)
51 			adev->ip_map.dev_inst[i][j] = j;
52 	}
53 }
54 
55 int amdgpu_ip_block_suspend(struct amdgpu_ip_block *ip_block)
56 {
57 	int r;
58 
59 	if (ip_block->version->funcs->suspend) {
60 		r = ip_block->version->funcs->suspend(ip_block);
61 		if (r) {
62 			dev_err(ip_block->adev->dev,
63 				"suspend of IP block <%s> failed %d\n",
64 				ip_block->version->funcs->name, r);
65 			return r;
66 		}
67 	}
68 
69 	ip_block->status.hw = false;
70 	return 0;
71 }
72 
73 int amdgpu_ip_block_resume(struct amdgpu_ip_block *ip_block)
74 {
75 	int r;
76 
77 	if (ip_block->version->funcs->resume) {
78 		r = ip_block->version->funcs->resume(ip_block);
79 		if (r) {
80 			dev_err(ip_block->adev->dev,
81 				"resume of IP block <%s> failed %d\n",
82 				ip_block->version->funcs->name, r);
83 			return r;
84 		}
85 	}
86 
87 	ip_block->status.hw = true;
88 	return 0;
89 }
90 
91 /**
92  * amdgpu_device_ip_get_ip_block - get a hw IP pointer
93  *
94  * @adev: amdgpu_device pointer
95  * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
96  *
97  * Returns a pointer to the hardware IP block structure
98  * if it exists for the asic, otherwise NULL.
99  */
100 struct amdgpu_ip_block *
101 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
102 			      enum amd_ip_block_type type)
103 {
104 	int i;
105 
106 	for (i = 0; i < adev->num_ip_blocks; i++)
107 		if (adev->ip_blocks[i].version->type == type)
108 			return &adev->ip_blocks[i];
109 
110 	return NULL;
111 }
112 
113 /**
114  * amdgpu_device_ip_block_version_cmp
115  *
116  * @adev: amdgpu_device pointer
117  * @type: enum amd_ip_block_type
118  * @major: major version
119  * @minor: minor version
120  *
121  * return 0 if equal or greater
122  * return 1 if smaller or the ip_block doesn't exist
123  */
124 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
125 				       enum amd_ip_block_type type, u32 major,
126 				       u32 minor)
127 {
128 	struct amdgpu_ip_block *ip_block =
129 		amdgpu_device_ip_get_ip_block(adev, type);
130 
131 	if (ip_block && ((ip_block->version->major > major) ||
132 			 ((ip_block->version->major == major) &&
133 			  (ip_block->version->minor >= minor))))
134 		return 0;
135 
136 	return 1;
137 }
138 
139 static const char *const ip_block_names[] = {
140 	[AMD_IP_BLOCK_TYPE_COMMON] = "common",
141 	[AMD_IP_BLOCK_TYPE_GMC] = "gmc",
142 	[AMD_IP_BLOCK_TYPE_IH] = "ih",
143 	[AMD_IP_BLOCK_TYPE_SMC] = "smu",
144 	[AMD_IP_BLOCK_TYPE_PSP] = "psp",
145 	[AMD_IP_BLOCK_TYPE_DCE] = "dce",
146 	[AMD_IP_BLOCK_TYPE_GFX] = "gfx",
147 	[AMD_IP_BLOCK_TYPE_SDMA] = "sdma",
148 	[AMD_IP_BLOCK_TYPE_UVD] = "uvd",
149 	[AMD_IP_BLOCK_TYPE_VCE] = "vce",
150 	[AMD_IP_BLOCK_TYPE_ACP] = "acp",
151 	[AMD_IP_BLOCK_TYPE_VCN] = "vcn",
152 	[AMD_IP_BLOCK_TYPE_MES] = "mes",
153 	[AMD_IP_BLOCK_TYPE_JPEG] = "jpeg",
154 	[AMD_IP_BLOCK_TYPE_VPE] = "vpe",
155 	[AMD_IP_BLOCK_TYPE_UMSCH_MM] = "umsch_mm",
156 	[AMD_IP_BLOCK_TYPE_ISP] = "isp",
157 	[AMD_IP_BLOCK_TYPE_RAS] = "ras",
158 	[AMD_IP_BLOCK_TYPE_UALINK] = "ualink",
159 };
160 
161 static const char *ip_block_name(struct amdgpu_device *adev,
162 				 enum amd_ip_block_type type)
163 {
164 	int idx = (int)type;
165 
166 	return idx < ARRAY_SIZE(ip_block_names) ? ip_block_names[idx] :
167 						  "unknown";
168 }
169 
170 /**
171  * amdgpu_device_ip_block_add
172  *
173  * @adev: amdgpu_device pointer
174  * @ip_block_version: pointer to the IP to add
175  *
176  * Adds the IP block driver information to the collection of IPs
177  * on the asic.
178  */
179 int amdgpu_device_ip_block_add(
180 	struct amdgpu_device *adev,
181 	const struct amdgpu_ip_block_version *ip_block_version)
182 {
183 	if (!ip_block_version)
184 		return -EINVAL;
185 
186 	switch (ip_block_version->type) {
187 	case AMD_IP_BLOCK_TYPE_VCN:
188 		if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
189 			return 0;
190 		break;
191 	case AMD_IP_BLOCK_TYPE_JPEG:
192 		if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
193 			return 0;
194 		break;
195 	default:
196 		break;
197 	}
198 
199 	dev_info(adev->dev, "detected ip block number %d <%s_v%d_%d_%d> (%s)\n",
200 		 adev->num_ip_blocks,
201 		 ip_block_name(adev, ip_block_version->type),
202 		 ip_block_version->major, ip_block_version->minor,
203 		 ip_block_version->rev, ip_block_version->funcs->name);
204 
205 	adev->ip_blocks[adev->num_ip_blocks].adev = adev;
206 
207 	adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
208 
209 	return 0;
210 }
211 
212 /**
213  * amdgpu_device_ip_set_clockgating_state - set the CG state
214  *
215  * @adev: amdgpu_device pointer
216  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
217  * @state: clockgating state (gate or ungate)
218  *
219  * Sets the requested clockgating state for all instances of
220  * the hardware IP specified.
221  * Returns the error code from the last instance.
222  */
223 int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
224 					   enum amd_ip_block_type block_type,
225 					   enum amd_clockgating_state state)
226 {
227 	int i, r = 0;
228 
229 	for (i = 0; i < adev->num_ip_blocks; i++) {
230 		if (!adev->ip_blocks[i].status.valid)
231 			continue;
232 		if (adev->ip_blocks[i].version->type != block_type)
233 			continue;
234 		if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
235 			continue;
236 		r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
237 			&adev->ip_blocks[i], state);
238 		if (r)
239 			dev_err(adev->dev,
240 				"set_clockgating_state of IP block <%s> failed %d\n",
241 				adev->ip_blocks[i].version->funcs->name, r);
242 	}
243 	return r;
244 }
245 
246 /**
247  * amdgpu_device_ip_set_powergating_state - set the PG state
248  *
249  * @adev: amdgpu_device pointer
250  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
251  * @state: powergating state (gate or ungate)
252  *
253  * Sets the requested powergating state for all instances of
254  * the hardware IP specified.
255  * Returns the error code from the last instance.
256  */
257 int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
258 					   enum amd_ip_block_type block_type,
259 					   enum amd_powergating_state state)
260 {
261 	int i, r = 0;
262 
263 	for (i = 0; i < adev->num_ip_blocks; i++) {
264 		if (!adev->ip_blocks[i].status.valid)
265 			continue;
266 		if (adev->ip_blocks[i].version->type != block_type)
267 			continue;
268 		if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
269 			continue;
270 		r = adev->ip_blocks[i].version->funcs->set_powergating_state(
271 			&adev->ip_blocks[i], state);
272 		if (r)
273 			dev_err(adev->dev,
274 				"set_powergating_state of IP block <%s> failed %d\n",
275 				adev->ip_blocks[i].version->funcs->name, r);
276 	}
277 	return r;
278 }
279 
280 /**
281  * amdgpu_device_ip_get_clockgating_state - get the CG state
282  *
283  * @adev: amdgpu_device pointer
284  * @flags: clockgating feature flags
285  *
286  * Walks the list of IPs on the device and updates the clockgating
287  * flags for each IP.
288  * Updates @flags with the feature flags for each hardware IP where
289  * clockgating is enabled.
290  */
291 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
292 					    u64 *flags)
293 {
294 	int i;
295 
296 	for (i = 0; i < adev->num_ip_blocks; i++) {
297 		if (!adev->ip_blocks[i].status.valid)
298 			continue;
299 		if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
300 			adev->ip_blocks[i].version->funcs->get_clockgating_state(
301 				&adev->ip_blocks[i], flags);
302 	}
303 }
304 
305 /**
306  * amdgpu_device_ip_wait_for_idle - wait for idle
307  *
308  * @adev: amdgpu_device pointer
309  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
310  *
311  * Waits for the request hardware IP to be idle.
312  * Returns 0 for success or a negative error code on failure.
313  */
314 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
315 				   enum amd_ip_block_type block_type)
316 {
317 	struct amdgpu_ip_block *ip_block;
318 
319 	ip_block = amdgpu_device_ip_get_ip_block(adev, block_type);
320 	if (!ip_block || !ip_block->status.valid)
321 		return 0;
322 
323 	if (ip_block->version->funcs->wait_for_idle)
324 		return ip_block->version->funcs->wait_for_idle(ip_block);
325 
326 	return 0;
327 }
328 
329 /**
330  * amdgpu_device_ip_is_valid - is the hardware IP valid
331  *
332  * @adev: amdgpu_device pointer
333  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
334  *
335  * Check if the hardware IP is valid or not.
336  * Returns true if it the IP is valid, false if not.
337  */
338 bool amdgpu_device_ip_is_valid(struct amdgpu_device *adev,
339 			       enum amd_ip_block_type block_type)
340 {
341 	struct amdgpu_ip_block *ip_block;
342 
343 	ip_block = amdgpu_device_ip_get_ip_block(adev, block_type);
344 	if (ip_block)
345 		return ip_block->status.valid;
346 
347 	return false;
348 }
349 
350 /**
351  * amdgpu_ip_from_ring() - Find IP block type corresponding to ring type.
352  *
353  * @ring_type: The ring type whose IP block you are looking for.
354  */
355 static enum amd_ip_block_type amdgpu_ip_from_ring(const enum amdgpu_ring_type ring_type)
356 {
357 	switch (ring_type) {
358 	case AMDGPU_RING_TYPE_GFX:
359 	case AMDGPU_RING_TYPE_COMPUTE:
360 		return AMD_IP_BLOCK_TYPE_GFX;
361 
362 	case AMDGPU_RING_TYPE_SDMA:
363 		return AMD_IP_BLOCK_TYPE_SDMA;
364 
365 	case AMDGPU_RING_TYPE_UVD:
366 	case AMDGPU_RING_TYPE_UVD_ENC:
367 		return AMD_IP_BLOCK_TYPE_UVD;
368 
369 	case AMDGPU_RING_TYPE_VCE:
370 		return AMD_IP_BLOCK_TYPE_VCE;
371 
372 	case AMDGPU_RING_TYPE_VCN_DEC:
373 	case AMDGPU_RING_TYPE_VCN_ENC:
374 		return AMD_IP_BLOCK_TYPE_VCN;
375 
376 	case AMDGPU_RING_TYPE_VCN_JPEG:
377 		return AMD_IP_BLOCK_TYPE_JPEG;
378 
379 	case AMDGPU_RING_TYPE_VPE:
380 		return AMD_IP_BLOCK_TYPE_VPE;
381 
382 	default:
383 		return AMD_IP_BLOCK_TYPE_NUM;
384 	}
385 }
386 
387 /**
388  * amdgpu_ring_mask_from_ip() - Find mask of ring types corresponding to an IP block type.
389  *
390  * @ip_type: The IP block type whose rings you are looking for.
391  */
392 static u32 amdgpu_ring_mask_from_ip(const enum amd_ip_block_type ip_type)
393 {
394 	switch (ip_type) {
395 	case AMD_IP_BLOCK_TYPE_GFX:
396 		return BIT(AMDGPU_RING_TYPE_GFX) | BIT(AMDGPU_RING_TYPE_COMPUTE);
397 
398 	case AMD_IP_BLOCK_TYPE_SDMA:
399 		return BIT(AMDGPU_RING_TYPE_SDMA);
400 
401 	case AMD_IP_BLOCK_TYPE_UVD:
402 		return BIT(AMDGPU_RING_TYPE_UVD) | BIT(AMDGPU_RING_TYPE_UVD_ENC);
403 
404 	case AMD_IP_BLOCK_TYPE_VCE:
405 		return BIT(AMD_IP_BLOCK_TYPE_VCE);
406 
407 	case AMD_IP_BLOCK_TYPE_VCN:
408 		return BIT(AMDGPU_RING_TYPE_VCN_DEC) | BIT(AMDGPU_RING_TYPE_VCN_ENC);
409 
410 	case AMD_IP_BLOCK_TYPE_JPEG:
411 		return BIT(AMDGPU_RING_TYPE_VCN_JPEG);
412 
413 	case AMD_IP_BLOCK_TYPE_VPE:
414 		return BIT(AMDGPU_RING_TYPE_VPE);
415 
416 	default:
417 		return 0;
418 	}
419 }
420 
421 /**
422  * amdgpu_device_ip_soft_reset() - Perform a graceful soft reset on an IP block.
423  *
424  * @guilty_ring: The ring which is guilty of causing a reset.
425  * @guilty_fence: The fence which didn't signal.
426  *
427  * IP block soft reset is used when attempting to recover
428  * from a GPU hang in a situation where a more fine grained
429  * reset type isn't available or didn't work. This effectively
430  * resets all rings that belong to the same device IP block
431  * and re-initializes the device IP block.
432  *
433  * The reset is handled gracefully, meaning that we try to
434  * minimize collateral damage (ie. avoid rejecting non-guilty jobs)
435  * as well as back up and restore the contents of all rings
436  * so that the system can move on from the hang.
437  */
438 int amdgpu_device_ip_soft_reset(struct amdgpu_ring *guilty_ring,
439 				struct amdgpu_fence *guilty_fence)
440 {
441 	struct amdgpu_device *adev = guilty_ring->adev;
442 	struct amdgpu_ip_block *ip_block;
443 	enum amd_ip_block_type ip_type;
444 	u32 ring_type_mask;
445 	int r;
446 
447 	ip_type = amdgpu_ip_from_ring(guilty_ring->funcs->type);
448 	ip_block = amdgpu_device_ip_get_ip_block(adev, ip_type);
449 
450 	if (unlikely(!ip_block)) {
451 		dev_warn(adev->dev, "IP block not found for ring %s\n",
452 			 guilty_ring->name);
453 		return -EOPNOTSUPP;
454 	}
455 
456 	if (!ip_block->version->funcs->soft_reset) {
457 		dev_warn(adev->dev, "IP block soft reset not supported on %s\n",
458 			 ip_block->version->funcs->name);
459 		return -EOPNOTSUPP;
460 	}
461 
462 	dev_err(adev->dev, "Starting %s IP block soft reset\n",
463 		ip_block->version->funcs->name);
464 
465 	ring_type_mask = amdgpu_ring_mask_from_ip(ip_type);
466 
467 	amdgpu_device_lock_reset_domain(adev->reset_domain);
468 	amdgpu_multi_ring_reset_helper_begin(ring_type_mask, guilty_ring, guilty_fence);
469 
470 	r = ip_block->version->funcs->soft_reset(ip_block);
471 
472 	r = amdgpu_multi_ring_reset_helper_end(ring_type_mask, guilty_ring, r);
473 	amdgpu_device_unlock_reset_domain(adev->reset_domain);
474 
475 	if (r) {
476 		dev_err(adev->dev, "Failed %s IP block soft reset: %d\n",
477 			ip_block->version->funcs->name, r);
478 		return r;
479 	}
480 
481 	dev_err(adev->dev, "Successful %s IP block soft reset\n",
482 		ip_block->version->funcs->name);
483 	return 0;
484 }
485