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