Merge branch 'lorenzo/pci/endpoint'

* lorenzo/pci/endpoint:
  misc: pci_endpoint_test: Handle 64-bit BARs properly
  PCI: designware-ep: Make dw_pcie_ep_reset_bar() handle 64-bit BARs properly
  PCI: endpoint: Make sure that BAR_5 does not have 64-bit flag set when clearing
  PCI: endpoint: Make epc->ops->clear_bar()/pci_epc_clear_bar() take struct *epf_bar
  PCI: endpoint: Handle 64-bit BARs properly
  PCI: cadence: Set PCI_BASE_ADDRESS_MEM_TYPE_64 if a 64-bit BAR was set-up
  PCI: designware-ep: Make dw_pcie_ep_set_bar() handle 64-bit BARs properly
  PCI: endpoint: Setting a BAR size > 4 GB is invalid if 64-bit flag is not set
  PCI: endpoint: Setting 64-bit/prefetch bit is invalid when IO is set
  PCI: endpoint: Setting BAR_5 to 64-bits wide is invalid
  PCI: endpoint: Simplify epc->ops->set_bar()/pci_epc_set_bar()
  PCI: endpoint: BAR width should not depend on sizeof dma_addr_t
  PCI: endpoint: Remove goto labels in pci_epf_create()
  PCI: endpoint: Fix kernel panic after put_device()
  PCI: endpoint: Simplify name allocation for EPF device
This commit is contained in:
Bjorn Helgaas 2018-04-04 13:28:47 -05:00 committed by Bjorn Helgaas
commit 14d8d776ae
8 changed files with 108 additions and 80 deletions

View File

@ -534,12 +534,14 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
} }
for (bar = BAR_0; bar <= BAR_5; bar++) { for (bar = BAR_0; bar <= BAR_5; bar++) {
base = pci_ioremap_bar(pdev, bar); if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
if (!base) { base = pci_ioremap_bar(pdev, bar);
dev_err(dev, "failed to read BAR%d\n", bar); if (!base) {
WARN_ON(bar == test_reg_bar); dev_err(dev, "failed to read BAR%d\n", bar);
WARN_ON(bar == test_reg_bar);
}
test->bar[bar] = base;
} }
test->bar[bar] = base;
} }
test->base = test->bar[test_reg_bar]; test->base = test->bar[test_reg_bar];

View File

