Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
// SPDX-License-Identifier: GPL-2.0+
/*
 * Tests for ACPI table generation
 *
 * Copyright 2019 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#include <bloblist.h>
#include <bootstage.h>
#include <console.h>
#include <dm.h>
#include <efi_log.h>
#include <malloc.h>
#include <mapmem.h>
#include <tables_csum.h>
#include <version_string.h>
#include <acpi/acpigen.h>
#include <acpi/acpi_device.h>
#include <acpi/acpi_table.h>
#include <asm/global_data.h>
#include <dm/acpi.h>
#include <dm/test.h>
#include <test/ut.h>
#include "acpi.h"

#define BUF_SIZE		4096

#define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \
		      (((version_num / 100) % 10) << 24) | \
		      (((version_num / 10) % 10) << 20) | \
		      ((version_num % 10) << 16) | \
		      (((version_num_patch / 10) % 10) << 12) | \
		      ((version_num_patch % 10) << 8) | \
		      0x01)

/**
 * struct testacpi_plat - Platform data for the test ACPI device
 *
 * @no_name: true to emit an empty ACPI name from testacpi_get_name()
 * @return_error: true to return an error instead of a name
 */
struct testacpi_plat {
	bool return_error;
	bool no_name;
};

/**
 * setup_ctx_and_base_tables() - Set up context along with RSDP, RSDT and XSDT
 *
 * Set up the context with the given start position. Some basic tables are
 * always needed, so set them up as well.
 *
 * @ctx: Context to set up
 */
static int setup_ctx_and_base_tables(struct unit_test_state *uts,
				     struct acpi_ctx *ctx, ulong start)
{
	struct acpi_writer *entry = ACPI_WRITER_GET(0base);

	acpi_setup_ctx(ctx, start);

	ctx->tab_start = ctx->current;
	ut_assertok(acpi_write_one(ctx, entry));

	return 0;
}

static int testacpi_write_tables(const struct udevice *dev,
				 struct acpi_ctx *ctx)
{
	struct acpi_dmar *dmar;
	int ret;

	dmar = (struct acpi_dmar *)ctx->current;
	acpi_create_dmar(dmar, DMAR_INTR_REMAP);
	ctx->current += sizeof(struct acpi_dmar);
	ret = acpi_add_table(ctx, dmar);
	if (ret)
		return log_msg_ret("add", ret);

	return 0;
}

static int testacpi_get_name(const struct udevice *dev, char *out_name)
{
	struct testacpi_plat *plat = dev_get_plat(dev);

	if (plat->return_error)
		return -EINVAL;
	if (plat->no_name) {
		*out_name = '\0';
		return 0;
	}
	if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI)
		return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME);
	else
		return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
}

static int testacpi_fill_madt(const struct udevice *dev, struct acpi_ctx *ctx)
{
	u64 *data = ctx->current;

	/* Only fill madt once */
	if (device_get_uclass_id(dev->parent) != UCLASS_TEST_ACPI)
		return 0;

	*data = 0xdeadbeef;

	acpi_inc(ctx, sizeof(u64));

	return 0;
}

static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
{
	const char *data;

	data = dev_read_string(dev, "acpi-ssdt-test-data");
	if (data) {
		while (*data)
			acpigen_emit_byte(ctx, *data++);
	}

	return 0;
}

static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
{
	const char *data;

	data = dev_read_string(dev, "acpi-dsdt-test-data");
	if (data) {
		while (*data)
			acpigen_emit_byte(ctx, *data++);
	}

	return 0;
}

struct acpi_ops testacpi_ops = {
	.get_name	= testacpi_get_name,
	.write_tables	= testacpi_write_tables,
	.fill_madt	= testacpi_fill_madt,
	.fill_ssdt	= testacpi_fill_ssdt,
	.inject_dsdt	= testacpi_inject_dsdt,
};

static const struct udevice_id testacpi_ids[] = {
	{ .compatible = "denx,u-boot-acpi-test" },
	{ }
};

