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.2/sys/i386/i386/nexus.c 162558 2006-09-22 19:04:51Z jhb $");
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/cbus/cbus.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;
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 for (irq = 0; irq < NUM_IO_INTS; irq++)
181 if (intr_lookup_source(irq) != NULL)
182 if (rman_manage_region(&irq_rman, irq, irq) != 0)
183 panic("nexus_probe irq_rman add");
184
185 /*
186 * ISA DMA on PCI systems is implemented in the ISA part of each
187 * PCI->ISA bridge and the channels can be duplicated if there are
188 * multiple bridges. (eg: laptops with docking stations)
189 */
190 drq_rman.rm_start = 0;
191 #ifdef PC98
192 drq_rman.rm_end = 3;
193 #else
194 drq_rman.rm_end = 7;
195 #endif
196 drq_rman.rm_type = RMAN_ARRAY;
197 drq_rman.rm_descr = "DMA request lines";
198 /* XXX drq 0 not available on some machines */
199 if (rman_init(&drq_rman)
200 || rman_manage_region(&drq_rman,
201 drq_rman.rm_start, drq_rman.rm_end))
202 panic("nexus_probe drq_rman");
203
204 /*
205 * However, IO ports and Memory truely are global at this level,
206 * as are APIC interrupts (however many IO APICS there turn out
207 * to be on large systems..)
208 */
209 port_rman.rm_start = 0;
210 port_rman.rm_end = 0xffff;
211 port_rman.rm_type = RMAN_ARRAY;
212 port_rman.rm_descr = "I/O ports";
213 if (rman_init(&port_rman)
214 || rman_manage_region(&port_rman, 0, 0xffff))
215 panic("nexus_probe port_rman");
216
217 mem_rman.rm_start = 0;
218 mem_rman.rm_end = ~0u;
219 mem_rman.rm_type = RMAN_ARRAY;
220 mem_rman.rm_descr = "I/O memory addresses";
221 if (rman_init(&mem_rman)
222 || rman_manage_region(&mem_rman, 0, ~0))
223 panic("nexus_probe mem_rman");
224
225 return 0;
226 }
227
228 static int
229 nexus_attach(device_t dev)
230 {
231
232 bus_generic_probe(dev);
233 bus_generic_attach(dev);
234 return 0;
235 }
236
237 static int
238 nexus_print_all_resources(device_t dev)
239 {
240 struct nexus_device *ndev = DEVTONX(dev);
241 struct resource_list *rl = &ndev->nx_resources;
242 int retval = 0;
243
244 if (STAILQ_FIRST(rl))
245 retval += printf(" at");
246
247 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
248 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
249 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
250
251 return retval;
252 }
253
254 static int
255 nexus_print_child(device_t bus, device_t child)
256 {
257 int retval = 0;
258
259 retval += bus_print_child_header(bus, child);
260 retval += nexus_print_all_resources(child);
261 if (device_get_flags(child))
262 retval += printf(" flags %#x", device_get_flags(child));
263 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
264
265 return (retval);
266 }
267
268 static device_t
269 nexus_add_child(device_t bus, int order, const char *name, int unit)
270 {
271 device_t child;
272 struct nexus_device *ndev;
273
274 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
275 if (!ndev)
276 return(0);
277 resource_list_init(&ndev->nx_resources);
278
279 child = device_add_child_ordered(bus, order, name, unit);
280
281 /* should we free this in nexus_child_detached? */
282 device_set_ivars(child, ndev);
283
284 return(child);
285 }
286
287 /*
288 * Allocate a resource on behalf of child. NB: child is usually going to be a
289 * child of one of our descendants, not a direct child of nexus0.
290 * (Exceptions include npx.)
291 */
292 static struct resource *
293 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
294 u_long start, u_long end, u_long count, u_int flags)
295 {
296 struct nexus_device *ndev = DEVTONX(child);
297 struct resource *rv;
298 struct resource_list_entry *rle;
299 struct rman *rm;
300 int needactivate = flags & RF_ACTIVE;
301 #ifdef PC98
302 bus_space_handle_t bh;
303 #endif
304
305 /*
306 * If this is an allocation of the "default" range for a given RID, and
307 * we know what the resources for this device are (ie. they aren't maintained
308 * by a child bus), then work out the start/end values.
309 */
310 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
311 if (ndev == NULL)
312 return(NULL);
313 rle = resource_list_find(&ndev->nx_resources, type, *rid);
314 if (rle == NULL)
315 return(NULL);
316 start = rle->start;
317 end = rle->end;
318 count = rle->count;
319 }
320
321 flags &= ~RF_ACTIVE;
322
323 switch (type) {
324 case SYS_RES_IRQ:
325 rm = &irq_rman;
326 break;
327
328 case SYS_RES_DRQ:
329 rm = &drq_rman;
330 break;
331
332 case SYS_RES_IOPORT:
333 rm = &port_rman;
334 break;
335
336 case SYS_RES_MEMORY:
337 rm = &mem_rman;
338 break;
339
340 default:
341 return 0;
342 }
343
344 rv = rman_reserve_resource(rm, start, end, count, flags, child);
345 if (rv == 0)
346 return 0;
347
348 if (type == SYS_RES_MEMORY) {
349 rman_set_bustag(rv, I386_BUS_SPACE_MEM);
350 } else if (type == SYS_RES_IOPORT) {
351 rman_set_bustag(rv, I386_BUS_SPACE_IO);
352 #ifndef PC98
353 rman_set_bushandle(rv, rman_get_start(rv));
354 #endif
355 }
356
357 #ifdef PC98
358 if ((type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) &&
359 i386_bus_space_handle_alloc(rman_get_bustag(rv),
360 rman_get_start(rv), count, &bh) != 0) {
361 rman_release_resource(rv);
362 return 0;
363 }
364 rman_set_bushandle(rv, bh);
365 #endif
366
367 if (needactivate) {
368 if (bus_activate_resource(child, type, *rid, rv)) {
369 #ifdef PC98
370 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
371 bh = rman_get_bushandle(rv);
372 i386_bus_space_handle_free(rman_get_bustag(rv),
373 bh, bh->bsh_sz);
374 }
375 #endif
376 rman_release_resource(rv);
377 return 0;
378 }
379 }
380
381 return rv;
382 }
383
384 static int
385 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
386 struct resource *r)
387 {
388 #ifdef PC98
389 bus_space_handle_t bh;
390 #endif
391 /*
392 * If this is a memory resource, map it into the kernel.
393 */
394 if (rman_get_bustag(r) == I386_BUS_SPACE_MEM) {
395 caddr_t vaddr = 0;
396
397 if (rman_get_end(r) < 1024 * 1024) {
398 /*
399 * The first 1Mb is mapped at KERNBASE.
400 */
401 vaddr = (caddr_t)(uintptr_t)(KERNBASE + rman_get_start(r));
402 } else {
403 u_int32_t paddr;
404 u_int32_t psize;
405 u_int32_t poffs;
406
407 paddr = rman_get_start(r);
408 psize = rman_get_size(r);
409
410 poffs = paddr - trunc_page(paddr);
411 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
412 }
413 rman_set_virtual(r, vaddr);
414 #ifdef PC98
415 /* PC-98: the type of bus_space_handle_t is the structure. */
416 bh = rman_get_bushandle(r);
417 bh->bsh_base = (bus_addr_t) vaddr;
418 #else
419 /* IBM-PC: the type of bus_space_handle_t is u_int */
420 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
421 #endif
422 }
423 return (rman_activate_resource(r));
424 }
425
426 static int
427 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
428 struct resource *r)
429 {
430 /*
431 * If this is a memory resource, unmap it.
432 */
433 if ((rman_get_bustag(r) == I386_BUS_SPACE_MEM) &&
434 (rman_get_end(r) >= 1024 * 1024)) {
435 u_int32_t psize;
436
437 psize = rman_get_size(r);
438 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
439 }
440
441 return (rman_deactivate_resource(r));
442 }
443
444 static int
445 nexus_release_resource(device_t bus, device_t child, int type, int rid,
446 struct resource *r)
447 {
448 if (rman_get_flags(r) & RF_ACTIVE) {
449 int error = bus_deactivate_resource(child, type, rid, r);
450 if (error)
451 return error;
452 }
453 #ifdef PC98
454 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
455 bus_space_handle_t bh;
456
457 bh = rman_get_bushandle(r);
458 i386_bus_space_handle_free(rman_get_bustag(r), bh, bh->bsh_sz);
459 }
460 #endif
461 return (rman_release_resource(r));
462 }
463
464 /*
465 * Currently this uses the really grody interface from kern/kern_intr.c
466 * (which really doesn't belong in kern/anything.c). Eventually, all of
467 * the code in kern_intr.c and machdep_intr.c should get moved here, since
468 * this is going to be the official interface.
469 */
470 static int
471 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
472 int flags, void (*ihand)(void *), void *arg, void **cookiep)
473 {
474 int error;
475
476 /* somebody tried to setup an irq that failed to allocate! */
477 if (irq == NULL)
478 panic("nexus_setup_intr: NULL irq resource!");
479
480 *cookiep = 0;
481 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
482 flags |= INTR_EXCL;
483
484 /*
485 * We depend here on rman_activate_resource() being idempotent.
486 */
487 error = rman_activate_resource(irq);
488 if (error)
489 return (error);
490
491 error = intr_add_handler(device_get_nameunit(child),
492 rman_get_start(irq), ihand, arg, flags, cookiep);
493
494 return (error);
495 }
496
497 static int
498 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
499 {
500 return (intr_remove_handler(ih));
501 }
502
503 static int
504 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
505 enum intr_polarity pol)
506 {
507 return (intr_config_intr(irq, trig, pol));
508 }
509
510 static struct resource_list *
511 nexus_get_reslist(device_t dev, device_t child)
512 {
513 struct nexus_device *ndev = DEVTONX(child);
514
515 return (&ndev->nx_resources);
516 }
517
518 static int
519 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
520 {
521 struct nexus_device *ndev = DEVTONX(child);
522 struct resource_list *rl = &ndev->nx_resources;
523
524 /* XXX this should return a success/failure indicator */
525 resource_list_add(rl, type, rid, start, start + count - 1, count);
526 return(0);
527 }
528
529 static int
530 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
531 {
532 struct nexus_device *ndev = DEVTONX(child);
533 struct resource_list *rl = &ndev->nx_resources;
534 struct resource_list_entry *rle;
535
536 rle = resource_list_find(rl, type, rid);
537 if (!rle)
538 return(ENOENT);
539 if (startp)
540 *startp = rle->start;
541 if (countp)
542 *countp = rle->count;
543 return(0);
544 }
545
546 static void
547 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
548 {
549 struct nexus_device *ndev = DEVTONX(child);
550 struct resource_list *rl = &ndev->nx_resources;
551
552 resource_list_delete(rl, type, rid);
553 }
554
555 #ifdef DEV_ISA
556 /*
557 * Placeholder which claims PnP 'devices' which describe system
558 * resources.
559 */
560 static struct isa_pnp_id sysresource_ids[] = {
561 { 0x010cd041 /* PNP0c01 */, "System Memory" },
562 { 0x020cd041 /* PNP0c02 */, "System Resource" },
563 { 0 }
564 };
565
566 static int
567 sysresource_probe(device_t dev)
568 {
569 int result;
570
571 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
572 device_quiet(dev);
573 }
574 return(result);
575 }
576
577 static int
578 sysresource_attach(device_t dev)
579 {
580 return(0);
581 }
582
583 static device_method_t sysresource_methods[] = {
584 /* Device interface */
585 DEVMETHOD(device_probe, sysresource_probe),
586 DEVMETHOD(device_attach, sysresource_attach),
587 DEVMETHOD(device_detach, bus_generic_detach),
588 DEVMETHOD(device_shutdown, bus_generic_shutdown),
589 DEVMETHOD(device_suspend, bus_generic_suspend),
590 DEVMETHOD(device_resume, bus_generic_resume),
591 { 0, 0 }
592 };
593
594 static driver_t sysresource_driver = {
595 "sysresource",
596 sysresource_methods,
597 1, /* no softc */
598 };
599
600 static devclass_t sysresource_devclass;
601
602 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);
603 #endif /* DEV_ISA */
Cache object: da83b9b27ae6071cfb210b603dc880f9
|