Skip to content

Commit 8cf82bb

Browse files
Koichiro Denbjorn-helgaas
authored andcommitted
misc: pci_endpoint_test: Add BAR subrange mapping test case
Add a new PCITEST_BAR_SUBRANGE ioctl to exercise EPC BAR subrange mapping end-to-end. The test programs a simple 2-subrange layout on the endpoint (via pci-epf-test) and verifies that: - the endpoint-provided per-subrange signature bytes are observed at the expected PCIe BAR offsets, and - writes to each subrange are routed to the correct backing region (i.e. the submap order is applied rather than accidentally working due to an identity mapping). Return -EOPNOTSUPP when the endpoint does not advertise subrange mapping, -ENODATA when the BAR is disabled, and -EBUSY when the BAR is reserved for the test register space. Signed-off-by: Koichiro Den <den@valinux.co.jp> Signed-off-by: Manivannan Sadhasivam <mani@kernel.org> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Link: https://patch.msgid.link/20260124145012.2794108-8-den@valinux.co.jp
1 parent 6c5e610 commit 8cf82bb

2 files changed

Lines changed: 203 additions & 1 deletion

File tree

drivers/misc/pci_endpoint_test.c

Lines changed: 202 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#define COMMAND_COPY BIT(5)
4040
#define COMMAND_ENABLE_DOORBELL BIT(6)
4141
#define COMMAND_DISABLE_DOORBELL BIT(7)
42+
#define COMMAND_BAR_SUBRANGE_SETUP BIT(8)
43+
#define COMMAND_BAR_SUBRANGE_CLEAR BIT(9)
4244

4345
#define PCI_ENDPOINT_TEST_STATUS 0x8
4446
#define STATUS_READ_SUCCESS BIT(0)
@@ -55,6 +57,10 @@
5557
#define STATUS_DOORBELL_ENABLE_FAIL BIT(11)
5658
#define STATUS_DOORBELL_DISABLE_SUCCESS BIT(12)
5759
#define STATUS_DOORBELL_DISABLE_FAIL BIT(13)
60+
#define STATUS_BAR_SUBRANGE_SETUP_SUCCESS BIT(14)
61+
#define STATUS_BAR_SUBRANGE_SETUP_FAIL BIT(15)
62+
#define STATUS_BAR_SUBRANGE_CLEAR_SUCCESS BIT(16)
63+
#define STATUS_BAR_SUBRANGE_CLEAR_FAIL BIT(17)
5864

5965
#define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c
6066
#define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
@@ -77,6 +83,7 @@
7783
#define CAP_MSI BIT(1)
7884
#define CAP_MSIX BIT(2)
7985
#define CAP_INTX BIT(3)
86+
#define CAP_SUBRANGE_MAPPING BIT(4)
8087

8188
#define PCI_ENDPOINT_TEST_DB_BAR 0x34
8289
#define PCI_ENDPOINT_TEST_DB_OFFSET 0x38
@@ -100,6 +107,8 @@
100107

101108
#define PCI_DEVICE_ID_ROCKCHIP_RK3588 0x3588
102109

110+
#define PCI_ENDPOINT_TEST_BAR_SUBRANGE_NSUB 2
111+
103112
static DEFINE_IDA(pci_endpoint_test_ida);
104113

105114
#define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
@@ -414,6 +423,193 @@ static int pci_endpoint_test_bars(struct pci_endpoint_test *test)
414423
return 0;
415424
}
416425