U_BOOT_DRIVER(testacpi_drv) = {
	.name	= "testacpi_drv",
	.of_match	= testacpi_ids,
	.id	= UCLASS_TEST_ACPI,
	.bind	= dm_scan_fdt_dev,
	.plat_auto	= sizeof(struct testacpi_plat),
	ACPI_OPS_PTR(&testacpi_ops)
};

UCLASS_DRIVER(testacpi) = {
	.name		= "testacpi",
	.id		= UCLASS_TEST_ACPI,
};

/* Test ACPI get_name() */
static int dm_test_acpi_get_name(struct unit_test_state *uts)
{
	char name[ACPI_NAME_MAX];
	struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
	struct udevice *pci, *root;

	/* Test getting the name from the driver */
	ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
	ut_assertok(acpi_get_name(dev, name));
	ut_asserteq_str(ACPI_TEST_DEV_NAME, name);

	/* Test getting the name from the device tree */
	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
					      &dev2));
	ut_assertok(acpi_get_name(dev2, name));
	ut_asserteq_str("GHIJ", name);

	/* Test getting the name from acpi_device_get_name() */
	ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
	ut_assertok(acpi_get_name(i2c, name));
	ut_asserteq_str("I2C0", name);

	ut_assertok(uclass_first_device_err(UCLASS_SPI, &spi));
	ut_assertok(acpi_get_name(spi, name));
	ut_asserteq_str("SPI0", name);

	/* ACPI doesn't know about the timer */
	ut_assertok(uclass_first_device_err(UCLASS_TIMER, &timer));
	ut_asserteq(-ENOENT, acpi_get_name(timer, name));

	/* May as well test the rest of the cases */
	ut_assertok(uclass_first_device_err(UCLASS_SOUND, &sound));
	ut_assertok(acpi_get_name(sound, name));
	ut_asserteq_str("HDAS", name);

	ut_assertok(uclass_first_device_err(UCLASS_PCI, &pci));
	ut_assertok(acpi_get_name(pci, name));
	ut_asserteq_str("PCI0", name);

	ut_assertok(uclass_first_device_err(UCLASS_ROOT, &root));
	ut_assertok(acpi_get_name(root, name));
	ut_asserteq_str("\\_SB", name);

	/* Note that we don't have tests for acpi_name_from_id() */

	return 0;
}
DM_TEST(dm_test_acpi_get_name, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test acpi_get_table_revision() */
static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
{
	ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
	ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
	ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
	ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));

	return 0;
}
DM_TEST(dm_test_acpi_get_table_revision, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test acpi_create_dmar() */
static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
{
	struct acpi_dmar dmar;
	struct udevice *cpu;

	ut_assertok(uclass_first_device_err(UCLASS_CPU, &cpu));
	ut_assertnonnull(cpu);
	ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
	ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
		    dmar.host_address_width);

	return 0;
}
DM_TEST(dm_test_acpi_create_dmar, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test acpi_fill_header() */
static int dm_test_acpi_fill_header(struct unit_test_state *uts)
{
	struct acpi_table_header hdr;

	/* Make sure these 5 fields are not changed */
	hdr.length = 0x11;
	hdr.revision = 0x22;
	hdr.checksum = 0x33;
	acpi_fill_header(&hdr, "ABCD");

	ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
	ut_asserteq(0x11, hdr.length);
	ut_asserteq(0x22, hdr.revision);
	ut_asserteq(0x33, hdr.checksum);
	ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
	ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
			sizeof(hdr.oem_table_id));
	ut_asserteq(OEM_REVISION, hdr.oem_revision);
	ut_asserteq_mem(ASLC_ID, hdr.creator_id, sizeof(hdr.creator_id));
	ut_asserteq(ASL_REVISION, hdr.creator_revision);

	return 0;
}
DM_TEST(dm_test_acpi_fill_header, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test ACPI write_tables() */
static int dm_test_acpi_write_tables(struct unit_test_state *uts)
{
	struct acpi_dmar *dmar;
	struct acpi_ctx ctx;
	ulong addr;
	void *buf;
	int i;

	buf = malloc(BUF_SIZE);
	ut_assertnonnull(buf);
	addr = map_to_sysmem(buf);

	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
	dmar = ctx.current;
	ut_assertok(acpi_write_dev_tables(&ctx));

	/*
	 * We should have three dmar tables, one for each
	 * "denx,u-boot-acpi-test" device
	 */
	ut_asserteq_ptr(dmar + 3, ctx.current);
	ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
		    dmar->host_address_width);

	ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
		    dmar[1].host_address_width);

	ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
		    dmar[2].host_address_width);

	/* Check that the pointers were added correctly */
	for (i = 0; i < 3; i++) {
		ut_asserteq(nomap_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
		ut_asserteq(nomap_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
	}
	ut_asserteq(0, ctx.rsdt->entry[3]);
	ut_asserteq(0, ctx.xsdt->entry[3]);
	unmap_sysmem(buf);
	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_write_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test basic ACPI functions */
static int dm_test_acpi_basic(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;

	/* Check align works */
	ctx.current = (void *)5;
	acpi_align(&ctx);
	ut_asserteq_ptr((void *)16, ctx.current);

	/* Check that align does nothing if already aligned */
	acpi_align(&ctx);
	ut_asserteq_ptr((void *)16, ctx.current);
	acpi_align64(&ctx);
	ut_asserteq_ptr((void *)64, ctx.current);
	acpi_align64(&ctx);
	ut_asserteq_ptr((void *)64, ctx.current);

	/* Check incrementing */
	acpi_inc(&ctx, 3);
	ut_asserteq_ptr((void *)67, ctx.current);
	acpi_inc_align(&ctx, 3);
	ut_asserteq_ptr((void *)80, ctx.current);

	return 0;
}
DM_TEST(dm_test_acpi_basic, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test setup_ctx_and_base_tables */
static int dm_test_acpi_ctx_and_base_tables(struct unit_test_state *uts)
{
	struct acpi_rsdp *rsdp;
	struct acpi_rsdt *rsdt;
	struct acpi_xsdt *xsdt;
	struct acpi_ctx ctx;
	void *buf, *end;
	ulong addr;

	/*
	 * Use an unaligned address deliberately, by allocating an aligned
	 * address and then adding 4 to it
	 */
	buf = memalign(64, BUF_SIZE);
	ut_assertnonnull(buf);
	addr = map_to_sysmem(buf);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr + 4));
	ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd_acpi_start());

	rsdp = buf + 16;
	ut_asserteq_ptr(rsdp, ctx.rsdp);
	ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
	ut_asserteq(sizeof(*rsdp), rsdp->length);
	ut_assertok(table_compute_checksum(rsdp, 20));
	ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));

	rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
	ut_asserteq_ptr(rsdt, ctx.rsdt);
	ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
	ut_asserteq(sizeof(*rsdt), rsdt->header.length);
	ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));

	xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
	ut_asserteq_ptr(xsdt, ctx.xsdt);
	ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
	ut_asserteq(sizeof(*xsdt), xsdt->header.length);
	ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));

	end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
	ut_asserteq_ptr(end, ctx.current);

	ut_asserteq(nomap_to_sysmem(rsdt), rsdp->rsdt_address);
	ut_asserteq(nomap_to_sysmem(xsdt), rsdp->xsdt_address);
	unmap_sysmem(buf);
	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_ctx_and_base_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test 'acpi list' command */
