apr_vm.c 35 KB

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