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