qcom_common.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Qualcomm Peripheral Image Loader helpers
  4. *
  5. * Copyright (C) 2016 Linaro Ltd
  6. * Copyright (C) 2015 Sony Mobile Communications Inc
  7. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  8. */
  9. #include <linux/firmware.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/notifier.h>
  13. #include <linux/remoteproc.h>
  14. #include <linux/rpmsg/qcom_glink.h>
  15. #include <linux/rpmsg/qcom_smd.h>
  16. #include <linux/soc/qcom/mdt_loader.h>
  17. #include "remoteproc_internal.h"
  18. #include "qcom_common.h"
  19. #define to_glink_subdev(d) container_of(d, struct qcom_rproc_glink, subdev)
  20. #define to_smd_subdev(d) container_of(d, struct qcom_rproc_subdev, subdev)
  21. #define to_ssr_subdev(d) container_of(d, struct qcom_rproc_ssr, subdev)
  22. static BLOCKING_NOTIFIER_HEAD(ssr_notifiers);
  23. static int glink_subdev_start(struct rproc_subdev *subdev)
  24. {
  25. struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
  26. glink->edge = qcom_glink_smem_register(glink->dev, glink->node);
  27. return PTR_ERR_OR_ZERO(glink->edge);
  28. }
  29. static void glink_subdev_stop(struct rproc_subdev *subdev, bool crashed)
  30. {
  31. struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
  32. qcom_glink_smem_unregister(glink->edge);
  33. glink->edge = NULL;
  34. }
  35. /**
  36. * qcom_add_glink_subdev() - try to add a GLINK subdevice to rproc
  37. * @rproc: rproc handle to parent the subdevice
  38. * @glink: reference to a GLINK subdev context
  39. */
  40. void qcom_add_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink)
  41. {
  42. struct device *dev = &rproc->dev;
  43. glink->node = of_get_child_by_name(dev->parent->of_node, "glink-edge");
  44. if (!glink->node)
  45. return;
  46. glink->dev = dev;
  47. glink->subdev.start = glink_subdev_start;
  48. glink->subdev.stop = glink_subdev_stop;
  49. rproc_add_subdev(rproc, &glink->subdev);
  50. }
  51. EXPORT_SYMBOL_GPL(qcom_add_glink_subdev);
  52. /**
  53. * qcom_remove_glink_subdev() - remove a GLINK subdevice from rproc
  54. * @rproc: rproc handle
  55. * @glink: reference to a GLINK subdev context
  56. */
  57. void qcom_remove_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink)
  58. {
  59. if (!glink->node)
  60. return;
  61. rproc_remove_subdev(rproc, &glink->subdev);
  62. of_node_put(glink->node);
  63. }
  64. EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev);
  65. /**
  66. * qcom_register_dump_segments() - register segments for coredump
  67. * @rproc: remoteproc handle
  68. * @fw: firmware header
  69. *
  70. * Register all segments of the ELF in the remoteproc coredump segment list
  71. *
  72. * Return: 0 on success, negative errno on failure.
  73. */
  74. int qcom_register_dump_segments(struct rproc *rproc,
  75. const struct firmware *fw)
  76. {
  77. const struct elf32_phdr *phdrs;
  78. const struct elf32_phdr *phdr;
  79. const struct elf32_hdr *ehdr;
  80. int ret;
  81. int i;
  82. ehdr = (struct elf32_hdr *)fw->data;
  83. phdrs = (struct elf32_phdr *)(ehdr + 1);
  84. for (i = 0; i < ehdr->e_phnum; i++) {
  85. phdr = &phdrs[i];
  86. if (phdr->p_type != PT_LOAD)
  87. continue;
  88. if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
  89. continue;
  90. if (!phdr->p_memsz)
  91. continue;
  92. ret = rproc_coredump_add_segment(rproc, phdr->p_paddr,
  93. phdr->p_memsz);
  94. if (ret)
  95. return ret;
  96. }
  97. return 0;
  98. }
  99. EXPORT_SYMBOL_GPL(qcom_register_dump_segments);
  100. static int smd_subdev_start(struct rproc_subdev *subdev)
  101. {
  102. struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
  103. smd->edge = qcom_smd_register_edge(smd->dev, smd->node);
  104. return PTR_ERR_OR_ZERO(smd->edge);
  105. }
  106. static void smd_subdev_stop(struct rproc_subdev *subdev, bool crashed)
  107. {
  108. struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
  109. qcom_smd_unregister_edge(smd->edge);
  110. smd->edge = NULL;
  111. }
  112. /**
  113. * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
  114. * @rproc: rproc handle to parent the subdevice
  115. * @smd: reference to a Qualcomm subdev context
  116. */
  117. void qcom_add_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
  118. {
  119. struct device *dev = &rproc->dev;
  120. smd->node = of_get_child_by_name(dev->parent->of_node, "smd-edge");
  121. if (!smd->node)
  122. return;
  123. smd->dev = dev;
  124. smd->subdev.start = smd_subdev_start;
  125. smd->subdev.stop = smd_subdev_stop;
  126. rproc_add_subdev(rproc, &smd->subdev);
  127. }
  128. EXPORT_SYMBOL_GPL(qcom_add_smd_subdev);
  129. /**
  130. * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
  131. * @rproc: rproc handle
  132. * @smd: the SMD subdevice to remove
  133. */
  134. void qcom_remove_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
  135. {
  136. if (!smd->node)
  137. return;
  138. rproc_remove_subdev(rproc, &smd->subdev);
  139. of_node_put(smd->node);
  140. }
  141. EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev);
  142. /**
  143. * qcom_register_ssr_notifier() - register SSR notification handler
  144. * @nb: notifier_block to notify for restart notifications
  145. *
  146. * Returns 0 on success, negative errno on failure.
  147. *
  148. * This register the @notify function as handler for restart notifications. As
  149. * remote processors are stopped this function will be called, with the SSR
  150. * name passed as a parameter.
  151. */
  152. int qcom_register_ssr_notifier(struct notifier_block *nb)
  153. {
  154. return blocking_notifier_chain_register(&ssr_notifiers, nb);
  155. }
  156. EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier);
  157. /**
  158. * qcom_unregister_ssr_notifier() - unregister SSR notification handler
  159. * @nb: notifier_block to unregister
  160. */
  161. void qcom_unregister_ssr_notifier(struct notifier_block *nb)
  162. {
  163. blocking_notifier_chain_unregister(&ssr_notifiers, nb);
  164. }
  165. EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier);
  166. static void ssr_notify_unprepare(struct rproc_subdev *subdev)
  167. {
  168. struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
  169. blocking_notifier_call_chain(&ssr_notifiers, 0, (void *)ssr->name);
  170. }
  171. /**
  172. * qcom_add_ssr_subdev() - register subdevice as restart notification source
  173. * @rproc: rproc handle
  174. * @ssr: SSR subdevice handle
  175. * @ssr_name: identifier to use for notifications originating from @rproc
  176. *
  177. * As the @ssr is registered with the @rproc SSR events will be sent to all
  178. * registered listeners in the system as the remoteproc is shut down.
  179. */
  180. void qcom_add_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr,
  181. const char *ssr_name)
  182. {
  183. ssr->name = ssr_name;
  184. ssr->subdev.unprepare = ssr_notify_unprepare;
  185. rproc_add_subdev(rproc, &ssr->subdev);
  186. }
  187. EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev);
  188. /**
  189. * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
  190. * @rproc: rproc handle
  191. * @ssr: SSR subdevice handle
  192. */
  193. void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr)
  194. {
  195. rproc_remove_subdev(rproc, &ssr->subdev);
  196. }
  197. EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
  198. MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
  199. MODULE_LICENSE("GPL v2");