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 * $FreeBSD$ 30 */ 31 32/* 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. 41 */ 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/bus.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> 51#include <sys/rman.h> 52#include <sys/interrupt.h> 53#include <sys/pcpu.h> 54 55#include <vm/vm.h> 56#include <vm/pmap.h> 57 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> 63 64#include <contrib/dev/acpica/include/acpi.h> 65 66#include <dev/acpica/acpivar.h> 67 68#include <isa/isareg.h> 69#include <sys/rtprio.h> 70 71#include "clock_if.h" 72 73static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device"); 74struct nexus_device { 75 struct resource_list nx_resources; 76}; 77 78#define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev)) 79 80static struct rman irq_rman, port_rman, mem_rman; 81 82static int nexus_probe(device_t); 83static int nexus_attach(device_t); 84static int nexus_print_child(device_t, device_t); 85static device_t nexus_add_child(device_t bus, u_int order, const char *name, 86 int unit); 87static struct resource *nexus_alloc_resource(device_t, device_t, int, int *, 88 u_long, u_long, u_long, u_int); 89static int nexus_adjust_resource(device_t, device_t, int, struct resource *, 90 u_long, u_long); 91static int nexus_activate_resource(device_t, device_t, int, int, 92 struct resource *); 93static int nexus_deactivate_resource(device_t, device_t, int, int, 94 struct resource *); 95static int nexus_release_resource(device_t, device_t, int, int, 96 struct resource *); 97static int nexus_setup_intr(device_t, device_t, struct resource *, int flags, 98 driver_filter_t filter, void (*)(void *), void *, 99 void **); 100static int nexus_teardown_intr(device_t, device_t, struct resource *, 101 void *); 102static struct resource_list *nexus_get_reslist(device_t dev, device_t child); 103static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long); 104static int nexus_get_resource(device_t, device_t, int, int, u_long *, 105 u_long *); 106static void nexus_delete_resource(device_t, device_t, int, int); 107static int nexus_bind_intr(device_t, device_t, struct resource *, int); 108static int nexus_config_intr(device_t, int, enum intr_trigger, 109 enum intr_polarity); 110 111static int nexus_gettime(device_t, struct timespec *); 112static int nexus_settime(device_t, struct timespec *); 113 114static 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), 122 123 /* Bus interface */ 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), 139 140 /* Clock interface */ 141 DEVMETHOD(clock_gettime, nexus_gettime), 142 DEVMETHOD(clock_settime, nexus_settime), 143 144 { 0, 0 } 145}; 146 147static driver_t nexus_driver = { 148 "nexus", 149 nexus_methods, 150 1, /* no softc */ 151}; 152static devclass_t nexus_devclass; 153 154DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); 155 156static int 157nexus_probe(device_t dev) 158{ 159 160 device_quiet(dev); /* suppress attach message for neatness */ 161 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"); 170 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"); 178 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"); 186 187 return bus_generic_probe(dev); 188} 189 190static int 191nexus_attach(device_t dev) 192{ 193 194 /* 195 * Mask the legacy PICs - we will use the I/O SAPIC for interrupt. 196 */ 197 outb(IO_ICU1+1, 0xff); 198 outb(IO_ICU2+1, 0xff); 199 200 if (acpi_identify() == 0) 201 BUS_ADD_CHILD(dev, 10, "acpi", 0); 202 clock_register(dev, 1000); 203 bus_generic_attach(dev); 204 return 0; 205} 206 207static int 208nexus_print_child(device_t bus, device_t child) 209{ 210 struct nexus_device *ndev = DEVTONX(child); 211 struct resource_list *rl = &ndev->nx_resources; 212 int retval = 0; 213 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 */ 221 222 return (retval); 223} 224 225static device_t 226nexus_add_child(device_t bus, u_int order, const char *name, int unit) 227{ 228 device_t child; 229 struct nexus_device *ndev; 230 231 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO); 232 if (!ndev) 233 return(0); 234 resource_list_init(&ndev->nx_resources); 235 236 child = device_add_child_ordered(bus, order, name, unit); 237 238 /* should we free this in nexus_child_detached? */ 239 device_set_ivars(child, ndev); 240 241 return(child); 242} 243 244static struct rman * 245nexus_rman(int type) 246{ 247 switch (type) { 248 case SYS_RES_IRQ: 249 return (&irq_rman); 250 case SYS_RES_IOPORT: 251 return (&port_rman); 252 case SYS_RES_MEMORY: 253 return (&mem_rman); 254 default: 255 return (NULL); 256 } 257} 258 259/* 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.) 263 */ 264static struct resource * 265nexus_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) 267{ 268 struct nexus_device *ndev = DEVTONX(child); 269 struct resource *rv; 270 struct resource_list_entry *rle; 271 struct rman *rm; 272 int needactivate = flags & RF_ACTIVE; 273 274 /* 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. 278 */ 279 if ((start == 0UL) && (end == ~0UL) && (count == 1)) { 280 if (ndev == NULL) 281 return(NULL); 282 rle = resource_list_find(&ndev->nx_resources, type, *rid); 283 if (rle == NULL) 284 return(NULL); 285 start = rle->start; 286 end = rle->end; 287 count = rle->count; 288 } 289 290 flags &= ~RF_ACTIVE; 291 rm = nexus_rman(type); 292 if (rm == NULL) 293 return (NULL); 294 295 rv = rman_reserve_resource(rm, start, end, count, flags, child); 296 if (rv == 0) 297 return 0; 298 rman_set_rid(rv, *rid); 299 300 if (needactivate) { 301 if (bus_activate_resource(child, type, *rid, rv)) { 302 rman_release_resource(rv); 303 return 0; 304 } 305 } 306 307 return rv; 308} 309 310static int 311nexus_adjust_resource(device_t bus, device_t child, int type, 312 struct resource *r, u_long start, u_long end) 313{ 314 struct rman *rm; 315 316 rm = nexus_rman(type); 317 if (rm == NULL) 318 return (ENXIO); 319 if (!rman_is_region_manager(r, rm)) 320 return (EINVAL); 321 return (rman_adjust_resource(r, start, end)); 322} 323 324static int 325nexus_activate_resource(device_t bus, device_t child, int type, int rid, 326 struct resource *r) 327{ 328 vm_paddr_t paddr; 329 void *vaddr; 330 331 paddr = rman_get_start(r); 332 333 switch (type) { 334 case SYS_RES_IOPORT: 335 rman_set_bustag(r, IA64_BUS_SPACE_IO); 336 rman_set_bushandle(r, paddr); 337 break; 338 case SYS_RES_MEMORY: 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); 343 break; 344 } 345 return (rman_activate_resource(r)); 346} 347 348static int 349nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 350 struct resource *r) 351{ 352 353 return (rman_deactivate_resource(r)); 354} 355 356static int 357nexus_release_resource(device_t bus, device_t child, int type, int rid, 358 struct resource *r) 359{ 360 if (rman_get_flags(r) & RF_ACTIVE) { 361 int error = bus_deactivate_resource(child, type, rid, r); 362 if (error) 363 return error; 364 } 365 return (rman_release_resource(r)); 366} 367 368/* 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. 373 */ 374static int 375nexus_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) 378{ 379 driver_t *driver; 380 int error; 381 382 /* somebody tried to setup an irq that failed to allocate! */ 383 if (irq == NULL) 384 panic("nexus_setup_intr: NULL irq resource!"); 385 386 *cookiep = 0; 387 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0) 388 flags |= INTR_EXCL; 389 390 driver = device_get_driver(child); 391 392 /* 393 * We depend here on rman_activate_resource() being idempotent. 394 */ 395 error = rman_activate_resource(irq); 396 if (error) 397 return (error); 398 399 error = ia64_setup_intr(device_get_nameunit(child), 400 rman_get_start(irq), filter, ihand, arg, flags, cookiep); 401 402 return (error); 403} 404 405static int 406nexus_teardown_intr(device_t dev, device_t child, struct resource *ires, 407 void *cookie) 408{ 409 410 return (ia64_teardown_intr(cookie)); 411} 412 413static struct resource_list * 414nexus_get_reslist(device_t dev, device_t child) 415{ 416 struct nexus_device *ndev = DEVTONX(child); 417 418 return (&ndev->nx_resources); 419} 420 421static int 422nexus_set_resource(device_t dev, device_t child, int type, int rid, 423 u_long start, u_long count) 424{ 425 struct nexus_device *ndev = DEVTONX(child); 426 struct resource_list *rl = &ndev->nx_resources; 427 428 if (type == SYS_RES_IOPORT && start > (0x10000 - count)) { 429 /* 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. 435 */ 436 if (bootverbose) 437 printf("%s: invalid port range (%#lx-%#lx); " 438 "assuming I/O memory range.\n", __func__, start, 439 start + count - 1); 440 type = SYS_RES_MEMORY; 441 } 442 443 /* XXX this should return a success/failure indicator */ 444 resource_list_add(rl, type, rid, start, start + count - 1, count); 445 return(0); 446} 447 448static int 449nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 450{ 451 struct nexus_device *ndev = DEVTONX(child); 452 struct resource_list *rl = &ndev->nx_resources; 453 struct resource_list_entry *rle; 454 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); 458 if (!rle) 459 return(ENOENT); 460 if (startp) 461 *startp = rle->start; 462 if (countp) 463 *countp = rle->count; 464 return(0); 465} 466 467static void 468nexus_delete_resource(device_t dev, device_t child, int type, int rid) 469{ 470 struct nexus_device *ndev = DEVTONX(child); 471 struct resource_list *rl = &ndev->nx_resources; 472 473 resource_list_delete(rl, type, rid); 474} 475 476static int 477nexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 478 enum intr_polarity pol) 479{ 480 481 return (sapic_config_intr(irq, trig, pol)); 482} 483 484static int 485nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu) 486{ 487 struct pcpu *pc; 488 489 pc = cpuid_to_pcpu[cpu]; 490 if (pc == NULL) 491 return (EINVAL); 492 return (sapic_bind_intr(rman_get_start(irq), pc)); 493} 494 495static int 496nexus_gettime(device_t dev, struct timespec *ts) 497{ 498 struct clocktime ct; 499 struct efi_tm tm; 500 501 efi_get_time(&tm); 502 503 /* 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. 507 */ 508 if (tm.tm_year < 2005) 509 return (EINVAL); 510 511 ct.nsec = tm.tm_nsec; 512 ct.sec = tm.tm_sec; 513 ct.min = tm.tm_min; 514 ct.hour = tm.tm_hour; 515 ct.day = tm.tm_mday; 516 ct.mon = tm.tm_mon; 517 ct.year = tm.tm_year; 518 ct.dow = -1; 519 return (clock_ct_to_ts(&ct, ts)); 520} 521 522static int 523nexus_settime(device_t dev, struct timespec *ts) 524{ 525 struct clocktime ct; 526 struct efi_tm tm; 527 528 efi_get_time(&tm); 529 530 clock_ts_to_ct(ts, &ct); 531 tm.tm_nsec = ts->tv_nsec; 532 tm.tm_sec = ct.sec; 533 tm.tm_min = ct.min; 534 tm.tm_hour = ct.hour; 535 tm.tm_year = ct.year; 536 tm.tm_mon = ct.mon; 537 tm.tm_mday = ct.day; 538 return (efi_set_time(&tm)); 539} 540