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