core.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (c) 2018-2021 Intel Corporation
  3. #include <linux/bug.h>
  4. #include <linux/device.h>
  5. #include <linux/export.h>
  6. #include <linux/idr.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/peci.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/property.h>
  12. #include <linux/slab.h>
  13. #include "internal.h"
  14. static DEFINE_IDA(peci_controller_ida);
  15. static void peci_controller_dev_release(struct device *dev)
  16. {
  17. struct peci_controller *controller = to_peci_controller(dev);
  18. mutex_destroy(&controller->bus_lock);
  19. ida_free(&peci_controller_ida, controller->id);
  20. kfree(controller);
  21. }
  22. struct device_type peci_controller_type = {
  23. .release = peci_controller_dev_release,
  24. };
  25. int peci_controller_scan_devices(struct peci_controller *controller)
  26. {
  27. int ret;
  28. u8 addr;
  29. for (addr = PECI_BASE_ADDR; addr < PECI_BASE_ADDR + PECI_DEVICE_NUM_MAX; addr++) {
  30. ret = peci_device_create(controller, addr);
  31. if (ret)
  32. return ret;
  33. }
  34. return 0;
  35. }
  36. static struct peci_controller *peci_controller_alloc(struct device *dev,
  37. struct peci_controller_ops *ops)
  38. {
  39. struct peci_controller *controller;
  40. int ret;
  41. if (!ops->xfer)
  42. return ERR_PTR(-EINVAL);
  43. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  44. if (!controller)
  45. return ERR_PTR(-ENOMEM);
  46. ret = ida_alloc_max(&peci_controller_ida, U8_MAX, GFP_KERNEL);
  47. if (ret < 0)
  48. goto err;
  49. controller->id = ret;
  50. controller->ops = ops;
  51. controller->dev.parent = dev;
  52. controller->dev.bus = &peci_bus_type;
  53. controller->dev.type = &peci_controller_type;
  54. device_initialize(&controller->dev);
  55. mutex_init(&controller->bus_lock);
  56. return controller;
  57. err:
  58. kfree(controller);
  59. return ERR_PTR(ret);
  60. }
  61. static int unregister_child(struct device *dev, void *dummy)
  62. {
  63. peci_device_destroy(to_peci_device(dev));
  64. return 0;
  65. }
  66. static void unregister_controller(void *_controller)
  67. {
  68. struct peci_controller *controller = _controller;
  69. /*
  70. * Detach any active PECI devices. This can't fail, thus we do not
  71. * check the returned value.
  72. */
  73. device_for_each_child_reverse(&controller->dev, NULL, unregister_child);
  74. device_unregister(&controller->dev);
  75. fwnode_handle_put(controller->dev.fwnode);
  76. pm_runtime_disable(&controller->dev);
  77. }
  78. /**
  79. * devm_peci_controller_add() - add PECI controller
  80. * @dev: device for devm operations
  81. * @ops: pointer to controller specific methods
  82. *
  83. * In final stage of its probe(), peci_controller driver calls
  84. * devm_peci_controller_add() to register itself with the PECI bus.
  85. *
  86. * Return: Pointer to the newly allocated controller or ERR_PTR() in case of failure.
  87. */
  88. struct peci_controller *devm_peci_controller_add(struct device *dev,
  89. struct peci_controller_ops *ops)
  90. {
  91. struct peci_controller *controller;
  92. int ret;
  93. controller = peci_controller_alloc(dev, ops);
  94. if (IS_ERR(controller))
  95. return controller;
  96. ret = dev_set_name(&controller->dev, "peci-%d", controller->id);
  97. if (ret)
  98. goto err_put;
  99. pm_runtime_no_callbacks(&controller->dev);
  100. pm_suspend_ignore_children(&controller->dev, true);
  101. pm_runtime_enable(&controller->dev);
  102. device_set_node(&controller->dev, fwnode_handle_get(dev_fwnode(dev)));
  103. ret = device_add(&controller->dev);
  104. if (ret)
  105. goto err_fwnode;
  106. ret = devm_add_action_or_reset(dev, unregister_controller, controller);
  107. if (ret)
  108. return ERR_PTR(ret);
  109. /*
  110. * Ignoring retval since failures during scan are non-critical for
  111. * controller itself.
  112. */
  113. peci_controller_scan_devices(controller);
  114. return controller;
  115. err_fwnode:
  116. fwnode_handle_put(controller->dev.fwnode);
  117. pm_runtime_disable(&controller->dev);
  118. err_put:
  119. put_device(&controller->dev);
  120. return ERR_PTR(ret);
  121. }
  122. EXPORT_SYMBOL_NS_GPL(devm_peci_controller_add, PECI);
  123. static const struct peci_device_id *
  124. peci_bus_match_device_id(const struct peci_device_id *id, struct peci_device *device)
  125. {
  126. while (id->family != 0) {
  127. if (id->family == device->info.family &&
  128. id->model == device->info.model)
  129. return id;
  130. id++;
  131. }
  132. return NULL;
  133. }
  134. static int peci_bus_device_match(struct device *dev, struct device_driver *drv)
  135. {
  136. struct peci_device *device = to_peci_device(dev);
  137. struct peci_driver *peci_drv = to_peci_driver(drv);
  138. if (dev->type != &peci_device_type)
  139. return 0;
  140. return !!peci_bus_match_device_id(peci_drv->id_table, device);
  141. }
  142. static int peci_bus_device_probe(struct device *dev)
  143. {
  144. struct peci_device *device = to_peci_device(dev);
  145. struct peci_driver *driver = to_peci_driver(dev->driver);
  146. return driver->probe(device, peci_bus_match_device_id(driver->id_table, device));
  147. }
  148. static void peci_bus_device_remove(struct device *dev)
  149. {
  150. struct peci_device *device = to_peci_device(dev);
  151. struct peci_driver *driver = to_peci_driver(dev->driver);
  152. if (driver->remove)
  153. driver->remove(device);
  154. }
  155. struct bus_type peci_bus_type = {
  156. .name = "peci",
  157. .match = peci_bus_device_match,
  158. .probe = peci_bus_device_probe,
  159. .remove = peci_bus_device_remove,
  160. .bus_groups = peci_bus_groups,
  161. };
  162. static int __init peci_init(void)
  163. {
  164. int ret;
  165. ret = bus_register(&peci_bus_type);
  166. if (ret < 0) {
  167. pr_err("peci: failed to register PECI bus type!\n");
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. module_init(peci_init);
  173. static void __exit peci_exit(void)
  174. {
  175. bus_unregister(&peci_bus_type);
  176. }
  177. module_exit(peci_exit);
  178. MODULE_AUTHOR("Jason M Bills <[email protected]>");
  179. MODULE_AUTHOR("Jae Hyun Yoo <[email protected]>");
  180. MODULE_AUTHOR("Iwona Winiarska <[email protected]>");
  181. MODULE_DESCRIPTION("PECI bus core module");
  182. MODULE_LICENSE("GPL");