test: dm: Test for default led naming
[platform/kernel/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/test.h>
14 #include <test/ut.h>
15
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 }
71 DM_TEST(dm_test_remoteproc_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
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,
108                 /* shoff (section header offset @ 0x90) */
109                 0x90, 0x00, 0x00, 0x00,
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,
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,
124                 /* padding */
125                 0x00, 0x00, 0x00, 0x00,
126                 0x00, 0x00, 0x00, 0x00,
127                 0x00, 0x00, 0x00, 0x00,
128
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,
142                 /* flags : readable and exectuable */
143                 0x05, 0x00, 0x00, 0x00,
144                 /* padding */
145                 0x00, 0x00, 0x00, 0x00,
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,
193         };
194         unsigned int size = ARRAY_SIZE(valid_elf32);
195         struct udevice *dev;
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);
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          */
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
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),
230                     0x08000000);
231         unmap_physmem(loaded_firmware, MAP_NOCACHE);
232
233         /* 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);
248         ut_asserteq_mem(loaded_firmware, valid_elf32 + shdr->sh_offset,
249                         shdr->sh_size);
250         unmap_physmem(loaded_firmware, MAP_NOCACHE);
251
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 }
259 DM_TEST(dm_test_remoteproc_elf, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);