bus.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Message Protocol bus layer
  4. *
  5. * Copyright (C) 2018 ARM Ltd.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/device.h>
  13. #include "common.h"
  14. static DEFINE_IDA(scmi_bus_id);
  15. static DEFINE_IDR(scmi_available_protocols);
  16. static DEFINE_SPINLOCK(protocol_lock);
  17. static const struct scmi_device_id *
  18. scmi_dev_match_id(struct scmi_device *scmi_dev, struct scmi_driver *scmi_drv)
  19. {
  20. const struct scmi_device_id *id = scmi_drv->id_table;
  21. if (!id)
  22. return NULL;
  23. for (; id->protocol_id; id++)
  24. if (id->protocol_id == scmi_dev->protocol_id) {
  25. if (!id->name)
  26. return id;
  27. else if (!strcmp(id->name, scmi_dev->name))
  28. return id;
  29. }
  30. return NULL;
  31. }
  32. static int scmi_dev_match(struct device *dev, struct device_driver *drv)
  33. {
  34. struct scmi_driver *scmi_drv = to_scmi_driver(drv);
  35. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  36. const struct scmi_device_id *id;
  37. id = scmi_dev_match_id(scmi_dev, scmi_drv);
  38. if (id)
  39. return 1;
  40. return 0;
  41. }
  42. static int scmi_match_by_id_table(struct device *dev, void *data)
  43. {
  44. struct scmi_device *sdev = to_scmi_dev(dev);
  45. struct scmi_device_id *id_table = data;
  46. return sdev->protocol_id == id_table->protocol_id &&
  47. !strcmp(sdev->name, id_table->name);
  48. }
  49. struct scmi_device *scmi_find_child_dev(struct device *parent,
  50. int prot_id, const char *name)
  51. {
  52. struct scmi_device_id id_table;
  53. struct device *dev;
  54. id_table.protocol_id = prot_id;
  55. id_table.name = name;
  56. dev = device_find_child(parent, &id_table, scmi_match_by_id_table);
  57. if (!dev)
  58. return NULL;
  59. return to_scmi_dev(dev);
  60. }
  61. const struct scmi_protocol *scmi_get_protocol(int protocol_id)
  62. {
  63. const struct scmi_protocol *proto;
  64. proto = idr_find(&scmi_available_protocols, protocol_id);
  65. if (!proto || !try_module_get(proto->owner)) {
  66. pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id);
  67. return NULL;
  68. }
  69. pr_debug("GOT SCMI Protocol 0x%x\n", protocol_id);
  70. return proto;
  71. }
  72. void scmi_put_protocol(int protocol_id)
  73. {
  74. const struct scmi_protocol *proto;
  75. proto = idr_find(&scmi_available_protocols, protocol_id);
  76. if (proto)
  77. module_put(proto->owner);
  78. }
  79. static int scmi_dev_probe(struct device *dev)
  80. {
  81. struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
  82. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  83. const struct scmi_device_id *id;
  84. id = scmi_dev_match_id(scmi_dev, scmi_drv);
  85. if (!id)
  86. return -ENODEV;
  87. if (!scmi_dev->handle)
  88. return -EPROBE_DEFER;
  89. return scmi_drv->probe(scmi_dev);
  90. }
  91. static int scmi_dev_remove(struct device *dev)
  92. {
  93. struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
  94. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  95. if (scmi_drv->remove)
  96. scmi_drv->remove(scmi_dev);
  97. return 0;
  98. }
  99. static struct bus_type scmi_bus_type = {
  100. .name = "scmi_protocol",
  101. .match = scmi_dev_match,
  102. .probe = scmi_dev_probe,
  103. .remove = scmi_dev_remove,
  104. };
  105. int scmi_driver_register(struct scmi_driver *driver, struct module *owner,
  106. const char *mod_name)
  107. {
  108. int retval;
  109. if (!driver->probe)
  110. return -EINVAL;
  111. retval = scmi_request_protocol_device(driver->id_table);
  112. if (retval)
  113. return retval;
  114. driver->driver.bus = &scmi_bus_type;
  115. driver->driver.name = driver->name;
  116. driver->driver.owner = owner;
  117. driver->driver.mod_name = mod_name;
  118. retval = driver_register(&driver->driver);
  119. if (!retval)
  120. pr_debug("registered new scmi driver %s\n", driver->name);
  121. return retval;
  122. }
  123. EXPORT_SYMBOL_GPL(scmi_driver_register);
  124. void scmi_driver_unregister(struct scmi_driver *driver)
  125. {
  126. driver_unregister(&driver->driver);
  127. scmi_unrequest_protocol_device(driver->id_table);
  128. }
  129. EXPORT_SYMBOL_GPL(scmi_driver_unregister);
  130. static void scmi_device_release(struct device *dev)
  131. {
  132. kfree(to_scmi_dev(dev));
  133. }
  134. struct scmi_device *
  135. scmi_device_create(struct device_node *np, struct device *parent, int protocol,
  136. const char *name)
  137. {
  138. int id, retval;
  139. struct scmi_device *scmi_dev;
  140. scmi_dev = kzalloc(sizeof(*scmi_dev), GFP_KERNEL);
  141. if (!scmi_dev)
  142. return NULL;
  143. scmi_dev->name = kstrdup_const(name ?: "unknown", GFP_KERNEL);
  144. if (!scmi_dev->name) {
  145. kfree(scmi_dev);
  146. return NULL;
  147. }
  148. id = ida_simple_get(&scmi_bus_id, 1, 0, GFP_KERNEL);
  149. if (id < 0) {
  150. kfree_const(scmi_dev->name);
  151. kfree(scmi_dev);
  152. return NULL;
  153. }
  154. scmi_dev->id = id;
  155. scmi_dev->protocol_id = protocol;
  156. scmi_dev->dev.parent = parent;
  157. scmi_dev->dev.of_node = np;
  158. scmi_dev->dev.bus = &scmi_bus_type;
  159. scmi_dev->dev.release = scmi_device_release;
  160. dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id);
  161. retval = device_register(&scmi_dev->dev);
  162. if (retval)
  163. goto put_dev;
  164. return scmi_dev;
  165. put_dev:
  166. kfree_const(scmi_dev->name);
  167. put_device(&scmi_dev->dev);
  168. ida_simple_remove(&scmi_bus_id, id);
  169. return NULL;
  170. }
  171. void scmi_device_destroy(struct scmi_device *scmi_dev)
  172. {
  173. kfree_const(scmi_dev->name);
  174. scmi_handle_put(scmi_dev->handle);
  175. ida_simple_remove(&scmi_bus_id, scmi_dev->id);
  176. device_unregister(&scmi_dev->dev);
  177. }
  178. void scmi_set_handle(struct scmi_device *scmi_dev)
  179. {
  180. scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
  181. }
  182. int scmi_protocol_register(const struct scmi_protocol *proto)
  183. {
  184. int ret;
  185. if (!proto) {
  186. pr_err("invalid protocol\n");
  187. return -EINVAL;
  188. }
  189. if (!proto->init_instance) {
  190. pr_err("missing .init() for protocol 0x%x\n", proto->id);
  191. return -EINVAL;
  192. }
  193. spin_lock(&protocol_lock);
  194. ret = idr_alloc(&scmi_available_protocols, (void *)proto,
  195. proto->id, proto->id + 1, GFP_ATOMIC);
  196. spin_unlock(&protocol_lock);
  197. if (ret != proto->id) {
  198. pr_err("unable to allocate SCMI idr slot for 0x%x - err %d\n",
  199. proto->id, ret);
  200. return ret;
  201. }
  202. pr_debug("Registered SCMI Protocol 0x%x\n", proto->id);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(scmi_protocol_register);
  206. void scmi_protocol_unregister(const struct scmi_protocol *proto)
  207. {
  208. spin_lock(&protocol_lock);
  209. idr_remove(&scmi_available_protocols, proto->id);
  210. spin_unlock(&protocol_lock);
  211. pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id);
  212. return;
  213. }
  214. EXPORT_SYMBOL_GPL(scmi_protocol_unregister);
  215. static int __scmi_devices_unregister(struct device *dev, void *data)
  216. {
  217. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  218. scmi_device_destroy(scmi_dev);
  219. return 0;
  220. }
  221. static void scmi_devices_unregister(void)
  222. {
  223. bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister);
  224. }
  225. int __init scmi_bus_init(void)
  226. {
  227. int retval;
  228. retval = bus_register(&scmi_bus_type);
  229. if (retval)
  230. pr_err("scmi protocol bus register failed (%d)\n", retval);
  231. return retval;
  232. }
  233. void __exit scmi_bus_exit(void)
  234. {
  235. scmi_devices_unregister();
  236. bus_unregister(&scmi_bus_type);
  237. ida_destroy(&scmi_bus_id);
  238. }