2005-06-24 13:01:16 +08:00
|
|
|
/*
|
2006-10-04 05:01:26 +08:00
|
|
|
* arch/xtensa/kernel/pci.c
|
2005-06-24 13:01:16 +08:00
|
|
|
*
|
|
|
|
* PCI bios-type initialisation for PCI machines
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2005 Tensilica Inc.
|
|
|
|
*
|
|
|
|
* Based largely on work from Cort (ppc/kernel/pci.c)
|
|
|
|
* IO functions copied from sparc.
|
|
|
|
*
|
|
|
|
* Chris Zankel <chris@zankel.net>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/bootmem.h>
|
|
|
|
|
|
|
|
#include <asm/pci-bridge.h>
|
|
|
|
#include <asm/platform.h>
|
|
|
|
|
|
|
|
/* PCI Controller */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pcibios_alloc_controller
|
|
|
|
* pcibios_enable_device
|
|
|
|
* pcibios_fixups
|
|
|
|
* pcibios_align_resource
|
|
|
|
* pcibios_fixup_bus
|
|
|
|
* pci_bus_add_device
|
|
|
|
*/
|
|
|
|
|
2018-05-22 20:56:12 +08:00
|
|
|
static struct pci_controller *pci_ctrl_head;
|
|
|
|
static struct pci_controller **pci_ctrl_tail = &pci_ctrl_head;
|
2005-06-24 13:01:16 +08:00
|
|
|
|
|
|
|
static int pci_bus_count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to avoid collisions with `mirrored' VGA ports
|
|
|
|
* and other strange ISA hardware, so we always want the
|
|
|
|
* addresses to be allocated in the 0x000-0x0ff region
|
|
|
|
* modulo 0x400.
|
|
|
|
*
|
|
|
|
* Why? Because some silly external IO cards only decode
|
|
|
|
* the low 10 bits of the IO address. The 0x00-0xff region
|
|
|
|
* is reserved for motherboard devices that decode all 16
|
|
|
|
* bits, so it's ok to allocate at, say, 0x2800-0x28ff,
|
|
|
|
* but we want to try to avoid allocating at 0x2900-0x2bff
|
|
|
|
* which might have be mirrored at 0x0100-0x03ff..
|
|
|
|
*/
|
2010-01-02 00:40:49 +08:00
|
|
|
resource_size_t
|
2010-01-02 00:40:50 +08:00
|
|
|
pcibios_align_resource(void *data, const struct resource *res,
|
|
|
|
resource_size_t size, resource_size_t align)
|
2005-06-24 13:01:16 +08:00
|
|
|
{
|
|
|
|
struct pci_dev *dev = data;
|
2010-01-02 00:40:49 +08:00
|
|
|
resource_size_t start = res->start;
|
2005-06-24 13:01:16 +08:00
|
|
|
|
|
|
|
if (res->flags & IORESOURCE_IO) {
|
|
|
|
if (size > 0x100) {
|
2013-05-27 23:45:58 +08:00
|
|
|
pr_err("PCI: I/O Region %s/%d too large (%u bytes)\n",
|
|
|
|
pci_name(dev), dev->resource - res,
|
|
|
|
size);
|
2005-06-24 13:01:16 +08:00
|
|
|
}
|
|
|
|
|
2010-01-02 00:40:49 +08:00
|
|
|
if (start & 0x300)
|
2005-06-24 13:01:16 +08:00
|
|
|
start = (start + 0x3ff) & ~0x3ff;
|
|
|
|
}
|
2010-01-02 00:40:49 +08:00
|
|
|
|
|
|
|
return start;
|
2005-06-24 13:01:16 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 06:28:19 +08:00
|
|
|
static void __init pci_controller_apertures(struct pci_controller *pci_ctrl,
|
|
|
|
struct list_head *resources)
|
|
|
|
{
|
|
|
|
struct resource *res;
|
|
|
|
unsigned long io_offset;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
io_offset = (unsigned long)pci_ctrl->io_space.base;
|
|
|
|
res = &pci_ctrl->io_resource;
|
|
|
|
if (!res->flags) {
|
|
|
|
if (io_offset)
|
2017-12-16 04:00:30 +08:00
|
|
|
pr_err("I/O resource not set for host bridge %d\n",
|
|
|
|
pci_ctrl->index);
|
2011-10-29 06:28:19 +08:00
|
|
|
res->start = 0;
|
|
|
|
res->end = IO_SPACE_LIMIT;
|
|
|
|
res->flags = IORESOURCE_IO;
|
|
|
|
}
|
|
|
|
res->start += io_offset;
|
|
|
|
res->end += io_offset;
|
2012-02-24 11:19:04 +08:00
|
|
|
pci_add_resource_offset(resources, res, io_offset);
|
2011-10-29 06:28:19 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
res = &pci_ctrl->mem_resources[i];
|
|
|
|
if (!res->flags) {
|
|
|
|
if (i > 0)
|
|
|
|
continue;
|
2017-12-16 04:00:30 +08:00
|
|
|
pr_err("Memory resource not set for host bridge %d\n",
|
|
|
|
pci_ctrl->index);
|
2011-10-29 06:28:19 +08:00
|
|
|
res->start = 0;
|
|
|
|
res->end = ~0U;
|
|
|
|
res->flags = IORESOURCE_MEM;
|
|
|
|
}
|
|
|
|
pci_add_resource(resources, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 13:01:16 +08:00
|
|
|
static int __init pcibios_init(void)
|
|
|
|
{
|
|
|
|
struct pci_controller *pci_ctrl;
|
2011-10-29 06:28:19 +08:00
|
|
|
struct list_head resources;
|
2005-06-24 13:01:16 +08:00
|
|
|
struct pci_bus *bus;
|
PCI: Assign resources before drivers claim devices (pci_scan_root_bus())
Previously, pci_scan_root_bus() created a root PCI bus, enumerated the
devices on it, and called pci_bus_add_devices(), which made the devices
available for drivers to claim them.
Most callers assigned resources to devices after pci_scan_root_bus()
returns, which may be after drivers have claimed the devices. This is
incorrect; the PCI core should not change device resources while a driver
is managing the device.
Remove pci_bus_add_devices() from pci_scan_root_bus() and do it after any
resource assignment in the callers.
Note that ARM's pci_common_init_dev() already called pci_bus_add_devices()
after pci_scan_root_bus(), so we only need to remove the first call:
pci_common_init_dev
pcibios_init_hw
pci_scan_root_bus
pci_bus_add_devices # first call
pci_bus_assign_resources
pci_bus_add_devices # second call
[bhelgaas: changelog, drop "root_bus" var in alpha common_init_pci(),
return failure earlier in mn10300, add "return" in x86 pcibios_scan_root(),
return early if xtensa platform_pcibios_fixup() fails]
Signed-off-by: Yijing Wang <wangyijing@huawei.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
CC: Richard Henderson <rth@twiddle.net>
CC: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
CC: Matt Turner <mattst88@gmail.com>
CC: David Howells <dhowells@redhat.com>
CC: Tony Luck <tony.luck@intel.com>
CC: Michal Simek <monstr@monstr.eu>
CC: Ralf Baechle <ralf@linux-mips.org>
CC: Koichi Yasutake <yasutake.koichi@jp.panasonic.com>
CC: Sebastian Ott <sebott@linux.vnet.ibm.com>
CC: "David S. Miller" <davem@davemloft.net>
CC: Chris Metcalf <cmetcalf@ezchip.com>
CC: Chris Zankel <chris@zankel.net>
CC: Max Filippov <jcmvbkbc@gmail.com>
CC: Thomas Gleixner <tglx@linutronix.de>
2015-03-16 11:18:56 +08:00
|
|
|
int next_busno = 0, ret;
|
2005-06-24 13:01:16 +08:00
|
|
|
|
2017-12-16 04:00:30 +08:00
|
|
|
pr_info("PCI: Probing PCI hardware\n");
|
2005-06-24 13:01:16 +08:00
|
|
|
|
|
|
|
/* Scan all of the recorded PCI controllers. */
|
|
|
|
for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
|
|
|
|
pci_ctrl->last_busno = 0xff;
|
2011-10-29 06:28:19 +08:00
|
|
|
INIT_LIST_HEAD(&resources);
|
|
|
|
pci_controller_apertures(pci_ctrl, &resources);
|
|
|
|
bus = pci_scan_root_bus(NULL, pci_ctrl->first_busno,
|
|
|
|
pci_ctrl->ops, pci_ctrl, &resources);
|
PCI: Assign resources before drivers claim devices (pci_scan_root_bus())
Previously, pci_scan_root_bus() created a root PCI bus, enumerated the
devices on it, and called pci_bus_add_devices(), which made the devices
available for drivers to claim them.
Most callers assigned resources to devices after pci_scan_root_bus()
returns, which may be after drivers have claimed the devices. This is
incorrect; the PCI core should not change device resources while a driver
is managing the device.
Remove pci_bus_add_devices() from pci_scan_root_bus() and do it after any
resource assignment in the callers.
Note that ARM's pci_common_init_dev() already called pci_bus_add_devices()
after pci_scan_root_bus(), so we only need to remove the first call:
pci_common_init_dev
pcibios_init_hw
pci_scan_root_bus
pci_bus_add_devices # first call
pci_bus_assign_resources
pci_bus_add_devices # second call
[bhelgaas: changelog, drop "root_bus" var in alpha common_init_pci(),
return failure earlier in mn10300, add "return" in x86 pcibios_scan_root(),
return early if xtensa platform_pcibios_fixup() fails]
Signed-off-by: Yijing Wang <wangyijing@huawei.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
CC: Richard Henderson <rth@twiddle.net>
CC: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
CC: Matt Turner <mattst88@gmail.com>
CC: David Howells <dhowells@redhat.com>
CC: Tony Luck <tony.luck@intel.com>
CC: Michal Simek <monstr@monstr.eu>
CC: Ralf Baechle <ralf@linux-mips.org>
CC: Koichi Yasutake <yasutake.koichi@jp.panasonic.com>
CC: Sebastian Ott <sebott@linux.vnet.ibm.com>
CC: "David S. Miller" <davem@davemloft.net>
CC: Chris Metcalf <cmetcalf@ezchip.com>
CC: Chris Zankel <chris@zankel.net>
CC: Max Filippov <jcmvbkbc@gmail.com>
CC: Thomas Gleixner <tglx@linutronix.de>
2015-03-16 11:18:56 +08:00
|
|
|
if (!bus)
|
|
|
|
continue;
|
|
|
|
|
2005-06-24 13:01:16 +08:00
|
|
|
pci_ctrl->bus = bus;
|
2012-05-18 09:51:11 +08:00
|
|
|
pci_ctrl->last_busno = bus->busn_res.end;
|
2005-06-24 13:01:16 +08:00
|
|
|
if (next_busno <= pci_ctrl->last_busno)
|
|
|
|
next_busno = pci_ctrl->last_busno+1;
|
|
|
|
}
|
|
|
|
pci_bus_count = next_busno;
|
PCI: Assign resources before drivers claim devices (pci_scan_root_bus())
Previously, pci_scan_root_bus() created a root PCI bus, enumerated the
devices on it, and called pci_bus_add_devices(), which made the devices
available for drivers to claim them.
Most callers assigned resources to devices after pci_scan_root_bus()
returns, which may be after drivers have claimed the devices. This is
incorrect; the PCI core should not change device resources while a driver
is managing the device.
Remove pci_bus_add_devices() from pci_scan_root_bus() and do it after any
resource assignment in the callers.
Note that ARM's pci_common_init_dev() already called pci_bus_add_devices()
after pci_scan_root_bus(), so we only need to remove the first call:
pci_common_init_dev
pcibios_init_hw
pci_scan_root_bus
pci_bus_add_devices # first call
pci_bus_assign_resources
pci_bus_add_devices # second call
[bhelgaas: changelog, drop "root_bus" var in alpha common_init_pci(),
return failure earlier in mn10300, add "return" in x86 pcibios_scan_root(),
return early if xtensa platform_pcibios_fixup() fails]
Signed-off-by: Yijing Wang <wangyijing@huawei.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
CC: Richard Henderson <rth@twiddle.net>
CC: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
CC: Matt Turner <mattst88@gmail.com>
CC: David Howells <dhowells@redhat.com>
CC: Tony Luck <tony.luck@intel.com>
CC: Michal Simek <monstr@monstr.eu>
CC: Ralf Baechle <ralf@linux-mips.org>
CC: Koichi Yasutake <yasutake.koichi@jp.panasonic.com>
CC: Sebastian Ott <sebott@linux.vnet.ibm.com>
CC: "David S. Miller" <davem@davemloft.net>
CC: Chris Metcalf <cmetcalf@ezchip.com>
CC: Chris Zankel <chris@zankel.net>
CC: Max Filippov <jcmvbkbc@gmail.com>
CC: Thomas Gleixner <tglx@linutronix.de>
2015-03-16 11:18:56 +08:00
|
|
|
ret = platform_pcibios_fixup();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2005-06-24 13:01:16 +08:00
|
|
|
|
PCI: Assign resources before drivers claim devices (pci_scan_root_bus())
Previously, pci_scan_root_bus() created a root PCI bus, enumerated the
devices on it, and called pci_bus_add_devices(), which made the devices
available for drivers to claim them.
Most callers assigned resources to devices after pci_scan_root_bus()
returns, which may be after drivers have claimed the devices. This is
incorrect; the PCI core should not change device resources while a driver
is managing the device.
Remove pci_bus_add_devices() from pci_scan_root_bus() and do it after any
resource assignment in the callers.
Note that ARM's pci_common_init_dev() already called pci_bus_add_devices()
after pci_scan_root_bus(), so we only need to remove the first call:
pci_common_init_dev
pcibios_init_hw
pci_scan_root_bus
pci_bus_add_devices # first call
pci_bus_assign_resources
pci_bus_add_devices # second call
[bhelgaas: changelog, drop "root_bus" var in alpha common_init_pci(),
return failure earlier in mn10300, add "return" in x86 pcibios_scan_root(),
return early if xtensa platform_pcibios_fixup() fails]
Signed-off-by: Yijing Wang <wangyijing@huawei.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
CC: Richard Henderson <rth@twiddle.net>
CC: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
CC: Matt Turner <mattst88@gmail.com>
CC: David Howells <dhowells@redhat.com>
CC: Tony Luck <tony.luck@intel.com>
CC: Michal Simek <monstr@monstr.eu>
CC: Ralf Baechle <ralf@linux-mips.org>
CC: Koichi Yasutake <yasutake.koichi@jp.panasonic.com>
CC: Sebastian Ott <sebott@linux.vnet.ibm.com>
CC: "David S. Miller" <davem@davemloft.net>
CC: Chris Metcalf <cmetcalf@ezchip.com>
CC: Chris Zankel <chris@zankel.net>
CC: Max Filippov <jcmvbkbc@gmail.com>
CC: Thomas Gleixner <tglx@linutronix.de>
2015-03-16 11:18:56 +08:00
|
|
|
for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
|
|
|
|
if (pci_ctrl->bus)
|
|
|
|
pci_bus_add_devices(pci_ctrl->bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-06-24 13:01:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(pcibios_init);
|
|
|
|
|
2013-05-27 23:45:58 +08:00
|
|
|
void pcibios_fixup_bus(struct pci_bus *bus)
|
2005-06-24 13:01:16 +08:00
|
|
|
{
|
PCI: Revert "PCI: Call pci_read_bridge_bases() from core instead of arch code"
Revert dff22d2054b5 ("PCI: Call pci_read_bridge_bases() from core instead
of arch code").
Reading PCI bridge windows is not arch-specific in itself, but there is PCI
core code that doesn't work correctly if we read them too early. For
example, Hannes found this case on an ARM Freescale i.mx6 board:
pci_bus 0000:00: root bus resource [mem 0x01000000-0x01efffff]
pci 0000:00:00.0: PCI bridge to [bus 01-ff]
pci 0000:00:00.0: BAR 8: no space for [mem size 0x01000000] (mem window)
pci 0000:01:00.0: BAR 2: failed to assign [mem size 0x00200000]
pci 0000:01:00.0: BAR 1: failed to assign [mem size 0x00004000]
pci 0000:01:00.0: BAR 0: failed to assign [mem size 0x00000100]
The 00:00.0 mem window needs to be at least 3MB: the 01:00.0 device needs
0x204100 of space, and mem windows are megabyte-aligned.
Bus sizing can increase a bridge window size, but never *decrease* it (see
d65245c3297a ("PCI: don't shrink bridge resources")). Prior to
dff22d2054b5, ARM didn't read bridge windows at all, so the "original size"
was zero, and we assigned a 3MB window.
After dff22d2054b5, we read the bridge windows before sizing the bus. The
firmware programmed a 16MB window (size 0x01000000) in 00:00.0, and since
we never decrease the size, we kept 16MB even though we only needed 3MB.
But 16MB doesn't fit in the host bridge aperture, so we failed to assign
space for the window and the downstream devices.
I think this is a defect in the PCI core: we shouldn't rely on the firmware
to assign sensible windows.
Ray reported a similar problem, also on ARM, with Broadcom iProc.
Issues like this are too hard to fix right now, so revert dff22d2054b5.
Reported-by: Hannes <oe5hpm@gmail.com>
Reported-by: Ray Jui <rjui@broadcom.com>
Link: http://lkml.kernel.org/r/CAAa04yFQEUJm7Jj1qMT57-LG7ZGtnhNDBe=PpSRa70Mj+XhW-A@mail.gmail.com
Link: http://lkml.kernel.org/r/55F75BB8.4070405@broadcom.com
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Acked-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
2015-09-16 02:18:04 +08:00
|
|
|
if (bus->parent) {
|
|
|
|
/* This is a subordinate bridge */
|
|
|
|
pci_read_bridge_bases(bus);
|
|
|
|
}
|
2005-06-24 13:01:16 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 05:48:31 +08:00
|
|
|
void pcibios_set_master(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
/* No special bus mastering setup handling */
|
|
|
|
}
|
|
|
|
|
2005-06-24 13:01:16 +08:00
|
|
|
int pcibios_enable_device(struct pci_dev *dev, int mask)
|
|
|
|
{
|
|
|
|
u16 cmd, old_cmd;
|
|
|
|
int idx;
|
|
|
|
struct resource *r;
|
|
|
|
|
|
|
|
pci_read_config_word(dev, PCI_COMMAND, &cmd);
|
|
|
|
old_cmd = cmd;
|
|
|
|
for (idx=0; idx<6; idx++) {
|
|
|
|
r = &dev->resource[idx];
|
|
|
|
if (!r->start && r->end) {
|
2018-05-22 20:56:17 +08:00
|
|
|
pci_err(dev, "can't enable device: resource collisions\n");
|
2005-06-24 13:01:16 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (r->flags & IORESOURCE_IO)
|
|
|
|
cmd |= PCI_COMMAND_IO;
|
|
|
|
if (r->flags & IORESOURCE_MEM)
|
|
|
|
cmd |= PCI_COMMAND_MEMORY;
|
|
|
|
}
|
|
|
|
if (cmd != old_cmd) {
|
2018-05-22 20:56:17 +08:00
|
|
|
pci_info(dev, "enabling device (%04x -> %04x)\n", old_cmd, cmd);
|
2005-06-24 13:01:16 +08:00
|
|
|
pci_write_config_word(dev, PCI_COMMAND, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-02-19 21:02:33 +08:00
|
|
|
* Platform support for /proc/bus/pci/X/Y mmap()s.
|
2005-06-24 13:01:16 +08:00
|
|
|
* -- paulus.
|
|
|
|
*/
|
|
|
|
|
2018-02-19 21:02:33 +08:00
|
|
|
int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
|
2005-06-24 13:01:16 +08:00
|
|
|
{
|
2018-02-19 21:02:33 +08:00
|
|
|
struct pci_controller *pci_ctrl = (struct pci_controller*) pdev->sysdata;
|
|
|
|
resource_size_t ioaddr = pci_resource_start(pdev, bar);
|
2005-06-24 13:01:16 +08:00
|
|
|
|
|
|
|
if (pci_ctrl == 0)
|
|
|
|
return -EINVAL; /* should never happen */
|
|
|
|
|
2018-02-19 21:02:33 +08:00
|
|
|
/* Convert to an offset within this PCI controller */
|
|
|
|
ioaddr -= (unsigned long)pci_ctrl->io_space.base;
|
2005-06-24 13:01:16 +08:00
|
|
|
|
2018-02-19 21:02:33 +08:00
|
|
|
vma->vm_pgoff += (ioaddr + pci_ctrl->io_space.start) >> PAGE_SHIFT;
|
|
|
|
return 0;
|
2005-06-24 13:01:16 +08:00
|
|
|
}
|