qcom_pil_info.c 3.0 KB

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