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