q6core.c 37 KB

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