xref: /titanic_44/usr/src/uts/i86pc/os/ddi_impl.c (revision b9238976491622ad75a67ab0c12edf99e36212b9)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * PC specific DDI implementation
31  */
32 #include <sys/types.h>
33 #include <sys/autoconf.h>
34 #include <sys/avintr.h>
35 #include <sys/bootconf.h>
36 #include <sys/conf.h>
37 #include <sys/cpuvar.h>
38 #include <sys/ddi_impldefs.h>
39 #include <sys/ddi_subrdefs.h>
40 #include <sys/ethernet.h>
41 #include <sys/fp.h>
42 #include <sys/instance.h>
43 #include <sys/kmem.h>
44 #include <sys/machsystm.h>
45 #include <sys/modctl.h>
46 #include <sys/promif.h>
47 #include <sys/prom_plat.h>
48 #include <sys/sunndi.h>
49 #include <sys/ndi_impldefs.h>
50 #include <sys/ddi_impldefs.h>
51 #include <sys/sysmacros.h>
52 #include <sys/systeminfo.h>
53 #include <sys/utsname.h>
54 #include <sys/atomic.h>
55 #include <sys/spl.h>
56 #include <sys/archsystm.h>
57 #include <vm/seg_kmem.h>
58 #include <sys/ontrap.h>
59 #include <sys/fm/protocol.h>
60 #include <sys/ramdisk.h>
61 #include <sys/sunndi.h>
62 #include <sys/vmem.h>
63 #include <sys/pci_impl.h>
64 #if defined(__xpv)
65 #include <sys/hypervisor.h>
66 #endif
67 #include <sys/mach_intr.h>
68 #include <vm/hat_i86.h>
69 #include <sys/x86_archext.h>
70 
71 /*
72  * DDI Boot Configuration
73  */
74 
75 /*
76  * Platform drivers on this platform
77  */
78 char *platform_module_list[] = {
79 	"acpippm",
80 	"ppm",
81 	(char *)0
82 };
83 
84 /* pci bus resource maps */
85 struct pci_bus_resource *pci_bus_res;
86 
87 size_t dma_max_copybuf_size = 0x101000;		/* 1M + 4K */
88 
89 extern int root_is_svm;
90 uint64_t ramdisk_start, ramdisk_end;
91 
92 /*
93  * Forward declarations
94  */
95 static int getlongprop_buf();
96 static void get_boot_properties(void);
97 static void impl_bus_initialprobe(void);
98 static void impl_bus_reprobe(void);
99 
100 static int poke_mem(peekpoke_ctlops_t *in_args);
101 static int peek_mem(peekpoke_ctlops_t *in_args);
102 
103 static int kmem_override_cache_attrs(caddr_t, size_t, uint_t);
104 
105 #define	CTGENTRIES	15
106 
107 static struct ctgas {
108 	struct ctgas	*ctg_next;
109 	int		ctg_index;
110 	void		*ctg_addr[CTGENTRIES];
111 	size_t		ctg_size[CTGENTRIES];
112 } ctglist;
113 
114 static kmutex_t		ctgmutex;
115 #define	CTGLOCK()	mutex_enter(&ctgmutex)
116 #define	CTGUNLOCK()	mutex_exit(&ctgmutex)
117 
118 /*
119  * Minimum pfn value of page_t's put on the free list.  This is to simplify
120  * support of ddi dma memory requests which specify small, non-zero addr_lo
121  * values.
122  *
123  * The default value of 2, which corresponds to the only known non-zero addr_lo
124  * value used, means a single page will be sacrificed (pfn typically starts
125  * at 1).  ddiphysmin can be set to 0 to disable. It cannot be set above 0x100
126  * otherwise mp startup panics.
127  */
128 pfn_t	ddiphysmin = 2;
129 
130 static void
131 check_driver_disable(void)
132 {
133 	int proplen = 128;
134 	char *prop_name;
135 	char *drv_name, *propval;
136 	major_t major;
137 
138 	prop_name = kmem_alloc(proplen, KM_SLEEP);
139 	for (major = 0; major < devcnt; major++) {
140 		drv_name = ddi_major_to_name(major);
141 		if (drv_name == NULL)
142 			continue;
143 		(void) snprintf(prop_name, proplen, "disable-%s", drv_name);
144 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
145 		    DDI_PROP_DONTPASS, prop_name, &propval) == DDI_SUCCESS) {
146 			if (strcmp(propval, "true") == 0) {
147 				devnamesp[major].dn_flags |= DN_DRIVER_REMOVED;
148 				cmn_err(CE_NOTE, "driver %s disabled",
149 				    drv_name);
150 			}
151 			ddi_prop_free(propval);
152 		}
153 	}
154 	kmem_free(prop_name, proplen);
155 }
156 
157 
158 /*
159  * Configure the hardware on the system.
160  * Called before the rootfs is mounted
161  */
162 void
163 configure(void)
164 {
165 	extern void i_ddi_init_root();
166 
167 #if defined(__i386)
168 	extern int fpu_pentium_fdivbug;
169 #endif	/* __i386 */
170 	extern int fpu_ignored;
171 
172 	/*
173 	 * Determine if an FPU is attached
174 	 */
175 
176 	fpu_probe();
177 
178 #if defined(__i386)
179 	if (fpu_pentium_fdivbug) {
180 		printf("\
181 FP hardware exhibits Pentium floating point divide problem\n");
182 	}
183 #endif	/* __i386 */
184 
185 	if (fpu_ignored) {
186 		printf("FP hardware will not be used\n");
187 	} else if (!fpu_exists) {
188 		printf("No FPU in configuration\n");
189 	}
190 
191 	/*
192 	 * Initialize devices on the machine.
193 	 * Uses configuration tree built by the PROMs to determine what
194 	 * is present, and builds a tree of prototype dev_info nodes
195 	 * corresponding to the hardware which identified itself.
196 	 */
197 #if !defined(SAS) && !defined(MPSAS)
198 	/*
199 	 * Check for disabled drivers and initialize root node.
200 	 */
201 	check_driver_disable();
202 	i_ddi_init_root();
203 
204 	/*
205 	 * attach the isa nexus to get ACPI resource usage
206 	 * isa is "kind of" a pseudo node
207 	 */
208 #if defined(__xpv)
209 	if (DOMAIN_IS_INITDOMAIN(xen_info))
210 		(void) i_ddi_attach_pseudo_node("isa");
211 #else
212 	(void) i_ddi_attach_pseudo_node("isa");
213 #endif
214 
215 	/* reprogram devices not set up by firmware (BIOS) */
216 	impl_bus_reprobe();
217 #endif	/* !SAS && !MPSAS */
218 }
219 
220 /*
221  * The "status" property indicates the operational status of a device.
222  * If this property is present, the value is a string indicating the
223  * status of the device as follows:
224  *
225  *	"okay"		operational.
226  *	"disabled"	not operational, but might become operational.
227  *	"fail"		not operational because a fault has been detected,
228  *			and it is unlikely that the device will become
229  *			operational without repair. no additional details
230  *			are available.
231  *	"fail-xxx"	not operational because a fault has been detected,
232  *			and it is unlikely that the device will become
233  *			operational without repair. "xxx" is additional
234  *			human-readable information about the particular
235  *			fault condition that was detected.
236  *
237  * The absence of this property means that the operational status is
238  * unknown or okay.
239  *
240  * This routine checks the status property of the specified device node
241  * and returns 0 if the operational status indicates failure, and 1 otherwise.
242  *
243  * The property may exist on plug-in cards the existed before IEEE 1275-1994.
244  * And, in that case, the property may not even be a string. So we carefully
245  * check for the value "fail", in the beginning of the string, noting
246  * the property length.
247  */
248 int
249 status_okay(int id, char *buf, int buflen)
250 {
251 	char status_buf[OBP_MAXPROPNAME];
252 	char *bufp = buf;
253 	int len = buflen;
254 	int proplen;
255 	static const char *status = "status";
256 	static const char *fail = "fail";
257 	int fail_len = (int)strlen(fail);
258 
259 	/*
260 	 * Get the proplen ... if it's smaller than "fail",
261 	 * or doesn't exist ... then we don't care, since
262 	 * the value can't begin with the char string "fail".
263 	 *
264 	 * NB: proplen, if it's a string, includes the NULL in the
265 	 * the size of the property, and fail_len does not.
266 	 */
267 	proplen = prom_getproplen((pnode_t)id, (caddr_t)status);
268 	if (proplen <= fail_len)	/* nonexistant or uninteresting len */
269 		return (1);
270 
271 	/*
272 	 * if a buffer was provided, use it
273 	 */
274 	if ((buf == (char *)NULL) || (buflen <= 0)) {
275 		bufp = status_buf;
276 		len = sizeof (status_buf);
277 	}
278 	*bufp = (char)0;
279 
280 	/*
281 	 * Get the property into the buffer, to the extent of the buffer,
282 	 * and in case the buffer is smaller than the property size,
283 	 * NULL terminate the buffer. (This handles the case where
284 	 * a buffer was passed in and the caller wants to print the
285 	 * value, but the buffer was too small).
286 	 */
287 	(void) prom_bounded_getprop((pnode_t)id, (caddr_t)status,
288 	    (caddr_t)bufp, len);
289 	*(bufp + len - 1) = (char)0;
290 
291 	/*
292 	 * If the value begins with the char string "fail",
293 	 * then it means the node is failed. We don't care
294 	 * about any other values. We assume the node is ok
295 	 * although it might be 'disabled'.
296 	 */
297 	if (strncmp(bufp, fail, fail_len) == 0)
298 		return (0);
299 
300 	return (1);
301 }
302 
303 /*
304  * Check the status of the device node passed as an argument.
305  *
306  *	if ((status is OKAY) || (status is DISABLED))
307  *		return DDI_SUCCESS
308  *	else
309  *		print a warning and return DDI_FAILURE
310  */
311 /*ARGSUSED1*/
312 int
313 check_status(int id, char *name, dev_info_t *parent)
314 {
315 	char status_buf[64];
316 	char devtype_buf[OBP_MAXPROPNAME];
317 	int retval = DDI_FAILURE;
318 
319 	/*
320 	 * is the status okay?
321 	 */
322 	if (status_okay(id, status_buf, sizeof (status_buf)))
323 		return (DDI_SUCCESS);
324 
325 	/*
326 	 * a status property indicating bad memory will be associated
327 	 * with a node which has a "device_type" property with a value of
328 	 * "memory-controller". in this situation, return DDI_SUCCESS
329 	 */
330 	if (getlongprop_buf(id, OBP_DEVICETYPE, devtype_buf,
331 	    sizeof (devtype_buf)) > 0) {
332 		if (strcmp(devtype_buf, "memory-controller") == 0)
333 			retval = DDI_SUCCESS;
334 	}
335 
336 	/*
337 	 * print the status property information
338 	 */
339 	cmn_err(CE_WARN, "status '%s' for '%s'", status_buf, name);
340 	return (retval);
341 }
342 
343 /*ARGSUSED*/
344 uint_t
345 softlevel1(caddr_t arg1, caddr_t arg2)
346 {
347 	softint();
348 	return (1);
349 }
350 
351 /*
352  * Allow for implementation specific correction of PROM property values.
353  */
354 
355 /*ARGSUSED*/
356 void
357 impl_fix_props(dev_info_t *dip, dev_info_t *ch_dip, char *name, int len,
358     caddr_t buffer)
359 {
360 	/*
361 	 * There are no adjustments needed in this implementation.
362 	 */
363 }
364 
365 static int
366 getlongprop_buf(int id, char *name, char *buf, int maxlen)
367 {
368 	int size;
369 
370 	size = prom_getproplen((pnode_t)id, name);
371 	if (size <= 0 || (size > maxlen - 1))
372 		return (-1);
373 
374 	if (-1 == prom_getprop((pnode_t)id, name, buf))
375 		return (-1);
376 
377 	if (strcmp("name", name) == 0) {
378 		if (buf[size - 1] != '\0') {
379 			buf[size] = '\0';
380 			size += 1;
381 		}
382 	}
383 
384 	return (size);
385 }
386 
387 static int
388 get_prop_int_array(dev_info_t *di, char *pname, int **pval, uint_t *plen)
389 {
390 	int ret;
391 
392 	if ((ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, di,
393 	    DDI_PROP_DONTPASS, pname, pval, plen))
394 	    == DDI_PROP_SUCCESS) {
395 		*plen = (*plen) * (sizeof (int));
396 	}
397 	return (ret);
398 }
399 
400 
401 /*
402  * Node Configuration
403  */
404 
405 struct prop_ispec {
406 	uint_t	pri, vec;
407 };
408 
409 /*
410  * For the x86, we're prepared to claim that the interrupt string
411  * is in the form of a list of <ipl,vec> specifications.
412  */
413 
414 #define	VEC_MIN	1
415 #define	VEC_MAX	255
416 
417 static int
418 impl_xlate_intrs(dev_info_t *child, int *in,
419     struct ddi_parent_private_data *pdptr)
420 {
421 	size_t size;
422 	int n;
423 	struct intrspec *new;
424 	caddr_t got_prop;
425 	int *inpri;
426 	int got_len;
427 	extern int ignore_hardware_nodes;	/* force flag from ddi_impl.c */
428 
429 	static char bad_intr_fmt[] =
430 	    "bad interrupt spec from %s%d - ipl %d, irq %d\n";
431 
432 	/*
433 	 * determine if the driver is expecting the new style "interrupts"
434 	 * property which just contains the IRQ, or the old style which
435 	 * contains pairs of <IPL,IRQ>.  if it is the new style, we always
436 	 * assign IPL 5 unless an "interrupt-priorities" property exists.
437 	 * in that case, the "interrupt-priorities" property contains the
438 	 * IPL values that match, one for one, the IRQ values in the
439 	 * "interrupts" property.
440 	 */
441 	inpri = NULL;
442 	if ((ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
443 	    "ignore-hardware-nodes", -1) != -1) || ignore_hardware_nodes) {
444 		/* the old style "interrupts" property... */
445 
446 		/*
447 		 * The list consists of <ipl,vec> elements
448 		 */
449 		if ((n = (*in++ >> 1)) < 1)
450 			return (DDI_FAILURE);
451 
452 		pdptr->par_nintr = n;
453 		size = n * sizeof (struct intrspec);
454 		new = pdptr->par_intr = kmem_zalloc(size, KM_SLEEP);
455 
456 		while (n--) {
457 			int level = *in++;
458 			int vec = *in++;
459 
460 			if (level < 1 || level > MAXIPL ||
461 			    vec < VEC_MIN || vec > VEC_MAX) {
462 				cmn_err(CE_CONT, bad_intr_fmt,
463 				    DEVI(child)->devi_name,
464 				    DEVI(child)->devi_instance, level, vec);
465 				goto broken;
466 			}
467 			new->intrspec_pri = level;
468 			if (vec != 2)
469 				new->intrspec_vec = vec;
470 			else
471 				/*
472 				 * irq 2 on the PC bus is tied to irq 9
473 				 * on ISA, EISA and MicroChannel
474 				 */
475 				new->intrspec_vec = 9;
476 			new++;
477 		}
478 
479 		return (DDI_SUCCESS);
480 	} else {
481 		/* the new style "interrupts" property... */
482 
483 		/*
484 		 * The list consists of <vec> elements
485 		 */
486 		if ((n = (*in++)) < 1)
487 			return (DDI_FAILURE);
488 
489 		pdptr->par_nintr = n;
490 		size = n * sizeof (struct intrspec);
491 		new = pdptr->par_intr = kmem_zalloc(size, KM_SLEEP);
492 
493 		/* XXX check for "interrupt-priorities" property... */
494 		if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
495 		    "interrupt-priorities", (caddr_t)&got_prop, &got_len)
496 		    == DDI_PROP_SUCCESS) {
497 			if (n != (got_len / sizeof (int))) {
498 				cmn_err(CE_CONT,
499 				    "bad interrupt-priorities length"
500 				    " from %s%d: expected %d, got %d\n",
501 				    DEVI(child)->devi_name,
502 				    DEVI(child)->devi_instance, n,
503 				    (int)(got_len / sizeof (int)));
504 				goto broken;
505 			}
506 			inpri = (int *)got_prop;
507 		}
508 
509 		while (n--) {
510 			int level;
511 			int vec = *in++;
512 
513 			if (inpri == NULL)
514 				level = 5;
515 			else
516 				level = *inpri++;
517 
518 			if (level < 1 || level > MAXIPL ||
519 			    vec < VEC_MIN || vec > VEC_MAX) {
520 				cmn_err(CE_CONT, bad_intr_fmt,
521 				    DEVI(child)->devi_name,
522 				    DEVI(child)->devi_instance, level, vec);
523 				goto broken;
524 			}
525 			new->intrspec_pri = level;
526 			if (vec != 2)
527 				new->intrspec_vec = vec;
528 			else
529 				/*
530 				 * irq 2 on the PC bus is tied to irq 9
531 				 * on ISA, EISA and MicroChannel
532 				 */
533 				new->intrspec_vec = 9;
534 			new++;
535 		}
536 
537 		if (inpri != NULL)
538 			kmem_free(got_prop, got_len);
539 		return (DDI_SUCCESS);
540 	}
541 
542 broken:
543 	kmem_free(pdptr->par_intr, size);
544 	pdptr->par_intr = NULL;
545 	pdptr->par_nintr = 0;
546 	if (inpri != NULL)
547 		kmem_free(got_prop, got_len);
548 
549 	return (DDI_FAILURE);
550 }
551 
552 /*
553  * Create a ddi_parent_private_data structure from the ddi properties of
554  * the dev_info node.
555  *
556  * The "reg" and either an "intr" or "interrupts" properties are required
557  * if the driver wishes to create mappings or field interrupts on behalf
558  * of the device.
559  *
560  * The "reg" property is assumed to be a list of at least one triple
561  *
562  *	<bustype, address, size>*1
563  *
564  * The "intr" property is assumed to be a list of at least one duple
565  *
566  *	<SPARC ipl, vector#>*1
567  *
568  * The "interrupts" property is assumed to be a list of at least one
569  * n-tuples that describes the interrupt capabilities of the bus the device
570  * is connected to.  For SBus, this looks like
571  *
572  *	<SBus-level>*1
573  *
574  * (This property obsoletes the 'intr' property).
575  *
576  * The "ranges" property is optional.
577  */
578 void
579 make_ddi_ppd(dev_info_t *child, struct ddi_parent_private_data **ppd)
580 {
581 	struct ddi_parent_private_data *pdptr;
582 	int n;
583 	int *reg_prop, *rng_prop, *intr_prop, *irupts_prop;
584 	uint_t reg_len, rng_len, intr_len, irupts_len;
585 
586 	*ppd = pdptr = kmem_zalloc(sizeof (*pdptr), KM_SLEEP);
587 
588 	/*
589 	 * Handle the 'reg' property.
590 	 */
591 	if ((get_prop_int_array(child, "reg", &reg_prop, &reg_len) ==
592 	    DDI_PROP_SUCCESS) && (reg_len != 0)) {
593 		pdptr->par_nreg = reg_len / (int)sizeof (struct regspec);
594 		pdptr->par_reg = (struct regspec *)reg_prop;
595 	}
596 
597 	/*
598 	 * See if I have a range (adding one where needed - this
599 	 * means to add one for sbus node in sun4c, when romvec > 0,
600 	 * if no range is already defined in the PROM node.
601 	 * (Currently no sun4c PROMS define range properties,
602 	 * but they should and may in the future.)  For the SBus
603 	 * node, the range is defined by the SBus reg property.
604 	 */
605 	if (get_prop_int_array(child, "ranges", &rng_prop, &rng_len)
606 	    == DDI_PROP_SUCCESS) {
607 		pdptr->par_nrng = rng_len / (int)(sizeof (struct rangespec));
608 		pdptr->par_rng = (struct rangespec *)rng_prop;
609 	}
610 
611 	/*
612 	 * Handle the 'intr' and 'interrupts' properties
613 	 */
614 
615 	/*
616 	 * For backwards compatibility
617 	 * we first look for the 'intr' property for the device.
618 	 */
619 	if (get_prop_int_array(child, "intr", &intr_prop, &intr_len)
620 	    != DDI_PROP_SUCCESS) {
621 		intr_len = 0;
622 	}
623 
624 	/*
625 	 * If we're to support bus adapters and future platforms cleanly,
626 	 * we need to support the generalized 'interrupts' property.
627 	 */
628 	if (get_prop_int_array(child, "interrupts", &irupts_prop,
629 	    &irupts_len) != DDI_PROP_SUCCESS) {
630 		irupts_len = 0;
631 	} else if (intr_len != 0) {
632 		/*
633 		 * If both 'intr' and 'interrupts' are defined,
634 		 * then 'interrupts' wins and we toss the 'intr' away.
635 		 */
636 		ddi_prop_free((void *)intr_prop);
637 		intr_len = 0;
638 	}
639 
640 	if (intr_len != 0) {
641 
642 		/*
643 		 * Translate the 'intr' property into an array
644 		 * an array of struct intrspec's.  There's not really
645 		 * very much to do here except copy what's out there.
646 		 */
647 
648 		struct intrspec *new;
649 		struct prop_ispec *l;
650 
651 		n = pdptr->par_nintr = intr_len / sizeof (struct prop_ispec);
652 		l = (struct prop_ispec *)intr_prop;
653 		pdptr->par_intr =
654 		    new = kmem_zalloc(n * sizeof (struct intrspec), KM_SLEEP);
655 		while (n--) {
656 			new->intrspec_pri = l->pri;
657 			new->intrspec_vec = l->vec;
658 			new++;
659 			l++;
660 		}
661 		ddi_prop_free((void *)intr_prop);
662 
663 	} else if ((n = irupts_len) != 0) {
664 		size_t size;
665 		int *out;
666 
667 		/*
668 		 * Translate the 'interrupts' property into an array
669 		 * of intrspecs for the rest of the DDI framework to
670 		 * toy with.  Only our ancestors really know how to
671 		 * do this, so ask 'em.  We massage the 'interrupts'
672 		 * property so that it is pre-pended by a count of
673 		 * the number of integers in the argument.
674 		 */
675 		size = sizeof (int) + n;
676 		out = kmem_alloc(size, KM_SLEEP);
677 		*out = n / sizeof (int);
678 		bcopy(irupts_prop, out + 1, (size_t)n);
679 		ddi_prop_free((void *)irupts_prop);
680 		if (impl_xlate_intrs(child, out, pdptr) != DDI_SUCCESS) {
681 			cmn_err(CE_CONT,
682 			    "Unable to translate 'interrupts' for %s%d\n",
683 			    DEVI(child)->devi_binding_name,
684 			    DEVI(child)->devi_instance);
685 		}
686 		kmem_free(out, size);
687 	}
688 }
689 
690 /*
691  * Name a child
692  */
693 static int
694 impl_sunbus_name_child(dev_info_t *child, char *name, int namelen)
695 {
696 	/*
697 	 * Fill in parent-private data and this function returns to us
698 	 * an indication if it used "registers" to fill in the data.
699 	 */
700 	if (ddi_get_parent_data(child) == NULL) {
701 		struct ddi_parent_private_data *pdptr;
702 		make_ddi_ppd(child, &pdptr);
703 		ddi_set_parent_data(child, pdptr);
704 	}
705 
706 	name[0] = '\0';
707 	if (sparc_pd_getnreg(child) > 0) {
708 		(void) snprintf(name, namelen, "%x,%x",
709 		    (uint_t)sparc_pd_getreg(child, 0)->regspec_bustype,
710 		    (uint_t)sparc_pd_getreg(child, 0)->regspec_addr);
711 	}
712 
713 	return (DDI_SUCCESS);
714 }
715 
716 /*
717  * Called from the bus_ctl op of sunbus (sbus, obio, etc) nexus drivers
718  * to implement the DDI_CTLOPS_INITCHILD operation.  That is, it names
719  * the children of sun busses based on the reg spec.
720  *
721  * Handles the following properties (in make_ddi_ppd):
722  *	Property		value
723  *	  Name			type
724  *	reg		register spec
725  *	intr		old-form interrupt spec
726  *	interrupts	new (bus-oriented) interrupt spec
727  *	ranges		range spec
728  */
729 int
730 impl_ddi_sunbus_initchild(dev_info_t *child)
731 {
732 	char name[MAXNAMELEN];
733 	void impl_ddi_sunbus_removechild(dev_info_t *);
734 
735 	/*
736 	 * Name the child, also makes parent private data
737 	 */
738 	(void) impl_sunbus_name_child(child, name, MAXNAMELEN);
739 	ddi_set_name_addr(child, name);
740 
741 	/*
742 	 * Attempt to merge a .conf node; if successful, remove the
743 	 * .conf node.
744 	 */
745 	if ((ndi_dev_is_persistent_node(child) == 0) &&
746 	    (ndi_merge_node(child, impl_sunbus_name_child) == DDI_SUCCESS)) {
747 		/*
748 		 * Return failure to remove node
749 		 */
750 		impl_ddi_sunbus_removechild(child);
751 		return (DDI_FAILURE);
752 	}
753 	return (DDI_SUCCESS);
754 }
755 
756 void
757 impl_free_ddi_ppd(dev_info_t *dip)
758 {
759 	struct ddi_parent_private_data *pdptr;
760 	size_t n;
761 
762 	if ((pdptr = ddi_get_parent_data(dip)) == NULL)
763 		return;
764 
765 	if ((n = (size_t)pdptr->par_nintr) != 0)
766 		/*
767 		 * Note that kmem_free is used here (instead of
768 		 * ddi_prop_free) because the contents of the
769 		 * property were placed into a separate buffer and
770 		 * mucked with a bit before being stored in par_intr.
771 		 * The actual return value from the prop lookup
772 		 * was freed with ddi_prop_free previously.
773 		 */
774 		kmem_free(pdptr->par_intr, n * sizeof (struct intrspec));
775 
776 	if ((n = (size_t)pdptr->par_nrng) != 0)
777 		ddi_prop_free((void *)pdptr->par_rng);
778 
779 	if ((n = pdptr->par_nreg) != 0)
780 		ddi_prop_free((void *)pdptr->par_reg);
781 
782 	kmem_free(pdptr, sizeof (*pdptr));
783 	ddi_set_parent_data(dip, NULL);
784 }
785 
786 void
787 impl_ddi_sunbus_removechild(dev_info_t *dip)
788 {
789 	impl_free_ddi_ppd(dip);
790 	ddi_set_name_addr(dip, NULL);
791 	/*
792 	 * Strip the node to properly convert it back to prototype form
793 	 */
794 	impl_rem_dev_props(dip);
795 }
796 
797 /*
798  * DDI Interrupt
799  */
800 
801 /*
802  * turn this on to force isa, eisa, and mca device to ignore the new
803  * hardware nodes in the device tree (normally turned on only for
804  * drivers that need it by setting the property "ignore-hardware-nodes"
805  * in their driver.conf file).
806  *
807  * 7/31/96 -- Turned off globally.  Leaving variable in for the moment
808  *		as safety valve.
809  */
810 int ignore_hardware_nodes = 0;
811 
812 /*
813  * Local data
814  */
815 static struct impl_bus_promops *impl_busp;
816 
817 
818 /*
819  * New DDI interrupt framework
820  */
821 
822 /*
823  * i_ddi_intr_ops:
824  *
825  * This is the interrupt operator function wrapper for the bus function
826  * bus_intr_op.
827  */
828 int
829 i_ddi_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
830     ddi_intr_handle_impl_t *hdlp, void * result)
831 {
832 	dev_info_t	*pdip = (dev_info_t *)DEVI(dip)->devi_parent;
833 	int		ret = DDI_FAILURE;
834 
835 	/* request parent to process this interrupt op */
836 	if (NEXUS_HAS_INTR_OP(pdip))
837 		ret = (*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_intr_op))(
838 		    pdip, rdip, op, hdlp, result);
839 	else
840 		cmn_err(CE_WARN, "Failed to process interrupt "
841 		    "for %s%d due to down-rev nexus driver %s%d",
842 		    ddi_get_name(rdip), ddi_get_instance(rdip),
843 		    ddi_get_name(pdip), ddi_get_instance(pdip));
844 	return (ret);
845 }
846 
847 /*
848  * i_ddi_add_softint - allocate and add a soft interrupt to the system
849  */
850 int
851 i_ddi_add_softint(ddi_softint_hdl_impl_t *hdlp)
852 {
853 	int ret;
854 
855 	/* add soft interrupt handler */
856 	ret = add_avsoftintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func,
857 	    DEVI(hdlp->ih_dip)->devi_name, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
858 	return (ret ? DDI_SUCCESS : DDI_FAILURE);
859 }
860 
861 
862 void
863 i_ddi_remove_softint(ddi_softint_hdl_impl_t *hdlp)
864 {
865 	(void) rem_avsoftintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func);
866 }
867 
868 
869 extern void (*setsoftint)(int, struct av_softinfo *);
870 extern boolean_t av_check_softint_pending(struct av_softinfo *, boolean_t);
871 
872 int
873 i_ddi_trigger_softint(ddi_softint_hdl_impl_t *hdlp, void *arg2)
874 {
875 	if (av_check_softint_pending(hdlp->ih_pending, B_FALSE))
876 		return (DDI_EPENDING);
877 
878 	update_avsoftintr_args((void *)hdlp, hdlp->ih_pri, arg2);
879 
880 	(*setsoftint)(hdlp->ih_pri, hdlp->ih_pending);
881 	return (DDI_SUCCESS);
882 }
883 
884 /*
885  * i_ddi_set_softint_pri:
886  *
887  * The way this works is that it first tries to add a softint vector
888  * at the new priority in hdlp. If that succeeds; then it removes the
889  * existing softint vector at the old priority.
890  */
891 int
892 i_ddi_set_softint_pri(ddi_softint_hdl_impl_t *hdlp, uint_t old_pri)
893 {
894 	int ret;
895 
896 	/*
897 	 * If a softint is pending at the old priority then fail the request.
898 	 */
899 	if (av_check_softint_pending(hdlp->ih_pending, B_TRUE))
900 		return (DDI_FAILURE);
901 
902 	ret = av_softint_movepri((void *)hdlp, old_pri);
903 	return (ret ? DDI_SUCCESS : DDI_FAILURE);
904 }
905 
906 void
907 i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t *hdlp)
908 {
909 	hdlp->ih_private = (void *)kmem_zalloc(sizeof (ihdl_plat_t), KM_SLEEP);
910 }
911 
912 void
913 i_ddi_free_intr_phdl(ddi_intr_handle_impl_t *hdlp)
914 {
915 	kmem_free(hdlp->ih_private, sizeof (ihdl_plat_t));
916 	hdlp->ih_private = NULL;
917 }
918 
919 int
920 i_ddi_get_intx_nintrs(dev_info_t *dip)
921 {
922 	struct ddi_parent_private_data *pdp;
923 
924 	if ((pdp = ddi_get_parent_data(dip)) == NULL)
925 		return (0);
926 
927 	return (pdp->par_nintr);
928 }
929 
930 /*
931  * DDI Memory/DMA
932  */
933 
934 /*
935  * Support for allocating DMAable memory to implement
936  * ddi_dma_mem_alloc(9F) interface.
937  */
938 
939 #define	KA_ALIGN_SHIFT	7
940 #define	KA_ALIGN	(1 << KA_ALIGN_SHIFT)
941 #define	KA_NCACHE	(PAGESHIFT + 1 - KA_ALIGN_SHIFT)
942 
943 /*
944  * Dummy DMA attribute template for kmem_io[].kmem_io_attr.  We only
945  * care about addr_lo, addr_hi, and align.  addr_hi will be dynamically set.
946  */
947 
948 static ddi_dma_attr_t kmem_io_attr = {
949 	DMA_ATTR_V0,
950 	0x0000000000000000ULL,		/* dma_attr_addr_lo */
951 	0x0000000000000000ULL,		/* dma_attr_addr_hi */
952 	0x00ffffff,
953 	0x1000,				/* dma_attr_align */
954 	1, 1, 0xffffffffULL, 0xffffffffULL, 0x1, 1, 0
955 };
956 
957 /* kmem io memory ranges and indices */
958 enum {
959 	IO_4P, IO_64G, IO_4G, IO_2G, IO_1G, IO_512M,
960 	IO_256M, IO_128M, IO_64M, IO_32M, IO_16M, MAX_MEM_RANGES
961 };
962 
963 static struct {
964 	vmem_t		*kmem_io_arena;
965 	kmem_cache_t	*kmem_io_cache[KA_NCACHE];
966 	ddi_dma_attr_t	kmem_io_attr;
967 } kmem_io[MAX_MEM_RANGES];
968 
969 static int kmem_io_idx;		/* index of first populated kmem_io[] */
970 
971 static page_t *
972 page_create_io_wrapper(void *addr, size_t len, int vmflag, void *arg)
973 {
974 	extern page_t *page_create_io(vnode_t *, u_offset_t, uint_t,
975 	    uint_t, struct as *, caddr_t, ddi_dma_attr_t *);
976 
977 	return (page_create_io(&kvp, (u_offset_t)(uintptr_t)addr, len,
978 	    PG_EXCL | ((vmflag & VM_NOSLEEP) ? 0 : PG_WAIT), &kas, addr, arg));
979 }
980 
981 #ifdef __xpv
982 static void
983 segkmem_free_io(vmem_t *vmp, void * ptr, size_t size)
984 {
985 	extern void page_destroy_io(page_t *);
986 	segkmem_xfree(vmp, ptr, size, page_destroy_io);
987 }
988 #endif
989 
990 static void *
991 segkmem_alloc_io_4P(vmem_t *vmp, size_t size, int vmflag)
992 {
993 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
994 	    page_create_io_wrapper, &kmem_io[IO_4P].kmem_io_attr));
995 }
996 
997 static void *
998 segkmem_alloc_io_64G(vmem_t *vmp, size_t size, int vmflag)
999 {
1000 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1001 	    page_create_io_wrapper, &kmem_io[IO_64G].kmem_io_attr));
1002 }
1003 
1004 static void *
1005 segkmem_alloc_io_4G(vmem_t *vmp, size_t size, int vmflag)
1006 {
1007 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1008 	    page_create_io_wrapper, &kmem_io[IO_4G].kmem_io_attr));
1009 }
1010 
1011 static void *
1012 segkmem_alloc_io_2G(vmem_t *vmp, size_t size, int vmflag)
1013 {
1014 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1015 	    page_create_io_wrapper, &kmem_io[IO_2G].kmem_io_attr));
1016 }
1017 
1018 static void *
1019 segkmem_alloc_io_1G(vmem_t *vmp, size_t size, int vmflag)
1020 {
1021 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1022 	    page_create_io_wrapper, &kmem_io[IO_1G].kmem_io_attr));
1023 }
1024 
1025 static void *
1026 segkmem_alloc_io_512M(vmem_t *vmp, size_t size, int vmflag)
1027 {
1028 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1029 	    page_create_io_wrapper, &kmem_io[IO_512M].kmem_io_attr));
1030 }
1031 
1032 static void *
1033 segkmem_alloc_io_256M(vmem_t *vmp, size_t size, int vmflag)
1034 {
1035 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1036 	    page_create_io_wrapper, &kmem_io[IO_256M].kmem_io_attr));
1037 }
1038 
1039 static void *
1040 segkmem_alloc_io_128M(vmem_t *vmp, size_t size, int vmflag)
1041 {
1042 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1043 	    page_create_io_wrapper, &kmem_io[IO_128M].kmem_io_attr));
1044 }
1045 
1046 static void *
1047 segkmem_alloc_io_64M(vmem_t *vmp, size_t size, int vmflag)
1048 {
1049 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1050 	    page_create_io_wrapper, &kmem_io[IO_64M].kmem_io_attr));
1051 }
1052 
1053 static void *
1054 segkmem_alloc_io_32M(vmem_t *vmp, size_t size, int vmflag)
1055 {
1056 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1057 	    page_create_io_wrapper, &kmem_io[IO_32M].kmem_io_attr));
1058 }
1059 
1060 static void *
1061 segkmem_alloc_io_16M(vmem_t *vmp, size_t size, int vmflag)
1062 {
1063 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
1064 	    page_create_io_wrapper, &kmem_io[IO_16M].kmem_io_attr));
1065 }
1066 
1067 struct {
1068 	uint64_t	io_limit;
1069 	char		*io_name;
1070 	void		*(*io_alloc)(vmem_t *, size_t, int);
1071 	int		io_initial;	/* kmem_io_init during startup */
1072 } io_arena_params[MAX_MEM_RANGES] = {
1073 	{0x000fffffffffffffULL,	"kmem_io_4P",	segkmem_alloc_io_4P,	1},
1074 	{0x0000000fffffffffULL,	"kmem_io_64G",	segkmem_alloc_io_64G,	0},
1075 	{0x00000000ffffffffULL,	"kmem_io_4G",	segkmem_alloc_io_4G,	1},
1076 	{0x000000007fffffffULL,	"kmem_io_2G",	segkmem_alloc_io_2G,	1},
1077 	{0x000000003fffffffULL,	"kmem_io_1G",	segkmem_alloc_io_1G,	0},
1078 	{0x000000001fffffffULL,	"kmem_io_512M",	segkmem_alloc_io_512M,	0},
1079 	{0x000000000fffffffULL,	"kmem_io_256M",	segkmem_alloc_io_256M,	0},
1080 	{0x0000000007ffffffULL,	"kmem_io_128M",	segkmem_alloc_io_128M,	0},
1081 	{0x0000000003ffffffULL,	"kmem_io_64M",	segkmem_alloc_io_64M,	0},
1082 	{0x0000000001ffffffULL,	"kmem_io_32M",	segkmem_alloc_io_32M,	0},
1083 	{0x0000000000ffffffULL,	"kmem_io_16M",	segkmem_alloc_io_16M,	1}
1084 };
1085 
1086 void
1087 kmem_io_init(int a)
1088 {
1089 	int	c;
1090 	char name[40];
1091 
1092 	kmem_io[a].kmem_io_arena = vmem_create(io_arena_params[a].io_name,
1093 	    NULL, 0, PAGESIZE, io_arena_params[a].io_alloc,
1094 #ifdef __xpv
1095 	    segkmem_free_io,
1096 #else
1097 	    segkmem_free,
1098 #endif
1099 	    heap_arena, 0, VM_SLEEP);
1100 
1101 	for (c = 0; c < KA_NCACHE; c++) {
1102 		size_t size = KA_ALIGN << c;
1103 		(void) sprintf(name, "%s_%lu",
1104 		    io_arena_params[a].io_name, size);
1105 		kmem_io[a].kmem_io_cache[c] = kmem_cache_create(name,
1106 		    size, size, NULL, NULL, NULL, NULL,
1107 		    kmem_io[a].kmem_io_arena, 0);
1108 	}
1109 }
1110 
1111 /*
1112  * Return the index of the highest memory range for addr.
1113  */
1114 static int
1115 kmem_io_index(uint64_t addr)
1116 {
1117 	int n;
1118 
1119 	for (n = kmem_io_idx; n < MAX_MEM_RANGES; n++) {
1120 		if (kmem_io[n].kmem_io_attr.dma_attr_addr_hi <= addr) {
1121 			if (kmem_io[n].kmem_io_arena == NULL)
1122 				kmem_io_init(n);
1123 			return (n);
1124 		}
1125 	}
1126 	panic("kmem_io_index: invalid addr - must be at least 16m");
1127 
1128 	/*NOTREACHED*/
1129 }
1130 
1131 /*
1132  * Return the index of the next kmem_io populated memory range
1133  * after curindex.
1134  */
1135 static int
1136 kmem_io_index_next(int curindex)
1137 {
1138 	int n;
1139 
1140 	for (n = curindex + 1; n < MAX_MEM_RANGES; n++) {
1141 		if (kmem_io[n].kmem_io_arena)
1142 			return (n);
1143 	}
1144 	return (-1);
1145 }
1146 
1147 /*
1148  * allow kmem to be mapped in with different PTE cache attribute settings.
1149  * Used by i_ddi_mem_alloc()
1150  */
1151 int
1152 kmem_override_cache_attrs(caddr_t kva, size_t size, uint_t order)
1153 {
1154 	uint_t hat_flags;
1155 	caddr_t kva_end;
1156 	uint_t hat_attr;
1157 	pfn_t pfn;
1158 
1159 	if (hat_getattr(kas.a_hat, kva, &hat_attr) == -1) {
1160 		return (-1);
1161 	}
1162 
1163 	hat_attr &= ~HAT_ORDER_MASK;
1164 	hat_attr |= order | HAT_NOSYNC;
1165 	hat_flags = HAT_LOAD_LOCK;
1166 
1167 	kva_end = (caddr_t)(((uintptr_t)kva + size + PAGEOFFSET) &
1168 	    (uintptr_t)PAGEMASK);
1169 	kva = (caddr_t)((uintptr_t)kva & (uintptr_t)PAGEMASK);
1170 
1171 	while (kva < kva_end) {
1172 		pfn = hat_getpfnum(kas.a_hat, kva);
1173 		hat_unload(kas.a_hat, kva, PAGESIZE, HAT_UNLOAD_UNLOCK);
1174 		hat_devload(kas.a_hat, kva, PAGESIZE, pfn, hat_attr, hat_flags);
1175 		kva += MMU_PAGESIZE;
1176 	}
1177 
1178 	return (0);
1179 }
1180 
1181 void
1182 ka_init(void)
1183 {
1184 	int a;
1185 	paddr_t maxphysaddr;
1186 #if !defined(__xpv)
1187 	extern pfn_t physmax;
1188 
1189 	maxphysaddr = mmu_ptob((paddr_t)physmax) + MMU_PAGEOFFSET;
1190 #else
1191 	maxphysaddr = mmu_ptob((paddr_t)HYPERVISOR_memory_op(
1192 	    XENMEM_maximum_ram_page, NULL)) + MMU_PAGEOFFSET;
1193 #endif
1194 
1195 	ASSERT(maxphysaddr <= io_arena_params[0].io_limit);
1196 
1197 	for (a = 0; a < MAX_MEM_RANGES; a++) {
1198 		if (maxphysaddr >= io_arena_params[a + 1].io_limit) {
1199 			if (maxphysaddr > io_arena_params[a + 1].io_limit)
1200 				io_arena_params[a].io_limit = maxphysaddr;
1201 			else
1202 				a++;
1203 			break;
1204 		}
1205 	}
1206 	kmem_io_idx = a;
1207 
1208 	for (; a < MAX_MEM_RANGES; a++) {
1209 		kmem_io[a].kmem_io_attr = kmem_io_attr;
1210 		kmem_io[a].kmem_io_attr.dma_attr_addr_hi =
1211 		    io_arena_params[a].io_limit;
1212 		/*
1213 		 * initialize kmem_io[] arena/cache corresponding to
1214 		 * maxphysaddr and to the "common" io memory ranges that
1215 		 * have io_initial set to a non-zero value.
1216 		 */
1217 		if (io_arena_params[a].io_initial || a == kmem_io_idx)
1218 			kmem_io_init(a);
1219 	}
1220 }
1221 
1222 /*
1223  * put contig address/size
1224  */
1225 static void *
1226 putctgas(void *addr, size_t size)
1227 {
1228 	struct ctgas	*ctgp = &ctglist;
1229 	int		i;
1230 
1231 	CTGLOCK();
1232 	do {
1233 		if ((i = ctgp->ctg_index) < CTGENTRIES) {
1234 			ctgp->ctg_addr[i] = addr;
1235 			ctgp->ctg_size[i] = size;
1236 			ctgp->ctg_index++;
1237 			break;
1238 		}
1239 		if (!ctgp->ctg_next)
1240 			ctgp->ctg_next = kmem_zalloc(sizeof (struct ctgas),
1241 			    KM_NOSLEEP);
1242 		ctgp = ctgp->ctg_next;
1243 	} while (ctgp);
1244 
1245 	CTGUNLOCK();
1246 	return (ctgp);
1247 }
1248 
1249 /*
1250  * get contig size by addr
1251  */
1252 static size_t
1253 getctgsz(void *addr)
1254 {
1255 	struct ctgas	*ctgp = &ctglist;
1256 	int		i, j;
1257 	size_t		sz;
1258 
1259 	ASSERT(addr);
1260 	CTGLOCK();
1261 
1262 	while (ctgp) {
1263 		for (i = 0; i < ctgp->ctg_index; i++) {
1264 			if (addr != ctgp->ctg_addr[i])
1265 				continue;
1266 
1267 			sz = ctgp->ctg_size[i];
1268 			j = --ctgp->ctg_index;
1269 			if (i != j) {
1270 				ctgp->ctg_size[i] = ctgp->ctg_size[j];
1271 				ctgp->ctg_addr[i] = ctgp->ctg_addr[j];
1272 			}
1273 			CTGUNLOCK();
1274 			return (sz);
1275 		}
1276 		ctgp = ctgp->ctg_next;
1277 	}
1278 
1279 	CTGUNLOCK();
1280 	return (0);
1281 }
1282 
1283 /*
1284  * contig_alloc:
1285  *
1286  *	allocates contiguous memory to satisfy the 'size' and dma attributes
1287  *	specified in 'attr'.
1288  *
1289  *	Not all of memory need to be physically contiguous if the
1290  *	scatter-gather list length is greater than 1.
1291  */
1292 
1293 /*ARGSUSED*/
1294 void *
1295 contig_alloc(size_t size, ddi_dma_attr_t *attr, uintptr_t align, int cansleep)
1296 {
1297 	pgcnt_t		pgcnt = btopr(size);
1298 	size_t		asize = pgcnt * PAGESIZE;
1299 	page_t		*ppl;
1300 	int		pflag;
1301 	void		*addr;
1302 
1303 	extern page_t *page_create_io(vnode_t *, u_offset_t, uint_t,
1304 	    uint_t, struct as *, caddr_t, ddi_dma_attr_t *);
1305 
1306 	/* segkmem_xalloc */
1307 
1308 	if (align <= PAGESIZE)
1309 		addr = vmem_alloc(heap_arena, asize,
1310 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
1311 	else
1312 		addr = vmem_xalloc(heap_arena, asize, align, 0, 0, NULL, NULL,
1313 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
1314 	if (addr) {
1315 		ASSERT(!((uintptr_t)addr & (align - 1)));
1316 
1317 		if (page_resv(pgcnt, (cansleep) ? KM_SLEEP : KM_NOSLEEP) == 0) {
1318 			vmem_free(heap_arena, addr, asize);
1319 			return (NULL);
1320 		}
1321 		pflag = PG_EXCL;
1322 
1323 		if (cansleep)
1324 			pflag |= PG_WAIT;
1325 
1326 		/* 4k req gets from freelists rather than pfn search */
1327 		if (pgcnt > 1 || align > PAGESIZE)
1328 			pflag |= PG_PHYSCONTIG;
1329 
1330 		ppl = page_create_io(&kvp, (u_offset_t)(uintptr_t)addr,
1331 		    asize, pflag, &kas, (caddr_t)addr, attr);
1332 
1333 		if (!ppl) {
1334 			vmem_free(heap_arena, addr, asize);
1335 			page_unresv(pgcnt);
1336 			return (NULL);
1337 		}
1338 
1339 		while (ppl != NULL) {
1340 			page_t	*pp = ppl;
1341 			page_sub(&ppl, pp);
1342 			ASSERT(page_iolock_assert(pp));
1343 			page_io_unlock(pp);
1344 			page_downgrade(pp);
1345 			hat_memload(kas.a_hat, (caddr_t)(uintptr_t)pp->p_offset,
1346 			    pp, (PROT_ALL & ~PROT_USER) |
1347 			    HAT_NOSYNC, HAT_LOAD_LOCK);
1348 		}
1349 	}
1350 	return (addr);
1351 }
1352 
1353 static void
1354 contig_free(void *addr, size_t size)
1355 {
1356 	pgcnt_t	pgcnt = btopr(size);
1357 	size_t	asize = pgcnt * PAGESIZE;
1358 	caddr_t	a, ea;
1359 	page_t	*pp;
1360 
1361 	hat_unload(kas.a_hat, addr, asize, HAT_UNLOAD_UNLOCK);
1362 
1363 	for (a = addr, ea = a + asize; a < ea; a += PAGESIZE) {
1364 		pp = page_find(&kvp, (u_offset_t)(uintptr_t)a);
1365 		if (!pp)
1366 			panic("contig_free: contig pp not found");
1367 
1368 		if (!page_tryupgrade(pp)) {
1369 			page_unlock(pp);
1370 			pp = page_lookup(&kvp,
1371 			    (u_offset_t)(uintptr_t)a, SE_EXCL);
1372 			if (pp == NULL)
1373 				panic("contig_free: page freed");
1374 		}
1375 		page_destroy(pp, 0);
1376 	}
1377 
1378 	page_unresv(pgcnt);
1379 	vmem_free(heap_arena, addr, asize);
1380 }
1381 
1382 /*
1383  * Allocate from the system, aligned on a specific boundary.
1384  * The alignment, if non-zero, must be a power of 2.
1385  */
1386 static void *
1387 kalloca(size_t size, size_t align, int cansleep, int physcontig,
1388 	ddi_dma_attr_t *attr)
1389 {
1390 	size_t *addr, *raddr, rsize;
1391 	size_t hdrsize = 4 * sizeof (size_t);	/* must be power of 2 */
1392 	int a, i, c;
1393 	vmem_t *vmp;
1394 	kmem_cache_t *cp = NULL;
1395 
1396 	if (attr->dma_attr_addr_lo > mmu_ptob((uint64_t)ddiphysmin))
1397 		return (NULL);
1398 
1399 	align = MAX(align, hdrsize);
1400 	ASSERT((align & (align - 1)) == 0);
1401 
1402 	/*
1403 	 * All of our allocators guarantee 16-byte alignment, so we don't
1404 	 * need to reserve additional space for the header.
1405 	 * To simplify picking the correct kmem_io_cache, we round up to
1406 	 * a multiple of KA_ALIGN.
1407 	 */
1408 	rsize = P2ROUNDUP_TYPED(size + align, KA_ALIGN, size_t);
1409 
1410 	if (physcontig && rsize > PAGESIZE) {
1411 		if (addr = contig_alloc(size, attr, align, cansleep)) {
1412 			if (!putctgas(addr, size))
1413 				contig_free(addr, size);
1414 			else
1415 				return (addr);
1416 		}
1417 		return (NULL);
1418 	}
1419 
1420 	a = kmem_io_index(attr->dma_attr_addr_hi);
1421 
1422 	if (rsize > PAGESIZE) {
1423 		vmp = kmem_io[a].kmem_io_arena;
1424 		raddr = vmem_alloc(vmp, rsize,
1425 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
1426 	} else {
1427 		c = highbit((rsize >> KA_ALIGN_SHIFT) - 1);
1428 		cp = kmem_io[a].kmem_io_cache[c];
1429 		raddr = kmem_cache_alloc(cp, (cansleep) ? KM_SLEEP :
1430 		    KM_NOSLEEP);
1431 	}
1432 
1433 	if (raddr == NULL) {
1434 		int	na;
1435 
1436 		ASSERT(cansleep == 0);
1437 		if (rsize > PAGESIZE)
1438 			return (NULL);
1439 		/*
1440 		 * System does not have memory in the requested range.
1441 		 * Try smaller kmem io ranges and larger cache sizes
1442 		 * to see if there might be memory available in
1443 		 * these other caches.
1444 		 */
1445 
1446 		for (na = kmem_io_index_next(a); na >= 0;
1447 		    na = kmem_io_index_next(na)) {
1448 			ASSERT(kmem_io[na].kmem_io_arena);
1449 			cp = kmem_io[na].kmem_io_cache[c];
1450 			raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
1451 			if (raddr)
1452 				goto kallocdone;
1453 		}
1454 		/* now try the larger kmem io cache sizes */
1455 		for (na = a; na >= 0; na = kmem_io_index_next(na)) {
1456 			for (i = c + 1; i < KA_NCACHE; i++) {
1457 				cp = kmem_io[na].kmem_io_cache[i];
1458 				raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
1459 				if (raddr)
1460 					goto kallocdone;
1461 			}
1462 		}
1463 		return (NULL);
1464 	}
1465 
1466 kallocdone:
1467 	ASSERT(!P2CROSS((uintptr_t)raddr, (uintptr_t)raddr + rsize - 1,
1468 	    PAGESIZE) || rsize > PAGESIZE);
1469 
1470 	addr = (size_t *)P2ROUNDUP((uintptr_t)raddr + hdrsize, align);
1471 	ASSERT((uintptr_t)addr + size - (uintptr_t)raddr <= rsize);
1472 
1473 	addr[-4] = (size_t)cp;
1474 	addr[-3] = (size_t)vmp;
1475 	addr[-2] = (size_t)raddr;
1476 	addr[-1] = rsize;
1477 
1478 	return (addr);
1479 }
1480 
1481 static void
1482 kfreea(void *addr)
1483 {
1484 	size_t		size;
1485 
1486 	if (!((uintptr_t)addr & PAGEOFFSET) && (size = getctgsz(addr))) {
1487 		contig_free(addr, size);
1488 	} else {
1489 		size_t	*saddr = addr;
1490 		if (saddr[-4] == 0)
1491 			vmem_free((vmem_t *)saddr[-3], (void *)saddr[-2],
1492 			    saddr[-1]);
1493 		else
1494 			kmem_cache_free((kmem_cache_t *)saddr[-4],
1495 			    (void *)saddr[-2]);
1496 	}
1497 }
1498 
1499 /*ARGSUSED*/
1500 void
1501 i_ddi_devacc_to_hatacc(ddi_device_acc_attr_t *devaccp, uint_t *hataccp)
1502 {
1503 }
1504 
1505 /*
1506  * Check if the specified cache attribute is supported on the platform.
1507  * This function must be called before i_ddi_cacheattr_to_hatacc().
1508  */
1509 boolean_t
1510 i_ddi_check_cache_attr(uint_t flags)
1511 {
1512 	/*
1513 	 * The cache attributes are mutually exclusive. Any combination of
1514 	 * the attributes leads to a failure.
1515 	 */
1516 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
1517 	if ((cache_attr != 0) && ((cache_attr & (cache_attr - 1)) != 0))
1518 		return (B_FALSE);
1519 
1520 	/* All cache attributes are supported on X86/X64 */
1521 	if (cache_attr & (IOMEM_DATA_UNCACHED | IOMEM_DATA_CACHED |
1522 	    IOMEM_DATA_UC_WR_COMBINE))
1523 		return (B_TRUE);
1524 
1525 	/* undefined attributes */
1526 	return (B_FALSE);
1527 }
1528 
1529 /* set HAT cache attributes from the cache attributes */
1530 void
1531 i_ddi_cacheattr_to_hatacc(uint_t flags, uint_t *hataccp)
1532 {
1533 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
1534 	static char *fname = "i_ddi_cacheattr_to_hatacc";
1535 
1536 	/*
1537 	 * If write-combining is not supported, then it falls back
1538 	 * to uncacheable.
1539 	 */
1540 	if (cache_attr == IOMEM_DATA_UC_WR_COMBINE && !(x86_feature & X86_PAT))
1541 		cache_attr = IOMEM_DATA_UNCACHED;
1542 
1543 	/*
1544 	 * set HAT attrs according to the cache attrs.
1545 	 */
1546 	switch (cache_attr) {
1547 	case IOMEM_DATA_UNCACHED:
1548 		*hataccp &= ~HAT_ORDER_MASK;
1549 		*hataccp |= (HAT_STRICTORDER | HAT_PLAT_NOCACHE);
1550 		break;
1551 	case IOMEM_DATA_UC_WR_COMBINE:
1552 		*hataccp &= ~HAT_ORDER_MASK;
1553 		*hataccp |= (HAT_MERGING_OK | HAT_PLAT_NOCACHE);
1554 		break;
1555 	case IOMEM_DATA_CACHED:
1556 		*hataccp &= ~HAT_ORDER_MASK;
1557 		*hataccp |= HAT_UNORDERED_OK;
1558 		break;
1559 	/*
1560 	 * This case must not occur because the cache attribute is scrutinized
1561 	 * before this function is called.
1562 	 */
1563 	default:
1564 		/*
1565 		 * set cacheable to hat attrs.
1566 		 */
1567 		*hataccp &= ~HAT_ORDER_MASK;
1568 		*hataccp |= HAT_UNORDERED_OK;
1569 		cmn_err(CE_WARN, "%s: cache_attr=0x%x is ignored.",
1570 		    fname, cache_attr);
1571 	}
1572 }
1573 
1574 /*
1575  * This should actually be called i_ddi_dma_mem_alloc. There should
1576  * also be an i_ddi_pio_mem_alloc. i_ddi_dma_mem_alloc should call
1577  * through the device tree with the DDI_CTLOPS_DMA_ALIGN ctl ops to
1578  * get alignment requirements for DMA memory. i_ddi_pio_mem_alloc
1579  * should use DDI_CTLOPS_PIO_ALIGN. Since we only have i_ddi_mem_alloc
1580  * so far which is used for both, DMA and PIO, we have to use the DMA
1581  * ctl ops to make everybody happy.
1582  */
1583 /*ARGSUSED*/
1584 int
1585 i_ddi_mem_alloc(dev_info_t *dip, ddi_dma_attr_t *attr,
1586 	size_t length, int cansleep, int flags,
1587 	ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
1588 	size_t *real_length, ddi_acc_hdl_t *ap)
1589 {
1590 	caddr_t a;
1591 	int iomin;
1592 	ddi_acc_impl_t *iap;
1593 	int physcontig = 0;
1594 	pgcnt_t npages;
1595 	pgcnt_t minctg;
1596 	uint_t order;
1597 	int e;
1598 
1599 	/*
1600 	 * Check legality of arguments
1601 	 */
1602 	if (length == 0 || kaddrp == NULL || attr == NULL) {
1603 		return (DDI_FAILURE);
1604 	}
1605 
1606 	if (attr->dma_attr_minxfer == 0 || attr->dma_attr_align == 0 ||
1607 	    (attr->dma_attr_align & (attr->dma_attr_align - 1)) ||
1608 	    (attr->dma_attr_minxfer & (attr->dma_attr_minxfer - 1))) {
1609 			return (DDI_FAILURE);
1610 	}
1611 
1612 	/*
1613 	 * figure out most restrictive alignment requirement
1614 	 */
1615 	iomin = attr->dma_attr_minxfer;
1616 	iomin = maxbit(iomin, attr->dma_attr_align);
1617 	if (iomin == 0)
1618 		return (DDI_FAILURE);
1619 
1620 	ASSERT((iomin & (iomin - 1)) == 0);
1621 
1622 	/*
1623 	 * if we allocate memory with IOMEM_DATA_UNCACHED or
1624 	 * IOMEM_DATA_UC_WR_COMBINE, make sure we allocate a page aligned
1625 	 * memory that ends on a page boundry.
1626 	 * Don't want to have to different cache mappings to the same
1627 	 * physical page.
1628 	 */
1629 	if (OVERRIDE_CACHE_ATTR(flags)) {
1630 		iomin = (iomin + MMU_PAGEOFFSET) & MMU_PAGEMASK;
1631 		length = (length + MMU_PAGEOFFSET) & (size_t)MMU_PAGEMASK;
1632 	}
1633 
1634 	/*
1635 	 * Determine if we need to satisfy the request for physically
1636 	 * contiguous memory or alignments larger than pagesize.
1637 	 */
1638 	npages = btopr(length + attr->dma_attr_align);
1639 	minctg = howmany(npages, attr->dma_attr_sgllen);
1640 
1641 	if (minctg > 1) {
1642 		uint64_t pfnseg = attr->dma_attr_seg >> PAGESHIFT;
1643 		/*
1644 		 * verify that the minimum contig requirement for the
1645 		 * actual length does not cross segment boundary.
1646 		 */
1647 		length = P2ROUNDUP_TYPED(length, attr->dma_attr_minxfer,
1648 		    size_t);
1649 		npages = btopr(length);
1650 		minctg = howmany(npages, attr->dma_attr_sgllen);
1651 		if (minctg > pfnseg + 1)
1652 			return (DDI_FAILURE);
1653 		physcontig = 1;
1654 	} else {
1655 		length = P2ROUNDUP_TYPED(length, iomin, size_t);
1656 	}
1657 
1658 	/*
1659 	 * Allocate the requested amount from the system.
1660 	 */
1661 	a = kalloca(length, iomin, cansleep, physcontig, attr);
1662 
1663 	if ((*kaddrp = a) == NULL)
1664 		return (DDI_FAILURE);
1665 
1666 	/*
1667 	 * if we to modify the cache attributes, go back and muck with the
1668 	 * mappings.
1669 	 */
1670 	if (OVERRIDE_CACHE_ATTR(flags)) {
1671 		order = 0;
1672 		i_ddi_cacheattr_to_hatacc(flags, &order);
1673 		e = kmem_override_cache_attrs(a, length, order);
1674 		if (e != 0) {
1675 			kfreea(a);
1676 			return (DDI_FAILURE);
1677 		}
1678 	}
1679 
1680 	if (real_length) {
1681 		*real_length = length;
1682 	}
1683 	if (ap) {
1684 		/*
1685 		 * initialize access handle
1686 		 */
1687 		iap = (ddi_acc_impl_t *)ap->ah_platform_private;
1688 		iap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR;
1689 		impl_acc_hdl_init(ap);
1690 	}
1691 
1692 	return (DDI_SUCCESS);
1693 }
1694 
1695 /*
1696  * covert old DMA limits structure to DMA attribute structure
1697  * and continue
1698  */
1699 int
1700 i_ddi_mem_alloc_lim(dev_info_t *dip, ddi_dma_lim_t *limits,
1701 	size_t length, int cansleep, int streaming,
1702 	ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
1703 	uint_t *real_length, ddi_acc_hdl_t *ap)
1704 {
1705 	ddi_dma_attr_t dma_attr, *attrp;
1706 	size_t rlen;
1707 	int ret;
1708 
1709 	if (limits == NULL) {
1710 		return (DDI_FAILURE);
1711 	}
1712 
1713 	/*
1714 	 * set up DMA attribute structure to pass to i_ddi_mem_alloc()
1715 	 */
1716 	attrp = &dma_attr;
1717 	attrp->dma_attr_version = DMA_ATTR_V0;
1718 	attrp->dma_attr_addr_lo = (uint64_t)limits->dlim_addr_lo;
1719 	attrp->dma_attr_addr_hi = (uint64_t)limits->dlim_addr_hi;
1720 	attrp->dma_attr_count_max = (uint64_t)limits->dlim_ctreg_max;
1721 	attrp->dma_attr_align = 1;
1722 	attrp->dma_attr_burstsizes = (uint_t)limits->dlim_burstsizes;
1723 	attrp->dma_attr_minxfer = (uint32_t)limits->dlim_minxfer;
1724 	attrp->dma_attr_maxxfer = (uint64_t)limits->dlim_reqsize;
1725 	attrp->dma_attr_seg = (uint64_t)limits->dlim_adreg_max;
1726 	attrp->dma_attr_sgllen = limits->dlim_sgllen;
1727 	attrp->dma_attr_granular = (uint32_t)limits->dlim_granular;
1728 	attrp->dma_attr_flags = 0;
1729 
1730 	ret = i_ddi_mem_alloc(dip, attrp, length, cansleep, streaming,
1731 	    accattrp, kaddrp, &rlen, ap);
1732 	if (ret == DDI_SUCCESS) {
1733 		if (real_length)
1734 			*real_length = (uint_t)rlen;
1735 	}
1736 	return (ret);
1737 }
1738 
1739 /* ARGSUSED */
1740 void
1741 i_ddi_mem_free(caddr_t kaddr, ddi_acc_hdl_t *ap)
1742 {
1743 	if (ap != NULL) {
1744 		/*
1745 		 * if we modified the cache attributes on alloc, go back and
1746 		 * fix them since this memory could be returned to the
1747 		 * general pool.
1748 		 */
1749 		if (OVERRIDE_CACHE_ATTR(ap->ah_xfermodes)) {
1750 			uint_t order = 0;
1751 			int e;
1752 			i_ddi_cacheattr_to_hatacc(IOMEM_DATA_CACHED, &order);
1753 			e = kmem_override_cache_attrs(kaddr, ap->ah_len, order);
1754 			if (e != 0) {
1755 				cmn_err(CE_WARN, "i_ddi_mem_free() failed to "
1756 				    "override cache attrs, memory leaked\n");
1757 				return;
1758 			}
1759 		}
1760 	}
1761 	kfreea(kaddr);
1762 }
1763 
1764 /*
1765  * Access Barriers
1766  *
1767  */
1768 /*ARGSUSED*/
1769 int
1770 i_ddi_ontrap(ddi_acc_handle_t hp)
1771 {
1772 	return (DDI_FAILURE);
1773 }
1774 
1775 /*ARGSUSED*/
1776 void
1777 i_ddi_notrap(ddi_acc_handle_t hp)
1778 {
1779 }
1780 
1781 
1782 /*
1783  * Misc Functions
1784  */
1785 
1786 /*
1787  * Implementation instance override functions
1788  *
1789  * No override on i86pc
1790  */
1791 /*ARGSUSED*/
1792 uint_t
1793 impl_assign_instance(dev_info_t *dip)
1794 {
1795 	return ((uint_t)-1);
1796 }
1797 
1798 /*ARGSUSED*/
1799 int
1800 impl_keep_instance(dev_info_t *dip)
1801 {
1802 
1803 #if defined(__xpv)
1804 	/*
1805 	 * Do not persist instance numbers assigned to devices in dom0
1806 	 */
1807 	dev_info_t *pdip;
1808 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1809 		if (((pdip = ddi_get_parent(dip)) != NULL) &&
1810 		    (strcmp(ddi_get_name(pdip), "xpvd") == 0))
1811 			return (DDI_SUCCESS);
1812 	}
1813 #endif
1814 	return (DDI_FAILURE);
1815 }
1816 
1817 /*ARGSUSED*/
1818 int
1819 impl_free_instance(dev_info_t *dip)
1820 {
1821 	return (DDI_FAILURE);
1822 }
1823 
1824 /*ARGSUSED*/
1825 int
1826 impl_check_cpu(dev_info_t *devi)
1827 {
1828 	return (DDI_SUCCESS);
1829 }
1830 
1831 /*
1832  * Referenced in common/cpr_driver.c: Power off machine.
1833  * Don't know how to power off i86pc.
1834  */
1835 void
1836 arch_power_down()
1837 {}
1838 
1839 /*
1840  * Copy name to property_name, since name
1841  * is in the low address range below kernelbase.
1842  */
1843 static void
1844 copy_boot_str(const char *boot_str, char *kern_str, int len)
1845 {
1846 	int i = 0;
1847 
1848 	while (i < len - 1 && boot_str[i] != '\0') {
1849 		kern_str[i] = boot_str[i];
1850 		i++;
1851 	}
1852 
1853 	kern_str[i] = 0;	/* null terminate */
1854 	if (boot_str[i] != '\0')
1855 		cmn_err(CE_WARN,
1856 		    "boot property string is truncated to %s", kern_str);
1857 }
1858 
1859 static void
1860 get_boot_properties(void)
1861 {
1862 	extern char hw_provider[];
1863 	dev_info_t *devi;
1864 	char *name;
1865 	int length;
1866 	char property_name[50], property_val[50];
1867 	void *bop_staging_area;
1868 
1869 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP);
1870 
1871 	/*
1872 	 * Import "root" properties from the boot.
1873 	 *
1874 	 * We do this by invoking BOP_NEXTPROP until the list
1875 	 * is completely copied in.
1876 	 */
1877 
1878 	devi = ddi_root_node();
1879 	for (name = BOP_NEXTPROP(bootops, "");		/* get first */
1880 	    name;					/* NULL => DONE */
1881 	    name = BOP_NEXTPROP(bootops, name)) {	/* get next */
1882 
1883 		/* copy string to memory above kernelbase */
1884 		copy_boot_str(name, property_name, 50);
1885 
1886 		/*
1887 		 * Skip vga properties. They will be picked up later
1888 		 * by get_vga_properties.
1889 		 */
1890 		if (strcmp(property_name, "display-edif-block") == 0 ||
1891 		    strcmp(property_name, "display-edif-id") == 0) {
1892 			continue;
1893 		}
1894 
1895 		length = BOP_GETPROPLEN(bootops, property_name);
1896 		if (length == 0)
1897 			continue;
1898 		if (length > MMU_PAGESIZE) {
1899 			cmn_err(CE_NOTE,
1900 			    "boot property %s longer than 0x%x, ignored\n",
1901 			    property_name, MMU_PAGESIZE);
1902 			continue;
1903 		}
1904 		BOP_GETPROP(bootops, property_name, bop_staging_area);
1905 
1906 		/*
1907 		 * special properties:
1908 		 * si-machine, si-hw-provider
1909 		 *	goes to kernel data structures.
1910 		 * bios-boot-device and stdout
1911 		 *	goes to hardware property list so it may show up
1912 		 *	in the prtconf -vp output. This is needed by
1913 		 *	Install/Upgrade. Once we fix install upgrade,
1914 		 *	this can be taken out.
1915 		 */
1916 		if (strcmp(name, "si-machine") == 0) {
1917 			(void) strncpy(utsname.machine, bop_staging_area,
1918 			    SYS_NMLN);
1919 			utsname.machine[SYS_NMLN - 1] = (char)NULL;
1920 		} else if (strcmp(name, "si-hw-provider") == 0) {
1921 			(void) strncpy(hw_provider, bop_staging_area, SYS_NMLN);
1922 			hw_provider[SYS_NMLN - 1] = (char)NULL;
1923 		} else if (strcmp(name, "bios-boot-device") == 0) {
1924 			copy_boot_str(bop_staging_area, property_val, 50);
1925 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, devi,
1926 			    property_name, property_val);
1927 		} else if (strcmp(name, "stdout") == 0) {
1928 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi,
1929 			    property_name, *((int *)bop_staging_area));
1930 		} else {
1931 			/* Property type unknown, use old prop interface */
1932 			(void) e_ddi_prop_create(DDI_DEV_T_NONE, devi,
1933 			    DDI_PROP_CANSLEEP, property_name, bop_staging_area,
1934 			    length);
1935 		}
1936 	}
1937 
1938 	kmem_free(bop_staging_area, MMU_PAGESIZE);
1939 }
1940 
1941 static void
1942 get_vga_properties(void)
1943 {
1944 	dev_info_t *devi;
1945 	major_t major;
1946 	char *name;
1947 	int length;
1948 	char property_val[50];
1949 	void *bop_staging_area;
1950 
1951 	/*
1952 	 * XXXX Hack Allert!
1953 	 * There really needs to be a better way for identifying various
1954 	 * console framebuffers and their related issues.  Till then,
1955 	 * check for this one as a replacement to vgatext.
1956 	 */
1957 	major = ddi_name_to_major("ragexl");
1958 	if (major == (major_t)-1) {
1959 		major = ddi_name_to_major("vgatext");
1960 		if (major == (major_t)-1)
1961 			return;
1962 	}
1963 	devi = devnamesp[major].dn_head;
1964 	if (devi == NULL)
1965 		return;
1966 
1967 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
1968 
1969 	/*
1970 	 * Import "vga" properties from the boot.
1971 	 */
1972 	name = "display-edif-block";
1973 	length = BOP_GETPROPLEN(bootops, name);
1974 	if (length > 0 && length < MMU_PAGESIZE) {
1975 		BOP_GETPROP(bootops, name, bop_staging_area);
1976 		(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
1977 		    devi, name, bop_staging_area, length);
1978 	}
1979 
1980 	/*
1981 	 * kdmconfig is also looking for display-type and
1982 	 * video-adapter-type. We default to color and svga.
1983 	 *
1984 	 * Could it be "monochrome", "vga"?
1985 	 * Nah, you've got to come to the 21st century...
1986 	 * And you can set monitor type manually in kdmconfig
1987 	 * if you are really an old junky.
1988 	 */
1989 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1990 	    devi, "display-type", "color");
1991 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1992 	    devi, "video-adapter-type", "svga");
1993 
1994 	name = "display-edif-id";
1995 	length = BOP_GETPROPLEN(bootops, name);
1996 	if (length > 0 && length < MMU_PAGESIZE) {
1997 		BOP_GETPROP(bootops, name, bop_staging_area);
1998 		copy_boot_str(bop_staging_area, property_val, length);
1999 		(void) ndi_prop_update_string(DDI_DEV_T_NONE,
2000 		    devi, name, property_val);
2001 	}
2002 
2003 	kmem_free(bop_staging_area, MMU_PAGESIZE);
2004 }
2005 
2006 
2007 /*
2008  * This is temporary, but absolutely necessary.  If we are being
2009  * booted with a device tree created by the DevConf project's bootconf
2010  * program, then we have device information nodes that reflect
2011  * reality.  At this point in time in the Solaris release schedule, the
2012  * kernel drivers aren't prepared for reality.  They still depend on their
2013  * own ad-hoc interpretations of the properties created when their .conf
2014  * files were interpreted. These drivers use an "ignore-hardware-nodes"
2015  * property to prevent them from using the nodes passed up from the bootconf
2016  * device tree.
2017  *
2018  * Trying to assemble root file system drivers as we are booting from
2019  * devconf will fail if the kernel driver is basing its name_addr's on the
2020  * psuedo-node device info while the bootpath passed up from bootconf is using
2021  * reality-based name_addrs.  We help the boot along in this case by
2022  * looking at the pre-bootconf bootpath and determining if we would have
2023  * successfully matched if that had been the bootpath we had chosen.
2024  *
2025  * Note that we only even perform this extra check if we've booted
2026  * using bootconf's 1275 compliant bootpath, this is the boot device, and
2027  * we're trying to match the name_addr specified in the 1275 bootpath.
2028  */
2029 
2030 #define	MAXCOMPONENTLEN	32
2031 
2032 int
2033 x86_old_bootpath_name_addr_match(dev_info_t *cdip, char *caddr, char *naddr)
2034 {
2035 	/*
2036 	 *  There are multiple criteria to be met before we can even
2037 	 *  consider allowing a name_addr match here.
2038 	 *
2039 	 *  1) We must have been booted such that the bootconf program
2040 	 *	created device tree nodes and properties.  This can be
2041 	 *	determined by examining the 'bootpath' property.  This
2042 	 *	property will be a non-null string iff bootconf was
2043 	 *	involved in the boot.
2044 	 *
2045 	 *  2) The module that we want to match must be the boot device.
2046 	 *
2047 	 *  3) The instance of the module we are thinking of letting be
2048 	 *	our match must be ignoring hardware nodes.
2049 	 *
2050 	 *  4) The name_addr we want to match must be the name_addr
2051 	 *	specified in the 1275 bootpath.
2052 	 */
2053 	static char bootdev_module[MAXCOMPONENTLEN];
2054 	static char bootdev_oldmod[MAXCOMPONENTLEN];
2055 	static char bootdev_newaddr[MAXCOMPONENTLEN];
2056 	static char bootdev_oldaddr[MAXCOMPONENTLEN];
2057 	static int  quickexit;
2058 
2059 	char *daddr;
2060 	int dlen;
2061 
2062 	char	*lkupname;
2063 	int	rv = DDI_FAILURE;
2064 
2065 	if ((ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
2066 	    "devconf-addr", (caddr_t)&daddr, &dlen) == DDI_PROP_SUCCESS) &&
2067 	    (ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
2068 	    "ignore-hardware-nodes", -1) != -1)) {
2069 		if (strcmp(daddr, caddr) == 0) {
2070 			return (DDI_SUCCESS);
2071 		}
2072 	}
2073 
2074 	if (quickexit)
2075 		return (rv);
2076 
2077 	if (bootdev_module[0] == '\0') {
2078 		char *addrp, *eoaddrp;
2079 		char *busp, *modp, *atp;
2080 		char *bp1275, *bp;
2081 		int  bp1275len, bplen;
2082 
2083 		bp1275 = bp = addrp = eoaddrp = busp = modp = atp = NULL;
2084 
2085 		if (ddi_getlongprop(DDI_DEV_T_ANY,
2086 		    ddi_root_node(), 0, "bootpath",
2087 		    (caddr_t)&bp1275, &bp1275len) != DDI_PROP_SUCCESS ||
2088 		    bp1275len <= 1) {
2089 			/*
2090 			 * We didn't boot from bootconf so we never need to
2091 			 * do any special matches.
2092 			 */
2093 			quickexit = 1;
2094 			if (bp1275)
2095 				kmem_free(bp1275, bp1275len);
2096 			return (rv);
2097 		}
2098 
2099 		if (ddi_getlongprop(DDI_DEV_T_ANY,
2100 		    ddi_root_node(), 0, "boot-path",
2101 		    (caddr_t)&bp, &bplen) != DDI_PROP_SUCCESS || bplen <= 1) {
2102 			/*
2103 			 * No fallback position for matching. This is
2104 			 * certainly unexpected, but we'll handle it
2105 			 * just in case.
2106 			 */
2107 			quickexit = 1;
2108 			kmem_free(bp1275, bp1275len);
2109 			if (bp)
2110 				kmem_free(bp, bplen);
2111 			return (rv);
2112 		}
2113 
2114 		/*
2115 		 *  Determine boot device module and 1275 name_addr
2116 		 *
2117 		 *  bootpath assumed to be of the form /bus/module@name_addr
2118 		 */
2119 		if (busp = strchr(bp1275, '/')) {
2120 			if (modp = strchr(busp + 1, '/')) {
2121 				if (atp = strchr(modp + 1, '@')) {
2122 					*atp = '\0';
2123 					addrp = atp + 1;
2124 					if (eoaddrp = strchr(addrp, '/'))
2125 						*eoaddrp = '\0';
2126 				}
2127 			}
2128 		}
2129 
2130 		if (modp && addrp) {
2131 			(void) strncpy(bootdev_module, modp + 1,
2132 			    MAXCOMPONENTLEN);
2133 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
2134 
2135 			(void) strncpy(bootdev_newaddr, addrp, MAXCOMPONENTLEN);
2136 			bootdev_newaddr[MAXCOMPONENTLEN - 1] = '\0';
2137 		} else {
2138 			quickexit = 1;
2139 			kmem_free(bp1275, bp1275len);
2140 			kmem_free(bp, bplen);
2141 			return (rv);
2142 		}
2143 
2144 		/*
2145 		 *  Determine fallback name_addr
2146 		 *
2147 		 *  10/3/96 - Also save fallback module name because it
2148 		 *  might actually be different than the current module
2149 		 *  name.  E.G., ISA pnp drivers have new names.
2150 		 *
2151 		 *  bootpath assumed to be of the form /bus/module@name_addr
2152 		 */
2153 		addrp = NULL;
2154 		if (busp = strchr(bp, '/')) {
2155 			if (modp = strchr(busp + 1, '/')) {
2156 				if (atp = strchr(modp + 1, '@')) {
2157 					*atp = '\0';
2158 					addrp = atp + 1;
2159 					if (eoaddrp = strchr(addrp, '/'))
2160 						*eoaddrp = '\0';
2161 				}
2162 			}
2163 		}
2164 
2165 		if (modp && addrp) {
2166 			(void) strncpy(bootdev_oldmod, modp + 1,
2167 			    MAXCOMPONENTLEN);
2168 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
2169 
2170 			(void) strncpy(bootdev_oldaddr, addrp, MAXCOMPONENTLEN);
2171 			bootdev_oldaddr[MAXCOMPONENTLEN - 1] = '\0';
2172 		}
2173 
2174 		/* Free up the bootpath storage now that we're done with it. */
2175 		kmem_free(bp1275, bp1275len);
2176 		kmem_free(bp, bplen);
2177 
2178 		if (bootdev_oldaddr[0] == '\0') {
2179 			quickexit = 1;
2180 			return (rv);
2181 		}
2182 	}
2183 
2184 	if (((lkupname = ddi_get_name(cdip)) != NULL) &&
2185 	    (strcmp(bootdev_module, lkupname) == 0 ||
2186 	    strcmp(bootdev_oldmod, lkupname) == 0) &&
2187 	    ((ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
2188 	    "ignore-hardware-nodes", -1) != -1) ||
2189 	    ignore_hardware_nodes) &&
2190 	    strcmp(bootdev_newaddr, caddr) == 0 &&
2191 	    strcmp(bootdev_oldaddr, naddr) == 0) {
2192 		rv = DDI_SUCCESS;
2193 	}
2194 
2195 	return (rv);
2196 }
2197 
2198 /*
2199  * Perform a copy from a memory mapped device (whose devinfo pointer is devi)
2200  * separately mapped at devaddr in the kernel to a kernel buffer at kaddr.
2201  */
2202 /*ARGSUSED*/
2203 int
2204 e_ddi_copyfromdev(dev_info_t *devi,
2205     off_t off, const void *devaddr, void *kaddr, size_t len)
2206 {
2207 	bcopy(devaddr, kaddr, len);
2208 	return (0);
2209 }
2210 
2211 /*
2212  * Perform a copy to a memory mapped device (whose devinfo pointer is devi)
2213  * separately mapped at devaddr in the kernel from a kernel buffer at kaddr.
2214  */
2215 /*ARGSUSED*/
2216 int
2217 e_ddi_copytodev(dev_info_t *devi,
2218     off_t off, const void *kaddr, void *devaddr, size_t len)
2219 {
2220 	bcopy(kaddr, devaddr, len);
2221 	return (0);
2222 }
2223 
2224 
2225 static int
2226 poke_mem(peekpoke_ctlops_t *in_args)
2227 {
2228 	int err = DDI_SUCCESS;
2229 	on_trap_data_t otd;
2230 
2231 	/* Set up protected environment. */
2232 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
2233 		switch (in_args->size) {
2234 		case sizeof (uint8_t):
2235 			*(uint8_t *)(in_args->dev_addr) =
2236 			    *(uint8_t *)in_args->host_addr;
2237 			break;
2238 
2239 		case sizeof (uint16_t):
2240 			*(uint16_t *)(in_args->dev_addr) =
2241 			    *(uint16_t *)in_args->host_addr;
2242 			break;
2243 
2244 		case sizeof (uint32_t):
2245 			*(uint32_t *)(in_args->dev_addr) =
2246 			    *(uint32_t *)in_args->host_addr;
2247 			break;
2248 
2249 		case sizeof (uint64_t):
2250 			*(uint64_t *)(in_args->dev_addr) =
2251 			    *(uint64_t *)in_args->host_addr;
2252 			break;
2253 
2254 		default:
2255 			err = DDI_FAILURE;
2256 			break;
2257 		}
2258 	} else
2259 		err = DDI_FAILURE;
2260 
2261 	/* Take down protected environment. */
2262 	no_trap();
2263 
2264 	return (err);
2265 }
2266 
2267 
2268 static int
2269 peek_mem(peekpoke_ctlops_t *in_args)
2270 {
2271 	int err = DDI_SUCCESS;
2272 	on_trap_data_t otd;
2273 
2274 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
2275 		switch (in_args->size) {
2276 		case sizeof (uint8_t):
2277 			*(uint8_t *)in_args->host_addr =
2278 			    *(uint8_t *)in_args->dev_addr;
2279 			break;
2280 
2281 		case sizeof (uint16_t):
2282 			*(uint16_t *)in_args->host_addr =
2283 			    *(uint16_t *)in_args->dev_addr;
2284 			break;
2285 
2286 		case sizeof (uint32_t):
2287 			*(uint32_t *)in_args->host_addr =
2288 			    *(uint32_t *)in_args->dev_addr;
2289 			break;
2290 
2291 		case sizeof (uint64_t):
2292 			*(uint64_t *)in_args->host_addr =
2293 			    *(uint64_t *)in_args->dev_addr;
2294 			break;
2295 
2296 		default:
2297 			err = DDI_FAILURE;
2298 			break;
2299 		}
2300 	} else
2301 		err = DDI_FAILURE;
2302 
2303 	no_trap();
2304 	return (err);
2305 }
2306 
2307 
2308 /*
2309  * This is called only to process peek/poke when the DIP is NULL.
2310  * Assume that this is for memory, as nexi take care of device safe accesses.
2311  */
2312 int
2313 peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args)
2314 {
2315 	return (cmd == DDI_CTLOPS_PEEK ? peek_mem(in_args) : poke_mem(in_args));
2316 }
2317 
2318 /*
2319  * we've just done a cautious put/get. Check if it was successful by
2320  * calling pci_ereport_post() on all puts and for any gets that return -1
2321  */
2322 static int
2323 pci_peekpoke_check_fma(dev_info_t *dip, void *arg, ddi_ctl_enum_t ctlop)
2324 {
2325 	int	rval = DDI_SUCCESS;
2326 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
2327 	ddi_fm_error_t de;
2328 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
2329 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
2330 	int check_err = 0;
2331 	int repcount = in_args->repcount;
2332 
2333 	if (ctlop == DDI_CTLOPS_POKE &&
2334 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC)
2335 		return (DDI_SUCCESS);
2336 
2337 	if (ctlop == DDI_CTLOPS_PEEK &&
2338 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC) {
2339 		for (; repcount; repcount--) {
2340 			switch (in_args->size) {
2341 			case sizeof (uint8_t):
2342 				if (*(uint8_t *)in_args->host_addr == 0xff)
2343 					check_err = 1;
2344 				break;
2345 			case sizeof (uint16_t):
2346 				if (*(uint16_t *)in_args->host_addr == 0xffff)
2347 					check_err = 1;
2348 				break;
2349 			case sizeof (uint32_t):
2350 				if (*(uint32_t *)in_args->host_addr ==
2351 				    0xffffffff)
2352 					check_err = 1;
2353 				break;
2354 			case sizeof (uint64_t):
2355 				if (*(uint64_t *)in_args->host_addr ==
2356 				    0xffffffffffffffff)
2357 					check_err = 1;
2358 				break;
2359 			}
2360 		}
2361 		if (check_err == 0)
2362 			return (DDI_SUCCESS);
2363 	}
2364 	/*
2365 	 * for a cautious put or get or a non-cautious get that returned -1 call
2366 	 * io framework to see if there really was an error
2367 	 */
2368 	bzero(&de, sizeof (ddi_fm_error_t));
2369 	de.fme_version = DDI_FME_VERSION;
2370 	de.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
2371 	if (hdlp->ah_acc.devacc_attr_access == DDI_CAUTIOUS_ACC) {
2372 		de.fme_flag = DDI_FM_ERR_EXPECTED;
2373 		de.fme_acc_handle = in_args->handle;
2374 	} else if (hdlp->ah_acc.devacc_attr_access == DDI_DEFAULT_ACC) {
2375 		/*
2376 		 * We only get here with DDI_DEFAULT_ACC for config space gets.
2377 		 * Non-hardened drivers may be probing the hardware and
2378 		 * expecting -1 returned. So need to treat errors on
2379 		 * DDI_DEFAULT_ACC as DDI_FM_ERR_EXPECTED.
2380 		 */
2381 		de.fme_flag = DDI_FM_ERR_EXPECTED;
2382 		de.fme_acc_handle = in_args->handle;
2383 	} else {
2384 		/*
2385 		 * Hardened driver doing protected accesses shouldn't
2386 		 * get errors unless there's a hardware problem. Treat
2387 		 * as nonfatal if there's an error, but set UNEXPECTED
2388 		 * so we raise ereports on any errors and potentially
2389 		 * fault the device
2390 		 */
2391 		de.fme_flag = DDI_FM_ERR_UNEXPECTED;
2392 	}
2393 	pci_ereport_post(dip, &de, NULL);
2394 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
2395 	    de.fme_status != DDI_FM_OK) {
2396 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
2397 		rval = DDI_FAILURE;
2398 		errp->err_ena = de.fme_ena;
2399 		errp->err_expected = de.fme_flag;
2400 		errp->err_status = DDI_FM_NONFATAL;
2401 	}
2402 	return (rval);
2403 }
2404 
2405 /*
2406  * pci_peekpoke_check_nofma() is for when an error occurs on a register access
2407  * during pci_ereport_post(). We can't call pci_ereport_post() again or we'd
2408  * recurse, so assume all puts are OK and gets have failed if they return -1
2409  */
2410 static int
2411 pci_peekpoke_check_nofma(void *arg, ddi_ctl_enum_t ctlop)
2412 {
2413 	int rval = DDI_SUCCESS;
2414 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
2415 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
2416 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
2417 	int repcount = in_args->repcount;
2418 
2419 	if (ctlop == DDI_CTLOPS_POKE)
2420 		return (rval);
2421 
2422 	for (; repcount; repcount--) {
2423 		switch (in_args->size) {
2424 		case sizeof (uint8_t):
2425 			if (*(uint8_t *)in_args->host_addr == 0xff)
2426 				rval = DDI_FAILURE;
2427 			break;
2428 		case sizeof (uint16_t):
2429 			if (*(uint16_t *)in_args->host_addr == 0xffff)
2430 				rval = DDI_FAILURE;
2431 			break;
2432 		case sizeof (uint32_t):
2433 			if (*(uint32_t *)in_args->host_addr == 0xffffffff)
2434 				rval = DDI_FAILURE;
2435 			break;
2436 		case sizeof (uint64_t):
2437 			if (*(uint64_t *)in_args->host_addr ==
2438 			    0xffffffffffffffff)
2439 				rval = DDI_FAILURE;
2440 			break;
2441 		}
2442 	}
2443 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
2444 	    rval == DDI_FAILURE) {
2445 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
2446 		errp->err_ena = fm_ena_generate(0, FM_ENA_FMT1);
2447 		errp->err_expected = DDI_FM_ERR_UNEXPECTED;
2448 		errp->err_status = DDI_FM_NONFATAL;
2449 	}
2450 	return (rval);
2451 }
2452 
2453 int
2454 pci_peekpoke_check(dev_info_t *dip, dev_info_t *rdip,
2455 	ddi_ctl_enum_t ctlop, void *arg, void *result,
2456 	int (*handler)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
2457 	void *), kmutex_t *err_mutexp, kmutex_t *peek_poke_mutexp)
2458 {
2459 	int rval;
2460 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
2461 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
2462 
2463 	/*
2464 	 * this function only supports cautious accesses, not peeks/pokes
2465 	 * which don't have a handle
2466 	 */
2467 	if (hp == NULL)
2468 		return (DDI_FAILURE);
2469 
2470 	if (hp->ahi_acc_attr & DDI_ACCATTR_CONFIG_SPACE) {
2471 		if (!mutex_tryenter(err_mutexp)) {
2472 			/*
2473 			 * As this may be a recursive call from within
2474 			 * pci_ereport_post() we can't wait for the mutexes.
2475 			 * Fortunately we know someone is already calling
2476 			 * pci_ereport_post() which will handle the error bits
2477 			 * for us, and as this is a config space access we can
2478 			 * just do the access and check return value for -1
2479 			 * using pci_peekpoke_check_nofma().
2480 			 */
2481 			rval = handler(dip, rdip, ctlop, arg, result);
2482 			if (rval == DDI_SUCCESS)
2483 				rval = pci_peekpoke_check_nofma(arg, ctlop);
2484 			return (rval);
2485 		}
2486 		/*
2487 		 * This can't be a recursive call. Drop the err_mutex and get
2488 		 * both mutexes in the right order. If an error hasn't already
2489 		 * been detected by the ontrap code, use pci_peekpoke_check_fma
2490 		 * which will call pci_ereport_post() to check error status.
2491 		 */
2492 		mutex_exit(err_mutexp);
2493 	}
2494 	mutex_enter(peek_poke_mutexp);
2495 	rval = handler(dip, rdip, ctlop, arg, result);
2496 	if (rval == DDI_SUCCESS) {
2497 		mutex_enter(err_mutexp);
2498 		rval = pci_peekpoke_check_fma(dip, arg, ctlop);
2499 		mutex_exit(err_mutexp);
2500 	}
2501 	mutex_exit(peek_poke_mutexp);
2502 	return (rval);
2503 }
2504 
2505 void
2506 impl_setup_ddi(void)
2507 {
2508 	dev_info_t *xdip, *isa_dip;
2509 	rd_existing_t rd_mem_prop;
2510 	int err;
2511 
2512 	ndi_devi_alloc_sleep(ddi_root_node(), "ramdisk",
2513 	    (pnode_t)DEVI_SID_NODEID, &xdip);
2514 
2515 	(void) BOP_GETPROP(bootops,
2516 	    "ramdisk_start", (void *)&ramdisk_start);
2517 	(void) BOP_GETPROP(bootops,
2518 	    "ramdisk_end", (void *)&ramdisk_end);
2519 
2520 #ifdef __xpv
2521 	ramdisk_start -= ONE_GIG;
2522 	ramdisk_end -= ONE_GIG;
2523 #endif
2524 	rd_mem_prop.phys = ramdisk_start;
2525 	rd_mem_prop.size = ramdisk_end - ramdisk_start + 1;
2526 
2527 	(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, xdip,
2528 	    RD_EXISTING_PROP_NAME, (uchar_t *)&rd_mem_prop,
2529 	    sizeof (rd_mem_prop));
2530 	err = ndi_devi_bind_driver(xdip, 0);
2531 	ASSERT(err == 0);
2532 
2533 	/* isa node */
2534 	ndi_devi_alloc_sleep(ddi_root_node(), "isa",
2535 	    (pnode_t)DEVI_SID_NODEID, &isa_dip);
2536 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
2537 	    "device_type", "isa");
2538 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
2539 	    "bus-type", "isa");
2540 	(void) ndi_devi_bind_driver(isa_dip, 0);
2541 
2542 	/*
2543 	 * Read in the properties from the boot.
2544 	 */
2545 	get_boot_properties();
2546 
2547 	/* do bus dependent probes. */
2548 	impl_bus_initialprobe();
2549 
2550 	/* not framebuffer should be enumerated, if present */
2551 	get_vga_properties();
2552 }
2553 
2554 dev_t
2555 getrootdev(void)
2556 {
2557 	/*
2558 	 * Precedence given to rootdev if set in /etc/system
2559 	 */
2560 	if (root_is_svm) {
2561 		return (ddi_pathname_to_dev_t(svm_bootpath));
2562 	}
2563 
2564 	/*
2565 	 * Usually rootfs.bo_name is initialized by the
2566 	 * the bootpath property from bootenv.rc, but
2567 	 * defaults to "/ramdisk:a" otherwise.
2568 	 */
2569 	return (ddi_pathname_to_dev_t(rootfs.bo_name));
2570 }
2571 
2572 static struct bus_probe {
2573 	struct bus_probe *next;
2574 	void (*probe)(int);
2575 } *bus_probes;
2576 
2577 void
2578 impl_bus_add_probe(void (*func)(int))
2579 {
2580 	struct bus_probe *probe;
2581 
2582 	probe = kmem_alloc(sizeof (*probe), KM_SLEEP);
2583 	probe->next = bus_probes;
2584 	probe->probe = func;
2585 	bus_probes = probe;
2586 }
2587 
2588 /*ARGSUSED*/
2589 void
2590 impl_bus_delete_probe(void (*func)(int))
2591 {
2592 	struct bus_probe *prev = NULL;
2593 	struct bus_probe *probe = bus_probes;
2594 
2595 	while (probe) {
2596 		if (probe->probe == func)
2597 			break;
2598 		prev = probe;
2599 		probe = probe->next;
2600 	}
2601 
2602 	if (probe == NULL)
2603 		return;
2604 
2605 	if (prev)
2606 		prev->next = probe->next;
2607 	else
2608 		bus_probes = probe->next;
2609 
2610 	kmem_free(probe, sizeof (struct bus_probe));
2611 }
2612 
2613 /*
2614  * impl_bus_initialprobe
2615  *	Modload the prom simulator, then let it probe to verify existence
2616  *	and type of PCI support.
2617  */
2618 static void
2619 impl_bus_initialprobe(void)
2620 {
2621 	struct bus_probe *probe;
2622 
2623 	/* load modules to install bus probes */
2624 #if defined(__xpv)
2625 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
2626 		if (modload("misc", "pci_autoconfig") < 0) {
2627 			panic("failed to load misc/pci_autoconfig");
2628 		}
2629 	}
2630 	(void) modload("misc", "xpv_autoconfig");
2631 #else
2632 	if (modload("misc", "pci_autoconfig") < 0) {
2633 		panic("failed to load misc/pci_autoconfig");
2634 	}
2635 #endif
2636 
2637 	probe = bus_probes;
2638 	while (probe) {
2639 		/* run the probe functions */
2640 		(*probe->probe)(0);
2641 		probe = probe->next;
2642 	}
2643 }
2644 
2645 /*
2646  * impl_bus_reprobe
2647  *	Reprogram devices not set up by firmware.
2648  */
2649 static void
2650 impl_bus_reprobe(void)
2651 {
2652 	struct bus_probe *probe;
2653 
2654 	probe = bus_probes;
2655 	while (probe) {
2656 		/* run the probe function */
2657 		(*probe->probe)(1);
2658 		probe = probe->next;
2659 	}
2660 }
2661 
2662 
2663 /*
2664  * The following functions ready a cautious request to go up to the nexus
2665  * driver.  It is up to the nexus driver to decide how to process the request.
2666  * It may choose to call i_ddi_do_caut_get/put in this file, or do it
2667  * differently.
2668  */
2669 
2670 static void
2671 i_ddi_caut_getput_ctlops(ddi_acc_impl_t *hp, uint64_t host_addr,
2672     uint64_t dev_addr, size_t size, size_t repcount, uint_t flags,
2673     ddi_ctl_enum_t cmd)
2674 {
2675 	peekpoke_ctlops_t	cautacc_ctlops_arg;
2676 
2677 	cautacc_ctlops_arg.size = size;
2678 	cautacc_ctlops_arg.dev_addr = dev_addr;
2679 	cautacc_ctlops_arg.host_addr = host_addr;
2680 	cautacc_ctlops_arg.handle = (ddi_acc_handle_t)hp;
2681 	cautacc_ctlops_arg.repcount = repcount;
2682 	cautacc_ctlops_arg.flags = flags;
2683 
2684 	(void) ddi_ctlops(hp->ahi_common.ah_dip, hp->ahi_common.ah_dip, cmd,
2685 	    &cautacc_ctlops_arg, NULL);
2686 }
2687 
2688 uint8_t
2689 i_ddi_caut_get8(ddi_acc_impl_t *hp, uint8_t *addr)
2690 {
2691 	uint8_t value;
2692 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2693 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_PEEK);
2694 
2695 	return (value);
2696 }
2697 
2698 uint16_t
2699 i_ddi_caut_get16(ddi_acc_impl_t *hp, uint16_t *addr)
2700 {
2701 	uint16_t value;
2702 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2703 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_PEEK);
2704 
2705 	return (value);
2706 }
2707 
2708 uint32_t
2709 i_ddi_caut_get32(ddi_acc_impl_t *hp, uint32_t *addr)
2710 {
2711 	uint32_t value;
2712 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2713 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_PEEK);
2714 
2715 	return (value);
2716 }
2717 
2718 uint64_t
2719 i_ddi_caut_get64(ddi_acc_impl_t *hp, uint64_t *addr)
2720 {
2721 	uint64_t value;
2722 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2723 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_PEEK);
2724 
2725 	return (value);
2726 }
2727 
2728 void
2729 i_ddi_caut_put8(ddi_acc_impl_t *hp, uint8_t *addr, uint8_t value)
2730 {
2731 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2732 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_POKE);
2733 }
2734 
2735 void
2736 i_ddi_caut_put16(ddi_acc_impl_t *hp, uint16_t *addr, uint16_t value)
2737 {
2738 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2739 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_POKE);
2740 }
2741 
2742 void
2743 i_ddi_caut_put32(ddi_acc_impl_t *hp, uint32_t *addr, uint32_t value)
2744 {
2745 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2746 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_POKE);
2747 }
2748 
2749 void
2750 i_ddi_caut_put64(ddi_acc_impl_t *hp, uint64_t *addr, uint64_t value)
2751 {
2752 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2753 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_POKE);
2754 }
2755 
2756 void
2757 i_ddi_caut_rep_get8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
2758 	size_t repcount, uint_t flags)
2759 {
2760 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2761 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_PEEK);
2762 }
2763 
2764 void
2765 i_ddi_caut_rep_get16(ddi_acc_impl_t *hp, uint16_t *host_addr,
2766     uint16_t *dev_addr, size_t repcount, uint_t flags)
2767 {
2768 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2769 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_PEEK);
2770 }
2771 
2772 void
2773 i_ddi_caut_rep_get32(ddi_acc_impl_t *hp, uint32_t *host_addr,
2774     uint32_t *dev_addr, size_t repcount, uint_t flags)
2775 {
2776 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2777 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_PEEK);
2778 }
2779 
2780 void
2781 i_ddi_caut_rep_get64(ddi_acc_impl_t *hp, uint64_t *host_addr,
2782     uint64_t *dev_addr, size_t repcount, uint_t flags)
2783 {
2784 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2785 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_PEEK);
2786 }
2787 
2788 void
2789 i_ddi_caut_rep_put8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
2790 	size_t repcount, uint_t flags)
2791 {
2792 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2793 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_POKE);
2794 }
2795 
2796 void
2797 i_ddi_caut_rep_put16(ddi_acc_impl_t *hp, uint16_t *host_addr,
2798     uint16_t *dev_addr, size_t repcount, uint_t flags)
2799 {
2800 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2801 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_POKE);
2802 }
2803 
2804 void
2805 i_ddi_caut_rep_put32(ddi_acc_impl_t *hp, uint32_t *host_addr,
2806     uint32_t *dev_addr, size_t repcount, uint_t flags)
2807 {
2808 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2809 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_POKE);
2810 }
2811 
2812 void
2813 i_ddi_caut_rep_put64(ddi_acc_impl_t *hp, uint64_t *host_addr,
2814     uint64_t *dev_addr, size_t repcount, uint_t flags)
2815 {
2816 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2817 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_POKE);
2818 }
2819 
2820 boolean_t
2821 i_ddi_copybuf_required(ddi_dma_attr_t *attrp)
2822 {
2823 	uint64_t hi_pa;
2824 
2825 	hi_pa = ((uint64_t)physmax + 1ull) << PAGESHIFT;
2826 	if (attrp->dma_attr_addr_hi < hi_pa) {
2827 		return (B_TRUE);
2828 	}
2829 
2830 	return (B_FALSE);
2831 }
2832 
2833 size_t
2834 i_ddi_copybuf_size()
2835 {
2836 	return (dma_max_copybuf_size);
2837 }
2838 
2839 /*
2840  * i_ddi_dma_max()
2841  *    returns the maximum DMA size which can be performed in a single DMA
2842  *    window taking into account the devices DMA contraints (attrp), the
2843  *    maximum copy buffer size (if applicable), and the worse case buffer
2844  *    fragmentation.
2845  */
2846 /*ARGSUSED*/
2847 uint32_t
2848 i_ddi_dma_max(dev_info_t *dip, ddi_dma_attr_t *attrp)
2849 {
2850 	uint64_t maxxfer;
2851 
2852 
2853 	/*
2854 	 * take the min of maxxfer and the the worse case fragementation
2855 	 * (e.g. every cookie <= 1 page)
2856 	 */
2857 	maxxfer = MIN(attrp->dma_attr_maxxfer,
2858 	    ((uint64_t)(attrp->dma_attr_sgllen - 1) << PAGESHIFT));
2859 
2860 	/*
2861 	 * If the DMA engine can't reach all off memory, we also need to take
2862 	 * the max size of the copybuf into consideration.
2863 	 */
2864 	if (i_ddi_copybuf_required(attrp)) {
2865 		maxxfer = MIN(i_ddi_copybuf_size(), maxxfer);
2866 	}
2867 
2868 	/*
2869 	 * we only return a 32-bit value. Make sure it's not -1. Round to a
2870 	 * page so it won't be mistaken for an error value during debug.
2871 	 */
2872 	if (maxxfer >= 0xFFFFFFFF) {
2873 		maxxfer = 0xFFFFF000;
2874 	}
2875 
2876 	/*
2877 	 * make sure the value we return is a whole multiple of the
2878 	 * granlarity.
2879 	 */
2880 	if (attrp->dma_attr_granular > 1) {
2881 		maxxfer = maxxfer - (maxxfer % attrp->dma_attr_granular);
2882 	}
2883 
2884 	return ((uint32_t)maxxfer);
2885 }
2886