apr_vm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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. /**
  975. * apr_start_rx_rt - Clients call to vote for thread
  976. * priority upgrade whenever needed.
  977. *
  978. * @handle: APR service handle
  979. *
  980. * Returns 0 on success or error otherwise.
  981. */
  982. int apr_start_rx_rt(void *handle)
  983. {
  984. int rc = 0;
  985. return rc;
  986. }
  987. EXPORT_SYMBOL(apr_start_rx_rt);
  988. /**
  989. * apr_end_rx_rt - Clients call to unvote for thread
  990. * priority upgrade (perviously voted with
  991. * apr_start_rx_rt()).
  992. *
  993. * @handle: APR service handle
  994. *
  995. * Returns 0 on success or error otherwise.
  996. */
  997. int apr_end_rx_rt(void *handle)
  998. {
  999. int rc = 0;
  1000. return rc;
  1001. }
  1002. EXPORT_SYMBOL(apr_end_rx_rt);
  1003. /**
  1004. * apr_deregister - Clients call to de-register
  1005. * from APR.
  1006. *
  1007. * @handle: APR service handle to de-register
  1008. *
  1009. * Returns 0 on success or -EINVAL on error.
  1010. */
  1011. int apr_deregister(void *handle)
  1012. {
  1013. struct apr_svc *svc = handle;
  1014. struct apr_client *clnt;
  1015. uint16_t dest_id;
  1016. uint16_t client_id;
  1017. if (!handle)
  1018. return -EINVAL;
  1019. mutex_lock(&svc->m_lock);
  1020. dest_id = svc->dest_id;
  1021. client_id = svc->client_id;
  1022. clnt = &client[dest_id][client_id];
  1023. if (svc->port_cnt > 0 || svc->svc_cnt > 0) {
  1024. if (svc->port_cnt)
  1025. svc->port_cnt--;
  1026. else if (svc->svc_cnt)
  1027. svc->svc_cnt--;
  1028. if (!svc->port_cnt && !svc->svc_cnt) {
  1029. client[dest_id][client_id].svc_cnt--;
  1030. svc->need_reset = 0x0;
  1031. }
  1032. } else if (client[dest_id][client_id].svc_cnt > 0) {
  1033. client[dest_id][client_id].svc_cnt--;
  1034. if (!client[dest_id][client_id].svc_cnt) {
  1035. svc->need_reset = 0x0;
  1036. pr_debug("%s: service is reset %p\n", __func__, svc);
  1037. }
  1038. }
  1039. if (!svc->port_cnt && !svc->svc_cnt) {
  1040. if (apr_vm_rel_svc(svc->dest_domain, svc->id, svc->vm_handle))
  1041. pr_err("%s: apr_vm_rel_svc failed\n", __func__);
  1042. svc->priv = NULL;
  1043. svc->id = 0;
  1044. svc->vm_dest_svc = 0;
  1045. svc->fn = NULL;
  1046. svc->dest_id = 0;
  1047. svc->client_id = 0;
  1048. svc->need_reset = 0x0;
  1049. svc->vm_handle = 0;
  1050. }
  1051. mutex_unlock(&svc->m_lock);
  1052. return 0;
  1053. }
  1054. EXPORT_SYMBOL(apr_deregister);
  1055. /**
  1056. * apr_reset - sets up workqueue to de-register
  1057. * the given APR service handle.
  1058. *
  1059. * @handle: APR service handle
  1060. *
  1061. */
  1062. void apr_reset(void *handle)
  1063. {
  1064. struct apr_reset_work *apr_reset_worker = NULL;
  1065. if (!handle)
  1066. return;
  1067. pr_debug("%s: handle[%pK]\n", __func__, handle);
  1068. if (apr_reset_workqueue == NULL) {
  1069. pr_err("%s: apr_reset_workqueue is NULL\n", __func__);
  1070. return;
  1071. }
  1072. apr_reset_worker = kzalloc(sizeof(struct apr_reset_work),
  1073. GFP_ATOMIC);
  1074. if (apr_reset_worker == NULL) {
  1075. pr_err("%s: mem failure\n", __func__);
  1076. return;
  1077. }
  1078. apr_reset_worker->handle = handle;
  1079. INIT_WORK(&apr_reset_worker->work, apr_reset_deregister);
  1080. queue_work(apr_reset_workqueue, &apr_reset_worker->work);
  1081. }
  1082. EXPORT_SYMBOL(apr_reset);
  1083. /* Dispatch the Reset events to Modem and audio clients */
  1084. static void dispatch_event(unsigned long code, uint16_t proc)
  1085. {
  1086. struct apr_client *apr_client;
  1087. struct apr_client_data data;
  1088. struct apr_svc *svc;
  1089. uint16_t clnt;
  1090. int i, j;
  1091. memset(&data, 0, sizeof(data));
  1092. data.opcode = RESET_EVENTS;
  1093. data.reset_event = code;
  1094. /* Service domain can be different from the processor */
  1095. data.reset_proc = apr_get_reset_domain(proc);
  1096. clnt = APR_CLIENT_AUDIO;
  1097. apr_client = &client[proc][clnt];
  1098. for (i = 0; i < APR_SVC_MAX; i++) {
  1099. mutex_lock(&apr_client->svc[i].m_lock);
  1100. if (apr_client->svc[i].fn) {
  1101. apr_client->svc[i].need_reset = 0x1;
  1102. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  1103. }
  1104. if (apr_client->svc[i].port_cnt) {
  1105. svc = &(apr_client->svc[i]);
  1106. svc->need_reset = 0x1;
  1107. for (j = 0; j < APR_MAX_PORTS; j++)
  1108. if (svc->port_fn[j])
  1109. svc->port_fn[j](&data,
  1110. svc->port_priv[j]);
  1111. }
  1112. mutex_unlock(&apr_client->svc[i].m_lock);
  1113. }
  1114. clnt = APR_CLIENT_VOICE;
  1115. apr_client = &client[proc][clnt];
  1116. for (i = 0; i < APR_SVC_MAX; i++) {
  1117. mutex_lock(&apr_client->svc[i].m_lock);
  1118. if (apr_client->svc[i].fn) {
  1119. apr_client->svc[i].need_reset = 0x1;
  1120. apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
  1121. }
  1122. if (apr_client->svc[i].port_cnt) {
  1123. svc = &(apr_client->svc[i]);
  1124. svc->need_reset = 0x1;
  1125. for (j = 0; j < APR_MAX_PORTS; j++)
  1126. if (svc->port_fn[j])
  1127. svc->port_fn[j](&data,
  1128. svc->port_priv[j]);
  1129. }
  1130. mutex_unlock(&apr_client->svc[i].m_lock);
  1131. }
  1132. }
  1133. static int apr_notifier_service_cb(struct notifier_block *this,
  1134. unsigned long opcode, void *data)
  1135. {
  1136. struct audio_notifier_cb_data *cb_data = data;
  1137. if (cb_data == NULL) {
  1138. pr_err("%s: Callback data is NULL!\n", __func__);
  1139. goto done;
  1140. }
  1141. pr_debug("%s: Service opcode 0x%lx, domain %d\n",
  1142. __func__, opcode, cb_data->domain);
  1143. switch (opcode) {
  1144. case AUDIO_NOTIFIER_SERVICE_DOWN:
  1145. /*
  1146. * Use flag to ignore down notifications during
  1147. * initial boot. There is no benefit from error
  1148. * recovery notifications during initial boot
  1149. * up since everything is expected to be down.
  1150. */
  1151. spin_lock(&apr_priv->apr_lock);
  1152. if (apr_priv->is_initial_boot) {
  1153. spin_unlock(&apr_priv->apr_lock);
  1154. break;
  1155. }
  1156. spin_unlock(&apr_priv->apr_lock);
  1157. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  1158. apr_modem_down(opcode);
  1159. else
  1160. apr_adsp_down(opcode);
  1161. break;
  1162. case AUDIO_NOTIFIER_SERVICE_UP:
  1163. if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
  1164. apr_modem_up();
  1165. else
  1166. apr_adsp_up();
  1167. spin_lock(&apr_priv->apr_lock);
  1168. apr_priv->is_initial_boot = false;
  1169. spin_unlock(&apr_priv->apr_lock);
  1170. break;
  1171. default:
  1172. break;
  1173. }
  1174. done:
  1175. return NOTIFY_OK;
  1176. }
  1177. static struct notifier_block adsp_service_nb = {
  1178. .notifier_call = apr_notifier_service_cb,
  1179. .priority = 0,
  1180. };
  1181. static struct notifier_block modem_service_nb = {
  1182. .notifier_call = apr_notifier_service_cb,
  1183. .priority = 0,
  1184. };
  1185. #ifdef CONFIG_DEBUG_FS
  1186. static int __init apr_debug_init(void)
  1187. {
  1188. debugfs_apr_debug = debugfs_create_file("msm_apr_debug",
  1189. S_IFREG | 0444, NULL, NULL,
  1190. &apr_debug_ops);
  1191. return 0;
  1192. }
  1193. #else
  1194. static int __init apr_debug_init(void)
  1195. {
  1196. return 0;
  1197. }
  1198. #endif
  1199. static void apr_cleanup(void)
  1200. {
  1201. int i, j, k;
  1202. of_platform_depopulate(apr_priv->dev);
  1203. subsys_notif_deregister(subsys_name);
  1204. if (apr_reset_workqueue) {
  1205. flush_workqueue(apr_reset_workqueue);
  1206. destroy_workqueue(apr_reset_workqueue);
  1207. }
  1208. mutex_destroy(&q6.lock);
  1209. for (i = 0; i < APR_DEST_MAX; i++) {
  1210. for (j = 0; j < APR_CLIENT_MAX; j++) {
  1211. mutex_destroy(&client[i][j].m_lock);
  1212. for (k = 0; k < APR_SVC_MAX; k++)
  1213. mutex_destroy(&client[i][j].svc[k].m_lock);
  1214. }
  1215. }
  1216. #ifdef CONFIG_DEBUG_FS
  1217. debugfs_remove(debugfs_apr_debug);
  1218. #endif
  1219. }
  1220. static int apr_probe(struct platform_device *pdev)
  1221. {
  1222. int i, j, k, ret = 0;
  1223. init_waitqueue_head(&modem_wait);
  1224. apr_priv = devm_kzalloc(&pdev->dev, sizeof(*apr_priv), GFP_KERNEL);
  1225. if (!apr_priv)
  1226. return -ENOMEM;
  1227. apr_priv->dev = &pdev->dev;
  1228. spin_lock_init(&apr_priv->apr_lock);
  1229. INIT_WORK(&apr_priv->add_chld_dev_work, apr_add_child_devices);
  1230. /* open apr channel tx and rx, store as global */
  1231. ret = habmm_socket_open(&hab_handle_tx,
  1232. MM_AUD_1,
  1233. 0xFFFFFFFF,
  1234. HABMM_SOCKET_OPEN_FLAGS_SINGLE_BE_SINGLE_FE);
  1235. if (ret) {
  1236. pr_err("%s: habmm_socket_open tx failed %d\n", __func__, ret);
  1237. return ret;
  1238. }
  1239. spin_lock_init(&hab_tx_lock);
  1240. ret = habmm_socket_open(&hab_handle_rx,
  1241. MM_AUD_2,
  1242. 0xFFFFFFFF,
  1243. HABMM_SOCKET_OPEN_FLAGS_SINGLE_BE_SINGLE_FE);
  1244. if (ret) {
  1245. pr_err("%s: habmm_socket_open rx failed %d\n", __func__, ret);
  1246. habmm_socket_close(hab_handle_tx);
  1247. return ret;
  1248. }
  1249. pr_info("%s: hab_handle_tx %x hab_handle_rx %x\n",
  1250. __func__, hab_handle_tx, hab_handle_rx);
  1251. /* create apr ch rx cb thread */
  1252. apr_vm_cb_thread_task = kthread_run(apr_vm_cb_thread,
  1253. NULL,
  1254. APR_VM_CB_THREAD_NAME);
  1255. if (IS_ERR(apr_vm_cb_thread_task)) {
  1256. ret = PTR_ERR(apr_vm_cb_thread_task);
  1257. pr_err("%s: kthread_run failed %d\n", __func__, ret);
  1258. habmm_socket_close(hab_handle_tx);
  1259. habmm_socket_close(hab_handle_rx);
  1260. return ret;
  1261. }
  1262. pid = apr_vm_cb_thread_task->pid;
  1263. pr_info("%s: apr_vm_cb_thread started pid %d\n",
  1264. __func__, pid);
  1265. for (i = 0; i < APR_DEST_MAX; i++)
  1266. for (j = 0; j < APR_CLIENT_MAX; j++) {
  1267. mutex_init(&client[i][j].m_lock);
  1268. for (k = 0; k < APR_SVC_MAX; k++) {
  1269. mutex_init(&client[i][j].svc[k].m_lock);
  1270. spin_lock_init(&client[i][j].svc[k].w_lock);
  1271. }
  1272. }
  1273. spin_lock(&apr_priv->apr_lock);
  1274. apr_priv->is_initial_boot = true;
  1275. spin_unlock(&apr_priv->apr_lock);
  1276. apr_vm_set_subsys_state();
  1277. mutex_init(&q6.lock);
  1278. apr_reset_workqueue = create_singlethread_workqueue("apr_driver");
  1279. if (!apr_reset_workqueue) {
  1280. habmm_socket_close(hab_handle_tx);
  1281. habmm_socket_close(hab_handle_rx);
  1282. kthread_stop(apr_vm_cb_thread_task);
  1283. apr_priv = NULL;
  1284. return -ENOMEM;
  1285. }
  1286. apr_pkt_ctx = ipc_log_context_create(APR_PKT_IPC_LOG_PAGE_CNT,
  1287. "apr", 0);
  1288. if (!apr_pkt_ctx)
  1289. pr_err("%s: Unable to create ipc log context\n", __func__);
  1290. ret = of_property_read_string(pdev->dev.of_node,
  1291. "qcom,subsys-name",
  1292. (const char **)(&subsys_name));
  1293. if (ret) {
  1294. pr_err("%s: missing subsys-name entry in dt node\n", __func__);
  1295. return -EINVAL;
  1296. }
  1297. if (!strcmp(subsys_name, "apr_adsp")) {
  1298. subsys_notif_register("apr_adsp",
  1299. AUDIO_NOTIFIER_ADSP_DOMAIN,
  1300. &adsp_service_nb);
  1301. } else if (!strcmp(subsys_name, "apr_modem")) {
  1302. subsys_notif_register("apr_modem",
  1303. AUDIO_NOTIFIER_MODEM_DOMAIN,
  1304. &modem_service_nb);
  1305. } else {
  1306. pr_err("%s: invalid subsys-name %s\n", __func__, subsys_name);
  1307. return -EINVAL;
  1308. }
  1309. ret = snd_event_client_register(&pdev->dev, &apr_ssr_ops, NULL);
  1310. if (ret) {
  1311. pr_err("%s: Registration with SND event fwk failed ret = %d\n",
  1312. __func__, ret);
  1313. ret = 0;
  1314. }
  1315. return apr_debug_init();
  1316. }
  1317. static int apr_remove(struct platform_device *pdev)
  1318. {
  1319. habmm_socket_close(hab_handle_tx);
  1320. habmm_socket_close(hab_handle_rx);
  1321. kthread_stop(apr_vm_cb_thread_task);
  1322. snd_event_client_deregister(&pdev->dev);
  1323. apr_cleanup();
  1324. apr_priv = NULL;
  1325. return 0;
  1326. }
  1327. static const struct of_device_id apr_machine_of_match[] = {
  1328. { .compatible = "qcom,msm-audio-apr", },
  1329. {},
  1330. };
  1331. static struct platform_driver apr_driver = {
  1332. .probe = apr_probe,
  1333. .remove = apr_remove,
  1334. .driver = {
  1335. .name = "audio_apr",
  1336. .owner = THIS_MODULE,
  1337. .of_match_table = apr_machine_of_match,
  1338. }
  1339. };
  1340. module_platform_driver(apr_driver);
  1341. MODULE_DESCRIPTION("APR DRIVER");
  1342. MODULE_LICENSE("GPL v2");
  1343. MODULE_DEVICE_TABLE(of, apr_machine_of_match);