2 * Copyright 1998 Massachusetts Institute of Technology
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
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
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.
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/clock.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <machine/bus.h>
52 #include <sys/interrupt.h>
57 #include <machine/efi.h>
58 #include <machine/intr.h>
59 #include <machine/nexusvar.h>
60 #include <machine/pmap.h>
61 #include <machine/resource.h>
62 #include <machine/sapicvar.h>
63 #include <machine/vmparam.h>
65 #include <contrib/dev/acpica/include/acpi.h>
67 #include <dev/acpica/acpivar.h>
69 #include <isa/isareg.h>
70 #include <sys/rtprio.h>
74 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
76 struct resource_list nx_resources;
80 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
82 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
84 static int nexus_probe(device_t);
85 static int nexus_attach(device_t);
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,
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_read_ivar(device_t, device_t, int, uintptr_t *);
92 static int nexus_write_ivar(device_t, device_t, int, uintptr_t);
93 static int nexus_activate_resource(device_t, device_t, int, int,
95 static int nexus_deactivate_resource(device_t, device_t, int, int,
97 static int nexus_release_resource(device_t, device_t, int, int,
99 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
100 driver_filter_t filter, void (*)(void *), void *,
102 static int nexus_teardown_intr(device_t, device_t, struct resource *,
104 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
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 *,
108 static void nexus_delete_resource(device_t, device_t, int, int);
109 static int nexus_config_intr(device_t, int, enum intr_trigger,
112 static int nexus_gettime(device_t, struct timespec *);
113 static int nexus_settime(device_t, struct timespec *);
115 static device_method_t nexus_methods[] = {
116 /* Device interface */
117 DEVMETHOD(device_probe, nexus_probe),
118 DEVMETHOD(device_attach, nexus_attach),
119 DEVMETHOD(device_detach, bus_generic_detach),
120 DEVMETHOD(device_shutdown, bus_generic_shutdown),
121 DEVMETHOD(device_suspend, bus_generic_suspend),
122 DEVMETHOD(device_resume, bus_generic_resume),
125 DEVMETHOD(bus_print_child, nexus_print_child),
126 DEVMETHOD(bus_add_child, nexus_add_child),
127 DEVMETHOD(bus_read_ivar, nexus_read_ivar),
128 DEVMETHOD(bus_write_ivar, nexus_write_ivar),
129 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
130 DEVMETHOD(bus_release_resource, nexus_release_resource),
131 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
132 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
133 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
134 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
135 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
136 DEVMETHOD(bus_set_resource, nexus_set_resource),
137 DEVMETHOD(bus_get_resource, nexus_get_resource),
138 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
139 DEVMETHOD(bus_config_intr, nexus_config_intr),
141 /* Clock interface */
142 DEVMETHOD(clock_gettime, nexus_gettime),
143 DEVMETHOD(clock_settime, nexus_settime),
148 static driver_t nexus_driver = {
153 static devclass_t nexus_devclass;
155 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
158 nexus_probe(device_t dev)
161 device_quiet(dev); /* suppress attach message for neatness */
166 * - IRQ resource creation should be moved to the PIC/APIC driver.
167 * - DRQ resource creation should be moved to the DMAC driver.
168 * - The above should be sorted to probe earlier than any child busses.
170 * - Leave I/O and memory creation here, as child probes may need them.
171 * (especially eg. ACPI)
175 * IRQ's are on the mainboard on old systems, but on the ISA part
176 * of PCI->ISA bridges. There would be multiple sets of IRQs on
177 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
178 * component, so in a way, PCI can be a partial child of an ISA bus(!).
179 * APIC interrupts are global though.
181 * XXX We depend on the AT PIC driver correctly claiming IRQ 2
182 * to prevent its reuse elsewhere in the !APIC_IO case.
184 irq_rman.rm_start = 0;
185 irq_rman.rm_type = RMAN_ARRAY;
186 irq_rman.rm_descr = "Interrupt request lines";
187 irq_rman.rm_end = 255;
188 if (rman_init(&irq_rman)
189 || rman_manage_region(&irq_rman,
190 irq_rman.rm_start, irq_rman.rm_end))
191 panic("nexus_probe irq_rman");
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)
198 drq_rman.rm_start = 0;
200 drq_rman.rm_type = RMAN_ARRAY;
201 drq_rman.rm_descr = "DMA request lines";
202 /* XXX drq 0 not available on some machines */
203 if (rman_init(&drq_rman)
204 || rman_manage_region(&drq_rman,
205 drq_rman.rm_start, drq_rman.rm_end))
206 panic("nexus_probe drq_rman");
209 * However, IO ports and Memory truely are global at this level,
210 * as are APIC interrupts (however many IO APICS there turn out
211 * to be on large systems..)
213 port_rman.rm_start = 0;
214 port_rman.rm_end = 0xffff;
215 port_rman.rm_type = RMAN_ARRAY;
216 port_rman.rm_descr = "I/O ports";
217 if (rman_init(&port_rman)
218 || rman_manage_region(&port_rman, 0, 0xffff))
219 panic("nexus_probe port_rman");
221 mem_rman.rm_start = 0;
222 mem_rman.rm_end = ~0u;
223 mem_rman.rm_type = RMAN_ARRAY;
224 mem_rman.rm_descr = "I/O memory addresses";
225 if (rman_init(&mem_rman)
226 || rman_manage_region(&mem_rman, 0, ~0))
227 panic("nexus_probe mem_rman");
229 return bus_generic_probe(dev);
233 nexus_attach(device_t dev)
237 * Mask the legacy PICs - we will use the I/O SAPIC for interrupt.
239 outb(IO_ICU1+1, 0xff);
240 outb(IO_ICU2+1, 0xff);
242 if (acpi_identify() == 0)
243 BUS_ADD_CHILD(dev, 10, "acpi", 0);
244 clock_register(dev, 1000);
245 bus_generic_attach(dev);
250 nexus_print_child(device_t bus, device_t child)
252 struct nexus_device *ndev = DEVTONX(child);
253 struct resource_list *rl = &ndev->nx_resources;
256 retval += bus_print_child_header(bus, child);
257 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
258 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
259 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
260 if (ndev->nx_pcibus != -1)
261 retval += printf(" pcibus %d", ndev->nx_pcibus);
262 if (device_get_flags(child))
263 retval += printf(" flags %#x", device_get_flags(child));
264 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
270 nexus_add_child(device_t bus, int order, const char *name, int unit)
273 struct nexus_device *ndev;
275 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
278 resource_list_init(&ndev->nx_resources);
279 ndev->nx_pcibus = -1;
281 child = device_add_child_ordered(bus, order, name, unit);
283 /* should we free this in nexus_child_detached? */
284 device_set_ivars(child, ndev);
290 nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
292 struct nexus_device *ndev = DEVTONX(child);
295 case NEXUS_IVAR_PCIBUS:
296 *result = ndev->nx_pcibus;
306 nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
308 struct nexus_device *ndev = DEVTONX(child);
311 case NEXUS_IVAR_PCIBUS:
312 ndev->nx_pcibus = value;
322 * Allocate a resource on behalf of child. NB: child is usually going to be a
323 * child of one of our descendants, not a direct child of nexus0.
324 * (Exceptions include npx.)
326 static struct resource *
327 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
328 u_long start, u_long end, u_long count, u_int flags)
330 struct nexus_device *ndev = DEVTONX(child);
332 struct resource_list_entry *rle;
334 int needactivate = flags & RF_ACTIVE;
337 * If this is an allocation of the "default" range for a given RID, and
338 * we know what the resources for this device are (ie. they aren't maintained
339 * by a child bus), then work out the start/end values.
341 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
344 rle = resource_list_find(&ndev->nx_resources, type, *rid);
375 rv = rman_reserve_resource(rm, start, end, count, flags, child);
378 rman_set_rid(rv, *rid);
381 if (bus_activate_resource(child, type, *rid, rv)) {
382 rman_release_resource(rv);
391 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
394 vm_paddr_t paddr, psize;
398 * If this is a memory resource, map it into the kernel.
402 rman_set_bustag(r, IA64_BUS_SPACE_IO);
403 rman_set_bushandle(r, rman_get_start(r));
406 paddr = rman_get_start(r);
407 psize = rman_get_size(r);
408 vaddr = pmap_mapdev(paddr, psize);
409 rman_set_virtual(r, vaddr);
410 rman_set_bustag(r, IA64_BUS_SPACE_MEM);
411 rman_set_bushandle(r, (bus_space_handle_t) paddr);
414 return (rman_activate_resource(r));
418 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
422 return (rman_deactivate_resource(r));
426 nexus_release_resource(device_t bus, device_t child, int type, int rid,
429 if (rman_get_flags(r) & RF_ACTIVE) {
430 int error = bus_deactivate_resource(child, type, rid, r);
434 return (rman_release_resource(r));
438 * Currently this uses the really grody interface from kern/kern_intr.c
439 * (which really doesn't belong in kern/anything.c). Eventually, all of
440 * the code in kern_intr.c and machdep_intr.c should get moved here, since
441 * this is going to be the official interface.
444 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
445 int flags, driver_filter_t filter, void (*ihand)(void *),
446 void *arg, void **cookiep)
451 /* somebody tried to setup an irq that failed to allocate! */
453 panic("nexus_setup_intr: NULL irq resource!");
456 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
459 driver = device_get_driver(child);
462 * We depend here on rman_activate_resource() being idempotent.
464 error = rman_activate_resource(irq);
468 error = ia64_setup_intr(device_get_nameunit(child),
469 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
475 nexus_teardown_intr(device_t dev, device_t child, struct resource *ires,
479 return (ia64_teardown_intr(cookie));
482 static struct resource_list *
483 nexus_get_reslist(device_t dev, device_t child)
485 struct nexus_device *ndev = DEVTONX(child);
487 return (&ndev->nx_resources);
491 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
493 struct nexus_device *ndev = DEVTONX(child);
494 struct resource_list *rl = &ndev->nx_resources;
496 /* XXX this should return a success/failure indicator */
497 resource_list_add(rl, type, rid, start, start + count - 1, count);
502 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
504 struct nexus_device *ndev = DEVTONX(child);
505 struct resource_list *rl = &ndev->nx_resources;
506 struct resource_list_entry *rle;
508 rle = resource_list_find(rl, type, rid);
509 device_printf(child, "type %d rid %d startp %p countp %p - got %p\n",
510 type, rid, startp, countp, rle);
514 *startp = rle->start;
516 *countp = rle->count;
521 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
523 struct nexus_device *ndev = DEVTONX(child);
524 struct resource_list *rl = &ndev->nx_resources;
526 resource_list_delete(rl, type, rid);
530 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
531 enum intr_polarity pol)
534 return (sapic_config_intr(irq, trig, pol));
538 nexus_gettime(device_t dev, struct timespec *ts)
546 * This code was written in 2005, so logically EFI cannot return
547 * a year smaller than that. Assume the EFI clock is out of whack
548 * in that case and reset the EFI clock.
550 if (tm.tm_year < 2005)
553 ct.nsec = tm.tm_nsec;
556 ct.hour = tm.tm_hour;
559 ct.year = tm.tm_year;
561 return (clock_ct_to_ts(&ct, ts));
565 nexus_settime(device_t dev, struct timespec *ts)
572 clock_ts_to_ct(ts, &ct);
573 tm.tm_nsec = ts->tv_nsec;
576 tm.tm_hour = ct.hour;
577 tm.tm_year = ct.year;
580 return (efi_set_time(&tm));