static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	ulong addr;
	void *buf;

	buf = memalign(16, BUF_SIZE);
	ut_assertnonnull(buf);
	addr = map_to_sysmem(buf);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));

	ut_assertok(acpi_write_dev_tables(&ctx));

	run_command("acpi list", 0);
	ut_assert_nextline("Name              Base   Size  Detail");
	ut_assert_nextline("----  ----------------  -----  ----------------------------");
	ut_assert_nextline("RSDP  %16lx  %5zx  v02 U-BOOT", addr,
			   sizeof(struct acpi_rsdp));
	addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
	ut_assert_nextline("RSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
			   addr, sizeof(struct acpi_table_header) +
			   3 * sizeof(u32), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
	ut_assert_nextline("XSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
			   addr, sizeof(struct acpi_table_header) +
			   3 * sizeof(u64), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
	ut_assert_console_end();
	unmap_sysmem(buf);
	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_cmd_list, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);

/* Test 'acpi list -c' command */
static int dm_test_acpi_cmd_list_chksum(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	ulong addr;
	void *buf;

	buf = memalign(16, BUF_SIZE);
	ut_assertnonnull(buf);
	addr = map_to_sysmem(buf);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));

	ut_assertok(acpi_write_dev_tables(&ctx));

	run_command("acpi list -c", 0);
	ut_assert_nextline("Name              Base   Size  Detail");
	ut_assert_nextline("----  ----------------  -----  ----------------------------");
	ut_assert_nextline("RSDP  %16lx  %5zx  v02 U-BOOT  OK  OK", addr,
			   sizeof(struct acpi_rsdp));
	addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
	ut_assert_nextline("RSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
			   addr, sizeof(struct acpi_table_header) +
			   3 * sizeof(u32), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
	ut_assert_nextline("XSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
			   addr, sizeof(struct acpi_table_header) +
			   3 * sizeof(u64), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
	ut_assert_console_end();
	ut_assert_console_end();
	unmap_sysmem(buf);
	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_cmd_list_chksum,
	UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);

/* Test 'acpi dump' command */
static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	ulong addr;
	void *buf;

	buf = memalign(16, BUF_SIZE);
	ut_assertnonnull(buf);
	addr = map_to_sysmem(buf);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));

	ut_assertok(acpi_write_dev_tables(&ctx));

	/* First search for a non-existent table */
	run_command("acpi dump rdst", 0);
	ut_assert_nextline("Table 'RDST' not found");
	ut_assert_console_end();

	/* Now a real table */
	run_command("acpi dump dmar", 0);
	addr = ALIGN(nomap_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
	ut_assert_nextline("DMAR @ %16lx", addr);
	ut_assert_nextlines_are_dump(0x30);
	ut_assert_console_end();
	unmap_sysmem(buf);
	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_cmd_dump, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);

