q6core.c 41 KB

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