adsp-loader.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2014, 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/err.h>
  9. #include <linux/string.h>
  10. #include <linux/delay.h>
  11. #include <linux/platform_device.h>
  12. #include <dsp/spf-core.h>
  13. #include <linux/of_device.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/nvmem-consumer.h>
  17. #include <linux/slab.h>
  18. #include <soc/qcom/subsystem_restart.h>
  19. #define Q6_PIL_GET_DELAY_MS 100
  20. #define BOOT_CMD 1
  21. #define SSR_RESET_CMD 1
  22. #define IMAGE_UNLOAD_CMD 0
  23. #define MAX_FW_IMAGES 4
  24. enum apr_subsys_state {
  25. APR_SUBSYS_DOWN,
  26. APR_SUBSYS_UP,
  27. APR_SUBSYS_LOADED,
  28. };
  29. static ssize_t adsp_boot_store(struct kobject *kobj,
  30. struct kobj_attribute *attr,
  31. const char *buf, size_t count);
  32. static ssize_t adsp_ssr_store(struct kobject *kobj,
  33. struct kobj_attribute *attr,
  34. const char *buf, size_t count);
  35. struct adsp_loader_private {
  36. void *pil_h;
  37. struct kobject *boot_adsp_obj;
  38. struct attribute_group *attr_group;
  39. char *adsp_fw_name;
  40. };
  41. static struct kobj_attribute adsp_boot_attribute =
  42. __ATTR(boot, 0220, NULL, adsp_boot_store);
  43. static struct kobj_attribute adsp_ssr_attribute =
  44. __ATTR(ssr, 0220, NULL, adsp_ssr_store);
  45. static struct attribute *attrs[] = {
  46. &adsp_boot_attribute.attr,
  47. &adsp_ssr_attribute.attr,
  48. NULL,
  49. };
  50. static struct work_struct adsp_ldr_work;
  51. static struct platform_device *adsp_private;
  52. static void adsp_loader_unload(struct platform_device *pdev);
  53. static void adsp_load_fw(struct work_struct *adsp_ldr_work)
  54. {
  55. struct platform_device *pdev = adsp_private;
  56. struct adsp_loader_private *priv = NULL;
  57. const char *adsp_dt = "qcom,adsp-state";
  58. int rc = 0;
  59. u32 adsp_state;
  60. const char *img_name;
  61. if (!pdev) {
  62. dev_err(&pdev->dev, "%s: Platform device null\n", __func__);
  63. goto fail;
  64. }
  65. if (!pdev->dev.of_node) {
  66. dev_err(&pdev->dev,
  67. "%s: Device tree information missing\n", __func__);
  68. goto fail;
  69. }
  70. rc = of_property_read_u32(pdev->dev.of_node, adsp_dt, &adsp_state);
  71. if (rc) {
  72. dev_err(&pdev->dev,
  73. "%s: ADSP state = %x\n", __func__, adsp_state);
  74. goto fail;
  75. }
  76. rc = of_property_read_string(pdev->dev.of_node,
  77. "qcom,proc-img-to-load",
  78. &img_name);
  79. if (rc) {
  80. dev_dbg(&pdev->dev,
  81. "%s: loading default image ADSP\n", __func__);
  82. goto load_adsp;
  83. }
  84. if (!strcmp(img_name, "modem")) {
  85. /* adsp_state always returns "0". So load modem image based on
  86. * apr_modem_state to prevent loading of image twice
  87. */
  88. //adsp_state = apr_get_modem_state();
  89. if (adsp_state == APR_SUBSYS_DOWN) {
  90. priv = platform_get_drvdata(pdev);
  91. if (!priv) {
  92. dev_err(&pdev->dev,
  93. " %s: Private data get failed\n", __func__);
  94. goto fail;
  95. }
  96. priv->pil_h = subsystem_get("modem");
  97. if (IS_ERR(priv->pil_h)) {
  98. dev_err(&pdev->dev, "%s: pil get failed,\n",
  99. __func__);
  100. goto fail;
  101. }
  102. /* Set the state of the ADSP in APR driver */
  103. //apr_set_modem_state(APR_SUBSYS_LOADED);
  104. } else if (adsp_state == APR_SUBSYS_LOADED) {
  105. dev_dbg(&pdev->dev,
  106. "%s: MDSP state = %x\n", __func__, adsp_state);
  107. }
  108. dev_dbg(&pdev->dev, "%s: Q6/MDSP image is loaded\n", __func__);
  109. return;
  110. }
  111. load_adsp:
  112. {
  113. adsp_state = spf_core_is_apm_ready();
  114. if (adsp_state == APR_SUBSYS_DOWN) {
  115. priv = platform_get_drvdata(pdev);
  116. if (!priv) {
  117. dev_err(&pdev->dev,
  118. " %s: Private data get failed\n", __func__);
  119. goto fail;
  120. }
  121. if (!priv->adsp_fw_name) {
  122. dev_dbg(&pdev->dev, "%s: Load default ADSP\n",
  123. __func__);
  124. priv->pil_h = subsystem_get("adsp");
  125. } else {
  126. dev_dbg(&pdev->dev, "%s: Load ADSP with fw name %s\n",
  127. __func__, priv->adsp_fw_name);
  128. priv->pil_h = subsystem_get_with_fwname("adsp", priv->adsp_fw_name);
  129. }
  130. if (IS_ERR(priv->pil_h)) {
  131. dev_err(&pdev->dev, "%s: pil get failed,\n",
  132. __func__);
  133. goto fail;
  134. }
  135. } else if (adsp_state == APR_SUBSYS_LOADED) {
  136. dev_dbg(&pdev->dev,
  137. "%s: ADSP state = %x\n", __func__, adsp_state);
  138. }
  139. dev_dbg(&pdev->dev, "%s: Q6/ADSP image is loaded\n", __func__);
  140. return;
  141. }
  142. fail:
  143. dev_err(&pdev->dev, "%s: Q6 image loading failed\n", __func__);
  144. }
  145. static void adsp_loader_do(struct platform_device *pdev)
  146. {
  147. schedule_work(&adsp_ldr_work);
  148. }
  149. static ssize_t adsp_ssr_store(struct kobject *kobj,
  150. struct kobj_attribute *attr,
  151. const char *buf,
  152. size_t count)
  153. {
  154. int ssr_command = 0;
  155. struct subsys_device *adsp_dev = NULL;
  156. struct platform_device *pdev = adsp_private;
  157. struct adsp_loader_private *priv = NULL;
  158. int rc;
  159. dev_dbg(&pdev->dev, "%s: going to call adsp ssr\n ", __func__);
  160. if (kstrtoint(buf, 10, &ssr_command) < 0)
  161. return -EINVAL;
  162. if (ssr_command != SSR_RESET_CMD)
  163. return -EINVAL;
  164. priv = platform_get_drvdata(pdev);
  165. if (!priv)
  166. return -EINVAL;
  167. adsp_dev = (struct subsys_device *)priv->pil_h;
  168. if (!adsp_dev)
  169. return -EINVAL;
  170. dev_err(&pdev->dev, "requesting for ADSP restart\n");
  171. /* subsystem_restart_dev has worker queue to handle */
  172. rc = subsystem_restart_dev(adsp_dev);
  173. if (rc) {
  174. dev_err(&pdev->dev, "subsystem_restart_dev failed\n");
  175. return rc;
  176. }
  177. dev_dbg(&pdev->dev, "ADSP restarted\n");
  178. return count;
  179. }
  180. static ssize_t adsp_boot_store(struct kobject *kobj,
  181. struct kobj_attribute *attr,
  182. const char *buf,
  183. size_t count)
  184. {
  185. int boot = 0;
  186. if (sscanf(buf, "%du", &boot) != 1) {
  187. pr_err("%s: failed to read boot info from string\n", __func__);
  188. return -EINVAL;
  189. }
  190. if (boot == BOOT_CMD) {
  191. pr_debug("%s: going to call adsp_loader_do\n", __func__);
  192. adsp_loader_do(adsp_private);
  193. } else if (boot == IMAGE_UNLOAD_CMD) {
  194. pr_debug("%s: going to call adsp_unloader\n", __func__);
  195. adsp_loader_unload(adsp_private);
  196. }
  197. return count;
  198. }
  199. static void adsp_loader_unload(struct platform_device *pdev)
  200. {
  201. struct adsp_loader_private *priv = NULL;
  202. priv = platform_get_drvdata(pdev);
  203. if (!priv)
  204. return;
  205. if (priv->pil_h) {
  206. dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__);
  207. subsystem_put(priv->pil_h);
  208. priv->pil_h = NULL;
  209. }
  210. }
  211. static int adsp_loader_init_sysfs(struct platform_device *pdev)
  212. {
  213. int ret = -EINVAL;
  214. struct adsp_loader_private *priv = NULL;
  215. adsp_private = NULL;
  216. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  217. if (!priv) {
  218. ret = -ENOMEM;
  219. return ret;
  220. }
  221. platform_set_drvdata(pdev, priv);
  222. priv->pil_h = NULL;
  223. priv->boot_adsp_obj = NULL;
  224. priv->attr_group = devm_kzalloc(&pdev->dev,
  225. sizeof(*(priv->attr_group)),
  226. GFP_KERNEL);
  227. if (!priv->attr_group) {
  228. ret = -ENOMEM;
  229. goto error_return;
  230. }
  231. priv->attr_group->attrs = attrs;
  232. priv->boot_adsp_obj = kobject_create_and_add("boot_adsp", kernel_kobj);
  233. if (!priv->boot_adsp_obj) {
  234. dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
  235. __func__);
  236. ret = -ENOMEM;
  237. goto error_return;
  238. }
  239. ret = sysfs_create_group(priv->boot_adsp_obj, priv->attr_group);
  240. if (ret) {
  241. dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
  242. __func__, ret);
  243. goto error_return;
  244. }
  245. adsp_private = pdev;
  246. return 0;
  247. error_return:
  248. if (priv->boot_adsp_obj) {
  249. kobject_del(priv->boot_adsp_obj);
  250. priv->boot_adsp_obj = NULL;
  251. }
  252. return ret;
  253. }
  254. static int adsp_loader_remove(struct platform_device *pdev)
  255. {
  256. struct adsp_loader_private *priv = NULL;
  257. priv = platform_get_drvdata(pdev);
  258. if (!priv)
  259. return 0;
  260. if (priv->pil_h) {
  261. subsystem_put(priv->pil_h);
  262. priv->pil_h = NULL;
  263. }
  264. if (priv->boot_adsp_obj) {
  265. sysfs_remove_group(priv->boot_adsp_obj, priv->attr_group);
  266. kobject_del(priv->boot_adsp_obj);
  267. priv->boot_adsp_obj = NULL;
  268. }
  269. return 0;
  270. }
  271. static int adsp_loader_probe(struct platform_device *pdev)
  272. {
  273. struct adsp_loader_private *priv = NULL;
  274. struct nvmem_cell *cell;
  275. size_t len;
  276. u32 *buf;
  277. const char **adsp_fw_name_array = NULL;
  278. int adsp_fw_cnt;
  279. u32* adsp_fw_bit_values = NULL;
  280. int i;
  281. int fw_name_size;
  282. u32 adsp_var_idx = 0;
  283. int ret = 0;
  284. ret = adsp_loader_init_sysfs(pdev);
  285. if (ret != 0) {
  286. dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
  287. return ret;
  288. }
  289. priv = platform_get_drvdata(pdev);
  290. /* get adsp variant idx */
  291. cell = nvmem_cell_get(&pdev->dev, "adsp_variant");
  292. if (IS_ERR_OR_NULL(cell)) {
  293. dev_dbg(&pdev->dev, "%s: FAILED to get nvmem cell \n", __func__);
  294. goto wqueue;
  295. }
  296. buf = nvmem_cell_read(cell, &len);
  297. nvmem_cell_put(cell);
  298. if (IS_ERR_OR_NULL(buf) || len <= 0 || len > sizeof(u32)) {
  299. dev_dbg(&pdev->dev, "%s: FAILED to read nvmem cell \n", __func__);
  300. goto wqueue;
  301. }
  302. memcpy(&adsp_var_idx, buf, len);
  303. kfree(buf);
  304. /* Get count of fw images */
  305. adsp_fw_cnt = of_property_count_strings(pdev->dev.of_node,
  306. "adsp-fw-names");
  307. if (adsp_fw_cnt <= 0 || adsp_fw_cnt > MAX_FW_IMAGES) {
  308. dev_dbg(&pdev->dev, "%s: Invalid number of fw images %d",
  309. __func__, adsp_fw_cnt);
  310. goto wqueue;
  311. }
  312. adsp_fw_bit_values = devm_kzalloc(&pdev->dev,
  313. adsp_fw_cnt * sizeof(u32), GFP_KERNEL);
  314. if (!adsp_fw_bit_values)
  315. goto wqueue;
  316. /* Read bit values corresponding to each firmware image entry */
  317. ret = of_property_read_u32_array(pdev->dev.of_node,
  318. "adsp-fw-bit-values",
  319. adsp_fw_bit_values,
  320. adsp_fw_cnt);
  321. if (ret) {
  322. dev_dbg(&pdev->dev, "%s: unable to read fw-bit-values\n",
  323. __func__);
  324. goto wqueue;
  325. }
  326. adsp_fw_name_array = devm_kzalloc(&pdev->dev,
  327. adsp_fw_cnt * sizeof(char *), GFP_KERNEL);
  328. if (!adsp_fw_name_array)
  329. goto wqueue;
  330. /* Read ADSP firmware image names */
  331. ret = of_property_read_string_array(pdev->dev.of_node,
  332. "adsp-fw-names",
  333. adsp_fw_name_array,
  334. adsp_fw_cnt);
  335. if (ret < 0) {
  336. dev_dbg(&pdev->dev, "%s: unable to read fw-names\n",
  337. __func__);
  338. goto wqueue;
  339. }
  340. for (i = 0; i < adsp_fw_cnt; i++) {
  341. if (adsp_fw_bit_values[i] == adsp_var_idx) {
  342. fw_name_size = strlen(adsp_fw_name_array[i]) + 1;
  343. priv->adsp_fw_name = devm_kzalloc(&pdev->dev,
  344. fw_name_size,
  345. GFP_KERNEL);
  346. if (!priv->adsp_fw_name)
  347. goto wqueue;
  348. strlcpy(priv->adsp_fw_name, adsp_fw_name_array[i],
  349. fw_name_size);
  350. break;
  351. }
  352. }
  353. wqueue:
  354. INIT_WORK(&adsp_ldr_work, adsp_load_fw);
  355. if (adsp_fw_bit_values)
  356. devm_kfree(&pdev->dev, adsp_fw_bit_values);
  357. if (adsp_fw_name_array)
  358. devm_kfree(&pdev->dev, adsp_fw_name_array);
  359. return 0;
  360. }
  361. static const struct of_device_id adsp_loader_dt_match[] = {
  362. { .compatible = "qcom,adsp-loader" },
  363. { }
  364. };
  365. MODULE_DEVICE_TABLE(of, adsp_loader_dt_match);
  366. static struct platform_driver adsp_loader_driver = {
  367. .driver = {
  368. .name = "adsp-loader",
  369. .owner = THIS_MODULE,
  370. .of_match_table = adsp_loader_dt_match,
  371. .suppress_bind_attrs = true,
  372. },
  373. .probe = adsp_loader_probe,
  374. .remove = adsp_loader_remove,
  375. };
  376. static int __init adsp_loader_init(void)
  377. {
  378. return platform_driver_register(&adsp_loader_driver);
  379. }
  380. module_init(adsp_loader_init);
  381. static void __exit adsp_loader_exit(void)
  382. {
  383. platform_driver_unregister(&adsp_loader_driver);
  384. }
  385. module_exit(adsp_loader_exit);
  386. MODULE_DESCRIPTION("ADSP Loader module");
  387. MODULE_LICENSE("GPL v2");