cdsp-loader.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2014, 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/err.h>
  10. #include <linux/delay.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/of_device.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/remoteproc.h>
  15. #define BOOT_CMD 1
  16. #define IMAGE_UNLOAD_CMD 0
  17. #define CDSP_SUBSYS_DOWN 0
  18. #define CDSP_SUBSYS_LOADED 1
  19. static ssize_t cdsp_boot_store(struct kobject *kobj,
  20. struct kobj_attribute *attr,
  21. const char *buf, size_t count);
  22. struct cdsp_loader_private {
  23. void *pil_h;
  24. struct kobject *boot_cdsp_obj;
  25. struct attribute_group *attr_group;
  26. };
  27. static struct kobj_attribute cdsp_boot_attribute =
  28. __ATTR(boot, 0220, NULL, cdsp_boot_store);
  29. static struct attribute *attrs[] = {
  30. &cdsp_boot_attribute.attr,
  31. NULL,
  32. };
  33. static u32 cdsp_state = CDSP_SUBSYS_DOWN;
  34. static struct platform_device *cdsp_private;
  35. static void cdsp_loader_unload(struct platform_device *pdev);
  36. static int cdsp_loader_do(struct platform_device *pdev)
  37. {
  38. struct cdsp_loader_private *priv = NULL;
  39. phandle rproc_phandle;
  40. int rc = 0, sz = 0;
  41. const char *img_name;
  42. if (!pdev) {
  43. pr_err("%s: Platform device null\n", __func__);
  44. goto fail;
  45. }
  46. if (!pdev->dev.of_node) {
  47. dev_err(&pdev->dev,
  48. "%s: Device tree information missing\n", __func__);
  49. goto fail;
  50. }
  51. rc = of_property_read_string(pdev->dev.of_node,
  52. "qcom,proc-img-to-load",
  53. &img_name);
  54. if (rc)
  55. goto fail;
  56. if (!strcmp(img_name, "cdsp")) {
  57. /* cdsp_state always returns "0".*/
  58. if (cdsp_state == CDSP_SUBSYS_DOWN) {
  59. priv = platform_get_drvdata(pdev);
  60. if (!priv) {
  61. dev_err(&pdev->dev,
  62. "%s: Private data get failed\n", __func__);
  63. goto fail;
  64. }
  65. sz = of_property_read_u32(pdev->dev.of_node, "qcom,rproc-handle",
  66. &rproc_phandle);
  67. if (sz) {
  68. pr_err("%s: of_property_read failed, returned value %d\n",
  69. __func__, sz);
  70. dev_err(&pdev->dev, "error reading rproc phandle\n");
  71. goto fail;
  72. }
  73. priv->pil_h = rproc_get_by_phandle(rproc_phandle);
  74. if (!priv->pil_h) {
  75. dev_err(&pdev->dev, "rproc not found\n");
  76. goto fail;
  77. }
  78. dev_dbg(&pdev->dev, "%s: calling rproc_boot on %s\n",
  79. __func__, img_name);
  80. rc = rproc_boot(priv->pil_h);
  81. if (rc) {
  82. dev_err(&pdev->dev, "%s: rproc_boot failed with error %d\n",
  83. __func__, rc);
  84. goto fail;
  85. }
  86. /* Set the state of the CDSP.*/
  87. cdsp_state = CDSP_SUBSYS_LOADED;
  88. } else if (cdsp_state == CDSP_SUBSYS_LOADED) {
  89. dev_dbg(&pdev->dev,
  90. "%s: CDSP state = 0x%x\n", __func__, cdsp_state);
  91. }
  92. dev_dbg(&pdev->dev, "%s: CDSP image is loaded\n", __func__);
  93. return rc;
  94. }
  95. fail:
  96. if (pdev)
  97. dev_err(&pdev->dev,
  98. "%s: CDSP image loading failed\n", __func__);
  99. return rc;
  100. }
  101. static ssize_t cdsp_boot_store(struct kobject *kobj,
  102. struct kobj_attribute *attr,
  103. const char *buf,
  104. size_t count)
  105. {
  106. int ret = 0;
  107. uint32_t boot = 0;
  108. ret = kstrtou32(buf, 0, &boot);
  109. if (ret) {
  110. pr_debug("%s: invalid arguments for cdsp_loader.\n", __func__);
  111. return ret;
  112. }
  113. if (boot == BOOT_CMD) {
  114. pr_debug("%s: going to call cdsp_loader_do\n", __func__);
  115. cdsp_loader_do(cdsp_private);
  116. } else if (boot == IMAGE_UNLOAD_CMD) {
  117. pr_debug("%s: going to call cdsp_unloader\n", __func__);
  118. cdsp_loader_unload(cdsp_private);
  119. }
  120. return count;
  121. }
  122. static void cdsp_loader_unload(struct platform_device *pdev)
  123. {
  124. struct cdsp_loader_private *priv = NULL;
  125. priv = platform_get_drvdata(pdev);
  126. if (!priv)
  127. return;
  128. if (priv->pil_h) {
  129. dev_dbg(&pdev->dev, "%s: calling subsystem_put\n", __func__);
  130. rproc_shutdown(priv->pil_h);
  131. priv->pil_h = NULL;
  132. cdsp_state = CDSP_SUBSYS_DOWN;
  133. }
  134. }
  135. static int cdsp_loader_init_sysfs(struct platform_device *pdev)
  136. {
  137. int ret = -EINVAL;
  138. struct cdsp_loader_private *priv = NULL;
  139. cdsp_private = NULL;
  140. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  141. if (!priv) {
  142. ret = -ENOMEM;
  143. return ret;
  144. }
  145. platform_set_drvdata(pdev, priv);
  146. priv->pil_h = NULL;
  147. priv->boot_cdsp_obj = NULL;
  148. priv->attr_group = devm_kzalloc(&pdev->dev,
  149. sizeof(*(priv->attr_group)),
  150. GFP_KERNEL);
  151. if (!priv->attr_group) {
  152. ret = -ENOMEM;
  153. goto error_return;
  154. }
  155. priv->attr_group->attrs = attrs;
  156. priv->boot_cdsp_obj = kobject_create_and_add("boot_cdsp", kernel_kobj);
  157. if (!priv->boot_cdsp_obj) {
  158. dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
  159. __func__);
  160. ret = -ENOMEM;
  161. goto error_return;
  162. }
  163. ret = sysfs_create_group(priv->boot_cdsp_obj, priv->attr_group);
  164. if (ret) {
  165. dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
  166. __func__, ret);
  167. goto error_return;
  168. }
  169. cdsp_private = pdev;
  170. return 0;
  171. error_return:
  172. if (priv->boot_cdsp_obj) {
  173. kobject_del(priv->boot_cdsp_obj);
  174. priv->boot_cdsp_obj = NULL;
  175. }
  176. if (ret)
  177. dev_err(&pdev->dev, "%s failed with ret %d\n",
  178. __func__, ret);
  179. return ret;
  180. }
  181. static int cdsp_loader_remove(struct platform_device *pdev)
  182. {
  183. struct cdsp_loader_private *priv = NULL;
  184. priv = platform_get_drvdata(pdev);
  185. if (!priv)
  186. return 0;
  187. if (priv->pil_h) {
  188. rproc_shutdown(priv->pil_h);
  189. priv->pil_h = NULL;
  190. cdsp_state = CDSP_SUBSYS_DOWN;
  191. }
  192. if (priv->boot_cdsp_obj) {
  193. sysfs_remove_group(priv->boot_cdsp_obj, priv->attr_group);
  194. kobject_del(priv->boot_cdsp_obj);
  195. priv->boot_cdsp_obj = NULL;
  196. }
  197. return 0;
  198. }
  199. static int cdsp_loader_probe(struct platform_device *pdev)
  200. {
  201. phandle rproc_phandle;
  202. struct property *prop = NULL;
  203. int size = 0;
  204. struct rproc *cdsp = NULL;
  205. int ret = 0;
  206. prop = of_find_property(pdev->dev.of_node, "qcom,rproc-handle", &size);
  207. if (!prop) {
  208. dev_err(&pdev->dev, "%s: error reading rproc phandle\n", __func__);
  209. return -ENOPARAM;
  210. }
  211. rproc_phandle = be32_to_cpup(prop->value);
  212. cdsp = rproc_get_by_phandle(rproc_phandle);
  213. if (!cdsp) {
  214. dev_err(&pdev->dev, "%s: rproc not found\n", __func__);
  215. return -EPROBE_DEFER;
  216. }
  217. ret = cdsp_loader_init_sysfs(pdev);
  218. if (ret != 0) {
  219. dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. static const struct of_device_id cdsp_loader_dt_match[] = {
  225. { .compatible = "qcom,cdsp-loader" },
  226. { }
  227. };
  228. MODULE_DEVICE_TABLE(of, cdsp_loader_dt_match);
  229. static struct platform_driver cdsp_loader_driver = {
  230. .driver = {
  231. .name = "cdsp-loader",
  232. .of_match_table = cdsp_loader_dt_match,
  233. },
  234. .probe = cdsp_loader_probe,
  235. .remove = cdsp_loader_remove,
  236. };
  237. static int __init cdsp_loader_init(void)
  238. {
  239. return platform_driver_register(&cdsp_loader_driver);
  240. }
  241. module_init(cdsp_loader_init);
  242. static void __exit cdsp_loader_exit(void)
  243. {
  244. platform_driver_unregister(&cdsp_loader_driver);
  245. }
  246. module_exit(cdsp_loader_exit);
  247. MODULE_DESCRIPTION("CDSP Loader module");
  248. MODULE_LICENSE("GPL v2");