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