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