gpr-lite.c 15 KB

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