/* Test acpi_device_path() */
static int dm_test_acpi_device_path(struct unit_test_state *uts)
{
	struct testacpi_plat *plat;
	char buf[ACPI_PATH_MAX];
	struct udevice *dev, *child;

	ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
	ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
	ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);

	/* Test running out of space */
	buf[5] = '\0';
	ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
	ut_asserteq('\0', buf[5]);

	/* Test a three-component name */
	ut_assertok(device_first_child_err(dev, &child));
	ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
	ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
			buf);

	/* Test handling of a device which doesn't produce a name */
	plat = dev_get_plat(dev);
	plat->no_name = true;
	ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
	ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);

	/* Test handling of a device which returns an error */
	plat = dev_get_plat(dev);
	plat->return_error = true;
	ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));

	return 0;
}
DM_TEST(dm_test_acpi_device_path, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test acpi_device_status() */
static int dm_test_acpi_device_status(struct unit_test_state *uts)
{
	struct udevice *dev;

	ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
	ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));

	return 0;
}
DM_TEST(dm_test_acpi_device_status, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test acpi_fill_ssdt() */
static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	u8 *buf;

	buf = malloc(BUF_SIZE);
	ut_assertnonnull(buf);

	acpi_reset_items();
	ctx.current = buf;
	buf[4] = 'z';	/* sentinel */
	ut_assertok(acpi_fill_ssdt(&ctx));

	/*
	 * These values come from acpi-test2's acpi-ssdt-test-data property.
	 * This device comes first because of u-boot,acpi-ssdt-order
	 */
	ut_asserteq('c', buf[0]);
	ut_asserteq('d', buf[1]);

	/* These values come from acpi-test's acpi-ssdt-test-data property */
	ut_asserteq('a', buf[2]);
	ut_asserteq('b', buf[3]);

	ut_asserteq('z', buf[4]);

	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_fill_ssdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test acpi_fill_madt() */
static int dm_test_acpi_fill_madt(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	u64 *buf;

	buf = malloc(BUF_SIZE);
	ut_assertnonnull(buf);

	acpi_reset_items();
	ctx.current = buf;
	buf[1] = 'z';	/* sentinel */
	ut_assertok(acpi_fill_madt_subtbl(&ctx));

	/*
	 * These values come from acpi-test2's acpi-ssdt-test-data property.
	 * This device comes first because of u-boot,acpi-ssdt-order
	 */
	ut_asserteq(0xdeadbeef, buf[0]);

	ut_asserteq('z', buf[1]);

	free(buf);

	return 0;
}

DM_TEST(dm_test_acpi_fill_madt, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test acpi_inject_dsdt() */
static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	u8 *buf;

	buf = malloc(BUF_SIZE);
	ut_assertnonnull(buf);

	acpi_reset_items();
	ctx.current = buf;
	buf[4] = 'z';	/* sentinel */
	ut_assertok(acpi_inject_dsdt(&ctx));

	/*
	 * These values come from acpi-test's acpi-dsdt-test-data property.
	 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
	 */
	ut_asserteq('h', buf[0]);
	ut_asserteq('i', buf[1]);

	/* These values come from acpi-test's acpi-dsdt-test-data property */
	ut_asserteq('j', buf[2]);
	ut_asserteq('k', buf[3]);

	ut_asserteq('z', buf[4]);

	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_inject_dsdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test 'acpi items' command */
static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	ulong addr;
	void *buf;

	buf = malloc(BUF_SIZE);
	ut_assertnonnull(buf);
	addr = map_to_sysmem(buf);

	acpi_reset_items();
	ctx.current = buf;
	ut_assertok(acpi_fill_ssdt(&ctx));
	run_command("acpi items", 0);
	ut_assert_nextline("Seq  Type       Base   Size  Device/Writer");
	ut_assert_nextline("---  -----  --------   ----  -------------");
	ut_assert_nextline("  0  ssdt   %8lx      2  acpi-test", addr);
	ut_assert_nextline("  1  ssdt   %8lx      2  acpi-test2", addr + 2);
	ut_assert_console_end();

	acpi_reset_items();
	ctx.current = buf;
	ut_assertok(acpi_inject_dsdt(&ctx));
	run_command("acpi items", 0);
	ut_assert_nextlinen("Seq");
	ut_assert_nextlinen("---");
	ut_assert_nextline("  0  dsdt   %8lx      2  acpi-test", addr);
	ut_assert_nextline("  1  dsdt   %8lx      2  acpi-test2", addr + 2);
	ut_assert_console_end();

	run_command("acpi items -d", 0);
	ut_assert_nextlinen("Seq");
	ut_assert_nextlinen("---");
	ut_assert_nextline("  0  dsdt   %8lx      2  acpi-test", addr);
	ut_assert_nextlines_are_dump(2);
	ut_assert_nextline("%s", "");
	ut_assert_nextline("  1  dsdt   %8lx      2  acpi-test2", addr + 2);
	ut_assert_nextlines_are_dump(2);
	ut_assert_nextline("%s", "");
	ut_assert_console_end();
	unmap_sysmem(buf);
	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_cmd_items, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);

/* Test 'acpi set' command */
static int dm_test_acpi_cmd_set(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	ulong addr;
	void *buf;

	gd_set_acpi_start(0);

	ut_asserteq(0, gd_acpi_start());
	ut_assertok(run_command("acpi set", 0));
	ut_assert_nextline("ACPI pointer: 0");

	buf = memalign(16, BUF_SIZE);
	ut_assertnonnull(buf);
	addr = map_to_sysmem(buf);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));

	ut_assertok(acpi_write_dev_tables(&ctx));

	ut_assertok(run_command("acpi set", 0));
	ut_assert_nextline("ACPI pointer: %lx", addr);

	ut_assertok(run_command("acpi set 0", 0));
	ut_assert_nextline("Setting ACPI pointer to 0");
	ut_asserteq(0, gd_acpi_start());

	ut_assertok(run_commandf("acpi set %lx", addr));
	ut_assert_nextline("Setting ACPI pointer to %lx", addr);
	ut_asserteq(addr, gd_acpi_start());

	ut_assert_console_end();
	unmap_sysmem(buf);
	free(buf);

	return 0;
}
DM_TEST(dm_test_acpi_cmd_set, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);

