q6core.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/of_device.h>
  9. #include <linux/string.h>
  10. #include <linux/types.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mutex.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/kobject.h>
  17. #include <linux/delay.h>
  18. #include <dsp/q6core.h>
  19. #include <dsp/audio_cal_utils.h>
  20. #include <dsp/apr_audio-v2.h>
  21. #include <soc/snd_event.h>
  22. #include <ipc/apr.h>
  23. #include "adsp_err.h"
  24. #define TIMEOUT_MS 1000
  25. /*
  26. * AVS bring up in the modem is optimized for the new
  27. * Sub System Restart design and 100 milliseconds timeout
  28. * is sufficient to make sure the Q6 will be ready.
  29. */
  30. #define Q6_READY_TIMEOUT_MS 100
  31. #define ADSP_STATE_READY_TIMEOUT_MS 3000
  32. #define APR_ENOTREADY 10
  33. #define MEMPOOL_ID_MASK 0xFF
  34. #define MDF_MAP_TOKEN 0xF000
  35. enum {
  36. META_CAL,
  37. CUST_TOP_CAL,
  38. CORE_MAX_CAL
  39. };
  40. enum ver_query_status {
  41. VER_QUERY_UNATTEMPTED,
  42. VER_QUERY_UNSUPPORTED,
  43. VER_QUERY_SUPPORTED
  44. };
  45. struct q6core_avcs_ver_info {
  46. enum ver_query_status status;
  47. struct avcs_fwk_ver_info *ver_info;
  48. };
  49. struct q6core_str {
  50. struct apr_svc *core_handle_q;
  51. wait_queue_head_t bus_bw_req_wait;
  52. wait_queue_head_t mdf_map_resp_wait;
  53. wait_queue_head_t cmd_req_wait;
  54. wait_queue_head_t avcs_fwk_ver_req_wait;
  55. wait_queue_head_t lpass_npa_rsc_wait;
  56. wait_queue_head_t avcs_module_load_unload_wait;
  57. u32 lpass_npa_rsc_rsp_rcvd;
  58. u32 bus_bw_resp_received;
  59. u32 mdf_map_resp_received;
  60. u32 avcs_module_resp_received;
  61. enum cmd_flags {
  62. FLAG_NONE,
  63. FLAG_CMDRSP_LICENSE_RESULT
  64. } cmd_resp_received_flag;
  65. u32 avcs_fwk_ver_resp_received;
  66. struct mutex cmd_lock;
  67. struct mutex ver_lock;
  68. union {
  69. struct avcs_cmdrsp_get_license_validation_result
  70. cmdrsp_license_result;
  71. } cmd_resp_payload;
  72. u32 param;
  73. struct cal_type_data *cal_data[CORE_MAX_CAL];
  74. uint32_t mem_map_cal_handle;
  75. uint32_t mdf_mem_map_cal_handle;
  76. uint32_t npa_client_handle;
  77. int32_t adsp_status;
  78. int32_t avs_state;
  79. struct q6core_avcs_ver_info q6core_avcs_ver_info;
  80. };
  81. static struct q6core_str q6core_lcl;
  82. /* Global payload used for AVCS_CMD_RSP_MODULES command */
  83. static struct avcs_load_unload_modules_payload *rsp_payload;
  84. struct generic_get_data_ {
  85. int valid;
  86. int size_in_ints;
  87. int ints[];
  88. };
  89. static struct generic_get_data_ *generic_get_data;
  90. static DEFINE_MUTEX(kset_lock);
  91. static struct kset *audio_uevent_kset;
  92. static int q6core_init_uevent_kset(void)
  93. {
  94. int ret = 0;
  95. mutex_lock(&kset_lock);
  96. if (audio_uevent_kset)
  97. goto done;
  98. /* Create a kset under /sys/kernel/ */
  99. audio_uevent_kset = kset_create_and_add("q6audio", NULL, kernel_kobj);
  100. if (!audio_uevent_kset) {
  101. pr_err("%s: error creating uevent kernel set", __func__);
  102. ret = -EINVAL;
  103. }
  104. done:
  105. mutex_unlock(&kset_lock);
  106. return ret;
  107. }
  108. static void q6core_destroy_uevent_kset(void)
  109. {
  110. if (audio_uevent_kset) {
  111. kset_unregister(audio_uevent_kset);
  112. audio_uevent_kset = NULL;
  113. }
  114. }
  115. /**
  116. * q6core_init_uevent_data - initialize kernel object required to send uevents.
  117. *
  118. * @uevent_data: uevent data (dynamically allocated memory).
  119. * @name: name of the kernel object.
  120. *
  121. * Returns 0 on success or error otherwise.
  122. */
  123. int q6core_init_uevent_data(struct audio_uevent_data *uevent_data, char *name)
  124. {
  125. int ret = -EINVAL;
  126. if (!uevent_data || !name)
  127. return ret;
  128. ret = q6core_init_uevent_kset();
  129. if (ret)
  130. return ret;
  131. /* Set kset for kobject before initializing the kobject */
  132. uevent_data->kobj.kset = audio_uevent_kset;
  133. /* Initialize kobject and add it to kernel */
  134. ret = kobject_init_and_add(&uevent_data->kobj, &uevent_data->ktype,
  135. NULL, "%s", name);
  136. if (ret) {
  137. pr_err("%s: error initializing uevent kernel object: %d",
  138. __func__, ret);
  139. kobject_put(&uevent_data->kobj);
  140. return ret;
  141. }
  142. /* Send kobject add event to the system */
  143. kobject_uevent(&uevent_data->kobj, KOBJ_ADD);
  144. return ret;
  145. }
  146. EXPORT_SYMBOL(q6core_init_uevent_data);
  147. /**
  148. * q6core_destroy_uevent_data - destroy kernel object.
  149. *
  150. * @uevent_data: uevent data.
  151. */
  152. void q6core_destroy_uevent_data(struct audio_uevent_data *uevent_data)
  153. {
  154. if (uevent_data)
  155. kobject_put(&uevent_data->kobj);
  156. }
  157. EXPORT_SYMBOL(q6core_destroy_uevent_data);
  158. /**
  159. * q6core_send_uevent - send uevent to userspace.
  160. *
  161. * @uevent_data: uevent data.
  162. * @event: event to send.
  163. *
  164. * Returns 0 on success or error otherwise.
  165. */
  166. int q6core_send_uevent(struct audio_uevent_data *uevent_data, char *event)
  167. {
  168. char *env[] = { event, NULL };
  169. if (!event || !uevent_data)
  170. return -EINVAL;
  171. return kobject_uevent_env(&uevent_data->kobj, KOBJ_CHANGE, env);
  172. }
  173. EXPORT_SYMBOL(q6core_send_uevent);
  174. static int parse_fwk_version_info(uint32_t *payload, uint16_t payload_size)
  175. {
  176. size_t ver_size;
  177. int num_services;
  178. pr_debug("%s: Payload info num services %d\n",
  179. __func__, payload[4]);
  180. /*
  181. * payload1[4] is the number of services running on DSP
  182. * Based on this info, we copy the payload into core
  183. * avcs version info structure.
  184. */
  185. if (payload_size < 5 * sizeof(uint32_t)) {
  186. pr_err("%s: payload has invalid size %d\n",
  187. __func__, payload_size);
  188. return -EINVAL;
  189. }
  190. num_services = payload[4];
  191. if (num_services > VSS_MAX_AVCS_NUM_SERVICES) {
  192. pr_err("%s: num_services: %d greater than max services: %d\n",
  193. __func__, num_services, VSS_MAX_AVCS_NUM_SERVICES);
  194. return -EINVAL;
  195. }
  196. /*
  197. * Dynamically allocate memory for all
  198. * the services based on num_services
  199. */
  200. ver_size = sizeof(struct avcs_get_fwk_version) +
  201. num_services * sizeof(struct avs_svc_api_info);
  202. if (payload_size < ver_size) {
  203. pr_err("%s: payload has invalid size %d, expected size %zu\n",
  204. __func__, payload_size, ver_size);
  205. return -EINVAL;
  206. }
  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. if (data->payload_size < 2 * sizeof(uint32_t)) {
  235. pr_err("%s: payload has invalid size %d\n",
  236. __func__, data->payload_size);
  237. return -EINVAL;
  238. }
  239. switch (payload1[0]) {
  240. case AVCS_CMD_SHARED_MEM_UNMAP_REGIONS:
  241. pr_debug("%s: Cmd = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS status[0x%x]\n",
  242. __func__, payload1[1]);
  243. /* -ADSP status to match Linux error standard */
  244. q6core_lcl.adsp_status = -payload1[1];
  245. q6core_lcl.bus_bw_resp_received = 1;
  246. wake_up(&q6core_lcl.bus_bw_req_wait);
  247. break;
  248. case AVCS_CMD_SHARED_MEM_MAP_REGIONS:
  249. pr_debug("%s: Cmd = AVCS_CMD_SHARED_MEM_MAP_REGIONS status[0x%x]\n",
  250. __func__, payload1[1]);
  251. /* -ADSP status to match Linux error standard */
  252. q6core_lcl.adsp_status = -payload1[1];
  253. q6core_lcl.bus_bw_resp_received = 1;
  254. wake_up(&q6core_lcl.bus_bw_req_wait);
  255. break;
  256. case AVCS_CMD_MAP_MDF_SHARED_MEMORY:
  257. pr_debug("%s: Cmd = AVCS_CMD_MAP_MDF_SHARED_MEMORY status[0x%x]\n",
  258. __func__, payload1[1]);
  259. /* -ADSP status to match Linux error standard */
  260. q6core_lcl.adsp_status = -payload1[1];
  261. q6core_lcl.bus_bw_resp_received = 1;
  262. wake_up(&q6core_lcl.bus_bw_req_wait);
  263. break;
  264. case AVCS_CMD_REGISTER_TOPOLOGIES:
  265. pr_debug("%s: Cmd = AVCS_CMD_REGISTER_TOPOLOGIES status[0x%x]\n",
  266. __func__, payload1[1]);
  267. /* -ADSP status to match Linux error standard */
  268. q6core_lcl.adsp_status = -payload1[1];
  269. q6core_lcl.bus_bw_resp_received = 1;
  270. wake_up(&q6core_lcl.bus_bw_req_wait);
  271. break;
  272. case AVCS_CMD_DEREGISTER_TOPOLOGIES:
  273. pr_debug("%s: Cmd = AVCS_CMD_DEREGISTER_TOPOLOGIES status[0x%x]\n",
  274. __func__, payload1[1]);
  275. q6core_lcl.bus_bw_resp_received = 1;
  276. wake_up(&q6core_lcl.bus_bw_req_wait);
  277. break;
  278. case AVCS_CMD_GET_FWK_VERSION:
  279. pr_debug("%s: Cmd = AVCS_CMD_GET_FWK_VERSION status[%s]\n",
  280. __func__, adsp_err_get_err_str(payload1[1]));
  281. /* ADSP status to match Linux error standard */
  282. q6core_lcl.adsp_status = -payload1[1];
  283. if (payload1[1] == ADSP_EUNSUPPORTED)
  284. q6core_lcl.q6core_avcs_ver_info.status =
  285. VER_QUERY_UNSUPPORTED;
  286. q6core_lcl.avcs_fwk_ver_resp_received = 1;
  287. wake_up(&q6core_lcl.avcs_fwk_ver_req_wait);
  288. break;
  289. case AVCS_CMD_LOAD_TOPO_MODULES:
  290. case AVCS_CMD_UNLOAD_TOPO_MODULES:
  291. pr_debug("%s: Cmd = %s status[%s]\n",
  292. __func__,
  293. (payload1[0] == AVCS_CMD_LOAD_TOPO_MODULES) ?
  294. "AVCS_CMD_LOAD_TOPO_MODULES" :
  295. "AVCS_CMD_UNLOAD_TOPO_MODULES",
  296. adsp_err_get_err_str(payload1[1]));
  297. break;
  298. case AVCS_CMD_DESTROY_LPASS_NPA_CLIENT:
  299. case AVCS_CMD_REQUEST_LPASS_NPA_RESOURCES:
  300. pr_debug("%s: Cmd = AVCS_CMD_CREATE_LPASS_NPA_CLIENT/AVCS_CMD_DESTROY_LPASS_NPA_CLIENT status[%s]\n",
  301. __func__, adsp_err_get_err_str(payload1[1]));
  302. /* ADSP status to match Linux error standard */
  303. q6core_lcl.adsp_status = -payload1[1];
  304. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 1;
  305. wake_up(&q6core_lcl.lpass_npa_rsc_wait);
  306. break;
  307. case AVCS_CMD_LOAD_MODULES:
  308. pr_err("%s: Cmd = %s failed status[%s]\n",
  309. __func__, "AVCS_CMD_LOAD__MODULES",
  310. adsp_err_get_err_str(payload1[1]));
  311. q6core_lcl.avcs_module_resp_received = 1;
  312. q6core_lcl.adsp_status = -payload1[1];
  313. wake_up(&q6core_lcl.avcs_module_load_unload_wait);
  314. break;
  315. case AVCS_CMD_UNLOAD_MODULES:
  316. if (payload1[1] == ADSP_EOK) {
  317. pr_debug("%s: Cmd = %s success status[%s]\n",
  318. __func__, "AVCS_CMD_UNLOAD_MODULES",
  319. "ADSP_EOK");
  320. } else {
  321. pr_err("%s: Cmd = %s failed status[%s]\n",
  322. __func__, "AVCS_CMD_UNLOAD_MODULES",
  323. adsp_err_get_err_str(payload1[1]));
  324. q6core_lcl.adsp_status = -payload1[1];
  325. }
  326. q6core_lcl.avcs_module_resp_received = 1;
  327. wake_up(&q6core_lcl.avcs_module_load_unload_wait);
  328. break;
  329. default:
  330. pr_err("%s: Invalid cmd rsp[0x%x][0x%x] opcode %d\n",
  331. __func__,
  332. payload1[0], payload1[1], data->opcode);
  333. break;
  334. }
  335. break;
  336. }
  337. case RESET_EVENTS:{
  338. pr_debug("%s: Reset event received in Core service\n",
  339. __func__);
  340. /*
  341. * no reset for q6core_avcs_ver_info done as
  342. * the data will not change after SSR
  343. */
  344. apr_reset(q6core_lcl.core_handle_q);
  345. q6core_lcl.core_handle_q = NULL;
  346. break;
  347. }
  348. case AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS:
  349. if (data->payload_size < sizeof(uint32_t)) {
  350. pr_err("%s: payload has invalid size %d\n",
  351. __func__, data->payload_size);
  352. return -EINVAL;
  353. }
  354. payload1 = data->payload;
  355. pr_debug("%s: AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS handle %d\n",
  356. __func__, payload1[0]);
  357. if (data->token == MDF_MAP_TOKEN) {
  358. q6core_lcl.mdf_mem_map_cal_handle = payload1[0];
  359. q6core_lcl.mdf_map_resp_received = 1;
  360. wake_up(&q6core_lcl.mdf_map_resp_wait);
  361. } else {
  362. q6core_lcl.mem_map_cal_handle = payload1[0];
  363. q6core_lcl.bus_bw_resp_received = 1;
  364. wake_up(&q6core_lcl.bus_bw_req_wait);
  365. }
  366. break;
  367. case AVCS_CMDRSP_CREATE_LPASS_NPA_CLIENT:
  368. if (data->payload_size < 2 * sizeof(uint32_t)) {
  369. pr_err("%s: payload has invalid size %d\n",
  370. __func__, data->payload_size);
  371. return -EINVAL;
  372. }
  373. payload1 = data->payload;
  374. pr_debug("%s: AVCS_CMDRSP_CREATE_LPASS_NPA_CLIENT handle %d\n",
  375. __func__, payload1[1]);
  376. q6core_lcl.adsp_status = payload1[0];
  377. q6core_lcl.npa_client_handle = payload1[1];
  378. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 1;
  379. wake_up(&q6core_lcl.lpass_npa_rsc_wait);
  380. break;
  381. case AVCS_CMDRSP_ADSP_EVENT_GET_STATE:
  382. if (data->payload_size < sizeof(uint32_t)) {
  383. pr_err("%s: payload has invalid size %d\n",
  384. __func__, data->payload_size);
  385. return -EINVAL;
  386. }
  387. payload1 = data->payload;
  388. q6core_lcl.param = payload1[0];
  389. pr_debug("%s: Received ADSP get state response 0x%x\n",
  390. __func__, q6core_lcl.param);
  391. /* ensure .param is updated prior to .bus_bw_resp_received */
  392. wmb();
  393. q6core_lcl.bus_bw_resp_received = 1;
  394. wake_up(&q6core_lcl.bus_bw_req_wait);
  395. break;
  396. case AVCS_CMDRSP_GET_LICENSE_VALIDATION_RESULT:
  397. if (data->payload_size < sizeof(uint32_t)) {
  398. pr_err("%s: payload has invalid size %d\n",
  399. __func__, data->payload_size);
  400. return -EINVAL;
  401. }
  402. payload1 = data->payload;
  403. pr_debug("%s: cmd = LICENSE_VALIDATION_RESULT, result = 0x%x\n",
  404. __func__, payload1[0]);
  405. q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result
  406. = payload1[0];
  407. q6core_lcl.cmd_resp_received_flag = FLAG_CMDRSP_LICENSE_RESULT;
  408. wake_up(&q6core_lcl.cmd_req_wait);
  409. break;
  410. case AVCS_CMDRSP_GET_FWK_VERSION:
  411. pr_debug("%s: Received AVCS_CMDRSP_GET_FWK_VERSION\n",
  412. __func__);
  413. payload1 = data->payload;
  414. ret = parse_fwk_version_info(payload1, data->payload_size);
  415. if (ret < 0) {
  416. q6core_lcl.adsp_status = ret;
  417. pr_err("%s: Failed to parse payload:%d\n",
  418. __func__, ret);
  419. } else {
  420. q6core_lcl.q6core_avcs_ver_info.status =
  421. VER_QUERY_SUPPORTED;
  422. }
  423. q6core_lcl.avcs_fwk_ver_resp_received = 1;
  424. wake_up(&q6core_lcl.avcs_fwk_ver_req_wait);
  425. break;
  426. case AVCS_CMD_RSP_LOAD_MODULES:
  427. pr_debug("%s: Received AVCS_CMD_RSP_LOAD_MODULES\n",
  428. __func__);
  429. memcpy(rsp_payload, data->payload, data->payload_size);
  430. q6core_lcl.avcs_module_resp_received = 1;
  431. wake_up(&q6core_lcl.avcs_module_load_unload_wait);
  432. break;
  433. default:
  434. pr_err("%s: Message id from adsp core svc: 0x%x\n",
  435. __func__, data->opcode);
  436. if (generic_get_data) {
  437. generic_get_data->valid = 1;
  438. generic_get_data->size_in_ints =
  439. data->payload_size/sizeof(int);
  440. pr_debug("callback size = %i\n",
  441. data->payload_size);
  442. memcpy(generic_get_data->ints, data->payload,
  443. data->payload_size);
  444. q6core_lcl.bus_bw_resp_received = 1;
  445. wake_up(&q6core_lcl.bus_bw_req_wait);
  446. break;
  447. }
  448. break;
  449. }
  450. return 0;
  451. }
  452. void ocm_core_open(void)
  453. {
  454. if (q6core_lcl.core_handle_q == NULL)
  455. q6core_lcl.core_handle_q = apr_register("ADSP", "CORE",
  456. aprv2_core_fn_q, 0xFFFFFFFF, NULL);
  457. pr_debug("%s: Open_q %pK\n", __func__, q6core_lcl.core_handle_q);
  458. if (q6core_lcl.core_handle_q == NULL)
  459. pr_err_ratelimited("%s: Unable to register CORE\n", __func__);
  460. }
  461. struct cal_block_data *cal_utils_get_cal_block_by_key(
  462. struct cal_type_data *cal_type, uint32_t key)
  463. {
  464. struct list_head *ptr, *next;
  465. struct cal_block_data *cal_block = NULL;
  466. struct audio_cal_info_metainfo *metainfo;
  467. list_for_each_safe(ptr, next,
  468. &cal_type->cal_blocks) {
  469. cal_block = list_entry(ptr,
  470. struct cal_block_data, list);
  471. metainfo = (struct audio_cal_info_metainfo *)
  472. cal_block->cal_info;
  473. if (metainfo->nKey != key) {
  474. pr_debug("%s: metainfo key mismatch!!! found:%x, needed:%x\n",
  475. __func__, metainfo->nKey, key);
  476. } else {
  477. pr_debug("%s: metainfo key match found", __func__);
  478. return cal_block;
  479. }
  480. }
  481. return NULL;
  482. }
  483. static int q6core_send_get_avcs_fwk_ver_cmd(void)
  484. {
  485. struct apr_hdr avcs_ver_cmd;
  486. int ret;
  487. mutex_lock(&q6core_lcl.cmd_lock);
  488. avcs_ver_cmd.hdr_field =
  489. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  490. APR_PKT_VER);
  491. avcs_ver_cmd.pkt_size = sizeof(struct apr_hdr);
  492. avcs_ver_cmd.src_port = 0;
  493. avcs_ver_cmd.dest_port = 0;
  494. avcs_ver_cmd.token = 0;
  495. avcs_ver_cmd.opcode = AVCS_CMD_GET_FWK_VERSION;
  496. q6core_lcl.adsp_status = 0;
  497. q6core_lcl.avcs_fwk_ver_resp_received = 0;
  498. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  499. (uint32_t *) &avcs_ver_cmd);
  500. if (ret < 0) {
  501. pr_err("%s: failed to send apr packet, ret=%d\n", __func__,
  502. ret);
  503. goto done;
  504. }
  505. ret = wait_event_timeout(q6core_lcl.avcs_fwk_ver_req_wait,
  506. (q6core_lcl.avcs_fwk_ver_resp_received == 1),
  507. msecs_to_jiffies(TIMEOUT_MS));
  508. if (!ret) {
  509. pr_err("%s: wait_event timeout for AVCS fwk version info\n",
  510. __func__);
  511. ret = -ETIMEDOUT;
  512. goto done;
  513. }
  514. if (q6core_lcl.adsp_status < 0) {
  515. /*
  516. * adsp_err_get_err_str expects a positive value but we store
  517. * the DSP error as negative to match the Linux error standard.
  518. * Pass in the negated value so adsp_err_get_err_str returns
  519. * the correct string.
  520. */
  521. pr_err("%s: DSP returned error[%s]\n", __func__,
  522. adsp_err_get_err_str(-q6core_lcl.adsp_status));
  523. ret = adsp_err_get_lnx_err_code(q6core_lcl.adsp_status);
  524. goto done;
  525. }
  526. ret = 0;
  527. done:
  528. mutex_unlock(&q6core_lcl.cmd_lock);
  529. return ret;
  530. }
  531. int q6core_get_service_version(uint32_t service_id,
  532. struct avcs_fwk_ver_info *ver_info,
  533. size_t size)
  534. {
  535. struct avcs_fwk_ver_info *cached_ver_info = NULL;
  536. int i;
  537. uint32_t num_services;
  538. size_t ver_size;
  539. int ret;
  540. if (ver_info == NULL) {
  541. pr_err("%s: ver_info is NULL\n", __func__);
  542. return -EINVAL;
  543. }
  544. ret = q6core_get_fwk_version_size(service_id);
  545. if (ret < 0) {
  546. pr_err("%s: Failed to get service size for service id %d with error %d\n",
  547. __func__, service_id, ret);
  548. return ret;
  549. }
  550. ver_size = ret;
  551. if (ver_size != size) {
  552. pr_err("%s: Expected size %zu and provided size %zu do not match\n",
  553. __func__, ver_size, size);
  554. return -EINVAL;
  555. }
  556. cached_ver_info = q6core_lcl.q6core_avcs_ver_info.ver_info;
  557. num_services = cached_ver_info->avcs_fwk_version.num_services;
  558. if (service_id == AVCS_SERVICE_ID_ALL) {
  559. memcpy(ver_info, cached_ver_info, ver_size);
  560. return 0;
  561. }
  562. ver_info->avcs_fwk_version = cached_ver_info->avcs_fwk_version;
  563. for (i = 0; i < num_services; i++) {
  564. if (cached_ver_info->services[i].service_id == service_id) {
  565. ver_info->services[0] = cached_ver_info->services[i];
  566. return 0;
  567. }
  568. }
  569. pr_err("%s: No service matching service ID %d\n", __func__, service_id);
  570. return -EINVAL;
  571. }
  572. EXPORT_SYMBOL(q6core_get_service_version);
  573. static int q6core_get_avcs_fwk_version(void)
  574. {
  575. int ret = 0;
  576. mutex_lock(&(q6core_lcl.ver_lock));
  577. pr_debug("%s: q6core_avcs_ver_info.status(%d)\n", __func__,
  578. q6core_lcl.q6core_avcs_ver_info.status);
  579. switch (q6core_lcl.q6core_avcs_ver_info.status) {
  580. case VER_QUERY_SUPPORTED:
  581. pr_debug("%s: AVCS FWK version query already attempted\n",
  582. __func__);
  583. break;
  584. case VER_QUERY_UNSUPPORTED:
  585. ret = -EOPNOTSUPP;
  586. break;
  587. case VER_QUERY_UNATTEMPTED:
  588. pr_debug("%s: Attempting AVCS FWK version query\n", __func__);
  589. if (q6core_is_adsp_ready()) {
  590. ret = q6core_send_get_avcs_fwk_ver_cmd();
  591. } else {
  592. pr_err("%s: ADSP is not ready to query version\n",
  593. __func__);
  594. ret = -ENODEV;
  595. }
  596. break;
  597. default:
  598. pr_err("%s: Invalid version query status %d\n", __func__,
  599. q6core_lcl.q6core_avcs_ver_info.status);
  600. ret = -EINVAL;
  601. break;
  602. }
  603. mutex_unlock(&(q6core_lcl.ver_lock));
  604. return ret;
  605. }
  606. size_t q6core_get_fwk_version_size(uint32_t service_id)
  607. {
  608. int ret = 0;
  609. uint32_t num_services;
  610. ret = q6core_get_avcs_fwk_version();
  611. if (ret)
  612. goto done;
  613. if (q6core_lcl.q6core_avcs_ver_info.ver_info != NULL) {
  614. num_services = q6core_lcl.q6core_avcs_ver_info.ver_info
  615. ->avcs_fwk_version.num_services;
  616. } else {
  617. pr_err("%s: ver_info is NULL\n", __func__);
  618. ret = -EINVAL;
  619. goto done;
  620. }
  621. ret = sizeof(struct avcs_get_fwk_version);
  622. if (service_id == AVCS_SERVICE_ID_ALL)
  623. ret += num_services * sizeof(struct avs_svc_api_info);
  624. else
  625. ret += sizeof(struct avs_svc_api_info);
  626. done:
  627. return ret;
  628. }
  629. EXPORT_SYMBOL(q6core_get_fwk_version_size);
  630. /**
  631. * q6core_get_avcs_version_per_service -
  632. * to get api version of a particular service
  633. *
  634. * @service_id: id of the service
  635. *
  636. * Returns valid version on success or error (negative value) on failure
  637. */
  638. int q6core_get_avcs_api_version_per_service(uint32_t service_id)
  639. {
  640. struct avcs_fwk_ver_info *cached_ver_info = NULL;
  641. int i;
  642. uint32_t num_services;
  643. int ret = 0;
  644. if (service_id == AVCS_SERVICE_ID_ALL)
  645. return -EINVAL;
  646. ret = q6core_get_avcs_fwk_version();
  647. if (ret < 0) {
  648. pr_err("%s: failure in getting AVCS version\n", __func__);
  649. return ret;
  650. }
  651. cached_ver_info = q6core_lcl.q6core_avcs_ver_info.ver_info;
  652. num_services = cached_ver_info->avcs_fwk_version.num_services;
  653. for (i = 0; i < num_services; i++) {
  654. if (cached_ver_info->services[i].service_id == service_id)
  655. return cached_ver_info->services[i].api_version;
  656. }
  657. pr_err("%s: No service matching service ID %d\n", __func__, service_id);
  658. return -EINVAL;
  659. }
  660. EXPORT_SYMBOL(q6core_get_avcs_api_version_per_service);
  661. /**
  662. * core_set_license -
  663. * command to set license for module
  664. *
  665. * @key: license key hash
  666. * @module_id: DSP Module ID
  667. *
  668. * Returns 0 on success or error on failure
  669. */
  670. int32_t core_set_license(uint32_t key, uint32_t module_id)
  671. {
  672. struct avcs_cmd_set_license *cmd_setl = NULL;
  673. struct cal_block_data *cal_block = NULL;
  674. int rc = 0, packet_size = 0;
  675. pr_debug("%s: key:0x%x, id:0x%x\n", __func__, key, module_id);
  676. mutex_lock(&(q6core_lcl.cmd_lock));
  677. if (q6core_lcl.cal_data[META_CAL] == NULL) {
  678. pr_err("%s: cal_data not initialized yet!!\n", __func__);
  679. rc = -EINVAL;
  680. goto cmd_unlock;
  681. }
  682. mutex_lock(&((q6core_lcl.cal_data[META_CAL])->lock));
  683. cal_block = cal_utils_get_cal_block_by_key(
  684. q6core_lcl.cal_data[META_CAL], key);
  685. if (cal_block == NULL ||
  686. cal_block->cal_data.kvaddr == NULL ||
  687. cal_block->cal_data.size <= 0) {
  688. pr_err("%s: Invalid cal block to send", __func__);
  689. rc = -EINVAL;
  690. goto cal_data_unlock;
  691. }
  692. packet_size = sizeof(struct avcs_cmd_set_license) +
  693. cal_block->cal_data.size;
  694. /*round up total packet_size to next 4 byte boundary*/
  695. packet_size = ((packet_size + 0x3)>>2)<<2;
  696. cmd_setl = kzalloc(packet_size, GFP_KERNEL);
  697. if (cmd_setl == NULL) {
  698. rc = -ENOMEM;
  699. goto cal_data_unlock;
  700. }
  701. ocm_core_open();
  702. if (q6core_lcl.core_handle_q == NULL) {
  703. pr_err("%s: apr registration for CORE failed\n", __func__);
  704. rc = -ENODEV;
  705. goto fail_cmd;
  706. }
  707. cmd_setl->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  708. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  709. cmd_setl->hdr.pkt_size = packet_size;
  710. cmd_setl->hdr.src_port = 0;
  711. cmd_setl->hdr.dest_port = 0;
  712. cmd_setl->hdr.token = 0;
  713. cmd_setl->hdr.opcode = AVCS_CMD_SET_LICENSE;
  714. cmd_setl->id = module_id;
  715. cmd_setl->overwrite = 1;
  716. cmd_setl->size = cal_block->cal_data.size;
  717. memcpy((uint8_t *)cmd_setl + sizeof(struct avcs_cmd_set_license),
  718. cal_block->cal_data.kvaddr,
  719. cal_block->cal_data.size);
  720. pr_info("%s: Set license opcode=0x%x, id =0x%x, size = %d\n",
  721. __func__, cmd_setl->hdr.opcode,
  722. cmd_setl->id, cmd_setl->size);
  723. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)cmd_setl);
  724. if (rc < 0)
  725. pr_err("%s: SET_LICENSE failed op[0x%x]rc[%d]\n",
  726. __func__, cmd_setl->hdr.opcode, rc);
  727. fail_cmd:
  728. kfree(cmd_setl);
  729. cal_data_unlock:
  730. mutex_unlock(&((q6core_lcl.cal_data[META_CAL])->lock));
  731. cmd_unlock:
  732. mutex_unlock(&(q6core_lcl.cmd_lock));
  733. return rc;
  734. }
  735. EXPORT_SYMBOL(core_set_license);
  736. /**
  737. * core_get_license_status -
  738. * command to retrieve license status for module
  739. *
  740. * @module_id: DSP Module ID
  741. *
  742. * Returns 0 on success or error on failure
  743. */
  744. int32_t core_get_license_status(uint32_t module_id)
  745. {
  746. struct avcs_cmd_get_license_validation_result get_lvr_cmd;
  747. int ret = 0;
  748. pr_debug("%s: module_id 0x%x", __func__, module_id);
  749. mutex_lock(&(q6core_lcl.cmd_lock));
  750. ocm_core_open();
  751. if (q6core_lcl.core_handle_q == NULL) {
  752. pr_err("%s: apr registration for CORE failed\n", __func__);
  753. ret = -ENODEV;
  754. goto fail_cmd;
  755. }
  756. get_lvr_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  757. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  758. get_lvr_cmd.hdr.pkt_size =
  759. sizeof(struct avcs_cmd_get_license_validation_result);
  760. get_lvr_cmd.hdr.src_port = 0;
  761. get_lvr_cmd.hdr.dest_port = 0;
  762. get_lvr_cmd.hdr.token = 0;
  763. get_lvr_cmd.hdr.opcode = AVCS_CMD_GET_LICENSE_VALIDATION_RESULT;
  764. get_lvr_cmd.id = module_id;
  765. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &get_lvr_cmd);
  766. if (ret < 0) {
  767. pr_err("%s: license_validation request failed, err %d\n",
  768. __func__, ret);
  769. ret = -EREMOTE;
  770. goto fail_cmd;
  771. }
  772. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  773. mutex_unlock(&(q6core_lcl.cmd_lock));
  774. ret = wait_event_timeout(q6core_lcl.cmd_req_wait,
  775. (q6core_lcl.cmd_resp_received_flag ==
  776. FLAG_CMDRSP_LICENSE_RESULT),
  777. msecs_to_jiffies(TIMEOUT_MS));
  778. mutex_lock(&(q6core_lcl.cmd_lock));
  779. if (!ret) {
  780. pr_err("%s: wait_event timeout for CMDRSP_LICENSE_RESULT\n",
  781. __func__);
  782. ret = -ETIME;
  783. goto fail_cmd;
  784. }
  785. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  786. ret = q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result;
  787. fail_cmd:
  788. mutex_unlock(&(q6core_lcl.cmd_lock));
  789. pr_info("%s: cmdrsp_license_result.result = 0x%x for module 0x%x\n",
  790. __func__, ret, module_id);
  791. return ret;
  792. }
  793. EXPORT_SYMBOL(core_get_license_status);
  794. /**
  795. * core_set_dolby_manufacturer_id -
  796. * command to set dolby manufacturer id
  797. *
  798. * @manufacturer_id: Dolby manufacturer id
  799. *
  800. * Returns 0 on success or error on failure
  801. */
  802. uint32_t core_set_dolby_manufacturer_id(int manufacturer_id)
  803. {
  804. struct adsp_dolby_manufacturer_id payload;
  805. int rc = 0;
  806. pr_debug("%s: manufacturer_id :%d\n", __func__, manufacturer_id);
  807. mutex_lock(&(q6core_lcl.cmd_lock));
  808. ocm_core_open();
  809. if (q6core_lcl.core_handle_q) {
  810. payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  811. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  812. payload.hdr.pkt_size =
  813. sizeof(struct adsp_dolby_manufacturer_id);
  814. payload.hdr.src_port = 0;
  815. payload.hdr.dest_port = 0;
  816. payload.hdr.token = 0;
  817. payload.hdr.opcode = ADSP_CMD_SET_DOLBY_MANUFACTURER_ID;
  818. payload.manufacturer_id = manufacturer_id;
  819. pr_debug("%s: Send Dolby security opcode=0x%x manufacturer ID = %d\n",
  820. __func__,
  821. payload.hdr.opcode, payload.manufacturer_id);
  822. rc = apr_send_pkt(q6core_lcl.core_handle_q,
  823. (uint32_t *)&payload);
  824. if (rc < 0)
  825. pr_err("%s: SET_DOLBY_MANUFACTURER_ID failed op[0x%x]rc[%d]\n",
  826. __func__, payload.hdr.opcode, rc);
  827. }
  828. mutex_unlock(&(q6core_lcl.cmd_lock));
  829. return rc;
  830. }
  831. EXPORT_SYMBOL(core_set_dolby_manufacturer_id);
  832. int32_t q6core_avcs_load_unload_modules(struct avcs_load_unload_modules_payload
  833. *payload, uint32_t preload_type)
  834. {
  835. int ret = 0;
  836. size_t packet_size = 0, payload_size = 0;
  837. struct avcs_cmd_dynamic_modules *mod = NULL;
  838. int num_modules;
  839. if (payload == NULL) {
  840. pr_err("%s: payload is null\n", __func__);
  841. return -EINVAL;
  842. }
  843. mutex_lock(&(q6core_lcl.cmd_lock));
  844. num_modules = payload->num_modules;
  845. ocm_core_open();
  846. if (q6core_lcl.core_handle_q == NULL) {
  847. pr_err("%s: apr registration for CORE failed\n", __func__);
  848. mutex_unlock(&(q6core_lcl.cmd_lock));
  849. return -ENODEV;
  850. }
  851. payload_size = (sizeof(struct avcs_load_unload_modules_sec_payload)
  852. * num_modules) + sizeof(uint32_t);
  853. packet_size = sizeof(struct avcs_cmd_dynamic_modules) +
  854. payload_size - sizeof(uint32_t);
  855. mod = kzalloc(packet_size, GFP_KERNEL);
  856. if (!mod) {
  857. mutex_unlock(&(q6core_lcl.cmd_lock));
  858. return -ENOMEM;
  859. }
  860. rsp_payload = kzalloc(payload_size, GFP_KERNEL);
  861. if (!rsp_payload) {
  862. kfree(mod);
  863. mutex_unlock(&(q6core_lcl.cmd_lock));
  864. return -ENOMEM;
  865. }
  866. memcpy((uint8_t *)mod + sizeof(struct apr_hdr) +
  867. sizeof(struct avcs_load_unload_modules_meminfo),
  868. payload, payload_size);
  869. mod->hdr.hdr_field =
  870. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  871. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  872. mod->hdr.pkt_size = packet_size;
  873. mod->hdr.src_port = 0;
  874. mod->hdr.dest_port = 0;
  875. mod->hdr.token = 0;
  876. mod->meminfo.data_payload_addr_lsw = 0;
  877. mod->meminfo.data_payload_addr_msw = 0;
  878. mod->meminfo.mem_map_handle = 0;
  879. mod->meminfo.buffer_size = payload_size;
  880. if (preload_type == AVCS_LOAD_MODULES)
  881. mod->hdr.opcode = AVCS_CMD_LOAD_MODULES;
  882. else
  883. mod->hdr.opcode = AVCS_CMD_UNLOAD_MODULES;
  884. q6core_lcl.adsp_status = 0;
  885. q6core_lcl.avcs_module_resp_received = 0;
  886. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  887. (uint32_t *)mod);
  888. if (ret < 0) {
  889. pr_err("%s: modules load/unload failed ret = %d\n",
  890. __func__, ret);
  891. goto done;
  892. }
  893. ret = wait_event_timeout(q6core_lcl.avcs_module_load_unload_wait,
  894. (q6core_lcl.avcs_module_resp_received == 1),
  895. msecs_to_jiffies(TIMEOUT_MS));
  896. if (!ret) {
  897. pr_err("%s wait event timeout for avcs load/unload module\n",
  898. __func__);
  899. ret = -ETIMEDOUT;
  900. goto done;
  901. }
  902. if (q6core_lcl.adsp_status < 0) {
  903. pr_err("%s: modules load/unload failed %d\n", __func__,
  904. q6core_lcl.adsp_status);
  905. ret = q6core_lcl.adsp_status;
  906. goto done;
  907. } else {
  908. if (mod->hdr.opcode == AVCS_CMD_LOAD_MODULES)
  909. memcpy(payload, rsp_payload, payload_size);
  910. }
  911. done:
  912. kfree(mod);
  913. kfree(rsp_payload);
  914. mutex_unlock(&(q6core_lcl.cmd_lock));
  915. return ret;
  916. }
  917. EXPORT_SYMBOL(q6core_avcs_load_unload_modules);
  918. int32_t q6core_load_unload_topo_modules(uint32_t topo_id,
  919. bool preload_type)
  920. {
  921. struct avcs_cmd_load_unload_topo_modules load_unload_topo_modules;
  922. int ret = 0;
  923. mutex_lock(&(q6core_lcl.cmd_lock));
  924. ocm_core_open();
  925. if (q6core_lcl.core_handle_q == NULL) {
  926. pr_err("%s: apr registration for CORE failed\n", __func__);
  927. ret = -ENODEV;
  928. goto done;
  929. }
  930. memset(&load_unload_topo_modules, 0, sizeof(load_unload_topo_modules));
  931. load_unload_topo_modules.hdr.hdr_field =
  932. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  933. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  934. load_unload_topo_modules.hdr.pkt_size =
  935. sizeof(struct avcs_cmd_load_unload_topo_modules);
  936. load_unload_topo_modules.hdr.src_port = 0;
  937. load_unload_topo_modules.hdr.dest_port = 0;
  938. load_unload_topo_modules.hdr.token = 0;
  939. if (preload_type == CORE_LOAD_TOPOLOGY)
  940. load_unload_topo_modules.hdr.opcode =
  941. AVCS_CMD_LOAD_TOPO_MODULES;
  942. else
  943. load_unload_topo_modules.hdr.opcode =
  944. AVCS_CMD_UNLOAD_TOPO_MODULES;
  945. load_unload_topo_modules.topology_id = topo_id;
  946. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  947. (uint32_t *) &load_unload_topo_modules);
  948. if (ret < 0) {
  949. pr_err("%s: Load/unload topo modules failed for topology = %d ret = %d\n",
  950. __func__, topo_id, ret);
  951. ret = -EINVAL;
  952. }
  953. done:
  954. mutex_unlock(&(q6core_lcl.cmd_lock));
  955. return ret;
  956. }
  957. EXPORT_SYMBOL(q6core_load_unload_topo_modules);
  958. /**
  959. * q6core_is_adsp_ready - check adsp ready status
  960. *
  961. * Returns true if adsp is ready otherwise returns false
  962. */
  963. bool q6core_is_adsp_ready(void)
  964. {
  965. int rc = 0;
  966. bool ret = false;
  967. struct apr_hdr hdr;
  968. pr_debug("%s: enter\n", __func__);
  969. memset(&hdr, 0, sizeof(hdr));
  970. hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  971. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  972. hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE, 0);
  973. hdr.opcode = AVCS_CMD_ADSP_EVENT_GET_STATE;
  974. mutex_lock(&(q6core_lcl.cmd_lock));
  975. ocm_core_open();
  976. if (q6core_lcl.core_handle_q) {
  977. q6core_lcl.bus_bw_resp_received = 0;
  978. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)&hdr);
  979. if (rc < 0) {
  980. pr_err_ratelimited("%s: Get ADSP state APR packet send event %d\n",
  981. __func__, rc);
  982. goto bail;
  983. }
  984. rc = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  985. (q6core_lcl.bus_bw_resp_received == 1),
  986. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  987. if (rc > 0 && q6core_lcl.bus_bw_resp_received) {
  988. /* ensure to read updated param by callback thread */
  989. rmb();
  990. ret = !!q6core_lcl.param;
  991. }
  992. }
  993. bail:
  994. pr_debug("%s: leave, rc %d, adsp ready %d\n", __func__, rc, ret);
  995. mutex_unlock(&(q6core_lcl.cmd_lock));
  996. return ret;
  997. }
  998. EXPORT_SYMBOL(q6core_is_adsp_ready);
  999. int q6core_create_lpass_npa_client(uint32_t node_id, char *client_name,
  1000. uint32_t *client_handle)
  1001. {
  1002. struct avcs_cmd_create_lpass_npa_client_t create_lpass_npa_client;
  1003. struct avcs_cmd_create_lpass_npa_client_t *cmd_ptr =
  1004. &create_lpass_npa_client;
  1005. int ret = 0;
  1006. if (!client_name) {
  1007. pr_err("%s: Invalid params\n", __func__);
  1008. return -EINVAL;
  1009. }
  1010. mutex_lock(&(q6core_lcl.cmd_lock));
  1011. memset(cmd_ptr, 0, sizeof(create_lpass_npa_client));
  1012. cmd_ptr->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1013. APR_HDR_LEN(APR_HDR_SIZE),
  1014. APR_PKT_VER);
  1015. cmd_ptr->hdr.pkt_size = sizeof(create_lpass_npa_client);
  1016. cmd_ptr->hdr.src_port = 0;
  1017. cmd_ptr->hdr.dest_port = 0;
  1018. cmd_ptr->hdr.token = 0;
  1019. cmd_ptr->hdr.opcode = AVCS_CMD_CREATE_LPASS_NPA_CLIENT;
  1020. cmd_ptr->node_id = AVCS_SLEEP_ISLAND_CORE_DRIVER_NODE_ID;
  1021. strlcpy(cmd_ptr->client_name, client_name,
  1022. sizeof(cmd_ptr->client_name));
  1023. pr_debug("%s: create lpass npa client opcode[0x%x] node id[0x%x]\n",
  1024. __func__, cmd_ptr->hdr.opcode, cmd_ptr->node_id);
  1025. *client_handle = 0;
  1026. q6core_lcl.adsp_status = 0;
  1027. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 0;
  1028. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) cmd_ptr);
  1029. if (ret < 0) {
  1030. pr_err("%s: create lpass npa client failed %d\n",
  1031. __func__, ret);
  1032. ret = -EINVAL;
  1033. goto done;
  1034. }
  1035. ret = wait_event_timeout(q6core_lcl.lpass_npa_rsc_wait,
  1036. (q6core_lcl.lpass_npa_rsc_rsp_rcvd == 1),
  1037. msecs_to_jiffies(TIMEOUT_MS));
  1038. if (!ret) {
  1039. pr_err("%s: timeout. waited for create lpass npa rsc client\n",
  1040. __func__);
  1041. ret = -ETIMEDOUT;
  1042. goto done;
  1043. } else {
  1044. /* set ret to 0 as no timeout happened */
  1045. ret = 0;
  1046. }
  1047. if (q6core_lcl.adsp_status < 0) {
  1048. pr_err("%s: DSP returned error %d\n",
  1049. __func__, q6core_lcl.adsp_status);
  1050. ret = q6core_lcl.adsp_status;
  1051. goto done;
  1052. }
  1053. *client_handle = q6core_lcl.npa_client_handle;
  1054. pr_debug("%s: q6core_lcl.npa_client_handle %d\n", __func__,
  1055. q6core_lcl.npa_client_handle);
  1056. done:
  1057. mutex_unlock(&q6core_lcl.cmd_lock);
  1058. return ret;
  1059. }
  1060. EXPORT_SYMBOL(q6core_create_lpass_npa_client);
  1061. int q6core_destroy_lpass_npa_client(uint32_t client_handle)
  1062. {
  1063. struct avcs_cmd_destroy_lpass_npa_client_t destroy_lpass_npa_client;
  1064. struct avcs_cmd_destroy_lpass_npa_client_t *cmd_ptr =
  1065. &destroy_lpass_npa_client;
  1066. int ret = 0;
  1067. mutex_lock(&(q6core_lcl.cmd_lock));
  1068. memset(cmd_ptr, 0, sizeof(destroy_lpass_npa_client));
  1069. cmd_ptr->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1070. APR_HDR_LEN(APR_HDR_SIZE),
  1071. APR_PKT_VER);
  1072. cmd_ptr->hdr.pkt_size = sizeof(destroy_lpass_npa_client);
  1073. cmd_ptr->hdr.src_port = 0;
  1074. cmd_ptr->hdr.dest_port = 0;
  1075. cmd_ptr->hdr.token = 0;
  1076. cmd_ptr->hdr.opcode = AVCS_CMD_DESTROY_LPASS_NPA_CLIENT;
  1077. cmd_ptr->client_handle = client_handle;
  1078. pr_debug("%s: dstry lpass npa client opcode[0x%x] client hdl[0x%x]\n",
  1079. __func__, cmd_ptr->hdr.opcode, cmd_ptr->client_handle);
  1080. q6core_lcl.adsp_status = 0;
  1081. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 0;
  1082. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) cmd_ptr);
  1083. if (ret < 0) {
  1084. pr_err("%s: destroy lpass npa client failed %d\n",
  1085. __func__, ret);
  1086. ret = -EINVAL;
  1087. goto done;
  1088. }
  1089. ret = wait_event_timeout(q6core_lcl.lpass_npa_rsc_wait,
  1090. (q6core_lcl.lpass_npa_rsc_rsp_rcvd == 1),
  1091. msecs_to_jiffies(TIMEOUT_MS));
  1092. if (!ret) {
  1093. pr_err("%s: timeout. waited for destroy lpass npa rsc client\n",
  1094. __func__);
  1095. ret = -ETIMEDOUT;
  1096. goto done;
  1097. } else {
  1098. /* set ret to 0 as no timeout happened */
  1099. ret = 0;
  1100. }
  1101. if (q6core_lcl.adsp_status < 0) {
  1102. pr_err("%s: DSP returned error %d\n",
  1103. __func__, q6core_lcl.adsp_status);
  1104. ret = q6core_lcl.adsp_status;
  1105. }
  1106. done:
  1107. mutex_unlock(&q6core_lcl.cmd_lock);
  1108. return ret;
  1109. }
  1110. EXPORT_SYMBOL(q6core_destroy_lpass_npa_client);
  1111. int q6core_request_island_transition(uint32_t client_handle,
  1112. uint32_t island_allow_mode)
  1113. {
  1114. struct avcs_sleep_node_island_transition_config_t island_tsn_cfg;
  1115. struct avcs_sleep_node_island_transition_config_t *cmd_ptr =
  1116. &island_tsn_cfg;
  1117. int ret = 0;
  1118. mutex_lock(&(q6core_lcl.cmd_lock));
  1119. memset(cmd_ptr, 0, sizeof(island_tsn_cfg));
  1120. cmd_ptr->req_lpass_npa_rsc.hdr.hdr_field =
  1121. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1122. APR_HDR_LEN(APR_HDR_SIZE),
  1123. APR_PKT_VER);
  1124. cmd_ptr->req_lpass_npa_rsc.hdr.pkt_size = sizeof(island_tsn_cfg);
  1125. cmd_ptr->req_lpass_npa_rsc.hdr.src_port = 0;
  1126. cmd_ptr->req_lpass_npa_rsc.hdr.dest_port = 0;
  1127. cmd_ptr->req_lpass_npa_rsc.hdr.token = 0;
  1128. cmd_ptr->req_lpass_npa_rsc.hdr.opcode =
  1129. AVCS_CMD_REQUEST_LPASS_NPA_RESOURCES;
  1130. cmd_ptr->req_lpass_npa_rsc.client_handle = client_handle;
  1131. cmd_ptr->req_lpass_npa_rsc.resource_id =
  1132. AVCS_SLEEP_NODE_ISLAND_TRANSITION_RESOURCE_ID;
  1133. cmd_ptr->island_allow_mode = island_allow_mode;
  1134. pr_debug("%s: req islnd tnsn opcode[0x%x] island_allow_mode[0x%x]\n",
  1135. __func__, cmd_ptr->req_lpass_npa_rsc.hdr.opcode,
  1136. cmd_ptr->island_allow_mode);
  1137. q6core_lcl.adsp_status = 0;
  1138. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 0;
  1139. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) cmd_ptr);
  1140. if (ret < 0) {
  1141. pr_err("%s: island tnsn cmd send failed %d\n",
  1142. __func__, ret);
  1143. ret = -EINVAL;
  1144. goto done;
  1145. }
  1146. ret = wait_event_timeout(q6core_lcl.lpass_npa_rsc_wait,
  1147. (q6core_lcl.lpass_npa_rsc_rsp_rcvd == 1),
  1148. msecs_to_jiffies(TIMEOUT_MS));
  1149. if (!ret) {
  1150. pr_err("%s: timeout. waited for island lpass npa rsc req\n",
  1151. __func__);
  1152. ret = -ETIMEDOUT;
  1153. goto done;
  1154. } else {
  1155. /* set ret to 0 as no timeout happened */
  1156. ret = 0;
  1157. }
  1158. if (q6core_lcl.adsp_status < 0) {
  1159. pr_err("%s: DSP returned error %d\n",
  1160. __func__, q6core_lcl.adsp_status);
  1161. ret = q6core_lcl.adsp_status;
  1162. }
  1163. done:
  1164. mutex_unlock(&q6core_lcl.cmd_lock);
  1165. return ret;
  1166. }
  1167. EXPORT_SYMBOL(q6core_request_island_transition);
  1168. int q6core_map_memory_regions(phys_addr_t *buf_add, uint32_t mempool_id,
  1169. uint32_t *bufsz, uint32_t bufcnt, uint32_t *map_handle)
  1170. {
  1171. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  1172. struct avs_shared_map_region_payload *mregions = NULL;
  1173. void *mmap_region_cmd = NULL;
  1174. void *payload = NULL;
  1175. int ret = 0;
  1176. int i = 0;
  1177. int cmd_size = 0;
  1178. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  1179. + sizeof(struct avs_shared_map_region_payload)
  1180. * bufcnt;
  1181. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1182. if (mmap_region_cmd == NULL)
  1183. return -ENOMEM;
  1184. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  1185. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1186. APR_HDR_LEN(APR_HDR_SIZE),
  1187. APR_PKT_VER);
  1188. mmap_regions->hdr.pkt_size = cmd_size;
  1189. mmap_regions->hdr.src_port = 0;
  1190. mmap_regions->hdr.dest_port = 0;
  1191. mmap_regions->hdr.token = 0;
  1192. mmap_regions->hdr.opcode = AVCS_CMD_SHARED_MEM_MAP_REGIONS;
  1193. mmap_regions->mem_pool_id = mempool_id & 0x00ff;
  1194. mmap_regions->num_regions = bufcnt & 0x00ff;
  1195. mmap_regions->property_flag = 0x00;
  1196. payload = ((u8 *) mmap_region_cmd +
  1197. sizeof(struct avs_cmd_shared_mem_map_regions));
  1198. mregions = (struct avs_shared_map_region_payload *)payload;
  1199. for (i = 0; i < bufcnt; i++) {
  1200. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  1201. mregions->shm_addr_msw =
  1202. msm_audio_populate_upper_32_bits(buf_add[i]);
  1203. mregions->mem_size_bytes = bufsz[i];
  1204. ++mregions;
  1205. }
  1206. pr_debug("%s: sending memory map, addr %pK, size %d, bufcnt = %d\n",
  1207. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  1208. *map_handle = 0;
  1209. q6core_lcl.adsp_status = 0;
  1210. q6core_lcl.bus_bw_resp_received = 0;
  1211. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1212. mmap_regions);
  1213. if (ret < 0) {
  1214. pr_err("%s: mmap regions failed %d\n",
  1215. __func__, ret);
  1216. ret = -EINVAL;
  1217. goto done;
  1218. }
  1219. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1220. (q6core_lcl.bus_bw_resp_received == 1),
  1221. msecs_to_jiffies(TIMEOUT_MS));
  1222. if (!ret) {
  1223. pr_err("%s: timeout. waited for memory map\n", __func__);
  1224. ret = -ETIME;
  1225. goto done;
  1226. } else {
  1227. /* set ret to 0 as no timeout happened */
  1228. ret = 0;
  1229. }
  1230. if (q6core_lcl.adsp_status < 0) {
  1231. pr_err("%s: DSP returned error %d\n",
  1232. __func__, q6core_lcl.adsp_status);
  1233. ret = q6core_lcl.adsp_status;
  1234. goto done;
  1235. }
  1236. *map_handle = q6core_lcl.mem_map_cal_handle;
  1237. done:
  1238. kfree(mmap_region_cmd);
  1239. return ret;
  1240. }
  1241. /**
  1242. * q6core_map_mdf_memory_regions - for sending MDF shared memory map information
  1243. * to ADSP.
  1244. *
  1245. * @buf_add: array of buffers.
  1246. * @mempool_id: memory pool ID
  1247. * @bufsz: size of the buffer
  1248. * @bufcnt: buffers count
  1249. * @map_handle: map handle received from ADSP
  1250. */
  1251. int q6core_map_mdf_memory_regions(uint64_t *buf_add, uint32_t mempool_id,
  1252. uint32_t *bufsz, uint32_t bufcnt, uint32_t *map_handle)
  1253. {
  1254. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  1255. struct avs_shared_map_region_payload *mregions = NULL;
  1256. void *mmap_region_cmd = NULL;
  1257. void *payload = NULL;
  1258. int ret = 0;
  1259. int i = 0;
  1260. int cmd_size = 0;
  1261. mutex_lock(&q6core_lcl.cmd_lock);
  1262. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  1263. + sizeof(struct avs_shared_map_region_payload)
  1264. * bufcnt;
  1265. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1266. if (mmap_region_cmd == NULL)
  1267. return -ENOMEM;
  1268. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  1269. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1270. APR_HDR_LEN(APR_HDR_SIZE),
  1271. APR_PKT_VER);
  1272. mmap_regions->hdr.pkt_size = cmd_size;
  1273. mmap_regions->hdr.src_port = 0;
  1274. mmap_regions->hdr.dest_port = 0;
  1275. mmap_regions->hdr.token = MDF_MAP_TOKEN;
  1276. mmap_regions->hdr.opcode = AVCS_CMD_SHARED_MEM_MAP_REGIONS;
  1277. mmap_regions->mem_pool_id = mempool_id & MEMPOOL_ID_MASK;
  1278. mmap_regions->num_regions = bufcnt & 0x00ff;
  1279. mmap_regions->property_flag = 0x00;
  1280. payload = ((u8 *) mmap_region_cmd +
  1281. sizeof(struct avs_cmd_shared_mem_map_regions));
  1282. mregions = (struct avs_shared_map_region_payload *)payload;
  1283. for (i = 0; i < bufcnt; i++) {
  1284. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  1285. mregions->shm_addr_msw = upper_32_bits(buf_add[i]);
  1286. mregions->mem_size_bytes = bufsz[i];
  1287. ++mregions;
  1288. }
  1289. pr_debug("%s: sending MDF memory map, addr %pK, size %d, bufcnt = %d\n",
  1290. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  1291. *map_handle = 0;
  1292. q6core_lcl.adsp_status = 0;
  1293. q6core_lcl.mdf_map_resp_received = 0;
  1294. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1295. mmap_regions);
  1296. if (ret < 0) {
  1297. pr_err("%s: mmap regions failed %d\n",
  1298. __func__, ret);
  1299. ret = -EINVAL;
  1300. goto done;
  1301. }
  1302. ret = wait_event_timeout(q6core_lcl.mdf_map_resp_wait,
  1303. (q6core_lcl.mdf_map_resp_received == 1),
  1304. msecs_to_jiffies(TIMEOUT_MS));
  1305. if (!ret) {
  1306. pr_err("%s: timeout. waited for memory map\n", __func__);
  1307. ret = -ETIMEDOUT;
  1308. goto done;
  1309. } else {
  1310. /* set ret to 0 as no timeout happened */
  1311. ret = 0;
  1312. }
  1313. if (q6core_lcl.adsp_status < 0) {
  1314. pr_err("%s: DSP returned error %d\n",
  1315. __func__, q6core_lcl.adsp_status);
  1316. ret = q6core_lcl.adsp_status;
  1317. goto done;
  1318. }
  1319. *map_handle = q6core_lcl.mdf_mem_map_cal_handle;
  1320. done:
  1321. kfree(mmap_region_cmd);
  1322. mutex_unlock(&q6core_lcl.cmd_lock);
  1323. return ret;
  1324. }
  1325. EXPORT_SYMBOL(q6core_map_mdf_memory_regions);
  1326. int q6core_memory_unmap_regions(uint32_t mem_map_handle)
  1327. {
  1328. struct avs_cmd_shared_mem_unmap_regions unmap_regions;
  1329. int ret = 0;
  1330. memset(&unmap_regions, 0, sizeof(unmap_regions));
  1331. unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1332. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1333. unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
  1334. unmap_regions.hdr.src_svc = APR_SVC_ADSP_CORE;
  1335. unmap_regions.hdr.src_domain = APR_DOMAIN_APPS;
  1336. unmap_regions.hdr.src_port = 0;
  1337. unmap_regions.hdr.dest_svc = APR_SVC_ADSP_CORE;
  1338. unmap_regions.hdr.dest_domain = APR_DOMAIN_ADSP;
  1339. unmap_regions.hdr.dest_port = 0;
  1340. unmap_regions.hdr.token = 0;
  1341. unmap_regions.hdr.opcode = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS;
  1342. unmap_regions.mem_map_handle = mem_map_handle;
  1343. q6core_lcl.adsp_status = 0;
  1344. q6core_lcl.bus_bw_resp_received = 0;
  1345. pr_debug("%s: unmap regions map handle %d\n",
  1346. __func__, mem_map_handle);
  1347. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1348. &unmap_regions);
  1349. if (ret < 0) {
  1350. pr_err("%s: unmap regions failed %d\n",
  1351. __func__, ret);
  1352. ret = -EINVAL;
  1353. goto done;
  1354. }
  1355. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1356. (q6core_lcl.bus_bw_resp_received == 1),
  1357. msecs_to_jiffies(TIMEOUT_MS));
  1358. if (!ret) {
  1359. pr_err("%s: timeout. waited for memory_unmap\n",
  1360. __func__);
  1361. ret = -ETIME;
  1362. goto done;
  1363. } else {
  1364. /* set ret to 0 as no timeout happened */
  1365. ret = 0;
  1366. }
  1367. if (q6core_lcl.adsp_status < 0) {
  1368. pr_err("%s: DSP returned error %d\n",
  1369. __func__, q6core_lcl.adsp_status);
  1370. ret = q6core_lcl.adsp_status;
  1371. goto done;
  1372. }
  1373. done:
  1374. return ret;
  1375. }
  1376. int q6core_map_mdf_shared_memory(uint32_t map_handle, uint64_t *buf_add,
  1377. uint32_t proc_id, uint32_t *bufsz, uint32_t bufcnt)
  1378. {
  1379. struct avs_cmd_map_mdf_shared_memory *mmap_regions = NULL;
  1380. struct avs_shared_map_region_payload *mregions = NULL;
  1381. void *mmap_region_cmd = NULL;
  1382. void *payload = NULL;
  1383. int ret = 0;
  1384. int i = 0;
  1385. int cmd_size = 0;
  1386. mutex_lock(&q6core_lcl.cmd_lock);
  1387. cmd_size = sizeof(struct avs_cmd_map_mdf_shared_memory)
  1388. + sizeof(struct avs_shared_map_region_payload)
  1389. * bufcnt;
  1390. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1391. if (mmap_region_cmd == NULL) {
  1392. mutex_unlock(&q6core_lcl.cmd_lock);
  1393. return -ENOMEM;
  1394. }
  1395. mmap_regions = (struct avs_cmd_map_mdf_shared_memory *)mmap_region_cmd;
  1396. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1397. APR_HDR_LEN(APR_HDR_SIZE),
  1398. APR_PKT_VER);
  1399. mmap_regions->hdr.pkt_size = cmd_size;
  1400. mmap_regions->hdr.src_port = 0;
  1401. mmap_regions->hdr.dest_port = 0;
  1402. mmap_regions->hdr.token = 0;
  1403. mmap_regions->hdr.opcode = AVCS_CMD_MAP_MDF_SHARED_MEMORY;
  1404. mmap_regions->mem_map_handle = map_handle;
  1405. mmap_regions->proc_id = proc_id & 0x00ff;
  1406. mmap_regions->num_regions = bufcnt & 0x00ff;
  1407. payload = ((u8 *) mmap_region_cmd +
  1408. sizeof(struct avs_cmd_map_mdf_shared_memory));
  1409. mregions = (struct avs_shared_map_region_payload *)payload;
  1410. for (i = 0; i < bufcnt; i++) {
  1411. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  1412. mregions->shm_addr_msw = upper_32_bits(buf_add[i]);
  1413. mregions->mem_size_bytes = bufsz[i];
  1414. ++mregions;
  1415. }
  1416. pr_debug("%s: sending mdf memory map, addr %pa, size %d, bufcnt = %d\n",
  1417. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  1418. q6core_lcl.adsp_status = 0;
  1419. q6core_lcl.bus_bw_resp_received = 0;
  1420. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1421. mmap_regions);
  1422. if (ret < 0) {
  1423. pr_err("%s: mdf memory map failed %d\n",
  1424. __func__, ret);
  1425. ret = -EINVAL;
  1426. goto done;
  1427. }
  1428. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1429. (q6core_lcl.bus_bw_resp_received == 1),
  1430. msecs_to_jiffies(TIMEOUT_MS));
  1431. if (!ret) {
  1432. pr_err("%s: timeout. waited for mdf memory map\n",
  1433. __func__);
  1434. ret = -ETIME;
  1435. goto done;
  1436. } else {
  1437. /* set ret to 0 as no timeout happened */
  1438. ret = 0;
  1439. }
  1440. /*
  1441. * When the remote DSP is not ready, the ADSP will validate and store
  1442. * the memory information and return APR_ENOTREADY to HLOS. The ADSP
  1443. * will map the memory with remote DSP when it is ready. HLOS should
  1444. * not treat APR_ENOTREADY as an error.
  1445. */
  1446. if (q6core_lcl.adsp_status != -APR_ENOTREADY) {
  1447. pr_err("%s: DSP returned error %d\n",
  1448. __func__, q6core_lcl.adsp_status);
  1449. ret = q6core_lcl.adsp_status;
  1450. goto done;
  1451. }
  1452. done:
  1453. kfree(mmap_region_cmd);
  1454. mutex_unlock(&q6core_lcl.cmd_lock);
  1455. return ret;
  1456. }
  1457. static int q6core_dereg_all_custom_topologies(void)
  1458. {
  1459. int ret = 0;
  1460. struct avcs_cmd_deregister_topologies dereg_top;
  1461. memset(&dereg_top, 0, sizeof(dereg_top));
  1462. dereg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1463. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1464. dereg_top.hdr.pkt_size = sizeof(dereg_top);
  1465. dereg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  1466. dereg_top.hdr.src_domain = APR_DOMAIN_APPS;
  1467. dereg_top.hdr.src_port = 0;
  1468. dereg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  1469. dereg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  1470. dereg_top.hdr.dest_port = 0;
  1471. dereg_top.hdr.token = 0;
  1472. dereg_top.hdr.opcode = AVCS_CMD_DEREGISTER_TOPOLOGIES;
  1473. dereg_top.payload_addr_lsw = 0;
  1474. dereg_top.payload_addr_msw = 0;
  1475. dereg_top.mem_map_handle = 0;
  1476. dereg_top.payload_size = 0;
  1477. dereg_top.mode = AVCS_MODE_DEREGISTER_ALL_CUSTOM_TOPOLOGIES;
  1478. q6core_lcl.bus_bw_resp_received = 0;
  1479. pr_debug("%s: Deregister topologies mode %d\n",
  1480. __func__, dereg_top.mode);
  1481. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &dereg_top);
  1482. if (ret < 0) {
  1483. pr_err("%s: Deregister topologies failed %d\n",
  1484. __func__, ret);
  1485. goto done;
  1486. }
  1487. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1488. (q6core_lcl.bus_bw_resp_received == 1),
  1489. msecs_to_jiffies(TIMEOUT_MS));
  1490. if (!ret) {
  1491. pr_err("%s: wait_event timeout for Deregister topologies\n",
  1492. __func__);
  1493. goto done;
  1494. }
  1495. done:
  1496. return ret;
  1497. }
  1498. static int q6core_send_custom_topologies(void)
  1499. {
  1500. int ret = 0;
  1501. int ret2 = 0;
  1502. struct cal_block_data *cal_block = NULL;
  1503. struct avcs_cmd_register_topologies reg_top;
  1504. if (!q6core_is_adsp_ready()) {
  1505. pr_err("%s: ADSP is not ready!\n", __func__);
  1506. return -ENODEV;
  1507. }
  1508. memset(&reg_top, 0, sizeof(reg_top));
  1509. mutex_lock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  1510. mutex_lock(&q6core_lcl.cmd_lock);
  1511. cal_block = cal_utils_get_only_cal_block(
  1512. q6core_lcl.cal_data[CUST_TOP_CAL]);
  1513. if (cal_block == NULL) {
  1514. pr_debug("%s: cal block is NULL!\n", __func__);
  1515. goto unlock;
  1516. }
  1517. if (cal_block->cal_data.size <= 0) {
  1518. pr_debug("%s: cal size is %zd not sending\n",
  1519. __func__, cal_block->cal_data.size);
  1520. goto unlock;
  1521. }
  1522. q6core_dereg_all_custom_topologies();
  1523. ret = q6core_map_memory_regions(&cal_block->cal_data.paddr,
  1524. ADSP_MEMORY_MAP_SHMEM8_4K_POOL,
  1525. (uint32_t *)&cal_block->map_data.map_size, 1,
  1526. &cal_block->map_data.q6map_handle);
  1527. if (ret) {
  1528. pr_err("%s: q6core_map_memory_regions failed\n", __func__);
  1529. goto unlock;
  1530. }
  1531. reg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1532. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1533. reg_top.hdr.pkt_size = sizeof(reg_top);
  1534. reg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  1535. reg_top.hdr.src_domain = APR_DOMAIN_APPS;
  1536. reg_top.hdr.src_port = 0;
  1537. reg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  1538. reg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  1539. reg_top.hdr.dest_port = 0;
  1540. reg_top.hdr.token = 0;
  1541. reg_top.hdr.opcode = AVCS_CMD_REGISTER_TOPOLOGIES;
  1542. reg_top.payload_addr_lsw =
  1543. lower_32_bits(cal_block->cal_data.paddr);
  1544. reg_top.payload_addr_msw =
  1545. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  1546. reg_top.mem_map_handle = cal_block->map_data.q6map_handle;
  1547. reg_top.payload_size = cal_block->cal_data.size;
  1548. q6core_lcl.adsp_status = 0;
  1549. q6core_lcl.bus_bw_resp_received = 0;
  1550. pr_debug("%s: Register topologies addr %pK, size %zd, map handle %d\n",
  1551. __func__, &cal_block->cal_data.paddr, cal_block->cal_data.size,
  1552. cal_block->map_data.q6map_handle);
  1553. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &reg_top);
  1554. if (ret < 0) {
  1555. pr_err("%s: Register topologies failed %d\n",
  1556. __func__, ret);
  1557. goto unmap;
  1558. }
  1559. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1560. (q6core_lcl.bus_bw_resp_received == 1),
  1561. msecs_to_jiffies(TIMEOUT_MS));
  1562. if (!ret) {
  1563. pr_err("%s: wait_event timeout for Register topologies\n",
  1564. __func__);
  1565. goto unmap;
  1566. }
  1567. if (q6core_lcl.adsp_status < 0)
  1568. ret = q6core_lcl.adsp_status;
  1569. unmap:
  1570. ret2 = q6core_memory_unmap_regions(cal_block->map_data.q6map_handle);
  1571. if (ret2) {
  1572. pr_err("%s: q6core_memory_unmap_regions failed for map handle %d\n",
  1573. __func__, cal_block->map_data.q6map_handle);
  1574. ret = ret2;
  1575. goto unlock;
  1576. }
  1577. unlock:
  1578. mutex_unlock(&q6core_lcl.cmd_lock);
  1579. mutex_unlock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  1580. return ret;
  1581. }
  1582. static int get_cal_type_index(int32_t cal_type)
  1583. {
  1584. int ret = -EINVAL;
  1585. switch (cal_type) {
  1586. case AUDIO_CORE_METAINFO_CAL_TYPE:
  1587. ret = META_CAL;
  1588. break;
  1589. case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
  1590. ret = CUST_TOP_CAL;
  1591. break;
  1592. default:
  1593. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  1594. }
  1595. return ret;
  1596. }
  1597. static int q6core_alloc_cal(int32_t cal_type,
  1598. size_t data_size, void *data)
  1599. {
  1600. int ret = 0;
  1601. int cal_index;
  1602. cal_index = get_cal_type_index(cal_type);
  1603. if (cal_index < 0) {
  1604. pr_err("%s: could not get cal index %d!\n",
  1605. __func__, cal_index);
  1606. ret = -EINVAL;
  1607. goto done;
  1608. }
  1609. ret = cal_utils_alloc_cal(data_size, data,
  1610. q6core_lcl.cal_data[cal_index], 0, NULL);
  1611. if (ret < 0) {
  1612. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  1613. __func__, ret, cal_type);
  1614. goto done;
  1615. }
  1616. done:
  1617. return ret;
  1618. }
  1619. static int q6core_dealloc_cal(int32_t cal_type,
  1620. size_t data_size, void *data)
  1621. {
  1622. int ret = 0;
  1623. int cal_index;
  1624. cal_index = get_cal_type_index(cal_type);
  1625. if (cal_index < 0) {
  1626. pr_err("%s: could not get cal index %d!\n",
  1627. __func__, cal_index);
  1628. ret = -EINVAL;
  1629. goto done;
  1630. }
  1631. ret = cal_utils_dealloc_cal(data_size, data,
  1632. q6core_lcl.cal_data[cal_index]);
  1633. if (ret < 0) {
  1634. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  1635. __func__, ret, cal_type);
  1636. goto done;
  1637. }
  1638. done:
  1639. return ret;
  1640. }
  1641. static int q6core_set_cal(int32_t cal_type,
  1642. size_t data_size, void *data)
  1643. {
  1644. int ret = 0;
  1645. int cal_index;
  1646. cal_index = get_cal_type_index(cal_type);
  1647. if (cal_index < 0) {
  1648. pr_err("%s: could not get cal index %d!\n",
  1649. __func__, cal_index);
  1650. ret = -EINVAL;
  1651. goto done;
  1652. }
  1653. ret = cal_utils_set_cal(data_size, data,
  1654. q6core_lcl.cal_data[cal_index], 0, NULL);
  1655. if (ret < 0) {
  1656. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  1657. __func__, ret, cal_type);
  1658. goto done;
  1659. }
  1660. if (cal_index == CUST_TOP_CAL)
  1661. ret = q6core_send_custom_topologies();
  1662. done:
  1663. return ret;
  1664. }
  1665. static void q6core_delete_cal_data(void)
  1666. {
  1667. pr_debug("%s:\n", __func__);
  1668. cal_utils_destroy_cal_types(CORE_MAX_CAL, q6core_lcl.cal_data);
  1669. }
  1670. static int q6core_init_cal_data(void)
  1671. {
  1672. int ret = 0;
  1673. struct cal_type_info cal_type_info[] = {
  1674. {{AUDIO_CORE_METAINFO_CAL_TYPE,
  1675. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  1676. q6core_set_cal, NULL, NULL} },
  1677. {NULL, NULL, cal_utils_match_buf_num} },
  1678. {{CORE_CUSTOM_TOPOLOGIES_CAL_TYPE,
  1679. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  1680. q6core_set_cal, NULL, NULL} },
  1681. {NULL, NULL, cal_utils_match_buf_num} }
  1682. };
  1683. pr_debug("%s:\n", __func__);
  1684. ret = cal_utils_create_cal_types(CORE_MAX_CAL,
  1685. q6core_lcl.cal_data, cal_type_info);
  1686. if (ret < 0) {
  1687. pr_err("%s: could not create cal type!\n",
  1688. __func__);
  1689. goto err;
  1690. }
  1691. return ret;
  1692. err:
  1693. q6core_delete_cal_data();
  1694. return ret;
  1695. }
  1696. static int q6core_is_avs_up(int32_t *avs_state)
  1697. {
  1698. unsigned long timeout;
  1699. int32_t adsp_ready = 0;
  1700. int ret = 0;
  1701. timeout = jiffies +
  1702. msecs_to_jiffies(ADSP_STATE_READY_TIMEOUT_MS);
  1703. /* sleep for 100ms before querying AVS up */
  1704. msleep(100);
  1705. do {
  1706. adsp_ready = q6core_is_adsp_ready();
  1707. pr_debug("%s: ADSP Audio is %s\n", __func__,
  1708. adsp_ready ? "ready" : "not ready");
  1709. if (adsp_ready)
  1710. break;
  1711. /*
  1712. * ADSP will be coming up after boot up and AVS might
  1713. * not be fully up when the control reaches here.
  1714. * So, wait for 50msec before checking ADSP state again.
  1715. */
  1716. msleep(50);
  1717. } while (time_after(timeout, jiffies));
  1718. *avs_state = adsp_ready;
  1719. pr_debug("%s: ADSP Audio is %s\n", __func__,
  1720. adsp_ready ? "ready" : "not ready");
  1721. if (!adsp_ready) {
  1722. pr_err_ratelimited("%s: Timeout. ADSP Audio is not ready\n",
  1723. __func__);
  1724. ret = -ETIMEDOUT;
  1725. }
  1726. return ret;
  1727. }
  1728. static int q6core_ssr_enable(struct device *dev, void *data)
  1729. {
  1730. int32_t avs_state = 0;
  1731. int ret = 0;
  1732. if (!dev) {
  1733. pr_err("%s: dev is NULL\n", __func__);
  1734. return -EINVAL;
  1735. }
  1736. if (!q6core_lcl.avs_state) {
  1737. ret = q6core_is_avs_up(&avs_state);
  1738. if (ret < 0)
  1739. goto err;
  1740. q6core_lcl.avs_state = avs_state;
  1741. }
  1742. err:
  1743. return ret;
  1744. }
  1745. static void q6core_ssr_disable(struct device *dev, void *data)
  1746. {
  1747. /* Reset AVS state to 0 */
  1748. q6core_lcl.avs_state = 0;
  1749. }
  1750. static const struct snd_event_ops q6core_ssr_ops = {
  1751. .enable = q6core_ssr_enable,
  1752. .disable = q6core_ssr_disable,
  1753. };
  1754. static int q6core_probe(struct platform_device *pdev)
  1755. {
  1756. int32_t avs_state = 0;
  1757. int rc = 0;
  1758. rc = q6core_is_avs_up(&avs_state);
  1759. if (rc < 0)
  1760. goto err;
  1761. q6core_lcl.avs_state = avs_state;
  1762. rc = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  1763. if (rc) {
  1764. dev_err(&pdev->dev, "%s: failed to add child nodes, rc=%d\n",
  1765. __func__, rc);
  1766. rc = -EINVAL;
  1767. goto err;
  1768. }
  1769. dev_dbg(&pdev->dev, "%s: added child node\n", __func__);
  1770. rc = snd_event_client_register(&pdev->dev, &q6core_ssr_ops, NULL);
  1771. if (!rc) {
  1772. snd_event_notify(&pdev->dev, SND_EVENT_UP);
  1773. } else {
  1774. dev_err(&pdev->dev,
  1775. "%s: Registration with SND event fwk failed rc = %d\n",
  1776. __func__, rc);
  1777. rc = 0;
  1778. }
  1779. err:
  1780. return rc;
  1781. }
  1782. static int q6core_remove(struct platform_device *pdev)
  1783. {
  1784. snd_event_client_deregister(&pdev->dev);
  1785. of_platform_depopulate(&pdev->dev);
  1786. return 0;
  1787. }
  1788. static const struct of_device_id q6core_of_match[] = {
  1789. { .compatible = "qcom,q6core-audio", },
  1790. {},
  1791. };
  1792. static struct platform_driver q6core_driver = {
  1793. .probe = q6core_probe,
  1794. .remove = q6core_remove,
  1795. .driver = {
  1796. .name = "q6core_audio",
  1797. .owner = THIS_MODULE,
  1798. .of_match_table = q6core_of_match,
  1799. .suppress_bind_attrs = true,
  1800. }
  1801. };
  1802. int __init core_init(void)
  1803. {
  1804. memset(&q6core_lcl, 0, sizeof(struct q6core_str));
  1805. init_waitqueue_head(&q6core_lcl.bus_bw_req_wait);
  1806. init_waitqueue_head(&q6core_lcl.cmd_req_wait);
  1807. init_waitqueue_head(&q6core_lcl.avcs_fwk_ver_req_wait);
  1808. init_waitqueue_head(&q6core_lcl.mdf_map_resp_wait);
  1809. init_waitqueue_head(&q6core_lcl.lpass_npa_rsc_wait);
  1810. init_waitqueue_head(&q6core_lcl.avcs_module_load_unload_wait);
  1811. q6core_lcl.cmd_resp_received_flag = FLAG_NONE;
  1812. mutex_init(&q6core_lcl.cmd_lock);
  1813. mutex_init(&q6core_lcl.ver_lock);
  1814. q6core_init_cal_data();
  1815. q6core_init_uevent_kset();
  1816. return platform_driver_register(&q6core_driver);
  1817. }
  1818. void core_exit(void)
  1819. {
  1820. mutex_destroy(&q6core_lcl.cmd_lock);
  1821. mutex_destroy(&q6core_lcl.ver_lock);
  1822. q6core_delete_cal_data();
  1823. q6core_destroy_uevent_kset();
  1824. platform_driver_unregister(&q6core_driver);
  1825. }
  1826. MODULE_DESCRIPTION("ADSP core driver");
  1827. MODULE_LICENSE("GPL v2");