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