Skip to content

Commit e19a0ad

Browse files
kishonLorenzo Pieralisi
authored andcommitted
PCI: cadence: Add support to configure virtual functions
Now that support for SR-IOV is added in PCIe endpoint core, add support to configure virtual functions in the Cadence PCIe EP driver. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kishon Vijay Abraham I <[email protected]> Signed-off-by: Lorenzo Pieralisi <[email protected]>
1 parent 0cf985d commit e19a0ad

File tree

2 files changed

+128
-21
lines changed

2 files changed

+128
-21
lines changed

drivers/pci/controller/cadence/pcie-cadence-ep.c

Lines changed: 119 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,37 @@
1616
#define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
1717
#define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
1818

19+
static u8 cdns_pcie_get_fn_from_vfn(struct cdns_pcie *pcie, u8 fn, u8 vfn)
20+
{
21+
u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
22+
u32 first_vf_offset, stride;
23+
24+
if (vfn == 0)
25+
return fn;
26+
27+
first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_SRIOV_VF_OFFSET);
28+
stride = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_SRIOV_VF_STRIDE);
29+
fn = fn + first_vf_offset + ((vfn - 1) * stride);
30+
31+
return fn;
32+
}
33+
1934
static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn, u8 vfn,
2035
struct pci_epf_header *hdr)
2136
{
2237
struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
38+
u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
2339
struct cdns_pcie *pcie = &ep->pcie;
40+
u32 reg;
41+
42+
if (vfn > 1) {
43+
dev_err(&epc->dev, "Only Virtual Function #1 has deviceID\n");
44+
return -EINVAL;
45+
} else if (vfn == 1) {
46+
reg = cap + PCI_SRIOV_VF_DID;
47+
cdns_pcie_ep_fn_writew(pcie, fn, reg, hdr->deviceid);
48+
return 0;
49+
}
2450

2551
cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
2652
cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
@@ -92,21 +118,30 @@ static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn, u8 vfn,
92118

93119
addr0 = lower_32_bits(bar_phys);
94120
addr1 = upper_32_bits(bar_phys);
121+
122+
if (vfn == 1)
123+
reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG(bar, fn);
124+
else
125+
reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
126+
b = (bar < BAR_4) ? bar : bar - BAR_4;
127+
128+
if (vfn == 0 || vfn == 1) {
129+
cfg = cdns_pcie_readl(pcie, reg);
130+
cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
131+
CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
132+
cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
133+
CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
134+
cdns_pcie_writel(pcie, reg, cfg);
135+
}
136+
137+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
95138
cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
96139
addr0);
97140
cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
98141
addr1);
99142

100-
reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
101-
b = (bar < BAR_4) ? bar : bar - BAR_4;
102-
103-
cfg = cdns_pcie_readl(pcie, reg);
104-
cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
105-
CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
106-
cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
107-
CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
108-
cdns_pcie_writel(pcie, reg, cfg);
109-
143+
if (vfn > 0)
144+
epf = &epf->epf[vfn - 1];
110145
epf->epf_bar[bar] = epf_bar;
111146

112147
return 0;
@@ -121,19 +156,27 @@ static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn, u8 vfn,
121156
enum pci_barno bar = epf_bar->barno;
122157
u32 reg, cfg, b, ctrl;
123158

124-
reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
159+
if (vfn == 1)
160+
reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG(bar, fn);
161+
else
162+
reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG(bar, fn);
125163
b = (bar < BAR_4) ? bar : bar - BAR_4;
126164

127-
ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
128-
cfg = cdns_pcie_readl(pcie, reg);
129-
cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
130-
CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
131-
cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
132-
cdns_pcie_writel(pcie, reg, cfg);
165+
if (vfn == 0 || vfn == 1) {
166+
ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
167+
cfg = cdns_pcie_readl(pcie, reg);
168+
cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
169+
CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
170+
cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
171+
cdns_pcie_writel(pcie, reg, cfg);
172+
}
133173

174+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
134175
cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
135176
cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);
136177

178+
if (vfn > 0)
179+
epf = &epf->epf[vfn - 1];
137180
epf->epf_bar[bar] = NULL;
138181
}
139182

@@ -151,6 +194,7 @@ static int cdns_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, u8 vfn,
151194
return -EINVAL;
152195
}
153196

197+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
154198
cdns_pcie_set_outbound_region(pcie, 0, fn, r, false, addr, pci_addr, size);
155199

156200
set_bit(r, &ep->ob_region_map);
@@ -186,6 +230,8 @@ static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 vfn, u8 mmc)
186230
u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
187231
u16 flags;
188232

