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