bus.c 16 KB

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