blob: ef9e8e5a0df6d2a8af9252feff7ee5e6cea63045 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nishanth Menonaab9a932015-09-17 15:42:42 -05002/*
3 * (C) Copyright 2015
Nishanth Menoneaa39c62023-11-01 15:56:03 -05004 * Texas Instruments Incorporated - https://www.ti.com/
Nishanth Menonaab9a932015-09-17 15:42:42 -05005 */
Tom Rinidec7ea02024-05-20 13:35:03 -06006
7#include <config.h>
Nishanth Menonaab9a932015-09-17 15:42:42 -05008#include <dm.h>
Fabien Dessenneedbbdad2019-05-31 15:11:33 +02009#include <elf.h>
Nishanth Menonaab9a932015-09-17 15:42:42 -050010#include <errno.h>
11#include <remoteproc.h>
Fabien Dessenneedbbdad2019-05-31 15:11:33 +020012#include <asm/io.h>
Nishanth Menonaab9a932015-09-17 15:42:42 -050013#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060014#include <test/test.h>
Nishanth Menonaab9a932015-09-17 15:42:42 -050015#include <test/ut.h>
Simon Glass75c4d412020-07-19 10:15:37 -060016
Nishanth Menonaab9a932015-09-17 15:42:42 -050017/**
18 * dm_test_remoteproc_base() - test the operations after initializations
19 * @uts: unit test state
20 *
21 * Return: 0 if test passed, else error
22 */
23static int dm_test_remoteproc_base(struct unit_test_state *uts)
24{
25 if (!rproc_is_initialized())
26 ut_assertok(rproc_init());
27
28 /* Ensure we are initialized */
29 ut_asserteq(true, rproc_is_initialized());
30
31
32 /* platform data device 1 */
33 ut_assertok(rproc_stop(0));
34 ut_assertok(rproc_reset(0));
35 /* -> invalid attempt tests */
36 ut_asserteq(-EINVAL, rproc_start(0));
37 ut_asserteq(-EINVAL, rproc_ping(0));
38 /* Valid tests */
39 ut_assertok(rproc_load(0, 1, 0));
40 ut_assertok(rproc_start(0));
41 ut_assertok(rproc_is_running(0));
42 ut_assertok(rproc_ping(0));
43 ut_assertok(rproc_reset(0));
44 ut_assertok(rproc_stop(0));
45
46 /* dt device device 1 */
47 ut_assertok(rproc_stop(1));
48 ut_assertok(rproc_reset(1));
49 ut_assertok(rproc_load(1, 1, 0));
50 ut_assertok(rproc_start(1));
51 ut_assertok(rproc_is_running(1));
52 ut_assertok(rproc_ping(1));
53 ut_assertok(rproc_reset(1));
54 ut_assertok(rproc_stop(1));
55
56 /* dt device device 2 */
57 ut_assertok(rproc_stop(0));
58 ut_assertok(rproc_reset(0));
59 /* -> invalid attempt tests */
60 ut_asserteq(-EINVAL, rproc_start(0));
61 ut_asserteq(-EINVAL, rproc_ping(0));
62 /* Valid tests */
63 ut_assertok(rproc_load(2, 1, 0));
64 ut_assertok(rproc_start(2));
65 ut_assertok(rproc_is_running(2));
66 ut_assertok(rproc_ping(2));
67 ut_assertok(rproc_reset(2));
68 ut_assertok(rproc_stop(2));
69
70 return 0;
71}
Simon Glass974dccd2020-07-28 19:41:12 -060072DM_TEST(dm_test_remoteproc_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Fabien Dessenneedbbdad2019-05-31 15:11:33 +020073
74#define DEVICE_TO_PHYSICAL_OFFSET 0x1000
75/**
76 * dm_test_remoteproc_elf() - test the ELF operations
77 * @uts: unit test state
78 *
79 * Return: 0 if test passed, else error
80 */
81static int dm_test_remoteproc_elf(struct unit_test_state *uts)
82{
83 u8 valid_elf32[] = {
84 /* @0x00 - ELF HEADER - */
85 /* ELF magic */
86 0x7f, 0x45, 0x4c, 0x46,
87 /* 32 Bits */
88 0x01,
89 /* Endianness */
90#ifdef __LITTLE_ENDIAN
91 0x01,
92#else
93 0x02,
94#endif
95 /* Version */
96 0x01,
97 /* Padding */
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 /* Type : executable */
100 0x02, 0x00,
101 /* Machine: ARM */
102 0x28, 0x00,
103 /* Version */
104 0x01, 0x00, 0x00, 0x00,
105 /* Entry */
106 0x00, 0x00, 0x00, 0x08,
107 /* phoff (program header offset @ 0x40)*/
108 0x40, 0x00, 0x00, 0x00,
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100109 /* shoff (section header offset @ 0x90) */
110 0x90, 0x00, 0x00, 0x00,
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200111 /* flags */
112 0x00, 0x00, 0x00, 0x00,
113 /* ehsize (elf header size = 0x34) */
114 0x34, 0x00,
115 /* phentsize (program header size = 0x20) */
116 0x20, 0x00,
117 /* phnum (program header number : 1) */
118 0x01, 0x00,
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100119 /* shentsize (section header size : 40 bytes) */
120 0x28, 0x00,
121 /* shnum (section header number: 3) */
122 0x02, 0x00,
123 /* shstrndx (section header name section index: 1) */
124 0x01, 0x00,
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200125 /* padding */
126 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x00,
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100129
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200130 /* @0x40 - PROGRAM HEADER TABLE - */
131 /* type : PT_LOAD */
132 0x01, 0x00, 0x00, 0x00,
133 /* offset */
134 0x00, 0x00, 0x00, 0x00,
135 /* vaddr */
136 0x00, 0x00, 0x00, 0x00,
137 /* paddr : physical address */
138 0x00, 0x00, 0x00, 0x00,
139 /* filesz : 0x20 bytes (program header size) */
140 0x20, 0x00, 0x00, 0x00,
141 /* memsz = filesz */
142 0x20, 0x00, 0x00, 0x00,
Yuepeng Xing71cc0212022-12-02 14:23:07 +0800143 /* flags : readable and executable */
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200144 0x05, 0x00, 0x00, 0x00,
145 /* padding */
146 0x00, 0x00, 0x00, 0x00,
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100147
148 /* @0x60 - RESOURCE TABLE SECTION - */
149 /* version */
150 0x01, 0x00, 0x00, 0x00,
151 /* num (0, no entries) */
152 0x00, 0x00, 0x00, 0x00,
153 /* Reserved */
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155
156 /* @0x70 - SECTION'S NAMES SECTION - */
157 /* section 0 name (".shrtrtab") */
158 0x2e, 0x73, 0x68, 0x73, 0x74, 0x72, 0x74, 0x61, 0x62, 0x00,
159 /* section 1 name (".resource_table") */
160 0x2e, 0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x5f,
161 0x74, 0x61, 0x62, 0x6c, 0x65, 0x00,
162 /* padding */
163 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164
165 /* @0x90 - SECTION HEADER TABLE - */
166 /* Section 0 : resource table header */
167 /* sh_name - index into section header string table section */
168 0x0a, 0x00, 0x00, 0x00,
169 /* sh_type and sh_flags */
170 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
171 /* sh_addr = where the resource table has to be copied to */
172 0x00, 0x00, 0x00, 0x00,
173 /* sh_offset = 0x60 */
174 0x60, 0x00, 0x00, 0x00,
175 /* sh_size = 16 bytes */
176 0x10, 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 /* Section 1 : section's names section header */
181 /* sh_name - index into section header string table section */
182 0x00, 0x00, 0x00, 0x00,
183 /* sh_type and sh_flags */
184 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185 /* sh_addr */
186 0x00, 0x00, 0x00, 0x00,
187 /* sh_offset = 0x70 */
188 0x70, 0x00, 0x00, 0x00,
189 /* sh_size = 27 bytes */
190 0x1b, 0x00, 0x00, 0x00,
191 /* sh_link, sh_info, sh_addralign, sh_entsize */
192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200194 };
195 unsigned int size = ARRAY_SIZE(valid_elf32);
196 struct udevice *dev;
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100197 phys_addr_t loaded_firmware_paddr, loaded_rsc_table_paddr;
198 void *loaded_firmware, *loaded_rsc_table;
199 u32 loaded_firmware_size, rsc_table_size;
200 ulong rsc_addr, rsc_size;
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200201 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)valid_elf32;
202 Elf32_Phdr *phdr = (Elf32_Phdr *)(valid_elf32 + ehdr->e_phoff);
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100203 Elf32_Shdr *shdr = (Elf32_Shdr *)(valid_elf32 + ehdr->e_shoff);
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200204
205 ut_assertok(uclass_get_device(UCLASS_REMOTEPROC, 0, &dev));
206
207 /*
208 * In its Program Header Table, let the firmware specifies to be loaded
209 * at SDRAM_BASE *device* address (p_paddr field).
210 * Its size is defined by the p_filesz field.
211 */
Tom Rinibb4dd962022-11-16 13:10:37 -0500212 phdr->p_paddr = CFG_SYS_SDRAM_BASE;
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200213 loaded_firmware_size = phdr->p_filesz;
214
215 /*
216 * This *device* address is converted to a *physical* address by the
217 * device_to_virt() operation of sandbox_test_rproc which returns
218 * DeviceAddress + DEVICE_TO_PHYSICAL_OFFSET.
219 * This is where we expect to get the firmware loaded.
220 */
221 loaded_firmware_paddr = phdr->p_paddr + DEVICE_TO_PHYSICAL_OFFSET;
222 loaded_firmware = map_physmem(loaded_firmware_paddr,
223 loaded_firmware_size, MAP_NOCACHE);
224 ut_assertnonnull(loaded_firmware);
225 memset(loaded_firmware, 0, loaded_firmware_size);
226
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200227 /* Load firmware in loaded_firmware, and verify it */
Lokesh Vutlab506cba2019-09-04 16:01:28 +0530228 ut_assertok(rproc_elf32_load_image(dev, (ulong)valid_elf32, size));
Simon Glassa3186e62020-05-10 12:52:45 -0600229 ut_asserteq_mem(loaded_firmware, valid_elf32, loaded_firmware_size);
Lokesh Vutla3275e5d2019-09-04 16:01:31 +0530230 ut_asserteq(rproc_elf_get_boot_addr(dev, (unsigned long)valid_elf32),
231 0x08000000);
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200232 unmap_physmem(loaded_firmware, MAP_NOCACHE);
233
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100234 /* Resource table */
Tom Rinibb4dd962022-11-16 13:10:37 -0500235 shdr->sh_addr = CFG_SYS_SDRAM_BASE;
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100236 rsc_table_size = shdr->sh_size;
237
238 loaded_rsc_table_paddr = shdr->sh_addr + DEVICE_TO_PHYSICAL_OFFSET;
239 loaded_rsc_table = map_physmem(loaded_rsc_table_paddr,
240 rsc_table_size, MAP_NOCACHE);
241 ut_assertnonnull(loaded_rsc_table);
242 memset(loaded_rsc_table, 0, rsc_table_size);
243
244 /* Load and verify */
245 ut_assertok(rproc_elf32_load_rsc_table(dev, (ulong)valid_elf32, size,
246 &rsc_addr, &rsc_size));
Tom Rinibb4dd962022-11-16 13:10:37 -0500247 ut_asserteq(rsc_addr, CFG_SYS_SDRAM_BASE);
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100248 ut_asserteq(rsc_size, rsc_table_size);
Simon Glassa3186e62020-05-10 12:52:45 -0600249 ut_asserteq_mem(loaded_firmware, valid_elf32 + shdr->sh_offset,
250 shdr->sh_size);
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100251 unmap_physmem(loaded_firmware, MAP_NOCACHE);
252
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200253 /* Invalid ELF Magic */
254 valid_elf32[0] = 0;
255 ut_asserteq(-EPROTONOSUPPORT,
256 rproc_elf32_sanity_check((ulong)valid_elf32, size));
257
258 return 0;
259}
Simon Glass974dccd2020-07-28 19:41:12 -0600260DM_TEST(dm_test_remoteproc_elf, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);