qcom_common.c 6.8 KB

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