q6core.c 42 KB

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