q6core.c 32 KB

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