Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / remoteproc / qcom_common.c
blob4b91e3c9eafa1e2fd264ee2826c65ef1ee92f5ac
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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.
8 */
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)
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
44 struct minidump_region {
45 char name[MAX_REGION_NAME_LENGTH];
46 __le32 seq_num;
47 __le32 valid;
48 __le64 address;
49 __le64 size;
52 /**
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 {
62 __le32 status;
63 __le32 enabled;
64 __le32 encryption_status;
65 __le32 encryption_required;
66 __le32 region_count;
67 __le64 regions_baseptr;
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
77 struct minidump_global_toc {
78 __le32 status;
79 __le32 md_revision;
80 __le32 enabled;
81 struct minidump_subsystem subsystems[MAX_NUM_OF_SS];
84 struct qcom_ssr_subsystem {
85 const char *name;
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);
99 kfree(entry->priv);
100 kfree(entry);
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;
108 int seg_cnt, i;
109 dma_addr_t da;
110 size_t size;
111 char *name;
113 if (WARN_ON(!list_empty(&rproc->dump_segments))) {
114 dev_err(&rproc->dev, "dump segment list already populated\n");
115 return -EUCLEAN;
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));
121 if (!ptr)
122 return -EFAULT;
124 for (i = 0; i < seg_cnt; i++) {
125 memcpy_fromio(&region, ptr + i, sizeof(region));
126 if (region.valid == MD_REGION_VALID) {
127 name = kstrdup(region.name, GFP_KERNEL);
128 if (!name) {
129 iounmap(ptr);
130 return -ENOMEM;
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);
138 iounmap(ptr);
139 return 0;
142 void qcom_minidump(struct rproc *rproc, unsigned int minidump_id)
144 int ret;
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");
154 return;
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");
169 return;
172 ret = qcom_add_minidump_segments(rproc, subsystem);
173 if (ret) {
174 dev_err(&rproc->dev, "Failed with error: %d while adding minidump entries\n", ret);
175 goto clean_minidump;
177 rproc_coredump_using_sections(rproc);
178 clean_minidump:
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);
197 glink->edge = NULL;
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");
219 if (!glink->node)
220 return;
222 glink->ssr_name = kstrdup_const(ssr_name, GFP_KERNEL);
223 if (!glink->ssr_name)
224 return;
226 glink->dev = dev;
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)
242 if (!glink->node)
243 return;
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;
266 int ret;
267 int i;
269 ehdr = (struct elf32_hdr *)fw->data;
270 phdrs = (struct elf32_phdr *)(ehdr + 1);
272 for (i = 0; i < ehdr->e_phnum; i++) {
273 phdr = &phdrs[i];
275 if (phdr->p_type != PT_LOAD)
276 continue;
278 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
279 continue;
281 if (!phdr->p_memsz)
282 continue;
284 ret = rproc_coredump_add_segment(rproc, phdr->p_paddr,
285 phdr->p_memsz);
286 if (ret)
287 return ret;
290 return 0;
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);
308 smd->edge = NULL;
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");
321 if (!smd->node)
322 return;
324 smd->dev = dev;
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)
339 if (!smd->node)
340 return;
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))
355 goto out;
357 info = kzalloc(sizeof(*info), GFP_KERNEL);
358 if (!info) {
359 info = ERR_PTR(-ENOMEM);
360 goto out;
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);
368 out:
369 mutex_unlock(&qcom_ssr_subsys_lock);
370 return info;
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);
390 if (IS_ERR(info))
391 return info;
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
405 * chain.
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,
420 .crashed = false,
423 srcu_notifier_call_chain(&ssr->info->notifier_list,
424 QCOM_SSR_BEFORE_POWERUP, &data);
425 return 0;
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,
433 .crashed = false,
436 srcu_notifier_call_chain(&ssr->info->notifier_list,
437 QCOM_SSR_AFTER_POWERUP, &data);
438 return 0;
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,
446 .crashed = crashed,
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,
458 .crashed = false,
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);
481 if (IS_ERR(info)) {
482 dev_err(&rproc->dev, "Failed to add ssr subdevice\n");
483 return;
486 ssr->info = info;
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);
504 ssr->info = NULL;
506 EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
508 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
509 MODULE_LICENSE("GPL v2");