q6core.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. default:
  255. pr_err("%s: Invalid cmd rsp[0x%x][0x%x] opcode %d\n",
  256. __func__,
  257. payload1[0], payload1[1], data->opcode);
  258. break;
  259. }
  260. break;
  261. }
  262. case RESET_EVENTS:{
  263. pr_debug("%s: Reset event received in Core service\n",
  264. __func__);
  265. /*
  266. * no reset for q6core_avcs_ver_info done as
  267. * the data will not change after SSR
  268. */
  269. apr_reset(q6core_lcl.core_handle_q);
  270. q6core_lcl.core_handle_q = NULL;
  271. break;
  272. }
  273. case AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS:
  274. payload1 = data->payload;
  275. pr_debug("%s: AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS handle %d\n",
  276. __func__, payload1[0]);
  277. q6core_lcl.mem_map_cal_handle = payload1[0];
  278. q6core_lcl.bus_bw_resp_received = 1;
  279. wake_up(&q6core_lcl.bus_bw_req_wait);
  280. break;
  281. case AVCS_CMDRSP_ADSP_EVENT_GET_STATE:
  282. payload1 = data->payload;
  283. q6core_lcl.param = payload1[0];
  284. pr_debug("%s: Received ADSP get state response 0x%x\n",
  285. __func__, q6core_lcl.param);
  286. /* ensure .param is updated prior to .bus_bw_resp_received */
  287. wmb();
  288. q6core_lcl.bus_bw_resp_received = 1;
  289. wake_up(&q6core_lcl.bus_bw_req_wait);
  290. break;
  291. case AVCS_CMDRSP_GET_LICENSE_VALIDATION_RESULT:
  292. payload1 = data->payload;
  293. pr_debug("%s: cmd = LICENSE_VALIDATION_RESULT, result = 0x%x\n",
  294. __func__, payload1[0]);
  295. q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result
  296. = payload1[0];
  297. q6core_lcl.cmd_resp_received_flag = FLAG_CMDRSP_LICENSE_RESULT;
  298. wake_up(&q6core_lcl.cmd_req_wait);
  299. break;
  300. case AVCS_CMDRSP_GET_FWK_VERSION:
  301. pr_debug("%s: Received AVCS_CMDRSP_GET_FWK_VERSION\n",
  302. __func__);
  303. payload1 = data->payload;
  304. ret = parse_fwk_version_info(payload1);
  305. if (ret < 0) {
  306. q6core_lcl.adsp_status = ret;
  307. pr_err("%s: Failed to parse payload:%d\n",
  308. __func__, ret);
  309. } else {
  310. q6core_lcl.q6core_avcs_ver_info.status =
  311. VER_QUERY_SUPPORTED;
  312. }
  313. q6core_lcl.avcs_fwk_ver_resp_received = 1;
  314. wake_up(&q6core_lcl.avcs_fwk_ver_req_wait);
  315. break;
  316. default:
  317. pr_err("%s: Message id from adsp core svc: 0x%x\n",
  318. __func__, data->opcode);
  319. if (generic_get_data) {
  320. generic_get_data->valid = 1;
  321. generic_get_data->size_in_ints =
  322. data->payload_size/sizeof(int);
  323. pr_debug("callback size = %i\n",
  324. data->payload_size);
  325. memcpy(generic_get_data->ints, data->payload,
  326. data->payload_size);
  327. q6core_lcl.bus_bw_resp_received = 1;
  328. wake_up(&q6core_lcl.bus_bw_req_wait);
  329. break;
  330. }
  331. break;
  332. }
  333. return 0;
  334. }
  335. void ocm_core_open(void)
  336. {
  337. if (q6core_lcl.core_handle_q == NULL)
  338. q6core_lcl.core_handle_q = apr_register("ADSP", "CORE",
  339. aprv2_core_fn_q, 0xFFFFFFFF, NULL);
  340. pr_debug("%s: Open_q %pK\n", __func__, q6core_lcl.core_handle_q);
  341. if (q6core_lcl.core_handle_q == NULL)
  342. pr_err("%s: Unable to register CORE\n", __func__);
  343. }
  344. struct cal_block_data *cal_utils_get_cal_block_by_key(
  345. struct cal_type_data *cal_type, uint32_t key)
  346. {
  347. struct list_head *ptr, *next;
  348. struct cal_block_data *cal_block = NULL;
  349. struct audio_cal_info_metainfo *metainfo;
  350. list_for_each_safe(ptr, next,
  351. &cal_type->cal_blocks) {
  352. cal_block = list_entry(ptr,
  353. struct cal_block_data, list);
  354. metainfo = (struct audio_cal_info_metainfo *)
  355. cal_block->cal_info;
  356. if (metainfo->nKey != key) {
  357. pr_debug("%s: metainfo key mismatch!!! found:%x, needed:%x\n",
  358. __func__, metainfo->nKey, key);
  359. } else {
  360. pr_debug("%s: metainfo key match found", __func__);
  361. return cal_block;
  362. }
  363. }
  364. return NULL;
  365. }
  366. static int q6core_send_get_avcs_fwk_ver_cmd(void)
  367. {
  368. struct apr_hdr avcs_ver_cmd;
  369. int ret;
  370. avcs_ver_cmd.hdr_field =
  371. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  372. APR_PKT_VER);
  373. avcs_ver_cmd.pkt_size = sizeof(struct apr_hdr);
  374. avcs_ver_cmd.src_port = 0;
  375. avcs_ver_cmd.dest_port = 0;
  376. avcs_ver_cmd.token = 0;
  377. avcs_ver_cmd.opcode = AVCS_CMD_GET_FWK_VERSION;
  378. q6core_lcl.adsp_status = 0;
  379. q6core_lcl.avcs_fwk_ver_resp_received = 0;
  380. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  381. (uint32_t *) &avcs_ver_cmd);
  382. if (ret < 0) {
  383. pr_err("%s: failed to send apr packet, ret=%d\n", __func__,
  384. ret);
  385. goto done;
  386. }
  387. ret = wait_event_timeout(q6core_lcl.avcs_fwk_ver_req_wait,
  388. (q6core_lcl.avcs_fwk_ver_resp_received == 1),
  389. msecs_to_jiffies(TIMEOUT_MS));
  390. if (!ret) {
  391. pr_err("%s: wait_event timeout for AVCS fwk version info\n",
  392. __func__);
  393. ret = -ETIMEDOUT;
  394. goto done;
  395. }
  396. if (q6core_lcl.adsp_status < 0) {
  397. /*
  398. * adsp_err_get_err_str expects a positive value but we store
  399. * the DSP error as negative to match the Linux error standard.
  400. * Pass in the negated value so adsp_err_get_err_str returns
  401. * the correct string.
  402. */
  403. pr_err("%s: DSP returned error[%s]\n", __func__,
  404. adsp_err_get_err_str(-q6core_lcl.adsp_status));
  405. ret = adsp_err_get_lnx_err_code(q6core_lcl.adsp_status);
  406. goto done;
  407. }
  408. ret = 0;
  409. done:
  410. return ret;
  411. }
  412. int q6core_get_service_version(uint32_t service_id,
  413. struct avcs_fwk_ver_info *ver_info,
  414. size_t size)
  415. {
  416. struct avcs_fwk_ver_info *cached_ver_info = NULL;
  417. int i;
  418. uint32_t num_services;
  419. size_t ver_size;
  420. int ret;
  421. if (ver_info == NULL) {
  422. pr_err("%s: ver_info is NULL\n", __func__);
  423. return -EINVAL;
  424. }
  425. ret = q6core_get_fwk_version_size(service_id);
  426. if (ret < 0) {
  427. pr_err("%s: Failed to get service size for service id %d with error %d\n",
  428. __func__, service_id, ret);
  429. return ret;
  430. }
  431. ver_size = ret;
  432. if (ver_size != size) {
  433. pr_err("%s: Expected size %zu and provided size %zu do not match\n",
  434. __func__, ver_size, size);
  435. return -EINVAL;
  436. }
  437. cached_ver_info = q6core_lcl.q6core_avcs_ver_info.ver_info;
  438. num_services = cached_ver_info->avcs_fwk_version.num_services;
  439. if (service_id == AVCS_SERVICE_ID_ALL) {
  440. memcpy(ver_info, cached_ver_info, ver_size);
  441. return 0;
  442. }
  443. ver_info->avcs_fwk_version = cached_ver_info->avcs_fwk_version;
  444. for (i = 0; i < num_services; i++) {
  445. if (cached_ver_info->services[i].service_id == service_id) {
  446. ver_info->services[0] = cached_ver_info->services[i];
  447. return 0;
  448. }
  449. }
  450. pr_err("%s: No service matching service ID %d\n", __func__, service_id);
  451. return -EINVAL;
  452. }
  453. EXPORT_SYMBOL(q6core_get_service_version);
  454. static int q6core_get_avcs_fwk_version(void)
  455. {
  456. int ret = 0;
  457. mutex_lock(&(q6core_lcl.ver_lock));
  458. pr_debug("%s: q6core_avcs_ver_info.status(%d)\n", __func__,
  459. q6core_lcl.q6core_avcs_ver_info.status);
  460. switch (q6core_lcl.q6core_avcs_ver_info.status) {
  461. case VER_QUERY_SUPPORTED:
  462. pr_debug("%s: AVCS FWK version query already attempted\n",
  463. __func__);
  464. break;
  465. case VER_QUERY_UNSUPPORTED:
  466. ret = -EOPNOTSUPP;
  467. break;
  468. case VER_QUERY_UNATTEMPTED:
  469. pr_debug("%s: Attempting AVCS FWK version query\n", __func__);
  470. if (q6core_is_adsp_ready()) {
  471. ret = q6core_send_get_avcs_fwk_ver_cmd();
  472. } else {
  473. pr_err("%s: ADSP is not ready to query version\n",
  474. __func__);
  475. ret = -ENODEV;
  476. }
  477. break;
  478. default:
  479. pr_err("%s: Invalid version query status %d\n", __func__,
  480. q6core_lcl.q6core_avcs_ver_info.status);
  481. ret = -EINVAL;
  482. break;
  483. }
  484. mutex_unlock(&(q6core_lcl.ver_lock));
  485. return ret;
  486. }
  487. size_t q6core_get_fwk_version_size(uint32_t service_id)
  488. {
  489. int ret = 0;
  490. uint32_t num_services;
  491. ret = q6core_get_avcs_fwk_version();
  492. if (ret)
  493. goto done;
  494. if (q6core_lcl.q6core_avcs_ver_info.ver_info != NULL) {
  495. num_services = q6core_lcl.q6core_avcs_ver_info.ver_info
  496. ->avcs_fwk_version.num_services;
  497. } else {
  498. pr_err("%s: ver_info is NULL\n", __func__);
  499. ret = -EINVAL;
  500. goto done;
  501. }
  502. ret = sizeof(struct avcs_get_fwk_version);
  503. if (service_id == AVCS_SERVICE_ID_ALL)
  504. ret += num_services * sizeof(struct avs_svc_api_info);
  505. else
  506. ret += sizeof(struct avs_svc_api_info);
  507. done:
  508. return ret;
  509. }
  510. EXPORT_SYMBOL(q6core_get_fwk_version_size);
  511. /**
  512. * q6core_get_avcs_version_per_service -
  513. * to get api version of a particular service
  514. *
  515. * @service_id: id of the service
  516. *
  517. * Returns valid version on success or error (negative value) on failure
  518. */
  519. int q6core_get_avcs_api_version_per_service(uint32_t service_id)
  520. {
  521. struct avcs_fwk_ver_info *cached_ver_info = NULL;
  522. int i;
  523. uint32_t num_services;
  524. int ret = 0;
  525. if (service_id == AVCS_SERVICE_ID_ALL)
  526. return -EINVAL;
  527. ret = q6core_get_avcs_fwk_version();
  528. if (ret < 0) {
  529. pr_err("%s: failure in getting AVCS version\n", __func__);
  530. return ret;
  531. }
  532. cached_ver_info = q6core_lcl.q6core_avcs_ver_info.ver_info;
  533. num_services = cached_ver_info->avcs_fwk_version.num_services;
  534. for (i = 0; i < num_services; i++) {
  535. if (cached_ver_info->services[i].service_id == service_id)
  536. return cached_ver_info->services[i].api_version;
  537. }
  538. pr_err("%s: No service matching service ID %d\n", __func__, service_id);
  539. return -EINVAL;
  540. }
  541. EXPORT_SYMBOL(q6core_get_avcs_api_version_per_service);
  542. /**
  543. * core_set_license -
  544. * command to set license for module
  545. *
  546. * @key: license key hash
  547. * @module_id: DSP Module ID
  548. *
  549. * Returns 0 on success or error on failure
  550. */
  551. int32_t core_set_license(uint32_t key, uint32_t module_id)
  552. {
  553. struct avcs_cmd_set_license *cmd_setl = NULL;
  554. struct cal_block_data *cal_block = NULL;
  555. int rc = 0, packet_size = 0;
  556. pr_debug("%s: key:0x%x, id:0x%x\n", __func__, key, module_id);
  557. mutex_lock(&(q6core_lcl.cmd_lock));
  558. if (q6core_lcl.cal_data[META_CAL] == NULL) {
  559. pr_err("%s: cal_data not initialized yet!!\n", __func__);
  560. rc = -EINVAL;
  561. goto cmd_unlock;
  562. }
  563. mutex_lock(&((q6core_lcl.cal_data[META_CAL])->lock));
  564. cal_block = cal_utils_get_cal_block_by_key(
  565. q6core_lcl.cal_data[META_CAL], key);
  566. if (cal_block == NULL ||
  567. cal_block->cal_data.kvaddr == NULL ||
  568. cal_block->cal_data.size <= 0) {
  569. pr_err("%s: Invalid cal block to send", __func__);
  570. rc = -EINVAL;
  571. goto cal_data_unlock;
  572. }
  573. packet_size = sizeof(struct avcs_cmd_set_license) +
  574. cal_block->cal_data.size;
  575. /*round up total packet_size to next 4 byte boundary*/
  576. packet_size = ((packet_size + 0x3)>>2)<<2;
  577. cmd_setl = kzalloc(packet_size, GFP_KERNEL);
  578. if (cmd_setl == NULL) {
  579. rc = -ENOMEM;
  580. goto cal_data_unlock;
  581. }
  582. ocm_core_open();
  583. if (q6core_lcl.core_handle_q == NULL) {
  584. pr_err("%s: apr registration for CORE failed\n", __func__);
  585. rc = -ENODEV;
  586. goto fail_cmd;
  587. }
  588. cmd_setl->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  589. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  590. cmd_setl->hdr.pkt_size = packet_size;
  591. cmd_setl->hdr.src_port = 0;
  592. cmd_setl->hdr.dest_port = 0;
  593. cmd_setl->hdr.token = 0;
  594. cmd_setl->hdr.opcode = AVCS_CMD_SET_LICENSE;
  595. cmd_setl->id = module_id;
  596. cmd_setl->overwrite = 1;
  597. cmd_setl->size = cal_block->cal_data.size;
  598. memcpy((uint8_t *)cmd_setl + sizeof(struct avcs_cmd_set_license),
  599. cal_block->cal_data.kvaddr,
  600. cal_block->cal_data.size);
  601. pr_info("%s: Set license opcode=0x%x, id =0x%x, size = %d\n",
  602. __func__, cmd_setl->hdr.opcode,
  603. cmd_setl->id, cmd_setl->size);
  604. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)cmd_setl);
  605. if (rc < 0)
  606. pr_err("%s: SET_LICENSE failed op[0x%x]rc[%d]\n",
  607. __func__, cmd_setl->hdr.opcode, rc);
  608. fail_cmd:
  609. kfree(cmd_setl);
  610. cal_data_unlock:
  611. mutex_unlock(&((q6core_lcl.cal_data[META_CAL])->lock));
  612. cmd_unlock:
  613. mutex_unlock(&(q6core_lcl.cmd_lock));
  614. return rc;
  615. }
  616. EXPORT_SYMBOL(core_set_license);
  617. /**
  618. * core_get_license_status -
  619. * command to retrieve license status for module
  620. *
  621. * @module_id: DSP Module ID
  622. *
  623. * Returns 0 on success or error on failure
  624. */
  625. int32_t core_get_license_status(uint32_t module_id)
  626. {
  627. struct avcs_cmd_get_license_validation_result get_lvr_cmd;
  628. int ret = 0;
  629. pr_debug("%s: module_id 0x%x", __func__, module_id);
  630. mutex_lock(&(q6core_lcl.cmd_lock));
  631. ocm_core_open();
  632. if (q6core_lcl.core_handle_q == NULL) {
  633. pr_err("%s: apr registration for CORE failed\n", __func__);
  634. ret = -ENODEV;
  635. goto fail_cmd;
  636. }
  637. get_lvr_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  638. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  639. get_lvr_cmd.hdr.pkt_size =
  640. sizeof(struct avcs_cmd_get_license_validation_result);
  641. get_lvr_cmd.hdr.src_port = 0;
  642. get_lvr_cmd.hdr.dest_port = 0;
  643. get_lvr_cmd.hdr.token = 0;
  644. get_lvr_cmd.hdr.opcode = AVCS_CMD_GET_LICENSE_VALIDATION_RESULT;
  645. get_lvr_cmd.id = module_id;
  646. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &get_lvr_cmd);
  647. if (ret < 0) {
  648. pr_err("%s: license_validation request failed, err %d\n",
  649. __func__, ret);
  650. ret = -EREMOTE;
  651. goto fail_cmd;
  652. }
  653. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  654. mutex_unlock(&(q6core_lcl.cmd_lock));
  655. ret = wait_event_timeout(q6core_lcl.cmd_req_wait,
  656. (q6core_lcl.cmd_resp_received_flag ==
  657. FLAG_CMDRSP_LICENSE_RESULT),
  658. msecs_to_jiffies(TIMEOUT_MS));
  659. mutex_lock(&(q6core_lcl.cmd_lock));
  660. if (!ret) {
  661. pr_err("%s: wait_event timeout for CMDRSP_LICENSE_RESULT\n",
  662. __func__);
  663. ret = -ETIME;
  664. goto fail_cmd;
  665. }
  666. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  667. ret = q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result;
  668. fail_cmd:
  669. mutex_unlock(&(q6core_lcl.cmd_lock));
  670. pr_info("%s: cmdrsp_license_result.result = 0x%x for module 0x%x\n",
  671. __func__, ret, module_id);
  672. return ret;
  673. }
  674. EXPORT_SYMBOL(core_get_license_status);
  675. /**
  676. * core_set_dolby_manufacturer_id -
  677. * command to set dolby manufacturer id
  678. *
  679. * @manufacturer_id: Dolby manufacturer id
  680. *
  681. * Returns 0 on success or error on failure
  682. */
  683. uint32_t core_set_dolby_manufacturer_id(int manufacturer_id)
  684. {
  685. struct adsp_dolby_manufacturer_id payload;
  686. int rc = 0;
  687. pr_debug("%s: manufacturer_id :%d\n", __func__, manufacturer_id);
  688. mutex_lock(&(q6core_lcl.cmd_lock));
  689. ocm_core_open();
  690. if (q6core_lcl.core_handle_q) {
  691. payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  692. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  693. payload.hdr.pkt_size =
  694. sizeof(struct adsp_dolby_manufacturer_id);
  695. payload.hdr.src_port = 0;
  696. payload.hdr.dest_port = 0;
  697. payload.hdr.token = 0;
  698. payload.hdr.opcode = ADSP_CMD_SET_DOLBY_MANUFACTURER_ID;
  699. payload.manufacturer_id = manufacturer_id;
  700. pr_debug("%s: Send Dolby security opcode=0x%x manufacturer ID = %d\n",
  701. __func__,
  702. payload.hdr.opcode, payload.manufacturer_id);
  703. rc = apr_send_pkt(q6core_lcl.core_handle_q,
  704. (uint32_t *)&payload);
  705. if (rc < 0)
  706. pr_err("%s: SET_DOLBY_MANUFACTURER_ID failed op[0x%x]rc[%d]\n",
  707. __func__, payload.hdr.opcode, rc);
  708. }
  709. mutex_unlock(&(q6core_lcl.cmd_lock));
  710. return rc;
  711. }
  712. EXPORT_SYMBOL(core_set_dolby_manufacturer_id);
  713. /**
  714. * q6core_is_adsp_ready - check adsp ready status
  715. *
  716. * Returns true if adsp is ready otherwise returns false
  717. */
  718. bool q6core_is_adsp_ready(void)
  719. {
  720. int rc = 0;
  721. bool ret = false;
  722. struct apr_hdr hdr;
  723. pr_debug("%s: enter\n", __func__);
  724. memset(&hdr, 0, sizeof(hdr));
  725. hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  726. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  727. hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE, 0);
  728. hdr.opcode = AVCS_CMD_ADSP_EVENT_GET_STATE;
  729. mutex_lock(&(q6core_lcl.cmd_lock));
  730. ocm_core_open();
  731. if (q6core_lcl.core_handle_q) {
  732. q6core_lcl.bus_bw_resp_received = 0;
  733. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)&hdr);
  734. if (rc < 0) {
  735. pr_err("%s: Get ADSP state APR packet send event %d\n",
  736. __func__, rc);
  737. goto bail;
  738. }
  739. rc = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  740. (q6core_lcl.bus_bw_resp_received == 1),
  741. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  742. if (rc > 0 && q6core_lcl.bus_bw_resp_received) {
  743. /* ensure to read updated param by callback thread */
  744. rmb();
  745. ret = !!q6core_lcl.param;
  746. }
  747. }
  748. bail:
  749. pr_debug("%s: leave, rc %d, adsp ready %d\n", __func__, rc, ret);
  750. mutex_unlock(&(q6core_lcl.cmd_lock));
  751. return ret;
  752. }
  753. EXPORT_SYMBOL(q6core_is_adsp_ready);
  754. static int q6core_map_memory_regions(phys_addr_t *buf_add, uint32_t mempool_id,
  755. uint32_t *bufsz, uint32_t bufcnt, uint32_t *map_handle)
  756. {
  757. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  758. struct avs_shared_map_region_payload *mregions = NULL;
  759. void *mmap_region_cmd = NULL;
  760. void *payload = NULL;
  761. int ret = 0;
  762. int i = 0;
  763. int cmd_size = 0;
  764. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  765. + sizeof(struct avs_shared_map_region_payload)
  766. * bufcnt;
  767. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  768. if (mmap_region_cmd == NULL)
  769. return -ENOMEM;
  770. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  771. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  772. APR_HDR_LEN(APR_HDR_SIZE),
  773. APR_PKT_VER);
  774. mmap_regions->hdr.pkt_size = cmd_size;
  775. mmap_regions->hdr.src_port = 0;
  776. mmap_regions->hdr.dest_port = 0;
  777. mmap_regions->hdr.token = 0;
  778. mmap_regions->hdr.opcode = AVCS_CMD_SHARED_MEM_MAP_REGIONS;
  779. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
  780. mmap_regions->num_regions = bufcnt & 0x00ff;
  781. mmap_regions->property_flag = 0x00;
  782. payload = ((u8 *) mmap_region_cmd +
  783. sizeof(struct avs_cmd_shared_mem_map_regions));
  784. mregions = (struct avs_shared_map_region_payload *)payload;
  785. for (i = 0; i < bufcnt; i++) {
  786. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  787. mregions->shm_addr_msw =
  788. msm_audio_populate_upper_32_bits(buf_add[i]);
  789. mregions->mem_size_bytes = bufsz[i];
  790. ++mregions;
  791. }
  792. pr_debug("%s: sending memory map, addr %pK, size %d, bufcnt = %d\n",
  793. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  794. *map_handle = 0;
  795. q6core_lcl.bus_bw_resp_received = 0;
  796. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  797. mmap_regions);
  798. if (ret < 0) {
  799. pr_err("%s: mmap regions failed %d\n",
  800. __func__, ret);
  801. ret = -EINVAL;
  802. goto done;
  803. }
  804. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  805. (q6core_lcl.bus_bw_resp_received == 1),
  806. msecs_to_jiffies(TIMEOUT_MS));
  807. if (!ret) {
  808. pr_err("%s: timeout. waited for memory map\n", __func__);
  809. ret = -ETIME;
  810. goto done;
  811. }
  812. *map_handle = q6core_lcl.mem_map_cal_handle;
  813. done:
  814. kfree(mmap_region_cmd);
  815. return ret;
  816. }
  817. static int q6core_memory_unmap_regions(uint32_t mem_map_handle)
  818. {
  819. struct avs_cmd_shared_mem_unmap_regions unmap_regions;
  820. int ret = 0;
  821. memset(&unmap_regions, 0, sizeof(unmap_regions));
  822. unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  823. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  824. unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
  825. unmap_regions.hdr.src_svc = APR_SVC_ADSP_CORE;
  826. unmap_regions.hdr.src_domain = APR_DOMAIN_APPS;
  827. unmap_regions.hdr.src_port = 0;
  828. unmap_regions.hdr.dest_svc = APR_SVC_ADSP_CORE;
  829. unmap_regions.hdr.dest_domain = APR_DOMAIN_ADSP;
  830. unmap_regions.hdr.dest_port = 0;
  831. unmap_regions.hdr.token = 0;
  832. unmap_regions.hdr.opcode = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS;
  833. unmap_regions.mem_map_handle = mem_map_handle;
  834. q6core_lcl.bus_bw_resp_received = 0;
  835. pr_debug("%s: unmap regions map handle %d\n",
  836. __func__, mem_map_handle);
  837. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  838. &unmap_regions);
  839. if (ret < 0) {
  840. pr_err("%s: unmap regions failed %d\n",
  841. __func__, ret);
  842. ret = -EINVAL;
  843. goto done;
  844. }
  845. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  846. (q6core_lcl.bus_bw_resp_received == 1),
  847. msecs_to_jiffies(TIMEOUT_MS));
  848. if (!ret) {
  849. pr_err("%s: timeout. waited for memory_unmap\n",
  850. __func__);
  851. ret = -ETIME;
  852. goto done;
  853. }
  854. done:
  855. return ret;
  856. }
  857. static int q6core_dereg_all_custom_topologies(void)
  858. {
  859. int ret = 0;
  860. struct avcs_cmd_deregister_topologies dereg_top;
  861. memset(&dereg_top, 0, sizeof(dereg_top));
  862. dereg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  863. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  864. dereg_top.hdr.pkt_size = sizeof(dereg_top);
  865. dereg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  866. dereg_top.hdr.src_domain = APR_DOMAIN_APPS;
  867. dereg_top.hdr.src_port = 0;
  868. dereg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  869. dereg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  870. dereg_top.hdr.dest_port = 0;
  871. dereg_top.hdr.token = 0;
  872. dereg_top.hdr.opcode = AVCS_CMD_DEREGISTER_TOPOLOGIES;
  873. dereg_top.payload_addr_lsw = 0;
  874. dereg_top.payload_addr_msw = 0;
  875. dereg_top.mem_map_handle = 0;
  876. dereg_top.payload_size = 0;
  877. dereg_top.mode = AVCS_MODE_DEREGISTER_ALL_CUSTOM_TOPOLOGIES;
  878. q6core_lcl.bus_bw_resp_received = 0;
  879. pr_debug("%s: Deregister topologies mode %d\n",
  880. __func__, dereg_top.mode);
  881. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &dereg_top);
  882. if (ret < 0) {
  883. pr_err("%s: Deregister topologies failed %d\n",
  884. __func__, ret);
  885. goto done;
  886. }
  887. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  888. (q6core_lcl.bus_bw_resp_received == 1),
  889. msecs_to_jiffies(TIMEOUT_MS));
  890. if (!ret) {
  891. pr_err("%s: wait_event timeout for Deregister topologies\n",
  892. __func__);
  893. goto done;
  894. }
  895. done:
  896. return ret;
  897. }
  898. static int q6core_send_custom_topologies(void)
  899. {
  900. int ret = 0;
  901. int ret2 = 0;
  902. struct cal_block_data *cal_block = NULL;
  903. struct avcs_cmd_register_topologies reg_top;
  904. if (!q6core_is_adsp_ready()) {
  905. pr_err("%s: ADSP is not ready!\n", __func__);
  906. return -ENODEV;
  907. }
  908. memset(&reg_top, 0, sizeof(reg_top));
  909. mutex_lock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  910. mutex_lock(&q6core_lcl.cmd_lock);
  911. cal_block = cal_utils_get_only_cal_block(
  912. q6core_lcl.cal_data[CUST_TOP_CAL]);
  913. if (cal_block == NULL) {
  914. pr_debug("%s: cal block is NULL!\n", __func__);
  915. goto unlock;
  916. }
  917. if (cal_block->cal_data.size <= 0) {
  918. pr_debug("%s: cal size is %zd not sending\n",
  919. __func__, cal_block->cal_data.size);
  920. goto unlock;
  921. }
  922. q6core_dereg_all_custom_topologies();
  923. ret = q6core_map_memory_regions(&cal_block->cal_data.paddr, 0,
  924. (uint32_t *)&cal_block->map_data.map_size, 1,
  925. &cal_block->map_data.q6map_handle);
  926. if (!ret) {
  927. pr_err("%s: q6core_map_memory_regions failed\n", __func__);
  928. goto unlock;
  929. }
  930. reg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  931. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  932. reg_top.hdr.pkt_size = sizeof(reg_top);
  933. reg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  934. reg_top.hdr.src_domain = APR_DOMAIN_APPS;
  935. reg_top.hdr.src_port = 0;
  936. reg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  937. reg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  938. reg_top.hdr.dest_port = 0;
  939. reg_top.hdr.token = 0;
  940. reg_top.hdr.opcode = AVCS_CMD_REGISTER_TOPOLOGIES;
  941. reg_top.payload_addr_lsw =
  942. lower_32_bits(cal_block->cal_data.paddr);
  943. reg_top.payload_addr_msw =
  944. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  945. reg_top.mem_map_handle = cal_block->map_data.q6map_handle;
  946. reg_top.payload_size = cal_block->cal_data.size;
  947. q6core_lcl.adsp_status = 0;
  948. q6core_lcl.bus_bw_resp_received = 0;
  949. pr_debug("%s: Register topologies addr %pK, size %zd, map handle %d\n",
  950. __func__, &cal_block->cal_data.paddr, cal_block->cal_data.size,
  951. cal_block->map_data.q6map_handle);
  952. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &reg_top);
  953. if (ret < 0) {
  954. pr_err("%s: Register topologies failed %d\n",
  955. __func__, ret);
  956. goto unmap;
  957. }
  958. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  959. (q6core_lcl.bus_bw_resp_received == 1),
  960. msecs_to_jiffies(TIMEOUT_MS));
  961. if (!ret) {
  962. pr_err("%s: wait_event timeout for Register topologies\n",
  963. __func__);
  964. goto unmap;
  965. }
  966. if (q6core_lcl.adsp_status < 0)
  967. ret = q6core_lcl.adsp_status;
  968. unmap:
  969. ret2 = q6core_memory_unmap_regions(cal_block->map_data.q6map_handle);
  970. if (!ret2) {
  971. pr_err("%s: q6core_memory_unmap_regions failed for map handle %d\n",
  972. __func__, cal_block->map_data.q6map_handle);
  973. ret = ret2;
  974. goto unlock;
  975. }
  976. unlock:
  977. mutex_unlock(&q6core_lcl.cmd_lock);
  978. mutex_unlock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  979. return ret;
  980. }
  981. static int get_cal_type_index(int32_t cal_type)
  982. {
  983. int ret = -EINVAL;
  984. switch (cal_type) {
  985. case AUDIO_CORE_METAINFO_CAL_TYPE:
  986. ret = META_CAL;
  987. break;
  988. case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
  989. ret = CUST_TOP_CAL;
  990. break;
  991. default:
  992. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  993. }
  994. return ret;
  995. }
  996. static int q6core_alloc_cal(int32_t cal_type,
  997. size_t data_size, void *data)
  998. {
  999. int ret = 0;
  1000. int cal_index;
  1001. cal_index = get_cal_type_index(cal_type);
  1002. if (cal_index < 0) {
  1003. pr_err("%s: could not get cal index %d!\n",
  1004. __func__, cal_index);
  1005. ret = -EINVAL;
  1006. goto done;
  1007. }
  1008. ret = cal_utils_alloc_cal(data_size, data,
  1009. q6core_lcl.cal_data[cal_index], 0, NULL);
  1010. if (ret < 0) {
  1011. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  1012. __func__, ret, cal_type);
  1013. goto done;
  1014. }
  1015. done:
  1016. return ret;
  1017. }
  1018. static int q6core_dealloc_cal(int32_t cal_type,
  1019. size_t data_size, void *data)
  1020. {
  1021. int ret = 0;
  1022. int cal_index;
  1023. cal_index = get_cal_type_index(cal_type);
  1024. if (cal_index < 0) {
  1025. pr_err("%s: could not get cal index %d!\n",
  1026. __func__, cal_index);
  1027. ret = -EINVAL;
  1028. goto done;
  1029. }
  1030. ret = cal_utils_dealloc_cal(data_size, data,
  1031. q6core_lcl.cal_data[cal_index]);
  1032. if (ret < 0) {
  1033. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  1034. __func__, ret, cal_type);
  1035. goto done;
  1036. }
  1037. done:
  1038. return ret;
  1039. }
  1040. static int q6core_set_cal(int32_t cal_type,
  1041. size_t data_size, void *data)
  1042. {
  1043. int ret = 0;
  1044. int cal_index;
  1045. cal_index = get_cal_type_index(cal_type);
  1046. if (cal_index < 0) {
  1047. pr_err("%s: could not get cal index %d!\n",
  1048. __func__, cal_index);
  1049. ret = -EINVAL;
  1050. goto done;
  1051. }
  1052. ret = cal_utils_set_cal(data_size, data,
  1053. q6core_lcl.cal_data[cal_index], 0, NULL);
  1054. if (ret < 0) {
  1055. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  1056. __func__, ret, cal_type);
  1057. goto done;
  1058. }
  1059. if (cal_index == CUST_TOP_CAL)
  1060. ret = q6core_send_custom_topologies();
  1061. done:
  1062. return ret;
  1063. }
  1064. static void q6core_delete_cal_data(void)
  1065. {
  1066. pr_debug("%s:\n", __func__);
  1067. cal_utils_destroy_cal_types(CORE_MAX_CAL, q6core_lcl.cal_data);
  1068. }
  1069. static int q6core_init_cal_data(void)
  1070. {
  1071. int ret = 0;
  1072. struct cal_type_info cal_type_info[] = {
  1073. {{AUDIO_CORE_METAINFO_CAL_TYPE,
  1074. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  1075. q6core_set_cal, NULL, NULL} },
  1076. {NULL, NULL, cal_utils_match_buf_num} },
  1077. {{CORE_CUSTOM_TOPOLOGIES_CAL_TYPE,
  1078. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  1079. q6core_set_cal, NULL, NULL} },
  1080. {NULL, NULL, cal_utils_match_buf_num} }
  1081. };
  1082. pr_debug("%s:\n", __func__);
  1083. ret = cal_utils_create_cal_types(CORE_MAX_CAL,
  1084. q6core_lcl.cal_data, cal_type_info);
  1085. if (ret < 0) {
  1086. pr_err("%s: could not create cal type!\n",
  1087. __func__);
  1088. goto err;
  1089. }
  1090. return ret;
  1091. err:
  1092. q6core_delete_cal_data();
  1093. return ret;
  1094. }
  1095. static int q6core_probe(struct platform_device *pdev)
  1096. {
  1097. unsigned long timeout;
  1098. int adsp_ready = 0, rc;
  1099. timeout = jiffies +
  1100. msecs_to_jiffies(ADSP_STATE_READY_TIMEOUT_MS);
  1101. do {
  1102. if (!adsp_ready) {
  1103. adsp_ready = q6core_is_adsp_ready();
  1104. dev_dbg(&pdev->dev, "%s: ADSP Audio is %s\n", __func__,
  1105. adsp_ready ? "ready" : "not ready");
  1106. }
  1107. if (adsp_ready)
  1108. break;
  1109. /*
  1110. * ADSP will be coming up after loading (PD up event) and
  1111. * it might not be fully up when the control reaches
  1112. * here. So, wait for 50msec before checking ADSP state
  1113. */
  1114. msleep(50);
  1115. } while (time_after(timeout, jiffies));
  1116. if (!adsp_ready) {
  1117. dev_err(&pdev->dev, "%s: Timeout. ADSP Audio is %s\n",
  1118. __func__,
  1119. adsp_ready ? "ready" : "not ready");
  1120. return -ETIMEDOUT;
  1121. }
  1122. rc = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  1123. if (rc) {
  1124. dev_err(&pdev->dev, "%s: failed to add child nodes, rc=%d\n",
  1125. __func__, rc);
  1126. return -EINVAL;
  1127. }
  1128. dev_dbg(&pdev->dev, "%s: added child node\n", __func__);
  1129. return 0;
  1130. }
  1131. static int q6core_remove(struct platform_device *pdev)
  1132. {
  1133. of_platform_depopulate(&pdev->dev);
  1134. return 0;
  1135. }
  1136. static const struct of_device_id q6core_of_match[] = {
  1137. { .compatible = "qcom,q6core-audio", },
  1138. {},
  1139. };
  1140. static struct platform_driver q6core_driver = {
  1141. .probe = q6core_probe,
  1142. .remove = q6core_remove,
  1143. .driver = {
  1144. .name = "q6core_audio",
  1145. .owner = THIS_MODULE,
  1146. .of_match_table = q6core_of_match,
  1147. }
  1148. };
  1149. int __init core_init(void)
  1150. {
  1151. memset(&q6core_lcl, 0, sizeof(struct q6core_str));
  1152. init_waitqueue_head(&q6core_lcl.bus_bw_req_wait);
  1153. init_waitqueue_head(&q6core_lcl.cmd_req_wait);
  1154. init_waitqueue_head(&q6core_lcl.avcs_fwk_ver_req_wait);
  1155. q6core_lcl.cmd_resp_received_flag = FLAG_NONE;
  1156. mutex_init(&q6core_lcl.cmd_lock);
  1157. mutex_init(&q6core_lcl.ver_lock);
  1158. q6core_init_cal_data();
  1159. q6core_init_uevent_kset();
  1160. return platform_driver_register(&q6core_driver);
  1161. }
  1162. void core_exit(void)
  1163. {
  1164. mutex_destroy(&q6core_lcl.cmd_lock);
  1165. mutex_destroy(&q6core_lcl.ver_lock);
  1166. q6core_delete_cal_data();
  1167. q6core_destroy_uevent_kset();
  1168. platform_driver_unregister(&q6core_driver);
  1169. }
  1170. MODULE_DESCRIPTION("ADSP core driver");
  1171. MODULE_LICENSE("GPL v2");