q6core.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2018, Linaro Limited
  4. #include <linux/slab.h>
  5. #include <linux/wait.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/of.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/wait.h>
  13. #include <linux/soc/qcom/apr.h>
  14. #include "q6core.h"
  15. #include "q6dsp-errno.h"
  16. #define ADSP_STATE_READY_TIMEOUT_MS 3000
  17. #define Q6_READY_TIMEOUT_MS 100
  18. #define AVCS_CMD_ADSP_EVENT_GET_STATE 0x0001290C
  19. #define AVCS_CMDRSP_ADSP_EVENT_GET_STATE 0x0001290D
  20. #define AVCS_GET_VERSIONS 0x00012905
  21. #define AVCS_GET_VERSIONS_RSP 0x00012906
  22. #define AVCS_CMD_GET_FWK_VERSION 0x001292c
  23. #define AVCS_CMDRSP_GET_FWK_VERSION 0x001292d
  24. struct avcs_svc_info {
  25. uint32_t service_id;
  26. uint32_t version;
  27. } __packed;
  28. struct avcs_cmdrsp_get_version {
  29. uint32_t build_id;
  30. uint32_t num_services;
  31. struct avcs_svc_info svc_api_info[];
  32. } __packed;
  33. /* for ADSP2.8 and above */
  34. struct avcs_svc_api_info {
  35. uint32_t service_id;
  36. uint32_t api_version;
  37. uint32_t api_branch_version;
  38. } __packed;
  39. struct avcs_cmdrsp_get_fwk_version {
  40. uint32_t build_major_version;
  41. uint32_t build_minor_version;
  42. uint32_t build_branch_version;
  43. uint32_t build_subbranch_version;
  44. uint32_t num_services;
  45. struct avcs_svc_api_info svc_api_info[];
  46. } __packed;
  47. struct q6core {
  48. struct apr_device *adev;
  49. wait_queue_head_t wait;
  50. uint32_t avcs_state;
  51. struct mutex lock;
  52. bool resp_received;
  53. uint32_t num_services;
  54. struct avcs_cmdrsp_get_fwk_version *fwk_version;
  55. struct avcs_cmdrsp_get_version *svc_version;
  56. bool fwk_version_supported;
  57. bool get_state_supported;
  58. bool get_version_supported;
  59. bool is_version_requested;
  60. };
  61. static struct q6core *g_core;
  62. static int q6core_callback(struct apr_device *adev, struct apr_resp_pkt *data)
  63. {
  64. struct q6core *core = dev_get_drvdata(&adev->dev);
  65. struct aprv2_ibasic_rsp_result_t *result;
  66. struct apr_hdr *hdr = &data->hdr;
  67. result = data->payload;
  68. switch (hdr->opcode) {
  69. case APR_BASIC_RSP_RESULT:{
  70. result = data->payload;
  71. switch (result->opcode) {
  72. case AVCS_GET_VERSIONS:
  73. if (result->status == ADSP_EUNSUPPORTED)
  74. core->get_version_supported = false;
  75. core->resp_received = true;
  76. break;
  77. case AVCS_CMD_GET_FWK_VERSION:
  78. if (result->status == ADSP_EUNSUPPORTED)
  79. core->fwk_version_supported = false;
  80. core->resp_received = true;
  81. break;
  82. case AVCS_CMD_ADSP_EVENT_GET_STATE:
  83. if (result->status == ADSP_EUNSUPPORTED)
  84. core->get_state_supported = false;
  85. core->resp_received = true;
  86. break;
  87. }
  88. break;
  89. }
  90. case AVCS_CMDRSP_GET_FWK_VERSION: {
  91. struct avcs_cmdrsp_get_fwk_version *fwk;
  92. int bytes;
  93. fwk = data->payload;
  94. bytes = sizeof(*fwk) + fwk->num_services *
  95. sizeof(fwk->svc_api_info[0]);
  96. core->fwk_version = kzalloc(bytes, GFP_ATOMIC);
  97. if (!core->fwk_version)
  98. return -ENOMEM;
  99. memcpy(core->fwk_version, data->payload, bytes);
  100. core->fwk_version_supported = true;
  101. core->resp_received = true;
  102. break;
  103. }
  104. case AVCS_GET_VERSIONS_RSP: {
  105. struct avcs_cmdrsp_get_version *v;
  106. int len;
  107. v = data->payload;
  108. len = sizeof(*v) + v->num_services * sizeof(v->svc_api_info[0]);
  109. core->svc_version = kzalloc(len, GFP_ATOMIC);
  110. if (!core->svc_version)
  111. return -ENOMEM;
  112. memcpy(core->svc_version, data->payload, len);
  113. core->get_version_supported = true;
  114. core->resp_received = true;
  115. break;
  116. }
  117. case AVCS_CMDRSP_ADSP_EVENT_GET_STATE:
  118. core->get_state_supported = true;
  119. core->avcs_state = result->opcode;
  120. core->resp_received = true;
  121. break;
  122. default:
  123. dev_err(&adev->dev, "Message id from adsp core svc: 0x%x\n",
  124. hdr->opcode);
  125. break;
  126. }
  127. if (core->resp_received)
  128. wake_up(&core->wait);
  129. return 0;
  130. }
  131. static int q6core_get_fwk_versions(struct q6core *core)
  132. {
  133. struct apr_device *adev = core->adev;
  134. struct apr_pkt pkt;
  135. int rc;
  136. pkt.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  137. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  138. pkt.hdr.pkt_size = APR_HDR_SIZE;
  139. pkt.hdr.opcode = AVCS_CMD_GET_FWK_VERSION;
  140. rc = apr_send_pkt(adev, &pkt);
  141. if (rc < 0)
  142. return rc;
  143. rc = wait_event_timeout(core->wait, (core->resp_received),
  144. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  145. if (rc > 0 && core->resp_received) {
  146. core->resp_received = false;
  147. if (!core->fwk_version_supported)
  148. return -ENOTSUPP;
  149. else
  150. return 0;
  151. }
  152. return rc;
  153. }
  154. static int q6core_get_svc_versions(struct q6core *core)
  155. {
  156. struct apr_device *adev = core->adev;
  157. struct apr_pkt pkt;
  158. int rc;
  159. pkt.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  160. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  161. pkt.hdr.pkt_size = APR_HDR_SIZE;
  162. pkt.hdr.opcode = AVCS_GET_VERSIONS;
  163. rc = apr_send_pkt(adev, &pkt);
  164. if (rc < 0)
  165. return rc;
  166. rc = wait_event_timeout(core->wait, (core->resp_received),
  167. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  168. if (rc > 0 && core->resp_received) {
  169. core->resp_received = false;
  170. return 0;
  171. }
  172. return rc;
  173. }
  174. static bool __q6core_is_adsp_ready(struct q6core *core)
  175. {
  176. struct apr_device *adev = core->adev;
  177. struct apr_pkt pkt;
  178. int rc;
  179. core->get_state_supported = false;
  180. pkt.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  181. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  182. pkt.hdr.pkt_size = APR_HDR_SIZE;
  183. pkt.hdr.opcode = AVCS_CMD_ADSP_EVENT_GET_STATE;
  184. rc = apr_send_pkt(adev, &pkt);
  185. if (rc < 0)
  186. return false;
  187. rc = wait_event_timeout(core->wait, (core->resp_received),
  188. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  189. if (rc > 0 && core->resp_received) {
  190. core->resp_received = false;
  191. if (core->avcs_state)
  192. return true;
  193. }
  194. /* assume that the adsp is up if we not support this command */
  195. if (!core->get_state_supported)
  196. return true;
  197. return false;
  198. }
  199. /**
  200. * q6core_get_svc_api_info() - Get version number of a service.
  201. *
  202. * @svc_id: service id of the service.
  203. * @ainfo: Valid struct pointer to fill svc api information.
  204. *
  205. * Return: zero on success and error code on failure or unsupported
  206. */
  207. int q6core_get_svc_api_info(int svc_id, struct q6core_svc_api_info *ainfo)
  208. {
  209. int i;
  210. int ret = -ENOTSUPP;
  211. if (!g_core || !ainfo)
  212. return 0;
  213. mutex_lock(&g_core->lock);
  214. if (!g_core->is_version_requested) {
  215. if (q6core_get_fwk_versions(g_core) == -ENOTSUPP)
  216. q6core_get_svc_versions(g_core);
  217. g_core->is_version_requested = true;
  218. }
  219. if (g_core->fwk_version_supported) {
  220. for (i = 0; i < g_core->fwk_version->num_services; i++) {
  221. struct avcs_svc_api_info *info;
  222. info = &g_core->fwk_version->svc_api_info[i];
  223. if (svc_id != info->service_id)
  224. continue;
  225. ainfo->api_version = info->api_version;
  226. ainfo->api_branch_version = info->api_branch_version;
  227. ret = 0;
  228. break;
  229. }
  230. } else if (g_core->get_version_supported) {
  231. for (i = 0; i < g_core->svc_version->num_services; i++) {
  232. struct avcs_svc_info *info;
  233. info = &g_core->svc_version->svc_api_info[i];
  234. if (svc_id != info->service_id)
  235. continue;
  236. ainfo->api_version = info->version;
  237. ainfo->api_branch_version = 0;
  238. ret = 0;
  239. break;
  240. }
  241. }
  242. mutex_unlock(&g_core->lock);
  243. return ret;
  244. }
  245. EXPORT_SYMBOL_GPL(q6core_get_svc_api_info);
  246. /**
  247. * q6core_is_adsp_ready() - Get status of adsp
  248. *
  249. * Return: Will be an true if adsp is ready and false if not.
  250. */
  251. bool q6core_is_adsp_ready(void)
  252. {
  253. unsigned long timeout;
  254. bool ret = false;
  255. if (!g_core)
  256. return false;
  257. mutex_lock(&g_core->lock);
  258. timeout = jiffies + msecs_to_jiffies(ADSP_STATE_READY_TIMEOUT_MS);
  259. for (;;) {
  260. if (__q6core_is_adsp_ready(g_core)) {
  261. ret = true;
  262. break;
  263. }
  264. if (!time_after(timeout, jiffies)) {
  265. ret = false;
  266. break;
  267. }
  268. }
  269. mutex_unlock(&g_core->lock);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(q6core_is_adsp_ready);
  273. static int q6core_probe(struct apr_device *adev)
  274. {
  275. g_core = kzalloc(sizeof(*g_core), GFP_KERNEL);
  276. if (!g_core)
  277. return -ENOMEM;
  278. dev_set_drvdata(&adev->dev, g_core);
  279. mutex_init(&g_core->lock);
  280. g_core->adev = adev;
  281. init_waitqueue_head(&g_core->wait);
  282. return 0;
  283. }
  284. static int q6core_exit(struct apr_device *adev)
  285. {
  286. struct q6core *core = dev_get_drvdata(&adev->dev);
  287. if (core->fwk_version_supported)
  288. kfree(core->fwk_version);
  289. if (core->get_version_supported)
  290. kfree(core->svc_version);
  291. g_core = NULL;
  292. kfree(core);
  293. return 0;
  294. }
  295. static const struct of_device_id q6core_device_id[] = {
  296. { .compatible = "qcom,q6core" },
  297. {},
  298. };
  299. MODULE_DEVICE_TABLE(of, q6core_device_id);
  300. static struct apr_driver qcom_q6core_driver = {
  301. .probe = q6core_probe,
  302. .remove = q6core_exit,
  303. .callback = q6core_callback,
  304. .driver = {
  305. .name = "qcom-q6core",
  306. .of_match_table = of_match_ptr(q6core_device_id),
  307. },
  308. };
  309. module_apr_driver(qcom_q6core_driver);
  310. MODULE_DESCRIPTION("q6 core");
  311. MODULE_LICENSE("GPL v2");