apr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /* Copyright (c) 2010-2014, 2016-2017 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/list.h>
  18. #include <linux/sched.h>
  19. #include <linux/wait.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/delay.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/device.h>
  27. #include <linux/slab.h>
  28. #include <linux/ipc_logging.h>
  29. #include <soc/qcom/subsystem_restart.h>
  30. #include <soc/qcom/scm.h>
  31. #include <dsp/apr_audio-v2.h>
  32. #include <dsp/audio_notifier.h>
  33. #include <ipc/apr.h>
  34. #include <ipc/apr_tal.h>
  35. #define APR_PKT_IPC_LOG_PAGE_CNT 2
  36. static struct apr_q6 q6;
  37. static struct apr_client client[APR_DEST_MAX][APR_CLIENT_MAX];
  38. static void *apr_pkt_ctx;
  39. static wait_queue_head_t dsp_wait;
  40. static wait_queue_head_t modem_wait;
  41. static bool is_modem_up;
  42. static bool is_initial_boot;
  43. /* Subsystem restart: QDSP6 data, functions */
  44. static struct workqueue_struct *apr_reset_workqueue;
  45. static void apr_reset_deregister(struct work_struct *work);
  46. static void dispatch_event(unsigned long code, uint16_t proc);
  47. struct apr_reset_work {
  48. void *handle;
  49. struct work_struct work;
  50. };
  51. static bool apr_cf_debug;
  52. #ifdef CONFIG_DEBUG_FS
  53. static struct dentry *debugfs_apr_debug;
  54. static ssize_t apr_debug_write(struct file *filp, const char __user *ubuf,
  55. size_t cnt, loff_t *ppos)
  56. {
  57. char cmd;
  58. if (copy_from_user(&cmd, ubuf, 1))
  59. return -EFAULT;
  60. apr_cf_debug = (cmd == '1') ? true : false;
  61. return cnt;
  62. }
  63. static const struct file_operations apr_debug_ops = {
  64. .write = apr_debug_write,
  65. };
  66. #endif
  67. #define APR_PKT_INFO(x...) \
  68. do { \
  69. if (apr_pkt_ctx) \
  70. ipc_log_string(apr_pkt_ctx, "<APR>: "x); \
  71. } while (0)
  72. struct apr_svc_table {
  73. char name[64];
  74. int idx;
  75. int id;
  76. int client_id;
  77. };
  78. static const struct apr_svc_table svc_tbl_qdsp6[] = {
  79. {
  80. .name = "AFE",
  81. .idx = 0,
  82. .id = APR_SVC_AFE,
  83. .client_id = APR_CLIENT_AUDIO,
  84. },
  85. {
  86. .name = "ASM",
  87. .idx = 1,
  88. .id = APR_SVC_ASM,
  89. .client_id = APR_CLIENT_AUDIO,
  90. },
  91. {
  92. .name = "ADM",
  93. .idx = 2,
  94. .id = APR_SVC_ADM,
  95. .client_id = APR_CLIENT_AUDIO,
  96. },
  97. {
  98. .name = "CORE",
  99. .idx = 3,
  100. .id = APR_SVC_ADSP_CORE,
  101. .client_id = APR_CLIENT_AUDIO,
  102. },
  103. {
  104. .name = "TEST",
  105. .idx = 4,
  106. .id = APR_SVC_TEST_CLIENT,
  107. .client_id = APR_CLIENT_AUDIO,
  108. },
  109. {
  110. .name = "MVM",
  111. .idx = 5,
  112. .id = APR_SVC_ADSP_MVM,
  113. .client_id = APR_CLIENT_AUDIO,
  114. },
  115. {
  116. .name = "CVS",
  117. .idx = 6,
  118. .id = APR_SVC_ADSP_CVS,
  119. .client_id = APR_CLIENT_AUDIO,
  120. },
  121. {
  122. .name = "CVP",
  123. .idx = 7,
  124. .id = APR_SVC_ADSP_CVP,
  125. .client_id = APR_CLIENT_AUDIO,
  126. },
  127. {
  128. .name = "USM",
  129. .idx = 8,
  130. .id = APR_SVC_USM,
  131. .client_id = APR_CLIENT_AUDIO,
  132. },
  133. {
  134. .name = "VIDC",
  135. .idx = 9,
  136. .id = APR_SVC_VIDC,
  137. },
  138. {
  139. .name = "LSM",
  140. .idx = 10,
  141. .id = APR_SVC_LSM,
  142. .client_id = APR_CLIENT_AUDIO,
  143. },
  144. };
  145. static struct apr_svc_table svc_tbl_voice[] = {
  146. {
  147. .name = "VSM",
  148. .idx = 0,
  149. .id = APR_SVC_VSM,
  150. .client_id = APR_CLIENT_VOICE,
  151. },
  152. {
  153. .name = "VPM",
  154. .idx = 1,
  155. .id = APR_SVC_VPM,
  156. .client_id = APR_CLIENT_VOICE,
  157. },
  158. {
  159. .name = "MVS",
  160. .idx = 2,
  161. .id = APR_SVC_MVS,
  162. .client_id = APR_CLIENT_VOICE,
  163. },
  164. {
  165. .name = "MVM",
  166. .idx = 3,
  167. .id = APR_SVC_MVM,
  168. .client_id = APR_CLIENT_VOICE,
  169. },
  170. {
  171. .name = "CVS",
  172. .idx = 4,
  173. .id = APR_SVC_CVS,
  174. .client_id = APR_CLIENT_VOICE,
  175. },
  176. {
  177. .name = "CVP",
  178. .idx = 5,
  179. .id = APR_SVC_CVP,
  180. .client_id = APR_CLIENT_VOICE,
  181. },
  182. {
  183. .name = "SRD",
  184. .idx = 6,
  185. .id = APR_SVC_SRD,
  186. .client_id = APR_CLIENT_VOICE,
  187. },
  188. {
  189. .name = "TEST",
  190. .idx = 7,
  191. .id = APR_SVC_TEST_CLIENT,
  192. .client_id = APR_CLIENT_VOICE,
  193. },
  194. };
  195. enum apr_subsys_state apr_get_modem_state(void)
  196. {
  197. return atomic_read(&q6.modem_state);
  198. }
  199. void apr_set_modem_state(enum apr_subsys_state state)
  200. {
  201. atomic_set(&q6.modem_state, state);
  202. }
  203. enum apr_subsys_state apr_cmpxchg_modem_state(enum apr_subsys_state prev,
  204. enum apr_subsys_state new)
  205. {
  206. return atomic_cmpxchg(&q6.modem_state, prev, new);
  207. }
  208. static void apr_modem_down(unsigned long opcode)
  209. {
  210. apr_set_modem_state(APR_SUBSYS_DOWN);
  211. dispatch_event(opcode, APR_DEST_MODEM);
  212. }
  213. static void apr_modem_up(void)
  214. {
  215. if (apr_cmpxchg_modem_state(APR_SUBSYS_DOWN, APR_SUBSYS_UP) ==
  216. APR_SUBSYS_DOWN)
  217. wake_up(&modem_wait);
  218. is_modem_up = 1;
  219. }
  220. enum apr_subsys_state apr_get_q6_state(void)
  221. {
  222. return atomic_read(&q6.q6_state);
  223. }
  224. EXPORT_SYMBOL(apr_get_q6_state);
  225. int apr_set_q6_state(enum apr_subsys_state state)
  226. {
  227. pr_debug("%s: setting adsp state %d\n", __func__, state);
  228. if (state < APR_SUBSYS_DOWN || state > APR_SUBSYS_LOADED)
  229. return -EINVAL;
  230. atomic_set(&q6.q6_state, state);
  231. return 0;
  232. }
  233. EXPORT_SYMBOL(apr_set_q6_state);
  234. enum apr_subsys_state apr_cmpxchg_q6_state(enum apr_subsys_state prev,
  235. enum apr_subsys_state new)
  236. {
  237. return atomic_cmpxchg(&q6.q6_state, prev, new);
  238. }
  239. static void apr_adsp_down(unsigned long opcode)
  240. {
  241. apr_set_q6_state(APR_SUBSYS_DOWN);
  242. dispatch_event(opcode, APR_DEST_QDSP6);
  243. }
  244. static void apr_adsp_up(void)
  245. {
  246. if (apr_cmpxchg_q6_state(APR_SUBSYS_DOWN, APR_SUBSYS_LOADED) ==
  247. APR_SUBSYS_DOWN)
  248. wake_up(&dsp_wait);
  249. }
  250. int apr_wait_for_device_up(int dest_id)
  251. {
  252. int rc = -1;
  253. if (dest_id == APR_DEST_MODEM)
  254. rc = wait_event_interruptible_timeout(modem_wait,
  255. (apr_get_modem_state() == APR_SUBSYS_UP),
  256. (1 * HZ));
  257. else if (dest_id == APR_DEST_QDSP6)
  258. rc = wait_event_interruptible_timeout(dsp_wait,
  259. (apr_get_q6_state() == APR_SUBSYS_UP),
  260. (1 * HZ));
  261. else
  262. pr_err("%s: unknown dest_id %d\n", __func__, dest_id);
  263. /* returns left time */
  264. return rc;
  265. }
  266. int apr_load_adsp_image(void)
  267. {
  268. int rc = 0;
  269. mutex_lock(&q6.lock);
  270. if (apr_get_q6_state() == APR_SUBSYS_UP) {
  271. q6.pil = subsystem_get("adsp");
  272. if (IS_ERR(q6.pil)) {
  273. rc = PTR_ERR(q6.pil);
  274. pr_err("APR: Unable to load q6 image, error:%d\n", rc);
  275. } else {
  276. apr_set_q6_state(APR_SUBSYS_LOADED);
  277. pr_debug("APR: Image is loaded, stated\n");
  278. }
  279. } else if (apr_get_q6_state() == APR_SUBSYS_LOADED) {
  280. pr_debug("APR: q6 image already loaded\n");
  281. } else {
  282. pr_debug("APR: cannot load state %d\n", apr_get_q6_state());
  283. }
  284. mutex_unlock(&q6.lock);
  285. return rc;
  286. }
  287. struct apr_client *apr_get_client(int dest_id, int client_id)
  288. {
  289. return &client[dest_id][client_id];
  290. }
  291. int apr_send_pkt(void *handle, uint32_t *buf)
  292. {
  293. struct apr_svc *svc = handle;
  294. struct apr_client *clnt;
  295. struct apr_hdr *hdr;
  296. uint16_t dest_id;
  297. uint16_t client_id;
  298. uint16_t w_len;
  299. int rc;
  300. unsigned long flags;
  301. if (!handle || !buf) {
  302. pr_err("APR: Wrong parameters\n");
  303. return -EINVAL;
  304. }
  305. if (svc->need_reset) {
  306. pr_err("apr: send_pkt service need reset\n");
  307. return -ENETRESET;
  308. }
  309. if ((svc->dest_id == APR_DEST_QDSP6) &&
  310. (apr_get_q6_state() != APR_SUBSYS_LOADED)) {
  311. pr_err("%s: Still dsp is not Up\n", __func__);
  312. return -ENETRESET;
  313. } else if ((svc->dest_id == APR_DEST_MODEM) &&
  314. (apr_get_modem_state() == APR_SUBSYS_DOWN)) {
  315. pr_err("apr: Still Modem is not Up\n");
  316. return -ENETRESET;
  317. }
  318. spin_lock_irqsave(&svc->w_lock, flags);
  319. dest_id = svc->dest_id;
  320. client_id = svc->client_id;
  321. clnt = &client[dest_id][client_id];
  322. if (!client[dest_id][client_id].handle) {
  323. pr_err("APR: Still service is not yet opened\n");
  324. spin_unlock_irqrestore(&svc->w_lock, flags);
  325. return -EINVAL;
  326. }
  327. hdr = (struct apr_hdr *)buf;
  328. hdr->src_domain = APR_DOMAIN_APPS;
  329. hdr->src_svc = svc->id;
  330. hdr->dest_domain = svc->dest_domain;
  331. hdr->dest_svc = svc->id;
  332. if (unlikely(apr_cf_debug)) {
  333. APR_PKT_INFO(
  334. "Tx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  335. (hdr->src_domain << 8) | hdr->src_svc,
  336. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  337. hdr->token);
  338. }
  339. rc = apr_tal_write(clnt->handle, buf,
  340. (struct apr_pkt_priv *)&svc->pkt_owner,
  341. hdr->pkt_size);
  342. if (rc >= 0) {
  343. w_len = rc;
  344. if (w_len != hdr->pkt_size) {
  345. pr_err("%s: Unable to write whole APR pkt successfully: %d\n",
  346. __func__, rc);
  347. rc = -EINVAL;
  348. }
  349. } else {
  350. pr_err("%s: Write APR pkt failed with error %d\n",
  351. __func__, rc);
  352. }
  353. spin_unlock_irqrestore(&svc->w_lock, flags);
  354. return rc;
  355. }
  356. int apr_pkt_config(void *handle, struct apr_pkt_cfg *cfg)
  357. {
  358. struct apr_svc *svc = (struct apr_svc *)handle;
  359. uint16_t dest_id;
  360. uint16_t client_id;
  361. struct apr_client *clnt;
  362. if (!handle) {
  363. pr_err("%s: Invalid handle\n", __func__);
  364. return -EINVAL;
  365. }
  366. if (svc->need_reset) {
  367. pr_err("%s: service need reset\n", __func__);
  368. return -ENETRESET;
  369. }
  370. svc->pkt_owner = cfg->pkt_owner;
  371. dest_id = svc->dest_id;
  372. client_id = svc->client_id;
  373. clnt = &client[dest_id][client_id];
  374. return apr_tal_rx_intents_config(clnt->handle,
  375. cfg->intents.num_of_intents, cfg->intents.size);
  376. }
  377. struct apr_svc *apr_register(char *dest, char *svc_name, apr_fn svc_fn,
  378. uint32_t src_port, void *priv)
  379. {
  380. struct apr_client *clnt;
  381. int client_id = 0;
  382. int svc_idx = 0;
  383. int svc_id = 0;
  384. int dest_id = 0;
  385. int domain_id = 0;
  386. int temp_port = 0;
  387. struct apr_svc *svc = NULL;
  388. int rc = 0;
  389. bool can_open_channel = true;
  390. if (!dest || !svc_name || !svc_fn)
  391. return NULL;
  392. if (!strcmp(dest, "ADSP"))
  393. domain_id = APR_DOMAIN_ADSP;
  394. else if (!strcmp(dest, "MODEM")) {
  395. /* Don't request for SMD channels if destination is MODEM,
  396. * as these channels are no longer used and these clients
  397. * are to listen only for MODEM SSR events
  398. */
  399. can_open_channel = false;
  400. domain_id = APR_DOMAIN_MODEM;
  401. } else {
  402. pr_err("APR: wrong destination\n");
  403. goto done;
  404. }
  405. dest_id = apr_get_dest_id(dest);
  406. if (dest_id == APR_DEST_QDSP6) {
  407. if (apr_get_q6_state() != APR_SUBSYS_LOADED) {
  408. pr_err("%s: adsp not up\n", __func__);
  409. return NULL;
  410. }
  411. pr_debug("%s: adsp Up\n", __func__);
  412. } else if (dest_id == APR_DEST_MODEM) {
  413. if (apr_get_modem_state() == APR_SUBSYS_DOWN) {
  414. if (is_modem_up) {
  415. pr_err("%s: modem shutdown due to SSR, ret",
  416. __func__);
  417. return NULL;
  418. }
  419. pr_debug("%s: Wait for modem to bootup\n", __func__);
  420. rc = apr_wait_for_device_up(APR_DEST_MODEM);
  421. if (rc == 0) {
  422. pr_err("%s: Modem is not Up\n", __func__);
  423. return NULL;
  424. }
  425. }
  426. pr_debug("%s: modem Up\n", __func__);
  427. }
  428. if (apr_get_svc(svc_name, domain_id, &client_id, &svc_idx, &svc_id)) {
  429. pr_err("%s: apr_get_svc failed\n", __func__);
  430. goto done;
  431. }
  432. clnt = &client[dest_id][client_id];
  433. mutex_lock(&clnt->m_lock);
  434. if (!clnt->handle && can_open_channel) {
  435. clnt->handle = apr_tal_open(client_id, dest_id,
  436. APR_DL_SMD, apr_cb_func, NULL);
  437. if (!clnt->handle) {
  438. svc = NULL;
  439. pr_err("APR: Unable to open handle\n");
  440. mutex_unlock(&clnt->m_lock);
  441. goto done;
  442. }
  443. }
  444. mutex_unlock(&clnt->m_lock);
  445. svc = &clnt->svc[svc_idx];
  446. mutex_lock(&svc->m_lock);
  447. clnt->id = client_id;
  448. if (svc->need_reset) {
  449. mutex_unlock(&svc->m_lock);
  450. pr_err("APR: Service needs reset\n");
  451. goto done;
  452. }
  453. svc->id = svc_id;
  454. svc->dest_id = dest_id;
  455. svc->client_id = client_id;
  456. svc->dest_domain = domain_id;
  457. svc->pkt_owner = APR_PKT_OWNER_DRIVER;
  458. if (src_port != 0xFFFFFFFF) {
  459. temp_port = ((src_port >> 8) * 8) + (src_port & 0xFF);
  460. pr_debug("port = %d t_port = %d\n", src_port, temp_port);
  461. if (temp_port >= APR_MAX_PORTS || temp_port < 0) {
  462. pr_err("APR: temp_port out of bounds\n");
  463. mutex_unlock(&svc->m_lock);
  464. return NULL;
  465. }
  466. if (!svc->svc_cnt)
  467. clnt->svc_cnt++;
  468. svc->port_cnt++;
  469. svc->port_fn[temp_port] = svc_fn;
  470. svc->port_priv[temp_port] = priv;
  471. svc->svc_cnt++;
  472. } else {
  473. if (!svc->fn) {
  474. if (!svc->svc_cnt)
  475. clnt->svc_cnt++;
  476. svc->fn = svc_fn;
  477. svc->priv = priv;
  478. svc->svc_cnt++;
  479. }
  480. }
  481. mutex_unlock(&svc->m_lock);
  482. done:
  483. return svc;
  484. }
  485. void apr_cb_func(void *buf, int len, void *priv)
  486. {
  487. struct apr_client_data data;
  488. struct apr_client *apr_client;
  489. struct apr_svc *c_svc;
  490. struct apr_hdr *hdr;
  491. uint16_t hdr_size;
  492. uint16_t msg_type;
  493. uint16_t ver;
  494. uint16_t src;
  495. uint16_t svc;
  496. uint16_t clnt;
  497. int i;
  498. int temp_port = 0;
  499. uint32_t *ptr;
  500. pr_debug("APR2: len = %d\n", len);
  501. ptr = buf;
  502. pr_debug("\n*****************\n");
  503. for (i = 0; i < len/4; i++)
  504. pr_debug("%x ", ptr[i]);
  505. pr_debug("\n");
  506. pr_debug("\n*****************\n");
  507. if (!buf || len <= APR_HDR_SIZE) {
  508. pr_err("APR: Improper apr pkt received:%pK %d\n", buf, len);
  509. return;
  510. }
  511. hdr = buf;
  512. ver = hdr->hdr_field;
  513. ver = (ver & 0x000F);
  514. if (ver > APR_PKT_VER + 1) {
  515. pr_err("APR: Wrong version: %d\n", ver);
  516. return;
  517. }
  518. hdr_size = hdr->hdr_field;
  519. hdr_size = ((hdr_size & 0x00F0) >> 0x4) * 4;
  520. if (hdr_size < APR_HDR_SIZE) {
  521. pr_err("APR: Wrong hdr size:%d\n", hdr_size);
  522. return;
  523. }
  524. if (hdr->pkt_size < APR_HDR_SIZE) {
  525. pr_err("APR: Wrong paket size\n");
  526. return;
  527. }
  528. msg_type = hdr->hdr_field;
  529. msg_type = (msg_type >> 0x08) & 0x0003;
  530. if (msg_type >= APR_MSG_TYPE_MAX && msg_type != APR_BASIC_RSP_RESULT) {
  531. pr_err("APR: Wrong message type: %d\n", msg_type);
  532. return;
  533. }
  534. if (hdr->src_domain >= APR_DOMAIN_MAX ||
  535. hdr->dest_domain >= APR_DOMAIN_MAX ||
  536. hdr->src_svc >= APR_SVC_MAX ||
  537. hdr->dest_svc >= APR_SVC_MAX) {
  538. pr_err("APR: Wrong APR header\n");
  539. return;
  540. }
  541. svc = hdr->dest_svc;
  542. if (hdr->src_domain == APR_DOMAIN_MODEM) {
  543. if (svc == APR_SVC_MVS || svc == APR_SVC_MVM ||
  544. svc == APR_SVC_CVS || svc == APR_SVC_CVP ||
  545. svc == APR_SVC_TEST_CLIENT)
  546. clnt = APR_CLIENT_VOICE;
  547. else {
  548. pr_err("APR: Wrong svc :%d\n", svc);
  549. return;
  550. }
  551. } else if (hdr->src_domain == APR_DOMAIN_ADSP) {
  552. if (svc == APR_SVC_AFE || svc == APR_SVC_ASM ||
  553. svc == APR_SVC_VSM || svc == APR_SVC_VPM ||
  554. svc == APR_SVC_ADM || svc == APR_SVC_ADSP_CORE ||
  555. svc == APR_SVC_USM ||
  556. svc == APR_SVC_TEST_CLIENT || svc == APR_SVC_ADSP_MVM ||
  557. svc == APR_SVC_ADSP_CVS || svc == APR_SVC_ADSP_CVP ||
  558. svc == APR_SVC_LSM)
  559. clnt = APR_CLIENT_AUDIO;
  560. else if (svc == APR_SVC_VIDC)
  561. clnt = APR_CLIENT_AUDIO;
  562. else {
  563. pr_err("APR: Wrong svc :%d\n", svc);
  564. return;
  565. }
  566. } else {
  567. pr_err("APR: Pkt from wrong source: %d\n", hdr->src_domain);
  568. return;
  569. }
  570. src = apr_get_data_src(hdr);
  571. if (src == APR_DEST_MAX)
  572. return;
  573. pr_debug("src =%d clnt = %d\n", src, clnt);
  574. apr_client = &client[src][clnt];
  575. for (i = 0; i < APR_SVC_MAX; i++)
  576. if (apr_client->svc[i].id == svc) {
  577. pr_debug("%d\n", apr_client->svc[i].id);
  578. c_svc = &apr_client->svc[i];
  579. break;
  580. }
  581. if (i == APR_SVC_MAX) {
  582. pr_err("APR: service is not registered\n");
  583. return;
  584. }
  585. pr_debug("svc_idx = %d\n", i);
  586. pr_debug("%x %x %x %pK %pK\n", c_svc->id, c_svc->dest_id,
  587. c_svc->client_id, c_svc->fn, c_svc->priv);
  588. data.payload_size = hdr->pkt_size - hdr_size;
  589. data.opcode = hdr->opcode;
  590. data.src = src;
  591. data.src_port = hdr->src_port;
  592. data.dest_port = hdr->dest_port;
  593. data.token = hdr->token;
  594. data.msg_type = msg_type;
  595. data.payload = NULL;
  596. if (data.payload_size > 0)
  597. data.payload = (char *)hdr + hdr_size;
  598. if (unlikely(apr_cf_debug)) {
  599. if (hdr->opcode == APR_BASIC_RSP_RESULT && data.payload) {
  600. uint32_t *ptr = data.payload;
  601. APR_PKT_INFO(
  602. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X] rc[0x%X]",
  603. (hdr->src_domain << 8) | hdr->src_svc,
  604. (hdr->dest_domain << 8) | hdr->dest_svc,
  605. hdr->opcode, hdr->token, ptr[1]);
  606. } else {
  607. APR_PKT_INFO(
  608. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  609. (hdr->src_domain << 8) | hdr->src_svc,
  610. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  611. hdr->token);
  612. }
  613. }
  614. temp_port = ((data.dest_port >> 8) * 8) + (data.dest_port & 0xFF);
  615. pr_debug("port = %d t_port = %d\n", data.src_port, temp_port);
  616. if (c_svc->port_cnt && c_svc->port_fn[temp_port])
  617. c_svc->port_fn[temp_port](&data, c_svc->port_priv[temp_port]);
  618. else if (c_svc->fn)
  619. c_svc->fn(&data, c_svc->priv);
  620. else
  621. pr_err("APR: Rxed a packet for NULL callback\n");
  622. }
  623. int apr_get_svc(const char *svc_name, int domain_id, int *client_id,
  624. int *svc_idx, int *svc_id)
  625. {
  626. int i;
  627. int size;
  628. struct apr_svc_table *tbl;
  629. int ret = 0;
  630. if (domain_id == APR_DOMAIN_ADSP) {
  631. tbl = (struct apr_svc_table *)&svc_tbl_qdsp6;
  632. size = ARRAY_SIZE(svc_tbl_qdsp6);
  633. } else {
  634. tbl = (struct apr_svc_table *)&svc_tbl_voice;
  635. size = ARRAY_SIZE(svc_tbl_voice);
  636. }
  637. for (i = 0; i < size; i++) {
  638. if (!strcmp(svc_name, tbl[i].name)) {
  639. *client_id = tbl[i].client_id;
  640. *svc_idx = tbl[i].idx;
  641. *svc_id = tbl[i].id;
  642. break;
  643. }
  644. }
  645. pr_debug("%s: svc_name = %s c_id = %d domain_id = %d\n",
  646. __func__, svc_name, *client_id, domain_id);
  647. if (i == size) {
  648. pr_err("%s: APR: Wrong svc name %s\n", __func__, svc_name);
  649. ret = -EINVAL;
  650. }
  651. return ret;
  652. }
  653. static void apr_reset_deregister(struct work_struct *work)
  654. {
  655. struct apr_svc *handle = NULL;
  656. struct apr_reset_work *apr_reset =
  657. container_of(work, struct apr_reset_work, work);
  658. handle = apr_reset->handle;
  659. pr_debug("%s:handle[%pK]\n", __func__, handle);
  660. apr_deregister(handle);
  661. kfree(apr_reset);
  662. }
  663. int apr_deregister(void *handle)
  664. {
  665. struct apr_svc *svc = handle;
  666. struct apr_client *clnt;
  667. uint16_t dest_id;
  668. uint16_t client_id;
  669. if (!handle)
  670. return -EINVAL;
  671. mutex_lock(&svc->m_lock);
  672. if (!svc->svc_cnt) {
  673. pr_err("%s: svc already deregistered. svc = %pK\n",
  674. __func__, svc);
  675. mutex_unlock(&svc->m_lock);
  676. return -EINVAL;
  677. }
  678. dest_id = svc->dest_id;
  679. client_id = svc->client_id;
  680. clnt = &client[dest_id][client_id];
  681. if (svc->svc_cnt > 0) {
  682. if (svc->port_cnt)
  683. svc->port_cnt--;
  684. svc->svc_cnt--;
  685. if (!svc->svc_cnt) {
  686. client[dest_id][client_id].svc_cnt--;
  687. pr_debug("%s: service is reset %pK\n", __func__, svc);
  688. }
  689. }
  690. if (!svc->svc_cnt) {
  691. svc->priv = NULL;
  692. svc->id = 0;
  693. svc->fn = NULL;
  694. svc->dest_id = 0;
  695. svc->client_id = 0;
  696. svc->need_reset = 0x0;
  697. }
  698. if (client[dest_id][client_id].handle &&
  699. !client[dest_id][client_id].svc_cnt) {
  700. apr_tal_close(client[dest_id][client_id].handle);
  701. client[dest_id][client_id].handle = NULL;
  702. }
  703. mutex_unlock(&svc->m_lock);
  704. return 0;
  705. }
  706. void apr_reset(void *handle)
  707. {
  708. struct apr_reset_work *apr_reset_worker = NULL;
  709. if (!handle)
  710. return;
  711. pr_debug("%s: handle[%pK]\n", __func__, handle);
  712. if (apr_reset_workqueue == NULL) {
  713. pr_err("%s: apr_reset_workqueue is NULL\n", __func__);
  714. return;
  715. }
  716. apr_reset_worker = kzalloc(sizeof(struct apr_reset_work),
  717. GFP_ATOMIC);
  718. if (apr_reset_worker == NULL) {
  719. pr_err("%s: mem failure\n", __func__);
  720. return;
  721. }
  722. apr_reset_worker->handle = handle;
  723. INIT_WORK(&apr_reset_worker->work, apr_reset_deregister);
  724. queue_work(apr_reset_workqueue, &apr_reset_worker->work);
  725. }
  726. /* Dispatch the Reset events to Modem and audio clients */
  727. static void dispatch_event(unsigned long code, uint16_t proc)
  728. {
  729. struct apr_client *apr_client;
  730. struct apr_client_data data;
  731. struct apr_svc *svc;
  732. uint16_t clnt;
  733. int i, j;
  734. data.opcode = RESET_EVENTS;
  735. data.reset_event = code;
  736. /* Service domain can be different from the processor */
  737. data.reset_proc = apr_get_reset_domain(proc);
  738. clnt = APR_CLIENT_AUDIO;
  739. apr_client = &client[proc][clnt];
  740. for (i = 0; i < APR_SVC_MAX; i++) {
  741. mutex_lock(&apr_client->svc[i].m_lock);
  742. if (apr_client->svc[i].fn) {
  743. apr_client->svc[i].need_reset = 0x1;
  744. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  745. }
  746. if (apr_client->svc[i].port_cnt) {
  747. svc = &(apr_client->svc[i]);
  748. svc->need_reset = 0x1;
  749. for (j = 0; j < APR_MAX_PORTS; j++)
  750. if (svc->port_fn[j])
  751. svc->port_fn[j](&data,
  752. svc->port_priv[j]);
  753. }
  754. mutex_unlock(&apr_client->svc[i].m_lock);
  755. }
  756. clnt = APR_CLIENT_VOICE;
  757. apr_client = &client[proc][clnt];
  758. for (i = 0; i < APR_SVC_MAX; i++) {
  759. mutex_lock(&apr_client->svc[i].m_lock);
  760. if (apr_client->svc[i].fn) {
  761. apr_client->svc[i].need_reset = 0x1;
  762. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  763. }
  764. if (apr_client->svc[i].port_cnt) {
  765. svc = &(apr_client->svc[i]);
  766. svc->need_reset = 0x1;
  767. for (j = 0; j < APR_MAX_PORTS; j++)
  768. if (svc->port_fn[j])
  769. svc->port_fn[j](&data,
  770. svc->port_priv[j]);
  771. }
  772. mutex_unlock(&apr_client->svc[i].m_lock);
  773. }
  774. }
  775. static int apr_notifier_service_cb(struct notifier_block *this,
  776. unsigned long opcode, void *data)
  777. {
  778. struct audio_notifier_cb_data *cb_data = data;
  779. if (cb_data == NULL) {
  780. pr_err("%s: Callback data is NULL!\n", __func__);
  781. goto done;
  782. }
  783. pr_debug("%s: Service opcode 0x%lx, domain %d\n",
  784. __func__, opcode, cb_data->domain);
  785. switch (opcode) {
  786. case AUDIO_NOTIFIER_SERVICE_DOWN:
  787. /*
  788. * Use flag to ignore down notifications during
  789. * initial boot. There is no benefit from error
  790. * recovery notifications during initial boot
  791. * up since everything is expected to be down.
  792. */
  793. if (is_initial_boot) {
  794. is_initial_boot = false;
  795. break;
  796. }
  797. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  798. apr_modem_down(opcode);
  799. else
  800. apr_adsp_down(opcode);
  801. break;
  802. case AUDIO_NOTIFIER_SERVICE_UP:
  803. is_initial_boot = false;
  804. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  805. apr_modem_up();
  806. else
  807. apr_adsp_up();
  808. break;
  809. default:
  810. break;
  811. }
  812. done:
  813. return NOTIFY_OK;
  814. }
  815. static struct notifier_block adsp_service_nb = {
  816. .notifier_call = apr_notifier_service_cb,
  817. .priority = 0,
  818. };
  819. static struct notifier_block modem_service_nb = {
  820. .notifier_call = apr_notifier_service_cb,
  821. .priority = 0,
  822. };
  823. static int __init apr_init(void)
  824. {
  825. int i, j, k;
  826. for (i = 0; i < APR_DEST_MAX; i++)
  827. for (j = 0; j < APR_CLIENT_MAX; j++) {
  828. mutex_init(&client[i][j].m_lock);
  829. for (k = 0; k < APR_SVC_MAX; k++) {
  830. mutex_init(&client[i][j].svc[k].m_lock);
  831. spin_lock_init(&client[i][j].svc[k].w_lock);
  832. }
  833. }
  834. apr_set_subsys_state();
  835. mutex_init(&q6.lock);
  836. apr_reset_workqueue = create_singlethread_workqueue("apr_driver");
  837. if (!apr_reset_workqueue)
  838. return -ENOMEM;
  839. apr_pkt_ctx = ipc_log_context_create(APR_PKT_IPC_LOG_PAGE_CNT,
  840. "apr", 0);
  841. if (!apr_pkt_ctx)
  842. pr_err("%s: Unable to create ipc log context\n", __func__);
  843. is_initial_boot = true;
  844. subsys_notif_register("apr_adsp", AUDIO_NOTIFIER_ADSP_DOMAIN,
  845. &adsp_service_nb);
  846. subsys_notif_register("apr_modem", AUDIO_NOTIFIER_MODEM_DOMAIN,
  847. &modem_service_nb);
  848. return 0;
  849. }
  850. device_initcall(apr_init);
  851. static int __init apr_late_init(void)
  852. {
  853. int ret = 0;
  854. init_waitqueue_head(&dsp_wait);
  855. init_waitqueue_head(&modem_wait);
  856. return ret;
  857. }
  858. late_initcall(apr_late_init);
  859. #ifdef CONFIG_DEBUG_FS
  860. static int __init apr_debug_init(void)
  861. {
  862. debugfs_apr_debug = debugfs_create_file("msm_apr_debug",
  863. S_IFREG | 0444, NULL, NULL,
  864. &apr_debug_ops);
  865. return 0;
  866. }
  867. device_initcall(apr_debug_init);
  868. #endif