pci_machdep.c revision 1.30 1 /* $NetBSD: pci_machdep.c,v 1.30 2001/03/09 20:55:47 leo Exp $ */
2
3 /*
4 * Copyright (c) 1996 Leo Weppelman. All rights reserved.
5 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
6 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles M. Hannum.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41
42 #define _ATARI_BUS_DMA_PRIVATE
43 #include <machine/bus.h>
44
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47
48 #include <uvm/uvm_extern.h>
49
50 #include <machine/cpu.h>
51 #include <machine/iomap.h>
52 #include <machine/mfp.h>
53 #include <machine/bswap.h>
54
55 #include <atari/atari/device.h>
56 #include <atari/pci/pci_vga.h>
57
58 /*
59 * Sizes of pci memory and I/O area.
60 */
61 #define PCI_MEM_END 0x10000000 /* 256 MByte */
62 #define PCI_IO_END 0x10000000 /* 256 MByte */
63
64 /*
65 * We preserve some space at the begin of the pci area for 32BIT_1M
66 * devices and standard vga.
67 */
68 #define PCI_MEM_START 0x00100000 /* 1 MByte */
69 #define PCI_IO_START 0x00004000 /* 16 kByte (some PCI cards allow only
70 I/O addresses up to 0xffff) */
71
72 /*
73 * PCI memory and IO should be aligned acording to this masks
74 */
75 #define PCI_MACHDEP_IO_ALIGN_MASK 0xffffff00
76 #define PCI_MACHDEP_MEM_ALIGN_MASK 0xfffff000
77
78 /*
79 * Convert a PCI 'device' number to a slot number.
80 */
81 #define DEV2SLOT(dev) (3 - dev)
82
83 /*
84 * Struct to hold the memory and I/O datas of the pci devices
85 */
86 struct pci_memreg {
87 LIST_ENTRY(pci_memreg) link;
88 int dev;
89 pcitag_t tag;
90 pcireg_t reg, address, mask;
91 u_int32_t size;
92 u_int32_t csr;
93 };
94
95 typedef LIST_HEAD(pci_memreg_head, pci_memreg) PCI_MEMREG;
96
97 /*
98 * Entry points for PCI DMA. Use only the 'standard' functions.
99 */
100 int _bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int, bus_size_t,
101 bus_size_t, int, bus_dmamap_t *));
102 struct atari_bus_dma_tag pci_bus_dma_tag = {
103 0,
104 0x80000000, /* XXX */
105 _bus_dmamap_create,
106 _bus_dmamap_destroy,
107 _bus_dmamap_load,
108 _bus_dmamap_load_mbuf,
109 _bus_dmamap_load_uio,
110 _bus_dmamap_load_raw,
111 _bus_dmamap_unload,
112 _bus_dmamap_sync,
113 };
114
115 int pcibusprint __P((void *auxp, const char *));
116 int pcibusmatch __P((struct device *, struct cfdata *, void *));
117 void pcibusattach __P((struct device *, struct device *, void *));
118
119 static void enable_pci_devices __P((void));
120 static void insert_into_list __P((PCI_MEMREG *head, struct pci_memreg *elem));
121 static int overlap_pci_areas __P((struct pci_memreg *p,
122 struct pci_memreg *self, u_int addr, u_int size, u_int what));
123 static int pci_config_offset __P((pcitag_t));
124
125 struct cfattach pcibus_ca = {
126 sizeof(struct device), pcibusmatch, pcibusattach
127 };
128
129 /*
130 * We need some static storage to probe pci-busses for VGA cards during
131 * early console init.
132 */
133 static struct atari_bus_space bs_storage[2]; /* 1 iot, 1 memt */
134
135 int
136 pcibusmatch(pdp, cfp, auxp)
137 struct device *pdp;
138 struct cfdata *cfp;
139 void *auxp;
140 {
141 static int nmatched = 0;
142
143 if (strcmp((char *)auxp, "pcibus"))
144 return (0); /* Wrong number... */
145
146 if(atari_realconfig == 0)
147 return (1);
148
149 if (machineid & ATARI_HADES) {
150 /*
151 * The Hades has only one pci bus
152 */
153 if (nmatched)
154 return (0);
155 nmatched++;
156 return (1);
157 }
158 return (0);
159 }
160
161 void
162 pcibusattach(pdp, dp, auxp)
163 struct device *pdp, *dp;
164 void *auxp;
165 {
166 struct pcibus_attach_args pba;
167
168 pba.pba_busname = "pci";
169 pba.pba_pc = NULL;
170 pba.pba_bus = 0;
171 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
172 pba.pba_dmat = &pci_bus_dma_tag;
173 pba.pba_iot = leb_alloc_bus_space_tag(&bs_storage[0]);
174 pba.pba_memt = leb_alloc_bus_space_tag(&bs_storage[1]);
175 if ((pba.pba_iot == NULL) || (pba.pba_memt == NULL)) {
176 printf("leb_alloc_bus_space_tag failed!\n");
177 return;
178 }
179 pba.pba_iot->base = PCI_IO_PHYS;
180 pba.pba_memt->base = PCI_MEM_PHYS;
181
182 if (dp == NULL) {
183 /*
184 * Scan the bus for a VGA-card that we support. If we
185 * find one, try to initialize it to a 'standard' text
186 * mode (80x25).
187 */
188 check_for_vga();
189 return;
190 }
191
192 enable_pci_devices();
193
194 MFP2->mf_aer &= ~(0x27); /* PCI interrupts: HIGH -> LOW */
195
196 printf("\n");
197
198 config_found(dp, &pba, pcibusprint);
199 }
200
201 int
202 pcibusprint(auxp, name)
203 void *auxp;
204 const char *name;
205 {
206 if(name == NULL)
207 return(UNCONF);
208 return(QUIET);
209 }
210
211 void
212 pci_attach_hook(parent, self, pba)
213 struct device *parent, *self;
214 struct pcibus_attach_args *pba;
215 {
216 }
217
218 /*
219 * Initialize the PCI-bus. The Atari-BIOS does not do this, so....
220 * We only disable all devices here. Memory and I/O enabling is done
221 * later at pcibusattach.
222 */
223 void
224 init_pci_bus()
225 {
226 pci_chipset_tag_t pc = NULL; /* XXX */
227 pcitag_t tag;
228 pcireg_t csr;
229 int device, id, maxndevs;
230
231 tag = 0;
232 id = 0;
233
234 maxndevs = pci_bus_maxdevs(pc, 0);
235
236 for (device = 0; device < maxndevs; device++) {
237
238 tag = pci_make_tag(pc, 0, device, 0);
239 id = pci_conf_read(pc, tag, PCI_ID_REG);
240 if (id == 0 || id == 0xffffffff)
241 continue;
242
243 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
244 csr &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
245 csr &= ~PCI_COMMAND_MASTER_ENABLE;
246 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
247 }
248 }
249
250 /*
251 * insert a new element in an existing list that the ID's (size in struct
252 * pci_memreg) are sorted.
253 */
254 static void
255 insert_into_list(head, elem)
256 PCI_MEMREG *head;
257 struct pci_memreg *elem;
258 {
259 struct pci_memreg *p, *q;
260
261 p = LIST_FIRST(head);
262 q = NULL;
263
264 for (; p != NULL && p->size < elem->size; q = p, p = LIST_NEXT(p, link));
265
266 if (q == NULL) {
267 LIST_INSERT_HEAD(head, elem, link);
268 } else {
269 LIST_INSERT_AFTER(q, elem, link);
270 }
271 }
272
273 /*
274 * Test if a new selected area overlaps with an already (probably preselected)
275 * pci area.
276 */
277 static int
278 overlap_pci_areas(p, self, addr, size, what)
279 struct pci_memreg *p, *self;
280 u_int addr, size, what;
281 {
282 struct pci_memreg *q;
283
284 if (p == NULL)
285 return 0;
286
287 q = p;
288 while (q != NULL) {
289 if ((q != self) && (q->csr & what)) {
290 if ((addr >= q->address) && (addr < (q->address + q->size))) {
291 #ifdef DEBUG_PCI_MACHDEP
292 printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
293 self->dev, self->reg, q->dev, q->reg);
294 #endif
295 return 1;
296 }
297 if ((q->address >= addr) && (q->address < (addr + size))) {
298 #ifdef DEBUG_PCI_MACHDEP
299 printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
300 self->dev, self->reg, q->dev, q->reg);
301 #endif
302 return 1;
303 }
304 }
305 q = LIST_NEXT(q, link);
306 }
307 return 0;
308 }
309
310 /*
311 * Enable memory and I/O on pci devices. Care about already enabled devices
312 * (probabaly by the console driver).
313 *
314 * The idea behind the following code is:
315 * We build a by sizes sorted list of the requirements of the different
316 * pci devices. After that we choose the start addresses of that areas
317 * in such a way that they are placed as closed as possible together.
318 */
319 static void
320 enable_pci_devices()
321 {
322 PCI_MEMREG memlist;
323 PCI_MEMREG iolist;
324 struct pci_memreg *p, *q;
325 int dev, reg, id, class;
326 pcitag_t tag;
327 pcireg_t csr, address, mask;
328 pci_chipset_tag_t pc;
329 int sizecnt, membase_1m;
330
331 pc = 0;
332 csr = 0;
333 tag = 0;
334
335 LIST_INIT(&memlist);
336 LIST_INIT(&iolist);
337
338 /*
339 * first step: go through all devices and gather memory and I/O
340 * sizes
341 */
342 for (dev = 0; dev < pci_bus_maxdevs(pc,0); dev++) {
343
344 tag = pci_make_tag(pc, 0, dev, 0);
345 id = pci_conf_read(pc, tag, PCI_ID_REG);
346 if (id == 0 || id == 0xffffffff)
347 continue;
348
349 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
350
351 /*
352 * special case: if a display card is found and memory is enabled
353 * preserve 128k at 0xa0000 as vga memory.
354 * XXX: if a display card is found without being enabled, leave
355 * it alone! You will usually only create conflicts by enabeling
356 * it.
357 */
358 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
359 switch (PCI_CLASS(class)) {
360 case PCI_CLASS_PREHISTORIC:
361 case PCI_CLASS_DISPLAY:
362 if (csr & (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
363 p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
364 M_TEMP, M_WAITOK);
365 memset(p, '\0', sizeof(struct pci_memreg));
366 p->dev = dev;
367 p->csr = csr;
368 p->tag = tag;
369 p->reg = 0; /* there is no register about this */
370 p->size = 0x20000; /* 128kByte */
371 p->mask = 0xfffe0000;
372 p->address = 0xa0000;
373
374 insert_into_list(&memlist, p);
375 }
376 else continue;
377 }
378
379 for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; reg += 4) {
380
381 address = pci_conf_read(pc, tag, reg);
382 pci_conf_write(pc, tag, reg, 0xffffffff);
383 mask = pci_conf_read(pc, tag, reg);
384 pci_conf_write(pc, tag, reg, address);
385 if (mask == 0)
386 continue; /* Register unused */
387
388 p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
389 M_TEMP, M_WAITOK);
390 memset(p, '\0', sizeof(struct pci_memreg));
391 p->dev = dev;
392 p->csr = csr;
393 p->tag = tag;
394 p->reg = reg;
395 p->mask = mask;
396 p->address = 0;
397
398 if (mask & PCI_MAPREG_TYPE_IO) {
399 p->size = PCI_MAPREG_IO_SIZE(mask);
400
401 /*
402 * Align IO if necessary
403 */
404 if (p->size < PCI_MAPREG_IO_SIZE(PCI_MACHDEP_IO_ALIGN_MASK)) {
405 p->mask = PCI_MACHDEP_IO_ALIGN_MASK;
406 p->size = PCI_MAPREG_IO_SIZE(p->mask);
407 }
408
409 /*
410 * if I/O is already enabled (probably by the console driver)
411 * save the address in order to take care about it later.
412 */
413 if (csr & PCI_COMMAND_IO_ENABLE)
414 p->address = address;
415
416 insert_into_list(&iolist, p);
417 } else {
418 p->size = PCI_MAPREG_MEM_SIZE(mask);
419
420 /*
421 * Align memory if necessary
422 */
423 if (p->size < PCI_MAPREG_IO_SIZE(PCI_MACHDEP_MEM_ALIGN_MASK)) {
424 p->mask = PCI_MACHDEP_MEM_ALIGN_MASK;
425 p->size = PCI_MAPREG_MEM_SIZE(p->mask);
426 }
427
428 /*
429 * if memory is already enabled (probably by the console driver)
430 * save the address in order to take care about it later.
431 */
432 if (csr & PCI_COMMAND_MEM_ENABLE)
433 p->address = address;
434
435 insert_into_list(&memlist, p);
436
437 if (PCI_MAPREG_MEM_TYPE(mask) == PCI_MAPREG_MEM_TYPE_64BIT)
438 reg++;
439 }
440 }
441
442 /*
443 * Both interrupt pin & line are set to the device (== slot)
444 * number. This makes sense on the atari because the
445 * individual slots are hard-wired to a specific MFP-pin.
446 */
447 csr = (DEV2SLOT(dev) << PCI_INTERRUPT_PIN_SHIFT);
448 csr |= (DEV2SLOT(dev) << PCI_INTERRUPT_LINE_SHIFT);
449 pci_conf_write(pc, tag, PCI_INTERRUPT_REG, csr);
450 }
451
452 /*
453 * second step: calculate the memory and I/O adresses beginning from
454 * PCI_MEM_START and PCI_IO_START. Care about already mapped areas.
455 *
456 * begin with memory list
457 */
458
459 address = PCI_MEM_START;
460 sizecnt = 0;
461 membase_1m = 0;
462 p = LIST_FIRST(&memlist);
463 while (p != NULL) {
464 if (!(p->csr & PCI_COMMAND_MEM_ENABLE)) {
465 if (PCI_MAPREG_MEM_TYPE(p->mask) == PCI_MAPREG_MEM_TYPE_32BIT_1M) {
466 if (p->size > membase_1m)
467 membase_1m = p->size;
468 do {
469 p->address = membase_1m;
470 membase_1m += p->size;
471 } while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
472 p->size, PCI_COMMAND_MEM_ENABLE));
473 if (membase_1m > 0x00100000) {
474 /*
475 * Should we panic here?
476 */
477 printf("\npcibus0: dev %d reg %d: memory not configured",
478 p->dev, p->reg);
479 p->reg = 0;
480 }
481 } else {
482
483 if (sizecnt && (p->size > sizecnt))
484 sizecnt = ((p->size + sizecnt) & p->mask) &
485 PCI_MAPREG_MEM_ADDR_MASK;
486 if (sizecnt > address) {
487 address = sizecnt;
488 sizecnt = 0;
489 }
490
491 do {
492 p->address = address + sizecnt;
493 sizecnt += p->size;
494 } while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
495 p->size, PCI_COMMAND_MEM_ENABLE));
496
497 if ((address + sizecnt) > PCI_MEM_END) {
498 /*
499 * Should we panic here?
500 */
501 printf("\npcibus0: dev %d reg %d: memory not configured",
502 p->dev, p->reg);
503 p->reg = 0;
504 }
505 }
506 if (p->reg > 0) {
507 pci_conf_write(pc, p->tag, p->reg, p->address);
508 csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
509 csr |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
510 pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
511 p->csr = csr;
512 }
513 }
514 p = LIST_NEXT(p, link);
515 }
516
517 /*
518 * now the I/O list
519 */
520
521 address = PCI_IO_START;
522 sizecnt = 0;
523 p = LIST_FIRST(&iolist);
524 while (p != NULL) {
525 if (!(p->csr & PCI_COMMAND_IO_ENABLE)) {
526
527 if (sizecnt && (p->size > sizecnt))
528 sizecnt = ((p->size + sizecnt) & p->mask) &
529 PCI_MAPREG_IO_ADDR_MASK;
530 if (sizecnt > address) {
531 address = sizecnt;
532 sizecnt = 0;
533 }
534
535 do {
536 p->address = address + sizecnt;
537 sizecnt += p->size;
538 } while (overlap_pci_areas(LIST_FIRST(&iolist), p, p->address,
539 p->size, PCI_COMMAND_IO_ENABLE));
540
541 if ((address + sizecnt) > PCI_IO_END) {
542 /*
543 * Should we panic here?
544 */
545 printf("\npcibus0: dev %d reg %d: io not configured",
546 p->dev, p->reg);
547 } else {
548 pci_conf_write(pc, p->tag, p->reg, p->address);
549 csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
550 csr |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
551 pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
552 p->csr = csr;
553 }
554 }
555 p = LIST_NEXT(p, link);
556 }
557
558 #ifdef DEBUG_PCI_MACHDEP
559 printf("\nI/O List:\n");
560 p = LIST_FIRST(&iolist);
561
562 while (p != NULL) {
563 printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
564 p->reg, p->size, p->address);
565 p = LIST_NEXT(p, link);
566 }
567 printf("\nMemlist:");
568 p = LIST_FIRST(&memlist);
569
570 while (p != NULL) {
571 printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
572 p->reg, p->size, p->address);
573 p = LIST_NEXT(p, link);
574 }
575 #endif
576
577 /*
578 * Free the lists
579 */
580 p = LIST_FIRST(&iolist);
581 while (p != NULL) {
582 q = p;
583 LIST_REMOVE(q, link);
584 free(p, M_WAITOK);
585 p = LIST_FIRST(&iolist);
586 }
587 p = LIST_FIRST(&memlist);
588 while (p != NULL) {
589 q = p;
590 LIST_REMOVE(q, link);
591 free(p, M_WAITOK);
592 p = LIST_FIRST(&memlist);
593 }
594 }
595
596 /*
597 * Atari_init.c maps the config areas NBPG bytes apart....
598 */
599 static int pci_config_offset(tag)
600 pcitag_t tag;
601 {
602 int device;
603
604 device = (tag >> 11) & 0x1f;
605 return(device * NBPG);
606 }
607
608 int
609 pci_bus_maxdevs(pc, busno)
610 pci_chipset_tag_t pc;
611 int busno;
612 {
613 return (4);
614 }
615
616 pcitag_t
617 pci_make_tag(pc, bus, device, function)
618 pci_chipset_tag_t pc;
619 int bus, device, function;
620 {
621 return ((bus << 16) | (device << 11) | (function << 8));
622 }
623
624 pcireg_t
625 pci_conf_read(pc, tag, reg)
626 pci_chipset_tag_t pc;
627 pcitag_t tag;
628 int reg;
629 {
630 u_long data;
631
632 data = *(u_long *)(pci_conf_addr + pci_config_offset(tag) + reg);
633 return (bswap32(data));
634 }
635
636 void
637 pci_conf_write(pc, tag, reg, data)
638 pci_chipset_tag_t pc;
639 pcitag_t tag;
640 int reg;
641 pcireg_t data;
642 {
643 *((u_long *)(pci_conf_addr + pci_config_offset(tag) + reg))
644 = bswap32(data);
645 }
646
647 int
648 pci_intr_map(pa, ihp)
649 struct pci_attach_args *pa;
650 pci_intr_handle_t *ihp;
651 {
652 int line = pa->pa_intrline;
653
654 /*
655 * According to the PCI-spec, 255 means `unknown' or `no connection'.
656 * Interpret this as 'no interrupt assigned'.
657 */
658 if (line == 255) {
659 *ihp = -1;
660 return 1;
661 }
662
663 /*
664 * Values are pretty useless because the on the Hades all interrupt
665 * lines for a card are tied together and hardwired to the TT-MFP
666 * I/O port.
667 */
668 *ihp = line;
669 return 0;
670 }
671
672 const char *
673 pci_intr_string(pc, ih)
674 pci_chipset_tag_t pc;
675 pci_intr_handle_t ih;
676 {
677 static char irqstr[8]; /* 4 + 2 + NULL + sanity */
678
679 if (ih == -1)
680 panic("pci_intr_string: bogus handle 0x%x\n", ih);
681
682 sprintf(irqstr, "irq %d", ih);
683 return (irqstr);
684
685 }
686
687 const struct evcnt *
688 pci_intr_evcnt(pc, ih)
689 pci_chipset_tag_t pc;
690 pci_intr_handle_t ih;
691 {
692
693 /* XXX for now, no evcnt parent reported */
694 return NULL;
695 }
696
697 /*
698 * The interrupt stuff is rather ugly. On the Hades, all interrupt lines
699 * for a slot are wired together and connected to IO 0,1,2 or 5 (slots:
700 * (0-3) on the TT-MFP. The Pci-config code initializes the irq. number
701 * to the slot position.
702 */
703 static pci_intr_info_t iinfo[4] = { { -1 }, { -1 }, { -1 }, { -1 } };
704
705 static int iifun __P((int, int));
706
707 static int
708 iifun(slot, sr)
709 int slot;
710 int sr;
711 {
712 pci_intr_info_t *iinfo_p;
713 int s;
714
715 iinfo_p = &iinfo[slot];
716
717 /*
718 * Disable the interrupts
719 */
720 MFP2->mf_imrb &= ~iinfo_p->imask;
721
722 if ((sr & PSL_IPL) >= (iinfo_p->ipl & PSL_IPL)) {
723 /*
724 * We're running at a too high priority now.
725 */
726 add_sicallback((si_farg)iifun, (void*)slot, 0);
727 }
728 else {
729 s = splx(iinfo_p->ipl);
730 (void) (iinfo_p->ifunc)(iinfo_p->iarg);
731 splx(s);
732
733 /*
734 * Re-enable interrupts after handling
735 */
736 MFP2->mf_imrb |= iinfo_p->imask;
737 }
738 return 1;
739 }
740
741 void *
742 pci_intr_establish(pc, ih, level, ih_fun, ih_arg)
743 pci_chipset_tag_t pc;
744 pci_intr_handle_t ih;
745 int level;
746 int (*ih_fun) __P((void *));
747 void *ih_arg;
748 {
749 pci_intr_info_t *iinfo_p;
750 struct intrhand *ihand;
751 int slot;
752
753 slot = ih;
754 iinfo_p = &iinfo[slot];
755
756 if (iinfo_p->ipl > 0)
757 panic("pci_intr_establish: interrupt was already established\n");
758
759 ihand = intr_establish((slot == 3) ? 23 : 16 + slot, USER_VEC, 0,
760 (hw_ifun_t)iifun, (void *)slot);
761 if (ihand != NULL) {
762 iinfo_p->ipl = level;
763 iinfo_p->imask = (slot == 3) ? 0x80 : (0x01 << slot);
764 iinfo_p->ifunc = ih_fun;
765 iinfo_p->iarg = ih_arg;
766 iinfo_p->ihand = ihand;
767
768 /*
769 * Enable (unmask) the interrupt
770 */
771 MFP2->mf_imrb |= iinfo_p->imask;
772 MFP2->mf_ierb |= iinfo_p->imask;
773 return(iinfo_p);
774 }
775 return NULL;
776 }
777
778 void
779 pci_intr_disestablish(pc, cookie)
780 pci_chipset_tag_t pc;
781 void *cookie;
782 {
783 pci_intr_info_t *iinfo_p = (pci_intr_info_t *)cookie;
784
785 if (iinfo->ipl < 0)
786 panic("pci_intr_disestablish: interrupt was not established\n");
787
788 MFP2->mf_imrb &= ~iinfo->imask;
789 MFP2->mf_ierb &= ~iinfo->imask;
790 (void) intr_disestablish(iinfo_p->ihand);
791 iinfo_p->ipl = -1;
792 }
793