qcom_common.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_probe(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_remove(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. rproc_add_subdev(rproc, &glink->subdev, glink_subdev_probe, glink_subdev_remove);
  56. }
  57. EXPORT_SYMBOL_GPL(qcom_add_glink_subdev);
  58. /**
  59. * qcom_remove_glink_subdev() - remove a GLINK subdevice from rproc
  60. * @rproc: rproc handle
  61. * @glink: reference to a GLINK subdev context
  62. */
  63. void qcom_remove_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink)
  64. {
  65. if (!glink->node)
  66. return;
  67. rproc_remove_subdev(rproc, &glink->subdev);
  68. of_node_put(glink->node);
  69. }
  70. EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev);
  71. /**
  72. * qcom_register_dump_segments() - register segments for coredump
  73. * @rproc: remoteproc handle
  74. * @fw: firmware header
  75. *
  76. * Register all segments of the ELF in the remoteproc coredump segment list
  77. *
  78. * Return: 0 on success, negative errno on failure.
  79. */
  80. int qcom_register_dump_segments(struct rproc *rproc,
  81. const struct firmware *fw)
  82. {
  83. const struct elf32_phdr *phdrs;
  84. const struct elf32_phdr *phdr;
  85. const struct elf32_hdr *ehdr;
  86. int ret;
  87. int i;
  88. ehdr = (struct elf32_hdr *)fw->data;
  89. phdrs = (struct elf32_phdr *)(ehdr + 1);
  90. for (i = 0; i < ehdr->e_phnum; i++) {
  91. phdr = &phdrs[i];
  92. if (phdr->p_type != PT_LOAD)
  93. continue;
  94. if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
  95. continue;
  96. if (!phdr->p_memsz)
  97. continue;
  98. ret = rproc_coredump_add_segment(rproc, phdr->p_paddr,
  99. phdr->p_memsz);
  100. if (ret)
  101. return ret;
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(qcom_register_dump_segments);
  106. static int smd_subdev_probe(struct rproc_subdev *subdev)
  107. {
  108. struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
  109. smd->edge = qcom_smd_register_edge(smd->dev, smd->node);
  110. return PTR_ERR_OR_ZERO(smd->edge);
  111. }
  112. static void smd_subdev_remove(struct rproc_subdev *subdev, bool crashed)
  113. {
  114. struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
  115. qcom_smd_unregister_edge(smd->edge);
  116. smd->edge = NULL;
  117. }
  118. /**
  119. * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
  120. * @rproc: rproc handle to parent the subdevice
  121. * @smd: reference to a Qualcomm subdev context
  122. */
  123. void qcom_add_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
  124. {
  125. struct device *dev = &rproc->dev;
  126. smd->node = of_get_child_by_name(dev->parent->of_node, "smd-edge");
  127. if (!smd->node)
  128. return;
  129. smd->dev = dev;
  130. rproc_add_subdev(rproc, &smd->subdev, smd_subdev_probe, smd_subdev_remove);
  131. }
  132. EXPORT_SYMBOL_GPL(qcom_add_smd_subdev);
  133. /**
  134. * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
  135. * @rproc: rproc handle
  136. * @smd: the SMD subdevice to remove
  137. */
  138. void qcom_remove_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
  139. {
  140. if (!smd->node)
  141. return;
  142. rproc_remove_subdev(rproc, &smd->subdev);
  143. of_node_put(smd->node);
  144. }
  145. EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev);
  146. /**
  147. * qcom_register_ssr_notifier() - register SSR notification handler
  148. * @nb: notifier_block to notify for restart notifications
  149. *
  150. * Returns 0 on success, negative errno on failure.
  151. *
  152. * This register the @notify function as handler for restart notifications. As
  153. * remote processors are stopped this function will be called, with the SSR
  154. * name passed as a parameter.
  155. */
  156. int qcom_register_ssr_notifier(struct notifier_block *nb)
  157. {
  158. return blocking_notifier_chain_register(&ssr_notifiers, nb);
  159. }
  160. EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier);
  161. /**
  162. * qcom_unregister_ssr_notifier() - unregister SSR notification handler
  163. * @nb: notifier_block to unregister
  164. */
  165. void qcom_unregister_ssr_notifier(struct notifier_block *nb)
  166. {
  167. blocking_notifier_chain_unregister(&ssr_notifiers, nb);
  168. }
  169. EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier);
  170. static int ssr_notify_start(struct rproc_subdev *subdev)
  171. {
  172. return 0;
  173. }
  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. rproc_add_subdev(rproc, &ssr->subdev, ssr_notify_start, ssr_notify_stop);
  193. }
  194. EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev);
  195. /**
  196. * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
  197. * @rproc: rproc handle
  198. * @ssr: SSR subdevice handle
  199. */
  200. void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr)
  201. {
  202. rproc_remove_subdev(rproc, &ssr->subdev);
  203. }
  204. EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
  205. MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
  206. MODULE_LICENSE("GPL v2");