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/5.3/sys/i386/i386/nexus.c 134567 2004-08-31 05:26:37Z njl $");
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_isa.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bus.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <machine/bus.h>
53 #include <machine/intr_machdep.h>
54 #include <sys/rman.h>
55 #include <sys/interrupt.h>
56
57 #include <machine/vmparam.h>
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <machine/pmap.h>
61
62 #include <machine/resource.h>
63
64 #ifdef DEV_ISA
65 #include <isa/isavar.h>
66 #ifdef PC98
67 #include <pc98/pc98/pc98.h>
68 #else
69 #include <i386/isa/isa.h>
70 #endif
71 #endif
72 #include <sys/rtprio.h>
73
74 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
75 struct nexus_device {
76 struct resource_list nx_resources;
77 };
78
79 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
80
81 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
82
83 static int nexus_probe(device_t);
84 static int nexus_attach(device_t);
85 static int nexus_print_all_resources(device_t dev);
86 static int nexus_print_child(device_t, device_t);
87 static device_t nexus_add_child(device_t bus, int order, const char *name,
88 int unit);
89 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
90 u_long, u_long, u_long, u_int);
91 static int nexus_config_intr(device_t, int, enum intr_trigger,
92 enum intr_polarity);
93 static int nexus_activate_resource(device_t, device_t, int, int,
94 struct resource *);
95 static int nexus_deactivate_resource(device_t, device_t, int, int,
96 struct resource *);
97 static int nexus_release_resource(device_t, device_t, int, int,
98 struct resource *);
99 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
100 void (*)(void *), void *, void **);
101 static int nexus_teardown_intr(device_t, device_t, struct resource *,
102 void *);
103 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
104 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
105 static int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
106 static void nexus_delete_resource(device_t, device_t, int, int);
107
108 static device_method_t nexus_methods[] = {
109 /* Device interface */
110 DEVMETHOD(device_probe, nexus_probe),
111 DEVMETHOD(device_attach, nexus_attach),
112 DEVMETHOD(device_detach, bus_generic_detach),
113 DEVMETHOD(device_shutdown, bus_generic_shutdown),
114 DEVMETHOD(device_suspend, bus_generic_suspend),
115 DEVMETHOD(device_resume, bus_generic_resume),
116
117 /* Bus interface */
118 DEVMETHOD(bus_print_child, nexus_print_child),
119 DEVMETHOD(bus_add_child, nexus_add_child),
120 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
121 DEVMETHOD(bus_release_resource, nexus_release_resource),
122 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
123 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
124 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
125 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
126 DEVMETHOD(bus_config_intr, nexus_config_intr),
127 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
128 DEVMETHOD(bus_set_resource, nexus_set_resource),
129 DEVMETHOD(bus_get_resource, nexus_get_resource),
130 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
131
132 { 0, 0 }
133 };
134
135 static driver_t nexus_driver = {
136 "nexus",
137 nexus_methods,
138 1, /* no softc */
139 };
140 static devclass_t nexus_devclass;
141
142 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
143
144 static int
145 nexus_probe(device_t dev)
146 {
147 int irq, last;
148
149 device_quiet(dev); /* suppress attach message for neatness */
150
151 /*
152 * XXX working notes:
153 *
154 * - IRQ resource creation should be moved to the PIC/APIC driver.
155 * - DRQ resource creation should be moved to the DMAC driver.
156 * - The above should be sorted to probe earlier than any child busses.
157 *
158 * - Leave I/O and memory creation here, as child probes may need them.
159 * (especially eg. ACPI)
160 */
161
162 /*
163 * IRQ's are on the mainboard on old systems, but on the ISA part
164 * of PCI->ISA bridges. There would be multiple sets of IRQs on
165 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
166 * component, so in a way, PCI can be a partial child of an ISA bus(!).
167 * APIC interrupts are global though.
168 */
169 irq_rman.rm_start = 0;
170 irq_rman.rm_type = RMAN_ARRAY;
171 irq_rman.rm_descr = "Interrupt request lines";
172 irq_rman.rm_end = NUM_IO_INTS - 1;
173 if (rman_init(&irq_rman))
174 panic("nexus_probe irq_rman");
175
176 /*
177 * We search for regions of existing IRQs and add those to the IRQ
178 * resource manager.
179 */
180 last = -1;
181 for (irq = 0; irq < NUM_IO_INTS; irq++)
182 if (intr_lookup_source(irq) != NULL) {
183 if (last == -1)
184 last = irq;
185 } else if (last != -1) {
186 if (rman_manage_region(&irq_rman, last, irq - 1) != 0)
187 panic("nexus_probe irq_rman add");
188 last = -1;
189 }
190 if (last != -1 && rman_manage_region(&irq_rman, last, irq - 1) != 0)
191 panic("nexus_probe irq_rman add");
192
193 /*
194 * ISA DMA on PCI systems is implemented in the ISA part of each
195 * PCI->ISA bridge and the channels can be duplicated if there are
196 * multiple bridges. (eg: laptops with docking stations)
197 */
198 drq_rman.rm_start = 0;
199 #ifdef PC98
200 drq_rman.rm_end = 3;
201 #else
202 drq_rman.rm_end = 7;
203 #endif
204 drq_rman.rm_type = RMAN_ARRAY;
205 drq_rman.rm_descr = "DMA request lines";
206 /* XXX drq 0 not available on some machines */
207 if (rman_init(&drq_rman)
208 || rman_manage_region(&drq_rman,
209 drq_rman.rm_start, drq_rman.rm_end))
210 panic("nexus_probe drq_rman");
211
212 /*
213 * However, IO ports and Memory truely are global at this level,
214 * as are APIC interrupts (however many IO APICS there turn out
215 * to be on large systems..)
216 */
217 port_rman.rm_start = 0;
218 port_rman.rm_end = 0xffff;
219 port_rman.rm_type = RMAN_ARRAY;
220 port_rman.rm_descr = "I/O ports";
221 if (rman_init(&port_rman)
222 || rman_manage_region(&port_rman, 0, 0xffff))
223 panic("nexus_probe port_rman");
224
225 mem_rman.rm_start = 0;
226 mem_rman.rm_end = ~0u;
227 mem_rman.rm_type = RMAN_ARRAY;
228 mem_rman.rm_descr = "I/O memory addresses";
229 if (rman_init(&mem_rman)
230 || rman_manage_region(&mem_rman, 0, ~0))
231 panic("nexus_probe mem_rman");
232
233 return 0;
234 }
235
236 static int
237 nexus_attach(device_t dev)
238 {
239
240 bus_generic_probe(dev);
241 bus_generic_attach(dev);
242 return 0;
243 }
244
245 static int
246 nexus_print_all_resources(device_t dev)
247 {
248 struct nexus_device *ndev = DEVTONX(dev);
249 struct resource_list *rl = &ndev->nx_resources;
250 int retval = 0;
251
252 if (SLIST_FIRST(rl))
253 retval += printf(" at");
254
255 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
256 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
257 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
258
259 return retval;
260 }
261
262 static int
263 nexus_print_child(device_t bus, device_t child)
264 {
265 int retval = 0;
266
267 retval += bus_print_child_header(bus, child);
268 retval += nexus_print_all_resources(child);
269 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
270
271 return (retval);
272 }
273
274 static device_t
275 nexus_add_child(device_t bus, int order, const char *name, int unit)
276 {
277 device_t child;
278 struct nexus_device *ndev;
279
280 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
281 if (!ndev)
282 return(0);
283 resource_list_init(&ndev->nx_resources);
284
285 child = device_add_child_ordered(bus, order, name, unit);
286
287 /* should we free this in nexus_child_detached? */
288 device_set_ivars(child, ndev);
289
290 return(child);
291 }
292
293 /*
294 * Allocate a resource on behalf of child. NB: child is usually going to be a
295 * child of one of our descendants, not a direct child of nexus0.
296 * (Exceptions include npx.)
297 */
298 static struct resource *
299 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
300 u_long start, u_long end, u_long count, u_int flags)
301 {
302 struct nexus_device *ndev = DEVTONX(child);
303 struct resource *rv;
304 struct resource_list_entry *rle;
305 struct rman *rm;
306 int needactivate = flags & RF_ACTIVE;
307 #ifdef PC98
308 bus_space_handle_t bh;
309 #endif
310
311 /*
312 * If this is an allocation of the "default" range for a given RID, and
313 * we know what the resources for this device are (ie. they aren't maintained
314 * by a child bus), then work out the start/end values.
315 */
316 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
317 if (ndev == NULL)
318 return(NULL);
319 rle = resource_list_find(&ndev->nx_resources, type, *rid);
320 if (rle == NULL)
321 return(NULL);
322 start = rle->start;
323 end = rle->end;
324 count = rle->count;
325 }
326
327 flags &= ~RF_ACTIVE;
328
329 switch (type) {
330 case SYS_RES_IRQ:
331 rm = &irq_rman;
332 break;
333
334 case SYS_RES_DRQ:
335 rm = &drq_rman;
336 break;
337
338 case SYS_RES_IOPORT:
339 rm = &port_rman;
340 break;
341
342 case SYS_RES_MEMORY:
343 rm = &mem_rman;
344 break;
345
346 default:
347 return 0;
348 }
349
350 rv = rman_reserve_resource(rm, start, end, count, flags, child);
351 if (rv == 0)
352 return 0;
353
354 if (type == SYS_RES_MEMORY) {
355 rman_set_bustag(rv, I386_BUS_SPACE_MEM);
356 } else if (type == SYS_RES_IOPORT) {
357 rman_set_bustag(rv, I386_BUS_SPACE_IO);
358 #ifndef PC98
359 rman_set_bushandle(rv, rman_get_start(rv));
360 #endif
361 }
362
363 #ifdef PC98
364 if ((type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) &&
365 i386_bus_space_handle_alloc(rman_get_bustag(rv),
366 rman_get_start(rv), count, &bh) != 0) {
367 rman_release_resource(rv);
368 return 0;
369 }
370 rman_set_bushandle(rv, bh);
371 #endif
372
373 if (needactivate) {
374 if (bus_activate_resource(child, type, *rid, rv)) {
375 #ifdef PC98
376 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
377 bh = rman_get_bushandle(rv);
378 i386_bus_space_handle_free(rman_get_bustag(rv),
379 bh, bh->bsh_sz);
380 }
381 #endif
382 rman_release_resource(rv);
383 return 0;
384 }
385 }
386
387 return rv;
388 }
389
390 static int
391 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
392 struct resource *r)
393 {
394 #ifdef PC98
395 bus_space_handle_t bh;
396 #endif
397 /*
398 * If this is a memory resource, map it into the kernel.
399 */
400 if (rman_get_bustag(r) == I386_BUS_SPACE_MEM) {
401 caddr_t vaddr = 0;
402
403 if (rman_get_end(r) < 1024 * 1024) {
404 /*
405 * The first 1Mb is mapped at KERNBASE.
406 */
407 vaddr = (caddr_t)(uintptr_t)(KERNBASE + rman_get_start(r));
408 } else {
409 u_int32_t paddr;
410 u_int32_t psize;
411 u_int32_t poffs;
412
413 paddr = rman_get_start(r);
414 psize = rman_get_size(r);
415
416 poffs = paddr - trunc_page(paddr);
417 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
418 }
419 rman_set_virtual(r, vaddr);
420 #ifdef PC98
421 /* PC-98: the type of bus_space_handle_t is the structure. */
422 bh = rman_get_bushandle(r);
423 bh->bsh_base = (bus_addr_t) vaddr;
424 #else
425 /* IBM-PC: the type of bus_space_handle_t is u_int */
426 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
427 #endif
428 }
429 return (rman_activate_resource(r));
430 }
431
432 static int
433 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
434 struct resource *r)
435 {
436 /*
437 * If this is a memory resource, unmap it.
438 */
439 if ((rman_get_bustag(r) == I386_BUS_SPACE_MEM) &&
440 (rman_get_end(r) >= 1024 * 1024)) {
441 u_int32_t psize;
442
443 psize = rman_get_size(r);
444 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
445 }
446
447 return (rman_deactivate_resource(r));
448 }
449
450 static int
451 nexus_release_resource(device_t bus, device_t child, int type, int rid,
452 struct resource *r)
453 {
454 if (rman_get_flags(r) & RF_ACTIVE) {
455 int error = bus_deactivate_resource(child, type, rid, r);
456 if (error)
457 return error;
458 }
459 #ifdef PC98
460 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
461 bus_space_handle_t bh;
462
463 bh = rman_get_bushandle(r);
464 i386_bus_space_handle_free(rman_get_bustag(r), bh, bh->bsh_sz);
465 }
466 #endif
467 return (rman_release_resource(r));
468 }
469
470 /*
471 * Currently this uses the really grody interface from kern/kern_intr.c
472 * (which really doesn't belong in kern/anything.c). Eventually, all of
473 * the code in kern_intr.c and machdep_intr.c should get moved here, since
474 * this is going to be the official interface.
475 */
476 static int
477 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
478 int flags, void (*ihand)(void *), void *arg, void **cookiep)
479 {
480 int error;
481
482 /* somebody tried to setup an irq that failed to allocate! */
483 if (irq == NULL)
484 panic("nexus_setup_intr: NULL irq resource!");
485
486 *cookiep = 0;
487 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
488 flags |= INTR_EXCL;
489
490 /*
491 * We depend here on rman_activate_resource() being idempotent.
492 */
493 error = rman_activate_resource(irq);
494 if (error)
495 return (error);
496
497 error = intr_add_handler(device_get_nameunit(child),
498 rman_get_start(irq), ihand, arg, flags, cookiep);
499
500 return (error);
501 }
502
503 static int
504 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
505 {
506 return (intr_remove_handler(ih));
507 }
508
509 static int
510 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
511 enum intr_polarity pol)
512 {
513 return (intr_config_intr(irq, trig, pol));
514 }
515
516 static struct resource_list *
517 nexus_get_reslist(device_t dev, device_t child)
518 {
519 struct nexus_device *ndev = DEVTONX(child);
520
521 return (&ndev->nx_resources);
522 }
523
524 static int
525 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
526 {
527 struct nexus_device *ndev = DEVTONX(child);
528 struct resource_list *rl = &ndev->nx_resources;
529
530 /* XXX this should return a success/failure indicator */
531 resource_list_add(rl, type, rid, start, start + count - 1, count);
532 return(0);
533 }
534
535 static int
536 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
537 {
538 struct nexus_device *ndev = DEVTONX(child);
539 struct resource_list *rl = &ndev->nx_resources;
540 struct resource_list_entry *rle;
541
542 rle = resource_list_find(rl, type, rid);
543 if (!rle)
544 return(ENOENT);
545 if (startp)
546 *startp = rle->start;
547 if (countp)
548 *countp = rle->count;
549 return(0);
550 }
551
552 static void
553 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
554 {
555 struct nexus_device *ndev = DEVTONX(child);
556 struct resource_list *rl = &ndev->nx_resources;
557
558 resource_list_delete(rl, type, rid);
559 }
560
561 #ifdef DEV_ISA
562 /*
563 * Placeholder which claims PnP 'devices' which describe system
564 * resources.
565 */
566 static struct isa_pnp_id sysresource_ids[] = {
567 { 0x010cd041 /* PNP0c01 */, "System Memory" },
568 { 0x020cd041 /* PNP0c02 */, "System Resource" },
569 { 0 }
570 };
571
572 static int
573 sysresource_probe(device_t dev)
574 {
575 int result;
576
577 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
578 device_quiet(dev);
579 }
580 return(result);
581 }
582
583 static int
584 sysresource_attach(device_t dev)
585 {
586 return(0);
587 }
588
589 static device_method_t sysresource_methods[] = {
590 /* Device interface */
591 DEVMETHOD(device_probe, sysresource_probe),
592 DEVMETHOD(device_attach, sysresource_attach),
593 DEVMETHOD(device_detach, bus_generic_detach),
594 DEVMETHOD(device_shutdown, bus_generic_shutdown),
595 DEVMETHOD(device_suspend, bus_generic_suspend),
596 DEVMETHOD(device_resume, bus_generic_resume),
597 { 0, 0 }
598 };
599
600 static driver_t sysresource_driver = {
601 "sysresource",
602 sysresource_methods,
603 1, /* no softc */
604 };
605
606 static devclass_t sysresource_devclass;
607
608 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);
609 #endif /* DEV_ISA */
Cache object: ae32331cb5a261d2d9a4ac9eea0cde38
|