apr.c 28 KB

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