1 /* $NetBSD: pciconf.c,v 1.30 2007/05/24 15:57:58 briggs Exp $ */
4 * Copyright 2001 Wasabi Systems, Inc.
7 * Written by Allen Briggs for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * Derived in part from code from PMON/2000 (http://pmon.groupbsd.org/).
43 * - Perform all data structure allocation dynamically, don't have
44 * statically-sized arrays ("oops, you lose because you have too
45 * many slots filled!")
46 * - Do this in 2 passes, with an MD hook to control the behavior:
47 * (1) Configure the bus (possibly including expansion
49 * (2) Another pass to disable expansion ROMs if they're
50 * mapped (since you're not supposed to leave them
51 * mapped when you're not using them).
52 * This would facilitate MD code executing the expansion ROMs
53 * if necessary (possibly with an x86 emulator) to configure
54 * devices (e.g. VGA cards).
55 * - Deal with "anything can be hot-plugged" -- i.e., carry configuration
56 * information around & be able to reconfigure on the fly
57 * - Deal with segments (See IA64 System Abstraction Layer)
58 * - Deal with subtractive bridges (& non-spec positive/subtractive decode)
59 * - Deal with ISA/VGA/VGA palette snooping
60 * - Deal with device capabilities on bridges
61 * - Worry about changing a bridge to/from transparency
62 * From thorpej (05/25/01)
63 * - Try to handle devices that are already configured (perhaps using that
64 * as a hint to where we put other devices)
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: pciconf.c,v 1.30 2007/05/24 15:57:58 briggs Exp $");
72 #include <sys/param.h>
73 #include <sys/extent.h>
74 #include <sys/queue.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <dev/pci/pcivar.h>
79 #include <dev/pci/pciconf.h>
80 #include <dev/pci/pcidevs.h>
81 #include <dev/pci/pccbbreg.h>
83 int pci_conf_debug
= 0;
86 #define MIN(a,b) (((a)<(b))?(a):(b))
87 #define MAX(a,b) (((a)>(b))?(a):(b))
90 /* per-bus constants. */
91 #define MAX_CONF_DEV 32 /* Arbitrary */
92 #define MAX_CONF_MEM (3 * MAX_CONF_DEV) /* Avg. 3 per device -- Arb. */
93 #define MAX_CONF_IO (3 * MAX_CONF_DEV) /* Avg. 1 per device -- Arb. */
95 struct _s_pciconf_bus_t
; /* Forward declaration */
97 typedef struct _s_pciconf_dev_t
{
104 pci_chipset_tag_t pc
;
105 struct _s_pciconf_bus_t
*ppb
; /* I am really a bridge */
108 typedef struct _s_pciconf_win_t
{
110 int reg
; /* 0 for busses */
117 typedef struct _s_pciconf_bus_t
{
135 pciconf_dev_t device
[MAX_CONF_DEV
];
137 /* These should be sorted in order of decreasing size */
139 pciconf_win_t pcimemwin
[MAX_CONF_MEM
];
141 pciconf_win_t pciiowin
[MAX_CONF_IO
];
144 bus_size_t mem_total
;
145 bus_size_t pmem_total
;
147 struct extent
*ioext
;
148 struct extent
*memext
;
149 struct extent
*pmemext
;
151 pci_chipset_tag_t pc
;
152 struct _s_pciconf_bus_t
*parent_bus
;
155 static int probe_bus(pciconf_bus_t
*);
156 static void alloc_busno(pciconf_bus_t
*, pciconf_bus_t
*);
157 static void set_busreg(pci_chipset_tag_t
, pcitag_t
, int, int, int);
158 static int pci_do_device_query(pciconf_bus_t
*, pcitag_t
, int, int, int);
159 static int setup_iowins(pciconf_bus_t
*);
160 static int setup_memwins(pciconf_bus_t
*);
161 static int configure_bridge(pciconf_dev_t
*);
162 static int configure_bus(pciconf_bus_t
*);
163 static u_int64_t
pci_allocate_range(struct extent
*, u_int64_t
, int);
164 static pciconf_win_t
*get_io_desc(pciconf_bus_t
*, bus_size_t
);
165 static pciconf_win_t
*get_mem_desc(pciconf_bus_t
*, bus_size_t
);
166 static pciconf_bus_t
*query_bus(pciconf_bus_t
*, pciconf_dev_t
*, int);
168 static void print_tag(pci_chipset_tag_t
, pcitag_t
);
171 print_tag(pci_chipset_tag_t pc
, pcitag_t tag
)
175 pci_decompose_tag(pc
, tag
, &bus
, &dev
, &func
);
176 printf("PCI: bus %d, device %d, function %d: ", bus
, dev
, func
);
179 /************************************************************************/
180 /************************************************************************/
181 /*********************** Bus probing routines ***********************/
182 /************************************************************************/
183 /************************************************************************/
184 static pciconf_win_t
*
185 get_io_desc(pciconf_bus_t
*pb
, bus_size_t size
)
190 for (i
=n
; i
> 0 && size
> pb
->pciiowin
[i
-1].size
; i
--)
191 pb
->pciiowin
[i
] = pb
->pciiowin
[i
-1]; /* struct copy */
192 return &pb
->pciiowin
[i
];
195 static pciconf_win_t
*
196 get_mem_desc(pciconf_bus_t
*pb
, bus_size_t size
)
201 for (i
=n
; i
> 0 && size
> pb
->pcimemwin
[i
-1].size
; i
--)
202 pb
->pcimemwin
[i
] = pb
->pcimemwin
[i
-1]; /* struct copy */
203 return &pb
->pcimemwin
[i
];
207 * Set up bus common stuff, then loop over devices & functions.
208 * If we find something, call pci_do_device_query()).
211 probe_bus(pciconf_bus_t
*pb
)
214 #ifdef __PCI_BUS_DEVORDER
219 maxdevs
= pci_bus_maxdevs(pb
->pc
, pb
->busno
);
224 #ifdef PCICONF_NO_FAST_B2B
230 pb
->max_mingnt
= 0; /* we are looking for the maximum */
231 pb
->min_maxlat
= 0x100; /* we are looking for the minimum */
232 pb
->bandwidth_used
= 0;
234 #ifdef __PCI_BUS_DEVORDER
235 pci_bus_devorder(pb
->pc
, pb
->busno
, devs
);
236 for (i
= 0; (device
= devs
[i
]) < 32 && device
>= 0; i
++) {
238 for (device
= 0; device
< maxdevs
; device
++) {
242 int function
, nfunction
;
245 tag
= pci_make_tag(pb
->pc
, pb
->busno
, device
, 0);
246 if (pci_conf_debug
) {
247 print_tag(pb
->pc
, tag
);
249 id
= pci_conf_read(pb
->pc
, tag
, PCI_ID_REG
);
251 if (pci_conf_debug
) {
252 printf("id=%x: Vendor=%x, Product=%x\n",
253 id
, PCI_VENDOR(id
),PCI_PRODUCT(id
));
255 /* Invalid vendor ID value? */
256 if (PCI_VENDOR(id
) == PCI_VENDOR_INVALID
)
259 bhlcr
= pci_conf_read(pb
->pc
, tag
, PCI_BHLC_REG
);
260 nfunction
= PCI_HDRTYPE_MULTIFN(bhlcr
) ? 8 : 1;
261 for (function
= 0 ; function
< nfunction
; function
++) {
262 tag
= pci_make_tag(pb
->pc
, pb
->busno
, device
, function
);
263 id
= pci_conf_read(pb
->pc
, tag
, PCI_ID_REG
);
264 if (PCI_VENDOR(id
) == PCI_VENDOR_INVALID
)
266 if (pb
->ndevs
+1 < MAX_CONF_DEV
) {
267 if (pci_conf_debug
) {
268 print_tag(pb
->pc
, tag
);
269 printf("Found dev 0x%04x 0x%04x -- "
271 PCI_VENDOR(id
), PCI_PRODUCT(id
));
273 #ifdef __HAVE_PCI_CONF_HOOK
274 confmode
= pci_conf_hook(pb
->pc
, pb
->busno
,
275 device
, function
, id
);
280 * Don't enable expansion ROMS -- some cards
281 * share address decoders between the EXPROM
282 * and PCI memory space, and enabling the ROM
283 * when not needed will cause all sorts of
286 confmode
= PCI_CONF_DEFAULT
;
288 if (pci_do_device_query(pb
, tag
, device
,
299 alloc_busno(pciconf_bus_t
*parent
, pciconf_bus_t
*pb
)
301 pb
->busno
= parent
->next_busno
;
302 pb
->next_busno
= pb
->busno
+ 1;
306 set_busreg(pci_chipset_tag_t pc
, pcitag_t tag
, int prim
, int sec
, int sub
)
310 busreg
= prim
<< PCI_BRIDGE_BUS_PRIMARY_SHIFT
;
311 busreg
|= sec
<< PCI_BRIDGE_BUS_SECONDARY_SHIFT
;
312 busreg
|= sub
<< PCI_BRIDGE_BUS_SUBORDINATE_SHIFT
;
313 pci_conf_write(pc
, tag
, PCI_BRIDGE_BUS_REG
, busreg
);
316 static pciconf_bus_t
*
317 query_bus(pciconf_bus_t
*parent
, pciconf_dev_t
*pd
, int dev
)
321 pciconf_win_t
*pi
, *pm
;
323 pb
= malloc (sizeof (pciconf_bus_t
), M_DEVBUF
, M_NOWAIT
);
325 panic("Unable to allocate memory for PCI configuration.");
327 pb
->cacheline_size
= parent
->cacheline_size
;
328 pb
->parent_bus
= parent
;
329 alloc_busno(parent
, pb
);
331 set_busreg(parent
->pc
, pd
->tag
, parent
->busno
, pb
->busno
, 0xff);
333 pb
->swiz
= parent
->swiz
+ dev
;
339 pb
->io_total
= pb
->mem_total
= pb
->pmem_total
= 0;
342 if (parent
->io_32bit
) {
343 io
= pci_conf_read(parent
->pc
, pd
->tag
, PCI_BRIDGE_STATIO_REG
);
344 if (PCI_BRIDGE_IO_32BITS(io
)) {
350 if (parent
->pmem_64bit
) {
351 pmem
= pci_conf_read(parent
->pc
, pd
->tag
,
352 PCI_BRIDGE_PREFETCHMEM_REG
);
353 if (PCI_BRIDGE_PREFETCHMEM_64BITS(pmem
)) {
359 printf("Failed to probe bus %d\n", pb
->busno
);
363 /* We have found all subordinate busses now, reprogram busreg. */
364 pb
->last_busno
= pb
->next_busno
-1;
365 parent
->next_busno
= pb
->next_busno
;
366 set_busreg(parent
->pc
, pd
->tag
, parent
->busno
, pb
->busno
,
369 printf("PCI bus bridge (parent %d) covers busses %d-%d\n",
370 parent
->busno
, pb
->busno
, pb
->last_busno
);
372 if (pb
->io_total
> 0) {
373 if (parent
->niowin
>= MAX_CONF_IO
) {
374 printf("pciconf: too many I/O windows\n");
377 pb
->io_total
|= 0xfff; /* Round up */
378 pi
= get_io_desc(parent
, pb
->io_total
);
381 pi
->size
= pb
->io_total
;
382 pi
->align
= 0x1000; /* 4K alignment */
385 parent
->io_total
+= pb
->io_total
;
388 if (pb
->mem_total
> 0) {
389 if (parent
->nmemwin
>= MAX_CONF_MEM
) {
390 printf("pciconf: too many MEM windows\n");
393 pb
->mem_total
|= 0xfffff; /* Round up */
394 pm
= get_mem_desc(parent
, pb
->mem_total
);
397 pm
->size
= pb
->mem_total
;
398 pm
->align
= 0x100000; /* 1M alignment */
401 parent
->mem_total
+= pb
->mem_total
;
404 if (pb
->pmem_total
> 0) {
405 if (parent
->nmemwin
>= MAX_CONF_MEM
) {
406 printf("pciconf: too many MEM windows\n");
409 pb
->pmem_total
|= 0xfffff; /* Round up */
410 pm
= get_mem_desc(parent
, pb
->pmem_total
);
413 pm
->size
= pb
->pmem_total
;
414 pm
->align
= 0x100000; /* 1M alignment */
417 parent
->pmem_total
+= pb
->pmem_total
;
427 pci_do_device_query(pciconf_bus_t
*pb
, pcitag_t tag
, int dev
, int func
, int mode
)
430 pciconf_win_t
*pi
, *pm
;
431 pcireg_t
class, cmd
, icr
, bhlc
, bar
, mask
, bar64
, mask64
, busreg
;
433 int br
, width
, reg_start
, reg_end
;
435 pd
= &pb
->device
[pb
->ndevs
];
441 class = pci_conf_read(pb
->pc
, tag
, PCI_CLASS_REG
);
443 cmd
= pci_conf_read(pb
->pc
, tag
, PCI_COMMAND_STATUS_REG
);
445 if (PCI_CLASS(class) != PCI_CLASS_BRIDGE
) {
446 cmd
&= ~(PCI_COMMAND_MASTER_ENABLE
|
447 PCI_COMMAND_IO_ENABLE
| PCI_COMMAND_MEM_ENABLE
);
448 pci_conf_write(pb
->pc
, tag
, PCI_COMMAND_STATUS_REG
, cmd
);
449 } else if (pci_conf_debug
) {
450 print_tag(pb
->pc
, tag
);
451 printf("device is a bridge; not clearing enables\n");
454 if ((cmd
& PCI_STATUS_BACKTOBACK_SUPPORT
) == 0)
457 if ((cmd
& PCI_STATUS_66MHZ_SUPPORT
) == 0)
460 bhlc
= pci_conf_read(pb
->pc
, tag
, PCI_BHLC_REG
);
461 switch (PCI_HDRTYPE_TYPE(bhlc
)) {
462 case PCI_HDRTYPE_DEVICE
:
463 reg_start
= PCI_MAPREG_START
;
464 reg_end
= PCI_MAPREG_END
;
466 case PCI_HDRTYPE_PPB
:
467 pd
->ppb
= query_bus(pb
, pd
, dev
);
471 case PCI_HDRTYPE_PCB
:
472 reg_start
= PCI_MAPREG_START
;
473 reg_end
= PCI_MAPREG_PCB_END
;
475 busreg
= pci_conf_read(pb
->pc
, tag
, PCI_BUSNUM
);
476 busreg
= (busreg
& 0xff000000) |
477 pb
->busno
<< PCI_BRIDGE_BUS_PRIMARY_SHIFT
|
478 pb
->next_busno
<< PCI_BRIDGE_BUS_SECONDARY_SHIFT
|
479 pb
->next_busno
<< PCI_BRIDGE_BUS_SUBORDINATE_SHIFT
;
480 pci_conf_write(pb
->pc
, tag
, PCI_BUSNUM
, busreg
);
488 icr
= pci_conf_read(pb
->pc
, tag
, PCI_INTERRUPT_REG
);
489 pd
->ipin
= PCI_INTERRUPT_PIN(icr
);
490 pd
->iline
= PCI_INTERRUPT_LINE(icr
);
491 pd
->min_gnt
= PCI_MIN_GNT(icr
);
492 pd
->max_lat
= PCI_MAX_LAT(icr
);
493 if (pd
->iline
|| pd
->ipin
) {
494 pci_conf_interrupt(pb
->pc
, pb
->busno
, dev
, pd
->ipin
, pb
->swiz
,
496 icr
&= ~(PCI_INTERRUPT_LINE_MASK
<< PCI_INTERRUPT_LINE_SHIFT
);
497 icr
|= (pd
->iline
<< PCI_INTERRUPT_LINE_SHIFT
);
498 pci_conf_write(pb
->pc
, tag
, PCI_INTERRUPT_REG
, icr
);
501 if (pd
->min_gnt
!= 0 || pd
->max_lat
!= 0) {
502 if (pd
->min_gnt
!= 0 && pd
->min_gnt
> pb
->max_mingnt
)
503 pb
->max_mingnt
= pd
->min_gnt
;
505 if (pd
->max_lat
!= 0 && pd
->max_lat
< pb
->min_maxlat
)
506 pb
->min_maxlat
= pd
->max_lat
;
508 pb
->bandwidth_used
+= pd
->min_gnt
* 4000000 /
509 (pd
->min_gnt
+ pd
->max_lat
);
513 for (br
= reg_start
; br
< reg_end
; br
+= width
) {
515 /* XXX Should only ignore if IDE not in legacy mode? */
516 if (PCI_CLASS(class) == PCI_CLASS_MASS_STORAGE
&&
517 PCI_SUBCLASS(class) == PCI_SUBCLASS_MASS_STORAGE_IDE
) {
521 bar
= pci_conf_read(pb
->pc
, tag
, br
);
522 pci_conf_write(pb
->pc
, tag
, br
, 0xffffffff);
523 mask
= pci_conf_read(pb
->pc
, tag
, br
);
524 pci_conf_write(pb
->pc
, tag
, br
, bar
);
527 if ( (mode
& PCI_CONF_MAP_IO
)
528 && (PCI_MAPREG_TYPE(mask
) == PCI_MAPREG_TYPE_IO
)) {
530 * Upper 16 bits must be one. Devices may hardwire
531 * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
535 size
= PCI_MAPREG_IO_SIZE(mask
);
537 if (pci_conf_debug
) {
538 print_tag(pb
->pc
, tag
);
539 printf("I/O BAR 0x%x is void\n", br
);
544 if (pb
->niowin
>= MAX_CONF_IO
) {
545 printf("pciconf: too many I/O windows\n");
549 pi
= get_io_desc(pb
, size
);
552 pi
->size
= (u_int64_t
) size
;
555 if (pci_conf_debug
) {
556 print_tag(pb
->pc
, tag
);
557 printf("Register 0x%x, I/O size %" PRIu64
"\n",
561 pb
->io_total
+= size
;
562 } else if ((mode
& PCI_CONF_MAP_MEM
)
563 && (PCI_MAPREG_TYPE(mask
) == PCI_MAPREG_TYPE_MEM
)) {
564 switch (PCI_MAPREG_MEM_TYPE(mask
)) {
565 case PCI_MAPREG_MEM_TYPE_32BIT
:
566 case PCI_MAPREG_MEM_TYPE_32BIT_1M
:
567 size
= (u_int64_t
) PCI_MAPREG_MEM_SIZE(mask
);
569 case PCI_MAPREG_MEM_TYPE_64BIT
:
570 bar64
= pci_conf_read(pb
->pc
, tag
, br
+ 4);
571 pci_conf_write(pb
->pc
, tag
, br
+ 4, 0xffffffff);
572 mask64
= pci_conf_read(pb
->pc
, tag
, br
+ 4);
573 pci_conf_write(pb
->pc
, tag
, br
+ 4, bar64
);
574 size
= (u_int64_t
) PCI_MAPREG_MEM64_SIZE(
575 (((u_int64_t
) mask64
) << 32) | mask
);
579 print_tag(pb
->pc
, tag
);
580 printf("reserved mapping type 0x%x\n",
581 PCI_MAPREG_MEM_TYPE(mask
));
586 if (pci_conf_debug
) {
587 print_tag(pb
->pc
, tag
);
588 printf("MEM%d BAR 0x%x is void\n",
589 PCI_MAPREG_MEM_TYPE(mask
) ==
590 PCI_MAPREG_MEM_TYPE_64BIT
?
595 if (pci_conf_debug
) {
596 print_tag(pb
->pc
, tag
);
597 printf("MEM%d BAR 0x%x has size %lx\n",
598 PCI_MAPREG_MEM_TYPE(mask
) ==
599 PCI_MAPREG_MEM_TYPE_64BIT
?
600 64 : 32, br
, (unsigned long)size
);
604 if (pb
->nmemwin
>= MAX_CONF_MEM
) {
605 printf("pciconf: too many memory windows\n");
609 pm
= get_mem_desc(pb
, size
);
614 pm
->prefetch
= PCI_MAPREG_MEM_PREFETCHABLE(mask
);
615 if (pci_conf_debug
) {
616 print_tag(pb
->pc
, tag
);
617 printf("Register 0x%x, memory size %"
618 PRIu64
"\n", br
, pm
->size
);
622 pb
->pmem_total
+= size
;
624 pb
->mem_total
+= size
;
629 if (mode
& PCI_CONF_MAP_ROM
) {
630 bar
= pci_conf_read(pb
->pc
, tag
, PCI_MAPREG_ROM
);
631 pci_conf_write(pb
->pc
, tag
, PCI_MAPREG_ROM
, 0xfffffffe);
632 mask
= pci_conf_read(pb
->pc
, tag
, PCI_MAPREG_ROM
);
633 pci_conf_write(pb
->pc
, tag
, PCI_MAPREG_ROM
, bar
);
635 if (mask
!= 0 && mask
!= 0xffffffff) {
636 if (pb
->nmemwin
>= MAX_CONF_MEM
) {
637 printf("pciconf: too many memory windows\n");
640 size
= (u_int64_t
) PCI_MAPREG_MEM_SIZE(mask
);
642 pm
= get_mem_desc(pb
, size
);
644 pm
->reg
= PCI_MAPREG_ROM
;
648 if (pci_conf_debug
) {
649 print_tag(pb
->pc
, tag
);
650 printf("Expansion ROM memory size %"
651 PRIu64
"\n", pm
->size
);
654 pb
->pmem_total
+= size
;
657 /* Don't enable ROMs if we aren't going to map them. */
658 mode
&= ~PCI_CONF_ENABLE_ROM
;
659 pd
->enable
&= ~PCI_CONF_ENABLE_ROM
;
662 if (!(mode
& PCI_CONF_ENABLE_ROM
)) {
663 /* Ensure ROM is disabled */
664 bar
= pci_conf_read(pb
->pc
, tag
, PCI_MAPREG_ROM
);
665 pci_conf_write(pb
->pc
, tag
, PCI_MAPREG_ROM
,
666 bar
& ~PCI_MAPREG_ROM_ENABLE
);
672 /************************************************************************/
673 /************************************************************************/
674 /******************** Bus configuration routines ********************/
675 /************************************************************************/
676 /************************************************************************/
678 pci_allocate_range(struct extent
*ex
, u_int64_t amt
, int align
)
683 r
= extent_alloc(ex
, amt
, align
, 0, EX_NOWAIT
, &addr
);
686 printf("extent_alloc(%p, %" PRIu64
", %d) returned %d\n",
690 return (pcireg_t
) addr
;
694 setup_iowins(pciconf_bus_t
*pb
)
699 for (pi
=pb
->pciiowin
; pi
< &pb
->pciiowin
[pb
->niowin
] ; pi
++) {
704 pi
->address
= pci_allocate_range(pb
->ioext
, pi
->size
,
706 if (pi
->address
== -1) {
707 print_tag(pd
->pc
, pd
->tag
);
708 printf("Failed to allocate PCI I/O space (%"
709 PRIu64
" req)\n", pi
->size
);
712 if (pd
->ppb
&& pi
->reg
== 0) {
713 pd
->ppb
->ioext
= extent_create("pciconf", pi
->address
,
714 pi
->address
+ pi
->size
, M_DEVBUF
, NULL
, 0,
716 if (pd
->ppb
->ioext
== NULL
) {
717 print_tag(pd
->pc
, pd
->tag
);
718 printf("Failed to alloc I/O ext. for bus %d\n",
724 if (!pb
->io_32bit
&& pi
->address
> 0xFFFF) {
726 pd
->enable
&= ~PCI_CONF_ENABLE_IO
;
728 pd
->enable
|= PCI_CONF_ENABLE_IO
;
730 if (pci_conf_debug
) {
731 print_tag(pd
->pc
, pd
->tag
);
732 printf("Putting %" PRIu64
" I/O bytes @ %#" PRIx64
733 " (reg %x)\n", pi
->size
, pi
->address
, pi
->reg
);
735 pci_conf_write(pd
->pc
, pd
->tag
, pi
->reg
,
736 PCI_MAPREG_IO_ADDR(pi
->address
) | PCI_MAPREG_TYPE_IO
);
742 setup_memwins(pciconf_bus_t
*pb
)
749 for (pm
=pb
->pcimemwin
; pm
< &pb
->pcimemwin
[pb
->nmemwin
] ; pm
++) {
754 ex
= (pm
->prefetch
) ? pb
->pmemext
: pb
->memext
;
755 pm
->address
= pci_allocate_range(ex
, pm
->size
, pm
->align
);
756 if (pm
->address
== -1) {
757 print_tag(pd
->pc
, pd
->tag
);
759 "Failed to allocate PCI memory space (%" PRIu64
760 " req)\n", pm
->size
);
763 if (pd
->ppb
&& pm
->reg
== 0) {
764 ex
= extent_create("pciconf", pm
->address
,
765 pm
->address
+ pm
->size
, M_DEVBUF
, NULL
, 0,
768 print_tag(pd
->pc
, pd
->tag
);
769 printf("Failed to alloc MEM ext. for bus %d\n",
774 pd
->ppb
->pmemext
= ex
;
776 pd
->ppb
->memext
= ex
;
780 if (pm
->prefetch
&& !pb
->pmem_64bit
&&
781 pm
->address
> 0xFFFFFFFFULL
) {
783 pd
->enable
&= ~PCI_CONF_ENABLE_MEM
;
785 pd
->enable
|= PCI_CONF_ENABLE_MEM
;
787 if (pm
->reg
!= PCI_MAPREG_ROM
) {
788 if (pci_conf_debug
) {
789 print_tag(pd
->pc
, pd
->tag
);
791 "Putting %" PRIu64
" MEM bytes @ %#"
792 PRIx64
" (reg %x)\n", pm
->size
,
793 pm
->address
, pm
->reg
);
795 base
= pci_conf_read(pd
->pc
, pd
->tag
, pm
->reg
);
796 base
= PCI_MAPREG_MEM_ADDR(pm
->address
) |
797 PCI_MAPREG_MEM_TYPE(base
);
798 pci_conf_write(pd
->pc
, pd
->tag
, pm
->reg
, base
);
799 if (PCI_MAPREG_MEM_TYPE(base
) ==
800 PCI_MAPREG_MEM_TYPE_64BIT
) {
802 (PCI_MAPREG_MEM64_ADDR(pm
->address
) >> 32);
803 pci_conf_write(pd
->pc
, pd
->tag
, pm
->reg
+ 4,
808 for (pm
=pb
->pcimemwin
; pm
< &pb
->pcimemwin
[pb
->nmemwin
] ; pm
++) {
809 if (pm
->reg
== PCI_MAPREG_ROM
&& pm
->address
!= -1) {
811 if (!(pd
->enable
& PCI_CONF_MAP_ROM
))
813 if (pci_conf_debug
) {
814 print_tag(pd
->pc
, pd
->tag
);
816 "Putting %" PRIu64
" ROM bytes @ %#"
817 PRIx64
" (reg %x)\n", pm
->size
,
818 pm
->address
, pm
->reg
);
820 base
= (pcireg_t
) pm
->address
;
821 if (pd
->enable
& PCI_CONF_ENABLE_ROM
)
822 base
|= PCI_MAPREG_ROM_ENABLE
;
824 pci_conf_write(pd
->pc
, pd
->tag
, pm
->reg
, base
);
831 * Configure I/O, memory, and prefetcable memory spaces, then make
832 * a call to configure_bus().
835 configure_bridge(pciconf_dev_t
*pd
)
837 unsigned long io_base
, io_limit
, mem_base
, mem_limit
;
839 pcireg_t io
, iohigh
, mem
, cmd
;
843 /* Configure I/O base & limit*/
845 io_base
= pb
->ioext
->ex_start
;
846 io_limit
= pb
->ioext
->ex_end
;
848 io_base
= 0x1000; /* 4K */
853 ((io_base
>> 16) << PCI_BRIDGE_IOHIGH_BASE_SHIFT
) |
854 ((io_limit
>> 16) << PCI_BRIDGE_IOHIGH_LIMIT_SHIFT
);
856 if (io_limit
> 0xFFFF) {
857 printf("Bus %d bridge does not support 32-bit I/O. ",
859 printf("Disabling I/O accesses\n");
860 io_base
= 0x1000; /* 4K */
865 io
= pci_conf_read(pb
->pc
, pd
->tag
, PCI_BRIDGE_STATIO_REG
) &
866 (PCI_BRIDGE_STATIO_STATUS_MASK
<< PCI_BRIDGE_STATIO_STATUS_SHIFT
);
867 io
|= (((io_base
>> 8) & PCI_BRIDGE_STATIO_IOBASE_MASK
)
868 << PCI_BRIDGE_STATIO_IOBASE_SHIFT
);
869 io
|= (((io_limit
>> 8) & PCI_BRIDGE_STATIO_IOLIMIT_MASK
)
870 << PCI_BRIDGE_STATIO_IOLIMIT_SHIFT
);
871 pci_conf_write(pb
->pc
, pd
->tag
, PCI_BRIDGE_STATIO_REG
, io
);
872 pci_conf_write(pb
->pc
, pd
->tag
, PCI_BRIDGE_IOHIGH_REG
, iohigh
);
874 /* Configure mem base & limit */
876 mem_base
= pb
->memext
->ex_start
;
877 mem_limit
= pb
->memext
->ex_end
;
879 mem_base
= 0x100000; /* 1M */
880 mem_limit
= 0x000000;
882 #if ULONG_MAX > 0xffffffff
883 if (mem_limit
> 0xFFFFFFFFULL
) {
884 printf("Bus %d bridge MEM range out of range. ", pb
->busno
);
885 printf("Disabling MEM accesses\n");
886 mem_base
= 0x100000; /* 1M */
887 mem_limit
= 0x000000;
890 mem
= (((mem_base
>> 20) & PCI_BRIDGE_MEMORY_BASE_MASK
)
891 << PCI_BRIDGE_MEMORY_BASE_SHIFT
);
892 mem
|= (((mem_limit
>> 20) & PCI_BRIDGE_MEMORY_LIMIT_MASK
)
893 << PCI_BRIDGE_MEMORY_LIMIT_SHIFT
);
894 pci_conf_write(pb
->pc
, pd
->tag
, PCI_BRIDGE_MEMORY_REG
, mem
);
896 /* Configure prefetchable mem base & limit */
898 mem_base
= pb
->pmemext
->ex_start
;
899 mem_limit
= pb
->pmemext
->ex_end
;
901 mem_base
= 0x100000; /* 1M */
902 mem_limit
= 0x000000;
904 mem
= pci_conf_read(pb
->pc
, pd
->tag
, PCI_BRIDGE_PREFETCHMEM_REG
);
905 #if ULONG_MAX > 0xffffffff
906 if (!PCI_BRIDGE_PREFETCHMEM_64BITS(mem
) && mem_limit
> 0xFFFFFFFFULL
) {
907 printf("Bus %d bridge does not support 64-bit PMEM. ",
909 printf("Disabling prefetchable-MEM accesses\n");
910 mem_base
= 0x100000; /* 1M */
911 mem_limit
= 0x000000;
914 mem
= (((mem_base
>> 20) & PCI_BRIDGE_PREFETCHMEM_BASE_MASK
)
915 << PCI_BRIDGE_PREFETCHMEM_BASE_SHIFT
);
916 mem
|= (((mem_limit
>> 20) & PCI_BRIDGE_PREFETCHMEM_LIMIT_MASK
)
917 << PCI_BRIDGE_PREFETCHMEM_LIMIT_SHIFT
);
918 pci_conf_write(pb
->pc
, pd
->tag
, PCI_BRIDGE_PREFETCHMEM_REG
, mem
);
920 * XXX -- 64-bit systems need a lot more than just this...
922 if (sizeof(u_long
) > 4) {
923 mem_base
= (int64_t) mem_base
>> 32;
924 mem_limit
= (int64_t) mem_limit
>> 32;
926 pci_conf_write(pb
->pc
, pd
->tag
, PCI_BRIDGE_PREFETCHBASE32_REG
,
927 mem_base
& 0xffffffff);
928 pci_conf_write(pb
->pc
, pd
->tag
, PCI_BRIDGE_PREFETCHLIMIT32_REG
,
929 mem_limit
& 0xffffffff);
931 rv
= configure_bus(pb
);
934 extent_destroy(pb
->ioext
);
936 extent_destroy(pb
->memext
);
938 extent_destroy(pb
->pmemext
);
940 cmd
= pci_conf_read(pd
->pc
, pd
->tag
, PCI_BRIDGE_CONTROL_REG
);
941 cmd
&= PCI_BRIDGE_CONTROL_MASK
;
942 cmd
|= (PCI_BRIDGE_CONTROL_PERE
| PCI_BRIDGE_CONTROL_SERR
)
943 << PCI_BRIDGE_CONTROL_SHIFT
;
945 cmd
|= PCI_BRIDGE_CONTROL_SECFASTB2B
946 << PCI_BRIDGE_CONTROL_SHIFT
;
948 pci_conf_write(pd
->pc
, pd
->tag
, PCI_BRIDGE_CONTROL_REG
, cmd
);
949 cmd
= pci_conf_read(pd
->pc
, pd
->tag
, PCI_COMMAND_STATUS_REG
);
950 cmd
|= PCI_COMMAND_IO_ENABLE
| PCI_COMMAND_MEM_ENABLE
;
951 pci_conf_write(pd
->pc
, pd
->tag
, PCI_COMMAND_STATUS_REG
, cmd
);
958 * Calculate latency values, allocate I/O and MEM segments, then set them
959 * up. If a PCI-PCI bridge is found, configure the bridge separately,
960 * which will cause a recursive call back here.
963 configure_bus(pciconf_bus_t
*pb
)
966 int def_ltim
, max_ltim
, band
, bus_mhz
;
968 if (pb
->ndevs
== 0) {
970 printf("PCI bus %d - no devices\n", pb
->busno
);
973 bus_mhz
= pb
->freq_66
? 66 : 33;
974 max_ltim
= pb
->max_mingnt
* bus_mhz
/ 4; /* cvt to cycle count */
975 band
= 4000000; /* 0.25us cycles/sec */
976 if (band
< pb
->bandwidth_used
) {
977 printf("PCI bus %d: Warning: Total bandwidth exceeded!? (%d)\n",
978 pb
->busno
, pb
->bandwidth_used
);
981 def_ltim
= (band
- pb
->bandwidth_used
) / pb
->ndevs
;
982 if (def_ltim
> pb
->min_maxlat
)
983 def_ltim
= pb
->min_maxlat
;
984 def_ltim
= def_ltim
* bus_mhz
/ 4;
986 def_ltim
= (def_ltim
+ 7) & ~7;
987 max_ltim
= (max_ltim
+ 7) & ~7;
989 pb
->def_ltim
= MIN( def_ltim
, 255 );
990 pb
->max_ltim
= MIN( MAX(max_ltim
, def_ltim
), 255 );
993 * Now we have what we need to initialize the devices.
994 * It would probably be better if we could allocate all of these
995 * for all busses at once, but "not right now". First, get a list
996 * of free memory ranges from the m.d. system.
998 if (setup_iowins(pb
) || setup_memwins(pb
)) {
999 printf("PCI bus configuration failed: ");
1000 printf("unable to assign all I/O and memory ranges.");
1005 * Configure the latency for the devices, and enable them.
1007 for (pd
=pb
->device
; pd
< &pb
->device
[pb
->ndevs
] ; pd
++) {
1008 pcireg_t cmd
, class, misc
;
1011 if (pci_conf_debug
) {
1012 print_tag(pd
->pc
, pd
->tag
);
1013 printf("Configuring device.\n");
1015 class = pci_conf_read(pd
->pc
, pd
->tag
, PCI_CLASS_REG
);
1016 misc
= pci_conf_read(pd
->pc
, pd
->tag
, PCI_BHLC_REG
);
1017 cmd
= pci_conf_read(pd
->pc
, pd
->tag
, PCI_COMMAND_STATUS_REG
);
1018 if (pd
->enable
& PCI_CONF_ENABLE_PARITY
)
1019 cmd
|= PCI_COMMAND_PARITY_ENABLE
;
1020 if (pd
->enable
& PCI_CONF_ENABLE_SERR
)
1021 cmd
|= PCI_COMMAND_SERR_ENABLE
;
1023 cmd
|= PCI_COMMAND_BACKTOBACK_ENABLE
;
1024 if (PCI_CLASS(class) != PCI_CLASS_BRIDGE
||
1025 PCI_SUBCLASS(class) != PCI_SUBCLASS_BRIDGE_PCI
) {
1026 if (pd
->enable
& PCI_CONF_ENABLE_IO
)
1027 cmd
|= PCI_COMMAND_IO_ENABLE
;
1028 if (pd
->enable
& PCI_CONF_ENABLE_MEM
)
1029 cmd
|= PCI_COMMAND_MEM_ENABLE
;
1030 if (pd
->enable
& PCI_CONF_ENABLE_BM
)
1031 cmd
|= PCI_COMMAND_MASTER_ENABLE
;
1032 ltim
= pd
->min_gnt
* bus_mhz
/ 4;
1033 ltim
= MIN (MAX (pb
->def_ltim
, ltim
), pb
->max_ltim
);
1035 cmd
|= PCI_COMMAND_MASTER_ENABLE
;
1036 ltim
= MIN (pb
->def_ltim
, pb
->max_ltim
);
1039 (PCI_CONF_ENABLE_MEM
|PCI_CONF_ENABLE_IO
)) == 0) {
1040 print_tag(pd
->pc
, pd
->tag
);
1041 printf("Disabled due to lack of resources.\n");
1042 cmd
&= ~(PCI_COMMAND_MASTER_ENABLE
|
1043 PCI_COMMAND_IO_ENABLE
| PCI_COMMAND_MEM_ENABLE
);
1045 pci_conf_write(pd
->pc
, pd
->tag
, PCI_COMMAND_STATUS_REG
, cmd
);
1047 misc
&= ~((PCI_LATTIMER_MASK
<< PCI_LATTIMER_SHIFT
) |
1048 (PCI_CACHELINE_MASK
<< PCI_CACHELINE_SHIFT
));
1049 misc
|= (ltim
& PCI_LATTIMER_MASK
) << PCI_LATTIMER_SHIFT
;
1050 misc
|= ((pb
->cacheline_size
>> 2) & PCI_CACHELINE_MASK
) <<
1051 PCI_CACHELINE_SHIFT
;
1052 pci_conf_write(pd
->pc
, pd
->tag
, PCI_BHLC_REG
, misc
);
1055 if (configure_bridge(pd
) < 0)
1061 if (pci_conf_debug
) {
1062 printf("PCI bus %d configured\n", pb
->busno
);
1069 * Let's configure the PCI bus.
1070 * This consists of basically scanning for all existing devices,
1071 * identifying their needs, and then making another pass over them
1074 * 2. Memory addresses (Prefetchable and not)
1075 * 3. PCI command register
1076 * 4. The latency part of the PCI BHLC (BIST (Built-In Self Test),
1077 * Header type, Latency timer, Cache line size) register
1079 * The command register is set to enable fast back-to-back transactions
1080 * if the host bridge says it can handle it. We also configure
1081 * Master Enable, SERR enable, parity enable, and (if this is not a
1082 * PCI-PCI bridge) the I/O and Memory spaces. Apparently some devices
1083 * will not report some I/O space.
1085 * The latency is computed to be a "fair share" of the bus bandwidth.
1086 * The bus bandwidth variable is initialized to the number of PCI cycles
1087 * in one second. The number of cycles taken for one transaction by each
1088 * device (MAX_LAT + MIN_GNT) is then subtracted from the bandwidth.
1089 * Care is taken to ensure that the latency timer won't be set such that
1090 * it would exceed the critical time for any device.
1092 * This is complicated somewhat due to the presence of bridges. PCI-PCI
1093 * bridges are probed and configured recursively.
1096 pci_configure_bus(pci_chipset_tag_t pc
, struct extent
*ioext
,
1097 struct extent
*memext
, struct extent
*pmemext
, int firstbus
,
1103 pb
= malloc (sizeof (pciconf_bus_t
), M_DEVBUF
, M_NOWAIT
);
1104 pb
->busno
= firstbus
;
1105 pb
->next_busno
= pb
->busno
+ 1;
1106 pb
->last_busno
= 255;
1107 pb
->cacheline_size
= cacheline_size
;
1108 pb
->parent_bus
= NULL
;
1113 pb
->memext
= memext
;
1114 if (pmemext
== NULL
) {
1115 pb
->pmemext
= memext
;
1117 pb
->pmemext
= pmemext
;
1120 pb
->io_total
= pb
->mem_total
= pb
->pmem_total
= 0;
1123 pb
->last_busno
= pb
->next_busno
-1;
1125 rv
= configure_bus(pb
);