gpr-lite.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* Copyright (c) 2011-2017, 2019-2020 The Linux Foundation. All rights reserved.
  2. * Copyright (c) 2018, Linaro Limited
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/idr.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_device.h>
  20. #include <ipc/gpr-lite.h>
  21. #include <linux/rpmsg.h>
  22. #include <linux/of.h>
  23. #include <soc/snd_event.h>
  24. #include <dsp/audio_notifier.h>
  25. struct gpr {
  26. struct rpmsg_endpoint *ch;
  27. struct device *dev;
  28. spinlock_t gpr_lock;
  29. bool is_initial_boot;
  30. spinlock_t svcs_lock;
  31. struct idr svcs_idr;
  32. int dest_domain_id;
  33. };
  34. static struct gpr_q6 q6;
  35. static struct gpr *gpr_priv;
  36. enum gpr_subsys_state gpr_get_q6_state(void)
  37. {
  38. return atomic_read(&q6.q6_state);
  39. }
  40. EXPORT_SYMBOL(gpr_get_q6_state);
  41. enum gpr_subsys_state gpr_get_modem_state(void)
  42. {
  43. return atomic_read(&q6.modem_state);
  44. }
  45. EXPORT_SYMBOL(gpr_get_modem_state);
  46. void gpr_subsys_notif_register(char *client_name, int domain,
  47. struct notifier_block *nb)
  48. {
  49. int ret;
  50. ret = audio_notifier_register(client_name, domain, nb);
  51. if (ret < 0)
  52. dev_err(gpr_priv->dev, "%s: Audio notifier register failed for domain %d ret = %d\n",
  53. __func__, domain, ret);
  54. }
  55. void gpr_subsys_notif_deregister(char *client_name)
  56. {
  57. int ret;
  58. ret = audio_notifier_deregister(client_name);
  59. if (ret < 0)
  60. dev_err(gpr_priv->dev, "%s: Audio notifier de-register failed for client %s\n",
  61. __func__, client_name);
  62. }
  63. /**
  64. * gpr_send_pkt() - Send a gpr message from gpr device
  65. *
  66. * @adev: Pointer to previously registered gpr device.
  67. * @pkt: Pointer to gpr packet to send
  68. *
  69. * Return: Will be an negative and/or packet size on success.
  70. */
  71. int gpr_send_pkt(struct gpr_device *adev, struct gpr_pkt *pkt)
  72. {
  73. struct gpr *gpr;
  74. struct gpr_hdr *hdr;
  75. unsigned long flags;
  76. uint32_t pkt_size;
  77. int ret;
  78. if(!adev)
  79. {
  80. pr_err("%s: enter pointer adev[%p] \n", __func__, adev);
  81. return -EINVAL;
  82. }
  83. if(!(adev->dev.parent))
  84. {
  85. pr_err("%s: enter pointer adev->dev.parent[%p] \n",
  86. __func__, adev->dev.parent);
  87. return -EINVAL;
  88. }
  89. gpr = dev_get_drvdata(adev->dev.parent);
  90. if ((adev->domain_id == GPR_IDS_DOMAIN_ID_ADSP_V) &&
  91. (gpr_get_q6_state() != GPR_SUBSYS_LOADED)) {
  92. dev_err(gpr->dev,"%s: domain_id[%d], Still Dsp is not Up\n",
  93. __func__, adev->domain_id);
  94. return -ENETRESET;
  95. } else if ((adev->domain_id == GPR_IDS_DOMAIN_ID_MODEM_V) &&
  96. (gpr_get_modem_state() == GPR_SUBSYS_DOWN)) {
  97. dev_err(gpr->dev, "%s: domain_id[%d], Still Modem is not Up\n",
  98. __func__, adev->domain_id );
  99. return -ENETRESET;
  100. }
  101. spin_lock_irqsave(&adev->lock, flags);
  102. hdr = &pkt->hdr;
  103. pkt_size = GPR_PKT_GET_PACKET_BYTE_SIZE(hdr->header);
  104. dev_dbg(gpr->dev, "SVC_ID %d %s packet size %d\n",
  105. adev->svc_id, __func__, pkt_size);
  106. ret = rpmsg_trysend(gpr->ch, pkt, pkt_size);
  107. spin_unlock_irqrestore(&adev->lock, flags);
  108. return ret ? ret : pkt_size;
  109. }
  110. EXPORT_SYMBOL_GPL(gpr_send_pkt);
  111. /**
  112. * apr_set_modem_state - Update modem load status.
  113. *
  114. * @state: State to update modem load status
  115. *
  116. */
  117. void gpr_set_modem_state(enum gpr_subsys_state state)
  118. {
  119. atomic_set(&q6.modem_state, state);
  120. }
  121. EXPORT_SYMBOL(gpr_set_modem_state);
  122. static void gpr_modem_down(unsigned long opcode)
  123. {
  124. gpr_set_modem_state(GPR_SUBSYS_DOWN);
  125. //dispatch_event(opcode, APR_DEST_MODEM);
  126. }
  127. static void gpr_modem_up(void)
  128. {
  129. //if (apr_cmpxchg_modem_state(APR_SUBSYS_DOWN, APR_SUBSYS_UP) ==
  130. // APR_SUBSYS_DOWN)
  131. // wake_up(&modem_wait);
  132. //is_modem_up = 1;
  133. }
  134. int gpr_set_q6_state(enum gpr_subsys_state state)
  135. {
  136. dev_dbg(gpr_priv->dev,"%s: setting adsp state %d\n", __func__, state);
  137. if (state < GPR_SUBSYS_DOWN || state > GPR_SUBSYS_LOADED)
  138. return -EINVAL;
  139. atomic_set(&q6.q6_state, state);
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(gpr_set_q6_state);
  143. static void gpr_ssr_disable(struct device *dev, void *data)
  144. {
  145. gpr_set_q6_state(GPR_SUBSYS_DOWN);
  146. }
  147. static const struct snd_event_ops gpr_ssr_ops = {
  148. .disable = gpr_ssr_disable,
  149. };
  150. static void gpr_adsp_down(unsigned long opcode)
  151. {
  152. dev_dbg(gpr_priv->dev,"%s: Q6 is Down\n", __func__);
  153. snd_event_notify(gpr_priv->dev, SND_EVENT_DOWN);
  154. gpr_set_q6_state(GPR_SUBSYS_DOWN);
  155. }
  156. static void gpr_adsp_up(void)
  157. {
  158. dev_dbg(gpr_priv->dev,"%s: Q6 is Up\n", __func__);
  159. gpr_set_q6_state(GPR_SUBSYS_LOADED);
  160. snd_event_notify(gpr_priv->dev, SND_EVENT_UP);
  161. }
  162. static int gpr_notifier_service_cb(struct notifier_block *this,
  163. unsigned long opcode, void *data)
  164. {
  165. struct audio_notifier_cb_data *cb_data = data;
  166. if (cb_data == NULL) {
  167. dev_err(gpr_priv->dev,"%s: Callback data is NULL!\n", __func__);
  168. goto done;
  169. }
  170. dev_dbg(gpr_priv->dev,"%s: Service opcode 0x%lx, domain %d\n",
  171. __func__, opcode, cb_data->domain);
  172. switch (opcode) {
  173. case AUDIO_NOTIFIER_SERVICE_DOWN:
  174. /*
  175. * Use flag to ignore down notifications during
  176. * initial boot. There is no benefit from error
  177. * recovery notifications during initial boot
  178. * up since everything is expected to be down.
  179. */
  180. spin_lock(&gpr_priv->gpr_lock);
  181. if (gpr_priv->is_initial_boot) {
  182. spin_unlock(&gpr_priv->gpr_lock);
  183. break;
  184. }
  185. spin_unlock(&gpr_priv->gpr_lock);
  186. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  187. gpr_modem_down(opcode);
  188. else
  189. gpr_adsp_down(opcode);
  190. break;
  191. case AUDIO_NOTIFIER_SERVICE_UP:
  192. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  193. gpr_modem_up();
  194. else
  195. gpr_adsp_up();
  196. spin_lock(&gpr_priv->gpr_lock);
  197. gpr_priv->is_initial_boot = false;
  198. spin_unlock(&gpr_priv->gpr_lock);
  199. break;
  200. default:
  201. break;
  202. }
  203. done:
  204. return NOTIFY_OK;
  205. }
  206. static struct notifier_block adsp_service_nb = {
  207. .notifier_call = gpr_notifier_service_cb,
  208. .priority = 0,
  209. };
  210. static struct notifier_block modem_service_nb = {
  211. .notifier_call = gpr_notifier_service_cb,
  212. .priority = 0,
  213. };
  214. static void gpr_dev_release(struct device *dev)
  215. {
  216. struct gpr_device *adev = to_gpr_device(dev);
  217. kfree(adev);
  218. }
  219. static int gpr_callback(struct rpmsg_device *rpdev, void *buf,
  220. int len, void *priv, u32 addr)
  221. {
  222. struct gpr *gpr = dev_get_drvdata(&rpdev->dev);
  223. uint16_t hdr_size, pkt_size, svc_id;
  224. //uint16_t ver;
  225. struct gpr_device *svc = NULL;
  226. struct gpr_driver *adrv = NULL;
  227. struct gpr_hdr *hdr;
  228. unsigned long flags;
  229. //uint32_t opcode_type;
  230. if (len <= GPR_HDR_SIZE) {
  231. dev_err(gpr->dev, "GPR: Improper gpr pkt received:%p %d\n",
  232. buf, len);
  233. return -EINVAL;
  234. }
  235. hdr = buf;
  236. hdr_size = GPR_PKT_GET_HEADER_BYTE_SIZE(hdr->header);
  237. if (hdr_size < GPR_HDR_SIZE) {
  238. dev_err(gpr->dev, "GPR: Wrong hdr size:%d\n", hdr_size);
  239. return -EINVAL;
  240. }
  241. pkt_size = GPR_PKT_GET_PACKET_BYTE_SIZE(hdr->header);
  242. dev_dbg(gpr->dev,"Header %x", hdr->header);
  243. if (pkt_size < GPR_HDR_SIZE || pkt_size != len) {
  244. dev_err(gpr->dev, "GPR: Wrong packet size\n");
  245. return -EINVAL;
  246. }
  247. dev_dbg(gpr->dev, "%s: dst_port %x hdr_size %d pkt_size %d\n",
  248. __func__ , hdr->dst_port, hdr_size, pkt_size);
  249. svc_id = hdr->dst_port;
  250. spin_lock_irqsave(&gpr->svcs_lock, flags);
  251. svc = idr_find(&gpr->svcs_idr, svc_id);
  252. if (svc && svc->dev.driver) {
  253. adrv = to_gpr_driver(svc->dev.driver);
  254. } else {
  255. /*Does not match any SVC ID hence would be routed to audio passthrough*/
  256. svc = idr_find(&gpr->svcs_idr, GPR_SVC_MAX);
  257. if (svc && svc->dev.driver)
  258. adrv = to_gpr_driver(svc->dev.driver);
  259. }
  260. spin_unlock_irqrestore(&gpr->svcs_lock, flags);
  261. if (!adrv) {
  262. dev_err(gpr->dev, "GPR: service is not registered\n");
  263. return -EINVAL;
  264. }
  265. /*
  266. * NOTE: hdr_size is not same as GPR_HDR_SIZE as remote can include
  267. * optional headers in to gpr_hdr which should be ignored
  268. */
  269. adrv->callback(svc, buf);
  270. return 0;
  271. }
  272. static int gpr_device_match(struct device *dev, struct device_driver *drv)
  273. {
  274. struct gpr_device *adev = to_gpr_device(dev);
  275. struct gpr_driver *adrv = to_gpr_driver(drv);
  276. const struct gpr_device_id *id = adrv->id_table;
  277. /* Attempt an OF style match first */
  278. if (of_driver_match_device(dev, drv))
  279. return 1;
  280. if (!id)
  281. return 0;
  282. while (id->domain_id != 0 || id->svc_id != 0) {
  283. if (id->domain_id == adev->domain_id &&
  284. id->svc_id == adev->svc_id)
  285. return 1;
  286. id++;
  287. }
  288. return 0;
  289. }
  290. static int gpr_device_probe(struct device *dev)
  291. {
  292. struct gpr_device *adev = to_gpr_device(dev);
  293. struct gpr_driver *adrv = to_gpr_driver(dev->driver);
  294. return adrv->probe(adev);
  295. }
  296. static int gpr_device_remove(struct device *dev)
  297. {
  298. struct gpr_device *adev = to_gpr_device(dev);
  299. struct gpr_driver *adrv;
  300. struct gpr *gpr = dev_get_drvdata(adev->dev.parent);
  301. if (dev->driver) {
  302. adrv = to_gpr_driver(dev->driver);
  303. if (adrv->remove)
  304. adrv->remove(adev);
  305. spin_lock(&gpr->svcs_lock);
  306. idr_remove(&gpr->svcs_idr, adev->svc_id);
  307. spin_unlock(&gpr->svcs_lock);
  308. }
  309. return 0;
  310. }
  311. static int gpr_uevent(struct device *dev, struct kobj_uevent_env *env)
  312. {
  313. struct gpr_device *adev = to_gpr_device(dev);
  314. int ret;
  315. ret = of_device_uevent_modalias(dev, env);
  316. if (ret != -ENODEV)
  317. return ret;
  318. return add_uevent_var(env, "MODALIAS=gpr:%s", adev->name);
  319. }
  320. struct bus_type gprbus = {
  321. .name = "gprbus",
  322. .match = gpr_device_match,
  323. .probe = gpr_device_probe,
  324. .uevent = gpr_uevent,
  325. .remove = gpr_device_remove,
  326. };
  327. EXPORT_SYMBOL_GPL(gprbus);
  328. static int gpr_add_device(struct device *dev, struct device_node *np,
  329. const struct gpr_device_id *id)
  330. {
  331. struct gpr *gpr = dev_get_drvdata(dev);
  332. struct gpr_device *adev = NULL;
  333. int ret;
  334. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  335. if (!adev)
  336. return -ENOMEM;
  337. spin_lock_init(&adev->lock);
  338. adev->svc_id = id->svc_id;
  339. adev->domain_id = id->domain_id;
  340. adev->version = id->svc_version;
  341. if (np)
  342. strscpy(adev->name, np->name, GPR_NAME_SIZE);
  343. else
  344. strscpy(adev->name, id->name, GPR_NAME_SIZE);
  345. dev_set_name(&adev->dev, "gprsvc:%s:%x:%x", adev->name,
  346. id->domain_id, id->svc_id);
  347. adev->dev.bus = &gprbus;
  348. adev->dev.parent = dev;
  349. adev->dev.of_node = np;
  350. adev->dev.release = gpr_dev_release;
  351. adev->dev.driver = NULL;
  352. spin_lock(&gpr->svcs_lock);
  353. idr_alloc(&gpr->svcs_idr, adev, id->svc_id,
  354. id->svc_id + 1, GFP_ATOMIC);
  355. spin_unlock(&gpr->svcs_lock);
  356. dev_info(dev, "Adding GPR dev: %s\n", dev_name(&adev->dev));
  357. ret = device_register(&adev->dev);
  358. if (ret) {
  359. dev_err(dev, "device_register failed: %d\n", ret);
  360. put_device(&adev->dev);
  361. }
  362. return ret;
  363. }
  364. static void of_register_gpr_devices(struct device *dev)
  365. {
  366. struct gpr *gpr = dev_get_drvdata(dev);
  367. struct device_node *node;
  368. for_each_child_of_node(dev->of_node, node) {
  369. struct gpr_device_id id = { {0} };
  370. if (of_property_read_u32(node, "reg", &id.svc_id))
  371. continue;
  372. id.domain_id = gpr->dest_domain_id;
  373. if (gpr_add_device(dev, node, &id))
  374. dev_err(dev, "Failed to add gpr %d svc\n", id.svc_id);
  375. }
  376. }
  377. static int gpr_probe(struct rpmsg_device *rpdev)
  378. {
  379. struct device *dev = &rpdev->dev;
  380. int ret;
  381. gpr_priv = devm_kzalloc(dev, sizeof(*gpr_priv), GFP_KERNEL);
  382. if (!gpr_priv)
  383. return -ENOMEM;
  384. spin_lock_init(&gpr_priv->gpr_lock);
  385. mutex_init(&q6.lock);
  386. spin_lock(&gpr_priv->gpr_lock);
  387. gpr_priv->is_initial_boot = true;
  388. spin_unlock(&gpr_priv->gpr_lock);
  389. ret = of_property_read_u32(dev->of_node, "reg", &gpr_priv->dest_domain_id);
  390. if (ret) {
  391. dev_err(dev, "GPR Domain ID not specified in DT\n");
  392. return ret;
  393. }
  394. if (GPR_DOMAIN_ADSP == gpr_priv->dest_domain_id) {
  395. gpr_subsys_notif_register("gpr_adsp",
  396. AUDIO_NOTIFIER_ADSP_DOMAIN,
  397. &adsp_service_nb);
  398. } else if (GPR_DOMAIN_MODEM == gpr_priv->dest_domain_id) {
  399. gpr_subsys_notif_register("gpr_modem",
  400. AUDIO_NOTIFIER_MODEM_DOMAIN,
  401. &modem_service_nb);
  402. } else {
  403. dev_err(dev, "%s: invalid dest_domain_id %s\n", __func__,
  404. gpr_priv->dest_domain_id);
  405. return -EINVAL;
  406. }
  407. dev_info(dev, "%s: registered via subsys_notif_register for domain id(%d)",
  408. __func__, gpr_priv->dest_domain_id );
  409. dev_set_drvdata(dev, gpr_priv);
  410. gpr_priv->ch = rpdev->ept;
  411. gpr_priv->dev = dev;
  412. spin_lock_init(&gpr_priv->svcs_lock);
  413. idr_init(&gpr_priv->svcs_idr);
  414. of_register_gpr_devices(dev);
  415. ret = snd_event_client_register(&rpdev->dev, &gpr_ssr_ops, NULL);
  416. if (ret) {
  417. dev_err(dev,"%s: Registration with SND event fwk failed ret = %d\n",
  418. __func__, ret);
  419. ret = 0;
  420. }
  421. return 0;
  422. }
  423. static int gpr_remove_device(struct device *dev, void *null)
  424. {
  425. struct gpr_device *adev = to_gpr_device(dev);
  426. device_unregister(&adev->dev);
  427. return 0;
  428. }
  429. static void gpr_remove(struct rpmsg_device *rpdev)
  430. {
  431. struct device *dev = &rpdev->dev;
  432. snd_event_client_deregister(&rpdev->dev);
  433. dev_info(dev, "%s: deregistering via subsys_notif_register for domain_id(%d)",
  434. __func__, gpr_priv->dest_domain_id );
  435. if (GPR_DOMAIN_ADSP == gpr_priv->dest_domain_id)
  436. {
  437. gpr_subsys_notif_deregister("gpr_adsp");
  438. }
  439. else if(GPR_DOMAIN_MODEM == gpr_priv->dest_domain_id)
  440. {
  441. gpr_subsys_notif_deregister("gpr_modem");
  442. }
  443. device_for_each_child(&rpdev->dev, NULL, gpr_remove_device);
  444. }
  445. /*
  446. * __gpr_driver_register() - Client driver registration with gprbus
  447. *
  448. * @drv:Client driver to be associated with client-device.
  449. * @owner: owning module/driver
  450. *
  451. * This API will register the client driver with the gprbus
  452. * It is called from the driver's module-init function.
  453. */
  454. int __gpr_driver_register(struct gpr_driver *drv, struct module *owner)
  455. {
  456. drv->driver.bus = &gprbus;
  457. drv->driver.owner = owner;
  458. return driver_register(&drv->driver);
  459. }
  460. EXPORT_SYMBOL_GPL(__gpr_driver_register);
  461. /*
  462. * gpr_driver_unregister() - Undo effect of gpr_driver_register
  463. *
  464. * @drv: Client driver to be unregistered
  465. */
  466. void gpr_driver_unregister(struct gpr_driver *drv)
  467. {
  468. driver_unregister(&drv->driver);
  469. }
  470. EXPORT_SYMBOL_GPL(gpr_driver_unregister);
  471. static const struct of_device_id gpr_of_match[] = {
  472. { .compatible = "qcom,gpr"},
  473. {}
  474. };
  475. MODULE_DEVICE_TABLE(of, gpr_of_match);
  476. static struct rpmsg_driver gpr_driver = {
  477. .probe = gpr_probe,
  478. .remove = gpr_remove,
  479. .callback = gpr_callback,
  480. .drv = {
  481. .name = "qcom,gpr",
  482. .of_match_table = gpr_of_match,
  483. },
  484. };
  485. static int __init gpr_init(void)
  486. {
  487. int ret;
  488. ret = bus_register(&gprbus);
  489. if (!ret)
  490. ret = register_rpmsg_driver(&gpr_driver);
  491. else
  492. bus_unregister(&gprbus);
  493. return ret;
  494. }
  495. static void __exit gpr_exit(void)
  496. {
  497. bus_unregister(&gprbus);
  498. unregister_rpmsg_driver(&gpr_driver);
  499. }
  500. subsys_initcall(gpr_init);
  501. module_exit(gpr_exit);
  502. MODULE_LICENSE("GPL v2");
  503. MODULE_DESCRIPTION("QTI GPR Bus");