1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments Incorporated - https://www.ti.com/
11 #include <remoteproc.h>
14 #include <test/test.h>
18 * dm_test_remoteproc_base() - test the operations after initializations
19 * @uts: unit test state
21 * Return: 0 if test passed, else error
23 static int dm_test_remoteproc_base(struct unit_test_state *uts)
25 if (!rproc_is_initialized())
26 ut_assertok(rproc_init());
28 /* Ensure we are initialized */
29 ut_asserteq(true, rproc_is_initialized());
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));
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));
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));
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));
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));
71 DM_TEST(dm_test_remoteproc_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
73 #define DEVICE_TO_PHYSICAL_OFFSET 0x1000
75 * dm_test_remoteproc_elf() - test the ELF operations
76 * @uts: unit test state
78 * Return: 0 if test passed, else error
80 static int dm_test_remoteproc_elf(struct unit_test_state *uts)
83 /* @0x00 - ELF HEADER - */
85 0x7f, 0x45, 0x4c, 0x46,
89 #ifdef __LITTLE_ENDIAN
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 /* Type : executable */
103 0x01, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x08,
106 /* phoff (program header offset @ 0x40)*/
107 0x40, 0x00, 0x00, 0x00,
108 /* shoff (section header offset @ 0x90) */
109 0x90, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00,
112 /* ehsize (elf header size = 0x34) */
114 /* phentsize (program header size = 0x20) */
116 /* phnum (program header number : 1) */
118 /* shentsize (section header size : 40 bytes) */
120 /* shnum (section header number: 3) */
122 /* shstrndx (section header name section index: 1) */
125 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00,
129 /* @0x40 - PROGRAM HEADER TABLE - */
131 0x01, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x00, 0x00,
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,
141 0x20, 0x00, 0x00, 0x00,
142 /* flags : readable and executable */
143 0x05, 0x00, 0x00, 0x00,
145 0x00, 0x00, 0x00, 0x00,
147 /* @0x60 - RESOURCE TABLE SECTION - */
149 0x01, 0x00, 0x00, 0x00,
150 /* num (0, no entries) */
151 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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,
162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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,
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,
194 unsigned int size = ARRAY_SIZE(valid_elf32);
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;
200 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)valid_elf32;
201 Elf32_Phdr *phdr = (Elf32_Phdr *)(valid_elf32 + ehdr->e_phoff);
202 Elf32_Shdr *shdr = (Elf32_Shdr *)(valid_elf32 + ehdr->e_shoff);
204 ut_assertok(uclass_get_device(UCLASS_REMOTEPROC, 0, &dev));
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.
211 phdr->p_paddr = CFG_SYS_SDRAM_BASE;
212 loaded_firmware_size = phdr->p_filesz;
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.
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);
226 /* Load firmware in loaded_firmware, and verify it */
227 ut_assertok(rproc_elf32_load_image(dev, (ulong)valid_elf32, size));
228 ut_asserteq_mem(loaded_firmware, valid_elf32, loaded_firmware_size);
229 ut_asserteq(rproc_elf_get_boot_addr(dev, (unsigned long)valid_elf32),
231 unmap_physmem(loaded_firmware, MAP_NOCACHE);
234 shdr->sh_addr = CFG_SYS_SDRAM_BASE;
235 rsc_table_size = shdr->sh_size;
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);
243 /* Load and verify */
244 ut_assertok(rproc_elf32_load_rsc_table(dev, (ulong)valid_elf32, size,
245 &rsc_addr, &rsc_size));
246 ut_asserteq(rsc_addr, CFG_SYS_SDRAM_BASE);
247 ut_asserteq(rsc_size, rsc_table_size);
248 ut_asserteq_mem(loaded_firmware, valid_elf32 + shdr->sh_offset,
250 unmap_physmem(loaded_firmware, MAP_NOCACHE);
252 /* Invalid ELF Magic */
254 ut_asserteq(-EPROTONOSUPPORT,
255 rproc_elf32_sanity_check((ulong)valid_elf32, size));
259 DM_TEST(dm_test_remoteproc_elf, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);