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