q6core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /* Copyright (c) 2012-2017, 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/string.h>
  15. #include <linux/types.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/mutex.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <dsp/q6core.h>
  21. #include <dsp/audio_cal_utils.h>
  22. #include <dsp/apr_audio-v2.h>
  23. #include <ipc/apr.h>
  24. #include "adsp_err.h"
  25. #define TIMEOUT_MS 1000
  26. /*
  27. * AVS bring up in the modem is optimitized for the new
  28. * Sub System Restart design and 100 milliseconds timeout
  29. * is sufficient to make sure the Q6 will be ready.
  30. */
  31. #define Q6_READY_TIMEOUT_MS 100
  32. enum {
  33. META_CAL,
  34. CUST_TOP_CAL,
  35. CORE_MAX_CAL
  36. };
  37. enum ver_query_status {
  38. VER_QUERY_UNATTEMPTED,
  39. VER_QUERY_UNSUPPORTED,
  40. VER_QUERY_SUPPORTED
  41. };
  42. struct q6core_avcs_ver_info {
  43. enum ver_query_status status;
  44. struct avcs_fwk_ver_info *ver_info;
  45. };
  46. struct q6core_str {
  47. struct apr_svc *core_handle_q;
  48. wait_queue_head_t bus_bw_req_wait;
  49. wait_queue_head_t cmd_req_wait;
  50. wait_queue_head_t avcs_fwk_ver_req_wait;
  51. u32 bus_bw_resp_received;
  52. enum cmd_flags {
  53. FLAG_NONE,
  54. FLAG_CMDRSP_LICENSE_RESULT
  55. } cmd_resp_received_flag;
  56. u32 avcs_fwk_ver_resp_received;
  57. struct mutex cmd_lock;
  58. struct mutex ver_lock;
  59. union {
  60. struct avcs_cmdrsp_get_license_validation_result
  61. cmdrsp_license_result;
  62. } cmd_resp_payload;
  63. u32 param;
  64. struct cal_type_data *cal_data[CORE_MAX_CAL];
  65. uint32_t mem_map_cal_handle;
  66. int32_t adsp_status;
  67. struct q6core_avcs_ver_info q6core_avcs_ver_info;
  68. };
  69. static struct q6core_str q6core_lcl;
  70. struct generic_get_data_ {
  71. int valid;
  72. int size_in_ints;
  73. int ints[];
  74. };
  75. static struct generic_get_data_ *generic_get_data;
  76. static int parse_fwk_version_info(uint32_t *payload)
  77. {
  78. size_t ver_size;
  79. int num_services;
  80. pr_debug("%s: Payload info num services %d\n",
  81. __func__, payload[4]);
  82. /*
  83. * payload1[4] is the number of services running on DSP
  84. * Based on this info, we copy the payload into core
  85. * avcs version info structure.
  86. */
  87. num_services = payload[4];
  88. if (num_services > VSS_MAX_AVCS_NUM_SERVICES) {
  89. pr_err("%s: num_services: %d greater than max services: %d\n",
  90. __func__, num_services, VSS_MAX_AVCS_NUM_SERVICES);
  91. return -EINVAL;
  92. }
  93. /*
  94. * Dynamically allocate memory for all
  95. * the services based on num_services
  96. */
  97. ver_size = sizeof(struct avcs_get_fwk_version) +
  98. num_services * sizeof(struct avs_svc_api_info);
  99. if (q6core_lcl.q6core_avcs_ver_info.ver_info != NULL)
  100. pr_warn("%s: Version info is not NULL\n", __func__);
  101. q6core_lcl.q6core_avcs_ver_info.ver_info =
  102. kzalloc(ver_size, GFP_ATOMIC);
  103. if (q6core_lcl.q6core_avcs_ver_info.ver_info == NULL)
  104. return -ENOMEM;
  105. memcpy(q6core_lcl.q6core_avcs_ver_info.ver_info, (uint8_t *) payload,
  106. ver_size);
  107. return 0;
  108. }
  109. static int32_t aprv2_core_fn_q(struct apr_client_data *data, void *priv)
  110. {
  111. uint32_t *payload1;
  112. int ret = 0;
  113. if (data == NULL) {
  114. pr_err("%s: data argument is null\n", __func__);
  115. return -EINVAL;
  116. }
  117. pr_debug("%s: core msg: payload len = %u, apr resp opcode = 0x%x\n",
  118. __func__,
  119. data->payload_size, data->opcode);
  120. switch (data->opcode) {
  121. case APR_BASIC_RSP_RESULT:{
  122. if (data->payload_size == 0) {
  123. pr_err("%s: APR_BASIC_RSP_RESULT No Payload ",
  124. __func__);
  125. return 0;
  126. }
  127. payload1 = data->payload;
  128. switch (payload1[0]) {
  129. case AVCS_CMD_SHARED_MEM_UNMAP_REGIONS:
  130. pr_debug("%s: Cmd = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS status[0x%x]\n",
  131. __func__, payload1[1]);
  132. q6core_lcl.bus_bw_resp_received = 1;
  133. wake_up(&q6core_lcl.bus_bw_req_wait);
  134. break;
  135. case AVCS_CMD_SHARED_MEM_MAP_REGIONS:
  136. pr_debug("%s: Cmd = AVCS_CMD_SHARED_MEM_MAP_REGIONS status[0x%x]\n",
  137. __func__, payload1[1]);
  138. q6core_lcl.bus_bw_resp_received = 1;
  139. wake_up(&q6core_lcl.bus_bw_req_wait);
  140. break;
  141. case AVCS_CMD_REGISTER_TOPOLOGIES:
  142. pr_debug("%s: Cmd = AVCS_CMD_REGISTER_TOPOLOGIES status[0x%x]\n",
  143. __func__, payload1[1]);
  144. /* -ADSP status to match Linux error standard */
  145. q6core_lcl.adsp_status = -payload1[1];
  146. q6core_lcl.bus_bw_resp_received = 1;
  147. wake_up(&q6core_lcl.bus_bw_req_wait);
  148. break;
  149. case AVCS_CMD_DEREGISTER_TOPOLOGIES:
  150. pr_debug("%s: Cmd = AVCS_CMD_DEREGISTER_TOPOLOGIES status[0x%x]\n",
  151. __func__, payload1[1]);
  152. q6core_lcl.bus_bw_resp_received = 1;
  153. wake_up(&q6core_lcl.bus_bw_req_wait);
  154. break;
  155. case AVCS_CMD_GET_FWK_VERSION:
  156. pr_debug("%s: Cmd = AVCS_CMD_GET_FWK_VERSION status[%s]\n",
  157. __func__, adsp_err_get_err_str(payload1[1]));
  158. /* ADSP status to match Linux error standard */
  159. q6core_lcl.adsp_status = -payload1[1];
  160. if (payload1[1] == ADSP_EUNSUPPORTED)
  161. q6core_lcl.q6core_avcs_ver_info.status =
  162. VER_QUERY_UNSUPPORTED;
  163. q6core_lcl.avcs_fwk_ver_resp_received = 1;
  164. wake_up(&q6core_lcl.avcs_fwk_ver_req_wait);
  165. break;
  166. default:
  167. pr_err("%s: Invalid cmd rsp[0x%x][0x%x] opcode %d\n",
  168. __func__,
  169. payload1[0], payload1[1], data->opcode);
  170. break;
  171. }
  172. break;
  173. }
  174. case RESET_EVENTS:{
  175. pr_debug("%s: Reset event received in Core service\n",
  176. __func__);
  177. /*
  178. * no reset for q6core_avcs_ver_info done as
  179. * the data will not change after SSR
  180. */
  181. apr_reset(q6core_lcl.core_handle_q);
  182. q6core_lcl.core_handle_q = NULL;
  183. break;
  184. }
  185. case AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS:
  186. payload1 = data->payload;
  187. pr_debug("%s: AVCS_CMDRSP_SHARED_MEM_MAP_REGIONS handle %d\n",
  188. __func__, payload1[0]);
  189. q6core_lcl.mem_map_cal_handle = payload1[0];
  190. q6core_lcl.bus_bw_resp_received = 1;
  191. wake_up(&q6core_lcl.bus_bw_req_wait);
  192. break;
  193. case AVCS_CMDRSP_ADSP_EVENT_GET_STATE:
  194. payload1 = data->payload;
  195. q6core_lcl.param = payload1[0];
  196. pr_debug("%s: Received ADSP get state response 0x%x\n",
  197. __func__, q6core_lcl.param);
  198. /* ensure .param is updated prior to .bus_bw_resp_received */
  199. wmb();
  200. q6core_lcl.bus_bw_resp_received = 1;
  201. wake_up(&q6core_lcl.bus_bw_req_wait);
  202. break;
  203. case AVCS_CMDRSP_GET_LICENSE_VALIDATION_RESULT:
  204. payload1 = data->payload;
  205. pr_debug("%s: cmd = LICENSE_VALIDATION_RESULT, result = 0x%x\n",
  206. __func__, payload1[0]);
  207. q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result
  208. = payload1[0];
  209. q6core_lcl.cmd_resp_received_flag = FLAG_CMDRSP_LICENSE_RESULT;
  210. wake_up(&q6core_lcl.cmd_req_wait);
  211. break;
  212. case AVCS_CMDRSP_GET_FWK_VERSION:
  213. pr_debug("%s: Received AVCS_CMDRSP_GET_FWK_VERSION\n",
  214. __func__);
  215. payload1 = data->payload;
  216. q6core_lcl.q6core_avcs_ver_info.status = VER_QUERY_SUPPORTED;
  217. q6core_lcl.avcs_fwk_ver_resp_received = 1;
  218. ret = parse_fwk_version_info(payload1);
  219. if (ret < 0)
  220. pr_err("%s: Failed to parse payload:%d\n",
  221. __func__, ret);
  222. wake_up(&q6core_lcl.avcs_fwk_ver_req_wait);
  223. break;
  224. default:
  225. pr_err("%s: Message id from adsp core svc: 0x%x\n",
  226. __func__, data->opcode);
  227. if (generic_get_data) {
  228. generic_get_data->valid = 1;
  229. generic_get_data->size_in_ints =
  230. data->payload_size/sizeof(int);
  231. pr_debug("callback size = %i\n",
  232. data->payload_size);
  233. memcpy(generic_get_data->ints, data->payload,
  234. data->payload_size);
  235. q6core_lcl.bus_bw_resp_received = 1;
  236. wake_up(&q6core_lcl.bus_bw_req_wait);
  237. break;
  238. }
  239. break;
  240. }
  241. return 0;
  242. }
  243. void ocm_core_open(void)
  244. {
  245. if (q6core_lcl.core_handle_q == NULL)
  246. q6core_lcl.core_handle_q = apr_register("ADSP", "CORE",
  247. aprv2_core_fn_q, 0xFFFFFFFF, NULL);
  248. pr_debug("%s: Open_q %pK\n", __func__, q6core_lcl.core_handle_q);
  249. if (q6core_lcl.core_handle_q == NULL)
  250. pr_err("%s: Unable to register CORE\n", __func__);
  251. }
  252. struct cal_block_data *cal_utils_get_cal_block_by_key(
  253. struct cal_type_data *cal_type, uint32_t key)
  254. {
  255. struct list_head *ptr, *next;
  256. struct cal_block_data *cal_block = NULL;
  257. struct audio_cal_info_metainfo *metainfo;
  258. list_for_each_safe(ptr, next,
  259. &cal_type->cal_blocks) {
  260. cal_block = list_entry(ptr,
  261. struct cal_block_data, list);
  262. metainfo = (struct audio_cal_info_metainfo *)
  263. cal_block->cal_info;
  264. if (metainfo->nKey != key) {
  265. pr_debug("%s: metainfo key mismatch!!! found:%x, needed:%x\n",
  266. __func__, metainfo->nKey, key);
  267. } else {
  268. pr_debug("%s: metainfo key match found", __func__);
  269. return cal_block;
  270. }
  271. }
  272. return NULL;
  273. }
  274. static int q6core_send_get_avcs_fwk_ver_cmd(void)
  275. {
  276. struct apr_hdr avcs_ver_cmd;
  277. int ret;
  278. avcs_ver_cmd.hdr_field =
  279. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  280. APR_PKT_VER);
  281. avcs_ver_cmd.pkt_size = sizeof(struct apr_hdr);
  282. avcs_ver_cmd.src_port = 0;
  283. avcs_ver_cmd.dest_port = 0;
  284. avcs_ver_cmd.token = 0;
  285. avcs_ver_cmd.opcode = AVCS_CMD_GET_FWK_VERSION;
  286. q6core_lcl.adsp_status = 0;
  287. q6core_lcl.avcs_fwk_ver_resp_received = 0;
  288. ret = apr_send_pkt(q6core_lcl.core_handle_q,
  289. (uint32_t *) &avcs_ver_cmd);
  290. if (ret < 0) {
  291. pr_err("%s: failed to send apr packet, ret=%d\n", __func__,
  292. ret);
  293. goto done;
  294. }
  295. ret = wait_event_timeout(q6core_lcl.avcs_fwk_ver_req_wait,
  296. (q6core_lcl.avcs_fwk_ver_resp_received == 1),
  297. msecs_to_jiffies(TIMEOUT_MS));
  298. if (!ret) {
  299. pr_err("%s: wait_event timeout for AVCS fwk version info\n",
  300. __func__);
  301. ret = -ETIMEDOUT;
  302. goto done;
  303. }
  304. if (q6core_lcl.adsp_status < 0) {
  305. /*
  306. * adsp_err_get_err_str expects a positive value but we store
  307. * the DSP error as negative to match the Linux error standard.
  308. * Pass in the negated value so adsp_err_get_err_str returns
  309. * the correct string.
  310. */
  311. pr_err("%s: DSP returned error[%s]\n", __func__,
  312. adsp_err_get_err_str(-q6core_lcl.adsp_status));
  313. ret = adsp_err_get_lnx_err_code(q6core_lcl.adsp_status);
  314. goto done;
  315. }
  316. ret = 0;
  317. done:
  318. return ret;
  319. }
  320. int q6core_get_service_version(uint32_t service_id,
  321. struct avcs_fwk_ver_info *ver_info,
  322. size_t size)
  323. {
  324. struct avcs_fwk_ver_info *cached_ver_info = NULL;
  325. int i;
  326. uint32_t num_services;
  327. size_t ver_size;
  328. int ret;
  329. if (ver_info == NULL) {
  330. pr_err("%s: ver_info is NULL\n", __func__);
  331. return -EINVAL;
  332. }
  333. ret = q6core_get_fwk_version_size(service_id);
  334. if (ret < 0) {
  335. pr_err("%s: Failed to get service size for service id %d with error %d\n",
  336. __func__, service_id, ret);
  337. return ret;
  338. }
  339. ver_size = ret;
  340. if (ver_size != size) {
  341. pr_err("%s: Expected size %zu and provided size %zu do not match\n",
  342. __func__, ver_size, size);
  343. return -EINVAL;
  344. }
  345. cached_ver_info = q6core_lcl.q6core_avcs_ver_info.ver_info;
  346. num_services = cached_ver_info->avcs_fwk_version.num_services;
  347. if (service_id == AVCS_SERVICE_ID_ALL) {
  348. memcpy(ver_info, cached_ver_info, ver_size);
  349. return 0;
  350. }
  351. ver_info->avcs_fwk_version = cached_ver_info->avcs_fwk_version;
  352. for (i = 0; i < num_services; i++) {
  353. if (cached_ver_info->services[i].service_id == service_id) {
  354. ver_info->services[0] = cached_ver_info->services[i];
  355. return 0;
  356. }
  357. }
  358. pr_err("%s: No service matching service ID %d\n", __func__, service_id);
  359. return -EINVAL;
  360. }
  361. EXPORT_SYMBOL(q6core_get_service_version);
  362. size_t q6core_get_fwk_version_size(uint32_t service_id)
  363. {
  364. int ret = 0;
  365. uint32_t num_services;
  366. mutex_lock(&(q6core_lcl.ver_lock));
  367. pr_debug("%s: q6core_avcs_ver_info.status(%d)\n", __func__,
  368. q6core_lcl.q6core_avcs_ver_info.status);
  369. switch (q6core_lcl.q6core_avcs_ver_info.status) {
  370. case VER_QUERY_SUPPORTED:
  371. pr_debug("%s: AVCS FWK version query already attempted\n",
  372. __func__);
  373. break;
  374. case VER_QUERY_UNSUPPORTED:
  375. ret = -EOPNOTSUPP;
  376. break;
  377. case VER_QUERY_UNATTEMPTED:
  378. pr_debug("%s: Attempting AVCS FWK version query\n", __func__);
  379. if (q6core_is_adsp_ready()) {
  380. ret = q6core_send_get_avcs_fwk_ver_cmd();
  381. } else {
  382. pr_err("%s: ADSP is not ready to query version\n",
  383. __func__);
  384. ret = -ENODEV;
  385. }
  386. break;
  387. default:
  388. pr_err("%s: Invalid version query status %d\n", __func__,
  389. q6core_lcl.q6core_avcs_ver_info.status);
  390. ret = -EINVAL;
  391. break;
  392. }
  393. mutex_unlock(&(q6core_lcl.ver_lock));
  394. if (ret)
  395. goto done;
  396. num_services = q6core_lcl.q6core_avcs_ver_info.ver_info
  397. ->avcs_fwk_version.num_services;
  398. ret = sizeof(struct avcs_get_fwk_version);
  399. if (service_id == AVCS_SERVICE_ID_ALL)
  400. ret += num_services * sizeof(struct avs_svc_api_info);
  401. else
  402. ret += sizeof(struct avs_svc_api_info);
  403. done:
  404. return ret;
  405. }
  406. EXPORT_SYMBOL(q6core_get_fwk_version_size);
  407. int32_t core_set_license(uint32_t key, uint32_t module_id)
  408. {
  409. struct avcs_cmd_set_license *cmd_setl = NULL;
  410. struct cal_block_data *cal_block = NULL;
  411. int rc = 0, packet_size = 0;
  412. pr_debug("%s: key:0x%x, id:0x%x\n", __func__, key, module_id);
  413. mutex_lock(&(q6core_lcl.cmd_lock));
  414. if (q6core_lcl.cal_data[META_CAL] == NULL) {
  415. pr_err("%s: cal_data not initialized yet!!\n", __func__);
  416. rc = -EINVAL;
  417. goto cmd_unlock;
  418. }
  419. mutex_lock(&((q6core_lcl.cal_data[META_CAL])->lock));
  420. cal_block = cal_utils_get_cal_block_by_key(
  421. q6core_lcl.cal_data[META_CAL], key);
  422. if (cal_block == NULL ||
  423. cal_block->cal_data.kvaddr == NULL ||
  424. cal_block->cal_data.size <= 0) {
  425. pr_err("%s: Invalid cal block to send", __func__);
  426. rc = -EINVAL;
  427. goto cal_data_unlock;
  428. }
  429. packet_size = sizeof(struct avcs_cmd_set_license) +
  430. cal_block->cal_data.size;
  431. /*round up total packet_size to next 4 byte boundary*/
  432. packet_size = ((packet_size + 0x3)>>2)<<2;
  433. cmd_setl = kzalloc(packet_size, GFP_KERNEL);
  434. if (cmd_setl == NULL) {
  435. rc = -ENOMEM;
  436. goto cal_data_unlock;
  437. }
  438. ocm_core_open();
  439. if (q6core_lcl.core_handle_q == NULL) {
  440. pr_err("%s: apr registration for CORE failed\n", __func__);
  441. rc = -ENODEV;
  442. goto fail_cmd;
  443. }
  444. cmd_setl->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  445. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  446. cmd_setl->hdr.pkt_size = packet_size;
  447. cmd_setl->hdr.src_port = 0;
  448. cmd_setl->hdr.dest_port = 0;
  449. cmd_setl->hdr.token = 0;
  450. cmd_setl->hdr.opcode = AVCS_CMD_SET_LICENSE;
  451. cmd_setl->id = module_id;
  452. cmd_setl->overwrite = 1;
  453. cmd_setl->size = cal_block->cal_data.size;
  454. memcpy((uint8_t *)cmd_setl + sizeof(struct avcs_cmd_set_license),
  455. cal_block->cal_data.kvaddr,
  456. cal_block->cal_data.size);
  457. pr_info("%s: Set license opcode=0x%x, id =0x%x, size = %d\n",
  458. __func__, cmd_setl->hdr.opcode,
  459. cmd_setl->id, cmd_setl->size);
  460. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)cmd_setl);
  461. if (rc < 0)
  462. pr_err("%s: SET_LICENSE failed op[0x%x]rc[%d]\n",
  463. __func__, cmd_setl->hdr.opcode, rc);
  464. fail_cmd:
  465. kfree(cmd_setl);
  466. cal_data_unlock:
  467. mutex_unlock(&((q6core_lcl.cal_data[META_CAL])->lock));
  468. cmd_unlock:
  469. mutex_unlock(&(q6core_lcl.cmd_lock));
  470. return rc;
  471. }
  472. int32_t core_get_license_status(uint32_t module_id)
  473. {
  474. struct avcs_cmd_get_license_validation_result get_lvr_cmd;
  475. int ret = 0;
  476. pr_debug("%s: module_id 0x%x", __func__, module_id);
  477. mutex_lock(&(q6core_lcl.cmd_lock));
  478. ocm_core_open();
  479. if (q6core_lcl.core_handle_q == NULL) {
  480. pr_err("%s: apr registration for CORE failed\n", __func__);
  481. ret = -ENODEV;
  482. goto fail_cmd;
  483. }
  484. get_lvr_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  485. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  486. get_lvr_cmd.hdr.pkt_size =
  487. sizeof(struct avcs_cmd_get_license_validation_result);
  488. get_lvr_cmd.hdr.src_port = 0;
  489. get_lvr_cmd.hdr.dest_port = 0;
  490. get_lvr_cmd.hdr.token = 0;
  491. get_lvr_cmd.hdr.opcode = AVCS_CMD_GET_LICENSE_VALIDATION_RESULT;
  492. get_lvr_cmd.id = module_id;
  493. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &get_lvr_cmd);
  494. if (ret < 0) {
  495. pr_err("%s: license_validation request failed, err %d\n",
  496. __func__, ret);
  497. ret = -EREMOTE;
  498. goto fail_cmd;
  499. }
  500. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  501. mutex_unlock(&(q6core_lcl.cmd_lock));
  502. ret = wait_event_timeout(q6core_lcl.cmd_req_wait,
  503. (q6core_lcl.cmd_resp_received_flag ==
  504. FLAG_CMDRSP_LICENSE_RESULT),
  505. msecs_to_jiffies(TIMEOUT_MS));
  506. mutex_lock(&(q6core_lcl.cmd_lock));
  507. if (!ret) {
  508. pr_err("%s: wait_event timeout for CMDRSP_LICENSE_RESULT\n",
  509. __func__);
  510. ret = -ETIME;
  511. goto fail_cmd;
  512. }
  513. q6core_lcl.cmd_resp_received_flag &= ~(FLAG_CMDRSP_LICENSE_RESULT);
  514. ret = q6core_lcl.cmd_resp_payload.cmdrsp_license_result.result;
  515. fail_cmd:
  516. mutex_unlock(&(q6core_lcl.cmd_lock));
  517. pr_info("%s: cmdrsp_license_result.result = 0x%x for module 0x%x\n",
  518. __func__, ret, module_id);
  519. return ret;
  520. }
  521. uint32_t core_set_dolby_manufacturer_id(int manufacturer_id)
  522. {
  523. struct adsp_dolby_manufacturer_id payload;
  524. int rc = 0;
  525. pr_debug("%s: manufacturer_id :%d\n", __func__, manufacturer_id);
  526. mutex_lock(&(q6core_lcl.cmd_lock));
  527. ocm_core_open();
  528. if (q6core_lcl.core_handle_q) {
  529. payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  530. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  531. payload.hdr.pkt_size =
  532. sizeof(struct adsp_dolby_manufacturer_id);
  533. payload.hdr.src_port = 0;
  534. payload.hdr.dest_port = 0;
  535. payload.hdr.token = 0;
  536. payload.hdr.opcode = ADSP_CMD_SET_DOLBY_MANUFACTURER_ID;
  537. payload.manufacturer_id = manufacturer_id;
  538. pr_debug("%s: Send Dolby security opcode=0x%x manufacturer ID = %d\n",
  539. __func__,
  540. payload.hdr.opcode, payload.manufacturer_id);
  541. rc = apr_send_pkt(q6core_lcl.core_handle_q,
  542. (uint32_t *)&payload);
  543. if (rc < 0)
  544. pr_err("%s: SET_DOLBY_MANUFACTURER_ID failed op[0x%x]rc[%d]\n",
  545. __func__, payload.hdr.opcode, rc);
  546. }
  547. mutex_unlock(&(q6core_lcl.cmd_lock));
  548. return rc;
  549. }
  550. /**
  551. * q6core_is_adsp_ready - check adsp ready status
  552. *
  553. * Returns true if adsp is ready otherwise returns false
  554. */
  555. bool q6core_is_adsp_ready(void)
  556. {
  557. int rc = 0;
  558. bool ret = false;
  559. struct apr_hdr hdr;
  560. pr_debug("%s: enter\n", __func__);
  561. memset(&hdr, 0, sizeof(hdr));
  562. hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  563. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  564. hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE, 0);
  565. hdr.opcode = AVCS_CMD_ADSP_EVENT_GET_STATE;
  566. mutex_lock(&(q6core_lcl.cmd_lock));
  567. ocm_core_open();
  568. if (q6core_lcl.core_handle_q) {
  569. q6core_lcl.bus_bw_resp_received = 0;
  570. rc = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)&hdr);
  571. if (rc < 0) {
  572. pr_err("%s: Get ADSP state APR packet send event %d\n",
  573. __func__, rc);
  574. goto bail;
  575. }
  576. rc = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  577. (q6core_lcl.bus_bw_resp_received == 1),
  578. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  579. if (rc > 0 && q6core_lcl.bus_bw_resp_received) {
  580. /* ensure to read updated param by callback thread */
  581. rmb();
  582. ret = !!q6core_lcl.param;
  583. }
  584. }
  585. bail:
  586. pr_debug("%s: leave, rc %d, adsp ready %d\n", __func__, rc, ret);
  587. mutex_unlock(&(q6core_lcl.cmd_lock));
  588. return ret;
  589. }
  590. EXPORT_SYMBOL(q6core_is_adsp_ready);
  591. static int q6core_map_memory_regions(phys_addr_t *buf_add, uint32_t mempool_id,
  592. uint32_t *bufsz, uint32_t bufcnt, uint32_t *map_handle)
  593. {
  594. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  595. struct avs_shared_map_region_payload *mregions = NULL;
  596. void *mmap_region_cmd = NULL;
  597. void *payload = NULL;
  598. int ret = 0;
  599. int i = 0;
  600. int cmd_size = 0;
  601. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  602. + sizeof(struct avs_shared_map_region_payload)
  603. * bufcnt;
  604. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  605. if (mmap_region_cmd == NULL)
  606. return -ENOMEM;
  607. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  608. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  609. APR_HDR_LEN(APR_HDR_SIZE),
  610. APR_PKT_VER);
  611. mmap_regions->hdr.pkt_size = cmd_size;
  612. mmap_regions->hdr.src_port = 0;
  613. mmap_regions->hdr.dest_port = 0;
  614. mmap_regions->hdr.token = 0;
  615. mmap_regions->hdr.opcode = AVCS_CMD_SHARED_MEM_MAP_REGIONS;
  616. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
  617. mmap_regions->num_regions = bufcnt & 0x00ff;
  618. mmap_regions->property_flag = 0x00;
  619. payload = ((u8 *) mmap_region_cmd +
  620. sizeof(struct avs_cmd_shared_mem_map_regions));
  621. mregions = (struct avs_shared_map_region_payload *)payload;
  622. for (i = 0; i < bufcnt; i++) {
  623. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  624. mregions->shm_addr_msw =
  625. msm_audio_populate_upper_32_bits(buf_add[i]);
  626. mregions->mem_size_bytes = bufsz[i];
  627. ++mregions;
  628. }
  629. pr_debug("%s: sending memory map, addr %pK, size %d, bufcnt = %d\n",
  630. __func__, buf_add, bufsz[0], mmap_regions->num_regions);
  631. *map_handle = 0;
  632. q6core_lcl.bus_bw_resp_received = 0;
  633. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  634. mmap_regions);
  635. if (ret < 0) {
  636. pr_err("%s: mmap regions failed %d\n",
  637. __func__, ret);
  638. ret = -EINVAL;
  639. goto done;
  640. }
  641. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  642. (q6core_lcl.bus_bw_resp_received == 1),
  643. msecs_to_jiffies(TIMEOUT_MS));
  644. if (!ret) {
  645. pr_err("%s: timeout. waited for memory map\n", __func__);
  646. ret = -ETIME;
  647. goto done;
  648. }
  649. *map_handle = q6core_lcl.mem_map_cal_handle;
  650. done:
  651. kfree(mmap_region_cmd);
  652. return ret;
  653. }
  654. static int q6core_memory_unmap_regions(uint32_t mem_map_handle)
  655. {
  656. struct avs_cmd_shared_mem_unmap_regions unmap_regions;
  657. int ret = 0;
  658. memset(&unmap_regions, 0, sizeof(unmap_regions));
  659. unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  660. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  661. unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
  662. unmap_regions.hdr.src_svc = APR_SVC_ADSP_CORE;
  663. unmap_regions.hdr.src_domain = APR_DOMAIN_APPS;
  664. unmap_regions.hdr.src_port = 0;
  665. unmap_regions.hdr.dest_svc = APR_SVC_ADSP_CORE;
  666. unmap_regions.hdr.dest_domain = APR_DOMAIN_ADSP;
  667. unmap_regions.hdr.dest_port = 0;
  668. unmap_regions.hdr.token = 0;
  669. unmap_regions.hdr.opcode = AVCS_CMD_SHARED_MEM_UNMAP_REGIONS;
  670. unmap_regions.mem_map_handle = mem_map_handle;
  671. q6core_lcl.bus_bw_resp_received = 0;
  672. pr_debug("%s: unmap regions map handle %d\n",
  673. __func__, mem_map_handle);
  674. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *)
  675. &unmap_regions);
  676. if (ret < 0) {
  677. pr_err("%s: unmap regions failed %d\n",
  678. __func__, ret);
  679. ret = -EINVAL;
  680. goto done;
  681. }
  682. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  683. (q6core_lcl.bus_bw_resp_received == 1),
  684. msecs_to_jiffies(TIMEOUT_MS));
  685. if (!ret) {
  686. pr_err("%s: timeout. waited for memory_unmap\n",
  687. __func__);
  688. ret = -ETIME;
  689. goto done;
  690. }
  691. done:
  692. return ret;
  693. }
  694. static int q6core_dereg_all_custom_topologies(void)
  695. {
  696. int ret = 0;
  697. struct avcs_cmd_deregister_topologies dereg_top;
  698. memset(&dereg_top, 0, sizeof(dereg_top));
  699. dereg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  700. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  701. dereg_top.hdr.pkt_size = sizeof(dereg_top);
  702. dereg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  703. dereg_top.hdr.src_domain = APR_DOMAIN_APPS;
  704. dereg_top.hdr.src_port = 0;
  705. dereg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  706. dereg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  707. dereg_top.hdr.dest_port = 0;
  708. dereg_top.hdr.token = 0;
  709. dereg_top.hdr.opcode = AVCS_CMD_DEREGISTER_TOPOLOGIES;
  710. dereg_top.payload_addr_lsw = 0;
  711. dereg_top.payload_addr_msw = 0;
  712. dereg_top.mem_map_handle = 0;
  713. dereg_top.payload_size = 0;
  714. dereg_top.mode = AVCS_MODE_DEREGISTER_ALL_CUSTOM_TOPOLOGIES;
  715. q6core_lcl.bus_bw_resp_received = 0;
  716. pr_debug("%s: Deregister topologies mode %d\n",
  717. __func__, dereg_top.mode);
  718. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &dereg_top);
  719. if (ret < 0) {
  720. pr_err("%s: Deregister topologies failed %d\n",
  721. __func__, ret);
  722. goto done;
  723. }
  724. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  725. (q6core_lcl.bus_bw_resp_received == 1),
  726. msecs_to_jiffies(TIMEOUT_MS));
  727. if (!ret) {
  728. pr_err("%s: wait_event timeout for Deregister topologies\n",
  729. __func__);
  730. goto done;
  731. }
  732. done:
  733. return ret;
  734. }
  735. static int q6core_send_custom_topologies(void)
  736. {
  737. int ret = 0;
  738. int ret2 = 0;
  739. struct cal_block_data *cal_block = NULL;
  740. struct avcs_cmd_register_topologies reg_top;
  741. if (!q6core_is_adsp_ready()) {
  742. pr_err("%s: ADSP is not ready!\n", __func__);
  743. return -ENODEV;
  744. }
  745. memset(&reg_top, 0, sizeof(reg_top));
  746. mutex_lock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  747. mutex_lock(&q6core_lcl.cmd_lock);
  748. cal_block = cal_utils_get_only_cal_block(
  749. q6core_lcl.cal_data[CUST_TOP_CAL]);
  750. if (cal_block == NULL) {
  751. pr_debug("%s: cal block is NULL!\n", __func__);
  752. goto unlock;
  753. }
  754. if (cal_block->cal_data.size <= 0) {
  755. pr_debug("%s: cal size is %zd not sending\n",
  756. __func__, cal_block->cal_data.size);
  757. goto unlock;
  758. }
  759. q6core_dereg_all_custom_topologies();
  760. ret = q6core_map_memory_regions(&cal_block->cal_data.paddr, 0,
  761. (uint32_t *)&cal_block->map_data.map_size, 1,
  762. &cal_block->map_data.q6map_handle);
  763. if (!ret) {
  764. pr_err("%s: q6core_map_memory_regions failed\n", __func__);
  765. goto unlock;
  766. }
  767. reg_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  768. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  769. reg_top.hdr.pkt_size = sizeof(reg_top);
  770. reg_top.hdr.src_svc = APR_SVC_ADSP_CORE;
  771. reg_top.hdr.src_domain = APR_DOMAIN_APPS;
  772. reg_top.hdr.src_port = 0;
  773. reg_top.hdr.dest_svc = APR_SVC_ADSP_CORE;
  774. reg_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  775. reg_top.hdr.dest_port = 0;
  776. reg_top.hdr.token = 0;
  777. reg_top.hdr.opcode = AVCS_CMD_REGISTER_TOPOLOGIES;
  778. reg_top.payload_addr_lsw =
  779. lower_32_bits(cal_block->cal_data.paddr);
  780. reg_top.payload_addr_msw =
  781. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  782. reg_top.mem_map_handle = cal_block->map_data.q6map_handle;
  783. reg_top.payload_size = cal_block->cal_data.size;
  784. q6core_lcl.adsp_status = 0;
  785. q6core_lcl.bus_bw_resp_received = 0;
  786. pr_debug("%s: Register topologies addr %pK, size %zd, map handle %d\n",
  787. __func__, &cal_block->cal_data.paddr, cal_block->cal_data.size,
  788. cal_block->map_data.q6map_handle);
  789. ret = apr_send_pkt(q6core_lcl.core_handle_q, (uint32_t *) &reg_top);
  790. if (ret < 0) {
  791. pr_err("%s: Register topologies failed %d\n",
  792. __func__, ret);
  793. goto unmap;
  794. }
  795. ret = wait_event_timeout(q6core_lcl.bus_bw_req_wait,
  796. (q6core_lcl.bus_bw_resp_received == 1),
  797. msecs_to_jiffies(TIMEOUT_MS));
  798. if (!ret) {
  799. pr_err("%s: wait_event timeout for Register topologies\n",
  800. __func__);
  801. goto unmap;
  802. }
  803. if (q6core_lcl.adsp_status < 0)
  804. ret = q6core_lcl.adsp_status;
  805. unmap:
  806. ret2 = q6core_memory_unmap_regions(cal_block->map_data.q6map_handle);
  807. if (!ret2) {
  808. pr_err("%s: q6core_memory_unmap_regions failed for map handle %d\n",
  809. __func__, cal_block->map_data.q6map_handle);
  810. ret = ret2;
  811. goto unlock;
  812. }
  813. unlock:
  814. mutex_unlock(&q6core_lcl.cmd_lock);
  815. mutex_unlock(&q6core_lcl.cal_data[CUST_TOP_CAL]->lock);
  816. return ret;
  817. }
  818. static int get_cal_type_index(int32_t cal_type)
  819. {
  820. int ret = -EINVAL;
  821. switch (cal_type) {
  822. case AUDIO_CORE_METAINFO_CAL_TYPE:
  823. ret = META_CAL;
  824. break;
  825. case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
  826. ret = CUST_TOP_CAL;
  827. break;
  828. default:
  829. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  830. }
  831. return ret;
  832. }
  833. static int q6core_alloc_cal(int32_t cal_type,
  834. size_t data_size, void *data)
  835. {
  836. int ret = 0;
  837. int cal_index;
  838. cal_index = get_cal_type_index(cal_type);
  839. if (cal_index < 0) {
  840. pr_err("%s: could not get cal index %d!\n",
  841. __func__, cal_index);
  842. ret = -EINVAL;
  843. goto done;
  844. }
  845. ret = cal_utils_alloc_cal(data_size, data,
  846. q6core_lcl.cal_data[cal_index], 0, NULL);
  847. if (ret < 0) {
  848. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  849. __func__, ret, cal_type);
  850. goto done;
  851. }
  852. done:
  853. return ret;
  854. }
  855. static int q6core_dealloc_cal(int32_t cal_type,
  856. size_t data_size, void *data)
  857. {
  858. int ret = 0;
  859. int cal_index;
  860. cal_index = get_cal_type_index(cal_type);
  861. if (cal_index < 0) {
  862. pr_err("%s: could not get cal index %d!\n",
  863. __func__, cal_index);
  864. ret = -EINVAL;
  865. goto done;
  866. }
  867. ret = cal_utils_dealloc_cal(data_size, data,
  868. q6core_lcl.cal_data[cal_index]);
  869. if (ret < 0) {
  870. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  871. __func__, ret, cal_type);
  872. goto done;
  873. }
  874. done:
  875. return ret;
  876. }
  877. static int q6core_set_cal(int32_t cal_type,
  878. size_t data_size, void *data)
  879. {
  880. int ret = 0;
  881. int cal_index;
  882. cal_index = get_cal_type_index(cal_type);
  883. if (cal_index < 0) {
  884. pr_err("%s: could not get cal index %d!\n",
  885. __func__, cal_index);
  886. ret = -EINVAL;
  887. goto done;
  888. }
  889. ret = cal_utils_set_cal(data_size, data,
  890. q6core_lcl.cal_data[cal_index], 0, NULL);
  891. if (ret < 0) {
  892. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  893. __func__, ret, cal_type);
  894. goto done;
  895. }
  896. if (cal_index == CUST_TOP_CAL)
  897. ret = q6core_send_custom_topologies();
  898. done:
  899. return ret;
  900. }
  901. static void q6core_delete_cal_data(void)
  902. {
  903. pr_debug("%s:\n", __func__);
  904. cal_utils_destroy_cal_types(CORE_MAX_CAL, q6core_lcl.cal_data);
  905. }
  906. static int q6core_init_cal_data(void)
  907. {
  908. int ret = 0;
  909. struct cal_type_info cal_type_info[] = {
  910. {{AUDIO_CORE_METAINFO_CAL_TYPE,
  911. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  912. q6core_set_cal, NULL, NULL} },
  913. {NULL, NULL, cal_utils_match_buf_num} },
  914. {{CORE_CUSTOM_TOPOLOGIES_CAL_TYPE,
  915. {q6core_alloc_cal, q6core_dealloc_cal, NULL,
  916. q6core_set_cal, NULL, NULL} },
  917. {NULL, NULL, cal_utils_match_buf_num} }
  918. };
  919. pr_debug("%s:\n", __func__);
  920. ret = cal_utils_create_cal_types(CORE_MAX_CAL,
  921. q6core_lcl.cal_data, cal_type_info);
  922. if (ret < 0) {
  923. pr_err("%s: could not create cal type!\n",
  924. __func__);
  925. goto err;
  926. }
  927. return ret;
  928. err:
  929. q6core_delete_cal_data();
  930. return ret;
  931. }
  932. static int __init core_init(void)
  933. {
  934. memset(&q6core_lcl, 0, sizeof(struct q6core_str));
  935. init_waitqueue_head(&q6core_lcl.bus_bw_req_wait);
  936. init_waitqueue_head(&q6core_lcl.cmd_req_wait);
  937. init_waitqueue_head(&q6core_lcl.avcs_fwk_ver_req_wait);
  938. q6core_lcl.cmd_resp_received_flag = FLAG_NONE;
  939. mutex_init(&q6core_lcl.cmd_lock);
  940. mutex_init(&q6core_lcl.ver_lock);
  941. q6core_init_cal_data();
  942. return 0;
  943. }
  944. module_init(core_init);
  945. static void __exit core_exit(void)
  946. {
  947. mutex_destroy(&q6core_lcl.cmd_lock);
  948. mutex_destroy(&q6core_lcl.ver_lock);
  949. q6core_delete_cal_data();
  950. }
  951. module_exit(core_exit);
  952. MODULE_DESCRIPTION("ADSP core driver");
  953. MODULE_LICENSE("GPL v2");