]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/remoteproc/qcom_pil_info.c
UBUNTU: SAUCE: remoteproc: qcom: Use div_u64() for 64-bit division
[mirror_ubuntu-hirsute-kernel.git] / drivers / remoteproc / qcom_pil_info.c
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 <linux/math64.h>
10 #include "qcom_pil_info.h"
11
12 /*
13 * The PIL relocation information region is used to communicate memory regions
14 * occupied by co-processor firmware for post mortem crash analysis.
15 *
16 * It consists of an array of entries with an 8 byte textual identifier of the
17 * region followed by a 64 bit base address and 32 bit size, both little
18 * endian.
19 */
20 #define PIL_RELOC_NAME_LEN 8
21 #define PIL_RELOC_ENTRY_SIZE (PIL_RELOC_NAME_LEN + sizeof(__le64) + sizeof(__le32))
22
23 struct pil_reloc {
24 void __iomem *base;
25 size_t num_entries;
26 };
27
28 static struct pil_reloc _reloc __read_mostly;
29 static DEFINE_MUTEX(pil_reloc_lock);
30
31 static int qcom_pil_info_init(void)
32 {
33 struct device_node *np;
34 struct resource imem;
35 void __iomem *base;
36 int ret;
37
38 /* Already initialized? */
39 if (_reloc.base)
40 return 0;
41
42 np = of_find_compatible_node(NULL, NULL, "qcom,pil-reloc-info");
43 if (!np)
44 return -ENOENT;
45
46 ret = of_address_to_resource(np, 0, &imem);
47 of_node_put(np);
48 if (ret < 0)
49 return ret;
50
51 base = ioremap(imem.start, resource_size(&imem));
52 if (!base) {
53 pr_err("failed to map PIL relocation info region\n");
54 return -ENOMEM;
55 }
56
57 memset_io(base, 0, resource_size(&imem));
58
59 _reloc.base = base;
60 _reloc.num_entries = div_u64(resource_size(&imem), PIL_RELOC_ENTRY_SIZE);
61
62 return 0;
63 }
64
65 /**
66 * qcom_pil_info_store() - store PIL information of image in IMEM
67 * @image: name of the image
68 * @base: base address of the loaded image
69 * @size: size of the loaded image
70 *
71 * Return: 0 on success, negative errno on failure
72 */
73 int qcom_pil_info_store(const char *image, phys_addr_t base, size_t size)
74 {
75 char buf[PIL_RELOC_NAME_LEN];
76 void __iomem *entry;
77 int ret;
78 int i;
79
80 mutex_lock(&pil_reloc_lock);
81 ret = qcom_pil_info_init();
82 if (ret < 0) {
83 mutex_unlock(&pil_reloc_lock);
84 return ret;
85 }
86
87 for (i = 0; i < _reloc.num_entries; i++) {
88 entry = _reloc.base + i * PIL_RELOC_ENTRY_SIZE;
89
90 memcpy_fromio(buf, entry, PIL_RELOC_NAME_LEN);
91
92 /*
93 * An empty record means we didn't find it, given that the
94 * records are packed.
95 */
96 if (!buf[0])
97 goto found_unused;
98
99 if (!strncmp(buf, image, PIL_RELOC_NAME_LEN))
100 goto found_existing;
101 }
102
103 pr_warn("insufficient PIL info slots\n");
104 mutex_unlock(&pil_reloc_lock);
105 return -ENOMEM;
106
107 found_unused:
108 memcpy_toio(entry, image, PIL_RELOC_NAME_LEN);
109 found_existing:
110 /* Use two writel() as base is only aligned to 4 bytes on odd entries */
111 writel(base, entry + PIL_RELOC_NAME_LEN);
112 writel((u64)base >> 32, entry + PIL_RELOC_NAME_LEN + 4);
113 writel(size, entry + PIL_RELOC_NAME_LEN + sizeof(__le64));
114 mutex_unlock(&pil_reloc_lock);
115
116 return 0;
117 }
118 EXPORT_SYMBOL_GPL(qcom_pil_info_store);
119
120 static void __exit pil_reloc_exit(void)
121 {
122 mutex_lock(&pil_reloc_lock);
123 iounmap(_reloc.base);
124 _reloc.base = NULL;
125 mutex_unlock(&pil_reloc_lock);
126 }
127 module_exit(pil_reloc_exit);
128
129 MODULE_DESCRIPTION("Qualcomm PIL relocation info");
130 MODULE_LICENSE("GPL v2");