apr.c 27 KB

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