cpu.c 7.3 KB

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