q6core.c 51 KB

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