Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / peci / cpu.c
blob2dac8ba827872810833d3b3d9cc408fd3bb0ae31
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2021 Intel Corporation
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>
10 #include "internal.h"
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
17 * It uses GetTemp PECI command.
19 * Return: 0 if succeeded, other values in case errors.
21 int peci_temp_read(struct peci_device *device, s16 *temp_raw)
23 struct peci_request *req;
25 req = peci_xfer_get_temp(device);
26 if (IS_ERR(req))
27 return PTR_ERR(req);
29 *temp_raw = peci_request_temp_read(req);
31 peci_request_free(req);
33 return 0;
35 EXPORT_SYMBOL_NS_GPL(peci_temp_read, "PECI_CPU");
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
44 * It uses RdPkgConfig PECI command.
46 * Return: 0 if succeeded, other values in case errors.
48 int peci_pcs_read(struct peci_device *device, u8 index, u16 param, u32 *data)
50 struct peci_request *req;
51 int ret;
53 req = peci_xfer_pkg_cfg_readl(device, index, param);
54 if (IS_ERR(req))
55 return PTR_ERR(req);
57 ret = peci_request_status(req);
58 if (ret)
59 goto out_req_free;
61 *data = peci_request_data_readl(req);
62 out_req_free:
63 peci_request_free(req);
65 return ret;
67 EXPORT_SYMBOL_NS_GPL(peci_pcs_read, "PECI_CPU");
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
78 * It uses RdPCIConfigLocal PECI command.
80 * Return: 0 if succeeded, other values in case errors.
82 int peci_pci_local_read(struct peci_device *device, u8 bus, u8 dev, u8 func,
83 u16 reg, u32 *data)
85 struct peci_request *req;
86 int ret;
88 req = peci_xfer_pci_cfg_local_readl(device, bus, dev, func, reg);
89 if (IS_ERR(req))
90 return PTR_ERR(req);
92 ret = peci_request_status(req);
93 if (ret)
94 goto out_req_free;
96 *data = peci_request_data_readl(req);
97 out_req_free:
98 peci_request_free(req);
100 return ret;
102 EXPORT_SYMBOL_NS_GPL(peci_pci_local_read, "PECI_CPU");
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
114 * Like &peci_pci_local_read, but it uses RdEndpointConfig PECI command.
116 * Return: 0 if succeeded, other values in case errors.
118 int peci_ep_pci_local_read(struct peci_device *device, u8 seg,
119 u8 bus, u8 dev, u8 func, u16 reg, u32 *data)
121 struct peci_request *req;
122 int ret;
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);
128 ret = peci_request_status(req);
129 if (ret)
130 goto out_req_free;
132 *data = peci_request_data_readl(req);
133 out_req_free:
134 peci_request_free(req);
136 return ret;
138 EXPORT_SYMBOL_NS_GPL(peci_ep_pci_local_read, "PECI_CPU");
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
151 * It uses RdEndpointConfig PECI command.
153 * Return: 0 if succeeded, other values in case errors.
155 int peci_mmio_read(struct peci_device *device, u8 bar, u8 seg,
156 u8 bus, u8 dev, u8 func, u64 address, u32 *data)
158 struct peci_request *req;
159 int ret;
161 req = peci_xfer_ep_mmio64_readl(device, bar, seg, bus, dev, func, address);
162 if (IS_ERR(req))
163 return PTR_ERR(req);
165 ret = peci_request_status(req);
166 if (ret)
167 goto out_req_free;
169 *data = peci_request_data_readl(req);
170 out_req_free:
171 peci_request_free(req);
173 return ret;
175 EXPORT_SYMBOL_NS_GPL(peci_mmio_read, "PECI_CPU");
177 static const char * const peci_adev_types[] = {
178 "cputemp",
179 "dimmtemp",
182 struct peci_cpu {
183 struct peci_device *device;
184 const struct peci_device_id *id;
187 static void adev_release(struct device *dev)
189 struct auxiliary_device *adev = to_auxiliary_dev(dev);
191 kfree(adev->name);
192 kfree(adev);
195 static struct auxiliary_device *adev_alloc(struct peci_cpu *priv, int idx)
197 struct peci_controller *controller = to_peci_controller(priv->device->dev.parent);
198 struct auxiliary_device *adev;
199 const char *name;
200 int ret;
202 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
203 if (!adev)
204 return ERR_PTR(-ENOMEM);
206 name = kasprintf(GFP_KERNEL, "%s.%s", peci_adev_types[idx], (const char *)priv->id->data);
207 if (!name) {
208 ret = -ENOMEM;
209 goto free_adev;
212 adev->name = name;
213 adev->dev.parent = &priv->device->dev;
214 adev->dev.release = adev_release;
215 adev->id = (controller->id << 16) | (priv->device->addr);
217 ret = auxiliary_device_init(adev);
218 if (ret)
219 goto free_name;
221 return adev;
223 free_name:
224 kfree(name);
225 free_adev:
226 kfree(adev);
227 return ERR_PTR(ret);
230 static void unregister_adev(void *_adev)
232 struct auxiliary_device *adev = _adev;
234 auxiliary_device_delete(adev);
235 auxiliary_device_uninit(adev);
238 static int devm_adev_add(struct device *dev, int idx)
240 struct peci_cpu *priv = dev_get_drvdata(dev);
241 struct auxiliary_device *adev;
242 int ret;
244 adev = adev_alloc(priv, idx);
245 if (IS_ERR(adev))
246 return PTR_ERR(adev);
248 ret = auxiliary_device_add(adev);
249 if (ret) {
250 auxiliary_device_uninit(adev);
251 return ret;
254 ret = devm_add_action_or_reset(&priv->device->dev, unregister_adev, adev);
255 if (ret)
256 return ret;
258 return 0;
261 static void peci_cpu_add_adevices(struct peci_cpu *priv)
263 struct device *dev = &priv->device->dev;
264 int ret, i;
266 for (i = 0; i < ARRAY_SIZE(peci_adev_types); i++) {
267 ret = devm_adev_add(dev, i);
268 if (ret) {
269 dev_warn(dev, "Failed to register PECI auxiliary: %s, ret = %d\n",
270 peci_adev_types[i], ret);
271 continue;
276 static int
277 peci_cpu_probe(struct peci_device *device, const struct peci_device_id *id)
279 struct device *dev = &device->dev;
280 struct peci_cpu *priv;
282 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
283 if (!priv)
284 return -ENOMEM;
286 dev_set_drvdata(dev, priv);
287 priv->device = device;
288 priv->id = id;
290 peci_cpu_add_adevices(priv);
292 return 0;
295 static const struct peci_device_id peci_cpu_device_ids[] = {
296 { /* Haswell Xeon */
297 .x86_vfm = INTEL_HASWELL_X,
298 .data = "hsx",
300 { /* Broadwell Xeon */
301 .x86_vfm = INTEL_BROADWELL_X,
302 .data = "bdx",
304 { /* Broadwell Xeon D */
305 .x86_vfm = INTEL_BROADWELL_D,
306 .data = "bdxd",
308 { /* Skylake Xeon */
309 .x86_vfm = INTEL_SKYLAKE_X,
310 .data = "skx",
312 { /* Icelake Xeon */
313 .x86_vfm = INTEL_ICELAKE_X,
314 .data = "icx",
316 { /* Icelake Xeon D */
317 .x86_vfm = INTEL_ICELAKE_D,
318 .data = "icxd",
320 { /* Sapphire Rapids Xeon */
321 .x86_vfm = INTEL_SAPPHIRERAPIDS_X,
322 .data = "spr",
326 MODULE_DEVICE_TABLE(peci, peci_cpu_device_ids);
328 static struct peci_driver peci_cpu_driver = {
329 .probe = peci_cpu_probe,
330 .id_table = peci_cpu_device_ids,
331 .driver = {
332 .name = "peci-cpu",
335 module_peci_driver(peci_cpu_driver);
337 MODULE_AUTHOR("Iwona Winiarska <iwona.winiarska@intel.com>");
338 MODULE_DESCRIPTION("PECI CPU driver");
339 MODULE_LICENSE("GPL");
340 MODULE_IMPORT_NS("PECI");