]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PCI: endpoint: pci-epf-test: Add doorbell test support
authorFrank Li <Frank.Li@nxp.com>
Thu, 10 Jul 2025 19:13:52 +0000 (15:13 -0400)
committerBjorn Helgaas <bhelgaas@google.com>
Thu, 24 Jul 2025 21:51:43 +0000 (16:51 -0500)
Add doorbell support by allocating a dedicated BAR using the
pci_epf_alloc_doorbell() API and mapping the Endpoint MSI controller
message data address to it. The data to be written in the message address
is stored in the 'pci_epf_test_reg::doorbell_data' register. Finally, the
RC can trigger doorbell in the Endpoint by writing the content of
'doorbell_data' register to the offset specified in 'doorbell_offset' of
the 'doorbell_bar' BAR.

Triggering of the doorbell is detected by pci_epf_test_doorbell_handler(),
which is bound to the doorbell IRQ. On successful completion,
STATUS_DOORBELL_SUCCESS status is set in the above mentioned handler.

To avoid breaking compatibility between host and endpoint, add two new
commands: COMMAND_ENABLE_DOORBELL and COMMAND_DISABLE_DOORBELL.

The doorbell is allocated when COMMAND_ENABLE_DOORBELL command is called
and destroyed when COMMAND_DISABLE_DOORBELL is called.

This doorbell feature only works when both RC and EP drivers support it.
If one of them doesn't support the feature, the testcase will fail.

Signed-off-by: Frank Li <Frank.Li@nxp.com>
[mani: code cleanups and reworded commit message]
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Tested-by: Niklas Cassel <cassel@kernel.org>
Link: https://patch.msgid.link/20250710-ep-msi-v21-6-57683fc7fb25@nxp.com
drivers/pci/endpoint/functions/pci-epf-test.c

index 50eb4106369f41afa00ed3ae58c84922e0a49e51..e091193bd8a8a13aeb45c8b8ab35b0f43912eca9 100644 (file)
 #include <linux/dmaengine.h>
 #include <linux/io.h>
 #include <linux/module.h>
+#include <linux/msi.h>
 #include <linux/slab.h>
 #include <linux/pci_ids.h>
 #include <linux/random.h>
 
 #include <linux/pci-epc.h>
 #include <linux/pci-epf.h>
+#include <linux/pci-ep-msi.h>
 #include <linux/pci_regs.h>
 
 #define IRQ_TYPE_INTX                  0
@@ -29,6 +31,8 @@
 #define COMMAND_READ                   BIT(3)
 #define COMMAND_WRITE                  BIT(4)
 #define COMMAND_COPY                   BIT(5)
+#define COMMAND_ENABLE_DOORBELL                BIT(6)
+#define COMMAND_DISABLE_DOORBELL       BIT(7)
 
 #define STATUS_READ_SUCCESS            BIT(0)
 #define STATUS_READ_FAIL               BIT(1)
 #define STATUS_IRQ_RAISED              BIT(6)
 #define STATUS_SRC_ADDR_INVALID                BIT(7)
 #define STATUS_DST_ADDR_INVALID                BIT(8)
+#define STATUS_DOORBELL_SUCCESS                BIT(9)
+#define STATUS_DOORBELL_ENABLE_SUCCESS BIT(10)
+#define STATUS_DOORBELL_ENABLE_FAIL    BIT(11)
+#define STATUS_DOORBELL_DISABLE_SUCCESS BIT(12)
+#define STATUS_DOORBELL_DISABLE_FAIL   BIT(13)
 
 #define FLAG_USE_DMA                   BIT(0)
 
