FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/nexus.c
1 /*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: releng/6.3/sys/i386/i386/nexus.c 173886 2007-11-24 19:45:58Z cvs2svn $");
32
33 /*
34 * This code implements a `root nexus' for Intel Architecture
35 * machines. The function of the root nexus is to serve as an
36 * attachment point for both processors and buses, and to manage
37 * resources which are common to all of them. In particular,
38 * this code implements the core resource managers for interrupt
39 * requests, DMA requests (which rightfully should be a part of the
40 * ISA code but it's easier to do it here for now), I/O port addresses,
41 * and I/O memory address space.
42 */
43
44 #include "opt_apic.h"
45 #include "opt_isa.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/bus.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <machine/bus.h>
54 #include <machine/intr_machdep.h>
55 #include <sys/rman.h>
56 #include <sys/interrupt.h>
57
58 #include <machine/vmparam.h>
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <machine/pmap.h>
62
63 #include <machine/resource.h>
64
65 #ifdef DEV_APIC
66 #include "pcib_if.h"
67 #endif
68
69 #ifdef DEV_ISA
70 #include <isa/isavar.h>
71 #ifdef PC98
72 #include <pc98/cbus/cbus.h>
73 #else
74 #include <i386/isa/isa.h>
75 #endif
76 #endif
77 #include <sys/rtprio.h>
78
79 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
80 struct nexus_device {
81 struct resource_list nx_resources;
82 };
83
84 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
85
86 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
87
88 static int nexus_probe(device_t);
89 static int nexus_attach(device_t);
90 static int nexus_print_all_resources(device_t dev);
91 static int nexus_print_child(device_t, device_t);
92 static device_t nexus_add_child(device_t bus, int order, const char *name,
93 int unit);
94 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
95 u_long, u_long, u_long, u_int);
96 static int nexus_config_intr(device_t, int, enum intr_trigger,
97 enum intr_polarity);
98 static int nexus_activate_resource(device_t, device_t, int, int,
99 struct resource *);
100 static int nexus_deactivate_resource(device_t, device_t, int, int,
101 struct resource *);
102 static int nexus_release_resource(device_t, device_t, int, int,
103 struct resource *);
104 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
105 void (*)(void *), void *, void **);
106 static int nexus_teardown_intr(device_t, device_t, struct resource *,
107 void *);
108 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
109 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
110 static int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
111 static void nexus_delete_resource(device_t, device_t, int, int);
112 #ifdef DEV_APIC
113 static int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs);
114 static int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs);
115 static int nexus_alloc_msix(device_t pcib, device_t dev, int *irq);
116 static int nexus_release_msix(device_t pcib, device_t dev, int irq);
117 static int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data);
118 #endif
119
120 static device_method_t nexus_methods[] = {
121 /* Device interface */
122 DEVMETHOD(device_probe, nexus_probe),
123 DEVMETHOD(device_attach, nexus_attach),
124 DEVMETHOD(device_detach, bus_generic_detach),
125 DEVMETHOD(device_shutdown, bus_generic_shutdown),
126 DEVMETHOD(device_suspend, bus_generic_suspend),
127 DEVMETHOD(device_resume, bus_generic_resume),
128
129 /* Bus interface */
130 DEVMETHOD(bus_print_child, nexus_print_child),
131 DEVMETHOD(bus_add_child, nexus_add_child),
132 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
133 DEVMETHOD(bus_release_resource, nexus_release_resource),
134 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
135 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
136 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
137 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
138 DEVMETHOD(bus_config_intr, nexus_config_intr),
139 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
140 DEVMETHOD(bus_set_resource, nexus_set_resource),
141 DEVMETHOD(bus_get_resource, nexus_get_resource),
142 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
143
144 /* pcib interface */
145 #ifdef DEV_APIC
146 DEVMETHOD(pcib_alloc_msi, nexus_alloc_msi),
147 DEVMETHOD(pcib_release_msi, nexus_release_msi),
148 DEVMETHOD(pcib_alloc_msix, nexus_alloc_msix),
149 DEVMETHOD(pcib_release_msix, nexus_release_msix),
150 DEVMETHOD(pcib_map_msi, nexus_map_msi),
151 #endif
152
153 { 0, 0 }
154 };
155
156 static driver_t nexus_driver = {
157 "nexus",
158 nexus_methods,
159 1, /* no softc */
160 };
161 static devclass_t nexus_devclass;
162
163 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
164
165 static int
166 nexus_probe(device_t dev)
167 {
168 int irq;
169
170 device_quiet(dev); /* suppress attach message for neatness */
171
172 /*
173 * XXX working notes:
174 *
175 * - IRQ resource creation should be moved to the PIC/APIC driver.
176 * - DRQ resource creation should be moved to the DMAC driver.
177 * - The above should be sorted to probe earlier than any child busses.
178 *
179 * - Leave I/O and memory creation here, as child probes may need them.
180 * (especially eg. ACPI)
181 */
182
183 /*
184 * IRQ's are on the mainboard on old systems, but on the ISA part
185 * of PCI->ISA bridges. There would be multiple sets of IRQs on
186 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
187 * component, so in a way, PCI can be a partial child of an ISA bus(!).
188 * APIC interrupts are global though.
189 */
190 irq_rman.rm_start = 0;
191 irq_rman.rm_type = RMAN_ARRAY;
192 irq_rman.rm_descr = "Interrupt request lines";
193 irq_rman.rm_end = NUM_IO_INTS - 1;
194 if (rman_init(&irq_rman))
195 panic("nexus_probe irq_rman");
196
197 /*
198 * We search for regions of existing IRQs and add those to the IRQ
199 * resource manager.
200 */
201 for (irq = 0; irq < NUM_IO_INTS; irq++)
202 if (intr_lookup_source(irq) != NULL)
203 if (rman_manage_region(&irq_rman, irq, irq) != 0)
204 panic("nexus_probe irq_rman add");
205
206 /*
207 * ISA DMA on PCI systems is implemented in the ISA part of each
208 * PCI->ISA bridge and the channels can be duplicated if there are
209 * multiple bridges. (eg: laptops with docking stations)
210 */
211 drq_rman.rm_start = 0;
212 #ifdef PC98
213 drq_rman.rm_end = 3;
214 #else
215 drq_rman.rm_end = 7;
216 #endif
217 drq_rman.rm_type = RMAN_ARRAY;
218 drq_rman.rm_descr = "DMA request lines";
219 /* XXX drq 0 not available on some machines */
220 if (rman_init(&drq_rman)
221 || rman_manage_region(&drq_rman,
222 drq_rman.rm_start, drq_rman.rm_end))
223 panic("nexus_probe drq_rman");
224
225 /*
226 * However, IO ports and Memory truely are global at this level,
227 * as are APIC interrupts (however many IO APICS there turn out
228 * to be on large systems..)
229 */
230 port_rman.rm_start = 0;
231 port_rman.rm_end = 0xffff;
232 port_rman.rm_type = RMAN_ARRAY;
233 port_rman.rm_descr = "I/O ports";
234 if (rman_init(&port_rman)
235 || rman_manage_region(&port_rman, 0, 0xffff))
236 panic("nexus_probe port_rman");
237
238 mem_rman.rm_start = 0;
239 mem_rman.rm_end = ~0u;
240 mem_rman.rm_type = RMAN_ARRAY;
241 mem_rman.rm_descr = "I/O memory addresses";
242 if (rman_init(&mem_rman)
243 || rman_manage_region(&mem_rman, 0, ~0))
244 panic("nexus_probe mem_rman");
245
246 return 0;
247 }
248
249 static int
250 nexus_attach(device_t dev)
251 {
252
253 bus_generic_probe(dev);
254 bus_generic_attach(dev);
255 return 0;
256 }
257
258 static int
259 nexus_print_all_resources(device_t dev)
260 {
261 struct nexus_device *ndev = DEVTONX(dev);
262 struct resource_list *rl = &ndev->nx_resources;
263 int retval = 0;
264
265 if (STAILQ_FIRST(rl))
266 retval += printf(" at");
267
268 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
269 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
270 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
271
272 return retval;
273 }
274
275 static int
276 nexus_print_child(device_t bus, device_t child)
277 {
278 int retval = 0;
279
280 retval += bus_print_child_header(bus, child);
281 retval += nexus_print_all_resources(child);
282 if (device_get_flags(child))
283 retval += printf(" flags %#x", device_get_flags(child));
284 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
285
286 return (retval);
287 }
288
289 static device_t
290 nexus_add_child(device_t bus, int order, const char *name, int unit)
291 {
292 device_t child;
293 struct nexus_device *ndev;
294
295 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
296 if (!ndev)
297 return(0);
298 resource_list_init(&ndev->nx_resources);
299
300 child = device_add_child_ordered(bus, order, name, unit);
301
302 /* should we free this in nexus_child_detached? */
303 device_set_ivars(child, ndev);
304
305 return(child);
306 }
307
308 /*
309 * Allocate a resource on behalf of child. NB: child is usually going to be a
310 * child of one of our descendants, not a direct child of nexus0.
311 * (Exceptions include npx.)
312 */
313 static struct resource *
314 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
315 u_long start, u_long end, u_long count, u_int flags)
316 {
317 struct nexus_device *ndev = DEVTONX(child);
318 struct resource *rv;
319 struct resource_list_entry *rle;
320 struct rman *rm;
321 int needactivate = flags & RF_ACTIVE;
322 #ifdef PC98
323 bus_space_handle_t bh;
324 #endif
325
326 /*
327 * If this is an allocation of the "default" range for a given RID, and
328 * we know what the resources for this device are (ie. they aren't maintained
329 * by a child bus), then work out the start/end values.
330 */
331 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
332 if (ndev == NULL)
333 return(NULL);
334 rle = resource_list_find(&ndev->nx_resources, type, *rid);
335 if (rle == NULL)
336 return(NULL);
337 start = rle->start;
338 end = rle->end;
339 count = rle->count;
340 }
341
342 flags &= ~RF_ACTIVE;
343
344 switch (type) {
345 case SYS_RES_IRQ:
346 rm = &irq_rman;
347 break;
348
349 case SYS_RES_DRQ:
350 rm = &drq_rman;
351 break;
352
353 case SYS_RES_IOPORT:
354 rm = &port_rman;
355 break;
356
357 case SYS_RES_MEMORY:
358 rm = &mem_rman;
359 break;
360
361 default:
362 return 0;
363 }
364
365 rv = rman_reserve_resource(rm, start, end, count, flags, child);
366 if (rv == 0)
367 return 0;
368 rman_set_rid(rv, *rid);
369 if (type == SYS_RES_MEMORY) {
370 rman_set_bustag(rv, I386_BUS_SPACE_MEM);
371 } else if (type == SYS_RES_IOPORT) {
372 rman_set_bustag(rv, I386_BUS_SPACE_IO);
373 #ifndef PC98
374 rman_set_bushandle(rv, rman_get_start(rv));
375 #endif
376 }
377
378 #ifdef PC98
379 if ((type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) &&
380 i386_bus_space_handle_alloc(rman_get_bustag(rv),
381 rman_get_start(rv), count, &bh) != 0) {
382 rman_release_resource(rv);
383 return 0;
384 }
385 rman_set_bushandle(rv, bh);
386 #endif
387
388 if (needactivate) {
389 if (bus_activate_resource(child, type, *rid, rv)) {
390 #ifdef PC98
391 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
392 bh = rman_get_bushandle(rv);
393 i386_bus_space_handle_free(rman_get_bustag(rv),
394 bh, bh->bsh_sz);
395 }
396 #endif
397 rman_release_resource(rv);
398 return 0;
399 }
400 }
401
402 return rv;
403 }
404
405 static int
406 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
407 struct resource *r)
408 {
409 #ifdef PC98
410 bus_space_handle_t bh;
411 #endif
412 /*
413 * If this is a memory resource, map it into the kernel.
414 */
415 if (rman_get_bustag(r) == I386_BUS_SPACE_MEM) {
416 caddr_t vaddr = 0;
417
418 if (rman_get_end(r) < 1024 * 1024) {
419 /*
420 * The first 1Mb is mapped at KERNBASE.
421 */
422 vaddr = (caddr_t)(uintptr_t)(KERNBASE + rman_get_start(r));
423 } else {
424 u_int32_t paddr;
425 u_int32_t psize;
426 u_int32_t poffs;
427
428 paddr = rman_get_start(r);
429 psize = rman_get_size(r);
430
431 poffs = paddr - trunc_page(paddr);
432 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
433 }
434 rman_set_virtual(r, vaddr);
435 #ifdef PC98
436 /* PC-98: the type of bus_space_handle_t is the structure. */
437 bh = rman_get_bushandle(r);
438 bh->bsh_base = (bus_addr_t) vaddr;
439 #else
440 /* IBM-PC: the type of bus_space_handle_t is u_int */
441 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
442 #endif
443 }
444 return (rman_activate_resource(r));
445 }
446
447 static int
448 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
449 struct resource *r)
450 {
451 /*
452 * If this is a memory resource, unmap it.
453 */
454 if ((rman_get_bustag(r) == I386_BUS_SPACE_MEM) &&
455 (rman_get_end(r) >= 1024 * 1024)) {
456 u_int32_t psize;
457
458 psize = rman_get_size(r);
459 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
460 }
461
462 return (rman_deactivate_resource(r));
463 }
464
465 static int
466 nexus_release_resource(device_t bus, device_t child, int type, int rid,
467 struct resource *r)
468 {
469 if (rman_get_flags(r) & RF_ACTIVE) {
470 int error = bus_deactivate_resource(child, type, rid, r);
471 if (error)
472 return error;
473 }
474 #ifdef PC98
475 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
476 bus_space_handle_t bh;
477
478 bh = rman_get_bushandle(r);
479 i386_bus_space_handle_free(rman_get_bustag(r), bh, bh->bsh_sz);
480 }
481 #endif
482 return (rman_release_resource(r));
483 }
484
485 /*
486 * Currently this uses the really grody interface from kern/kern_intr.c
487 * (which really doesn't belong in kern/anything.c). Eventually, all of
488 * the code in kern_intr.c and machdep_intr.c should get moved here, since
489 * this is going to be the official interface.
490 */
491 static int
492 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
493 int flags, void (*ihand)(void *), void *arg, void **cookiep)
494 {
495 int error;
496
497 /* somebody tried to setup an irq that failed to allocate! */
498 if (irq == NULL)
499 panic("nexus_setup_intr: NULL irq resource!");
500
501 *cookiep = 0;
502 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
503 flags |= INTR_EXCL;
504
505 /*
506 * We depend here on rman_activate_resource() being idempotent.
507 */
508 error = rman_activate_resource(irq);
509 if (error)
510 return (error);
511
512 error = intr_add_handler(device_get_nameunit(child),
513 rman_get_start(irq), ihand, arg, flags, cookiep);
514
515 return (error);
516 }
517
518 static int
519 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
520 {
521 return (intr_remove_handler(ih));
522 }
523
524 static int
525 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
526 enum intr_polarity pol)
527 {
528 return (intr_config_intr(irq, trig, pol));
529 }
530
531 static struct resource_list *
532 nexus_get_reslist(device_t dev, device_t child)
533 {
534 struct nexus_device *ndev = DEVTONX(child);
535
536 return (&ndev->nx_resources);
537 }
538
539 static int
540 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
541 {
542 struct nexus_device *ndev = DEVTONX(child);
543 struct resource_list *rl = &ndev->nx_resources;
544
545 /* XXX this should return a success/failure indicator */
546 resource_list_add(rl, type, rid, start, start + count - 1, count);
547 return(0);
548 }
549
550 static int
551 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
552 {
553 struct nexus_device *ndev = DEVTONX(child);
554 struct resource_list *rl = &ndev->nx_resources;
555 struct resource_list_entry *rle;
556
557 rle = resource_list_find(rl, type, rid);
558 if (!rle)
559 return(ENOENT);
560 if (startp)
561 *startp = rle->start;
562 if (countp)
563 *countp = rle->count;
564 return(0);
565 }
566
567 static void
568 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
569 {
570 struct nexus_device *ndev = DEVTONX(child);
571 struct resource_list *rl = &ndev->nx_resources;
572
573 resource_list_delete(rl, type, rid);
574 }
575
576 /* Called from the MSI code to add new IRQs to the IRQ rman. */
577 void
578 nexus_add_irq(u_long irq)
579 {
580
581 if (rman_manage_region(&irq_rman, irq, irq) != 0)
582 panic("%s: failed", __func__);
583 }
584
585 #ifdef DEV_APIC
586 static int
587 nexus_alloc_msix(device_t pcib, device_t dev, int *irq)
588 {
589
590 return (msix_alloc(dev, irq));
591 }
592
593 static int
594 nexus_release_msix(device_t pcib, device_t dev, int irq)
595 {
596
597 return (msix_release(irq));
598 }
599
600 static int
601 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
602 {
603
604 return (msi_alloc(dev, count, maxcount, irqs));
605 }
606
607 static int
608 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs)
609 {
610
611 return (msi_release(irqs, count));
612 }
613
614 static int
615 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
616 {
617
618 return (msi_map(irq, addr, data));
619 }
620 #endif
621
622 #ifdef DEV_ISA
623 /*
624 * Placeholder which claims PnP 'devices' which describe system
625 * resources.
626 */
627 static struct isa_pnp_id sysresource_ids[] = {
628 { 0x010cd041 /* PNP0c01 */, "System Memory" },
629 { 0x020cd041 /* PNP0c02 */, "System Resource" },
630 { 0 }
631 };
632
633 static int
634 sysresource_probe(device_t dev)
635 {
636 int result;
637
638 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
639 device_quiet(dev);
640 }
641 return(result);
642 }
643
644 static int
645 sysresource_attach(device_t dev)
646 {
647 return(0);
648 }
649
650 static device_method_t sysresource_methods[] = {
651 /* Device interface */
652 DEVMETHOD(device_probe, sysresource_probe),
653 DEVMETHOD(device_attach, sysresource_attach),
654 DEVMETHOD(device_detach, bus_generic_detach),
655 DEVMETHOD(device_shutdown, bus_generic_shutdown),
656 DEVMETHOD(device_suspend, bus_generic_suspend),
657 DEVMETHOD(device_resume, bus_generic_resume),
658 { 0, 0 }
659 };
660
661 static driver_t sysresource_driver = {
662 "sysresource",
663 sysresource_methods,
664 1, /* no softc */
665 };
666
667 static devclass_t sysresource_devclass;
668
669 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);
670 #endif /* DEV_ISA */
Cache object: 03fb1da33096373b8af98ea5b97efe24
|