xref: /linux/drivers/gpu/drm/xe/xe_guc_hwconfig.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1dd08ebf6SMatthew Brost // SPDX-License-Identifier: MIT
2dd08ebf6SMatthew Brost /*
3dd08ebf6SMatthew Brost  * Copyright © 2022 Intel Corporation
4dd08ebf6SMatthew Brost  */
5dd08ebf6SMatthew Brost 
6ea9f879dSLucas De Marchi #include "xe_guc_hwconfig.h"
7ea9f879dSLucas De Marchi 
8dd08ebf6SMatthew Brost #include <drm/drm_managed.h>
98a0f58ecSMatt Roper #include <drm/drm_print.h>
10dd08ebf6SMatthew Brost 
11b67cb798SMichal Wajdeczko #include "abi/guc_actions_abi.h"
12dd08ebf6SMatthew Brost #include "xe_bo.h"
13dd08ebf6SMatthew Brost #include "xe_device.h"
14dd08ebf6SMatthew Brost #include "xe_gt.h"
15dd08ebf6SMatthew Brost #include "xe_guc.h"
16dd08ebf6SMatthew Brost #include "xe_map.h"
17dd08ebf6SMatthew Brost 
send_get_hwconfig(struct xe_guc * guc,u64 ggtt_addr,u32 size)184c15a6dcSDaniele Ceraolo Spurio static int send_get_hwconfig(struct xe_guc *guc, u64 ggtt_addr, u32 size)
19dd08ebf6SMatthew Brost {
20dd08ebf6SMatthew Brost 	u32 action[] = {
21dd08ebf6SMatthew Brost 		XE_GUC_ACTION_GET_HWCONFIG,
22dd08ebf6SMatthew Brost 		lower_32_bits(ggtt_addr),
23dd08ebf6SMatthew Brost 		upper_32_bits(ggtt_addr),
24dd08ebf6SMatthew Brost 		size,
25dd08ebf6SMatthew Brost 	};
26dd08ebf6SMatthew Brost 
27f900725aSMatthew Brost 	return xe_guc_mmio_send(guc, action, ARRAY_SIZE(action));
28dd08ebf6SMatthew Brost }
29dd08ebf6SMatthew Brost 
guc_hwconfig_size(struct xe_guc * guc,u32 * size)30dd08ebf6SMatthew Brost static int guc_hwconfig_size(struct xe_guc *guc, u32 *size)
31dd08ebf6SMatthew Brost {
32dd08ebf6SMatthew Brost 	int ret = send_get_hwconfig(guc, 0, 0);
33dd08ebf6SMatthew Brost 
34dd08ebf6SMatthew Brost 	if (ret < 0)
35dd08ebf6SMatthew Brost 		return ret;
36dd08ebf6SMatthew Brost 
37dd08ebf6SMatthew Brost 	*size = ret;
38dd08ebf6SMatthew Brost 	return 0;
39dd08ebf6SMatthew Brost }
40dd08ebf6SMatthew Brost 
guc_hwconfig_copy(struct xe_guc * guc)41dd08ebf6SMatthew Brost static int guc_hwconfig_copy(struct xe_guc *guc)
42dd08ebf6SMatthew Brost {
43dd08ebf6SMatthew Brost 	int ret = send_get_hwconfig(guc, xe_bo_ggtt_addr(guc->hwconfig.bo),
44dd08ebf6SMatthew Brost 				    guc->hwconfig.size);
45dd08ebf6SMatthew Brost 
46dd08ebf6SMatthew Brost 	if (ret < 0)
47dd08ebf6SMatthew Brost 		return ret;
48dd08ebf6SMatthew Brost 
49dd08ebf6SMatthew Brost 	return 0;
50dd08ebf6SMatthew Brost }
51dd08ebf6SMatthew Brost 
xe_guc_hwconfig_init(struct xe_guc * guc)52dd08ebf6SMatthew Brost int xe_guc_hwconfig_init(struct xe_guc *guc)
53dd08ebf6SMatthew Brost {
54dd08ebf6SMatthew Brost 	struct xe_device *xe = guc_to_xe(guc);
55dd08ebf6SMatthew Brost 	struct xe_gt *gt = guc_to_gt(guc);
56876611c2SMatt Roper 	struct xe_tile *tile = gt_to_tile(gt);
57dd08ebf6SMatthew Brost 	struct xe_bo *bo;
58dd08ebf6SMatthew Brost 	u32 size;
59dd08ebf6SMatthew Brost 	int err;
60dd08ebf6SMatthew Brost 
61dd08ebf6SMatthew Brost 	/* Initialization already done */
62dd08ebf6SMatthew Brost 	if (guc->hwconfig.bo)
63dd08ebf6SMatthew Brost 		return 0;
64dd08ebf6SMatthew Brost 
65dd08ebf6SMatthew Brost 	/*
66dd08ebf6SMatthew Brost 	 * All hwconfig the same across GTs so only GT0 needs to be configured
67dd08ebf6SMatthew Brost 	 */
68dd08ebf6SMatthew Brost 	if (gt->info.id != XE_GT0)
69dd08ebf6SMatthew Brost 		return 0;
70dd08ebf6SMatthew Brost 
71dd08ebf6SMatthew Brost 	/* ADL_P, DG2+ supports hwconfig table */
72dd08ebf6SMatthew Brost 	if (GRAPHICS_VERx100(xe) < 1255 && xe->info.platform != XE_ALDERLAKE_P)
73dd08ebf6SMatthew Brost 		return 0;
74dd08ebf6SMatthew Brost 
75dd08ebf6SMatthew Brost 	err = guc_hwconfig_size(guc, &size);
76dd08ebf6SMatthew Brost 	if (err)
77dd08ebf6SMatthew Brost 		return err;
78dd08ebf6SMatthew Brost 	if (!size)
79dd08ebf6SMatthew Brost 		return -EINVAL;
80dd08ebf6SMatthew Brost 
810e1a47fcSMichał Winiarski 	bo = xe_managed_bo_create_pin_map(xe, tile, PAGE_ALIGN(size),
8262742d12SLucas De Marchi 					  XE_BO_FLAG_SYSTEM |
8362742d12SLucas De Marchi 					  XE_BO_FLAG_GGTT |
8462742d12SLucas De Marchi 					  XE_BO_FLAG_GGTT_INVALIDATE);
85dd08ebf6SMatthew Brost 	if (IS_ERR(bo))
86dd08ebf6SMatthew Brost 		return PTR_ERR(bo);
87dd08ebf6SMatthew Brost 	guc->hwconfig.bo = bo;
88dd08ebf6SMatthew Brost 	guc->hwconfig.size = size;
89dd08ebf6SMatthew Brost 
90dd08ebf6SMatthew Brost 	return guc_hwconfig_copy(guc);
91dd08ebf6SMatthew Brost }
92dd08ebf6SMatthew Brost 
xe_guc_hwconfig_size(struct xe_guc * guc)93dd08ebf6SMatthew Brost u32 xe_guc_hwconfig_size(struct xe_guc *guc)
94dd08ebf6SMatthew Brost {
95dd08ebf6SMatthew Brost 	return !guc->hwconfig.bo ? 0 : guc->hwconfig.size;
96dd08ebf6SMatthew Brost }
97dd08ebf6SMatthew Brost 
xe_guc_hwconfig_copy(struct xe_guc * guc,void * dst)98dd08ebf6SMatthew Brost void xe_guc_hwconfig_copy(struct xe_guc *guc, void *dst)
99dd08ebf6SMatthew Brost {
100dd08ebf6SMatthew Brost 	struct xe_device *xe = guc_to_xe(guc);
101dd08ebf6SMatthew Brost 
10299fea682SFrancois Dugast 	XE_WARN_ON(!guc->hwconfig.bo);
103dd08ebf6SMatthew Brost 
104dd08ebf6SMatthew Brost 	xe_map_memcpy_from(xe, dst, &guc->hwconfig.bo->vmap, 0,
105dd08ebf6SMatthew Brost 			   guc->hwconfig.size);
106dd08ebf6SMatthew Brost }
1078a0f58ecSMatt Roper 
xe_guc_hwconfig_dump(struct xe_guc * guc,struct drm_printer * p)1088a0f58ecSMatt Roper void xe_guc_hwconfig_dump(struct xe_guc *guc, struct drm_printer *p)
1098a0f58ecSMatt Roper {
1108a0f58ecSMatt Roper 	size_t size = xe_guc_hwconfig_size(guc);
1118a0f58ecSMatt Roper 	u32 *hwconfig;
1128a0f58ecSMatt Roper 	u64 num_dw;
1138a0f58ecSMatt Roper 	u32 extra_bytes;
1148a0f58ecSMatt Roper 	int i = 0;
1158a0f58ecSMatt Roper 
1168a0f58ecSMatt Roper 	if (size == 0) {
1178a0f58ecSMatt Roper 		drm_printf(p, "No hwconfig available\n");
1188a0f58ecSMatt Roper 		return;
1198a0f58ecSMatt Roper 	}
1208a0f58ecSMatt Roper 
1218a0f58ecSMatt Roper 	num_dw = div_u64_rem(size, sizeof(u32), &extra_bytes);
1228a0f58ecSMatt Roper 
1238a0f58ecSMatt Roper 	hwconfig = kzalloc(size, GFP_KERNEL);
1248a0f58ecSMatt Roper 	if (!hwconfig) {
1258a0f58ecSMatt Roper 		drm_printf(p, "Error: could not allocate hwconfig memory\n");
1268a0f58ecSMatt Roper 		return;
1278a0f58ecSMatt Roper 	}
1288a0f58ecSMatt Roper 
1298a0f58ecSMatt Roper 	xe_guc_hwconfig_copy(guc, hwconfig);
1308a0f58ecSMatt Roper 
1318a0f58ecSMatt Roper 	/* An entry requires at least three dwords for key, length, value */
1328a0f58ecSMatt Roper 	while (i + 3 <= num_dw) {
1338a0f58ecSMatt Roper 		u32 attribute = hwconfig[i++];
1348a0f58ecSMatt Roper 		u32 len_dw = hwconfig[i++];
1358a0f58ecSMatt Roper 
1368a0f58ecSMatt Roper 		if (i + len_dw > num_dw) {
1378a0f58ecSMatt Roper 			drm_printf(p, "Error: Attribute %u is %u dwords, but only %llu remain\n",
1388a0f58ecSMatt Roper 				   attribute, len_dw, num_dw - i);
1398a0f58ecSMatt Roper 			len_dw = num_dw - i;
1408a0f58ecSMatt Roper 		}
1418a0f58ecSMatt Roper 
1428a0f58ecSMatt Roper 		/*
1438a0f58ecSMatt Roper 		 * If it's a single dword (as most hwconfig attributes are),
1448a0f58ecSMatt Roper 		 * then it's probably a number that makes sense to display
1458a0f58ecSMatt Roper 		 * in decimal form.  In the rare cases where it's more than
1468a0f58ecSMatt Roper 		 * one dword, just print it in hex form and let the user
1478a0f58ecSMatt Roper 		 * figure out how to interpret it.
1488a0f58ecSMatt Roper 		 */
1498a0f58ecSMatt Roper 		if (len_dw == 1)
1508a0f58ecSMatt Roper 			drm_printf(p, "[%2u] = %u\n", attribute, hwconfig[i]);
1518a0f58ecSMatt Roper 		else
1528a0f58ecSMatt Roper 			drm_printf(p, "[%2u] = { %*ph }\n", attribute,
1538a0f58ecSMatt Roper 				   (int)(len_dw * sizeof(u32)), &hwconfig[i]);
1548a0f58ecSMatt Roper 		i += len_dw;
1558a0f58ecSMatt Roper 	}
1568a0f58ecSMatt Roper 
1578a0f58ecSMatt Roper 	if (i < num_dw || extra_bytes)
1588a0f58ecSMatt Roper 		drm_printf(p, "Error: %llu extra bytes at end of hwconfig\n",
1598a0f58ecSMatt Roper 			   (num_dw - i) * sizeof(u32) + extra_bytes);
1608a0f58ecSMatt Roper 
1618a0f58ecSMatt Roper 	kfree(hwconfig);
1628a0f58ecSMatt Roper }
163*fc7c7498SMatt Roper 
164*fc7c7498SMatt Roper /*
165*fc7c7498SMatt Roper  * Lookup a specific 32-bit attribute value in the GuC's hwconfig table.
166*fc7c7498SMatt Roper  */
xe_guc_hwconfig_lookup_u32(struct xe_guc * guc,u32 attribute,u32 * val)167*fc7c7498SMatt Roper int xe_guc_hwconfig_lookup_u32(struct xe_guc *guc, u32 attribute, u32 *val)
168*fc7c7498SMatt Roper {
169*fc7c7498SMatt Roper 	size_t size = xe_guc_hwconfig_size(guc);
170*fc7c7498SMatt Roper 	u64 num_dw = div_u64(size, sizeof(u32));
171*fc7c7498SMatt Roper 	u32 *hwconfig;
172*fc7c7498SMatt Roper 	bool found = false;
173*fc7c7498SMatt Roper 	int i = 0;
174*fc7c7498SMatt Roper 
175*fc7c7498SMatt Roper 	if (num_dw == 0)
176*fc7c7498SMatt Roper 		return -EINVAL;
177*fc7c7498SMatt Roper 
178*fc7c7498SMatt Roper 	hwconfig = kzalloc(size, GFP_KERNEL);
179*fc7c7498SMatt Roper 	if (!hwconfig)
180*fc7c7498SMatt Roper 		return -ENOMEM;
181*fc7c7498SMatt Roper 
182*fc7c7498SMatt Roper 	xe_guc_hwconfig_copy(guc, hwconfig);
183*fc7c7498SMatt Roper 
184*fc7c7498SMatt Roper 	/* An entry requires at least three dwords for key, length, value */
185*fc7c7498SMatt Roper 	while (i + 3 <= num_dw) {
186*fc7c7498SMatt Roper 		u32 key = hwconfig[i++];
187*fc7c7498SMatt Roper 		u32 len_dw = hwconfig[i++];
188*fc7c7498SMatt Roper 
189*fc7c7498SMatt Roper 		if (key != attribute) {
190*fc7c7498SMatt Roper 			i += len_dw;
191*fc7c7498SMatt Roper 			continue;
192*fc7c7498SMatt Roper 		}
193*fc7c7498SMatt Roper 
194*fc7c7498SMatt Roper 		*val = hwconfig[i];
195*fc7c7498SMatt Roper 		found = true;
196*fc7c7498SMatt Roper 		break;
197*fc7c7498SMatt Roper 	}
198*fc7c7498SMatt Roper 
199*fc7c7498SMatt Roper 	kfree(hwconfig);
200*fc7c7498SMatt Roper 
201*fc7c7498SMatt Roper 	return found ? 0 : -ENOENT;
202*fc7c7498SMatt Roper }
203