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>
58 #include <machine/efi.h>
59 #include <machine/intr.h>
60 #include <machine/pmap.h>
61 #include <machine/resource.h>
62 #include <machine/vmparam.h>
64 #include <contrib/dev/acpica/include/acpi.h>
66 #include <dev/acpica/acpivar.h>
68 #include <isa/isareg.h>
69 #include <sys/rtprio.h>
73 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
75 struct resource_list nx_resources;
78 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
80 static struct rman irq_rman, port_rman, mem_rman;
82 static int nexus_probe(device_t);
83 static int nexus_attach(device_t);
84 static int nexus_print_child(device_t, device_t);
85 static device_t nexus_add_child(device_t bus, u_int order, const char *name,
87 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
88 u_long, u_long, u_long, u_int);
89 static int nexus_activate_resource(device_t, device_t, int, int,
91 static int nexus_deactivate_resource(device_t, device_t, int, int,
93 static int nexus_release_resource(device_t, device_t, int, int,
95 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
96 driver_filter_t filter, void (*)(void *), void *,
98 static int nexus_teardown_intr(device_t, device_t, struct resource *,
100 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
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 *,
104 static void nexus_delete_resource(device_t, device_t, int, int);
105 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
106 static int nexus_config_intr(device_t, int, enum intr_trigger,
109 static int nexus_gettime(device_t, struct timespec *);
110 static int nexus_settime(device_t, struct timespec *);
112 static device_method_t nexus_methods[] = {
113 /* Device interface */
114 DEVMETHOD(device_probe, nexus_probe),
115 DEVMETHOD(device_attach, nexus_attach),
116 DEVMETHOD(device_detach, bus_generic_detach),
117 DEVMETHOD(device_shutdown, bus_generic_shutdown),
118 DEVMETHOD(device_suspend, bus_generic_suspend),
119 DEVMETHOD(device_resume, bus_generic_resume),
122 DEVMETHOD(bus_print_child, nexus_print_child),
123 DEVMETHOD(bus_add_child, nexus_add_child),
124 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
125 DEVMETHOD(bus_release_resource, nexus_release_resource),
126 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
127 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
128 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
129 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
130 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
131 DEVMETHOD(bus_set_resource, nexus_set_resource),
132 DEVMETHOD(bus_get_resource, nexus_get_resource),
133 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
134 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
135 DEVMETHOD(bus_config_intr, nexus_config_intr),
137 /* Clock interface */
138 DEVMETHOD(clock_gettime, nexus_gettime),
139 DEVMETHOD(clock_settime, nexus_settime),
144 static driver_t nexus_driver = {
149 static devclass_t nexus_devclass;
151 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
154 nexus_probe(device_t dev)
157 device_quiet(dev); /* suppress attach message for neatness */
159 irq_rman.rm_type = RMAN_ARRAY;
160 irq_rman.rm_descr = "Interrupt request lines";
161 irq_rman.rm_start = 0;
162 irq_rman.rm_end = IA64_NXIVS - 1;
163 if (rman_init(&irq_rman)
164 || rman_manage_region(&irq_rman,
165 irq_rman.rm_start, irq_rman.rm_end))
166 panic("nexus_probe irq_rman");
168 port_rman.rm_start = 0;
169 port_rman.rm_end = 0xffff;
170 port_rman.rm_type = RMAN_ARRAY;
171 port_rman.rm_descr = "I/O ports";
172 if (rman_init(&port_rman)
173 || rman_manage_region(&port_rman, 0, 0xffff))
174 panic("nexus_probe port_rman");
176 mem_rman.rm_start = 0;
177 mem_rman.rm_end = ~0u;
178 mem_rman.rm_type = RMAN_ARRAY;
179 mem_rman.rm_descr = "I/O memory addresses";
180 if (rman_init(&mem_rman)
181 || rman_manage_region(&mem_rman, 0, ~0))
182 panic("nexus_probe mem_rman");
184 return bus_generic_probe(dev);
188 nexus_attach(device_t dev)
192 * Mask the legacy PICs - we will use the I/O SAPIC for interrupt.
194 outb(IO_ICU1+1, 0xff);
195 outb(IO_ICU2+1, 0xff);
197 if (acpi_identify() == 0)
198 BUS_ADD_CHILD(dev, 10, "acpi", 0);
199 clock_register(dev, 1000);
200 bus_generic_attach(dev);
205 nexus_print_child(device_t bus, device_t child)
207 struct nexus_device *ndev = DEVTONX(child);
208 struct resource_list *rl = &ndev->nx_resources;
211 retval += bus_print_child_header(bus, child);
212 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
213 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
214 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
215 if (device_get_flags(child))
216 retval += printf(" flags %#x", device_get_flags(child));
217 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
223 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
226 struct nexus_device *ndev;
228 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
231 resource_list_init(&ndev->nx_resources);
233 child = device_add_child_ordered(bus, order, name, unit);
235 /* should we free this in nexus_child_detached? */
236 device_set_ivars(child, ndev);
243 * Allocate a resource on behalf of child. NB: child is usually going to be a
244 * child of one of our descendants, not a direct child of nexus0.
245 * (Exceptions include npx.)
247 static struct resource *
248 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
249 u_long start, u_long end, u_long count, u_int flags)
251 struct nexus_device *ndev = DEVTONX(child);
253 struct resource_list_entry *rle;
255 int needactivate = flags & RF_ACTIVE;
258 * If this is an allocation of the "default" range for a given RID, and
259 * we know what the resources for this device are (ie. they aren't maintained
260 * by a child bus), then work out the start/end values.
262 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
265 rle = resource_list_find(&ndev->nx_resources, type, *rid);
292 rv = rman_reserve_resource(rm, start, end, count, flags, child);
295 rman_set_rid(rv, *rid);
298 if (bus_activate_resource(child, type, *rid, rv)) {
299 rman_release_resource(rv);
308 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
314 paddr = rman_get_start(r);
318 rman_set_bustag(r, IA64_BUS_SPACE_IO);
319 rman_set_bushandle(r, paddr);
322 vaddr = pmap_mapdev(paddr, rman_get_size(r));
323 rman_set_bustag(r, IA64_BUS_SPACE_MEM);
324 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
325 rman_set_virtual(r, vaddr);
328 return (rman_activate_resource(r));
332 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
336 return (rman_deactivate_resource(r));
340 nexus_release_resource(device_t bus, device_t child, int type, int rid,
343 if (rman_get_flags(r) & RF_ACTIVE) {
344 int error = bus_deactivate_resource(child, type, rid, r);
348 return (rman_release_resource(r));
352 * Currently this uses the really grody interface from kern/kern_intr.c
353 * (which really doesn't belong in kern/anything.c). Eventually, all of
354 * the code in kern_intr.c and machdep_intr.c should get moved here, since
355 * this is going to be the official interface.
358 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
359 int flags, driver_filter_t filter, void (*ihand)(void *),
360 void *arg, void **cookiep)
365 /* somebody tried to setup an irq that failed to allocate! */
367 panic("nexus_setup_intr: NULL irq resource!");
370 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
373 driver = device_get_driver(child);
376 * We depend here on rman_activate_resource() being idempotent.
378 error = rman_activate_resource(irq);
382 error = ia64_setup_intr(device_get_nameunit(child),
383 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
389 nexus_teardown_intr(device_t dev, device_t child, struct resource *ires,
393 return (ia64_teardown_intr(cookie));
396 static struct resource_list *
397 nexus_get_reslist(device_t dev, device_t child)
399 struct nexus_device *ndev = DEVTONX(child);
401 return (&ndev->nx_resources);
405 nexus_set_resource(device_t dev, device_t child, int type, int rid,
406 u_long start, u_long count)
408 struct nexus_device *ndev = DEVTONX(child);
409 struct resource_list *rl = &ndev->nx_resources;
411 if (type == SYS_RES_IOPORT && start > (0x10000 - count)) {
413 * Work around a firmware bug in the HP rx2660, where in ACPI
414 * an I/O port is really a memory mapped I/O address. The bug
415 * is in the GAS that describes the address and in particular
416 * the SpaceId field. The field should not say the address is
417 * an I/O port when it is in fact an I/O memory address.
420 printf("%s: invalid port range (%#lx-%#lx); "
421 "assuming I/O memory range.\n", __func__, start,
423 type = SYS_RES_MEMORY;
426 /* XXX this should return a success/failure indicator */
427 resource_list_add(rl, type, rid, start, start + count - 1, count);
432 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
434 struct nexus_device *ndev = DEVTONX(child);
435 struct resource_list *rl = &ndev->nx_resources;
436 struct resource_list_entry *rle;
438 rle = resource_list_find(rl, type, rid);
439 device_printf(child, "type %d rid %d startp %p countp %p - got %p\n",
440 type, rid, startp, countp, rle);
444 *startp = rle->start;
446 *countp = rle->count;
451 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
453 struct nexus_device *ndev = DEVTONX(child);
454 struct resource_list *rl = &ndev->nx_resources;
456 resource_list_delete(rl, type, rid);
460 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
461 enum intr_polarity pol)
464 return (sapic_config_intr(irq, trig, pol));
468 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
472 pc = cpuid_to_pcpu[cpu];
475 return (sapic_bind_intr(rman_get_start(irq), pc));
479 nexus_gettime(device_t dev, struct timespec *ts)
487 * This code was written in 2005, so logically EFI cannot return
488 * a year smaller than that. Assume the EFI clock is out of whack
489 * in that case and reset the EFI clock.
491 if (tm.tm_year < 2005)
494 ct.nsec = tm.tm_nsec;
497 ct.hour = tm.tm_hour;
500 ct.year = tm.tm_year;
502 return (clock_ct_to_ts(&ct, ts));
506 nexus_settime(device_t dev, struct timespec *ts)
513 clock_ts_to_ct(ts, &ct);
514 tm.tm_nsec = ts->tv_nsec;
517 tm.tm_hour = ct.hour;
518 tm.tm_year = ct.year;
521 return (efi_set_time(&tm));