/**
 * dm_test_write_test_table() - create test ACPI table
 *
 * Create an ACPI table TSTn, where n is given by @index.
 *
 * @ctx:	ACPI table writing context
 * @index:	table index
 * Return:	generated table
 */
static struct acpi_table_header
*dm_test_write_test_table(struct acpi_ctx *ctx, int index)
{
	struct acpi_table_header *tbl = ctx->current;
	char signature[5];

	snprintf(signature, sizeof(signature), "TST%1d", index);
	memset(tbl, 0, sizeof(*tbl));
	acpi_fill_header(tbl, signature);
	acpi_inc(ctx, sizeof(struct acpi_table_header));
	tbl->length = (u8 *)ctx->current - (u8 *)tbl;
	tbl->checksum = table_compute_checksum(tbl, tbl->length);
	acpi_add_table(ctx, tbl);

	return tbl;
}

/* Test acpi_find_table() */
static int dm_test_acpi_find_table(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	ulong acpi_start, addr;
	void *buf;
	struct acpi_table_header *table, *table1, *table2, *table3;
	struct acpi_rsdp *rsdp;
	ulong rsdt;
	ulong xsdt;

	/* Keep reference to original ACPI tables */
	acpi_start = gd_acpi_start();

	/* Setup new ACPI tables */
	addr = 0x1000;
	buf = map_sysmem(addr, BUF_SIZE);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
	table3 = dm_test_write_test_table(&ctx, 3);
	table1 = dm_test_write_test_table(&ctx, 1);
	table2 = dm_test_write_test_table(&ctx, 2);

	/* Retrieve RSDP, RSDT, XSDT */
	rsdp = map_sysmem(gd_acpi_start(), 0);
	ut_assertnonnull(rsdp);
	rsdt = rsdp->rsdt_address;
	ut_assert(rsdt);
	xsdt = rsdp->xsdt_address;
	ut_assert(xsdt);

	/* Find with both RSDT and XSDT */
	table = acpi_find_table("TST1");
	ut_asserteq_ptr(table1, table);
	ut_asserteq_strn("TST1", table->signature);
	table = acpi_find_table("TST2");
	ut_asserteq_ptr(table2, table);
	ut_asserteq_strn("TST2", table->signature);
	table = acpi_find_table("TST3");
	ut_asserteq_ptr(table3, table);
	ut_asserteq_strn("TST3", table->signature);

	table = acpi_find_table("RSDT");
	ut_asserteq_ptr(nomap_sysmem(rsdt, 0), table);

	/* Find with XSDT only */
	rsdp->rsdt_address = 0;
	acpi_udpate_rsdp_checksum(rsdp);

	table = acpi_find_table("TST1");
	ut_asserteq_ptr(table1, table);
	table = acpi_find_table("TST2");
	ut_asserteq_ptr(table2, table);
	table = acpi_find_table("TST3");
	ut_asserteq_ptr(table3, table);
	rsdp->rsdt_address = rsdt;

	/* Find with RSDT only */
	rsdp->xsdt_address = 0;
	acpi_udpate_rsdp_checksum(rsdp);

	table = acpi_find_table("TST1");
	ut_asserteq_ptr(table1, table);
	table = acpi_find_table("TST2");
	ut_asserteq_ptr(table2, table);
	table = acpi_find_table("TST3");
	ut_asserteq_ptr(table3, table);
	rsdp->xsdt_address = xsdt;

	/* Restore previous ACPI tables */
	gd_set_acpi_start(acpi_start);
	unmap_sysmem(buf);

	return 0;
}
DM_TEST(dm_test_acpi_find_table, 0);

