bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Message Protocol bus layer
  4. *
  5. * Copyright (C) 2018-2021 ARM Ltd.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/atomic.h>
  9. #include <linux/types.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. #include "common.h"
  16. BLOCKING_NOTIFIER_HEAD(scmi_requested_devices_nh);
  17. EXPORT_SYMBOL_GPL(scmi_requested_devices_nh);
  18. static DEFINE_IDA(scmi_bus_id);
  19. static DEFINE_IDR(scmi_requested_devices);
  20. /* Protect access to scmi_requested_devices */
  21. static DEFINE_MUTEX(scmi_requested_devices_mtx);
  22. struct scmi_requested_dev {
  23. const struct scmi_device_id *id_table;
  24. struct list_head node;
  25. };
  26. /* Track globally the creation of SCMI SystemPower related devices */
  27. static atomic_t scmi_syspower_registered = ATOMIC_INIT(0);
  28. /**
  29. * scmi_protocol_device_request - Helper to request a device
  30. *
  31. * @id_table: A protocol/name pair descriptor for the device to be created.
  32. *
  33. * This helper let an SCMI driver request specific devices identified by the
  34. * @id_table to be created for each active SCMI instance.
  35. *
  36. * The requested device name MUST NOT be already existent for this protocol;
  37. * at first the freshly requested @id_table is annotated in the IDR table
  38. * @scmi_requested_devices and then the requested device is advertised to any
  39. * registered party via the @scmi_requested_devices_nh notification chain.
  40. *
  41. * Return: 0 on Success
  42. */
  43. static int scmi_protocol_device_request(const struct scmi_device_id *id_table)
  44. {
  45. int ret = 0;
  46. struct list_head *head, *phead = NULL;
  47. struct scmi_requested_dev *rdev;
  48. pr_debug("Requesting SCMI device (%s) for protocol %x\n",
  49. id_table->name, id_table->protocol_id);
  50. if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT) &&
  51. !IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX)) {
  52. pr_warn("SCMI Raw mode active. Rejecting '%s'/0x%02X\n",
  53. id_table->name, id_table->protocol_id);
  54. return -EINVAL;
  55. }
  56. /*
  57. * Find the matching protocol rdev list and then search of any
  58. * existent equally named device...fails if any duplicate found.
  59. */
  60. mutex_lock(&scmi_requested_devices_mtx);
  61. phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
  62. if (phead) {
  63. head = phead;
  64. list_for_each_entry(rdev, head, node) {
  65. if (!strcmp(rdev->id_table->name, id_table->name)) {
  66. pr_err("Ignoring duplicate request [%d] %s\n",
  67. rdev->id_table->protocol_id,
  68. rdev->id_table->name);
  69. ret = -EINVAL;
  70. goto out;
  71. }
  72. }
  73. }
  74. /*
  75. * No duplicate found for requested id_table, so let's create a new
  76. * requested device entry for this new valid request.
  77. */
  78. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  79. if (!rdev) {
  80. ret = -ENOMEM;
  81. goto out;
  82. }
  83. rdev->id_table = id_table;
  84. /*
  85. * Append the new requested device table descriptor to the head of the
  86. * related protocol list, eventually creating such head if not already
  87. * there.
  88. */
  89. if (!phead) {
  90. phead = kzalloc(sizeof(*phead), GFP_KERNEL);
  91. if (!phead) {
  92. kfree(rdev);
  93. ret = -ENOMEM;
  94. goto out;
  95. }
  96. INIT_LIST_HEAD(phead);
  97. ret = idr_alloc(&scmi_requested_devices, (void *)phead,
  98. id_table->protocol_id,
  99. id_table->protocol_id + 1, GFP_KERNEL);
  100. if (ret != id_table->protocol_id) {
  101. pr_err("Failed to save SCMI device - ret:%d\n", ret);
  102. kfree(rdev);
  103. kfree(phead);
  104. ret = -EINVAL;
  105. goto out;
  106. }
  107. ret = 0;
  108. }
  109. list_add(&rdev->node, phead);
  110. out:
  111. mutex_unlock(&scmi_requested_devices_mtx);
  112. if (!ret)
  113. blocking_notifier_call_chain(&scmi_requested_devices_nh,
  114. SCMI_BUS_NOTIFY_DEVICE_REQUEST,
  115. (void *)rdev->id_table);
  116. return ret;
  117. }
  118. static int scmi_protocol_table_register(const struct scmi_device_id *id_table)
  119. {
  120. int ret = 0;
  121. const struct scmi_device_id *entry;
  122. for (entry = id_table; entry->name && ret == 0; entry++)
  123. ret = scmi_protocol_device_request(entry);
  124. return ret;
  125. }
  126. /**
  127. * scmi_protocol_device_unrequest - Helper to unrequest a device
  128. *
  129. * @id_table: A protocol/name pair descriptor for the device to be unrequested.
  130. *
  131. * The unrequested device, described by the provided id_table, is at first
  132. * removed from the IDR @scmi_requested_devices and then the removal is
  133. * advertised to any registered party via the @scmi_requested_devices_nh
  134. * notification chain.
  135. */
  136. static void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
  137. {
  138. struct list_head *phead;
  139. pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
  140. id_table->name, id_table->protocol_id);
  141. mutex_lock(&scmi_requested_devices_mtx);
  142. phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
  143. if (phead) {
  144. struct scmi_requested_dev *victim, *tmp;
  145. list_for_each_entry_safe(victim, tmp, phead, node) {
  146. if (!strcmp(victim->id_table->name, id_table->name)) {
  147. list_del(&victim->node);
  148. mutex_unlock(&scmi_requested_devices_mtx);
  149. blocking_notifier_call_chain(&scmi_requested_devices_nh,
  150. SCMI_BUS_NOTIFY_DEVICE_UNREQUEST,
  151. (void *)victim->id_table);
  152. kfree(victim);
  153. mutex_lock(&scmi_requested_devices_mtx);
  154. break;
  155. }
  156. }
  157. if (list_empty(phead)) {
  158. idr_remove(&scmi_requested_devices,
  159. id_table->protocol_id);
  160. kfree(phead);
  161. }
  162. }
  163. mutex_unlock(&scmi_requested_devices_mtx);
  164. }
  165. static void
  166. scmi_protocol_table_unregister(const struct scmi_device_id *id_table)
  167. {
  168. const struct scmi_device_id *entry;
  169. for (entry = id_table; entry->name; entry++)
  170. scmi_protocol_device_unrequest(entry);
  171. }
  172. static const struct scmi_device_id *
  173. scmi_dev_match_id(struct scmi_device *scmi_dev, const struct scmi_driver *scmi_drv)
  174. {
  175. const struct scmi_device_id *id = scmi_drv->id_table;
  176. if (!id)
  177. return NULL;
  178. for (; id->protocol_id; id++)
  179. if (id->protocol_id == scmi_dev->protocol_id) {
  180. if (!id->name)
  181. return id;
  182. else if (!strcmp(id->name, scmi_dev->name))
  183. return id;
  184. }
  185. return NULL;
  186. }
  187. static int scmi_dev_match(struct device *dev, const struct device_driver *drv)
  188. {
  189. const struct scmi_driver *scmi_drv = to_scmi_driver(drv);
  190. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  191. const struct scmi_device_id *id;
  192. id = scmi_dev_match_id(scmi_dev, scmi_drv);
  193. if (id)
  194. return 1;
  195. return 0;
  196. }
  197. static int scmi_match_by_id_table(struct device *dev, void *data)
  198. {
  199. struct scmi_device *sdev = to_scmi_dev(dev);
  200. struct scmi_device_id *id_table = data;
  201. return sdev->protocol_id == id_table->protocol_id &&
  202. (id_table->name && !strcmp(sdev->name, id_table->name));
  203. }
  204. static struct scmi_device *scmi_child_dev_find(struct device *parent,
  205. int prot_id, const char *name)
  206. {
  207. struct scmi_device_id id_table;
  208. struct device *dev;
  209. id_table.protocol_id = prot_id;
  210. id_table.name = name;
  211. dev = device_find_child(parent, &id_table, scmi_match_by_id_table);
  212. if (!dev)
  213. return NULL;
  214. /* Drop the refcnt bumped implicitly by device_find_child */
  215. put_device(dev);
  216. return to_scmi_dev(dev);
  217. }
  218. static int scmi_dev_probe(struct device *dev)
  219. {
  220. struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
  221. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  222. if (!scmi_dev->handle)
  223. return -EPROBE_DEFER;
  224. return scmi_drv->probe(scmi_dev);
  225. }
  226. static void scmi_dev_remove(struct device *dev)
  227. {
  228. struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
  229. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  230. if (scmi_drv->remove)
  231. scmi_drv->remove(scmi_dev);
  232. }
  233. const struct bus_type scmi_bus_type = {
  234. .name = "scmi_protocol",
  235. .match = scmi_dev_match,
  236. .probe = scmi_dev_probe,
  237. .remove = scmi_dev_remove,
  238. };
  239. EXPORT_SYMBOL_GPL(scmi_bus_type);
  240. int scmi_driver_register(struct scmi_driver *driver, struct module *owner,
  241. const char *mod_name)
  242. {
  243. int retval;
  244. if (!driver->probe)
  245. return -EINVAL;
  246. retval = scmi_protocol_table_register(driver->id_table);
  247. if (retval)
  248. return retval;
  249. driver->driver.bus = &scmi_bus_type;
  250. driver->driver.name = driver->name;
  251. driver->driver.owner = owner;
  252. driver->driver.mod_name = mod_name;
  253. retval = driver_register(&driver->driver);
  254. if (!retval)
  255. pr_debug("Registered new scmi driver %s\n", driver->name);
  256. return retval;
  257. }
  258. EXPORT_SYMBOL_GPL(scmi_driver_register);
  259. void scmi_driver_unregister(struct scmi_driver *driver)
  260. {
  261. driver_unregister(&driver->driver);
  262. scmi_protocol_table_unregister(driver->id_table);
  263. }
  264. EXPORT_SYMBOL_GPL(scmi_driver_unregister);
  265. static void scmi_device_release(struct device *dev)
  266. {
  267. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  268. kfree_const(scmi_dev->name);
  269. kfree(scmi_dev);
  270. }
  271. static void __scmi_device_destroy(struct scmi_device *scmi_dev)
  272. {
  273. pr_debug("(%s) Destroying SCMI device '%s' for protocol 0x%x (%s)\n",
  274. of_node_full_name(scmi_dev->dev.parent->of_node),
  275. dev_name(&scmi_dev->dev), scmi_dev->protocol_id,
  276. scmi_dev->name);
  277. if (scmi_dev->protocol_id == SCMI_PROTOCOL_SYSTEM)
  278. atomic_set(&scmi_syspower_registered, 0);
  279. ida_free(&scmi_bus_id, scmi_dev->id);
  280. device_unregister(&scmi_dev->dev);
  281. }
  282. static struct scmi_device *
  283. __scmi_device_create(struct device_node *np, struct device *parent,
  284. int protocol, const char *name)
  285. {
  286. int id, retval;
  287. struct scmi_device *scmi_dev;
  288. /*
  289. * If the same protocol/name device already exist under the same parent
  290. * (i.e. SCMI instance) just return the existent device.
  291. * This avoids any race between the SCMI driver, creating devices for
  292. * each DT defined protocol at probe time, and the concurrent
  293. * registration of SCMI drivers.
  294. */
  295. scmi_dev = scmi_child_dev_find(parent, protocol, name);
  296. if (scmi_dev)
  297. return scmi_dev;
  298. /*
  299. * Ignore any possible subsequent failures while creating the device
  300. * since we are doomed anyway at that point; not using a mutex which
  301. * spans across this whole function to keep things simple and to avoid
  302. * to serialize all the __scmi_device_create calls across possibly
  303. * different SCMI server instances (parent)
  304. */
  305. if (protocol == SCMI_PROTOCOL_SYSTEM &&
  306. atomic_cmpxchg(&scmi_syspower_registered, 0, 1)) {
  307. dev_warn(parent,
  308. "SCMI SystemPower protocol device must be unique !\n");
  309. return NULL;
  310. }
  311. scmi_dev = kzalloc(sizeof(*scmi_dev), GFP_KERNEL);
  312. if (!scmi_dev)
  313. return NULL;
  314. scmi_dev->name = kstrdup_const(name ?: "unknown", GFP_KERNEL);
  315. if (!scmi_dev->name) {
  316. kfree(scmi_dev);
  317. return NULL;
  318. }
  319. id = ida_alloc_min(&scmi_bus_id, 1, GFP_KERNEL);
  320. if (id < 0) {
  321. kfree_const(scmi_dev->name);
  322. kfree(scmi_dev);
  323. return NULL;
  324. }
  325. scmi_dev->id = id;
  326. scmi_dev->protocol_id = protocol;
  327. scmi_dev->dev.parent = parent;
  328. device_set_node(&scmi_dev->dev, of_fwnode_handle(np));
  329. scmi_dev->dev.bus = &scmi_bus_type;
  330. scmi_dev->dev.release = scmi_device_release;
  331. dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id);
  332. retval = device_register(&scmi_dev->dev);
  333. if (retval)
  334. goto put_dev;
  335. pr_debug("(%s) Created SCMI device '%s' for protocol 0x%x (%s)\n",
  336. of_node_full_name(parent->of_node),
  337. dev_name(&scmi_dev->dev), protocol, name);
  338. return scmi_dev;
  339. put_dev:
  340. put_device(&scmi_dev->dev);
  341. ida_free(&scmi_bus_id, id);
  342. return NULL;
  343. }
  344. /**
  345. * scmi_device_create - A method to create one or more SCMI devices
  346. *
  347. * @np: A reference to the device node to use for the new device(s)
  348. * @parent: The parent device to use identifying a specific SCMI instance
  349. * @protocol: The SCMI protocol to be associated with this device
  350. * @name: The requested-name of the device to be created; this is optional
  351. * and if no @name is provided, all the devices currently known to
  352. * be requested on the SCMI bus for @protocol will be created.
  353. *
  354. * This method can be invoked to create a single well-defined device (like
  355. * a transport device or a device requested by an SCMI driver loaded after
  356. * the core SCMI stack has been probed), or to create all the devices currently
  357. * known to have been requested by the loaded SCMI drivers for a specific
  358. * protocol (typically during SCMI core protocol enumeration at probe time).
  359. *
  360. * Return: The created device (or one of them if @name was NOT provided and
  361. * multiple devices were created) or NULL if no device was created;
  362. * note that NULL indicates an error ONLY in case a specific @name
  363. * was provided: when @name param was not provided, a number of devices
  364. * could have been potentially created for a whole protocol, unless no
  365. * device was found to have been requested for that specific protocol.
  366. */
  367. struct scmi_device *scmi_device_create(struct device_node *np,
  368. struct device *parent, int protocol,
  369. const char *name)
  370. {
  371. struct list_head *phead;
  372. struct scmi_requested_dev *rdev;
  373. struct scmi_device *scmi_dev = NULL;
  374. if (name)
  375. return __scmi_device_create(np, parent, protocol, name);
  376. mutex_lock(&scmi_requested_devices_mtx);
  377. phead = idr_find(&scmi_requested_devices, protocol);
  378. /* Nothing to do. */
  379. if (!phead) {
  380. mutex_unlock(&scmi_requested_devices_mtx);
  381. return NULL;
  382. }
  383. /* Walk the list of requested devices for protocol and create them */
  384. list_for_each_entry(rdev, phead, node) {
  385. struct scmi_device *sdev;
  386. sdev = __scmi_device_create(np, parent,
  387. rdev->id_table->protocol_id,
  388. rdev->id_table->name);
  389. /* Report errors and carry on... */
  390. if (sdev)
  391. scmi_dev = sdev;
  392. else
  393. pr_err("(%s) Failed to create device for protocol 0x%x (%s)\n",
  394. of_node_full_name(parent->of_node),
  395. rdev->id_table->protocol_id,
  396. rdev->id_table->name);
  397. }
  398. mutex_unlock(&scmi_requested_devices_mtx);
  399. return scmi_dev;
  400. }
  401. EXPORT_SYMBOL_GPL(scmi_device_create);
  402. void scmi_device_destroy(struct device *parent, int protocol, const char *name)
  403. {
  404. struct scmi_device *scmi_dev;
  405. scmi_dev = scmi_child_dev_find(parent, protocol, name);
  406. if (scmi_dev)
  407. __scmi_device_destroy(scmi_dev);
  408. }
  409. EXPORT_SYMBOL_GPL(scmi_device_destroy);
  410. static int __scmi_devices_unregister(struct device *dev, void *data)
  411. {
  412. struct scmi_device *scmi_dev = to_scmi_dev(dev);
  413. __scmi_device_destroy(scmi_dev);
  414. return 0;
  415. }
  416. static void scmi_devices_unregister(void)
  417. {
  418. bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister);
  419. }
  420. static int __init scmi_bus_init(void)
  421. {
  422. int retval;
  423. retval = bus_register(&scmi_bus_type);
  424. if (retval)
  425. pr_err("SCMI protocol bus register failed (%d)\n", retval);
  426. pr_info("SCMI protocol bus registered\n");
  427. return retval;
  428. }
  429. subsys_initcall(scmi_bus_init);
  430. static void __exit scmi_bus_exit(void)
  431. {
  432. /*
  433. * Destroy all remaining devices: just in case the drivers were
  434. * manually unbound and at first and then the modules unloaded.
  435. */
  436. scmi_devices_unregister();
  437. bus_unregister(&scmi_bus_type);
  438. ida_destroy(&scmi_bus_id);
  439. }
  440. module_exit(scmi_bus_exit);
  441. MODULE_ALIAS("scmi-core");
  442. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  443. MODULE_DESCRIPTION("ARM SCMI protocol bus");
  444. MODULE_LICENSE("GPL");