@ -77,16 +77,19 @@ static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn,
return 0; return 0;
} }
static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn, enum pci_barno bar, static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn,
dma_addr_t bar_phys, size_t size, int flags) struct pci_epf_bar *epf_bar)
{ {
struct cdns_pcie_ep *ep = epc_get_drvdata(epc); struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
struct cdns_pcie *pcie = &ep->pcie; struct cdns_pcie *pcie = &ep->pcie;
dma_addr_t bar_phys = epf_bar->phys_addr;
enum pci_barno bar = epf_bar->barno;
int flags = epf_bar->flags;
u32 addr0, addr1, reg, cfg, b, aperture, ctrl; u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
u64 sz; u64 sz;
/* BAR size is 2^(aperture + 7) */ /* BAR size is 2^(aperture + 7) */
sz = max_t(size_t, size, CDNS_PCIE_EP_MIN_APERTURE); sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE);
/* /*
* roundup_pow_of_two() returns an unsigned long, which is not suited * roundup_pow_of_two() returns an unsigned long, which is not suited
* for 64bit values. * for 64bit values.
@ -103,6 +106,9 @@ static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn, enum pci_barno bar,
if (is_64bits && (bar & 1)) if (is_64bits && (bar & 1))
return -EINVAL; return -EINVAL;
if (is_64bits && !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
if (is_64bits && is_prefetch) if (is_64bits && is_prefetch)
ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS; ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
else if (is_prefetch) else if (is_prefetch)
@ -139,10 +145,11 @@ static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn, enum pci_barno bar,
} }
static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn, static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn,
enum pci_barno bar) struct pci_epf_bar *epf_bar)
{ {
struct cdns_pcie_ep *ep = epc_get_drvdata(epc); struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
struct cdns_pcie *pcie = &ep->pcie; struct cdns_pcie *pcie = &ep->pcie;
enum pci_barno bar = epf_bar->barno;
u32 reg, cfg, b, ctrl; u32 reg, cfg, b, ctrl;
if (bar < BAR_4) { if (bar < BAR_4) {

View File

@ -19,7 +19,8 @@ void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
pci_epc_linkup(epc); pci_epc_linkup(epc);
} }
void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar) static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar,
int flags)
{ {
u32 reg; u32 reg;
@ -27,9 +28,18 @@ void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
dw_pcie_dbi_ro_wr_en(pci); dw_pcie_dbi_ro_wr_en(pci);
dw_pcie_writel_dbi2(pci, reg, 0x0); dw_pcie_writel_dbi2(pci, reg, 0x0);
dw_pcie_writel_dbi(pci, reg, 0x0); dw_pcie_writel_dbi(pci, reg, 0x0);
if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
dw_pcie_writel_dbi2(pci, reg + 4, 0x0);
dw_pcie_writel_dbi(pci, reg + 4, 0x0);
}
dw_pcie_dbi_ro_wr_dis(pci); dw_pcie_dbi_ro_wr_dis(pci);
} }
void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
{
__dw_pcie_ep_reset_bar(pci, bar, 0);
}
static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no,
struct pci_epf_header *hdr) struct pci_epf_header *hdr)
{ {
@ -104,25 +114,28 @@ static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, phys_addr_t phys_addr,
} }
static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no,
enum pci_barno bar) struct pci_epf_bar *epf_bar)
{ {
struct dw_pcie_ep *ep = epc_get_drvdata(epc); struct dw_pcie_ep *ep = epc_get_drvdata(epc);
struct dw_pcie *pci = to_dw_pcie_from_ep(ep); struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
enum pci_barno bar = epf_bar->barno;
u32 atu_index = ep->bar_to_atu[bar]; u32 atu_index = ep->bar_to_atu[bar];
dw_pcie_ep_reset_bar(pci, bar); __dw_pcie_ep_reset_bar(pci, bar, epf_bar->flags);
dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND); dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
clear_bit(atu_index, ep->ib_window_map); clear_bit(atu_index, ep->ib_window_map);
} }
static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no,
enum pci_barno bar, struct pci_epf_bar *epf_bar)
dma_addr_t bar_phys, size_t size, int flags)
{ {
int ret; int ret;
struct dw_pcie_ep *ep = epc_get_drvdata(epc); struct dw_pcie_ep *ep = epc_get_drvdata(epc);
struct dw_pcie *pci = to_dw_pcie_from_ep(ep); struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
enum pci_barno bar = epf_bar->barno;
size_t size = epf_bar->size;
int flags = epf_bar->flags;
enum dw_pcie_as_type as_type; enum dw_pcie_as_type as_type;
u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar); u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
@ -131,13 +144,20 @@ static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no,
else else
as_type = DW_PCIE_AS_IO; as_type = DW_PCIE_AS_IO;
ret = dw_pcie_ep_inbound_atu(ep, bar, bar_phys, as_type); ret = dw_pcie_ep_inbound_atu(ep, bar, epf_bar->phys_addr, as_type);
if (ret) if (ret)
return ret; return ret;
dw_pcie_dbi_ro_wr_en(pci); dw_pcie_dbi_ro_wr_en(pci);
dw_pcie_writel_dbi2(pci, reg, size - 1);
dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
dw_pcie_writel_dbi(pci, reg, flags); dw_pcie_writel_dbi(pci, reg, flags);
if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
dw_pcie_writel_dbi(pci, reg + 4, 0);
}
dw_pcie_dbi_ro_wr_dis(pci); dw_pcie_dbi_ro_wr_dis(pci);
return 0; return 0;

View File

@ -70,7 +70,7 @@ struct pci_epf_test_data {
bool linkup_notifier; bool linkup_notifier;
}; };
static int bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 }; static size_t bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 };
static int pci_epf_test_copy(struct pci_epf_test *epf_test) static int pci_epf_test_copy(struct pci_epf_test *epf_test)
{ {
@ -344,21 +344,23 @@ static void pci_epf_test_unbind(struct pci_epf *epf)
{ {
struct pci_epf_test *epf_test = epf_get_drvdata(epf); struct pci_epf_test *epf_test = epf_get_drvdata(epf);
struct pci_epc *epc = epf->epc; struct pci_epc *epc = epf->epc;
struct pci_epf_bar *epf_bar;
int bar; int bar;
cancel_delayed_work(&epf_test->cmd_handler); cancel_delayed_work(&epf_test->cmd_handler);
pci_epc_stop(epc); pci_epc_stop(epc);
for (bar = BAR_0; bar <= BAR_5; bar++) { for (bar = BAR_0; bar <= BAR_5; bar++) {
epf_bar = &epf->bar[bar];
if (epf_test->reg[bar]) { if (epf_test->reg[bar]) {
pci_epf_free_space(epf, epf_test->reg[bar], bar); pci_epf_free_space(epf, epf_test->reg[bar], bar);
pci_epc_clear_bar(epc, epf->func_no, bar); pci_epc_clear_bar(epc, epf->func_no, epf_bar);
} }
} }
} }
static int pci_epf_test_set_bar(struct pci_epf *epf) static int pci_epf_test_set_bar(struct pci_epf *epf)
{ {
int flags;
int bar; int bar;
int ret; int ret;
struct pci_epf_bar *epf_bar; struct pci_epf_bar *epf_bar;
@ -367,21 +369,27 @@ static int pci_epf_test_set_bar(struct pci_epf *epf)
struct pci_epf_test *epf_test = epf_get_drvdata(epf); struct pci_epf_test *epf_test = epf_get_drvdata(epf);
enum pci_barno test_reg_bar = epf_test->test_reg_bar; enum pci_barno test_reg_bar = epf_test->test_reg_bar;
flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32;
if (sizeof(dma_addr_t) == 0x8)
flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
for (bar = BAR_0; bar <= BAR_5; bar++) { for (bar = BAR_0; bar <= BAR_5; bar++) {
epf_bar = &epf->bar[bar]; epf_bar = &epf->bar[bar];
ret = pci_epc_set_bar(epc, epf->func_no, bar,
epf_bar->phys_addr, epf_bar->flags |= upper_32_bits(epf_bar->size) ?
epf_bar->size, flags); PCI_BASE_ADDRESS_MEM_TYPE_64 :
PCI_BASE_ADDRESS_MEM_TYPE_32;
ret = pci_epc_set_bar(epc, epf->func_no, epf_bar);
if (ret) { if (ret) {
pci_epf_free_space(epf, epf_test->reg[bar], bar); pci_epf_free_space(epf, epf_test->reg[bar], bar);
dev_err(dev, "failed to set BAR%d\n", bar); dev_err(dev, "failed to set BAR%d\n", bar);
if (bar == test_reg_bar) if (bar == test_reg_bar)
return ret; return ret;
} }
/*
* pci_epc_set_bar() sets PCI_BASE_ADDRESS_MEM_TYPE_64
* if the specific implementation required a 64-bit BAR,
* even if we only requested a 32-bit BAR.
*/
if (epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
bar++;
} }
return 0; return 0;