/* Test offsets in RSDT, XSDT */
static int dm_test_acpi_offsets(struct unit_test_state *uts)
{
	ut_asserteq(36, offsetof(struct acpi_rsdt, entry));
	ut_asserteq(36, offsetof(struct acpi_xsdt, entry));

	return 0;
}
DM_TEST(dm_test_acpi_offsets, 0);

/* Test finding the end of the ACPI tables */
static int dm_test_acpi_get_end(struct unit_test_state *uts)
{
	struct acpi_ctx ctx;
	ulong acpi_start, addr;
	void *buf;
	struct acpi_table_header *table1, *table2, *table3;
	void *ptr;

	/* Keep reference to original ACPI tables */
	acpi_start = gd_acpi_start();

	/* Setup new ACPI tables */
	addr = 0;
	buf = map_sysmem(addr, BUF_SIZE);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
	table3 = dm_test_write_test_table(&ctx, 3);
	table1 = dm_test_write_test_table(&ctx, 1);
	table2 = dm_test_write_test_table(&ctx, 2);

	ptr = acpi_get_end();
	ut_asserteq(0x288 + 0x24, ptr - buf);

	run_command("acpi list", 0);
	ut_assert_nextline("Name              Base   Size  Detail");
	ut_assert_nextlinen("--");
	ut_assert_nextline("RSDP                 0     24  v02 U-BOOT");
	ut_assert_nextlinen(
		"RSDT                30     30  v01 U-BOOT U-BOOTBL 202");
	ut_assert_nextlinen(
		"XSDT                e0     3c  v01 U-BOOT U-BOOTBL 202");
	ut_assert_nextlinen(
		"TST3               240     24  v00 U-BOOT U-BOOTBL 202");
	ut_assert_nextlinen(
		"TST1               264     24  v00 U-BOOT U-BOOTBL 202");
	ut_assert_nextlinen(
		"TST2               288     24  v00 U-BOOT U-BOOTBL 202");

	/* Restore previous ACPI tables */
	gd_set_acpi_start(acpi_start);
	unmap_sysmem(buf);

	return 0;
}
DM_TEST(dm_test_acpi_get_end, 0);

