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>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <machine/bus.h>
53 #include <sys/interrupt.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>
70 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
72 struct resource_list nx_resources;
75 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
77 static struct rman irq_rman, port_rman, mem_rman;
79 static int nexus_probe(device_t);
80 static int nexus_attach(device_t);
81 static int nexus_print_child(device_t, device_t);
82 static device_t nexus_add_child(device_t bus, u_int order, const char *name,
84 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
85 u_long, u_long, u_long, u_int);
86 static int nexus_adjust_resource(device_t, device_t, int, struct resource *,
88 static int nexus_activate_resource(device_t, device_t, int, int,
90 static int nexus_deactivate_resource(device_t, device_t, int, int,
92 static int nexus_release_resource(device_t, device_t, int, int,
94 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
95 driver_filter_t filter, void (*)(void *), void *,
97 static int nexus_teardown_intr(device_t, device_t, struct resource *,
99 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
100 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
101 static int nexus_get_resource(device_t, device_t, int, int, u_long *,
103 static void nexus_delete_resource(device_t, device_t, int, int);
104 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
105 static int nexus_config_intr(device_t, int, enum intr_trigger,
108 static int nexus_gettime(device_t, struct timespec *);
109 static int nexus_settime(device_t, struct timespec *);
111 static device_method_t nexus_methods[] = {
112 /* Device interface */
113 DEVMETHOD(device_probe, nexus_probe),
114 DEVMETHOD(device_attach, nexus_attach),
115 DEVMETHOD(device_detach, bus_generic_detach),
116 DEVMETHOD(device_shutdown, bus_generic_shutdown),
117 DEVMETHOD(device_suspend, bus_generic_suspend),
118 DEVMETHOD(device_resume, bus_generic_resume),
121 DEVMETHOD(bus_print_child, nexus_print_child),
122 DEVMETHOD(bus_add_child, nexus_add_child),
123 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
124 DEVMETHOD(bus_adjust_resource, nexus_adjust_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 = ~0ul;
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)
191 if (acpi_identify() == 0)
192 BUS_ADD_CHILD(dev, 10, "acpi", 0);
193 clock_register(dev, 1000);
194 bus_generic_attach(dev);
199 nexus_print_child(device_t bus, device_t child)
201 struct nexus_device *ndev = DEVTONX(child);
202 struct resource_list *rl = &ndev->nx_resources;
205 retval += bus_print_child_header(bus, child);
206 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
207 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
208 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
209 if (device_get_flags(child))
210 retval += printf(" flags %#x", device_get_flags(child));
211 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
217 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
220 struct nexus_device *ndev;
222 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
225 resource_list_init(&ndev->nx_resources);
227 child = device_add_child_ordered(bus, order, name, unit);
229 /* should we free this in nexus_child_detached? */
230 device_set_ivars(child, ndev);
251 * Allocate a resource on behalf of child. NB: child is usually going to be a
252 * child of one of our descendants, not a direct child of nexus0.
253 * (Exceptions include npx.)
255 static struct resource *
256 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
257 u_long start, u_long end, u_long count, u_int flags)
259 struct nexus_device *ndev = DEVTONX(child);
261 struct resource_list_entry *rle;
263 int needactivate = flags & RF_ACTIVE;
266 * If this is an allocation of the "default" range for a given RID, and
267 * we know what the resources for this device are (ie. they aren't maintained
268 * by a child bus), then work out the start/end values.
270 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
273 rle = resource_list_find(&ndev->nx_resources, type, *rid);
282 rm = nexus_rman(type);
286 rv = rman_reserve_resource(rm, start, end, count, flags, child);
289 rman_set_rid(rv, *rid);
292 if (bus_activate_resource(child, type, *rid, rv)) {
293 rman_release_resource(rv);
302 nexus_adjust_resource(device_t bus, device_t child, int type,
303 struct resource *r, u_long start, u_long end)
307 rm = nexus_rman(type);
310 if (!rman_is_region_manager(r, rm))
312 return (rman_adjust_resource(r, start, end));
316 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
322 paddr = rman_get_start(r);
326 rman_set_bustag(r, IA64_BUS_SPACE_IO);
327 rman_set_bushandle(r, paddr);
330 vaddr = pmap_mapdev(paddr, rman_get_size(r));
331 rman_set_bustag(r, IA64_BUS_SPACE_MEM);
332 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
333 rman_set_virtual(r, vaddr);
336 return (rman_activate_resource(r));
340 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
344 return (rman_deactivate_resource(r));
348 nexus_release_resource(device_t bus, device_t child, int type, int rid,
351 if (rman_get_flags(r) & RF_ACTIVE) {
352 int error = bus_deactivate_resource(child, type, rid, r);
356 return (rman_release_resource(r));
360 * Currently this uses the really grody interface from kern/kern_intr.c
361 * (which really doesn't belong in kern/anything.c). Eventually, all of
362 * the code in kern_intr.c and machdep_intr.c should get moved here, since
363 * this is going to be the official interface.
366 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
367 int flags, driver_filter_t filter, void (*ihand)(void *),
368 void *arg, void **cookiep)
373 /* somebody tried to setup an irq that failed to allocate! */
375 panic("nexus_setup_intr: NULL irq resource!");
378 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
381 driver = device_get_driver(child);
384 * We depend here on rman_activate_resource() being idempotent.
386 error = rman_activate_resource(irq);
390 error = ia64_setup_intr(device_get_nameunit(child),
391 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
397 nexus_teardown_intr(device_t dev, device_t child, struct resource *ires,
401 return (ia64_teardown_intr(cookie));
404 static struct resource_list *
405 nexus_get_reslist(device_t dev, device_t child)
407 struct nexus_device *ndev = DEVTONX(child);
409 return (&ndev->nx_resources);
413 nexus_set_resource(device_t dev, device_t child, int type, int rid,
414 u_long start, u_long count)
416 struct nexus_device *ndev = DEVTONX(child);
417 struct resource_list *rl = &ndev->nx_resources;
419 if (type == SYS_RES_IOPORT && start > (0x10000 - count)) {
421 * Work around a firmware bug in the HP rx2660, where in ACPI
422 * an I/O port is really a memory mapped I/O address. The bug
423 * is in the GAS that describes the address and in particular
424 * the SpaceId field. The field should not say the address is
425 * an I/O port when it is in fact an I/O memory address.
428 printf("%s: invalid port range (%#lx-%#lx); "
429 "assuming I/O memory range.\n", __func__, start,
431 type = SYS_RES_MEMORY;
434 /* XXX this should return a success/failure indicator */
435 resource_list_add(rl, type, rid, start, start + count - 1, count);
440 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
442 struct nexus_device *ndev = DEVTONX(child);
443 struct resource_list *rl = &ndev->nx_resources;
444 struct resource_list_entry *rle;
446 rle = resource_list_find(rl, type, rid);
447 device_printf(child, "type %d rid %d startp %p countp %p - got %p\n",
448 type, rid, startp, countp, rle);
452 *startp = rle->start;
454 *countp = rle->count;
459 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
461 struct nexus_device *ndev = DEVTONX(child);
462 struct resource_list *rl = &ndev->nx_resources;
464 resource_list_delete(rl, type, rid);
468 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
469 enum intr_polarity pol)
472 return (sapic_config_intr(irq, trig, pol));
476 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
480 pc = cpuid_to_pcpu[cpu];
483 return (sapic_bind_intr(rman_get_start(irq), pc));
487 nexus_gettime(device_t dev, struct timespec *ts)
495 * This code was written in 2005, so logically EFI cannot return
496 * a year smaller than that. Assume the EFI clock is out of whack
497 * in that case and reset the EFI clock.
499 if (tm.tm_year < 2005)
502 ct.nsec = tm.tm_nsec;
505 ct.hour = tm.tm_hour;
508 ct.year = tm.tm_year;
510 return (clock_ct_to_ts(&ct, ts));
514 nexus_settime(device_t dev, struct timespec *ts)
521 clock_ts_to_ct(ts, &ct);
522 tm.tm_nsec = ts->tv_nsec;
525 tm.tm_hour = ct.hour;
526 tm.tm_year = ct.year;
529 return (efi_set_time(&tm));