- supported.conf: Added sparse_keymap (eeepc_laptop depends on it)
[linux-flexiantxendom0-3.2.10.git] / drivers / pci / iomulti.c
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  */
20
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/fs.h>
24 #include <linux/miscdevice.h>
25 #include <linux/pci.h>
26 #include <linux/sort.h>
27
28 #include <asm/setup.h>
29 #include <asm/uaccess.h>
30
31 #include "pci.h"
32 #include "iomulti.h"
33
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
39
40 /* see pci_resource_len */
41 static inline resource_size_t pci_iomul_len(const struct resource* r)
42 {
43         if (r->start == 0 && r->start == r->end)
44                 return 0;
45         return r->end - r->start + 1;
46 }
47
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)
51 {
52         unsigned long size = 0, size1 = 0;
53         int i;
54
55         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
56                 struct resource *r = &pdev->resource[i];
57                 unsigned long r_size;
58
59                 if (!(r->flags & IORESOURCE_IO))
60                         continue;
61
62                 r_size = r->end - r->start + 1;
63
64                 if (r_size < 0x400)
65                         /* Might be re-aligned for ISA */
66                         size += r_size;
67                 else
68                         size1 += r_size;
69         }
70
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);
75 #endif
76         size = ROUND_UP(size + size1, 4096);
77         return size;
78 }
79
80 /*
81  * primary bus number of PCI-PCI bridge in switch on which
82  * this slots sits.
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
86  *      in switch.
87  */
88 static inline unsigned char pci_dev_switch_busnr(struct pci_dev *pdev)
89 {
90         if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
91                 return pdev->bus->primary;
92         return pdev->bus->number;
93 }
94
95 struct pci_iomul_func {
96         int             segment;
97         uint8_t         bus;
98         uint8_t         devfn;
99
100         /* only start and end are used */
101         unsigned long   io_size;
102         uint8_t         io_bar;
103         struct resource resource[PCI_NUM_BARS];
104         struct resource dummy_parent;
105 };
106
107 struct pci_iomul_switch {
108         struct list_head        list;   /* bus_list_lock protects */
109
110         /*
111          * This lock the following entry and following
112          * pci_iomul_slot/pci_iomul_func.
113          */
114         struct mutex            lock;
115         struct kref             kref;
116
117         struct resource         io_resource;
118         struct resource         *io_region;
119         unsigned int            count;
120         struct pci_dev          *current_pdev;
121
122         int                     segment;
123         uint8_t                 bus;
124
125         uint32_t                io_base;
126         uint32_t                io_limit;
127
128         /* func which has the largeset io size*/
129         struct pci_iomul_func   *func;
130
131         struct list_head        slots;
132 };
133
134 struct pci_iomul_slot {
135         struct list_head        sibling;
136         struct kref             kref;
137         /*
138          * busnr
139          * when pcie, the primary busnr of the PCI-PCI bridge on which
140          * this devices sits.
141          */
142         uint8_t                 switch_busnr;
143         struct resource         dummy_parent[PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES];
144
145         /* device */
146         int                     segment;
147         uint8_t                 bus;
148         uint8_t                 dev;
149
150         struct pci_iomul_func   *func[PCI_NUM_FUNC];
151 };
152
153 static LIST_HEAD(switch_list);
154 static DEFINE_MUTEX(switch_list_lock);
155
156 /*****************************************************************************/
157 static int inline pci_iomul_switch_io_allocated(
158         const struct pci_iomul_switch *sw)
159 {
160         return !(sw->io_base == 0 || sw->io_base > sw->io_limit);
161 }
162
163 static struct pci_iomul_switch *pci_iomul_find_switch_locked(int segment,
164                                                              uint8_t bus)
165 {
166         struct pci_iomul_switch *sw;
167
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)
171                         return sw;
172         }
173         return NULL;
174 }
175
176 static struct pci_iomul_slot *pci_iomul_find_slot_locked(
177         struct pci_iomul_switch *sw, uint8_t busnr, uint8_t dev)
178 {
179         struct pci_iomul_slot *slot;
180
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)
184                         return slot;
185         }
186         return NULL;
187 }
188
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.
192  */
193 static void pci_iomul_get_lock_switch(struct pci_dev *pdev,
194                                       struct pci_iomul_switch **swp,
195                                       struct pci_iomul_slot **slot)
196 {
197         mutex_lock(&switch_list_lock);
198
199         *swp = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
200                                             pci_dev_switch_busnr(pdev));
201         if (*swp == NULL) {
202                 *slot = NULL;
203                 goto out;
204         }
205
206         mutex_lock(&(*swp)->lock);
207         *slot = pci_iomul_find_slot_locked(*swp, pdev->bus->number,
208                                            PCI_SLOT(pdev->devfn));
209         if (*slot == NULL) {
210                 mutex_unlock(&(*swp)->lock);
211                 *swp = NULL;
212         } else {
213                 pci_iomul_switch_get(*swp);
214         }
215 out:
216         mutex_unlock(&switch_list_lock);
217 }
218
219 static struct pci_iomul_switch *pci_iomul_switch_alloc(int segment,
220                                                        uint8_t bus)
221 {
222         struct pci_iomul_switch *sw;
223
224         BUG_ON(!mutex_is_locked(&switch_list_lock));
225
226         sw = kmalloc(sizeof(*sw), GFP_KERNEL);
227
228         mutex_init(&sw->lock);
229         kref_init(&sw->kref);
230         sw->io_region = NULL;
231         sw->count = 0;
232         sw->current_pdev = NULL;
233         sw->segment = segment;
234         sw->bus = bus;
235         sw->io_base = 0;
236         sw->io_limit = 0;
237         sw->func = NULL;
238         INIT_LIST_HEAD(&sw->slots);
239
240         return sw;
241 }
242
243 static void pci_iomul_switch_add_locked(struct pci_iomul_switch *sw)
244 {
245         BUG_ON(!mutex_is_locked(&switch_list_lock));
246         list_add(&sw->list, &switch_list);
247 }
248
249 #ifdef CONFIG_HOTPLUG_PCI
250 static void pci_iomul_switch_del_locked(struct pci_iomul_switch *sw)
251 {
252         BUG_ON(!mutex_is_locked(&switch_list_lock));
253         list_del(&sw->list);
254 }
255 #endif
256
257 static void pci_iomul_switch_get(struct pci_iomul_switch *sw)
258 {
259         kref_get(&sw->kref);
260 }
261
262 static void pci_iomul_switch_release(struct kref *kref)
263 {
264         struct pci_iomul_switch *sw = container_of(kref,
265                                                    struct pci_iomul_switch,
266                                                    kref);
267         kfree(sw);
268 }
269
270 static void pci_iomul_switch_put(struct pci_iomul_switch *sw)
271 {
272         kref_put(&sw->kref, &pci_iomul_switch_release);
273 }
274
275 static int __devinit pci_iomul_slot_init(struct pci_dev *pdev,
276                                          struct pci_iomul_slot *slot)
277 {
278         u16 rpcap;
279         u16 cap;
280
281         rpcap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
282         if (!rpcap) {
283                 /* pci device isn't supported */
284                 printk(KERN_INFO
285                        "PCI: sharing io port of non PCIe device %s "
286                        "isn't supported. ignoring.\n",
287                        pci_name(pdev));
288                 return -ENOSYS;
289         }
290
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:
294                 printk(KERN_INFO
295                        "PCI: io port sharing of root complex integrated "
296                        "endpoint %s isn't supported. ignoring.\n",
297                        pci_name(pdev));
298                 return -ENOSYS;
299         case PCI_EXP_TYPE_ENDPOINT:
300         case PCI_EXP_TYPE_LEG_END:
301                 break;
302         default:
303                 printk(KERN_INFO
304                        "PCI: io port sharing of non endpoint %s "
305                        "doesn't make sense. ignoring.\n",
306                        pci_name(pdev));
307                 return -EINVAL;
308         }
309
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);
315
316         return 0;
317 }
318
319 static struct pci_iomul_slot *__devinit
320 pci_iomul_slot_alloc(struct pci_dev *pdev)
321 {
322         struct pci_iomul_slot *slot;
323
324         slot = kzalloc(sizeof(*slot), GFP_KERNEL);
325         if (slot == NULL)
326                 return NULL;
327
328         if (pci_iomul_slot_init(pdev, slot) != 0) {
329                 kfree(slot);
330                 return NULL;
331         }
332         return slot;
333 }
334
335 static void pci_iomul_slot_add_locked(struct pci_iomul_switch *sw,
336                                       struct pci_iomul_slot *slot)
337 {
338         BUG_ON(!mutex_is_locked(&sw->lock));
339         list_add(&slot->sibling, &sw->slots);
340 }
341
342 #ifdef CONFIG_HOTPLUG_PCI
343 static void pci_iomul_slot_del_locked(struct pci_iomul_switch *sw,
344                                        struct pci_iomul_slot *slot)
345 {
346         BUG_ON(!mutex_is_locked(&sw->lock));
347         list_del(&slot->sibling);
348 }
349 #endif
350
351 static void pci_iomul_slot_get(struct pci_iomul_slot *slot)
352 {
353         kref_get(&slot->kref);
354 }
355
356 static void pci_iomul_slot_release(struct kref *kref)
357 {
358         struct pci_iomul_slot *slot = container_of(kref, struct pci_iomul_slot,
359                                                    kref);
360         kfree(slot);
361 }
362
363 static void pci_iomul_slot_put(struct pci_iomul_slot *slot)
364 {
365         kref_put(&slot->kref, &pci_iomul_slot_release);
366 }
367
368 /*****************************************************************************/
369 static int pci_get_sbd(const char *str,
370                        int *segment__, uint8_t *bus__, uint8_t *dev__)
371 {
372         int segment;
373         int bus;
374         int dev;
375
376         if (sscanf(str, "%x:%x:%x", &segment, &bus, &dev) != 3) {
377                 if (sscanf(str, "%x:%x", &bus, &dev) == 2)
378                         segment = 0;
379                 else
380                         return -EINVAL;
381         }
382
383         if (segment < 0 || INT_MAX <= segment)
384                 return -EINVAL;
385         if (bus < 0 || PCI_BUS_MAX < bus)
386                 return -EINVAL;
387         if (dev < 0 || PCI_DEV_MAX < dev)
388                 return -EINVAL;
389
390         *segment__ = segment;
391         *bus__ = bus;
392         *dev__ = dev;
393         return 0;
394 }
395
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)
399 {
400         int len;
401         char *p;
402         char *next_str;
403
404         for (p = &iomul_param[0]; *p != '\0'; p = next_str + 1) {
405                 next_str = strchr(p, ',');
406                 if (next_str != NULL)
407                         len = next_str - p;
408                 else
409                         len = strlen(p);
410
411                 if (len > 0 && len <= TOKEN_MAX) {
412                         char tmp[TOKEN_MAX+1];
413                         int seg;
414                         uint8_t bus;
415                         uint8_t dev;
416
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)
422                                 return 1;
423                 }
424                 if (next_str == NULL)
425                         break;
426         }
427
428         /* check guestcev=<device>+iomul option */
429         return pci_is_iomuldev(pdev);
430 }
431
432 /*
433  * Format: [<segment>:]<bus>:<dev>[,[<segment>:]<bus>:<dev>[,...]
434  */
435 static int __init pci_iomul_param_setup(char *str)
436 {
437         if (strlen(str) >= COMMAND_LINE_SIZE)
438                 return 0;
439
440         /* parse it after pci bus scanning */
441         strlcpy(iomul_param, str, sizeof(iomul_param));
442         return 1;
443 }
444 __setup("guestiomuldev=", pci_iomul_param_setup);
445
446 /*****************************************************************************/
447 static void __devinit pci_iomul_set_bridge_io_window(struct pci_dev *bridge,
448                                                      uint32_t io_base,
449                                                      uint32_t io_limit)
450 {
451         uint16_t l;
452         uint32_t upper16;
453
454         io_base >>= 12;
455         io_base <<= 4;
456         io_limit >>= 12;
457         io_limit <<= 4;
458         l = (io_base & 0xff) | ((io_limit & 0xff) << 8);
459         upper16 = ((io_base & 0xffff00) >> 8) |
460                 (((io_limit & 0xffff00) >> 8) << 16);
461
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);
468 }
469
470 static void __devinit pci_disable_bridge_io_window(struct pci_dev *bridge)
471 {
472         /* set base = 0xffffff limit = 0x0 */
473         pci_iomul_set_bridge_io_window(bridge, 0xffffff, 0);
474 }
475
476 static int __devinit pci_iomul_func_scan(struct pci_dev *pdev,
477                                          struct pci_iomul_slot *slot,
478                                          uint8_t func)
479 {
480         struct pci_iomul_func *f;
481         unsigned int i;
482
483         f = kzalloc(sizeof(*f), GFP_KERNEL);
484         if (f == NULL)
485                 return -ENOMEM;
486
487         f->segment = slot->segment;
488         f->bus = slot->bus;
489         f->devfn = PCI_DEVFN(slot->dev, func);
490         f->io_size = pdev_size_io(pdev);
491
492         for (i = 0; i < PCI_NUM_BARS; i++) {
493                 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO))
494                         continue;
495                 if (pci_resource_len(pdev, i) == 0)
496                         continue;
497
498                 f->io_bar |= 1 << i;
499                 f->resource[i] = pdev->resource[i];
500         }
501
502         if (f->io_bar)
503                 slot->func[func] = f;
504         else
505                 kfree(f);
506         return 0;
507 }
508
509 /*
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().
515  *
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.
521  */
522 static void __devinit pci_iomul_disable_resource(struct resource *r)
523 {
524         /* don't allocate this resource */
525         r->flags = 0;
526 }
527
528 static void __devinit pci_iomul_reenable_resource(
529         struct resource *dummy_parent, struct resource *r)
530 {
531         int ret;
532
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";
537
538         ret = request_resource(dummy_parent, r);
539         BUG_ON(ret);
540 }
541
542 static void __devinit pci_iomul_fixup_ioresource(struct pci_dev *pdev,
543                                                  struct pci_iomul_func *func,
544                                                  int reassign, int dealloc)
545 {
546         uint8_t i;
547         struct resource *r;
548
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)))
554                         continue;
555
556                 if (reassign) {
557                         r->end -= r->start;
558                         r->start = 0;
559                         pci_update_resource(pdev, i);
560                         func->resource[i] = *r;
561                 }
562
563                 if (dealloc)
564                         /* don't allocate this resource */
565                         pci_iomul_disable_resource(r);
566         }
567
568         /* parent PCI-PCI bridge */
569         if (!reassign)
570                 return;
571         pdev = pdev->bus->self;
572         if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
573                 return;
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))
578                         continue;
579
580                 r->end -= r->start;
581                 r->start = 0;
582                 if (i < PCI_BRIDGE_RESOURCES)
583                         pci_update_resource(pdev, i);
584         }
585 }
586
587 static void __devinit __quirk_iomul_dealloc_ioresource(
588         struct pci_iomul_switch *sw,
589         struct pci_dev *pdev, struct pci_iomul_slot *slot)
590 {
591         struct pci_iomul_func *f;
592         struct pci_iomul_func *__f;
593
594         if (pci_iomul_func_scan(pdev, slot, PCI_FUNC(pdev->devfn)) != 0)
595                 return;
596
597         f = slot->func[PCI_FUNC(pdev->devfn)];
598         if (f == NULL)
599                 return;
600
601         __f = sw->func;
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)) {
606                 if (__f != NULL) {
607                         struct pci_bus *__pbus;
608                         struct pci_dev *__pdev;
609
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);
615                         pci_dev_put(__pdev);
616                 }
617
618                 pci_iomul_fixup_ioresource(pdev, f, 1, 0);
619                 sw->func = f;
620         } else {
621                 pci_iomul_fixup_ioresource(pdev, f, 1, 1);
622         }
623 }
624
625 static void __devinit quirk_iomul_dealloc_ioresource(struct pci_dev *pdev)
626 {
627         struct pci_iomul_switch *sw;
628         struct pci_iomul_slot *slot;
629
630         if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
631                 return;
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))
635                 return;
636
637         mutex_lock(&switch_list_lock);
638         sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
639                                           pci_dev_switch_busnr(pdev));
640         if (sw == NULL) {
641                 sw = pci_iomul_switch_alloc(pci_domain_nr(pdev->bus),
642                                             pci_dev_switch_busnr(pdev));
643                 if (sw == NULL) {
644                         mutex_unlock(&switch_list_lock);
645                         printk(KERN_WARNING
646                                "PCI: can't allocate memory "
647                                "for sw of IO mulplexing %s", pci_name(pdev));
648                         return;
649                 }
650                 pci_iomul_switch_add_locked(sw);
651         }
652         pci_iomul_switch_get(sw);
653         mutex_unlock(&switch_list_lock);
654
655         mutex_lock(&sw->lock);
656         slot = pci_iomul_find_slot_locked(sw, pdev->bus->number,
657                                           PCI_SLOT(pdev->devfn));
658         if (slot == NULL) {
659                 slot = pci_iomul_slot_alloc(pdev);
660                 if (slot == NULL) {
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));
665                         return;
666                 }
667                 pci_iomul_slot_add_locked(sw, slot);
668         }
669
670         printk(KERN_INFO "PCI: disable device and release io resource[%s].\n",
671                pci_name(pdev));
672         pci_disable_device(pdev);
673
674         __quirk_iomul_dealloc_ioresource(sw, pdev, slot);
675
676         mutex_unlock(&sw->lock);
677         pci_iomul_switch_put(sw);
678 }
679 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID,
680                          quirk_iomul_dealloc_ioresource);
681
682 static void __devinit pci_iomul_read_bridge_io(struct pci_iomul_switch *sw)
683 {
684         struct pci_iomul_func *f = sw->func;
685
686         struct pci_bus *pbus;
687         struct pci_dev *pdev;
688         struct pci_dev *bridge;
689
690         uint16_t l;
691         uint16_t base_upper16;
692         uint16_t limit_upper16;
693         uint32_t io_base;
694         uint32_t io_limit;
695
696         pbus = pci_find_bus(f->segment, f->bus);
697         BUG_ON(pbus == NULL);
698
699         pdev = pci_get_slot(pbus, f->devfn);
700         BUG_ON(pdev == NULL);
701
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);
706
707         io_base = (l & 0xf0) | ((uint32_t)base_upper16 << 8);
708         io_base <<= 8;
709         io_limit = (l >> 8) | ((uint32_t)limit_upper16 << 8);
710         io_limit <<= 8;
711         io_limit |= 0xfff;
712
713         sw->io_base = io_base;
714         sw->io_limit = io_limit;
715
716         pci_dev_put(pdev);
717         printk(KERN_INFO "PCI: bridge %s base 0x%x limit 0x%x\n",
718                pci_name(bridge), sw->io_base, sw->io_limit);
719 }
720
721 static void __devinit pci_iomul_setup_brige(struct pci_dev *bridge,
722                                             uint32_t io_base,
723                                             uint32_t io_limit)
724 {
725         uint16_t cmd;
726
727         if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_HOST)
728                 return;
729
730         pci_iomul_set_bridge_io_window(bridge, io_base, io_limit);
731
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",
737                        pci_name(bridge));
738                 pci_write_config_word(bridge, PCI_COMMAND, cmd);
739         }
740 }
741
742 struct __bar {
743         unsigned long size;
744         uint8_t bar;
745 };
746
747 /* decending order */
748 static int __devinit pci_iomul_bar_cmp(const void *lhs__, const void *rhs__)
749 {
750         const struct __bar *lhs = (struct __bar*)lhs__;
751         const struct __bar *rhs = (struct __bar*)rhs__;
752         return - (lhs->size - rhs->size);
753 }
754
755 static void __devinit pci_iomul_setup_dev(struct pci_dev *pdev,
756                                           struct pci_iomul_func *f,
757                                           uint32_t io_base)
758 {
759         struct __bar bars[PCI_NUM_BARS];
760         int i;
761         uint8_t num_bars = 0;
762         struct resource *r;
763
764         printk(KERN_INFO "PCI: Forcibly assign IO %s from 0x%x\n",
765                pci_name(pdev), io_base);
766
767         for (i = 0; i < PCI_NUM_BARS; i++) {
768                 if (!(f->io_bar & (1 << i)))
769                         continue;
770
771                 r = &f->resource[i];
772                 bars[num_bars].size = pci_iomul_len(r);
773                 bars[num_bars].bar = i;
774
775                 num_bars++;
776         }
777
778         sort(bars, num_bars, sizeof(bars[0]), &pci_iomul_bar_cmp, NULL);
779
780         for (i = 0; i < num_bars; i++) {
781                 struct resource *fr = &f->resource[bars[i].bar];
782                 r = &pdev->resource[bars[i].bar];
783
784                 BUG_ON(r->start != 0);
785                 r->start += io_base;
786                 r->end += io_base;
787
788                 fr->start = r->start;
789                 fr->end = r->end;
790
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);
795
796                 io_base += bars[i].size;
797         }
798 }
799
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)
803 {
804         int i;
805         struct resource *r;
806
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];
811                         f->resource[i] = *r;
812                         release_resource(r);
813                         pci_iomul_reenable_resource(&f->dummy_parent, r);
814                 }
815         }
816
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;
822
823                         if (pci_resource_flags(pdev, i) & IORESOURCE_IO &&
824                             parent != NULL) {
825                                 r = &pdev->resource[i];
826
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";
831
832                                 release_resource(r);
833                                 pci_iomul_reenable_resource(
834                                         &slot->dummy_parent[i - PCI_BRIDGE_RESOURCES], r);
835
836                                 if (request_resource(parent,
837                                                      &sw->io_resource))
838                                         printk(KERN_ERR
839                                                "PCI IOMul: can't allocate "
840                                                "resource. [0x%x, 0x%x]",
841                                                sw->io_base, sw->io_limit);
842                         }
843                 }
844         }
845 }
846
847 static void __devinit quirk_iomul_reassign_ioresource(struct pci_dev *pdev)
848 {
849         struct pci_iomul_switch *sw;
850         struct pci_iomul_slot *slot;
851         struct pci_iomul_func *sf;
852         struct pci_iomul_func *f;
853
854         pci_iomul_get_lock_switch(pdev, &sw, &slot);
855         if (sw == NULL || slot == NULL)
856                 return;
857
858         if (sw->io_base == 0)
859                 pci_iomul_read_bridge_io(sw);
860         if (!pci_iomul_switch_io_allocated(sw))
861                 goto out;
862
863         sf = sw->func;
864         f = slot->func[PCI_FUNC(pdev->devfn)];
865         if (f == NULL)
866                 /* (sf == NULL || f == NULL) case
867                  * can happen when all the specified devices
868                  * don't have io space
869                  */
870                 goto out;
871
872         if (sf != NULL &&
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);
879         }
880
881         BUG_ON(f->io_size > sw->io_limit - sw->io_base + 1);
882         if (/* f == sf */
883             sf != NULL &&
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);
888         else
889                 pci_iomul_setup_dev(pdev, f, sw->io_base);
890
891 out:
892         mutex_unlock(&sw->lock);
893         pci_iomul_switch_put(sw);
894 }
895
896 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID,
897                         quirk_iomul_reassign_ioresource);
898
899 /*****************************************************************************/
900 #ifdef CONFIG_HOTPLUG_PCI
901 static int __devinit __pci_iomul_notifier_del_device(struct pci_dev *pdev)
902 {
903         struct pci_iomul_switch *sw;
904         struct pci_iomul_slot *slot;
905         int i;
906
907         pci_iomul_get_lock_switch(pdev, &sw, &slot);
908         if (sw == NULL || slot == NULL)
909                 return 0;
910
911         if (sw->func == slot->func[PCI_FUNC(pdev->devfn)])
912                 sw->func = NULL;
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)
917                         goto out;
918         }
919
920         pci_iomul_slot_del_locked(sw, slot);
921         pci_iomul_slot_put(slot);
922
923 out:
924         mutex_unlock(&sw->lock);
925         pci_iomul_switch_put(sw);
926         return 0;
927 }
928
929 static int __devinit __pci_iomul_notifier_del_switch(struct pci_dev *pdev)
930 {
931         struct pci_iomul_switch *sw;
932
933         mutex_lock(&switch_list_lock);
934         sw = pci_iomul_find_switch_locked(pci_domain_nr(pdev->bus),
935                                           pdev->bus->number);
936         if (sw == NULL)
937                 goto out;
938
939         pci_iomul_switch_del_locked(sw);
940
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 */
945         sw->io_limit = 0;
946         BUG_ON(!list_empty(&sw->slots));
947         mutex_unlock(&sw->lock);
948
949 out:
950         mutex_unlock(&switch_list_lock);
951         pci_iomul_switch_put(sw);
952         return 0;
953 }
954
955 static int __devinit pci_iomul_notifier_del_device(struct pci_dev *pdev)
956 {
957         int ret;
958         switch (pdev->hdr_type) {
959         case PCI_HEADER_TYPE_NORMAL:
960                 ret = __pci_iomul_notifier_del_device(pdev);
961                 break;
962         case PCI_HEADER_TYPE_BRIDGE:
963                 ret = __pci_iomul_notifier_del_switch(pdev);
964                 break;
965         default:
966                 printk(KERN_WARNING "PCI IOMUL: "
967                        "device %s has unknown header type %02x, ignoring.\n",
968                        pci_name(pdev), pdev->hdr_type);
969                 ret = -EIO;
970                 break;
971         }
972         return ret;
973 }
974
975 static int __devinit pci_iomul_notifier(struct notifier_block *nb,
976                                         unsigned long action, void *data)
977 {
978         struct device *dev = data;
979         struct pci_dev *pdev = to_pci_dev(dev);
980
981         switch (action) {
982         case BUS_NOTIFY_ADD_DEVICE:
983                 quirk_iomul_reassign_ioresource(pdev);
984                 break;
985         case BUS_NOTIFY_DEL_DEVICE:
986                 return pci_iomul_notifier_del_device(pdev);
987         default:
988                 /* nothing */
989                 break;
990         }
991
992         return 0;
993 }
994
995 static struct notifier_block pci_iomul_nb = {
996         .notifier_call = pci_iomul_notifier,
997 };
998
999 static int __init pci_iomul_hotplug_init(void)
1000 {
1001         bus_register_notifier(&pci_bus_type, &pci_iomul_nb);
1002         return 0;
1003 }
1004
1005 late_initcall(pci_iomul_hotplug_init);
1006 #endif
1007
1008 /*****************************************************************************/
1009 struct pci_iomul_data {
1010         struct mutex lock;
1011
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 */
1017 };
1018
1019 static int pci_iomul_func_ioport(struct pci_iomul_func *func,
1020                                  uint8_t bar, uint64_t offset, int *port)
1021 {
1022         if (!(func->io_bar & (1 << bar)))
1023                 return -EINVAL;
1024
1025         *port = func->resource[bar].start + offset;
1026         if (*port < func->resource[bar].start ||
1027             *port > func->resource[bar].end)
1028                 return -EINVAL;
1029
1030         return 0;
1031 }
1032
1033 static inline int pci_iomul_valid(struct pci_iomul_data *iomul)
1034 {
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;
1039 }
1040
1041 static void __pci_iomul_enable_io(struct pci_dev *pdev)
1042 {
1043         uint16_t cmd;
1044
1045         pci_dev_get(pdev);
1046         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1047         cmd |= PCI_COMMAND_IO;
1048         pci_write_config_word(pdev, PCI_COMMAND, cmd);
1049 }
1050
1051 static void __pci_iomul_disable_io(struct pci_iomul_data *iomul,
1052                                    struct pci_dev *pdev)
1053 {
1054         uint16_t cmd;
1055
1056         if (!pci_iomul_valid(iomul))
1057                 return;
1058
1059         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1060         cmd &= ~PCI_COMMAND_IO;
1061         pci_write_config_word(pdev, PCI_COMMAND, cmd);
1062         pci_dev_put(pdev);
1063 }
1064
1065 static int pci_iomul_open(struct inode *inode, struct file *filp)
1066 {
1067         struct pci_iomul_data *iomul;
1068         iomul = kmalloc(sizeof(*iomul), GFP_KERNEL);
1069         if (iomul == NULL)
1070                 return -ENOMEM;
1071
1072         mutex_init(&iomul->lock);
1073         iomul->pdev = NULL;
1074         iomul->sw = NULL;
1075         iomul->slot = NULL;
1076         iomul->func = NULL;
1077         filp->private_data = (void*)iomul;
1078
1079         return 0;
1080 }
1081
1082 static int pci_iomul_release(struct inode *inode, struct file *filp)
1083 {
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;
1088
1089         mutex_lock(&iomul->lock);
1090         sw = iomul->sw;
1091         slot = iomul->slot;
1092         if (iomul->pdev != NULL) {
1093                 if (sw != NULL) {
1094                         mutex_lock(&sw->lock);
1095                         if (sw->current_pdev == iomul->pdev) {
1096                                 __pci_iomul_disable_io(iomul,
1097                                                        sw->current_pdev);
1098                                 sw->current_pdev = NULL;
1099                         }
1100                         sw->count--;
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;
1104                         }
1105                         mutex_unlock(&sw->lock);
1106                 }
1107                 pci_dev_put(iomul->pdev);
1108         }
1109         mutex_unlock(&iomul->lock);
1110
1111         if (slot != NULL)
1112                 pci_iomul_slot_put(slot);
1113         if (sw != NULL)
1114                 pci_iomul_switch_put(sw);
1115         kfree(iomul);
1116         return 0;
1117 }
1118
1119 static long pci_iomul_setup(struct pci_iomul_data *iomul,
1120                             struct pci_iomul_setup __user *arg)
1121 {
1122         long error = 0;
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;
1128
1129         if (copy_from_user(&setup, arg, sizeof(setup)))
1130                 return -EFAULT;
1131
1132         pbus = pci_find_bus(setup.segment, setup.bus);
1133         if (pbus == NULL)
1134                 return -ENODEV;
1135         pdev = pci_get_slot(pbus, setup.dev);
1136         if (pdev == NULL)
1137                 return -ENODEV;
1138
1139         mutex_lock(&iomul->lock);
1140         if (iomul->sw != NULL) {
1141                 error = -EBUSY;
1142                 goto out0;
1143         }
1144
1145         pci_iomul_get_lock_switch(pdev, &sw, &slot);
1146         if (sw == NULL || slot == NULL) {
1147                 error = -ENODEV;
1148                 goto out0;
1149         }
1150         if (!pci_iomul_switch_io_allocated(sw)) {
1151                 error = -ENODEV;
1152                 goto out;
1153         }
1154
1155         if (slot->func[setup.func] == NULL) {
1156                 error = -ENODEV;
1157                 goto out;
1158         }
1159
1160         if (sw->count == 0) {
1161                 BUG_ON(sw->io_region != NULL);
1162                 sw->io_region =
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);
1168                         error = -EBUSY;
1169                         goto out;
1170                 }
1171         }
1172         sw->count++;
1173         pci_iomul_slot_get(slot);
1174
1175         iomul->pdev = pdev;
1176         iomul->sw = sw;
1177         iomul->slot = slot;
1178         iomul->func = &slot->func[setup.func];
1179
1180 out:
1181         mutex_unlock(&sw->lock);
1182 out0:
1183         mutex_unlock(&iomul->lock);
1184         if (error != 0) {
1185                 if (sw != NULL)
1186                         pci_iomul_switch_put(sw);
1187                 pci_dev_put(pdev);
1188         }
1189         return error;
1190 }
1191
1192 static int pci_iomul_lock(struct pci_iomul_data *iomul,
1193                           struct pci_iomul_switch **sw,
1194                           struct pci_iomul_func **func)
1195 {
1196         mutex_lock(&iomul->lock);
1197         *sw = iomul->sw;
1198         if (*sw == NULL) {
1199                 mutex_unlock(&iomul->lock);
1200                 return -ENODEV;
1201         }
1202         mutex_lock(&(*sw)->lock);
1203         if (!pci_iomul_valid(iomul)) {
1204                 mutex_unlock(&(*sw)->lock);
1205                 mutex_unlock(&iomul->lock);
1206                 return -ENODEV;
1207         }
1208         *func = *iomul->func;
1209
1210         return 0;
1211 }
1212
1213 static long pci_iomul_disable_io(struct pci_iomul_data *iomul)
1214 {
1215         long error = 0;
1216         struct pci_iomul_switch *sw;
1217         struct pci_iomul_func *dummy_func;
1218         struct pci_dev *pdev;
1219
1220         if (pci_iomul_lock(iomul, &sw, &dummy_func) < 0)
1221                 return -ENODEV;
1222
1223         pdev = iomul->pdev;
1224         if (pdev == NULL)
1225                 error = -ENODEV;
1226
1227         if (pdev != NULL && sw->current_pdev == pdev) {
1228                 __pci_iomul_disable_io(iomul, pdev);
1229                 sw->current_pdev = NULL;
1230         }
1231
1232         mutex_unlock(&sw->lock);
1233         mutex_unlock(&iomul->lock);
1234         return error;
1235 }
1236
1237 static void pci_iomul_switch_to(
1238         struct pci_iomul_data *iomul, struct pci_iomul_switch *sw,
1239         struct pci_dev *next_pdev)
1240 {
1241         if (sw->current_pdev == next_pdev)
1242                 /* nothing to do */
1243                 return;
1244
1245         if (sw->current_pdev != NULL)
1246                 __pci_iomul_disable_io(iomul, sw->current_pdev);
1247
1248         __pci_iomul_enable_io(next_pdev);
1249         sw->current_pdev = next_pdev;
1250 }
1251
1252 static long pci_iomul_in(struct pci_iomul_data *iomul,
1253                          struct pci_iomul_in __user *arg)
1254 {
1255         struct pci_iomul_in in;
1256         struct pci_iomul_switch *sw;
1257         struct pci_iomul_func *func;
1258
1259         long error = 0;
1260         int port;
1261         uint32_t value = 0;
1262
1263         if (copy_from_user(&in, arg, sizeof(in)))
1264                 return -EFAULT;
1265
1266         if (pci_iomul_lock(iomul, &sw, &func) < 0)
1267                 return -ENODEV;
1268
1269         error = pci_iomul_func_ioport(func, in.bar, in.offset, &port);
1270         if (error)
1271                 goto out;
1272
1273         pci_iomul_switch_to(iomul, sw, iomul->pdev);
1274         switch (in.size) {
1275         case 4:
1276                 value = inl(port);
1277                 break;
1278         case 2:
1279                 value = inw(port);
1280                 break;
1281         case 1:
1282                 value = inb(port);
1283                 break;
1284         default:
1285                 error = -EINVAL;
1286                 break;
1287         }
1288
1289 out:
1290         mutex_unlock(&sw->lock);
1291         mutex_unlock(&iomul->lock);
1292
1293         if (error == 0 && put_user(value, &arg->value))
1294                 return -EFAULT;
1295         return error;
1296 }
1297
1298 static long pci_iomul_out(struct pci_iomul_data *iomul,
1299                           struct pci_iomul_out __user *arg)
1300 {
1301         struct pci_iomul_in out;
1302         struct pci_iomul_switch *sw;
1303         struct pci_iomul_func *func;
1304
1305         long error = 0;
1306         int port;
1307
1308         if (copy_from_user(&out, arg, sizeof(out)))
1309                 return -EFAULT;
1310
1311         if (pci_iomul_lock(iomul, &sw, &func) < 0)
1312                 return -ENODEV;
1313
1314         error = pci_iomul_func_ioport(func, out.bar, out.offset, &port);
1315         if (error)
1316                 goto out;
1317
1318         pci_iomul_switch_to(iomul, sw, iomul->pdev);
1319         switch (out.size) {
1320         case 4:
1321                 outl(out.value, port);
1322                 break;
1323         case 2:
1324                 outw(out.value, port);
1325                 break;
1326         case 1:
1327                 outb(out.value, port);
1328                 break;
1329         default:
1330                 error = -EINVAL;
1331                 break;
1332         }
1333
1334 out:
1335         mutex_unlock(&sw->lock);
1336         mutex_unlock(&iomul->lock);
1337         return error;
1338 }
1339
1340 static long pci_iomul_ioctl(struct file *filp,
1341                             unsigned int cmd, unsigned long arg)
1342 {
1343         long error;
1344         struct pci_iomul_data *iomul =
1345                 (struct pci_iomul_data*)filp->private_data;
1346
1347         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1348                 return -EPERM;
1349
1350         switch (cmd) {
1351         case PCI_IOMUL_SETUP:
1352                 error = pci_iomul_setup(iomul,
1353                                         (struct pci_iomul_setup __user *)arg);
1354                 break;
1355         case PCI_IOMUL_DISABLE_IO:
1356                 error = pci_iomul_disable_io(iomul);
1357                 break;
1358         case PCI_IOMUL_IN:
1359                 error = pci_iomul_in(iomul, (struct pci_iomul_in __user *)arg);
1360                 break;
1361         case PCI_IOMUL_OUT:
1362                 error = pci_iomul_out(iomul,
1363                                       (struct pci_iomul_out __user *)arg);
1364                 break;
1365         default:
1366                 error = -ENOSYS;
1367                 break;
1368         }
1369
1370         return error;
1371 }
1372
1373 static const struct file_operations pci_iomul_fops = {
1374         .owner = THIS_MODULE,
1375
1376         .open = pci_iomul_open, /* nonseekable_open */
1377         .release = pci_iomul_release,
1378
1379         .unlocked_ioctl = pci_iomul_ioctl,
1380 };
1381
1382 static struct miscdevice pci_iomul_miscdev = {
1383         .minor = MISC_DYNAMIC_MINOR,
1384         .name = "pci_iomul",
1385         .fops = &pci_iomul_fops,
1386 };
1387
1388 static int pci_iomul_init(void)
1389 {
1390         int error;
1391         error = misc_register(&pci_iomul_miscdev);
1392         if (error != 0) {
1393                 printk(KERN_ALERT "Couldn't register /dev/misc/pci_iomul");
1394                 return error;
1395         }
1396         printk("PCI IO multiplexer device installed.\n");
1397         return 0;
1398 }
1399
1400 #if 0
1401 static void pci_iomul_cleanup(void)
1402 {
1403         misc_deregister(&pci_iomul_miscdev);
1404 }
1405 #endif
1406
1407 /*
1408  * This must be called after pci fixup final which is called by
1409  * device_initcall(pci_init).
1410  */
1411 late_initcall(pci_iomul_init);
1412
1413 MODULE_LICENSE("GPL");
1414 MODULE_AUTHOR("Isaku Yamahata <yamahata@valinux.co.jp>");
1415 MODULE_DESCRIPTION("PCI IO space multiplexing driver");