xref: /openbmc/linux/drivers/remoteproc/qcom_common.c (revision dcb57ed43d9ec5e16628c337143cd6b387f42778)
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)
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 	rproc_remove_subdev(rproc, &glink->subdev);
79 	of_node_put(glink->node);
80 }
81 EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev);
82 
83 /**
84  * qcom_register_dump_segments() - register segments for coredump
85  * @rproc:	remoteproc handle
86  * @fw:		firmware header
87  *
88  * Register all segments of the ELF in the remoteproc coredump segment list
89  *
90  * Return: 0 on success, negative errno on failure.
91  */
92 int qcom_register_dump_segments(struct rproc *rproc,
93 				const struct firmware *fw)
94 {
95 	const struct elf32_phdr *phdrs;
96 	const struct elf32_phdr *phdr;
97 	const struct elf32_hdr *ehdr;
98 	int ret;
99 	int i;
100 
101 	ehdr = (struct elf32_hdr *)fw->data;
102 	phdrs = (struct elf32_phdr *)(ehdr + 1);
103 
104 	for (i = 0; i < ehdr->e_phnum; i++) {
105 		phdr = &phdrs[i];
106 
107 		if (phdr->p_type != PT_LOAD)
108 			continue;
109 
110 		if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
111 			continue;
112 
113 		if (!phdr->p_memsz)
114 			continue;
115 
116 		ret = rproc_coredump_add_segment(rproc, phdr->p_paddr,
117 						 phdr->p_memsz);
118 		if (ret)
119 			return ret;
120 	}
121 
122 	return 0;
123 }
124 EXPORT_SYMBOL_GPL(qcom_register_dump_segments);
125 
126 static int smd_subdev_probe(struct rproc_subdev *subdev)
127 {
128 	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
129 
130 	smd->edge = qcom_smd_register_edge(smd->dev, smd->node);
131 
132 	return PTR_ERR_OR_ZERO(smd->edge);
133 }
134 
135 static void smd_subdev_remove(struct rproc_subdev *subdev)
136 {
137 	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
138 
139 	qcom_smd_unregister_edge(smd->edge);
140 	smd->edge = NULL;
141 }
142 
143 /**
144  * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
145  * @rproc:	rproc handle to parent the subdevice
146  * @smd:	reference to a Qualcomm subdev context
147  */
148 void qcom_add_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
149 {
150 	struct device *dev = &rproc->dev;
151 
152 	smd->node = of_get_child_by_name(dev->parent->of_node, "smd-edge");
153 	if (!smd->node)
154 		return;
155 
156 	smd->dev = dev;
157 	rproc_add_subdev(rproc, &smd->subdev, smd_subdev_probe, smd_subdev_remove);
158 }
159 EXPORT_SYMBOL_GPL(qcom_add_smd_subdev);
160 
161 /**
162  * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
163  * @rproc:	rproc handle
164  * @smd:	the SMD subdevice to remove
165  */
166 void qcom_remove_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
167 {
168 	rproc_remove_subdev(rproc, &smd->subdev);
169 	of_node_put(smd->node);
170 }
171 EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev);
172 
173 /**
174  * qcom_register_ssr_notifier() - register SSR notification handler
175  * @nb:		notifier_block to notify for restart notifications
176  *
177  * Returns 0 on success, negative errno on failure.
178  *
179  * This register the @notify function as handler for restart notifications. As
180  * remote processors are stopped this function will be called, with the SSR
181  * name passed as a parameter.
182  */
183 int qcom_register_ssr_notifier(struct notifier_block *nb)
184 {
185 	return blocking_notifier_chain_register(&ssr_notifiers, nb);
186 }
187 EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier);
188 
189 /**
190  * qcom_unregister_ssr_notifier() - unregister SSR notification handler
191  * @nb:		notifier_block to unregister
192  */
193 void qcom_unregister_ssr_notifier(struct notifier_block *nb)
194 {
195 	blocking_notifier_chain_unregister(&ssr_notifiers, nb);
196 }
197 EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier);
198 
199 static int ssr_notify_start(struct rproc_subdev *subdev)
200 {
201 	return  0;
202 }
203 
204 static void ssr_notify_stop(struct rproc_subdev *subdev)
205 {
206 	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
207 
208 	blocking_notifier_call_chain(&ssr_notifiers, 0, (void *)ssr->name);
209 }
210 
211 /**
212  * qcom_add_ssr_subdev() - register subdevice as restart notification source
213  * @rproc:	rproc handle
214  * @ssr:	SSR subdevice handle
215  * @ssr_name:	identifier to use for notifications originating from @rproc
216  *
217  * As the @ssr is registered with the @rproc SSR events will be sent to all
218  * registered listeners in the system as the remoteproc is shut down.
219  */
220 void qcom_add_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr,
221 			 const char *ssr_name)
222 {
223 	ssr->name = ssr_name;
224 
225 	rproc_add_subdev(rproc, &ssr->subdev, ssr_notify_start, ssr_notify_stop);
226 }
227 EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev);
228 
229 /**
230  * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
231  * @rproc:	rproc handle
232  * @ssr:	SSR subdevice handle
233  */
234 void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr)
235 {
236 	rproc_remove_subdev(rproc, &ssr->subdev);
237 }
238 EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
239 
240 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
241 MODULE_LICENSE("GPL v2");
242