remoteproc.c 7.6 KB

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