Linux Audio

Check our new training course

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