apr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /* Copyright (c) 2010-2014, 2016-2018 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. /**
  196. * apr_get_modem_state:
  197. *
  198. * Returns current modem load status
  199. *
  200. */
  201. enum apr_subsys_state apr_get_modem_state(void)
  202. {
  203. return atomic_read(&q6.modem_state);
  204. }
  205. EXPORT_SYMBOL(apr_get_modem_state);
  206. /**
  207. * apr_set_modem_state - Update modem load status.
  208. *
  209. * @state: State to update modem load status
  210. *
  211. */
  212. void apr_set_modem_state(enum apr_subsys_state state)
  213. {
  214. atomic_set(&q6.modem_state, state);
  215. }
  216. EXPORT_SYMBOL(apr_set_modem_state);
  217. enum apr_subsys_state apr_cmpxchg_modem_state(enum apr_subsys_state prev,
  218. enum apr_subsys_state new)
  219. {
  220. return atomic_cmpxchg(&q6.modem_state, prev, new);
  221. }
  222. static void apr_modem_down(unsigned long opcode)
  223. {
  224. apr_set_modem_state(APR_SUBSYS_DOWN);
  225. dispatch_event(opcode, APR_DEST_MODEM);
  226. }
  227. static void apr_modem_up(void)
  228. {
  229. if (apr_cmpxchg_modem_state(APR_SUBSYS_DOWN, APR_SUBSYS_UP) ==
  230. APR_SUBSYS_DOWN)
  231. wake_up(&modem_wait);
  232. is_modem_up = 1;
  233. }
  234. enum apr_subsys_state apr_get_q6_state(void)
  235. {
  236. return atomic_read(&q6.q6_state);
  237. }
  238. EXPORT_SYMBOL(apr_get_q6_state);
  239. int apr_set_q6_state(enum apr_subsys_state state)
  240. {
  241. pr_debug("%s: setting adsp state %d\n", __func__, state);
  242. if (state < APR_SUBSYS_DOWN || state > APR_SUBSYS_LOADED)
  243. return -EINVAL;
  244. atomic_set(&q6.q6_state, state);
  245. return 0;
  246. }
  247. EXPORT_SYMBOL(apr_set_q6_state);
  248. enum apr_subsys_state apr_cmpxchg_q6_state(enum apr_subsys_state prev,
  249. enum apr_subsys_state new)
  250. {
  251. return atomic_cmpxchg(&q6.q6_state, prev, new);
  252. }
  253. static void apr_adsp_down(unsigned long opcode)
  254. {
  255. apr_set_q6_state(APR_SUBSYS_DOWN);
  256. dispatch_event(opcode, APR_DEST_QDSP6);
  257. }
  258. static void apr_adsp_up(void)
  259. {
  260. if (apr_cmpxchg_q6_state(APR_SUBSYS_DOWN, APR_SUBSYS_LOADED) ==
  261. APR_SUBSYS_DOWN)
  262. wake_up(&dsp_wait);
  263. }
  264. int apr_wait_for_device_up(int dest_id)
  265. {
  266. int rc = -1;
  267. if (dest_id == APR_DEST_MODEM)
  268. rc = wait_event_interruptible_timeout(modem_wait,
  269. (apr_get_modem_state() == APR_SUBSYS_UP),
  270. (1 * HZ));
  271. else if (dest_id == APR_DEST_QDSP6)
  272. rc = wait_event_interruptible_timeout(dsp_wait,
  273. (apr_get_q6_state() == APR_SUBSYS_UP),
  274. (1 * HZ));
  275. else
  276. pr_err("%s: unknown dest_id %d\n", __func__, dest_id);
  277. /* returns left time */
  278. return rc;
  279. }
  280. int apr_load_adsp_image(void)
  281. {
  282. int rc = 0;
  283. mutex_lock(&q6.lock);
  284. if (apr_get_q6_state() == APR_SUBSYS_UP) {
  285. q6.pil = subsystem_get("adsp");
  286. if (IS_ERR(q6.pil)) {
  287. rc = PTR_ERR(q6.pil);
  288. pr_err("APR: Unable to load q6 image, error:%d\n", rc);
  289. } else {
  290. apr_set_q6_state(APR_SUBSYS_LOADED);
  291. pr_debug("APR: Image is loaded, stated\n");
  292. }
  293. } else if (apr_get_q6_state() == APR_SUBSYS_LOADED) {
  294. pr_debug("APR: q6 image already loaded\n");
  295. } else {
  296. pr_debug("APR: cannot load state %d\n", apr_get_q6_state());
  297. }
  298. mutex_unlock(&q6.lock);
  299. return rc;
  300. }
  301. struct apr_client *apr_get_client(int dest_id, int client_id)
  302. {
  303. return &client[dest_id][client_id];
  304. }
  305. /**
  306. * apr_send_pkt - Clients call to send packet
  307. * to destination processor.
  308. *
  309. * @handle: APR service handle
  310. * @buf: payload to send to destination processor.
  311. *
  312. * Returns Bytes(>0)pkt_size on success or error on failure.
  313. */
  314. int apr_send_pkt(void *handle, uint32_t *buf)
  315. {
  316. struct apr_svc *svc = handle;
  317. struct apr_client *clnt;
  318. struct apr_hdr *hdr;
  319. uint16_t dest_id;
  320. uint16_t client_id;
  321. uint16_t w_len;
  322. int rc;
  323. unsigned long flags;
  324. if (!handle || !buf) {
  325. pr_err("APR: Wrong parameters\n");
  326. return -EINVAL;
  327. }
  328. if (svc->need_reset) {
  329. pr_err("apr: send_pkt service need reset\n");
  330. return -ENETRESET;
  331. }
  332. if ((svc->dest_id == APR_DEST_QDSP6) &&
  333. (apr_get_q6_state() != APR_SUBSYS_LOADED)) {
  334. pr_err("%s: Still dsp is not Up\n", __func__);
  335. return -ENETRESET;
  336. } else if ((svc->dest_id == APR_DEST_MODEM) &&
  337. (apr_get_modem_state() == APR_SUBSYS_DOWN)) {
  338. pr_err("apr: Still Modem is not Up\n");
  339. return -ENETRESET;
  340. }
  341. spin_lock_irqsave(&svc->w_lock, flags);
  342. dest_id = svc->dest_id;
  343. client_id = svc->client_id;
  344. clnt = &client[dest_id][client_id];
  345. if (!client[dest_id][client_id].handle) {
  346. pr_err("APR: Still service is not yet opened\n");
  347. spin_unlock_irqrestore(&svc->w_lock, flags);
  348. return -EINVAL;
  349. }
  350. hdr = (struct apr_hdr *)buf;
  351. hdr->src_domain = APR_DOMAIN_APPS;
  352. hdr->src_svc = svc->id;
  353. hdr->dest_domain = svc->dest_domain;
  354. hdr->dest_svc = svc->id;
  355. if (unlikely(apr_cf_debug)) {
  356. APR_PKT_INFO(
  357. "Tx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  358. (hdr->src_domain << 8) | hdr->src_svc,
  359. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  360. hdr->token);
  361. }
  362. rc = apr_tal_write(clnt->handle, buf,
  363. (struct apr_pkt_priv *)&svc->pkt_owner,
  364. hdr->pkt_size);
  365. if (rc >= 0) {
  366. w_len = rc;
  367. if (w_len != hdr->pkt_size) {
  368. pr_err("%s: Unable to write whole APR pkt successfully: %d\n",
  369. __func__, rc);
  370. rc = -EINVAL;
  371. }
  372. } else {
  373. pr_err("%s: Write APR pkt failed with error %d\n",
  374. __func__, rc);
  375. }
  376. spin_unlock_irqrestore(&svc->w_lock, flags);
  377. return rc;
  378. }
  379. EXPORT_SYMBOL(apr_send_pkt);
  380. int apr_pkt_config(void *handle, struct apr_pkt_cfg *cfg)
  381. {
  382. struct apr_svc *svc = (struct apr_svc *)handle;
  383. uint16_t dest_id;
  384. uint16_t client_id;
  385. struct apr_client *clnt;
  386. if (!handle) {
  387. pr_err("%s: Invalid handle\n", __func__);
  388. return -EINVAL;
  389. }
  390. if (svc->need_reset) {
  391. pr_err("%s: service need reset\n", __func__);
  392. return -ENETRESET;
  393. }
  394. svc->pkt_owner = cfg->pkt_owner;
  395. dest_id = svc->dest_id;
  396. client_id = svc->client_id;
  397. clnt = &client[dest_id][client_id];
  398. return apr_tal_rx_intents_config(clnt->handle,
  399. cfg->intents.num_of_intents, cfg->intents.size);
  400. }
  401. /**
  402. * apr_register - Clients call to register
  403. * to APR.
  404. *
  405. * @dest: destination processor
  406. * @svc_name: name of service to register as
  407. * @svc_fn: callback function to trigger when response
  408. * ack or packets received from destination processor.
  409. * @src_port: Port number within a service
  410. * @priv: private data of client, passed back in cb fn.
  411. *
  412. * Returns apr_svc handle on success or NULL on failure.
  413. */
  414. struct apr_svc *apr_register(char *dest, char *svc_name, apr_fn svc_fn,
  415. uint32_t src_port, void *priv)
  416. {
  417. struct apr_client *clnt;
  418. int client_id = 0;
  419. int svc_idx = 0;
  420. int svc_id = 0;
  421. int dest_id = 0;
  422. int domain_id = 0;
  423. int temp_port = 0;
  424. struct apr_svc *svc = NULL;
  425. int rc = 0;
  426. bool can_open_channel = true;
  427. if (!dest || !svc_name || !svc_fn)
  428. return NULL;
  429. if (!strcmp(dest, "ADSP"))
  430. domain_id = APR_DOMAIN_ADSP;
  431. else if (!strcmp(dest, "MODEM")) {
  432. /* Don't request for SMD channels if destination is MODEM,
  433. * as these channels are no longer used and these clients
  434. * are to listen only for MODEM SSR events
  435. */
  436. can_open_channel = false;
  437. domain_id = APR_DOMAIN_MODEM;
  438. } else {
  439. pr_err("APR: wrong destination\n");
  440. goto done;
  441. }
  442. dest_id = apr_get_dest_id(dest);
  443. if (dest_id == APR_DEST_QDSP6) {
  444. if (apr_get_q6_state() != APR_SUBSYS_LOADED) {
  445. pr_err("%s: adsp not up\n", __func__);
  446. return NULL;
  447. }
  448. pr_debug("%s: adsp Up\n", __func__);
  449. } else if (dest_id == APR_DEST_MODEM) {
  450. if (apr_get_modem_state() == APR_SUBSYS_DOWN) {
  451. if (is_modem_up) {
  452. pr_err("%s: modem shutdown due to SSR, ret",
  453. __func__);
  454. return NULL;
  455. }
  456. pr_debug("%s: Wait for modem to bootup\n", __func__);
  457. rc = apr_wait_for_device_up(APR_DEST_MODEM);
  458. if (rc == 0) {
  459. pr_err("%s: Modem is not Up\n", __func__);
  460. return NULL;
  461. }
  462. }
  463. pr_debug("%s: modem Up\n", __func__);
  464. }
  465. if (apr_get_svc(svc_name, domain_id, &client_id, &svc_idx, &svc_id)) {
  466. pr_err("%s: apr_get_svc failed\n", __func__);
  467. goto done;
  468. }
  469. clnt = &client[dest_id][client_id];
  470. mutex_lock(&clnt->m_lock);
  471. if (!clnt->handle && can_open_channel) {
  472. clnt->handle = apr_tal_open(client_id, dest_id,
  473. APR_DL_SMD, apr_cb_func, NULL);
  474. if (!clnt->handle) {
  475. svc = NULL;
  476. pr_err("APR: Unable to open handle\n");
  477. mutex_unlock(&clnt->m_lock);
  478. goto done;
  479. }
  480. }
  481. mutex_unlock(&clnt->m_lock);
  482. svc = &clnt->svc[svc_idx];
  483. mutex_lock(&svc->m_lock);
  484. clnt->id = client_id;
  485. if (svc->need_reset) {
  486. mutex_unlock(&svc->m_lock);
  487. pr_err("APR: Service needs reset\n");
  488. goto done;
  489. }
  490. svc->id = svc_id;
  491. svc->dest_id = dest_id;
  492. svc->client_id = client_id;
  493. svc->dest_domain = domain_id;
  494. svc->pkt_owner = APR_PKT_OWNER_DRIVER;
  495. if (src_port != 0xFFFFFFFF) {
  496. temp_port = ((src_port >> 8) * 8) + (src_port & 0xFF);
  497. pr_debug("port = %d t_port = %d\n", src_port, temp_port);
  498. if (temp_port >= APR_MAX_PORTS || temp_port < 0) {
  499. pr_err("APR: temp_port out of bounds\n");
  500. mutex_unlock(&svc->m_lock);
  501. return NULL;
  502. }
  503. if (!svc->svc_cnt)
  504. clnt->svc_cnt++;
  505. svc->port_cnt++;
  506. svc->port_fn[temp_port] = svc_fn;
  507. svc->port_priv[temp_port] = priv;
  508. svc->svc_cnt++;
  509. } else {
  510. if (!svc->fn) {
  511. if (!svc->svc_cnt)
  512. clnt->svc_cnt++;
  513. svc->fn = svc_fn;
  514. svc->priv = priv;
  515. svc->svc_cnt++;
  516. }
  517. }
  518. mutex_unlock(&svc->m_lock);
  519. done:
  520. return svc;
  521. }
  522. EXPORT_SYMBOL(apr_register);
  523. void apr_cb_func(void *buf, int len, void *priv)
  524. {
  525. struct apr_client_data data;
  526. struct apr_client *apr_client;
  527. struct apr_svc *c_svc;
  528. struct apr_hdr *hdr;
  529. uint16_t hdr_size;
  530. uint16_t msg_type;
  531. uint16_t ver;
  532. uint16_t src;
  533. uint16_t svc;
  534. uint16_t clnt;
  535. int i;
  536. int temp_port = 0;
  537. uint32_t *ptr;
  538. pr_debug("APR2: len = %d\n", len);
  539. ptr = buf;
  540. pr_debug("\n*****************\n");
  541. for (i = 0; i < len/4; i++)
  542. pr_debug("%x ", ptr[i]);
  543. pr_debug("\n");
  544. pr_debug("\n*****************\n");
  545. if (!buf || len <= APR_HDR_SIZE) {
  546. pr_err("APR: Improper apr pkt received:%pK %d\n", buf, len);
  547. return;
  548. }
  549. hdr = buf;
  550. ver = hdr->hdr_field;
  551. ver = (ver & 0x000F);
  552. if (ver > APR_PKT_VER + 1) {
  553. pr_err("APR: Wrong version: %d\n", ver);
  554. return;
  555. }
  556. hdr_size = hdr->hdr_field;
  557. hdr_size = ((hdr_size & 0x00F0) >> 0x4) * 4;
  558. if (hdr_size < APR_HDR_SIZE) {
  559. pr_err("APR: Wrong hdr size:%d\n", hdr_size);
  560. return;
  561. }
  562. if (hdr->pkt_size < APR_HDR_SIZE) {
  563. pr_err("APR: Wrong paket size\n");
  564. return;
  565. }
  566. msg_type = hdr->hdr_field;
  567. msg_type = (msg_type >> 0x08) & 0x0003;
  568. if (msg_type >= APR_MSG_TYPE_MAX && msg_type != APR_BASIC_RSP_RESULT) {
  569. pr_err("APR: Wrong message type: %d\n", msg_type);
  570. return;
  571. }
  572. if (hdr->src_domain >= APR_DOMAIN_MAX ||
  573. hdr->dest_domain >= APR_DOMAIN_MAX ||
  574. hdr->src_svc >= APR_SVC_MAX ||
  575. hdr->dest_svc >= APR_SVC_MAX) {
  576. pr_err("APR: Wrong APR header\n");
  577. return;
  578. }
  579. svc = hdr->dest_svc;
  580. if (hdr->src_domain == APR_DOMAIN_MODEM) {
  581. if (svc == APR_SVC_MVS || svc == APR_SVC_MVM ||
  582. svc == APR_SVC_CVS || svc == APR_SVC_CVP ||
  583. svc == APR_SVC_TEST_CLIENT)
  584. clnt = APR_CLIENT_VOICE;
  585. else {
  586. pr_err("APR: Wrong svc :%d\n", svc);
  587. return;
  588. }
  589. } else if (hdr->src_domain == APR_DOMAIN_ADSP) {
  590. if (svc == APR_SVC_AFE || svc == APR_SVC_ASM ||
  591. svc == APR_SVC_VSM || svc == APR_SVC_VPM ||
  592. svc == APR_SVC_ADM || svc == APR_SVC_ADSP_CORE ||
  593. svc == APR_SVC_USM ||
  594. svc == APR_SVC_TEST_CLIENT || svc == APR_SVC_ADSP_MVM ||
  595. svc == APR_SVC_ADSP_CVS || svc == APR_SVC_ADSP_CVP ||
  596. svc == APR_SVC_LSM)
  597. clnt = APR_CLIENT_AUDIO;
  598. else if (svc == APR_SVC_VIDC)
  599. clnt = APR_CLIENT_AUDIO;
  600. else {
  601. pr_err("APR: Wrong svc :%d\n", svc);
  602. return;
  603. }
  604. } else {
  605. pr_err("APR: Pkt from wrong source: %d\n", hdr->src_domain);
  606. return;
  607. }
  608. src = apr_get_data_src(hdr);
  609. if (src == APR_DEST_MAX)
  610. return;
  611. pr_debug("src =%d clnt = %d\n", src, clnt);
  612. apr_client = &client[src][clnt];
  613. for (i = 0; i < APR_SVC_MAX; i++)
  614. if (apr_client->svc[i].id == svc) {
  615. pr_debug("%d\n", apr_client->svc[i].id);
  616. c_svc = &apr_client->svc[i];
  617. break;
  618. }
  619. if (i == APR_SVC_MAX) {
  620. pr_err("APR: service is not registered\n");
  621. return;
  622. }
  623. pr_debug("svc_idx = %d\n", i);
  624. pr_debug("%x %x %x %pK %pK\n", c_svc->id, c_svc->dest_id,
  625. c_svc->client_id, c_svc->fn, c_svc->priv);
  626. data.payload_size = hdr->pkt_size - hdr_size;
  627. data.opcode = hdr->opcode;
  628. data.src = src;
  629. data.src_port = hdr->src_port;
  630. data.dest_port = hdr->dest_port;
  631. data.token = hdr->token;
  632. data.msg_type = msg_type;
  633. data.payload = NULL;
  634. if (data.payload_size > 0)
  635. data.payload = (char *)hdr + hdr_size;
  636. if (unlikely(apr_cf_debug)) {
  637. if (hdr->opcode == APR_BASIC_RSP_RESULT && data.payload) {
  638. uint32_t *ptr = data.payload;
  639. APR_PKT_INFO(
  640. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X] rc[0x%X]",
  641. (hdr->src_domain << 8) | hdr->src_svc,
  642. (hdr->dest_domain << 8) | hdr->dest_svc,
  643. hdr->opcode, hdr->token, ptr[1]);
  644. } else {
  645. APR_PKT_INFO(
  646. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  647. (hdr->src_domain << 8) | hdr->src_svc,
  648. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  649. hdr->token);
  650. }
  651. }
  652. temp_port = ((data.dest_port >> 8) * 8) + (data.dest_port & 0xFF);
  653. if (((temp_port >= 0) && (temp_port < APR_MAX_PORTS))
  654. && (c_svc->port_cnt && c_svc->port_fn[temp_port]))
  655. c_svc->port_fn[temp_port](&data,
  656. c_svc->port_priv[temp_port]);
  657. else if (c_svc->fn)
  658. c_svc->fn(&data, c_svc->priv);
  659. else
  660. pr_err("APR: Rxed a packet for NULL callback\n");
  661. }
  662. int apr_get_svc(const char *svc_name, int domain_id, int *client_id,
  663. int *svc_idx, int *svc_id)
  664. {
  665. int i;
  666. int size;
  667. struct apr_svc_table *tbl;
  668. int ret = 0;
  669. if (domain_id == APR_DOMAIN_ADSP) {
  670. tbl = (struct apr_svc_table *)&svc_tbl_qdsp6;
  671. size = ARRAY_SIZE(svc_tbl_qdsp6);
  672. } else {
  673. tbl = (struct apr_svc_table *)&svc_tbl_voice;
  674. size = ARRAY_SIZE(svc_tbl_voice);
  675. }
  676. for (i = 0; i < size; i++) {
  677. if (!strcmp(svc_name, tbl[i].name)) {
  678. *client_id = tbl[i].client_id;
  679. *svc_idx = tbl[i].idx;
  680. *svc_id = tbl[i].id;
  681. break;
  682. }
  683. }
  684. pr_debug("%s: svc_name = %s c_id = %d domain_id = %d\n",
  685. __func__, svc_name, *client_id, domain_id);
  686. if (i == size) {
  687. pr_err("%s: APR: Wrong svc name %s\n", __func__, svc_name);
  688. ret = -EINVAL;
  689. }
  690. return ret;
  691. }
  692. static void apr_reset_deregister(struct work_struct *work)
  693. {
  694. struct apr_svc *handle = NULL;
  695. struct apr_reset_work *apr_reset =
  696. container_of(work, struct apr_reset_work, work);
  697. handle = apr_reset->handle;
  698. pr_debug("%s:handle[%pK]\n", __func__, handle);
  699. apr_deregister(handle);
  700. kfree(apr_reset);
  701. }
  702. /**
  703. * apr_start_rx_rt - Clients call to vote for thread
  704. * priority upgrade whenever needed.
  705. *
  706. * @handle: APR service handle
  707. *
  708. * Returns 0 on success or error otherwise.
  709. */
  710. int apr_start_rx_rt(void *handle)
  711. {
  712. int rc = 0;
  713. struct apr_svc *svc = handle;
  714. uint16_t dest_id = 0;
  715. uint16_t client_id = 0;
  716. if (!svc) {
  717. pr_err("%s: Invalid APR handle\n", __func__);
  718. return -EINVAL;
  719. }
  720. mutex_lock(&svc->m_lock);
  721. dest_id = svc->dest_id;
  722. client_id = svc->client_id;
  723. if ((client_id >= APR_CLIENT_MAX) || (dest_id >= APR_DEST_MAX)) {
  724. pr_err("%s: %s invalid. client_id = %u, dest_id = %u\n",
  725. __func__,
  726. client_id >= APR_CLIENT_MAX ? "Client ID" : "Dest ID",
  727. client_id, dest_id);
  728. rc = -EINVAL;
  729. goto exit;
  730. }
  731. if (!client[dest_id][client_id].handle) {
  732. pr_err("%s: Client handle is NULL\n", __func__);
  733. rc = -EINVAL;
  734. goto exit;
  735. }
  736. rc = apr_tal_start_rx_rt(client[dest_id][client_id].handle);
  737. if (rc)
  738. pr_err("%s: failed to set RT thread priority for APR RX. rc = %d\n",
  739. __func__, rc);
  740. exit:
  741. mutex_unlock(&svc->m_lock);
  742. return rc;
  743. }
  744. EXPORT_SYMBOL(apr_start_rx_rt);
  745. /**
  746. * apr_end_rx_rt - Clients call to unvote for thread
  747. * priority upgrade (perviously voted with
  748. * apr_start_rx_rt()).
  749. *
  750. * @handle: APR service handle
  751. *
  752. * Returns 0 on success or error otherwise.
  753. */
  754. int apr_end_rx_rt(void *handle)
  755. {
  756. int rc = 0;
  757. struct apr_svc *svc = handle;
  758. uint16_t dest_id = 0;
  759. uint16_t client_id = 0;
  760. if (!svc) {
  761. pr_err("%s: Invalid APR handle\n", __func__);
  762. return -EINVAL;
  763. }
  764. mutex_lock(&svc->m_lock);
  765. dest_id = svc->dest_id;
  766. client_id = svc->client_id;
  767. if ((client_id >= APR_CLIENT_MAX) || (dest_id >= APR_DEST_MAX)) {
  768. pr_err("%s: %s invalid. client_id = %u, dest_id = %u\n",
  769. __func__,
  770. client_id >= APR_CLIENT_MAX ? "Client ID" : "Dest ID",
  771. client_id, dest_id);
  772. rc = -EINVAL;
  773. goto exit;
  774. }
  775. if (!client[dest_id][client_id].handle) {
  776. pr_err("%s: Client handle is NULL\n", __func__);
  777. rc = -EINVAL;
  778. goto exit;
  779. }
  780. rc = apr_tal_end_rx_rt(client[dest_id][client_id].handle);
  781. if (rc)
  782. pr_err("%s: failed to reset RT thread priority for APR RX. rc = %d\n",
  783. __func__, rc);
  784. exit:
  785. mutex_unlock(&svc->m_lock);
  786. return rc;
  787. }
  788. EXPORT_SYMBOL(apr_end_rx_rt);
  789. /**
  790. * apr_deregister - Clients call to de-register
  791. * from APR.
  792. *
  793. * @handle: APR service handle to de-register
  794. *
  795. * Returns 0 on success or -EINVAL on error.
  796. */
  797. int apr_deregister(void *handle)
  798. {
  799. struct apr_svc *svc = handle;
  800. struct apr_client *clnt;
  801. uint16_t dest_id;
  802. uint16_t client_id;
  803. if (!handle)
  804. return -EINVAL;
  805. mutex_lock(&svc->m_lock);
  806. if (!svc->svc_cnt) {
  807. pr_err("%s: svc already deregistered. svc = %pK\n",
  808. __func__, svc);
  809. mutex_unlock(&svc->m_lock);
  810. return -EINVAL;
  811. }
  812. dest_id = svc->dest_id;
  813. client_id = svc->client_id;
  814. clnt = &client[dest_id][client_id];
  815. if (svc->svc_cnt > 0) {
  816. if (svc->port_cnt)
  817. svc->port_cnt--;
  818. svc->svc_cnt--;
  819. if (!svc->svc_cnt) {
  820. client[dest_id][client_id].svc_cnt--;
  821. pr_debug("%s: service is reset %pK\n", __func__, svc);
  822. }
  823. }
  824. if (!svc->svc_cnt) {
  825. svc->priv = NULL;
  826. svc->id = 0;
  827. svc->fn = NULL;
  828. svc->dest_id = 0;
  829. svc->client_id = 0;
  830. svc->need_reset = 0x0;
  831. }
  832. if (client[dest_id][client_id].handle &&
  833. !client[dest_id][client_id].svc_cnt) {
  834. apr_tal_close(client[dest_id][client_id].handle);
  835. client[dest_id][client_id].handle = NULL;
  836. }
  837. mutex_unlock(&svc->m_lock);
  838. return 0;
  839. }
  840. EXPORT_SYMBOL(apr_deregister);
  841. /**
  842. * apr_reset - sets up workqueue to de-register
  843. * the given APR service handle.
  844. *
  845. * @handle: APR service handle
  846. *
  847. */
  848. void apr_reset(void *handle)
  849. {
  850. struct apr_reset_work *apr_reset_worker = NULL;
  851. if (!handle)
  852. return;
  853. pr_debug("%s: handle[%pK]\n", __func__, handle);
  854. if (apr_reset_workqueue == NULL) {
  855. pr_err("%s: apr_reset_workqueue is NULL\n", __func__);
  856. return;
  857. }
  858. apr_reset_worker = kzalloc(sizeof(struct apr_reset_work),
  859. GFP_ATOMIC);
  860. if (apr_reset_worker == NULL) {
  861. pr_err("%s: mem failure\n", __func__);
  862. return;
  863. }
  864. apr_reset_worker->handle = handle;
  865. INIT_WORK(&apr_reset_worker->work, apr_reset_deregister);
  866. queue_work(apr_reset_workqueue, &apr_reset_worker->work);
  867. }
  868. EXPORT_SYMBOL(apr_reset);
  869. /* Dispatch the Reset events to Modem and audio clients */
  870. static void dispatch_event(unsigned long code, uint16_t proc)
  871. {
  872. struct apr_client *apr_client;
  873. struct apr_client_data data;
  874. struct apr_svc *svc;
  875. uint16_t clnt;
  876. int i, j;
  877. memset(&data, 0, sizeof(data));
  878. data.opcode = RESET_EVENTS;
  879. data.reset_event = code;
  880. /* Service domain can be different from the processor */
  881. data.reset_proc = apr_get_reset_domain(proc);
  882. clnt = APR_CLIENT_AUDIO;
  883. apr_client = &client[proc][clnt];
  884. for (i = 0; i < APR_SVC_MAX; i++) {
  885. mutex_lock(&apr_client->svc[i].m_lock);
  886. if (apr_client->svc[i].fn) {
  887. apr_client->svc[i].need_reset = 0x1;
  888. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  889. }
  890. if (apr_client->svc[i].port_cnt) {
  891. svc = &(apr_client->svc[i]);
  892. svc->need_reset = 0x1;
  893. for (j = 0; j < APR_MAX_PORTS; j++)
  894. if (svc->port_fn[j])
  895. svc->port_fn[j](&data,
  896. svc->port_priv[j]);
  897. }
  898. mutex_unlock(&apr_client->svc[i].m_lock);
  899. }
  900. clnt = APR_CLIENT_VOICE;
  901. apr_client = &client[proc][clnt];
  902. for (i = 0; i < APR_SVC_MAX; i++) {
  903. mutex_lock(&apr_client->svc[i].m_lock);
  904. if (apr_client->svc[i].fn) {
  905. apr_client->svc[i].need_reset = 0x1;
  906. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  907. }
  908. if (apr_client->svc[i].port_cnt) {
  909. svc = &(apr_client->svc[i]);
  910. svc->need_reset = 0x1;
  911. for (j = 0; j < APR_MAX_PORTS; j++)
  912. if (svc->port_fn[j])
  913. svc->port_fn[j](&data,
  914. svc->port_priv[j]);
  915. }
  916. mutex_unlock(&apr_client->svc[i].m_lock);
  917. }
  918. }
  919. static int apr_notifier_service_cb(struct notifier_block *this,
  920. unsigned long opcode, void *data)
  921. {
  922. struct audio_notifier_cb_data *cb_data = data;
  923. if (cb_data == NULL) {
  924. pr_err("%s: Callback data is NULL!\n", __func__);
  925. goto done;
  926. }
  927. pr_debug("%s: Service opcode 0x%lx, domain %d\n",
  928. __func__, opcode, cb_data->domain);
  929. switch (opcode) {
  930. case AUDIO_NOTIFIER_SERVICE_DOWN:
  931. /*
  932. * Use flag to ignore down notifications during
  933. * initial boot. There is no benefit from error
  934. * recovery notifications during initial boot
  935. * up since everything is expected to be down.
  936. */
  937. if (is_initial_boot) {
  938. is_initial_boot = false;
  939. break;
  940. }
  941. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  942. apr_modem_down(opcode);
  943. else
  944. apr_adsp_down(opcode);
  945. break;
  946. case AUDIO_NOTIFIER_SERVICE_UP:
  947. is_initial_boot = false;
  948. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  949. apr_modem_up();
  950. else
  951. apr_adsp_up();
  952. break;
  953. default:
  954. break;
  955. }
  956. done:
  957. return NOTIFY_OK;
  958. }
  959. static struct notifier_block adsp_service_nb = {
  960. .notifier_call = apr_notifier_service_cb,
  961. .priority = 0,
  962. };
  963. static struct notifier_block modem_service_nb = {
  964. .notifier_call = apr_notifier_service_cb,
  965. .priority = 0,
  966. };
  967. #ifdef CONFIG_DEBUG_FS
  968. static int __init apr_debug_init(void)
  969. {
  970. debugfs_apr_debug = debugfs_create_file("msm_apr_debug",
  971. S_IFREG | 0444, NULL, NULL,
  972. &apr_debug_ops);
  973. return 0;
  974. }
  975. #else
  976. static int __init apr_debug_init(void)
  977. (
  978. return 0;
  979. )
  980. #endif
  981. static int __init apr_init(void)
  982. {
  983. int i, j, k;
  984. init_waitqueue_head(&dsp_wait);
  985. init_waitqueue_head(&modem_wait);
  986. for (i = 0; i < APR_DEST_MAX; i++)
  987. for (j = 0; j < APR_CLIENT_MAX; j++) {
  988. mutex_init(&client[i][j].m_lock);
  989. for (k = 0; k < APR_SVC_MAX; k++) {
  990. mutex_init(&client[i][j].svc[k].m_lock);
  991. spin_lock_init(&client[i][j].svc[k].w_lock);
  992. }
  993. }
  994. apr_set_subsys_state();
  995. mutex_init(&q6.lock);
  996. apr_reset_workqueue = create_singlethread_workqueue("apr_driver");
  997. if (!apr_reset_workqueue)
  998. return -ENOMEM;
  999. apr_pkt_ctx = ipc_log_context_create(APR_PKT_IPC_LOG_PAGE_CNT,
  1000. "apr", 0);
  1001. if (!apr_pkt_ctx)
  1002. pr_err("%s: Unable to create ipc log context\n", __func__);
  1003. is_initial_boot = true;
  1004. subsys_notif_register("apr_adsp", AUDIO_NOTIFIER_ADSP_DOMAIN,
  1005. &adsp_service_nb);
  1006. subsys_notif_register("apr_modem", AUDIO_NOTIFIER_MODEM_DOMAIN,
  1007. &modem_service_nb);
  1008. apr_tal_init();
  1009. return apr_debug_init();
  1010. }
  1011. module_init(apr_init);
  1012. void __exit apr_exit(void)
  1013. {
  1014. subsys_notif_deregister("apr_modem");
  1015. subsys_notif_deregister("apr_adsp");
  1016. apr_tal_exit();
  1017. }
  1018. module_exit(apr_exit);
  1019. MODULE_DESCRIPTION("APR module");
  1020. MODULE_LICENSE("GPL v2");