pciconf.c revision 1.15 1 /* $NetBSD: pciconf.c,v 1.15 2002/02/21 20:22:48 kleink Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Allen Briggs for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
23 * written permission.
24 *
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.
36 */
37 /*
38 * Derived in part from code from PMON/2000 (http://pmon.groupbsd.org/).
39 */
40
41 /*
42 * To do:
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
48 * ROMs.
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)
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: pciconf.c,v 1.15 2002/02/21 20:22:48 kleink Exp $");
69
70 #include "opt_pci.h"
71
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>
77
78 #include <dev/pci/pcivar.h>
79 #include <dev/pci/pciconf.h>
80 #include <dev/pci/pcidevs.h>
81
82 int pci_conf_debug = 0;
83
84 #if !defined(MIN)
85 #define MIN(a,b) (((a)<(b))?(a):(b))
86 #define MAX(a,b) (((a)>(b))?(a):(b))
87 #endif
88
89 /* per-bus constants. */
90 #define MAX_CONF_DEV 32 /* Arbitrary */
91 #define MAX_CONF_MEM (3 * MAX_CONF_DEV) /* Avg. 3 per device -- Arb. */
92 #define MAX_CONF_IO (3 * MAX_CONF_DEV) /* Avg. 1 per device -- Arb. */
93
94 #define PCI_BUSNO_SPACING (1 << 5)
95
96 struct _s_pciconf_bus_t; /* Forward declaration */
97
98 typedef struct _s_pciconf_dev_t {
99 int ipin;
100 int iline;
101 int min_gnt;
102 int max_lat;
103 int enable;
104 pcitag_t tag;
105 pci_chipset_tag_t pc;
106 struct _s_pciconf_bus_t *ppb; /* I am really a bridge */
107 } pciconf_dev_t;
108
109 typedef struct _s_pciconf_win_t {
110 pciconf_dev_t *dev;
111 int reg; /* 0 for busses */
112 int align;
113 int prefetch;
114 u_int64_t size;
115 u_int64_t address;
116 } pciconf_win_t;
117
118 typedef struct _s_pciconf_bus_t {
119 int busno;
120 int next_busno;
121 int last_busno;
122 int busno_spacing;
123 int max_mingnt;
124 int min_maxlat;
125 int cacheline_size;
126 int prefetch;
127 int fast_b2b;
128 int freq_66;
129 int def_ltim;
130 int max_ltim;
131 int bandwidth_used;
132 int swiz;
133 int io_32bit;
134 int pmem_64bit;
135
136 int ndevs;
137 pciconf_dev_t device[MAX_CONF_DEV];
138
139 /* These should be sorted in order of decreasing size */
140 int nmemwin;
141 pciconf_win_t pcimemwin[MAX_CONF_MEM];
142 int niowin;
143 pciconf_win_t pciiowin[MAX_CONF_IO];
144
145 bus_size_t io_total;
146 bus_size_t mem_total;
147 bus_size_t pmem_total;
148
149 struct extent *ioext;
150 struct extent *memext;
151 struct extent *pmemext;
152
153 pci_chipset_tag_t pc;
154 struct _s_pciconf_bus_t *parent_bus;
155 } pciconf_bus_t;
156
157 static int probe_bus(pciconf_bus_t *);
158 static void alloc_busno(pciconf_bus_t *, pciconf_bus_t *);
159 static int pci_do_device_query(pciconf_bus_t *, pcitag_t, int, int, int);
160 static int setup_iowins(pciconf_bus_t *);
161 static int setup_memwins(pciconf_bus_t *);
162 static int configure_bridge(pciconf_dev_t *);
163 static int configure_bus(pciconf_bus_t *);
164 static u_int64_t pci_allocate_range(struct extent *, u_int64_t, int);
165 static pciconf_win_t *get_io_desc(pciconf_bus_t *, bus_size_t);
166 static pciconf_win_t *get_mem_desc(pciconf_bus_t *, bus_size_t);
167 static pciconf_bus_t *query_bus(pciconf_bus_t *, pciconf_dev_t *, int);
168
169 static void print_tag(pci_chipset_tag_t, pcitag_t);
170
171 static void
172 print_tag(pci_chipset_tag_t pc, pcitag_t tag)
173 {
174 int bus, dev, func;
175
176 pci_decompose_tag(pc, tag, &bus, &dev, &func);
177 printf("PCI: bus %d, device %d, function %d: ", bus, dev, func);
178 }
179
180 /************************************************************************/
181 /************************************************************************/
182 /*********************** Bus probing routines ***********************/
183 /************************************************************************/
184 /************************************************************************/
185 static pciconf_win_t *
186 get_io_desc(pciconf_bus_t *pb, bus_size_t size)
187 {
188 int i, n;
189
190 n = pb->niowin;
191 for (i=n; i > 0 && size > pb->pciiowin[i-1].size; i--)
192 pb->pciiowin[i] = pb->pciiowin[i-1]; /* struct copy */
193 return &pb->pciiowin[i];
194 }
195
196 static pciconf_win_t *
197 get_mem_desc(pciconf_bus_t *pb, bus_size_t size)
198 {
199 int i, n;
200
201 n = pb->nmemwin;
202 for (i=n; i > 0 && size > pb->pcimemwin[i-1].size; i--)
203 pb->pcimemwin[i] = pb->pcimemwin[i-1]; /* struct copy */
204 return &pb->pcimemwin[i];
205 }
206
207 /*
208 * Set up bus common stuff, then loop over devices & functions.
209 * If we find something, call pci_do_device_query()).
210 */
211 static int
212 probe_bus(pciconf_bus_t *pb)
213 {
214 int device, maxdevs;
215 #ifdef __PCI_BUS_DEVORDER
216 char devs[32];
217 int i;
218 #endif
219
220 maxdevs = pci_bus_maxdevs(pb->pc, pb->busno);
221 pb->ndevs = 0;
222 pb->niowin = 0;
223 pb->nmemwin = 0;
224 pb->freq_66 = 1;
225 pb->fast_b2b = 1;
226 pb->prefetch = 1;
227 pb->max_mingnt = 0; /* we are looking for the maximum */
228 pb->min_maxlat = 0x100; /* we are looking for the minimum */
229 pb->bandwidth_used = 0;
230
231 #ifdef __PCI_BUS_DEVORDER
232 pci_bus_devorder(pb->pc, pb->busno, devs);
233 for (i=0; (device=devs[i]) < 32 && device >= 0; i++) {
234 #else
235 for (device=0; device < maxdevs; device++) {
236 #endif
237 pcitag_t tag;
238 pcireg_t id, bhlcr;
239 int function, nfunction;
240 int confmode;
241
242 tag = pci_make_tag(pb->pc, pb->busno, device, 0);
243 if (pci_conf_debug) {
244 print_tag(pb->pc, tag);
245 }
246 id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
247
248 if (pci_conf_debug) {
249 printf("id=%x: Vendor=%x, Product=%x\n",
250 id, PCI_VENDOR(id),PCI_PRODUCT(id));
251 }
252 /* Invalid vendor ID value? */
253 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
254 continue;
255
256 bhlcr = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
257 nfunction = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
258 for (function = 0 ; function < nfunction ; function++) {
259 tag = pci_make_tag(pb->pc, pb->busno, device, function);
260 id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
261 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
262 continue;
263 if (pb->ndevs+1 < MAX_CONF_DEV) {
264 if (pci_conf_debug) {
265 print_tag(pb->pc, tag);
266 printf("Found dev 0x%04x 0x%04x -- "
267 "really probing.\n",
268 PCI_VENDOR(id), PCI_PRODUCT(id));
269 }
270 #ifdef __HAVE_PCI_CONF_HOOK
271 confmode = pci_conf_hook(pb->pc, pb->busno,
272 device, function, id);
273 if (confmode == 0)
274 continue;
275 #else
276 /*
277 * Don't enable expansion ROMS -- some cards
278 * share address decoders between the EXPROM
279 * and PCI memory space, and enabling the ROM
280 * when not needed will cause all sorts of
281 * lossage.
282 */
283 confmode = PCI_CONF_ALL & ~PCI_CONF_MAP_ROM;
284 #endif
285 if (pci_do_device_query(pb, tag, device,
286 function, confmode))
287 return -1;
288 pb->ndevs++;
289 }
290 }
291 }
292 return 0;
293 }
294
295 static void
296 alloc_busno(pciconf_bus_t *parent, pciconf_bus_t *pb)
297 {
298 pb->busno = parent->next_busno;
299 if (parent->next_busno + parent->busno_spacing > parent->last_busno)
300 panic("Too many PCI busses on bus %d", parent->busno);
301 parent->next_busno = parent->next_busno + parent->busno_spacing;
302 pb->next_busno = pb->busno+1;
303 pb->busno_spacing = parent->busno_spacing >> 1;
304 if (!pb->busno_spacing)
305 panic("PCI busses nested too deep.");
306 pb->last_busno = parent->next_busno - 1;
307 }
308
309 static pciconf_bus_t *
310 query_bus(pciconf_bus_t *parent, pciconf_dev_t *pd, int dev)
311 {
312 pciconf_bus_t *pb;
313 pcireg_t busreg, io, pmem;
314 pciconf_win_t *pi, *pm;
315
316 pb = malloc (sizeof (pciconf_bus_t), M_DEVBUF, M_NOWAIT);
317 if (!pb)
318 panic("Unable to allocate memory for PCI configuration.");
319
320 pb->cacheline_size = parent->cacheline_size;
321 pb->parent_bus = parent;
322 alloc_busno(parent, pb);
323 if (pci_conf_debug)
324 printf("PCI bus bridge covers busses %d-%d\n",
325 pb->busno, pb->last_busno);
326
327 busreg = parent->busno << PCI_BRIDGE_BUS_PRIMARY_SHIFT;
328 busreg |= pb->busno << PCI_BRIDGE_BUS_SECONDARY_SHIFT;
329 busreg |= pb->last_busno << PCI_BRIDGE_BUS_SUBORDINATE_SHIFT;
330 pci_conf_write(parent->pc, pd->tag, PCI_BRIDGE_BUS_REG, busreg);
331
332 pb->swiz = parent->swiz + dev;
333
334 pb->ioext = NULL;
335 pb->memext = NULL;
336 pb->pmemext = NULL;
337 pb->pc = parent->pc;
338 pb->io_total = pb->mem_total = pb->pmem_total = 0;
339
340 pb->io_32bit = 0;
341 if (parent->io_32bit) {
342 io = pci_conf_read(parent->pc, pd->tag, PCI_BRIDGE_STATIO_REG);
343 if (PCI_BRIDGE_IO_32BITS(io)) {
344 pb->io_32bit = 1;
345 }
346 }
347
348 pb->pmem_64bit = 0;
349 if (parent->pmem_64bit) {
350 pmem = pci_conf_read(parent->pc, pd->tag,
351 PCI_BRIDGE_PREFETCHMEM_REG);
352 if (PCI_BRIDGE_PREFETCHMEM_64BITS(pmem)) {
353 pb->pmem_64bit = 1;
354 }
355 }
356
357 if (probe_bus(pb)) {
358 printf("Failed to probe bus %d\n", pb->busno);
359 goto err;
360 }
361
362 if (pb->io_total > 0) {
363 if (parent->niowin >= MAX_CONF_IO) {
364 printf("pciconf: too many I/O windows\n");
365 goto err;
366 }
367 pb->io_total |= 0xfff; /* Round up */
368 pi = get_io_desc(parent, pb->io_total);
369 pi->dev = pd;
370 pi->reg = 0;
371 pi->size = pb->io_total;
372 pi->align = 0x1000; /* 4K alignment */
373 pi->prefetch = 0;
374 parent->niowin++;
375 parent->io_total += pb->io_total;
376 }
377
378 if (pb->mem_total > 0) {
379 if (parent->nmemwin >= MAX_CONF_MEM) {
380 printf("pciconf: too many MEM windows\n");
381 goto err;
382 }
383 pb->mem_total |= 0xfffff; /* Round up */
384 pm = get_mem_desc(parent, pb->mem_total);
385 pm->dev = pd;
386 pm->reg = 0;
387 pm->size = pb->mem_total;
388 pm->align = 0x100000; /* 1M alignment */
389 pm->prefetch = 0;
390 parent->nmemwin++;
391 parent->mem_total += pb->mem_total;
392 }
393
394 if (pb->pmem_total > 0) {
395 if (parent->nmemwin >= MAX_CONF_MEM) {
396 printf("pciconf: too many MEM windows\n");
397 goto err;
398 }
399 pb->pmem_total |= 0xfffff; /* Round up */
400 pm = get_mem_desc(parent, pb->pmem_total);
401 pm->dev = pd;
402 pm->reg = 0;
403 pm->size = pb->pmem_total;
404 pm->align = 0x100000; /* 1M alignment */
405 pm->prefetch = 1;
406 parent->nmemwin++;
407 parent->pmem_total += pb->pmem_total;
408 }
409
410 return pb;
411 err:
412 free(pb, M_DEVBUF);
413 return NULL;
414 }
415
416 static int
417 pci_do_device_query(pciconf_bus_t *pb, pcitag_t tag, int dev, int func, int mode)
418 {
419 pciconf_dev_t *pd;
420 pciconf_win_t *pi, *pm;
421 pcireg_t class, cmd, icr, bar, mask, bar64, mask64;
422 u_int64_t size;
423 int br, width;
424
425 pd = &pb->device[pb->ndevs];
426 pd->pc = pb->pc;
427 pd->tag = tag;
428 pd->ppb = NULL;
429 pd->enable = mode;
430
431 class = pci_conf_read(pb->pc, tag, PCI_CLASS_REG);
432
433 cmd = pci_conf_read(pb->pc, tag, PCI_COMMAND_STATUS_REG);
434
435 if (PCI_CLASS(class) != PCI_CLASS_BRIDGE) {
436 cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
437 PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
438 pci_conf_write(pb->pc, tag, PCI_COMMAND_STATUS_REG, cmd);
439 } else if (pci_conf_debug) {
440 print_tag(pb->pc, tag);
441 printf("device is a bridge; not clearing enables\n");
442 }
443
444 if ((cmd & PCI_STATUS_BACKTOBACK_SUPPORT) == 0)
445 pb->fast_b2b = 0;
446
447 if ((cmd & PCI_STATUS_66MHZ_SUPPORT) == 0)
448 pb->freq_66 = 0;
449
450 if ( (PCI_CLASS(class) == PCI_CLASS_BRIDGE)
451 && (PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI)) {
452 pd->ppb = query_bus(pb, pd, dev);
453 if (pd->ppb == NULL)
454 return -1;
455 return 0;
456 }
457
458 icr = pci_conf_read(pb->pc, tag, PCI_INTERRUPT_REG);
459 pd->ipin = PCI_INTERRUPT_PIN(icr);
460 pd->iline = PCI_INTERRUPT_LINE(icr);
461 pd->min_gnt = PCI_MIN_GNT(icr);
462 pd->max_lat = PCI_MAX_LAT(icr);
463 if (pd->iline || pd->ipin) {
464 pci_conf_interrupt(pb->pc, pb->busno, dev, pd->ipin, pb->swiz,
465 &pd->iline);
466 icr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
467 icr |= (pd->iline << PCI_INTERRUPT_LINE_SHIFT);
468 pci_conf_write(pb->pc, tag, PCI_INTERRUPT_REG, icr);
469 }
470
471 if (pd->min_gnt != 0 || pd->max_lat != 0) {
472 if (pd->min_gnt != 0 && pd->min_gnt > pb->max_mingnt)
473 pb->max_mingnt = pd->min_gnt;
474
475 if (pd->max_lat != 0 && pd->max_lat < pb->min_maxlat)
476 pb->min_maxlat = pd->max_lat;
477
478 pb->bandwidth_used += pd->min_gnt * 4000000 /
479 (pd->min_gnt + pd->max_lat);
480 }
481
482 width = 4;
483 for (br = PCI_MAPREG_START; br < PCI_MAPREG_END; br += width) {
484 #if 0
485 /* XXX Should only ignore if IDE not in legacy mode? */
486 if (PCI_CLASS(class) == PCI_CLASS_MASS_STORAGE &&
487 PCI_SUBCLASS(class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
488 break;
489 }
490 #endif
491 bar = pci_conf_read(pb->pc, tag, br);
492 pci_conf_write(pb->pc, tag, br, 0xffffffff);
493 mask = pci_conf_read(pb->pc, tag, br);
494 pci_conf_write(pb->pc, tag, br, bar);
495 width = 4;
496
497 if ( (mode & PCI_CONF_MAP_IO)
498 && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_IO)) {
499 /*
500 * Upper 16 bits must be one. Devices may hardwire
501 * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
502 */
503 mask |= 0xffff0000;
504
505 size = PCI_MAPREG_IO_SIZE(mask);
506 if (size == 0) {
507 if (pci_conf_debug) {
508 print_tag(pb->pc, tag);
509 printf("I/O BAR 0x%x is void\n", br);
510 }
511 continue;
512 }
513
514 if (pb->niowin >= MAX_CONF_IO) {
515 printf("pciconf: too many I/O windows\n");
516 return -1;
517 }
518
519 pi = get_io_desc(pb, size);
520 pi->dev = pd;
521 pi->reg = br;
522 pi->size = (u_int64_t) size;
523 pi->align = 4;
524 pi->prefetch = 0;
525 if (pci_conf_debug) {
526 print_tag(pb->pc, tag);
527 printf("Register 0x%x, I/O size %llu\n",
528 br, pi->size);
529 }
530 pb->niowin++;
531 pb->io_total += size;
532 } else if ((mode & PCI_CONF_MAP_MEM)
533 && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_MEM)) {
534 switch (PCI_MAPREG_MEM_TYPE(mask)) {
535 case PCI_MAPREG_MEM_TYPE_32BIT:
536 case PCI_MAPREG_MEM_TYPE_32BIT_1M:
537 size = (u_int64_t) PCI_MAPREG_MEM_SIZE(mask);
538 break;
539 case PCI_MAPREG_MEM_TYPE_64BIT:
540 bar64 = pci_conf_read(pb->pc, tag, br + 4);
541 pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
542 mask64 = pci_conf_read(pb->pc, tag, br + 4);
543 pci_conf_write(pb->pc, tag, br + 4, bar64);
544 size = (u_int64_t) PCI_MAPREG_MEM64_SIZE(
545 (((u_int64_t) mask64) << 32) | mask);
546 width = 8;
547 continue;
548 default:
549 print_tag(pb->pc, tag);
550 printf("reserved mapping type 0x%x\n",
551 PCI_MAPREG_MEM_TYPE(mask));
552 continue;
553 }
554
555 if (size == 0) {
556 if (pci_conf_debug) {
557 print_tag(pb->pc, tag);
558 printf("MEM%d BAR 0x%x is void\n",
559 PCI_MAPREG_MEM_TYPE(mask) ==
560 PCI_MAPREG_MEM_TYPE_64BIT ?
561 64 : 32, br);
562 }
563 continue;
564 }
565
566 if (pb->nmemwin >= MAX_CONF_MEM) {
567 printf("pciconf: too many memory windows\n");
568 return -1;
569 }
570
571 pm = get_mem_desc(pb, size);
572 pm->dev = pd;
573 pm->reg = br;
574 pm->size = size;
575 pm->align = 4;
576 pm->prefetch = PCI_MAPREG_MEM_PREFETCHABLE(mask);
577 if (pci_conf_debug) {
578 print_tag(pb->pc, tag);
579 printf("Register 0x%x, memory size %llu\n",
580 br, pm->size);
581 }
582 pb->nmemwin++;
583 if (pm->prefetch) {
584 pb->pmem_total += size;
585 } else {
586 pb->mem_total += size;
587 }
588 }
589 }
590
591 if (mode & PCI_CONF_MAP_ROM) {
592 bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
593 pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, 0xfffffffe);
594 mask = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
595 pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, bar);
596
597 if (mask != 0 && mask != 0xffffffff) {
598 if (pb->nmemwin >= MAX_CONF_MEM) {
599 printf("pciconf: too many memory windows\n");
600 return -1;
601 }
602 size = (u_int64_t) PCI_MAPREG_MEM_SIZE(mask);
603
604 pm = get_mem_desc(pb, size);
605 pm->dev = pd;
606 pm->reg = PCI_MAPREG_ROM;
607 pm->size = size;
608 pm->align = 4;
609 pm->prefetch = 1;
610 if (pci_conf_debug) {
611 print_tag(pb->pc, tag);
612 printf("Expansion ROM memory size %llu\n", pm->size);
613 }
614 pb->nmemwin++;
615 pb->pmem_total += size;
616 }
617 } else {
618 /* Ensure ROM is disabled */
619 bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
620 pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, 0xfffffffe);
621 mask = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
622 pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM,
623 bar & ~PCI_MAPREG_ROM_ENABLE);
624 }
625
626 return 0;
627 }
628
629 /************************************************************************/
630 /************************************************************************/
631 /******************** Bus configuration routines ********************/
632 /************************************************************************/
633 /************************************************************************/
634 static u_int64_t
635 pci_allocate_range(struct extent *ex, u_int64_t amt, int align)
636 {
637 int r;
638 u_long addr;
639
640 r = extent_alloc(ex, amt, align, 0, EX_NOWAIT, &addr);
641 if (r) {
642 addr = (u_long) -1;
643 printf("extent_alloc(%p, %llu, %d) returned %d\n",
644 ex, amt, align, r);
645 extent_print(ex);
646 }
647 return (pcireg_t) addr;
648 }
649
650 static int
651 setup_iowins(pciconf_bus_t *pb)
652 {
653 pciconf_win_t *pi;
654 pciconf_dev_t *pd;
655
656 for (pi=pb->pciiowin; pi < &pb->pciiowin[pb->niowin] ; pi++) {
657 if (pi->size == 0)
658 continue;
659
660 pd = pi->dev;
661 pi->address = pci_allocate_range(pb->ioext, pi->size,
662 pi->align);
663 if (pi->address == -1) {
664 print_tag(pd->pc, pd->tag);
665 printf("Failed to allocate PCI I/O space (%llu req)\n",
666 pi->size);
667 return -1;
668 }
669 if (!pb->io_32bit && pi->address > 0xFFFF) {
670 pi->address = 0;
671 pd->enable = 0;
672 }
673 if (pd->ppb && pi->reg == 0) {
674 pd->ppb->ioext = extent_create("pciconf", pi->address,
675 pi->address + pi->size, M_DEVBUF, NULL, 0,
676 EX_NOWAIT);
677 if (pd->ppb->ioext == NULL) {
678 print_tag(pd->pc, pd->tag);
679 printf("Failed to alloc I/O ext. for bus %d\n",
680 pd->ppb->busno);
681 return -1;
682 }
683 continue;
684 }
685 pd->enable |= PCI_CONF_ENABLE_IO;
686 if (pci_conf_debug) {
687 print_tag(pd->pc, pd->tag);
688 printf("Putting %llu I/O bytes @ %#llx (reg %x)\n",
689 pi->size, pi->address, pi->reg);
690 }
691 pci_conf_write(pd->pc, pd->tag, pi->reg,
692 PCI_MAPREG_IO_ADDR(pi->address) | PCI_MAPREG_TYPE_IO);
693 }
694 return 0;
695 }
696
697 static int
698 setup_memwins(pciconf_bus_t *pb)
699 {
700 pciconf_win_t *pm;
701 pciconf_dev_t *pd;
702 pcireg_t base;
703 struct extent *ex;
704
705 for (pm=pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin] ; pm++) {
706 if (pm->size == 0)
707 continue;
708
709 pd = pm->dev;
710 ex = (pm->prefetch) ? pb->pmemext : pb->memext;
711 pm->address = pci_allocate_range(ex, pm->size, pm->align);
712 if (pm->address == -1) {
713 print_tag(pd->pc, pd->tag);
714 printf(
715 "Failed to allocate PCI memory space (%llu req)\n",
716 pm->size);
717 return -1;
718 }
719 if (pd->ppb && pm->reg == 0) {
720 ex = extent_create("pciconf", pm->address,
721 pm->address + pm->size, M_DEVBUF, NULL, 0,
722 EX_NOWAIT);
723 if (ex == NULL) {
724 print_tag(pd->pc, pd->tag);
725 printf("Failed to alloc MEM ext. for bus %d\n",
726 pd->ppb->busno);
727 return -1;
728 }
729 if (pm->prefetch) {
730 pd->ppb->pmemext = ex;
731 } else {
732 pd->ppb->memext = ex;
733 }
734 continue;
735 }
736 if (pm->prefetch && !pb->pmem_64bit &&
737 pm->address > 0xFFFFFFFFULL) {
738 pm->address = 0;
739 pd->enable = 0;
740 } else {
741 pd->enable |= PCI_CONF_ENABLE_MEM;
742 }
743 if (pm->reg != PCI_MAPREG_ROM) {
744 if (pci_conf_debug) {
745 print_tag(pd->pc, pd->tag);
746 printf(
747 "Putting %llu MEM bytes @ %#llx (reg %x)\n",
748 pm->size, pm->address, pm->reg);
749 }
750 base = pci_conf_read(pd->pc, pd->tag, pm->reg);
751 base = PCI_MAPREG_MEM_ADDR(pm->address) |
752 PCI_MAPREG_MEM_TYPE(base);
753 pci_conf_write(pd->pc, pd->tag, pm->reg, base);
754 if (PCI_MAPREG_MEM_TYPE(base) ==
755 PCI_MAPREG_MEM_TYPE_64BIT) {
756 base = (pcireg_t)
757 (PCI_MAPREG_MEM64_ADDR(pm->address) >> 32);
758 pci_conf_write(pd->pc, pd->tag, pm->reg + 4,
759 base);
760 }
761 }
762 }
763 for (pm=pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin] ; pm++) {
764 if (pm->reg == PCI_MAPREG_ROM && pm->address != -1) {
765 pd = pm->dev;
766 if (pci_conf_debug) {
767 print_tag(pd->pc, pd->tag);
768 printf(
769 "Putting %llu ROM bytes @ %#llx (reg %x)\n",
770 pm->size, pm->address, pm->reg);
771 }
772 base = (pcireg_t) (pm->address | PCI_MAPREG_ROM_ENABLE);
773 pci_conf_write(pd->pc, pd->tag, pm->reg, base);
774 }
775 }
776 return 0;
777 }
778
779 /*
780 * Configure I/O, memory, and prefetcable memory spaces, then make
781 * a call to configure_bus().
782 */
783 static int
784 configure_bridge(pciconf_dev_t *pd)
785 {
786 unsigned long io_base, io_limit, mem_base, mem_limit;
787 pciconf_bus_t *pb;
788 pcireg_t io, iohigh, mem, cmd;
789 int rv;
790
791 pb = pd->ppb;
792 /* Configure I/O base & limit*/
793 if (pb->ioext) {
794 io_base = pb->ioext->ex_start;
795 io_limit = pb->ioext->ex_end;
796 } else {
797 io_base = 0x1000; /* 4K */
798 io_limit = 0x0000;
799 }
800 if (pb->io_32bit) {
801 iohigh =
802 ((io_base >> 16) << PCI_BRIDGE_IOHIGH_BASE_SHIFT) |
803 ((io_limit >> 16) << PCI_BRIDGE_IOHIGH_LIMIT_SHIFT);
804 } else {
805 if (io_limit > 0xFFFF) {
806 printf("Bus %d bridge does not support 32-bit I/O. ",
807 pb->busno);
808 printf("Disabling I/O accesses\n");
809 io_base = 0x1000; /* 4K */
810 io_limit = 0x0000;
811 }
812 iohigh = 0;
813 }
814 io = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG) &
815 (PCI_BRIDGE_STATIO_STATUS_MASK << PCI_BRIDGE_STATIO_STATUS_SHIFT);
816 io |= (((io_base >> 8) & PCI_BRIDGE_STATIO_IOBASE_MASK)
817 << PCI_BRIDGE_STATIO_IOBASE_SHIFT);
818 io |= (((io_limit >> 8) & PCI_BRIDGE_STATIO_IOLIMIT_MASK)
819 << PCI_BRIDGE_STATIO_IOLIMIT_SHIFT);
820 pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG, io);
821 pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_IOHIGH_REG, iohigh);
822
823 /* Configure mem base & limit */
824 if (pb->memext) {
825 mem_base = pb->memext->ex_start;
826 mem_limit = pb->memext->ex_end;
827 } else {
828 mem_base = 0x100000; /* 1M */
829 mem_limit = 0x000000;
830 }
831 if (mem_limit > 0xFFFFFFFFULL) {
832 printf("Bus %d bridge MEM range out of range. ", pb->busno);
833 printf("Disabling MEM accesses\n");
834 mem_base = 0x100000; /* 1M */
835 mem_limit = 0x000000;
836 }
837 mem = (((mem_base >> 20) & PCI_BRIDGE_MEMORY_BASE_MASK)
838 << PCI_BRIDGE_MEMORY_BASE_SHIFT);
839 mem |= (((mem_limit >> 20) & PCI_BRIDGE_MEMORY_LIMIT_MASK)
840 << PCI_BRIDGE_MEMORY_LIMIT_SHIFT);
841 pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_MEMORY_REG, mem);
842
843 /* Configure prefetchable mem base & limit */
844 if (pb->pmemext) {
845 mem_base = pb->pmemext->ex_start;
846 mem_limit = pb->pmemext->ex_end;
847 } else {
848 mem_base = 0x100000; /* 1M */
849 mem_limit = 0x000000;
850 }
851 mem = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG);
852 if (!PCI_BRIDGE_PREFETCHMEM_64BITS(mem) && mem_limit > 0xFFFFFFFFULL) {
853 printf("Bus %d bridge does not support 64-bit PMEM. ",
854 pb->busno);
855 printf("Disabling prefetchable-MEM accesses\n");
856 mem_base = 0x100000; /* 1M */
857 mem_limit = 0x000000;
858 }
859 mem = (((mem_base >> 20) & PCI_BRIDGE_PREFETCHMEM_BASE_MASK)
860 << PCI_BRIDGE_PREFETCHMEM_BASE_SHIFT);
861 mem |= (((mem_limit >> 20) & PCI_BRIDGE_PREFETCHMEM_LIMIT_MASK)
862 << PCI_BRIDGE_PREFETCHMEM_LIMIT_SHIFT);
863 pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG, mem);
864 /*
865 * XXX -- 64-bit systems need a lot more than just this...
866 */
867 if (sizeof(u_long) > 4) {
868 mem_base = (int64_t) mem_base >> 32;
869 mem_limit = (int64_t) mem_limit >> 32;
870 }
871 pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHBASE32_REG,
872 mem_base & 0xffffffff);
873 pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHLIMIT32_REG,
874 mem_limit & 0xffffffff);
875
876 rv = configure_bus(pb);
877
878 if (pb->ioext)
879 extent_destroy(pb->ioext);
880 if (pb->memext)
881 extent_destroy(pb->memext);
882 if (pb->pmemext)
883 extent_destroy(pb->pmemext);
884 if (rv == 0) {
885 cmd = pci_conf_read(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG);
886 cmd &= PCI_BRIDGE_CONTROL_MASK;
887 cmd |= (PCI_BRIDGE_CONTROL_PERE | PCI_BRIDGE_CONTROL_SERR)
888 << PCI_BRIDGE_CONTROL_SHIFT;
889 if (pb->fast_b2b) {
890 cmd |= PCI_BRIDGE_CONTROL_SECFASTB2B
891 << PCI_BRIDGE_CONTROL_SHIFT;
892 }
893 pci_conf_write(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG, cmd);
894 cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
895 cmd |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE;
896 pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
897 }
898
899 return rv;
900 }
901
902 /*
903 * Calculate latency values, allocate I/O and MEM segments, then set them
904 * up. If a PCI-PCI bridge is found, configure the bridge separately,
905 * which will cause a recursive call back here.
906 */
907 static int
908 configure_bus(pciconf_bus_t *pb)
909 {
910 pciconf_dev_t *pd;
911 int def_ltim, max_ltim, band, bus_mhz;
912
913 bus_mhz = pb->freq_66 ? 66 : 33;
914 max_ltim = pb->max_mingnt * bus_mhz / 4; /* cvt to cycle count */
915 band = 40000000; /* 0.25us cycles/sec */
916 if (band < pb->bandwidth_used) {
917 printf("PCI bus %d: Warning: Total bandwidth exceeded!?\n",
918 pb->busno);
919 def_ltim = -1;
920 } else {
921 def_ltim = (band - pb->bandwidth_used) / pb->ndevs;
922 if (def_ltim > pb->min_maxlat)
923 def_ltim = pb->min_maxlat;
924 def_ltim = def_ltim * bus_mhz / 4;
925 }
926 def_ltim = (def_ltim + 7) & ~7;
927 max_ltim = (max_ltim + 7) & ~7;
928
929 pb->def_ltim = MIN( def_ltim, 255 );
930 pb->max_ltim = MIN( MAX(max_ltim, def_ltim ), 255 );
931
932 /*
933 * Now we have what we need to initialize the devices.
934 * It would probably be better if we could allocate all of these
935 * for all busses at once, but "not right now". First, get a list
936 * of free memory ranges from the m.d. system.
937 */
938 if (setup_iowins(pb) || setup_memwins(pb)) {
939 printf("PCI bus configuration failed: ");
940 printf("unable to assign all I/O and memory ranges.");
941 return -1;
942 }
943
944 /*
945 * Configure the latency for the devices, and enable them.
946 */
947 for (pd=pb->device ; pd < &pb->device[pb->ndevs] ; pd++) {
948 pcireg_t cmd, class, misc;
949 int ltim;
950
951 if (pci_conf_debug) {
952 print_tag(pd->pc, pd->tag);
953 printf("Configuring device.\n");
954 }
955 class = pci_conf_read(pd->pc, pd->tag, PCI_CLASS_REG);
956 misc = pci_conf_read(pd->pc, pd->tag, PCI_BHLC_REG);
957 cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
958 cmd |= PCI_COMMAND_SERR_ENABLE | PCI_COMMAND_PARITY_ENABLE;
959 if (pb->fast_b2b)
960 cmd |= PCI_COMMAND_BACKTOBACK_ENABLE;
961 if (PCI_CLASS(class) != PCI_CLASS_BRIDGE ||
962 PCI_SUBCLASS(class) != PCI_SUBCLASS_BRIDGE_PCI) {
963 if (pd->enable & PCI_CONF_ENABLE_IO)
964 cmd |= PCI_COMMAND_IO_ENABLE;
965 if (pd->enable & PCI_CONF_ENABLE_MEM)
966 cmd |= PCI_COMMAND_MEM_ENABLE;
967 if (pd->enable & PCI_CONF_ENABLE_BM)
968 cmd |= PCI_COMMAND_MASTER_ENABLE;
969 ltim = pd->min_gnt * bus_mhz / 4;
970 ltim = MIN (MAX (pb->def_ltim, ltim), pb->max_ltim);
971 } else {
972 cmd |= PCI_COMMAND_MASTER_ENABLE;
973 ltim = MIN (pb->def_ltim, pb->max_ltim);
974 }
975 if (!(pd->enable)) {
976 print_tag(pd->pc, pd->tag);
977 printf("Disabled due to lack of resources.\n");
978 cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
979 PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
980 }
981 pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
982
983 misc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
984 (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
985 misc |= (ltim & PCI_LATTIMER_MASK) << PCI_LATTIMER_SHIFT;
986 misc |= ((pb->cacheline_size >> 2) & PCI_CACHELINE_MASK) <<
987 PCI_CACHELINE_SHIFT;
988 pci_conf_write(pd->pc, pd->tag, PCI_BHLC_REG, misc);
989
990 if (pd->ppb) {
991 if (configure_bridge(pd) < 0)
992 return -1;
993 continue;
994 }
995 }
996
997 if (pci_conf_debug) {
998 printf("PCI bus %d configured\n", pb->busno);
999 }
1000
1001 return 0;
1002 }
1003
1004 /*
1005 * Let's configure the PCI bus.
1006 * This consists of basically scanning for all existing devices,
1007 * identifying their needs, and then making another pass over them
1008 * to set:
1009 * 1. I/O addresses
1010 * 2. Memory addresses (Prefetchable and not)
1011 * 3. PCI command register
1012 * 4. The latency part of the PCI BHLC (BIST (Built-In Self Test),
1013 * Header type, Latency timer, Cache line size) register
1014 *
1015 * The command register is set to enable fast back-to-back transactions
1016 * if the host bridge says it can handle it. We also configure
1017 * Master Enable, SERR enable, parity enable, and (if this is not a
1018 * PCI-PCI bridge) the I/O and Memory spaces. Apparently some devices
1019 * will not report some I/O space.
1020 *
1021 * The latency is computed to be a "fair share" of the bus bandwidth.
1022 * The bus bandwidth variable is initialized to the number of PCI cycles
1023 * in one second. The number of cycles taken for one transaction by each
1024 * device (MAX_LAT + MIN_GNT) is then subtracted from the bandwidth.
1025 * Care is taken to ensure that the latency timer won't be set such that
1026 * it would exceed the critical time for any device.
1027 *
1028 * This is complicated somewhat due to the presence of bridges. PCI-PCI
1029 * bridges are probed and configured recursively.
1030 */
1031 int
1032 pci_configure_bus(pci_chipset_tag_t pc, struct extent *ioext,
1033 struct extent *memext, struct extent *pmemext, int firstbus,
1034 int cacheline_size)
1035 {
1036 pciconf_bus_t *pb;
1037 int rv;
1038
1039 pb = malloc (sizeof (pciconf_bus_t), M_DEVBUF, M_NOWAIT);
1040 pb->busno = firstbus;
1041 pb->busno_spacing = PCI_BUSNO_SPACING;
1042 pb->next_busno = pb->busno + 1;
1043 pb->last_busno = 255;
1044 pb->cacheline_size = cacheline_size;
1045 pb->parent_bus = NULL;
1046 pb->swiz = 0;
1047 pb->io_32bit = 1;
1048 pb->pmem_64bit = 0;
1049 pb->ioext = ioext;
1050 pb->memext = memext;
1051 if (pmemext == NULL) {
1052 pb->pmemext = memext;
1053 } else {
1054 pb->pmemext = pmemext;
1055 }
1056 pb->pc = pc;
1057 pb->io_total = pb->mem_total = pb->pmem_total = 0;
1058
1059 rv = probe_bus(pb);
1060 if (rv == 0) {
1061 rv = configure_bus(pb);
1062 }
1063
1064 /*
1065 * All done!
1066 */
1067 free(pb, M_DEVBUF);
1068 return rv;
1069 }
1070