q6core.c 41 KB

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