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