Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / remoteproc / qcom_pil_info.c
blob5521c4437ffabd1848f8dcd43d1526bd9d440116
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2019-2020 Linaro Ltd.
4 */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
8 #include <linux/of_address.h>
9 #include "qcom_pil_info.h"
12 * The PIL relocation information region is used to communicate memory regions
13 * occupied by co-processor firmware for post mortem crash analysis.
15 * It consists of an array of entries with an 8 byte textual identifier of the
16 * region followed by a 64 bit base address and 32 bit size, both little
17 * endian.
19 #define PIL_RELOC_NAME_LEN 8
20 #define PIL_RELOC_ENTRY_SIZE (PIL_RELOC_NAME_LEN + sizeof(__le64) + sizeof(__le32))
22 struct pil_reloc {
23 void __iomem *base;
24 size_t num_entries;
27 static struct pil_reloc _reloc __read_mostly;
28 static DEFINE_MUTEX(pil_reloc_lock);
30 static int qcom_pil_info_init(void)
32 struct device_node *np;
33 struct resource imem;
34 void __iomem *base;
35 int ret;
37 /* Already initialized? */
38 if (_reloc.base)
39 return 0;
41 np = of_find_compatible_node(NULL, NULL, "qcom,pil-reloc-info");
42 if (!np)
43 return -ENOENT;
45 ret = of_address_to_resource(np, 0, &imem);
46 of_node_put(np);
47 if (ret < 0)
48 return ret;
50 base = ioremap(imem.start, resource_size(&imem));
51 if (!base) {
52 pr_err("failed to map PIL relocation info region\n");
53 return -ENOMEM;
56 memset_io(base, 0, resource_size(&imem));
58 _reloc.base = base;
59 _reloc.num_entries = resource_size(&imem) / PIL_RELOC_ENTRY_SIZE;
61 return 0;
64 /**
65 * qcom_pil_info_store() - store PIL information of image in IMEM
66 * @image: name of the image
67 * @base: base address of the loaded image
68 * @size: size of the loaded image
70 * Return: 0 on success, negative errno on failure
72 int qcom_pil_info_store(const char *image, phys_addr_t base, size_t size)
74 char buf[PIL_RELOC_NAME_LEN];
75 void __iomem *entry;
76 int ret;
77 int i;
79 mutex_lock(&pil_reloc_lock);
80 ret = qcom_pil_info_init();
81 if (ret < 0) {
82 mutex_unlock(&pil_reloc_lock);
83 return ret;
86 for (i = 0; i < _reloc.num_entries; i++) {
87 entry = _reloc.base + i * PIL_RELOC_ENTRY_SIZE;
89 memcpy_fromio(buf, entry, PIL_RELOC_NAME_LEN);
92 * An empty record means we didn't find it, given that the
93 * records are packed.
95 if (!buf[0])
96 goto found_unused;
98 if (!strncmp(buf, image, PIL_RELOC_NAME_LEN))
99 goto found_existing;
102 pr_warn("insufficient PIL info slots\n");
103 mutex_unlock(&pil_reloc_lock);
104 return -ENOMEM;
106 found_unused:
107 memcpy_toio(entry, image, PIL_RELOC_NAME_LEN);
108 found_existing:
109 /* Use two writel() as base is only aligned to 4 bytes on odd entries */
110 writel(base, entry + PIL_RELOC_NAME_LEN);
111 writel((u64)base >> 32, entry + PIL_RELOC_NAME_LEN + 4);
112 writel(size, entry + PIL_RELOC_NAME_LEN + sizeof(__le64));
113 mutex_unlock(&pil_reloc_lock);
115 return 0;
117 EXPORT_SYMBOL_GPL(qcom_pil_info_store);
119 static void __exit pil_reloc_exit(void)
121 mutex_lock(&pil_reloc_lock);
122 iounmap(_reloc.base);
123 _reloc.base = NULL;
124 mutex_unlock(&pil_reloc_lock);
126 module_exit(pil_reloc_exit);
128 MODULE_DESCRIPTION("Qualcomm PIL relocation info");
129 MODULE_LICENSE("GPL v2");