apr.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2014, 2016-2019 The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/list.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/errno.h>
  14. #include <linux/fs.h>
  15. #include <linux/delay.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/device.h>
  20. #include <linux/of.h>
  21. #include <linux/slab.h>
  22. #include <linux/ipc_logging.h>
  23. #include <linux/of_platform.h>
  24. #include <soc/qcom/subsystem_restart.h>
  25. #include <linux/qcom_scm.h>
  26. #include <soc/snd_event.h>
  27. #include <dsp/apr_audio-v2.h>
  28. #include <dsp/audio_notifier.h>
  29. #include <ipc/apr.h>
  30. #include <ipc/apr_tal.h>
  31. #define APR_PKT_IPC_LOG_PAGE_CNT 2
  32. static struct apr_q6 q6;
  33. static struct apr_client client[APR_DEST_MAX][APR_CLIENT_MAX];
  34. static void *apr_pkt_ctx;
  35. static wait_queue_head_t modem_wait;
  36. static bool is_modem_up;
  37. static char *subsys_name = NULL;
  38. /* Subsystem restart: QDSP6 data, functions */
  39. static struct workqueue_struct *apr_reset_workqueue;
  40. static void apr_reset_deregister(struct work_struct *work);
  41. static void dispatch_event(unsigned long code, uint16_t proc);
  42. struct apr_reset_work {
  43. void *handle;
  44. struct work_struct work;
  45. };
  46. struct apr_chld_device {
  47. struct platform_device *pdev;
  48. struct list_head node;
  49. };
  50. struct apr_private {
  51. struct device *dev;
  52. spinlock_t apr_lock;
  53. bool is_initial_boot;
  54. struct work_struct add_chld_dev_work;
  55. };
  56. static struct apr_private *apr_priv;
  57. static bool apr_cf_debug;
  58. #ifdef CONFIG_DEBUG_FS
  59. static struct dentry *debugfs_apr_debug;
  60. static ssize_t apr_debug_write(struct file *filp, const char __user *ubuf,
  61. size_t cnt, loff_t *ppos)
  62. {
  63. char cmd;
  64. if (copy_from_user(&cmd, ubuf, 1))
  65. return -EFAULT;
  66. apr_cf_debug = (cmd == '1') ? true : false;
  67. return cnt;
  68. }
  69. static const struct file_operations apr_debug_ops = {
  70. .write = apr_debug_write,
  71. };
  72. #endif
  73. #define APR_PKT_INFO(x...) \
  74. do { \
  75. if (apr_pkt_ctx) \
  76. ipc_log_string(apr_pkt_ctx, "<APR>: "x); \
  77. } while (0)
  78. struct apr_svc_table {
  79. char name[64];
  80. int idx;
  81. int id;
  82. int client_id;
  83. };
  84. static const struct apr_svc_table svc_tbl_qdsp6[] = {
  85. {
  86. .name = "AFE",
  87. .idx = 0,
  88. .id = APR_SVC_AFE,
  89. .client_id = APR_CLIENT_AUDIO,
  90. },
  91. {
  92. .name = "ASM",
  93. .idx = 1,
  94. .id = APR_SVC_ASM,
  95. .client_id = APR_CLIENT_AUDIO,
  96. },
  97. {
  98. .name = "ADM",
  99. .idx = 2,
  100. .id = APR_SVC_ADM,
  101. .client_id = APR_CLIENT_AUDIO,
  102. },
  103. {
  104. .name = "CORE",
  105. .idx = 3,
  106. .id = APR_SVC_ADSP_CORE,
  107. .client_id = APR_CLIENT_AUDIO,
  108. },
  109. {
  110. .name = "TEST",
  111. .idx = 4,
  112. .id = APR_SVC_TEST_CLIENT,
  113. .client_id = APR_CLIENT_AUDIO,
  114. },
  115. {
  116. .name = "MVM",
  117. .idx = 5,
  118. .id = APR_SVC_ADSP_MVM,
  119. .client_id = APR_CLIENT_AUDIO,
  120. },
  121. {
  122. .name = "CVS",
  123. .idx = 6,
  124. .id = APR_SVC_ADSP_CVS,
  125. .client_id = APR_CLIENT_AUDIO,
  126. },
  127. {
  128. .name = "CVP",
  129. .idx = 7,
  130. .id = APR_SVC_ADSP_CVP,
  131. .client_id = APR_CLIENT_AUDIO,
  132. },
  133. {
  134. .name = "USM",
  135. .idx = 8,
  136. .id = APR_SVC_USM,
  137. .client_id = APR_CLIENT_AUDIO,
  138. },
  139. {
  140. .name = "VIDC",
  141. .idx = 9,
  142. .id = APR_SVC_VIDC,
  143. },
  144. {
  145. .name = "LSM",
  146. .idx = 10,
  147. .id = APR_SVC_LSM,
  148. .client_id = APR_CLIENT_AUDIO,
  149. },
  150. };
  151. static struct apr_svc_table svc_tbl_voice[] = {
  152. {
  153. .name = "VSM",
  154. .idx = 0,
  155. .id = APR_SVC_VSM,
  156. .client_id = APR_CLIENT_VOICE,
  157. },
  158. {
  159. .name = "VPM",
  160. .idx = 1,
  161. .id = APR_SVC_VPM,
  162. .client_id = APR_CLIENT_VOICE,
  163. },
  164. {
  165. .name = "MVS",
  166. .idx = 2,
  167. .id = APR_SVC_MVS,
  168. .client_id = APR_CLIENT_VOICE,
  169. },
  170. {
  171. .name = "MVM",
  172. .idx = 3,
  173. .id = APR_SVC_MVM,
  174. .client_id = APR_CLIENT_VOICE,
  175. },
  176. {
  177. .name = "CVS",
  178. .idx = 4,
  179. .id = APR_SVC_CVS,
  180. .client_id = APR_CLIENT_VOICE,
  181. },
  182. {
  183. .name = "CVP",
  184. .idx = 5,
  185. .id = APR_SVC_CVP,
  186. .client_id = APR_CLIENT_VOICE,
  187. },
  188. {
  189. .name = "SRD",
  190. .idx = 6,
  191. .id = APR_SVC_SRD,
  192. .client_id = APR_CLIENT_VOICE,
  193. },
  194. {
  195. .name = "TEST",
  196. .idx = 7,
  197. .id = APR_SVC_TEST_CLIENT,
  198. .client_id = APR_CLIENT_VOICE,
  199. },
  200. };
  201. /**
  202. * apr_get_modem_state:
  203. *
  204. * Returns current modem load status
  205. *
  206. */
  207. enum apr_subsys_state apr_get_modem_state(void)
  208. {
  209. return atomic_read(&q6.modem_state);
  210. }
  211. EXPORT_SYMBOL(apr_get_modem_state);
  212. /**
  213. * apr_set_modem_state - Update modem load status.
  214. *
  215. * @state: State to update modem load status
  216. *
  217. */
  218. void apr_set_modem_state(enum apr_subsys_state state)
  219. {
  220. atomic_set(&q6.modem_state, state);
  221. }
  222. EXPORT_SYMBOL(apr_set_modem_state);
  223. enum apr_subsys_state apr_cmpxchg_modem_state(enum apr_subsys_state prev,
  224. enum apr_subsys_state new)
  225. {
  226. return atomic_cmpxchg(&q6.modem_state, prev, new);
  227. }
  228. static void apr_modem_down(unsigned long opcode)
  229. {
  230. apr_set_modem_state(APR_SUBSYS_DOWN);
  231. dispatch_event(opcode, APR_DEST_MODEM);
  232. }
  233. static void apr_modem_up(void)
  234. {
  235. if (apr_cmpxchg_modem_state(APR_SUBSYS_DOWN, APR_SUBSYS_UP) ==
  236. APR_SUBSYS_DOWN)
  237. wake_up(&modem_wait);
  238. is_modem_up = 1;
  239. }
  240. enum apr_subsys_state apr_get_q6_state(void)
  241. {
  242. return atomic_read(&q6.q6_state);
  243. }
  244. EXPORT_SYMBOL(apr_get_q6_state);
  245. int apr_set_q6_state(enum apr_subsys_state state)
  246. {
  247. pr_debug("%s: setting adsp state %d\n", __func__, state);
  248. if (state < APR_SUBSYS_DOWN || state > APR_SUBSYS_LOADED)
  249. return -EINVAL;
  250. atomic_set(&q6.q6_state, state);
  251. return 0;
  252. }
  253. EXPORT_SYMBOL(apr_set_q6_state);
  254. static void apr_ssr_disable(struct device *dev, void *data)
  255. {
  256. apr_set_q6_state(APR_SUBSYS_DOWN);
  257. }
  258. static const struct snd_event_ops apr_ssr_ops = {
  259. .disable = apr_ssr_disable,
  260. };
  261. static void apr_adsp_down(unsigned long opcode)
  262. {
  263. pr_info("%s: Q6 is Down\n", __func__);
  264. snd_event_notify(apr_priv->dev, SND_EVENT_DOWN);
  265. apr_set_q6_state(APR_SUBSYS_DOWN);
  266. dispatch_event(opcode, APR_DEST_QDSP6);
  267. }
  268. static void apr_add_child_devices(struct work_struct *work)
  269. {
  270. int ret;
  271. ret = of_platform_populate(apr_priv->dev->of_node,
  272. NULL, NULL, apr_priv->dev);
  273. if (ret)
  274. dev_err(apr_priv->dev, "%s: failed to add child nodes, ret=%d\n",
  275. __func__, ret);
  276. }
  277. static void apr_adsp_up(void)
  278. {
  279. pr_info("%s: Q6 is Up\n", __func__);
  280. apr_set_q6_state(APR_SUBSYS_LOADED);
  281. spin_lock(&apr_priv->apr_lock);
  282. if (apr_priv->is_initial_boot)
  283. schedule_work(&apr_priv->add_chld_dev_work);
  284. spin_unlock(&apr_priv->apr_lock);
  285. snd_event_notify(apr_priv->dev, SND_EVENT_UP);
  286. }
  287. int apr_load_adsp_image(void)
  288. {
  289. int rc = 0;
  290. mutex_lock(&q6.lock);
  291. if (apr_get_q6_state() == APR_SUBSYS_UP) {
  292. q6.pil = subsystem_get("adsp");
  293. if (IS_ERR(q6.pil)) {
  294. rc = PTR_ERR(q6.pil);
  295. pr_err("APR: Unable to load q6 image, error:%d\n", rc);
  296. } else {
  297. apr_set_q6_state(APR_SUBSYS_LOADED);
  298. pr_debug("APR: Image is loaded, stated\n");
  299. }
  300. } else if (apr_get_q6_state() == APR_SUBSYS_LOADED) {
  301. pr_debug("APR: q6 image already loaded\n");
  302. } else {
  303. pr_debug("APR: cannot load state %d\n", apr_get_q6_state());
  304. }
  305. mutex_unlock(&q6.lock);
  306. return rc;
  307. }
  308. struct apr_client *apr_get_client(int dest_id, int client_id)
  309. {
  310. return &client[dest_id][client_id];
  311. }
  312. /**
  313. * apr_send_pkt - Clients call to send packet
  314. * to destination processor.
  315. *
  316. * @handle: APR service handle
  317. * @buf: payload to send to destination processor.
  318. *
  319. * Returns Bytes(>0)pkt_size on success or error on failure.
  320. */
  321. int apr_send_pkt(void *handle, uint32_t *buf)
  322. {
  323. struct apr_svc *svc = handle;
  324. struct apr_client *clnt;
  325. struct apr_hdr *hdr;
  326. uint16_t dest_id;
  327. uint16_t client_id;
  328. uint16_t w_len;
  329. int rc;
  330. unsigned long flags;
  331. if (!handle || !buf) {
  332. pr_err("APR: Wrong parameters for %s\n",
  333. !handle ? "handle" : "buf");
  334. return -EINVAL;
  335. }
  336. if (svc->need_reset) {
  337. pr_err_ratelimited("apr: send_pkt service need reset\n");
  338. return -ENETRESET;
  339. }
  340. if ((svc->dest_id == APR_DEST_QDSP6) &&
  341. (apr_get_q6_state() != APR_SUBSYS_LOADED)) {
  342. pr_err_ratelimited("%s: Still dsp is not Up\n", __func__);
  343. return -ENETRESET;
  344. } else if ((svc->dest_id == APR_DEST_MODEM) &&
  345. (apr_get_modem_state() == APR_SUBSYS_DOWN)) {
  346. pr_err("apr: Still Modem is not Up\n");
  347. return -ENETRESET;
  348. }
  349. spin_lock_irqsave(&svc->w_lock, flags);
  350. dest_id = svc->dest_id;
  351. client_id = svc->client_id;
  352. clnt = &client[dest_id][client_id];
  353. if (!client[dest_id][client_id].handle) {
  354. pr_err_ratelimited("APR: Still service is not yet opened\n");
  355. spin_unlock_irqrestore(&svc->w_lock, flags);
  356. return -EINVAL;
  357. }
  358. hdr = (struct apr_hdr *)buf;
  359. hdr->src_domain = APR_DOMAIN_APPS;
  360. hdr->src_svc = svc->id;
  361. hdr->dest_domain = svc->dest_domain;
  362. hdr->dest_svc = svc->id;
  363. if (unlikely(apr_cf_debug)) {
  364. APR_PKT_INFO(
  365. "Tx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  366. (hdr->src_domain << 8) | hdr->src_svc,
  367. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  368. hdr->token);
  369. }
  370. rc = apr_tal_write(clnt->handle, buf,
  371. (struct apr_pkt_priv *)&svc->pkt_owner,
  372. hdr->pkt_size);
  373. if (rc >= 0) {
  374. w_len = rc;
  375. if (w_len != hdr->pkt_size) {
  376. pr_err("%s: Unable to write whole APR pkt successfully: %d\n",
  377. __func__, rc);
  378. rc = -EINVAL;
  379. }
  380. } else {
  381. pr_err_ratelimited("%s: Write APR pkt failed with error %d\n",
  382. __func__, rc);
  383. if (rc == -ECONNRESET) {
  384. pr_err_ratelimited("%s: Received reset error from tal\n",
  385. __func__);
  386. rc = -ENETRESET;
  387. }
  388. }
  389. spin_unlock_irqrestore(&svc->w_lock, flags);
  390. return rc;
  391. }
  392. EXPORT_SYMBOL(apr_send_pkt);
  393. int apr_pkt_config(void *handle, struct apr_pkt_cfg *cfg)
  394. {
  395. struct apr_svc *svc = (struct apr_svc *)handle;
  396. uint16_t dest_id;
  397. uint16_t client_id;
  398. struct apr_client *clnt;
  399. if (!handle) {
  400. pr_err("%s: Invalid handle\n", __func__);
  401. return -EINVAL;
  402. }
  403. if (svc->need_reset) {
  404. pr_err("%s: service need reset\n", __func__);
  405. return -ENETRESET;
  406. }
  407. svc->pkt_owner = cfg->pkt_owner;
  408. dest_id = svc->dest_id;
  409. client_id = svc->client_id;
  410. clnt = &client[dest_id][client_id];
  411. return apr_tal_rx_intents_config(clnt->handle,
  412. cfg->intents.num_of_intents, cfg->intents.size);
  413. }
  414. /**
  415. * apr_register - Clients call to register
  416. * to APR.
  417. *
  418. * @dest: destination processor
  419. * @svc_name: name of service to register as
  420. * @svc_fn: callback function to trigger when response
  421. * ack or packets received from destination processor.
  422. * @src_port: Port number within a service
  423. * @priv: private data of client, passed back in cb fn.
  424. *
  425. * Returns apr_svc handle on success or NULL on failure.
  426. */
  427. struct apr_svc *apr_register(char *dest, char *svc_name, apr_fn svc_fn,
  428. uint32_t src_port, void *priv)
  429. {
  430. struct apr_client *clnt;
  431. int client_id = 0;
  432. int svc_idx = 0;
  433. int svc_id = 0;
  434. int dest_id = 0;
  435. int domain_id = 0;
  436. int temp_port = 0;
  437. struct apr_svc *svc = NULL;
  438. int rc = 0;
  439. bool can_open_channel = true;
  440. if (!dest || !svc_name || !svc_fn)
  441. return NULL;
  442. if (!strcmp(dest, "ADSP"))
  443. domain_id = APR_DOMAIN_ADSP;
  444. else if (!strcmp(dest, "MODEM")) {
  445. /* Don't request for SMD channels if destination is MODEM,
  446. * as these channels are no longer used and these clients
  447. * are to listen only for MODEM SSR events
  448. */
  449. can_open_channel = false;
  450. domain_id = APR_DOMAIN_MODEM;
  451. } else {
  452. pr_err("APR: wrong destination\n");
  453. goto done;
  454. }
  455. dest_id = apr_get_dest_id(dest);
  456. if (dest_id == APR_DEST_QDSP6) {
  457. if (apr_get_q6_state() != APR_SUBSYS_LOADED) {
  458. pr_err_ratelimited("%s: adsp not up\n", __func__);
  459. return NULL;
  460. }
  461. pr_debug("%s: adsp Up\n", __func__);
  462. } else if (dest_id == APR_DEST_MODEM) {
  463. if (apr_get_modem_state() == APR_SUBSYS_DOWN) {
  464. if (is_modem_up) {
  465. pr_err("%s: modem shutdown due to SSR, ret",
  466. __func__);
  467. return NULL;
  468. }
  469. pr_debug("%s: Wait for modem to bootup\n", __func__);
  470. rc = wait_event_interruptible_timeout(modem_wait,
  471. (apr_get_modem_state() == APR_SUBSYS_UP),
  472. (1 * HZ));
  473. if (rc == 0) {
  474. pr_err("%s: Modem is not Up\n", __func__);
  475. return NULL;
  476. }
  477. }
  478. pr_debug("%s: modem Up\n", __func__);
  479. }
  480. if (apr_get_svc(svc_name, domain_id, &client_id, &svc_idx, &svc_id)) {
  481. pr_err_ratelimited("%s: apr_get_svc failed\n", __func__);
  482. goto done;
  483. }
  484. clnt = &client[dest_id][client_id];
  485. mutex_lock(&clnt->m_lock);
  486. if (!clnt->handle && can_open_channel) {
  487. clnt->handle = apr_tal_open(client_id, dest_id,
  488. APR_DL_SMD, apr_cb_func, NULL);
  489. if (!clnt->handle) {
  490. svc = NULL;
  491. pr_err_ratelimited("APR: Unable to open handle\n");
  492. mutex_unlock(&clnt->m_lock);
  493. goto done;
  494. }
  495. }
  496. mutex_unlock(&clnt->m_lock);
  497. svc = &clnt->svc[svc_idx];
  498. mutex_lock(&svc->m_lock);
  499. clnt->id = client_id;
  500. if (svc->need_reset) {
  501. mutex_unlock(&svc->m_lock);
  502. pr_err_ratelimited("APR: Service needs reset\n");
  503. svc = NULL;
  504. goto done;
  505. }
  506. svc->id = svc_id;
  507. svc->dest_id = dest_id;
  508. svc->client_id = client_id;
  509. svc->dest_domain = domain_id;
  510. svc->pkt_owner = APR_PKT_OWNER_DRIVER;
  511. if (src_port != 0xFFFFFFFF) {
  512. temp_port = ((src_port >> 8) * 8) + (src_port & 0xFF);
  513. pr_debug("port = %d t_port = %d\n", src_port, temp_port);
  514. if (temp_port >= APR_MAX_PORTS || temp_port < 0) {
  515. pr_err("APR: temp_port out of bounds\n");
  516. mutex_unlock(&svc->m_lock);
  517. return NULL;
  518. }
  519. if (!svc->svc_cnt)
  520. clnt->svc_cnt++;
  521. svc->port_cnt++;
  522. svc->port_fn[temp_port] = svc_fn;
  523. svc->port_priv[temp_port] = priv;
  524. svc->svc_cnt++;
  525. } else {
  526. if (!svc->fn) {
  527. if (!svc->svc_cnt)
  528. clnt->svc_cnt++;
  529. svc->fn = svc_fn;
  530. svc->priv = priv;
  531. svc->svc_cnt++;
  532. }
  533. }
  534. mutex_unlock(&svc->m_lock);
  535. done:
  536. return svc;
  537. }
  538. EXPORT_SYMBOL(apr_register);
  539. void apr_cb_func(void *buf, int len, void *priv)
  540. {
  541. struct apr_client_data data;
  542. struct apr_client *apr_client;
  543. struct apr_svc *c_svc;
  544. struct apr_hdr *hdr;
  545. uint16_t hdr_size;
  546. uint16_t msg_type;
  547. uint16_t ver;
  548. uint16_t src;
  549. uint16_t svc;
  550. uint16_t clnt;
  551. int i;
  552. int temp_port = 0;
  553. uint32_t *ptr;
  554. pr_debug("APR2: len = %d\n", len);
  555. ptr = buf;
  556. pr_debug("\n*****************\n");
  557. for (i = 0; i < len/4; i++)
  558. pr_debug("%x ", ptr[i]);
  559. pr_debug("\n");
  560. pr_debug("\n*****************\n");
  561. if (!buf || len <= APR_HDR_SIZE) {
  562. pr_err("APR: Improper apr pkt received:%pK %d\n", buf, len);
  563. return;
  564. }
  565. hdr = buf;
  566. ver = hdr->hdr_field;
  567. ver = (ver & 0x000F);
  568. if (ver > APR_PKT_VER + 1) {
  569. pr_err("APR: Wrong version: %d\n", ver);
  570. return;
  571. }
  572. hdr_size = hdr->hdr_field;
  573. hdr_size = ((hdr_size & 0x00F0) >> 0x4) * 4;
  574. if (hdr_size < APR_HDR_SIZE) {
  575. pr_err("APR: Wrong hdr size:%d\n", hdr_size);
  576. return;
  577. }
  578. if (hdr->pkt_size < APR_HDR_SIZE) {
  579. pr_err("APR: Wrong paket size\n");
  580. return;
  581. }
  582. if (hdr->pkt_size < hdr_size) {
  583. pr_err("APR: Packet size less than header size\n");
  584. return;
  585. }
  586. msg_type = hdr->hdr_field;
  587. msg_type = (msg_type >> 0x08) & 0x0003;
  588. if (msg_type >= APR_MSG_TYPE_MAX && msg_type != APR_BASIC_RSP_RESULT) {
  589. pr_err("APR: Wrong message type: %d\n", msg_type);
  590. return;
  591. }
  592. if (hdr->src_domain >= APR_DOMAIN_MAX ||
  593. hdr->dest_domain >= APR_DOMAIN_MAX ||
  594. hdr->src_svc >= APR_SVC_MAX ||
  595. hdr->dest_svc >= APR_SVC_MAX) {
  596. pr_err("APR: Wrong APR header\n");
  597. return;
  598. }
  599. svc = hdr->dest_svc;
  600. if (hdr->src_domain == APR_DOMAIN_MODEM) {
  601. if (svc == APR_SVC_MVS || svc == APR_SVC_MVM ||
  602. svc == APR_SVC_CVS || svc == APR_SVC_CVP ||
  603. svc == APR_SVC_TEST_CLIENT)
  604. clnt = APR_CLIENT_VOICE;
  605. else {
  606. pr_err("APR: Wrong svc :%d\n", svc);
  607. return;
  608. }
  609. } else if (hdr->src_domain == APR_DOMAIN_ADSP) {
  610. if (svc == APR_SVC_AFE || svc == APR_SVC_ASM ||
  611. svc == APR_SVC_VSM || svc == APR_SVC_VPM ||
  612. svc == APR_SVC_ADM || svc == APR_SVC_ADSP_CORE ||
  613. svc == APR_SVC_USM ||
  614. svc == APR_SVC_TEST_CLIENT || svc == APR_SVC_ADSP_MVM ||
  615. svc == APR_SVC_ADSP_CVS || svc == APR_SVC_ADSP_CVP ||
  616. svc == APR_SVC_LSM)
  617. clnt = APR_CLIENT_AUDIO;
  618. else if (svc == APR_SVC_VIDC)
  619. clnt = APR_CLIENT_AUDIO;
  620. else {
  621. pr_err("APR: Wrong svc :%d\n", svc);
  622. return;
  623. }
  624. } else {
  625. pr_err("APR: Pkt from wrong source: %d\n", hdr->src_domain);
  626. return;
  627. }
  628. src = apr_get_data_src(hdr);
  629. if (src == APR_DEST_MAX)
  630. return;
  631. pr_debug("src =%d clnt = %d\n", src, clnt);
  632. apr_client = &client[src][clnt];
  633. for (i = 0; i < APR_SVC_MAX; i++)
  634. if (apr_client->svc[i].id == svc) {
  635. pr_debug("%d\n", apr_client->svc[i].id);
  636. c_svc = &apr_client->svc[i];
  637. break;
  638. }
  639. if (i == APR_SVC_MAX) {
  640. pr_err("APR: service is not registered\n");
  641. return;
  642. }
  643. pr_debug("svc_idx = %d\n", i);
  644. pr_debug("%x %x %x %pK %pK\n", c_svc->id, c_svc->dest_id,
  645. c_svc->client_id, c_svc->fn, c_svc->priv);
  646. data.payload_size = hdr->pkt_size - hdr_size;
  647. data.opcode = hdr->opcode;
  648. data.src = src;
  649. data.src_port = hdr->src_port;
  650. data.dest_port = hdr->dest_port;
  651. data.token = hdr->token;
  652. data.msg_type = msg_type;
  653. data.payload = NULL;
  654. if (data.payload_size > 0)
  655. data.payload = (char *)hdr + hdr_size;
  656. if (unlikely(apr_cf_debug)) {
  657. if (hdr->opcode == APR_BASIC_RSP_RESULT && data.payload) {
  658. uint32_t *ptr = data.payload;
  659. APR_PKT_INFO(
  660. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X] rc[0x%X]",
  661. (hdr->src_domain << 8) | hdr->src_svc,
  662. (hdr->dest_domain << 8) | hdr->dest_svc,
  663. hdr->opcode, hdr->token, ptr[1]);
  664. } else {
  665. APR_PKT_INFO(
  666. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  667. (hdr->src_domain << 8) | hdr->src_svc,
  668. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  669. hdr->token);
  670. }
  671. }
  672. temp_port = ((data.dest_port >> 8) * 8) + (data.dest_port & 0xFF);
  673. if (((temp_port >= 0) && (temp_port < APR_MAX_PORTS))
  674. && (c_svc->port_cnt && c_svc->port_fn[temp_port]))
  675. c_svc->port_fn[temp_port](&data,
  676. c_svc->port_priv[temp_port]);
  677. else if (c_svc->fn)
  678. c_svc->fn(&data, c_svc->priv);
  679. else
  680. pr_err("APR: Rxed a packet for NULL callback\n");
  681. }
  682. int apr_get_svc(const char *svc_name, int domain_id, int *client_id,
  683. int *svc_idx, int *svc_id)
  684. {
  685. int i;
  686. int size;
  687. struct apr_svc_table *tbl;
  688. int ret = 0;
  689. if (domain_id == APR_DOMAIN_ADSP) {
  690. tbl = (struct apr_svc_table *)&svc_tbl_qdsp6;
  691. size = ARRAY_SIZE(svc_tbl_qdsp6);
  692. } else {
  693. tbl = (struct apr_svc_table *)&svc_tbl_voice;
  694. size = ARRAY_SIZE(svc_tbl_voice);
  695. }
  696. for (i = 0; i < size; i++) {
  697. if (!strcmp(svc_name, tbl[i].name)) {
  698. *client_id = tbl[i].client_id;
  699. *svc_idx = tbl[i].idx;
  700. *svc_id = tbl[i].id;
  701. break;
  702. }
  703. }
  704. pr_debug("%s: svc_name = %s c_id = %d domain_id = %d\n",
  705. __func__, svc_name, *client_id, domain_id);
  706. if (i == size) {
  707. pr_err("%s: APR: Wrong svc name %s\n", __func__, svc_name);
  708. ret = -EINVAL;
  709. }
  710. return ret;
  711. }
  712. static void apr_reset_deregister(struct work_struct *work)
  713. {
  714. struct apr_svc *handle = NULL;
  715. struct apr_reset_work *apr_reset =
  716. container_of(work, struct apr_reset_work, work);
  717. handle = apr_reset->handle;
  718. pr_debug("%s:handle[%pK]\n", __func__, handle);
  719. apr_deregister(handle);
  720. kfree(apr_reset);
  721. }
  722. /**
  723. * apr_start_rx_rt - Clients call to vote for thread
  724. * priority upgrade whenever needed.
  725. *
  726. * @handle: APR service handle
  727. *
  728. * Returns 0 on success or error otherwise.
  729. */
  730. int apr_start_rx_rt(void *handle)
  731. {
  732. int rc = 0;
  733. struct apr_svc *svc = handle;
  734. uint16_t dest_id = 0;
  735. uint16_t client_id = 0;
  736. if (!svc) {
  737. pr_err("%s: Invalid APR handle\n", __func__);
  738. return -EINVAL;
  739. }
  740. mutex_lock(&svc->m_lock);
  741. dest_id = svc->dest_id;
  742. client_id = svc->client_id;
  743. if ((client_id >= APR_CLIENT_MAX) || (dest_id >= APR_DEST_MAX)) {
  744. pr_err("%s: %s invalid. client_id = %u, dest_id = %u\n",
  745. __func__,
  746. client_id >= APR_CLIENT_MAX ? "Client ID" : "Dest ID",
  747. client_id, dest_id);
  748. rc = -EINVAL;
  749. goto exit;
  750. }
  751. if (!client[dest_id][client_id].handle) {
  752. pr_err("%s: Client handle is NULL\n", __func__);
  753. rc = -EINVAL;
  754. goto exit;
  755. }
  756. rc = apr_tal_start_rx_rt(client[dest_id][client_id].handle);
  757. if (rc)
  758. pr_err("%s: failed to set RT thread priority for APR RX. rc = %d\n",
  759. __func__, rc);
  760. exit:
  761. mutex_unlock(&svc->m_lock);
  762. return rc;
  763. }
  764. EXPORT_SYMBOL(apr_start_rx_rt);
  765. /**
  766. * apr_end_rx_rt - Clients call to unvote for thread
  767. * priority upgrade (perviously voted with
  768. * apr_start_rx_rt()).
  769. *
  770. * @handle: APR service handle
  771. *
  772. * Returns 0 on success or error otherwise.
  773. */
  774. int apr_end_rx_rt(void *handle)
  775. {
  776. int rc = 0;
  777. struct apr_svc *svc = handle;
  778. uint16_t dest_id = 0;
  779. uint16_t client_id = 0;
  780. if (!svc) {
  781. pr_err("%s: Invalid APR handle\n", __func__);
  782. return -EINVAL;
  783. }
  784. mutex_lock(&svc->m_lock);
  785. dest_id = svc->dest_id;
  786. client_id = svc->client_id;
  787. if ((client_id >= APR_CLIENT_MAX) || (dest_id >= APR_DEST_MAX)) {
  788. pr_err("%s: %s invalid. client_id = %u, dest_id = %u\n",
  789. __func__,
  790. client_id >= APR_CLIENT_MAX ? "Client ID" : "Dest ID",
  791. client_id, dest_id);
  792. rc = -EINVAL;
  793. goto exit;
  794. }
  795. if (!client[dest_id][client_id].handle) {
  796. pr_err("%s: Client handle is NULL\n", __func__);
  797. rc = -EINVAL;
  798. goto exit;
  799. }
  800. rc = apr_tal_end_rx_rt(client[dest_id][client_id].handle);
  801. if (rc)
  802. pr_err("%s: failed to reset RT thread priority for APR RX. rc = %d\n",
  803. __func__, rc);
  804. exit:
  805. mutex_unlock(&svc->m_lock);
  806. return rc;
  807. }
  808. EXPORT_SYMBOL(apr_end_rx_rt);
  809. /**
  810. * apr_deregister - Clients call to de-register
  811. * from APR.
  812. *
  813. * @handle: APR service handle to de-register
  814. *
  815. * Returns 0 on success or -EINVAL on error.
  816. */
  817. int apr_deregister(void *handle)
  818. {
  819. struct apr_svc *svc = handle;
  820. struct apr_client *clnt;
  821. uint16_t dest_id;
  822. uint16_t client_id;
  823. if (!handle)
  824. return -EINVAL;
  825. mutex_lock(&svc->m_lock);
  826. if (!svc->svc_cnt) {
  827. pr_err("%s: svc already deregistered. svc = %pK\n",
  828. __func__, svc);
  829. mutex_unlock(&svc->m_lock);
  830. return -EINVAL;
  831. }
  832. dest_id = svc->dest_id;
  833. client_id = svc->client_id;
  834. clnt = &client[dest_id][client_id];
  835. if (svc->svc_cnt > 0) {
  836. if (svc->port_cnt)
  837. svc->port_cnt--;
  838. svc->svc_cnt--;
  839. if (!svc->svc_cnt) {
  840. client[dest_id][client_id].svc_cnt--;
  841. pr_debug("%s: service is reset %pK\n", __func__, svc);
  842. }
  843. }
  844. if (!svc->svc_cnt) {
  845. svc->priv = NULL;
  846. svc->id = 0;
  847. svc->fn = NULL;
  848. svc->dest_id = 0;
  849. svc->client_id = 0;
  850. svc->need_reset = 0x0;
  851. }
  852. if (client[dest_id][client_id].handle &&
  853. !client[dest_id][client_id].svc_cnt) {
  854. apr_tal_close(client[dest_id][client_id].handle);
  855. client[dest_id][client_id].handle = NULL;
  856. }
  857. mutex_unlock(&svc->m_lock);
  858. return 0;
  859. }
  860. EXPORT_SYMBOL(apr_deregister);
  861. /**
  862. * apr_reset - sets up workqueue to de-register
  863. * the given APR service handle.
  864. *
  865. * @handle: APR service handle
  866. *
  867. */
  868. void apr_reset(void *handle)
  869. {
  870. struct apr_reset_work *apr_reset_worker = NULL;
  871. if (!handle)
  872. return;
  873. pr_debug("%s: handle[%pK]\n", __func__, handle);
  874. if (apr_reset_workqueue == NULL) {
  875. pr_err("%s: apr_reset_workqueue is NULL\n", __func__);
  876. return;
  877. }
  878. apr_reset_worker = kzalloc(sizeof(struct apr_reset_work),
  879. GFP_ATOMIC);
  880. if (apr_reset_worker == NULL) {
  881. pr_err("%s: mem failure\n", __func__);
  882. return;
  883. }
  884. apr_reset_worker->handle = handle;
  885. INIT_WORK(&apr_reset_worker->work, apr_reset_deregister);
  886. queue_work(apr_reset_workqueue, &apr_reset_worker->work);
  887. }
  888. EXPORT_SYMBOL(apr_reset);
  889. /* Dispatch the Reset events to Modem and audio clients */
  890. static void dispatch_event(unsigned long code, uint16_t proc)
  891. {
  892. struct apr_client *apr_client;
  893. struct apr_client_data data;
  894. struct apr_svc *svc;
  895. uint16_t clnt;
  896. int i, j;
  897. memset(&data, 0, sizeof(data));
  898. data.opcode = RESET_EVENTS;
  899. data.reset_event = code;
  900. /* Service domain can be different from the processor */
  901. data.reset_proc = apr_get_reset_domain(proc);
  902. clnt = APR_CLIENT_AUDIO;
  903. apr_client = &client[proc][clnt];
  904. for (i = 0; i < APR_SVC_MAX; i++) {
  905. mutex_lock(&apr_client->svc[i].m_lock);
  906. if (apr_client->svc[i].fn) {
  907. apr_client->svc[i].need_reset = 0x1;
  908. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  909. }
  910. if (apr_client->svc[i].port_cnt) {
  911. svc = &(apr_client->svc[i]);
  912. svc->need_reset = 0x1;
  913. for (j = 0; j < APR_MAX_PORTS; j++)
  914. if (svc->port_fn[j])
  915. svc->port_fn[j](&data,
  916. svc->port_priv[j]);
  917. }
  918. mutex_unlock(&apr_client->svc[i].m_lock);
  919. }
  920. clnt = APR_CLIENT_VOICE;
  921. apr_client = &client[proc][clnt];
  922. for (i = 0; i < APR_SVC_MAX; i++) {
  923. mutex_lock(&apr_client->svc[i].m_lock);
  924. if (apr_client->svc[i].fn) {
  925. apr_client->svc[i].need_reset = 0x1;
  926. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  927. }
  928. if (apr_client->svc[i].port_cnt) {
  929. svc = &(apr_client->svc[i]);
  930. svc->need_reset = 0x1;
  931. for (j = 0; j < APR_MAX_PORTS; j++)
  932. if (svc->port_fn[j])
  933. svc->port_fn[j](&data,
  934. svc->port_priv[j]);
  935. }
  936. mutex_unlock(&apr_client->svc[i].m_lock);
  937. }
  938. }
  939. static int apr_notifier_service_cb(struct notifier_block *this,
  940. unsigned long opcode, void *data)
  941. {
  942. struct audio_notifier_cb_data *cb_data = data;
  943. if (cb_data == NULL) {
  944. pr_err("%s: Callback data is NULL!\n", __func__);
  945. goto done;
  946. }
  947. pr_debug("%s: Service opcode 0x%lx, domain %d\n",
  948. __func__, opcode, cb_data->domain);
  949. switch (opcode) {
  950. case AUDIO_NOTIFIER_SERVICE_DOWN:
  951. /*
  952. * Use flag to ignore down notifications during
  953. * initial boot. There is no benefit from error
  954. * recovery notifications during initial boot
  955. * up since everything is expected to be down.
  956. */
  957. spin_lock(&apr_priv->apr_lock);
  958. if (apr_priv->is_initial_boot) {
  959. spin_unlock(&apr_priv->apr_lock);
  960. break;
  961. }
  962. spin_unlock(&apr_priv->apr_lock);
  963. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  964. apr_modem_down(opcode);
  965. else
  966. apr_adsp_down(opcode);
  967. break;
  968. case AUDIO_NOTIFIER_SERVICE_UP:
  969. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  970. apr_modem_up();
  971. else
  972. apr_adsp_up();
  973. spin_lock(&apr_priv->apr_lock);
  974. apr_priv->is_initial_boot = false;
  975. spin_unlock(&apr_priv->apr_lock);
  976. break;
  977. default:
  978. break;
  979. }
  980. done:
  981. return NOTIFY_OK;
  982. }
  983. static struct notifier_block adsp_service_nb = {
  984. .notifier_call = apr_notifier_service_cb,
  985. .priority = 0,
  986. };
  987. static struct notifier_block modem_service_nb = {
  988. .notifier_call = apr_notifier_service_cb,
  989. .priority = 0,
  990. };
  991. #ifdef CONFIG_DEBUG_FS
  992. static int __init apr_debug_init(void)
  993. {
  994. debugfs_apr_debug = debugfs_create_file("msm_apr_debug",
  995. S_IFREG | 0444, NULL, NULL,
  996. &apr_debug_ops);
  997. return 0;
  998. }
  999. #else
  1000. static int __init apr_debug_init(void)
  1001. (
  1002. return 0;
  1003. )
  1004. #endif
  1005. static void apr_cleanup(void)
  1006. {
  1007. int i, j, k;
  1008. of_platform_depopulate(apr_priv->dev);
  1009. subsys_notif_deregister(subsys_name);
  1010. if (apr_reset_workqueue) {
  1011. flush_workqueue(apr_reset_workqueue);
  1012. destroy_workqueue(apr_reset_workqueue);
  1013. }
  1014. mutex_destroy(&q6.lock);
  1015. for (i = 0; i < APR_DEST_MAX; i++) {
  1016. for (j = 0; j < APR_CLIENT_MAX; j++) {
  1017. mutex_destroy(&client[i][j].m_lock);
  1018. for (k = 0; k < APR_SVC_MAX; k++)
  1019. mutex_destroy(&client[i][j].svc[k].m_lock);
  1020. }
  1021. }
  1022. debugfs_remove(debugfs_apr_debug);
  1023. }
  1024. static int apr_probe(struct platform_device *pdev)
  1025. {
  1026. int i, j, k, ret = 0;
  1027. init_waitqueue_head(&modem_wait);
  1028. apr_priv = devm_kzalloc(&pdev->dev, sizeof(*apr_priv), GFP_KERNEL);
  1029. if (!apr_priv)
  1030. return -ENOMEM;
  1031. apr_priv->dev = &pdev->dev;
  1032. spin_lock_init(&apr_priv->apr_lock);
  1033. INIT_WORK(&apr_priv->add_chld_dev_work, apr_add_child_devices);
  1034. for (i = 0; i < APR_DEST_MAX; i++)
  1035. for (j = 0; j < APR_CLIENT_MAX; j++) {
  1036. mutex_init(&client[i][j].m_lock);
  1037. for (k = 0; k < APR_SVC_MAX; k++) {
  1038. mutex_init(&client[i][j].svc[k].m_lock);
  1039. spin_lock_init(&client[i][j].svc[k].w_lock);
  1040. }
  1041. }
  1042. apr_set_subsys_state();
  1043. mutex_init(&q6.lock);
  1044. apr_reset_workqueue = create_singlethread_workqueue("apr_driver");
  1045. if (!apr_reset_workqueue) {
  1046. apr_priv = NULL;
  1047. return -ENOMEM;
  1048. }
  1049. apr_pkt_ctx = ipc_log_context_create(APR_PKT_IPC_LOG_PAGE_CNT,
  1050. "apr", 0);
  1051. if (!apr_pkt_ctx)
  1052. pr_err("%s: Unable to create ipc log context\n", __func__);
  1053. spin_lock(&apr_priv->apr_lock);
  1054. apr_priv->is_initial_boot = true;
  1055. spin_unlock(&apr_priv->apr_lock);
  1056. ret = of_property_read_string(pdev->dev.of_node,
  1057. "qcom,subsys-name",
  1058. (const char **)(&subsys_name));
  1059. if (ret) {
  1060. pr_err("%s: missing subsys-name entry in dt node\n", __func__);
  1061. return -EINVAL;
  1062. }
  1063. if (!strcmp(subsys_name, "apr_adsp")) {
  1064. subsys_notif_register("apr_adsp",
  1065. AUDIO_NOTIFIER_ADSP_DOMAIN,
  1066. &adsp_service_nb);
  1067. } else if (!strcmp(subsys_name, "apr_modem")) {
  1068. subsys_notif_register("apr_modem",
  1069. AUDIO_NOTIFIER_MODEM_DOMAIN,
  1070. &modem_service_nb);
  1071. } else {
  1072. pr_err("%s: invalid subsys-name %s\n", __func__, subsys_name);
  1073. return -EINVAL;
  1074. }
  1075. apr_tal_init();
  1076. ret = snd_event_client_register(&pdev->dev, &apr_ssr_ops, NULL);
  1077. if (ret) {
  1078. pr_err("%s: Registration with SND event fwk failed ret = %d\n",
  1079. __func__, ret);
  1080. ret = 0;
  1081. }
  1082. return apr_debug_init();
  1083. }
  1084. static int apr_remove(struct platform_device *pdev)
  1085. {
  1086. snd_event_client_deregister(&pdev->dev);
  1087. apr_cleanup();
  1088. apr_tal_exit();
  1089. apr_priv = NULL;
  1090. return 0;
  1091. }
  1092. static const struct of_device_id apr_machine_of_match[] = {
  1093. { .compatible = "qcom,msm-audio-apr", },
  1094. {},
  1095. };
  1096. static struct platform_driver apr_driver = {
  1097. .probe = apr_probe,
  1098. .remove = apr_remove,
  1099. .driver = {
  1100. .name = "audio_apr",
  1101. .owner = THIS_MODULE,
  1102. .of_match_table = apr_machine_of_match,
  1103. .suppress_bind_attrs = true,
  1104. }
  1105. };
  1106. module_platform_driver(apr_driver);
  1107. MODULE_DESCRIPTION("APR DRIVER");
  1108. MODULE_LICENSE("GPL v2");
  1109. MODULE_DEVICE_TABLE(of, apr_machine_of_match);