426+
static u8 pci_endpoint_test_subrange_sig_byte(enum pci_barno barno,
427+
unsigned int subno)
428+
{
429+
return 0x50 + (barno * 8) + subno;
430+
}
431+
432+
static u8 pci_endpoint_test_subrange_test_byte(enum pci_barno barno,
433+
unsigned int subno)
434+
{
435+
return 0xa0 + (barno * 8) + subno;
436+
}
437+
438+
static int pci_endpoint_test_bar_subrange_cmd(struct pci_endpoint_test *test,
439+
enum pci_barno barno, u32 command,
440+
u32 ok_bit, u32 fail_bit)
441+
{
442+
struct pci_dev *pdev = test->pdev;
443+
struct device *dev = &pdev->dev;
444+
int irq_type = test->irq_type;
445+
u32 status;
446+
447+
if (irq_type < PCITEST_IRQ_TYPE_INTX ||
448+
irq_type > PCITEST_IRQ_TYPE_MSIX) {
449+
dev_err(dev, "Invalid IRQ type\n");
450+
return -EINVAL;
451+
}
452+
453+
reinit_completion(&test->irq_raised);
454+
455+
pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS, 0);
456+
pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
457+
pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
458+
/* Reuse SIZE as a command parameter: bar number. */
459+
pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, barno);
460+
pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND, command);
461+
462+
if (!wait_for_completion_timeout(&test->irq_raised,
463+
msecs_to_jiffies(1000)))
464+
return -ETIMEDOUT;
465+
466+
status = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
467+
if (status & fail_bit)
468+
return -EIO;
469+
470+
if (!(status & ok_bit))
471+
return -EIO;
472+
473+
return 0;
474+
}
475+
476+
static int pci_endpoint_test_bar_subrange_setup(struct pci_endpoint_test *test,
477+
enum pci_barno barno)
478+
{
479+
return pci_endpoint_test_bar_subrange_cmd(test, barno,
480+
COMMAND_BAR_SUBRANGE_SETUP,
481+
STATUS_BAR_SUBRANGE_SETUP_SUCCESS,
482+
STATUS_BAR_SUBRANGE_SETUP_FAIL);
483+
}
484+
485+
static int pci_endpoint_test_bar_subrange_clear(struct pci_endpoint_test *test,
486+
enum pci_barno barno)
487+
{
488+
return pci_endpoint_test_bar_subrange_cmd(test, barno,
489+
COMMAND_BAR_SUBRANGE_CLEAR,
490+
STATUS_BAR_SUBRANGE_CLEAR_SUCCESS,
491+
STATUS_BAR_SUBRANGE_CLEAR_FAIL);
492+
}
493+
494+
static int pci_endpoint_test_bar_subrange(struct pci_endpoint_test *test,
495+
enum pci_barno barno)
496+
{
497+
u32 nsub = PCI_ENDPOINT_TEST_BAR_SUBRANGE_NSUB;
498+
struct device *dev = &test->pdev->dev;
499+
size_t sub_size, buf_size;
500+
resource_size_t bar_size;
501+
void __iomem *bar_addr;
502+
void *read_buf = NULL;
503+
int ret, clear_ret;
504+
size_t off, chunk;
505+
u32 i, exp, val;
506+
u8 pattern;
507+
508+
if (!(test->ep_caps & CAP_SUBRANGE_MAPPING))
509+
return -EOPNOTSUPP;
510+
511+
/*
512+
* The test register BAR is not safe to reprogram and write/read
513+
* over its full size. BAR_TEST already special-cases it to a tiny
514+
* range. For subrange mapping tests, let's simply skip it.
515+
*/
516+
if (barno == test->test_reg_bar)
517+
return -EBUSY;
518+
519+
bar_size = pci_resource_len(test->pdev, barno);
520+
if (!bar_size)
521+
return -ENODATA;
522+
523+
bar_addr = test->bar[barno];
524+
if (!bar_addr)
525+
return -ENOMEM;
526+
527+
ret = pci_endpoint_test_bar_subrange_setup(test, barno);
528+
if (ret)
529+
return ret;
530+
531+
if (bar_size % nsub || bar_size / nsub > SIZE_MAX) {
532+
ret = -EINVAL;
533+
goto out_clear;
534+
}
535+
536+
sub_size = bar_size / nsub;
537+
if (sub_size < sizeof(u32)) {
538+
ret = -ENOSPC;
539+
goto out_clear;
540+
}
541+
542+
/* Limit the temporary buffer size */
543+
buf_size = min_t(size_t, sub_size, SZ_1M);
544+
545+
read_buf = kmalloc(buf_size, GFP_KERNEL);
546+
if (!read_buf) {
547+
ret = -ENOMEM;
548+
goto out_clear;
549+
}
550+
551+
/*
552+
* Step 1: verify EP-provided signature per subrange. This detects
553+
* whether the EP actually applied the submap order.
554+
*/
555+
for (i = 0; i < nsub; i++) {
556+
exp = (u32)pci_endpoint_test_subrange_sig_byte(barno, i) *
557+
0x01010101U;
558+
val = ioread32(bar_addr + (i * sub_size));
559+
if (val != exp) {
560+
dev_err(dev,
561+
"BAR%d subrange%u signature mismatch @%#zx: exp %#08x got %#08x\n",
562+
barno, i, (size_t)i * sub_size, exp, val);
563+
ret = -EIO;
564+
goto out_clear;
565+
}
566+
val = ioread32(bar_addr + (i * sub_size) + sub_size - sizeof(u32));
567+
if (val != exp) {
568+
dev_err(dev,
569+
"BAR%d subrange%u signature mismatch @%#zx: exp %#08x got %#08x\n",
570+
barno, i,
571+
((size_t)i * sub_size) + sub_size - sizeof(u32),
572+
exp, val);
573+
ret = -EIO;
574+
goto out_clear;
575+
}
576+
}
577+
578+
/* Step 2: write unique pattern per subrange (write all first). */
579+
for (i = 0; i < nsub; i++) {
580+
pattern = pci_endpoint_test_subrange_test_byte(barno, i);
581+
memset_io(bar_addr + (i * sub_size), pattern, sub_size);
582+
}
583+
584+
/* Step 3: read back and verify (read all after all writes). */
585+
for (i = 0; i < nsub; i++) {
586+
pattern = pci_endpoint_test_subrange_test_byte(barno, i);
587+
for (off = 0; off < sub_size; off += chunk) {
588+
void *bad;
589+
590+
chunk = min_t(size_t, buf_size, sub_size - off);
591+
memcpy_fromio(read_buf, bar_addr + (i * sub_size) + off,
592+
chunk);
593+
bad = memchr_inv(read_buf, pattern, chunk);
594+
if (bad) {
595+
size_t bad_off = (u8 *)bad - (u8 *)read_buf;
596+
597+
dev_err(dev,
598+
"BAR%d subrange%u data mismatch @%#zx (pattern %#02x)\n",
599+
barno, i, (size_t)i * sub_size + off + bad_off,
600+
pattern);
601+
ret = -EIO;
602+
goto out_clear;
603+
}
604+
}
605+
}
606+
607+
out_clear:
608+
kfree(read_buf);
609+
clear_ret = pci_endpoint_test_bar_subrange_clear(test, barno);
610+
return ret ?: clear_ret;
611+
}
612+
417613
static int pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
418614
{
419615
u32 val;
@@ -936,12 +1132,17 @@ static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
9361132

9371133
switch (cmd) {
9381134
case PCITEST_BAR:
1135+
case PCITEST_BAR_SUBRANGE:
9391136
bar = arg;
9401137
if (bar <= NO_BAR || bar > BAR_5)
9411138
goto ret;
9421139
if (is_am654_pci_dev(pdev) && bar == BAR_0)
9431140
goto ret;
944-
ret = pci_endpoint_test_bar(test, bar);
1141+
1142+
if (cmd == PCITEST_BAR)
1143+
ret = pci_endpoint_test_bar(test, bar);
1144+
else
1145+
ret = pci_endpoint_test_bar_subrange(test, bar);
9451146
break;
9461147
case PCITEST_BARS:
9471148
ret = pci_endpoint_test_bars(test);

include/uapi/linux/pcitest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define PCITEST_GET_IRQTYPE _IO('P', 0x9)
2323
#define PCITEST_BARS _IO('P', 0xa)
2424
#define PCITEST_DOORBELL _IO('P', 0xb)
25+
#define PCITEST_BAR_SUBRANGE _IO('P', 0xc)
2526
#define PCITEST_CLEAR_IRQ _IO('P', 0x10)
2627

2728
#define PCITEST_IRQ_TYPE_UNDEFINED -1

0 commit comments

Comments
 (0)