blob: 99f02b7951b892b2a9a79c5c2e55573f495ac40a [file] [log] [blame]
// SPDX-License-Identifier: GPL-2.0+
/*
* UPL handoff testing
*
* Copyright 2024 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <abuf.h>
#include <mapmem.h>
#include <upl.h>
#include <dm/ofnode.h>
#include <test/suites.h>
#include <test/test.h>
#include <test/ut.h>
#include "bootstd_common.h"
/* Declare a new upl test */
#define UPL_TEST(_name, _flags) UNIT_TEST(_name, _flags, upl_test)
static int add_region(struct unit_test_state *uts, struct alist *lst,
ulong base, ulong size)
{
struct memregion region;
region.base = base;
region.size = size;
ut_assertnonnull(alist_add(lst, region));
return 0;
}
int upl_get_test_data(struct unit_test_state *uts, struct upl *upl)
{
struct upl_memmap memmap;
struct upl_memres memres;
struct upl_image img;
struct upl_mem mem;
upl_init(upl);
upl->addr_cells = 1;
upl->size_cells = 1;
upl->smbios = 0x123;
upl->acpi = 0x456;
upl->bootmode = BIT(UPLBM_DEFAULT) | BIT(UPLBM_S3);
upl->fit = 0x789;
upl->conf_offset = 0x234;
upl->addr_width = 46;
upl->acpi_nvs_size = 0x100;
/* image[0] */
img.load = 0x1;
img.size = 0x2;
img.offset = 0x3;
img.description = "U-Boot";
ut_assertnonnull(alist_add(&upl->image, img));
/* image[1] */
img.load = 0x4;
img.size = 0x5;
img.offset = 0x6;
img.description = "ATF";
ut_assertnonnull(alist_add(&upl->image, img));
/* mem[0] : 3 regions */
memset(&mem, '\0', sizeof(mem));
alist_init_struct(&mem.region, struct memregion);
ut_assertok(add_region(uts, &mem.region, 0x10, 0x20));
ut_assertok(add_region(uts, &mem.region, 0x30, 0x40));
ut_assertok(add_region(uts, &mem.region, 0x40, 0x50));
ut_assertnonnull(alist_add(&upl->mem, mem));
/* mem[0] : 1 region */
alist_init_struct(&mem.region, struct memregion);
ut_assertok(add_region(uts, &mem.region, 0x70, 0x80));
mem.hotpluggable = true;
ut_assertnonnull(alist_add(&upl->mem, mem));
mem.hotpluggable = false;
/* memmap[0] : 5 regions */
alist_init_struct(&memmap.region, struct memregion);
memmap.name = "acpi";
memmap.usage = BIT(UPLUS_ACPI_RECLAIM);
ut_assertok(add_region(uts, &memmap.region, 0x11, 0x12));
ut_assertok(add_region(uts, &memmap.region, 0x13, 0x14));
ut_assertok(add_region(uts, &memmap.region, 0x15, 0x16));
ut_assertok(add_region(uts, &memmap.region, 0x17, 0x18));
ut_assertok(add_region(uts, &memmap.region, 0x19, 0x1a));
ut_assertnonnull(alist_add(&upl->memmap, memmap));
/* memmap[1] : 1 region */
memmap.name = "u-boot";
memmap.usage = BIT(UPLUS_BOOT_DATA);
alist_init_struct(&memmap.region, struct memregion);
ut_assertok(add_region(uts, &memmap.region, 0x21, 0x22));
ut_assertnonnull(alist_add(&upl->memmap, memmap));
/* memmap[2] : 1 region */
alist_init_struct(&memmap.region, struct memregion);
memmap.name = "efi";
memmap.usage = BIT(UPLUS_RUNTIME_CODE);
ut_assertok(add_region(uts, &memmap.region, 0x23, 0x24));
ut_assertnonnull(alist_add(&upl->memmap, memmap));
/* memmap[3]: 2 regions */
alist_init_struct(&memmap.region, struct memregion);
memmap.name = "empty";
memmap.usage = 0;
ut_assertok(add_region(uts, &memmap.region, 0x25, 0x26));
ut_assertok(add_region(uts, &memmap.region, 0x27, 0x28));
ut_assertnonnull(alist_add(&upl->memmap, memmap));
/* memmap[4]: 1 region */
alist_init_struct(&memmap.region, struct memregion);
memmap.name = "acpi-things";
memmap.usage = BIT(UPLUS_RUNTIME_CODE) | BIT(UPLUS_ACPI_NVS);
ut_assertok(add_region(uts, &memmap.region, 0x29, 0x2a));
ut_assertnonnull(alist_add(&upl->memmap, memmap));
/* memres[0]: 1 region */
alist_init_struct(&memres.region, struct memregion);
memset(&memres, '\0', sizeof(memres));
memres.name = "mmio";
ut_assertok(add_region(uts, &memres.region, 0x2b, 0x2c));
ut_assertnonnull(alist_add(&upl->memres, memres));
/* memres[1]: 2 regions */
alist_init_struct(&memres.region, struct memregion);
memres.name = "memory";
ut_assertok(add_region(uts, &memres.region, 0x2d, 0x2e));
ut_assertok(add_region(uts, &memres.region, 0x2f, 0x30));
memres.no_map = true;
ut_assertnonnull(alist_add(&upl->memres, memres));
upl->serial.compatible = "ns16550a";
upl->serial.clock_frequency = 1843200;
upl->serial.current_speed = 115200;
alist_init_struct(&upl->serial.reg, struct memregion);
ut_assertok(add_region(uts, &upl->serial.reg, 0xf1de0000, 0x100));
upl->serial.reg_io_shift = 2;
upl->serial.reg_offset = 0x40;
upl->serial.reg_io_width = 1;
upl->serial.virtual_reg = 0x20000000;
upl->serial.access_type = UPLSAT_MMIO;
alist_init_struct(&upl->graphics.reg, struct memregion);
ut_assertok(add_region(uts, &upl->graphics.reg, 0xd0000000, 0x10000000));
upl->graphics.width = 1280;
upl->graphics.height = 1280;
upl->graphics.stride = upl->graphics.width * 4;
upl->graphics.format = UPLGF_ARGB32;
return 0;
}
static int compare_upl_image(struct unit_test_state *uts,
const struct upl_image *base,
const struct upl_image *cmp)
{
ut_asserteq(base->load, cmp->load);
ut_asserteq(base->size, cmp->size);
ut_asserteq(base->offset, cmp->offset);
ut_asserteq_str(base->description, cmp->description);
return 0;
}
static int compare_upl_memregion(struct unit_test_state *uts,
const struct memregion *base,
const struct memregion *cmp)
{
ut_asserteq(base->base, cmp->base);
ut_asserteq(base->size, cmp->size);
return 0;
}
static int compare_upl_mem(struct unit_test_state *uts,
const struct upl_mem *base,
const struct upl_mem *cmp)
{
int i;
ut_asserteq(base->region.count, cmp->region.count);
ut_asserteq(base->hotpluggable, cmp->hotpluggable);
for (i = 0; i < base->region.count; i++) {
ut_assertok(compare_upl_memregion(uts,
alist_get(&base->region, i, struct memregion),
alist_get(&cmp->region, i, struct memregion)));
}
return 0;
}
static int check_device_name(struct unit_test_state *uts, const char *base,
const char *cmp)
{
const char *p;
p = strchr(cmp, '@');
if (p) {
ut_assertnonnull(p);
ut_asserteq_strn(base, cmp);
ut_asserteq(p - cmp, strlen(base));
} else {
ut_asserteq_str(base, cmp);
}
return 0;
}
static int compare_upl_memmap(struct unit_test_state *uts,
const struct upl_memmap *base,
const struct upl_memmap *cmp)
{
int i;
ut_assertok(check_device_name(uts, base->name, cmp->name));
ut_asserteq(base->region.count, cmp->region.count);
ut_asserteq(base->usage, cmp->usage);
for (i = 0; i < base->region.count; i++)
ut_assertok(compare_upl_memregion(uts,
alist_get(&base->region, i, struct memregion),
alist_get(&cmp->region, i, struct memregion)));
return 0;
}
static int compare_upl_memres(struct unit_test_state *uts,
const struct upl_memres *base,
const struct upl_memres *cmp)
{
int i;
ut_assertok(check_device_name(uts, base->name, cmp->name));
ut_asserteq(base->region.count, cmp->region.count);
ut_asserteq(base->no_map, cmp->no_map);
for (i = 0; i < base->region.count; i++)
ut_assertok(compare_upl_memregion(uts,
alist_get(&base->region, i, struct memregion),
alist_get(&cmp->region, i, struct memregion)));
return 0;
}
static int compare_upl_serial(struct unit_test_state *uts,
struct upl_serial *base, struct upl_serial *cmp)
{
int i;
ut_asserteq_str(base->compatible, cmp->compatible);
ut_asserteq(base->clock_frequency, cmp->clock_frequency);
ut_asserteq(base->current_speed, cmp->current_speed);
for (i = 0; i < base->reg.count; i++)
ut_assertok(compare_upl_memregion(uts,
alist_get(&base->reg, i, struct memregion),
alist_get(&cmp->reg, i, struct memregion)));
ut_asserteq(base->reg_io_shift, cmp->reg_io_shift);
ut_asserteq(base->reg_offset, cmp->reg_offset);
ut_asserteq(base->reg_io_width, cmp->reg_io_width);
ut_asserteq(base->virtual_reg, cmp->virtual_reg);
ut_asserteq(base->access_type, cmp->access_type);
return 0;
}
static int compare_upl_graphics(struct unit_test_state *uts,
struct upl_graphics *base,
struct upl_graphics *cmp)
{
int i;
for (i = 0; i < base->reg.count; i++)
ut_assertok(compare_upl_memregion(uts,
alist_get(&base->reg, i, struct memregion),
alist_get(&cmp->reg, i, struct memregion)));
ut_asserteq(base->width, cmp->width);
ut_asserteq(base->height, cmp->height);
ut_asserteq(base->stride, cmp->stride);
ut_asserteq(base->format, cmp->format);
return 0;
}
static int compare_upl(struct unit_test_state *uts, struct upl *base,
struct upl *cmp)
{
int i;
ut_asserteq(base->addr_cells, cmp->addr_cells);
ut_asserteq(base->size_cells, cmp->size_cells);
ut_asserteq(base->smbios, cmp->smbios);
ut_asserteq(base->acpi, cmp->acpi);
ut_asserteq(base->bootmode, cmp->bootmode);
ut_asserteq(base->fit, cmp->fit);
ut_asserteq(base->conf_offset, cmp->conf_offset);
ut_asserteq(base->addr_width, cmp->addr_width);
ut_asserteq(base->acpi_nvs_size, cmp->acpi_nvs_size);
ut_asserteq(base->image.count, cmp->image.count);
for (i = 0; i < base->image.count; i++)
ut_assertok(compare_upl_image(uts,
alist_get(&base->image, i, struct upl_image),
alist_get(&cmp->image, i, struct upl_image)));
ut_asserteq(base->mem.count, cmp->mem.count);
for (i = 0; i < base->mem.count; i++)
ut_assertok(compare_upl_mem(uts,
alist_get(&base->mem, i, struct upl_mem),
alist_get(&cmp->mem, i, struct upl_mem)));
ut_asserteq(base->memmap.count, cmp->memmap.count);
for (i = 0; i < base->memmap.count; i++)
ut_assertok(compare_upl_memmap(uts,
alist_get(&base->memmap, i, struct upl_memmap),
alist_get(&cmp->memmap, i, struct upl_memmap)));
ut_asserteq(base->memres.count, cmp->memres.count);
for (i = 0; i < base->memres.count; i++)
ut_assertok(compare_upl_memres(uts,
alist_get(&base->memres, i, struct upl_memres),
alist_get(&cmp->memres, i, struct upl_memres)));
ut_assertok(compare_upl_serial(uts, &base->serial, &cmp->serial));
ut_assertok(compare_upl_graphics(uts, &base->graphics, &cmp->graphics));
return 0;
}
/* Basic test of writing and reading UPL handoff */
static int upl_test_base(struct unit_test_state *uts)
{
oftree tree, check_tree;
struct upl upl, check;
struct abuf buf;
if (!CONFIG_IS_ENABLED(OFNODE_MULTI_TREE))
return -EAGAIN; /* skip test */
ut_assertok(upl_get_test_data(uts, &upl));
ut_assertok(upl_create_handoff_tree(&upl, &tree));
ut_assertok(oftree_to_fdt(tree, &buf));
/*
* strings in check_tree and therefore check are only valid so long as
* buf stays around. As soon as we call abuf_uninit they go away
*/
check_tree = oftree_from_fdt(abuf_data(&buf));
ut_assert(ofnode_valid(oftree_path(check_tree, "/")));
ut_assertok(upl_read_handoff(&check, check_tree));
ut_assertok(compare_upl(uts, &upl, &check));
abuf_uninit(&buf);
return 0;
}
UPL_TEST(upl_test_base, 0);
/* Test 'upl info' command */
static int upl_test_info(struct unit_test_state *uts)
{
gd_set_upl(NULL);
ut_assertok(run_command("upl info", 0));
ut_assert_nextline("UPL state: inactive");
ut_assert_console_end();
gd_set_upl((struct upl *)uts); /* set it to any non-zero value */
ut_assertok(run_command("upl info", 0));
ut_assert_nextline("UPL state: active");
ut_assert_console_end();
gd_set_upl(NULL);
return 0;
}
UPL_TEST(upl_test_info, UTF_CONSOLE);
/* Test 'upl read' and 'upl_write' commands */
static int upl_test_read_write(struct unit_test_state *uts)
{
ulong addr;
if (!CONFIG_IS_ENABLED(OFNODE_MULTI_TREE))
return -EAGAIN; /* skip test */
ut_assertok(run_command("upl write", 0));
addr = env_get_hex("upladdr", 0);
ut_assert_nextline("UPL handoff written to %lx size %lx", addr,
env_get_hex("uplsize", 0));
ut_assert_console_end();
ut_assertok(run_command("upl read ${upladdr}", 0));
ut_assert_nextline("Reading UPL at %lx", addr);
ut_assert_console_end();
return 0;
}
UPL_TEST(upl_test_read_write, UTF_CONSOLE);
/* Test UPL passthrough */
static int upl_test_info_norun(struct unit_test_state *uts)
{
const struct upl_image *img;
struct upl *upl = gd_upl();
const void *fit;
ut_assertok(run_command("upl info -v", 0));
ut_assert_nextline("UPL state: active");
ut_assert_nextline("fit %lx", upl->fit);
ut_assert_nextline("conf_offset %x", upl->conf_offset);
ut_assert_nextlinen("image 0");
ut_assert_nextlinen("image 1");
ut_assert_console_end();
/* check the offsets */
fit = map_sysmem(upl->fit, 0);
ut_asserteq_str("conf-1", fdt_get_name(fit, upl->conf_offset, NULL));
ut_asserteq(2, upl->image.count);
img = alist_get(&upl->image, 1, struct upl_image);
ut_asserteq_str("firmware-1", fdt_get_name(fit, img->offset, NULL));
ut_asserteq(CONFIG_TEXT_BASE, img->load);
return 0;
}
UPL_TEST(upl_test_info_norun, UTF_CONSOLE | UTF_MANUAL);
int do_ut_upl(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{
struct unit_test *tests = UNIT_TEST_SUITE_START(upl_test);
const int n_ents = UNIT_TEST_SUITE_COUNT(upl_test);
return cmd_ut_category("cmd_upl", "cmd_upl_", tests, n_ents, argc,
argv);
}