1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * (C) Copyright 2015 4 * Texas Instruments Incorporated - https://www.ti.com/ 5 */ 6 7#include <config.h> 8#include <dm.h> 9#include <elf.h> 10#include <errno.h> 11#include <remoteproc.h> 12#include <asm/io.h> 13#include <dm/test.h> 14#include <test/test.h> 15#include <test/ut.h> 16 17/** 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} 72DM_TEST(dm_test_remoteproc_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); 73 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, 109 /* shoff (section header offset @ 0x90) */ 110 0x90, 0x00, 0x00, 0x00, 111 /* 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, 119 /* 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, 125 /* padding */ 126 0x00, 0x00, 0x00, 0x00, 127 0x00, 0x00, 0x00, 0x00, 128 0x00, 0x00, 0x00, 0x00, 129 130 /* @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, 143 /* flags : readable and executable */ 144 0x05, 0x00, 0x00, 0x00, 145 /* padding */ 146 0x00, 0x00, 0x00, 0x00, 147 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, 194 }; 195 unsigned int size = ARRAY_SIZE(valid_elf32); 196 struct udevice *dev; 197 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; 201 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)valid_elf32; 202 Elf32_Phdr *phdr = (Elf32_Phdr *)(valid_elf32 + ehdr->e_phoff); 203 Elf32_Shdr *shdr = (Elf32_Shdr *)(valid_elf32 + ehdr->e_shoff); 204 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 */ 212 phdr->p_paddr = CFG_SYS_SDRAM_BASE; 213 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 227 /* Load firmware in loaded_firmware, and verify it */ 228 ut_assertok(rproc_elf32_load_image(dev, (ulong)valid_elf32, size)); 229 ut_asserteq_mem(loaded_firmware, valid_elf32, loaded_firmware_size); 230 ut_asserteq(rproc_elf_get_boot_addr(dev, (unsigned long)valid_elf32), 231 0x08000000); 232 unmap_physmem(loaded_firmware, MAP_NOCACHE); 233 234 /* Resource table */ 235 shdr->sh_addr = CFG_SYS_SDRAM_BASE; 236 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)); 247 ut_asserteq(rsc_addr, CFG_SYS_SDRAM_BASE); 248 ut_asserteq(rsc_size, rsc_table_size); 249 ut_asserteq_mem(loaded_firmware, valid_elf32 + shdr->sh_offset, 250 shdr->sh_size); 251 unmap_physmem(loaded_firmware, MAP_NOCACHE); 252 253 /* 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} 260DM_TEST(dm_test_remoteproc_elf, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); 261