233+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
234+
189235
/*
190236
* Set the Multiple Message Capable bitfield into the Message Control
191237
* register.
@@ -206,6 +252,8 @@ static int cdns_pcie_ep_get_msi(struct pci_epc *epc, u8 fn, u8 vfn)
206252
u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
207253
u16 flags, mme;
208254

255+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
256+
209257
/* Validate that the MSI feature is actually enabled. */
210258
flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
211259
if (!(flags & PCI_MSI_FLAGS_ENABLE))
@@ -227,6 +275,8 @@ static int cdns_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
227275
u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
228276
u32 val, reg;
229277

278+
func_no = cdns_pcie_get_fn_from_vfn(pcie, func_no, vfunc_no);
279+
230280
reg = cap + PCI_MSIX_FLAGS;
231281
val = cdns_pcie_ep_fn_readw(pcie, func_no, reg);
232282
if (!(val & PCI_MSIX_FLAGS_ENABLE))
@@ -246,6 +296,8 @@ static int cdns_pcie_ep_set_msix(struct pci_epc *epc, u8 fn, u8 vfn,
246296
u32 cap = CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET;
247297
u32 val, reg;
248298

299+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
300+
249301
reg = cap + PCI_MSIX_FLAGS;
250302
val = cdns_pcie_ep_fn_readw(pcie, fn, reg);
251303
val &= ~PCI_MSIX_FLAGS_QSIZE;
@@ -265,8 +317,8 @@ static int cdns_pcie_ep_set_msix(struct pci_epc *epc, u8 fn, u8 vfn,
265317
return 0;
266318
}
267319

268-
static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
269-
u8 intx, bool is_asserted)
320+
static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn, u8 intx,
321+
bool is_asserted)
270322
{
271323
struct cdns_pcie *pcie = &ep->pcie;
272324
unsigned long flags;
@@ -335,6 +387,8 @@ static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
335387
u8 msi_count;
336388
u64 pci_addr, pci_addr_mask = 0xff;
337389

390+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
391+
338392
/* Check whether the MSI feature has been enabled by the PCI host. */
339393
flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
340394
if (!(flags & PCI_MSI_FLAGS_ENABLE))
@@ -388,6 +442,8 @@ static int cdns_pcie_ep_map_msi_irq(struct pci_epc *epc, u8 fn, u8 vfn,
388442
int ret;
389443
int i;
390444

445+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
446+
391447
/* Check whether the MSI feature has been enabled by the PCI host. */
392448
flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
393449
if (!(flags & PCI_MSI_FLAGS_ENABLE))
@@ -438,6 +494,12 @@ static int cdns_pcie_ep_send_msix_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
438494
u16 flags;
439495
u8 bir;
440496

497+
epf = &ep->epf[fn];
498+
if (vfn > 0)
499+
epf = &epf->epf[vfn - 1];
500+
501+
fn = cdns_pcie_get_fn_from_vfn(pcie, fn, vfn);
502+
441503
/* Check whether the MSI-X feature has been enabled by the PCI host. */
442504
flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSIX_FLAGS);
443505
if (!(flags & PCI_MSIX_FLAGS_ENABLE))
@@ -448,7 +510,6 @@ static int cdns_pcie_ep_send_msix_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
448510
bir = tbl_offset & PCI_MSIX_TABLE_BIR;
449511
tbl_offset &= PCI_MSIX_TABLE_OFFSET;
450512

451-
epf = &ep->epf[fn];
452513
msix_tbl = epf->epf_bar[bir]->addr + tbl_offset;
453514
msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
454515
msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
@@ -475,9 +536,15 @@ static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn, u8 vfn,
475536
u16 interrupt_num)
476537
{
477538
struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
539+
struct cdns_pcie *pcie = &ep->pcie;
540+
struct device *dev = pcie->dev;
478541

479542
switch (type) {
480543
case PCI_EPC_IRQ_LEGACY:
544+
if (vfn > 0) {
545+
dev_err(dev, "Cannot raise legacy interrupts for VF\n");
546+
return -EINVAL;
547+
}
481548
return cdns_pcie_ep_send_legacy_irq(ep, fn, vfn, 0);
482549

483550
case PCI_EPC_IRQ_MSI:
@@ -515,6 +582,13 @@ static int cdns_pcie_ep_start(struct pci_epc *epc)
515582
return 0;
516583
}
517584

585+
static const struct pci_epc_features cdns_pcie_epc_vf_features = {
586+
.linkup_notifier = false,
587+
.msi_capable = true,
588+
.msix_capable = true,
589+
.align = 65536,
590+
};
591+
518592
static const struct pci_epc_features cdns_pcie_epc_features = {
519593
.linkup_notifier = false,
520594
.msi_capable = true,
@@ -525,7 +599,10 @@ static const struct pci_epc_features cdns_pcie_epc_features = {
525599
static const struct pci_epc_features*
526600
cdns_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
527601
{
528-
return &cdns_pcie_epc_features;
602+
if (!vfunc_no)
603+
return &cdns_pcie_epc_features;
604+
605+
return &cdns_pcie_epc_vf_features;
529606
}
530607

531608
static const struct pci_epc_ops cdns_pcie_epc_ops = {
@@ -551,9 +628,11 @@ int cdns_pcie_ep_setup(struct cdns_pcie_ep *ep)
551628
struct platform_device *pdev = to_platform_device(dev);
552629
struct device_node *np = dev->of_node;
553630
struct cdns_pcie *pcie = &ep->pcie;
631+
struct cdns_pcie_epf *epf;
554632
struct resource *res;
555633
struct pci_epc *epc;
556634
int ret;
635+
int i;
557636

558637
pcie->is_rc = false;
559638

@@ -598,6 +677,25 @@ int cdns_pcie_ep_setup(struct cdns_pcie_ep *ep)
598677
if (!ep->epf)
599678
return -ENOMEM;
600679

680+
epc->max_vfs = devm_kcalloc(dev, epc->max_functions,
681+
sizeof(*epc->max_vfs), GFP_KERNEL);
682+
if (!epc->max_vfs)
683+
return -ENOMEM;
684+
685+
ret = of_property_read_u8_array(np, "max-virtual-functions",
686+
epc->max_vfs, epc->max_functions);
687+
if (ret == 0) {
688+
for (i = 0; i < epc->max_functions; i++) {
689+
epf = &ep->epf[i];
690+
if (epc->max_vfs[i] == 0)
691+
continue;
692+
epf->epf = devm_kcalloc(dev, epc->max_vfs[i],
693+
sizeof(*ep->epf), GFP_KERNEL);
694+
if (!epf->epf)
695+
return -ENOMEM;
696+
}
697+
}
698+
601699
ret = pci_epc_mem_init(epc, pcie->mem_res->start,
602700
resource_size(pcie->mem_res), PAGE_SIZE);
603701
if (ret < 0) {

drivers/pci/controller/cadence/pcie-cadence.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
(CDNS_PCIE_LM_BASE + 0x0240 + (fn) * 0x0008)
5454
#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn) \
5555
(CDNS_PCIE_LM_BASE + 0x0244 + (fn) * 0x0008)
56+
#define CDNS_PCIE_LM_EP_VFUNC_BAR_CFG(bar, fn) \
57+
(((bar) < BAR_4) ? CDNS_PCIE_LM_EP_VFUNC_BAR_CFG0(fn) : CDNS_PCIE_LM_EP_VFUNC_BAR_CFG1(fn))
58+
#define CDNS_PCIE_LM_EP_VFUNC_BAR_CFG0(fn) \
59+
(CDNS_PCIE_LM_BASE + 0x0280 + (fn) * 0x0008)
60+
#define CDNS_PCIE_LM_EP_VFUNC_BAR_CFG1(fn) \
61+
(CDNS_PCIE_LM_BASE + 0x0284 + (fn) * 0x0008)
5662
#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) \
5763
(GENMASK(4, 0) << ((b) * 8))
5864
#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, a) \
@@ -117,6 +123,7 @@
117123

118124
#define CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET 0x90
119125
#define CDNS_PCIE_EP_FUNC_MSIX_CAP_OFFSET 0xb0
126+
#define CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET 0x200
120127

121128
/*
122129
* Root Port Registers (PCI configuration space for the root port function)
@@ -311,9 +318,11 @@ struct cdns_pcie_rc {
311318

312319
/**
313320
* struct cdns_pcie_epf - Structure to hold info about endpoint function
321+
* @epf: Info about virtual functions attached to the physical function
314322
* @epf_bar: reference to the pci_epf_bar for the six Base Address Registers
315323
*/
316324
struct cdns_pcie_epf {
325+
struct cdns_pcie_epf *epf;
317326
struct pci_epf_bar *epf_bar[PCI_STD_NUM_BARS];
318327
};
319328

0 commit comments

Comments
 (0)