auxiliary.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2020 Intel Corporation
  4. *
  5. * Please see Documentation/driver-api/auxiliary_bus.rst for more information.
  6. */
  7. #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
  8. #include <linux/device.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/pm_domain.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/string.h>
  15. #include <linux/auxiliary_bus.h>
  16. #include "base.h"
  17. /**
  18. * DOC: PURPOSE
  19. *
  20. * In some subsystems, the functionality of the core device (PCI/ACPI/other) is
  21. * too complex for a single device to be managed by a monolithic driver (e.g.
  22. * Sound Open Firmware), multiple devices might implement a common intersection
  23. * of functionality (e.g. NICs + RDMA), or a driver may want to export an
  24. * interface for another subsystem to drive (e.g. SIOV Physical Function export
  25. * Virtual Function management). A split of the functionality into child-
  26. * devices representing sub-domains of functionality makes it possible to
  27. * compartmentalize, layer, and distribute domain-specific concerns via a Linux
  28. * device-driver model.
  29. *
  30. * An example for this kind of requirement is the audio subsystem where a
  31. * single IP is handling multiple entities such as HDMI, Soundwire, local
  32. * devices such as mics/speakers etc. The split for the core's functionality
  33. * can be arbitrary or be defined by the DSP firmware topology and include
  34. * hooks for test/debug. This allows for the audio core device to be minimal
  35. * and focused on hardware-specific control and communication.
  36. *
  37. * Each auxiliary_device represents a part of its parent functionality. The
  38. * generic behavior can be extended and specialized as needed by encapsulating
  39. * an auxiliary_device within other domain-specific structures and the use of
  40. * .ops callbacks. Devices on the auxiliary bus do not share any structures and
  41. * the use of a communication channel with the parent is domain-specific.
  42. *
  43. * Note that ops are intended as a way to augment instance behavior within a
  44. * class of auxiliary devices, it is not the mechanism for exporting common
  45. * infrastructure from the parent. Consider EXPORT_SYMBOL_NS() to convey
  46. * infrastructure from the parent module to the auxiliary module(s).
  47. */
  48. /**
  49. * DOC: USAGE
  50. *
  51. * The auxiliary bus is to be used when a driver and one or more kernel
  52. * modules, who share a common header file with the driver, need a mechanism to
  53. * connect and provide access to a shared object allocated by the
  54. * auxiliary_device's registering driver. The registering driver for the
  55. * auxiliary_device(s) and the kernel module(s) registering auxiliary_drivers
  56. * can be from the same subsystem, or from multiple subsystems.
  57. *
  58. * The emphasis here is on a common generic interface that keeps subsystem
  59. * customization out of the bus infrastructure.
  60. *
  61. * One example is a PCI network device that is RDMA-capable and exports a child
  62. * device to be driven by an auxiliary_driver in the RDMA subsystem. The PCI
  63. * driver allocates and registers an auxiliary_device for each physical
  64. * function on the NIC. The RDMA driver registers an auxiliary_driver that
  65. * claims each of these auxiliary_devices. This conveys data/ops published by
  66. * the parent PCI device/driver to the RDMA auxiliary_driver.
  67. *
  68. * Another use case is for the PCI device to be split out into multiple sub
  69. * functions. For each sub function an auxiliary_device is created. A PCI sub
  70. * function driver binds to such devices that creates its own one or more class
  71. * devices. A PCI sub function auxiliary device is likely to be contained in a
  72. * struct with additional attributes such as user defined sub function number
  73. * and optional attributes such as resources and a link to the parent device.
  74. * These attributes could be used by systemd/udev; and hence should be
  75. * initialized before a driver binds to an auxiliary_device.
  76. *
  77. * A key requirement for utilizing the auxiliary bus is that there is no
  78. * dependency on a physical bus, device, register accesses or regmap support.
  79. * These individual devices split from the core cannot live on the platform bus
  80. * as they are not physical devices that are controlled by DT/ACPI. The same
  81. * argument applies for not using MFD in this scenario as MFD relies on
  82. * individual function devices being physical devices.
  83. */
  84. /**
  85. * DOC: EXAMPLE
  86. *
  87. * Auxiliary devices are created and registered by a subsystem-level core
  88. * device that needs to break up its functionality into smaller fragments. One
  89. * way to extend the scope of an auxiliary_device is to encapsulate it within a
  90. * domain-specific structure defined by the parent device. This structure
  91. * contains the auxiliary_device and any associated shared data/callbacks
  92. * needed to establish the connection with the parent.
  93. *
  94. * An example is:
  95. *
  96. * .. code-block:: c
  97. *
  98. * struct foo {
  99. * struct auxiliary_device auxdev;
  100. * void (*connect)(struct auxiliary_device *auxdev);
  101. * void (*disconnect)(struct auxiliary_device *auxdev);
  102. * void *data;
  103. * };
  104. *
  105. * The parent device then registers the auxiliary_device by calling
  106. * auxiliary_device_init(), and then auxiliary_device_add(), with the pointer
  107. * to the auxdev member of the above structure. The parent provides a name for
  108. * the auxiliary_device that, combined with the parent's KBUILD_MODNAME,
  109. * creates a match_name that is be used for matching and binding with a driver.
  110. *
  111. * Whenever an auxiliary_driver is registered, based on the match_name, the
  112. * auxiliary_driver's probe() is invoked for the matching devices. The
  113. * auxiliary_driver can also be encapsulated inside custom drivers that make
  114. * the core device's functionality extensible by adding additional
  115. * domain-specific ops as follows:
  116. *
  117. * .. code-block:: c
  118. *
  119. * struct my_ops {
  120. * void (*send)(struct auxiliary_device *auxdev);
  121. * void (*receive)(struct auxiliary_device *auxdev);
  122. * };
  123. *
  124. *
  125. * struct my_driver {
  126. * struct auxiliary_driver auxiliary_drv;
  127. * const struct my_ops ops;
  128. * };
  129. *
  130. * An example of this type of usage is:
  131. *
  132. * .. code-block:: c
  133. *
  134. * const struct auxiliary_device_id my_auxiliary_id_table[] = {
  135. * { .name = "foo_mod.foo_dev" },
  136. * { },
  137. * };
  138. *
  139. * const struct my_ops my_custom_ops = {
  140. * .send = my_tx,
  141. * .receive = my_rx,
  142. * };
  143. *
  144. * const struct my_driver my_drv = {
  145. * .auxiliary_drv = {
  146. * .name = "myauxiliarydrv",
  147. * .id_table = my_auxiliary_id_table,
  148. * .probe = my_probe,
  149. * .remove = my_remove,
  150. * .shutdown = my_shutdown,
  151. * },
  152. * .ops = my_custom_ops,
  153. * };
  154. *
  155. * Please note that such custom ops approach is valid, but it is hard to implement
  156. * it right without global locks per-device to protect from auxiliary_drv removal
  157. * during call to that ops. In addition, this implementation lacks proper module
  158. * dependency, which causes to load/unload races between auxiliary parent and devices
  159. * modules.
  160. *
  161. * The most easiest way to provide these ops reliably without needing to
  162. * have a lock is to EXPORT_SYMBOL*() them and rely on already existing
  163. * modules infrastructure for validity and correct dependencies chains.
  164. */
  165. static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id,
  166. const struct auxiliary_device *auxdev)
  167. {
  168. const char *auxdev_name = dev_name(&auxdev->dev);
  169. const char *p = strrchr(auxdev_name, '.');
  170. int match_size;
  171. if (!p)
  172. return NULL;
  173. match_size = p - auxdev_name;
  174. for (; id->name[0]; id++) {
  175. /* use dev_name(&auxdev->dev) prefix before last '.' char to match to */
  176. if (strlen(id->name) == match_size &&
  177. !strncmp(auxdev_name, id->name, match_size))
  178. return id;
  179. }
  180. return NULL;
  181. }
  182. static int auxiliary_match(struct device *dev, const struct device_driver *drv)
  183. {
  184. struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
  185. const struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv);
  186. return !!auxiliary_match_id(auxdrv->id_table, auxdev);
  187. }
  188. static int auxiliary_uevent(const struct device *dev, struct kobj_uevent_env *env)
  189. {
  190. const char *name, *p;
  191. name = dev_name(dev);
  192. p = strrchr(name, '.');
  193. return add_uevent_var(env, "MODALIAS=%s%.*s", AUXILIARY_MODULE_PREFIX,
  194. (int)(p - name), name);
  195. }
  196. static const struct dev_pm_ops auxiliary_dev_pm_ops = {
  197. SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL)
  198. SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
  199. };
  200. static int auxiliary_bus_probe(struct device *dev)
  201. {
  202. const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
  203. struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
  204. int ret;
  205. ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON |
  206. PD_FLAG_DETACH_POWER_OFF);
  207. if (ret) {
  208. dev_warn(dev, "Failed to attach to PM Domain : %d\n", ret);
  209. return ret;
  210. }
  211. return auxdrv->probe(auxdev, auxiliary_match_id(auxdrv->id_table, auxdev));
  212. }
  213. static void auxiliary_bus_remove(struct device *dev)
  214. {
  215. const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
  216. struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
  217. if (auxdrv->remove)
  218. auxdrv->remove(auxdev);
  219. }
  220. static void auxiliary_bus_shutdown(struct device *dev)
  221. {
  222. const struct auxiliary_driver *auxdrv = NULL;
  223. struct auxiliary_device *auxdev;
  224. if (dev->driver) {
  225. auxdrv = to_auxiliary_drv(dev->driver);
  226. auxdev = to_auxiliary_dev(dev);
  227. }
  228. if (auxdrv && auxdrv->shutdown)
  229. auxdrv->shutdown(auxdev);
  230. }
  231. static const struct bus_type auxiliary_bus_type = {
  232. .name = "auxiliary",
  233. .probe = auxiliary_bus_probe,
  234. .remove = auxiliary_bus_remove,
  235. .shutdown = auxiliary_bus_shutdown,
  236. .match = auxiliary_match,
  237. .uevent = auxiliary_uevent,
  238. .pm = &auxiliary_dev_pm_ops,
  239. };
  240. /**
  241. * auxiliary_device_init - check auxiliary_device and initialize
  242. * @auxdev: auxiliary device struct
  243. *
  244. * This is the second step in the three-step process to register an
  245. * auxiliary_device.
  246. *
  247. * When this function returns an error code, then the device_initialize will
  248. * *not* have been performed, and the caller will be responsible to free any
  249. * memory allocated for the auxiliary_device in the error path directly.
  250. *
  251. * It returns 0 on success. On success, the device_initialize has been
  252. * performed. After this point any error unwinding will need to include a call
  253. * to auxiliary_device_uninit(). In this post-initialize error scenario, a call
  254. * to the device's .release callback will be triggered, and all memory clean-up
  255. * is expected to be handled there.
  256. */
  257. int auxiliary_device_init(struct auxiliary_device *auxdev)
  258. {
  259. struct device *dev = &auxdev->dev;
  260. if (!dev->parent) {
  261. pr_err("auxiliary_device has a NULL dev->parent\n");
  262. return -EINVAL;
  263. }
  264. if (!auxdev->name) {
  265. pr_err("auxiliary_device has a NULL name\n");
  266. return -EINVAL;
  267. }
  268. dev->bus = &auxiliary_bus_type;
  269. device_initialize(&auxdev->dev);
  270. mutex_init(&auxdev->sysfs.lock);
  271. return 0;
  272. }
  273. EXPORT_SYMBOL_GPL(auxiliary_device_init);
  274. /**
  275. * __auxiliary_device_add - add an auxiliary bus device
  276. * @auxdev: auxiliary bus device to add to the bus
  277. * @modname: name of the parent device's driver module
  278. *
  279. * This is the third step in the three-step process to register an
  280. * auxiliary_device.
  281. *
  282. * This function must be called after a successful call to
  283. * auxiliary_device_init(), which will perform the device_initialize. This
  284. * means that if this returns an error code, then a call to
  285. * auxiliary_device_uninit() must be performed so that the .release callback
  286. * will be triggered to free the memory associated with the auxiliary_device.
  287. *
  288. * The expectation is that users will call the "auxiliary_device_add" macro so
  289. * that the caller's KBUILD_MODNAME is automatically inserted for the modname
  290. * parameter. Only if a user requires a custom name would this version be
  291. * called directly.
  292. */
  293. int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
  294. {
  295. struct device *dev = &auxdev->dev;
  296. int ret;
  297. if (!modname) {
  298. dev_err(dev, "auxiliary device modname is NULL\n");
  299. return -EINVAL;
  300. }
  301. ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);
  302. if (ret) {
  303. dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);
  304. return ret;
  305. }
  306. ret = device_add(dev);
  307. if (ret)
  308. dev_err(dev, "adding auxiliary device failed!: %d\n", ret);
  309. return ret;
  310. }
  311. EXPORT_SYMBOL_GPL(__auxiliary_device_add);
  312. /**
  313. * __auxiliary_driver_register - register a driver for auxiliary bus devices
  314. * @auxdrv: auxiliary_driver structure
  315. * @owner: owning module/driver
  316. * @modname: KBUILD_MODNAME for parent driver
  317. *
  318. * The expectation is that users will call the "auxiliary_driver_register"
  319. * macro so that the caller's KBUILD_MODNAME is automatically inserted for the
  320. * modname parameter. Only if a user requires a custom name would this version
  321. * be called directly.
  322. */
  323. int __auxiliary_driver_register(struct auxiliary_driver *auxdrv,
  324. struct module *owner, const char *modname)
  325. {
  326. int ret;
  327. if (WARN_ON(!auxdrv->probe) || WARN_ON(!auxdrv->id_table))
  328. return -EINVAL;
  329. if (auxdrv->name)
  330. auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s.%s", modname,
  331. auxdrv->name);
  332. else
  333. auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s", modname);
  334. if (!auxdrv->driver.name)
  335. return -ENOMEM;
  336. auxdrv->driver.owner = owner;
  337. auxdrv->driver.bus = &auxiliary_bus_type;
  338. auxdrv->driver.mod_name = modname;
  339. ret = driver_register(&auxdrv->driver);
  340. if (ret)
  341. kfree(auxdrv->driver.name);
  342. return ret;
  343. }
  344. EXPORT_SYMBOL_GPL(__auxiliary_driver_register);
  345. /**
  346. * auxiliary_driver_unregister - unregister a driver
  347. * @auxdrv: auxiliary_driver structure
  348. */
  349. void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv)
  350. {
  351. driver_unregister(&auxdrv->driver);
  352. kfree(auxdrv->driver.name);
  353. }
  354. EXPORT_SYMBOL_GPL(auxiliary_driver_unregister);
  355. static void auxiliary_device_release(struct device *dev)
  356. {
  357. struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
  358. of_node_put(dev->of_node);
  359. kfree(auxdev);
  360. }
  361. /**
  362. * auxiliary_device_create - create a device on the auxiliary bus
  363. * @dev: parent device
  364. * @modname: module name used to create the auxiliary driver name.
  365. * @devname: auxiliary bus device name
  366. * @platform_data: auxiliary bus device platform data
  367. * @id: auxiliary bus device id
  368. *
  369. * Helper to create an auxiliary bus device.
  370. * The device created matches driver 'modname.devname' on the auxiliary bus.
  371. */
  372. struct auxiliary_device *auxiliary_device_create(struct device *dev,
  373. const char *modname,
  374. const char *devname,
  375. void *platform_data,
  376. int id)
  377. {
  378. struct auxiliary_device *auxdev;
  379. int ret;
  380. auxdev = kzalloc_obj(*auxdev);
  381. if (!auxdev)
  382. return NULL;
  383. auxdev->id = id;
  384. auxdev->name = devname;
  385. auxdev->dev.parent = dev;
  386. auxdev->dev.platform_data = platform_data;
  387. auxdev->dev.release = auxiliary_device_release;
  388. device_set_of_node_from_dev(&auxdev->dev, dev);
  389. ret = auxiliary_device_init(auxdev);
  390. if (ret) {
  391. of_node_put(auxdev->dev.of_node);
  392. kfree(auxdev);
  393. return NULL;
  394. }
  395. ret = __auxiliary_device_add(auxdev, modname);
  396. if (ret) {
  397. /*
  398. * It may look odd but auxdev should not be freed here.
  399. * auxiliary_device_uninit() calls device_put() which call
  400. * the device release function, freeing auxdev.
  401. */
  402. auxiliary_device_uninit(auxdev);
  403. return NULL;
  404. }
  405. return auxdev;
  406. }
  407. EXPORT_SYMBOL_GPL(auxiliary_device_create);
  408. /**
  409. * auxiliary_device_destroy - remove an auxiliary device
  410. * @auxdev: pointer to the auxdev to be removed
  411. *
  412. * Helper to remove an auxiliary device created with
  413. * auxiliary_device_create()
  414. */
  415. void auxiliary_device_destroy(void *auxdev)
  416. {
  417. struct auxiliary_device *_auxdev = auxdev;
  418. auxiliary_device_delete(_auxdev);
  419. auxiliary_device_uninit(_auxdev);
  420. }
  421. EXPORT_SYMBOL_GPL(auxiliary_device_destroy);
  422. /**
  423. * __devm_auxiliary_device_create - create a managed device on the auxiliary bus
  424. * @dev: parent device
  425. * @modname: module name used to create the auxiliary driver name.
  426. * @devname: auxiliary bus device name
  427. * @platform_data: auxiliary bus device platform data
  428. * @id: auxiliary bus device id
  429. *
  430. * Device managed helper to create an auxiliary bus device.
  431. * The device created matches driver 'modname.devname' on the auxiliary bus.
  432. */
  433. struct auxiliary_device *__devm_auxiliary_device_create(struct device *dev,
  434. const char *modname,
  435. const char *devname,
  436. void *platform_data,
  437. int id)
  438. {
  439. struct auxiliary_device *auxdev;
  440. int ret;
  441. auxdev = auxiliary_device_create(dev, modname, devname, platform_data, id);
  442. if (!auxdev)
  443. return NULL;
  444. ret = devm_add_action_or_reset(dev, auxiliary_device_destroy,
  445. auxdev);
  446. if (ret)
  447. return NULL;
  448. return auxdev;
  449. }
  450. EXPORT_SYMBOL_GPL(__devm_auxiliary_device_create);
  451. void __init auxiliary_bus_init(void)
  452. {
  453. WARN_ON(bus_register(&auxiliary_bus_type));
  454. }