cdsp-loader.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 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. }
  133. }
  134. static int cdsp_loader_init_sysfs(struct platform_device *pdev)
  135. {
  136. int ret = -EINVAL;
  137. struct cdsp_loader_private *priv = NULL;
  138. cdsp_private = NULL;
  139. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  140. if (!priv) {
  141. ret = -ENOMEM;
  142. return ret;
  143. }
  144. platform_set_drvdata(pdev, priv);
  145. priv->pil_h = NULL;
  146. priv->boot_cdsp_obj = NULL;
  147. priv->attr_group = devm_kzalloc(&pdev->dev,
  148. sizeof(*(priv->attr_group)),
  149. GFP_KERNEL);
  150. if (!priv->attr_group) {
  151. ret = -ENOMEM;
  152. goto error_return;
  153. }
  154. priv->attr_group->attrs = attrs;
  155. priv->boot_cdsp_obj = kobject_create_and_add("boot_cdsp", kernel_kobj);
  156. if (!priv->boot_cdsp_obj) {
  157. dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
  158. __func__);
  159. ret = -ENOMEM;
  160. goto error_return;
  161. }
  162. ret = sysfs_create_group(priv->boot_cdsp_obj, priv->attr_group);
  163. if (ret) {
  164. dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
  165. __func__, ret);
  166. goto error_return;
  167. }
  168. cdsp_private = pdev;
  169. return 0;
  170. error_return:
  171. if (priv->boot_cdsp_obj) {
  172. kobject_del(priv->boot_cdsp_obj);
  173. priv->boot_cdsp_obj = NULL;
  174. }
  175. if (ret)
  176. dev_err(&pdev->dev, "%s failed with ret %d\n",
  177. __func__, ret);
  178. return ret;
  179. }
  180. static int cdsp_loader_remove(struct platform_device *pdev)
  181. {
  182. struct cdsp_loader_private *priv = NULL;
  183. priv = platform_get_drvdata(pdev);
  184. if (!priv)
  185. return 0;
  186. if (priv->pil_h) {
  187. rproc_shutdown(priv->pil_h);
  188. priv->pil_h = NULL;
  189. }
  190. if (priv->boot_cdsp_obj) {
  191. sysfs_remove_group(priv->boot_cdsp_obj, priv->attr_group);
  192. kobject_del(priv->boot_cdsp_obj);
  193. priv->boot_cdsp_obj = NULL;
  194. }
  195. return 0;
  196. }
  197. static int cdsp_loader_probe(struct platform_device *pdev)
  198. {
  199. phandle rproc_phandle;
  200. struct property *prop = NULL;
  201. int size = 0;
  202. struct rproc *cdsp = NULL;
  203. int ret = 0;
  204. prop = of_find_property(pdev->dev.of_node, "qcom,rproc-handle", &size);
  205. if (!prop) {
  206. dev_err(&pdev->dev, "%s: error reading rproc phandle\n", __func__);
  207. return -ENOPARAM;
  208. }
  209. rproc_phandle = be32_to_cpup(prop->value);
  210. cdsp = rproc_get_by_phandle(rproc_phandle);
  211. if (!cdsp) {
  212. dev_err(&pdev->dev, "%s: rproc not found\n", __func__);
  213. return -EPROBE_DEFER;
  214. }
  215. ret = cdsp_loader_init_sysfs(pdev);
  216. if (ret != 0) {
  217. dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
  218. return ret;
  219. }
  220. return 0;
  221. }
  222. static const struct of_device_id cdsp_loader_dt_match[] = {
  223. { .compatible = "qcom,cdsp-loader" },
  224. { }
  225. };
  226. MODULE_DEVICE_TABLE(of, cdsp_loader_dt_match);
  227. static struct platform_driver cdsp_loader_driver = {
  228. .driver = {
  229. .name = "cdsp-loader",
  230. .of_match_table = cdsp_loader_dt_match,
  231. },
  232. .probe = cdsp_loader_probe,
  233. .remove = cdsp_loader_remove,
  234. };
  235. static int __init cdsp_loader_init(void)
  236. {
  237. return platform_driver_register(&cdsp_loader_driver);
  238. }
  239. module_init(cdsp_loader_init);
  240. static void __exit cdsp_loader_exit(void)
  241. {
  242. platform_driver_unregister(&cdsp_loader_driver);
  243. }
  244. module_exit(cdsp_loader_exit);
  245. MODULE_DESCRIPTION("CDSP Loader module");
  246. MODULE_LICENSE("GPL v2");