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_adjust_resource(device_t, device_t, int, struct resource *,
91 static int nexus_activate_resource(device_t, device_t, int, int,
93 static int nexus_deactivate_resource(device_t, device_t, int, int,
95 static int nexus_release_resource(device_t, device_t, int, int,
97 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
98 driver_filter_t filter, void (*)(void *), void *,
100 static int nexus_teardown_intr(device_t, device_t, struct resource *,
102 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
103 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
104 static int nexus_get_resource(device_t, device_t, int, int, u_long *,
106 static void nexus_delete_resource(device_t, device_t, int, int);
107 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
108 static int nexus_config_intr(device_t, int, enum intr_trigger,
111 static int nexus_gettime(device_t, struct timespec *);
112 static int nexus_settime(device_t, struct timespec *);
114 static device_method_t nexus_methods[] = {
115 /* Device interface */
116 DEVMETHOD(device_probe, nexus_probe),
117 DEVMETHOD(device_attach, nexus_attach),
118 DEVMETHOD(device_detach, bus_generic_detach),
119 DEVMETHOD(device_shutdown, bus_generic_shutdown),
120 DEVMETHOD(device_suspend, bus_generic_suspend),
121 DEVMETHOD(device_resume, bus_generic_resume),
124 DEVMETHOD(bus_print_child, nexus_print_child),
125 DEVMETHOD(bus_add_child, nexus_add_child),
126 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
127 DEVMETHOD(bus_adjust_resource, nexus_adjust_resource),
128 DEVMETHOD(bus_release_resource, nexus_release_resource),
129 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
130 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
131 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
132 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
133 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
134 DEVMETHOD(bus_set_resource, nexus_set_resource),
135 DEVMETHOD(bus_get_resource, nexus_get_resource),
136 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
137 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
138 DEVMETHOD(bus_config_intr, nexus_config_intr),
140 /* Clock interface */
141 DEVMETHOD(clock_gettime, nexus_gettime),
142 DEVMETHOD(clock_settime, nexus_settime),
147 static driver_t nexus_driver = {
152 static devclass_t nexus_devclass;
154 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
157 nexus_probe(device_t dev)
160 device_quiet(dev); /* suppress attach message for neatness */
162 irq_rman.rm_type = RMAN_ARRAY;
163 irq_rman.rm_descr = "Interrupt request lines";
164 irq_rman.rm_start = 0;
165 irq_rman.rm_end = IA64_NXIVS - 1;
166 if (rman_init(&irq_rman)
167 || rman_manage_region(&irq_rman,
168 irq_rman.rm_start, irq_rman.rm_end))
169 panic("nexus_probe irq_rman");
171 port_rman.rm_start = 0;
172 port_rman.rm_end = 0xffff;
173 port_rman.rm_type = RMAN_ARRAY;
174 port_rman.rm_descr = "I/O ports";
175 if (rman_init(&port_rman)
176 || rman_manage_region(&port_rman, 0, 0xffff))
177 panic("nexus_probe port_rman");
179 mem_rman.rm_start = 0;
180 mem_rman.rm_end = ~0ul;
181 mem_rman.rm_type = RMAN_ARRAY;
182 mem_rman.rm_descr = "I/O memory addresses";
183 if (rman_init(&mem_rman)
184 || rman_manage_region(&mem_rman, 0, ~0))
185 panic("nexus_probe mem_rman");
187 return bus_generic_probe(dev);
191 nexus_attach(device_t dev)
195 * Mask the legacy PICs - we will use the I/O SAPIC for interrupt.
197 outb(IO_ICU1+1, 0xff);
198 outb(IO_ICU2+1, 0xff);
200 if (acpi_identify() == 0)
201 BUS_ADD_CHILD(dev, 10, "acpi", 0);
202 clock_register(dev, 1000);
203 bus_generic_attach(dev);
208 nexus_print_child(device_t bus, device_t child)
210 struct nexus_device *ndev = DEVTONX(child);
211 struct resource_list *rl = &ndev->nx_resources;
214 retval += bus_print_child_header(bus, child);
215 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
216 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
217 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
218 if (device_get_flags(child))
219 retval += printf(" flags %#x", device_get_flags(child));
220 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
226 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
229 struct nexus_device *ndev;
231 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
234 resource_list_init(&ndev->nx_resources);
236 child = device_add_child_ordered(bus, order, name, unit);
238 /* should we free this in nexus_child_detached? */
239 device_set_ivars(child, ndev);
260 * Allocate a resource on behalf of child. NB: child is usually going to be a
261 * child of one of our descendants, not a direct child of nexus0.
262 * (Exceptions include npx.)
264 static struct resource *
265 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
266 u_long start, u_long end, u_long count, u_int flags)
268 struct nexus_device *ndev = DEVTONX(child);
270 struct resource_list_entry *rle;
272 int needactivate = flags & RF_ACTIVE;
275 * If this is an allocation of the "default" range for a given RID, and
276 * we know what the resources for this device are (ie. they aren't maintained
277 * by a child bus), then work out the start/end values.
279 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
282 rle = resource_list_find(&ndev->nx_resources, type, *rid);
291 rm = nexus_rman(type);
295 rv = rman_reserve_resource(rm, start, end, count, flags, child);
298 rman_set_rid(rv, *rid);
301 if (bus_activate_resource(child, type, *rid, rv)) {
302 rman_release_resource(rv);
311 nexus_adjust_resource(device_t bus, device_t child, int type,
312 struct resource *r, u_long start, u_long end)
316 rm = nexus_rman(type);
319 if (!rman_is_region_manager(r, rm))
321 return (rman_adjust_resource(r, start, end));
325 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
331 paddr = rman_get_start(r);
335 rman_set_bustag(r, IA64_BUS_SPACE_IO);
336 rman_set_bushandle(r, paddr);
339 vaddr = pmap_mapdev(paddr, rman_get_size(r));
340 rman_set_bustag(r, IA64_BUS_SPACE_MEM);
341 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
342 rman_set_virtual(r, vaddr);
345 return (rman_activate_resource(r));
349 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
353 return (rman_deactivate_resource(r));
357 nexus_release_resource(device_t bus, device_t child, int type, int rid,
360 if (rman_get_flags(r) & RF_ACTIVE) {
361 int error = bus_deactivate_resource(child, type, rid, r);
365 return (rman_release_resource(r));
369 * Currently this uses the really grody interface from kern/kern_intr.c
370 * (which really doesn't belong in kern/anything.c). Eventually, all of
371 * the code in kern_intr.c and machdep_intr.c should get moved here, since
372 * this is going to be the official interface.
375 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
376 int flags, driver_filter_t filter, void (*ihand)(void *),
377 void *arg, void **cookiep)
382 /* somebody tried to setup an irq that failed to allocate! */
384 panic("nexus_setup_intr: NULL irq resource!");
387 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
390 driver = device_get_driver(child);
393 * We depend here on rman_activate_resource() being idempotent.
395 error = rman_activate_resource(irq);
399 error = ia64_setup_intr(device_get_nameunit(child),
400 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
406 nexus_teardown_intr(device_t dev, device_t child, struct resource *ires,
410 return (ia64_teardown_intr(cookie));
413 static struct resource_list *
414 nexus_get_reslist(device_t dev, device_t child)
416 struct nexus_device *ndev = DEVTONX(child);
418 return (&ndev->nx_resources);
422 nexus_set_resource(device_t dev, device_t child, int type, int rid,
423 u_long start, u_long count)
425 struct nexus_device *ndev = DEVTONX(child);
426 struct resource_list *rl = &ndev->nx_resources;
428 if (type == SYS_RES_IOPORT && start > (0x10000 - count)) {
430 * Work around a firmware bug in the HP rx2660, where in ACPI
431 * an I/O port is really a memory mapped I/O address. The bug
432 * is in the GAS that describes the address and in particular
433 * the SpaceId field. The field should not say the address is
434 * an I/O port when it is in fact an I/O memory address.
437 printf("%s: invalid port range (%#lx-%#lx); "
438 "assuming I/O memory range.\n", __func__, start,
440 type = SYS_RES_MEMORY;
443 /* XXX this should return a success/failure indicator */
444 resource_list_add(rl, type, rid, start, start + count - 1, count);
449 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
451 struct nexus_device *ndev = DEVTONX(child);
452 struct resource_list *rl = &ndev->nx_resources;
453 struct resource_list_entry *rle;
455 rle = resource_list_find(rl, type, rid);
456 device_printf(child, "type %d rid %d startp %p countp %p - got %p\n",
457 type, rid, startp, countp, rle);
461 *startp = rle->start;
463 *countp = rle->count;
468 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
470 struct nexus_device *ndev = DEVTONX(child);
471 struct resource_list *rl = &ndev->nx_resources;
473 resource_list_delete(rl, type, rid);
477 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
478 enum intr_polarity pol)
481 return (sapic_config_intr(irq, trig, pol));
485 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
489 pc = cpuid_to_pcpu[cpu];
492 return (sapic_bind_intr(rman_get_start(irq), pc));
496 nexus_gettime(device_t dev, struct timespec *ts)
504 * This code was written in 2005, so logically EFI cannot return
505 * a year smaller than that. Assume the EFI clock is out of whack
506 * in that case and reset the EFI clock.
508 if (tm.tm_year < 2005)
511 ct.nsec = tm.tm_nsec;
514 ct.hour = tm.tm_hour;
517 ct.year = tm.tm_year;
519 return (clock_ct_to_ts(&ct, ts));
523 nexus_settime(device_t dev, struct timespec *ts)
530 clock_ts_to_ct(ts, &ct);
531 tm.tm_nsec = ts->tv_nsec;
534 tm.tm_hour = ct.hour;
535 tm.tm_year = ct.year;
538 return (efi_set_time(&tm));