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/remoteproc/qcom_rproc.h>
16 #include <linux/rpmsg/qcom_glink.h>
17 #include <linux/rpmsg/qcom_smd.h>
18 #include <linux/slab.h>
19 #include <linux/soc/qcom/mdt_loader.h>
20 #include <linux/soc/qcom/smem.h>
21 
22 #include "remoteproc_internal.h"
23 #include "qcom_common.h"
24 
25 #define to_glink_subdev(d) container_of(d, struct qcom_rproc_glink, subdev)
26 #define to_smd_subdev(d) container_of(d, struct qcom_rproc_subdev, subdev)
27 #define to_ssr_subdev(d) container_of(d, struct qcom_rproc_ssr, subdev)
28 
29 #define MAX_NUM_OF_SS           10
30 #define MAX_REGION_NAME_LENGTH  16
31 #define SBL_MINIDUMP_SMEM_ID	602
32 #define MD_REGION_VALID		('V' << 24 | 'A' << 16 | 'L' << 8 | 'I' << 0)
33 #define MD_SS_ENCR_DONE		('D' << 24 | 'O' << 16 | 'N' << 8 | 'E' << 0)
34 #define MD_SS_ENABLED		('E' << 24 | 'N' << 16 | 'B' << 8 | 'L' << 0)
35 
36 /**
37  * struct minidump_region - Minidump region
38  * @name		: Name of the region to be dumped
39  * @seq_num:		: Use to differentiate regions with same name.
40  * @valid		: This entry to be dumped (if set to 1)
41  * @address		: Physical address of region to be dumped
42  * @size		: Size of the region
43  */
44 struct minidump_region {
45 	char	name[MAX_REGION_NAME_LENGTH];
46 	__le32	seq_num;
47 	__le32	valid;
48 	__le64	address;
49 	__le64	size;
50 };
51 
52 /**
53  * struct minidump_subsystem - Subsystem's SMEM Table of content
54  * @status : Subsystem toc init status
55  * @enabled : if set to 1, this region would be copied during coredump
56  * @encryption_status: Encryption status for this subsystem
57  * @encryption_required : Decides to encrypt the subsystem regions or not
58  * @region_count : Number of regions added in this subsystem toc
59  * @regions_baseptr : regions base pointer of the subsystem
60  */
61 struct minidump_subsystem {
62 	__le32	status;
63 	__le32	enabled;
64 	__le32	encryption_status;
65 	__le32	encryption_required;
66 	__le32	region_count;
67 	__le64	regions_baseptr;
68 };
69 
70 /**
71  * struct minidump_global_toc - Global Table of Content
72  * @status : Global Minidump init status
73  * @md_revision : Minidump revision
74  * @enabled : Minidump enable status
75  * @subsystems : Array of subsystems toc
76  */
77 struct minidump_global_toc {
78 	__le32				status;
79 	__le32				md_revision;
80 	__le32				enabled;
81 	struct minidump_subsystem	subsystems[MAX_NUM_OF_SS];
82 };
83 
84 struct qcom_ssr_subsystem {
85 	const char *name;
86 	struct srcu_notifier_head notifier_list;
87 	struct list_head list;
88 };
89 
90 static LIST_HEAD(qcom_ssr_subsystem_list);
91 static DEFINE_MUTEX(qcom_ssr_subsys_lock);
92 
93 static void qcom_minidump_cleanup(struct rproc *rproc)
94 {
95 	struct rproc_dump_segment *entry, *tmp;
96 
97 	list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
98 		list_del(&entry->node);
99 		kfree(entry->priv);
100 		kfree(entry);
101 	}
102 }
103 
104 static int qcom_add_minidump_segments(struct rproc *rproc, struct minidump_subsystem *subsystem,
105 			void (*rproc_dumpfn_t)(struct rproc *rproc, struct rproc_dump_segment *segment,
106 				void *dest, size_t offset, size_t size))
107 {
108 	struct minidump_region __iomem *ptr;
109 	struct minidump_region region;
110 	int seg_cnt, i;
111 	dma_addr_t da;
112 	size_t size;
113 	char *name;
114 
115 	if (WARN_ON(!list_empty(&rproc->dump_segments))) {
116 		dev_err(&rproc->dev, "dump segment list already populated\n");
117 		return -EUCLEAN;
118 	}
119 
120 	seg_cnt = le32_to_cpu(subsystem->region_count);
121 	ptr = ioremap((unsigned long)le64_to_cpu(subsystem->regions_baseptr),
122 		      seg_cnt * sizeof(struct minidump_region));
123 	if (!ptr)
124 		return -EFAULT;
125 
126 	for (i = 0; i < seg_cnt; i++) {
127 		memcpy_fromio(&region, ptr + i, sizeof(region));
128 		if (le32_to_cpu(region.valid) == MD_REGION_VALID) {
129 			name = kstrndup(region.name, MAX_REGION_NAME_LENGTH - 1, GFP_KERNEL);
130 			if (!name) {
131 				iounmap(ptr);
132 				return -ENOMEM;
133 			}
134 			da = le64_to_cpu(region.address);
135 			size = le64_to_cpu(region.size);
136 			rproc_coredump_add_custom_segment(rproc, da, size, rproc_dumpfn_t, name);
137 		}
138 	}
139 
140 	iounmap(ptr);
141 	return 0;
142 }
143 
144 void qcom_minidump(struct rproc *rproc, unsigned int minidump_id,
145 		void (*rproc_dumpfn_t)(struct rproc *rproc,
146 		struct rproc_dump_segment *segment, void *dest, size_t offset,
147 		size_t size))
148 {
149 	int ret;
150 	struct minidump_subsystem *subsystem;
151 	struct minidump_global_toc *toc;
152 
153 	/* Get Global minidump ToC*/
154 	toc = qcom_smem_get(QCOM_SMEM_HOST_ANY, SBL_MINIDUMP_SMEM_ID, NULL);
155 
156 	/* check if global table pointer exists and init is set */
157 	if (IS_ERR(toc) || !toc->status) {
158 		dev_err(&rproc->dev, "Minidump TOC not found in SMEM\n");
159 		return;
160 	}
161 
162 	/* Get subsystem table of contents using the minidump id */
163 	subsystem = &toc->subsystems[minidump_id];
164 
165 	/**
166 	 * Collect minidump if SS ToC is valid and segment table
167 	 * is initialized in memory and encryption status is set.
168 	 */
169 	if (subsystem->regions_baseptr == 0 ||
170 	    le32_to_cpu(subsystem->status) != 1 ||
171 	    le32_to_cpu(subsystem->enabled) != MD_SS_ENABLED ||
172 	    le32_to_cpu(subsystem->encryption_status) != MD_SS_ENCR_DONE) {
173 		dev_err(&rproc->dev, "Minidump not ready, skipping\n");
174 		return;
175 	}
176 
177 	ret = qcom_add_minidump_segments(rproc, subsystem, rproc_dumpfn_t);
178 	if (ret) {
179 		dev_err(&rproc->dev, "Failed with error: %d while adding minidump entries\n", ret);
180 		goto clean_minidump;
181 	}
182 	rproc_coredump_using_sections(rproc);
183 clean_minidump:
184 	qcom_minidump_cleanup(rproc);
185 }
186 EXPORT_SYMBOL_GPL(qcom_minidump);
187 
188 static int glink_subdev_start(struct rproc_subdev *subdev)
189 {
190 	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
191 
192 	glink->edge = qcom_glink_smem_register(glink->dev, glink->node);
193 
194 	return PTR_ERR_OR_ZERO(glink->edge);
195 }
196 
197 static void glink_subdev_stop(struct rproc_subdev *subdev, bool crashed)
198 {
199 	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
200 
201 	qcom_glink_smem_unregister(glink->edge);
202 	glink->edge = NULL;
203 }
204 
205 static void glink_subdev_unprepare(struct rproc_subdev *subdev)
206 {
207 	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
208 
209 	qcom_glink_ssr_notify(glink->ssr_name);
210 }
211 
212 /**
213  * qcom_add_glink_subdev() - try to add a GLINK subdevice to rproc
214  * @rproc:	rproc handle to parent the subdevice
215  * @glink:	reference to a GLINK subdev context
216  * @ssr_name:	identifier of the associated remoteproc for ssr notifications
217  */
218 void qcom_add_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink,
219 			   const char *ssr_name)
220 {
221 	struct device *dev = &rproc->dev;
222 
223 	glink->node = of_get_child_by_name(dev->parent->of_node, "glink-edge");
224 	if (!glink->node)
225 		return;
226 
227 	glink->ssr_name = kstrdup_const(ssr_name, GFP_KERNEL);
228 	if (!glink->ssr_name)
229 		return;
230 
231 	glink->dev = dev;
232 	glink->subdev.start = glink_subdev_start;
233 	glink->subdev.stop = glink_subdev_stop;
234 	glink->subdev.unprepare = glink_subdev_unprepare;
235 
236 	rproc_add_subdev(rproc, &glink->subdev);
237 }
238 EXPORT_SYMBOL_GPL(qcom_add_glink_subdev);
239 
240 /**
241  * qcom_remove_glink_subdev() - remove a GLINK subdevice from rproc
242  * @rproc:	rproc handle
243  * @glink:	reference to a GLINK subdev context
244  */
245 void qcom_remove_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink)
246 {
247 	if (!glink->node)
248 		return;
249 
250 	rproc_remove_subdev(rproc, &glink->subdev);
251 	kfree_const(glink->ssr_name);
252 	of_node_put(glink->node);
253 }
254 EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev);
255 
256 /**
257  * qcom_register_dump_segments() - register segments for coredump
258  * @rproc:	remoteproc handle
259  * @fw:		firmware header
260  *
261  * Register all segments of the ELF in the remoteproc coredump segment list
262  *
263  * Return: 0 on success, negative errno on failure.
264  */
265 int qcom_register_dump_segments(struct rproc *rproc,
266 				const struct firmware *fw)
267 {
268 	const struct elf32_phdr *phdrs;
269 	const struct elf32_phdr *phdr;
270 	const struct elf32_hdr *ehdr;
271 	int ret;
272 	int i;
273 
274 	ehdr = (struct elf32_hdr *)fw->data;
275 	phdrs = (struct elf32_phdr *)(ehdr + 1);
276 
277 	for (i = 0; i < ehdr->e_phnum; i++) {
278 		phdr = &phdrs[i];
279 
280 		if (phdr->p_type != PT_LOAD)
281 			continue;
282 
283 		if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
284 			continue;
285 
286 		if (!phdr->p_memsz)
287 			continue;
288 
289 		ret = rproc_coredump_add_segment(rproc, phdr->p_paddr,
290 						 phdr->p_memsz);
291 		if (ret)
292 			return ret;
293 	}
294 
295 	return 0;
296 }
297 EXPORT_SYMBOL_GPL(qcom_register_dump_segments);
298 
299 static int smd_subdev_start(struct rproc_subdev *subdev)
300 {
301 	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
302 
303 	smd->edge = qcom_smd_register_edge(smd->dev, smd->node);
304 
305 	return PTR_ERR_OR_ZERO(smd->edge);
306 }
307 
308 static void smd_subdev_stop(struct rproc_subdev *subdev, bool crashed)
309 {
310 	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
311 
312 	qcom_smd_unregister_edge(smd->edge);
313 	smd->edge = NULL;
314 }
315 
316 /**
317  * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
318  * @rproc:	rproc handle to parent the subdevice
319  * @smd:	reference to a Qualcomm subdev context
320  */
321 void qcom_add_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
322 {
323 	struct device *dev = &rproc->dev;
324 
325 	smd->node = of_get_child_by_name(dev->parent->of_node, "smd-edge");
326 	if (!smd->node)
327 		return;
328 
329 	smd->dev = dev;
330 	smd->subdev.start = smd_subdev_start;
331 	smd->subdev.stop = smd_subdev_stop;
332 
333 	rproc_add_subdev(rproc, &smd->subdev);
334 }
335 EXPORT_SYMBOL_GPL(qcom_add_smd_subdev);
336 
337 /**
338  * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
339  * @rproc:	rproc handle
340  * @smd:	the SMD subdevice to remove
341  */
342 void qcom_remove_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
343 {
344 	if (!smd->node)
345 		return;
346 
347 	rproc_remove_subdev(rproc, &smd->subdev);
348 	of_node_put(smd->node);
349 }
350 EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev);
351 
352 static struct qcom_ssr_subsystem *qcom_ssr_get_subsys(const char *name)
353 {
354 	struct qcom_ssr_subsystem *info;
355 
356 	mutex_lock(&qcom_ssr_subsys_lock);
357 	/* Match in the global qcom_ssr_subsystem_list with name */
358 	list_for_each_entry(info, &qcom_ssr_subsystem_list, list)
359 		if (!strcmp(info->name, name))
360 			goto out;
361 
362 	info = kzalloc(sizeof(*info), GFP_KERNEL);
363 	if (!info) {
364 		info = ERR_PTR(-ENOMEM);
365 		goto out;
366 	}
367 	info->name = kstrdup_const(name, GFP_KERNEL);
368 	srcu_init_notifier_head(&info->notifier_list);
369 
370 	/* Add to global notification list */
371 	list_add_tail(&info->list, &qcom_ssr_subsystem_list);
372 
373 out:
374 	mutex_unlock(&qcom_ssr_subsys_lock);
375 	return info;
376 }
377 
378 /**
379  * qcom_register_ssr_notifier() - register SSR notification handler
380  * @name:	Subsystem's SSR name
381  * @nb:		notifier_block to be invoked upon subsystem's state change
382  *
383  * This registers the @nb notifier block as part the notifier chain for a
384  * remoteproc associated with @name. The notifier block's callback
385  * will be invoked when the remote processor's SSR events occur
386  * (pre/post startup and pre/post shutdown).
387  *
388  * Return: a subsystem cookie on success, ERR_PTR on failure.
389  */
390 void *qcom_register_ssr_notifier(const char *name, struct notifier_block *nb)
391 {
392 	struct qcom_ssr_subsystem *info;
393 
394 	info = qcom_ssr_get_subsys(name);
395 	if (IS_ERR(info))
396 		return info;
397 
398 	srcu_notifier_chain_register(&info->notifier_list, nb);
399 
400 	return &info->notifier_list;
401 }
402 EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier);
403 
404 /**
405  * qcom_unregister_ssr_notifier() - unregister SSR notification handler
406  * @notify:	subsystem cookie returned from qcom_register_ssr_notifier
407  * @nb:		notifier_block to unregister
408  *
409  * This function will unregister the notifier from the particular notifier
410  * chain.
411  *
412  * Return: 0 on success, %ENOENT otherwise.
413  */
414 int qcom_unregister_ssr_notifier(void *notify, struct notifier_block *nb)
415 {
416 	return srcu_notifier_chain_unregister(notify, nb);
417 }
418 EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier);
419 
420 static int ssr_notify_prepare(struct rproc_subdev *subdev)
421 {
422 	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
423 	struct qcom_ssr_notify_data data = {
424 		.name = ssr->info->name,
425 		.crashed = false,
426 	};
427 
428 	srcu_notifier_call_chain(&ssr->info->notifier_list,
429 				 QCOM_SSR_BEFORE_POWERUP, &data);
430 	return 0;
431 }
432 
433 static int ssr_notify_start(struct rproc_subdev *subdev)
434 {
435 	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
436 	struct qcom_ssr_notify_data data = {
437 		.name = ssr->info->name,
438 		.crashed = false,
439 	};
440 
441 	srcu_notifier_call_chain(&ssr->info->notifier_list,
442 				 QCOM_SSR_AFTER_POWERUP, &data);
443 	return 0;
444 }
445 
446 static void ssr_notify_stop(struct rproc_subdev *subdev, bool crashed)
447 {
448 	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
449 	struct qcom_ssr_notify_data data = {
450 		.name = ssr->info->name,
451 		.crashed = crashed,
452 	};
453 
454 	srcu_notifier_call_chain(&ssr->info->notifier_list,
455 				 QCOM_SSR_BEFORE_SHUTDOWN, &data);
456 }
457 
458 static void ssr_notify_unprepare(struct rproc_subdev *subdev)
459 {
460 	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
461 	struct qcom_ssr_notify_data data = {
462 		.name = ssr->info->name,
463 		.crashed = false,
464 	};
465 
466 	srcu_notifier_call_chain(&ssr->info->notifier_list,
467 				 QCOM_SSR_AFTER_SHUTDOWN, &data);
468 }
469 
470 /**
471  * qcom_add_ssr_subdev() - register subdevice as restart notification source
472  * @rproc:	rproc handle
473  * @ssr:	SSR subdevice handle
474  * @ssr_name:	identifier to use for notifications originating from @rproc
475  *
476  * As the @ssr is registered with the @rproc SSR events will be sent to all
477  * registered listeners for the remoteproc when it's SSR events occur
478  * (pre/post startup and pre/post shutdown).
479  */
480 void qcom_add_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr,
481 			 const char *ssr_name)
482 {
483 	struct qcom_ssr_subsystem *info;
484 
485 	info = qcom_ssr_get_subsys(ssr_name);
486 	if (IS_ERR(info)) {
487 		dev_err(&rproc->dev, "Failed to add ssr subdevice\n");
488 		return;
489 	}
490 
491 	ssr->info = info;
492 	ssr->subdev.prepare = ssr_notify_prepare;
493 	ssr->subdev.start = ssr_notify_start;
494 	ssr->subdev.stop = ssr_notify_stop;
495 	ssr->subdev.unprepare = ssr_notify_unprepare;
496 
497 	rproc_add_subdev(rproc, &ssr->subdev);
498 }
499 EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev);
500 
501 /**
502  * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
503  * @rproc:	rproc handle
504  * @ssr:	SSR subdevice handle
505  */
506 void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr)
507 {
508 	rproc_remove_subdev(rproc, &ssr->subdev);
509 	ssr->info = NULL;
510 }
511 EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
512 
513 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
514 MODULE_LICENSE("GPL v2");
515