q6core.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  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");
  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.avcs_module_resp_received = 0;
  885. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  886. (uint32_t *)mod);
  887. if (ret < 0) {
  888. pr_err("%s: modules load/unload failed ret = %d\n",
  889. __func__, ret);
  890. goto done;
  891. }
  892. ret = wait_event_timeout(q6core_lcl.avcs_module_load_unload_wait,
  893. (q6core_lcl.avcs_module_resp_received == 1),
  894. msecs_to_jiffies(TIMEOUT_MS));
  895. if (!ret) {
  896. pr_err("%s wait event timeout for avcs load/unload module\n",
  897. __func__);
  898. ret = -ETIMEDOUT;
  899. goto done;
  900. }
  901. if (q6core_lcl.adsp_status < 0) {
  902. pr_err("%s: modules load/unload failed %d\n", __func__,
  903. q6core_lcl.adsp_status);
  904. ret = q6core_lcl.adsp_status;
  905. goto done;
  906. } else {
  907. if (mod->hdr.opcode == AVCS_CMD_LOAD_MODULES)
  908. memcpy(payload, rsp_payload, payload_size);
  909. }
  910. done:
  911. kfree(mod);
  912. kfree(rsp_payload);
  913. mutex_unlock(&(q6core_lcl.cmd_lock));
  914. return ret;
  915. }
  916. EXPORT_SYMBOL(q6core_avcs_load_unload_modules);
  917. int32_t q6core_load_unload_topo_modules(uint32_t topo_id,
  918. bool preload_type)
  919. {
  920. struct avcs_cmd_load_unload_topo_modules load_unload_topo_modules;
  921. int ret = 0;
  922. mutex_lock(&(q6core_lcl.cmd_lock));
  923. ocm_core_open();
  924. if (q6core_lcl.core_handle_q == NULL) {
  925. pr_err("%s: apr registration for CORE failed\n", __func__);
  926. ret = -ENODEV;
  927. goto done;
  928. }
  929. memset(&load_unload_topo_modules, 0, sizeof(load_unload_topo_modules));
  930. load_unload_topo_modules.hdr.hdr_field =
  931. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  932. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  933. load_unload_topo_modules.hdr.pkt_size =
  934. sizeof(struct avcs_cmd_load_unload_topo_modules);
  935. load_unload_topo_modules.hdr.src_port = 0;
  936. load_unload_topo_modules.hdr.dest_port = 0;
  937. load_unload_topo_modules.hdr.token = 0;
  938. if (preload_type == CORE_LOAD_TOPOLOGY)
  939. load_unload_topo_modules.hdr.opcode =
  940. AVCS_CMD_LOAD_TOPO_MODULES;
  941. else
  942. load_unload_topo_modules.hdr.opcode =
  943. AVCS_CMD_UNLOAD_TOPO_MODULES;
  944. load_unload_topo_modules.topology_id = topo_id;
  945. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  946. (uint32_t *) &load_unload_topo_modules);
  947. if (ret < 0) {
  948. pr_err("%s: Load/unload topo modules failed for topology = %d ret = %d\n",
  949. __func__, topo_id, ret);
  950. ret = -EINVAL;
  951. }
  952. done:
  953. mutex_unlock(&(q6core_lcl.cmd_lock));
  954. return ret;
  955. }
  956. EXPORT_SYMBOL(q6core_load_unload_topo_modules);
  957. /**
  958. * q6core_is_adsp_ready - check adsp ready status
  959. *
  960. * Returns true if adsp is ready otherwise returns false
  961. */
  962. bool q6core_is_adsp_ready(void)
  963. {
  964. int rc = 0;
  965. bool ret = false;
  966. struct apr_hdr hdr;
  967. pr_debug("%s: enter\n", __func__);
  968. memset(&hdr, 0, sizeof(hdr));
  969. hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  970. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  971. hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE, 0);
  972. hdr.opcode = AVCS_CMD_ADSP_EVENT_GET_STATE;
  973. mutex_lock(&(q6core_lcl.cmd_lock));
  974. ocm_core_open();
  975. if (q6core_lcl.core_handle_q) {
  976. q6core_lcl.bus_bw_resp_received = 0;
  977. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)&hdr);
  978. if (rc < 0) {
  979. pr_err_ratelimited("%s: Get ADSP state APR packet send event %d\n",
  980. __func__, rc);
  981. goto bail;
  982. }
  983. rc = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  984. (q6core_lcl.bus_bw_resp_received == 1),
  985. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  986. if (rc > 0 && q6core_lcl.bus_bw_resp_received) {
  987. /* ensure to read updated param by callback thread */
  988. rmb();
  989. ret = !!q6core_lcl.param;
  990. }
  991. }
  992. bail:
  993. pr_debug("%s: leave, rc %d, adsp ready %d\n", __func__, rc, ret);
  994. mutex_unlock(&(q6core_lcl.cmd_lock));
  995. return ret;
  996. }
  997. EXPORT_SYMBOL(q6core_is_adsp_ready);
  998. int q6core_create_lpass_npa_client(uint32_t node_id, char *client_name,
  999. uint32_t *client_handle)
  1000. {
  1001. struct avcs_cmd_create_lpass_npa_client_t create_lpass_npa_client;
  1002. struct avcs_cmd_create_lpass_npa_client_t *cmd_ptr =
  1003. &create_lpass_npa_client;
  1004. int ret = 0;
  1005. if (!client_name) {
  1006. pr_err("%s: Invalid params\n", __func__);
  1007. return -EINVAL;
  1008. }
  1009. mutex_lock(&(q6core_lcl.cmd_lock));
  1010. memset(cmd_ptr, 0, sizeof(create_lpass_npa_client));
  1011. cmd_ptr->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1012. APR_HDR_LEN(APR_HDR_SIZE),
  1013. APR_PKT_VER);
  1014. cmd_ptr->hdr.pkt_size = sizeof(create_lpass_npa_client);
  1015. cmd_ptr->hdr.src_port = 0;
  1016. cmd_ptr->hdr.dest_port = 0;
  1017. cmd_ptr->hdr.token = 0;
  1018. cmd_ptr->hdr.opcode = AVCS_CMD_CREATE_LPASS_NPA_CLIENT;
  1019. cmd_ptr->node_id = AVCS_SLEEP_ISLAND_CORE_DRIVER_NODE_ID;
  1020. strlcpy(cmd_ptr->client_name, client_name,
  1021. sizeof(cmd_ptr->client_name));
  1022. pr_debug("%s: create lpass npa client opcode[0x%x] node id[0x%x]\n",
  1023. __func__, cmd_ptr->hdr.opcode, cmd_ptr->node_id);
  1024. *client_handle = 0;
  1025. q6core_lcl.adsp_status = 0;
  1026. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 0;
  1027. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) cmd_ptr);
  1028. if (ret < 0) {
  1029. pr_err("%s: create lpass npa client failed %d\n",
  1030. __func__, ret);
  1031. ret = -EINVAL;
  1032. goto done;
  1033. }
  1034. ret = wait_event_timeout(q6core_lcl.lpass_npa_rsc_wait,
  1035. (q6core_lcl.lpass_npa_rsc_rsp_rcvd == 1),
  1036. msecs_to_jiffies(TIMEOUT_MS));
  1037. if (!ret) {
  1038. pr_err("%s: timeout. waited for create lpass npa rsc client\n",
  1039. __func__);
  1040. ret = -ETIMEDOUT;
  1041. goto done;
  1042. } else {
  1043. /* set ret to 0 as no timeout happened */
  1044. ret = 0;
  1045. }
  1046. if (q6core_lcl.adsp_status < 0) {
  1047. pr_err("%s: DSP returned error %d\n",
  1048. __func__, q6core_lcl.adsp_status);
  1049. ret = q6core_lcl.adsp_status;
  1050. goto done;
  1051. }
  1052. *client_handle = q6core_lcl.npa_client_handle;
  1053. pr_debug("%s: q6core_lcl.npa_client_handle %d\n", __func__,
  1054. q6core_lcl.npa_client_handle);
  1055. done:
  1056. mutex_unlock(&q6core_lcl.cmd_lock);
  1057. return ret;
  1058. }
  1059. EXPORT_SYMBOL(q6core_create_lpass_npa_client);
  1060. int q6core_destroy_lpass_npa_client(uint32_t client_handle)
  1061. {
  1062. struct avcs_cmd_destroy_lpass_npa_client_t destroy_lpass_npa_client;
  1063. struct avcs_cmd_destroy_lpass_npa_client_t *cmd_ptr =
  1064. &destroy_lpass_npa_client;
  1065. int ret = 0;
  1066. mutex_lock(&(q6core_lcl.cmd_lock));
  1067. memset(cmd_ptr, 0, sizeof(destroy_lpass_npa_client));
  1068. cmd_ptr->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1069. APR_HDR_LEN(APR_HDR_SIZE),
  1070. APR_PKT_VER);
  1071. cmd_ptr->hdr.pkt_size = sizeof(destroy_lpass_npa_client);
  1072. cmd_ptr->hdr.src_port = 0;
  1073. cmd_ptr->hdr.dest_port = 0;
  1074. cmd_ptr->hdr.token = 0;
  1075. cmd_ptr->hdr.opcode = AVCS_CMD_DESTROY_LPASS_NPA_CLIENT;
  1076. cmd_ptr->client_handle = client_handle;
  1077. pr_debug("%s: dstry lpass npa client opcode[0x%x] client hdl[0x%x]\n",
  1078. __func__, cmd_ptr->hdr.opcode, cmd_ptr->client_handle);
  1079. q6core_lcl.adsp_status = 0;
  1080. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 0;
  1081. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) cmd_ptr);
  1082. if (ret < 0) {
  1083. pr_err("%s: destroy lpass npa client failed %d\n",
  1084. __func__, ret);
  1085. ret = -EINVAL;
  1086. goto done;
  1087. }
  1088. ret = wait_event_timeout(q6core_lcl.lpass_npa_rsc_wait,
  1089. (q6core_lcl.lpass_npa_rsc_rsp_rcvd == 1),
  1090. msecs_to_jiffies(TIMEOUT_MS));
  1091. if (!ret) {
  1092. pr_err("%s: timeout. waited for destroy lpass npa rsc client\n",
  1093. __func__);
  1094. ret = -ETIMEDOUT;
  1095. goto done;
  1096. } else {
  1097. /* set ret to 0 as no timeout happened */
  1098. ret = 0;
  1099. }
  1100. if (q6core_lcl.adsp_status < 0) {
  1101. pr_err("%s: DSP returned error %d\n",
  1102. __func__, q6core_lcl.adsp_status);
  1103. ret = q6core_lcl.adsp_status;
  1104. }
  1105. done:
  1106. mutex_unlock(&q6core_lcl.cmd_lock);
  1107. return ret;
  1108. }
  1109. EXPORT_SYMBOL(q6core_destroy_lpass_npa_client);
  1110. int q6core_request_island_transition(uint32_t client_handle,
  1111. uint32_t island_allow_mode)
  1112. {
  1113. struct avcs_sleep_node_island_transition_config_t island_tsn_cfg;
  1114. struct avcs_sleep_node_island_transition_config_t *cmd_ptr =
  1115. &island_tsn_cfg;
  1116. int ret = 0;
  1117. mutex_lock(&(q6core_lcl.cmd_lock));
  1118. memset(cmd_ptr, 0, sizeof(island_tsn_cfg));
  1119. cmd_ptr->req_lpass_npa_rsc.hdr.hdr_field =
  1120. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1121. APR_HDR_LEN(APR_HDR_SIZE),
  1122. APR_PKT_VER);
  1123. cmd_ptr->req_lpass_npa_rsc.hdr.pkt_size = sizeof(island_tsn_cfg);
  1124. cmd_ptr->req_lpass_npa_rsc.hdr.src_port = 0;
  1125. cmd_ptr->req_lpass_npa_rsc.hdr.dest_port = 0;
  1126. cmd_ptr->req_lpass_npa_rsc.hdr.token = 0;
  1127. cmd_ptr->req_lpass_npa_rsc.hdr.opcode =
  1128. AVCS_CMD_REQUEST_LPASS_NPA_RESOURCES;
  1129. cmd_ptr->req_lpass_npa_rsc.client_handle = client_handle;
  1130. cmd_ptr->req_lpass_npa_rsc.resource_id =
  1131. AVCS_SLEEP_NODE_ISLAND_TRANSITION_RESOURCE_ID;
  1132. cmd_ptr->island_allow_mode = island_allow_mode;
  1133. pr_debug("%s: req islnd tnsn opcode[0x%x] island_allow_mode[0x%x]\n",
  1134. __func__, cmd_ptr->req_lpass_npa_rsc.hdr.opcode,
  1135. cmd_ptr->island_allow_mode);
  1136. q6core_lcl.adsp_status = 0;
  1137. q6core_lcl.lpass_npa_rsc_rsp_rcvd = 0;
  1138. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) cmd_ptr);
  1139. if (ret < 0) {
  1140. pr_err("%s: island tnsn cmd send failed %d\n",
  1141. __func__, ret);
  1142. ret = -EINVAL;
  1143. goto done;
  1144. }
  1145. ret = wait_event_timeout(q6core_lcl.lpass_npa_rsc_wait,
  1146. (q6core_lcl.lpass_npa_rsc_rsp_rcvd == 1),
  1147. msecs_to_jiffies(TIMEOUT_MS));
  1148. if (!ret) {
  1149. pr_err("%s: timeout. waited for island lpass npa rsc req\n",
  1150. __func__);
  1151. ret = -ETIMEDOUT;
  1152. goto done;
  1153. } else {
  1154. /* set ret to 0 as no timeout happened */
  1155. ret = 0;
  1156. }
  1157. if (q6core_lcl.adsp_status < 0) {
  1158. pr_err("%s: DSP returned error %d\n",
  1159. __func__, q6core_lcl.adsp_status);
  1160. ret = q6core_lcl.adsp_status;
  1161. }
  1162. done:
  1163. mutex_unlock(&q6core_lcl.cmd_lock);
  1164. return ret;
  1165. }
  1166. EXPORT_SYMBOL(q6core_request_island_transition);
  1167. int q6core_map_memory_regions(phys_addr_t *buf_add, uint32_t mempool_id,
  1168. uint32_t *bufsz, uint32_t bufcnt, uint32_t *map_handle)
  1169. {
  1170. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  1171. struct avs_shared_map_region_payload *mregions = NULL;
  1172. void *mmap_region_cmd = NULL;
  1173. void *payload = NULL;
  1174. int ret = 0;
  1175. int i = 0;
  1176. int cmd_size = 0;
  1177. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  1178. + sizeof(struct avs_shared_map_region_payload)
  1179. * bufcnt;
  1180. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1181. if (mmap_region_cmd == NULL)
  1182. return -ENOMEM;
  1183. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  1184. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1185. APR_HDR_LEN(APR_HDR_SIZE),
  1186. APR_PKT_VER);
  1187. mmap_regions->hdr.pkt_size = cmd_size;
  1188. mmap_regions->hdr.src_port = 0;
  1189. mmap_regions->hdr.dest_port = 0;
  1190. mmap_regions->hdr.token = 0;
  1191. mmap_regions->hdr.opcode = AVCS_CMD_SHARED_MEM_MAP_REGIONS;
  1192. mmap_regions->mem_pool_id = mempool_id & 0x00ff;
  1193. mmap_regions->num_regions = bufcnt & 0x00ff;
  1194. mmap_regions->property_flag = 0x00;
  1195. payload = ((u8 *) mmap_region_cmd +
  1196. sizeof(struct avs_cmd_shared_mem_map_regions));
  1197. mregions = (struct avs_shared_map_region_payload *)payload;
  1198. for (i = 0; i < bufcnt; i++) {
  1199. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  1200. mregions->shm_addr_msw =
  1201. msm_audio_populate_upper_32_bits(buf_add[i]);
  1202. mregions->mem_size_bytes = bufsz[i];
  1203. ++mregions;
  1204. }
  1205. pr_debug("%s: sending memory map, addr %pK, size %d, bufcnt = %d\n",
  1206. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  1207. *map_handle = 0;
  1208. q6core_lcl.adsp_status = 0;
  1209. q6core_lcl.bus_bw_resp_received = 0;
  1210. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1211. mmap_regions);
  1212. if (ret < 0) {
  1213. pr_err("%s: mmap regions failed %d\n",
  1214. __func__, ret);
  1215. ret = -EINVAL;
  1216. goto done;
  1217. }
  1218. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1219. (q6core_lcl.bus_bw_resp_received == 1),
  1220. msecs_to_jiffies(TIMEOUT_MS));
  1221. if (!ret) {
  1222. pr_err("%s: timeout. waited for memory map\n", __func__);
  1223. ret = -ETIME;
  1224. goto done;
  1225. } else {
  1226. /* set ret to 0 as no timeout happened */
  1227. ret = 0;
  1228. }
  1229. if (q6core_lcl.adsp_status < 0) {
  1230. pr_err("%s: DSP returned error %d\n",
  1231. __func__, q6core_lcl.adsp_status);
  1232. ret = q6core_lcl.adsp_status;
  1233. goto done;
  1234. }
  1235. *map_handle = q6core_lcl.mem_map_cal_handle;
  1236. done:
  1237. kfree(mmap_region_cmd);
  1238. return ret;
  1239. }
  1240. /**
  1241. * q6core_map_mdf_memory_regions - for sending MDF shared memory map information
  1242. * to ADSP.
  1243. *
  1244. * @buf_add: array of buffers.
  1245. * @mempool_id: memory pool ID
  1246. * @bufsz: size of the buffer
  1247. * @bufcnt: buffers count
  1248. * @map_handle: map handle received from ADSP
  1249. */
  1250. int q6core_map_mdf_memory_regions(uint64_t *buf_add, uint32_t mempool_id,
  1251. uint32_t *bufsz, uint32_t bufcnt, uint32_t *map_handle)
  1252. {
  1253. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  1254. struct avs_shared_map_region_payload *mregions = NULL;
  1255. void *mmap_region_cmd = NULL;
  1256. void *payload = NULL;
  1257. int ret = 0;
  1258. int i = 0;
  1259. int cmd_size = 0;
  1260. mutex_lock(&q6core_lcl.cmd_lock);
  1261. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  1262. + sizeof(struct avs_shared_map_region_payload)
  1263. * bufcnt;
  1264. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1265. if (mmap_region_cmd == NULL)
  1266. return -ENOMEM;
  1267. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  1268. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1269. APR_HDR_LEN(APR_HDR_SIZE),
  1270. APR_PKT_VER);
  1271. mmap_regions->hdr.pkt_size = cmd_size;
  1272. mmap_regions->hdr.src_port = 0;
  1273. mmap_regions->hdr.dest_port = 0;
  1274. mmap_regions->hdr.token = MDF_MAP_TOKEN;
  1275. mmap_regions->hdr.opcode = AVCS_CMD_SHARED_MEM_MAP_REGIONS;
  1276. mmap_regions->mem_pool_id = mempool_id & MEMPOOL_ID_MASK;
  1277. mmap_regions->num_regions = bufcnt & 0x00ff;
  1278. mmap_regions->property_flag = 0x00;
  1279. payload = ((u8 *) mmap_region_cmd +
  1280. sizeof(struct avs_cmd_shared_mem_map_regions));
  1281. mregions = (struct avs_shared_map_region_payload *)payload;
  1282. for (i = 0; i < bufcnt; i++) {
  1283. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  1284. mregions->shm_addr_msw = upper_32_bits(buf_add[i]);
  1285. mregions->mem_size_bytes = bufsz[i];
  1286. ++mregions;
  1287. }
  1288. pr_debug("%s: sending MDF memory map, addr %pK, size %d, bufcnt = %d\n",
  1289. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  1290. *map_handle = 0;
  1291. q6core_lcl.adsp_status = 0;
  1292. q6core_lcl.mdf_map_resp_received = 0;
  1293. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1294. mmap_regions);
  1295. if (ret < 0) {
  1296. pr_err("%s: mmap regions failed %d\n",
  1297. __func__, ret);
  1298. ret = -EINVAL;
  1299. goto done;
  1300. }
  1301. ret = wait_event_timeout(q6core_lcl.mdf_map_resp_wait,
  1302. (q6core_lcl.mdf_map_resp_received == 1),
  1303. msecs_to_jiffies(TIMEOUT_MS));
  1304. if (!ret) {
  1305. pr_err("%s: timeout. waited for memory map\n", __func__);
  1306. ret = -ETIMEDOUT;
  1307. goto done;
  1308. } else {
  1309. /* set ret to 0 as no timeout happened */
  1310. ret = 0;
  1311. }
  1312. if (q6core_lcl.adsp_status < 0) {
  1313. pr_err("%s: DSP returned error %d\n",
  1314. __func__, q6core_lcl.adsp_status);
  1315. ret = q6core_lcl.adsp_status;
  1316. goto done;
  1317. }
  1318. *map_handle = q6core_lcl.mdf_mem_map_cal_handle;
  1319. done:
  1320. kfree(mmap_region_cmd);
  1321. mutex_unlock(&q6core_lcl.cmd_lock);
  1322. return ret;
  1323. }
  1324. EXPORT_SYMBOL(q6core_map_mdf_memory_regions);
  1325. int q6core_memory_unmap_regions(uint32_t mem_map_handle)
  1326. {
  1327. struct avs_cmd_shared_mem_unmap_regions unmap_regions;
  1328. int ret = 0;
  1329. memset(&unmap_regions, 0, sizeof(unmap_regions));
  1330. unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1331. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1332. unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
  1333. unmap_regions.hdr.src_svc = APR_SVC_ADSP_CORE;
  1334. unmap_regions.hdr.src_domain = APR_DOMAIN_APPS;
  1335. unmap_regions.hdr.src_port = 0;
  1336. unmap_regions.hdr.dest_svc = APR_SVC_ADSP_CORE;
  1337. unmap_regions.hdr.dest_domain = APR_DOMAIN_ADSP;
  1338. unmap_regions.hdr.dest_port = 0;
  1339. unmap_regions.hdr.token = 0;
  1340. unmap_regions.hdr.opcode = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS;
  1341. unmap_regions.mem_map_handle = mem_map_handle;
  1342. q6core_lcl.adsp_status = 0;
  1343. q6core_lcl.bus_bw_resp_received = 0;
  1344. pr_debug("%s: unmap regions map handle %d\n",
  1345. __func__, mem_map_handle);
  1346. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1347. &unmap_regions);
  1348. if (ret < 0) {
  1349. pr_err("%s: unmap regions failed %d\n",
  1350. __func__, ret);
  1351. ret = -EINVAL;
  1352. goto done;
  1353. }
  1354. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1355. (q6core_lcl.bus_bw_resp_received == 1),
  1356. msecs_to_jiffies(TIMEOUT_MS));
  1357. if (!ret) {
  1358. pr_err("%s: timeout. waited for memory_unmap\n",
  1359. __func__);
  1360. ret = -ETIME;
  1361. goto done;
  1362. } else {
  1363. /* set ret to 0 as no timeout happened */
  1364. ret = 0;
  1365. }
  1366. if (q6core_lcl.adsp_status < 0) {
  1367. pr_err("%s: DSP returned error %d\n",
  1368. __func__, q6core_lcl.adsp_status);
  1369. ret = q6core_lcl.adsp_status;
  1370. goto done;
  1371. }
  1372. done:
  1373. return ret;
  1374. }
  1375. int q6core_map_mdf_shared_memory(uint32_t map_handle, uint64_t *buf_add,
  1376. uint32_t proc_id, uint32_t *bufsz, uint32_t bufcnt)
  1377. {
  1378. struct avs_cmd_map_mdf_shared_memory *mmap_regions = NULL;
  1379. struct avs_shared_map_region_payload *mregions = NULL;
  1380. void *mmap_region_cmd = NULL;
  1381. void *payload = NULL;
  1382. int ret = 0;
  1383. int i = 0;
  1384. int cmd_size = 0;
  1385. mutex_lock(&q6core_lcl.cmd_lock);
  1386. cmd_size = sizeof(struct avs_cmd_map_mdf_shared_memory)
  1387. + sizeof(struct avs_shared_map_region_payload)
  1388. * bufcnt;
  1389. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1390. if (mmap_region_cmd == NULL) {
  1391. mutex_unlock(&q6core_lcl.cmd_lock);
  1392. return -ENOMEM;
  1393. }
  1394. mmap_regions = (struct avs_cmd_map_mdf_shared_memory *)mmap_region_cmd;
  1395. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1396. APR_HDR_LEN(APR_HDR_SIZE),
  1397. APR_PKT_VER);
  1398. mmap_regions->hdr.pkt_size = cmd_size;
  1399. mmap_regions->hdr.src_port = 0;
  1400. mmap_regions->hdr.dest_port = 0;
  1401. mmap_regions->hdr.token = 0;
  1402. mmap_regions->hdr.opcode = AVCS_CMD_MAP_MDF_SHARED_MEMORY;
  1403. mmap_regions->mem_map_handle = map_handle;
  1404. mmap_regions->proc_id = proc_id & 0x00ff;
  1405. mmap_regions->num_regions = bufcnt & 0x00ff;
  1406. payload = ((u8 *) mmap_region_cmd +
  1407. sizeof(struct avs_cmd_map_mdf_shared_memory));
  1408. mregions = (struct avs_shared_map_region_payload *)payload;
  1409. for (i = 0; i < bufcnt; i++) {
  1410. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  1411. mregions->shm_addr_msw = upper_32_bits(buf_add[i]);
  1412. mregions->mem_size_bytes = bufsz[i];
  1413. ++mregions;
  1414. }
  1415. pr_debug("%s: sending mdf memory map, addr %pa, size %d, bufcnt = %d\n",
  1416. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  1417. q6core_lcl.adsp_status = 0;
  1418. q6core_lcl.bus_bw_resp_received = 0;
  1419. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  1420. mmap_regions);
  1421. if (ret < 0) {
  1422. pr_err("%s: mdf memory map failed %d\n",
  1423. __func__, ret);
  1424. ret = -EINVAL;
  1425. goto done;
  1426. }
  1427. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1428. (q6core_lcl.bus_bw_resp_received == 1),
  1429. msecs_to_jiffies(TIMEOUT_MS));
  1430. if (!ret) {
  1431. pr_err("%s: timeout. waited for mdf memory map\n",
  1432. __func__);
  1433. ret = -ETIME;
  1434. goto done;
  1435. } else {
  1436. /* set ret to 0 as no timeout happened */
  1437. ret = 0;
  1438. }
  1439. /*
  1440. * When the remote DSP is not ready, the ADSP will validate and store
  1441. * the memory information and return APR_ENOTREADY to HLOS. The ADSP
  1442. * will map the memory with remote DSP when it is ready. HLOS should
  1443. * not treat APR_ENOTREADY as an error.
  1444. */
  1445. if (q6core_lcl.adsp_status != -APR_ENOTREADY) {
  1446. pr_err("%s: DSP returned error %d\n",
  1447. __func__, q6core_lcl.adsp_status);
  1448. ret = q6core_lcl.adsp_status;
  1449. goto done;
  1450. }
  1451. done:
  1452. kfree(mmap_region_cmd);
  1453. mutex_unlock(&q6core_lcl.cmd_lock);
  1454. return ret;
  1455. }
  1456. static int q6core_dereg_all_custom_topologies(void)
  1457. {
  1458. int ret = 0;
  1459. struct avcs_cmd_deregister_topologies dereg_top;
  1460. memset(&dereg_top, 0, sizeof(dereg_top));
  1461. dereg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1462. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1463. dereg_top.hdr.pkt_size = sizeof(dereg_top);
  1464. dereg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  1465. dereg_top.hdr.src_domain = APR_DOMAIN_APPS;
  1466. dereg_top.hdr.src_port = 0;
  1467. dereg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  1468. dereg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  1469. dereg_top.hdr.dest_port = 0;
  1470. dereg_top.hdr.token = 0;
  1471. dereg_top.hdr.opcode = AVCS_CMD_DEREGISTER_TOPOLOGIES;
  1472. dereg_top.payload_addr_lsw = 0;
  1473. dereg_top.payload_addr_msw = 0;
  1474. dereg_top.mem_map_handle = 0;
  1475. dereg_top.payload_size = 0;
  1476. dereg_top.mode = AVCS_MODE_DEREGISTER_ALL_CUSTOM_TOPOLOGIES;
  1477. q6core_lcl.bus_bw_resp_received = 0;
  1478. pr_debug("%s: Deregister topologies mode %d\n",
  1479. __func__, dereg_top.mode);
  1480. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &dereg_top);
  1481. if (ret < 0) {
  1482. pr_err("%s: Deregister topologies failed %d\n",
  1483. __func__, ret);
  1484. goto done;
  1485. }
  1486. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1487. (q6core_lcl.bus_bw_resp_received == 1),
  1488. msecs_to_jiffies(TIMEOUT_MS));
  1489. if (!ret) {
  1490. pr_err("%s: wait_event timeout for Deregister topologies\n",
  1491. __func__);
  1492. goto done;
  1493. }
  1494. done:
  1495. return ret;
  1496. }
  1497. static int q6core_send_custom_topologies(void)
  1498. {
  1499. int ret = 0;
  1500. int ret2 = 0;
  1501. struct cal_block_data *cal_block = NULL;
  1502. struct avcs_cmd_register_topologies reg_top;
  1503. if (!q6core_is_adsp_ready()) {
  1504. pr_err("%s: ADSP is not ready!\n", __func__);
  1505. return -ENODEV;
  1506. }
  1507. memset(&reg_top, 0, sizeof(reg_top));
  1508. mutex_lock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  1509. mutex_lock(&q6core_lcl.cmd_lock);
  1510. cal_block = cal_utils_get_only_cal_block(
  1511. q6core_lcl.cal_data[CUST_TOP_CAL]);
  1512. if (cal_block == NULL) {
  1513. pr_debug("%s: cal block is NULL!\n", __func__);
  1514. goto unlock;
  1515. }
  1516. if (cal_block->cal_data.size <= 0) {
  1517. pr_debug("%s: cal size is %zd not sending\n",
  1518. __func__, cal_block->cal_data.size);
  1519. goto unlock;
  1520. }
  1521. q6core_dereg_all_custom_topologies();
  1522. ret = q6core_map_memory_regions(&cal_block->cal_data.paddr,
  1523. ADSP_MEMORY_MAP_SHMEM8_4K_POOL,
  1524. (uint32_t *)&cal_block->map_data.map_size, 1,
  1525. &cal_block->map_data.q6map_handle);
  1526. if (ret) {
  1527. pr_err("%s: q6core_map_memory_regions failed\n", __func__);
  1528. goto unlock;
  1529. }
  1530. reg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1531. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1532. reg_top.hdr.pkt_size = sizeof(reg_top);
  1533. reg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  1534. reg_top.hdr.src_domain = APR_DOMAIN_APPS;
  1535. reg_top.hdr.src_port = 0;
  1536. reg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  1537. reg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  1538. reg_top.hdr.dest_port = 0;
  1539. reg_top.hdr.token = 0;
  1540. reg_top.hdr.opcode = AVCS_CMD_REGISTER_TOPOLOGIES;
  1541. reg_top.payload_addr_lsw =
  1542. lower_32_bits(cal_block->cal_data.paddr);
  1543. reg_top.payload_addr_msw =
  1544. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  1545. reg_top.mem_map_handle = cal_block->map_data.q6map_handle;
  1546. reg_top.payload_size = cal_block->cal_data.size;
  1547. q6core_lcl.adsp_status = 0;
  1548. q6core_lcl.bus_bw_resp_received = 0;
  1549. pr_debug("%s: Register topologies addr %pK, size %zd, map handle %d\n",
  1550. __func__, &cal_block->cal_data.paddr, cal_block->cal_data.size,
  1551. cal_block->map_data.q6map_handle);
  1552. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &reg_top);
  1553. if (ret < 0) {
  1554. pr_err("%s: Register topologies failed %d\n",
  1555. __func__, ret);
  1556. goto unmap;
  1557. }
  1558. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  1559. (q6core_lcl.bus_bw_resp_received == 1),
  1560. msecs_to_jiffies(TIMEOUT_MS));
  1561. if (!ret) {
  1562. pr_err("%s: wait_event timeout for Register topologies\n",
  1563. __func__);
  1564. goto unmap;
  1565. }
  1566. if (q6core_lcl.adsp_status < 0)
  1567. ret = q6core_lcl.adsp_status;
  1568. unmap:
  1569. ret2 = q6core_memory_unmap_regions(cal_block->map_data.q6map_handle);
  1570. if (ret2) {
  1571. pr_err("%s: q6core_memory_unmap_regions failed for map handle %d\n",
  1572. __func__, cal_block->map_data.q6map_handle);
  1573. ret = ret2;
  1574. goto unlock;
  1575. }
  1576. unlock:
  1577. mutex_unlock(&q6core_lcl.cmd_lock);
  1578. mutex_unlock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  1579. return ret;
  1580. }
  1581. static int get_cal_type_index(int32_t cal_type)
  1582. {
  1583. int ret = -EINVAL;
  1584. switch (cal_type) {
  1585. case AUDIO_CORE_METAINFO_CAL_TYPE:
  1586. ret = META_CAL;
  1587. break;
  1588. case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
  1589. ret = CUST_TOP_CAL;
  1590. break;
  1591. default:
  1592. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  1593. }
  1594. return ret;
  1595. }
  1596. static int q6core_alloc_cal(int32_t cal_type,
  1597. size_t data_size, void *data)
  1598. {
  1599. int ret = 0;
  1600. int cal_index;
  1601. cal_index = get_cal_type_index(cal_type);
  1602. if (cal_index < 0) {
  1603. pr_err("%s: could not get cal index %d!\n",
  1604. __func__, cal_index);
  1605. ret = -EINVAL;
  1606. goto done;
  1607. }
  1608. ret = cal_utils_alloc_cal(data_size, data,
  1609. q6core_lcl.cal_data[cal_index], 0, NULL);
  1610. if (ret < 0) {
  1611. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  1612. __func__, ret, cal_type);
  1613. goto done;
  1614. }
  1615. done:
  1616. return ret;
  1617. }
  1618. static int q6core_dealloc_cal(int32_t cal_type,
  1619. size_t data_size, void *data)
  1620. {
  1621. int ret = 0;
  1622. int cal_index;
  1623. cal_index = get_cal_type_index(cal_type);
  1624. if (cal_index < 0) {
  1625. pr_err("%s: could not get cal index %d!\n",
  1626. __func__, cal_index);
  1627. ret = -EINVAL;
  1628. goto done;
  1629. }
  1630. ret = cal_utils_dealloc_cal(data_size, data,
  1631. q6core_lcl.cal_data[cal_index]);
  1632. if (ret < 0) {
  1633. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  1634. __func__, ret, cal_type);
  1635. goto done;
  1636. }
  1637. done:
  1638. return ret;
  1639. }
  1640. static int q6core_set_cal(int32_t cal_type,
  1641. size_t data_size, void *data)
  1642. {
  1643. int ret = 0;
  1644. int cal_index;
  1645. cal_index = get_cal_type_index(cal_type);
  1646. if (cal_index < 0) {
  1647. pr_err("%s: could not get cal index %d!\n",
  1648. __func__, cal_index);
  1649. ret = -EINVAL;
  1650. goto done;
  1651. }
  1652. ret = cal_utils_set_cal(data_size, data,
  1653. q6core_lcl.cal_data[cal_index], 0, NULL);
  1654. if (ret < 0) {
  1655. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  1656. __func__, ret, cal_type);
  1657. goto done;
  1658. }
  1659. if (cal_index == CUST_TOP_CAL)
  1660. ret = q6core_send_custom_topologies();
  1661. done:
  1662. return ret;
  1663. }
  1664. static void q6core_delete_cal_data(void)
  1665. {
  1666. pr_debug("%s:\n", __func__);
  1667. cal_utils_destroy_cal_types(CORE_MAX_CAL, q6core_lcl.cal_data);
  1668. }
  1669. static int q6core_init_cal_data(void)
  1670. {
  1671. int ret = 0;
  1672. struct cal_type_info cal_type_info[] = {
  1673. {{AUDIO_CORE_METAINFO_CAL_TYPE,
  1674. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  1675. q6core_set_cal, NULL, NULL} },
  1676. {NULL, NULL, cal_utils_match_buf_num} },
  1677. {{CORE_CUSTOM_TOPOLOGIES_CAL_TYPE,
  1678. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  1679. q6core_set_cal, NULL, NULL} },
  1680. {NULL, NULL, cal_utils_match_buf_num} }
  1681. };
  1682. pr_debug("%s:\n", __func__);
  1683. ret = cal_utils_create_cal_types(CORE_MAX_CAL,
  1684. q6core_lcl.cal_data, cal_type_info);
  1685. if (ret < 0) {
  1686. pr_err("%s: could not create cal type!\n",
  1687. __func__);
  1688. goto err;
  1689. }
  1690. return ret;
  1691. err:
  1692. q6core_delete_cal_data();
  1693. return ret;
  1694. }
  1695. static int q6core_is_avs_up(int32_t *avs_state)
  1696. {
  1697. unsigned long timeout;
  1698. int32_t adsp_ready = 0;
  1699. int ret = 0;
  1700. timeout = jiffies +
  1701. msecs_to_jiffies(ADSP_STATE_READY_TIMEOUT_MS);
  1702. /* sleep for 100ms before querying AVS up */
  1703. msleep(100);
  1704. do {
  1705. adsp_ready = q6core_is_adsp_ready();
  1706. pr_debug("%s: ADSP Audio is %s\n", __func__,
  1707. adsp_ready ? "ready" : "not ready");
  1708. if (adsp_ready)
  1709. break;
  1710. /*
  1711. * ADSP will be coming up after boot up and AVS might
  1712. * not be fully up when the control reaches here.
  1713. * So, wait for 50msec before checking ADSP state again.
  1714. */
  1715. msleep(50);
  1716. } while (time_after(timeout, jiffies));
  1717. *avs_state = adsp_ready;
  1718. pr_debug("%s: ADSP Audio is %s\n", __func__,
  1719. adsp_ready ? "ready" : "not ready");
  1720. if (!adsp_ready) {
  1721. pr_err_ratelimited("%s: Timeout. ADSP Audio is not ready\n",
  1722. __func__);
  1723. ret = -ETIMEDOUT;
  1724. }
  1725. return ret;
  1726. }
  1727. static int q6core_ssr_enable(struct device *dev, void *data)
  1728. {
  1729. int32_t avs_state = 0;
  1730. int ret = 0;
  1731. if (!dev) {
  1732. pr_err("%s: dev is NULL\n", __func__);
  1733. return -EINVAL;
  1734. }
  1735. if (!q6core_lcl.avs_state) {
  1736. ret = q6core_is_avs_up(&avs_state);
  1737. if (ret < 0)
  1738. goto err;
  1739. q6core_lcl.avs_state = avs_state;
  1740. }
  1741. err:
  1742. return ret;
  1743. }
  1744. static void q6core_ssr_disable(struct device *dev, void *data)
  1745. {
  1746. /* Reset AVS state to 0 */
  1747. q6core_lcl.avs_state = 0;
  1748. }
  1749. static const struct snd_event_ops q6core_ssr_ops = {
  1750. .enable = q6core_ssr_enable,
  1751. .disable = q6core_ssr_disable,
  1752. };
  1753. static int q6core_probe(struct platform_device *pdev)
  1754. {
  1755. int32_t avs_state = 0;
  1756. int rc = 0;
  1757. rc = q6core_is_avs_up(&avs_state);
  1758. if (rc < 0)
  1759. goto err;
  1760. q6core_lcl.avs_state = avs_state;
  1761. rc = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  1762. if (rc) {
  1763. dev_err(&pdev->dev, "%s: failed to add child nodes, rc=%d\n",
  1764. __func__, rc);
  1765. rc = -EINVAL;
  1766. goto err;
  1767. }
  1768. dev_dbg(&pdev->dev, "%s: added child node\n", __func__);
  1769. rc = snd_event_client_register(&pdev->dev, &q6core_ssr_ops, NULL);
  1770. if (!rc) {
  1771. snd_event_notify(&pdev->dev, SND_EVENT_UP);
  1772. } else {
  1773. dev_err(&pdev->dev,
  1774. "%s: Registration with SND event fwk failed rc = %d\n",
  1775. __func__, rc);
  1776. rc = 0;
  1777. }
  1778. err:
  1779. return rc;
  1780. }
  1781. static int q6core_remove(struct platform_device *pdev)
  1782. {
  1783. snd_event_client_deregister(&pdev->dev);
  1784. of_platform_depopulate(&pdev->dev);
  1785. return 0;
  1786. }
  1787. static const struct of_device_id q6core_of_match[] = {
  1788. { .compatible = "qcom,q6core-audio", },
  1789. {},
  1790. };
  1791. static struct platform_driver q6core_driver = {
  1792. .probe = q6core_probe,
  1793. .remove = q6core_remove,
  1794. .driver = {
  1795. .name = "q6core_audio",
  1796. .owner = THIS_MODULE,
  1797. .of_match_table = q6core_of_match,
  1798. .suppress_bind_attrs = true,
  1799. }
  1800. };
  1801. int __init core_init(void)
  1802. {
  1803. memset(&q6core_lcl, 0, sizeof(struct q6core_str));
  1804. init_waitqueue_head(&q6core_lcl.bus_bw_req_wait);
  1805. init_waitqueue_head(&q6core_lcl.cmd_req_wait);
  1806. init_waitqueue_head(&q6core_lcl.avcs_fwk_ver_req_wait);
  1807. init_waitqueue_head(&q6core_lcl.mdf_map_resp_wait);
  1808. init_waitqueue_head(&q6core_lcl.lpass_npa_rsc_wait);
  1809. init_waitqueue_head(&q6core_lcl.avcs_module_load_unload_wait);
  1810. q6core_lcl.cmd_resp_received_flag = FLAG_NONE;
  1811. mutex_init(&q6core_lcl.cmd_lock);
  1812. mutex_init(&q6core_lcl.ver_lock);
  1813. q6core_init_cal_data();
  1814. q6core_init_uevent_kset();
  1815. return platform_driver_register(&q6core_driver);
  1816. }
  1817. void core_exit(void)
  1818. {
  1819. mutex_destroy(&q6core_lcl.cmd_lock);
  1820. mutex_destroy(&q6core_lcl.ver_lock);
  1821. q6core_delete_cal_data();
  1822. q6core_destroy_uevent_kset();
  1823. platform_driver_unregister(&q6core_driver);
  1824. }
  1825. MODULE_DESCRIPTION("ADSP core driver");
  1826. MODULE_LICENSE("GPL v2");