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