Skip to content

Commit 87a194e

Browse files
committed
Merge branch 'pci/endpoint'
- Convert the endpoint doorbell test to use a threaded IRQ to fix a 'sleeping while atomic' issue (Bhanu Seshu Kumar Valluri) - Add endpoint VNTB MSI doorbell support to reduce latency between host and endpoint (Frank Li) * pci/endpoint: PCI: endpoint: pci-epf-vntb: Add MSI doorbell support PCI: endpoint: Add pci_epf_assign_bar_space() API PCI: endpoint: Add pci_epf_get_required_bar_size() helper PCI: endpoint: Rename 'epf_bar::aligned_size' to 'epf_bar:mem_size' PCI: endpoint: pci-epf-test: Fix sleeping function being called from atomic context
2 parents f26a75c + dc693d6 commit 87a194e

4 files changed

Lines changed: 275 additions & 54 deletions

File tree

drivers/pci/endpoint/functions/pci-epf-test.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,9 @@ static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test,
730730
if (bar < BAR_0)
731731
goto err_doorbell_cleanup;
732732

733-
ret = request_irq(epf->db_msg[0].virq, pci_epf_test_doorbell_handler, 0,
734-
"pci-ep-test-doorbell", epf_test);
733+
ret = request_threaded_irq(epf->db_msg[0].virq, NULL,
734+
pci_epf_test_doorbell_handler, IRQF_ONESHOT,
735+
"pci-ep-test-doorbell", epf_test);
735736
if (ret) {
736737
dev_err(&epf->dev,
737738
"Failed to request doorbell IRQ: %d\n",

drivers/pci/endpoint/functions/pci-epf-vntb.c

Lines changed: 136 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
* PCIe Root Port PCI EP
3737
*/
3838

39+
#include <linux/atomic.h>
3940
#include <linux/delay.h>
4041
#include <linux/io.h>
4142
#include <linux/module.h>
4243
#include <linux/slab.h>
4344

45+
#include <linux/pci-ep-msi.h>
4446
#include <linux/pci-epc.h>
4547
#include <linux/pci-epf.h>
4648
#include <linux/ntb.h>
@@ -126,12 +128,13 @@ struct epf_ntb {
126128
u32 db_count;
127129
u32 spad_count;
128130
u64 mws_size[MAX_MW];
129-
u64 db;
131+
atomic64_t db;
130132
u32 vbus_number;
131133
u16 vntb_pid;
132134
u16 vntb_vid;
133135

134136
bool linkup;
137+
bool msi_doorbell;
135138
u32 spad_size;
136139

137140
enum pci_barno epf_ntb_bar[VNTB_BAR_NUM];
@@ -258,9 +261,9 @@ static void epf_ntb_cmd_handler(struct work_struct *work)
258261

259262
ntb = container_of(work, struct epf_ntb, cmd_handler.work);
260263

261-
for (i = 1; i < ntb->db_count; i++) {
264+
for (i = 1; i < ntb->db_count && !ntb->msi_doorbell; i++) {
262265
if (ntb->epf_db[i]) {
263-
ntb->db |= 1 << (i - 1);
266+
atomic64_or(1 << (i - 1), &ntb->db);
264267
ntb_db_event(&ntb->ntb, i);
265268
ntb->epf_db[i] = 0;
266269
}
@@ -319,7 +322,21 @@ static void epf_ntb_cmd_handler(struct work_struct *work)
319322

320323
reset_handler:
321324
queue_delayed_work(kpcintb_workqueue, &ntb->cmd_handler,
322-
msecs_to_jiffies(5));
325+
ntb->msi_doorbell ? msecs_to_jiffies(500) : msecs_to_jiffies(5));
326+
}
327+
328+
static irqreturn_t epf_ntb_doorbell_handler(int irq, void *data)
329+
{
330+
struct epf_ntb *ntb = data;
331+
int i;
332+
333+
for (i = 1; i < ntb->db_count; i++)
334+
if (irq == ntb->epf->db_msg[i].virq) {
335+
atomic64_or(1 << (i - 1), &ntb->db);
336+
ntb_db_event(&ntb->ntb, i);
337+
}
338+
339+
return IRQ_HANDLED;
323340
}
324341

325342
/**
@@ -500,6 +517,94 @@ static int epf_ntb_configure_interrupt(struct epf_ntb *ntb)
500517
return 0;
501518
}
502519

520+
static int epf_ntb_db_bar_init_msi_doorbell(struct epf_ntb *ntb,
521+
struct pci_epf_bar *db_bar,
522+
const struct pci_epc_features *epc_features,
523+
enum pci_barno barno)
524+
{
525+
struct pci_epf *epf = ntb->epf;
526+
dma_addr_t low, high;
527+
struct msi_msg *msg;
528+
size_t sz;
529+
int ret;
530+
int i;
531+
532+
ret = pci_epf_alloc_doorbell(epf, ntb->db_count);
533+
if (ret)
534+
return ret;
535+
536+
for (i = 0; i < ntb->db_count; i++) {
537+
ret = request_irq(epf->db_msg[i].virq, epf_ntb_doorbell_handler,
538+
0, "pci_epf_vntb_db", ntb);
539+
540+
if (ret) {
541+
dev_err(&epf->dev,
542+
"Failed to request doorbell IRQ: %d\n",
543+
epf->db_msg[i].virq);
544+
goto err_free_irq;
545+
}
546+
}
547+
548+
msg = &epf->db_msg[0].msg;
549+
550+
high = 0;
551+
low = (u64)msg->address_hi << 32 | msg->address_lo;
552+
553+
for (i = 0; i < ntb->db_count; i++) {
554+
struct msi_msg *msg = &epf->db_msg[i].msg;
555+
dma_addr_t addr = (u64)msg->address_hi << 32 | msg->address_lo;
556+
557+
low = min(low, addr);
558+
high = max(high, addr);
559+
}
560+
561+
sz = high - low + sizeof(u32);
562+
563+
ret = pci_epf_assign_bar_space(epf, sz, barno, epc_features, 0, low);
564+
if (ret) {
565+
dev_err(&epf->dev, "Failed to assign Doorbell BAR space\n");
566+
goto err_free_irq;
567+
}
568+
569+
ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no,
570+
ntb->epf->vfunc_no, db_bar);
571+
if (ret) {
572+
dev_err(&epf->dev, "Failed to set Doorbell BAR\n");
573+
goto err_free_irq;
574+
}
575+
576+
for (i = 0; i < ntb->db_count; i++) {
577+
struct msi_msg *msg = &epf->db_msg[i].msg;
578+
dma_addr_t addr;
579+
size_t offset;
580+
581+
ret = pci_epf_align_inbound_addr(epf, db_bar->barno,
582+
((u64)msg->address_hi << 32) | msg->address_lo,
583+
&addr, &offset);
584+
585+
if (ret) {
586+
ntb->msi_doorbell = false;
587+
goto err_free_irq;
588+
}
589+
590+
ntb->reg->db_data[i] = msg->data;
591+
ntb->reg->db_offset[i] = offset;
592+
}
593+
594+
ntb->reg->db_entry_size = 0;
595+
596+
ntb->msi_doorbell = true;
597+
598+
return 0;
599+
600+
err_free_irq:
601+
for (i--; i >= 0; i--)
602+
free_irq(epf->db_msg[i].virq, ntb);
603+
604+
pci_epf_free_doorbell(ntb->epf);
605+
return ret;
606+
}
607+
503608
/**
504609
* epf_ntb_db_bar_init() - Configure Doorbell window BARs
505610
* @ntb: NTB device that facilitates communication between HOST and VHOST
@@ -520,21 +625,25 @@ static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
520625
ntb->epf->func_no,
521626
ntb->epf->vfunc_no);
522627
barno = ntb->epf_ntb_bar[BAR_DB];
523-
524-
mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, epc_features, 0);
525-
if (!mw_addr) {
526-
dev_err(dev, "Failed to allocate OB address\n");
527-
return -ENOMEM;
528-
}
529-
530-
ntb->epf_db = mw_addr;
531-
532628
epf_bar = &ntb->epf->bar[barno];
533629

534-
ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no, epf_bar);
630+
ret = epf_ntb_db_bar_init_msi_doorbell(ntb, epf_bar, epc_features, barno);
535631
if (ret) {
536-
dev_err(dev, "Doorbell BAR set failed\n");
632+
/* fall back to polling mode */
633+
mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, epc_features, 0);
634+
if (!mw_addr) {
635+
dev_err(dev, "Failed to allocate OB address\n");
636+
return -ENOMEM;
637+
}
638+
639+
ntb->epf_db = mw_addr;
640+
641+
ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no,
642+
ntb->epf->vfunc_no, epf_bar);
643+
if (ret) {
644+
dev_err(dev, "Doorbell BAR set failed\n");
537645
goto err_alloc_peer_mem;
646+
}
538647
}
539648
return ret;
540649