/* Test generating the BGRT */
static int dm_test_acpi_bgrt(struct unit_test_state *uts)
{
	struct efil_hdr *hdr = bloblist_find(BLOBLISTT_EFI_LOG, 0);
	struct acpi_table_header *table;
	struct efil_rec_hdr *rec_hdr;
	struct acpi_ctx ctx;
	ulong acpi_start, addr;
	struct acpi_bgrt *bgrt;
	void *buf;

	/* Keep reference to original ACPI tables */
	acpi_start = gd_acpi_start();

	/* Setup new ACPI tables */
	addr = 0;
	buf = map_sysmem(addr, BUF_SIZE);
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));

	if (IS_ENABLED(CONFIG_EFI_LOG))
		ut_assertok(efi_log_reset());

	ut_assertok(acpi_write_bgrt(&ctx));
	table = acpi_find_table("BGRT");
	ut_assertnonnull(table);
	bgrt = (struct acpi_bgrt *)table;

	ut_asserteq(sizeof(*bgrt), table->length);
	ut_asserteq_strn("BGRT", table->signature);
	ut_asserteq(1, bgrt->version);
	ut_asserteq(1, bgrt->status);
	ut_asserteq(0, bgrt->image_type);
	ut_asserteq(0, bgrt->offset_x);
	ut_asserteq(0, bgrt->offset_y);

	if (!IS_ENABLED(CONFIG_EFI_LOG))
		return 0;

	/* check the BGRT has been allocated in the EFI pool */
	for (rec_hdr = (void *)hdr + sizeof(*hdr);
	     (void *)rec_hdr - (void *)hdr < hdr->upto;
	     rec_hdr = (void *)rec_hdr + rec_hdr->size) {
		void *start = (void *)rec_hdr + sizeof(struct efil_rec_hdr);

		switch (rec_hdr->tag) {
		case EFILT_ALLOCATE_POOL: {
			struct efil_allocate_pool *rec = start;

			ut_asserteq(bgrt->addr, nomap_to_sysmem(rec->e_buffer));
			break;
		}
		default:
			break;
		}
	}

	return 0;
}
DM_TEST(dm_test_acpi_bgrt, UTF_SCAN_FDT);

