apr.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2018, Linaro Limited
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/device.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/idr.h>
  9. #include <linux/slab.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/of_device.h>
  12. #include <linux/soc/qcom/apr.h>
  13. #include <linux/rpmsg.h>
  14. #include <linux/of.h>
  15. struct apr {
  16. struct rpmsg_endpoint *ch;
  17. struct device *dev;
  18. spinlock_t svcs_lock;
  19. spinlock_t rx_lock;
  20. struct idr svcs_idr;
  21. int dest_domain_id;
  22. struct workqueue_struct *rxwq;
  23. struct work_struct rx_work;
  24. struct list_head rx_list;
  25. };
  26. struct apr_rx_buf {
  27. struct list_head node;
  28. int len;
  29. uint8_t buf[];
  30. };
  31. /**
  32. * apr_send_pkt() - Send a apr message from apr device
  33. *
  34. * @adev: Pointer to previously registered apr device.
  35. * @pkt: Pointer to apr packet to send
  36. *
  37. * Return: Will be an negative on packet size on success.
  38. */
  39. int apr_send_pkt(struct apr_device *adev, struct apr_pkt *pkt)
  40. {
  41. struct apr *apr = dev_get_drvdata(adev->dev.parent);
  42. struct apr_hdr *hdr;
  43. unsigned long flags;
  44. int ret;
  45. spin_lock_irqsave(&adev->lock, flags);
  46. hdr = &pkt->hdr;
  47. hdr->src_domain = APR_DOMAIN_APPS;
  48. hdr->src_svc = adev->svc_id;
  49. hdr->dest_domain = adev->domain_id;
  50. hdr->dest_svc = adev->svc_id;
  51. ret = rpmsg_trysend(apr->ch, pkt, hdr->pkt_size);
  52. spin_unlock_irqrestore(&adev->lock, flags);
  53. return ret ? ret : hdr->pkt_size;
  54. }
  55. EXPORT_SYMBOL_GPL(apr_send_pkt);
  56. static void apr_dev_release(struct device *dev)
  57. {
  58. struct apr_device *adev = to_apr_device(dev);
  59. kfree(adev);
  60. }
  61. static int apr_callback(struct rpmsg_device *rpdev, void *buf,
  62. int len, void *priv, u32 addr)
  63. {
  64. struct apr *apr = dev_get_drvdata(&rpdev->dev);
  65. struct apr_rx_buf *abuf;
  66. unsigned long flags;
  67. if (len <= APR_HDR_SIZE) {
  68. dev_err(apr->dev, "APR: Improper apr pkt received:%p %d\n",
  69. buf, len);
  70. return -EINVAL;
  71. }
  72. abuf = kzalloc(sizeof(*abuf) + len, GFP_ATOMIC);
  73. if (!abuf)
  74. return -ENOMEM;
  75. abuf->len = len;
  76. memcpy(abuf->buf, buf, len);
  77. spin_lock_irqsave(&apr->rx_lock, flags);
  78. list_add_tail(&abuf->node, &apr->rx_list);
  79. spin_unlock_irqrestore(&apr->rx_lock, flags);
  80. queue_work(apr->rxwq, &apr->rx_work);
  81. return 0;
  82. }
  83. static int apr_do_rx_callback(struct apr *apr, struct apr_rx_buf *abuf)
  84. {
  85. uint16_t hdr_size, msg_type, ver, svc_id;
  86. struct apr_device *svc = NULL;
  87. struct apr_driver *adrv = NULL;
  88. struct apr_resp_pkt resp;
  89. struct apr_hdr *hdr;
  90. unsigned long flags;
  91. void *buf = abuf->buf;
  92. int len = abuf->len;
  93. hdr = buf;
  94. ver = APR_HDR_FIELD_VER(hdr->hdr_field);
  95. if (ver > APR_PKT_VER + 1)
  96. return -EINVAL;
  97. hdr_size = APR_HDR_FIELD_SIZE_BYTES(hdr->hdr_field);
  98. if (hdr_size < APR_HDR_SIZE) {
  99. dev_err(apr->dev, "APR: Wrong hdr size:%d\n", hdr_size);
  100. return -EINVAL;
  101. }
  102. if (hdr->pkt_size < APR_HDR_SIZE || hdr->pkt_size != len) {
  103. dev_err(apr->dev, "APR: Wrong packet size\n");
  104. return -EINVAL;
  105. }
  106. msg_type = APR_HDR_FIELD_MT(hdr->hdr_field);
  107. if (msg_type >= APR_MSG_TYPE_MAX) {
  108. dev_err(apr->dev, "APR: Wrong message type: %d\n", msg_type);
  109. return -EINVAL;
  110. }
  111. if (hdr->src_domain >= APR_DOMAIN_MAX ||
  112. hdr->dest_domain >= APR_DOMAIN_MAX ||
  113. hdr->src_svc >= APR_SVC_MAX ||
  114. hdr->dest_svc >= APR_SVC_MAX) {
  115. dev_err(apr->dev, "APR: Wrong APR header\n");
  116. return -EINVAL;
  117. }
  118. svc_id = hdr->dest_svc;
  119. spin_lock_irqsave(&apr->svcs_lock, flags);
  120. svc = idr_find(&apr->svcs_idr, svc_id);
  121. if (svc && svc->dev.driver)
  122. adrv = to_apr_driver(svc->dev.driver);
  123. spin_unlock_irqrestore(&apr->svcs_lock, flags);
  124. if (!adrv) {
  125. dev_err(apr->dev, "APR: service is not registered\n");
  126. return -EINVAL;
  127. }
  128. resp.hdr = *hdr;
  129. resp.payload_size = hdr->pkt_size - hdr_size;
  130. /*
  131. * NOTE: hdr_size is not same as APR_HDR_SIZE as remote can include
  132. * optional headers in to apr_hdr which should be ignored
  133. */
  134. if (resp.payload_size > 0)
  135. resp.payload = buf + hdr_size;
  136. adrv->callback(svc, &resp);
  137. return 0;
  138. }
  139. static void apr_rxwq(struct work_struct *work)
  140. {
  141. struct apr *apr = container_of(work, struct apr, rx_work);
  142. struct apr_rx_buf *abuf, *b;
  143. unsigned long flags;
  144. if (!list_empty(&apr->rx_list)) {
  145. list_for_each_entry_safe(abuf, b, &apr->rx_list, node) {
  146. apr_do_rx_callback(apr, abuf);
  147. spin_lock_irqsave(&apr->rx_lock, flags);
  148. list_del(&abuf->node);
  149. spin_unlock_irqrestore(&apr->rx_lock, flags);
  150. kfree(abuf);
  151. }
  152. }
  153. }
  154. static int apr_device_match(struct device *dev, struct device_driver *drv)
  155. {
  156. struct apr_device *adev = to_apr_device(dev);
  157. struct apr_driver *adrv = to_apr_driver(drv);
  158. const struct apr_device_id *id = adrv->id_table;
  159. /* Attempt an OF style match first */
  160. if (of_driver_match_device(dev, drv))
  161. return 1;
  162. if (!id)
  163. return 0;
  164. while (id->domain_id != 0 || id->svc_id != 0) {
  165. if (id->domain_id == adev->domain_id &&
  166. id->svc_id == adev->svc_id)
  167. return 1;
  168. id++;
  169. }
  170. return 0;
  171. }
  172. static int apr_device_probe(struct device *dev)
  173. {
  174. struct apr_device *adev = to_apr_device(dev);
  175. struct apr_driver *adrv = to_apr_driver(dev->driver);
  176. return adrv->probe(adev);
  177. }
  178. static int apr_device_remove(struct device *dev)
  179. {
  180. struct apr_device *adev = to_apr_device(dev);
  181. struct apr_driver *adrv;
  182. struct apr *apr = dev_get_drvdata(adev->dev.parent);
  183. if (dev->driver) {
  184. adrv = to_apr_driver(dev->driver);
  185. if (adrv->remove)
  186. adrv->remove(adev);
  187. spin_lock(&apr->svcs_lock);
  188. idr_remove(&apr->svcs_idr, adev->svc_id);
  189. spin_unlock(&apr->svcs_lock);
  190. }
  191. return 0;
  192. }
  193. static int apr_uevent(struct device *dev, struct kobj_uevent_env *env)
  194. {
  195. struct apr_device *adev = to_apr_device(dev);
  196. int ret;
  197. ret = of_device_uevent_modalias(dev, env);
  198. if (ret != -ENODEV)
  199. return ret;
  200. return add_uevent_var(env, "MODALIAS=apr:%s", adev->name);
  201. }
  202. struct bus_type aprbus = {
  203. .name = "aprbus",
  204. .match = apr_device_match,
  205. .probe = apr_device_probe,
  206. .uevent = apr_uevent,
  207. .remove = apr_device_remove,
  208. };
  209. EXPORT_SYMBOL_GPL(aprbus);
  210. static int apr_add_device(struct device *dev, struct device_node *np,
  211. const struct apr_device_id *id)
  212. {
  213. struct apr *apr = dev_get_drvdata(dev);
  214. struct apr_device *adev = NULL;
  215. int ret;
  216. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  217. if (!adev)
  218. return -ENOMEM;
  219. spin_lock_init(&adev->lock);
  220. adev->svc_id = id->svc_id;
  221. adev->domain_id = id->domain_id;
  222. adev->version = id->svc_version;
  223. if (np)
  224. snprintf(adev->name, APR_NAME_SIZE, "%pOFn", np);
  225. else
  226. strscpy(adev->name, id->name, APR_NAME_SIZE);
  227. dev_set_name(&adev->dev, "aprsvc:%s:%x:%x", adev->name,
  228. id->domain_id, id->svc_id);
  229. adev->dev.bus = &aprbus;
  230. adev->dev.parent = dev;
  231. adev->dev.of_node = np;
  232. adev->dev.release = apr_dev_release;
  233. adev->dev.driver = NULL;
  234. spin_lock(&apr->svcs_lock);
  235. idr_alloc(&apr->svcs_idr, adev, id->svc_id,
  236. id->svc_id + 1, GFP_ATOMIC);
  237. spin_unlock(&apr->svcs_lock);
  238. dev_info(dev, "Adding APR dev: %s\n", dev_name(&adev->dev));
  239. ret = device_register(&adev->dev);
  240. if (ret) {
  241. dev_err(dev, "device_register failed: %d\n", ret);
  242. put_device(&adev->dev);
  243. }
  244. return ret;
  245. }
  246. static void of_register_apr_devices(struct device *dev)
  247. {
  248. struct apr *apr = dev_get_drvdata(dev);
  249. struct device_node *node;
  250. for_each_child_of_node(dev->of_node, node) {
  251. struct apr_device_id id = { {0} };
  252. if (of_property_read_u32(node, "reg", &id.svc_id))
  253. continue;
  254. id.domain_id = apr->dest_domain_id;
  255. if (apr_add_device(dev, node, &id))
  256. dev_err(dev, "Failed to add apr %d svc\n", id.svc_id);
  257. }
  258. }
  259. static int apr_probe(struct rpmsg_device *rpdev)
  260. {
  261. struct device *dev = &rpdev->dev;
  262. struct apr *apr;
  263. int ret;
  264. apr = devm_kzalloc(dev, sizeof(*apr), GFP_KERNEL);
  265. if (!apr)
  266. return -ENOMEM;
  267. ret = of_property_read_u32(dev->of_node, "qcom,apr-domain", &apr->dest_domain_id);
  268. if (ret) {
  269. dev_err(dev, "APR Domain ID not specified in DT\n");
  270. return ret;
  271. }
  272. dev_set_drvdata(dev, apr);
  273. apr->ch = rpdev->ept;
  274. apr->dev = dev;
  275. apr->rxwq = create_singlethread_workqueue("qcom_apr_rx");
  276. if (!apr->rxwq) {
  277. dev_err(apr->dev, "Failed to start Rx WQ\n");
  278. return -ENOMEM;
  279. }
  280. INIT_WORK(&apr->rx_work, apr_rxwq);
  281. INIT_LIST_HEAD(&apr->rx_list);
  282. spin_lock_init(&apr->rx_lock);
  283. spin_lock_init(&apr->svcs_lock);
  284. idr_init(&apr->svcs_idr);
  285. of_register_apr_devices(dev);
  286. return 0;
  287. }
  288. static int apr_remove_device(struct device *dev, void *null)
  289. {
  290. struct apr_device *adev = to_apr_device(dev);
  291. device_unregister(&adev->dev);
  292. return 0;
  293. }
  294. static void apr_remove(struct rpmsg_device *rpdev)
  295. {
  296. struct apr *apr = dev_get_drvdata(&rpdev->dev);
  297. device_for_each_child(&rpdev->dev, NULL, apr_remove_device);
  298. flush_workqueue(apr->rxwq);
  299. destroy_workqueue(apr->rxwq);
  300. }
  301. /*
  302. * __apr_driver_register() - Client driver registration with aprbus
  303. *
  304. * @drv:Client driver to be associated with client-device.
  305. * @owner: owning module/driver
  306. *
  307. * This API will register the client driver with the aprbus
  308. * It is called from the driver's module-init function.
  309. */
  310. int __apr_driver_register(struct apr_driver *drv, struct module *owner)
  311. {
  312. drv->driver.bus = &aprbus;
  313. drv->driver.owner = owner;
  314. return driver_register(&drv->driver);
  315. }
  316. EXPORT_SYMBOL_GPL(__apr_driver_register);
  317. /*
  318. * apr_driver_unregister() - Undo effect of apr_driver_register
  319. *
  320. * @drv: Client driver to be unregistered
  321. */
  322. void apr_driver_unregister(struct apr_driver *drv)
  323. {
  324. driver_unregister(&drv->driver);
  325. }
  326. EXPORT_SYMBOL_GPL(apr_driver_unregister);
  327. static const struct of_device_id apr_of_match[] = {
  328. { .compatible = "qcom,apr"},
  329. { .compatible = "qcom,apr-v2"},
  330. {}
  331. };
  332. MODULE_DEVICE_TABLE(of, apr_of_match);
  333. static struct rpmsg_driver apr_driver = {
  334. .probe = apr_probe,
  335. .remove = apr_remove,
  336. .callback = apr_callback,
  337. .drv = {
  338. .name = "qcom,apr",
  339. .of_match_table = apr_of_match,
  340. },
  341. };
  342. static int __init apr_init(void)
  343. {
  344. int ret;
  345. ret = bus_register(&aprbus);
  346. if (!ret)
  347. ret = register_rpmsg_driver(&apr_driver);
  348. else
  349. bus_unregister(&aprbus);
  350. return ret;
  351. }
  352. static void __exit apr_exit(void)
  353. {
  354. bus_unregister(&aprbus);
  355. unregister_rpmsg_driver(&apr_driver);
  356. }
  357. subsys_initcall(apr_init);
  358. module_exit(apr_exit);
  359. MODULE_LICENSE("GPL v2");
  360. MODULE_DESCRIPTION("Qualcomm APR Bus");