remoteproc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015
  4. * Texas Instruments Incorporated - http://www.ti.com/
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <elf.h>
  9. #include <errno.h>
  10. #include <remoteproc.h>
  11. #include <asm/io.h>
  12. #include <dm/test.h>
  13. #include <test/ut.h>
  14. /**
  15. * dm_test_remoteproc_base() - test the operations after initializations
  16. * @uts: unit test state
  17. *
  18. * Return: 0 if test passed, else error
  19. */
  20. static int dm_test_remoteproc_base(struct unit_test_state *uts)
  21. {
  22. if (!rproc_is_initialized())
  23. ut_assertok(rproc_init());
  24. /* Ensure we are initialized */
  25. ut_asserteq(true, rproc_is_initialized());
  26. /* platform data device 1 */
  27. ut_assertok(rproc_stop(0));
  28. ut_assertok(rproc_reset(0));
  29. /* -> invalid attempt tests */
  30. ut_asserteq(-EINVAL, rproc_start(0));
  31. ut_asserteq(-EINVAL, rproc_ping(0));
  32. /* Valid tests */
  33. ut_assertok(rproc_load(0, 1, 0));
  34. ut_assertok(rproc_start(0));
  35. ut_assertok(rproc_is_running(0));
  36. ut_assertok(rproc_ping(0));
  37. ut_assertok(rproc_reset(0));
  38. ut_assertok(rproc_stop(0));
  39. /* dt device device 1 */
  40. ut_assertok(rproc_stop(1));
  41. ut_assertok(rproc_reset(1));
  42. ut_assertok(rproc_load(1, 1, 0));
  43. ut_assertok(rproc_start(1));
  44. ut_assertok(rproc_is_running(1));
  45. ut_assertok(rproc_ping(1));
  46. ut_assertok(rproc_reset(1));
  47. ut_assertok(rproc_stop(1));
  48. /* dt device device 2 */
  49. ut_assertok(rproc_stop(0));
  50. ut_assertok(rproc_reset(0));
  51. /* -> invalid attempt tests */
  52. ut_asserteq(-EINVAL, rproc_start(0));
  53. ut_asserteq(-EINVAL, rproc_ping(0));
  54. /* Valid tests */
  55. ut_assertok(rproc_load(2, 1, 0));
  56. ut_assertok(rproc_start(2));
  57. ut_assertok(rproc_is_running(2));
  58. ut_assertok(rproc_ping(2));
  59. ut_assertok(rproc_reset(2));
  60. ut_assertok(rproc_stop(2));
  61. return 0;
  62. }
  63. DM_TEST(dm_test_remoteproc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  64. #define DEVICE_TO_PHYSICAL_OFFSET 0x1000
  65. /**
  66. * dm_test_remoteproc_elf() - test the ELF operations
  67. * @uts: unit test state
  68. *
  69. * Return: 0 if test passed, else error
  70. */
  71. static int dm_test_remoteproc_elf(struct unit_test_state *uts)
  72. {
  73. u8 valid_elf32[] = {
  74. /* @0x00 - ELF HEADER - */
  75. /* ELF magic */
  76. 0x7f, 0x45, 0x4c, 0x46,
  77. /* 32 Bits */
  78. 0x01,
  79. /* Endianness */
  80. #ifdef __LITTLE_ENDIAN
  81. 0x01,
  82. #else
  83. 0x02,
  84. #endif
  85. /* Version */
  86. 0x01,
  87. /* Padding */
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. /* Type : executable */
  90. 0x02, 0x00,
  91. /* Machine: ARM */
  92. 0x28, 0x00,
  93. /* Version */
  94. 0x01, 0x00, 0x00, 0x00,
  95. /* Entry */
  96. 0x00, 0x00, 0x00, 0x08,
  97. /* phoff (program header offset @ 0x40)*/
  98. 0x40, 0x00, 0x00, 0x00,
  99. /* shoff (section header offset @ 0x90) */
  100. 0x90, 0x00, 0x00, 0x00,
  101. /* flags */
  102. 0x00, 0x00, 0x00, 0x00,
  103. /* ehsize (elf header size = 0x34) */
  104. 0x34, 0x00,
  105. /* phentsize (program header size = 0x20) */
  106. 0x20, 0x00,
  107. /* phnum (program header number : 1) */
  108. 0x01, 0x00,
  109. /* shentsize (section header size : 40 bytes) */
  110. 0x28, 0x00,
  111. /* shnum (section header number: 3) */
  112. 0x02, 0x00,
  113. /* shstrndx (section header name section index: 1) */
  114. 0x01, 0x00,
  115. /* padding */
  116. 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00, 0x00,
  119. /* @0x40 - PROGRAM HEADER TABLE - */
  120. /* type : PT_LOAD */
  121. 0x01, 0x00, 0x00, 0x00,
  122. /* offset */
  123. 0x00, 0x00, 0x00, 0x00,
  124. /* vaddr */
  125. 0x00, 0x00, 0x00, 0x00,
  126. /* paddr : physical address */
  127. 0x00, 0x00, 0x00, 0x00,
  128. /* filesz : 0x20 bytes (program header size) */
  129. 0x20, 0x00, 0x00, 0x00,
  130. /* memsz = filesz */
  131. 0x20, 0x00, 0x00, 0x00,
  132. /* flags : readable and exectuable */
  133. 0x05, 0x00, 0x00, 0x00,
  134. /* padding */
  135. 0x00, 0x00, 0x00, 0x00,
  136. /* @0x60 - RESOURCE TABLE SECTION - */
  137. /* version */
  138. 0x01, 0x00, 0x00, 0x00,
  139. /* num (0, no entries) */
  140. 0x00, 0x00, 0x00, 0x00,
  141. /* Reserved */
  142. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. /* @0x70 - SECTION'S NAMES SECTION - */
  144. /* section 0 name (".shrtrtab") */
  145. 0x2e, 0x73, 0x68, 0x73, 0x74, 0x72, 0x74, 0x61, 0x62, 0x00,
  146. /* section 1 name (".resource_table") */
  147. 0x2e, 0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x5f,
  148. 0x74, 0x61, 0x62, 0x6c, 0x65, 0x00,
  149. /* padding */
  150. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  151. /* @0x90 - SECTION HEADER TABLE - */
  152. /* Section 0 : resource table header */
  153. /* sh_name - index into section header string table section */
  154. 0x0a, 0x00, 0x00, 0x00,
  155. /* sh_type and sh_flags */
  156. 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  157. /* sh_addr = where the resource table has to be copied to */
  158. 0x00, 0x00, 0x00, 0x00,
  159. /* sh_offset = 0x60 */
  160. 0x60, 0x00, 0x00, 0x00,
  161. /* sh_size = 16 bytes */
  162. 0x10, 0x00, 0x00, 0x00,
  163. /* sh_link, sh_info, sh_addralign, sh_entsize */
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  166. /* Section 1 : section's names section header */
  167. /* sh_name - index into section header string table section */
  168. 0x00, 0x00, 0x00, 0x00,
  169. /* sh_type and sh_flags */
  170. 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  171. /* sh_addr */
  172. 0x00, 0x00, 0x00, 0x00,
  173. /* sh_offset = 0x70 */
  174. 0x70, 0x00, 0x00, 0x00,
  175. /* sh_size = 27 bytes */
  176. 0x1b, 0x00, 0x00, 0x00,
  177. /* sh_link, sh_info, sh_addralign, sh_entsize */
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  180. };
  181. unsigned int size = ARRAY_SIZE(valid_elf32);
  182. struct udevice *dev;
  183. phys_addr_t loaded_firmware_paddr, loaded_rsc_table_paddr;
  184. void *loaded_firmware, *loaded_rsc_table;
  185. u32 loaded_firmware_size, rsc_table_size;
  186. ulong rsc_addr, rsc_size;
  187. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)valid_elf32;
  188. Elf32_Phdr *phdr = (Elf32_Phdr *)(valid_elf32 + ehdr->e_phoff);
  189. Elf32_Shdr *shdr = (Elf32_Shdr *)(valid_elf32 + ehdr->e_shoff);
  190. ut_assertok(uclass_get_device(UCLASS_REMOTEPROC, 0, &dev));
  191. /*
  192. * In its Program Header Table, let the firmware specifies to be loaded
  193. * at SDRAM_BASE *device* address (p_paddr field).
  194. * Its size is defined by the p_filesz field.
  195. */
  196. phdr->p_paddr = CONFIG_SYS_SDRAM_BASE;
  197. loaded_firmware_size = phdr->p_filesz;
  198. /*
  199. * This *device* address is converted to a *physical* address by the
  200. * device_to_virt() operation of sandbox_test_rproc which returns
  201. * DeviceAddress + DEVICE_TO_PHYSICAL_OFFSET.
  202. * This is where we expect to get the firmware loaded.
  203. */
  204. loaded_firmware_paddr = phdr->p_paddr + DEVICE_TO_PHYSICAL_OFFSET;
  205. loaded_firmware = map_physmem(loaded_firmware_paddr,
  206. loaded_firmware_size, MAP_NOCACHE);
  207. ut_assertnonnull(loaded_firmware);
  208. memset(loaded_firmware, 0, loaded_firmware_size);
  209. /* Load firmware in loaded_firmware, and verify it */
  210. ut_assertok(rproc_elf32_load_image(dev, (ulong)valid_elf32, size));
  211. ut_asserteq_mem(loaded_firmware, valid_elf32, loaded_firmware_size);
  212. ut_asserteq(rproc_elf_get_boot_addr(dev, (unsigned long)valid_elf32),
  213. 0x08000000);
  214. unmap_physmem(loaded_firmware, MAP_NOCACHE);
  215. /* Resource table */
  216. shdr->sh_addr = CONFIG_SYS_SDRAM_BASE;
  217. rsc_table_size = shdr->sh_size;
  218. loaded_rsc_table_paddr = shdr->sh_addr + DEVICE_TO_PHYSICAL_OFFSET;
  219. loaded_rsc_table = map_physmem(loaded_rsc_table_paddr,
  220. rsc_table_size, MAP_NOCACHE);
  221. ut_assertnonnull(loaded_rsc_table);
  222. memset(loaded_rsc_table, 0, rsc_table_size);
  223. /* Load and verify */
  224. ut_assertok(rproc_elf32_load_rsc_table(dev, (ulong)valid_elf32, size,
  225. &rsc_addr, &rsc_size));
  226. ut_asserteq(rsc_addr, CONFIG_SYS_SDRAM_BASE);
  227. ut_asserteq(rsc_size, rsc_table_size);
  228. ut_asserteq_mem(loaded_firmware, valid_elf32 + shdr->sh_offset,
  229. shdr->sh_size);
  230. unmap_physmem(loaded_firmware, MAP_NOCACHE);
  231. /* Invalid ELF Magic */
  232. valid_elf32[0] = 0;
  233. ut_asserteq(-EPROTONOSUPPORT,
  234. rproc_elf32_sanity_check((ulong)valid_elf32, size));
  235. return 0;
  236. }
  237. DM_TEST(dm_test_remoteproc_elf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);