1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2019-2020 Linaro Ltd.
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
19 #define PIL_RELOC_NAME_LEN 8
20 #define PIL_RELOC_ENTRY_SIZE (PIL_RELOC_NAME_LEN + sizeof(__le64) + sizeof(__le32))
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
;
37 /* Already initialized? */
41 np
= of_find_compatible_node(NULL
, NULL
, "qcom,pil-reloc-info");
45 ret
= of_address_to_resource(np
, 0, &imem
);
50 base
= ioremap(imem
.start
, resource_size(&imem
));
52 pr_err("failed to map PIL relocation info region\n");
56 memset_io(base
, 0, resource_size(&imem
));
59 _reloc
.num_entries
= resource_size(&imem
) / PIL_RELOC_ENTRY_SIZE
;
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
];
79 mutex_lock(&pil_reloc_lock
);
80 ret
= qcom_pil_info_init();
82 mutex_unlock(&pil_reloc_lock
);
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
98 if (!strncmp(buf
, image
, PIL_RELOC_NAME_LEN
))
102 pr_warn("insufficient PIL info slots\n");
103 mutex_unlock(&pil_reloc_lock
);
107 memcpy_toio(entry
, image
, PIL_RELOC_NAME_LEN
);
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
);
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
);
124 mutex_unlock(&pil_reloc_lock
);
126 module_exit(pil_reloc_exit
);
128 MODULE_DESCRIPTION("Qualcomm PIL relocation info");
129 MODULE_LICENSE("GPL v2");