q6core.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /* Copyright (c) 2012-2017, 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/string.h>
  15. #include <linux/types.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/mutex.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <dsp/q6core.h>
  21. #include <dsp/audio_cal_utils.h>
  22. #include <dsp/apr_audio-v2.h>
  23. #include <ipc/apr.h>
  24. #include "adsp_err.h"
  25. #define TIMEOUT_MS 1000
  26. /*
  27. * AVS bring up in the modem is optimitized for the new
  28. * Sub System Restart design and 100 milliseconds timeout
  29. * is sufficient to make sure the Q6 will be ready.
  30. */
  31. #define Q6_READY_TIMEOUT_MS 100
  32. enum {
  33. META_CAL,
  34. CUST_TOP_CAL,
  35. CORE_MAX_CAL
  36. };
  37. enum ver_query_status {
  38. VER_QUERY_UNATTEMPTED,
  39. VER_QUERY_UNSUPPORTED,
  40. VER_QUERY_SUPPORTED
  41. };
  42. struct q6core_avcs_ver_info {
  43. enum ver_query_status status;
  44. struct avcs_fwk_ver_info ver_info;
  45. };
  46. struct q6core_str {
  47. struct apr_svc *core_handle_q;
  48. wait_queue_head_t bus_bw_req_wait;
  49. wait_queue_head_t cmd_req_wait;
  50. wait_queue_head_t avcs_fwk_ver_req_wait;
  51. u32 bus_bw_resp_received;
  52. enum cmd_flags {
  53. FLAG_NONE,
  54. FLAG_CMDRSP_LICENSE_RESULT
  55. } cmd_resp_received_flag;
  56. u32 avcs_fwk_ver_resp_received;
  57. struct mutex cmd_lock;
  58. struct mutex ver_lock;
  59. union {
  60. struct avcs_cmdrsp_get_license_validation_result
  61. cmdrsp_license_result;
  62. } cmd_resp_payload;
  63. u32 param;
  64. struct cal_type_data *cal_data[CORE_MAX_CAL];
  65. uint32_t mem_map_cal_handle;
  66. int32_t adsp_status;
  67. struct q6core_avcs_ver_info q6core_avcs_ver_info;
  68. };
  69. static struct q6core_str q6core_lcl;
  70. struct generic_get_data_ {
  71. int valid;
  72. int size_in_ints;
  73. int ints[];
  74. };
  75. static struct generic_get_data_ *generic_get_data;
  76. static int parse_fwk_version_info(uint32_t *payload)
  77. {
  78. size_t fwk_ver_size;
  79. size_t svc_size;
  80. int num_services;
  81. int ret = 0;
  82. pr_debug("%s: Payload info num services %d\n",
  83. __func__, payload[4]);
  84. /*
  85. * payload1[4] is the number of services running on DSP
  86. * Based on this info, we copy the payload into core
  87. * avcs version info structure.
  88. */
  89. num_services = payload[4];
  90. q6core_lcl.q6core_avcs_ver_info.ver_info.avcs_fwk_version.
  91. num_services = num_services;
  92. if (num_services > VSS_MAX_AVCS_NUM_SERVICES) {
  93. pr_err("%s: num_services: %d greater than max services: %d\n",
  94. __func__, num_services, VSS_MAX_AVCS_NUM_SERVICES);
  95. ret = -EINVAL;
  96. goto done;
  97. }
  98. fwk_ver_size = sizeof(struct avcs_get_fwk_version);
  99. svc_size = num_services * sizeof(struct avs_svc_api_info);
  100. /*
  101. * Dynamically allocate memory for all
  102. * the services based on num_services
  103. */
  104. q6core_lcl.q6core_avcs_ver_info.ver_info.services = NULL;
  105. q6core_lcl.q6core_avcs_ver_info.ver_info.services =
  106. kzalloc(svc_size, GFP_ATOMIC);
  107. if (q6core_lcl.q6core_avcs_ver_info.ver_info.services == NULL) {
  108. ret = -ENOMEM;
  109. goto done;
  110. }
  111. /*
  112. * memcpy is done twice because the memory allocated for
  113. * q6core_lcl.q6core_avcs_ver_info.ver_info is not
  114. * contiguous.
  115. */
  116. memcpy(&q6core_lcl.q6core_avcs_ver_info.ver_info,
  117. (uint8_t *)payload, fwk_ver_size);
  118. memcpy(q6core_lcl.q6core_avcs_ver_info.ver_info.services,
  119. (uint8_t *)&payload[sizeof(struct avcs_get_fwk_version)/
  120. sizeof(uint32_t)], svc_size);
  121. ret = 0;
  122. done:
  123. return ret;
  124. }
  125. static int32_t aprv2_core_fn_q(struct apr_client_data *data, void *priv)
  126. {
  127. uint32_t *payload1;
  128. int ret = 0;
  129. if (data == NULL) {
  130. pr_err("%s: data argument is null\n", __func__);
  131. return -EINVAL;
  132. }
  133. pr_debug("%s: core msg: payload len = %u, apr resp opcode = 0x%x\n",
  134. __func__,
  135. data->payload_size, data->opcode);
  136. switch (data->opcode) {
  137. case APR_BASIC_RSP_RESULT:{
  138. if (data->payload_size == 0) {
  139. pr_err("%s: APR_BASIC_RSP_RESULT No Payload ",
  140. __func__);
  141. return 0;
  142. }
  143. payload1 = data->payload;
  144. switch (payload1[0]) {
  145. case AVCS_CMD_SHARED_MEM_UNMAP_REGIONS:
  146. pr_debug("%s: Cmd = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS status[0x%x]\n",
  147. __func__, payload1[1]);
  148. q6core_lcl.bus_bw_resp_received = 1;
  149. wake_up(&q6core_lcl.bus_bw_req_wait);
  150. break;
  151. case AVCS_CMD_SHARED_MEM_MAP_REGIONS:
  152. pr_debug("%s: Cmd = AVCS_CMD_SHARED_MEM_MAP_REGIONS status[0x%x]\n",
  153. __func__, payload1[1]);
  154. q6core_lcl.bus_bw_resp_received = 1;
  155. wake_up(&q6core_lcl.bus_bw_req_wait);
  156. break;
  157. case AVCS_CMD_REGISTER_TOPOLOGIES:
  158. pr_debug("%s: Cmd = AVCS_CMD_REGISTER_TOPOLOGIES status[0x%x]\n",
  159. __func__, payload1[1]);
  160. /* -ADSP status to match Linux error standard */
  161. q6core_lcl.adsp_status = -payload1[1];
  162. q6core_lcl.bus_bw_resp_received = 1;
  163. wake_up(&q6core_lcl.bus_bw_req_wait);
  164. break;
  165. case AVCS_CMD_DEREGISTER_TOPOLOGIES:
  166. pr_debug("%s: Cmd = AVCS_CMD_DEREGISTER_TOPOLOGIES status[0x%x]\n",
  167. __func__, payload1[1]);
  168. q6core_lcl.bus_bw_resp_received = 1;
  169. wake_up(&q6core_lcl.bus_bw_req_wait);
  170. break;
  171. case AVCS_CMD_GET_FWK_VERSION:
  172. pr_debug("%s: Cmd = AVCS_CMD_GET_FWK_VERSION status[%s]\n",
  173. __func__, adsp_err_get_err_str(payload1[1]));
  174. /* ADSP status to match Linux error standard */
  175. q6core_lcl.adsp_status = -payload1[1];
  176. if (payload1[1] == ADSP_EUNSUPPORTED)
  177. q6core_lcl.q6core_avcs_ver_info.status =
  178. VER_QUERY_UNSUPPORTED;
  179. q6core_lcl.avcs_fwk_ver_resp_received = 1;
  180. wake_up(&q6core_lcl.avcs_fwk_ver_req_wait);
  181. break;
  182. default:
  183. pr_err("%s: Invalid cmd rsp[0x%x][0x%x] opcode %d\n",
  184. __func__,
  185. payload1[0], payload1[1], data->opcode);
  186. break;
  187. }
  188. break;
  189. }
  190. case RESET_EVENTS:{
  191. pr_debug("%s: Reset event received in Core service\n",
  192. __func__);
  193. /*
  194. * no reset for q6core_avcs_ver_info done as
  195. * the data will not change after SSR
  196. */
  197. apr_reset(q6core_lcl.core_handle_q);
  198. q6core_lcl.core_handle_q = NULL;
  199. break;
  200. }
  201. case AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS:
  202. payload1 = data->payload;
  203. pr_debug("%s: AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS handle %d\n",
  204. __func__, payload1[0]);
  205. q6core_lcl.mem_map_cal_handle = payload1[0];
  206. q6core_lcl.bus_bw_resp_received = 1;
  207. wake_up(&q6core_lcl.bus_bw_req_wait);
  208. break;
  209. case AVCS_CMDRSP_ADSP_EVENT_GET_STATE:
  210. payload1 = data->payload;
  211. q6core_lcl.param = payload1[0];
  212. pr_debug("%s: Received ADSP get state response 0x%x\n",
  213. __func__, q6core_lcl.param);
  214. /* ensure .param is updated prior to .bus_bw_resp_received */
  215. wmb();
  216. q6core_lcl.bus_bw_resp_received = 1;
  217. wake_up(&q6core_lcl.bus_bw_req_wait);
  218. break;
  219. case AVCS_CMDRSP_GET_LICENSE_VALIDATION_RESULT:
  220. payload1 = data->payload;
  221. pr_debug("%s: cmd = LICENSE_VALIDATION_RESULT, result = 0x%x\n",
  222. __func__, payload1[0]);
  223. q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result
  224. = payload1[0];
  225. q6core_lcl.cmd_resp_received_flag = FLAG_CMDRSP_LICENSE_RESULT;
  226. wake_up(&q6core_lcl.cmd_req_wait);
  227. break;
  228. case AVCS_CMDRSP_GET_FWK_VERSION:
  229. pr_debug("%s: Received AVCS_CMDRSP_GET_FWK_VERSION\n",
  230. __func__);
  231. payload1 = data->payload;
  232. q6core_lcl.q6core_avcs_ver_info.status = VER_QUERY_SUPPORTED;
  233. q6core_lcl.avcs_fwk_ver_resp_received = 1;
  234. ret = parse_fwk_version_info(payload1);
  235. if (ret < 0)
  236. pr_err("%s: Failed to parse payload:%d\n",
  237. __func__, ret);
  238. wake_up(&q6core_lcl.avcs_fwk_ver_req_wait);
  239. break;
  240. default:
  241. pr_err("%s: Message id from adsp core svc: 0x%x\n",
  242. __func__, data->opcode);
  243. if (generic_get_data) {
  244. generic_get_data->valid = 1;
  245. generic_get_data->size_in_ints =
  246. data->payload_size/sizeof(int);
  247. pr_debug("callback size = %i\n",
  248. data->payload_size);
  249. memcpy(generic_get_data->ints, data->payload,
  250. data->payload_size);
  251. q6core_lcl.bus_bw_resp_received = 1;
  252. wake_up(&q6core_lcl.bus_bw_req_wait);
  253. break;
  254. }
  255. break;
  256. }
  257. return 0;
  258. }
  259. void ocm_core_open(void)
  260. {
  261. if (q6core_lcl.core_handle_q == NULL)
  262. q6core_lcl.core_handle_q = apr_register("ADSP", "CORE",
  263. aprv2_core_fn_q, 0xFFFFFFFF, NULL);
  264. pr_debug("%s: Open_q %pK\n", __func__, q6core_lcl.core_handle_q);
  265. if (q6core_lcl.core_handle_q == NULL)
  266. pr_err("%s: Unable to register CORE\n", __func__);
  267. }
  268. struct cal_block_data *cal_utils_get_cal_block_by_key(
  269. struct cal_type_data *cal_type, uint32_t key)
  270. {
  271. struct list_head *ptr, *next;
  272. struct cal_block_data *cal_block = NULL;
  273. struct audio_cal_info_metainfo *metainfo;
  274. list_for_each_safe(ptr, next,
  275. &cal_type->cal_blocks) {
  276. cal_block = list_entry(ptr,
  277. struct cal_block_data, list);
  278. metainfo = (struct audio_cal_info_metainfo *)
  279. cal_block->cal_info;
  280. if (metainfo->nKey != key) {
  281. pr_debug("%s: metainfo key mismatch!!! found:%x, needed:%x\n",
  282. __func__, metainfo->nKey, key);
  283. } else {
  284. pr_debug("%s: metainfo key match found", __func__);
  285. return cal_block;
  286. }
  287. }
  288. return NULL;
  289. }
  290. static int q6core_send_get_avcs_fwk_ver_cmd(void)
  291. {
  292. struct apr_hdr avcs_ver_cmd;
  293. int ret;
  294. avcs_ver_cmd.hdr_field =
  295. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  296. APR_PKT_VER);
  297. avcs_ver_cmd.pkt_size = sizeof(struct apr_hdr);
  298. avcs_ver_cmd.src_port = 0;
  299. avcs_ver_cmd.dest_port = 0;
  300. avcs_ver_cmd.token = 0;
  301. avcs_ver_cmd.opcode = AVCS_CMD_GET_FWK_VERSION;
  302. q6core_lcl.adsp_status = 0;
  303. q6core_lcl.avcs_fwk_ver_resp_received = 0;
  304. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  305. (uint32_t *) &avcs_ver_cmd);
  306. if (ret < 0) {
  307. pr_err("%s: failed to send apr packet, ret=%d\n", __func__,
  308. ret);
  309. goto done;
  310. }
  311. ret = wait_event_timeout(q6core_lcl.avcs_fwk_ver_req_wait,
  312. (q6core_lcl.avcs_fwk_ver_resp_received == 1),
  313. msecs_to_jiffies(TIMEOUT_MS));
  314. if (!ret) {
  315. pr_err("%s: wait_event timeout for AVCS fwk version info\n",
  316. __func__);
  317. ret = -ETIMEDOUT;
  318. goto done;
  319. }
  320. if (q6core_lcl.adsp_status < 0) {
  321. /*
  322. * adsp_err_get_err_str expects a positive value but we store
  323. * the DSP error as negative to match the Linux error standard.
  324. * Pass in the negated value so adsp_err_get_err_str returns
  325. * the correct string.
  326. */
  327. pr_err("%s: DSP returned error[%s]\n", __func__,
  328. adsp_err_get_err_str(-q6core_lcl.adsp_status));
  329. ret = adsp_err_get_lnx_err_code(q6core_lcl.adsp_status);
  330. goto done;
  331. }
  332. ret = 0;
  333. done:
  334. return ret;
  335. }
  336. int q6core_get_service_version(uint32_t service_id,
  337. struct avcs_fwk_ver_info *ver_info,
  338. size_t size)
  339. {
  340. int i;
  341. uint32_t num_services;
  342. size_t svc_size;
  343. svc_size = q6core_get_avcs_service_size(service_id);
  344. if (svc_size != size) {
  345. pr_err("%s: Expected size: %zu, Provided size: %zu\n",
  346. __func__, svc_size, size);
  347. return -EINVAL;
  348. }
  349. num_services =
  350. q6core_lcl.q6core_avcs_ver_info.ver_info.
  351. avcs_fwk_version.num_services;
  352. if (ver_info == NULL) {
  353. pr_err("%s: NULL parameter ver_info\n", __func__);
  354. return -EINVAL;
  355. }
  356. memcpy(ver_info, &q6core_lcl.q6core_avcs_ver_info.
  357. ver_info.avcs_fwk_version, sizeof(struct avcs_get_fwk_version));
  358. if (service_id == AVCS_SERVICE_ID_ALL) {
  359. memcpy(&ver_info->services[0], &q6core_lcl.
  360. q6core_avcs_ver_info.ver_info.services[0],
  361. (num_services * sizeof(struct avs_svc_api_info)));
  362. } else {
  363. for (i = 0; i < num_services; i++) {
  364. if (q6core_lcl.q6core_avcs_ver_info.
  365. ver_info.services[i].service_id == service_id) {
  366. memcpy(&ver_info->services[0],
  367. &q6core_lcl.q6core_avcs_ver_info.
  368. ver_info.services[i], size);
  369. break;
  370. }
  371. }
  372. }
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(q6core_get_service_version);
  376. size_t q6core_get_avcs_service_size(uint32_t service_id)
  377. {
  378. int ret = 0;
  379. uint32_t num_services;
  380. num_services =
  381. q6core_lcl.q6core_avcs_ver_info.ver_info.
  382. avcs_fwk_version.num_services;
  383. mutex_lock(&(q6core_lcl.ver_lock));
  384. pr_debug("%s: q6core_avcs_ver_info.status(%d)\n", __func__,
  385. q6core_lcl.q6core_avcs_ver_info.status);
  386. switch (q6core_lcl.q6core_avcs_ver_info.status) {
  387. case VER_QUERY_SUPPORTED:
  388. pr_debug("%s: AVCS FWK version query already attempted\n",
  389. __func__);
  390. ret = num_services * sizeof(struct avs_svc_api_info);
  391. break;
  392. case VER_QUERY_UNSUPPORTED:
  393. ret = -EOPNOTSUPP;
  394. break;
  395. case VER_QUERY_UNATTEMPTED:
  396. pr_debug("%s: Attempting AVCS FWK version query\n", __func__);
  397. if (q6core_is_adsp_ready()) {
  398. ret = q6core_send_get_avcs_fwk_ver_cmd();
  399. if (ret == 0)
  400. ret = num_services *
  401. sizeof(struct avs_svc_api_info);
  402. } else {
  403. pr_err("%s: ADSP is not ready to query version\n",
  404. __func__);
  405. ret = -ENODEV;
  406. }
  407. break;
  408. default:
  409. pr_err("%s: Invalid version query status %d\n", __func__,
  410. q6core_lcl.q6core_avcs_ver_info.status);
  411. ret = -EINVAL;
  412. break;
  413. }
  414. mutex_unlock(&(q6core_lcl.ver_lock));
  415. if (service_id != AVCS_SERVICE_ID_ALL)
  416. return sizeof(struct avs_svc_api_info);
  417. return ret;
  418. }
  419. EXPORT_SYMBOL(q6core_get_avcs_service_size);
  420. int32_t core_set_license(uint32_t key, uint32_t module_id)
  421. {
  422. struct avcs_cmd_set_license *cmd_setl = NULL;
  423. struct cal_block_data *cal_block = NULL;
  424. int rc = 0, packet_size = 0;
  425. pr_debug("%s: key:0x%x, id:0x%x\n", __func__, key, module_id);
  426. mutex_lock(&(q6core_lcl.cmd_lock));
  427. if (q6core_lcl.cal_data[META_CAL] == NULL) {
  428. pr_err("%s: cal_data not initialized yet!!\n", __func__);
  429. rc = -EINVAL;
  430. goto cmd_unlock;
  431. }
  432. mutex_lock(&((q6core_lcl.cal_data[META_CAL])->lock));
  433. cal_block = cal_utils_get_cal_block_by_key(
  434. q6core_lcl.cal_data[META_CAL], key);
  435. if (cal_block == NULL ||
  436. cal_block->cal_data.kvaddr == NULL ||
  437. cal_block->cal_data.size <= 0) {
  438. pr_err("%s: Invalid cal block to send", __func__);
  439. rc = -EINVAL;
  440. goto cal_data_unlock;
  441. }
  442. packet_size = sizeof(struct avcs_cmd_set_license) +
  443. cal_block->cal_data.size;
  444. /*round up total packet_size to next 4 byte boundary*/
  445. packet_size = ((packet_size + 0x3)>>2)<<2;
  446. cmd_setl = kzalloc(packet_size, GFP_KERNEL);
  447. if (cmd_setl == NULL) {
  448. rc = -ENOMEM;
  449. goto cal_data_unlock;
  450. }
  451. ocm_core_open();
  452. if (q6core_lcl.core_handle_q == NULL) {
  453. pr_err("%s: apr registration for CORE failed\n", __func__);
  454. rc = -ENODEV;
  455. goto fail_cmd;
  456. }
  457. cmd_setl->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  458. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  459. cmd_setl->hdr.pkt_size = packet_size;
  460. cmd_setl->hdr.src_port = 0;
  461. cmd_setl->hdr.dest_port = 0;
  462. cmd_setl->hdr.token = 0;
  463. cmd_setl->hdr.opcode = AVCS_CMD_SET_LICENSE;
  464. cmd_setl->id = module_id;
  465. cmd_setl->overwrite = 1;
  466. cmd_setl->size = cal_block->cal_data.size;
  467. memcpy((uint8_t *)cmd_setl + sizeof(struct avcs_cmd_set_license),
  468. cal_block->cal_data.kvaddr,
  469. cal_block->cal_data.size);
  470. pr_info("%s: Set license opcode=0x%x, id =0x%x, size = %d\n",
  471. __func__, cmd_setl->hdr.opcode,
  472. cmd_setl->id, cmd_setl->size);
  473. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)cmd_setl);
  474. if (rc < 0)
  475. pr_err("%s: SET_LICENSE failed op[0x%x]rc[%d]\n",
  476. __func__, cmd_setl->hdr.opcode, rc);
  477. fail_cmd:
  478. kfree(cmd_setl);
  479. cal_data_unlock:
  480. mutex_unlock(&((q6core_lcl.cal_data[META_CAL])->lock));
  481. cmd_unlock:
  482. mutex_unlock(&(q6core_lcl.cmd_lock));
  483. return rc;
  484. }
  485. int32_t core_get_license_status(uint32_t module_id)
  486. {
  487. struct avcs_cmd_get_license_validation_result get_lvr_cmd;
  488. int ret = 0;
  489. pr_debug("%s: module_id 0x%x", __func__, module_id);
  490. mutex_lock(&(q6core_lcl.cmd_lock));
  491. ocm_core_open();
  492. if (q6core_lcl.core_handle_q == NULL) {
  493. pr_err("%s: apr registration for CORE failed\n", __func__);
  494. ret = -ENODEV;
  495. goto fail_cmd;
  496. }
  497. get_lvr_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  498. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  499. get_lvr_cmd.hdr.pkt_size =
  500. sizeof(struct avcs_cmd_get_license_validation_result);
  501. get_lvr_cmd.hdr.src_port = 0;
  502. get_lvr_cmd.hdr.dest_port = 0;
  503. get_lvr_cmd.hdr.token = 0;
  504. get_lvr_cmd.hdr.opcode = AVCS_CMD_GET_LICENSE_VALIDATION_RESULT;
  505. get_lvr_cmd.id = module_id;
  506. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &get_lvr_cmd);
  507. if (ret < 0) {
  508. pr_err("%s: license_validation request failed, err %d\n",
  509. __func__, ret);
  510. ret = -EREMOTE;
  511. goto fail_cmd;
  512. }
  513. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  514. mutex_unlock(&(q6core_lcl.cmd_lock));
  515. ret = wait_event_timeout(q6core_lcl.cmd_req_wait,
  516. (q6core_lcl.cmd_resp_received_flag ==
  517. FLAG_CMDRSP_LICENSE_RESULT),
  518. msecs_to_jiffies(TIMEOUT_MS));
  519. mutex_lock(&(q6core_lcl.cmd_lock));
  520. if (!ret) {
  521. pr_err("%s: wait_event timeout for CMDRSP_LICENSE_RESULT\n",
  522. __func__);
  523. ret = -ETIME;
  524. goto fail_cmd;
  525. }
  526. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  527. ret = q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result;
  528. fail_cmd:
  529. mutex_unlock(&(q6core_lcl.cmd_lock));
  530. pr_info("%s: cmdrsp_license_result.result = 0x%x for module 0x%x\n",
  531. __func__, ret, module_id);
  532. return ret;
  533. }
  534. uint32_t core_set_dolby_manufacturer_id(int manufacturer_id)
  535. {
  536. struct adsp_dolby_manufacturer_id payload;
  537. int rc = 0;
  538. pr_debug("%s: manufacturer_id :%d\n", __func__, manufacturer_id);
  539. mutex_lock(&(q6core_lcl.cmd_lock));
  540. ocm_core_open();
  541. if (q6core_lcl.core_handle_q) {
  542. payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  543. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  544. payload.hdr.pkt_size =
  545. sizeof(struct adsp_dolby_manufacturer_id);
  546. payload.hdr.src_port = 0;
  547. payload.hdr.dest_port = 0;
  548. payload.hdr.token = 0;
  549. payload.hdr.opcode = ADSP_CMD_SET_DOLBY_MANUFACTURER_ID;
  550. payload.manufacturer_id = manufacturer_id;
  551. pr_debug("%s: Send Dolby security opcode=0x%x manufacturer ID = %d\n",
  552. __func__,
  553. payload.hdr.opcode, payload.manufacturer_id);
  554. rc = apr_send_pkt(q6core_lcl.core_handle_q,
  555. (uint32_t *)&payload);
  556. if (rc < 0)
  557. pr_err("%s: SET_DOLBY_MANUFACTURER_ID failed op[0x%x]rc[%d]\n",
  558. __func__, payload.hdr.opcode, rc);
  559. }
  560. mutex_unlock(&(q6core_lcl.cmd_lock));
  561. return rc;
  562. }
  563. /**
  564. * q6core_is_adsp_ready - check adsp ready status
  565. *
  566. * Returns true if adsp is ready otherwise returns false
  567. */
  568. bool q6core_is_adsp_ready(void)
  569. {
  570. int rc = 0;
  571. bool ret = false;
  572. struct apr_hdr hdr;
  573. pr_debug("%s: enter\n", __func__);
  574. memset(&hdr, 0, sizeof(hdr));
  575. hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  576. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  577. hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE, 0);
  578. hdr.opcode = AVCS_CMD_ADSP_EVENT_GET_STATE;
  579. mutex_lock(&(q6core_lcl.cmd_lock));
  580. ocm_core_open();
  581. if (q6core_lcl.core_handle_q) {
  582. q6core_lcl.bus_bw_resp_received = 0;
  583. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)&hdr);
  584. if (rc < 0) {
  585. pr_err("%s: Get ADSP state APR packet send event %d\n",
  586. __func__, rc);
  587. goto bail;
  588. }
  589. rc = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  590. (q6core_lcl.bus_bw_resp_received == 1),
  591. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  592. if (rc > 0 && q6core_lcl.bus_bw_resp_received) {
  593. /* ensure to read updated param by callback thread */
  594. rmb();
  595. ret = !!q6core_lcl.param;
  596. }
  597. }
  598. bail:
  599. pr_debug("%s: leave, rc %d, adsp ready %d\n", __func__, rc, ret);
  600. mutex_unlock(&(q6core_lcl.cmd_lock));
  601. return ret;
  602. }
  603. EXPORT_SYMBOL(q6core_is_adsp_ready);
  604. static int q6core_map_memory_regions(phys_addr_t *buf_add, uint32_t mempool_id,
  605. uint32_t *bufsz, uint32_t bufcnt, uint32_t *map_handle)
  606. {
  607. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  608. struct avs_shared_map_region_payload *mregions = NULL;
  609. void *mmap_region_cmd = NULL;
  610. void *payload = NULL;
  611. int ret = 0;
  612. int i = 0;
  613. int cmd_size = 0;
  614. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  615. + sizeof(struct avs_shared_map_region_payload)
  616. * bufcnt;
  617. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  618. if (mmap_region_cmd == NULL)
  619. return -ENOMEM;
  620. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  621. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  622. APR_HDR_LEN(APR_HDR_SIZE),
  623. APR_PKT_VER);
  624. mmap_regions->hdr.pkt_size = cmd_size;
  625. mmap_regions->hdr.src_port = 0;
  626. mmap_regions->hdr.dest_port = 0;
  627. mmap_regions->hdr.token = 0;
  628. mmap_regions->hdr.opcode = AVCS_CMD_SHARED_MEM_MAP_REGIONS;
  629. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
  630. mmap_regions->num_regions = bufcnt & 0x00ff;
  631. mmap_regions->property_flag = 0x00;
  632. payload = ((u8 *) mmap_region_cmd +
  633. sizeof(struct avs_cmd_shared_mem_map_regions));
  634. mregions = (struct avs_shared_map_region_payload *)payload;
  635. for (i = 0; i < bufcnt; i++) {
  636. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  637. mregions->shm_addr_msw =
  638. msm_audio_populate_upper_32_bits(buf_add[i]);
  639. mregions->mem_size_bytes = bufsz[i];
  640. ++mregions;
  641. }
  642. pr_debug("%s: sending memory map, addr %pK, size %d, bufcnt = %d\n",
  643. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  644. *map_handle = 0;
  645. q6core_lcl.bus_bw_resp_received = 0;
  646. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  647. mmap_regions);
  648. if (ret < 0) {
  649. pr_err("%s: mmap regions failed %d\n",
  650. __func__, ret);
  651. ret = -EINVAL;
  652. goto done;
  653. }
  654. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  655. (q6core_lcl.bus_bw_resp_received == 1),
  656. msecs_to_jiffies(TIMEOUT_MS));
  657. if (!ret) {
  658. pr_err("%s: timeout. waited for memory map\n", __func__);
  659. ret = -ETIME;
  660. goto done;
  661. }
  662. *map_handle = q6core_lcl.mem_map_cal_handle;
  663. done:
  664. kfree(mmap_region_cmd);
  665. return ret;
  666. }
  667. static int q6core_memory_unmap_regions(uint32_t mem_map_handle)
  668. {
  669. struct avs_cmd_shared_mem_unmap_regions unmap_regions;
  670. int ret = 0;
  671. memset(&unmap_regions, 0, sizeof(unmap_regions));
  672. unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  673. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  674. unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
  675. unmap_regions.hdr.src_svc = APR_SVC_ADSP_CORE;
  676. unmap_regions.hdr.src_domain = APR_DOMAIN_APPS;
  677. unmap_regions.hdr.src_port = 0;
  678. unmap_regions.hdr.dest_svc = APR_SVC_ADSP_CORE;
  679. unmap_regions.hdr.dest_domain = APR_DOMAIN_ADSP;
  680. unmap_regions.hdr.dest_port = 0;
  681. unmap_regions.hdr.token = 0;
  682. unmap_regions.hdr.opcode = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS;
  683. unmap_regions.mem_map_handle = mem_map_handle;
  684. q6core_lcl.bus_bw_resp_received = 0;
  685. pr_debug("%s: unmap regions map handle %d\n",
  686. __func__, mem_map_handle);
  687. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  688. &unmap_regions);
  689. if (ret < 0) {
  690. pr_err("%s: unmap regions failed %d\n",
  691. __func__, ret);
  692. ret = -EINVAL;
  693. goto done;
  694. }
  695. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  696. (q6core_lcl.bus_bw_resp_received == 1),
  697. msecs_to_jiffies(TIMEOUT_MS));
  698. if (!ret) {
  699. pr_err("%s: timeout. waited for memory_unmap\n",
  700. __func__);
  701. ret = -ETIME;
  702. goto done;
  703. }
  704. done:
  705. return ret;
  706. }
  707. static int q6core_dereg_all_custom_topologies(void)
  708. {
  709. int ret = 0;
  710. struct avcs_cmd_deregister_topologies dereg_top;
  711. memset(&dereg_top, 0, sizeof(dereg_top));
  712. dereg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  713. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  714. dereg_top.hdr.pkt_size = sizeof(dereg_top);
  715. dereg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  716. dereg_top.hdr.src_domain = APR_DOMAIN_APPS;
  717. dereg_top.hdr.src_port = 0;
  718. dereg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  719. dereg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  720. dereg_top.hdr.dest_port = 0;
  721. dereg_top.hdr.token = 0;
  722. dereg_top.hdr.opcode = AVCS_CMD_DEREGISTER_TOPOLOGIES;
  723. dereg_top.payload_addr_lsw = 0;
  724. dereg_top.payload_addr_msw = 0;
  725. dereg_top.mem_map_handle = 0;
  726. dereg_top.payload_size = 0;
  727. dereg_top.mode = AVCS_MODE_DEREGISTER_ALL_CUSTOM_TOPOLOGIES;
  728. q6core_lcl.bus_bw_resp_received = 0;
  729. pr_debug("%s: Deregister topologies mode %d\n",
  730. __func__, dereg_top.mode);
  731. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &dereg_top);
  732. if (ret < 0) {
  733. pr_err("%s: Deregister topologies failed %d\n",
  734. __func__, ret);
  735. goto done;
  736. }
  737. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  738. (q6core_lcl.bus_bw_resp_received == 1),
  739. msecs_to_jiffies(TIMEOUT_MS));
  740. if (!ret) {
  741. pr_err("%s: wait_event timeout for Deregister topologies\n",
  742. __func__);
  743. goto done;
  744. }
  745. done:
  746. return ret;
  747. }
  748. static int q6core_send_custom_topologies(void)
  749. {
  750. int ret = 0;
  751. int ret2 = 0;
  752. struct cal_block_data *cal_block = NULL;
  753. struct avcs_cmd_register_topologies reg_top;
  754. if (!q6core_is_adsp_ready()) {
  755. pr_err("%s: ADSP is not ready!\n", __func__);
  756. return -ENODEV;
  757. }
  758. memset(&reg_top, 0, sizeof(reg_top));
  759. mutex_lock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  760. mutex_lock(&q6core_lcl.cmd_lock);
  761. cal_block = cal_utils_get_only_cal_block(
  762. q6core_lcl.cal_data[CUST_TOP_CAL]);
  763. if (cal_block == NULL) {
  764. pr_debug("%s: cal block is NULL!\n", __func__);
  765. goto unlock;
  766. }
  767. if (cal_block->cal_data.size <= 0) {
  768. pr_debug("%s: cal size is %zd not sending\n",
  769. __func__, cal_block->cal_data.size);
  770. goto unlock;
  771. }
  772. q6core_dereg_all_custom_topologies();
  773. ret = q6core_map_memory_regions(&cal_block->cal_data.paddr, 0,
  774. (uint32_t *)&cal_block->map_data.map_size, 1,
  775. &cal_block->map_data.q6map_handle);
  776. if (!ret) {
  777. pr_err("%s: q6core_map_memory_regions failed\n", __func__);
  778. goto unlock;
  779. }
  780. reg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  781. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  782. reg_top.hdr.pkt_size = sizeof(reg_top);
  783. reg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  784. reg_top.hdr.src_domain = APR_DOMAIN_APPS;
  785. reg_top.hdr.src_port = 0;
  786. reg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  787. reg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  788. reg_top.hdr.dest_port = 0;
  789. reg_top.hdr.token = 0;
  790. reg_top.hdr.opcode = AVCS_CMD_REGISTER_TOPOLOGIES;
  791. reg_top.payload_addr_lsw =
  792. lower_32_bits(cal_block->cal_data.paddr);
  793. reg_top.payload_addr_msw =
  794. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  795. reg_top.mem_map_handle = cal_block->map_data.q6map_handle;
  796. reg_top.payload_size = cal_block->cal_data.size;
  797. q6core_lcl.adsp_status = 0;
  798. q6core_lcl.bus_bw_resp_received = 0;
  799. pr_debug("%s: Register topologies addr %pK, size %zd, map handle %d\n",
  800. __func__, &cal_block->cal_data.paddr, cal_block->cal_data.size,
  801. cal_block->map_data.q6map_handle);
  802. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &reg_top);
  803. if (ret < 0) {
  804. pr_err("%s: Register topologies failed %d\n",
  805. __func__, ret);
  806. goto unmap;
  807. }
  808. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  809. (q6core_lcl.bus_bw_resp_received == 1),
  810. msecs_to_jiffies(TIMEOUT_MS));
  811. if (!ret) {
  812. pr_err("%s: wait_event timeout for Register topologies\n",
  813. __func__);
  814. goto unmap;
  815. }
  816. if (q6core_lcl.adsp_status < 0)
  817. ret = q6core_lcl.adsp_status;
  818. unmap:
  819. ret2 = q6core_memory_unmap_regions(cal_block->map_data.q6map_handle);
  820. if (!ret2) {
  821. pr_err("%s: q6core_memory_unmap_regions failed for map handle %d\n",
  822. __func__, cal_block->map_data.q6map_handle);
  823. ret = ret2;
  824. goto unlock;
  825. }
  826. unlock:
  827. mutex_unlock(&q6core_lcl.cmd_lock);
  828. mutex_unlock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  829. return ret;
  830. }
  831. static int get_cal_type_index(int32_t cal_type)
  832. {
  833. int ret = -EINVAL;
  834. switch (cal_type) {
  835. case AUDIO_CORE_METAINFO_CAL_TYPE:
  836. ret = META_CAL;
  837. break;
  838. case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
  839. ret = CUST_TOP_CAL;
  840. break;
  841. default:
  842. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  843. }
  844. return ret;
  845. }
  846. static int q6core_alloc_cal(int32_t cal_type,
  847. size_t data_size, void *data)
  848. {
  849. int ret = 0;
  850. int cal_index;
  851. cal_index = get_cal_type_index(cal_type);
  852. if (cal_index < 0) {
  853. pr_err("%s: could not get cal index %d!\n",
  854. __func__, cal_index);
  855. ret = -EINVAL;
  856. goto done;
  857. }
  858. ret = cal_utils_alloc_cal(data_size, data,
  859. q6core_lcl.cal_data[cal_index], 0, NULL);
  860. if (ret < 0) {
  861. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  862. __func__, ret, cal_type);
  863. goto done;
  864. }
  865. done:
  866. return ret;
  867. }
  868. static int q6core_dealloc_cal(int32_t cal_type,
  869. size_t data_size, void *data)
  870. {
  871. int ret = 0;
  872. int cal_index;
  873. cal_index = get_cal_type_index(cal_type);
  874. if (cal_index < 0) {
  875. pr_err("%s: could not get cal index %d!\n",
  876. __func__, cal_index);
  877. ret = -EINVAL;
  878. goto done;
  879. }
  880. ret = cal_utils_dealloc_cal(data_size, data,
  881. q6core_lcl.cal_data[cal_index]);
  882. if (ret < 0) {
  883. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  884. __func__, ret, cal_type);
  885. goto done;
  886. }
  887. done:
  888. return ret;
  889. }
  890. static int q6core_set_cal(int32_t cal_type,
  891. size_t data_size, void *data)
  892. {
  893. int ret = 0;
  894. int cal_index;
  895. cal_index = get_cal_type_index(cal_type);
  896. if (cal_index < 0) {
  897. pr_err("%s: could not get cal index %d!\n",
  898. __func__, cal_index);
  899. ret = -EINVAL;
  900. goto done;
  901. }
  902. ret = cal_utils_set_cal(data_size, data,
  903. q6core_lcl.cal_data[cal_index], 0, NULL);
  904. if (ret < 0) {
  905. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  906. __func__, ret, cal_type);
  907. goto done;
  908. }
  909. if (cal_index == CUST_TOP_CAL)
  910. ret = q6core_send_custom_topologies();
  911. done:
  912. return ret;
  913. }
  914. static void q6core_delete_cal_data(void)
  915. {
  916. pr_debug("%s:\n", __func__);
  917. cal_utils_destroy_cal_types(CORE_MAX_CAL, q6core_lcl.cal_data);
  918. }
  919. static int q6core_init_cal_data(void)
  920. {
  921. int ret = 0;
  922. struct cal_type_info cal_type_info[] = {
  923. {{AUDIO_CORE_METAINFO_CAL_TYPE,
  924. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  925. q6core_set_cal, NULL, NULL} },
  926. {NULL, NULL, cal_utils_match_buf_num} },
  927. {{CORE_CUSTOM_TOPOLOGIES_CAL_TYPE,
  928. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  929. q6core_set_cal, NULL, NULL} },
  930. {NULL, NULL, cal_utils_match_buf_num} }
  931. };
  932. pr_debug("%s:\n", __func__);
  933. ret = cal_utils_create_cal_types(CORE_MAX_CAL,
  934. q6core_lcl.cal_data, cal_type_info);
  935. if (ret < 0) {
  936. pr_err("%s: could not create cal type!\n",
  937. __func__);
  938. goto err;
  939. }
  940. return ret;
  941. err:
  942. q6core_delete_cal_data();
  943. return ret;
  944. }
  945. static int __init core_init(void)
  946. {
  947. memset(&q6core_lcl, 0, sizeof(struct q6core_str));
  948. init_waitqueue_head(&q6core_lcl.bus_bw_req_wait);
  949. init_waitqueue_head(&q6core_lcl.cmd_req_wait);
  950. init_waitqueue_head(&q6core_lcl.avcs_fwk_ver_req_wait);
  951. q6core_lcl.cmd_resp_received_flag = FLAG_NONE;
  952. mutex_init(&q6core_lcl.cmd_lock);
  953. mutex_init(&q6core_lcl.ver_lock);
  954. q6core_init_cal_data();
  955. return 0;
  956. }
  957. module_init(core_init);
  958. static void __exit core_exit(void)
  959. {
  960. mutex_destroy(&q6core_lcl.cmd_lock);
  961. mutex_destroy(&q6core_lcl.ver_lock);
  962. q6core_delete_cal_data();
  963. }
  964. module_exit(core_exit);
  965. MODULE_DESCRIPTION("ADSP core driver");
  966. MODULE_LICENSE("GPL v2");