xref: /illumos-gate/usr/src/uts/i86pc/io/isa.c (revision 613b2871)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
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/dma_engine.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 #include <sys/sunndi.h>
45 #include <sys/acpi/acpi_enum.h>
46 #if defined(__xpv)
47 #include <sys/hypervisor.h>
48 #include <sys/evtchn_impl.h>
49 #endif
50 
51 
52 extern int isa_resource_setup(void);
53 static char USED_RESOURCES[] = "used-resources";
54 static void isa_alloc_nodes(dev_info_t *);
55 static void enumerate_BIOS_serial(dev_info_t *);
56 
57 #define	BIOS_DATA_AREA	0x400
58 /*
59  * #define ISA_DEBUG 1
60  */
61 
62 /*
63  *      Local data
64  */
65 static ddi_dma_lim_t ISA_dma_limits = {
66 	0,		/* address low				*/
67 	0x00ffffff,	/* address high				*/
68 	0,		/* counter max				*/
69 	1,		/* burstsize				*/
70 	DMA_UNIT_8,	/* minimum xfer				*/
71 	0,		/* dma speed				*/
72 	(uint_t)DMALIM_VER0, /* version				*/
73 	0x0000ffff,	/* address register			*/
74 	0x0000ffff,	/* counter register			*/
75 	1,		/* sector size				*/
76 	0x00000001,	/* scatter/gather list length		*/
77 	(uint_t)0xffffffff /* request size			*/
78 };
79 
80 static ddi_dma_attr_t ISA_dma_attr = {
81 	DMA_ATTR_V0,
82 	(unsigned long long)0,
83 	(unsigned long long)0x00ffffff,
84 	0x0000ffff,
85 	1,
86 	1,
87 	1,
88 	(unsigned long long)0xffffffff,
89 	(unsigned long long)0x0000ffff,
90 	1,
91 	1,
92 	0
93 };
94 
95 
96 /*
97  * Config information
98  */
99 
100 static int
101 isa_dma_allochdl(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
102     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *);
103 
104 static int
105 isa_dma_mctl(dev_info_t *, dev_info_t *, ddi_dma_handle_t, enum ddi_dma_ctlops,
106     off_t *, size_t *, caddr_t *, uint_t);
107 
108 static int
109 isa_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
110 
111 struct bus_ops isa_bus_ops = {
112 	BUSO_REV,
113 	i_ddi_bus_map,
114 	NULL,
115 	NULL,
116 	NULL,
117 	i_ddi_map_fault,
118 	ddi_dma_map,
119 	isa_dma_allochdl,
120 	ddi_dma_freehdl,
121 	ddi_dma_bindhdl,
122 	ddi_dma_unbindhdl,
123 	ddi_dma_flush,
124 	ddi_dma_win,
125 	isa_dma_mctl,
126 	isa_ctlops,
127 	ddi_bus_prop_op,
128 	NULL,		/* (*bus_get_eventcookie)();	*/
129 	NULL,		/* (*bus_add_eventcall)();	*/
130 	NULL,		/* (*bus_remove_eventcall)();	*/
131 	NULL,		/* (*bus_post_event)();		*/
132 	NULL,		/* (*bus_intr_ctl)(); */
133 	NULL,		/* (*bus_config)(); */
134 	NULL,		/* (*bus_unconfig)(); */
135 	NULL,		/* (*bus_fm_init)(); */
136 	NULL,		/* (*bus_fm_fini)(); */
137 	NULL,		/* (*bus_fm_access_enter)(); */
138 	NULL,		/* (*bus_fm_access_exit)(); */
139 	NULL,		/* (*bus_power)(); */
140 	i_ddi_intr_ops	/* (*bus_intr_op)(); */
141 };
142 
143 
144 static int isa_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
145 
146 /*
147  * Internal isa ctlops support routines
148  */
149 static int isa_initchild(dev_info_t *child);
150 
151 struct dev_ops isa_ops = {
152 	DEVO_REV,		/* devo_rev, */
153 	0,			/* refcnt  */
154 	ddi_no_info,		/* info */
155 	nulldev,		/* identify */
156 	nulldev,		/* probe */
157 	isa_attach,		/* attach */
158 	nodev,			/* detach */
159 	nodev,			/* reset */
160 	(struct cb_ops *)0,	/* driver operations */
161 	&isa_bus_ops	/* bus operations */
162 
163 };
164 
165 /*
166  * Module linkage information for the kernel.
167  */
168 
169 static struct modldrv modldrv = {
170 	&mod_driverops, /* Type of module.  This is ISA bus driver */
171 	"isa nexus driver for 'ISA'",
172 	&isa_ops,	/* driver ops */
173 };
174 
175 static struct modlinkage modlinkage = {
176 	MODREV_1,
177 	&modldrv,
178 	NULL
179 };
180 
181 int
182 _init(void)
183 {
184 	return (mod_install(&modlinkage));
185 }
186 
187 int
188 _fini(void)
189 {
190 	return (mod_remove(&modlinkage));
191 }
192 
193 int
194 _info(struct modinfo *modinfop)
195 {
196 	return (mod_info(&modlinkage, modinfop));
197 }
198 
199 static int
200 isa_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
201 {
202 	int rval;
203 
204 #if defined(__xpv)
205 	/*
206 	 * don't allow isa to attach in domU. this can happen if someone sets
207 	 * the console wrong, etc. ISA devices assume the H/W is there and
208 	 * will cause the domU to panic.
209 	 */
210 	if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
211 		return (DDI_FAILURE);
212 	}
213 #endif
214 
215 	if (cmd != DDI_ATTACH)
216 		return (DDI_FAILURE);
217 
218 	if ((rval = i_dmae_init(devi)) == DDI_SUCCESS) {
219 		ddi_report_dev(devi);
220 		/*
221 		 * Enumerate children -- invoking ACPICA
222 		 * This is normally in bus_config(), but we need this
223 		 * to happen earlier to boot.
224 		 */
225 		isa_alloc_nodes(devi);
226 	}
227 	return (rval);
228 }
229 
230 static int
231 isa_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *dma_attr,
232     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
233 {
234 	ddi_dma_attr_merge(dma_attr, &ISA_dma_attr);
235 	return (ddi_dma_allochdl(dip, rdip, dma_attr, waitfp, arg, handlep));
236 }
237 
238 static int
239 isa_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
240     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
241     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
242 {
243 	int rval;
244 	ddi_dma_lim_t defalt;
245 	int arg = (int)(uintptr_t)objp;
246 
247 	switch (request) {
248 
249 	case DDI_DMA_E_PROG:
250 		return (i_dmae_prog(rdip, (struct ddi_dmae_req *)offp,
251 		    (ddi_dma_cookie_t *)lenp, arg));
252 
253 	case DDI_DMA_E_ACQUIRE:
254 		return (i_dmae_acquire(rdip, arg, (int(*)(caddr_t))offp,
255 		    (caddr_t)lenp));
256 
257 	case DDI_DMA_E_FREE:
258 		return (i_dmae_free(rdip, arg));
259 
260 	case DDI_DMA_E_STOP:
261 		i_dmae_stop(rdip, arg);
262 		return (DDI_SUCCESS);
263 
264 	case DDI_DMA_E_ENABLE:
265 		i_dmae_enable(rdip, arg);
266 		return (DDI_SUCCESS);
267 
268 	case DDI_DMA_E_DISABLE:
269 		i_dmae_disable(rdip, arg);
270 		return (DDI_SUCCESS);
271 
272 	case DDI_DMA_E_GETCNT:
273 		i_dmae_get_chan_stat(rdip, arg, NULL, (int *)lenp);
274 		return (DDI_SUCCESS);
275 
276 	case DDI_DMA_E_SWSETUP:
277 		return (i_dmae_swsetup(rdip, (struct ddi_dmae_req *)offp,
278 		    (ddi_dma_cookie_t *)lenp, arg));
279 
280 	case DDI_DMA_E_SWSTART:
281 		i_dmae_swstart(rdip, arg);
282 		return (DDI_SUCCESS);
283 
284 	case DDI_DMA_E_GETLIM:
285 		bcopy(&ISA_dma_limits, objp, sizeof (ddi_dma_lim_t));
286 		return (DDI_SUCCESS);
287 
288 	case DDI_DMA_E_GETATTR:
289 		bcopy(&ISA_dma_attr, objp, sizeof (ddi_dma_attr_t));
290 		return (DDI_SUCCESS);
291 
292 	case DDI_DMA_E_1STPTY:
293 		{
294 			struct ddi_dmae_req req1stpty =
295 			    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
296 			if (arg == 0) {
297 				req1stpty.der_command = DMAE_CMD_TRAN;
298 				req1stpty.der_trans = DMAE_TRANS_DMND;
299 			} else {
300 				req1stpty.der_trans = DMAE_TRANS_CSCD;
301 			}
302 			return (i_dmae_prog(rdip, &req1stpty, NULL, arg));
303 		}
304 
305 	case DDI_DMA_IOPB_ALLOC:	/* get contiguous DMA-able memory */
306 	case DDI_DMA_SMEM_ALLOC:
307 		if (!offp) {
308 			defalt = ISA_dma_limits;
309 			offp = (off_t *)&defalt;
310 		}
311 		/*FALLTHROUGH*/
312 	default:
313 		rval = ddi_dma_mctl(dip, rdip, handle, request, offp,
314 		    lenp, objp, flags);
315 	}
316 	return (rval);
317 }
318 
319 /*
320  * Check if driver should be treated as an old pre 2.6 driver
321  */
322 static int
323 old_driver(dev_info_t *dip)
324 {
325 	extern int ignore_hardware_nodes;	/* force flag from ddi_impl.c */
326 
327 	if (ndi_dev_is_persistent_node(dip)) {
328 		if (ignore_hardware_nodes)
329 			return (1);
330 		if (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
331 		    "ignore-hardware-nodes", -1) != -1)
332 			return (1);
333 	}
334 	return (0);
335 }
336 
337 typedef struct {
338 	uint32_t phys_hi;
339 	uint32_t phys_lo;
340 	uint32_t size;
341 } isa_regs_t;
342 
343 /*
344  * Return non-zero if device in tree is a PnP isa device
345  */
346 static int
347 is_pnpisa(dev_info_t *dip)
348 {
349 	isa_regs_t *isa_regs;
350 	int proplen, pnpisa;
351 
352 	if (ndi_dev_is_persistent_node(dip) == 0)
353 		return (0);
354 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
355 	    (caddr_t)&isa_regs, &proplen) != DDI_PROP_SUCCESS) {
356 		return (0);
357 	}
358 	pnpisa = isa_regs[0].phys_hi & 0x80000000;
359 	/*
360 	 * free the memory allocated by ddi_getlongprop().
361 	 */
362 	kmem_free(isa_regs, proplen);
363 	if (pnpisa)
364 		return (1);
365 	else
366 		return (0);
367 }
368 
369 /*ARGSUSED*/
370 static int
371 isa_ctlops(dev_info_t *dip, dev_info_t *rdip,
372 	ddi_ctl_enum_t ctlop, void *arg, void *result)
373 {
374 	switch (ctlop) {
375 	case DDI_CTLOPS_REPORTDEV:
376 		if (rdip == (dev_info_t *)0)
377 			return (DDI_FAILURE);
378 		cmn_err(CE_CONT, "?ISA-device: %s%d\n",
379 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
380 		return (DDI_SUCCESS);
381 
382 	case DDI_CTLOPS_INITCHILD:
383 		/*
384 		 * older drivers aren't expecting the "standard" device
385 		 * node format used by the hardware nodes.  these drivers
386 		 * only expect their own properties set in their driver.conf
387 		 * files.  so they tell us not to call them with hardware
388 		 * nodes by setting the property "ignore-hardware-nodes".
389 		 */
390 		if (old_driver((dev_info_t *)arg)) {
391 			return (DDI_NOT_WELL_FORMED);
392 		}
393 
394 		return (isa_initchild((dev_info_t *)arg));
395 
396 	case DDI_CTLOPS_UNINITCHILD:
397 		impl_ddi_sunbus_removechild((dev_info_t *)arg);
398 		return (DDI_SUCCESS);
399 
400 	case DDI_CTLOPS_SIDDEV:
401 		/*
402 		 * All ISA devices need to do confirming probes
403 		 * unless they are PnP ISA.
404 		 */
405 		if (is_pnpisa(dip))
406 			return (DDI_SUCCESS);
407 		else
408 			return (DDI_FAILURE);
409 
410 	default:
411 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
412 	}
413 }
414 
415 static void
416 isa_vendor(uint32_t id, char *vendor)
417 {
418 	vendor[0] = '@' + ((id >> 26) & 0x1f);
419 	vendor[1] = '@' + ((id >> 21) & 0x1f);
420 	vendor[2] = '@' + ((id >> 16) & 0x1f);
421 	vendor[3] = 0;
422 }
423 
424 /*
425  * Name a child
426  */
427 static int
428 isa_name_child(dev_info_t *child, char *name, int namelen)
429 {
430 	char vendor[8];
431 	int device;
432 	uint32_t serial;
433 	int func;
434 	int bustype;
435 	uint32_t base;
436 	int proplen;
437 	int pnpisa = 0;
438 	isa_regs_t *isa_regs;
439 
440 	void make_ddi_ppd(dev_info_t *, struct ddi_parent_private_data **);
441 
442 	/*
443 	 * older drivers aren't expecting the "standard" device
444 	 * node format used by the hardware nodes.  these drivers
445 	 * only expect their own properties set in their driver.conf
446 	 * files.  so they tell us not to call them with hardware
447 	 * nodes by setting the property "ignore-hardware-nodes".
448 	 */
449 	if (old_driver(child))
450 		return (DDI_FAILURE);
451 
452 	/*
453 	 * Fill in parent-private data
454 	 */
455 	if (ddi_get_parent_data(child) == NULL) {
456 		struct ddi_parent_private_data *pdptr;
457 		make_ddi_ppd(child, &pdptr);
458 		ddi_set_parent_data(child, pdptr);
459 	}
460 
461 	if (ndi_dev_is_persistent_node(child) == 0) {
462 		/*
463 		 * For .conf nodes, generate name from parent private data
464 		 */
465 		name[0] = '\0';
466 		if (sparc_pd_getnreg(child) > 0) {
467 			(void) snprintf(name, namelen, "%x,%x",
468 			    (uint_t)sparc_pd_getreg(child, 0)->regspec_bustype,
469 			    (uint_t)sparc_pd_getreg(child, 0)->regspec_addr);
470 		}
471 		return (DDI_SUCCESS);
472 	}
473 
474 	/*
475 	 * For hw nodes, look up "reg" property
476 	 */
477 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
478 	    (caddr_t)&isa_regs, &proplen) != DDI_PROP_SUCCESS) {
479 		return (DDI_FAILURE);
480 	}
481 
482 	/*
483 	 * extract the device identifications
484 	 */
485 	pnpisa = isa_regs[0].phys_hi & 0x80000000;
486 	if (pnpisa) {
487 		isa_vendor(isa_regs[0].phys_hi, vendor);
488 		device = isa_regs[0].phys_hi & 0xffff;
489 		serial = isa_regs[0].phys_lo;
490 		func = (isa_regs[0].size >> 24) & 0xff;
491 		if (func != 0)
492 			(void) snprintf(name, namelen, "pnp%s,%04x,%x,%x",
493 			    vendor, device, serial, func);
494 		else
495 			(void) snprintf(name, namelen, "pnp%s,%04x,%x",
496 			    vendor, device, serial);
497 	} else {
498 		bustype = isa_regs[0].phys_hi;
499 		base = isa_regs[0].phys_lo;
500 		(void) sprintf(name, "%x,%x", bustype, base);
501 	}
502 
503 	/*
504 	 * free the memory allocated by ddi_getlongprop().
505 	 */
506 	kmem_free(isa_regs, proplen);
507 
508 	return (DDI_SUCCESS);
509 }
510 
511 static int
512 isa_initchild(dev_info_t *child)
513 {
514 	char name[80];
515 
516 	if (isa_name_child(child, name, 80) != DDI_SUCCESS)
517 		return (DDI_FAILURE);
518 	ddi_set_name_addr(child, name);
519 
520 	if (ndi_dev_is_persistent_node(child) != 0)
521 		return (DDI_SUCCESS);
522 
523 	/*
524 	 * This is a .conf node, try merge properties onto a
525 	 * hw node with the same name.
526 	 */
527 	if (ndi_merge_node(child, isa_name_child) == DDI_SUCCESS) {
528 		/*
529 		 * Return failure to remove node
530 		 */
531 		impl_ddi_sunbus_removechild(child);
532 		return (DDI_FAILURE);
533 	}
534 	/*
535 	 * Cannot merge node, permit pseudo children
536 	 */
537 	return (DDI_SUCCESS);
538 }
539 
540 /*
541  * called when ACPI enumeration is not used
542  */
543 static void
544 add_known_used_resources(void)
545 {
546 	/* needs to be in increasing order */
547 	int intr[] = {0x1, 0x3, 0x4, 0x6, 0x7, 0xc};
548 	int dma[] = {0x2};
549 	int io[] = {0x60, 0x1, 0x64, 0x1, 0x2f8, 0x8, 0x378, 0x8, 0x3f0, 0x10,
550 	    0x778, 0x4};
551 	dev_info_t *usedrdip;
552 
553 	usedrdip = ddi_find_devinfo(USED_RESOURCES, -1, 0);
554 
555 	if (usedrdip == NULL) {
556 		(void) ndi_devi_alloc_sleep(ddi_root_node(), USED_RESOURCES,
557 		    (pnode_t)DEVI_SID_NODEID, &usedrdip);
558 	}
559 
560 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, usedrdip,
561 	    "interrupts", (int *)intr, (int)(sizeof (intr) / sizeof (int)));
562 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, usedrdip,
563 	    "io-space", (int *)io, (int)(sizeof (io) / sizeof (int)));
564 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, usedrdip,
565 	    "dma-channels", (int *)dma, (int)(sizeof (dma) / sizeof (int)));
566 	(void) ndi_devi_bind_driver(usedrdip, 0);
567 
568 }
569 
570 static void
571 isa_alloc_nodes(dev_info_t *isa_dip)
572 {
573 	static int alloced = 0;
574 	int circ, i;
575 	dev_info_t *xdip;
576 
577 	/* hard coded isa stuff */
578 	struct regspec asy_regs[] = {
579 		{1, 0x3f8, 0x8},
580 		{1, 0x2f8, 0x8}
581 	};
582 	int asy_intrs[] = {0x4, 0x3};
583 
584 	struct regspec i8042_regs[] = {
585 		{1, 0x60, 0x1},
586 		{1, 0x64, 0x1}
587 	};
588 	int i8042_intrs[] = {0x1, 0xc};
589 	char *acpi_prop;
590 	int acpi_enum = 1; /* ACPI is default to be on */
591 
592 	if (alloced)
593 		return;
594 
595 	ndi_devi_enter(isa_dip, &circ);
596 	if (alloced) {	/* just in case we are multi-threaded */
597 		ndi_devi_exit(isa_dip, circ);
598 		return;
599 	}
600 	alloced = 1;
601 
602 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
603 	    DDI_PROP_DONTPASS, "acpi-enum", &acpi_prop) == DDI_PROP_SUCCESS) {
604 		acpi_enum = strcmp("off", acpi_prop);
605 		ddi_prop_free(acpi_prop);
606 	}
607 
608 	if (acpi_enum) {
609 		if (acpi_isa_device_enum(isa_dip)) {
610 			ndi_devi_exit(isa_dip, circ);
611 			if (isa_resource_setup() != NDI_SUCCESS) {
612 				cmn_err(CE_WARN, "isa nexus: isa "
613 				    "resource setup failed");
614 			}
615 
616 			/* serial ports? */
617 			enumerate_BIOS_serial(isa_dip);
618 			return;
619 		}
620 		cmn_err(CE_NOTE, "!Solaris did not detect ACPI BIOS");
621 	}
622 	cmn_err(CE_NOTE, "!ACPI is off");
623 
624 	/* serial ports */
625 	for (i = 0; i < 2; i++) {
626 #if defined(__xpv)
627 		/*
628 		 * the hypervisor may be reserving the serial ports for console
629 		 * and/or debug use.  Probe the irqs to see if they are
630 		 * available.
631 		 */
632 		if (ec_probe_pirq(asy_intrs[i]) == 0)
633 			continue; /* in use */
634 #endif
635 		ndi_devi_alloc_sleep(isa_dip, "asy",
636 		    (pnode_t)DEVI_SID_NODEID, &xdip);
637 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
638 		    "reg", (int *)&asy_regs[i], 3);
639 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
640 		    "interrupts", asy_intrs[i]);
641 		(void) ndi_devi_bind_driver(xdip, 0);
642 	}
643 
644 	/* i8042 node */
645 	ndi_devi_alloc_sleep(isa_dip, "i8042",
646 	    (pnode_t)DEVI_SID_NODEID, &xdip);
647 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
648 	    "reg", (int *)i8042_regs, 6);
649 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
650 	    "interrupts", (int *)i8042_intrs, 2);
651 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
652 	    "unit-address", "1,60");
653 	(void) ndi_devi_bind_driver(xdip, 0);
654 
655 	add_known_used_resources();
656 
657 	ndi_devi_exit(isa_dip, circ);
658 
659 }
660 
661 /*
662  * On some machines, serial port 2 isn't listed in the ACPI table.
663  * This function goes through the BIOS data area and makes sure all
664  * the serial ports there are in the dev_info tree.  If any are missing,
665  * this function will add them.
666  */
667 
668 static int num_BIOS_serial = 2;	/* number of BIOS serial ports to look at */
669 
670 static void
671 enumerate_BIOS_serial(dev_info_t *isa_dip)
672 {
673 	ushort_t *bios_data;
674 	int i;
675 	dev_info_t *xdip;
676 	int found;
677 	int ret;
678 	struct regspec *tmpregs;
679 	int tmpregs_len;
680 	static struct regspec tmp_asy_regs[] = {
681 		{1, 0x3f8, 0x8},
682 	};
683 	static int default_asy_intrs[] = { 4, 3, 4, 3 };
684 	static size_t size = 4;
685 
686 	/*
687 	 * The first four 2-byte quantities of the BIOS data area contain
688 	 * the base I/O addresses of the first four serial ports.
689 	 */
690 	bios_data = (ushort_t *)psm_map_new((paddr_t)BIOS_DATA_AREA, size,
691 	    PSM_PROT_READ);
692 	for (i = 0; i < num_BIOS_serial; i++) {
693 		if (bios_data[i] == 0) {
694 			/* no COM[i]: port */
695 			continue;
696 		}
697 
698 		/* Look for it in the dev_info tree */
699 		found = 0;
700 		for (xdip = ddi_get_child(isa_dip); xdip != NULL;
701 		    xdip = ddi_get_next_sibling(xdip)) {
702 			if (strncmp(ddi_node_name(xdip), "asy", 3) != 0) {
703 				/* skip non asy */
704 				continue;
705 			}
706 
707 			/* Match by addr */
708 			ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, xdip,
709 			    DDI_PROP_DONTPASS, "reg", (int **)&tmpregs,
710 			    (uint_t *)&tmpregs_len);
711 			if (ret != DDI_PROP_SUCCESS) {
712 				/* error */
713 				continue;
714 			}
715 
716 			if (tmpregs->regspec_addr == bios_data[i])
717 				found = 1;
718 			/*
719 			 * Free the memory allocated by
720 			 * ddi_prop_lookup_int_array().
721 			 */
722 			ddi_prop_free(tmpregs);
723 
724 		}
725 
726 		/* If not found, then add it */
727 		if (!found) {
728 			ndi_devi_alloc_sleep(isa_dip, "asy",
729 			    (pnode_t)DEVI_SID_NODEID, &xdip);
730 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
731 			    "compatible", "PNP0500");
732 			/* This should be gotten from master file: */
733 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
734 			    "model", "Standard PC COM port");
735 			tmp_asy_regs[0].regspec_addr = bios_data[i];
736 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, xdip,
737 			    "reg", (int *)&tmp_asy_regs[0], 3);
738 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
739 			    "interrupts", default_asy_intrs[i]);
740 			(void) ndi_devi_bind_driver(xdip, 0);
741 		}
742 	}
743 #if defined(__xpv)
744 	/*
745 	 * Check each serial port to see if it is in use by the hypervisor.
746 	 * If it is in use, then remove the node from the device tree.
747 	 */
748 	i = 0;
749 	for (xdip = ddi_get_child(isa_dip); xdip != NULL; ) {
750 		int asy_intr;
751 		dev_info_t *curdip;
752 
753 		curdip = xdip;
754 		xdip = ddi_get_next_sibling(xdip);
755 		if (strncmp(ddi_node_name(curdip), "asy", 3) != 0) {
756 			/* skip non asy */
757 			continue;
758 		}
759 		/*
760 		 * Check if the hypervisor is using the serial port by probing
761 		 * the irq and if it is using it remove the node
762 		 * from the device tree
763 		 */
764 		asy_intr = ddi_prop_get_int(DDI_DEV_T_ANY, curdip,
765 		    DDI_PROP_DONTPASS, "interrupts", -1);
766 		if (asy_intr == -1) {
767 			/* error */
768 			continue;
769 		}
770 
771 		if (ec_probe_pirq(asy_intr)) {
772 			continue;
773 		}
774 		ret = ndi_devi_free(curdip);
775 		if (ret != DDI_SUCCESS)
776 			cmn_err(CE_WARN,
777 			    "could not remove asy%d node", i);
778 		else
779 			cmn_err(CE_NOTE, "!asy%d unavailable, reserved"
780 			    " to hypervisor", i);
781 		i++;
782 	}
783 #endif	/* __xpv */
784 
785 	psm_unmap((caddr_t)bios_data, size);
786 }
787