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 <linux/math64.h>
10 #include "qcom_pil_info.h"
13 * The PIL relocation information region is used to communicate memory regions
14 * occupied by co-processor firmware for post mortem crash analysis.
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
20 #define PIL_RELOC_NAME_LEN 8
21 #define PIL_RELOC_ENTRY_SIZE (PIL_RELOC_NAME_LEN + sizeof(__le64) + sizeof(__le32))
28 static struct pil_reloc _reloc __read_mostly
;
29 static DEFINE_MUTEX(pil_reloc_lock
);
31 static int qcom_pil_info_init(void)
33 struct device_node
*np
;
38 /* Already initialized? */
42 np
= of_find_compatible_node(NULL
, NULL
, "qcom,pil-reloc-info");
46 ret
= of_address_to_resource(np
, 0, &imem
);
51 base
= ioremap(imem
.start
, resource_size(&imem
));
53 pr_err("failed to map PIL relocation info region\n");
57 memset_io(base
, 0, resource_size(&imem
));
60 _reloc
.num_entries
= div_u64(resource_size(&imem
), PIL_RELOC_ENTRY_SIZE
);
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
71 * Return: 0 on success, negative errno on failure
73 int qcom_pil_info_store(const char *image
, phys_addr_t base
, size_t size
)
75 char buf
[PIL_RELOC_NAME_LEN
];
80 mutex_lock(&pil_reloc_lock
);
81 ret
= qcom_pil_info_init();
83 mutex_unlock(&pil_reloc_lock
);
87 for (i
= 0; i
< _reloc
.num_entries
; i
++) {
88 entry
= _reloc
.base
+ i
* PIL_RELOC_ENTRY_SIZE
;
90 memcpy_fromio(buf
, entry
, PIL_RELOC_NAME_LEN
);
93 * An empty record means we didn't find it, given that the
99 if (!strncmp(buf
, image
, PIL_RELOC_NAME_LEN
))
103 pr_warn("insufficient PIL info slots\n");
104 mutex_unlock(&pil_reloc_lock
);
108 memcpy_toio(entry
, image
, PIL_RELOC_NAME_LEN
);
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
);
118 EXPORT_SYMBOL_GPL(qcom_pil_info_store
);
120 static void __exit
pil_reloc_exit(void)
122 mutex_lock(&pil_reloc_lock
);
123 iounmap(_reloc
.base
);
125 mutex_unlock(&pil_reloc_lock
);
127 module_exit(pil_reloc_exit
);
129 MODULE_DESCRIPTION("Qualcomm PIL relocation info");
130 MODULE_LICENSE("GPL v2");