blob: 1d9a9b32d550fc07040c0f61d6bac7cbe916393d [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>
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 */
20static 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}
69DM_TEST(dm_test_remoteproc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +020070
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 */
78static 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 : none) */
107 0x00, 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 heade size : none) */
117 0x00, 0x00,
118 /* shnum (section header number: none) */
119 0x00, 0x00,
120 /* shstrndx (section header name section index: none) */
121 0x00, 0x00,
122 /* padding */
123 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00,
126 /* @0x40 - PROGRAM HEADER TABLE - */
127 /* type : PT_LOAD */
128 0x01, 0x00, 0x00, 0x00,
129 /* offset */
130 0x00, 0x00, 0x00, 0x00,
131 /* vaddr */
132 0x00, 0x00, 0x00, 0x00,
133 /* paddr : physical address */
134 0x00, 0x00, 0x00, 0x00,
135 /* filesz : 0x20 bytes (program header size) */
136 0x20, 0x00, 0x00, 0x00,
137 /* memsz = filesz */
138 0x20, 0x00, 0x00, 0x00,
139 /* flags : readable and exectuable */
140 0x05, 0x00, 0x00, 0x00,
141 /* padding */
142 0x00, 0x00, 0x00, 0x00,
143 };
144 unsigned int size = ARRAY_SIZE(valid_elf32);
145 struct udevice *dev;
146 phys_addr_t loaded_firmware_paddr;
147 void *loaded_firmware;
148 u32 loaded_firmware_size;
149 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)valid_elf32;
150 Elf32_Phdr *phdr = (Elf32_Phdr *)(valid_elf32 + ehdr->e_phoff);
151
152 ut_assertok(uclass_get_device(UCLASS_REMOTEPROC, 0, &dev));
153
154 /*
155 * In its Program Header Table, let the firmware specifies to be loaded
156 * at SDRAM_BASE *device* address (p_paddr field).
157 * Its size is defined by the p_filesz field.
158 */
159 phdr->p_paddr = CONFIG_SYS_SDRAM_BASE;
160 loaded_firmware_size = phdr->p_filesz;
161
162 /*
163 * This *device* address is converted to a *physical* address by the
164 * device_to_virt() operation of sandbox_test_rproc which returns
165 * DeviceAddress + DEVICE_TO_PHYSICAL_OFFSET.
166 * This is where we expect to get the firmware loaded.
167 */
168 loaded_firmware_paddr = phdr->p_paddr + DEVICE_TO_PHYSICAL_OFFSET;
169 loaded_firmware = map_physmem(loaded_firmware_paddr,
170 loaded_firmware_size, MAP_NOCACHE);
171 ut_assertnonnull(loaded_firmware);
172 memset(loaded_firmware, 0, loaded_firmware_size);
173
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200174 /* Load firmware in loaded_firmware, and verify it */
Lokesh Vutla14d963d2019-09-04 16:01:28 +0530175 ut_assertok(rproc_elf32_load_image(dev, (ulong)valid_elf32, size));
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200176 ut_assertok(memcmp(loaded_firmware, valid_elf32, loaded_firmware_size));
Lokesh Vutla81e39fb2019-09-04 16:01:31 +0530177 ut_asserteq(rproc_elf_get_boot_addr(dev, (unsigned long)valid_elf32),
178 0x08000000);
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200179 unmap_physmem(loaded_firmware, MAP_NOCACHE);
180
181 /* Invalid ELF Magic */
182 valid_elf32[0] = 0;
183 ut_asserteq(-EPROTONOSUPPORT,
184 rproc_elf32_sanity_check((ulong)valid_elf32, size));
185
186 return 0;
187}
188DM_TEST(dm_test_remoteproc_elf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);