xref: /linux/drivers/peci/cpu.c (revision 288440de9e5fdb4a3ff73864850f080c1250fc81)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2021 Intel Corporation
3 
4 #include <linux/auxiliary_bus.h>
5 #include <linux/module.h>
6 #include <linux/peci.h>
7 #include <linux/peci-cpu.h>
8 #include <linux/slab.h>
9 
10 #include "internal.h"
11 
12 /**
13  * peci_temp_read() - read the maximum die temperature from PECI target device
14  * @device: PECI device to which request is going to be sent
15  * @temp_raw: where to store the read temperature
16  *
17  * It uses GetTemp PECI command.
18  *
19  * Return: 0 if succeeded, other values in case errors.
20  */
21 int peci_temp_read(struct peci_device *device, s16 *temp_raw)
22 {
23 	struct peci_request *req;
24 
25 	req = peci_xfer_get_temp(device);
26 	if (IS_ERR(req))
27 		return PTR_ERR(req);
28 
29 	*temp_raw = peci_request_temp_read(req);
30 
31 	peci_request_free(req);
32 
33 	return 0;
34 }
35 EXPORT_SYMBOL_NS_GPL(peci_temp_read, PECI_CPU);
36 
37 /**
38  * peci_pcs_read() - read PCS register
39  * @device: PECI device to which request is going to be sent
40  * @index: PCS index
41  * @param: PCS parameter
42  * @data: where to store the read data
43  *
44  * It uses RdPkgConfig PECI command.
45  *
46  * Return: 0 if succeeded, other values in case errors.
47  */
48 int peci_pcs_read(struct peci_device *device, u8 index, u16 param, u32 *data)
49 {
50 	struct peci_request *req;
51 	int ret;
52 
53 	req = peci_xfer_pkg_cfg_readl(device, index, param);
54 	if (IS_ERR(req))
55 		return PTR_ERR(req);
56 
57 	ret = peci_request_status(req);
58 	if (ret)
59 		goto out_req_free;
60 
61 	*data = peci_request_data_readl(req);
62 out_req_free:
63 	peci_request_free(req);
64 
65 	return ret;
66 }
67 EXPORT_SYMBOL_NS_GPL(peci_pcs_read, PECI_CPU);
68 
69 /**
70  * peci_pci_local_read() - read 32-bit memory location using raw address
71  * @device: PECI device to which request is going to be sent
72  * @bus: bus
73  * @dev: device
74  * @func: function
75  * @reg: register
76  * @data: where to store the read data
77  *
78  * It uses RdPCIConfigLocal PECI command.
79  *
80  * Return: 0 if succeeded, other values in case errors.
81  */
82 int peci_pci_local_read(struct peci_device *device, u8 bus, u8 dev, u8 func,
83 			u16 reg, u32 *data)
84 {
85 	struct peci_request *req;
86 	int ret;
87 
88 	req = peci_xfer_pci_cfg_local_readl(device, bus, dev, func, reg);
89 	if (IS_ERR(req))
90 		return PTR_ERR(req);
91 
92 	ret = peci_request_status(req);
93 	if (ret)
94 		goto out_req_free;
95 
96 	*data = peci_request_data_readl(req);
97 out_req_free:
98 	peci_request_free(req);
99 
100 	return ret;
101 }
102 EXPORT_SYMBOL_NS_GPL(peci_pci_local_read, PECI_CPU);
103 
104 /**
105  * peci_ep_pci_local_read() - read 32-bit memory location using raw address
106  * @device: PECI device to which request is going to be sent
107  * @seg: PCI segment
108  * @bus: bus
109  * @dev: device
110  * @func: function
111  * @reg: register
112  * @data: where to store the read data
113  *
114  * Like &peci_pci_local_read, but it uses RdEndpointConfig PECI command.
115  *
116  * Return: 0 if succeeded, other values in case errors.
117  */
118 int peci_ep_pci_local_read(struct peci_device *device, u8 seg,
119 			   u8 bus, u8 dev, u8 func, u16 reg, u32 *data)
120 {
121 	struct peci_request *req;
122 	int ret;
123 
124 	req = peci_xfer_ep_pci_cfg_local_readl(device, seg, bus, dev, func, reg);
125 	if (IS_ERR(req))
126 		return PTR_ERR(req);
127 
128 	ret = peci_request_status(req);
129 	if (ret)
130 		goto out_req_free;
131 
132 	*data = peci_request_data_readl(req);
133 out_req_free:
134 	peci_request_free(req);
135 
136 	return ret;
137 }
138 EXPORT_SYMBOL_NS_GPL(peci_ep_pci_local_read, PECI_CPU);
139 
140 /**
141  * peci_mmio_read() - read 32-bit memory location using 64-bit bar offset address
142  * @device: PECI device to which request is going to be sent
143  * @bar: PCI bar
144  * @seg: PCI segment
145  * @bus: bus
146  * @dev: device
147  * @func: function
148  * @address: 64-bit MMIO address
149  * @data: where to store the read data
150  *
151  * It uses RdEndpointConfig PECI command.
152  *
153  * Return: 0 if succeeded, other values in case errors.
154  */
155 int peci_mmio_read(struct peci_device *device, u8 bar, u8 seg,
156 		   u8 bus, u8 dev, u8 func, u64 address, u32 *data)
157 {
158 	struct peci_request *req;
159 	int ret;
160 
161 	req = peci_xfer_ep_mmio64_readl(device, bar, seg, bus, dev, func, address);
162 	if (IS_ERR(req))
163 		return PTR_ERR(req);
164 
165 	ret = peci_request_status(req);
166 	if (ret)
167 		goto out_req_free;
168 
169 	*data = peci_request_data_readl(req);
170 out_req_free:
171 	peci_request_free(req);
172 
173 	return ret;
174 }
175 EXPORT_SYMBOL_NS_GPL(peci_mmio_read, PECI_CPU);
176 
177 static const char * const peci_adev_types[] = {
178 	"cputemp",
179 	"dimmtemp",
180 };
181 
182 struct peci_cpu {
183 	struct peci_device *device;
184 	const struct peci_device_id *id;
185 };
186 
187 static void adev_release(struct device *dev)
188 {
189 	struct auxiliary_device *adev = to_auxiliary_dev(dev);
190 
191 	auxiliary_device_uninit(adev);
192 
193 	kfree(adev->name);
194 	kfree(adev);
195 }
196 
197 static struct auxiliary_device *adev_alloc(struct peci_cpu *priv, int idx)
198 {
199 	struct peci_controller *controller = to_peci_controller(priv->device->dev.parent);
200 	struct auxiliary_device *adev;
201 	const char *name;
202 	int ret;
203 
204 	adev = kzalloc(sizeof(*adev), GFP_KERNEL);
205 	if (!adev)
206 		return ERR_PTR(-ENOMEM);
207 
208 	name = kasprintf(GFP_KERNEL, "%s.%s", peci_adev_types[idx], (const char *)priv->id->data);
209 	if (!name) {
210 		ret = -ENOMEM;
211 		goto free_adev;
212 	}
213 
214 	adev->name = name;
215 	adev->dev.parent = &priv->device->dev;
216 	adev->dev.release = adev_release;
217 	adev->id = (controller->id << 16) | (priv->device->addr);
218 
219 	ret = auxiliary_device_init(adev);
220 	if (ret)
221 		goto free_name;
222 
223 	return adev;
224 
225 free_name:
226 	kfree(name);
227 free_adev:
228 	kfree(adev);
229 	return ERR_PTR(ret);
230 }
231 
232 static void unregister_adev(void *_adev)
233 {
234 	struct auxiliary_device *adev = _adev;
235 
236 	auxiliary_device_delete(adev);
237 }
238 
239 static int devm_adev_add(struct device *dev, int idx)
240 {
241 	struct peci_cpu *priv = dev_get_drvdata(dev);
242 	struct auxiliary_device *adev;
243 	int ret;
244 
245 	adev = adev_alloc(priv, idx);
246 	if (IS_ERR(adev))
247 		return PTR_ERR(adev);
248 
249 	ret = auxiliary_device_add(adev);
250 	if (ret) {
251 		auxiliary_device_uninit(adev);
252 		return ret;
253 	}
254 
255 	ret = devm_add_action_or_reset(&priv->device->dev, unregister_adev, adev);
256 	if (ret)
257 		return ret;
258 
259 	return 0;
260 }
261 
262 static void peci_cpu_add_adevices(struct peci_cpu *priv)
263 {
264 	struct device *dev = &priv->device->dev;
265 	int ret, i;
266 
267 	for (i = 0; i < ARRAY_SIZE(peci_adev_types); i++) {
268 		ret = devm_adev_add(dev, i);
269 		if (ret) {
270 			dev_warn(dev, "Failed to register PECI auxiliary: %s, ret = %d\n",
271 				 peci_adev_types[i], ret);
272 			continue;
273 		}
274 	}
275 }
276 
277 static int
278 peci_cpu_probe(struct peci_device *device, const struct peci_device_id *id)
279 {
280 	struct device *dev = &device->dev;
281 	struct peci_cpu *priv;
282 
283 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
284 	if (!priv)
285 		return -ENOMEM;
286 
287 	dev_set_drvdata(dev, priv);
288 	priv->device = device;
289 	priv->id = id;
290 
291 	peci_cpu_add_adevices(priv);
292 
293 	return 0;
294 }
295 
296 static const struct peci_device_id peci_cpu_device_ids[] = {
297 	{ /* Haswell Xeon */
298 		.family	= 6,
299 		.model	= INTEL_FAM6_HASWELL_X,
300 		.data	= "hsx",
301 	},
302 	{ /* Broadwell Xeon */
303 		.family	= 6,
304 		.model	= INTEL_FAM6_BROADWELL_X,
305 		.data	= "bdx",
306 	},
307 	{ /* Broadwell Xeon D */
308 		.family	= 6,
309 		.model	= INTEL_FAM6_BROADWELL_D,
310 		.data	= "bdxd",
311 	},
312 	{ /* Skylake Xeon */
313 		.family	= 6,
314 		.model	= INTEL_FAM6_SKYLAKE_X,
315 		.data	= "skx",
316 	},
317 	{ /* Icelake Xeon */
318 		.family	= 6,
319 		.model	= INTEL_FAM6_ICELAKE_X,
320 		.data	= "icx",
321 	},
322 	{ /* Icelake Xeon D */
323 		.family	= 6,
324 		.model	= INTEL_FAM6_ICELAKE_D,
325 		.data	= "icxd",
326 	},
327 	{ }
328 };
329 MODULE_DEVICE_TABLE(peci, peci_cpu_device_ids);
330 
331 static struct peci_driver peci_cpu_driver = {
332 	.probe		= peci_cpu_probe,
333 	.id_table	= peci_cpu_device_ids,
334 	.driver		= {
335 		.name		= "peci-cpu",
336 	},
337 };
338 module_peci_driver(peci_cpu_driver);
339 
340 MODULE_AUTHOR("Iwona Winiarska <iwona.winiarska@intel.com>");
341 MODULE_DESCRIPTION("PECI CPU driver");
342 MODULE_LICENSE("GPL");
343 MODULE_IMPORT_NS(PECI);
344