apr.c 22 KB

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