/* Test ACPI FPDT (Firmware Performance Data Table) generation */
static int dm_test_acpi_fpdt(struct unit_test_state *uts)
{
	struct acpi_fpdt_boot *rec;
	struct acpi_fpdt *fpdt;
	struct acpi_ctx ctx;
	ulong addr, time;
	void *buf;

	addr = 0;
	buf = map_sysmem(addr, BUF_SIZE);

	/* Set up context with base tables (RSDP, RSDT, XSDT) */
	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));

	/* Save where the FPDT will be written */
	fpdt = ctx.current;

	/* Write the FPDT table with test U-Boot start time */
	time = timer_get_boot_us();
	ut_assertok(acpi_write_fpdt(&ctx, 1234));

	/* Verify the FPDT was written at the saved location */
	ut_asserteq_mem("FPDT", fpdt->header.signature, ACPI_NAME_LEN);
	ut_asserteq(1, fpdt->header.revision);
	ut_asserteq(sizeof(struct acpi_fpdt) + sizeof(struct acpi_fpdt_boot),
		    fpdt->header.length);

	/* Verify the boot performance record */
	rec = (struct acpi_fpdt_boot *)(fpdt + 1);
	ut_asserteq(FPDT_REC_BOOT, rec->hdr.type);
	ut_asserteq(sizeof(struct acpi_fpdt_boot), rec->hdr.length);
	ut_asserteq(2, rec->hdr.revision);

	ut_asserteq(1234, rec->reset_end);
	ut_assert(rec->loader_start != 0);
	ut_assert(rec->loader_exec != 0);
	ut_assert(rec->ebs_entry != 0);
	ut_assert(rec->ebs_exit != 0);

	/* Verify checksum is valid */
	ut_asserteq(0, table_compute_checksum(fpdt, fpdt->header.length));

	/* Get pointer to boot record and verify it matches */
	rec = acpi_get_fpdt_boot();
	ut_assertnonnull(rec);
	ut_asserteq_ptr(rec, (struct acpi_fpdt_boot *)(fpdt + 1));
	ut_asserteq(1234, rec->reset_end);

	/*
	 * Update timing fields - use 0 since we know the original values are
	 * non-zero
	 */
	rec->ebs_entry = 0;
	rec->ebs_exit = 0;

	/* Checksum should now be invalid */
	ut_assert(table_compute_checksum(fpdt, fpdt->header.length) != 0);

	/* Fix the checksum */
	ut_assertok(acpi_fix_fpdt_checksum());

	/* Checksum should now be valid again */
	ut_asserteq(0, table_compute_checksum(fpdt, fpdt->header.length));

	/* Verify the updated values are still there */
	ut_asserteq(0, rec->ebs_entry);
	ut_asserteq(0, rec->ebs_exit);

	unmap_sysmem(buf);

	return 0;
}
DM_TEST(dm_test_acpi_fpdt, 0);