View File

@ -276,22 +276,25 @@ EXPORT_SYMBOL_GPL(pci_epc_map_addr);
* pci_epc_clear_bar() - reset the BAR * pci_epc_clear_bar() - reset the BAR
* @epc: the EPC device for which the BAR has to be cleared * @epc: the EPC device for which the BAR has to be cleared
* @func_no: the endpoint function number in the EPC device * @func_no: the endpoint function number in the EPC device
* @bar: the BAR number that has to be reset * @epf_bar: the struct epf_bar that contains the BAR information
* *
* Invoke to reset the BAR of the endpoint device. * Invoke to reset the BAR of the endpoint device.
*/ */
void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, int bar) void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
struct pci_epf_bar *epf_bar)
{ {
unsigned long flags; unsigned long flags;
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
(epf_bar->barno == BAR_5 &&
epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
return; return;
if (!epc->ops->clear_bar) if (!epc->ops->clear_bar)
return; return;
spin_lock_irqsave(&epc->lock, flags); spin_lock_irqsave(&epc->lock, flags);
epc->ops->clear_bar(epc, func_no, bar); epc->ops->clear_bar(epc, func_no, epf_bar);
spin_unlock_irqrestore(&epc->lock, flags); spin_unlock_irqrestore(&epc->lock, flags);
} }
EXPORT_SYMBOL_GPL(pci_epc_clear_bar); EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
@ -300,26 +303,31 @@ EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
* pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
* @epc: the EPC device on which BAR has to be configured * @epc: the EPC device on which BAR has to be configured
* @func_no: the endpoint function number in the EPC device * @func_no: the endpoint function number in the EPC device
* @bar: the BAR number that has to be configured * @epf_bar: the struct epf_bar that contains the BAR information
* @size: the size of the addr space
* @flags: specify memory allocation/io allocation/32bit address/64 bit address
* *
* Invoke to configure the BAR of the endpoint device. * Invoke to configure the BAR of the endpoint device.
*/ */
int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, enum pci_barno bar, int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
dma_addr_t bar_phys, size_t size, int flags) struct pci_epf_bar *epf_bar)
{ {
int ret; int ret;
unsigned long irq_flags; unsigned long irq_flags;
int flags = epf_bar->flags;
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
(epf_bar->barno == BAR_5 &&
flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
(flags & PCI_BASE_ADDRESS_SPACE_IO &&
flags & PCI_BASE_ADDRESS_IO_MASK) ||
(upper_32_bits(epf_bar->size) &&
!(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
return -EINVAL; return -EINVAL;
if (!epc->ops->set_bar) if (!epc->ops->set_bar)
return 0; return 0;
spin_lock_irqsave(&epc->lock, irq_flags); spin_lock_irqsave(&epc->lock, irq_flags);
ret = epc->ops->set_bar(epc, func_no, bar, bar_phys, size, flags); ret = epc->ops->set_bar(epc, func_no, epf_bar);
spin_unlock_irqrestore(&epc->lock, irq_flags); spin_unlock_irqrestore(&epc->lock, irq_flags);
return ret; return ret;

View File

@ -98,6 +98,8 @@ void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
epf->bar[bar].phys_addr = 0; epf->bar[bar].phys_addr = 0;
epf->bar[bar].size = 0; epf->bar[bar].size = 0;
epf->bar[bar].barno = 0;
epf->bar[bar].flags = 0;
} }
EXPORT_SYMBOL_GPL(pci_epf_free_space); EXPORT_SYMBOL_GPL(pci_epf_free_space);
@ -126,6 +128,8 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar)
epf->bar[bar].phys_addr = phys_addr; epf->bar[bar].phys_addr = phys_addr;
epf->bar[bar].size = size; epf->bar[bar].size = size;
epf->bar[bar].barno = bar;
epf->bar[bar].flags = PCI_BASE_ADDRESS_SPACE_MEMORY;
return space; return space;
} }
@ -200,29 +204,17 @@ struct pci_epf *pci_epf_create(const char *name)
int ret; int ret;
struct pci_epf *epf; struct pci_epf *epf;
struct device *dev; struct device *dev;
char *func_name; int len;
char *buf;
epf = kzalloc(sizeof(*epf), GFP_KERNEL); epf = kzalloc(sizeof(*epf), GFP_KERNEL);
if (!epf) { if (!epf)
ret = -ENOMEM; return ERR_PTR(-ENOMEM);
goto err_ret;
}
buf = kstrdup(name, GFP_KERNEL); len = strchrnul(name, '.') - name;
if (!buf) { epf->name = kstrndup(name, len, GFP_KERNEL);
ret = -ENOMEM;
goto free_epf;
}
func_name = buf;
buf = strchrnul(buf, '.');
*buf = '\0';
epf->name = kstrdup(func_name, GFP_KERNEL);
if (!epf->name) { if (!epf->name) {
ret = -ENOMEM; kfree(epf);
goto free_func_name; return ERR_PTR(-ENOMEM);
} }
dev = &epf->dev; dev = &epf->dev;
@ -231,28 +223,18 @@ struct pci_epf *pci_epf_create(const char *name)
dev->type = &pci_epf_type; dev->type = &pci_epf_type;
ret = dev_set_name(dev, "%s", name); ret = dev_set_name(dev, "%s", name);
if (ret) if (ret) {
goto put_dev; put_device(dev);
return ERR_PTR(ret);
}
ret = device_add(dev); ret = device_add(dev);
if (ret) if (ret) {
goto put_dev; put_device(dev);
return ERR_PTR(ret);
}
kfree(func_name);
return epf; return epf;
put_dev:
put_device(dev);
kfree(epf->name);
free_func_name:
kfree(func_name);
free_epf:
kfree(epf);
err_ret:
return ERR_PTR(ret);
} }
EXPORT_SYMBOL_GPL(pci_epf_create); EXPORT_SYMBOL_GPL(pci_epf_create);

