ia64/linux-2.6.18-xen.hg

view drivers/pci/iomulti.c @ 883:b998614e2e2a

pci/guestdev: enhance guestdev to accept +iomul.

enhance guestdev to accept +iomul and use it.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 28 09:59:18 2009 +0100 (2009-05-28)
parents 8dec4aa9b8b9
children 20be7f6d414a
line source
1 /*
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.
6 *
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.
11 *
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.
15 *
16 * Copyright (c) 2009 Isaku Yamahata
17 * VA Linux Systems Japan K.K.
18 *
19 */
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/miscdevice.h>
24 #include <linux/pci.h>
25 #include <linux/sort.h>
27 #include <asm/setup.h>
28 #include <asm/uaccess.h>
30 #include "pci.h"
31 #include "iomulti.h"
33 #define PCI_NUM_BARS 6
34 #define PCI_BUS_MAX 255
35 #define PCI_DEV_MAX 31
36 #define PCI_FUNC_MAX 7
37 #define PCI_NUM_FUNC 8
39 /* see pci_resource_len */
40 static inline resource_size_t pci_iomul_len(const struct resource* r)
41 {
42 if (r->start == 0 && r->start == r->end)
43 return 0;
44 return r->end - r->start + 1;
45 }
47 #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
48 /* stolen from pbus_size_io() */
49 static unsigned long pdev_size_io(struct pci_dev *pdev)
50 {
51 unsigned long size = 0, size1 = 0;
52 int i;
54 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
55 struct resource *r = &pdev->resource[i];
56 unsigned long r_size;
58 if (!(r->flags & IORESOURCE_IO))
59 continue;
61 r_size = r->end - r->start + 1;
63 if (r_size < 0x400)
64 /* Might be re-aligned for ISA */
65 size += r_size;
66 else
67 size1 += r_size;
68 }
70 /* To be fixed in 2.5: we should have sort of HAVE_ISA
71 flag in the struct pci_bus. */
72 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
73 size = (size & 0xff) + ((size & ~0xffUL) << 2);
74 #endif
75 size = ROUND_UP(size + size1, 4096);
76 return size;
77 }
79 /*
80 * primary bus number of PCI-PCI bridge in switch on which
81 * this slots sits.
82 * i.e. the primary bus number of PCI-PCI bridge of downstream port
83 * or root port in switch.
84 * the secondary bus number of PCI-PCI bridge of upstream port
85 * in switch.
86 */
87 static inline unsigned char pci_dev_switch_busnr(struct pci_dev *pdev)
88 {
89 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
90 return pdev->bus->primary;
91 return pdev->bus->number;
92 }
94 struct pci_iomul_func {
95 int segment;
96 uint8_t bus;
97 uint8_t devfn;
99 /* only start and end are used */
100 unsigned long io_size;
101 uint8_t io_bar;
102 struct resource resource[PCI_NUM_BARS];
103 struct resource dummy_parent;
104 };
106 struct pci_iomul_switch {
107 struct list_head list; /* bus_list_lock protects */
109 /*
110 * This lock the following entry and following
111 * pci_iomul_slot/pci_iomul_func.
112 */
113 struct mutex lock;
114 struct kref kref;
116 struct resource io_resource;
117 struct resource *io_region;
118 unsigned int count;
119 struct pci_dev *current_pdev;
121 int segment;
122 uint8_t bus;
124 uint32_t io_base;
125 uint32_t io_limit;
127 /* func which has the largeset io size*/
128 struct pci_iomul_func *func;
130 struct list_head slots;
131 };
133 struct pci_iomul_slot {
134 struct list_head sibling;
135 struct kref kref;
136 /*
137 * busnr
138 * when pcie, the primary busnr of the PCI-PCI bridge on which
139 * this devices sits.
140 */
141 uint8_t switch_busnr;
142 struct resource dummy_parent[PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES];
144 /* device */
145 int segment;
146 uint8_t bus;
147 uint8_t dev;
149 struct pci_iomul_func *func[PCI_NUM_FUNC];
150 };
152 static LIST_HEAD(switch_list);
153 static DEFINE_MUTEX(switch_list_lock);
155 /*****************************************************************************/
156 static int inline pci_iomul_switch_io_allocated(
157 const struct pci_iomul_switch *sw)
158 {
159 return !(sw->io_base == 0 || sw->io_base > sw->io_limit);
160 }
162 static struct pci_iomul_switch *pci_iomul_find_switch_locked(int segment,
163 uint8_t bus)
164 {
165 struct pci_iomul_switch *sw;
167 BUG_ON(!mutex_is_locked(&switch_list_lock));
168 list_for_each_entry(sw, &switch_list, list) {
169 if (sw->segment == segment && sw->bus == bus)
170 return sw;
171 }
172 return NULL;
173 }
175 static struct pci_iomul_slot *pci_iomul_find_slot_locked(
176 struct pci_iomul_switch *sw, uint8_t busnr, uint8_t dev)
177 {
178 struct pci_iomul_slot *slot;
180 BUG_ON(!mutex_is_locked(&sw->lock));
181 list_for_each_entry(slot, &sw->slots, sibling) {
182 if (slot->bus == busnr && slot->dev == dev)
183 return slot;
184 }
185 return NULL;
186 }
188 static void pci_iomul_switch_get(struct pci_iomul_switch *sw);
189 /* on successfull exit, sw->lock is locked for use slot and
190 * refrence count of sw is incremented.
191 */
192 static void pci_iomul_get_lock_switch(struct pci_dev *pdev,
193 struct pci_iomul_switch **swp,
194 struct pci_iomul_slot **slot)
195 {
196 mutex_lock(&switch_list_lock);
198 *swp = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
199 pci_dev_switch_busnr(pdev));
200 if (*swp == NULL) {
201 *slot = NULL;
202 goto out;
203 }
205 mutex_lock(&(*swp)->lock);
206 *slot = pci_iomul_find_slot_locked(*swp, pdev->bus->number,
207 PCI_SLOT(pdev->devfn));
208 if (*slot == NULL) {
209 mutex_unlock(&(*swp)->lock);
210 *swp = NULL;
211 } else {
212 pci_iomul_switch_get(*swp);
213 }
214 out:
215 mutex_unlock(&switch_list_lock);
216 }
218 static struct pci_iomul_switch *pci_iomul_switch_alloc(int segment,
219 uint8_t bus)
220 {
221 struct pci_iomul_switch *sw;
223 BUG_ON(!mutex_is_locked(&switch_list_lock));
225 sw = kmalloc(sizeof(*sw), GFP_KERNEL);
227 mutex_init(&sw->lock);
228 kref_init(&sw->kref);
229 sw->io_region = NULL;
230 sw->count = 0;
231 sw->current_pdev = NULL;
232 sw->segment = segment;
233 sw->bus = bus;
234 sw->io_base = 0;
235 sw->io_limit = 0;
236 sw->func = NULL;
237 INIT_LIST_HEAD(&sw->slots);
239 return sw;
240 }
242 static void pci_iomul_switch_add_locked(struct pci_iomul_switch *sw)
243 {
244 BUG_ON(!mutex_is_locked(&switch_list_lock));
245 list_add(&sw->list, &switch_list);
246 }
248 #ifdef CONFIG_HOTPLUG_PCI
249 static void pci_iomul_switch_del_locked(struct pci_iomul_switch *sw)
250 {
251 BUG_ON(!mutex_is_locked(&switch_list_lock));
252 list_del(&sw->list);
253 }
254 #endif
256 static void pci_iomul_switch_get(struct pci_iomul_switch *sw)
257 {
258 kref_get(&sw->kref);
259 }
261 static void pci_iomul_switch_release(struct kref *kref)
262 {
263 struct pci_iomul_switch *sw = container_of(kref,
264 struct pci_iomul_switch,
265 kref);
266 kfree(sw);
267 }
269 static void pci_iomul_switch_put(struct pci_iomul_switch *sw)
270 {
271 kref_put(&sw->kref, &pci_iomul_switch_release);
272 }
274 static int __devinit pci_iomul_slot_init(struct pci_dev *pdev,
275 struct pci_iomul_slot *slot)
276 {
277 u16 rpcap;
278 u16 cap;
280 rpcap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
281 if (!rpcap) {
282 /* pci device isn't supported */
283 printk(KERN_INFO
284 "PCI: sharing io port of non PCIe device %s "
285 "isn't supported. ignoring.\n",
286 pci_name(pdev));
287 return -ENOSYS;
288 }
290 pci_read_config_word(pdev, rpcap + PCI_CAP_FLAGS, &cap);
291 switch ((cap & PCI_EXP_FLAGS_TYPE) >> 4) {
292 case PCI_EXP_TYPE_RC_END:
293 printk(KERN_INFO
294 "PCI: io port sharing of root complex integrated "
295 "endpoint %s isn't supported. ignoring.\n",
296 pci_name(pdev));
297 return -ENOSYS;
298 case PCI_EXP_TYPE_ENDPOINT:
299 case PCI_EXP_TYPE_LEG_END:
300 break;
301 default:
302 printk(KERN_INFO
303 "PCI: io port sharing of non endpoint %s "
304 "doesn't make sense. ignoring.\n",
305 pci_name(pdev));
306 return -EINVAL;
307 }
309 kref_init(&slot->kref);
310 slot->switch_busnr = pci_dev_switch_busnr(pdev);
311 slot->segment = pci_domain_nr(pdev->bus);
312 slot->bus = pdev->bus->number;
313 slot->dev = PCI_SLOT(pdev->devfn);
315 return 0;
316 }
318 static struct pci_iomul_slot *pci_iomul_slot_alloc(struct pci_dev *pdev)
319 {
320 struct pci_iomul_slot *slot;
322 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
323 if (slot == NULL)
324 return NULL;
326 if (pci_iomul_slot_init(pdev, slot) != 0) {
327 kfree(slot);
328 return NULL;
329 }
330 return slot;
331 }
333 static void pci_iomul_slot_add_locked(struct pci_iomul_switch *sw,
334 struct pci_iomul_slot *slot)
335 {
336 BUG_ON(!mutex_is_locked(&sw->lock));
337 list_add(&slot->sibling, &sw->slots);
338 }
340 #ifdef CONFIG_HOTPLUG_PCI
341 static void pci_iomul_slot_del_locked(struct pci_iomul_switch *sw,
342 struct pci_iomul_slot *slot)
343 {
344 BUG_ON(!mutex_is_locked(&sw->lock));
345 list_del(&slot->sibling);
346 }
347 #endif
349 static void pci_iomul_slot_get(struct pci_iomul_slot *slot)
350 {
351 kref_get(&slot->kref);
352 }
354 static void pci_iomul_slot_release(struct kref *kref)
355 {
356 struct pci_iomul_slot *slot = container_of(kref, struct pci_iomul_slot,
357 kref);
358 kfree(slot);
359 }
361 static void pci_iomul_slot_put(struct pci_iomul_slot *slot)
362 {
363 kref_put(&slot->kref, &pci_iomul_slot_release);
364 }
366 /*****************************************************************************/
367 static int pci_get_sbd(const char *str,
368 int *segment__, uint8_t *bus__, uint8_t *dev__)
369 {
370 int segment;
371 int bus;
372 int dev;
374 if (sscanf(str, "%x:%x:%x", &segment, &bus, &dev) != 3) {
375 if (sscanf(str, "%x:%x", &bus, &dev) == 2)
376 segment = 0;
377 else
378 return -EINVAL;
379 }
381 if (segment < 0 || INT_MAX <= segment)
382 return -EINVAL;
383 if (bus < 0 || PCI_BUS_MAX < bus)
384 return -EINVAL;
385 if (dev < 0 || PCI_DEV_MAX < dev)
386 return -EINVAL;
388 *segment__ = segment;
389 *bus__ = bus;
390 *dev__ = dev;
391 return 0;
392 }
394 static char iomul_param[COMMAND_LINE_SIZE];
395 #define TOKEN_MAX 10 /* SSSS:BB:DD length is 10 */
396 static int pci_is_iomul_dev_param(struct pci_dev *pdev)
397 {
398 int len;
399 char *p;
400 char *next_str;
402 for (p = &iomul_param[0]; *p != '\0'; p = next_str + 1) {
403 next_str = strchr(p, ',');
404 if (next_str != NULL)
405 len = next_str - p;
406 else
407 len = strlen(p);
409 if (len > 0 && len <= TOKEN_MAX) {
410 char tmp[TOKEN_MAX+1];
411 int seg;
412 uint8_t bus;
413 uint8_t dev;
415 strncpy(tmp, p, len);
416 *(tmp + len) = '\0';
417 if (pci_get_sbd(tmp, &seg, &bus, &dev) == 0 &&
418 pci_domain_nr(pdev->bus) == seg &&
419 pdev->bus->number == bus &&
420 PCI_SLOT(pdev->devfn) == dev)
421 return 1;
422 }
423 if (next_str == NULL)
424 break;
425 }
427 /* check guestcev=<device>+iomul option */
428 return pci_is_iomuldev(pdev);
429 }
431 /*
432 * Format: [<segment>:]<bus>:<dev>[,[<segment>:]<bus>:<dev>[,...]
433 */
434 static int __init pci_iomul_param_setup(char *str)
435 {
436 if (strlen(str) >= COMMAND_LINE_SIZE)
437 return 0;
439 /* parse it after pci bus scanning */
440 strncpy(iomul_param, str, sizeof(iomul_param));
441 return 1;
442 }
443 __setup("guestiomuldev=", pci_iomul_param_setup);
445 /*****************************************************************************/
446 static void __devinit pci_iomul_set_bridge_io_window(struct pci_dev *bridge,
447 uint32_t io_base,
448 uint32_t io_limit)
449 {
450 uint16_t l;
451 uint32_t upper16;
453 io_base >>= 12;
454 io_base <<= 4;
455 io_limit >>= 12;
456 io_limit <<= 4;
457 l = (io_base & 0xff) | ((io_limit & 0xff) << 8);
458 upper16 = ((io_base & 0xffff00) >> 8) |
459 (((io_limit & 0xffff00) >> 8) << 16);
461 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
462 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
463 /* Update lower 16 bits of I/O base/limit. */
464 pci_write_config_word(bridge, PCI_IO_BASE, l);
465 /* Update upper 16 bits of I/O base/limit. */
466 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, upper16);
467 }
469 static void __devinit pci_disable_bridge_io_window(struct pci_dev *bridge)
470 {
471 /* set base = 0xffffff limit = 0x0 */
472 pci_iomul_set_bridge_io_window(bridge, 0xffffff, 0);
473 }
475 static int __devinit pci_iomul_func_scan(struct pci_dev *pdev,
476 struct pci_iomul_slot *slot,
477 uint8_t func)
478 {
479 struct pci_iomul_func *f;
480 unsigned int i;
482 f = kzalloc(sizeof(*f), GFP_KERNEL);
483 if (f == NULL)
484 return -ENOMEM;
486 f->segment = slot->segment;
487 f->bus = slot->bus;
488 f->devfn = PCI_DEVFN(slot->dev, func);
489 f->io_size = pdev_size_io(pdev);
491 for (i = 0; i < PCI_NUM_BARS; i++) {
492 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO))
493 continue;
494 if (pci_resource_len(pdev, i) == 0)
495 continue;
497 f->io_bar |= 1 << i;
498 f->resource[i] = pdev->resource[i];
499 }
501 if (f->io_bar)
502 slot->func[func] = f;
503 else
504 kfree(f);
505 return 0;
506 }
508 /*
509 * This is tricky part.
510 * fake PCI resource assignment routines by setting flags to 0.
511 * PCI resource allocate routines think the resource should
512 * be allocated by checking flags. 0 means this resource isn't used.
513 * See pbus_size_io() and pdev_sort_resources().
514 *
515 * After allocated resources, flags (IORESOURCE_IO) is exported
516 * to other part including user process.
517 * So we have to set flags to IORESOURCE_IO, but at the same time
518 * we must prevent those resources from reassigning when pci hot plug.
519 * To achieve that, set r->parent to dummy resource.
520 */
521 static void __devinit pci_iomul_disable_resource(struct resource *r)
522 {
523 /* don't allocate this resource */
524 r->flags = 0;
525 }
527 static void __devinit pci_iomul_reenable_resource(
528 struct resource *dummy_parent, struct resource *r)
529 {
530 int ret;
532 dummy_parent->start = r->start;
533 dummy_parent->end = r->end;
534 dummy_parent->flags = r->flags;
535 dummy_parent->name = "PCI IOMUL dummy resource";
537 ret = request_resource(dummy_parent, r);
538 BUG_ON(ret);
539 }
541 static void __devinit pci_iomul_fixup_ioresource(struct pci_dev *pdev,
542 struct pci_iomul_func *func,
543 int reassign, int dealloc)
544 {
545 uint8_t i;
546 struct resource *r;
548 printk(KERN_INFO "PCI: deallocating io resource[%s]. io size 0x%lx\n",
549 pci_name(pdev), func->io_size);
550 for (i = 0; i < PCI_NUM_BARS; i++) {
551 r = &pdev->resource[i];
552 if (!(func->io_bar & (1 << i)))
553 continue;
555 if (reassign) {
556 r->end -= r->start;
557 r->start = 0;
558 pci_update_resource(pdev, i);
559 func->resource[i] = *r;
560 }
562 if (dealloc)
563 /* don't allocate this resource */
564 pci_iomul_disable_resource(r);
565 }
567 /* parent PCI-PCI bridge */
568 if (!reassign)
569 return;
570 pdev = pdev->bus->self;
571 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
572 return;
573 pci_disable_bridge_io_window(pdev);
574 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
575 r = &pdev->resource[i];
576 if (!(r->flags & IORESOURCE_IO))
577 continue;
579 r->end -= r->start;
580 r->start = 0;
581 if (i < PCI_BRIDGE_RESOURCES)
582 pci_update_resource(pdev, i);
583 }
584 }
586 static void __devinit __quirk_iomul_dealloc_ioresource(
587 struct pci_iomul_switch *sw,
588 struct pci_dev *pdev, struct pci_iomul_slot *slot)
589 {
590 struct pci_iomul_func *f;
591 struct pci_iomul_func *__f;
593 if (pci_iomul_func_scan(pdev, slot, PCI_FUNC(pdev->devfn)) != 0)
594 return;
596 f = slot->func[PCI_FUNC(pdev->devfn)];
597 if (f == NULL)
598 return;
600 __f = sw->func;
601 /* sw->io_base == 0 means that we are called at boot time.
602 * != 0 means that we are called by php after boot. */
603 if (sw->io_base == 0 &&
604 (__f == NULL || __f->io_size < f->io_size)) {
605 if (__f != NULL) {
606 struct pci_bus *__pbus;
607 struct pci_dev *__pdev;
609 __pbus = pci_find_bus(__f->segment, __f->bus);
610 BUG_ON(__pbus == NULL);
611 __pdev = pci_get_slot(__pbus, __f->devfn);
612 BUG_ON(__pdev == NULL);
613 pci_iomul_fixup_ioresource(__pdev, __f, 0, 1);
614 pci_dev_put(__pdev);
615 }
617 pci_iomul_fixup_ioresource(pdev, f, 1, 0);
618 sw->func = f;
619 } else {
620 pci_iomul_fixup_ioresource(pdev, f, 1, 1);
621 }
622 }
624 static void __devinit quirk_iomul_dealloc_ioresource(struct pci_dev *pdev)
625 {
626 struct pci_iomul_switch *sw;
627 struct pci_iomul_slot *slot;
629 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
630 return;
631 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
632 return; /* PCI Host Bridge isn't a target device */
633 if (!pci_is_iomul_dev_param(pdev))
634 return;
636 mutex_lock(&switch_list_lock);
637 sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
638 pci_dev_switch_busnr(pdev));
639 if (sw == NULL) {
640 sw = pci_iomul_switch_alloc(pci_domain_nr(pdev->bus),
641 pci_dev_switch_busnr(pdev));
642 if (sw == NULL) {
643 mutex_unlock(&switch_list_lock);
644 printk(KERN_WARNING
645 "PCI: can't allocate memory "
646 "for sw of IO mulplexing %s", pci_name(pdev));
647 return;
648 }
649 pci_iomul_switch_add_locked(sw);
650 }
651 pci_iomul_switch_get(sw);
652 mutex_unlock(&switch_list_lock);
654 mutex_lock(&sw->lock);
655 slot = pci_iomul_find_slot_locked(sw, pdev->bus->number,
656 PCI_SLOT(pdev->devfn));
657 if (slot == NULL) {
658 slot = pci_iomul_slot_alloc(pdev);
659 if (slot == NULL) {
660 mutex_unlock(&sw->lock);
661 pci_iomul_switch_put(sw);
662 printk(KERN_WARNING "PCI: can't allocate memory "
663 "for IO mulplexing %s", pci_name(pdev));
664 return;
665 }
666 pci_iomul_slot_add_locked(sw, slot);
667 }
669 printk(KERN_INFO "PCI: disable device and release io resource[%s].\n",
670 pci_name(pdev));
671 pci_disable_device(pdev);
673 __quirk_iomul_dealloc_ioresource(sw, pdev, slot);
675 mutex_unlock(&sw->lock);
676 pci_iomul_switch_put(sw);
677 }
678 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID,
679 quirk_iomul_dealloc_ioresource);
681 static void __devinit pci_iomul_read_bridge_io(struct pci_iomul_switch *sw)
682 {
683 struct pci_iomul_func *f = sw->func;
685 struct pci_bus *pbus;
686 struct pci_dev *pdev;
687 struct pci_dev *bridge;
689 uint16_t l;
690 uint16_t base_upper16;
691 uint16_t limit_upper16;
692 uint32_t io_base;
693 uint32_t io_limit;
695 pbus = pci_find_bus(f->segment, f->bus);
696 BUG_ON(pbus == NULL);
698 pdev = pci_get_slot(pbus, f->devfn);
699 BUG_ON(pdev == NULL);
701 bridge = pdev->bus->self;
702 pci_read_config_word(bridge, PCI_IO_BASE, &l);
703 pci_read_config_word(bridge, PCI_IO_BASE_UPPER16, &base_upper16);
704 pci_read_config_word(bridge, PCI_IO_LIMIT_UPPER16, &limit_upper16);
706 io_base = (l & 0xf0) | ((uint32_t)base_upper16 << 8);
707 io_base <<= 8;
708 io_limit = (l >> 8) | ((uint32_t)limit_upper16 << 8);
709 io_limit <<= 8;
710 io_limit |= 0xfff;
712 sw->io_base = io_base;
713 sw->io_limit = io_limit;
715 pci_dev_put(pdev);
716 printk(KERN_INFO "PCI: bridge %s base 0x%x limit 0x%x\n",
717 pci_name(bridge), sw->io_base, sw->io_limit);
718 }
720 static void __devinit pci_iomul_setup_brige(struct pci_dev *bridge,
721 uint32_t io_base,
722 uint32_t io_limit)
723 {
724 uint16_t cmd;
726 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_HOST)
727 return;
729 pci_iomul_set_bridge_io_window(bridge, io_base, io_limit);
731 /* and forcibly enables IO */
732 pci_read_config_word(bridge, PCI_COMMAND, &cmd);
733 if (!(cmd & PCI_COMMAND_IO)) {
734 cmd |= PCI_COMMAND_IO;
735 printk(KERN_INFO "PCI: Forcibly Enabling IO %s\n",
736 pci_name(bridge));
737 pci_write_config_word(bridge, PCI_COMMAND, cmd);
738 }
739 }
741 struct __bar {
742 unsigned long size;
743 uint8_t bar;
744 };
746 /* decending order */
747 static int __devinit pci_iomul_bar_cmp(const void *lhs__, const void *rhs__)
748 {
749 const struct __bar *lhs = (struct __bar*)lhs__;
750 const struct __bar *rhs = (struct __bar*)rhs__;
751 return - (lhs->size - rhs->size);
752 }
754 static void __devinit pci_iomul_setup_dev(struct pci_dev *pdev,
755 struct pci_iomul_func *f,
756 uint32_t io_base)
757 {
758 struct __bar bars[PCI_NUM_BARS];
759 int i;
760 uint8_t num_bars = 0;
761 struct resource *r;
763 printk(KERN_INFO "PCI: Forcibly assign IO %s from 0x%x\n",
764 pci_name(pdev), io_base);
766 for (i = 0; i < PCI_NUM_BARS; i++) {
767 if (!(f->io_bar & (1 << i)))
768 continue;
770 r = &f->resource[i];
771 bars[num_bars].size = pci_iomul_len(r);
772 bars[num_bars].bar = i;
774 num_bars++;
775 }
777 sort(bars, num_bars, sizeof(bars[0]), &pci_iomul_bar_cmp, NULL);
779 for (i = 0; i < num_bars; i++) {
780 struct resource *fr = &f->resource[bars[i].bar];
781 r = &pdev->resource[bars[i].bar];
783 BUG_ON(r->start != 0);
784 r->start += io_base;
785 r->end += io_base;
787 fr->start = r->start;
788 fr->end = r->end;
790 /* pci_update_resource() check flags. */
791 r->flags = fr->flags;
792 pci_update_resource(pdev, bars[i].bar);
793 pci_iomul_reenable_resource(&f->dummy_parent, r);
795 io_base += bars[i].size;
796 }
797 }
799 static void __devinit pci_iomul_release_io_resource(
800 struct pci_dev *pdev, struct pci_iomul_switch *sw,
801 struct pci_iomul_slot *slot, struct pci_iomul_func *f)
802 {
803 int i;
804 struct resource *r;
806 for (i = 0; i < PCI_NUM_BARS; i++) {
807 if (pci_resource_flags(pdev, i) & IORESOURCE_IO &&
808 pdev->resource[i].parent != NULL) {
809 r = &pdev->resource[i];
810 f->resource[i] = *r;
811 release_resource(r);
812 pci_iomul_reenable_resource(&f->dummy_parent, r);
813 }
814 }
816 /* parent PCI-PCI bridge */
817 pdev = pdev->bus->self;
818 if ((pdev->class >> 8) != PCI_CLASS_BRIDGE_HOST) {
819 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
820 struct resource *parent = pdev->resource[i].parent;
822 if (pci_resource_flags(pdev, i) & IORESOURCE_IO &&
823 parent != NULL) {
824 r = &pdev->resource[i];
826 sw->io_resource.flags = r->flags;
827 sw->io_resource.start = sw->io_base;
828 sw->io_resource.end = sw->io_limit;
829 sw->io_resource.name = "PCI IO Multiplexer";
831 release_resource(r);
832 pci_iomul_reenable_resource(
833 &slot->dummy_parent[i - PCI_BRIDGE_RESOURCES], r);
835 if (request_resource(parent,
836 &sw->io_resource))
837 printk(KERN_ERR
838 "PCI IOMul: can't allocate "
839 "resource. [0x%x, 0x%x]",
840 sw->io_base, sw->io_limit);
841 }
842 }
843 }
844 }
846 static void __devinit quirk_iomul_reassign_ioresource(struct pci_dev *pdev)
847 {
848 struct pci_iomul_switch *sw;
849 struct pci_iomul_slot *slot;
850 struct pci_iomul_func *sf;
851 struct pci_iomul_func *f;
853 pci_iomul_get_lock_switch(pdev, &sw, &slot);
854 if (sw == NULL || slot == NULL)
855 return;
857 if (sw->io_base == 0)
858 pci_iomul_read_bridge_io(sw);
859 if (!pci_iomul_switch_io_allocated(sw))
860 goto out;
862 sf = sw->func;
863 f = slot->func[PCI_FUNC(pdev->devfn)];
864 if (f == NULL)
865 /* (sf == NULL || f == NULL) case
866 * can happen when all the specified devices
867 * don't have io space
868 */
869 goto out;
871 if (sf != NULL &&
872 (pci_domain_nr(pdev->bus) != sf->segment ||
873 pdev->bus->number != sf->bus ||
874 PCI_SLOT(pdev->devfn) != PCI_SLOT(sf->devfn)) &&
875 PCI_FUNC(pdev->devfn) == 0) {
876 pci_iomul_setup_brige(pdev->bus->self,
877 sw->io_base, sw->io_limit);
878 }
880 BUG_ON(f->io_size > sw->io_limit - sw->io_base + 1);
881 if (/* f == sf */
882 sf != NULL &&
883 pci_domain_nr(pdev->bus) == sf->segment &&
884 pdev->bus->number == sf->bus &&
885 pdev->devfn == sf->devfn)
886 pci_iomul_release_io_resource(pdev, sw, slot, f);
887 else
888 pci_iomul_setup_dev(pdev, f, sw->io_base);
890 out:
891 mutex_unlock(&sw->lock);
892 pci_iomul_switch_put(sw);
893 }
895 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID,
896 quirk_iomul_reassign_ioresource);
898 /*****************************************************************************/
899 #ifdef CONFIG_HOTPLUG_PCI
900 static int __devinit __pci_iomul_notifier_del_device(struct pci_dev *pdev)
901 {
902 struct pci_iomul_switch *sw;
903 struct pci_iomul_slot *slot;
904 int i;
906 pci_iomul_get_lock_switch(pdev, &sw, &slot);
907 if (sw == NULL || slot == NULL)
908 return 0;
910 if (sw->func == slot->func[PCI_FUNC(pdev->devfn)])
911 sw->func = NULL;
912 kfree(slot->func[PCI_FUNC(pdev->devfn)]);
913 slot->func[PCI_FUNC(pdev->devfn)] = NULL;
914 for (i = 0; i < PCI_NUM_FUNC; i++) {
915 if (slot->func[i] != NULL)
916 goto out;
917 }
919 pci_iomul_slot_del_locked(sw, slot);
920 pci_iomul_slot_put(slot);
922 out:
923 mutex_unlock(&sw->lock);
924 pci_iomul_switch_put(sw);
925 return 0;
926 }
928 static int __devinit __pci_iomul_notifier_del_switch(struct pci_dev *pdev)
929 {
930 struct pci_iomul_switch *sw;
932 mutex_lock(&switch_list_lock);
933 sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
934 pdev->bus->number);
935 if (sw == NULL)
936 goto out;
938 pci_iomul_switch_del_locked(sw);
940 mutex_lock(&sw->lock);
941 if (sw->io_resource.parent)
942 release_resource(&sw->io_resource);
943 sw->io_base = 0; /* to tell this switch is removed */
944 sw->io_limit = 0;
945 BUG_ON(!list_empty(&sw->slots));
946 mutex_unlock(&sw->lock);
948 out:
949 mutex_unlock(&switch_list_lock);
950 pci_iomul_switch_put(sw);
951 return 0;
952 }
954 static int __devinit pci_iomul_notifier_del_device(struct pci_dev *pdev)
955 {
956 int ret;
957 switch (pdev->hdr_type) {
958 case PCI_HEADER_TYPE_NORMAL:
959 ret = __pci_iomul_notifier_del_device(pdev);
960 break;
961 case PCI_HEADER_TYPE_BRIDGE:
962 ret = __pci_iomul_notifier_del_switch(pdev);
963 break;
964 default:
965 printk(KERN_WARNING "PCI IOMUL: "
966 "device %s has unknown header type %02x, ignoring.\n",
967 pci_name(pdev), pdev->hdr_type);
968 ret = -EIO;
969 break;
970 }
971 return ret;
972 }
974 static int __devinit pci_iomul_notifier(struct notifier_block *nb,
975 unsigned long action, void *data)
976 {
977 struct device *dev = data;
978 struct pci_dev *pdev = to_pci_dev(dev);
980 switch (action) {
981 case BUS_NOTIFY_ADD_DEVICE:
982 quirk_iomul_reassign_ioresource(pdev);
983 break;
984 case BUS_NOTIFY_DEL_DEVICE:
985 return pci_iomul_notifier_del_device(pdev);
986 default:
987 /* nothing */
988 break;
989 }
991 return 0;
992 }
994 static struct notifier_block pci_iomul_nb = {
995 .notifier_call = pci_iomul_notifier,
996 };
998 static int __init pci_iomul_hotplug_init(void)
999 {
1000 bus_register_notifier(&pci_bus_type, &pci_iomul_nb);
1001 return 0;
1004 late_initcall(pci_iomul_hotplug_init);
1005 #endif
1007 /*****************************************************************************/
1008 struct pci_iomul_data {
1009 struct mutex lock;
1011 struct pci_dev *pdev;
1012 struct pci_iomul_switch *sw;
1013 struct pci_iomul_slot *slot; /* slot::kref */
1014 struct pci_iomul_func **func; /* when dereferencing,
1015 sw->lock is necessary */
1016 };
1018 static int pci_iomul_func_ioport(struct pci_iomul_func *func,
1019 uint8_t bar, uint64_t offset, int *port)
1021 if (!(func->io_bar & (1 << bar)))
1022 return -EINVAL;
1024 *port = func->resource[bar].start + offset;
1025 if (*port < func->resource[bar].start ||
1026 *port > func->resource[bar].end)
1027 return -EINVAL;
1029 return 0;
1032 static inline int pci_iomul_valid(struct pci_iomul_data *iomul)
1034 BUG_ON(!mutex_is_locked(&iomul->lock));
1035 BUG_ON(!mutex_is_locked(&iomul->sw->lock));
1036 return pci_iomul_switch_io_allocated(iomul->sw) &&
1037 *iomul->func != NULL;
1040 static void __pci_iomul_enable_io(struct pci_dev *pdev)
1042 uint16_t cmd;
1044 pci_dev_get(pdev);
1045 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1046 cmd |= PCI_COMMAND_IO;
1047 pci_write_config_word(pdev, PCI_COMMAND, cmd);
1050 static void __pci_iomul_disable_io(struct pci_iomul_data *iomul,
1051 struct pci_dev *pdev)
1053 uint16_t cmd;
1055 if (!pci_iomul_valid(iomul))
1056 return;
1058 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1059 cmd &= ~PCI_COMMAND_IO;
1060 pci_write_config_word(pdev, PCI_COMMAND, cmd);
1061 pci_dev_put(pdev);
1064 static int pci_iomul_open(struct inode *inode, struct file *filp)
1066 struct pci_iomul_data *iomul;
1067 iomul = kmalloc(sizeof(*iomul), GFP_KERNEL);
1068 if (iomul == NULL)
1069 return -ENOMEM;
1071 mutex_init(&iomul->lock);
1072 iomul->pdev = NULL;
1073 iomul->sw = NULL;
1074 iomul->slot = NULL;
1075 iomul->func = NULL;
1076 filp->private_data = (void*)iomul;
1078 return 0;
1081 static int pci_iomul_release(struct inode *inode, struct file *filp)
1083 struct pci_iomul_data *iomul =
1084 (struct pci_iomul_data*)filp->private_data;
1085 struct pci_iomul_switch *sw;
1086 struct pci_iomul_slot *slot = NULL;
1088 mutex_lock(&iomul->lock);
1089 sw = iomul->sw;
1090 slot = iomul->slot;
1091 if (iomul->pdev != NULL) {
1092 if (sw != NULL) {
1093 mutex_lock(&sw->lock);
1094 if (sw->current_pdev == iomul->pdev) {
1095 __pci_iomul_disable_io(iomul,
1096 sw->current_pdev);
1097 sw->current_pdev = NULL;
1099 sw->count--;
1100 if (sw->count == 0) {
1101 release_region(sw->io_region->start, sw->io_region->end - sw->io_region->start + 1);
1102 sw->io_region = NULL;
1104 mutex_unlock(&sw->lock);
1106 pci_dev_put(iomul->pdev);
1108 mutex_unlock(&iomul->lock);
1110 if (slot != NULL)
1111 pci_iomul_slot_put(slot);
1112 if (sw != NULL)
1113 pci_iomul_switch_put(sw);
1114 kfree(iomul);
1115 return 0;
1118 static long pci_iomul_setup(struct pci_iomul_data *iomul,
1119 struct pci_iomul_setup __user *arg)
1121 long error = 0;
1122 struct pci_iomul_setup setup;
1123 struct pci_iomul_switch *sw = NULL;
1124 struct pci_iomul_slot *slot;
1125 struct pci_bus *pbus;
1126 struct pci_dev *pdev;
1128 if (copy_from_user(&setup, arg, sizeof(setup)))
1129 return -EFAULT;
1131 pbus = pci_find_bus(setup.segment, setup.bus);
1132 if (pbus == NULL)
1133 return -ENODEV;
1134 pdev = pci_get_slot(pbus, setup.dev);
1135 if (pdev == NULL)
1136 return -ENODEV;
1138 mutex_lock(&iomul->lock);
1139 if (iomul->sw != NULL) {
1140 error = -EBUSY;
1141 goto out0;
1144 pci_iomul_get_lock_switch(pdev, &sw, &slot);
1145 if (sw == NULL || slot == NULL) {
1146 error = -ENODEV;
1147 goto out0;
1149 if (!pci_iomul_switch_io_allocated(sw)) {
1150 error = -ENODEV;
1151 goto out;
1154 if (slot->func[setup.func] == NULL) {
1155 error = -ENODEV;
1156 goto out;
1159 if (sw->count == 0) {
1160 BUG_ON(sw->io_region != NULL);
1161 sw->io_region =
1162 request_region(sw->io_base,
1163 sw->io_limit - sw->io_base + 1,
1164 "PCI IO Multiplexer driver");
1165 if (sw->io_region == NULL) {
1166 mutex_unlock(&sw->lock);
1167 error = -EBUSY;
1168 goto out;
1171 sw->count++;
1172 pci_iomul_slot_get(slot);
1174 iomul->pdev = pdev;
1175 iomul->sw = sw;
1176 iomul->slot = slot;
1177 iomul->func = &slot->func[setup.func];
1179 out:
1180 mutex_unlock(&sw->lock);
1181 out0:
1182 mutex_unlock(&iomul->lock);
1183 if (error != 0) {
1184 if (sw != NULL)
1185 pci_iomul_switch_put(sw);
1186 pci_dev_put(pdev);
1188 return error;
1191 static int pci_iomul_lock(struct pci_iomul_data *iomul,
1192 struct pci_iomul_switch **sw,
1193 struct pci_iomul_func **func)
1195 mutex_lock(&iomul->lock);
1196 *sw = iomul->sw;
1197 if (*sw == NULL) {
1198 mutex_unlock(&iomul->lock);
1199 return -ENODEV;
1201 mutex_lock(&(*sw)->lock);
1202 if (!pci_iomul_valid(iomul)) {
1203 mutex_unlock(&(*sw)->lock);
1204 mutex_unlock(&iomul->lock);
1205 return -ENODEV;
1207 *func = *iomul->func;
1209 return 0;
1212 static long pci_iomul_disable_io(struct pci_iomul_data *iomul)
1214 long error = 0;
1215 struct pci_iomul_switch *sw;
1216 struct pci_iomul_func *dummy_func;
1217 struct pci_dev *pdev;
1219 if (pci_iomul_lock(iomul, &sw, &dummy_func) < 0)
1220 return -ENODEV;
1222 pdev = iomul->pdev;
1223 if (pdev == NULL)
1224 error = -ENODEV;
1226 if (pdev != NULL && sw->current_pdev == pdev) {
1227 __pci_iomul_disable_io(iomul, pdev);
1228 sw->current_pdev = NULL;
1231 mutex_unlock(&sw->lock);
1232 mutex_unlock(&iomul->lock);
1233 return error;
1236 static void pci_iomul_switch_to(
1237 struct pci_iomul_data *iomul, struct pci_iomul_switch *sw,
1238 struct pci_dev *next_pdev)
1240 if (sw->current_pdev == next_pdev)
1241 /* nothing to do */
1242 return;
1244 if (sw->current_pdev != NULL)
1245 __pci_iomul_disable_io(iomul, sw->current_pdev);
1247 __pci_iomul_enable_io(next_pdev);
1248 sw->current_pdev = next_pdev;
1251 static long pci_iomul_in(struct pci_iomul_data *iomul,
1252 struct pci_iomul_in __user *arg)
1254 struct pci_iomul_in in;
1255 struct pci_iomul_switch *sw;
1256 struct pci_iomul_func *func;
1258 long error = 0;
1259 int port;
1260 uint32_t value = 0;
1262 if (copy_from_user(&in, arg, sizeof(in)))
1263 return -EFAULT;
1265 if (pci_iomul_lock(iomul, &sw, &func) < 0)
1266 return -ENODEV;
1268 error = pci_iomul_func_ioport(func, in.bar, in.offset, &port);
1269 if (error)
1270 goto out;
1272 pci_iomul_switch_to(iomul, sw, iomul->pdev);
1273 switch (in.size) {
1274 case 4:
1275 value = inl(port);
1276 break;
1277 case 2:
1278 value = inw(port);
1279 break;
1280 case 1:
1281 value = inb(port);
1282 break;
1283 default:
1284 error = -EINVAL;
1285 break;
1288 out:
1289 mutex_unlock(&sw->lock);
1290 mutex_unlock(&iomul->lock);
1292 if (error == 0 && put_user(value, &arg->value))
1293 return -EFAULT;
1294 return error;
1297 static long pci_iomul_out(struct pci_iomul_data *iomul,
1298 struct pci_iomul_out __user *arg)
1300 struct pci_iomul_in out;
1301 struct pci_iomul_switch *sw;
1302 struct pci_iomul_func *func;
1304 long error = 0;
1305 int port;
1307 if (copy_from_user(&out, arg, sizeof(out)))
1308 return -EFAULT;
1310 if (pci_iomul_lock(iomul, &sw, &func) < 0)
1311 return -ENODEV;
1313 error = pci_iomul_func_ioport(func, out.bar, out.offset, &port);
1314 if (error)
1315 goto out;
1317 pci_iomul_switch_to(iomul, sw, iomul->pdev);
1318 switch (out.size) {
1319 case 4:
1320 outl(out.value, port);
1321 break;
1322 case 2:
1323 outw(out.value, port);
1324 break;
1325 case 1:
1326 outb(out.value, port);
1327 break;
1328 default:
1329 error = -EINVAL;
1330 break;
1333 out:
1334 mutex_unlock(&sw->lock);
1335 mutex_unlock(&iomul->lock);
1336 return error;
1339 static long pci_iomul_ioctl(struct file *filp,
1340 unsigned int cmd, unsigned long arg)
1342 long error;
1343 struct pci_iomul_data *iomul =
1344 (struct pci_iomul_data*)filp->private_data;
1346 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1347 return -EPERM;
1349 switch (cmd) {
1350 case PCI_IOMUL_SETUP:
1351 error = pci_iomul_setup(iomul,
1352 (struct pci_iomul_setup __user *)arg);
1353 break;
1354 case PCI_IOMUL_DISABLE_IO:
1355 error = pci_iomul_disable_io(iomul);
1356 break;
1357 case PCI_IOMUL_IN:
1358 error = pci_iomul_in(iomul, (struct pci_iomul_in __user *)arg);
1359 break;
1360 case PCI_IOMUL_OUT:
1361 error = pci_iomul_out(iomul,
1362 (struct pci_iomul_out __user *)arg);
1363 break;
1364 default:
1365 error = -ENOSYS;
1366 break;
1369 return error;
1372 static const struct file_operations pci_iomul_fops = {
1373 .owner = THIS_MODULE,
1375 .open = pci_iomul_open, /* nonseekable_open */
1376 .release = pci_iomul_release,
1378 .unlocked_ioctl = pci_iomul_ioctl,
1379 };
1381 static struct miscdevice pci_iomul_miscdev = {
1382 .minor = MISC_DYNAMIC_MINOR,
1383 .name = "pci_iomul",
1384 .fops = &pci_iomul_fops,
1385 };
1387 static int pci_iomul_init(void)
1389 int error;
1390 error = misc_register(&pci_iomul_miscdev);
1391 if (error != 0) {
1392 printk(KERN_ALERT "Couldn't register /dev/misc/pci_iomul");
1393 return error;
1395 printk("PCI IO multiplexer device installed.\n");
1396 return 0;
1399 #if 0
1400 static void pci_iomul_cleanup(void)
1402 misc_deregister(&pci_iomul_miscdev);
1404 #endif
1406 /*
1407 * This must be called after pci fixup final which is called by
1408 * device_initcall(pci_init).
1409 */
1410 late_initcall(pci_iomul_init);
1412 MODULE_LICENSE("GPL");
1413 MODULE_AUTHOR("Isaku Yamahata <yamahata@valinux.co.jp>");
1414 MODULE_DESCRIPTION("PCI IO space multiplexing driver");