blob: 1cc07bc80832b9afef96fd442b48029322ba60e8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nishanth Menonbfaa2d92015-09-17 15:42:42 -05002/*
3 * (C) Copyright 2015
4 * Texas Instruments Incorporated - http://www.ti.com/
Nishanth Menonbfaa2d92015-09-17 15:42:42 -05005 */
6#include <common.h>
7#include <dm.h>
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +02008#include <elf.h>
Nishanth Menonbfaa2d92015-09-17 15:42:42 -05009#include <errno.h>
10#include <remoteproc.h>
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +020011#include <asm/io.h>
Nishanth Menonbfaa2d92015-09-17 15:42:42 -050012#include <dm/test.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060013#include <test/test.h>
Nishanth Menonbfaa2d92015-09-17 15:42:42 -050014#include <test/ut.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060015
Nishanth Menonbfaa2d92015-09-17 15:42:42 -050016/**
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 */
22static 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 Glasse180c2b2020-07-28 19:41:12 -060071DM_TEST(dm_test_remoteproc_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +020072
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 */
80static 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 Dessenneffcb8802019-10-30 14:38:28 +0100108 /* shoff (section header offset @ 0x90) */
109 0x90, 0x00, 0x00, 0x00,
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200110 /* 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 Dessenneffcb8802019-10-30 14:38:28 +0100118 /* 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 Dessenne7a7c4cb2019-05-31 15:11:33 +0200124 /* padding */
125 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00,
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100128
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200129 /* @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,
142 /* flags : readable and exectuable */
143 0x05, 0x00, 0x00, 0x00,
144 /* padding */
145 0x00, 0x00, 0x00, 0x00,
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100146
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 Dessenne7a7c4cb2019-05-31 15:11:33 +0200193 };
194 unsigned int size = ARRAY_SIZE(valid_elf32);
195 struct udevice *dev;
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100196 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 Dessenne7a7c4cb2019-05-31 15:11:33 +0200200 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)valid_elf32;
201 Elf32_Phdr *phdr = (Elf32_Phdr *)(valid_elf32 + ehdr->e_phoff);
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100202 Elf32_Shdr *shdr = (Elf32_Shdr *)(valid_elf32 + ehdr->e_shoff);
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200203
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 */
211 phdr->p_paddr = CONFIG_SYS_SDRAM_BASE;
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 Dessenne7a7c4cb2019-05-31 15:11:33 +0200226 /* Load firmware in loaded_firmware, and verify it */
Lokesh Vutla14d963d2019-09-04 16:01:28 +0530227 ut_assertok(rproc_elf32_load_image(dev, (ulong)valid_elf32, size));
Simon Glassf91f3662020-05-10 12:52:45 -0600228 ut_asserteq_mem(loaded_firmware, valid_elf32, loaded_firmware_size);
Lokesh Vutla81e39fb2019-09-04 16:01:31 +0530229 ut_asserteq(rproc_elf_get_boot_addr(dev, (unsigned long)valid_elf32),
230 0x08000000);
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200231 unmap_physmem(loaded_firmware, MAP_NOCACHE);
232
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100233 /* Resource table */
234 shdr->sh_addr = CONFIG_SYS_SDRAM_BASE;
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));
246 ut_asserteq(rsc_addr, CONFIG_SYS_SDRAM_BASE);
247 ut_asserteq(rsc_size, rsc_table_size);
Simon Glassf91f3662020-05-10 12:52:45 -0600248 ut_asserteq_mem(loaded_firmware, valid_elf32 + shdr->sh_offset,
249 shdr->sh_size);
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100250 unmap_physmem(loaded_firmware, MAP_NOCACHE);
251
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200252 /* 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 Glasse180c2b2020-07-28 19:41:12 -0600259DM_TEST(dm_test_remoteproc_elf, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);