q6lsm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. * Copyright (c) 2013-2015, Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/fs.h>
  14. #include <linux/mutex.h>
  15. #include <linux/wait.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/sched.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/delay.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/slab.h>
  23. #include <linux/memory_alloc.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/time.h>
  26. #include <linux/atomic.h>
  27. #include <sound/apr_audio-v2.h>
  28. #include <sound/lsm_params.h>
  29. #include <sound/q6core.h>
  30. #include <sound/q6lsm.h>
  31. #include <asm/ioctls.h>
  32. #include <mach/memory.h>
  33. #include <mach/debug_mm.h>
  34. #include "audio_acdb.h"
  35. #define APR_TIMEOUT (5 * HZ)
  36. #define LSM_CAL_SIZE 4096
  37. #define LSM_ALIGN_BOUNDARY 512
  38. enum {
  39. CMD_STATE_CLEARED = 0,
  40. CMD_STATE_WAIT_RESP = 1,
  41. };
  42. enum {
  43. LSM_INVALID_SESSION_ID = 0,
  44. LSM_MIN_SESSION_ID = 1,
  45. LSM_MAX_SESSION_ID = 8,
  46. LSM_CONTROL_SESSION = 0x0F,
  47. };
  48. #define CHECK_SESSION(x) (x < LSM_MIN_SESSION_ID || x > LSM_MAX_SESSION_ID)
  49. struct lsm_common {
  50. void *apr;
  51. atomic_t apr_users;
  52. struct lsm_client common_client[LSM_MAX_SESSION_ID + 1];
  53. struct mutex apr_lock;
  54. };
  55. static struct lsm_common lsm_common;
  56. /*
  57. * mmap_handle_p can point either client->sound_model.mem_map_handle or
  58. * lsm_common.mmap_handle_for_cal.
  59. * mmap_lock must be held while accessing this.
  60. */
  61. static spinlock_t mmap_lock;
  62. static uint32_t *mmap_handle_p;
  63. static spinlock_t lsm_session_lock;
  64. static struct lsm_client *lsm_session[LSM_MAX_SESSION_ID + 1];
  65. static int q6lsm_mmapcallback(struct apr_client_data *data, void *priv);
  66. static int q6lsm_send_cal(struct lsm_client *client);
  67. static int q6lsm_callback(struct apr_client_data *data, void *priv)
  68. {
  69. struct lsm_client *client = (struct lsm_client *)priv;
  70. uint32_t token;
  71. uint32_t *payload;
  72. if (!client || !data) {
  73. WARN_ON(1);
  74. return -EINVAL;
  75. }
  76. if (data->opcode == RESET_EVENTS) {
  77. pr_debug("%s: SSR event received 0x%x, event 0x%x, proc 0x%x\n",
  78. __func__, data->opcode, data->reset_event,
  79. data->reset_proc);
  80. return 0;
  81. }
  82. payload = data->payload;
  83. pr_debug("%s: Session %d opcode 0x%x token 0x%x payload size %d\n"
  84. "payload [0] = %x\n", __func__, client->session,
  85. data->opcode, data->token, data->payload_size, payload[0]);
  86. if (data->opcode == APR_BASIC_RSP_RESULT) {
  87. token = data->token;
  88. switch (payload[0]) {
  89. case LSM_SESSION_CMD_START:
  90. case LSM_SESSION_CMD_STOP:
  91. case LSM_SESSION_CMD_SET_PARAMS:
  92. case LSM_SESSION_CMD_OPEN_TX:
  93. case LSM_SESSION_CMD_CLOSE_TX:
  94. case LSM_SESSION_CMD_REGISTER_SOUND_MODEL:
  95. case LSM_SESSION_CMD_DEREGISTER_SOUND_MODEL:
  96. case LSM_SESSION_CMD_SHARED_MEM_UNMAP_REGIONS:
  97. if (token != client->session &&
  98. payload[0] !=
  99. LSM_SESSION_CMD_DEREGISTER_SOUND_MODEL) {
  100. pr_err("%s: Invalid session %d receivced expected %d\n",
  101. __func__, token, client->session);
  102. return -EINVAL;
  103. }
  104. if (atomic_cmpxchg(&client->cmd_state,
  105. CMD_STATE_WAIT_RESP,
  106. CMD_STATE_CLEARED) ==
  107. CMD_STATE_WAIT_RESP)
  108. wake_up(&client->cmd_wait);
  109. break;
  110. default:
  111. pr_debug("%s: Unknown command 0x%x\n",
  112. __func__, payload[0]);
  113. break;
  114. }
  115. return 0;
  116. }
  117. if (client->cb)
  118. client->cb(data->opcode, data->token, data->payload,
  119. client->priv);
  120. return 0;
  121. }
  122. static int q6lsm_session_alloc(struct lsm_client *client)
  123. {
  124. unsigned long flags;
  125. int n, ret = -ENOMEM;
  126. spin_lock_irqsave(&lsm_session_lock, flags);
  127. for (n = LSM_MIN_SESSION_ID; n <= LSM_MAX_SESSION_ID; n++) {
  128. if (!lsm_session[n]) {
  129. lsm_session[n] = client;
  130. ret = n;
  131. break;
  132. }
  133. }
  134. spin_unlock_irqrestore(&lsm_session_lock, flags);
  135. pr_debug("%s: Alloc Session %d", __func__, n);
  136. return ret;
  137. }
  138. static void q6lsm_session_free(struct lsm_client *client)
  139. {
  140. unsigned long flags;
  141. pr_debug("%s: Freeing session ID %d\n", __func__, client->session);
  142. spin_lock_irqsave(&lsm_session_lock, flags);
  143. lsm_session[client->session] = LSM_INVALID_SESSION_ID;
  144. spin_unlock_irqrestore(&lsm_session_lock, flags);
  145. client->session = LSM_INVALID_SESSION_ID;
  146. }
  147. static void *q6lsm_mmap_apr_reg(void)
  148. {
  149. if (atomic_inc_return(&lsm_common.apr_users) == 1) {
  150. lsm_common.apr =
  151. apr_register("ADSP", "LSM", q6lsm_mmapcallback,
  152. 0x0FFFFFFFF, &lsm_common);
  153. if (!lsm_common.apr) {
  154. pr_debug("%s Unable to register APR LSM common port\n",
  155. __func__);
  156. atomic_dec(&lsm_common.apr_users);
  157. }
  158. }
  159. return lsm_common.apr;
  160. }
  161. static int q6lsm_mmap_apr_dereg(void)
  162. {
  163. if (atomic_read(&lsm_common.apr_users) <= 0) {
  164. WARN("%s: APR common port already closed\n", __func__);
  165. } else {
  166. if (atomic_dec_return(&lsm_common.apr_users) == 0) {
  167. apr_deregister(lsm_common.apr);
  168. pr_debug("%s:APR De-Register common port\n", __func__);
  169. }
  170. }
  171. return 0;
  172. }
  173. struct lsm_client *q6lsm_client_alloc(lsm_app_cb cb, void *priv)
  174. {
  175. struct lsm_client *client;
  176. int n;
  177. pr_debug("%s: enter\n", __func__);
  178. client = kzalloc(sizeof(struct lsm_client), GFP_KERNEL);
  179. if (!client)
  180. return NULL;
  181. n = q6lsm_session_alloc(client);
  182. if (n <= 0) {
  183. kfree(client);
  184. return NULL;
  185. }
  186. client->session = n;
  187. client->cb = cb;
  188. client->priv = priv;
  189. pr_debug("%s:Client session %d\n", __func__, client->session);
  190. if (CHECK_SESSION(client->session)) {
  191. kfree(client);
  192. return NULL;
  193. }
  194. pr_debug("%s:Client Session %d\n", __func__, client->session);
  195. client->apr = apr_register("ADSP", "LSM", q6lsm_callback,
  196. ((client->session) << 8 | client->session),
  197. client);
  198. if (client->apr == NULL) {
  199. pr_err("%s: Registration with APR failed\n", __func__);
  200. goto fail;
  201. }
  202. pr_debug("%s Registering the common port with APR\n", __func__);
  203. client->mmap_apr = q6lsm_mmap_apr_reg();
  204. if (!client->mmap_apr) {
  205. pr_err("%s: APR registration failed\n", __func__);
  206. goto fail;
  207. }
  208. init_waitqueue_head(&client->cmd_wait);
  209. mutex_init(&client->cmd_lock);
  210. atomic_set(&client->cmd_state, CMD_STATE_CLEARED);
  211. pr_debug("%s: New client allocated\n", __func__);
  212. return client;
  213. fail:
  214. q6lsm_client_free(client);
  215. return NULL;
  216. }
  217. void q6lsm_client_free(struct lsm_client *client)
  218. {
  219. if (!client)
  220. return;
  221. if (CHECK_SESSION(client->session)) {
  222. pr_err("%s: Invalid Session %d", __func__, client->session);
  223. return;
  224. }
  225. apr_deregister(client->apr);
  226. client->mmap_apr = NULL;
  227. q6lsm_session_free(client);
  228. q6lsm_mmap_apr_dereg();
  229. mutex_destroy(&client->cmd_lock);
  230. kfree(client);
  231. client = NULL;
  232. }
  233. /*
  234. * q6lsm_apr_send_pkt : If wait == true, hold mutex to prevent from preempting
  235. * other thread's wait.
  236. * If mmap_handle_p != NULL, disable irq and spin lock to
  237. * protect mmap_handle_p
  238. */
  239. static int q6lsm_apr_send_pkt(struct lsm_client *client, void *handle,
  240. void *data, bool wait, uint32_t *mmap_p)
  241. {
  242. int ret;
  243. unsigned long flags = 0;
  244. pr_debug("%s: enter wait %d\n", __func__, wait);
  245. if (wait)
  246. mutex_lock(&lsm_common.apr_lock);
  247. if (mmap_p) {
  248. WARN_ON(!wait);
  249. spin_lock_irqsave(&mmap_lock, flags);
  250. mmap_handle_p = mmap_p;
  251. }
  252. atomic_set(&client->cmd_state, CMD_STATE_WAIT_RESP);
  253. ret = apr_send_pkt(handle, data);
  254. if (mmap_p)
  255. spin_unlock_irqrestore(&mmap_lock, flags);
  256. if (ret < 0) {
  257. pr_err("%s: apr_send_pkt failed %d\n", __func__, ret);
  258. } else if (wait) {
  259. ret = wait_event_timeout(client->cmd_wait,
  260. (atomic_read(&client->cmd_state) ==
  261. CMD_STATE_CLEARED),
  262. APR_TIMEOUT);
  263. if (likely(ret))
  264. ret = 0;
  265. else
  266. pr_err("%s: wait timedout\n", __func__);
  267. } else {
  268. ret = 0;
  269. }
  270. if (wait)
  271. mutex_unlock(&lsm_common.apr_lock);
  272. pr_debug("%s: leave ret %d\n", __func__, ret);
  273. return ret;
  274. }
  275. static void q6lsm_add_hdr(struct lsm_client *client, struct apr_hdr *hdr,
  276. uint32_t pkt_size, bool cmd_flg)
  277. {
  278. pr_debug("%s: pkt_size %d cmd_flg %d session %d\n", __func__,
  279. pkt_size, cmd_flg, client->session);
  280. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  281. APR_HDR_LEN(sizeof(struct apr_hdr)),
  282. APR_PKT_VER);
  283. hdr->src_svc = APR_SVC_LSM;
  284. hdr->src_domain = APR_DOMAIN_APPS;
  285. hdr->dest_svc = APR_SVC_LSM;
  286. hdr->dest_domain = APR_DOMAIN_ADSP;
  287. hdr->src_port = ((client->session << 8) & 0xFF00) | client->session;
  288. hdr->dest_port = ((client->session << 8) & 0xFF00) | client->session;
  289. hdr->pkt_size = pkt_size;
  290. if (cmd_flg)
  291. hdr->token = client->session;
  292. }
  293. int q6lsm_open(struct lsm_client *client)
  294. {
  295. int rc;
  296. struct lsm_stream_cmd_open_tx open;
  297. memset(&open, 0, sizeof(open));
  298. q6lsm_add_hdr(client, &open.hdr, sizeof(open), true);
  299. open.hdr.opcode = LSM_SESSION_CMD_OPEN_TX;
  300. open.app_id = 1;
  301. open.sampling_rate = 16000;
  302. rc = q6lsm_apr_send_pkt(client, client->apr, &open, true, NULL);
  303. if (rc)
  304. pr_err("%s: Open failed opcode 0x%x, rc %d\n",
  305. __func__, open.hdr.opcode, rc);
  306. pr_debug("%s: leave %d\n", __func__, rc);
  307. return rc;
  308. }
  309. static int q6lsm_set_params(struct lsm_client *client)
  310. {
  311. int rc;
  312. struct lsm_cmd_set_params params;
  313. struct lsm_params_payload *payload = &params.payload;
  314. pr_debug("%s: enter\n", __func__);
  315. q6lsm_add_hdr(client, &params.hdr, sizeof(params), true);
  316. params.hdr.opcode = LSM_SESSION_CMD_SET_PARAMS;
  317. params.data_payload_addr_lsw = 0;
  318. params.data_payload_addr_msw = 0;
  319. params.mem_map_handle = 0;
  320. params.data_payload_size = sizeof(struct lsm_params_payload);
  321. payload->op_mode.common.module_id = LSM_MODULE_ID_VOICE_WAKEUP;
  322. payload->op_mode.common.param_id = LSM_PARAM_ID_OPERATION_MODE;
  323. payload->op_mode.common.param_size =
  324. sizeof(struct lsm_param_op_mode) - sizeof(payload->op_mode.common);
  325. payload->op_mode.common.reserved = 0;
  326. payload->op_mode.minor_version = 1;
  327. payload->op_mode.mode = client->mode;
  328. payload->op_mode.reserved = 0;
  329. payload->connect_to_port.common.module_id = LSM_MODULE_ID_VOICE_WAKEUP;
  330. payload->connect_to_port.common.param_id = LSM_PARAM_ID_CONNECT_TO_PORT;
  331. payload->connect_to_port.common.param_size =
  332. sizeof(payload->connect_to_port) - sizeof(payload->op_mode.common);
  333. payload->connect_to_port.common.reserved = 0;
  334. payload->connect_to_port.minor_version = 1;
  335. payload->connect_to_port.port_id = client->connect_to_port;
  336. payload->connect_to_port.reserved = 0;
  337. payload->kwds.common.module_id = LSM_MODULE_ID_VOICE_WAKEUP;
  338. payload->kwds.common.param_id = LSM_PARAM_ID_KEYWORD_DETECT_SENSITIVITY;
  339. payload->kwds.common.param_size =
  340. sizeof(payload->kwds) - sizeof(payload->op_mode.common);
  341. payload->kwds.common.reserved = 0;
  342. payload->kwds.minor_version = 1;
  343. payload->kwds.keyword_sensitivity = client->kw_sensitivity;
  344. payload->kwds.reserved = 0;
  345. payload->uds.common.module_id = LSM_MODULE_ID_VOICE_WAKEUP;
  346. payload->uds.common.param_id = LSM_PARAM_ID_USER_DETECT_SENSITIVITY;
  347. payload->uds.common.param_size =
  348. sizeof(payload->uds) - sizeof(payload->op_mode.common);
  349. payload->uds.common.reserved = 0;
  350. payload->uds.minor_version = 1;
  351. payload->uds.user_sensitivity = client->user_sensitivity;
  352. payload->uds.reserved = 0;
  353. rc = q6lsm_apr_send_pkt(client, client->apr, &params, true, NULL);
  354. if (rc)
  355. pr_err("%s: Failed set_params opcode 0x%x, rc %d\n",
  356. __func__, params.hdr.opcode, rc);
  357. pr_debug("%s: leave %d\n", __func__, rc);
  358. return rc;
  359. }
  360. int q6lsm_register_sound_model(struct lsm_client *client,
  361. enum lsm_detection_mode mode, u16 minkeyword,
  362. u16 minuser, bool detectfailure)
  363. {
  364. int rc;
  365. struct lsm_cmd_reg_snd_model cmd;
  366. memset(&cmd, 0, sizeof(cmd));
  367. if (mode == LSM_MODE_KEYWORD_ONLY_DETECTION) {
  368. client->mode = 0x01;
  369. } else if (mode == LSM_MODE_USER_KEYWORD_DETECTION) {
  370. client->mode = 0x03;
  371. } else {
  372. pr_err("%s: Incorrect detection mode %d\n", __func__, mode);
  373. return -EINVAL;
  374. }
  375. client->mode |= detectfailure << 2;
  376. client->kw_sensitivity = minkeyword;
  377. client->user_sensitivity = minuser;
  378. client->connect_to_port = AFE_PORT_ID_SLIMBUS_MULTI_CHAN_5_TX;
  379. rc = q6lsm_set_params(client);
  380. if (rc < 0) {
  381. pr_err("%s: Failed to set lsm config params\n", __func__);
  382. return rc;
  383. }
  384. rc = q6lsm_send_cal(client);
  385. if (rc < 0) {
  386. pr_err("%s: Failed to send calibration data\n", __func__);
  387. return rc;
  388. }
  389. q6lsm_add_hdr(client, &cmd.hdr, sizeof(cmd), true);
  390. cmd.hdr.opcode = LSM_SESSION_CMD_REGISTER_SOUND_MODEL;
  391. cmd.model_addr_lsw = lower_32_bits(client->sound_model.phys);
  392. cmd.model_addr_msw = upper_32_bits(client->sound_model.phys);
  393. cmd.model_size = client->sound_model.size;
  394. /* read updated mem_map_handle by q6lsm_mmapcallback */
  395. rmb();
  396. cmd.mem_map_handle = client->sound_model.mem_map_handle;
  397. pr_debug("%s: addr %pa, size %d, handle %x\n", __func__,
  398. &client->sound_model.phys, cmd.model_size, cmd.mem_map_handle);
  399. rc = q6lsm_apr_send_pkt(client, client->apr, &cmd, true, NULL);
  400. if (rc)
  401. pr_err("%s: Failed cmd op[0x%x]rc[%d]\n", __func__,
  402. cmd.hdr.opcode, rc);
  403. else
  404. pr_debug("%s: Register sound model succeeded\n", __func__);
  405. return rc;
  406. }
  407. int q6lsm_deregister_sound_model(struct lsm_client *client)
  408. {
  409. int rc;
  410. struct lsm_cmd_reg_snd_model cmd;
  411. if (!client || !client->apr) {
  412. pr_err("APR handle NULL\n");
  413. return -EINVAL;
  414. }
  415. pr_debug("%s: session[%d]", __func__, client->session);
  416. if (CHECK_SESSION(client->session))
  417. return -EINVAL;
  418. memset(&cmd, 0, sizeof(cmd));
  419. q6lsm_add_hdr(client, &cmd.hdr, sizeof(cmd.hdr), false);
  420. cmd.hdr.opcode = LSM_SESSION_CMD_DEREGISTER_SOUND_MODEL;
  421. rc = q6lsm_apr_send_pkt(client, client->apr, &cmd.hdr, true, NULL);
  422. if (rc < 0) {
  423. pr_err("%s: Failed cmd opcode 0x%x, rc %d\n", __func__,
  424. cmd.hdr.opcode, rc);
  425. } else {
  426. pr_debug("%s: Deregister sound model succeeded\n", __func__);
  427. }
  428. q6lsm_snd_model_buf_free(client);
  429. return rc;
  430. }
  431. static void q6lsm_add_mmaphdr(struct lsm_client *client, struct apr_hdr *hdr,
  432. u32 pkt_size, u32 cmd_flg, u32 token)
  433. {
  434. pr_debug("%s:pkt size=%d cmd_flg=%d session=%d\n", __func__, pkt_size,
  435. cmd_flg, client->session);
  436. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  437. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  438. hdr->src_port = 0x00;
  439. hdr->dest_port = client->session;
  440. if (cmd_flg)
  441. hdr->token = token;
  442. hdr->pkt_size = pkt_size;
  443. return;
  444. }
  445. static int q6lsm_memory_map_regions(struct lsm_client *client,
  446. dma_addr_t dma_addr_p, uint32_t dma_buf_sz,
  447. uint32_t *mmap_p)
  448. {
  449. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  450. struct avs_shared_map_region_payload *mregions = NULL;
  451. void *mmap_region_cmd = NULL;
  452. void *payload = NULL;
  453. int rc;
  454. int cmd_size = 0;
  455. pr_debug("%s: dma_addr_p 0x%pa, dma_buf_sz %d, mmap_p 0x%p, session %d\n",
  456. __func__, &dma_addr_p, dma_buf_sz, mmap_p,
  457. client->session);
  458. if (CHECK_SESSION(client->session))
  459. return -EINVAL;
  460. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions) +
  461. sizeof(struct avs_shared_map_region_payload);
  462. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  463. if (!mmap_region_cmd)
  464. return -ENOMEM;
  465. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  466. q6lsm_add_mmaphdr(client, &mmap_regions->hdr, cmd_size, true,
  467. (client->session << 8));
  468. mmap_regions->hdr.opcode = LSM_SESSION_CMD_SHARED_MEM_MAP_REGIONS;
  469. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  470. mmap_regions->num_regions = 1;
  471. mmap_regions->property_flag = 0x00;
  472. payload = ((u8 *)mmap_region_cmd +
  473. sizeof(struct avs_cmd_shared_mem_map_regions));
  474. mregions = (struct avs_shared_map_region_payload *)payload;
  475. mregions->shm_addr_lsw = lower_32_bits(dma_addr_p);
  476. mregions->shm_addr_msw = upper_32_bits(dma_addr_p);
  477. mregions->mem_size_bytes = dma_buf_sz;
  478. rc = q6lsm_apr_send_pkt(client, client->mmap_apr, mmap_region_cmd,
  479. true, mmap_p);
  480. if (rc)
  481. pr_err("%s: Failed mmap_regions opcode 0x%x, rc %d\n",
  482. __func__, mmap_regions->hdr.opcode, rc);
  483. pr_debug("%s: leave %d\n", __func__, rc);
  484. kfree(mmap_region_cmd);
  485. return rc;
  486. }
  487. static int q6lsm_memory_unmap_regions(struct lsm_client *client,
  488. uint32_t handle)
  489. {
  490. struct avs_cmd_shared_mem_unmap_regions unmap;
  491. int rc = 0;
  492. int cmd_size = 0;
  493. if (CHECK_SESSION(client->session))
  494. return -EINVAL;
  495. cmd_size = sizeof(struct avs_cmd_shared_mem_unmap_regions);
  496. q6lsm_add_mmaphdr(client, &unmap.hdr, cmd_size,
  497. true, (client->session << 8));
  498. unmap.hdr.opcode = LSM_SESSION_CMD_SHARED_MEM_UNMAP_REGIONS;
  499. unmap.mem_map_handle = handle;
  500. pr_debug("%s: unmap handle 0x%x\n", __func__, unmap.mem_map_handle);
  501. rc = q6lsm_apr_send_pkt(client, client->mmap_apr, &unmap, true,
  502. NULL);
  503. if (rc)
  504. pr_err("%s: Failed mmap_regions opcode 0x%x rc %d\n",
  505. __func__, unmap.hdr.opcode, rc);
  506. return rc;
  507. }
  508. static int q6lsm_send_cal(struct lsm_client *client)
  509. {
  510. int rc;
  511. struct lsm_cmd_set_params params;
  512. struct acdb_cal_block lsm_cal;
  513. pr_debug("%s: Session %d\n", __func__, client->session);
  514. if (CHECK_SESSION(client->session))
  515. return -EINVAL;
  516. memset(&lsm_cal, 0, sizeof(lsm_cal));
  517. get_lsm_cal(&lsm_cal);
  518. /* Cache mmap address, only map once or if new addr */
  519. lsm_common.common_client[client->session].session = client->session;
  520. q6lsm_add_hdr(client, &params.hdr, sizeof(params), true);
  521. params.hdr.opcode = LSM_SESSION_CMD_SET_PARAMS;
  522. params.data_payload_addr_lsw = lower_32_bits(client->lsm_cal_phy_addr);
  523. params.data_payload_addr_msw = upper_32_bits(client->lsm_cal_phy_addr);
  524. params.mem_map_handle = client->sound_model.mem_map_handle;
  525. params.data_payload_size = lsm_cal.cal_size;
  526. pr_debug("%s: Cal Size = %x", __func__, client->lsm_cal_size);
  527. rc = q6lsm_apr_send_pkt(client, client->apr, &params, true, NULL);
  528. if (rc)
  529. pr_err("%s: Failed set_params opcode 0x%x, rc %d\n",
  530. __func__, params.hdr.opcode, rc);
  531. return rc;
  532. }
  533. int q6lsm_snd_model_buf_free(struct lsm_client *client)
  534. {
  535. int rc;
  536. pr_debug("%s: Session id %d\n", __func__, client->session);
  537. if (CHECK_SESSION(client->session))
  538. return -EINVAL;
  539. mutex_lock(&client->cmd_lock);
  540. rc = q6lsm_memory_unmap_regions(client,
  541. client->sound_model.mem_map_handle);
  542. if (rc < 0)
  543. pr_err("%s CMD Memory_unmap_regions failed\n", __func__);
  544. if (client->sound_model.data) {
  545. ion_unmap_kernel(client->sound_model.client,
  546. client->sound_model.handle);
  547. ion_free(client->sound_model.client,
  548. client->sound_model.handle);
  549. ion_client_destroy(client->sound_model.client);
  550. client->sound_model.client = NULL;
  551. client->sound_model.handle = NULL;
  552. client->sound_model.data = NULL;
  553. client->sound_model.phys = 0;
  554. client->lsm_cal_phy_addr = 0;
  555. client->lsm_cal_size = 0;
  556. }
  557. mutex_unlock(&client->cmd_lock);
  558. return rc;
  559. }
  560. static struct lsm_client *q6lsm_get_lsm_client(int session_id)
  561. {
  562. unsigned long flags;
  563. struct lsm_client *client = NULL;
  564. if (session_id == LSM_CONTROL_SESSION) {
  565. client = &lsm_common.common_client[session_id];
  566. goto done;
  567. }
  568. spin_lock_irqsave(&lsm_session_lock, flags);
  569. if (session_id < LSM_MIN_SESSION_ID || session_id > LSM_MAX_SESSION_ID)
  570. pr_err("%s: Invalid session %d\n", __func__, session_id);
  571. else if (!lsm_session[session_id])
  572. pr_err("%s: Not an active session %d\n", __func__, session_id);
  573. else
  574. client = lsm_session[session_id];
  575. spin_unlock_irqrestore(&lsm_session_lock, flags);
  576. done:
  577. return client;
  578. }
  579. /*
  580. * q6lsm_mmapcallback : atomic context
  581. */
  582. static int q6lsm_mmapcallback(struct apr_client_data *data, void *priv)
  583. {
  584. unsigned long flags;
  585. uint32_t command;
  586. uint32_t retcode;
  587. uint32_t sid;
  588. const uint32_t *payload = data->payload;
  589. struct lsm_client *client = NULL;
  590. if (data->opcode == RESET_EVENTS) {
  591. sid = (data->token >> 8) & 0x0F;
  592. pr_debug("%s: SSR event received 0x%x, event 0x%x,\n"
  593. "proc 0x%x SID 0x%x\n", __func__, data->opcode,
  594. data->reset_event, data->reset_proc, sid);
  595. lsm_common.common_client[sid].lsm_cal_phy_addr = 0;
  596. return 0;
  597. }
  598. command = payload[0];
  599. retcode = payload[1];
  600. sid = (data->token >> 8) & 0x0F;
  601. pr_debug("%s: opcode 0x%x command 0x%x return code 0x%x SID 0x%x\n",
  602. __func__, data->opcode, command, retcode, sid);
  603. client = q6lsm_get_lsm_client(sid);
  604. if (!client) {
  605. pr_debug("%s: Session %d already freed\n", __func__, sid);
  606. return 0;
  607. }
  608. switch (data->opcode) {
  609. case LSM_SESSION_CMDRSP_SHARED_MEM_MAP_REGIONS:
  610. if (atomic_read(&client->cmd_state) == CMD_STATE_WAIT_RESP) {
  611. spin_lock_irqsave(&mmap_lock, flags);
  612. *mmap_handle_p = command;
  613. /* spin_unlock_irqrestore implies barrier */
  614. spin_unlock_irqrestore(&mmap_lock, flags);
  615. atomic_set(&client->cmd_state, CMD_STATE_CLEARED);
  616. wake_up(&client->cmd_wait);
  617. }
  618. break;
  619. case APR_BASIC_RSP_RESULT:
  620. if (command == LSM_SESSION_CMD_SHARED_MEM_UNMAP_REGIONS) {
  621. atomic_set(&client->cmd_state, CMD_STATE_CLEARED);
  622. wake_up(&client->cmd_wait);
  623. } else {
  624. pr_warn("%s: Unexpected command 0x%x\n", __func__,
  625. command);
  626. }
  627. break;
  628. default:
  629. pr_debug("%s: command 0x%x return code 0x%x\n",
  630. __func__, command, retcode);
  631. break;
  632. }
  633. if (client->cb)
  634. client->cb(data->opcode, data->token,
  635. data->payload, client->priv);
  636. return 0;
  637. }
  638. int q6lsm_snd_model_buf_alloc(struct lsm_client *client, size_t len)
  639. {
  640. int rc = -EINVAL;
  641. struct acdb_cal_block lsm_cal;
  642. size_t pad_zero = 0, total_mem = 0;
  643. if (!client || len <= LSM_ALIGN_BOUNDARY)
  644. return rc;
  645. memset(&lsm_cal, 0, sizeof(lsm_cal));
  646. mutex_lock(&client->cmd_lock);
  647. get_lsm_cal(&lsm_cal);
  648. pr_debug("%s:Snd Model len = %d cal size %d", __func__,
  649. len, lsm_cal.cal_size);
  650. if (!lsm_cal.cal_paddr) {
  651. pr_err("%s: No LSM calibration set for session", __func__);
  652. mutex_unlock(&client->cmd_lock);
  653. return -EINVAL;
  654. }
  655. if (!client->sound_model.data) {
  656. client->sound_model.size = len;
  657. pad_zero = (LSM_ALIGN_BOUNDARY -
  658. (len % LSM_ALIGN_BOUNDARY));
  659. if ((len > SIZE_MAX - pad_zero) ||
  660. (len + pad_zero >
  661. SIZE_MAX - lsm_cal.cal_size)) {
  662. pr_err("%s: invalid allocation size, len = %zd, pad_zero =%zd, cal_size = %zd\n",
  663. __func__, len, pad_zero,
  664. lsm_cal.cal_size);
  665. rc = -EINVAL;
  666. goto fail;
  667. }
  668. total_mem = pad_zero + len + lsm_cal.cal_size;
  669. pr_debug("%s: Pad zeros sound model %d Total mem %d\n",
  670. __func__, pad_zero, total_mem);
  671. client->sound_model.client =
  672. msm_ion_client_create(UINT_MAX, "lsm_client");
  673. if (IS_ERR_OR_NULL(client->sound_model.client)) {
  674. pr_err("%s: ION create client for AUDIO failed\n",
  675. __func__);
  676. goto fail;
  677. }
  678. client->sound_model.handle =
  679. ion_alloc(client->sound_model.client,
  680. total_mem, SZ_4K, (0x1 << ION_AUDIO_HEAP_ID), 0);
  681. if (IS_ERR_OR_NULL(client->sound_model.handle)) {
  682. pr_err("%s: ION memory allocation for AUDIO failed\n",
  683. __func__);
  684. goto fail;
  685. }
  686. rc = ion_phys(client->sound_model.client,
  687. client->sound_model.handle,
  688. (ion_phys_addr_t *)&client->sound_model.phys,
  689. (size_t *)&len);
  690. if (rc) {
  691. pr_err("%s: ION get physical mem failed, rc%d\n",
  692. __func__, rc);
  693. goto fail;
  694. }
  695. client->sound_model.data =
  696. ion_map_kernel(client->sound_model.client,
  697. client->sound_model.handle);
  698. if (IS_ERR_OR_NULL(client->sound_model.data)) {
  699. pr_err("%s: ION memory mapping failed\n", __func__);
  700. goto fail;
  701. }
  702. memset(client->sound_model.data, 0, len);
  703. client->lsm_cal_phy_addr = (pad_zero +
  704. client->sound_model.phys +
  705. client->sound_model.size);
  706. client->lsm_cal_size = lsm_cal.cal_size;
  707. memcpy((client->sound_model.data + pad_zero +
  708. client->sound_model.size),
  709. (uint32_t *)lsm_cal.cal_kvaddr, lsm_cal.cal_size);
  710. } else {
  711. rc = -EBUSY;
  712. goto fail;
  713. }
  714. mutex_unlock(&client->cmd_lock);
  715. rc = q6lsm_memory_map_regions(client, client->sound_model.phys,
  716. len,
  717. &client->sound_model.mem_map_handle);
  718. if (rc < 0) {
  719. pr_err("%s:CMD Memory_map_regions failed\n", __func__);
  720. goto exit;
  721. }
  722. return 0;
  723. fail:
  724. mutex_unlock(&client->cmd_lock);
  725. exit:
  726. q6lsm_snd_model_buf_free(client);
  727. return rc;
  728. }
  729. static int q6lsm_cmd(struct lsm_client *client, int opcode, bool wait)
  730. {
  731. struct apr_hdr hdr;
  732. int rc;
  733. pr_debug("%s: enter opcode %d wait %d\n", __func__, opcode, wait);
  734. q6lsm_add_hdr(client, &hdr, sizeof(hdr), true);
  735. switch (opcode) {
  736. case LSM_SESSION_CMD_START:
  737. case LSM_SESSION_CMD_STOP:
  738. case LSM_SESSION_CMD_CLOSE_TX:
  739. hdr.opcode = opcode;
  740. break;
  741. default:
  742. pr_err("%s: Invalid opcode %d\n", __func__, opcode);
  743. return -EINVAL;
  744. }
  745. rc = q6lsm_apr_send_pkt(client, client->apr, &hdr, wait, NULL);
  746. if (rc)
  747. pr_err("%s: Failed commmand 0x%x\n", __func__, hdr.opcode);
  748. pr_debug("%s: leave %d\n", __func__, rc);
  749. return rc;
  750. }
  751. int q6lsm_start(struct lsm_client *client, bool wait)
  752. {
  753. return q6lsm_cmd(client, LSM_SESSION_CMD_START, wait);
  754. }
  755. int q6lsm_stop(struct lsm_client *client, bool wait)
  756. {
  757. return q6lsm_cmd(client, LSM_SESSION_CMD_STOP, wait);
  758. }
  759. int q6lsm_close(struct lsm_client *client)
  760. {
  761. return q6lsm_cmd(client, LSM_SESSION_CMD_CLOSE_TX, true);
  762. }
  763. static int __init q6lsm_init(void)
  764. {
  765. int i = 0;
  766. pr_debug("%s\n", __func__);
  767. spin_lock_init(&lsm_session_lock);
  768. spin_lock_init(&mmap_lock);
  769. mutex_init(&lsm_common.apr_lock);
  770. for (; i <= LSM_MAX_SESSION_ID; i++) {
  771. lsm_common.common_client[i].session = LSM_CONTROL_SESSION;
  772. init_waitqueue_head(&lsm_common.common_client[i].cmd_wait);
  773. mutex_init(&lsm_common.common_client[i].cmd_lock);
  774. atomic_set(&lsm_common.common_client[i].cmd_state,
  775. CMD_STATE_CLEARED);
  776. }
  777. return 0;
  778. }
  779. device_initcall(q6lsm_init);