2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * Copyright (c) 2009 Isaku Yamahata
17 * VA Linux Systems Japan K.K.
21 #include <linux/kernel.h>
22 #include <linux/list.h>
24 #include <linux/miscdevice.h>
25 #include <linux/pci.h>
26 #include <linux/sort.h>
28 #include <asm/setup.h>
29 #include <asm/uaccess.h>
34 #define PCI_NUM_BARS 6
35 #define PCI_BUS_MAX 255
36 #define PCI_DEV_MAX 31
37 #define PCI_FUNC_MAX 7
38 #define PCI_NUM_FUNC 8
40 /* see pci_resource_len */
41 static inline resource_size_t pci_iomul_len(const struct resource* r)
43 if (r->start == 0 && r->start == r->end)
45 return r->end - r->start + 1;
48 #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
49 /* stolen from pbus_size_io() */
50 static unsigned long pdev_size_io(struct pci_dev *pdev)
52 unsigned long size = 0, size1 = 0;
55 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
56 struct resource *r = &pdev->resource[i];
59 if (!(r->flags & IORESOURCE_IO))
62 r_size = r->end - r->start + 1;
65 /* Might be re-aligned for ISA */
71 /* To be fixed in 2.5: we should have sort of HAVE_ISA
72 flag in the struct pci_bus. */
73 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
74 size = (size & 0xff) + ((size & ~0xffUL) << 2);
76 size = ROUND_UP(size + size1, 4096);
81 * primary bus number of PCI-PCI bridge in switch on which
83 * i.e. the primary bus number of PCI-PCI bridge of downstream port
84 * or root port in switch.
85 * the secondary bus number of PCI-PCI bridge of upstream port
88 static inline unsigned char pci_dev_switch_busnr(struct pci_dev *pdev)
90 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
91 return pdev->bus->primary;
92 return pdev->bus->number;
95 struct pci_iomul_func {
100 /* only start and end are used */
101 unsigned long io_size;
103 struct resource resource[PCI_NUM_BARS];
104 struct resource dummy_parent;
107 struct pci_iomul_switch {
108 struct list_head list; /* bus_list_lock protects */
111 * This lock the following entry and following
112 * pci_iomul_slot/pci_iomul_func.
117 struct resource io_resource;
118 struct resource *io_region;
120 struct pci_dev *current_pdev;
128 /* func which has the largeset io size*/
129 struct pci_iomul_func *func;
131 struct list_head slots;
134 struct pci_iomul_slot {
135 struct list_head sibling;
139 * when pcie, the primary busnr of the PCI-PCI bridge on which
142 uint8_t switch_busnr;
143 struct resource dummy_parent[PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES];
150 struct pci_iomul_func *func[PCI_NUM_FUNC];
153 static LIST_HEAD(switch_list);
154 static DEFINE_MUTEX(switch_list_lock);
156 /*****************************************************************************/
157 static int inline pci_iomul_switch_io_allocated(
158 const struct pci_iomul_switch *sw)
160 return !(sw->io_base == 0 || sw->io_base > sw->io_limit);
163 static struct pci_iomul_switch *pci_iomul_find_switch_locked(int segment,
166 struct pci_iomul_switch *sw;
168 BUG_ON(!mutex_is_locked(&switch_list_lock));
169 list_for_each_entry(sw, &switch_list, list) {
170 if (sw->segment == segment && sw->bus == bus)
176 static struct pci_iomul_slot *pci_iomul_find_slot_locked(
177 struct pci_iomul_switch *sw, uint8_t busnr, uint8_t dev)
179 struct pci_iomul_slot *slot;
181 BUG_ON(!mutex_is_locked(&sw->lock));
182 list_for_each_entry(slot, &sw->slots, sibling) {
183 if (slot->bus == busnr && slot->dev == dev)
189 static void pci_iomul_switch_get(struct pci_iomul_switch *sw);
190 /* on successfull exit, sw->lock is locked for use slot and
191 * refrence count of sw is incremented.
193 static void pci_iomul_get_lock_switch(struct pci_dev *pdev,
194 struct pci_iomul_switch **swp,
195 struct pci_iomul_slot **slot)
197 mutex_lock(&switch_list_lock);
199 *swp = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
200 pci_dev_switch_busnr(pdev));
206 mutex_lock(&(*swp)->lock);
207 *slot = pci_iomul_find_slot_locked(*swp, pdev->bus->number,
208 PCI_SLOT(pdev->devfn));
210 mutex_unlock(&(*swp)->lock);
213 pci_iomul_switch_get(*swp);
216 mutex_unlock(&switch_list_lock);
219 static struct pci_iomul_switch *pci_iomul_switch_alloc(int segment,
222 struct pci_iomul_switch *sw;
224 BUG_ON(!mutex_is_locked(&switch_list_lock));
226 sw = kmalloc(sizeof(*sw), GFP_KERNEL);
228 mutex_init(&sw->lock);
229 kref_init(&sw->kref);
230 sw->io_region = NULL;
232 sw->current_pdev = NULL;
233 sw->segment = segment;
238 INIT_LIST_HEAD(&sw->slots);
243 static void pci_iomul_switch_add_locked(struct pci_iomul_switch *sw)
245 BUG_ON(!mutex_is_locked(&switch_list_lock));
246 list_add(&sw->list, &switch_list);
249 #ifdef CONFIG_HOTPLUG_PCI
250 static void pci_iomul_switch_del_locked(struct pci_iomul_switch *sw)
252 BUG_ON(!mutex_is_locked(&switch_list_lock));
257 static void pci_iomul_switch_get(struct pci_iomul_switch *sw)
262 static void pci_iomul_switch_release(struct kref *kref)
264 struct pci_iomul_switch *sw = container_of(kref,
265 struct pci_iomul_switch,
270 static void pci_iomul_switch_put(struct pci_iomul_switch *sw)
272 kref_put(&sw->kref, &pci_iomul_switch_release);
275 static int __devinit pci_iomul_slot_init(struct pci_dev *pdev,
276 struct pci_iomul_slot *slot)
281 rpcap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
283 /* pci device isn't supported */
285 "PCI: sharing io port of non PCIe device %s "
286 "isn't supported. ignoring.\n",
291 pci_read_config_word(pdev, rpcap + PCI_CAP_FLAGS, &cap);
292 switch ((cap & PCI_EXP_FLAGS_TYPE) >> 4) {
293 case PCI_EXP_TYPE_RC_END:
295 "PCI: io port sharing of root complex integrated "
296 "endpoint %s isn't supported. ignoring.\n",
299 case PCI_EXP_TYPE_ENDPOINT:
300 case PCI_EXP_TYPE_LEG_END:
304 "PCI: io port sharing of non endpoint %s "
305 "doesn't make sense. ignoring.\n",
310 kref_init(&slot->kref);
311 slot->switch_busnr = pci_dev_switch_busnr(pdev);
312 slot->segment = pci_domain_nr(pdev->bus);
313 slot->bus = pdev->bus->number;
314 slot->dev = PCI_SLOT(pdev->devfn);
319 static struct pci_iomul_slot *__devinit
320 pci_iomul_slot_alloc(struct pci_dev *pdev)
322 struct pci_iomul_slot *slot;
324 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
328 if (pci_iomul_slot_init(pdev, slot) != 0) {
335 static void pci_iomul_slot_add_locked(struct pci_iomul_switch *sw,
336 struct pci_iomul_slot *slot)
338 BUG_ON(!mutex_is_locked(&sw->lock));
339 list_add(&slot->sibling, &sw->slots);
342 #ifdef CONFIG_HOTPLUG_PCI
343 static void pci_iomul_slot_del_locked(struct pci_iomul_switch *sw,
344 struct pci_iomul_slot *slot)
346 BUG_ON(!mutex_is_locked(&sw->lock));
347 list_del(&slot->sibling);
351 static void pci_iomul_slot_get(struct pci_iomul_slot *slot)
353 kref_get(&slot->kref);
356 static void pci_iomul_slot_release(struct kref *kref)
358 struct pci_iomul_slot *slot = container_of(kref, struct pci_iomul_slot,
363 static void pci_iomul_slot_put(struct pci_iomul_slot *slot)
365 kref_put(&slot->kref, &pci_iomul_slot_release);
368 /*****************************************************************************/
369 static int pci_get_sbd(const char *str,
370 int *segment__, uint8_t *bus__, uint8_t *dev__)
376 if (sscanf(str, "%x:%x:%x", &segment, &bus, &dev) != 3) {
377 if (sscanf(str, "%x:%x", &bus, &dev) == 2)
383 if (segment < 0 || INT_MAX <= segment)
385 if (bus < 0 || PCI_BUS_MAX < bus)
387 if (dev < 0 || PCI_DEV_MAX < dev)
390 *segment__ = segment;
396 static char iomul_param[COMMAND_LINE_SIZE];
397 #define TOKEN_MAX 10 /* SSSS:BB:DD length is 10 */
398 static int pci_is_iomul_dev_param(struct pci_dev *pdev)
404 for (p = &iomul_param[0]; *p != '\0'; p = next_str + 1) {
405 next_str = strchr(p, ',');
406 if (next_str != NULL)
411 if (len > 0 && len <= TOKEN_MAX) {
412 char tmp[TOKEN_MAX+1];
417 strlcpy(tmp, p, len);
418 if (pci_get_sbd(tmp, &seg, &bus, &dev) == 0 &&
419 pci_domain_nr(pdev->bus) == seg &&
420 pdev->bus->number == bus &&
421 PCI_SLOT(pdev->devfn) == dev)
424 if (next_str == NULL)
428 /* check guestcev=<device>+iomul option */
429 return pci_is_iomuldev(pdev);
433 * Format: [<segment>:]<bus>:<dev>[,[<segment>:]<bus>:<dev>[,...]
435 static int __init pci_iomul_param_setup(char *str)
437 if (strlen(str) >= COMMAND_LINE_SIZE)
440 /* parse it after pci bus scanning */
441 strlcpy(iomul_param, str, sizeof(iomul_param));
444 __setup("guestiomuldev=", pci_iomul_param_setup);
446 /*****************************************************************************/
447 static void __devinit pci_iomul_set_bridge_io_window(struct pci_dev *bridge,
458 l = (io_base & 0xff) | ((io_limit & 0xff) << 8);
459 upper16 = ((io_base & 0xffff00) >> 8) |
460 (((io_limit & 0xffff00) >> 8) << 16);
462 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
463 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
464 /* Update lower 16 bits of I/O base/limit. */
465 pci_write_config_word(bridge, PCI_IO_BASE, l);
466 /* Update upper 16 bits of I/O base/limit. */
467 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, upper16);
470 static void __devinit pci_disable_bridge_io_window(struct pci_dev *bridge)
472 /* set base = 0xffffff limit = 0x0 */
473 pci_iomul_set_bridge_io_window(bridge, 0xffffff, 0);
476 static int __devinit pci_iomul_func_scan(struct pci_dev *pdev,
477 struct pci_iomul_slot *slot,
480 struct pci_iomul_func *f;
483 f = kzalloc(sizeof(*f), GFP_KERNEL);
487 f->segment = slot->segment;
489 f->devfn = PCI_DEVFN(slot->dev, func);
490 f->io_size = pdev_size_io(pdev);
492 for (i = 0; i < PCI_NUM_BARS; i++) {
493 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO))
495 if (pci_resource_len(pdev, i) == 0)
499 f->resource[i] = pdev->resource[i];
503 slot->func[func] = f;
510 * This is tricky part.
511 * fake PCI resource assignment routines by setting flags to 0.
512 * PCI resource allocate routines think the resource should
513 * be allocated by checking flags. 0 means this resource isn't used.
514 * See pbus_size_io() and pdev_sort_resources().
516 * After allocated resources, flags (IORESOURCE_IO) is exported
517 * to other part including user process.
518 * So we have to set flags to IORESOURCE_IO, but at the same time
519 * we must prevent those resources from reassigning when pci hot plug.
520 * To achieve that, set r->parent to dummy resource.
522 static void __devinit pci_iomul_disable_resource(struct resource *r)
524 /* don't allocate this resource */
528 static void __devinit pci_iomul_reenable_resource(
529 struct resource *dummy_parent, struct resource *r)
533 dummy_parent->start = r->start;
534 dummy_parent->end = r->end;
535 dummy_parent->flags = r->flags;
536 dummy_parent->name = "PCI IOMUL dummy resource";
538 ret = request_resource(dummy_parent, r);
542 static void __devinit pci_iomul_fixup_ioresource(struct pci_dev *pdev,
543 struct pci_iomul_func *func,
544 int reassign, int dealloc)
549 printk(KERN_INFO "PCI: deallocating io resource[%s]. io size 0x%lx\n",
550 pci_name(pdev), func->io_size);
551 for (i = 0; i < PCI_NUM_BARS; i++) {
552 r = &pdev->resource[i];
553 if (!(func->io_bar & (1 << i)))
559 pci_update_resource(pdev, i);
560 func->resource[i] = *r;
564 /* don't allocate this resource */
565 pci_iomul_disable_resource(r);
568 /* parent PCI-PCI bridge */
571 pdev = pdev->bus->self;
572 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
574 pci_disable_bridge_io_window(pdev);
575 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
576 r = &pdev->resource[i];
577 if (!(r->flags & IORESOURCE_IO))
582 if (i < PCI_BRIDGE_RESOURCES)
583 pci_update_resource(pdev, i);
587 static void __devinit __quirk_iomul_dealloc_ioresource(
588 struct pci_iomul_switch *sw,
589 struct pci_dev *pdev, struct pci_iomul_slot *slot)
591 struct pci_iomul_func *f;
592 struct pci_iomul_func *__f;
594 if (pci_iomul_func_scan(pdev, slot, PCI_FUNC(pdev->devfn)) != 0)
597 f = slot->func[PCI_FUNC(pdev->devfn)];
602 /* sw->io_base == 0 means that we are called at boot time.
603 * != 0 means that we are called by php after boot. */
604 if (sw->io_base == 0 &&
605 (__f == NULL || __f->io_size < f->io_size)) {
607 struct pci_bus *__pbus;
608 struct pci_dev *__pdev;
610 __pbus = pci_find_bus(__f->segment, __f->bus);
611 BUG_ON(__pbus == NULL);
612 __pdev = pci_get_slot(__pbus, __f->devfn);
613 BUG_ON(__pdev == NULL);
614 pci_iomul_fixup_ioresource(__pdev, __f, 0, 1);
618 pci_iomul_fixup_ioresource(pdev, f, 1, 0);
621 pci_iomul_fixup_ioresource(pdev, f, 1, 1);
625 static void __devinit quirk_iomul_dealloc_ioresource(struct pci_dev *pdev)
627 struct pci_iomul_switch *sw;
628 struct pci_iomul_slot *slot;
630 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
632 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
633 return; /* PCI Host Bridge isn't a target device */
634 if (!pci_is_iomul_dev_param(pdev))
637 mutex_lock(&switch_list_lock);
638 sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
639 pci_dev_switch_busnr(pdev));
641 sw = pci_iomul_switch_alloc(pci_domain_nr(pdev->bus),
642 pci_dev_switch_busnr(pdev));
644 mutex_unlock(&switch_list_lock);
646 "PCI: can't allocate memory "
647 "for sw of IO mulplexing %s", pci_name(pdev));
650 pci_iomul_switch_add_locked(sw);
652 pci_iomul_switch_get(sw);
653 mutex_unlock(&switch_list_lock);
655 mutex_lock(&sw->lock);
656 slot = pci_iomul_find_slot_locked(sw, pdev->bus->number,
657 PCI_SLOT(pdev->devfn));
659 slot = pci_iomul_slot_alloc(pdev);
661 mutex_unlock(&sw->lock);
662 pci_iomul_switch_put(sw);
663 printk(KERN_WARNING "PCI: can't allocate memory "
664 "for IO mulplexing %s", pci_name(pdev));
667 pci_iomul_slot_add_locked(sw, slot);
670 printk(KERN_INFO "PCI: disable device and release io resource[%s].\n",
672 pci_disable_device(pdev);
674 __quirk_iomul_dealloc_ioresource(sw, pdev, slot);
676 mutex_unlock(&sw->lock);
677 pci_iomul_switch_put(sw);
679 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID,
680 quirk_iomul_dealloc_ioresource);
682 static void __devinit pci_iomul_read_bridge_io(struct pci_iomul_switch *sw)
684 struct pci_iomul_func *f = sw->func;
686 struct pci_bus *pbus;
687 struct pci_dev *pdev;
688 struct pci_dev *bridge;
691 uint16_t base_upper16;
692 uint16_t limit_upper16;
696 pbus = pci_find_bus(f->segment, f->bus);
697 BUG_ON(pbus == NULL);
699 pdev = pci_get_slot(pbus, f->devfn);
700 BUG_ON(pdev == NULL);
702 bridge = pdev->bus->self;
703 pci_read_config_word(bridge, PCI_IO_BASE, &l);
704 pci_read_config_word(bridge, PCI_IO_BASE_UPPER16, &base_upper16);
705 pci_read_config_word(bridge, PCI_IO_LIMIT_UPPER16, &limit_upper16);
707 io_base = (l & 0xf0) | ((uint32_t)base_upper16 << 8);
709 io_limit = (l >> 8) | ((uint32_t)limit_upper16 << 8);
713 sw->io_base = io_base;
714 sw->io_limit = io_limit;
717 printk(KERN_INFO "PCI: bridge %s base 0x%x limit 0x%x\n",
718 pci_name(bridge), sw->io_base, sw->io_limit);
721 static void __devinit pci_iomul_setup_brige(struct pci_dev *bridge,
727 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_HOST)
730 pci_iomul_set_bridge_io_window(bridge, io_base, io_limit);
732 /* and forcibly enables IO */
733 pci_read_config_word(bridge, PCI_COMMAND, &cmd);
734 if (!(cmd & PCI_COMMAND_IO)) {
735 cmd |= PCI_COMMAND_IO;
736 printk(KERN_INFO "PCI: Forcibly Enabling IO %s\n",
738 pci_write_config_word(bridge, PCI_COMMAND, cmd);
747 /* decending order */
748 static int __devinit pci_iomul_bar_cmp(const void *lhs__, const void *rhs__)
750 const struct __bar *lhs = (struct __bar*)lhs__;
751 const struct __bar *rhs = (struct __bar*)rhs__;
752 return - (lhs->size - rhs->size);
755 static void __devinit pci_iomul_setup_dev(struct pci_dev *pdev,
756 struct pci_iomul_func *f,
759 struct __bar bars[PCI_NUM_BARS];
761 uint8_t num_bars = 0;
764 printk(KERN_INFO "PCI: Forcibly assign IO %s from 0x%x\n",
765 pci_name(pdev), io_base);
767 for (i = 0; i < PCI_NUM_BARS; i++) {
768 if (!(f->io_bar & (1 << i)))
772 bars[num_bars].size = pci_iomul_len(r);
773 bars[num_bars].bar = i;
778 sort(bars, num_bars, sizeof(bars[0]), &pci_iomul_bar_cmp, NULL);
780 for (i = 0; i < num_bars; i++) {
781 struct resource *fr = &f->resource[bars[i].bar];
782 r = &pdev->resource[bars[i].bar];
784 BUG_ON(r->start != 0);
788 fr->start = r->start;
791 /* pci_update_resource() check flags. */
792 r->flags = fr->flags;
793 pci_update_resource(pdev, bars[i].bar);
794 pci_iomul_reenable_resource(&f->dummy_parent, r);
796 io_base += bars[i].size;
800 static void __devinit pci_iomul_release_io_resource(
801 struct pci_dev *pdev, struct pci_iomul_switch *sw,
802 struct pci_iomul_slot *slot, struct pci_iomul_func *f)
807 for (i = 0; i < PCI_NUM_BARS; i++) {
808 if (pci_resource_flags(pdev, i) & IORESOURCE_IO &&
809 pdev->resource[i].parent != NULL) {
810 r = &pdev->resource[i];
813 pci_iomul_reenable_resource(&f->dummy_parent, r);
817 /* parent PCI-PCI bridge */
818 pdev = pdev->bus->self;
819 if ((pdev->class >> 8) != PCI_CLASS_BRIDGE_HOST) {
820 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
821 struct resource *parent = pdev->resource[i].parent;
823 if (pci_resource_flags(pdev, i) & IORESOURCE_IO &&
825 r = &pdev->resource[i];
827 sw->io_resource.flags = r->flags;
828 sw->io_resource.start = sw->io_base;
829 sw->io_resource.end = sw->io_limit;
830 sw->io_resource.name = "PCI IO Multiplexer";
833 pci_iomul_reenable_resource(
834 &slot->dummy_parent[i - PCI_BRIDGE_RESOURCES], r);
836 if (request_resource(parent,
839 "PCI IOMul: can't allocate "
840 "resource. [0x%x, 0x%x]",
841 sw->io_base, sw->io_limit);
847 static void __devinit quirk_iomul_reassign_ioresource(struct pci_dev *pdev)
849 struct pci_iomul_switch *sw;
850 struct pci_iomul_slot *slot;
851 struct pci_iomul_func *sf;
852 struct pci_iomul_func *f;
854 pci_iomul_get_lock_switch(pdev, &sw, &slot);
855 if (sw == NULL || slot == NULL)
858 if (sw->io_base == 0)
859 pci_iomul_read_bridge_io(sw);
860 if (!pci_iomul_switch_io_allocated(sw))
864 f = slot->func[PCI_FUNC(pdev->devfn)];
866 /* (sf == NULL || f == NULL) case
867 * can happen when all the specified devices
868 * don't have io space
873 (pci_domain_nr(pdev->bus) != sf->segment ||
874 pdev->bus->number != sf->bus ||
875 PCI_SLOT(pdev->devfn) != PCI_SLOT(sf->devfn)) &&
876 PCI_FUNC(pdev->devfn) == 0) {
877 pci_iomul_setup_brige(pdev->bus->self,
878 sw->io_base, sw->io_limit);
881 BUG_ON(f->io_size > sw->io_limit - sw->io_base + 1);
884 pci_domain_nr(pdev->bus) == sf->segment &&
885 pdev->bus->number == sf->bus &&
886 pdev->devfn == sf->devfn)
887 pci_iomul_release_io_resource(pdev, sw, slot, f);
889 pci_iomul_setup_dev(pdev, f, sw->io_base);
892 mutex_unlock(&sw->lock);
893 pci_iomul_switch_put(sw);
896 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID,
897 quirk_iomul_reassign_ioresource);
899 /*****************************************************************************/
900 #ifdef CONFIG_HOTPLUG_PCI
901 static int __devinit __pci_iomul_notifier_del_device(struct pci_dev *pdev)
903 struct pci_iomul_switch *sw;
904 struct pci_iomul_slot *slot;
907 pci_iomul_get_lock_switch(pdev, &sw, &slot);
908 if (sw == NULL || slot == NULL)
911 if (sw->func == slot->func[PCI_FUNC(pdev->devfn)])
913 kfree(slot->func[PCI_FUNC(pdev->devfn)]);
914 slot->func[PCI_FUNC(pdev->devfn)] = NULL;
915 for (i = 0; i < PCI_NUM_FUNC; i++) {
916 if (slot->func[i] != NULL)
920 pci_iomul_slot_del_locked(sw, slot);
921 pci_iomul_slot_put(slot);
924 mutex_unlock(&sw->lock);
925 pci_iomul_switch_put(sw);
929 static int __devinit __pci_iomul_notifier_del_switch(struct pci_dev *pdev)
931 struct pci_iomul_switch *sw;
933 mutex_lock(&switch_list_lock);
934 sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
939 pci_iomul_switch_del_locked(sw);
941 mutex_lock(&sw->lock);
942 if (sw->io_resource.parent)
943 release_resource(&sw->io_resource);
944 sw->io_base = 0; /* to tell this switch is removed */
946 BUG_ON(!list_empty(&sw->slots));
947 mutex_unlock(&sw->lock);
950 mutex_unlock(&switch_list_lock);
951 pci_iomul_switch_put(sw);
955 static int __devinit pci_iomul_notifier_del_device(struct pci_dev *pdev)
958 switch (pdev->hdr_type) {
959 case PCI_HEADER_TYPE_NORMAL:
960 ret = __pci_iomul_notifier_del_device(pdev);
962 case PCI_HEADER_TYPE_BRIDGE:
963 ret = __pci_iomul_notifier_del_switch(pdev);
966 printk(KERN_WARNING "PCI IOMUL: "
967 "device %s has unknown header type %02x, ignoring.\n",
968 pci_name(pdev), pdev->hdr_type);
975 static int __devinit pci_iomul_notifier(struct notifier_block *nb,
976 unsigned long action, void *data)
978 struct device *dev = data;
979 struct pci_dev *pdev = to_pci_dev(dev);
982 case BUS_NOTIFY_ADD_DEVICE:
983 quirk_iomul_reassign_ioresource(pdev);
985 case BUS_NOTIFY_DEL_DEVICE:
986 return pci_iomul_notifier_del_device(pdev);
995 static struct notifier_block pci_iomul_nb = {
996 .notifier_call = pci_iomul_notifier,
999 static int __init pci_iomul_hotplug_init(void)
1001 bus_register_notifier(&pci_bus_type, &pci_iomul_nb);
1005 late_initcall(pci_iomul_hotplug_init);
1008 /*****************************************************************************/
1009 struct pci_iomul_data {
1012 struct pci_dev *pdev;
1013 struct pci_iomul_switch *sw;
1014 struct pci_iomul_slot *slot; /* slot::kref */
1015 struct pci_iomul_func **func; /* when dereferencing,
1016 sw->lock is necessary */
1019 static int pci_iomul_func_ioport(struct pci_iomul_func *func,
1020 uint8_t bar, uint64_t offset, int *port)
1022 if (!(func->io_bar & (1 << bar)))
1025 *port = func->resource[bar].start + offset;
1026 if (*port < func->resource[bar].start ||
1027 *port > func->resource[bar].end)
1033 static inline int pci_iomul_valid(struct pci_iomul_data *iomul)
1035 BUG_ON(!mutex_is_locked(&iomul->lock));
1036 BUG_ON(!mutex_is_locked(&iomul->sw->lock));
1037 return pci_iomul_switch_io_allocated(iomul->sw) &&
1038 *iomul->func != NULL;
1041 static void __pci_iomul_enable_io(struct pci_dev *pdev)
1046 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1047 cmd |= PCI_COMMAND_IO;
1048 pci_write_config_word(pdev, PCI_COMMAND, cmd);
1051 static void __pci_iomul_disable_io(struct pci_iomul_data *iomul,
1052 struct pci_dev *pdev)
1056 if (!pci_iomul_valid(iomul))
1059 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1060 cmd &= ~PCI_COMMAND_IO;
1061 pci_write_config_word(pdev, PCI_COMMAND, cmd);
1065 static int pci_iomul_open(struct inode *inode, struct file *filp)
1067 struct pci_iomul_data *iomul;
1068 iomul = kmalloc(sizeof(*iomul), GFP_KERNEL);
1072 mutex_init(&iomul->lock);
1077 filp->private_data = (void*)iomul;
1082 static int pci_iomul_release(struct inode *inode, struct file *filp)
1084 struct pci_iomul_data *iomul =
1085 (struct pci_iomul_data*)filp->private_data;
1086 struct pci_iomul_switch *sw;
1087 struct pci_iomul_slot *slot = NULL;
1089 mutex_lock(&iomul->lock);
1092 if (iomul->pdev != NULL) {
1094 mutex_lock(&sw->lock);
1095 if (sw->current_pdev == iomul->pdev) {
1096 __pci_iomul_disable_io(iomul,
1098 sw->current_pdev = NULL;
1101 if (sw->count == 0) {
1102 release_region(sw->io_region->start, sw->io_region->end - sw->io_region->start + 1);
1103 sw->io_region = NULL;
1105 mutex_unlock(&sw->lock);
1107 pci_dev_put(iomul->pdev);
1109 mutex_unlock(&iomul->lock);
1112 pci_iomul_slot_put(slot);
1114 pci_iomul_switch_put(sw);
1119 static long pci_iomul_setup(struct pci_iomul_data *iomul,
1120 struct pci_iomul_setup __user *arg)
1123 struct pci_iomul_setup setup;
1124 struct pci_iomul_switch *sw = NULL;
1125 struct pci_iomul_slot *slot;
1126 struct pci_bus *pbus;
1127 struct pci_dev *pdev;
1129 if (copy_from_user(&setup, arg, sizeof(setup)))
1132 pbus = pci_find_bus(setup.segment, setup.bus);
1135 pdev = pci_get_slot(pbus, setup.dev);
1139 mutex_lock(&iomul->lock);
1140 if (iomul->sw != NULL) {
1145 pci_iomul_get_lock_switch(pdev, &sw, &slot);
1146 if (sw == NULL || slot == NULL) {
1150 if (!pci_iomul_switch_io_allocated(sw)) {
1155 if (slot->func[setup.func] == NULL) {
1160 if (sw->count == 0) {
1161 BUG_ON(sw->io_region != NULL);
1163 request_region(sw->io_base,
1164 sw->io_limit - sw->io_base + 1,
1165 "PCI IO Multiplexer driver");
1166 if (sw->io_region == NULL) {
1167 mutex_unlock(&sw->lock);
1173 pci_iomul_slot_get(slot);
1178 iomul->func = &slot->func[setup.func];
1181 mutex_unlock(&sw->lock);
1183 mutex_unlock(&iomul->lock);
1186 pci_iomul_switch_put(sw);
1192 static int pci_iomul_lock(struct pci_iomul_data *iomul,
1193 struct pci_iomul_switch **sw,
1194 struct pci_iomul_func **func)
1196 mutex_lock(&iomul->lock);
1199 mutex_unlock(&iomul->lock);
1202 mutex_lock(&(*sw)->lock);
1203 if (!pci_iomul_valid(iomul)) {
1204 mutex_unlock(&(*sw)->lock);
1205 mutex_unlock(&iomul->lock);
1208 *func = *iomul->func;
1213 static long pci_iomul_disable_io(struct pci_iomul_data *iomul)
1216 struct pci_iomul_switch *sw;
1217 struct pci_iomul_func *dummy_func;
1218 struct pci_dev *pdev;
1220 if (pci_iomul_lock(iomul, &sw, &dummy_func) < 0)
1227 if (pdev != NULL && sw->current_pdev == pdev) {
1228 __pci_iomul_disable_io(iomul, pdev);
1229 sw->current_pdev = NULL;
1232 mutex_unlock(&sw->lock);
1233 mutex_unlock(&iomul->lock);
1237 static void pci_iomul_switch_to(
1238 struct pci_iomul_data *iomul, struct pci_iomul_switch *sw,
1239 struct pci_dev *next_pdev)
1241 if (sw->current_pdev == next_pdev)
1245 if (sw->current_pdev != NULL)
1246 __pci_iomul_disable_io(iomul, sw->current_pdev);
1248 __pci_iomul_enable_io(next_pdev);
1249 sw->current_pdev = next_pdev;
1252 static long pci_iomul_in(struct pci_iomul_data *iomul,
1253 struct pci_iomul_in __user *arg)
1255 struct pci_iomul_in in;
1256 struct pci_iomul_switch *sw;
1257 struct pci_iomul_func *func;
1263 if (copy_from_user(&in, arg, sizeof(in)))
1266 if (pci_iomul_lock(iomul, &sw, &func) < 0)
1269 error = pci_iomul_func_ioport(func, in.bar, in.offset, &port);
1273 pci_iomul_switch_to(iomul, sw, iomul->pdev);
1290 mutex_unlock(&sw->lock);
1291 mutex_unlock(&iomul->lock);
1293 if (error == 0 && put_user(value, &arg->value))
1298 static long pci_iomul_out(struct pci_iomul_data *iomul,
1299 struct pci_iomul_out __user *arg)
1301 struct pci_iomul_in out;
1302 struct pci_iomul_switch *sw;
1303 struct pci_iomul_func *func;
1308 if (copy_from_user(&out, arg, sizeof(out)))
1311 if (pci_iomul_lock(iomul, &sw, &func) < 0)
1314 error = pci_iomul_func_ioport(func, out.bar, out.offset, &port);
1318 pci_iomul_switch_to(iomul, sw, iomul->pdev);
1321 outl(out.value, port);
1324 outw(out.value, port);
1327 outb(out.value, port);
1335 mutex_unlock(&sw->lock);
1336 mutex_unlock(&iomul->lock);
1340 static long pci_iomul_ioctl(struct file *filp,
1341 unsigned int cmd, unsigned long arg)
1344 struct pci_iomul_data *iomul =
1345 (struct pci_iomul_data*)filp->private_data;
1347 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1351 case PCI_IOMUL_SETUP:
1352 error = pci_iomul_setup(iomul,
1353 (struct pci_iomul_setup __user *)arg);
1355 case PCI_IOMUL_DISABLE_IO:
1356 error = pci_iomul_disable_io(iomul);
1359 error = pci_iomul_in(iomul, (struct pci_iomul_in __user *)arg);
1362 error = pci_iomul_out(iomul,
1363 (struct pci_iomul_out __user *)arg);
1373 static const struct file_operations pci_iomul_fops = {
1374 .owner = THIS_MODULE,
1376 .open = pci_iomul_open, /* nonseekable_open */
1377 .release = pci_iomul_release,
1379 .unlocked_ioctl = pci_iomul_ioctl,
1382 static struct miscdevice pci_iomul_miscdev = {
1383 .minor = MISC_DYNAMIC_MINOR,
1384 .name = "pci_iomul",
1385 .fops = &pci_iomul_fops,
1388 static int pci_iomul_init(void)
1391 error = misc_register(&pci_iomul_miscdev);
1393 printk(KERN_ALERT "Couldn't register /dev/misc/pci_iomul");
1396 printk("PCI IO multiplexer device installed.\n");
1401 static void pci_iomul_cleanup(void)
1403 misc_deregister(&pci_iomul_miscdev);
1408 * This must be called after pci fixup final which is called by
1409 * device_initcall(pci_init).
1411 late_initcall(pci_iomul_init);
1413 MODULE_LICENSE("GPL");
1414 MODULE_AUTHOR("Isaku Yamahata <yamahata@valinux.co.jp>");
1415 MODULE_DESCRIPTION("PCI IO space multiplexing driver");