1 // SPDX-License-Identifier: GPL-2.0-only
3 * Qualcomm Peripheral Image Loader helpers
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.
10 #include <linux/firmware.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/notifier.h>
14 #include <linux/remoteproc.h>
15 #include <linux/rpmsg/qcom_glink.h>
16 #include <linux/rpmsg/qcom_smd.h>
17 #include <linux/soc/qcom/mdt_loader.h>
19 #include "remoteproc_internal.h"
20 #include "qcom_common.h"
22 #define to_glink_subdev(d) container_of(d, struct qcom_rproc_glink, subdev)
23 #define to_smd_subdev(d) container_of(d, struct qcom_rproc_subdev, subdev)
24 #define to_ssr_subdev(d) container_of(d, struct qcom_rproc_ssr, subdev)
26 static BLOCKING_NOTIFIER_HEAD(ssr_notifiers
);
28 static int glink_subdev_start(struct rproc_subdev
*subdev
)
30 struct qcom_rproc_glink
*glink
= to_glink_subdev(subdev
);
32 glink
->edge
= qcom_glink_smem_register(glink
->dev
, glink
->node
);
34 return PTR_ERR_OR_ZERO(glink
->edge
);
37 static void glink_subdev_stop(struct rproc_subdev
*subdev
, bool crashed
)
39 struct qcom_rproc_glink
*glink
= to_glink_subdev(subdev
);
41 qcom_glink_smem_unregister(glink
->edge
);
46 * qcom_add_glink_subdev() - try to add a GLINK subdevice to rproc
47 * @rproc: rproc handle to parent the subdevice
48 * @glink: reference to a GLINK subdev context
50 void qcom_add_glink_subdev(struct rproc
*rproc
, struct qcom_rproc_glink
*glink
)
52 struct device
*dev
= &rproc
->dev
;
54 glink
->node
= of_get_child_by_name(dev
->parent
->of_node
, "glink-edge");
59 glink
->subdev
.start
= glink_subdev_start
;
60 glink
->subdev
.stop
= glink_subdev_stop
;
62 rproc_add_subdev(rproc
, &glink
->subdev
);
64 EXPORT_SYMBOL_GPL(qcom_add_glink_subdev
);
67 * qcom_remove_glink_subdev() - remove a GLINK subdevice from rproc
68 * @rproc: rproc handle
69 * @glink: reference to a GLINK subdev context
71 void qcom_remove_glink_subdev(struct rproc
*rproc
, struct qcom_rproc_glink
*glink
)
76 rproc_remove_subdev(rproc
, &glink
->subdev
);
77 of_node_put(glink
->node
);
79 EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev
);
82 * qcom_register_dump_segments() - register segments for coredump
83 * @rproc: remoteproc handle
84 * @fw: firmware header
86 * Register all segments of the ELF in the remoteproc coredump segment list
88 * Return: 0 on success, negative errno on failure.
90 int qcom_register_dump_segments(struct rproc
*rproc
,
91 const struct firmware
*fw
)
93 const struct elf32_phdr
*phdrs
;
94 const struct elf32_phdr
*phdr
;
95 const struct elf32_hdr
*ehdr
;
99 ehdr
= (struct elf32_hdr
*)fw
->data
;
100 phdrs
= (struct elf32_phdr
*)(ehdr
+ 1);
102 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
105 if (phdr
->p_type
!= PT_LOAD
)
108 if ((phdr
->p_flags
& QCOM_MDT_TYPE_MASK
) == QCOM_MDT_TYPE_HASH
)
114 ret
= rproc_coredump_add_segment(rproc
, phdr
->p_paddr
,
122 EXPORT_SYMBOL_GPL(qcom_register_dump_segments
);
124 static int smd_subdev_start(struct rproc_subdev
*subdev
)
126 struct qcom_rproc_subdev
*smd
= to_smd_subdev(subdev
);
128 smd
->edge
= qcom_smd_register_edge(smd
->dev
, smd
->node
);
130 return PTR_ERR_OR_ZERO(smd
->edge
);
133 static void smd_subdev_stop(struct rproc_subdev
*subdev
, bool crashed
)
135 struct qcom_rproc_subdev
*smd
= to_smd_subdev(subdev
);
137 qcom_smd_unregister_edge(smd
->edge
);
142 * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
143 * @rproc: rproc handle to parent the subdevice
144 * @smd: reference to a Qualcomm subdev context
146 void qcom_add_smd_subdev(struct rproc
*rproc
, struct qcom_rproc_subdev
*smd
)
148 struct device
*dev
= &rproc
->dev
;
150 smd
->node
= of_get_child_by_name(dev
->parent
->of_node
, "smd-edge");
155 smd
->subdev
.start
= smd_subdev_start
;
156 smd
->subdev
.stop
= smd_subdev_stop
;
158 rproc_add_subdev(rproc
, &smd
->subdev
);
160 EXPORT_SYMBOL_GPL(qcom_add_smd_subdev
);
163 * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
164 * @rproc: rproc handle
165 * @smd: the SMD subdevice to remove
167 void qcom_remove_smd_subdev(struct rproc
*rproc
, struct qcom_rproc_subdev
*smd
)
172 rproc_remove_subdev(rproc
, &smd
->subdev
);
173 of_node_put(smd
->node
);
175 EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev
);
178 * qcom_register_ssr_notifier() - register SSR notification handler
179 * @nb: notifier_block to notify for restart notifications
181 * Returns 0 on success, negative errno on failure.
183 * This register the @notify function as handler for restart notifications. As
184 * remote processors are stopped this function will be called, with the SSR
185 * name passed as a parameter.
187 int qcom_register_ssr_notifier(struct notifier_block
*nb
)
189 return blocking_notifier_chain_register(&ssr_notifiers
, nb
);
191 EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier
);
194 * qcom_unregister_ssr_notifier() - unregister SSR notification handler
195 * @nb: notifier_block to unregister
197 void qcom_unregister_ssr_notifier(struct notifier_block
*nb
)
199 blocking_notifier_chain_unregister(&ssr_notifiers
, nb
);
201 EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier
);
203 static void ssr_notify_unprepare(struct rproc_subdev
*subdev
)
205 struct qcom_rproc_ssr
*ssr
= to_ssr_subdev(subdev
);
207 blocking_notifier_call_chain(&ssr_notifiers
, 0, (void *)ssr
->name
);
211 * qcom_add_ssr_subdev() - register subdevice as restart notification source
212 * @rproc: rproc handle
213 * @ssr: SSR subdevice handle
214 * @ssr_name: identifier to use for notifications originating from @rproc
216 * As the @ssr is registered with the @rproc SSR events will be sent to all
217 * registered listeners in the system as the remoteproc is shut down.
219 void qcom_add_ssr_subdev(struct rproc
*rproc
, struct qcom_rproc_ssr
*ssr
,
220 const char *ssr_name
)
222 ssr
->name
= ssr_name
;
223 ssr
->subdev
.unprepare
= ssr_notify_unprepare
;
225 rproc_add_subdev(rproc
, &ssr
->subdev
);
227 EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev
);
230 * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
231 * @rproc: rproc handle
232 * @ssr: SSR subdevice handle
234 void qcom_remove_ssr_subdev(struct rproc
*rproc
, struct qcom_rproc_ssr
*ssr
)
236 rproc_remove_subdev(rproc
, &ssr
->subdev
);
238 EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev
);
240 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
241 MODULE_LICENSE("GPL v2");