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>
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
);
29 *temp_raw
= peci_request_temp_read(req
);
31 peci_request_free(req
);
35 EXPORT_SYMBOL_NS_GPL(peci_temp_read
, "PECI_CPU");
38 * peci_pcs_read() - read PCS register
39 * @device: PECI device to which request is going to be sent
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
;
53 req
= peci_xfer_pkg_cfg_readl(device
, index
, param
);
57 ret
= peci_request_status(req
);
61 *data
= peci_request_data_readl(req
);
63 peci_request_free(req
);
67 EXPORT_SYMBOL_NS_GPL(peci_pcs_read
, "PECI_CPU");
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
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
,
85 struct peci_request
*req
;
88 req
= peci_xfer_pci_cfg_local_readl(device
, bus
, dev
, func
, reg
);
92 ret
= peci_request_status(req
);
96 *data
= peci_request_data_readl(req
);
98 peci_request_free(req
);
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
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
;
124 req
= peci_xfer_ep_pci_cfg_local_readl(device
, seg
, bus
, dev
, func
, reg
);
128 ret
= peci_request_status(req
);
132 *data
= peci_request_data_readl(req
);
134 peci_request_free(req
);
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
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
;
161 req
= peci_xfer_ep_mmio64_readl(device
, bar
, seg
, bus
, dev
, func
, address
);
165 ret
= peci_request_status(req
);
169 *data
= peci_request_data_readl(req
);
171 peci_request_free(req
);
175 EXPORT_SYMBOL_NS_GPL(peci_mmio_read
, "PECI_CPU");
177 static const char * const peci_adev_types
[] = {
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
);
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
;
202 adev
= kzalloc(sizeof(*adev
), GFP_KERNEL
);
204 return ERR_PTR(-ENOMEM
);
206 name
= kasprintf(GFP_KERNEL
, "%s.%s", peci_adev_types
[idx
], (const char *)priv
->id
->data
);
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
);
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
;
244 adev
= adev_alloc(priv
, idx
);
246 return PTR_ERR(adev
);
248 ret
= auxiliary_device_add(adev
);
250 auxiliary_device_uninit(adev
);
254 ret
= devm_add_action_or_reset(&priv
->device
->dev
, unregister_adev
, adev
);
261 static void peci_cpu_add_adevices(struct peci_cpu
*priv
)
263 struct device
*dev
= &priv
->device
->dev
;
266 for (i
= 0; i
< ARRAY_SIZE(peci_adev_types
); i
++) {
267 ret
= devm_adev_add(dev
, i
);
269 dev_warn(dev
, "Failed to register PECI auxiliary: %s, ret = %d\n",
270 peci_adev_types
[i
], ret
);
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
);
286 dev_set_drvdata(dev
, priv
);
287 priv
->device
= device
;
290 peci_cpu_add_adevices(priv
);
295 static const struct peci_device_id peci_cpu_device_ids
[] = {
297 .x86_vfm
= INTEL_HASWELL_X
,
300 { /* Broadwell Xeon */
301 .x86_vfm
= INTEL_BROADWELL_X
,
304 { /* Broadwell Xeon D */
305 .x86_vfm
= INTEL_BROADWELL_D
,
309 .x86_vfm
= INTEL_SKYLAKE_X
,
313 .x86_vfm
= INTEL_ICELAKE_X
,
316 { /* Icelake Xeon D */
317 .x86_vfm
= INTEL_ICELAKE_D
,
320 { /* Sapphire Rapids Xeon */
321 .x86_vfm
= INTEL_SAPPHIRERAPIDS_X
,
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
,
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");