apr.c 28 KB

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