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 
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>
18 
19 #include "remoteproc_internal.h"
20 #include "qcom_common.h"
21 
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)
25 
26 static BLOCKING_NOTIFIER_HEAD(ssr_notifiers);
27 
28 static int glink_subdev_start(struct rproc_subdev *subdev)
29 {
30 	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
31 
32 	glink->edge = qcom_glink_smem_register(glink->dev, glink->node);
33 
34 	return PTR_ERR_OR_ZERO(glink->edge);
35 }
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 
41 	qcom_glink_smem_unregister(glink->edge);
42 	glink->edge = NULL;
43 }
44 
45 static void glink_subdev_unprepare(struct rproc_subdev *subdev)
46 {
47 	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
48 
49 	qcom_glink_ssr_notify(glink->ssr_name);
50 }
51 
52 /**
53  * qcom_add_glink_subdev() - try to add a GLINK subdevice to rproc
54  * @rproc:	rproc handle to parent the subdevice
55  * @glink:	reference to a GLINK subdev context
56  * @ssr_name:	identifier of the associated remoteproc for ssr notifications
57  */
58 void qcom_add_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink,
59 			   const char *ssr_name)
60 {
61 	struct device *dev = &rproc->dev;
62 
63 	glink->node = of_get_child_by_name(dev->parent->of_node, "glink-edge");
64 	if (!glink->node)
65 		return;
66 
67 	glink->ssr_name = kstrdup_const(ssr_name, GFP_KERNEL);
68 	if (!glink->ssr_name)
69 		return;
70 
71 	glink->dev = dev;
72 	glink->subdev.start = glink_subdev_start;
73 	glink->subdev.stop = glink_subdev_stop;
74 	glink->subdev.unprepare = glink_subdev_unprepare;
75 
76 	rproc_add_subdev(rproc, &glink->subdev);
77 }
78 EXPORT_SYMBOL_GPL(qcom_add_glink_subdev);
79 
80 /**
81  * qcom_remove_glink_subdev() - remove a GLINK subdevice from rproc
82  * @rproc:	rproc handle
83  * @glink:	reference to a GLINK subdev context
84  */
85 void qcom_remove_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink)
86 {
87 	if (!glink->node)
88 		return;
89 
90 	rproc_remove_subdev(rproc, &glink->subdev);
91 	kfree_const(glink->ssr_name);
92 	of_node_put(glink->node);
93 }
94 EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev);
95 
96 /**
97  * qcom_register_dump_segments() - register segments for coredump
98  * @rproc:	remoteproc handle
99  * @fw:		firmware header
100  *
101  * Register all segments of the ELF in the remoteproc coredump segment list
102  *
103  * Return: 0 on success, negative errno on failure.
104  */
105 int qcom_register_dump_segments(struct rproc *rproc,
106 				const struct firmware *fw)
107 {
108 	const struct elf32_phdr *phdrs;
109 	const struct elf32_phdr *phdr;
110 	const struct elf32_hdr *ehdr;
111 	int ret;
112 	int i;
113 
114 	ehdr = (struct elf32_hdr *)fw->data;
115 	phdrs = (struct elf32_phdr *)(ehdr + 1);
116 
117 	for (i = 0; i < ehdr->e_phnum; i++) {
118 		phdr = &phdrs[i];
119 
120 		if (phdr->p_type != PT_LOAD)
121 			continue;
122 
123 		if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
124 			continue;
125 
126 		if (!phdr->p_memsz)
127 			continue;
128 
129 		ret = rproc_coredump_add_segment(rproc, phdr->p_paddr,
130 						 phdr->p_memsz);
131 		if (ret)
132 			return ret;
133 	}
134 
135 	return 0;
136 }
137 EXPORT_SYMBOL_GPL(qcom_register_dump_segments);
138 
139 static int smd_subdev_start(struct rproc_subdev *subdev)
140 {
141 	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
142 
143 	smd->edge = qcom_smd_register_edge(smd->dev, smd->node);
144 
145 	return PTR_ERR_OR_ZERO(smd->edge);
146 }
147 
148 static void smd_subdev_stop(struct rproc_subdev *subdev, bool crashed)
149 {
150 	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
151 
152 	qcom_smd_unregister_edge(smd->edge);
153 	smd->edge = NULL;
154 }
155 
156 /**
157  * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
158  * @rproc:	rproc handle to parent the subdevice
159  * @smd:	reference to a Qualcomm subdev context
160  */
161 void qcom_add_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
162 {
163 	struct device *dev = &rproc->dev;
164 
165 	smd->node = of_get_child_by_name(dev->parent->of_node, "smd-edge");
166 	if (!smd->node)
167 		return;
168 
169 	smd->dev = dev;
170 	smd->subdev.start = smd_subdev_start;
171 	smd->subdev.stop = smd_subdev_stop;
172 
173 	rproc_add_subdev(rproc, &smd->subdev);
174 }
175 EXPORT_SYMBOL_GPL(qcom_add_smd_subdev);
176 
177 /**
178  * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
179  * @rproc:	rproc handle
180  * @smd:	the SMD subdevice to remove
181  */
182 void qcom_remove_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
183 {
184 	if (!smd->node)
185 		return;
186 
187 	rproc_remove_subdev(rproc, &smd->subdev);
188 	of_node_put(smd->node);
189 }
190 EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev);
191 
192 /**
193  * qcom_register_ssr_notifier() - register SSR notification handler
194  * @nb:		notifier_block to notify for restart notifications
195  *
196  * Returns 0 on success, negative errno on failure.
197  *
198  * This register the @notify function as handler for restart notifications. As
199  * remote processors are stopped this function will be called, with the SSR
200  * name passed as a parameter.
201  */
202 int qcom_register_ssr_notifier(struct notifier_block *nb)
203 {
204 	return blocking_notifier_chain_register(&ssr_notifiers, nb);
205 }
206 EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier);
207 
208 /**
209  * qcom_unregister_ssr_notifier() - unregister SSR notification handler
210  * @nb:		notifier_block to unregister
211  */
212 void qcom_unregister_ssr_notifier(struct notifier_block *nb)
213 {
214 	blocking_notifier_chain_unregister(&ssr_notifiers, nb);
215 }
216 EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier);
217 
218 static void ssr_notify_unprepare(struct rproc_subdev *subdev)
219 {
220 	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
221 
222 	blocking_notifier_call_chain(&ssr_notifiers, 0, (void *)ssr->name);
223 }
224 
225 /**
226  * qcom_add_ssr_subdev() - register subdevice as restart notification source
227  * @rproc:	rproc handle
228  * @ssr:	SSR subdevice handle
229  * @ssr_name:	identifier to use for notifications originating from @rproc
230  *
231  * As the @ssr is registered with the @rproc SSR events will be sent to all
232  * registered listeners in the system as the remoteproc is shut down.
233  */
234 void qcom_add_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr,
235 			 const char *ssr_name)
236 {
237 	ssr->name = ssr_name;
238 	ssr->subdev.unprepare = ssr_notify_unprepare;
239 
240 	rproc_add_subdev(rproc, &ssr->subdev);
241 }
242 EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev);
243 
244 /**
245  * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
246  * @rproc:	rproc handle
247  * @ssr:	SSR subdevice handle
248  */
249 void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr)
250 {
251 	rproc_remove_subdev(rproc, &ssr->subdev);
252 }
253 EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
254 
255 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
256 MODULE_LICENSE("GPL v2");
257