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