bus.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 ARM Ltd.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/arm_ffa.h>
  7. #include <linux/device.h>
  8. #include <linux/fs.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include "common.h"
  14. #define FFA_UEVENT_MODALIAS_FMT "arm_ffa:%04x:%pUb"
  15. static DEFINE_IDA(ffa_bus_id);
  16. static int ffa_device_match(struct device *dev, const struct device_driver *drv)
  17. {
  18. const struct ffa_device_id *id_table;
  19. struct ffa_device *ffa_dev;
  20. id_table = to_ffa_driver(drv)->id_table;
  21. ffa_dev = to_ffa_dev(dev);
  22. while (!uuid_is_null(&id_table->uuid)) {
  23. /*
  24. * FF-A v1.0 doesn't provide discovery of UUIDs, just the
  25. * partition IDs, so match it unconditionally here and handle
  26. * it via the installed bus notifier during driver binding.
  27. */
  28. if (uuid_is_null(&ffa_dev->uuid))
  29. return 1;
  30. if (uuid_equal(&ffa_dev->uuid, &id_table->uuid))
  31. return 1;
  32. id_table++;
  33. }
  34. return 0;
  35. }
  36. static int ffa_device_probe(struct device *dev)
  37. {
  38. struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
  39. struct ffa_device *ffa_dev = to_ffa_dev(dev);
  40. /* UUID can be still NULL with FF-A v1.0, so just skip probing them */
  41. if (uuid_is_null(&ffa_dev->uuid))
  42. return -ENODEV;
  43. return ffa_drv->probe(ffa_dev);
  44. }
  45. static void ffa_device_remove(struct device *dev)
  46. {
  47. struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
  48. if (ffa_drv->remove)
  49. ffa_drv->remove(to_ffa_dev(dev));
  50. }
  51. static int ffa_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
  52. {
  53. const struct ffa_device *ffa_dev = to_ffa_dev(dev);
  54. return add_uevent_var(env, "MODALIAS=" FFA_UEVENT_MODALIAS_FMT,
  55. ffa_dev->vm_id, &ffa_dev->uuid);
  56. }
  57. static ssize_t modalias_show(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct ffa_device *ffa_dev = to_ffa_dev(dev);
  61. return sysfs_emit(buf, FFA_UEVENT_MODALIAS_FMT, ffa_dev->vm_id,
  62. &ffa_dev->uuid);
  63. }
  64. static DEVICE_ATTR_RO(modalias);
  65. static ssize_t partition_id_show(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. struct ffa_device *ffa_dev = to_ffa_dev(dev);
  69. return sprintf(buf, "0x%04x\n", ffa_dev->vm_id);
  70. }
  71. static DEVICE_ATTR_RO(partition_id);
  72. static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
  73. char *buf)
  74. {
  75. struct ffa_device *ffa_dev = to_ffa_dev(dev);
  76. return sprintf(buf, "%pUb\n", &ffa_dev->uuid);
  77. }
  78. static DEVICE_ATTR_RO(uuid);
  79. static struct attribute *ffa_device_attributes_attrs[] = {
  80. &dev_attr_partition_id.attr,
  81. &dev_attr_uuid.attr,
  82. &dev_attr_modalias.attr,
  83. NULL,
  84. };
  85. ATTRIBUTE_GROUPS(ffa_device_attributes);
  86. const struct bus_type ffa_bus_type = {
  87. .name = "arm_ffa",
  88. .match = ffa_device_match,
  89. .probe = ffa_device_probe,
  90. .remove = ffa_device_remove,
  91. .uevent = ffa_device_uevent,
  92. .dev_groups = ffa_device_attributes_groups,
  93. };
  94. EXPORT_SYMBOL_GPL(ffa_bus_type);
  95. int ffa_driver_register(struct ffa_driver *driver, struct module *owner,
  96. const char *mod_name)
  97. {
  98. int ret;
  99. if (!driver->probe)
  100. return -EINVAL;
  101. driver->driver.bus = &ffa_bus_type;
  102. driver->driver.name = driver->name;
  103. driver->driver.owner = owner;
  104. driver->driver.mod_name = mod_name;
  105. ret = driver_register(&driver->driver);
  106. if (!ret)
  107. pr_debug("registered new ffa driver %s\n", driver->name);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL_GPL(ffa_driver_register);
  111. void ffa_driver_unregister(struct ffa_driver *driver)
  112. {
  113. driver_unregister(&driver->driver);
  114. }
  115. EXPORT_SYMBOL_GPL(ffa_driver_unregister);
  116. static void ffa_release_device(struct device *dev)
  117. {
  118. struct ffa_device *ffa_dev = to_ffa_dev(dev);
  119. ida_free(&ffa_bus_id, ffa_dev->id);
  120. kfree(ffa_dev);
  121. }
  122. static int __ffa_devices_unregister(struct device *dev, void *data)
  123. {
  124. device_unregister(dev);
  125. return 0;
  126. }
  127. void ffa_devices_unregister(void)
  128. {
  129. bus_for_each_dev(&ffa_bus_type, NULL, NULL,
  130. __ffa_devices_unregister);
  131. }
  132. EXPORT_SYMBOL_GPL(ffa_devices_unregister);
  133. bool ffa_device_is_valid(struct ffa_device *ffa_dev)
  134. {
  135. bool valid = false;
  136. struct device *dev = NULL;
  137. struct ffa_device *tmp_dev;
  138. do {
  139. dev = bus_find_next_device(&ffa_bus_type, dev);
  140. tmp_dev = to_ffa_dev(dev);
  141. if (tmp_dev == ffa_dev) {
  142. valid = true;
  143. break;
  144. }
  145. put_device(dev);
  146. } while (dev);
  147. put_device(dev);
  148. return valid;
  149. }
  150. struct ffa_device *
  151. ffa_device_register(const struct ffa_partition_info *part_info,
  152. const struct ffa_ops *ops)
  153. {
  154. int id, ret;
  155. struct device *dev;
  156. struct ffa_device *ffa_dev;
  157. if (!part_info)
  158. return NULL;
  159. id = ida_alloc_min(&ffa_bus_id, 1, GFP_KERNEL);
  160. if (id < 0)
  161. return NULL;
  162. ffa_dev = kzalloc_obj(*ffa_dev);
  163. if (!ffa_dev) {
  164. ida_free(&ffa_bus_id, id);
  165. return NULL;
  166. }
  167. dev = &ffa_dev->dev;
  168. dev->bus = &ffa_bus_type;
  169. dev->release = ffa_release_device;
  170. dev->dma_mask = &dev->coherent_dma_mask;
  171. dev_set_name(&ffa_dev->dev, "arm-ffa-%d", id);
  172. ffa_dev->id = id;
  173. ffa_dev->vm_id = part_info->id;
  174. ffa_dev->properties = part_info->properties;
  175. ffa_dev->ops = ops;
  176. uuid_copy(&ffa_dev->uuid, &part_info->uuid);
  177. ret = device_register(&ffa_dev->dev);
  178. if (ret) {
  179. dev_err(dev, "unable to register device %s err=%d\n",
  180. dev_name(dev), ret);
  181. put_device(dev);
  182. return NULL;
  183. }
  184. return ffa_dev;
  185. }
  186. EXPORT_SYMBOL_GPL(ffa_device_register);
  187. void ffa_device_unregister(struct ffa_device *ffa_dev)
  188. {
  189. if (!ffa_dev)
  190. return;
  191. device_unregister(&ffa_dev->dev);
  192. }
  193. EXPORT_SYMBOL_GPL(ffa_device_unregister);
  194. static int __init arm_ffa_bus_init(void)
  195. {
  196. return bus_register(&ffa_bus_type);
  197. }
  198. subsys_initcall(arm_ffa_bus_init);
  199. static void __exit arm_ffa_bus_exit(void)
  200. {
  201. ffa_devices_unregister();
  202. bus_unregister(&ffa_bus_type);
  203. ida_destroy(&ffa_bus_id);
  204. }
  205. module_exit(arm_ffa_bus_exit);
  206. MODULE_ALIAS("ffa-core");
  207. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  208. MODULE_DESCRIPTION("ARM FF-A bus");
  209. MODULE_LICENSE("GPL");