1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
  23  * Copyright (c) 2012 Gary Mills
  24  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
  25  */
  26 
  27 /*
  28  *      ISA bus nexus driver
  29  */
  30 
  31 #include <sys/types.h>
  32 #include <sys/cmn_err.h>
  33 #include <sys/conf.h>
  34 #include <sys/modctl.h>
  35 #include <sys/autoconf.h>
  36 #include <sys/errno.h>
  37 #include <sys/debug.h>
  38 #include <sys/kmem.h>
  39 #include <sys/psm.h>
  40 #include <sys/ddidmareq.h>
  41 #include <sys/ddi_impldefs.h>
  42 #include <sys/ddi_subrdefs.h>
  43 #include <sys/dma_engine.h>
  44 #include <sys/ddi.h>
  45 #include <sys/sunddi.h>
  46 #include <sys/sunndi.h>
  47 #include <sys/acpi/acpi_enum.h>
  48 #include <sys/mach_intr.h>
  49 #include <sys/pci.h>
  50 #include <sys/note.h>
  51 #include <sys/boot_console.h>
  52 #include <sys/apic.h>
  53 #if defined(__xpv)
  54 #include <sys/hypervisor.h>
  55 #include <sys/evtchn_impl.h>
  56 
  57 extern int console_hypervisor_dev_type(int *);
  58 #endif
  59 
  60 
  61 extern int pseudo_isa;
  62 extern int isa_resource_setup(void);
  63 extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
  64     psm_intr_op_t, int *);
  65 extern void pci_register_isa_resources(int, uint32_t, uint32_t);
  66 static void isa_enumerate(int);
  67 static void enumerate_BIOS_serial(dev_info_t *);
  68 static void adjust_prtsz(dev_info_t *isa_dip);
  69 static void isa_create_ranges_prop(dev_info_t *);
  70 
  71 #define USED_RESOURCES  "used-resources"
  72 
  73 /*
  74  * The following typedef is used to represent an entry in the "ranges"
  75  * property of a pci-isa bridge device node.
  76  */
  77 typedef struct {
  78         uint32_t child_high;
  79         uint32_t child_low;
  80         uint32_t parent_high;
  81         uint32_t parent_mid;
  82         uint32_t parent_low;
  83         uint32_t size;
  84 } pib_ranges_t;
  85 
  86 typedef struct {
  87         uint32_t base;
  88         uint32_t len;
  89 } used_ranges_t;
  90 
  91 #define USED_CELL_SIZE  2       /* 1 byte addr, 1 byte size */
  92 #define ISA_ADDR_IO     1       /* IO address space */
  93 #define ISA_ADDR_MEM    0       /* memory adress space */
  94 
  95 /*
  96  * #define ISA_DEBUG 1
  97  */
  98 
  99 #define num_BIOS_serial 4       /* number of BIOS serial ports to look at */
 100 #define min_BIOS_serial 2       /* minimum number of BIOS serial ports */
 101 #define COM_ISR         2       /* 16550 intr status register */
 102 #define COM_SCR         7       /* 16550 scratch register */
 103 
 104 /*
 105  * For serial ports not enumerated by ACPI, and parallel ports with
 106  * illegal size. Typically, a system can have as many as 4 serial
 107  * ports and 3 parallel ports.
 108  */
 109 #define MAX_EXTRA_RESOURCE      7
 110 static struct regspec isa_extra_resource[MAX_EXTRA_RESOURCE];
 111 static int isa_extra_count = 0;
 112 
 113 /* Register definitions for COM1 to COM4. */
 114 static struct regspec asy_regs[] = {
 115         {1, 0x3f8, 0x8},
 116         {1, 0x2f8, 0x8},
 117         {1, 0x3e8, 0x8},
 118         {1, 0x2e8, 0x8}
 119 };
 120 
 121 /* Serial port interrupt vectors for COM1 to COM4. */
 122 static int asy_intrs[] = {0x4, 0x3, 0x4, 0x3};
 123 /* Bitfield indicating which interrupts are overridden by eeprom config */
 124 static uchar_t asy_intr_override = 0;
 125 
 126 /*
 127  *      Local data
 128  */
 129 
 130 static ddi_dma_attr_t ISA_dma_attr = {
 131         DMA_ATTR_V0,
 132         (unsigned long long)0,
 133         (unsigned long long)0x00ffffff,
 134         0x0000ffff,
 135         1,
 136         1,
 137         1,
 138         (unsigned long long)0xffffffff,
 139         (unsigned long long)0x0000ffff,
 140         1,
 141         1,
 142         0
 143 };
 144 
 145 
 146 /*
 147  * Config information
 148  */
 149 
 150 static int
 151 isa_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
 152     off_t offset, off_t len, caddr_t *vaddrp);
 153 
 154 static int
 155 isa_dma_allochdl(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
 156     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *);
 157 
 158 static int
 159 isa_dma_mctl(dev_info_t *, dev_info_t *, ddi_dma_handle_t, enum ddi_dma_ctlops,
 160     off_t *, size_t *, caddr_t *, uint_t);
 161 
 162 static int
 163 isa_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
 164 
 165 static int
 166 isa_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
 167     ddi_intr_handle_impl_t *hdlp, void *result);
 168 static int isa_alloc_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *, void *);
 169 static int isa_free_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *);
 170 
 171 struct bus_ops isa_bus_ops = {
 172         BUSO_REV,
 173         isa_bus_map,
 174         NULL,
 175         NULL,
 176         NULL,
 177         i_ddi_map_fault,
 178         NULL,
 179         isa_dma_allochdl,
 180         ddi_dma_freehdl,
 181         ddi_dma_bindhdl,
 182         ddi_dma_unbindhdl,
 183         ddi_dma_flush,
 184         ddi_dma_win,
 185         isa_dma_mctl,
 186         isa_ctlops,
 187         ddi_bus_prop_op,
 188         NULL,           /* (*bus_get_eventcookie)();    */
 189         NULL,           /* (*bus_add_eventcall)();      */
 190         NULL,           /* (*bus_remove_eventcall)();   */
 191         NULL,           /* (*bus_post_event)();         */
 192         NULL,           /* (*bus_intr_ctl)(); */
 193         NULL,           /* (*bus_config)(); */
 194         NULL,           /* (*bus_unconfig)(); */
 195         NULL,           /* (*bus_fm_init)(); */
 196         NULL,           /* (*bus_fm_fini)(); */
 197         NULL,           /* (*bus_fm_access_enter)(); */
 198         NULL,           /* (*bus_fm_access_exit)(); */
 199         NULL,           /* (*bus_power)(); */
 200         isa_intr_ops    /* (*bus_intr_op)(); */
 201 };
 202 
 203 
 204 static int isa_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
 205 
 206 /*
 207  * Internal isa ctlops support routines
 208  */
 209 static int isa_initchild(dev_info_t *child);
 210 
 211 struct dev_ops isa_ops = {
 212         DEVO_REV,               /* devo_rev, */
 213         0,                      /* refcnt  */
 214         ddi_no_info,            /* info */
 215         nulldev,                /* identify */
 216         nulldev,                /* probe */
 217         isa_attach,             /* attach */
 218         nulldev,                /* detach */
 219         nodev,                  /* reset */
 220         (struct cb_ops *)0,     /* driver operations */
 221         &isa_bus_ops,               /* bus operations */
 222         NULL,                   /* power */
 223         ddi_quiesce_not_needed,         /* quiesce */
 224 };
 225 
 226 /*
 227  * Module linkage information for the kernel.
 228  */
 229 
 230 static struct modldrv modldrv = {
 231         &mod_driverops, /* Type of module.  This is ISA bus driver */
 232         "isa nexus driver for 'ISA'",
 233         &isa_ops,   /* driver ops */
 234 };
 235 
 236 static struct modlinkage modlinkage = {
 237         MODREV_1,
 238         { &modldrv, NULL }
 239 };
 240 
 241 int
 242 _init(void)
 243 {
 244         int     err;
 245         char tty_irq_param[9] = "ttyX-irq";
 246         char *tty_irq;
 247         int i;
 248 
 249         if ((err = mod_install(&modlinkage)) != 0)
 250                 return (err);
 251 
 252         /* Check if any tty irqs are overridden by eeprom config */
 253         for (i = 0; i < num_BIOS_serial; i++) {
 254                 tty_irq_param[3] = 'a' + i;
 255                 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
 256                     DDI_PROP_DONTPASS, tty_irq_param, &tty_irq)
 257                     == DDI_PROP_SUCCESS) {
 258                         long data;
 259 
 260                         if (ddi_strtol(tty_irq, NULL, 0, &data) == 0) {
 261                                 asy_intrs[i] = (int)data;
 262                                 asy_intr_override |= 1<<i;
 263                         }
 264 
 265                         ddi_prop_free(tty_irq);
 266                 }
 267         }
 268 
 269         impl_bus_add_probe(isa_enumerate);
 270         return (0);
 271 }
 272 
 273 int
 274 _fini(void)
 275 {
 276         int     err;
 277 
 278         impl_bus_delete_probe(isa_enumerate);
 279 
 280         if ((err = mod_remove(&modlinkage)) != 0)
 281                 return (err);
 282 
 283         return (0);
 284 }
 285 
 286 int
 287 _info(struct modinfo *modinfop)
 288 {
 289         return (mod_info(&modlinkage, modinfop));
 290 }
 291 
 292 static int
 293 isa_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
 294 {
 295         int rval;
 296 
 297 #if defined(__xpv)
 298         /*
 299          * don't allow isa to attach in domU. this can happen if someone sets
 300          * the console wrong, etc. ISA devices assume the H/W is there and
 301          * will cause the domU to panic.
 302          */
 303         if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
 304                 return (DDI_FAILURE);
 305         }
 306 #endif
 307 
 308         switch (cmd) {
 309         case DDI_ATTACH:
 310                 break;
 311         case DDI_RESUME:
 312                 return (DDI_SUCCESS);
 313         default:
 314                 return (DDI_FAILURE);
 315         }
 316 
 317         if ((rval = i_dmae_init(devi)) == DDI_SUCCESS)
 318                 ddi_report_dev(devi);
 319 
 320         return (rval);
 321 }
 322 
 323 #define SET_RNGS(rng_p, used_p, ctyp, ptyp) do {                        \
 324                 (rng_p)->child_high = (ctyp);                                \
 325                 (rng_p)->child_low = (rng_p)->parent_low = (used_p)->base; \
 326                 (rng_p)->parent_high = (ptyp);                               \
 327                 (rng_p)->parent_mid = 0;                             \
 328                 (rng_p)->size = (used_p)->len;                            \
 329                 _NOTE(CONSTCOND) } while (0)
 330 static uint_t
 331 isa_used_to_ranges(int ctype, int *array, uint_t size, pib_ranges_t *ranges)
 332 {
 333         used_ranges_t *used_p;
 334         pib_ranges_t *rng_p = ranges;
 335         uint_t  i, ptype;
 336 
 337         ptype = (ctype == ISA_ADDR_IO) ? PCI_ADDR_IO : PCI_ADDR_MEM32;
 338         ptype |= PCI_REG_REL_M;
 339         size /= USED_CELL_SIZE;
 340         used_p = (used_ranges_t *)array;
 341         SET_RNGS(rng_p, used_p, ctype, ptype);
 342         for (i = 1, used_p++; i < size; i++, used_p++) {
 343                 /* merge ranges record if applicable */
 344                 if (rng_p->child_low + rng_p->size == used_p->base)
 345                         rng_p->size += used_p->len;
 346                 else {
 347                         rng_p++;
 348                         SET_RNGS(rng_p, used_p, ctype, ptype);
 349                 }
 350         }
 351         return (rng_p - ranges + 1);
 352 }
 353 
 354 void
 355 isa_remove_res_from_pci(int type, int *array, uint_t size)
 356 {
 357         int i;
 358         used_ranges_t *used_p;
 359 
 360         size /= USED_CELL_SIZE;
 361         used_p = (used_ranges_t *)array;
 362         for (i = 0; i < size; i++, used_p++)
 363                 pci_register_isa_resources(type, used_p->base, used_p->len);
 364 }
 365 
 366 static void
 367 isa_create_ranges_prop(dev_info_t *dip)
 368 {
 369         dev_info_t *used;
 370         int *ioarray, *memarray, status;
 371         uint_t nio = 0, nmem = 0, nrng = 0, n;
 372         pib_ranges_t *ranges;
 373 
 374         used = ddi_find_devinfo(USED_RESOURCES, -1, 0);
 375         if (used == NULL) {
 376                 cmn_err(CE_WARN, "Failed to find used-resources <%s>\n",
 377                     ddi_get_name(dip));
 378                 return;
 379         }
 380         status = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, used,
 381             DDI_PROP_DONTPASS, "io-space", &ioarray, &nio);
 382         if (status != DDI_PROP_SUCCESS && status != DDI_PROP_NOT_FOUND) {
 383                 cmn_err(CE_WARN, "io-space property failure for %s (%x)\n",
 384                     ddi_get_name(used), status);
 385                 return;
 386         }
 387         status = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, used,
 388             DDI_PROP_DONTPASS, "device-memory", &memarray, &nmem);
 389         if (status != DDI_PROP_SUCCESS && status != DDI_PROP_NOT_FOUND) {
 390                 cmn_err(CE_WARN, "device-memory property failure for %s (%x)\n",
 391                     ddi_get_name(used), status);
 392                 return;
 393         }
 394         n = (nio + nmem) / USED_CELL_SIZE;
 395         ranges =  (pib_ranges_t *)kmem_zalloc(sizeof (pib_ranges_t) * n,
 396             KM_SLEEP);
 397 
 398         if (nio != 0) {
 399                 nrng = isa_used_to_ranges(ISA_ADDR_IO, ioarray, nio, ranges);
 400                 isa_remove_res_from_pci(ISA_ADDR_IO, ioarray, nio);
 401                 ddi_prop_free(ioarray);
 402         }
 403         if (nmem != 0) {
 404                 nrng += isa_used_to_ranges(ISA_ADDR_MEM, memarray, nmem,
 405                     ranges + nrng);
 406                 isa_remove_res_from_pci(ISA_ADDR_MEM, memarray, nmem);
 407                 ddi_prop_free(memarray);
 408         }
 409 
 410         if (!pseudo_isa)
 411                 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
 412                     (int *)ranges, nrng * sizeof (pib_ranges_t) / sizeof (int));
 413         kmem_free(ranges, sizeof (pib_ranges_t) * n);
 414 }
 415 
 416 /*ARGSUSED*/
 417 static int
 418 isa_apply_range(dev_info_t *dip, struct regspec *isa_reg_p,
 419     pci_regspec_t *pci_reg_p)
 420 {
 421         pib_ranges_t *ranges, *rng_p;
 422         int len, i, offset, nrange;
 423 
 424         if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
 425             "ranges", (caddr_t)&ranges, &len) != DDI_SUCCESS) {
 426                 cmn_err(CE_WARN, "Can't get %s ranges property",
 427                     ddi_get_name(dip));
 428                 return (DDI_ME_REGSPEC_RANGE);
 429         }
 430         nrange = len / sizeof (pib_ranges_t);
 431         rng_p = ranges;
 432         for (i = 0; i < nrange; i++, rng_p++) {
 433                 /* Check for correct space */
 434                 if (isa_reg_p->regspec_bustype != rng_p->child_high)
 435                         continue;
 436 
 437                 /* Detect whether request entirely fits within a range */
 438                 if (isa_reg_p->regspec_addr < rng_p->child_low)
 439                         continue;
 440                 if ((isa_reg_p->regspec_addr + isa_reg_p->regspec_size - 1) >
 441                     (rng_p->child_low + rng_p->size - 1))
 442                         continue;
 443 
 444                 offset = isa_reg_p->regspec_addr - rng_p->child_low;
 445 
 446                 pci_reg_p->pci_phys_hi = rng_p->parent_high;
 447                 pci_reg_p->pci_phys_mid = 0;
 448                 pci_reg_p->pci_phys_low = rng_p->parent_low + offset;
 449                 pci_reg_p->pci_size_hi = 0;
 450                 pci_reg_p->pci_size_low = isa_reg_p->regspec_size;
 451 
 452                 break;
 453         }
 454         kmem_free(ranges, len);
 455 
 456         if (i < nrange)
 457                 return (DDI_SUCCESS);
 458 
 459         /*
 460          * Check extra resource range specially for serial and parallel
 461          * devices, which are treated differently from all other ISA
 462          * devices. On some machines, serial ports are not enumerated
 463          * by ACPI but by BIOS, with io base addresses noted in legacy
 464          * BIOS data area. Parallel port on some machines comes with
 465          * illegal size.
 466          */
 467         if (isa_reg_p->regspec_bustype != ISA_ADDR_IO) {
 468                 cmn_err(CE_WARN, "Bus type not ISA I/O\n");
 469                 return (DDI_ME_REGSPEC_RANGE);
 470         }
 471 
 472         for (i = 0; i < isa_extra_count; i++) {
 473                 struct regspec *reg_p = &isa_extra_resource[i];
 474 
 475                 if (isa_reg_p->regspec_addr < reg_p->regspec_addr)
 476                         continue;
 477                 if ((isa_reg_p->regspec_addr + isa_reg_p->regspec_size) >
 478                     (reg_p->regspec_addr + reg_p->regspec_size))
 479                         continue;
 480 
 481                 pci_reg_p->pci_phys_hi = PCI_ADDR_IO | PCI_REG_REL_M;
 482                 pci_reg_p->pci_phys_mid = 0;
 483                 pci_reg_p->pci_phys_low = isa_reg_p->regspec_addr;
 484                 pci_reg_p->pci_size_hi = 0;
 485                 pci_reg_p->pci_size_low = isa_reg_p->regspec_size;
 486                 break;
 487         }
 488         if (i < isa_extra_count)
 489                 return (DDI_SUCCESS);
 490 
 491         cmn_err(CE_WARN, "isa_apply_range: Out of range base <0x%x>, size <%d>",
 492             isa_reg_p->regspec_addr, isa_reg_p->regspec_size);
 493         return (DDI_ME_REGSPEC_RANGE);
 494 }
 495 
 496 static int
 497 isa_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
 498     off_t offset, off_t len, caddr_t *vaddrp)
 499 {
 500         struct regspec tmp_reg, *rp;
 501         pci_regspec_t vreg;
 502         ddi_map_req_t mr = *mp;         /* Get private copy of request */
 503         int error;
 504 
 505         if (pseudo_isa)
 506                 return (i_ddi_bus_map(dip, rdip, mp, offset, len, vaddrp));
 507 
 508         mp = &mr;
 509 
 510         /*
 511          * First, if given an rnumber, convert it to a regspec...
 512          */
 513         if (mp->map_type == DDI_MT_RNUMBER)  {
 514 
 515                 int rnumber = mp->map_obj.rnumber;
 516 
 517                 rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
 518                 if (rp == (struct regspec *)0)
 519                         return (DDI_ME_RNUMBER_RANGE);
 520 
 521                 /*
 522                  * Convert the given ddi_map_req_t from rnumber to regspec...
 523                  */
 524                 mp->map_type = DDI_MT_REGSPEC;
 525                 mp->map_obj.rp = rp;
 526         }
 527 
 528         /*
 529          * Adjust offset and length correspnding to called values...
 530          * XXX: A non-zero length means override the one in the regspec.
 531          * XXX: (Regardless of what's in the parent's range)
 532          */
 533 
 534         tmp_reg = *(mp->map_obj.rp);         /* Preserve underlying data */
 535         rp = mp->map_obj.rp = &tmp_reg;          /* Use tmp_reg in request */
 536 
 537         rp->regspec_addr += (uint_t)offset;
 538         if (len != 0)
 539                 rp->regspec_size = (uint_t)len;
 540 
 541         if ((error = isa_apply_range(dip, rp, &vreg)) != 0)
 542                 return (error);
 543         mp->map_obj.rp = (struct regspec *)&vreg;
 544 
 545         /*
 546          * Call my parents bus_map function with modified values...
 547          */
 548 
 549         return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
 550 }
 551 
 552 static int
 553 isa_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *dma_attr,
 554     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
 555 {
 556         ddi_dma_attr_merge(dma_attr, &ISA_dma_attr);
 557         return (ddi_dma_allochdl(dip, rdip, dma_attr, waitfp, arg, handlep));
 558 }
 559 
 560 static int
 561 isa_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
 562     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
 563     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
 564 {
 565         int rval;
 566         int arg = (int)(uintptr_t)objp;
 567 
 568         switch (request) {
 569 
 570         case DDI_DMA_E_PROG:
 571                 return (i_dmae_prog(rdip, (struct ddi_dmae_req *)offp,
 572                     (ddi_dma_cookie_t *)lenp, arg));
 573 
 574         case DDI_DMA_E_ACQUIRE:
 575                 return (i_dmae_acquire(rdip, arg, (int(*)(caddr_t))offp,
 576                     (caddr_t)lenp));
 577 
 578         case DDI_DMA_E_FREE:
 579                 return (i_dmae_free(rdip, arg));
 580 
 581         case DDI_DMA_E_STOP:
 582                 i_dmae_stop(rdip, arg);
 583                 return (DDI_SUCCESS);
 584 
 585         case DDI_DMA_E_ENABLE:
 586                 i_dmae_enable(rdip, arg);
 587                 return (DDI_SUCCESS);
 588 
 589         case DDI_DMA_E_DISABLE:
 590                 i_dmae_disable(rdip, arg);
 591                 return (DDI_SUCCESS);
 592 
 593         case DDI_DMA_E_GETCNT:
 594                 i_dmae_get_chan_stat(rdip, arg, NULL, (int *)lenp);
 595                 return (DDI_SUCCESS);
 596 
 597         case DDI_DMA_E_SWSETUP:
 598                 return (i_dmae_swsetup(rdip, (struct ddi_dmae_req *)offp,
 599                     (ddi_dma_cookie_t *)lenp, arg));
 600 
 601         case DDI_DMA_E_SWSTART:
 602                 i_dmae_swstart(rdip, arg);
 603                 return (DDI_SUCCESS);
 604 
 605         case DDI_DMA_E_GETATTR:
 606                 bcopy(&ISA_dma_attr, objp, sizeof (ddi_dma_attr_t));
 607                 return (DDI_SUCCESS);
 608 
 609         case DDI_DMA_E_1STPTY:
 610                 {
 611                         struct ddi_dmae_req req1stpty =
 612                             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 613                         if (arg == 0) {
 614                                 req1stpty.der_command = DMAE_CMD_TRAN;
 615                                 req1stpty.der_trans = DMAE_TRANS_DMND;
 616                         } else {
 617                                 req1stpty.der_trans = DMAE_TRANS_CSCD;
 618                         }
 619                         return (i_dmae_prog(rdip, &req1stpty, NULL, arg));
 620                 }
 621 
 622         default:
 623                 /*
 624                  * We pass to rootnex, but it turns out that rootnex will just
 625                  * return failure, as we don't use ddi_dma_mctl() except
 626                  * for DMA engine (ISA) and DVMA (SPARC).  Arguably we could
 627                  * just return an error direclty here, instead.
 628                  */
 629                 rval = ddi_dma_mctl(dip, rdip, handle, request, offp,
 630                     lenp, objp, flags);
 631         }
 632         return (rval);
 633 }
 634 
 635 /*
 636  * Check if driver should be treated as an old pre 2.6 driver
 637  */
 638 static int
 639 old_driver(dev_info_t *dip)
 640 {
 641         extern int ignore_hardware_nodes;       /* force flag from ddi_impl.c */
 642 
 643         if (ndi_dev_is_persistent_node(dip)) {
 644                 if (ignore_hardware_nodes)
 645                         return (1);
 646                 if (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
 647                     "ignore-hardware-nodes", -1) != -1)
 648                         return (1);
 649         }
 650         return (0);
 651 }
 652 
 653 typedef struct {
 654         uint32_t phys_hi;
 655         uint32_t phys_lo;
 656         uint32_t size;
 657 } isa_regs_t;
 658 
 659 /*
 660  * Return non-zero if device in tree is a PnP isa device
 661  */
 662 static int
 663 is_pnpisa(dev_info_t *dip)
 664 {
 665         isa_regs_t *isa_regs;
 666         int proplen, pnpisa;
 667 
 668         if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
 669             (caddr_t)&isa_regs, &proplen) != DDI_PROP_SUCCESS) {
 670                 return (0);
 671         }
 672         pnpisa = isa_regs[0].phys_hi & 0x80000000;
 673         /*
 674          * free the memory allocated by ddi_getlongprop().
 675          */
 676         kmem_free(isa_regs, proplen);
 677         if (pnpisa)
 678                 return (1);
 679         else
 680                 return (0);
 681 }
 682 
 683 /*ARGSUSED*/
 684 static int
 685 isa_ctlops(dev_info_t *dip, dev_info_t *rdip,
 686         ddi_ctl_enum_t ctlop, void *arg, void *result)
 687 {
 688         int rn;
 689         struct ddi_parent_private_data *pdp;
 690 
 691         switch (ctlop) {
 692         case DDI_CTLOPS_REPORTDEV:
 693                 if (rdip == (dev_info_t *)0)
 694                         return (DDI_FAILURE);
 695                 cmn_err(CE_CONT, "?ISA-device: %s%d\n",
 696                     ddi_driver_name(rdip), ddi_get_instance(rdip));
 697                 return (DDI_SUCCESS);
 698 
 699         case DDI_CTLOPS_INITCHILD:
 700                 /*
 701                  * older drivers aren't expecting the "standard" device
 702                  * node format used by the hardware nodes.  these drivers
 703                  * only expect their own properties set in their driver.conf
 704                  * files.  so they tell us not to call them with hardware
 705                  * nodes by setting the property "ignore-hardware-nodes".
 706                  */
 707                 if (old_driver((dev_info_t *)arg)) {
 708                         return (DDI_NOT_WELL_FORMED);
 709                 }
 710 
 711                 return (isa_initchild((dev_info_t *)arg));
 712 
 713         case DDI_CTLOPS_UNINITCHILD:
 714                 impl_ddi_sunbus_removechild((dev_info_t *)arg);
 715                 return (DDI_SUCCESS);
 716 
 717         case DDI_CTLOPS_SIDDEV:
 718                 if (ndi_dev_is_persistent_node(rdip))
 719                         return (DDI_SUCCESS);
 720                 /*
 721                  * All ISA devices need to do confirming probes
 722                  * unless they are PnP ISA.
 723                  */
 724                 if (is_pnpisa(rdip))
 725                         return (DDI_SUCCESS);
 726                 else
 727                         return (DDI_FAILURE);
 728 
 729         case DDI_CTLOPS_REGSIZE:
 730         case DDI_CTLOPS_NREGS:
 731                 if (rdip == (dev_info_t *)0)
 732                         return (DDI_FAILURE);
 733 
 734                 if ((pdp = ddi_get_parent_data(rdip)) == NULL)
 735                         return (DDI_FAILURE);
 736 
 737                 if (ctlop == DDI_CTLOPS_NREGS) {
 738                         *(int *)result = pdp->par_nreg;
 739                 } else {
 740                         rn = *(int *)arg;
 741                         if (rn >= pdp->par_nreg)
 742                                 return (DDI_FAILURE);
 743                         *(off_t *)result = (off_t)pdp->par_reg[rn].regspec_size;
 744                 }
 745                 return (DDI_SUCCESS);
 746 
 747         case DDI_CTLOPS_ATTACH:
 748         case DDI_CTLOPS_DETACH:
 749         case DDI_CTLOPS_PEEK:
 750         case DDI_CTLOPS_POKE:
 751                 return (DDI_FAILURE);
 752 
 753         default:
 754                 return (ddi_ctlops(dip, rdip, ctlop, arg, result));
 755         }
 756 }
 757 
 758 static struct intrspec *
 759 isa_get_ispec(dev_info_t *rdip, int inum)
 760 {
 761         struct ddi_parent_private_data *pdp = ddi_get_parent_data(rdip);
 762 
 763         /* Validate the interrupt number */
 764         if (inum >= pdp->par_nintr)
 765                 return (NULL);
 766 
 767         /* Get the interrupt structure pointer and return that */
 768         return ((struct intrspec *)&pdp->par_intr[inum]);
 769 }
 770 
 771 static int
 772 isa_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
 773     ddi_intr_handle_impl_t *hdlp, void *result)
 774 {
 775         struct intrspec *ispec;
 776 #if defined(__xpv)
 777         int cons, ttyn;
 778 
 779         cons = console_hypervisor_dev_type(&ttyn);
 780 #endif
 781         if (pseudo_isa)
 782                 return (i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result));
 783 
 784 
 785         /* Process the interrupt operation */
 786         switch (intr_op) {
 787         case DDI_INTROP_GETCAP:
 788                 /* First check with pcplusmp */
 789                 if (psm_intr_ops == NULL)
 790                         return (DDI_FAILURE);
 791 
 792                 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_CAP, result)) {
 793                         *(int *)result = 0;
 794                         return (DDI_FAILURE);
 795                 }
 796                 break;
 797         case DDI_INTROP_SETCAP:
 798                 if (psm_intr_ops == NULL)
 799                         return (DDI_FAILURE);
 800 
 801                 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_CAP, result))
 802                         return (DDI_FAILURE);
 803                 break;
 804         case DDI_INTROP_ALLOC:
 805                 ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
 806                 return (isa_alloc_intr_fixed(rdip, hdlp, result));
 807         case DDI_INTROP_FREE:
 808                 ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
 809                 return (isa_free_intr_fixed(rdip, hdlp));
 810         case DDI_INTROP_GETPRI:
 811                 if ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL)
 812                         return (DDI_FAILURE);
 813                 *(int *)result = ispec->intrspec_pri;
 814                 break;
 815         case DDI_INTROP_SETPRI:
 816                 /* Validate the interrupt priority passed to us */
 817                 if (*(int *)result > LOCK_LEVEL)
 818                         return (DDI_FAILURE);
 819 
 820                 /* Ensure that PSM is all initialized and ispec is ok */
 821                 if ((psm_intr_ops == NULL) ||
 822                     ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL))
 823                         return (DDI_FAILURE);
 824 
 825                 /* update the ispec with the new priority */
 826                 ispec->intrspec_pri =  *(int *)result;
 827                 break;
 828         case DDI_INTROP_ADDISR:
 829                 if ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL)
 830                         return (DDI_FAILURE);
 831                 ispec->intrspec_func = hdlp->ih_cb_func;
 832                 break;
 833         case DDI_INTROP_REMISR:
 834                 if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
 835                         return (DDI_FAILURE);
 836                 if ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL)
 837                         return (DDI_FAILURE);
 838                 ispec->intrspec_func = (uint_t (*)()) 0;
 839                 break;
 840         case DDI_INTROP_ENABLE:
 841                 if ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL)
 842                         return (DDI_FAILURE);
 843 
 844                 /* Call psmi to translate irq with the dip */
 845                 if (psm_intr_ops == NULL)
 846                         return (DDI_FAILURE);
 847 
 848 #if defined(__xpv)
 849                 /*
 850                  * if the hypervisor is using an isa serial port for the
 851                  * console, make sure we don't try to use that interrupt as
 852                  * it will cause us to panic when xen_bind_pirq() fails.
 853                  */
 854                 if (cons == CONS_TTY && ispec->intrspec_vec == asy_intrs[ttyn])
 855                         return (DDI_FAILURE);
 856 #endif
 857                 ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
 858                 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR,
 859                     (int *)&hdlp->ih_vector) == PSM_FAILURE)
 860                         return (DDI_FAILURE);
 861 
 862                 /* Add the interrupt handler */
 863                 if (!add_avintr((void *)hdlp, ispec->intrspec_pri,
 864                     hdlp->ih_cb_func, DEVI(rdip)->devi_name, hdlp->ih_vector,
 865                     hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, NULL, rdip))
 866                         return (DDI_FAILURE);
 867                 break;
 868         case DDI_INTROP_DISABLE:
 869                 if ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL)
 870                         return (DDI_FAILURE);
 871 
 872                 /* Call psm_ops() to translate irq with the dip */
 873                 if (psm_intr_ops == NULL)
 874                         return (DDI_FAILURE);
 875 
 876                 ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
 877                 (void) (*psm_intr_ops)(rdip, hdlp,
 878                     PSM_INTR_OP_XLATE_VECTOR, (int *)&hdlp->ih_vector);
 879 
 880                 /* Remove the interrupt handler */
 881                 rem_avintr((void *)hdlp, ispec->intrspec_pri,
 882                     hdlp->ih_cb_func, hdlp->ih_vector);
 883                 break;
 884         case DDI_INTROP_SETMASK:
 885                 if (psm_intr_ops == NULL)
 886                         return (DDI_FAILURE);
 887 
 888                 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_MASK, NULL))
 889                         return (DDI_FAILURE);
 890                 break;
 891         case DDI_INTROP_CLRMASK:
 892                 if (psm_intr_ops == NULL)
 893                         return (DDI_FAILURE);
 894 
 895                 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_CLEAR_MASK, NULL))
 896                         return (DDI_FAILURE);
 897                 break;
 898         case DDI_INTROP_GETPENDING:
 899                 if (psm_intr_ops == NULL)
 900                         return (DDI_FAILURE);
 901 
 902                 if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_PENDING,
 903                     result)) {
 904                         *(int *)result = 0;
 905                         return (DDI_FAILURE);
 906                 }
 907                 break;
 908         case DDI_INTROP_NAVAIL:
 909         case DDI_INTROP_NINTRS:
 910                 *(int *)result = i_ddi_get_intx_nintrs(rdip);
 911                 if (*(int *)result == 0) {
 912                         return (DDI_FAILURE);
 913                 }
 914                 break;
 915         case DDI_INTROP_SUPPORTED_TYPES:
 916                 *(int *)result = DDI_INTR_TYPE_FIXED;   /* Always ... */
 917                 break;
 918         default:
 919                 return (DDI_FAILURE);
 920         }
 921 
 922         return (DDI_SUCCESS);
 923 }
 924 
 925 /*
 926  * Allocate interrupt vector for FIXED (legacy) type.
 927  */
 928 static int
 929 isa_alloc_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp,
 930     void *result)
 931 {
 932         struct intrspec         *ispec;
 933         ddi_intr_handle_impl_t  info_hdl;
 934         int                     ret;
 935         int                     free_phdl = 0;
 936         apic_get_type_t         type_info;
 937 
 938         if (psm_intr_ops == NULL)
 939                 return (DDI_FAILURE);
 940 
 941         if ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL)
 942                 return (DDI_FAILURE);
 943 
 944         /*
 945          * If the PSM module is "APIX" then pass the request for it
 946          * to allocate the vector now.
 947          */
 948         bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
 949         info_hdl.ih_private = &type_info;
 950         if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
 951             PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
 952                 if (hdlp->ih_private == NULL) { /* allocate phdl structure */
 953                         free_phdl = 1;
 954                         i_ddi_alloc_intr_phdl(hdlp);
 955                 }
 956                 ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
 957                 ret = (*psm_intr_ops)(rdip, hdlp,
 958                     PSM_INTR_OP_ALLOC_VECTORS, result);
 959                 if (free_phdl) { /* free up the phdl structure */
 960                         free_phdl = 0;
 961                         i_ddi_free_intr_phdl(hdlp);
 962                         hdlp->ih_private = NULL;
 963                 }
 964         } else {
 965                 /*
 966                  * No APIX module; fall back to the old scheme where the
 967                  * interrupt vector is allocated during ddi_enable_intr() call.
 968                  */
 969                 hdlp->ih_pri = ispec->intrspec_pri;
 970                 *(int *)result = hdlp->ih_scratch1;
 971                 ret = DDI_SUCCESS;
 972         }
 973 
 974         return (ret);
 975 }
 976 
 977 /*
 978  * Free up interrupt vector for FIXED (legacy) type.
 979  */
 980 static int
 981 isa_free_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
 982 {
 983         struct intrspec                 *ispec;
 984         ddi_intr_handle_impl_t          info_hdl;
 985         int                             ret;
 986         apic_get_type_t                 type_info;
 987 
 988         if (psm_intr_ops == NULL)
 989                 return (DDI_FAILURE);
 990 
 991         /*
 992          * If the PSM module is "APIX" then pass the request for it
 993          * to free up the vector now.
 994          */
 995         bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
 996         info_hdl.ih_private = &type_info;
 997         if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
 998             PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
 999                 if ((ispec = isa_get_ispec(rdip, hdlp->ih_inum)) == NULL)
1000                         return (DDI_FAILURE);
1001                 ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
1002                 ret = (*psm_intr_ops)(rdip, hdlp,
1003                     PSM_INTR_OP_FREE_VECTORS, NULL);
1004         } else {
1005                 /*
1006                  * No APIX module; fall back to the old scheme where
1007                  * the interrupt vector was already freed during
1008                  * ddi_disable_intr() call.
1009                  */
1010                 ret = DDI_SUCCESS;
1011         }
1012 
1013         return (ret);
1014 }
1015 
1016 static void
1017 isa_vendor(uint32_t id, char *vendor)
1018 {
1019         vendor[0] = '@' + ((id >> 26) & 0x1f);
1020         vendor[1] = '@' + ((id >> 21) & 0x1f);
1021         vendor[2] = '@' + ((id >> 16) & 0x1f);
1022         vendor[3] = 0;
1023 }
1024 
1025 /*
1026  * Name a child
1027  */
1028 static int
1029 isa_name_child(dev_info_t *child, char *name, int namelen)
1030 {
1031         char vendor[8];
1032         int device;
1033         uint32_t serial;
1034         int func;
1035         int bustype;
1036         uint32_t base;
1037         int proplen;
1038         int pnpisa = 0;
1039         isa_regs_t *isa_regs;
1040 
1041         void make_ddi_ppd(dev_info_t *, struct ddi_parent_private_data **);
1042 
1043         /*
1044          * older drivers aren't expecting the "standard" device
1045          * node format used by the hardware nodes.  these drivers
1046          * only expect their own properties set in their driver.conf
1047          * files.  so they tell us not to call them with hardware
1048          * nodes by setting the property "ignore-hardware-nodes".
1049          */
1050         if (old_driver(child))
1051                 return (DDI_FAILURE);
1052 
1053         /*
1054          * Fill in parent-private data
1055          */
1056         if (ddi_get_parent_data(child) == NULL) {
1057                 struct ddi_parent_private_data *pdptr;
1058                 make_ddi_ppd(child, &pdptr);
1059                 ddi_set_parent_data(child, pdptr);
1060         }
1061 
1062         if (ndi_dev_is_persistent_node(child) == 0) {
1063                 /*
1064                  * For .conf nodes, generate name from parent private data
1065                  */
1066                 name[0] = '\0';
1067                 if (sparc_pd_getnreg(child) > 0) {
1068                         (void) snprintf(name, namelen, "%x,%x",
1069                             (uint_t)sparc_pd_getreg(child, 0)->regspec_bustype,
1070                             (uint_t)sparc_pd_getreg(child, 0)->regspec_addr);
1071                 }
1072                 return (DDI_SUCCESS);
1073         }
1074 
1075         /*
1076          * For hw nodes, look up "reg" property
1077          */
1078         if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
1079             (caddr_t)&isa_regs, &proplen) != DDI_PROP_SUCCESS) {
1080                 return (DDI_FAILURE);
1081         }
1082 
1083         /*
1084          * extract the device identifications
1085          */
1086         pnpisa = isa_regs[0].phys_hi & 0x80000000;
1087         if (pnpisa) {
1088                 isa_vendor(isa_regs[0].phys_hi, vendor);
1089                 device = isa_regs[0].phys_hi & 0xffff;
1090                 serial = isa_regs[0].phys_lo;
1091                 func = (isa_regs[0].size >> 24) & 0xff;
1092                 if (func != 0)
1093                         (void) snprintf(name, namelen, "pnp%s,%04x,%x,%x",
1094                             vendor, device, serial, func);
1095                 else
1096                         (void) snprintf(name, namelen, "pnp%s,%04x,%x",
1097                             vendor, device, serial);
1098         } else {
1099                 bustype = isa_regs[0].phys_hi;
1100                 base = isa_regs[0].phys_lo;
1101                 (void) sprintf(name, "%x,%x", bustype, base);
1102         }
1103 
1104         /*
1105          * free the memory allocated by ddi_getlongprop().
1106          */
1107         kmem_free(isa_regs, proplen);
1108 
1109         return (DDI_SUCCESS);
1110 }
1111 
1112 static int
1113 isa_initchild(dev_info_t *child)
1114 {
1115         char name[80];
1116 
1117         if (isa_name_child(child, name, 80) != DDI_SUCCESS)
1118                 return (DDI_FAILURE);
1119         ddi_set_name_addr(child, name);
1120 
1121         if (ndi_dev_is_persistent_node(child) != 0)
1122                 return (DDI_SUCCESS);
1123 
1124         /*
1125          * This is a .conf node, try merge properties onto a
1126          * hw node with the same name.
1127          */
1128         if (ndi_merge_node(child, isa_name_child) == DDI_SUCCESS) {
1129                 /*
1130                  * Return failure to remove node
1131                  */
1132                 impl_ddi_sunbus_removechild(child);
1133                 return (DDI_FAILURE);
1134         }
1135         /*
1136          * Cannot merge node, permit pseudo children
1137          */
1138         return (DDI_SUCCESS);
1139 }
1140 
1141 /*
1142  * called when ACPI enumeration is not used
1143  */
1144 static void
1145 add_known_used_resources(void)
1146 {
1147         /* needs to be in increasing order */
1148         int intr[] = {0x1, 0x3, 0x4, 0x6, 0x7, 0xc};
1149         int dma[] = {0x2};
1150         int io[] = {0x60, 0x1, 0x64, 0x1, 0x2f8, 0x8, 0x378, 0x8, 0x3f0, 0x10,
1151             0x778, 0x4};
1152         dev_info_t *usedrdip;
1153 
1154         usedrdip = ddi_find_devinfo(USED_RESOURCES, -1, 0);
1155 
1156         if (usedrdip == NULL) {
1157                 (void) ndi_devi_alloc_sleep(ddi_root_node(), USED_RESOURCES,
1158                     (pnode_t)DEVI_SID_NODEID, &usedrdip);
1159         }
1160 
1161         (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, usedrdip,
1162             "interrupts", (int *)intr, (int)(sizeof (intr) / sizeof (int)));
1163         (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, usedrdip,
1164             "io-space", (int *)io, (int)(sizeof (io) / sizeof (int)));
1165         (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, usedrdip,
1166             "dma-channels", (int *)dma, (int)(sizeof (dma) / sizeof (int)));
1167         (void) ndi_devi_bind_driver(usedrdip, 0);
1168 
1169 }
1170 
1171 /*
1172  * Return non-zero if UART device exists.
1173  */
1174 static int
1175 uart_exists(ushort_t port)
1176 {
1177         outb(port + COM_SCR, (char)0x5a);
1178         outb(port + COM_ISR, (char)0x00);
1179         return (inb(port + COM_SCR) == (char)0x5a);
1180 }
1181 
1182 static void
1183 isa_enumerate(int reprogram)
1184 {
1185         int circ, i;
1186         dev_info_t *xdip;
1187         dev_info_t *isa_dip = ddi_find_devinfo("isa", -1, 0);
1188 
1189         struct regspec i8042_regs[] = {
1190                 {1, 0x60, 0x1},
1191                 {1, 0x64, 0x1}
1192         };
1193         int i8042_intrs[] = {0x1, 0xc};
1194         char *acpi_prop;
1195         int acpi_enum = 1; /* ACPI is default to be on */
1196 #if defined(__xpv)
1197         int cons, ttyn;
1198 
1199         cons = console_hypervisor_dev_type(&ttyn);
1200 #endif
1201         if (reprogram || !isa_dip)
1202                 return;
1203 
1204         bzero(isa_extra_resource, MAX_EXTRA_RESOURCE * sizeof (struct regspec));
1205 
1206         ndi_devi_enter(isa_dip, &circ);
1207 
1208         if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1209             DDI_PROP_DONTPASS, "acpi-enum", &acpi_prop) == DDI_PROP_SUCCESS) {
1210                 acpi_enum = strcmp("off", acpi_prop);
1211                 ddi_prop_free(acpi_prop);
1212         }
1213 
1214         if (acpi_enum) {
1215                 if (acpi_isa_device_enum(isa_dip)) {
1216                         ndi_devi_exit(isa_dip, circ);
1217                         if (isa_resource_setup() != NDI_SUCCESS) {
1218                                 cmn_err(CE_WARN, "isa nexus: isa "
1219                                     "resource setup failed");
1220                         }
1221 
1222                         /* serial ports? */
1223                         enumerate_BIOS_serial(isa_dip);
1224 
1225                         /* adjust parallel port size  */
1226                         adjust_prtsz(isa_dip);
1227 
1228                         isa_create_ranges_prop(isa_dip);
1229                         return;
1230                 }
1231                 cmn_err(CE_NOTE, "!Solaris did not detect ACPI BIOS");
1232         }
1233         cmn_err(CE_NOTE, "!ACPI is off");
1234 
1235         /* serial ports */
1236         for (i = 0; i < min_BIOS_serial; i++) {
1237                 ushort_t addr = asy_regs[i].regspec_addr;
1238                 if (!uart_exists(addr))
1239                         continue;
1240 #if defined(__xpv)
1241                 if (cons == CONS_TTY && ttyn == i)
1242                         continue;
1243 #endif
1244                 ndi_devi_alloc_sleep(isa_dip, "asy",
1245                     (pnode_t)DEVI_SID_NODEID, &xdip);
1246                 (void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
1247                     "compatible", "PNP0500");
1248                 /* This should be gotten from master file: */
1249                 (void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
1250                     "model", "Standard PC COM port");
1251                 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
1252                     "reg", (int *)&asy_regs[i], 3);
1253                 (void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
1254                     "interrupts", asy_intrs[i]);
1255                 (void) ndi_devi_bind_driver(xdip, 0);
1256                 /* Adjusting isa_extra here causes a kernel dump later. */
1257         }
1258 
1259         /* i8042 node */
1260         ndi_devi_alloc_sleep(isa_dip, "i8042",
1261             (pnode_t)DEVI_SID_NODEID, &xdip);
1262         (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
1263             "reg", (int *)i8042_regs, 6);
1264         (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
1265             "interrupts", (int *)i8042_intrs, 2);
1266         (void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
1267             "unit-address", "1,60");
1268         (void) ndi_devi_bind_driver(xdip, 0);
1269 
1270         add_known_used_resources();
1271 
1272         ndi_devi_exit(isa_dip, circ);
1273 
1274         isa_create_ranges_prop(isa_dip);
1275 }
1276 
1277 /*
1278  * On some machines, serial port 2 isn't listed in the ACPI table.
1279  * This function goes through the base I/O addresses and makes sure all
1280  * the serial ports there are in the dev_info tree.  If any are missing,
1281  * this function will add them.
1282  */
1283 
1284 static void
1285 enumerate_BIOS_serial(dev_info_t *isa_dip)
1286 {
1287         int i;
1288         dev_info_t *xdip;
1289         int found;
1290         int ret;
1291         struct regspec *tmpregs;
1292         int tmpregs_len;
1293 #if defined(__xpv)
1294         int cons, ttyn;
1295 
1296         cons = console_hypervisor_dev_type(&ttyn);
1297 #endif
1298 
1299         /*
1300          * Scan the base I/O addresses of the first four serial ports.
1301          */
1302         for (i = 0; i < num_BIOS_serial; i++) {
1303                 ushort_t addr = asy_regs[i].regspec_addr;
1304 
1305                 /* Look for it in the dev_info tree */
1306                 found = 0;
1307                 for (xdip = ddi_get_child(isa_dip); xdip != NULL;
1308                     xdip = ddi_get_next_sibling(xdip)) {
1309                         if (strncmp(ddi_node_name(xdip), "asy", 3) != 0) {
1310                                 /* skip non asy */
1311                                 continue;
1312                         }
1313 
1314                         /* Match by addr */
1315                         ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, xdip,
1316                             DDI_PROP_DONTPASS, "reg", (int **)&tmpregs,
1317                             (uint_t *)&tmpregs_len);
1318                         if (ret != DDI_PROP_SUCCESS) {
1319                                 /* error */
1320                                 continue;
1321                         }
1322 
1323                         if (tmpregs->regspec_addr == addr)
1324                                 found = 1;
1325 
1326                         /*
1327                          * Free the memory allocated by
1328                          * ddi_prop_lookup_int_array().
1329                          */
1330                         ddi_prop_free(tmpregs);
1331 
1332                         if (found) {
1333                                 if (asy_intr_override & 1<<i) {
1334                                         (void) ndi_prop_update_int(
1335                                             DDI_DEV_T_NONE, xdip,
1336                                             "interrupts", asy_intrs[i]);
1337                                 }
1338 
1339                                 break;
1340                         }
1341                 }
1342 
1343                 /* If not found, then add it */
1344                 if (!found && uart_exists(addr)) {
1345                         ndi_devi_alloc_sleep(isa_dip, "asy",
1346                             (pnode_t)DEVI_SID_NODEID, &xdip);
1347                         (void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
1348                             "compatible", "PNP0500");
1349                         /* This should be gotten from master file: */
1350                         (void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
1351                             "model", "Standard PC COM port");
1352                         (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
1353                             "reg", (int *)&asy_regs[i], 3);
1354                         (void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
1355                             "interrupts", asy_intrs[i]);
1356                         (void) ndi_devi_bind_driver(xdip, 0);
1357 
1358                         ASSERT(isa_extra_count < MAX_EXTRA_RESOURCE);
1359                         bcopy(&asy_regs[i],
1360                             isa_extra_resource + isa_extra_count,
1361                             sizeof (struct regspec));
1362                         isa_extra_count++;
1363                 }
1364         }
1365 
1366         /*
1367          * An asy node may have been attached via ACPI enumeration, or
1368          * directly from this file.  Check each serial port to see if it
1369          * is in use by the hypervisor.  If it is in use, then remove
1370          * the node from the device tree.
1371          */
1372 #if defined(__xpv)
1373         i = 0;
1374 
1375         for (xdip = ddi_get_child(isa_dip); xdip != NULL; ) {
1376                 dev_info_t *curdip;
1377 
1378                 curdip = xdip;
1379                 xdip = ddi_get_next_sibling(xdip);
1380 
1381                 if (strncmp(ddi_node_name(curdip), "asy", 3) != 0)
1382                         continue;
1383 
1384                 if (cons == CONS_TTY && ttyn == i) {
1385                         ret = ndi_devi_free(curdip);
1386                         if (ret != DDI_SUCCESS) {
1387                                 cmn_err(CE_WARN,
1388                                     "could not remove asy%d node", i);
1389                         }
1390 
1391                         cmn_err(CE_NOTE, "!asy%d unavailable, reserved"
1392                             " to hypervisor", i);
1393                 }
1394 
1395                 i++;
1396         }
1397 #endif
1398 
1399 }
1400 
1401 /*
1402  * Some machine comes with an illegal parallel port size of 3
1403  * bytes in ACPI, even parallel port mode is ECP.
1404  */
1405 #define DEFAULT_PRT_SIZE        8
1406 static void
1407 adjust_prtsz(dev_info_t *isa_dip)
1408 {
1409         dev_info_t *cdip;
1410         struct regspec *regs_p, *extreg_p;
1411         int regs_len, nreg, i;
1412         char *name;
1413 
1414         for (cdip = ddi_get_child(isa_dip); cdip != NULL;
1415             cdip = ddi_get_next_sibling(cdip)) {
1416                 name = ddi_node_name(cdip);
1417                 if ((strncmp(name, "lp", 2) != 0) || (strnlen(name, 3) != 2))
1418                         continue;       /* skip non parallel */
1419 
1420                 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, cdip,
1421                     DDI_PROP_DONTPASS, "reg", (int **)&regs_p,
1422                     (uint_t *)&regs_len) != DDI_PROP_SUCCESS)
1423                         continue;
1424 
1425                 nreg = regs_len / (sizeof (struct regspec) / sizeof (int));
1426                 for (i = 0; i < nreg; i++) {
1427                         if (regs_p[i].regspec_size == DEFAULT_PRT_SIZE)
1428                                 continue;
1429 
1430                         ASSERT(isa_extra_count < MAX_EXTRA_RESOURCE);
1431                         extreg_p = &isa_extra_resource[isa_extra_count++];
1432                         extreg_p->regspec_bustype = ISA_ADDR_IO;
1433                         extreg_p->regspec_addr = regs_p[i].regspec_addr;
1434                         extreg_p->regspec_size = DEFAULT_PRT_SIZE;
1435                 }
1436 
1437                 ddi_prop_free(regs_p);
1438         }
1439 }