ia64/linux-2.6.18-xen.hg

diff drivers/pci/iomulti.c @ 882:8dec4aa9b8b9

PCI pass through: PCIe IO space multiplexing

This is required for more than 16 HVM domain to boot from
PCIe pass through device.

Linux as dom0 exclusively assigns IO space to downstream PCI bridges
and the assignment unit of PCI bridge IO space is 4K. So the only up
to 16 PCIe device can be accessed via IO space within 64K IO ports.
PCI expansion ROM BIOS often uses IO port access to boot from the
device, so on virtualized environment, it means only up to 16 guest
domain can boot from pass-through device.

This patch allows PCIe IO space sharing of pass-through device.
- reassign IO space of PCIe devices specified by
"guestiomuldev=[<segment>:]<bus>:<dev>[,[<segment:><bus>:dev]][,...]"
to be shared.
This is implemented as Linux PCI quirk fixup.

The sharing unit is PCIe switch. Ie IO space of the end point
devices under the same switch will be shared. If there are more than
one switches, two areas of IO space will be used.

- And the driver which arbitrates the accesses to the multiplexed PCIe
IO space. Later qemu-dm will use this.

Limitation:
IO port of IO shared devices can't be accessed from dom0 Linux device
driver. But this wouldn't be a big issue because PCIe specification
discourages the use of IO space and recommends that IO space should be
used only for bootable device with ROM code. OS device driver should
work without IO space access.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 28 09:57:49 2009 +0100 (2009-05-28)
parents
children b998614e2e2a
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/pci/iomulti.c	Thu May 28 09:57:49 2009 +0100
     1.3 @@ -0,0 +1,1412 @@
     1.4 +/*
     1.5 + * This program is free software; you can redistribute it and/or modify
     1.6 + * it under the terms of the GNU General Public License as published by
     1.7 + * the Free Software Foundation; either version 2 of the License, or
     1.8 + * (at your option) any later version.
     1.9 + *
    1.10 + * This program is distributed in the hope that it will be useful,
    1.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.13 + * GNU General Public License for more details.
    1.14 + *
    1.15 + * You should have received a copy of the GNU General Public License
    1.16 + * along with this program; if not, write to the Free Software
    1.17 + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    1.18 + *
    1.19 + * Copyright (c) 2009 Isaku Yamahata
    1.20 + *                    VA Linux Systems Japan K.K.
    1.21 + *
    1.22 + */
    1.23 +
    1.24 +#include <linux/kernel.h>
    1.25 +#include <linux/list.h>
    1.26 +#include <linux/miscdevice.h>
    1.27 +#include <linux/pci.h>
    1.28 +#include <linux/sort.h>
    1.29 +
    1.30 +#include <asm/setup.h>
    1.31 +#include <asm/uaccess.h>
    1.32 +
    1.33 +#include "iomulti.h"
    1.34 +
    1.35 +#define PCI_NUM_BARS		6
    1.36 +#define PCI_BUS_MAX		255
    1.37 +#define PCI_DEV_MAX		31
    1.38 +#define PCI_FUNC_MAX		7
    1.39 +#define PCI_NUM_FUNC		8
    1.40 +
    1.41 +/* see pci_resource_len */
    1.42 +static inline resource_size_t pci_iomul_len(const struct resource* r)
    1.43 +{
    1.44 +	if (r->start == 0 && r->start == r->end)
    1.45 +		return 0;
    1.46 +	return r->end - r->start + 1;
    1.47 +}
    1.48 +
    1.49 +#define ROUND_UP(x, a)		(((x) + (a) - 1) & ~((a) - 1))
    1.50 +/* stolen from pbus_size_io() */
    1.51 +static unsigned long pdev_size_io(struct pci_dev *pdev)
    1.52 +{
    1.53 +	unsigned long size = 0, size1 = 0;
    1.54 +	int i;
    1.55 +
    1.56 +	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
    1.57 +		struct resource *r = &pdev->resource[i];
    1.58 +		unsigned long r_size;
    1.59 +
    1.60 +		if (!(r->flags & IORESOURCE_IO))
    1.61 +			continue;
    1.62 +
    1.63 +		r_size = r->end - r->start + 1;
    1.64 +
    1.65 +		if (r_size < 0x400)
    1.66 +			/* Might be re-aligned for ISA */
    1.67 +			size += r_size;
    1.68 +		else
    1.69 +			size1 += r_size;
    1.70 +	}
    1.71 +
    1.72 +/* To be fixed in 2.5: we should have sort of HAVE_ISA
    1.73 +   flag in the struct pci_bus. */
    1.74 +#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
    1.75 +	size = (size & 0xff) + ((size & ~0xffUL) << 2);
    1.76 +#endif
    1.77 +	size = ROUND_UP(size + size1, 4096);
    1.78 +	return size;
    1.79 +}
    1.80 +
    1.81 +/*
    1.82 + * primary bus number of PCI-PCI bridge in switch on which
    1.83 + * this slots sits.
    1.84 + * i.e. the primary bus number of PCI-PCI bridge of downstream port
    1.85 + *      or root port in switch.
    1.86 + *      the secondary bus number of PCI-PCI bridge of upstream port
    1.87 + *      in switch.
    1.88 + */
    1.89 +static inline unsigned char pci_dev_switch_busnr(struct pci_dev *pdev)
    1.90 +{
    1.91 +	if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
    1.92 +		return pdev->bus->primary;
    1.93 +	return pdev->bus->number;
    1.94 +}
    1.95 +
    1.96 +struct pci_iomul_func {
    1.97 +	int		segment;
    1.98 +	uint8_t		bus;
    1.99 +	uint8_t		devfn;
   1.100 +
   1.101 +	/* only start and end are used */
   1.102 +	unsigned long	io_size;
   1.103 +	uint8_t		io_bar;
   1.104 +	struct resource	resource[PCI_NUM_BARS];
   1.105 +	struct resource dummy_parent;
   1.106 +};
   1.107 +
   1.108 +struct pci_iomul_switch {
   1.109 +	struct list_head	list;	/* bus_list_lock protects */
   1.110 +
   1.111 +	/*
   1.112 +	 * This lock the following entry and following
   1.113 +	 * pci_iomul_slot/pci_iomul_func.
   1.114 +	 */
   1.115 +	struct mutex		lock;
   1.116 +	struct kref		kref;
   1.117 +
   1.118 +	struct resource		io_resource;
   1.119 +	struct resource		*io_region;
   1.120 +	unsigned int		count;
   1.121 +	struct pci_dev		*current_pdev;
   1.122 +
   1.123 +	int			segment;
   1.124 +	uint8_t			bus;
   1.125 +
   1.126 +	uint32_t		io_base;
   1.127 +	uint32_t		io_limit;
   1.128 +
   1.129 +	/* func which has the largeset io size*/
   1.130 +	struct pci_iomul_func	*func;
   1.131 +
   1.132 +	struct list_head	slots;
   1.133 +};
   1.134 +
   1.135 +struct pci_iomul_slot {
   1.136 +	struct list_head	sibling;
   1.137 +	struct kref		kref;
   1.138 +	/*
   1.139 +	 * busnr
   1.140 +	 * when pcie, the primary busnr of the PCI-PCI bridge on which
   1.141 +	 * this devices sits.
   1.142 +	 */
   1.143 +	uint8_t			switch_busnr;
   1.144 +	struct resource		dummy_parent[PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES];
   1.145 +
   1.146 +	/* device */
   1.147 +	int			segment;
   1.148 +	uint8_t			bus;
   1.149 +	uint8_t			dev;
   1.150 +
   1.151 +	struct pci_iomul_func	*func[PCI_NUM_FUNC];
   1.152 +};
   1.153 +
   1.154 +static LIST_HEAD(switch_list);
   1.155 +static DEFINE_MUTEX(switch_list_lock);
   1.156 +
   1.157 +/*****************************************************************************/
   1.158 +static int inline pci_iomul_switch_io_allocated(
   1.159 +	const struct pci_iomul_switch *sw)
   1.160 +{
   1.161 +	return !(sw->io_base == 0 || sw->io_base > sw->io_limit);
   1.162 +}
   1.163 +
   1.164 +static struct pci_iomul_switch *pci_iomul_find_switch_locked(int segment,
   1.165 +							     uint8_t bus)
   1.166 +{
   1.167 +	struct pci_iomul_switch *sw;
   1.168 +
   1.169 +	BUG_ON(!mutex_is_locked(&switch_list_lock));
   1.170 +	list_for_each_entry(sw, &switch_list, list) {
   1.171 +		if (sw->segment == segment && sw->bus == bus)
   1.172 +			return sw;
   1.173 +	}
   1.174 +	return NULL;
   1.175 +}
   1.176 +
   1.177 +static struct pci_iomul_slot *pci_iomul_find_slot_locked(
   1.178 +	struct pci_iomul_switch *sw, uint8_t busnr, uint8_t dev)
   1.179 +{
   1.180 +	struct pci_iomul_slot *slot;
   1.181 +
   1.182 +	BUG_ON(!mutex_is_locked(&sw->lock));
   1.183 +	list_for_each_entry(slot, &sw->slots, sibling) {
   1.184 +		if (slot->bus == busnr && slot->dev == dev)
   1.185 +			return slot;
   1.186 +	}
   1.187 +	return NULL;
   1.188 +}
   1.189 +
   1.190 +static void pci_iomul_switch_get(struct pci_iomul_switch *sw);
   1.191 +/* on successfull exit, sw->lock is locked for use slot and
   1.192 + * refrence count of sw is incremented.
   1.193 + */
   1.194 +static void pci_iomul_get_lock_switch(struct pci_dev *pdev,
   1.195 +				      struct pci_iomul_switch **swp,
   1.196 +				      struct pci_iomul_slot **slot)
   1.197 +{
   1.198 +	mutex_lock(&switch_list_lock);
   1.199 +
   1.200 +	*swp = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
   1.201 +					    pci_dev_switch_busnr(pdev));
   1.202 +	if (*swp == NULL) {
   1.203 +		*slot = NULL;
   1.204 +		goto out;
   1.205 +	}
   1.206 +
   1.207 +	mutex_lock(&(*swp)->lock);
   1.208 +	*slot = pci_iomul_find_slot_locked(*swp, pdev->bus->number,
   1.209 +					   PCI_SLOT(pdev->devfn));
   1.210 +	if (*slot == NULL) {
   1.211 +		mutex_unlock(&(*swp)->lock);
   1.212 +		*swp = NULL;
   1.213 +	} else {
   1.214 +		pci_iomul_switch_get(*swp);
   1.215 +	}
   1.216 +out:
   1.217 +	mutex_unlock(&switch_list_lock);
   1.218 +}
   1.219 +
   1.220 +static struct pci_iomul_switch *pci_iomul_switch_alloc(int segment,
   1.221 +						       uint8_t bus)
   1.222 +{
   1.223 +	struct pci_iomul_switch *sw;
   1.224 +
   1.225 +	BUG_ON(!mutex_is_locked(&switch_list_lock));
   1.226 +
   1.227 +	sw = kmalloc(sizeof(*sw), GFP_KERNEL);
   1.228 +
   1.229 +	mutex_init(&sw->lock);
   1.230 +	kref_init(&sw->kref);
   1.231 +	sw->io_region = NULL;
   1.232 +	sw->count = 0;
   1.233 +	sw->current_pdev = NULL;
   1.234 +	sw->segment = segment;
   1.235 +	sw->bus = bus;
   1.236 +	sw->io_base = 0;
   1.237 +	sw->io_limit = 0;
   1.238 +	sw->func = NULL;
   1.239 +	INIT_LIST_HEAD(&sw->slots);
   1.240 +
   1.241 +	return sw;
   1.242 +}
   1.243 +
   1.244 +static void pci_iomul_switch_add_locked(struct pci_iomul_switch *sw)
   1.245 +{
   1.246 +	BUG_ON(!mutex_is_locked(&switch_list_lock));
   1.247 +	list_add(&sw->list, &switch_list);
   1.248 +}
   1.249 +
   1.250 +#ifdef CONFIG_HOTPLUG_PCI
   1.251 +static void pci_iomul_switch_del_locked(struct pci_iomul_switch *sw)
   1.252 +{
   1.253 +	BUG_ON(!mutex_is_locked(&switch_list_lock));
   1.254 +	list_del(&sw->list);
   1.255 +}
   1.256 +#endif
   1.257 +
   1.258 +static void pci_iomul_switch_get(struct pci_iomul_switch *sw)
   1.259 +{
   1.260 +	kref_get(&sw->kref);
   1.261 +}
   1.262 +
   1.263 +static void pci_iomul_switch_release(struct kref *kref)
   1.264 +{
   1.265 +	struct pci_iomul_switch *sw = container_of(kref,
   1.266 +						   struct pci_iomul_switch,
   1.267 +						   kref);
   1.268 +	kfree(sw);
   1.269 +}
   1.270 +
   1.271 +static void pci_iomul_switch_put(struct pci_iomul_switch *sw)
   1.272 +{
   1.273 +	kref_put(&sw->kref, &pci_iomul_switch_release);
   1.274 +}
   1.275 +
   1.276 +static int __devinit pci_iomul_slot_init(struct pci_dev *pdev,
   1.277 +					 struct pci_iomul_slot *slot)
   1.278 +{
   1.279 +	u16 rpcap;
   1.280 +	u16 cap;
   1.281 +
   1.282 +	rpcap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
   1.283 +	if (!rpcap) {
   1.284 +		/* pci device isn't supported */
   1.285 +		printk(KERN_INFO
   1.286 +		       "PCI: sharing io port of non PCIe device %s "
   1.287 +		       "isn't supported. ignoring.\n",
   1.288 +		       pci_name(pdev));
   1.289 +		return -ENOSYS;
   1.290 +	}
   1.291 +
   1.292 +        pci_read_config_word(pdev, rpcap + PCI_CAP_FLAGS, &cap);
   1.293 +	switch ((cap & PCI_EXP_FLAGS_TYPE) >> 4) {
   1.294 +	case PCI_EXP_TYPE_RC_END:
   1.295 +		printk(KERN_INFO
   1.296 +		       "PCI: io port sharing of root complex integrated "
   1.297 +		       "endpoint %s isn't supported. ignoring.\n",
   1.298 +		       pci_name(pdev));
   1.299 +		return -ENOSYS;
   1.300 +	case PCI_EXP_TYPE_ENDPOINT:
   1.301 +	case PCI_EXP_TYPE_LEG_END:
   1.302 +		break;
   1.303 +	default:
   1.304 +		printk(KERN_INFO
   1.305 +		       "PCI: io port sharing of non endpoint %s "
   1.306 +		       "doesn't make sense. ignoring.\n",
   1.307 +		       pci_name(pdev));
   1.308 +		return -EINVAL;
   1.309 +	}
   1.310 +
   1.311 +	kref_init(&slot->kref);
   1.312 +	slot->switch_busnr = pci_dev_switch_busnr(pdev);
   1.313 +	slot->segment = pci_domain_nr(pdev->bus);
   1.314 +	slot->bus = pdev->bus->number;
   1.315 +	slot->dev = PCI_SLOT(pdev->devfn);
   1.316 +
   1.317 +	return 0;
   1.318 +}
   1.319 +
   1.320 +static struct pci_iomul_slot *pci_iomul_slot_alloc(struct pci_dev *pdev)
   1.321 +{
   1.322 +	struct pci_iomul_slot *slot;
   1.323 +
   1.324 +	slot = kzalloc(sizeof(*slot), GFP_KERNEL);
   1.325 +	if (slot == NULL)
   1.326 +		return NULL;
   1.327 +
   1.328 +	if (pci_iomul_slot_init(pdev, slot) != 0) {
   1.329 +		kfree(slot);
   1.330 +		return NULL;
   1.331 +	}
   1.332 +	return slot;
   1.333 +}
   1.334 +
   1.335 +static void pci_iomul_slot_add_locked(struct pci_iomul_switch *sw,
   1.336 +				      struct pci_iomul_slot *slot)
   1.337 +{
   1.338 +	BUG_ON(!mutex_is_locked(&sw->lock));
   1.339 +	list_add(&slot->sibling, &sw->slots);
   1.340 +}
   1.341 +
   1.342 +#ifdef CONFIG_HOTPLUG_PCI
   1.343 +static void pci_iomul_slot_del_locked(struct pci_iomul_switch *sw,
   1.344 +				       struct pci_iomul_slot *slot)
   1.345 +{
   1.346 +	BUG_ON(!mutex_is_locked(&sw->lock));
   1.347 +	list_del(&slot->sibling);
   1.348 +}
   1.349 +#endif
   1.350 +
   1.351 +static void pci_iomul_slot_get(struct pci_iomul_slot *slot)
   1.352 +{
   1.353 +	kref_get(&slot->kref);
   1.354 +}
   1.355 +
   1.356 +static void pci_iomul_slot_release(struct kref *kref)
   1.357 +{
   1.358 +	struct pci_iomul_slot *slot = container_of(kref, struct pci_iomul_slot,
   1.359 +						   kref);
   1.360 +	kfree(slot);
   1.361 +}
   1.362 +
   1.363 +static void pci_iomul_slot_put(struct pci_iomul_slot *slot)
   1.364 +{
   1.365 +	kref_put(&slot->kref, &pci_iomul_slot_release);
   1.366 +}
   1.367 +
   1.368 +/*****************************************************************************/
   1.369 +static int pci_get_sbd(const char *str,
   1.370 +		       int *segment__, uint8_t *bus__, uint8_t *dev__)
   1.371 +{
   1.372 +	int segment;
   1.373 +	int bus;
   1.374 +	int dev;
   1.375 +
   1.376 +	if (sscanf(str, "%x:%x:%x", &segment, &bus, &dev) != 3) {
   1.377 +		if (sscanf(str, "%x:%x", &bus, &dev) == 2)
   1.378 +			segment = 0;
   1.379 +		else
   1.380 +			return -EINVAL;
   1.381 +	}
   1.382 +
   1.383 +	if (segment < 0 || INT_MAX <= segment)
   1.384 +		return -EINVAL;
   1.385 +	if (bus < 0 || PCI_BUS_MAX < bus)
   1.386 +		return -EINVAL;
   1.387 +	if (dev < 0 || PCI_DEV_MAX < dev)
   1.388 +		return -EINVAL;
   1.389 +
   1.390 +	*segment__ = segment;
   1.391 +	*bus__ = bus;
   1.392 +	*dev__ = dev;
   1.393 +	return 0;
   1.394 +}
   1.395 +
   1.396 +static char iomul_param[COMMAND_LINE_SIZE];
   1.397 +#define TOKEN_MAX	10	/* SSSS:BB:DD length is 10 */
   1.398 +static int pci_is_iomul_dev_param(struct pci_dev *pdev)
   1.399 +{
   1.400 +        int len;
   1.401 +        char *p;
   1.402 +	char *next_str;
   1.403 +
   1.404 +	for (p = &iomul_param[0]; *p != '\0'; p = next_str + 1) {
   1.405 +		next_str = strchr(p, ',');
   1.406 +		if (next_str != NULL)
   1.407 +			len = next_str - p;
   1.408 +		else
   1.409 +			len = strlen(p);
   1.410 +
   1.411 +		if (len > 0 && len <= TOKEN_MAX) {
   1.412 +			char tmp[TOKEN_MAX+1];
   1.413 +			int seg;
   1.414 +			uint8_t bus;
   1.415 +			uint8_t dev;
   1.416 +
   1.417 +			strncpy(tmp, p, len);
   1.418 +			*(tmp + len) = '\0';
   1.419 +			if (pci_get_sbd(tmp, &seg, &bus, &dev) == 0 &&
   1.420 +			    pci_domain_nr(pdev->bus) == seg &&
   1.421 +			    pdev->bus->number == bus &&
   1.422 +			    PCI_SLOT(pdev->devfn) == dev)
   1.423 +				return 1;
   1.424 +		}
   1.425 +		if (next_str == NULL)
   1.426 +			break;
   1.427 +	}
   1.428 +
   1.429 +	return 0;
   1.430 +}
   1.431 +
   1.432 +/*
   1.433 + * Format: [<segment>:]<bus>:<dev>[,[<segment>:]<bus>:<dev>[,...]
   1.434 + */
   1.435 +static int __init pci_iomul_param_setup(char *str)
   1.436 +{
   1.437 +	if (strlen(str) >= COMMAND_LINE_SIZE)
   1.438 +		return 0;
   1.439 +
   1.440 +	/* parse it after pci bus scanning */
   1.441 +	strncpy(iomul_param, str, sizeof(iomul_param));
   1.442 +	return 1;
   1.443 +}
   1.444 +__setup("guestiomuldev=", pci_iomul_param_setup);
   1.445 +
   1.446 +/*****************************************************************************/
   1.447 +static void __devinit pci_iomul_set_bridge_io_window(struct pci_dev *bridge,
   1.448 +						     uint32_t io_base,
   1.449 +						     uint32_t io_limit)
   1.450 +{
   1.451 +	uint16_t l;
   1.452 +	uint32_t upper16;
   1.453 +
   1.454 +	io_base >>= 12;
   1.455 +	io_base <<= 4;
   1.456 +	io_limit >>= 12;
   1.457 +	io_limit <<= 4;
   1.458 +	l = (io_base & 0xff) | ((io_limit & 0xff) << 8);
   1.459 +	upper16 = ((io_base & 0xffff00) >> 8) |
   1.460 +		(((io_limit & 0xffff00) >> 8) << 16);
   1.461 +
   1.462 +        /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
   1.463 +        pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
   1.464 +        /* Update lower 16 bits of I/O base/limit. */
   1.465 +        pci_write_config_word(bridge, PCI_IO_BASE, l);
   1.466 +        /* Update upper 16 bits of I/O base/limit. */
   1.467 +        pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, upper16);
   1.468 +}
   1.469 +
   1.470 +static void __devinit pci_disable_bridge_io_window(struct pci_dev *bridge)
   1.471 +{
   1.472 +	/* set base = 0xffffff limit = 0x0 */
   1.473 +	pci_iomul_set_bridge_io_window(bridge, 0xffffff, 0);
   1.474 +}
   1.475 +
   1.476 +static int __devinit pci_iomul_func_scan(struct pci_dev *pdev,
   1.477 +					 struct pci_iomul_slot *slot,
   1.478 +					 uint8_t func)
   1.479 +{
   1.480 +	struct pci_iomul_func *f;
   1.481 +	unsigned int i;
   1.482 +
   1.483 +	f = kzalloc(sizeof(*f), GFP_KERNEL);
   1.484 +	if (f == NULL)
   1.485 +		return -ENOMEM;
   1.486 +
   1.487 +	f->segment = slot->segment;
   1.488 +	f->bus = slot->bus;
   1.489 +	f->devfn = PCI_DEVFN(slot->dev, func);
   1.490 +	f->io_size = pdev_size_io(pdev);
   1.491 +
   1.492 +	for (i = 0; i < PCI_NUM_BARS; i++) {
   1.493 +		if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO))
   1.494 +			continue;
   1.495 +		if (pci_resource_len(pdev, i) == 0)
   1.496 +			continue;
   1.497 +
   1.498 +		f->io_bar |= 1 << i;
   1.499 +		f->resource[i] = pdev->resource[i];
   1.500 +	}
   1.501 +
   1.502 +	if (f->io_bar)
   1.503 +		slot->func[func] = f;
   1.504 +	else
   1.505 +		kfree(f);
   1.506 +	return 0;
   1.507 +}
   1.508 +
   1.509 +/*
   1.510 + * This is tricky part.
   1.511 + * fake PCI resource assignment routines by setting flags to 0.
   1.512 + * PCI resource allocate routines think the resource should
   1.513 + * be allocated by checking flags. 0 means this resource isn't used.
   1.514 + * See pbus_size_io() and pdev_sort_resources().
   1.515 + *
   1.516 + * After allocated resources, flags (IORESOURCE_IO) is exported
   1.517 + * to other part including user process.
   1.518 + * So we have to set flags to IORESOURCE_IO, but at the same time
   1.519 + * we must prevent those resources from reassigning when pci hot plug.
   1.520 + * To achieve that, set r->parent to dummy resource.
   1.521 + */
   1.522 +static void __devinit pci_iomul_disable_resource(struct resource *r)
   1.523 +{
   1.524 +	/* don't allocate this resource */
   1.525 +	r->flags = 0;
   1.526 +}
   1.527 +
   1.528 +static void __devinit pci_iomul_reenable_resource(
   1.529 +	struct resource *dummy_parent, struct resource *r)
   1.530 +{
   1.531 +	int ret;
   1.532 +
   1.533 +	dummy_parent->start = r->start;
   1.534 +	dummy_parent->end = r->end;
   1.535 +	dummy_parent->flags = r->flags;
   1.536 +	dummy_parent->name = "PCI IOMUL dummy resource";
   1.537 +
   1.538 +	ret = request_resource(dummy_parent, r);
   1.539 +	BUG_ON(ret);
   1.540 +}
   1.541 +
   1.542 +static void __devinit pci_iomul_fixup_ioresource(struct pci_dev *pdev,
   1.543 +						 struct pci_iomul_func *func,
   1.544 +						 int reassign, int dealloc)
   1.545 +{
   1.546 +	uint8_t i;
   1.547 +	struct resource *r;
   1.548 +
   1.549 +	printk(KERN_INFO "PCI: deallocating io resource[%s]. io size 0x%lx\n",
   1.550 +	       pci_name(pdev), func->io_size);
   1.551 +	for (i = 0; i < PCI_NUM_BARS; i++) {
   1.552 +		r = &pdev->resource[i];
   1.553 +		if (!(func->io_bar & (1 << i)))
   1.554 +			continue;
   1.555 +
   1.556 +		if (reassign) {
   1.557 +			r->end -= r->start;
   1.558 +			r->start = 0;
   1.559 +			pci_update_resource(pdev, i);
   1.560 +			func->resource[i] = *r;
   1.561 +		}
   1.562 +
   1.563 +		if (dealloc)
   1.564 +			/* don't allocate this resource */
   1.565 +			pci_iomul_disable_resource(r);
   1.566 +	}
   1.567 +
   1.568 +	/* parent PCI-PCI bridge */
   1.569 +	if (!reassign)
   1.570 +		return;
   1.571 +	pdev = pdev->bus->self;
   1.572 +	if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
   1.573 +		return;
   1.574 +	pci_disable_bridge_io_window(pdev);
   1.575 +	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
   1.576 +		r = &pdev->resource[i];
   1.577 +		if (!(r->flags & IORESOURCE_IO))
   1.578 +			continue;
   1.579 +
   1.580 +		r->end -= r->start;
   1.581 +		r->start = 0;
   1.582 +		if (i < PCI_BRIDGE_RESOURCES)
   1.583 +			pci_update_resource(pdev, i);
   1.584 +	}
   1.585 +}
   1.586 +
   1.587 +static void __devinit __quirk_iomul_dealloc_ioresource(
   1.588 +	struct pci_iomul_switch *sw,
   1.589 +	struct pci_dev *pdev, struct pci_iomul_slot *slot)
   1.590 +{
   1.591 +	struct pci_iomul_func *f;
   1.592 +	struct pci_iomul_func *__f;
   1.593 +
   1.594 +	if (pci_iomul_func_scan(pdev, slot, PCI_FUNC(pdev->devfn)) != 0)
   1.595 +		return;
   1.596 +
   1.597 +	f = slot->func[PCI_FUNC(pdev->devfn)];
   1.598 +	if (f == NULL)
   1.599 +		return;
   1.600 +
   1.601 +	__f = sw->func;
   1.602 +	/* sw->io_base == 0 means that we are called at boot time.
   1.603 +	 * != 0 means that we are called by php after boot. */
   1.604 +	if (sw->io_base == 0 &&
   1.605 +	    (__f == NULL || __f->io_size < f->io_size)) {
   1.606 +		if (__f != NULL) {
   1.607 +			struct pci_bus *__pbus;
   1.608 +			struct pci_dev *__pdev;
   1.609 +
   1.610 +			__pbus = pci_find_bus(__f->segment, __f->bus);
   1.611 +			BUG_ON(__pbus == NULL);
   1.612 +			__pdev = pci_get_slot(__pbus, __f->devfn);
   1.613 +			BUG_ON(__pdev == NULL);
   1.614 +			pci_iomul_fixup_ioresource(__pdev, __f, 0, 1);
   1.615 +			pci_dev_put(__pdev);
   1.616 +		}
   1.617 +
   1.618 +		pci_iomul_fixup_ioresource(pdev, f, 1, 0);
   1.619 +		sw->func = f;
   1.620 +	} else {
   1.621 +		pci_iomul_fixup_ioresource(pdev, f, 1, 1);
   1.622 +	}
   1.623 +}
   1.624 +
   1.625 +static void __devinit quirk_iomul_dealloc_ioresource(struct pci_dev *pdev)
   1.626 +{
   1.627 +	struct pci_iomul_switch *sw;
   1.628 +	struct pci_iomul_slot *slot;
   1.629 +
   1.630 +	if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
   1.631 +		return;
   1.632 +	if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
   1.633 +		return;	/* PCI Host Bridge isn't a target device */
   1.634 +	if (!pci_is_iomul_dev_param(pdev))
   1.635 +		return;
   1.636 +
   1.637 +	mutex_lock(&switch_list_lock);
   1.638 +	sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
   1.639 +					  pci_dev_switch_busnr(pdev));
   1.640 +	if (sw == NULL) {
   1.641 +		sw = pci_iomul_switch_alloc(pci_domain_nr(pdev->bus),
   1.642 +					    pci_dev_switch_busnr(pdev));
   1.643 +		if (sw == NULL) {
   1.644 +			mutex_unlock(&switch_list_lock);
   1.645 +			printk(KERN_WARNING
   1.646 +			       "PCI: can't allocate memory "
   1.647 +			       "for sw of IO mulplexing %s", pci_name(pdev));
   1.648 +			return;
   1.649 +		}
   1.650 +		pci_iomul_switch_add_locked(sw);
   1.651 +	}
   1.652 +	pci_iomul_switch_get(sw);
   1.653 +	mutex_unlock(&switch_list_lock);
   1.654 +
   1.655 +	mutex_lock(&sw->lock);
   1.656 +	slot = pci_iomul_find_slot_locked(sw, pdev->bus->number,
   1.657 +					  PCI_SLOT(pdev->devfn));
   1.658 +	if (slot == NULL) {
   1.659 +		slot = pci_iomul_slot_alloc(pdev);
   1.660 +		if (slot == NULL) {
   1.661 +			mutex_unlock(&sw->lock);
   1.662 +			pci_iomul_switch_put(sw);
   1.663 +			printk(KERN_WARNING "PCI: can't allocate memory "
   1.664 +			       "for IO mulplexing %s", pci_name(pdev));
   1.665 +			return;
   1.666 +		}
   1.667 +		pci_iomul_slot_add_locked(sw, slot);
   1.668 +	}
   1.669 +
   1.670 +	printk(KERN_INFO "PCI: disable device and release io resource[%s].\n",
   1.671 +	       pci_name(pdev));
   1.672 +	pci_disable_device(pdev);
   1.673 +
   1.674 +	__quirk_iomul_dealloc_ioresource(sw, pdev, slot);
   1.675 +
   1.676 +	mutex_unlock(&sw->lock);
   1.677 +	pci_iomul_switch_put(sw);
   1.678 +}
   1.679 +DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID,
   1.680 +			 quirk_iomul_dealloc_ioresource);
   1.681 +
   1.682 +static void __devinit pci_iomul_read_bridge_io(struct pci_iomul_switch *sw)
   1.683 +{
   1.684 +	struct pci_iomul_func *f = sw->func;
   1.685 +
   1.686 +	struct pci_bus *pbus;
   1.687 +	struct pci_dev *pdev;
   1.688 +	struct pci_dev *bridge;
   1.689 +
   1.690 +	uint16_t l;
   1.691 +	uint16_t base_upper16;
   1.692 +	uint16_t limit_upper16;
   1.693 +	uint32_t io_base;
   1.694 +	uint32_t io_limit;
   1.695 +
   1.696 +	pbus = pci_find_bus(f->segment, f->bus);
   1.697 +	BUG_ON(pbus == NULL);
   1.698 +
   1.699 +	pdev = pci_get_slot(pbus, f->devfn);
   1.700 +	BUG_ON(pdev == NULL);
   1.701 +
   1.702 +	bridge = pdev->bus->self;
   1.703 +	pci_read_config_word(bridge, PCI_IO_BASE, &l);
   1.704 +	pci_read_config_word(bridge, PCI_IO_BASE_UPPER16, &base_upper16);
   1.705 +	pci_read_config_word(bridge, PCI_IO_LIMIT_UPPER16, &limit_upper16);
   1.706 +
   1.707 +	io_base = (l & 0xf0) | ((uint32_t)base_upper16 << 8);
   1.708 +	io_base <<= 8;
   1.709 +	io_limit = (l >> 8) | ((uint32_t)limit_upper16 << 8);
   1.710 +	io_limit <<= 8;
   1.711 +	io_limit |= 0xfff;
   1.712 +
   1.713 +	sw->io_base = io_base;
   1.714 +	sw->io_limit = io_limit;
   1.715 +
   1.716 +	pci_dev_put(pdev);
   1.717 +	printk(KERN_INFO "PCI: bridge %s base 0x%x limit 0x%x\n",
   1.718 +	       pci_name(bridge), sw->io_base, sw->io_limit);
   1.719 +}
   1.720 +
   1.721 +static void __devinit pci_iomul_setup_brige(struct pci_dev *bridge,
   1.722 +					    uint32_t io_base,
   1.723 +					    uint32_t io_limit)
   1.724 +{
   1.725 +	uint16_t cmd;
   1.726 +
   1.727 +	if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_HOST)
   1.728 +		return;
   1.729 +
   1.730 +	pci_iomul_set_bridge_io_window(bridge, io_base, io_limit);
   1.731 +
   1.732 +	/* and forcibly enables IO */
   1.733 +	pci_read_config_word(bridge, PCI_COMMAND, &cmd);
   1.734 +	if (!(cmd & PCI_COMMAND_IO)) {
   1.735 +		cmd |= PCI_COMMAND_IO;
   1.736 +                printk(KERN_INFO "PCI: Forcibly Enabling IO %s\n",
   1.737 +		       pci_name(bridge));
   1.738 +                pci_write_config_word(bridge, PCI_COMMAND, cmd);
   1.739 +	}
   1.740 +}
   1.741 +
   1.742 +struct __bar {
   1.743 +	unsigned long size;
   1.744 +	uint8_t bar;
   1.745 +};
   1.746 +
   1.747 +/* decending order */
   1.748 +static int __devinit pci_iomul_bar_cmp(const void *lhs__, const void *rhs__)
   1.749 +{
   1.750 +	const struct __bar *lhs = (struct __bar*)lhs__;
   1.751 +	const struct __bar *rhs = (struct __bar*)rhs__;
   1.752 +	return - (lhs->size - rhs->size);
   1.753 +}
   1.754 +
   1.755 +static void __devinit pci_iomul_setup_dev(struct pci_dev *pdev,
   1.756 +					  struct pci_iomul_func *f,
   1.757 +					  uint32_t io_base)
   1.758 +{
   1.759 +	struct __bar bars[PCI_NUM_BARS];
   1.760 +	int i;
   1.761 +	uint8_t num_bars = 0;
   1.762 +	struct resource *r;
   1.763 +
   1.764 +	printk(KERN_INFO "PCI: Forcibly assign IO %s from 0x%x\n",
   1.765 +	       pci_name(pdev), io_base);
   1.766 +
   1.767 +	for (i = 0; i < PCI_NUM_BARS; i++) {
   1.768 +		if (!(f->io_bar & (1 << i)))
   1.769 +			continue;
   1.770 +
   1.771 +		r = &f->resource[i];
   1.772 +		bars[num_bars].size = pci_iomul_len(r);
   1.773 +		bars[num_bars].bar = i;
   1.774 +
   1.775 +		num_bars++;
   1.776 +	}
   1.777 +
   1.778 +	sort(bars, num_bars, sizeof(bars[0]), &pci_iomul_bar_cmp, NULL);
   1.779 +
   1.780 +	for (i = 0; i < num_bars; i++) {
   1.781 +		struct resource *fr = &f->resource[bars[i].bar];
   1.782 +		r = &pdev->resource[bars[i].bar];
   1.783 +
   1.784 +		BUG_ON(r->start != 0);
   1.785 +		r->start += io_base;
   1.786 +		r->end += io_base;
   1.787 +
   1.788 +		fr->start = r->start;
   1.789 +		fr->end = r->end;
   1.790 +
   1.791 +		/* pci_update_resource() check flags. */
   1.792 +		r->flags = fr->flags;
   1.793 +		pci_update_resource(pdev, bars[i].bar);
   1.794 +		pci_iomul_reenable_resource(&f->dummy_parent, r);
   1.795 +
   1.796 +		io_base += bars[i].size;
   1.797 +	}
   1.798 +}
   1.799 +
   1.800 +static void __devinit pci_iomul_release_io_resource(
   1.801 +	struct pci_dev *pdev, struct pci_iomul_switch *sw,
   1.802 +	struct pci_iomul_slot *slot, struct pci_iomul_func *f)
   1.803 +{
   1.804 +	int i;
   1.805 +	struct resource *r;
   1.806 +
   1.807 +	for (i = 0; i < PCI_NUM_BARS; i++) {
   1.808 +		if (pci_resource_flags(pdev, i) & IORESOURCE_IO &&
   1.809 +		    pdev->resource[i].parent != NULL) {
   1.810 +			r = &pdev->resource[i];
   1.811 +			f->resource[i] = *r;
   1.812 +			release_resource(r);
   1.813 +			pci_iomul_reenable_resource(&f->dummy_parent, r);
   1.814 +		}
   1.815 +	}
   1.816 +
   1.817 +	/* parent PCI-PCI bridge */
   1.818 +	pdev = pdev->bus->self;
   1.819 +	if ((pdev->class >> 8) != PCI_CLASS_BRIDGE_HOST) {
   1.820 +		for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
   1.821 +			struct resource *parent = pdev->resource[i].parent;
   1.822 +
   1.823 +			if (pci_resource_flags(pdev, i) & IORESOURCE_IO &&
   1.824 +			    parent != NULL) {
   1.825 +				r = &pdev->resource[i];
   1.826 +
   1.827 +				sw->io_resource.flags = r->flags;
   1.828 +				sw->io_resource.start = sw->io_base;
   1.829 +				sw->io_resource.end = sw->io_limit;
   1.830 +				sw->io_resource.name = "PCI IO Multiplexer";
   1.831 +
   1.832 +				release_resource(r);
   1.833 +				pci_iomul_reenable_resource(
   1.834 +					&slot->dummy_parent[i - PCI_BRIDGE_RESOURCES], r);
   1.835 +
   1.836 +				if (request_resource(parent,
   1.837 +						     &sw->io_resource))
   1.838 +					printk(KERN_ERR
   1.839 +					       "PCI IOMul: can't allocate "
   1.840 +					       "resource. [0x%x, 0x%x]",
   1.841 +					       sw->io_base, sw->io_limit);
   1.842 +			}
   1.843 +		}
   1.844 +	}
   1.845 +}
   1.846 +
   1.847 +static void __devinit quirk_iomul_reassign_ioresource(struct pci_dev *pdev)
   1.848 +{
   1.849 +	struct pci_iomul_switch *sw;
   1.850 +	struct pci_iomul_slot *slot;
   1.851 +	struct pci_iomul_func *sf;
   1.852 +	struct pci_iomul_func *f;
   1.853 +
   1.854 +	pci_iomul_get_lock_switch(pdev, &sw, &slot);
   1.855 +	if (sw == NULL || slot == NULL)
   1.856 +		return;
   1.857 +
   1.858 +	if (sw->io_base == 0)
   1.859 +		pci_iomul_read_bridge_io(sw);
   1.860 +	if (!pci_iomul_switch_io_allocated(sw))
   1.861 +		goto out;
   1.862 +
   1.863 +	sf = sw->func;
   1.864 +	f = slot->func[PCI_FUNC(pdev->devfn)];
   1.865 +	if (f == NULL)
   1.866 +		/* (sf == NULL || f == NULL) case
   1.867 +		 * can happen when all the specified devices
   1.868 +		 * don't have io space
   1.869 +		 */
   1.870 +		goto out;
   1.871 +
   1.872 +	if (sf != NULL &&
   1.873 +	    (pci_domain_nr(pdev->bus) != sf->segment ||
   1.874 +	     pdev->bus->number != sf->bus ||
   1.875 +	     PCI_SLOT(pdev->devfn) != PCI_SLOT(sf->devfn)) &&
   1.876 +	    PCI_FUNC(pdev->devfn) == 0) {
   1.877 +		pci_iomul_setup_brige(pdev->bus->self,
   1.878 +				      sw->io_base, sw->io_limit);
   1.879 +	}
   1.880 +
   1.881 +	BUG_ON(f->io_size > sw->io_limit - sw->io_base + 1);
   1.882 +	if (/* f == sf */
   1.883 +            sf != NULL &&
   1.884 +	    pci_domain_nr(pdev->bus) == sf->segment &&
   1.885 +	    pdev->bus->number == sf->bus &&
   1.886 +	    pdev->devfn == sf->devfn)
   1.887 +		pci_iomul_release_io_resource(pdev, sw, slot, f);
   1.888 +	else
   1.889 +		pci_iomul_setup_dev(pdev, f, sw->io_base);
   1.890 +
   1.891 +out:
   1.892 +	mutex_unlock(&sw->lock);
   1.893 +	pci_iomul_switch_put(sw);
   1.894 +}
   1.895 +
   1.896 +DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID,
   1.897 +			quirk_iomul_reassign_ioresource);
   1.898 +
   1.899 +/*****************************************************************************/
   1.900 +#ifdef CONFIG_HOTPLUG_PCI
   1.901 +static int __devinit __pci_iomul_notifier_del_device(struct pci_dev *pdev)
   1.902 +{
   1.903 +	struct pci_iomul_switch *sw;
   1.904 +	struct pci_iomul_slot *slot;
   1.905 +	int i;
   1.906 +
   1.907 +	pci_iomul_get_lock_switch(pdev, &sw, &slot);
   1.908 +	if (sw == NULL || slot == NULL)
   1.909 +		return 0;
   1.910 +
   1.911 +	if (sw->func == slot->func[PCI_FUNC(pdev->devfn)])
   1.912 +		sw->func = NULL;
   1.913 +	kfree(slot->func[PCI_FUNC(pdev->devfn)]);
   1.914 +	slot->func[PCI_FUNC(pdev->devfn)] = NULL;
   1.915 +	for (i = 0; i < PCI_NUM_FUNC; i++) {
   1.916 +		if (slot->func[i] != NULL)
   1.917 +			goto out;
   1.918 +	}
   1.919 +
   1.920 +	pci_iomul_slot_del_locked(sw, slot);
   1.921 +	pci_iomul_slot_put(slot);
   1.922 +
   1.923 +out:
   1.924 +	mutex_unlock(&sw->lock);
   1.925 +	pci_iomul_switch_put(sw);
   1.926 +	return 0;
   1.927 +}
   1.928 +
   1.929 +static int __devinit __pci_iomul_notifier_del_switch(struct pci_dev *pdev)
   1.930 +{
   1.931 +	struct pci_iomul_switch *sw;
   1.932 +
   1.933 +	mutex_lock(&switch_list_lock);
   1.934 +	sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
   1.935 +					  pdev->bus->number);
   1.936 +	if (sw == NULL)
   1.937 +		goto out;
   1.938 +
   1.939 +	pci_iomul_switch_del_locked(sw);
   1.940 +
   1.941 +	mutex_lock(&sw->lock);
   1.942 +	if (sw->io_resource.parent)
   1.943 +		release_resource(&sw->io_resource);
   1.944 +	sw->io_base = 0;	/* to tell this switch is removed */
   1.945 +	sw->io_limit = 0;
   1.946 +	BUG_ON(!list_empty(&sw->slots));
   1.947 +	mutex_unlock(&sw->lock);
   1.948 +
   1.949 +out:
   1.950 +	mutex_unlock(&switch_list_lock);
   1.951 +	pci_iomul_switch_put(sw);
   1.952 +	return 0;
   1.953 +}
   1.954 +
   1.955 +static int __devinit pci_iomul_notifier_del_device(struct pci_dev *pdev)
   1.956 +{
   1.957 +	int ret;
   1.958 +	switch (pdev->hdr_type) {
   1.959 +	case PCI_HEADER_TYPE_NORMAL:
   1.960 +		ret = __pci_iomul_notifier_del_device(pdev);
   1.961 +		break;
   1.962 +	case PCI_HEADER_TYPE_BRIDGE:
   1.963 +		ret = __pci_iomul_notifier_del_switch(pdev);
   1.964 +		break;
   1.965 +	default:
   1.966 +                printk(KERN_WARNING "PCI IOMUL: "
   1.967 +		       "device %s has unknown header type %02x, ignoring.\n",
   1.968 +		       pci_name(pdev), pdev->hdr_type);
   1.969 +		ret = -EIO;
   1.970 +		break;
   1.971 +	}
   1.972 +	return ret;
   1.973 +}
   1.974 +
   1.975 +static int __devinit pci_iomul_notifier(struct notifier_block *nb,
   1.976 +					unsigned long action, void *data)
   1.977 +{
   1.978 +        struct device *dev = data;
   1.979 +        struct pci_dev *pdev = to_pci_dev(dev);
   1.980 +
   1.981 +        switch (action) {
   1.982 +	case BUS_NOTIFY_ADD_DEVICE:
   1.983 +		quirk_iomul_reassign_ioresource(pdev);
   1.984 +		break;
   1.985 +	case BUS_NOTIFY_DEL_DEVICE:
   1.986 +		return pci_iomul_notifier_del_device(pdev);
   1.987 +	default:
   1.988 +		/* nothing */
   1.989 +		break;
   1.990 +	}
   1.991 +
   1.992 +	return 0;
   1.993 +}
   1.994 +
   1.995 +static struct notifier_block pci_iomul_nb = {
   1.996 +        .notifier_call = pci_iomul_notifier,
   1.997 +};
   1.998 +
   1.999 +static int __init pci_iomul_hotplug_init(void)
  1.1000 +{
  1.1001 +        bus_register_notifier(&pci_bus_type, &pci_iomul_nb);
  1.1002 +	return 0;
  1.1003 +}
  1.1004 +
  1.1005 +late_initcall(pci_iomul_hotplug_init);
  1.1006 +#endif
  1.1007 +
  1.1008 +/*****************************************************************************/
  1.1009 +struct pci_iomul_data {
  1.1010 +	struct mutex lock;
  1.1011 +
  1.1012 +	struct pci_dev *pdev;
  1.1013 +	struct pci_iomul_switch *sw;
  1.1014 +	struct pci_iomul_slot *slot;	/* slot::kref */
  1.1015 +	struct pci_iomul_func **func;	/* when dereferencing,
  1.1016 +					   sw->lock is necessary */
  1.1017 +};
  1.1018 +
  1.1019 +static int pci_iomul_func_ioport(struct pci_iomul_func *func,
  1.1020 +				 uint8_t bar, uint64_t offset, int *port)
  1.1021 +{
  1.1022 +	if (!(func->io_bar & (1 << bar)))
  1.1023 +		return -EINVAL;
  1.1024 +
  1.1025 +	*port = func->resource[bar].start + offset;
  1.1026 +	if (*port < func->resource[bar].start ||
  1.1027 +	    *port > func->resource[bar].end)
  1.1028 +		return -EINVAL;
  1.1029 +
  1.1030 +	return 0;
  1.1031 +}
  1.1032 +
  1.1033 +static inline int pci_iomul_valid(struct pci_iomul_data *iomul)
  1.1034 +{
  1.1035 +	BUG_ON(!mutex_is_locked(&iomul->lock));
  1.1036 +	BUG_ON(!mutex_is_locked(&iomul->sw->lock));
  1.1037 +	return pci_iomul_switch_io_allocated(iomul->sw) &&
  1.1038 +		*iomul->func != NULL;
  1.1039 +}
  1.1040 +
  1.1041 +static void __pci_iomul_enable_io(struct pci_dev *pdev)
  1.1042 +{
  1.1043 +	uint16_t cmd;
  1.1044 +
  1.1045 +	pci_dev_get(pdev);
  1.1046 +	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  1.1047 +	cmd |= PCI_COMMAND_IO;
  1.1048 +	pci_write_config_word(pdev, PCI_COMMAND, cmd);
  1.1049 +}
  1.1050 +
  1.1051 +static void __pci_iomul_disable_io(struct pci_iomul_data *iomul,
  1.1052 +				   struct pci_dev *pdev)
  1.1053 +{
  1.1054 +	uint16_t cmd;
  1.1055 +
  1.1056 +	if (!pci_iomul_valid(iomul))
  1.1057 +		return;
  1.1058 +
  1.1059 +	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  1.1060 +	cmd &= ~PCI_COMMAND_IO;
  1.1061 +	pci_write_config_word(pdev, PCI_COMMAND, cmd);
  1.1062 +	pci_dev_put(pdev);
  1.1063 +}
  1.1064 +
  1.1065 +static int pci_iomul_open(struct inode *inode, struct file *filp)
  1.1066 +{
  1.1067 +	struct pci_iomul_data *iomul;
  1.1068 +	iomul = kmalloc(sizeof(*iomul), GFP_KERNEL);
  1.1069 +	if (iomul == NULL)
  1.1070 +		return -ENOMEM;
  1.1071 +
  1.1072 +	mutex_init(&iomul->lock);
  1.1073 +	iomul->pdev = NULL;
  1.1074 +	iomul->sw = NULL;
  1.1075 +	iomul->slot = NULL;
  1.1076 +	iomul->func = NULL;
  1.1077 +	filp->private_data = (void*)iomul;
  1.1078 +
  1.1079 +	return 0;
  1.1080 +}
  1.1081 +
  1.1082 +static int pci_iomul_release(struct inode *inode, struct file *filp)
  1.1083 +{
  1.1084 +	struct pci_iomul_data *iomul =
  1.1085 +		(struct pci_iomul_data*)filp->private_data;
  1.1086 +	struct pci_iomul_switch *sw;
  1.1087 +	struct pci_iomul_slot *slot = NULL;
  1.1088 +
  1.1089 +	mutex_lock(&iomul->lock);
  1.1090 +	sw = iomul->sw;
  1.1091 +	slot = iomul->slot;
  1.1092 +	if (iomul->pdev != NULL) {
  1.1093 +		if (sw != NULL) {
  1.1094 +			mutex_lock(&sw->lock);
  1.1095 +			if (sw->current_pdev == iomul->pdev) {
  1.1096 +				__pci_iomul_disable_io(iomul,
  1.1097 +						       sw->current_pdev);
  1.1098 +				sw->current_pdev = NULL;
  1.1099 +			}
  1.1100 +			sw->count--;
  1.1101 +			if (sw->count == 0) {
  1.1102 +				release_region(sw->io_region->start, sw->io_region->end - sw->io_region->start + 1);
  1.1103 +				sw->io_region = NULL;
  1.1104 +			}
  1.1105 +			mutex_unlock(&sw->lock);
  1.1106 +		}
  1.1107 +		pci_dev_put(iomul->pdev);
  1.1108 +	}
  1.1109 +	mutex_unlock(&iomul->lock);
  1.1110 +
  1.1111 +	if (slot != NULL)
  1.1112 +		pci_iomul_slot_put(slot);
  1.1113 +	if (sw != NULL)
  1.1114 +		pci_iomul_switch_put(sw);
  1.1115 +	kfree(iomul);
  1.1116 +	return 0;
  1.1117 +}
  1.1118 +
  1.1119 +static long pci_iomul_setup(struct pci_iomul_data *iomul,
  1.1120 +			    struct pci_iomul_setup __user *arg)
  1.1121 +{
  1.1122 +	long error = 0;
  1.1123 +	struct pci_iomul_setup setup;
  1.1124 +	struct pci_iomul_switch *sw = NULL;
  1.1125 +	struct pci_iomul_slot *slot;
  1.1126 +	struct pci_bus *pbus;
  1.1127 +	struct pci_dev *pdev;
  1.1128 +
  1.1129 +	if (copy_from_user(&setup, arg, sizeof(setup)))
  1.1130 +		return -EFAULT;
  1.1131 +
  1.1132 +	pbus = pci_find_bus(setup.segment, setup.bus);
  1.1133 +	if (pbus == NULL)
  1.1134 +		return -ENODEV;
  1.1135 +	pdev = pci_get_slot(pbus, setup.dev);
  1.1136 +	if (pdev == NULL)
  1.1137 +		return -ENODEV;
  1.1138 +
  1.1139 +	mutex_lock(&iomul->lock);
  1.1140 +	if (iomul->sw != NULL) {
  1.1141 +		error = -EBUSY;
  1.1142 +		goto out0;
  1.1143 +	}
  1.1144 +
  1.1145 +	pci_iomul_get_lock_switch(pdev, &sw, &slot);
  1.1146 +	if (sw == NULL || slot == NULL) {
  1.1147 +		error = -ENODEV;
  1.1148 +		goto out0;
  1.1149 +	}
  1.1150 +	if (!pci_iomul_switch_io_allocated(sw)) {
  1.1151 +		error = -ENODEV;
  1.1152 +		goto out;
  1.1153 +	}
  1.1154 +
  1.1155 +	if (slot->func[setup.func] == NULL) {
  1.1156 +		error = -ENODEV;
  1.1157 +		goto out;
  1.1158 +	}
  1.1159 +
  1.1160 +	if (sw->count == 0) {
  1.1161 +		BUG_ON(sw->io_region != NULL);
  1.1162 +		sw->io_region =
  1.1163 +			request_region(sw->io_base,
  1.1164 +				       sw->io_limit - sw->io_base + 1,
  1.1165 +				       "PCI IO Multiplexer driver");
  1.1166 +		if (sw->io_region == NULL) {
  1.1167 +			mutex_unlock(&sw->lock);
  1.1168 +			error = -EBUSY;
  1.1169 +			goto out;
  1.1170 +		}
  1.1171 +	}
  1.1172 +	sw->count++;
  1.1173 +	pci_iomul_slot_get(slot);
  1.1174 +
  1.1175 +	iomul->pdev = pdev;
  1.1176 +	iomul->sw = sw;
  1.1177 +	iomul->slot = slot;
  1.1178 +	iomul->func = &slot->func[setup.func];
  1.1179 +
  1.1180 +out:
  1.1181 +	mutex_unlock(&sw->lock);
  1.1182 +out0:
  1.1183 +	mutex_unlock(&iomul->lock);
  1.1184 +	if (error != 0) {
  1.1185 +		if (sw != NULL)
  1.1186 +			pci_iomul_switch_put(sw);
  1.1187 +		pci_dev_put(pdev);
  1.1188 +	}
  1.1189 +	return error;
  1.1190 +}
  1.1191 +
  1.1192 +static int pci_iomul_lock(struct pci_iomul_data *iomul,
  1.1193 +			  struct pci_iomul_switch **sw,
  1.1194 +			  struct pci_iomul_func **func)
  1.1195 +{
  1.1196 +	mutex_lock(&iomul->lock);
  1.1197 +	*sw = iomul->sw;
  1.1198 +	if (*sw == NULL) {
  1.1199 +		mutex_unlock(&iomul->lock);
  1.1200 +		return -ENODEV;
  1.1201 +	}
  1.1202 +	mutex_lock(&(*sw)->lock);
  1.1203 +	if (!pci_iomul_valid(iomul)) {
  1.1204 +		mutex_unlock(&(*sw)->lock);
  1.1205 +		mutex_unlock(&iomul->lock);
  1.1206 +		return -ENODEV;
  1.1207 +	}
  1.1208 +	*func = *iomul->func;
  1.1209 +
  1.1210 +	return 0;
  1.1211 +}
  1.1212 +
  1.1213 +static long pci_iomul_disable_io(struct pci_iomul_data *iomul)
  1.1214 +{
  1.1215 +	long error = 0;
  1.1216 +	struct pci_iomul_switch *sw;
  1.1217 +	struct pci_iomul_func *dummy_func;
  1.1218 +	struct pci_dev *pdev;
  1.1219 +
  1.1220 +	if (pci_iomul_lock(iomul, &sw, &dummy_func) < 0)
  1.1221 +		return -ENODEV;
  1.1222 +
  1.1223 +	pdev = iomul->pdev;
  1.1224 +	if (pdev == NULL)
  1.1225 +		error = -ENODEV;
  1.1226 +
  1.1227 +	if (pdev != NULL && sw->current_pdev == pdev) {
  1.1228 +		__pci_iomul_disable_io(iomul, pdev);
  1.1229 +		sw->current_pdev = NULL;
  1.1230 +	}
  1.1231 +
  1.1232 +	mutex_unlock(&sw->lock);
  1.1233 +	mutex_unlock(&iomul->lock);
  1.1234 +	return error;
  1.1235 +}
  1.1236 +
  1.1237 +static void pci_iomul_switch_to(
  1.1238 +	struct pci_iomul_data *iomul, struct pci_iomul_switch *sw,
  1.1239 +	struct pci_dev *next_pdev)
  1.1240 +{
  1.1241 +	if (sw->current_pdev == next_pdev)
  1.1242 +		/* nothing to do */
  1.1243 +		return;
  1.1244 +
  1.1245 +	if (sw->current_pdev != NULL)
  1.1246 +		__pci_iomul_disable_io(iomul, sw->current_pdev);
  1.1247 +
  1.1248 +	__pci_iomul_enable_io(next_pdev);
  1.1249 +	sw->current_pdev = next_pdev;
  1.1250 +}
  1.1251 +
  1.1252 +static long pci_iomul_in(struct pci_iomul_data *iomul,
  1.1253 +			 struct pci_iomul_in __user *arg)
  1.1254 +{
  1.1255 +	struct pci_iomul_in in;
  1.1256 +	struct pci_iomul_switch *sw;
  1.1257 +	struct pci_iomul_func *func;
  1.1258 +
  1.1259 +	long error = 0;
  1.1260 +	int port;
  1.1261 +	uint32_t value = 0;
  1.1262 +
  1.1263 +	if (copy_from_user(&in, arg, sizeof(in)))
  1.1264 +		return -EFAULT;
  1.1265 +
  1.1266 +	if (pci_iomul_lock(iomul, &sw, &func) < 0)
  1.1267 +		return -ENODEV;
  1.1268 +
  1.1269 +	error = pci_iomul_func_ioport(func, in.bar, in.offset, &port);
  1.1270 +	if (error)
  1.1271 +		goto out;
  1.1272 +
  1.1273 +	pci_iomul_switch_to(iomul, sw, iomul->pdev);
  1.1274 +	switch (in.size) {
  1.1275 +	case 4:
  1.1276 +		value = inl(port);
  1.1277 +		break;
  1.1278 +	case 2:
  1.1279 +		value = inw(port);
  1.1280 +		break;
  1.1281 +	case 1:
  1.1282 +		value = inb(port);
  1.1283 +		break;
  1.1284 +	default:
  1.1285 +		error = -EINVAL;
  1.1286 +		break;
  1.1287 +	}
  1.1288 +
  1.1289 +out:
  1.1290 +	mutex_unlock(&sw->lock);
  1.1291 +	mutex_unlock(&iomul->lock);
  1.1292 +
  1.1293 +	if (error == 0 && put_user(value, &arg->value))
  1.1294 +		return -EFAULT;
  1.1295 +	return error;
  1.1296 +}
  1.1297 +
  1.1298 +static long pci_iomul_out(struct pci_iomul_data *iomul,
  1.1299 +			  struct pci_iomul_out __user *arg)
  1.1300 +{
  1.1301 +	struct pci_iomul_in out;
  1.1302 +	struct pci_iomul_switch *sw;
  1.1303 +	struct pci_iomul_func *func;
  1.1304 +
  1.1305 +	long error = 0;
  1.1306 +	int port;
  1.1307 +
  1.1308 +	if (copy_from_user(&out, arg, sizeof(out)))
  1.1309 +		return -EFAULT;
  1.1310 +
  1.1311 +	if (pci_iomul_lock(iomul, &sw, &func) < 0)
  1.1312 +		return -ENODEV;
  1.1313 +
  1.1314 +	error = pci_iomul_func_ioport(func, out.bar, out.offset, &port);
  1.1315 +	if (error)
  1.1316 +		goto out;
  1.1317 +
  1.1318 +	pci_iomul_switch_to(iomul, sw, iomul->pdev);
  1.1319 +	switch (out.size) {
  1.1320 +	case 4:
  1.1321 +		outl(out.value, port);
  1.1322 +		break;
  1.1323 +	case 2:
  1.1324 +		outw(out.value, port);
  1.1325 +		break;
  1.1326 +	case 1:
  1.1327 +		outb(out.value, port);
  1.1328 +		break;
  1.1329 +	default:
  1.1330 +		error = -EINVAL;
  1.1331 +		break;
  1.1332 +	}
  1.1333 +
  1.1334 +out:
  1.1335 +	mutex_unlock(&sw->lock);
  1.1336 +	mutex_unlock(&iomul->lock);
  1.1337 +	return error;
  1.1338 +}
  1.1339 +
  1.1340 +static long pci_iomul_ioctl(struct file *filp,
  1.1341 +			    unsigned int cmd, unsigned long arg)
  1.1342 +{
  1.1343 +	long error;
  1.1344 +	struct pci_iomul_data *iomul =
  1.1345 +		(struct pci_iomul_data*)filp->private_data;
  1.1346 +
  1.1347 +	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  1.1348 +		return -EPERM;
  1.1349 +
  1.1350 +	switch (cmd) {
  1.1351 +	case PCI_IOMUL_SETUP:
  1.1352 +		error = pci_iomul_setup(iomul,
  1.1353 +					(struct pci_iomul_setup __user *)arg);
  1.1354 +		break;
  1.1355 +	case PCI_IOMUL_DISABLE_IO:
  1.1356 +		error = pci_iomul_disable_io(iomul);
  1.1357 +		break;
  1.1358 +	case PCI_IOMUL_IN:
  1.1359 +		error = pci_iomul_in(iomul, (struct pci_iomul_in __user *)arg);
  1.1360 +		break;
  1.1361 +	case PCI_IOMUL_OUT:
  1.1362 +		error = pci_iomul_out(iomul,
  1.1363 +				      (struct pci_iomul_out __user *)arg);
  1.1364 +		break;
  1.1365 +	default:
  1.1366 +		error = -ENOSYS;
  1.1367 +		break;
  1.1368 +	}
  1.1369 +
  1.1370 +	return error;
  1.1371 +}
  1.1372 +
  1.1373 +static const struct file_operations pci_iomul_fops = {
  1.1374 +	.owner = THIS_MODULE,
  1.1375 +
  1.1376 +	.open = pci_iomul_open, /* nonseekable_open */
  1.1377 +	.release = pci_iomul_release,
  1.1378 +
  1.1379 +	.unlocked_ioctl = pci_iomul_ioctl,
  1.1380 +};
  1.1381 +
  1.1382 +static struct miscdevice pci_iomul_miscdev = {
  1.1383 +	.minor = MISC_DYNAMIC_MINOR,
  1.1384 +	.name = "pci_iomul",
  1.1385 +	.fops = &pci_iomul_fops,
  1.1386 +};
  1.1387 +
  1.1388 +static int pci_iomul_init(void)
  1.1389 +{
  1.1390 +	int error;
  1.1391 +	error = misc_register(&pci_iomul_miscdev);
  1.1392 +	if (error != 0) {
  1.1393 +		printk(KERN_ALERT "Couldn't register /dev/misc/pci_iomul");
  1.1394 +		return error;
  1.1395 +	}
  1.1396 +	printk("PCI IO multiplexer device installed.\n");
  1.1397 +	return 0;
  1.1398 +}
  1.1399 +
  1.1400 +#if 0
  1.1401 +static void pci_iomul_cleanup(void)
  1.1402 +{
  1.1403 +	misc_deregister(&pci_iomul_miscdev);
  1.1404 +}
  1.1405 +#endif
  1.1406 +
  1.1407 +/*
  1.1408 + * This must be called after pci fixup final which is called by
  1.1409 + * device_initcall(pci_init).
  1.1410 + */
  1.1411 +late_initcall(pci_iomul_init);
  1.1412 +
  1.1413 +MODULE_LICENSE("GPL");
  1.1414 +MODULE_AUTHOR("Isaku Yamahata <yamahata@valinux.co.jp>");
  1.1415 +MODULE_DESCRIPTION("PCI IO space multiplexing driver");