mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Frank Li <Frank.li@nxp.com>
To: Koichiro Den <den@valinux.co.jp>
Cc: vkoul@kernel.org, mani@kernel.org, jingoohan1@gmail.com,
	lpieralisi@kernel.org, kwilczynski@kernel.org, robh@kernel.org,
	bhelgaas@google.com, dmaengine@vger.kernel.org,
	linux-pci@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 7/9] PCI: endpoint: pci-epf-test: Add embedded doorbell variant
Date: Fri, 6 Feb 2026 14:20:58 -0500	[thread overview]
Message-ID: <aYY_Ghig9wvnp8Wg@lizhi-Precision-Tower-5810> (raw)
In-Reply-To: <20260206172646.1556847-8-den@valinux.co.jp>

On Sat, Feb 07, 2026 at 02:26:44AM +0900, Koichiro Den wrote:
> Extend pci-epf-test with an "embedded doorbell" variant that does not
> rely on the EPC doorbell/MSI mechanism.
>
> When the host sets FLAG_DB_EMBEDDED, query EPC remote resources to
> locate the embedded DMA MMIO window and a per-channel
> interrupt-emulation doorbell register offset. Map the MMIO window into a
> free BAR and return BAR+offset to the host as the doorbell target.
>
> Handle the resulting shared IRQ by deferring completion signalling to a
> work item, then update the test status and raise the completion IRQ back
> to the host.
>
> The existing MSI doorbell remains the default when FLAG_DB_EMBEDDED is
> not set.
>
> Signed-off-by: Koichiro Den <den@valinux.co.jp>
> ---

Can you change pci_epf_alloc_doorbell() directly? Let it fall back to
edma implement.

Frank

