adsp-loader.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012-2014, 2017-2018, 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/delay.h>
  10. #include <linux/platform_device.h>
  11. #include <ipc/apr.h>
  12. #include <linux/of_device.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/workqueue.h>
  15. #include <soc/qcom/subsystem_restart.h>
  16. #define Q6_PIL_GET_DELAY_MS 100
  17. #define BOOT_CMD 1
  18. #define SSR_RESET_CMD 1
  19. #define IMAGE_UNLOAD_CMD 0
  20. static ssize_t adsp_boot_store(struct kobject *kobj,
  21. struct kobj_attribute *attr,
  22. const char *buf, size_t count);
  23. static ssize_t adsp_ssr_store(struct kobject *kobj,
  24. struct kobj_attribute *attr,
  25. const char *buf, size_t count);
  26. struct adsp_loader_private {
  27. void *pil_h;
  28. struct kobject *boot_adsp_obj;
  29. struct attribute_group *attr_group;
  30. };
  31. static struct kobj_attribute adsp_boot_attribute =
  32. __ATTR(boot, 0220, NULL, adsp_boot_store);
  33. static struct kobj_attribute adsp_ssr_attribute =
  34. __ATTR(ssr, 0220, NULL, adsp_ssr_store);
  35. static struct attribute *attrs[] = {
  36. &adsp_boot_attribute.attr,
  37. &adsp_ssr_attribute.attr,
  38. NULL,
  39. };
  40. static struct work_struct adsp_ldr_work;
  41. static struct platform_device *adsp_private;
  42. static void adsp_loader_unload(struct platform_device *pdev);
  43. static void adsp_load_fw(struct work_struct *adsp_ldr_work)
  44. {
  45. struct platform_device *pdev = adsp_private;
  46. struct adsp_loader_private *priv = NULL;
  47. const char *adsp_dt = "qcom,adsp-state";
  48. int rc = 0;
  49. u32 adsp_state;
  50. const char *img_name;
  51. if (!pdev) {
  52. dev_err(&pdev->dev, "%s: Platform device null\n", __func__);
  53. goto fail;
  54. }
  55. if (!pdev->dev.of_node) {
  56. dev_err(&pdev->dev,
  57. "%s: Device tree information missing\n", __func__);
  58. goto fail;
  59. }
  60. rc = of_property_read_u32(pdev->dev.of_node, adsp_dt, &adsp_state);
  61. if (rc) {
  62. dev_err(&pdev->dev,
  63. "%s: ADSP state = %x\n", __func__, adsp_state);
  64. goto fail;
  65. }
  66. rc = of_property_read_string(pdev->dev.of_node,
  67. "qcom,proc-img-to-load",
  68. &img_name);
  69. if (rc) {
  70. dev_dbg(&pdev->dev,
  71. "%s: loading default image ADSP\n", __func__);
  72. goto load_adsp;
  73. }
  74. if (!strcmp(img_name, "modem")) {
  75. /* adsp_state always returns "0". So load modem image based on
  76. * apr_modem_state to prevent loading of image twice
  77. */
  78. adsp_state = apr_get_modem_state();
  79. if (adsp_state == APR_SUBSYS_DOWN) {
  80. priv = platform_get_drvdata(pdev);
  81. if (!priv) {
  82. dev_err(&pdev->dev,
  83. " %s: Private data get failed\n", __func__);
  84. goto fail;
  85. }
  86. priv->pil_h = subsystem_get("modem");
  87. if (IS_ERR(priv->pil_h)) {
  88. dev_err(&pdev->dev, "%s: pil get failed,\n",
  89. __func__);
  90. goto fail;
  91. }
  92. /* Set the state of the ADSP in APR driver */
  93. apr_set_modem_state(APR_SUBSYS_LOADED);
  94. } else if (adsp_state == APR_SUBSYS_LOADED) {
  95. dev_dbg(&pdev->dev,
  96. "%s: MDSP state = %x\n", __func__, adsp_state);
  97. }
  98. dev_dbg(&pdev->dev, "%s: Q6/MDSP image is loaded\n", __func__);
  99. return;
  100. }
  101. load_adsp:
  102. {
  103. adsp_state = apr_get_q6_state();
  104. if (adsp_state == APR_SUBSYS_DOWN) {
  105. priv = platform_get_drvdata(pdev);
  106. if (!priv) {
  107. dev_err(&pdev->dev,
  108. " %s: Private data get failed\n", __func__);
  109. goto fail;
  110. }
  111. priv->pil_h = subsystem_get("adsp");
  112. if (IS_ERR(priv->pil_h)) {
  113. dev_err(&pdev->dev, "%s: pil get failed,\n",
  114. __func__);
  115. goto fail;
  116. }
  117. } else if (adsp_state == APR_SUBSYS_LOADED) {
  118. dev_dbg(&pdev->dev,
  119. "%s: ADSP state = %x\n", __func__, adsp_state);
  120. }
  121. dev_dbg(&pdev->dev, "%s: Q6/ADSP image is loaded\n", __func__);
  122. return;
  123. }
  124. fail:
  125. dev_err(&pdev->dev, "%s: Q6 image loading failed\n", __func__);
  126. }
  127. static void adsp_loader_do(struct platform_device *pdev)
  128. {
  129. schedule_work(&adsp_ldr_work);
  130. }
  131. static ssize_t adsp_ssr_store(struct kobject *kobj,
  132. struct kobj_attribute *attr,
  133. const char *buf,
  134. size_t count)
  135. {
  136. int ssr_command = 0;
  137. struct subsys_device *adsp_dev = NULL;
  138. struct platform_device *pdev = adsp_private;
  139. struct adsp_loader_private *priv = NULL;
  140. int rc;
  141. dev_dbg(&pdev->dev, "%s: going to call adsp ssr\n ", __func__);
  142. if (kstrtoint(buf, 10, &ssr_command) < 0)
  143. return -EINVAL;
  144. if (ssr_command != SSR_RESET_CMD)
  145. return -EINVAL;
  146. priv = platform_get_drvdata(pdev);
  147. if (!priv)
  148. return -EINVAL;
  149. adsp_dev = (struct subsys_device *)priv->pil_h;
  150. if (!adsp_dev)
  151. return -EINVAL;
  152. dev_err(&pdev->dev, "requesting for ADSP restart\n");
  153. /* subsystem_restart_dev has worker queue to handle */
  154. rc = subsystem_restart_dev(adsp_dev);
  155. if (rc) {
  156. dev_err(&pdev->dev, "subsystem_restart_dev failed\n");
  157. return rc;
  158. }
  159. dev_dbg(&pdev->dev, "ADSP restarted\n");
  160. return count;
  161. }
  162. static ssize_t adsp_boot_store(struct kobject *kobj,
  163. struct kobj_attribute *attr,
  164. const char *buf,
  165. size_t count)
  166. {
  167. int boot = 0;
  168. if (sscanf(buf, "%du", &boot) != 1) {
  169. pr_err("%s: failed to read boot info from string\n", __func__);
  170. return -EINVAL;
  171. }
  172. if (boot == BOOT_CMD) {
  173. pr_debug("%s: going to call adsp_loader_do\n", __func__);
  174. adsp_loader_do(adsp_private);
  175. } else if (boot == IMAGE_UNLOAD_CMD) {
  176. pr_debug("%s: going to call adsp_unloader\n", __func__);
  177. adsp_loader_unload(adsp_private);
  178. }
  179. return count;
  180. }
  181. static void adsp_loader_unload(struct platform_device *pdev)
  182. {
  183. struct adsp_loader_private *priv = NULL;
  184. priv = platform_get_drvdata(pdev);
  185. if (!priv)
  186. return;
  187. if (priv->pil_h) {
  188. dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__);
  189. subsystem_put(priv->pil_h);
  190. priv->pil_h = NULL;
  191. }
  192. }
  193. static int adsp_loader_init_sysfs(struct platform_device *pdev)
  194. {
  195. int ret = -EINVAL;
  196. struct adsp_loader_private *priv = NULL;
  197. adsp_private = NULL;
  198. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  199. if (!priv) {
  200. ret = -ENOMEM;
  201. return ret;
  202. }
  203. platform_set_drvdata(pdev, priv);
  204. priv->pil_h = NULL;
  205. priv->boot_adsp_obj = NULL;
  206. priv->attr_group = devm_kzalloc(&pdev->dev,
  207. sizeof(*(priv->attr_group)),
  208. GFP_KERNEL);
  209. if (!priv->attr_group) {
  210. ret = -ENOMEM;
  211. goto error_return;
  212. }
  213. priv->attr_group->attrs = attrs;
  214. priv->boot_adsp_obj = kobject_create_and_add("boot_adsp", kernel_kobj);
  215. if (!priv->boot_adsp_obj) {
  216. dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
  217. __func__);
  218. ret = -ENOMEM;
  219. goto error_return;
  220. }
  221. ret = sysfs_create_group(priv->boot_adsp_obj, priv->attr_group);
  222. if (ret) {
  223. dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
  224. __func__, ret);
  225. goto error_return;
  226. }
  227. adsp_private = pdev;
  228. return 0;
  229. error_return:
  230. if (priv->boot_adsp_obj) {
  231. kobject_del(priv->boot_adsp_obj);
  232. priv->boot_adsp_obj = NULL;
  233. }
  234. return ret;
  235. }
  236. static int adsp_loader_remove(struct platform_device *pdev)
  237. {
  238. struct adsp_loader_private *priv = NULL;
  239. priv = platform_get_drvdata(pdev);
  240. if (!priv)
  241. return 0;
  242. if (priv->pil_h) {
  243. subsystem_put(priv->pil_h);
  244. priv->pil_h = NULL;
  245. }
  246. if (priv->boot_adsp_obj) {
  247. sysfs_remove_group(priv->boot_adsp_obj, priv->attr_group);
  248. kobject_del(priv->boot_adsp_obj);
  249. priv->boot_adsp_obj = NULL;
  250. }
  251. return 0;
  252. }
  253. static int adsp_loader_probe(struct platform_device *pdev)
  254. {
  255. int ret = adsp_loader_init_sysfs(pdev);
  256. if (ret != 0) {
  257. dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
  258. return ret;
  259. }
  260. INIT_WORK(&adsp_ldr_work, adsp_load_fw);
  261. return 0;
  262. }
  263. static const struct of_device_id adsp_loader_dt_match[] = {
  264. { .compatible = "qcom,adsp-loader" },
  265. { }
  266. };
  267. MODULE_DEVICE_TABLE(of, adsp_loader_dt_match);
  268. static struct platform_driver adsp_loader_driver = {
  269. .driver = {
  270. .name = "adsp-loader",
  271. .owner = THIS_MODULE,
  272. .of_match_table = adsp_loader_dt_match,
  273. },
  274. .probe = adsp_loader_probe,
  275. .remove = adsp_loader_remove,
  276. };
  277. static int __init adsp_loader_init(void)
  278. {
  279. return platform_driver_register(&adsp_loader_driver);
  280. }
  281. module_init(adsp_loader_init);
  282. static void __exit adsp_loader_exit(void)
  283. {
  284. platform_driver_unregister(&adsp_loader_driver);
  285. }
  286. module_exit(adsp_loader_exit);
  287. MODULE_DESCRIPTION("ADSP Loader module");
  288. MODULE_LICENSE("GPL v2");