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 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * This code implements a `root nexus' for Intel Architecture 35 * machines. The function of the root nexus is to serve as an 36 * attachment point for both processors and buses, and to manage 37 * resources which are common to all of them. In particular, 38 * this code implements the core resource managers for interrupt 39 * requests, DMA requests (which rightfully should be a part of the 40 * ISA code but it's easier to do it here for now), I/O port addresses, 41 * and I/O memory address space. 42 */ 43 44 #ifdef __amd64__ 45 #define DEV_APIC 46 #else 47 #include "opt_apic.h" 48 #endif 49 #include "opt_isa.h" 50 #include "opt_pci.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/bus.h> 55 #include <sys/kernel.h> 56 #include <sys/linker.h> 57 #include <sys/malloc.h> 58 #include <sys/module.h> 59 #include <machine/bus.h> 60 #include <machine/intr_machdep.h> 61 #include <sys/rman.h> 62 #include <sys/interrupt.h> 63 64 #include <machine/md_var.h> 65 #include <machine/vmparam.h> 66 #include <vm/vm.h> 67 #include <vm/pmap.h> 68 69 #include <machine/metadata.h> 70 #include <machine/nexusvar.h> 71 #include <machine/resource.h> 72 #include <machine/pc/bios.h> 73 74 #ifdef DEV_APIC 75 #include "pcib_if.h" 76 #endif 77 78 #ifdef DEV_ISA 79 #include <isa/isavar.h> 80 #include <isa/isareg.h> 81 #endif 82 #include <sys/rtprio.h> 83 84 #define ELF_KERN_STR ("elf"__XSTRING(__ELF_WORD_SIZE)" kernel") 85 86 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device"); 87 88 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev)) 89 90 struct rman irq_rman, drq_rman, port_rman, mem_rman; 91 92 static int nexus_probe(device_t); 93 static int nexus_attach(device_t); 94 static int nexus_print_all_resources(device_t dev); 95 static int nexus_print_child(device_t, device_t); 96 static device_t nexus_add_child(device_t bus, u_int order, const char *name, 97 int unit); 98 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *, 99 rman_res_t, rman_res_t, rman_res_t, 100 u_int); 101 static int nexus_adjust_resource(device_t, device_t, int, struct resource *, 102 rman_res_t, rman_res_t); 103 #ifdef SMP 104 static int nexus_bind_intr(device_t, device_t, struct resource *, int); 105 #endif 106 static int nexus_config_intr(device_t, int, enum intr_trigger, 107 enum intr_polarity); 108 static int nexus_describe_intr(device_t dev, device_t child, 109 struct resource *irq, void *cookie, 110 const char *descr); 111 static int nexus_activate_resource(device_t, device_t, int, int, 112 struct resource *); 113 static int nexus_deactivate_resource(device_t, device_t, int, int, 114 struct resource *); 115 static int nexus_map_resource(device_t bus, device_t child, int type, 116 struct resource *r, 117 struct resource_map_request *argsp, 118 struct resource_map *map); 119 static int nexus_unmap_resource(device_t bus, device_t child, int type, 120 struct resource *r, struct resource_map *map); 121 static int nexus_release_resource(device_t, device_t, int, int, 122 struct resource *); 123 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags, 124 driver_filter_t filter, void (*)(void *), void *, 125 void **); 126 static int nexus_teardown_intr(device_t, device_t, struct resource *, 127 void *); 128 static int nexus_suspend_intr(device_t, device_t, struct resource *); 129 static int nexus_resume_intr(device_t, device_t, struct resource *); 130 static struct resource_list *nexus_get_reslist(device_t dev, device_t child); 131 static int nexus_set_resource(device_t, device_t, int, int, 132 rman_res_t, rman_res_t); 133 static int nexus_get_resource(device_t, device_t, int, int, 134 rman_res_t *, rman_res_t *); 135 static void nexus_delete_resource(device_t, device_t, int, int); 136 static int nexus_get_cpus(device_t, device_t, enum cpu_sets, size_t, 137 cpuset_t *); 138 #if defined(DEV_APIC) && defined(DEV_PCI) 139 static int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs); 140 static int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs); 141 static int nexus_alloc_msix(device_t pcib, device_t dev, int *irq); 142 static int nexus_release_msix(device_t pcib, device_t dev, int irq); 143 static int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data); 144 #endif 145 146 static device_method_t nexus_methods[] = { 147 /* Device interface */ 148 DEVMETHOD(device_probe, nexus_probe), 149 DEVMETHOD(device_attach, nexus_attach), 150 DEVMETHOD(device_detach, bus_generic_detach), 151 DEVMETHOD(device_shutdown, bus_generic_shutdown), 152 DEVMETHOD(device_suspend, bus_generic_suspend), 153 DEVMETHOD(device_resume, bus_generic_resume), 154 155 /* Bus interface */ 156 DEVMETHOD(bus_print_child, nexus_print_child), 157 DEVMETHOD(bus_add_child, nexus_add_child), 158 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 159 DEVMETHOD(bus_adjust_resource, nexus_adjust_resource), 160 DEVMETHOD(bus_release_resource, nexus_release_resource), 161 DEVMETHOD(bus_activate_resource, nexus_activate_resource), 162 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 163 DEVMETHOD(bus_map_resource, nexus_map_resource), 164 DEVMETHOD(bus_unmap_resource, nexus_unmap_resource), 165 DEVMETHOD(bus_setup_intr, nexus_setup_intr), 166 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 167 DEVMETHOD(bus_suspend_intr, nexus_suspend_intr), 168 DEVMETHOD(bus_resume_intr, nexus_resume_intr), 169 #ifdef SMP 170 DEVMETHOD(bus_bind_intr, nexus_bind_intr), 171 #endif 172 DEVMETHOD(bus_config_intr, nexus_config_intr), 173 DEVMETHOD(bus_describe_intr, nexus_describe_intr), 174 DEVMETHOD(bus_get_resource_list, nexus_get_reslist), 175 DEVMETHOD(bus_set_resource, nexus_set_resource), 176 DEVMETHOD(bus_get_resource, nexus_get_resource), 177 DEVMETHOD(bus_delete_resource, nexus_delete_resource), 178 DEVMETHOD(bus_get_cpus, nexus_get_cpus), 179 180 /* pcib interface */ 181 #if defined(DEV_APIC) && defined(DEV_PCI) 182 DEVMETHOD(pcib_alloc_msi, nexus_alloc_msi), 183 DEVMETHOD(pcib_release_msi, nexus_release_msi), 184 DEVMETHOD(pcib_alloc_msix, nexus_alloc_msix), 185 DEVMETHOD(pcib_release_msix, nexus_release_msix), 186 DEVMETHOD(pcib_map_msi, nexus_map_msi), 187 #endif 188 189 { 0, 0 } 190 }; 191 192 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1); 193 static devclass_t nexus_devclass; 194 195 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); 196 197 static int 198 nexus_probe(device_t dev) 199 { 200 201 device_quiet(dev); /* suppress attach message for neatness */ 202 return (BUS_PROBE_GENERIC); 203 } 204 205 void 206 nexus_init_resources(void) 207 { 208 int irq; 209 210 /* 211 * XXX working notes: 212 * 213 * - IRQ resource creation should be moved to the PIC/APIC driver. 214 * - DRQ resource creation should be moved to the DMAC driver. 215 * - The above should be sorted to probe earlier than any child buses. 216 * 217 * - Leave I/O and memory creation here, as child probes may need them. 218 * (especially eg. ACPI) 219 */ 220 221 /* 222 * IRQ's are on the mainboard on old systems, but on the ISA part 223 * of PCI->ISA bridges. There would be multiple sets of IRQs on 224 * multi-ISA-bus systems. PCI interrupts are routed to the ISA 225 * component, so in a way, PCI can be a partial child of an ISA bus(!). 226 * APIC interrupts are global though. 227 */ 228 irq_rman.rm_start = 0; 229 irq_rman.rm_type = RMAN_ARRAY; 230 irq_rman.rm_descr = "Interrupt request lines"; 231 irq_rman.rm_end = num_io_irqs - 1; 232 if (rman_init(&irq_rman)) 233 panic("nexus_init_resources irq_rman"); 234 235 /* 236 * We search for regions of existing IRQs and add those to the IRQ 237 * resource manager. 238 */ 239 for (irq = 0; irq < num_io_irqs; irq++) 240 if (intr_lookup_source(irq) != NULL) 241 if (rman_manage_region(&irq_rman, irq, irq) != 0) 242 panic("nexus_init_resources irq_rman add"); 243 244 /* 245 * ISA DMA on PCI systems is implemented in the ISA part of each 246 * PCI->ISA bridge and the channels can be duplicated if there are 247 * multiple bridges. (eg: laptops with docking stations) 248 */ 249 drq_rman.rm_start = 0; 250 drq_rman.rm_end = 7; 251 drq_rman.rm_type = RMAN_ARRAY; 252 drq_rman.rm_descr = "DMA request lines"; 253 /* XXX drq 0 not available on some machines */ 254 if (rman_init(&drq_rman) 255 || rman_manage_region(&drq_rman, 256 drq_rman.rm_start, drq_rman.rm_end)) 257 panic("nexus_init_resources drq_rman"); 258 259 /* 260 * However, IO ports and Memory truely are global at this level, 261 * as are APIC interrupts (however many IO APICS there turn out 262 * to be on large systems..) 263 */ 264 port_rman.rm_start = 0; 265 port_rman.rm_end = 0xffff; 266 port_rman.rm_type = RMAN_ARRAY; 267 port_rman.rm_descr = "I/O ports"; 268 if (rman_init(&port_rman) 269 || rman_manage_region(&port_rman, 0, 0xffff)) 270 panic("nexus_init_resources port_rman"); 271 272 mem_rman.rm_start = 0; 273 mem_rman.rm_end = cpu_getmaxphyaddr(); 274 mem_rman.rm_type = RMAN_ARRAY; 275 mem_rman.rm_descr = "I/O memory addresses"; 276 if (rman_init(&mem_rman) 277 || rman_manage_region(&mem_rman, 0, mem_rman.rm_end)) 278 panic("nexus_init_resources mem_rman"); 279 } 280 281 static int 282 nexus_attach(device_t dev) 283 { 284 285 nexus_init_resources(); 286 bus_generic_probe(dev); 287 288 /* 289 * Explicitly add the legacy0 device here. Other platform 290 * types (such as ACPI), use their own nexus(4) subclass 291 * driver to override this routine and add their own root bus. 292 */ 293 if (BUS_ADD_CHILD(dev, 10, "legacy", 0) == NULL) 294 panic("legacy: could not attach"); 295 bus_generic_attach(dev); 296 return 0; 297 } 298 299 static int 300 nexus_print_all_resources(device_t dev) 301 { 302 struct nexus_device *ndev = DEVTONX(dev); 303 struct resource_list *rl = &ndev->nx_resources; 304 int retval = 0; 305 306 if (STAILQ_FIRST(rl)) 307 retval += printf(" at"); 308 309 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx"); 310 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx"); 311 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 312 313 return retval; 314 } 315 316 static int 317 nexus_print_child(device_t bus, device_t child) 318 { 319 int retval = 0; 320 321 retval += bus_print_child_header(bus, child); 322 retval += nexus_print_all_resources(child); 323 if (device_get_flags(child)) 324 retval += printf(" flags %#x", device_get_flags(child)); 325 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */ 326 327 return (retval); 328 } 329 330 static device_t 331 nexus_add_child(device_t bus, u_int order, const char *name, int unit) 332 { 333 device_t child; 334 struct nexus_device *ndev; 335 336 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO); 337 if (!ndev) 338 return(0); 339 resource_list_init(&ndev->nx_resources); 340 341 child = device_add_child_ordered(bus, order, name, unit); 342 343 /* should we free this in nexus_child_detached? */ 344 device_set_ivars(child, ndev); 345 346 return(child); 347 } 348 349 static struct rman * 350 nexus_rman(int type) 351 { 352 switch (type) { 353 case SYS_RES_IRQ: 354 return (&irq_rman); 355 case SYS_RES_DRQ: 356 return (&drq_rman); 357 case SYS_RES_IOPORT: 358 return (&port_rman); 359 case SYS_RES_MEMORY: 360 return (&mem_rman); 361 default: 362 return (NULL); 363 } 364 } 365 366 /* 367 * Allocate a resource on behalf of child. NB: child is usually going to be a 368 * child of one of our descendants, not a direct child of nexus0. 369 * (Exceptions include npx.) 370 */ 371 static struct resource * 372 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 373 rman_res_t start, rman_res_t end, rman_res_t count, 374 u_int flags) 375 { 376 struct nexus_device *ndev = DEVTONX(child); 377 struct resource *rv; 378 struct resource_list_entry *rle; 379 struct rman *rm; 380 int needactivate = flags & RF_ACTIVE; 381 382 /* 383 * If this is an allocation of the "default" range for a given 384 * RID, and we know what the resources for this device are 385 * (ie. they aren't maintained by a child bus), then work out 386 * the start/end values. 387 */ 388 if (RMAN_IS_DEFAULT_RANGE(start, end) && (count == 1)) { 389 if (device_get_parent(child) != bus || ndev == NULL) 390 return(NULL); 391 rle = resource_list_find(&ndev->nx_resources, type, *rid); 392 if (rle == NULL) 393 return(NULL); 394 start = rle->start; 395 end = rle->end; 396 count = rle->count; 397 } 398 399 flags &= ~RF_ACTIVE; 400 rm = nexus_rman(type); 401 if (rm == NULL) 402 return (NULL); 403 404 rv = rman_reserve_resource(rm, start, end, count, flags, child); 405 if (rv == NULL) 406 return 0; 407 rman_set_rid(rv, *rid); 408 409 if (needactivate) { 410 if (bus_activate_resource(child, type, *rid, rv)) { 411 rman_release_resource(rv); 412 return 0; 413 } 414 } 415 416 return rv; 417 } 418 419 static int 420 nexus_adjust_resource(device_t bus, device_t child, int type, 421 struct resource *r, rman_res_t start, rman_res_t end) 422 { 423 struct rman *rm; 424 425 rm = nexus_rman(type); 426 if (rm == NULL) 427 return (ENXIO); 428 if (!rman_is_region_manager(r, rm)) 429 return (EINVAL); 430 return (rman_adjust_resource(r, start, end)); 431 } 432 433 static int 434 nexus_activate_resource(device_t bus, device_t child, int type, int rid, 435 struct resource *r) 436 { 437 struct resource_map map; 438 int error; 439 440 error = rman_activate_resource(r); 441 if (error != 0) 442 return (error); 443 444 if (!(rman_get_flags(r) & RF_UNMAPPED) && 445 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 446 error = nexus_map_resource(bus, child, type, r, NULL, &map); 447 if (error) { 448 rman_deactivate_resource(r); 449 return (error); 450 } 451 452 rman_set_mapping(r,&map); 453 } 454 return (0); 455 } 456 457 static int 458 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 459 struct resource *r) 460 { 461 struct resource_map map; 462 int error; 463 464 error = rman_deactivate_resource(r); 465 if (error) 466 return (error); 467 468 if (!(rman_get_flags(r) & RF_UNMAPPED) && 469 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 470 rman_get_mapping(r, &map); 471 nexus_unmap_resource(bus, child, type, r, &map); 472 } 473 return (0); 474 } 475 476 static int 477 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r, 478 struct resource_map_request *argsp, struct resource_map *map) 479 { 480 struct resource_map_request args; 481 rman_res_t end, length, start; 482 483 /* Resources must be active to be mapped. */ 484 if (!(rman_get_flags(r) & RF_ACTIVE)) 485 return (ENXIO); 486 487 /* Mappings are only supported on I/O and memory resources. */ 488 switch (type) { 489 case SYS_RES_IOPORT: 490 case SYS_RES_MEMORY: 491 break; 492 default: 493 return (EINVAL); 494 } 495 496 resource_init_map_request(&args); 497 if (argsp != NULL) 498 bcopy(argsp, &args, imin(argsp->size, args.size)); 499 start = rman_get_start(r) + args.offset; 500 if (args.length == 0) 501 length = rman_get_size(r); 502 else 503 length = args.length; 504 end = start + length - 1; 505 if (start > rman_get_end(r) || start < rman_get_start(r)) 506 return (EINVAL); 507 if (end > rman_get_end(r) || end < start) 508 return (EINVAL); 509 510 /* 511 * If this is a memory resource, map it into the kernel. 512 */ 513 switch (type) { 514 case SYS_RES_IOPORT: 515 map->r_bushandle = start; 516 map->r_bustag = X86_BUS_SPACE_IO; 517 map->r_size = length; 518 map->r_vaddr = NULL; 519 break; 520 case SYS_RES_MEMORY: 521 map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr); 522 map->r_bustag = X86_BUS_SPACE_MEM; 523 map->r_size = length; 524 525 /* 526 * The handle is the virtual address. 527 */ 528 map->r_bushandle = (bus_space_handle_t)map->r_vaddr; 529 break; 530 } 531 return (0); 532 } 533 534 static int 535 nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r, 536 struct resource_map *map) 537 { 538 539 /* 540 * If this is a memory resource, unmap it. 541 */ 542 switch (type) { 543 case SYS_RES_MEMORY: 544 pmap_unmapdev((vm_offset_t)map->r_vaddr, map->r_size); 545 /* FALLTHROUGH */ 546 case SYS_RES_IOPORT: 547 break; 548 default: 549 return (EINVAL); 550 } 551 return (0); 552 } 553 554 static int 555 nexus_release_resource(device_t bus, device_t child, int type, int rid, 556 struct resource *r) 557 { 558 559 if (rman_get_flags(r) & RF_ACTIVE) { 560 int error = bus_deactivate_resource(child, type, rid, r); 561 if (error) 562 return error; 563 } 564 return (rman_release_resource(r)); 565 } 566 567 /* 568 * Currently this uses the really grody interface from kern/kern_intr.c 569 * (which really doesn't belong in kern/anything.c). Eventually, all of 570 * the code in kern_intr.c and machdep_intr.c should get moved here, since 571 * this is going to be the official interface. 572 */ 573 static int 574 nexus_setup_intr(device_t bus, device_t child, struct resource *irq, 575 int flags, driver_filter_t filter, void (*ihand)(void *), 576 void *arg, void **cookiep) 577 { 578 int error, domain; 579 580 /* somebody tried to setup an irq that failed to allocate! */ 581 if (irq == NULL) 582 panic("nexus_setup_intr: NULL irq resource!"); 583 584 *cookiep = NULL; 585 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0) 586 flags |= INTR_EXCL; 587 588 /* 589 * We depend here on rman_activate_resource() being idempotent. 590 */ 591 error = rman_activate_resource(irq); 592 if (error) 593 return (error); 594 if (bus_get_domain(child, &domain) != 0) 595 domain = 0; 596 597 error = intr_add_handler(device_get_nameunit(child), 598 rman_get_start(irq), filter, ihand, arg, flags, cookiep, domain); 599 if (error == 0) 600 rman_set_irq_cookie(irq, *cookiep); 601 602 return (error); 603 } 604 605 static int 606 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih) 607 { 608 int error; 609 610 error = intr_remove_handler(ih); 611 if (error == 0) 612 rman_set_irq_cookie(r, NULL); 613 return (error); 614 } 615 616 static int 617 nexus_suspend_intr(device_t dev, device_t child, struct resource *irq) 618 { 619 return (intr_event_suspend_handler(rman_get_irq_cookie(irq))); 620 } 621 622 static int 623 nexus_resume_intr(device_t dev, device_t child, struct resource *irq) 624 { 625 return (intr_event_resume_handler(rman_get_irq_cookie(irq))); 626 } 627 628 #ifdef SMP 629 static int 630 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu) 631 { 632 return (intr_bind(rman_get_start(irq), cpu)); 633 } 634 #endif 635 636 static int 637 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 638 enum intr_polarity pol) 639 { 640 return (intr_config_intr(irq, trig, pol)); 641 } 642 643 static int 644 nexus_describe_intr(device_t dev, device_t child, struct resource *irq, 645 void *cookie, const char *descr) 646 { 647 648 return (intr_describe(rman_get_start(irq), cookie, descr)); 649 } 650 651 static struct resource_list * 652 nexus_get_reslist(device_t dev, device_t child) 653 { 654 struct nexus_device *ndev = DEVTONX(child); 655 656 return (&ndev->nx_resources); 657 } 658 659 static int 660 nexus_set_resource(device_t dev, device_t child, int type, int rid, 661 rman_res_t start, rman_res_t count) 662 { 663 struct nexus_device *ndev = DEVTONX(child); 664 struct resource_list *rl = &ndev->nx_resources; 665 666 /* XXX this should return a success/failure indicator */ 667 resource_list_add(rl, type, rid, start, start + count - 1, count); 668 return(0); 669 } 670 671 static int 672 nexus_get_resource(device_t dev, device_t child, int type, int rid, 673 rman_res_t *startp, rman_res_t *countp) 674 { 675 struct nexus_device *ndev = DEVTONX(child); 676 struct resource_list *rl = &ndev->nx_resources; 677 struct resource_list_entry *rle; 678 679 rle = resource_list_find(rl, type, rid); 680 if (!rle) 681 return(ENOENT); 682 if (startp) 683 *startp = rle->start; 684 if (countp) 685 *countp = rle->count; 686 return(0); 687 } 688 689 static void 690 nexus_delete_resource(device_t dev, device_t child, int type, int rid) 691 { 692 struct nexus_device *ndev = DEVTONX(child); 693 struct resource_list *rl = &ndev->nx_resources; 694 695 resource_list_delete(rl, type, rid); 696 } 697 698 static int 699 nexus_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize, 700 cpuset_t *cpuset) 701 { 702 703 switch (op) { 704 #ifdef SMP 705 case INTR_CPUS: 706 if (setsize != sizeof(cpuset_t)) 707 return (EINVAL); 708 *cpuset = intr_cpus; 709 return (0); 710 #endif 711 default: 712 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset)); 713 } 714 } 715 716 /* Called from the MSI code to add new IRQs to the IRQ rman. */ 717 void 718 nexus_add_irq(u_long irq) 719 { 720 721 if (rman_manage_region(&irq_rman, irq, irq) != 0) 722 panic("%s: failed", __func__); 723 } 724 725 #if defined(DEV_APIC) && defined(DEV_PCI) 726 static int 727 nexus_alloc_msix(device_t pcib, device_t dev, int *irq) 728 { 729 730 return (msix_alloc(dev, irq)); 731 } 732 733 static int 734 nexus_release_msix(device_t pcib, device_t dev, int irq) 735 { 736 737 return (msix_release(irq)); 738 } 739 740 static int 741 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 742 { 743 744 return (msi_alloc(dev, count, maxcount, irqs)); 745 } 746 747 static int 748 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs) 749 { 750 751 return (msi_release(irqs, count)); 752 } 753 754 static int 755 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data) 756 { 757 758 return (msi_map(irq, addr, data)); 759 } 760 #endif /* DEV_APIC && DEV_PCI */ 761 762 /* Placeholder for system RAM. */ 763 static void 764 ram_identify(driver_t *driver, device_t parent) 765 { 766 767 if (resource_disabled("ram", 0)) 768 return; 769 if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL) 770 panic("ram_identify"); 771 } 772 773 static int 774 ram_probe(device_t dev) 775 { 776 777 device_quiet(dev); 778 device_set_desc(dev, "System RAM"); 779 return (0); 780 } 781 782 static int 783 ram_attach(device_t dev) 784 { 785 struct bios_smap *smapbase, *smap, *smapend; 786 struct resource *res; 787 rman_res_t length; 788 vm_paddr_t *p; 789 caddr_t kmdp; 790 uint32_t smapsize; 791 int error, rid; 792 793 /* Retrieve the system memory map from the loader. */ 794 kmdp = preload_search_by_type("elf kernel"); 795 if (kmdp == NULL) 796 kmdp = preload_search_by_type(ELF_KERN_STR); 797 smapbase = (struct bios_smap *)preload_search_info(kmdp, 798 MODINFO_METADATA | MODINFOMD_SMAP); 799 if (smapbase != NULL) { 800 smapsize = *((u_int32_t *)smapbase - 1); 801 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize); 802 803 rid = 0; 804 for (smap = smapbase; smap < smapend; smap++) { 805 if (smap->type != SMAP_TYPE_MEMORY || 806 smap->length == 0) 807 continue; 808 if (smap->base > mem_rman.rm_end) 809 continue; 810 length = smap->base + smap->length > mem_rman.rm_end ? 811 mem_rman.rm_end - smap->base : smap->length; 812 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, 813 smap->base, length); 814 if (error) 815 panic( 816 "ram_attach: resource %d failed set with %d", 817 rid, error); 818 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 819 0); 820 if (res == NULL) 821 panic("ram_attach: resource %d failed to attach", 822 rid); 823 rid++; 824 } 825 return (0); 826 } 827 828 /* 829 * If the system map is not available, fall back to using 830 * dump_avail[]. We use the dump_avail[] array rather than 831 * phys_avail[] for the memory map as phys_avail[] contains 832 * holes for kernel memory, page 0, the message buffer, and 833 * the dcons buffer. We test the end address in the loop 834 * instead of the start since the start address for the first 835 * segment is 0. 836 */ 837 for (rid = 0, p = dump_avail; p[1] != 0; rid++, p += 2) { 838 if (p[0] > mem_rman.rm_end) 839 break; 840 length = (p[1] > mem_rman.rm_end ? mem_rman.rm_end : p[1]) - 841 p[0]; 842 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, p[0], 843 length); 844 if (error) 845 panic("ram_attach: resource %d failed set with %d", rid, 846 error); 847 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0); 848 if (res == NULL) 849 panic("ram_attach: resource %d failed to attach", rid); 850 } 851 return (0); 852 } 853 854 static device_method_t ram_methods[] = { 855 /* Device interface */ 856 DEVMETHOD(device_identify, ram_identify), 857 DEVMETHOD(device_probe, ram_probe), 858 DEVMETHOD(device_attach, ram_attach), 859 { 0, 0 } 860 }; 861 862 static driver_t ram_driver = { 863 "ram", 864 ram_methods, 865 1, /* no softc */ 866 }; 867 868 static devclass_t ram_devclass; 869 870 DRIVER_MODULE(ram, nexus, ram_driver, ram_devclass, 0, 0); 871 872 #ifdef DEV_ISA 873 /* 874 * Placeholder which claims PnP 'devices' which describe system 875 * resources. 876 */ 877 static struct isa_pnp_id sysresource_ids[] = { 878 { 0x010cd041 /* PNP0c01 */, "System Memory" }, 879 { 0x020cd041 /* PNP0c02 */, "System Resource" }, 880 { 0 } 881 }; 882 883 static int 884 sysresource_probe(device_t dev) 885 { 886 int result; 887 888 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) { 889 device_quiet(dev); 890 } 891 return(result); 892 } 893 894 static int 895 sysresource_attach(device_t dev) 896 { 897 return(0); 898 } 899 900 static device_method_t sysresource_methods[] = { 901 /* Device interface */ 902 DEVMETHOD(device_probe, sysresource_probe), 903 DEVMETHOD(device_attach, sysresource_attach), 904 DEVMETHOD(device_detach, bus_generic_detach), 905 DEVMETHOD(device_shutdown, bus_generic_shutdown), 906 DEVMETHOD(device_suspend, bus_generic_suspend), 907 DEVMETHOD(device_resume, bus_generic_resume), 908 { 0, 0 } 909 }; 910 911 static driver_t sysresource_driver = { 912 "sysresource", 913 sysresource_methods, 914 1, /* no softc */ 915 }; 916 917 static devclass_t sysresource_devclass; 918 919 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0); 920 ISA_PNP_INFO(sysresource_ids); 921 #endif /* DEV_ISA */ 922