@@ -554,6 +663,16 @@ static void epf_ntb_db_bar_clear(struct epf_ntb *ntb)
554663
{
555664
enum pci_barno barno;
556665

666+
if (ntb->msi_doorbell) {
667+
int i;
668+
669+
for (i = 0; i < ntb->db_count; i++)
670+
free_irq(ntb->epf->db_msg[i].virq, ntb);
671+
}
672+
673+
if (ntb->epf->db_msg)
674+
pci_epf_free_doorbell(ntb->epf);
675+
557676
barno = ntb->epf_ntb_bar[BAR_DB];
558677
pci_epf_free_space(ntb->epf, ntb->epf_db, barno, 0);
559678
pci_epc_clear_bar(ntb->epf->epc,
@@ -1268,7 +1387,7 @@ static u64 vntb_epf_db_read(struct ntb_dev *ndev)
12681387
{
12691388
struct epf_ntb *ntb = ntb_ndev(ndev);
12701389

1271-
return ntb->db;
1390+
return atomic64_read(&ntb->db);
12721391
}
12731392

12741393
static int vntb_epf_mw_get_align(struct ntb_dev *ndev, int pidx, int idx,
@@ -1308,7 +1427,7 @@ static int vntb_epf_db_clear(struct ntb_dev *ndev, u64 db_bits)
13081427
{
13091428
struct epf_ntb *ntb = ntb_ndev(ndev);
13101429

1311-
ntb->db &= ~db_bits;
1430+
atomic64_and(~db_bits, &ntb->db);
13121431
return 0;
13131432
}
13141433

0 commit comments

Comments
 (0)