1 // SPDX-License-Identifier: GPL-2.0-only
3 * Qualcomm Peripheral Image Loader helpers
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.
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>
22 #include "remoteproc_internal.h"
23 #include "qcom_common.h"
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)
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)
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
44 struct minidump_region
{
45 char name
[MAX_REGION_NAME_LENGTH
];
53 * struct minidump_subsystem_toc: 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
61 struct minidump_subsystem
{
64 __le32 encryption_status
;
65 __le32 encryption_required
;
67 __le64 regions_baseptr
;
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
77 struct minidump_global_toc
{
81 struct minidump_subsystem subsystems
[MAX_NUM_OF_SS
];
84 struct qcom_ssr_subsystem
{
86 struct srcu_notifier_head notifier_list
;
87 struct list_head list
;
90 static LIST_HEAD(qcom_ssr_subsystem_list
);
91 static DEFINE_MUTEX(qcom_ssr_subsys_lock
);
93 static void qcom_minidump_cleanup(struct rproc
*rproc
)
95 struct rproc_dump_segment
*entry
, *tmp
;
97 list_for_each_entry_safe(entry
, tmp
, &rproc
->dump_segments
, node
) {
98 list_del(&entry
->node
);
104 static int qcom_add_minidump_segments(struct rproc
*rproc
, struct minidump_subsystem
*subsystem
)
106 struct minidump_region __iomem
*ptr
;
107 struct minidump_region region
;
113 if (WARN_ON(!list_empty(&rproc
->dump_segments
))) {
114 dev_err(&rproc
->dev
, "dump segment list already populated\n");
118 seg_cnt
= le32_to_cpu(subsystem
->region_count
);
119 ptr
= ioremap((unsigned long)le64_to_cpu(subsystem
->regions_baseptr
),
120 seg_cnt
* sizeof(struct minidump_region
));
124 for (i
= 0; i
< seg_cnt
; i
++) {
125 memcpy_fromio(®ion
, ptr
+ i
, sizeof(region
));
126 if (region
.valid
== MD_REGION_VALID
) {
127 name
= kstrdup(region
.name
, GFP_KERNEL
);
132 da
= le64_to_cpu(region
.address
);
133 size
= le32_to_cpu(region
.size
);
134 rproc_coredump_add_custom_segment(rproc
, da
, size
, NULL
, name
);
142 void qcom_minidump(struct rproc
*rproc
, unsigned int minidump_id
)
145 struct minidump_subsystem
*subsystem
;
146 struct minidump_global_toc
*toc
;
148 /* Get Global minidump ToC*/
149 toc
= qcom_smem_get(QCOM_SMEM_HOST_ANY
, SBL_MINIDUMP_SMEM_ID
, NULL
);
151 /* check if global table pointer exists and init is set */
152 if (IS_ERR(toc
) || !toc
->status
) {
153 dev_err(&rproc
->dev
, "Minidump TOC not found in SMEM\n");
157 /* Get subsystem table of contents using the minidump id */
158 subsystem
= &toc
->subsystems
[minidump_id
];
161 * Collect minidump if SS ToC is valid and segment table
162 * is initialized in memory and encryption status is set.
164 if (subsystem
->regions_baseptr
== 0 ||
165 le32_to_cpu(subsystem
->status
) != 1 ||
166 le32_to_cpu(subsystem
->enabled
) != MD_SS_ENABLED
||
167 le32_to_cpu(subsystem
->encryption_status
) != MD_SS_ENCR_DONE
) {
168 dev_err(&rproc
->dev
, "Minidump not ready, skipping\n");
172 ret
= qcom_add_minidump_segments(rproc
, subsystem
);
174 dev_err(&rproc
->dev
, "Failed with error: %d while adding minidump entries\n", ret
);
177 rproc_coredump_using_sections(rproc
);
179 qcom_minidump_cleanup(rproc
);
181 EXPORT_SYMBOL_GPL(qcom_minidump
);
183 static int glink_subdev_start(struct rproc_subdev
*subdev
)
185 struct qcom_rproc_glink
*glink
= to_glink_subdev(subdev
);
187 glink
->edge
= qcom_glink_smem_register(glink
->dev
, glink
->node
);
189 return PTR_ERR_OR_ZERO(glink
->edge
);
192 static void glink_subdev_stop(struct rproc_subdev
*subdev
, bool crashed
)
194 struct qcom_rproc_glink
*glink
= to_glink_subdev(subdev
);
196 qcom_glink_smem_unregister(glink
->edge
);
200 static void glink_subdev_unprepare(struct rproc_subdev
*subdev
)
202 struct qcom_rproc_glink
*glink
= to_glink_subdev(subdev
);
204 qcom_glink_ssr_notify(glink
->ssr_name
);
208 * qcom_add_glink_subdev() - try to add a GLINK subdevice to rproc
209 * @rproc: rproc handle to parent the subdevice
210 * @glink: reference to a GLINK subdev context
211 * @ssr_name: identifier of the associated remoteproc for ssr notifications
213 void qcom_add_glink_subdev(struct rproc
*rproc
, struct qcom_rproc_glink
*glink
,
214 const char *ssr_name
)
216 struct device
*dev
= &rproc
->dev
;
218 glink
->node
= of_get_child_by_name(dev
->parent
->of_node
, "glink-edge");
222 glink
->ssr_name
= kstrdup_const(ssr_name
, GFP_KERNEL
);
223 if (!glink
->ssr_name
)
227 glink
->subdev
.start
= glink_subdev_start
;
228 glink
->subdev
.stop
= glink_subdev_stop
;
229 glink
->subdev
.unprepare
= glink_subdev_unprepare
;
231 rproc_add_subdev(rproc
, &glink
->subdev
);
233 EXPORT_SYMBOL_GPL(qcom_add_glink_subdev
);
236 * qcom_remove_glink_subdev() - remove a GLINK subdevice from rproc
237 * @rproc: rproc handle
238 * @glink: reference to a GLINK subdev context
240 void qcom_remove_glink_subdev(struct rproc
*rproc
, struct qcom_rproc_glink
*glink
)
245 rproc_remove_subdev(rproc
, &glink
->subdev
);
246 kfree_const(glink
->ssr_name
);
247 of_node_put(glink
->node
);
249 EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev
);
252 * qcom_register_dump_segments() - register segments for coredump
253 * @rproc: remoteproc handle
254 * @fw: firmware header
256 * Register all segments of the ELF in the remoteproc coredump segment list
258 * Return: 0 on success, negative errno on failure.
260 int qcom_register_dump_segments(struct rproc
*rproc
,
261 const struct firmware
*fw
)
263 const struct elf32_phdr
*phdrs
;
264 const struct elf32_phdr
*phdr
;
265 const struct elf32_hdr
*ehdr
;
269 ehdr
= (struct elf32_hdr
*)fw
->data
;
270 phdrs
= (struct elf32_phdr
*)(ehdr
+ 1);
272 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
275 if (phdr
->p_type
!= PT_LOAD
)
278 if ((phdr
->p_flags
& QCOM_MDT_TYPE_MASK
) == QCOM_MDT_TYPE_HASH
)
284 ret
= rproc_coredump_add_segment(rproc
, phdr
->p_paddr
,
292 EXPORT_SYMBOL_GPL(qcom_register_dump_segments
);
294 static int smd_subdev_start(struct rproc_subdev
*subdev
)
296 struct qcom_rproc_subdev
*smd
= to_smd_subdev(subdev
);
298 smd
->edge
= qcom_smd_register_edge(smd
->dev
, smd
->node
);
300 return PTR_ERR_OR_ZERO(smd
->edge
);
303 static void smd_subdev_stop(struct rproc_subdev
*subdev
, bool crashed
)
305 struct qcom_rproc_subdev
*smd
= to_smd_subdev(subdev
);
307 qcom_smd_unregister_edge(smd
->edge
);
312 * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
313 * @rproc: rproc handle to parent the subdevice
314 * @smd: reference to a Qualcomm subdev context
316 void qcom_add_smd_subdev(struct rproc
*rproc
, struct qcom_rproc_subdev
*smd
)
318 struct device
*dev
= &rproc
->dev
;
320 smd
->node
= of_get_child_by_name(dev
->parent
->of_node
, "smd-edge");
325 smd
->subdev
.start
= smd_subdev_start
;
326 smd
->subdev
.stop
= smd_subdev_stop
;
328 rproc_add_subdev(rproc
, &smd
->subdev
);
330 EXPORT_SYMBOL_GPL(qcom_add_smd_subdev
);
333 * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
334 * @rproc: rproc handle
335 * @smd: the SMD subdevice to remove
337 void qcom_remove_smd_subdev(struct rproc
*rproc
, struct qcom_rproc_subdev
*smd
)
342 rproc_remove_subdev(rproc
, &smd
->subdev
);
343 of_node_put(smd
->node
);
345 EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev
);
347 static struct qcom_ssr_subsystem
*qcom_ssr_get_subsys(const char *name
)
349 struct qcom_ssr_subsystem
*info
;
351 mutex_lock(&qcom_ssr_subsys_lock
);
352 /* Match in the global qcom_ssr_subsystem_list with name */
353 list_for_each_entry(info
, &qcom_ssr_subsystem_list
, list
)
354 if (!strcmp(info
->name
, name
))
357 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
359 info
= ERR_PTR(-ENOMEM
);
362 info
->name
= kstrdup_const(name
, GFP_KERNEL
);
363 srcu_init_notifier_head(&info
->notifier_list
);
365 /* Add to global notification list */
366 list_add_tail(&info
->list
, &qcom_ssr_subsystem_list
);
369 mutex_unlock(&qcom_ssr_subsys_lock
);
374 * qcom_register_ssr_notifier() - register SSR notification handler
375 * @name: Subsystem's SSR name
376 * @nb: notifier_block to be invoked upon subsystem's state change
378 * This registers the @nb notifier block as part the notifier chain for a
379 * remoteproc associated with @name. The notifier block's callback
380 * will be invoked when the remote processor's SSR events occur
381 * (pre/post startup and pre/post shutdown).
383 * Return: a subsystem cookie on success, ERR_PTR on failure.
385 void *qcom_register_ssr_notifier(const char *name
, struct notifier_block
*nb
)
387 struct qcom_ssr_subsystem
*info
;
389 info
= qcom_ssr_get_subsys(name
);
393 srcu_notifier_chain_register(&info
->notifier_list
, nb
);
395 return &info
->notifier_list
;
397 EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier
);
400 * qcom_unregister_ssr_notifier() - unregister SSR notification handler
401 * @notify: subsystem cookie returned from qcom_register_ssr_notifier
402 * @nb: notifier_block to unregister
404 * This function will unregister the notifier from the particular notifier
407 * Return: 0 on success, %ENOENT otherwise.
409 int qcom_unregister_ssr_notifier(void *notify
, struct notifier_block
*nb
)
411 return srcu_notifier_chain_unregister(notify
, nb
);
413 EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier
);
415 static int ssr_notify_prepare(struct rproc_subdev
*subdev
)
417 struct qcom_rproc_ssr
*ssr
= to_ssr_subdev(subdev
);
418 struct qcom_ssr_notify_data data
= {
419 .name
= ssr
->info
->name
,
423 srcu_notifier_call_chain(&ssr
->info
->notifier_list
,
424 QCOM_SSR_BEFORE_POWERUP
, &data
);
428 static int ssr_notify_start(struct rproc_subdev
*subdev
)
430 struct qcom_rproc_ssr
*ssr
= to_ssr_subdev(subdev
);
431 struct qcom_ssr_notify_data data
= {
432 .name
= ssr
->info
->name
,
436 srcu_notifier_call_chain(&ssr
->info
->notifier_list
,
437 QCOM_SSR_AFTER_POWERUP
, &data
);
441 static void ssr_notify_stop(struct rproc_subdev
*subdev
, bool crashed
)
443 struct qcom_rproc_ssr
*ssr
= to_ssr_subdev(subdev
);
444 struct qcom_ssr_notify_data data
= {
445 .name
= ssr
->info
->name
,
449 srcu_notifier_call_chain(&ssr
->info
->notifier_list
,
450 QCOM_SSR_BEFORE_SHUTDOWN
, &data
);
453 static void ssr_notify_unprepare(struct rproc_subdev
*subdev
)
455 struct qcom_rproc_ssr
*ssr
= to_ssr_subdev(subdev
);
456 struct qcom_ssr_notify_data data
= {
457 .name
= ssr
->info
->name
,
461 srcu_notifier_call_chain(&ssr
->info
->notifier_list
,
462 QCOM_SSR_AFTER_SHUTDOWN
, &data
);
466 * qcom_add_ssr_subdev() - register subdevice as restart notification source
467 * @rproc: rproc handle
468 * @ssr: SSR subdevice handle
469 * @ssr_name: identifier to use for notifications originating from @rproc
471 * As the @ssr is registered with the @rproc SSR events will be sent to all
472 * registered listeners for the remoteproc when it's SSR events occur
473 * (pre/post startup and pre/post shutdown).
475 void qcom_add_ssr_subdev(struct rproc
*rproc
, struct qcom_rproc_ssr
*ssr
,
476 const char *ssr_name
)
478 struct qcom_ssr_subsystem
*info
;
480 info
= qcom_ssr_get_subsys(ssr_name
);
482 dev_err(&rproc
->dev
, "Failed to add ssr subdevice\n");
487 ssr
->subdev
.prepare
= ssr_notify_prepare
;
488 ssr
->subdev
.start
= ssr_notify_start
;
489 ssr
->subdev
.stop
= ssr_notify_stop
;
490 ssr
->subdev
.unprepare
= ssr_notify_unprepare
;
492 rproc_add_subdev(rproc
, &ssr
->subdev
);
494 EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev
);
497 * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
498 * @rproc: rproc handle
499 * @ssr: SSR subdevice handle
501 void qcom_remove_ssr_subdev(struct rproc
*rproc
, struct qcom_rproc_ssr
*ssr
)
503 rproc_remove_subdev(rproc
, &ssr
->subdev
);
506 EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev
);
508 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
509 MODULE_LICENSE("GPL v2");