This source file includes following definitions.
- pci_attach_hook
- pci_bus_maxdevs
- pci_make_tag
- pci_decompose_tag
- pci_conf_read
- pci_conf_write
- pci_mode_detect
- pci_intr_map
- pci_intr_string
- pci_intr_establish
- pci_intr_disestablish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 #include <sys/types.h>
84 #include <sys/param.h>
85 #include <sys/time.h>
86 #include <sys/systm.h>
87 #include <sys/errno.h>
88 #include <sys/device.h>
89
90 #include <uvm/uvm_extern.h>
91
92 #define _I386_BUS_DMA_PRIVATE
93 #include <machine/bus.h>
94 #include <machine/pio.h>
95 #include <machine/i8259.h>
96
97 #include "bios.h"
98 #if NBIOS > 0
99 #include <machine/biosvar.h>
100 extern bios_pciinfo_t *bios_pciinfo;
101 #endif
102
103 #include <dev/isa/isavar.h>
104 #include <dev/pci/pcivar.h>
105 #include <dev/pci/pcireg.h>
106 #include <dev/pci/pcidevs.h>
107 #include <dev/pci/ppbreg.h>
108
109 #include "ioapic.h"
110
111 #include <machine/i82093var.h>
112 #if NIOAPIC > 0
113 #include <machine/mpbiosvar.h>
114 #endif
115
116 #include "pcibios.h"
117 #if NPCIBIOS > 0
118 #include <i386/pci/pcibiosvar.h>
119 #endif
120
121 int pci_mode = -1;
122
123 #define PCI_MODE1_ENABLE 0x80000000UL
124 #define PCI_MODE1_ADDRESS_REG 0x0cf8
125 #define PCI_MODE1_DATA_REG 0x0cfc
126
127 #define PCI_MODE2_ENABLE_REG 0x0cf8
128 #define PCI_MODE2_FORWARD_REG 0x0cfa
129
130 #define _m1tag(b, d, f) \
131 (PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8))
132 #define _qe(bus, dev, fcn, vend, prod) \
133 {_m1tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
134 struct {
135 u_int32_t tag;
136 pcireg_t id;
137 } pcim1_quirk_tbl[] = {
138 _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1),
139
140 _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2),
141 _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4),
142
143 _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
144
145 _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
146 {0, 0xffffffff}
147 };
148 #undef _m1tag
149 #undef _qe
150
151
152
153
154
155 struct i386_bus_dma_tag pci_bus_dma_tag = {
156 NULL,
157 _bus_dmamap_create,
158 _bus_dmamap_destroy,
159 _bus_dmamap_load,
160 _bus_dmamap_load_mbuf,
161 _bus_dmamap_load_uio,
162 _bus_dmamap_load_raw,
163 _bus_dmamap_unload,
164 NULL,
165 _bus_dmamem_alloc,
166 _bus_dmamem_free,
167 _bus_dmamem_map,
168 _bus_dmamem_unmap,
169 _bus_dmamem_mmap,
170 };
171
172 void
173 pci_attach_hook(struct device *parent, struct device *self,
174 struct pcibus_attach_args *pba)
175 {
176
177 #if NBIOS > 0
178 if (pba->pba_bus == 0)
179 printf(": configuration mode %d (%s)",
180 pci_mode, (bios_pciinfo?"bios":"no bios"));
181 #else
182 if (pba->pba_bus == 0)
183 printf(": configuration mode %d", pci_mode);
184 #endif
185 }
186
187 int
188 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
189 {
190
191
192
193
194
195
196
197 if (pci_mode == 2)
198 return (16);
199 else
200 return (32);
201 }
202
203 pcitag_t
204 pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
205 {
206 pcitag_t tag;
207
208 switch (pci_mode) {
209 case 1:
210 if (bus >= 256 || device >= 32 || function >= 8)
211 panic("pci_make_tag: bad request");
212
213 tag.mode1 = PCI_MODE1_ENABLE |
214 (bus << 16) | (device << 11) | (function << 8);
215 break;
216 case 2:
217 if (bus >= 256 || device >= 16 || function >= 8)
218 panic("pci_make_tag: bad request");
219
220 tag.mode2.port = 0xc000 | (device << 8);
221 tag.mode2.enable = 0xf0 | (function << 1);
222 tag.mode2.forward = bus;
223 break;
224 default:
225 panic("pci_make_tag: mode not configured");
226 }
227
228 return tag;
229 }
230
231 void
232 pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
233 {
234
235 switch (pci_mode) {
236 case 1:
237 if (bp != NULL)
238 *bp = (tag.mode1 >> 16) & 0xff;
239 if (dp != NULL)
240 *dp = (tag.mode1 >> 11) & 0x1f;
241 if (fp != NULL)
242 *fp = (tag.mode1 >> 8) & 0x7;
243 break;
244 case 2:
245 if (bp != NULL)
246 *bp = tag.mode2.forward & 0xff;
247 if (dp != NULL)
248 *dp = (tag.mode2.port >> 8) & 0xf;
249 if (fp != NULL)
250 *fp = (tag.mode2.enable >> 1) & 0x7;
251 break;
252 default:
253 panic("pci_decompose_tag: mode not configured");
254 }
255 }
256
257 pcireg_t
258 pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
259 {
260 pcireg_t data;
261
262 switch (pci_mode) {
263 case 1:
264 outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
265 data = inl(PCI_MODE1_DATA_REG);
266 outl(PCI_MODE1_ADDRESS_REG, 0);
267 break;
268 case 2:
269 outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
270 outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
271 data = inl(tag.mode2.port | reg);
272 outb(PCI_MODE2_ENABLE_REG, 0);
273 break;
274 default:
275 panic("pci_conf_read: mode not configured");
276 }
277
278 return data;
279 }
280
281 void
282 pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
283 {
284
285 switch (pci_mode) {
286 case 1:
287 outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
288 outl(PCI_MODE1_DATA_REG, data);
289 outl(PCI_MODE1_ADDRESS_REG, 0);
290 break;
291 case 2:
292 outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
293 outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
294 outl(tag.mode2.port | reg, data);
295 outb(PCI_MODE2_ENABLE_REG, 0);
296 break;
297 default:
298 panic("pci_conf_write: mode not configured");
299 }
300 }
301
302 int
303 pci_mode_detect(void)
304 {
305
306 #ifdef PCI_CONF_MODE
307 #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
308 return (pci_mode = PCI_CONF_MODE);
309 #else
310 #error Invalid PCI configuration mode.
311 #endif
312 #else
313 u_int32_t sav, val;
314 int i;
315 pcireg_t idreg;
316
317 if (pci_mode != -1)
318 return (pci_mode);
319
320 #if NBIOS > 0
321
322
323
324
325
326 if (bios_pciinfo != NULL) {
327 if (bios_pciinfo->pci_chars & 0x2)
328 return (pci_mode = 2);
329
330 if (bios_pciinfo->pci_chars & 0x1)
331 return (pci_mode = 1);
332
333
334 }
335 #endif
336
337
338
339
340
341
342
343
344
345 sav = inl(PCI_MODE1_ADDRESS_REG);
346
347 pci_mode = 1;
348
349
350
351 for (i = 0; i < sizeof(pcim1_quirk_tbl) / sizeof(pcim1_quirk_tbl[0]);
352 i++) {
353 pcitag_t t;
354
355 if (!pcim1_quirk_tbl[i].tag)
356 break;
357 t.mode1 = pcim1_quirk_tbl[i].tag;
358 idreg = pci_conf_read(0, t, PCI_ID_REG);
359 if (idreg == pcim1_quirk_tbl[i].id) {
360 #ifdef DEBUG
361 printf("known mode 1 PCI chipset (%08x)\n",
362 idreg);
363 #endif
364 return (pci_mode);
365 }
366 }
367
368
369
370
371
372
373 outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
374 outb(PCI_MODE1_ADDRESS_REG + 3, 0);
375 outw(PCI_MODE1_ADDRESS_REG + 2, 0);
376 val = inl(PCI_MODE1_ADDRESS_REG);
377 if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) {
378 #ifdef DEBUG
379 printf("pci_mode_detect: mode 1 enable failed (%x)\n",
380 val);
381 #endif
382 goto not1;
383 }
384 outl(PCI_MODE1_ADDRESS_REG, 0);
385 val = inl(PCI_MODE1_ADDRESS_REG);
386 if ((val & 0x80fffffc) != 0)
387 goto not1;
388 return (pci_mode);
389 not1:
390 outl(PCI_MODE1_ADDRESS_REG, sav);
391
392
393
394
395
396
397
398
399 outb(PCI_MODE2_ENABLE_REG, 0);
400 outb(PCI_MODE2_FORWARD_REG, 0);
401 if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
402 inb(PCI_MODE2_FORWARD_REG) != 0)
403 goto not2;
404 return (pci_mode = 2);
405 not2:
406 return (pci_mode = 0);
407 #endif
408 }
409
410 int
411 pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
412 {
413 int pin = pa->pa_intrpin;
414 int line = pa->pa_intrline;
415 #if NIOAPIC > 0
416 int rawpin = pa->pa_rawintrpin;
417 struct mp_intr_map *mip;
418 int bus, dev, func;
419 #endif
420
421 #if (NPCIBIOS > 0) || (NIOAPIC > 0)
422 pci_chipset_tag_t pc = pa->pa_pc;
423 pcitag_t intrtag = pa->pa_intrtag;
424 #endif
425
426 if (pin == 0) {
427
428 goto bad;
429 }
430
431 if (pin > PCI_INTERRUPT_PIN_MAX) {
432 printf("pci_intr_map: bad interrupt pin %d\n", pin);
433 goto bad;
434 }
435
436 ihp->line = line;
437 ihp->pin = pin;
438
439 #if NIOAPIC > 0
440 pci_decompose_tag (pc, intrtag, &bus, &dev, &func);
441
442 if (!(ihp->line & PCI_INT_VIA_ISA) && mp_busses != NULL) {
443
444
445
446
447
448 int mpspec_pin = (dev<<2)|(pin-1);
449
450 for (mip = mp_busses[bus].mb_intrs; mip != NULL; mip=mip->next) {
451 if (mip->bus_pin == mpspec_pin) {
452 ihp->line = mip->ioapic_ih | line;
453 return 0;
454 }
455 }
456
457 if (pa->pa_bridgetag) {
458 int pin = PPB_INTERRUPT_SWIZZLE(rawpin, dev);
459 if (pa->pa_bridgeih[pin - 1].line != -1) {
460 ihp->line = pa->pa_bridgeih[pin - 1].line;
461 ihp->line |= line;
462 return 0;
463 }
464 }
465
466
467
468
469 }
470 #endif
471
472 #if NPCIBIOS > 0
473 pci_intr_header_fixup(pc, intrtag, ihp);
474 line = ihp->line & APIC_INT_LINE_MASK;
475 #endif
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491 if (line == 0 || line == I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
492 goto bad;
493
494 if (line >= ICU_LEN) {
495 printf("pci_intr_map: bad interrupt line %d\n", line);
496 goto bad;
497 }
498 if (line == 2) {
499 printf("pci_intr_map: changed line 2 to line 9\n");
500 line = 9;
501 }
502
503 #if NIOAPIC > 0
504 if (!(ihp->line & PCI_INT_VIA_ISA) && mp_busses != NULL) {
505 if (mip == NULL && mp_isa_bus) {
506 for (mip = mp_isa_bus->mb_intrs; mip != NULL;
507 mip = mip->next) {
508 if (mip->bus_pin == line) {
509 ihp->line = mip->ioapic_ih | line;
510 return 0;
511 }
512 }
513 }
514 if (mip == NULL && mp_eisa_bus) {
515 for (mip = mp_eisa_bus->mb_intrs; mip != NULL;
516 mip = mip->next) {
517 if (mip->bus_pin == line) {
518 ihp->line = mip->ioapic_ih | line;
519 return 0;
520 }
521 }
522 }
523 if (mip == NULL) {
524 printf("pci_intr_map: "
525 "bus %d dev %d func %d pin %d; line %d\n",
526 bus, dev, func, pin, line);
527 printf("pci_intr_map: no MP mapping found\n");
528 }
529 }
530 #endif
531
532 return 0;
533
534 bad:
535 ihp->line = -1;
536 return 1;
537 }
538
539 const char *
540 pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
541 {
542 static char irqstr[64];
543 int line = ih.line & APIC_INT_LINE_MASK;
544
545 #if NIOAPIC > 0
546 if (ih.line & APIC_INT_VIA_APIC) {
547 snprintf(irqstr, sizeof irqstr, "apic %d int %d (irq %d)",
548 APIC_IRQ_APIC(ih.line), APIC_IRQ_PIN(ih.line), line);
549 return (irqstr);
550 }
551 #endif
552
553 if (line == 0 || line >= ICU_LEN || line == 2)
554 panic("pci_intr_string: bogus handle 0x%x", line);
555
556 snprintf(irqstr, sizeof irqstr, "irq %d", line);
557 return (irqstr);
558 }
559
560 void *
561 pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
562 int (*func)(void *), void *arg, char *what)
563 {
564 void *ret;
565 int l = ih.line & APIC_INT_LINE_MASK;
566
567 #if NIOAPIC > 0
568 if (l != -1 && ih.line & APIC_INT_VIA_APIC)
569 return (apic_intr_establish(ih.line, IST_LEVEL, level, func,
570 arg, what));
571 #endif
572 if (l == 0 || l >= ICU_LEN || l == 2)
573 panic("pci_intr_establish: bogus handle 0x%x", l);
574
575 ret = isa_intr_establish(NULL, l, IST_LEVEL, level, func, arg, what);
576 #if NPCIBIOS > 0
577 if (ret)
578 pci_intr_route_link(pc, &ih);
579 #endif
580 return (ret);
581 }
582
583 void
584 pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
585 {
586
587 isa_intr_disestablish(NULL, cookie);
588 }