xref: /linux/drivers/platform/x86/intel/pmc/pwrm_telemetry.c (revision 1fc5a74b108fc90951890ec513ac81869f5eaff1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel PMC PWRM ACPI driver
4  *
5  * Copyright (C) 2025, Intel Corporation
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/bits.h>
10 #include <linux/bitfield.h>
11 #include <linux/cleanup.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/intel_vsec.h>
16 #include <linux/limits.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/resource.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <linux/uuid.h>
23 
24 #include "core.h"
25 
26 #define ENTRY_LEN		5
27 
28 /* DWORD2 */
29 #define DVSEC_ID_MASK		GENMASK(15, 0)
30 #define NUM_ENTRIES_MASK	GENMASK(23, 16)
31 #define ENTRY_SIZE_MASK		GENMASK(31, 24)
32 
33 /* DWORD3 */
34 #define TBIR_MASK		GENMASK(2, 0)
35 #define DISC_TBL_OFF_MASK	GENMASK(31, 3)
36 
37 const guid_t intel_vsec_guid =
38 	GUID_INIT(0x294903fb, 0x634d, 0x4fc7, 0xaf, 0x1f, 0x0f, 0xb9,
39 		  0x56, 0xb0, 0x4f, 0xc1);
40 
is_valid_entry(union acpi_object * pkg)41 static bool is_valid_entry(union acpi_object *pkg)
42 {
43 	int i;
44 
45 	if (!pkg || pkg->type != ACPI_TYPE_PACKAGE || pkg->package.count != ENTRY_LEN)
46 		return false;
47 
48 	if (pkg->package.elements[0].type != ACPI_TYPE_STRING)
49 		return false;
50 
51 	for (i = 1; i < ENTRY_LEN; i++)
52 		if (pkg->package.elements[i].type != ACPI_TYPE_INTEGER)
53 			return false;
54 
55 	return true;
56 }
57 
pmc_parse_telem_dsd(union acpi_object * obj,struct intel_vsec_header * header)58 acpi_disc_t pmc_parse_telem_dsd(union acpi_object *obj,
59 			struct intel_vsec_header *header)
60 {
61 	union acpi_object *vsec_pkg;
62 	union acpi_object *disc_pkg;
63 	u64 hdr0, hdr1;
64 	int num_regions;
65 	int i;
66 
67 	if (!header)
68 		return ERR_PTR(-EINVAL);
69 
70 	if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 2)
71 		return ERR_PTR(-EINVAL);
72 
73 	/* First Package is DVSEC info */
74 	vsec_pkg = &obj->package.elements[0];
75 	if (!is_valid_entry(vsec_pkg))
76 		return ERR_PTR(-EINVAL);
77 
78 	hdr0 = vsec_pkg->package.elements[3].integer.value;
79 	hdr1 = vsec_pkg->package.elements[4].integer.value;
80 
81 	header->id          = FIELD_GET(DVSEC_ID_MASK, hdr0);
82 	header->num_entries = FIELD_GET(NUM_ENTRIES_MASK, hdr0);
83 	header->entry_size  = FIELD_GET(ENTRY_SIZE_MASK, hdr0);
84 	header->tbir        = FIELD_GET(TBIR_MASK, hdr1);
85 	header->offset      = hdr1 & DISC_TBL_OFF_MASK;
86 
87 	/* Second Package contains the discovery tables */
88 	disc_pkg = &obj->package.elements[1];
89 	if (disc_pkg->type != ACPI_TYPE_PACKAGE || disc_pkg->package.count < 1)
90 		return ERR_PTR(-EINVAL);
91 
92 	num_regions = disc_pkg->package.count;
93 	if (header->num_entries != num_regions)
94 		return ERR_PTR(-EINVAL);
95 
96 	acpi_disc_t disc __free(kfree) = kmalloc_objs(*disc, num_regions);
97 	if (!disc)
98 		return ERR_PTR(-ENOMEM);
99 
100 	for (i = 0; i < num_regions; i++) {
101 		union acpi_object *pkg;
102 		u64 value;
103 		int j;
104 
105 		pkg = &disc_pkg->package.elements[i];
106 		if (!is_valid_entry(pkg))
107 			return ERR_PTR(-EINVAL);
108 
109 		/* Element 0 is a descriptive string; DWORD values start at index 1. */
110 		for (j = 1; j < ENTRY_LEN; j++) {
111 			value = pkg->package.elements[j].integer.value;
112 			if (value > U32_MAX)
113 				return ERR_PTR(-ERANGE);
114 
115 			disc[i][j - 1] = value;
116 		}
117 	}
118 
119 	return no_free_ptr(disc);
120 }
121 EXPORT_SYMBOL_NS_GPL(pmc_parse_telem_dsd, "INTEL_PMC_CORE");
122 
pmc_find_telem_guid(union acpi_object * dsd)123 union acpi_object *pmc_find_telem_guid(union acpi_object *dsd)
124 {
125 	int i;
126 
127 	if (!dsd || dsd->type != ACPI_TYPE_PACKAGE)
128 		return NULL;
129 
130 	for (i = 0; i + 1 < dsd->package.count; i += 2) {
131 		union acpi_object *uuid_obj, *data_obj;
132 		guid_t uuid;
133 
134 		uuid_obj = &dsd->package.elements[i];
135 		data_obj = &dsd->package.elements[i + 1];
136 
137 		if (uuid_obj->type != ACPI_TYPE_BUFFER ||
138 		    uuid_obj->buffer.length != 16)
139 			continue;
140 
141 		memcpy(&uuid, uuid_obj->buffer.pointer, 16);
142 		if (guid_equal(&uuid, &intel_vsec_guid))
143 			return data_obj;
144 	}
145 
146 	return NULL;
147 }
148 EXPORT_SYMBOL_NS_GPL(pmc_find_telem_guid, "INTEL_PMC_CORE");
149 
pmc_pwrm_acpi_probe(struct platform_device * pdev)150 static int pmc_pwrm_acpi_probe(struct platform_device *pdev)
151 {
152 	struct intel_vsec_header header;
153 	struct intel_vsec_header *headers[2] = { &header, NULL };
154 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
155 	struct intel_vsec_platform_info info = { };
156 	struct device *dev = &pdev->dev;
157 	union acpi_object *dsd;
158 	struct resource *res;
159 	acpi_handle handle;
160 	acpi_status status;
161 
162 	handle = ACPI_HANDLE(&pdev->dev);
163 	if (!handle)
164 		return -ENODEV;
165 
166 	status = acpi_evaluate_object(handle, "_DSD", NULL, &buf);
167 	if (ACPI_FAILURE(status)) {
168 		return dev_err_probe(dev, -ENODEV, "Could not evaluate _DSD: %s\n",
169 				     acpi_format_exception(status));
170 	}
171 
172 	void *dsd_buf __free(pmc_acpi_free) = buf.pointer;
173 
174 	dsd = pmc_find_telem_guid(dsd_buf);
175 	if (!dsd)
176 		return -ENODEV;
177 
178 	acpi_disc_t acpi_disc __free(kfree) = pmc_parse_telem_dsd(dsd, &header);
179 	if (IS_ERR(acpi_disc))
180 		return PTR_ERR(acpi_disc);
181 
182 	res = platform_get_resource(pdev, IORESOURCE_MEM, header.tbir);
183 	if (!res)
184 		return -EINVAL;
185 
186 	info.headers = headers;
187 	info.caps = VSEC_CAP_TELEMETRY;
188 	info.acpi_disc = acpi_disc;
189 	info.src = INTEL_VSEC_DISC_ACPI;
190 	info.base_addr = res->start;
191 
192 	return intel_vsec_register(&pdev->dev, &info);
193 }
194 
195 static const struct acpi_device_id pmc_pwrm_acpi_ids[] = {
196 	{ "INTC1122", 0 }, /* Nova Lake */
197 	{ "INTC1129", 0 }, /* Nova Lake */
198 	{ }
199 };
200 MODULE_DEVICE_TABLE(acpi, pmc_pwrm_acpi_ids);
201 
202 static struct platform_driver pmc_pwrm_acpi_driver = {
203 	.probe = pmc_pwrm_acpi_probe,
204 	.driver = {
205 		.name = "intel_pmc_pwrm_acpi",
206 		.acpi_match_table = ACPI_PTR(pmc_pwrm_acpi_ids),
207 	},
208 };
209 module_platform_driver(pmc_pwrm_acpi_driver);
210 
211 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
212 MODULE_DESCRIPTION("Intel PMC PWRM ACPI driver");
213 MODULE_LICENSE("GPL");
214 MODULE_IMPORT_NS("INTEL_VSEC");
215