q6core.c 37 KB

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