apr_vm.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2014, 2016-2019 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. #include <ipc/aprv2_vm.h>
  32. #include <linux/habmm.h>
  33. #define APR_PKT_IPC_LOG_PAGE_CNT 2
  34. #define APR_VM_CB_THREAD_NAME "apr_vm_cb_thread"
  35. #define APR_TX_BUF_SIZE 4096
  36. #define APR_RX_BUF_SIZE 4096
  37. static struct apr_q6 q6;
  38. static struct apr_client client[APR_DEST_MAX][APR_CLIENT_MAX];
  39. static void *apr_pkt_ctx;
  40. static wait_queue_head_t modem_wait;
  41. static bool is_modem_up;
  42. static char *subsys_name = NULL;
  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. struct apr_chld_device {
  52. struct platform_device *pdev;
  53. struct list_head node;
  54. };
  55. struct apr_private {
  56. struct device *dev;
  57. spinlock_t apr_lock;
  58. bool is_initial_boot;
  59. struct work_struct add_chld_dev_work;
  60. };
  61. static struct apr_private *apr_priv;
  62. static bool apr_cf_debug;
  63. #ifdef CONFIG_DEBUG_FS
  64. static struct dentry *debugfs_apr_debug;
  65. static ssize_t apr_debug_write(struct file *filp, const char __user *ubuf,
  66. size_t cnt, loff_t *ppos)
  67. {
  68. char cmd;
  69. if (copy_from_user(&cmd, ubuf, 1))
  70. return -EFAULT;
  71. apr_cf_debug = (cmd == '1') ? true : false;
  72. return cnt;
  73. }
  74. static const struct file_operations apr_debug_ops = {
  75. .write = apr_debug_write,
  76. };
  77. #endif
  78. #define APR_PKT_INFO(x...) \
  79. do { \
  80. if (apr_pkt_ctx) \
  81. ipc_log_string(apr_pkt_ctx, "<APR>: "x); \
  82. } while (0)
  83. /* hab handle */
  84. static uint32_t hab_handle_tx;
  85. static uint32_t hab_handle_rx;
  86. static char apr_tx_buf[APR_TX_BUF_SIZE];
  87. static char apr_rx_buf[APR_RX_BUF_SIZE];
  88. static spinlock_t hab_tx_lock;
  89. /* apr callback thread task */
  90. static struct task_struct *apr_vm_cb_thread_task;
  91. static int pid;
  92. struct apr_svc_table {
  93. char name[64];
  94. int idx;
  95. int id;
  96. int dest_svc;
  97. int client_id;
  98. int handle;
  99. };
  100. /*
  101. * src svc should be assigned dynamically through apr registration:
  102. * 1. replace with a proper string name for registration.
  103. * e.g. "qcom.apps.lnx." + name
  104. * 2. register apr BE, retrieve dynamic src svc address,
  105. * apr handle and store in svc tbl.
  106. */
  107. static struct mutex m_lock_tbl_qdsp6;
  108. static struct apr_svc_table svc_tbl_qdsp6[] = {
  109. {
  110. .name = "AFE",
  111. .idx = 0,
  112. .id = 0,
  113. .dest_svc = APR_SVC_AFE,
  114. .client_id = APR_CLIENT_AUDIO,
  115. .handle = 0,
  116. },
  117. {
  118. .name = "ASM",
  119. .idx = 1,
  120. .id = 0,
  121. .dest_svc = APR_SVC_ASM,
  122. .client_id = APR_CLIENT_AUDIO,
  123. .handle = 0,
  124. },
  125. {
  126. .name = "ADM",
  127. .idx = 2,
  128. .id = 0,
  129. .dest_svc = APR_SVC_ADM,
  130. .client_id = APR_CLIENT_AUDIO,
  131. .handle = 0,
  132. },
  133. {
  134. .name = "CORE",
  135. .idx = 3,
  136. .id = 0,
  137. .dest_svc = APR_SVC_ADSP_CORE,
  138. .client_id = APR_CLIENT_AUDIO,
  139. .handle = 0,
  140. },
  141. {
  142. .name = "TEST",
  143. .idx = 4,
  144. .id = 0,
  145. .dest_svc = APR_SVC_TEST_CLIENT,
  146. .client_id = APR_CLIENT_AUDIO,
  147. .handle = 0,
  148. },
  149. {
  150. .name = "MVM",
  151. .idx = 5,
  152. .id = 0,
  153. .dest_svc = APR_SVC_ADSP_MVM,
  154. .client_id = APR_CLIENT_AUDIO,
  155. .handle = 0,
  156. },
  157. {
  158. .name = "CVS",
  159. .idx = 6,
  160. .id = 0,
  161. .dest_svc = APR_SVC_ADSP_CVS,
  162. .client_id = APR_CLIENT_AUDIO,
  163. .handle = 0,
  164. },
  165. {
  166. .name = "CVP",
  167. .idx = 7,
  168. .id = 0,
  169. .dest_svc = APR_SVC_ADSP_CVP,
  170. .client_id = APR_CLIENT_AUDIO,
  171. .handle = 0,
  172. },
  173. {
  174. .name = "USM",
  175. .idx = 8,
  176. .id = 0,
  177. .dest_svc = APR_SVC_USM,
  178. .client_id = APR_CLIENT_AUDIO,
  179. .handle = 0,
  180. },
  181. {
  182. .name = "VIDC",
  183. .idx = 9,
  184. .id = 0,
  185. .dest_svc = APR_SVC_VIDC,
  186. .handle = 0,
  187. },
  188. {
  189. .name = "LSM",
  190. .idx = 10,
  191. .id = 0,
  192. .dest_svc = APR_SVC_LSM,
  193. .client_id = APR_CLIENT_AUDIO,
  194. .handle = 0,
  195. },
  196. };
  197. static struct mutex m_lock_tbl_voice;
  198. static struct apr_svc_table svc_tbl_voice[] = {
  199. {
  200. .name = "VSM",
  201. .idx = 0,
  202. .id = 0,
  203. .dest_svc = APR_SVC_VSM,
  204. .client_id = APR_CLIENT_VOICE,
  205. .handle = 0,
  206. },
  207. {
  208. .name = "VPM",
  209. .idx = 1,
  210. .id = 0,
  211. .dest_svc = APR_SVC_VPM,
  212. .client_id = APR_CLIENT_VOICE,
  213. .handle = 0,
  214. },
  215. {
  216. .name = "MVS",
  217. .idx = 2,
  218. .id = 0,
  219. .dest_svc = APR_SVC_MVS,
  220. .client_id = APR_CLIENT_VOICE,
  221. .handle = 0,
  222. },
  223. {
  224. .name = "MVM",
  225. .idx = 3,
  226. .id = 0,
  227. .dest_svc = APR_SVC_MVM,
  228. .client_id = APR_CLIENT_VOICE,
  229. .handle = 0,
  230. },
  231. {
  232. .name = "CVS",
  233. .idx = 4,
  234. .id = 0,
  235. .dest_svc = APR_SVC_CVS,
  236. .client_id = APR_CLIENT_VOICE,
  237. .handle = 0,
  238. },
  239. {
  240. .name = "CVP",
  241. .idx = 5,
  242. .id = 0,
  243. .dest_svc = APR_SVC_CVP,
  244. .client_id = APR_CLIENT_VOICE,
  245. .handle = 0,
  246. },
  247. {
  248. .name = "SRD",
  249. .idx = 6,
  250. .id = 0,
  251. .dest_svc = APR_SVC_SRD,
  252. .client_id = APR_CLIENT_VOICE,
  253. .handle = 0,
  254. },
  255. {
  256. .name = "TEST",
  257. .idx = 7,
  258. .id = 0,
  259. .dest_svc = APR_SVC_TEST_CLIENT,
  260. .client_id = APR_CLIENT_VOICE,
  261. .handle = 0,
  262. },
  263. };
  264. /**
  265. * apr_get_modem_state:
  266. *
  267. * Returns current modem load status
  268. *
  269. */
  270. enum apr_subsys_state apr_get_modem_state(void)
  271. {
  272. return atomic_read(&q6.modem_state);
  273. }
  274. EXPORT_SYMBOL(apr_get_modem_state);
  275. /**
  276. * apr_set_modem_state - Update modem load status.
  277. *
  278. * @state: State to update modem load status
  279. *
  280. */
  281. void apr_set_modem_state(enum apr_subsys_state state)
  282. {
  283. atomic_set(&q6.modem_state, state);
  284. }
  285. EXPORT_SYMBOL(apr_set_modem_state);
  286. enum apr_subsys_state apr_cmpxchg_modem_state(enum apr_subsys_state prev,
  287. enum apr_subsys_state new)
  288. {
  289. return atomic_cmpxchg(&q6.modem_state, prev, new);
  290. }
  291. static void apr_modem_down(unsigned long opcode)
  292. {
  293. apr_set_modem_state(APR_SUBSYS_DOWN);
  294. dispatch_event(opcode, APR_DEST_MODEM);
  295. }
  296. static void apr_modem_up(void)
  297. {
  298. if (apr_cmpxchg_modem_state(APR_SUBSYS_DOWN, APR_SUBSYS_UP) ==
  299. APR_SUBSYS_DOWN)
  300. wake_up(&modem_wait);
  301. is_modem_up = 1;
  302. }
  303. enum apr_subsys_state apr_get_q6_state(void)
  304. {
  305. return atomic_read(&q6.q6_state);
  306. }
  307. EXPORT_SYMBOL(apr_get_q6_state);
  308. int apr_set_q6_state(enum apr_subsys_state state)
  309. {
  310. pr_debug("%s: setting adsp state %d\n", __func__, state);
  311. if (state < APR_SUBSYS_DOWN || state > APR_SUBSYS_LOADED)
  312. return -EINVAL;
  313. atomic_set(&q6.q6_state, state);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL(apr_set_q6_state);
  317. static void apr_ssr_disable(struct device *dev, void *data)
  318. {
  319. apr_set_q6_state(APR_SUBSYS_DOWN);
  320. }
  321. static const struct snd_event_ops apr_ssr_ops = {
  322. .disable = apr_ssr_disable,
  323. };
  324. static void apr_adsp_down(unsigned long opcode)
  325. {
  326. pr_info("%s: Q6 is Down\n", __func__);
  327. snd_event_notify(apr_priv->dev, SND_EVENT_DOWN);
  328. apr_set_q6_state(APR_SUBSYS_DOWN);
  329. dispatch_event(opcode, APR_DEST_QDSP6);
  330. }
  331. static void apr_add_child_devices(struct work_struct *work)
  332. {
  333. int ret;
  334. ret = of_platform_populate(apr_priv->dev->of_node,
  335. NULL, NULL, apr_priv->dev);
  336. if (ret)
  337. dev_err(apr_priv->dev, "%s: failed to add child nodes, ret=%d\n",
  338. __func__, ret);
  339. }
  340. static void apr_adsp_up(void)
  341. {
  342. pr_info("%s: Q6 is Up\n", __func__);
  343. apr_set_q6_state(APR_SUBSYS_LOADED);
  344. spin_lock(&apr_priv->apr_lock);
  345. if (apr_priv->is_initial_boot)
  346. schedule_work(&apr_priv->add_chld_dev_work);
  347. spin_unlock(&apr_priv->apr_lock);
  348. snd_event_notify(apr_priv->dev, SND_EVENT_UP);
  349. }
  350. int apr_load_adsp_image(void)
  351. {
  352. int rc = 0;
  353. mutex_lock(&q6.lock);
  354. if (apr_get_q6_state() == APR_SUBSYS_UP) {
  355. q6.pil = subsystem_get("adsp");
  356. if (IS_ERR(q6.pil)) {
  357. rc = PTR_ERR(q6.pil);
  358. pr_err("APR: Unable to load q6 image, error:%d\n", rc);
  359. } else {
  360. apr_set_q6_state(APR_SUBSYS_LOADED);
  361. pr_debug("APR: Image is loaded, stated\n");
  362. }
  363. } else if (apr_get_q6_state() == APR_SUBSYS_LOADED) {
  364. pr_debug("APR: q6 image already loaded\n");
  365. } else {
  366. pr_debug("APR: cannot load state %d\n", apr_get_q6_state());
  367. }
  368. mutex_unlock(&q6.lock);
  369. return rc;
  370. }
  371. struct apr_client *apr_get_client(int dest_id, int client_id)
  372. {
  373. return &client[dest_id][client_id];
  374. }
  375. static int apr_vm_nb_receive(int32_t handle, void *dest_buff,
  376. uint32_t *size_bytes, uint32_t timeout)
  377. {
  378. int rc;
  379. uint32_t dest_buff_bytes = *size_bytes;
  380. unsigned long delay = jiffies + (HZ / 2);
  381. do {
  382. *size_bytes = dest_buff_bytes;
  383. rc = habmm_socket_recv(handle,
  384. dest_buff,
  385. size_bytes,
  386. timeout,
  387. HABMM_SOCKET_RECV_FLAGS_NON_BLOCKING);
  388. } while (time_before(jiffies, delay) && (rc == -EAGAIN) &&
  389. (*size_bytes == 0));
  390. return rc;
  391. }
  392. static int apr_vm_cb_process_evt(char *buf, int len)
  393. {
  394. struct apr_client_data data = {0,};
  395. struct apr_client *apr_client;
  396. struct apr_svc *c_svc;
  397. struct apr_hdr *hdr;
  398. uint16_t hdr_size;
  399. uint16_t msg_type;
  400. uint16_t ver;
  401. uint16_t src;
  402. uint16_t svc;
  403. uint16_t clnt;
  404. int i;
  405. int temp_port = 0;
  406. uint32_t *ptr;
  407. uint32_t evt_id;
  408. pr_debug("APR: len = %d\n", len);
  409. ptr = (uint32_t *)buf;
  410. pr_debug("\n*****************\n");
  411. for (i = 0; i < len/4; i++)
  412. pr_debug("%x ", ptr[i]);
  413. pr_debug("\n");
  414. pr_debug("\n*****************\n");
  415. if (!buf || len <= APR_HDR_SIZE + sizeof(uint32_t)) {
  416. pr_err("APR: Improper apr pkt received: %p %d\n", buf, len);
  417. return -EINVAL;
  418. }
  419. evt_id = *((int32_t *)buf);
  420. if (evt_id != APRV2_VM_EVT_RX_PKT_AVAILABLE) {
  421. pr_err("APR: Wrong evt id: %d\n", evt_id);
  422. return -EINVAL;
  423. }
  424. hdr = (struct apr_hdr *)(buf + sizeof(uint32_t));
  425. ver = hdr->hdr_field;
  426. ver = (ver & 0x000F);
  427. if (ver > APR_PKT_VER + 1) {
  428. pr_err("APR: Wrong version: %d\n", ver);
  429. return -EINVAL;
  430. }
  431. hdr_size = hdr->hdr_field;
  432. hdr_size = ((hdr_size & 0x00F0) >> 0x4) * 4;
  433. if (hdr_size < APR_HDR_SIZE) {
  434. pr_err("APR: Wrong hdr size:%d\n", hdr_size);
  435. return -EINVAL;
  436. }
  437. if (hdr->pkt_size < APR_HDR_SIZE) {
  438. pr_err("APR: Wrong paket size\n");
  439. return -EINVAL;
  440. }
  441. msg_type = hdr->hdr_field;
  442. msg_type = (msg_type >> 0x08) & 0x0003;
  443. if (msg_type >= APR_MSG_TYPE_MAX && msg_type != APR_BASIC_RSP_RESULT) {
  444. pr_err("APR: Wrong message type: %d\n", msg_type);
  445. return -EINVAL;
  446. }
  447. /*
  448. * dest_svc is dynamic created by apr service
  449. * no need to check the range of dest_svc
  450. */
  451. if (hdr->src_domain >= APR_DOMAIN_MAX ||
  452. hdr->dest_domain >= APR_DOMAIN_MAX ||
  453. hdr->src_svc >= APR_SVC_MAX) {
  454. pr_err("APR: Wrong APR header\n");
  455. return -EINVAL;
  456. }
  457. svc = hdr->dest_svc;
  458. if (hdr->src_domain == APR_DOMAIN_MODEM)
  459. clnt = APR_CLIENT_VOICE;
  460. else if (hdr->src_domain == APR_DOMAIN_ADSP)
  461. clnt = APR_CLIENT_AUDIO;
  462. else {
  463. pr_err("APR: Pkt from wrong source: %d\n", hdr->src_domain);
  464. return -EINVAL;
  465. }
  466. src = apr_get_data_src(hdr);
  467. if (src == APR_DEST_MAX)
  468. return -EINVAL;
  469. pr_debug("src =%d clnt = %d\n", src, clnt);
  470. apr_client = &client[src][clnt];
  471. for (i = 0; i < APR_SVC_MAX; i++)
  472. if (apr_client->svc[i].id == svc) {
  473. pr_debug("svc_id = %d\n", apr_client->svc[i].id);
  474. c_svc = &apr_client->svc[i];
  475. break;
  476. }
  477. if (i == APR_SVC_MAX) {
  478. pr_err("APR: service is not registered\n");
  479. return -ENXIO;
  480. }
  481. pr_debug("svc_idx = %d\n", i);
  482. pr_debug("%x %x %x %p %p\n", c_svc->id, c_svc->dest_id,
  483. c_svc->client_id, c_svc->fn, c_svc->priv);
  484. data.payload_size = hdr->pkt_size - hdr_size;
  485. data.opcode = hdr->opcode;
  486. data.src = src;
  487. data.src_port = hdr->src_port;
  488. data.dest_port = hdr->dest_port;
  489. data.token = hdr->token;
  490. data.msg_type = msg_type;
  491. if (data.payload_size > 0)
  492. data.payload = (char *)hdr + hdr_size;
  493. if (unlikely(apr_cf_debug)) {
  494. if (hdr->opcode == APR_BASIC_RSP_RESULT && data.payload) {
  495. uint32_t *ptr = data.payload;
  496. APR_PKT_INFO(
  497. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X] rc[0x%X]",
  498. (hdr->src_domain << 8) | hdr->src_svc,
  499. (hdr->dest_domain << 8) | hdr->dest_svc,
  500. hdr->opcode, hdr->token, ptr[1]);
  501. } else {
  502. APR_PKT_INFO(
  503. "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  504. (hdr->src_domain << 8) | hdr->src_svc,
  505. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  506. hdr->token);
  507. }
  508. }
  509. temp_port = ((data.dest_port >> 8) * 8) + (data.dest_port & 0xFF);
  510. pr_debug("port = %d t_port = %d\n", data.src_port, temp_port);
  511. if (((temp_port >= 0) && (temp_port < APR_MAX_PORTS))
  512. && (c_svc->port_cnt && c_svc->port_fn[temp_port]))
  513. c_svc->port_fn[temp_port](&data, c_svc->port_priv[temp_port]);
  514. else if (c_svc->fn)
  515. c_svc->fn(&data, c_svc->priv);
  516. else
  517. pr_err("APR: Rxed a packet for NULL callback\n");
  518. return 0;
  519. }
  520. static int apr_vm_cb_thread(void *data)
  521. {
  522. uint32_t apr_rx_buf_len;
  523. struct aprv2_vm_ack_rx_pkt_available_t apr_ack;
  524. unsigned long delay = jiffies + (HZ / 2);
  525. int status = 0;
  526. int ret = 0;
  527. while (1) {
  528. do {
  529. apr_rx_buf_len = sizeof(apr_rx_buf);
  530. ret = habmm_socket_recv(hab_handle_rx,
  531. (void *)&apr_rx_buf,
  532. &apr_rx_buf_len,
  533. 0xFFFFFFFF,
  534. 0);
  535. } while (time_before(jiffies, delay) && (ret == -EINTR) &&
  536. (apr_rx_buf_len == 0));
  537. if (ret) {
  538. pr_err("%s: habmm_socket_recv failed %d\n",
  539. __func__, ret);
  540. break;
  541. }
  542. status = apr_vm_cb_process_evt(apr_rx_buf, apr_rx_buf_len);
  543. apr_ack.status = status;
  544. ret = habmm_socket_send(hab_handle_rx,
  545. (void *)&apr_ack,
  546. sizeof(apr_ack),
  547. 0);
  548. if (ret) {
  549. pr_err("%s: habmm_socket_send failed %d\n",
  550. __func__, ret);
  551. /* TODO: break if send failed ? */
  552. break;
  553. }
  554. }
  555. return ret;
  556. }
  557. static int apr_vm_get_svc(const char *svc_name, int domain_id, int *client_id,
  558. int *svc_idx, int *svc_id, int *dest_svc, int *handle)
  559. {
  560. int i;
  561. int size;
  562. struct apr_svc_table *tbl;
  563. struct mutex *lock;
  564. struct aprv2_vm_cmd_register_rsp_t apr_rsp;
  565. uint32_t apr_len;
  566. int ret = 0;
  567. struct {
  568. uint32_t cmd_id;
  569. struct aprv2_vm_cmd_register_t reg_cmd;
  570. } tx_data;
  571. if (domain_id == APR_DOMAIN_ADSP) {
  572. tbl = svc_tbl_qdsp6;
  573. size = ARRAY_SIZE(svc_tbl_qdsp6);
  574. lock = &m_lock_tbl_qdsp6;
  575. } else {
  576. tbl = svc_tbl_voice;
  577. size = ARRAY_SIZE(svc_tbl_voice);
  578. lock = &m_lock_tbl_voice;
  579. }
  580. mutex_lock(lock);
  581. for (i = 0; i < size; i++) {
  582. if (!strcmp(svc_name, tbl[i].name)) {
  583. *client_id = tbl[i].client_id;
  584. *svc_idx = tbl[i].idx;
  585. if (!tbl[i].id && !tbl[i].handle) {
  586. /* need to register a new service */
  587. memset((void *) &tx_data, 0, sizeof(tx_data));
  588. apr_len = sizeof(tx_data);
  589. tx_data.cmd_id = APRV2_VM_CMDID_REGISTER;
  590. tx_data.reg_cmd.name_size = snprintf(
  591. tx_data.reg_cmd.name,
  592. APRV2_VM_MAX_DNS_SIZE,
  593. "qcom.apps.lnx.%s",
  594. svc_name);
  595. tx_data.reg_cmd.addr = 0;
  596. ret = habmm_socket_send(hab_handle_tx,
  597. (void *) &tx_data,
  598. apr_len,
  599. 0);
  600. if (ret) {
  601. pr_err("%s: habmm_socket_send failed %d\n",
  602. __func__, ret);
  603. mutex_unlock(lock);
  604. return ret;
  605. }
  606. /* wait for response */
  607. apr_len = sizeof(apr_rsp);
  608. ret = apr_vm_nb_receive(hab_handle_tx,
  609. (void *)&apr_rsp,
  610. &apr_len,
  611. 0xFFFFFFFF);
  612. if (ret) {
  613. pr_err("%s: apr_vm_nb_receive failed %d\n",
  614. __func__, ret);
  615. mutex_unlock(lock);
  616. return ret;
  617. }
  618. if (apr_rsp.status) {
  619. pr_err("%s: apr_vm_nb_receive status %d\n",
  620. __func__, apr_rsp.status);
  621. ret = apr_rsp.status;
  622. mutex_unlock(lock);
  623. return ret;
  624. }
  625. /* update svc table */
  626. tbl[i].handle = apr_rsp.handle;
  627. tbl[i].id = apr_rsp.addr &
  628. APRV2_VM_PKT_SERVICE_ID_MASK;
  629. }
  630. *svc_id = tbl[i].id;
  631. *dest_svc = tbl[i].dest_svc;
  632. *handle = tbl[i].handle;
  633. break;
  634. }
  635. }
  636. mutex_unlock(lock);
  637. pr_debug("%s: svc_name = %s client_id = %d domain_id = %d\n",
  638. __func__, svc_name, *client_id, domain_id);
  639. pr_debug("%s: src_svc = %d dest_svc = %d handle = %d\n",
  640. __func__, *svc_id, *dest_svc, *handle);
  641. if (i == size) {
  642. pr_err("%s: APR: Wrong svc name %s\n", __func__, svc_name);
  643. ret = -EINVAL;
  644. }
  645. return ret;
  646. }
  647. static int apr_vm_rel_svc(int domain_id, int svc_id, int handle)
  648. {
  649. int i;
  650. int size;
  651. struct apr_svc_table *tbl;
  652. struct mutex *lock;
  653. struct aprv2_vm_cmd_deregister_rsp_t apr_rsp;
  654. uint32_t apr_len;
  655. int ret = 0;
  656. struct {
  657. uint32_t cmd_id;
  658. struct aprv2_vm_cmd_deregister_t dereg_cmd;
  659. } tx_data;
  660. if (domain_id == APR_DOMAIN_ADSP) {
  661. tbl = svc_tbl_qdsp6;
  662. size = ARRAY_SIZE(svc_tbl_qdsp6);
  663. lock = &m_lock_tbl_qdsp6;
  664. } else {
  665. tbl = svc_tbl_voice;
  666. size = ARRAY_SIZE(svc_tbl_voice);
  667. lock = &m_lock_tbl_voice;
  668. }
  669. mutex_lock(lock);
  670. for (i = 0; i < size; i++) {
  671. if (tbl[i].id == svc_id && tbl[i].handle == handle) {
  672. /* need to deregister a service */
  673. memset((void *) &tx_data, 0, sizeof(tx_data));
  674. apr_len = sizeof(tx_data);
  675. tx_data.cmd_id = APRV2_VM_CMDID_DEREGISTER;
  676. tx_data.dereg_cmd.handle = handle;
  677. ret = habmm_socket_send(hab_handle_tx,
  678. (void *) &tx_data,
  679. apr_len,
  680. 0);
  681. if (ret)
  682. pr_err("%s: habmm_socket_send failed %d\n",
  683. __func__, ret);
  684. /*
  685. * TODO: if send failed, should not wait for recv.
  686. * should clear regardless?
  687. */
  688. /* wait for response */
  689. apr_len = sizeof(apr_rsp);
  690. ret = apr_vm_nb_receive(hab_handle_tx,
  691. (void *)&apr_rsp,
  692. &apr_len,
  693. 0xFFFFFFFF);
  694. if (ret)
  695. pr_err("%s: apr_vm_nb_receive failed %d\n",
  696. __func__, ret);
  697. if (apr_rsp.status) {
  698. pr_err("%s: apr_vm_nb_receive status %d\n",
  699. __func__, apr_rsp.status);
  700. ret = apr_rsp.status;
  701. }
  702. /* clear svc table */
  703. tbl[i].handle = 0;
  704. tbl[i].id = 0;
  705. break;
  706. }
  707. }
  708. mutex_unlock(lock);
  709. if (i == size) {
  710. pr_err("%s: APR: Wrong svc id %d handle %d\n",
  711. __func__, svc_id, handle);
  712. ret = -EINVAL;
  713. }
  714. return ret;
  715. }
  716. static void apr_vm_set_subsys_state(void)
  717. {
  718. /* set default subsys state in vm env.
  719. * Both q6 and modem should be in LOADED state,
  720. * since vm boots up at late stage after pm.
  721. */
  722. apr_set_q6_state(APR_SUBSYS_LOADED);
  723. apr_set_modem_state(APR_SUBSYS_LOADED);
  724. spin_lock(&apr_priv->apr_lock);
  725. if (apr_priv->is_initial_boot)
  726. schedule_work(&apr_priv->add_chld_dev_work);
  727. spin_unlock(&apr_priv->apr_lock);
  728. snd_event_notify(apr_priv->dev, SND_EVENT_UP);
  729. }
  730. /**
  731. * apr_send_pkt - Clients call to send packet
  732. * to destination processor.
  733. *
  734. * @handle: APR service handle
  735. * @buf: payload to send to destination processor.
  736. *
  737. * Returns Bytes(>0)pkt_size on success or error on failure.
  738. */
  739. int apr_send_pkt(void *handle, uint32_t *buf)
  740. {
  741. struct apr_svc *svc = handle;
  742. struct apr_hdr *hdr;
  743. unsigned long flags;
  744. uint32_t *cmd_id = (uint32_t *)apr_tx_buf;
  745. struct aprv2_vm_cmd_async_send_t *apr_send =
  746. (struct aprv2_vm_cmd_async_send_t *)(apr_tx_buf +
  747. sizeof(uint32_t));
  748. uint32_t apr_send_len;
  749. struct aprv2_vm_cmd_async_send_rsp_t apr_rsp;
  750. uint32_t apr_rsp_len;
  751. int ret = 0;
  752. if (!handle || !buf) {
  753. pr_err("APR: Wrong parameters\n");
  754. return -EINVAL;
  755. }
  756. if (svc->need_reset) {
  757. pr_err("apr: send_pkt service need reset\n");
  758. return -ENETRESET;
  759. }
  760. if ((svc->dest_id == APR_DEST_QDSP6) &&
  761. (apr_get_q6_state() != APR_SUBSYS_LOADED)) {
  762. pr_err("%s: Still dsp is not Up\n", __func__);
  763. return -ENETRESET;
  764. } else if ((svc->dest_id == APR_DEST_MODEM) &&
  765. (apr_get_modem_state() == APR_SUBSYS_DOWN)) {
  766. pr_err("apr: Still Modem is not Up\n");
  767. return -ENETRESET;
  768. }
  769. spin_lock_irqsave(&svc->w_lock, flags);
  770. if (!svc->id || !svc->vm_handle) {
  771. pr_err("APR: Still service is not yet opened\n");
  772. ret = -EINVAL;
  773. goto done;
  774. }
  775. hdr = (struct apr_hdr *)buf;
  776. hdr->src_domain = APR_DOMAIN_APPS;
  777. hdr->src_svc = svc->id;
  778. hdr->dest_domain = svc->dest_domain;
  779. hdr->dest_svc = svc->vm_dest_svc;
  780. if (unlikely(apr_cf_debug)) {
  781. APR_PKT_INFO(
  782. "Tx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
  783. (hdr->src_domain << 8) | hdr->src_svc,
  784. (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
  785. hdr->token);
  786. }
  787. memset((void *)&apr_tx_buf, 0, sizeof(apr_tx_buf));
  788. /* pkt_size + cmd_id + handle */
  789. apr_send_len = hdr->pkt_size + sizeof(uint32_t) * 2;
  790. *cmd_id = APRV2_VM_CMDID_ASYNC_SEND;
  791. apr_send->handle = svc->vm_handle;
  792. /* safe check */
  793. if (hdr->pkt_size > APR_TX_BUF_SIZE - (sizeof(uint32_t) * 2)) {
  794. pr_err("APR: Wrong pkt size %d\n", hdr->pkt_size);
  795. ret = -ENOMEM;
  796. goto done;
  797. }
  798. memcpy(&apr_send->pkt_header, buf, hdr->pkt_size);
  799. ret = habmm_socket_send(hab_handle_tx,
  800. (void *)&apr_tx_buf,
  801. apr_send_len,
  802. 0);
  803. if (ret) {
  804. pr_err("%s: habmm_socket_send failed %d\n",
  805. __func__, ret);
  806. goto done;
  807. }
  808. /* wait for response */
  809. apr_rsp_len = sizeof(apr_rsp);
  810. ret = apr_vm_nb_receive(hab_handle_tx,
  811. (void *)&apr_rsp,
  812. &apr_rsp_len,
  813. 0xFFFFFFFF);
  814. if (ret) {
  815. pr_err("%s: apr_vm_nb_receive failed %d\n",
  816. __func__, ret);
  817. goto done;
  818. }
  819. if (apr_rsp.status) {
  820. pr_err("%s: apr_vm_nb_receive status %d\n",
  821. __func__, apr_rsp.status);
  822. /* should translate status properly */
  823. ret = -ECOMM;
  824. goto done;
  825. }
  826. /* upon successful send, return packet size */
  827. ret = hdr->pkt_size;
  828. done:
  829. spin_unlock_irqrestore(&svc->w_lock, flags);
  830. return ret;
  831. }
  832. EXPORT_SYMBOL(apr_send_pkt);
  833. /**
  834. * apr_register - Clients call to register
  835. * to APR.
  836. *
  837. * @dest: destination processor
  838. * @svc_name: name of service to register as
  839. * @svc_fn: callback function to trigger when response
  840. * ack or packets received from destination processor.
  841. * @src_port: Port number within a service
  842. * @priv: private data of client, passed back in cb fn.
  843. *
  844. * Returns apr_svc handle on success or NULL on failure.
  845. */
  846. struct apr_svc *apr_register(char *dest, char *svc_name, apr_fn svc_fn,
  847. uint32_t src_port, void *priv)
  848. {
  849. struct apr_client *clnt;
  850. int client_id = 0;
  851. int svc_idx = 0;
  852. int svc_id = 0;
  853. int dest_id = 0;
  854. int domain_id = 0;
  855. int temp_port = 0;
  856. struct apr_svc *svc = NULL;
  857. int rc = 0;
  858. bool can_open_channel = true;
  859. int dest_svc = 0;
  860. int handle = 0;
  861. if (!dest || !svc_name || !svc_fn)
  862. return NULL;
  863. if (!strcmp(dest, "ADSP"))
  864. domain_id = APR_DOMAIN_ADSP;
  865. else if (!strcmp(dest, "MODEM")) {
  866. /* Don't request for SMD channels if destination is MODEM,
  867. * as these channels are no longer used and these clients
  868. * are to listen only for MODEM SSR events
  869. */
  870. can_open_channel = false;
  871. domain_id = APR_DOMAIN_MODEM;
  872. } else {
  873. pr_err("APR: wrong destination\n");
  874. goto done;
  875. }
  876. dest_id = apr_get_dest_id(dest);
  877. if (dest_id == APR_DEST_QDSP6) {
  878. if (apr_get_q6_state() != APR_SUBSYS_LOADED) {
  879. pr_err("%s: adsp not up\n", __func__);
  880. return NULL;
  881. }
  882. pr_debug("%s: adsp Up\n", __func__);
  883. } else if (dest_id == APR_DEST_MODEM) {
  884. if (apr_get_modem_state() == APR_SUBSYS_DOWN) {
  885. if (is_modem_up) {
  886. pr_err("%s: modem shutdown due to SSR, ret",
  887. __func__);
  888. return NULL;
  889. }
  890. pr_debug("%s: Wait for modem to bootup\n", __func__);
  891. rc = wait_event_interruptible_timeout(modem_wait,
  892. (apr_get_modem_state() == APR_SUBSYS_UP),
  893. (1 * HZ));
  894. if (rc == 0) {
  895. pr_err("%s: Modem is not Up\n", __func__);
  896. return NULL;
  897. }
  898. }
  899. pr_debug("%s: modem Up\n", __func__);
  900. }
  901. if (apr_vm_get_svc(svc_name, domain_id, &client_id, &svc_idx, &svc_id,
  902. &dest_svc, &handle)) {
  903. pr_err("%s: apr_vm_get_svc failed\n", __func__);
  904. goto done;
  905. }
  906. clnt = &client[dest_id][client_id];
  907. svc = &clnt->svc[svc_idx];
  908. mutex_lock(&svc->m_lock);
  909. clnt->id = client_id;
  910. if (svc->need_reset) {
  911. mutex_unlock(&svc->m_lock);
  912. pr_err("APR: Service needs reset\n");
  913. svc = NULL;
  914. goto done;
  915. }
  916. svc->id = svc_id;
  917. svc->vm_dest_svc = dest_svc;
  918. svc->dest_id = dest_id;
  919. svc->client_id = client_id;
  920. svc->dest_domain = domain_id;
  921. svc->pkt_owner = APR_PKT_OWNER_DRIVER;
  922. svc->vm_handle = handle;
  923. if (src_port != 0xFFFFFFFF) {
  924. temp_port = ((src_port >> 8) * 8) + (src_port & 0xFF);
  925. pr_debug("port = %d t_port = %d\n", src_port, temp_port);
  926. if (temp_port >= APR_MAX_PORTS || temp_port < 0) {
  927. pr_err("APR: temp_port out of bounds\n");
  928. mutex_unlock(&svc->m_lock);
  929. return NULL;
  930. }
  931. if (!svc->port_cnt && !svc->svc_cnt)
  932. clnt->svc_cnt++;
  933. svc->port_cnt++;
  934. svc->port_fn[temp_port] = svc_fn;
  935. svc->port_priv[temp_port] = priv;
  936. } else {
  937. if (!svc->fn) {
  938. if (!svc->port_cnt && !svc->svc_cnt)
  939. clnt->svc_cnt++;
  940. svc->fn = svc_fn;
  941. if (svc->port_cnt)
  942. svc->svc_cnt++;
  943. svc->priv = priv;
  944. }
  945. }
  946. mutex_unlock(&svc->m_lock);
  947. done:
  948. return svc;
  949. }
  950. EXPORT_SYMBOL(apr_register);
  951. static void apr_reset_deregister(struct work_struct *work)
  952. {
  953. struct apr_svc *handle = NULL;
  954. struct apr_reset_work *apr_reset =
  955. container_of(work, struct apr_reset_work, work);
  956. handle = apr_reset->handle;
  957. pr_debug("%s:handle[%pK]\n", __func__, handle);
  958. apr_deregister(handle);
  959. kfree(apr_reset);
  960. }
  961. /**
  962. * apr_start_rx_rt - Clients call to vote for thread
  963. * priority upgrade whenever needed.
  964. *
  965. * @handle: APR service handle
  966. *
  967. * Returns 0 on success or error otherwise.
  968. */
  969. int apr_start_rx_rt(void *handle)
  970. {
  971. int rc = 0;
  972. return rc;
  973. }
  974. EXPORT_SYMBOL(apr_start_rx_rt);
  975. /**
  976. * apr_end_rx_rt - Clients call to unvote for thread
  977. * priority upgrade (perviously voted with
  978. * apr_start_rx_rt()).
  979. *
  980. * @handle: APR service handle
  981. *
  982. * Returns 0 on success or error otherwise.
  983. */
  984. int apr_end_rx_rt(void *handle)
  985. {
  986. int rc = 0;
  987. return rc;
  988. }
  989. EXPORT_SYMBOL(apr_end_rx_rt);
  990. /**
  991. * apr_deregister - Clients call to de-register
  992. * from APR.
  993. *
  994. * @handle: APR service handle to de-register
  995. *
  996. * Returns 0 on success or -EINVAL on error.
  997. */
  998. int apr_deregister(void *handle)
  999. {
  1000. struct apr_svc *svc = handle;
  1001. struct apr_client *clnt;
  1002. uint16_t dest_id;
  1003. uint16_t client_id;
  1004. if (!handle)
  1005. return -EINVAL;
  1006. mutex_lock(&svc->m_lock);
  1007. dest_id = svc->dest_id;
  1008. client_id = svc->client_id;
  1009. clnt = &client[dest_id][client_id];
  1010. if (svc->port_cnt > 0 || svc->svc_cnt > 0) {
  1011. if (svc->port_cnt)
  1012. svc->port_cnt--;
  1013. else if (svc->svc_cnt)
  1014. svc->svc_cnt--;
  1015. if (!svc->port_cnt && !svc->svc_cnt) {
  1016. client[dest_id][client_id].svc_cnt--;
  1017. svc->need_reset = 0x0;
  1018. }
  1019. } else if (client[dest_id][client_id].svc_cnt > 0) {
  1020. client[dest_id][client_id].svc_cnt--;
  1021. if (!client[dest_id][client_id].svc_cnt) {
  1022. svc->need_reset = 0x0;
  1023. pr_debug("%s: service is reset %p\n", __func__, svc);
  1024. }
  1025. }
  1026. if (!svc->port_cnt && !svc->svc_cnt) {
  1027. if (apr_vm_rel_svc(svc->dest_domain, svc->id, svc->vm_handle))
  1028. pr_err("%s: apr_vm_rel_svc failed\n", __func__);
  1029. svc->priv = NULL;
  1030. svc->id = 0;
  1031. svc->vm_dest_svc = 0;
  1032. svc->fn = NULL;
  1033. svc->dest_id = 0;
  1034. svc->client_id = 0;
  1035. svc->need_reset = 0x0;
  1036. svc->vm_handle = 0;
  1037. }
  1038. mutex_unlock(&svc->m_lock);
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL(apr_deregister);
  1042. /**
  1043. * apr_reset - sets up workqueue to de-register
  1044. * the given APR service handle.
  1045. *
  1046. * @handle: APR service handle
  1047. *
  1048. */
  1049. void apr_reset(void *handle)
  1050. {
  1051. struct apr_reset_work *apr_reset_worker = NULL;
  1052. if (!handle)
  1053. return;
  1054. pr_debug("%s: handle[%pK]\n", __func__, handle);
  1055. if (apr_reset_workqueue == NULL) {
  1056. pr_err("%s: apr_reset_workqueue is NULL\n", __func__);
  1057. return;
  1058. }
  1059. apr_reset_worker = kzalloc(sizeof(struct apr_reset_work),
  1060. GFP_ATOMIC);
  1061. if (apr_reset_worker == NULL) {
  1062. pr_err("%s: mem failure\n", __func__);
  1063. return;
  1064. }
  1065. apr_reset_worker->handle = handle;
  1066. INIT_WORK(&apr_reset_worker->work, apr_reset_deregister);
  1067. queue_work(apr_reset_workqueue, &apr_reset_worker->work);
  1068. }
  1069. EXPORT_SYMBOL(apr_reset);
  1070. /* Dispatch the Reset events to Modem and audio clients */
  1071. static void dispatch_event(unsigned long code, uint16_t proc)
  1072. {
  1073. struct apr_client *apr_client;
  1074. struct apr_client_data data;
  1075. struct apr_svc *svc;
  1076. uint16_t clnt;
  1077. int i, j;
  1078. memset(&data, 0, sizeof(data));
  1079. data.opcode = RESET_EVENTS;
  1080. data.reset_event = code;
  1081. /* Service domain can be different from the processor */
  1082. data.reset_proc = apr_get_reset_domain(proc);
  1083. clnt = APR_CLIENT_AUDIO;
  1084. apr_client = &client[proc][clnt];
  1085. for (i = 0; i < APR_SVC_MAX; i++) {
  1086. mutex_lock(&apr_client->svc[i].m_lock);
  1087. if (apr_client->svc[i].fn) {
  1088. apr_client->svc[i].need_reset = 0x1;
  1089. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  1090. }
  1091. if (apr_client->svc[i].port_cnt) {
  1092. svc = &(apr_client->svc[i]);
  1093. svc->need_reset = 0x1;
  1094. for (j = 0; j < APR_MAX_PORTS; j++)
  1095. if (svc->port_fn[j])
  1096. svc->port_fn[j](&data,
  1097. svc->port_priv[j]);
  1098. }
  1099. mutex_unlock(&apr_client->svc[i].m_lock);
  1100. }
  1101. clnt = APR_CLIENT_VOICE;
  1102. apr_client = &client[proc][clnt];
  1103. for (i = 0; i < APR_SVC_MAX; i++) {
  1104. mutex_lock(&apr_client->svc[i].m_lock);
  1105. if (apr_client->svc[i].fn) {
  1106. apr_client->svc[i].need_reset = 0x1;
  1107. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  1108. }
  1109. if (apr_client->svc[i].port_cnt) {
  1110. svc = &(apr_client->svc[i]);
  1111. svc->need_reset = 0x1;
  1112. for (j = 0; j < APR_MAX_PORTS; j++)
  1113. if (svc->port_fn[j])
  1114. svc->port_fn[j](&data,
  1115. svc->port_priv[j]);
  1116. }
  1117. mutex_unlock(&apr_client->svc[i].m_lock);
  1118. }
  1119. }
  1120. static int apr_notifier_service_cb(struct notifier_block *this,
  1121. unsigned long opcode, void *data)
  1122. {
  1123. struct audio_notifier_cb_data *cb_data = data;
  1124. if (cb_data == NULL) {
  1125. pr_err("%s: Callback data is NULL!\n", __func__);
  1126. goto done;
  1127. }
  1128. pr_debug("%s: Service opcode 0x%lx, domain %d\n",
  1129. __func__, opcode, cb_data->domain);
  1130. switch (opcode) {
  1131. case AUDIO_NOTIFIER_SERVICE_DOWN:
  1132. /*
  1133. * Use flag to ignore down notifications during
  1134. * initial boot. There is no benefit from error
  1135. * recovery notifications during initial boot
  1136. * up since everything is expected to be down.
  1137. */
  1138. spin_lock(&apr_priv->apr_lock);
  1139. if (apr_priv->is_initial_boot) {
  1140. spin_unlock(&apr_priv->apr_lock);
  1141. break;
  1142. }
  1143. spin_unlock(&apr_priv->apr_lock);
  1144. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  1145. apr_modem_down(opcode);
  1146. else
  1147. apr_adsp_down(opcode);
  1148. break;
  1149. case AUDIO_NOTIFIER_SERVICE_UP:
  1150. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  1151. apr_modem_up();
  1152. else
  1153. apr_adsp_up();
  1154. spin_lock(&apr_priv->apr_lock);
  1155. apr_priv->is_initial_boot = false;
  1156. spin_unlock(&apr_priv->apr_lock);
  1157. break;
  1158. default:
  1159. break;
  1160. }
  1161. done:
  1162. return NOTIFY_OK;
  1163. }
  1164. static struct notifier_block adsp_service_nb = {
  1165. .notifier_call = apr_notifier_service_cb,
  1166. .priority = 0,
  1167. };
  1168. static struct notifier_block modem_service_nb = {
  1169. .notifier_call = apr_notifier_service_cb,
  1170. .priority = 0,
  1171. };
  1172. #ifdef CONFIG_DEBUG_FS
  1173. static int __init apr_debug_init(void)
  1174. {
  1175. debugfs_apr_debug = debugfs_create_file("msm_apr_debug",
  1176. S_IFREG | 0444, NULL, NULL,
  1177. &apr_debug_ops);
  1178. return 0;
  1179. }
  1180. #else
  1181. static int __init apr_debug_init(void)
  1182. (
  1183. return 0;
  1184. )
  1185. #endif
  1186. static void apr_cleanup(void)
  1187. {
  1188. int i, j, k;
  1189. of_platform_depopulate(apr_priv->dev);
  1190. subsys_notif_deregister(subsys_name);
  1191. if (apr_reset_workqueue) {
  1192. flush_workqueue(apr_reset_workqueue);
  1193. destroy_workqueue(apr_reset_workqueue);
  1194. }
  1195. mutex_destroy(&q6.lock);
  1196. for (i = 0; i < APR_DEST_MAX; i++) {
  1197. for (j = 0; j < APR_CLIENT_MAX; j++) {
  1198. mutex_destroy(&client[i][j].m_lock);
  1199. for (k = 0; k < APR_SVC_MAX; k++)
  1200. mutex_destroy(&client[i][j].svc[k].m_lock);
  1201. }
  1202. }
  1203. debugfs_remove(debugfs_apr_debug);
  1204. }
  1205. static int apr_probe(struct platform_device *pdev)
  1206. {
  1207. int i, j, k, ret = 0;
  1208. init_waitqueue_head(&modem_wait);
  1209. apr_priv = devm_kzalloc(&pdev->dev, sizeof(*apr_priv), GFP_KERNEL);
  1210. if (!apr_priv)
  1211. return -ENOMEM;
  1212. apr_priv->dev = &pdev->dev;
  1213. spin_lock_init(&apr_priv->apr_lock);
  1214. INIT_WORK(&apr_priv->add_chld_dev_work, apr_add_child_devices);
  1215. /* open apr channel tx and rx, store as global */
  1216. ret = habmm_socket_open(&hab_handle_tx,
  1217. MM_AUD_1,
  1218. 0xFFFFFFFF,
  1219. HABMM_SOCKET_OPEN_FLAGS_SINGLE_BE_SINGLE_FE);
  1220. if (ret) {
  1221. pr_err("%s: habmm_socket_open tx failed %d\n", __func__, ret);
  1222. return ret;
  1223. }
  1224. spin_lock_init(&hab_tx_lock);
  1225. ret = habmm_socket_open(&hab_handle_rx,
  1226. MM_AUD_2,
  1227. 0xFFFFFFFF,
  1228. HABMM_SOCKET_OPEN_FLAGS_SINGLE_BE_SINGLE_FE);
  1229. if (ret) {
  1230. pr_err("%s: habmm_socket_open rx failed %d\n", __func__, ret);
  1231. habmm_socket_close(hab_handle_tx);
  1232. return ret;
  1233. }
  1234. pr_info("%s: hab_handle_tx %x hab_handle_rx %x\n",
  1235. __func__, hab_handle_tx, hab_handle_rx);
  1236. /* create apr ch rx cb thread */
  1237. apr_vm_cb_thread_task = kthread_run(apr_vm_cb_thread,
  1238. NULL,
  1239. APR_VM_CB_THREAD_NAME);
  1240. if (IS_ERR(apr_vm_cb_thread_task)) {
  1241. ret = PTR_ERR(apr_vm_cb_thread_task);
  1242. pr_err("%s: kthread_run failed %d\n", __func__, ret);
  1243. habmm_socket_close(hab_handle_tx);
  1244. habmm_socket_close(hab_handle_rx);
  1245. return ret;
  1246. }
  1247. pid = apr_vm_cb_thread_task->pid;
  1248. pr_info("%s: apr_vm_cb_thread started pid %d\n",
  1249. __func__, pid);
  1250. for (i = 0; i < APR_DEST_MAX; i++)
  1251. for (j = 0; j < APR_CLIENT_MAX; j++) {
  1252. mutex_init(&client[i][j].m_lock);
  1253. for (k = 0; k < APR_SVC_MAX; k++) {
  1254. mutex_init(&client[i][j].svc[k].m_lock);
  1255. spin_lock_init(&client[i][j].svc[k].w_lock);
  1256. }
  1257. }
  1258. spin_lock(&apr_priv->apr_lock);
  1259. apr_priv->is_initial_boot = true;
  1260. spin_unlock(&apr_priv->apr_lock);
  1261. apr_vm_set_subsys_state();
  1262. mutex_init(&q6.lock);
  1263. apr_reset_workqueue = create_singlethread_workqueue("apr_driver");
  1264. if (!apr_reset_workqueue) {
  1265. habmm_socket_close(hab_handle_tx);
  1266. habmm_socket_close(hab_handle_rx);
  1267. kthread_stop(apr_vm_cb_thread_task);
  1268. apr_priv = NULL;
  1269. return -ENOMEM;
  1270. }
  1271. apr_pkt_ctx = ipc_log_context_create(APR_PKT_IPC_LOG_PAGE_CNT,
  1272. "apr", 0);
  1273. if (!apr_pkt_ctx)
  1274. pr_err("%s: Unable to create ipc log context\n", __func__);
  1275. ret = of_property_read_string(pdev->dev.of_node,
  1276. "qcom,subsys-name",
  1277. (const char **)(&subsys_name));
  1278. if (ret) {
  1279. pr_err("%s: missing subsys-name entry in dt node\n", __func__);
  1280. return -EINVAL;
  1281. }
  1282. if (!strcmp(subsys_name, "apr_adsp")) {
  1283. subsys_notif_register("apr_adsp",
  1284. AUDIO_NOTIFIER_ADSP_DOMAIN,
  1285. &adsp_service_nb);
  1286. } else if (!strcmp(subsys_name, "apr_modem")) {
  1287. subsys_notif_register("apr_modem",
  1288. AUDIO_NOTIFIER_MODEM_DOMAIN,
  1289. &modem_service_nb);
  1290. } else {
  1291. pr_err("%s: invalid subsys-name %s\n", __func__, subsys_name);
  1292. return -EINVAL;
  1293. }
  1294. ret = snd_event_client_register(&pdev->dev, &apr_ssr_ops, NULL);
  1295. if (ret) {
  1296. pr_err("%s: Registration with SND event fwk failed ret = %d\n",
  1297. __func__, ret);
  1298. ret = 0;
  1299. }
  1300. return apr_debug_init();
  1301. }
  1302. static int apr_remove(struct platform_device *pdev)
  1303. {
  1304. habmm_socket_close(hab_handle_tx);
  1305. habmm_socket_close(hab_handle_rx);
  1306. kthread_stop(apr_vm_cb_thread_task);
  1307. snd_event_client_deregister(&pdev->dev);
  1308. apr_cleanup();
  1309. apr_priv = NULL;
  1310. return 0;
  1311. }
  1312. static const struct of_device_id apr_machine_of_match[] = {
  1313. { .compatible = "qcom,msm-audio-apr", },
  1314. {},
  1315. };
  1316. static struct platform_driver apr_driver = {
  1317. .probe = apr_probe,
  1318. .remove = apr_remove,
  1319. .driver = {
  1320. .name = "audio_apr",
  1321. .owner = THIS_MODULE,
  1322. .of_match_table = apr_machine_of_match,
  1323. }
  1324. };
  1325. module_platform_driver(apr_driver);
  1326. MODULE_DESCRIPTION("APR DRIVER");
  1327. MODULE_LICENSE("GPL v2");
  1328. MODULE_DEVICE_TABLE(of, apr_machine_of_match);