xrp_firmware.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * xrp_firmware: firmware manipulation for the XRP
  3. *
  4. * Copyright (c) 2015 - 2017 Cadence Design Systems, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Alternatively you can use and distribute this file under the terms of
  26. * the GNU General Public License version 2 or later.
  27. */
  28. #include "xrp_firmware.h"
  29. #include "xrp_address_map.h"
  30. #include "xrp_hw.h"
  31. #include "xrp_internal.h"
  32. #include "xrp_kernel_dsp_interface.h"
  33. #include <linux/dma-mapping.h>
  34. #include <linux/elf.h>
  35. #include <linux/firmware.h>
  36. #include <linux/highmem.h>
  37. #include <linux/io.h>
  38. #include <linux/of.h>
  39. #include <linux/of_address.h>
  40. static phys_addr_t xrp_translate_to_cpu(struct xvp *xvp, Elf32_Phdr *phdr) {
  41. phys_addr_t res;
  42. __be32 addr = cpu_to_be32((u32)phdr->p_paddr);
  43. struct device_node *node = of_get_next_child(xvp->dev->of_node, NULL);
  44. if (!node)
  45. node = xvp->dev->of_node;
  46. res = of_translate_address(node, &addr);
  47. if (node != xvp->dev->of_node)
  48. of_node_put(node);
  49. return res;
  50. }
  51. static int xrp_load_segment_to_sysmem(struct xvp *xvp, Elf32_Phdr *phdr) {
  52. // phys_addr_t pa = xrp_translate_to_cpu(xvp, phdr);
  53. phys_addr_t pa = xrp_translate_dsp_to_host(&xvp->address_map,phdr->p_paddr);
  54. struct page *page = pfn_to_page(__phys_to_pfn(pa));
  55. size_t page_offs = pa & ~PAGE_MASK;
  56. size_t offs;
  57. for (offs = 0; offs < phdr->p_memsz; ++page) {
  58. void *p = kmap(page);
  59. size_t sz;
  60. if (!p)
  61. return -ENOMEM;
  62. page_offs &= ~PAGE_MASK;
  63. sz = PAGE_SIZE - page_offs;
  64. dev_dbg(xvp->dev, "loading segment to host addr 0x%d by host visiul %p,size:%d\n",
  65. page_offs,p,sz);
  66. if (offs < phdr->p_filesz) {
  67. size_t copy_sz = sz;
  68. if (phdr->p_filesz - offs < copy_sz)
  69. copy_sz = phdr->p_filesz - offs;
  70. copy_sz = ALIGN(copy_sz, 4);
  71. memcpy(p + page_offs, (void *)xvp->firmware->data + phdr->p_offset + offs,
  72. copy_sz);
  73. page_offs += copy_sz;
  74. offs += copy_sz;
  75. sz -= copy_sz;
  76. }
  77. dev_dbg(xvp->dev, "loading segment to host addr 0x%d by host visiul %p,size:%d\n",
  78. page_offs,p, sz);
  79. if (offs < phdr->p_memsz && sz) {
  80. if (phdr->p_memsz - offs < sz)
  81. sz = phdr->p_memsz - offs;
  82. sz = ALIGN(sz, 4);
  83. memset(p + page_offs, 0, sz);
  84. page_offs += sz;
  85. offs += sz;
  86. }
  87. kunmap(page);
  88. }
  89. dma_sync_single_for_device(xvp->dev, pa, phdr->p_memsz, DMA_TO_DEVICE);
  90. dev_dbg(xvp->dev, "xrp_load_segment_to_sysmem");
  91. return 0;
  92. }
  93. static int xrp_load_segment_to_iomem(struct xvp *xvp, Elf32_Phdr *phdr) {
  94. // phys_addr_t pa = xrp_translate_to_cpu(xvp, phdr);
  95. phys_addr_t pa = xrp_translate_dsp_to_host(&xvp->address_map,phdr->p_paddr);
  96. if(pa==OF_BAD_ADDR)
  97. {
  98. dev_err(xvp->dev, "couldn't translate DSP addr 0x%x\n", phdr->p_paddr);
  99. return -EINVAL;
  100. }
  101. void __iomem *p = ioremap(pa, phdr->p_memsz);
  102. if (!p) {
  103. dev_err(xvp->dev, "couldn't ioremap %pap x 0x%08x\n", &pa,
  104. (u32)phdr->p_memsz);
  105. return -EINVAL;
  106. }
  107. dev_dbg(xvp->dev, "loading segment to host addr 0x%pap by host virtual 0x%llx,size:%d,total size:%d,fw dataptr:0x%llx,offset:0x%x\n",
  108. &pa,p, phdr->p_filesz,(u32)phdr->p_memsz,xvp->firmware->data,phdr->p_offset);
  109. if(phdr->p_filesz)
  110. {
  111. if (xvp->hw_ops->memcpy_tohw)
  112. xvp->hw_ops->memcpy_tohw(p, (void *)xvp->firmware->data + phdr->p_offset,
  113. ALIGN(phdr->p_filesz, 4));
  114. else
  115. memcpy_toio(p, (void *)xvp->firmware->data + phdr->p_offset,
  116. ALIGN(phdr->p_filesz, 4));
  117. dev_dbg(xvp->dev, "copy size:%d\n",ALIGN(phdr->p_filesz, 4));
  118. }
  119. if(phdr->p_memsz - phdr->p_filesz)
  120. {
  121. if (xvp->hw_ops->memset_hw)
  122. xvp->hw_ops->memset_hw(p + phdr->p_filesz, 0,
  123. phdr->p_memsz - phdr->p_filesz);
  124. else
  125. memset_io(p + ALIGN(phdr->p_filesz, 4), 0,
  126. ALIGN(phdr->p_memsz - ALIGN(phdr->p_filesz, 4), 4));
  127. dev_dbg(xvp->dev, "set size:%d\n",ALIGN(phdr->p_memsz - ALIGN(phdr->p_filesz, 4),4));
  128. }
  129. iounmap(p);
  130. dev_dbg(xvp->dev, "xrp_load_segment_to_iomem done\n");
  131. return 0;
  132. }
  133. static inline bool xrp_section_bad(struct xvp *xvp, const Elf32_Shdr *shdr) {
  134. return shdr->sh_offset > xvp->firmware->size ||
  135. shdr->sh_size > xvp->firmware->size - shdr->sh_offset;
  136. }
  137. static int xrp_firmware_find_symbol(struct xvp *xvp, const char *name,
  138. void **paddr, size_t *psize) {
  139. const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)xvp->firmware->data;
  140. const void *shdr_data = xvp->firmware->data + ehdr->e_shoff;
  141. const Elf32_Shdr *sh_symtab = NULL;
  142. const Elf32_Shdr *sh_strtab = NULL;
  143. const void *sym_data;
  144. const void *str_data;
  145. const Elf32_Sym *esym;
  146. void *addr = NULL;
  147. unsigned i;
  148. if (ehdr->e_shoff == 0) {
  149. dev_dbg(xvp->dev, "%s: no section header in the firmware image", __func__);
  150. return -ENOENT;
  151. }
  152. if (ehdr->e_shoff > xvp->firmware->size ||
  153. ehdr->e_shnum * ehdr->e_shentsize > xvp->firmware->size - ehdr->e_shoff) {
  154. dev_err(xvp->dev, "%s: bad firmware SHDR information", __func__);
  155. return -EINVAL;
  156. }
  157. /* find symbols and string sections */
  158. for (i = 0; i < ehdr->e_shnum; ++i) {
  159. const Elf32_Shdr *shdr = shdr_data + i * ehdr->e_shentsize;
  160. switch (shdr->sh_type) {
  161. case SHT_SYMTAB:
  162. sh_symtab = shdr;
  163. break;
  164. case SHT_STRTAB:
  165. sh_strtab = shdr;
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. if (!sh_symtab || !sh_strtab) {
  172. dev_dbg(xvp->dev, "%s: no symtab or strtab in the firmware image",
  173. __func__);
  174. return -ENOENT;
  175. }
  176. if (xrp_section_bad(xvp, sh_symtab)) {
  177. dev_err(xvp->dev, "%s: bad firmware SYMTAB section information", __func__);
  178. return -EINVAL;
  179. }
  180. if (xrp_section_bad(xvp, sh_strtab)) {
  181. dev_err(xvp->dev, "%s: bad firmware STRTAB section information", __func__);
  182. return -EINVAL;
  183. }
  184. /* iterate through all symbols, searching for the name */
  185. sym_data = xvp->firmware->data + sh_symtab->sh_offset;
  186. str_data = xvp->firmware->data + sh_strtab->sh_offset;
  187. for (i = 0; i < sh_symtab->sh_size; i += sh_symtab->sh_entsize) {
  188. esym = sym_data + i;
  189. if (!(ELF_ST_TYPE(esym->st_info) == STT_OBJECT &&
  190. esym->st_name < sh_strtab->sh_size &&
  191. strncmp(str_data + esym->st_name, name,
  192. sh_strtab->sh_size - esym->st_name) == 0))
  193. continue;
  194. if (esym->st_shndx > 0 && esym->st_shndx < ehdr->e_shnum) {
  195. const Elf32_Shdr *shdr = shdr_data + esym->st_shndx * ehdr->e_shentsize;
  196. Elf32_Off in_section_off = esym->st_value - shdr->sh_addr;
  197. if (xrp_section_bad(xvp, shdr)) {
  198. dev_err(xvp->dev, "%s: bad firmware section #%d information", __func__,
  199. esym->st_shndx);
  200. return -EINVAL;
  201. }
  202. if (esym->st_value < shdr->sh_addr || in_section_off > shdr->sh_size ||
  203. esym->st_size > shdr->sh_size - in_section_off) {
  204. dev_err(xvp->dev, "%s: bad symbol information", __func__);
  205. return -EINVAL;
  206. }
  207. addr = (void *)xvp->firmware->data + shdr->sh_offset + in_section_off;
  208. dev_dbg(xvp->dev,
  209. "%s: found symbol, st_shndx = %d, "
  210. "sh_offset = 0x%08x, sh_addr = 0x%08x, "
  211. "st_value = 0x%08x, address = %p",
  212. __func__, esym->st_shndx, shdr->sh_offset, shdr->sh_addr,
  213. esym->st_value, addr);
  214. } else {
  215. dev_dbg(xvp->dev, "%s: unsupported section index in found symbol: 0x%x",
  216. __func__, esym->st_shndx);
  217. return -EINVAL;
  218. }
  219. break;
  220. }
  221. if (!addr)
  222. return -ENOENT;
  223. *paddr = addr;
  224. *psize = esym->st_size;
  225. return 0;
  226. }
  227. static int xrp_firmware_fixup_symbol(struct xvp *xvp, const char *name,
  228. phys_addr_t v) {
  229. u32 v32 = XRP_DSP_COMM_BASE_MAGIC;
  230. void *addr;
  231. size_t sz;
  232. int rc;
  233. if(v == XRP_NO_TRANSLATION)
  234. {
  235. dev_err(xvp->dev, "%s: invalid dsp address %llx", __func__, name);
  236. return -EINVAL;
  237. }
  238. rc = xrp_firmware_find_symbol(xvp, name, &addr, &sz);
  239. if (rc < 0) {
  240. dev_err(xvp->dev, "%s: symbol \"%s\" is not found", __func__, name);
  241. return rc;
  242. }
  243. if (sz != sizeof(u32)) {
  244. dev_err(xvp->dev, "%s: symbol \"%s\" has wrong size: %zu", __func__, name,
  245. sz);
  246. return -EINVAL;
  247. }
  248. /* update data associated with symbol */
  249. if (memcmp(addr, &v32, sz) != 0) {
  250. dev_dbg(xvp->dev, "%s: value pointed to by symbol is incorrect: %*ph",
  251. __func__, (int)sz, addr);
  252. }
  253. v32 = v;
  254. memcpy(addr, &v32, sz);
  255. return 0;
  256. }
  257. static int xrp_load_firmware(struct xvp *xvp) {
  258. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)xvp->firmware->data;
  259. int i;
  260. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  261. dev_err(xvp->dev, "bad firmware ELF magic\n");
  262. return -EINVAL;
  263. }
  264. if (ehdr->e_type != ET_EXEC) {
  265. dev_err(xvp->dev, "bad firmware ELF type\n");
  266. return -EINVAL;
  267. }
  268. if (ehdr->e_machine != 94 /*EM_XTENSA*/) {
  269. dev_err(xvp->dev, "bad firmware ELF machine\n");
  270. return -EINVAL;
  271. }
  272. if (ehdr->e_phoff >= xvp->firmware->size ||
  273. ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum > xvp->firmware->size) {
  274. dev_err(xvp->dev, "bad firmware ELF PHDR information\n");
  275. return -EINVAL;
  276. }
  277. xrp_firmware_fixup_symbol(
  278. xvp, "xrp_dsp_comm_base",
  279. xrp_translate_to_dsp(&xvp->address_map, xvp->comm_phys));
  280. for (i = 0; i < ehdr->e_phnum; ++i) {
  281. Elf32_Phdr *phdr =
  282. (void *)xvp->firmware->data + ehdr->e_phoff + i * ehdr->e_phentsize;
  283. phys_addr_t pa;
  284. int rc;
  285. /* Only load non-empty loadable segments, R/W/X */
  286. if (!(phdr->p_type == PT_LOAD && (phdr->p_flags & (PF_X | PF_R | PF_W)) &&
  287. phdr->p_memsz > 0))
  288. continue;
  289. if (phdr->p_offset >= xvp->firmware->size ||
  290. phdr->p_offset + phdr->p_filesz > xvp->firmware->size) {
  291. dev_err(xvp->dev, "bad firmware ELF program header entry %d\n", i);
  292. return -EINVAL;
  293. }
  294. // pa = xrp_translate_to_cpu(xvp, phdr);
  295. pa=phdr->p_paddr;
  296. if (pa == (phys_addr_t)OF_BAD_ADDR) {
  297. dev_err(
  298. xvp->dev,
  299. "device address 0x%08x could not be mapped to host physical address",
  300. (u32)phdr->p_paddr);
  301. return -EINVAL;
  302. }
  303. dev_dbg(xvp->dev, "loading segment %d (device 0x%08x) to physical %pap\n",
  304. i, (u32)phdr->p_paddr, &pa);
  305. // if (pfn_valid(__phys_to_pfn(pa)))
  306. // rc = xrp_load_segment_to_sysmem(xvp, phdr);
  307. // else
  308. rc = xrp_load_segment_to_iomem(xvp, phdr);
  309. if (rc < 0)
  310. return rc;
  311. }
  312. dev_dbg(xvp->dev, "loading firmware sucessful\n");
  313. return 0;
  314. }
  315. int xrp_request_firmware(struct xvp *xvp,Elf32_Addr *boot_addr) {
  316. int ret = request_firmware(&xvp->firmware, xvp->firmware_name, xvp->dev);
  317. if (ret < 0 || boot_addr == NULL)
  318. return ret;
  319. ret = xrp_load_firmware(xvp);
  320. *boot_addr = xrp_get_firmware_entry_addr(xvp);
  321. release_firmware(xvp->firmware);
  322. return ret;
  323. }
  324. Elf32_Addr xrp_get_firmware_entry_addr(struct xvp *xvp)
  325. {
  326. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)xvp->firmware->data;
  327. int i;
  328. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  329. dev_err(xvp->dev, "bad firmware ELF magic\n");
  330. return 0;
  331. }
  332. if (ehdr->e_type != ET_EXEC) {
  333. dev_err(xvp->dev, "bad firmware ELF type\n");
  334. return 0;
  335. }
  336. if (ehdr->e_machine != 94 /*EM_XTENSA*/) {
  337. dev_err(xvp->dev, "bad firmware ELF machine\n");
  338. return 0;
  339. }
  340. return ehdr->e_entry;
  341. }