View File

@ -39,10 +39,9 @@ struct pci_epc_ops {
int (*write_header)(struct pci_epc *epc, u8 func_no, int (*write_header)(struct pci_epc *epc, u8 func_no,
struct pci_epf_header *hdr); struct pci_epf_header *hdr);
int (*set_bar)(struct pci_epc *epc, u8 func_no, int (*set_bar)(struct pci_epc *epc, u8 func_no,
enum pci_barno bar, struct pci_epf_bar *epf_bar);
dma_addr_t bar_phys, size_t size, int flags);
void (*clear_bar)(struct pci_epc *epc, u8 func_no, void (*clear_bar)(struct pci_epc *epc, u8 func_no,
enum pci_barno bar); struct pci_epf_bar *epf_bar);
int (*map_addr)(struct pci_epc *epc, u8 func_no, int (*map_addr)(struct pci_epc *epc, u8 func_no,
phys_addr_t addr, u64 pci_addr, size_t size); phys_addr_t addr, u64 pci_addr, size_t size);
void (*unmap_addr)(struct pci_epc *epc, u8 func_no, void (*unmap_addr)(struct pci_epc *epc, u8 func_no,
@ -127,9 +126,9 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf);
int pci_epc_write_header(struct pci_epc *epc, u8 func_no, int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
struct pci_epf_header *hdr); struct pci_epf_header *hdr);
int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
enum pci_barno bar, struct pci_epf_bar *epf_bar);
dma_addr_t bar_phys, size_t size, int flags); void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, int bar); struct pci_epf_bar *epf_bar);
int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
phys_addr_t phys_addr, phys_addr_t phys_addr,
u64 pci_addr, size_t size); u64 pci_addr, size_t size);

View File

@ -97,6 +97,8 @@ struct pci_epf_driver {
struct pci_epf_bar { struct pci_epf_bar {
dma_addr_t phys_addr; dma_addr_t phys_addr;
size_t size; size_t size;
enum pci_barno barno;
int flags;
}; };
/** /**