Merge from vendor branch LIBSTDC++:
[dragonfly.git] / sys / bus / pci / i386 / pci_cfgreg.c
1 /*
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000, BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/i386/isa/pci_cfgreg.c,v 1.1.2.7 2001/11/28 05:47:03 imp Exp $
29  * $DragonFly: src/sys/bus/pci/i386/pci_cfgreg.c,v 1.5 2004/01/15 08:05:41 joerg Exp $
30  *
31  */
32
33 #include <sys/param.h>          /* XXX trim includes */
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <machine/md_var.h>
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
44 #include <bus/isa/isavar.h>
45 #include <bus/pci/i386/pci_cfgreg.h>
46 #include <machine/segments.h>
47 #include <machine/pc/bios.h>
48
49 #ifdef APIC_IO
50 #include <machine/smp.h>
51 #endif /* APIC_IO */
52
53 #define PRVERB(a) do {                                                  \
54         if (bootverbose)                                                \
55                 printf a ;                                              \
56 } while(0)
57
58 static int cfgmech;
59 static int devmax;
60
61 static int      pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq);
62 static int      pci_cfgintr_unique(struct PIR_entry *pe, int pin);
63 static int      pci_cfgintr_linked(struct PIR_entry *pe, int pin);
64 static int      pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin);
65 static int      pci_cfgintr_virgin(struct PIR_entry *pe, int pin);
66
67 static void     pci_print_irqmask(u_int16_t irqs);
68 static void     pci_print_route_table(struct PIR_table *prt, int size);
69 static int      pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
70 static void     pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
71 static int      pcireg_cfgopen(void);
72
73 static struct PIR_table *pci_route_table;
74 static int              pci_route_count;
75
76 /*
77  * Some BIOS writers seem to want to ignore the spec and put
78  * 0 in the intline rather than 255 to indicate none. Some use
79  * numbers in the range 128-254 to indicate something strange and
80  * apparently undocumented anywhere. Assume these are completely bogus
81  * and map them to 255, which means "none".
82  */
83 static int
84 pci_i386_map_intline(int line)
85 {
86         if (line == 0 || line >= 128)
87                 return (PCI_INVALID_IRQ);
88         return (line);
89 }
90
91 static u_int16_t
92 pcibios_get_version(void)
93 {
94         struct bios_regs args;
95
96         if (PCIbios.ventry == 0) {
97                 PRVERB(("pcibios: No call entry point\n"));
98                 return (0);
99         }
100         args.eax = PCIBIOS_BIOS_PRESENT;
101         if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
102                 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
103                 return (0);
104         }
105         if (args.edx != 0x20494350) {
106                 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
107                 return (0);
108         }
109         return (args.ebx & 0xffff);
110 }
111
112 /* 
113  * Initialise access to PCI configuration space 
114  */
115 int
116 pci_cfgregopen(void)
117 {
118         static int              opened = 0;
119         u_long                  sigaddr;
120         static struct PIR_table *pt;
121         u_int16_t               v;
122         u_int8_t                ck, *cv;
123         int                     i;
124
125         if (opened)
126                 return (1);
127
128         if (pcireg_cfgopen() == 0)
129                 return (0);
130
131         v = pcibios_get_version();
132         if (v > 0)
133                 printf("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
134                        v & 0xff);
135
136         /*
137          * Look for the interrupt routing table.
138          *
139          * We use PCI BIOS's PIR table if it's available $PIR is the
140          * standard way to do this.  Sadly some machines are not
141          * standards conforming and have _PIR instead. We shrug and cope
142          * by looking for both.
143          */
144         if (pcibios_get_version() >= 0x0210 && pt == NULL) {
145                 sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
146                 if (sigaddr == 0)
147                         sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
148                 if (sigaddr != 0) {
149                         pt = (struct PIR_table *)(uintptr_t)
150                              BIOS_PADDRTOVADDR(sigaddr);
151                         for (cv = (u_int8_t *)pt, ck = 0, i = 0;
152                              i < (pt->pt_header.ph_length); i++)
153                                 ck += cv[i];
154                         if (ck == 0 && pt->pt_header.ph_length >
155                             sizeof(struct PIR_header)) {
156                                 pci_route_table = pt;
157                                 pci_route_count = (pt->pt_header.ph_length -
158                                     sizeof(struct PIR_header)) /
159                                     sizeof(struct PIR_entry);
160                                 printf("Using $PIR table, %d entries at %p\n",
161                                        pci_route_count, pci_route_table);
162                                 if (bootverbose)
163                                         pci_print_route_table(pci_route_table,
164                                             pci_route_count);
165                         }
166                 }
167         }
168         opened = 1;
169         return (1);     
170 }
171
172 /* 
173  * Read configuration space register
174  */
175 u_int32_t
176 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
177 {
178         uint32_t line;
179 #ifdef APIC_IO
180         uint32_t pin;
181
182         /*
183          * If we are using the APIC, the contents of the intline
184          * register will probably be wrong (since they are set up for
185          * use with the PIC.  Rather than rewrite these registers
186          * (maybe that would be smarter) we trap attempts to read them
187          * and translate to our private vector numbers.
188          */
189         if ((reg == PCIR_INTLINE) && (bytes == 1)) {
190
191                 pin = pcireg_cfgread(bus, slot, func, PCIR_INTPIN, 1);
192                 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
193
194                 if (pin != 0) {
195                         int airq;
196
197                         airq = pci_apic_irq(bus, slot, pin);
198                         if (airq >= 0) {
199                                 /* PCI specific entry found in MP table */
200                                 if (airq != line)
201                                         undirect_pci_irq(line);
202                                 return (airq);
203                         } else {
204                                 /* 
205                                  * PCI interrupts might be redirected to the
206                                  * ISA bus according to some MP tables. Use the
207                                  * same methods as used by the ISA devices
208                                  * devices to find the proper IOAPIC int pin.
209                                  */
210                                 airq = isa_apic_irq(line);
211                                 if ((airq >= 0) && (airq != line)) {
212                                         /* XXX: undirect_pci_irq() ? */
213                                         undirect_isa_irq(line);
214                                         return (airq);
215                                 }
216                         }
217                 }
218                 return (line);
219         }
220 #else
221         /*
222          * Some BIOS writers seem to want to ignore the spec and put
223          * 0 in the intline rather than 255 to indicate none.  The rest of
224          * the code uses 255 as an invalid IRQ.
225          */
226         if (reg == PCIR_INTLINE && bytes == 1) {
227                 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
228                 return pci_i386_map_intline(line);
229         }
230 #endif /* APIC_IO */
231         return (pcireg_cfgread(bus, slot, func, reg, bytes));
232 }
233
234 /* 
235  * Write configuration space register 
236  */
237 void
238 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
239 {
240         pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
241 }
242
243 int
244 pci_cfgread(pcicfgregs *cfg, int reg, int bytes)
245 {
246         return (pci_cfgregread(cfg->bus, cfg->slot, cfg->func, reg, bytes));
247 }
248
249 void
250 pci_cfgwrite(pcicfgregs *cfg, int reg, int data, int bytes)
251 {
252         pci_cfgregwrite(cfg->bus, cfg->slot, cfg->func, reg, data, bytes);
253 }
254
255
256 /*
257  * Route a PCI interrupt
258  */
259 int
260 pci_cfgintr(int bus, int device, int pin, int oldirq)
261 {
262         struct PIR_entry        *pe;
263         int                     i, irq;
264         struct bios_regs        args;
265         u_int16_t               v;
266
267         int already = 0;
268         int errok = 0;
269     
270         v = pcibios_get_version();
271         if (v < 0x0210) {
272                 PRVERB((
273                   "pci_cfgintr: BIOS %x.%02x doesn't support interrupt routing\n",
274                   (v & 0xff00) >> 8, v & 0xff));
275                 return (PCI_INVALID_IRQ);
276         }
277         if ((bus < 0) || (bus > 255) || (device < 0) || (device > 255) ||
278             (pin < 1) || (pin > 4))
279                 return (PCI_INVALID_IRQ);
280
281         /*
282          * Scan the entry table for a contender
283          */
284         for (i = 0, pe = &pci_route_table->pt_entry[0]; i < pci_route_count;
285              i++, pe++) {
286                 if ((bus != pe->pe_bus) || (device != pe->pe_device))
287                         continue;
288
289                 /*
290                  * A link of 0 means that this intpin is not connected to
291                  * any other device's interrupt pins and is not connected to
292                  * any of the Interrupt Router's interrupt pins, so we can't
293                  * route it.
294                  */
295                 if (pe->pe_intpin[pin - 1].link == 0)
296                         continue;
297
298                 if (pci_cfgintr_valid(pe, pin, oldirq)) {
299                         printf("pci_cfgintr: %d:%d INT%c BIOS irq %d\n", bus,
300                                device, 'A' + pin - 1, oldirq);
301                         return (oldirq);
302                 }
303
304                 /*
305                  * We try to find a linked interrupt, then we look to see
306                  * if the interrupt is uniquely routed, then we look for
307                  * a virgin interrupt. The virgin interrupt should return
308                  * an interrupt we can route, but if that fails, maybe we
309                  * should try harder to route a different interrupt.
310                  * However, experience has shown that that's rarely the
311                  * failure mode we see.
312                  */
313                 irq = pci_cfgintr_linked(pe, pin);
314                 if (irq != PCI_INVALID_IRQ)
315                         already = 1;
316                 if (irq == PCI_INVALID_IRQ) {
317                         irq = pci_cfgintr_unique(pe, pin);
318                         if (irq != PCI_INVALID_IRQ)
319                                 errok = 1;
320                 }
321                 if (irq == PCI_INVALID_IRQ)
322                         irq = pci_cfgintr_virgin(pe, pin);
323
324                 if (irq == PCI_INVALID_IRQ)
325                         break;
326
327                 /*
328                  * Ask the BIOS to route the interrupt. If we picked an
329                  * interrupt that failed, we should really try other
330                  * choices that the BIOS offers us.
331                  *
332                  * For uniquely routed interrupts, we need to try
333                  * to route them on some machines. Yet other machines
334                  * fail to route, so we have to pretend that in that
335                  * case it worked.  Isn't PC hardware fun?
336                  *
337                  * NOTE: if we want to whack hardware to do this, then
338                  * I think the right way to do that would be to have
339                  * bridge drivers that do this. I'm not sure that the
340                  * $PIR table would be valid for those interrupt
341                  * routers.
342                  */
343                 args.eax = PCIBIOS_ROUTE_INTERRUPT;
344                 args.ebx = (bus << 8) | (device << 3);
345                 /* pin value is 0xa - 0xd */
346                 args.ecx = (irq << 8) | (0xa + pin -1);
347                 if (!already &&
348                     bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)) &&
349                     !errok) {
350                         PRVERB(("pci_cfgintr: ROUTE_INTERRUPT failed.\n"));
351                         return (PCI_INVALID_IRQ);
352                 }
353                 printf("pci_cfgintr: %d:%d INT%c routed to irq %d\n", bus,
354                        device, 'A' + pin - 1, irq);
355                 return(irq);
356         }
357
358         PRVERB(("pci_cfgintr: can't route an interrupt to %d:%d INT%c\n", bus,
359                device, 'A' + pin - 1));
360         return (PCI_INVALID_IRQ);
361 }
362
363 /*
364  * Check to see if an existing IRQ setting is valid.
365  */
366 static int
367 pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq)
368 {
369         uint32_t irqmask;
370
371         if (!PCI_INTERRUPT_VALID(irq))
372                 return (0);
373         irqmask = pe->pe_intpin[pin - 1].irqs;
374         if (irqmask & (1 << irq)) {
375                 PRVERB(("pci_cfgintr_valid: BIOS irq %d is valid\n", irq));
376                 return (1);
377         }
378         return (0);
379 }
380
381 /*
382  * Look to see if the routing table claims this pin is uniquely routed.
383  */
384 static int
385 pci_cfgintr_unique(struct PIR_entry *pe, int pin)
386 {
387         int             irq;
388         uint32_t        irqmask;
389
390         irqmask = pe->pe_intpin[pin - 1].irqs;
391         if(irqmask != 0 && powerof2(irqmask)) {
392                 irq = ffs(irqmask) - 1;
393                 PRVERB(("pci_cfgintr_unique: hard-routed to irq %d\n", irq));
394                 return (irq);
395         }
396         return (PCI_INVALID_IRQ);
397 }
398
399 /*
400  * Look for another device which shares the same link byte and
401  * already has a unique IRQ, or which has had one routed already.
402  */
403 static int
404 pci_cfgintr_linked(struct PIR_entry *pe, int pin)
405 {
406         struct PIR_entry        *oe;
407         struct PIR_intpin       *pi;
408         int                     i, j, irq;
409
410         /*
411          * Scan table slots.
412          */
413         for (i = 0, oe = &pci_route_table->pt_entry[0]; i < pci_route_count;
414              i++, oe++) {
415                 /* scan interrupt pins */
416                 for (j = 0, pi = &oe->pe_intpin[0]; j < 4; j++, pi++) {
417
418                         /* don't look at the entry we're trying to match */
419                         if ((pe == oe) && (i == (pin - 1)))
420                                 continue;
421                         /* compare link bytes */
422                         if (pi->link != pe->pe_intpin[pin - 1].link)
423                                 continue;
424                         /* link destination mapped to a unique interrupt? */
425                         if (pi->irqs != 0 && powerof2(pi->irqs)) {
426                                 irq = ffs(pi->irqs) - 1;
427                                 PRVERB(("pci_cfgintr_linked: linked (%x) to hard-routed irq %d\n",
428                                        pi->link, irq));
429                                 return(irq);
430                         } 
431
432                         /*
433                          * look for the real PCI device that matches this
434                          * table entry
435                          */
436                         irq = pci_cfgintr_search(pe, oe->pe_bus, oe->pe_device,
437                                                  j, pin);
438                         if (irq != PCI_INVALID_IRQ)
439                                 return (irq);
440                 }
441         }
442         return (PCI_INVALID_IRQ);
443 }
444
445 /*
446  * Scan for the real PCI device at (bus)/(device) using intpin (matchpin) and
447  * see if it has already been assigned an interrupt.
448  */
449 static int
450 pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin)
451 {
452         devclass_t              pci_devclass;
453         device_t                *pci_devices;
454         int                     pci_count;
455         device_t                *pci_children;
456         int                     pci_childcount;
457         device_t                *busp, *childp;
458         int                     i, j, irq;
459
460         /*
461          * Find all the PCI busses.
462          */
463         pci_count = 0;
464         if ((pci_devclass = devclass_find("pci")) != NULL)
465                 devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
466
467         /*
468          * Scan all the PCI busses/devices looking for this one.
469          */
470         irq = PCI_INVALID_IRQ;
471         for (i = 0, busp = pci_devices; (i < pci_count) && (irq == PCI_INVALID_IRQ);
472              i++, busp++) {
473                 pci_childcount = 0;
474                 device_get_children(*busp, &pci_children, &pci_childcount);
475                 
476                 for (j = 0, childp = pci_children; j < pci_childcount; j++,
477                      childp++) {
478                         if ((pci_get_bus(*childp) == bus) &&
479                             (pci_get_slot(*childp) == device) &&
480                             (pci_get_intpin(*childp) == matchpin)) {
481                                 irq = pci_i386_map_intline(pci_get_irq(*childp));
482                                 if (irq != PCI_INVALID_IRQ)
483                                         PRVERB(("pci_cfgintr_search: linked (%x) to configured irq %d at %d:%d:%d\n",
484                                             pe->pe_intpin[pin - 1].link, irq,
485                                             pci_get_bus(*childp),
486                                             pci_get_slot(*childp),
487                                             pci_get_function(*childp)));
488                                 break;
489                         }
490                 }
491                 if (pci_children != NULL)
492                         free(pci_children, M_TEMP);
493         }
494         if (pci_devices != NULL)
495                 free(pci_devices, M_TEMP);
496         return (irq);
497 }
498
499 /*
500  * Pick a suitable IRQ from those listed as routable to this device.
501  */
502 static int
503 pci_cfgintr_virgin(struct PIR_entry *pe, int pin)
504 {
505         int irq, ibit;
506     
507         /*
508          * first scan the set of PCI-only interrupts and see if any of these
509          * are routable
510          */
511         for (irq = 0; irq < 16; irq++) {
512                 ibit = (1 << irq);
513
514                 /* can we use this interrupt? */
515                 if ((pci_route_table->pt_header.ph_pci_irqs & ibit) &&
516                     (pe->pe_intpin[pin - 1].irqs & ibit)) {
517                         PRVERB(("pci_cfgintr_virgin: using routable PCI-only interrupt %d\n", irq));
518                         return (irq);
519                 }
520         }
521     
522         /* life is tough, so just pick an interrupt */
523         for (irq = 0; irq < 16; irq++) {
524                 ibit = (1 << irq);
525     
526                 if (pe->pe_intpin[pin - 1].irqs & ibit) {
527                         PRVERB(("pci_cfgintr_virgin: using routable interrupt %d\n", irq));
528                         return (irq);
529                 }
530         }
531         return (PCI_INVALID_IRQ);
532 }
533
534 static void
535 pci_print_irqmask(u_int16_t irqs)
536 {
537         int i, first;
538
539         if (irqs == 0) {
540                 printf("none");
541                 return;
542         }
543         first = 1;
544         for (i = 0; i < 16; i++, irqs >>= 1)
545                 if (irqs & 1) {
546                         if (!first)
547                                 printf(" ");
548                         else
549                                 first = 0;
550                         printf("%d", i);
551                 }
552 }
553
554 /*
555  * Dump the contents of a PCI BIOS Interrupt Routing Table to the console.
556  */
557 static void
558 pci_print_route_table(struct PIR_table *ptr, int size)
559 {
560         struct PIR_entry *entry;
561         struct PIR_intpin *intpin;
562         int i, pin;
563
564         printf("PCI-Only Interrupts: ");
565         pci_print_irqmask(ptr->pt_header.ph_pci_irqs);
566         printf("\nLocation  Bus Device Pin  Link  IRQs\n");
567         entry = &ptr->pt_entry[0];
568         for (i = 0; i < size; i++, entry++) {
569                 intpin = &entry->pe_intpin[0];
570                 for (pin = 0; pin < 4; pin++, intpin++)
571                         if (intpin->link != 0) {
572                                 if (entry->pe_slot == 0)
573                                         printf("embedded ");
574                                 else
575                                         printf("slot %-3d ", entry->pe_slot);
576                                 printf(" %3d  %3d    %c   0x%02x  ",
577                                        entry->pe_bus, entry->pe_device,
578                                        'A' + pin, intpin->link);
579                                 pci_print_irqmask(intpin->irqs);
580                                 printf("\n");
581                         }
582         }
583 }
584
585 /*
586  * See if any interrupts for a given PCI bus are routed in the PIR.  Don't
587  * even bother looking if the BIOS doesn't support routing anyways.
588  */
589 int
590 pci_probe_route_table(int bus)
591 {
592         int i;
593         u_int16_t v;
594
595         v = pcibios_get_version();
596         if (v < 0x0210)
597                 return (0);
598         for (i = 0; i < pci_route_count; i++)
599                 if (pci_route_table->pt_entry[i].pe_bus == bus)
600                         return (1);
601         return (0);
602 }
603
604 /* 
605  * Configuration space access using direct register operations
606  */
607
608 /* enable configuration space accesses and return data port address */
609 static int
610 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
611 {
612         int dataport = 0;
613
614         if (bus <= PCI_BUSMAX
615             && slot < devmax
616             && func <= PCI_FUNCMAX
617             && reg <= PCI_REGMAX
618             && bytes != 3
619             && (unsigned) bytes <= 4
620             && (reg & (bytes - 1)) == 0) {
621                 switch (cfgmech) {
622                 case 1:
623                         outl(CONF1_ADDR_PORT, (1 << 31)
624                              | (bus << 16) | (slot << 11) 
625                              | (func << 8) | (reg & ~0x03));
626                         dataport = CONF1_DATA_PORT + (reg & 0x03);
627                         break;
628                 case 2:
629                         outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
630                         outb(CONF2_FORWARD_PORT, bus);
631                         dataport = 0xc000 | (slot << 8) | reg;
632                         break;
633                 }
634         }
635         return (dataport);
636 }
637
638 /* disable configuration space accesses */
639 static void
640 pci_cfgdisable(void)
641 {
642         switch (cfgmech) {
643         case 1:
644                 outl(CONF1_ADDR_PORT, 0);
645                 break;
646         case 2:
647                 outb(CONF2_ENABLE_PORT, 0);
648                 outb(CONF2_FORWARD_PORT, 0);
649                 break;
650         }
651 }
652
653 static int
654 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
655 {
656         int data = -1;
657         int port;
658
659         port = pci_cfgenable(bus, slot, func, reg, bytes);
660         if (port != 0) {
661                 switch (bytes) {
662                 case 1:
663                         data = inb(port);
664                         break;
665                 case 2:
666                         data = inw(port);
667                         break;
668                 case 4:
669                         data = inl(port);
670                         break;
671                 }
672                 pci_cfgdisable();
673         }
674         return (data);
675 }
676
677 static void
678 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
679 {
680         int port;
681
682         port = pci_cfgenable(bus, slot, func, reg, bytes);
683         if (port != 0) {
684                 switch (bytes) {
685                 case 1:
686                         outb(port, data);
687                         break;
688                 case 2:
689                         outw(port, data);
690                         break;
691                 case 4:
692                         outl(port, data);
693                         break;
694                 }
695                 pci_cfgdisable();
696         }
697 }
698
699 /* check whether the configuration mechanism has been correctly identified */
700 static int
701 pci_cfgcheck(int maxdev)
702 {
703         uint32_t id, class;
704         uint8_t header;
705         uint8_t device;
706         int port;
707
708         if (bootverbose) 
709                 printf("pci_cfgcheck:\tdevice ");
710
711         for (device = 0; device < maxdev; device++) {
712                 if (bootverbose) 
713                         printf("%d ", device);
714
715                 port = pci_cfgenable(0, device, 0, 0, 4);
716                 id = inl(port);
717                 if (id == 0 || id == 0xffffffff)
718                         continue;
719
720                 port = pci_cfgenable(0, device, 0, 8, 4);
721                 class = inl(port) >> 8;
722                 if (bootverbose)
723                         printf("[class=%06x] ", class);
724                 if (class == 0 || (class & 0xf870ff) != 0)
725                         continue;
726
727                 port = pci_cfgenable(0, device, 0, 14, 1);
728                 header = inb(port);
729                 if (bootverbose)
730                         printf("[hdr=%02x] ", header);
731                 if ((header & 0x7e) != 0)
732                         continue;
733
734                 if (bootverbose)
735                         printf("is there (id=%08x)\n", id);
736
737                 pci_cfgdisable();
738                 return (1);
739         }
740         if (bootverbose) 
741                 printf("-- nothing found\n");
742
743         pci_cfgdisable();
744         return (0);
745 }
746
747 static int
748 pcireg_cfgopen(void)
749 {
750         uint32_t mode1res,oldval1;
751         uint8_t mode2res,oldval2;
752
753         oldval1 = inl(CONF1_ADDR_PORT);
754
755         if (bootverbose) {
756                 printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
757                        oldval1);
758         }
759
760         if ((oldval1 & CONF1_ENABLE_MSK) == 0) {
761
762                 cfgmech = 1;
763                 devmax = 32;
764
765                 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
766                 outb(CONF1_ADDR_PORT + 3, 0);
767                 mode1res = inl(CONF1_ADDR_PORT);
768                 outl(CONF1_ADDR_PORT, oldval1);
769
770                 if (bootverbose)
771                         printf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n", 
772                                mode1res, CONF1_ENABLE_CHK);
773
774                 if (mode1res) {
775                         if (pci_cfgcheck(32)) 
776                                 return (cfgmech);
777                 }
778
779                 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
780                 mode1res = inl(CONF1_ADDR_PORT);
781                 outl(CONF1_ADDR_PORT, oldval1);
782
783                 if (bootverbose)
784                         printf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n", 
785                                mode1res, CONF1_ENABLE_CHK1);
786
787                 if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
788                         if (pci_cfgcheck(32)) 
789                                 return (cfgmech);
790                 }
791         }
792
793         oldval2 = inb(CONF2_ENABLE_PORT);
794
795         if (bootverbose) {
796                 printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
797                        oldval2);
798         }
799
800         if ((oldval2 & 0xf0) == 0) {
801
802                 cfgmech = 2;
803                 devmax = 16;
804
805                 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
806                 mode2res = inb(CONF2_ENABLE_PORT);
807                 outb(CONF2_ENABLE_PORT, oldval2);
808
809                 if (bootverbose)
810                         printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n", 
811                                mode2res, CONF2_ENABLE_CHK);
812
813                 if (mode2res == CONF2_ENABLE_RES) {
814                         if (bootverbose)
815                                 printf("pci_open(2a):\tnow trying mechanism 2\n");
816
817                         if (pci_cfgcheck(16)) 
818                                 return (cfgmech);
819                 }
820         }
821
822         cfgmech = 0;
823         devmax = 0;
824         return (cfgmech);
825 }