>  drivers/pci/endpoint/functions/pci-epf-test.c | 193 +++++++++++++++++-
>  1 file changed, 185 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
> index 6952ee418622..5871da8cbddf 100644
> --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> @@ -6,6 +6,7 @@
>   * Author: Kishon Vijay Abraham I <kishon@ti.com>
>   */
>
> +#include <linux/bitops.h>
>  #include <linux/crc32.h>
>  #include <linux/delay.h>
>  #include <linux/dmaengine.h>
> @@ -56,6 +57,7 @@
>  #define STATUS_BAR_SUBRANGE_CLEAR_FAIL		BIT(17)
>
>  #define FLAG_USE_DMA			BIT(0)
> +#define FLAG_DB_EMBEDDED		BIT(1)
>
>  #define TIMER_RESOLUTION		1
>
> @@ -69,6 +71,12 @@
>
>  static struct workqueue_struct *kpcitest_workqueue;
>
> +enum pci_epf_test_doorbell_variant {
> +	PCI_EPF_TEST_DB_NONE = 0,
> +	PCI_EPF_TEST_DB_MSI,
> +	PCI_EPF_TEST_DB_EMBEDDED,
> +};
> +
>  struct pci_epf_test {
>  	void			*reg[PCI_STD_NUM_BARS];
>  	struct pci_epf		*epf;
> @@ -85,7 +93,11 @@ struct pci_epf_test {
>  	bool			dma_supported;
>  	bool			dma_private;
>  	const struct pci_epc_features *epc_features;
> +	enum pci_epf_test_doorbell_variant db_variant;
>  	struct pci_epf_bar	db_bar;
> +	int			db_irq;
> +	unsigned long		db_irq_pending;
> +	struct work_struct	db_work;
>  	size_t			bar_size[PCI_STD_NUM_BARS];
>  };
>
> @@ -696,7 +708,7 @@ 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)
> +static irqreturn_t pci_epf_test_doorbell_msi_handler(int irq, void *data)
>  {
>  	struct pci_epf_test *epf_test = data;
>  	enum pci_barno test_reg_bar = epf_test->test_reg_bar;
> @@ -710,19 +722,58 @@ static irqreturn_t pci_epf_test_doorbell_handler(int irq, void *data)
>  	return IRQ_HANDLED;
>  }
>
> +static void pci_epf_test_doorbell_embedded_work(struct work_struct *work)
> +{
> +	struct pci_epf_test *epf_test =
> +		container_of(work, struct pci_epf_test, db_work);
> +	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);
> +
> +	clear_bit(0, &epf_test->db_irq_pending);
> +}
> +
> +static irqreturn_t pci_epf_test_doorbell_embedded_irq_handler(int irq, void *data)
> +{
> +	struct pci_epf_test *epf_test = data;
> +
> +	if (READ_ONCE(epf_test->db_variant) != PCI_EPF_TEST_DB_EMBEDDED)
> +		return IRQ_NONE;
> +
> +	if (test_and_set_bit(0, &epf_test->db_irq_pending))
> +		return IRQ_HANDLED;
> +
> +	queue_work(kpcitest_workqueue, &epf_test->db_work);
> +	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);
> +	if (epf_test->db_irq) {
> +		free_irq(epf_test->db_irq, epf_test);
> +		epf_test->db_irq = 0;
> +	}
> +
> +	if (epf_test->db_variant == PCI_EPF_TEST_DB_EMBEDDED) {
> +		cancel_work_sync(&epf_test->db_work);
> +		clear_bit(0, &epf_test->db_irq_pending);
> +	} else if (epf_test->db_variant == PCI_EPF_TEST_DB_MSI) {
> +		pci_epf_free_doorbell(epf);
> +	}
>
> -	pci_epf_free_doorbell(epf);
> +	reg->doorbell_bar = cpu_to_le32(NO_BAR);
> +	epf_test->db_variant = PCI_EPF_TEST_DB_NONE;
>  }
>
> -static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test,
> -					 struct pci_epf_test_reg *reg)
> +static void pci_epf_test_enable_doorbell_msi(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;
> @@ -736,20 +787,23 @@ static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test,
>  	if (ret)
>  		goto set_status_err;
>
> +	epf_test->db_variant = PCI_EPF_TEST_DB_MSI;
>  	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_threaded_irq(epf->db_msg[0].virq, NULL,
> -				   pci_epf_test_doorbell_handler, IRQF_ONESHOT,
> -				   "pci-ep-test-doorbell", epf_test);
> +				   pci_epf_test_doorbell_msi_handler,
> +				   IRQF_ONESHOT, "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;
>  	}
> +	epf_test->db_irq = epf->db_msg[0].virq;
>
>  	reg->doorbell_data = cpu_to_le32(msg->data);
>  	reg->doorbell_bar = cpu_to_le32(bar);
> @@ -782,6 +836,125 @@ static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test,
>  	reg->status = cpu_to_le32(status);
>  }
>
> +static void pci_epf_test_enable_doorbell_embedded(struct pci_epf_test *epf_test,
> +						  struct pci_epf_test_reg *reg)
> +{
> +	struct pci_epc_remote_resource *dma_ctrl = NULL, *chan0 = NULL;
> +	const char *irq_name = "pci-ep-test-doorbell-embedded";
> +	u32 status = le32_to_cpu(reg->status);
> +	struct pci_epf *epf = epf_test->epf;
> +	struct pci_epc *epc = epf->epc;
> +	struct device *dev = &epf->dev;
> +	enum pci_barno bar;
> +	size_t align_off;
> +	unsigned int i;
> +	int cnt, ret;
> +	u32 db_off;
> +
> +	cnt = pci_epc_get_remote_resources(epc, epf->func_no, epf->vfunc_no,
> +					   NULL, 0);
> +	if (cnt <= 0) {
> +		dev_err(dev, "No remote resources available for embedded doorbell\n");
> +		goto set_status_err;
> +	}
> +
> +	struct pci_epc_remote_resource *resources __free(kfree) =
> +				kcalloc(cnt, sizeof(*resources), GFP_KERNEL);
> +	if (!resources)
> +		goto set_status_err;
> +
> +	ret = pci_epc_get_remote_resources(epc, epf->func_no, epf->vfunc_no,
> +					   resources, cnt);
> +	if (ret < 0) {
> +		dev_err(dev, "Failed to get remote resources: %d\n", ret);
> +		goto set_status_err;
> +	}
> +	cnt = ret;
> +
> +	for (i = 0; i < cnt; i++) {
> +		if (resources[i].type == PCI_EPC_RR_DMA_CTRL_MMIO)
> +			dma_ctrl = &resources[i];
> +		else if (resources[i].type == PCI_EPC_RR_DMA_CHAN_DESC &&
> +			 !chan0)
> +			chan0 = &resources[i];
> +	}
> +
> +	if (!dma_ctrl || !chan0) {
> +		dev_err(dev, "Missing DMA ctrl MMIO or channel #0 info\n");
> +		goto set_status_err;
> +	}
> +
> +	bar = pci_epc_get_next_free_bar(epf_test->epc_features,
> +					epf_test->test_reg_bar + 1);
> +	if (bar < BAR_0) {
> +		dev_err(dev, "No free BAR for embedded doorbell\n");
> +		goto set_status_err;
> +	}
> +
> +	ret = pci_epf_align_inbound_addr(epf, bar, dma_ctrl->phys_addr,
> +					 &epf_test->db_bar.phys_addr,
> +					 &align_off);
> +	if (ret)
> +		goto set_status_err;
> +
> +	db_off = chan0->u.dma_chan_desc.db_offset;
> +	if (db_off >= dma_ctrl->size ||
> +	    align_off + db_off >= epf->bar[bar].size) {
> +		dev_err(dev, "BAR%d too small for embedded doorbell (off %#zx + %#x)\n",
> +			bar, align_off, db_off);
> +		goto set_status_err;
> +	}
> +
> +	epf_test->db_variant = PCI_EPF_TEST_DB_EMBEDDED;
> +
> +	ret = request_irq(chan0->u.dma_chan_desc.irq,
> +			  pci_epf_test_doorbell_embedded_irq_handler,
> +			  IRQF_SHARED, irq_name, epf_test);
> +	if (ret) {
> +		dev_err(dev, "Failed to request embedded doorbell IRQ: %d\n",
> +			chan0->u.dma_chan_desc.irq);
> +		goto err_cleanup;
> +	}
> +	epf_test->db_irq = chan0->u.dma_chan_desc.irq;
> +
> +	reg->doorbell_data = cpu_to_le32(0);
> +	reg->doorbell_bar = cpu_to_le32(bar);
> +	reg->doorbell_offset = cpu_to_le32(align_off + db_off);
> +
> +	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_cleanup;
> +
> +	status |= STATUS_DOORBELL_ENABLE_SUCCESS;
> +	reg->status = cpu_to_le32(status);
> +	return;
> +
> +err_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_enable_doorbell(struct pci_epf_test *epf_test,
> +					 struct pci_epf_test_reg *reg)
> +{
> +	u32 flags = le32_to_cpu(reg->flags);
> +
> +	/* If already enabled, drop previous setup first. */
> +	if (epf_test->db_variant != PCI_EPF_TEST_DB_NONE)
> +		pci_epf_test_doorbell_cleanup(epf_test);
> +
> +	if (flags & FLAG_DB_EMBEDDED)
> +		pci_epf_test_enable_doorbell_embedded(epf_test, reg);
> +	else
> +		pci_epf_test_enable_doorbell_msi(epf_test, reg);
> +}
> +
>  static void pci_epf_test_disable_doorbell(struct pci_epf_test *epf_test,
>  					  struct pci_epf_test_reg *reg)
>  {
> @@ -1309,6 +1482,9 @@ static void pci_epf_test_unbind(struct pci_epf *epf)
>
>  	cancel_delayed_work_sync(&epf_test->cmd_handler);
>  	if (epc->init_complete) {
> +		/* In case userspace never disabled doorbell explicitly. */
> +		if (epf_test->db_variant != PCI_EPF_TEST_DB_NONE)
> +			pci_epf_test_doorbell_cleanup(epf_test);
>  		pci_epf_test_clean_dma_chan(epf_test);
>  		pci_epf_test_clear_bar(epf);
>  	}
> @@ -1427,6 +1603,7 @@ static int pci_epf_test_probe(struct pci_epf *epf,
>  		epf_test->bar_size[bar] = default_bar_size[bar];
>
>  	INIT_DELAYED_WORK(&epf_test->cmd_handler, pci_epf_test_cmd_handler);
> +	INIT_WORK(&epf_test->db_work, pci_epf_test_doorbell_embedded_work);
>
>  	epf->event_ops = &pci_epf_test_event_ops;
>
> --
> 2.51.0
>

  reply	other threads:[~2026-02-06 19:21 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-06 17:26 [PATCH v4 0/9] dmaengine, PCI: endpoint: Enable remote use of integrated DesignWare eDMA Koichiro Den
2026-02-06 17:26 ` [PATCH v4 1/9] dmaengine: dw-edma: Add per-channel interrupt routing control Koichiro Den
2026-02-06 17:43   ` Frank Li
2026-02-09  4:06     ` Koichiro Den
2026-02-06 17:26 ` [PATCH v4 2/9] dmaengine: dw-edma: Deassert emulated interrupts in the IRQ handler Koichiro Den
2026-02-06 17:26 ` [PATCH v4 3/9] dmaengine: dw-edma: Export per-channel IRQ and doorbell register offset Koichiro Den
2026-02-06 17:56   ` Frank Li
2026-02-09  4:14     ` Koichiro Den
2026-02-08  8:57   ` kernel test robot
2026-02-06 17:26 ` [PATCH v4 4/9] PCI: endpoint: Add remote resource query API Koichiro Den
2026-02-06 18:01   ` Frank Li
2026-02-07 16:39     ` Koichiro Den
2026-02-10 12:20       ` Niklas Cassel
2026-02-10 14:18         ` Koichiro Den
2026-02-06 17:26 ` [PATCH v4 5/9] PCI: dwc: Record integrated eDMA register window Koichiro Den
2026-02-06 17:26 ` [PATCH v4 6/9] PCI: dwc: ep: Report integrated eDMA resources via EPC remote-resource API Koichiro Den
2026-02-06 18:06   ` Frank Li
2026-02-07 15:59     ` Koichiro Den
2026-02-06 17:26 ` [PATCH v4 7/9] PCI: endpoint: pci-epf-test: Add embedded doorbell variant Koichiro Den
2026-02-06 19:20   ` Frank Li [this message]
2026-02-07 16:25     ` Koichiro Den
2026-02-08 12:35   ` kernel test robot
2026-02-06 17:26 ` [PATCH v4 8/9] misc: pci_endpoint_test: Allow selecting embedded doorbell Koichiro Den
2026-02-06 17:26 ` [PATCH v4 9/9] selftests: pci_endpoint: Exercise MSI and " Koichiro Den

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aYY_Ghig9wvnp8Wg@lizhi-Precision-Tower-5810 \
    --to=frank.li@nxp.com \
    --cc=bhelgaas@google.com \
    --cc=den@valinux.co.jp \
    --cc=dmaengine@vger.kernel.org \
    --cc=jingoohan1@gmail.com \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lpieralisi@kernel.org \
    --cc=mani@kernel.org \
    --cc=robh@kernel.org \
    --cc=vkoul@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®