@@ -66,6 +75,7 @@ struct pci_epf_test {
        bool                    dma_supported;
        bool                    dma_private;
        const struct pci_epc_features *epc_features;
+       struct pci_epf_bar      db_bar;
 };
 
 struct pci_epf_test_reg {
@@ -80,6 +90,9 @@ struct pci_epf_test_reg {
        __le32 irq_number;
        __le32 flags;
        __le32 caps;
+       __le32 doorbell_bar;
+       __le32 doorbell_offset;
+       __le32 doorbell_data;
 } __packed;
 
 static struct pci_epf_header test_header = {
@@ -667,6 +680,115 @@ static void pci_epf_test_raise_irq(struct pci_epf_test *epf_test,
        }
 }
 
+static irqreturn_t pci_epf_test_doorbell_handler(int irq, void *data)
+{
+       struct pci_epf_test *epf_test = data;
+       enum pci_barno test_reg_bar = epf_test->test_reg_bar;
+       struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
+       u32 status = le32_to_cpu(reg->status);
+
+       status |= STATUS_DOORBELL_SUCCESS;
+       reg->status = cpu_to_le32(status);
+       pci_epf_test_raise_irq(epf_test, reg);
+
+       return IRQ_HANDLED;
+}
+
+static void pci_epf_test_doorbell_cleanup(struct pci_epf_test *epf_test)
+{
+       struct pci_epf_test_reg *reg = epf_test->reg[epf_test->test_reg_bar];
+       struct pci_epf *epf = epf_test->epf;
+
+       free_irq(epf->db_msg[0].virq, epf_test);
+       reg->doorbell_bar = cpu_to_le32(NO_BAR);
+
+       pci_epf_free_doorbell(epf);
+}
+
+static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test,
+                                        struct pci_epf_test_reg *reg)
+{
+       u32 status = le32_to_cpu(reg->status);
+       struct pci_epf *epf = epf_test->epf;
+       struct pci_epc *epc = epf->epc;
+       struct msi_msg *msg;
+       enum pci_barno bar;
+       size_t offset;
+       int ret;
+
+       ret = pci_epf_alloc_doorbell(epf, 1);
+       if (ret)
+               goto set_status_err;
+
+       msg = &epf->db_msg[0].msg;
+       bar = pci_epc_get_next_free_bar(epf_test->epc_features, epf_test->test_reg_bar + 1);
+       if (bar < BAR_0)
+               goto err_doorbell_cleanup;
+
+       ret = request_irq(epf->db_msg[0].virq, pci_epf_test_doorbell_handler, 0,
+                         "pci-ep-test-doorbell", epf_test);
+       if (ret) {
+               dev_err(&epf->dev,
+                       "Failed to request doorbell IRQ: %d\n",
+                       epf->db_msg[0].virq);
+               goto err_doorbell_cleanup;
+       }
+
+       reg->doorbell_data = cpu_to_le32(msg->data);
+       reg->doorbell_bar = cpu_to_le32(bar);
+
+       msg = &epf->db_msg[0].msg;
+       ret = pci_epf_align_inbound_addr(epf, bar, ((u64)msg->address_hi << 32) | msg->address_lo,
+                                        &epf_test->db_bar.phys_addr, &offset);
+
+       if (ret)
+               goto err_doorbell_cleanup;
+
+       reg->doorbell_offset = cpu_to_le32(offset);
+
+       epf_test->db_bar.barno = bar;
+       epf_test->db_bar.size = epf->bar[bar].size;
+       epf_test->db_bar.flags = epf->bar[bar].flags;
+
+       ret = pci_epc_set_bar(epc, epf->func_no, epf->vfunc_no, &epf_test->db_bar);
+       if (ret)
+               goto err_doorbell_cleanup;
+
+       status |= STATUS_DOORBELL_ENABLE_SUCCESS;
+       reg->status = cpu_to_le32(status);
+       return;
+
+err_doorbell_cleanup:
+       pci_epf_test_doorbell_cleanup(epf_test);
+set_status_err:
+       status |= STATUS_DOORBELL_ENABLE_FAIL;
+       reg->status = cpu_to_le32(status);
+}
+
+static void pci_epf_test_disable_doorbell(struct pci_epf_test *epf_test,
+                                         struct pci_epf_test_reg *reg)
+{
+       enum pci_barno bar = le32_to_cpu(reg->doorbell_bar);
+       u32 status = le32_to_cpu(reg->status);
+       struct pci_epf *epf = epf_test->epf;
+       struct pci_epc *epc = epf->epc;
+
+       if (bar < BAR_0)
+               goto set_status_err;
+
+       pci_epf_test_doorbell_cleanup(epf_test);
+       pci_epc_clear_bar(epc, epf->func_no, epf->vfunc_no, &epf_test->db_bar);
+
+       status |= STATUS_DOORBELL_DISABLE_SUCCESS;
+       reg->status = cpu_to_le32(status);
+
+       return;
+
+set_status_err:
+       status |= STATUS_DOORBELL_DISABLE_FAIL;
+       reg->status = cpu_to_le32(status);
+}
+
 static void pci_epf_test_cmd_handler(struct work_struct *work)
 {
        u32 command;
@@ -714,6 +836,14 @@ static void pci_epf_test_cmd_handler(struct work_struct *work)
                pci_epf_test_copy(epf_test, reg);
                pci_epf_test_raise_irq(epf_test, reg);
                break;
+       case COMMAND_ENABLE_DOORBELL:
+               pci_epf_test_enable_doorbell(epf_test, reg);
+               pci_epf_test_raise_irq(epf_test, reg);
+               break;
+       case COMMAND_DISABLE_DOORBELL:
+               pci_epf_test_disable_doorbell(epf_test, reg);
+               pci_epf_test_raise_irq(epf_test, reg);
+               break;
        default:
                dev_err(dev, "Invalid command 0x%x\n", command);
                break;