xref: /titanic_52/usr/src/uts/i86pc/os/ddi_impl.c (revision 6c9596d46e3a733328712fdad3ea5ee362795acc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * PC specific DDI implementation
31  */
32 #include <sys/types.h>
33 #include <sys/autoconf.h>
34 #include <sys/avintr.h>
35 #include <sys/bootconf.h>
36 #include <sys/conf.h>
37 #include <sys/cpuvar.h>
38 #include <sys/ddi_impldefs.h>
39 #include <sys/ddi_subrdefs.h>
40 #include <sys/ethernet.h>
41 #include <sys/fp.h>
42 #include <sys/instance.h>
43 #include <sys/kmem.h>
44 #include <sys/machsystm.h>
45 #include <sys/modctl.h>
46 #include <sys/promif.h>
47 #include <sys/prom_plat.h>
48 #include <sys/sunndi.h>
49 #include <sys/ndi_impldefs.h>
50 #include <sys/ddi_impldefs.h>
51 #include <sys/sysmacros.h>
52 #include <sys/systeminfo.h>
53 #include <sys/utsname.h>
54 #include <sys/atomic.h>
55 #include <sys/spl.h>
56 #include <sys/archsystm.h>
57 #include <vm/seg_kmem.h>
58 #include <sys/ontrap.h>
59 #include <sys/fm/protocol.h>
60 #include <sys/ramdisk.h>
61 #include <sys/sunndi.h>
62 #include <sys/vmem.h>
63 #include <sys/pci_impl.h>
64 #if defined(__xpv)
65 #include <sys/hypervisor.h>
66 #endif
67 #include <sys/mach_intr.h>
68 #include <vm/hat_i86.h>
69 #include <sys/x86_archext.h>
70 
71 /*
72  * DDI Boot Configuration
73  */
74 
75 /*
76  * No platform drivers on this platform
77  */
78 char *platform_module_list[] = {
79 	"ppm",
80 	(char *)0
81 };
82 
83 /* pci bus resource maps */
84 struct pci_bus_resource *pci_bus_res;
85 
86 extern int root_is_svm;
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 static 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(!P2CROSS((uintptr_t)raddr, (uintptr_t)raddr + rsize - 1,
1465 	    PAGESIZE) || 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 	major = ddi_name_to_major("vgatext");
1949 	if (major == (major_t)-1)
1950 		return;
1951 	devi = devnamesp[major].dn_head;
1952 	if (devi == NULL)
1953 		return;
1954 
1955 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
1956 
1957 	/*
1958 	 * Import "vga" properties from the boot.
1959 	 */
1960 	name = "display-edif-block";
1961 	length = BOP_GETPROPLEN(bootops, name);
1962 	if (length > 0 && length < MMU_PAGESIZE) {
1963 		BOP_GETPROP(bootops, name, bop_staging_area);
1964 		(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
1965 		    devi, name, bop_staging_area, length);
1966 	}
1967 
1968 	/*
1969 	 * kdmconfig is also looking for display-type and
1970 	 * video-adapter-type. We default to color and svga.
1971 	 *
1972 	 * Could it be "monochrome", "vga"?
1973 	 * Nah, you've got to come to the 21st century...
1974 	 * And you can set monitor type manually in kdmconfig
1975 	 * if you are really an old junky.
1976 	 */
1977 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1978 	    devi, "display-type", "color");
1979 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1980 	    devi, "video-adapter-type", "svga");
1981 
1982 	name = "display-edif-id";
1983 	length = BOP_GETPROPLEN(bootops, name);
1984 	if (length > 0 && length < MMU_PAGESIZE) {
1985 		BOP_GETPROP(bootops, name, bop_staging_area);
1986 		copy_boot_str(bop_staging_area, property_val, length);
1987 		(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1988 		    devi, name, property_val);
1989 	}
1990 
1991 	kmem_free(bop_staging_area, MMU_PAGESIZE);
1992 }
1993 
1994 
1995 /*
1996  * This is temporary, but absolutely necessary.  If we are being
1997  * booted with a device tree created by the DevConf project's bootconf
1998  * program, then we have device information nodes that reflect
1999  * reality.  At this point in time in the Solaris release schedule, the
2000  * kernel drivers aren't prepared for reality.  They still depend on their
2001  * own ad-hoc interpretations of the properties created when their .conf
2002  * files were interpreted. These drivers use an "ignore-hardware-nodes"
2003  * property to prevent them from using the nodes passed up from the bootconf
2004  * device tree.
2005  *
2006  * Trying to assemble root file system drivers as we are booting from
2007  * devconf will fail if the kernel driver is basing its name_addr's on the
2008  * psuedo-node device info while the bootpath passed up from bootconf is using
2009  * reality-based name_addrs.  We help the boot along in this case by
2010  * looking at the pre-bootconf bootpath and determining if we would have
2011  * successfully matched if that had been the bootpath we had chosen.
2012  *
2013  * Note that we only even perform this extra check if we've booted
2014  * using bootconf's 1275 compliant bootpath, this is the boot device, and
2015  * we're trying to match the name_addr specified in the 1275 bootpath.
2016  */
2017 
2018 #define	MAXCOMPONENTLEN	32
2019 
2020 int
2021 x86_old_bootpath_name_addr_match(dev_info_t *cdip, char *caddr, char *naddr)
2022 {
2023 	/*
2024 	 *  There are multiple criteria to be met before we can even
2025 	 *  consider allowing a name_addr match here.
2026 	 *
2027 	 *  1) We must have been booted such that the bootconf program
2028 	 *	created device tree nodes and properties.  This can be
2029 	 *	determined by examining the 'bootpath' property.  This
2030 	 *	property will be a non-null string iff bootconf was
2031 	 *	involved in the boot.
2032 	 *
2033 	 *  2) The module that we want to match must be the boot device.
2034 	 *
2035 	 *  3) The instance of the module we are thinking of letting be
2036 	 *	our match must be ignoring hardware nodes.
2037 	 *
2038 	 *  4) The name_addr we want to match must be the name_addr
2039 	 *	specified in the 1275 bootpath.
2040 	 */
2041 	static char bootdev_module[MAXCOMPONENTLEN];
2042 	static char bootdev_oldmod[MAXCOMPONENTLEN];
2043 	static char bootdev_newaddr[MAXCOMPONENTLEN];
2044 	static char bootdev_oldaddr[MAXCOMPONENTLEN];
2045 	static int  quickexit;
2046 
2047 	char *daddr;
2048 	int dlen;
2049 
2050 	char	*lkupname;
2051 	int	rv = DDI_FAILURE;
2052 
2053 	if ((ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
2054 	    "devconf-addr", (caddr_t)&daddr, &dlen) == DDI_PROP_SUCCESS) &&
2055 	    (ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
2056 	    "ignore-hardware-nodes", -1) != -1)) {
2057 		if (strcmp(daddr, caddr) == 0) {
2058 			return (DDI_SUCCESS);
2059 		}
2060 	}
2061 
2062 	if (quickexit)
2063 		return (rv);
2064 
2065 	if (bootdev_module[0] == '\0') {
2066 		char *addrp, *eoaddrp;
2067 		char *busp, *modp, *atp;
2068 		char *bp1275, *bp;
2069 		int  bp1275len, bplen;
2070 
2071 		bp1275 = bp = addrp = eoaddrp = busp = modp = atp = NULL;
2072 
2073 		if (ddi_getlongprop(DDI_DEV_T_ANY,
2074 		    ddi_root_node(), 0, "bootpath",
2075 		    (caddr_t)&bp1275, &bp1275len) != DDI_PROP_SUCCESS ||
2076 		    bp1275len <= 1) {
2077 			/*
2078 			 * We didn't boot from bootconf so we never need to
2079 			 * do any special matches.
2080 			 */
2081 			quickexit = 1;
2082 			if (bp1275)
2083 				kmem_free(bp1275, bp1275len);
2084 			return (rv);
2085 		}
2086 
2087 		if (ddi_getlongprop(DDI_DEV_T_ANY,
2088 		    ddi_root_node(), 0, "boot-path",
2089 		    (caddr_t)&bp, &bplen) != DDI_PROP_SUCCESS || bplen <= 1) {
2090 			/*
2091 			 * No fallback position for matching. This is
2092 			 * certainly unexpected, but we'll handle it
2093 			 * just in case.
2094 			 */
2095 			quickexit = 1;
2096 			kmem_free(bp1275, bp1275len);
2097 			if (bp)
2098 				kmem_free(bp, bplen);
2099 			return (rv);
2100 		}
2101 
2102 		/*
2103 		 *  Determine boot device module and 1275 name_addr
2104 		 *
2105 		 *  bootpath assumed to be of the form /bus/module@name_addr
2106 		 */
2107 		if (busp = strchr(bp1275, '/')) {
2108 			if (modp = strchr(busp + 1, '/')) {
2109 				if (atp = strchr(modp + 1, '@')) {
2110 					*atp = '\0';
2111 					addrp = atp + 1;
2112 					if (eoaddrp = strchr(addrp, '/'))
2113 						*eoaddrp = '\0';
2114 				}
2115 			}
2116 		}
2117 
2118 		if (modp && addrp) {
2119 			(void) strncpy(bootdev_module, modp + 1,
2120 			    MAXCOMPONENTLEN);
2121 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
2122 
2123 			(void) strncpy(bootdev_newaddr, addrp, MAXCOMPONENTLEN);
2124 			bootdev_newaddr[MAXCOMPONENTLEN - 1] = '\0';
2125 		} else {
2126 			quickexit = 1;
2127 			kmem_free(bp1275, bp1275len);
2128 			kmem_free(bp, bplen);
2129 			return (rv);
2130 		}
2131 
2132 		/*
2133 		 *  Determine fallback name_addr
2134 		 *
2135 		 *  10/3/96 - Also save fallback module name because it
2136 		 *  might actually be different than the current module
2137 		 *  name.  E.G., ISA pnp drivers have new names.
2138 		 *
2139 		 *  bootpath assumed to be of the form /bus/module@name_addr
2140 		 */
2141 		addrp = NULL;
2142 		if (busp = strchr(bp, '/')) {
2143 			if (modp = strchr(busp + 1, '/')) {
2144 				if (atp = strchr(modp + 1, '@')) {
2145 					*atp = '\0';
2146 					addrp = atp + 1;
2147 					if (eoaddrp = strchr(addrp, '/'))
2148 						*eoaddrp = '\0';
2149 				}
2150 			}
2151 		}
2152 
2153 		if (modp && addrp) {
2154 			(void) strncpy(bootdev_oldmod, modp + 1,
2155 			    MAXCOMPONENTLEN);
2156 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
2157 
2158 			(void) strncpy(bootdev_oldaddr, addrp, MAXCOMPONENTLEN);
2159 			bootdev_oldaddr[MAXCOMPONENTLEN - 1] = '\0';
2160 		}
2161 
2162 		/* Free up the bootpath storage now that we're done with it. */
2163 		kmem_free(bp1275, bp1275len);
2164 		kmem_free(bp, bplen);
2165 
2166 		if (bootdev_oldaddr[0] == '\0') {
2167 			quickexit = 1;
2168 			return (rv);
2169 		}
2170 	}
2171 
2172 	if (((lkupname = ddi_get_name(cdip)) != NULL) &&
2173 	    (strcmp(bootdev_module, lkupname) == 0 ||
2174 	    strcmp(bootdev_oldmod, lkupname) == 0) &&
2175 	    ((ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
2176 	    "ignore-hardware-nodes", -1) != -1) ||
2177 	    ignore_hardware_nodes) &&
2178 	    strcmp(bootdev_newaddr, caddr) == 0 &&
2179 	    strcmp(bootdev_oldaddr, naddr) == 0) {
2180 		rv = DDI_SUCCESS;
2181 	}
2182 
2183 	return (rv);
2184 }
2185 
2186 /*
2187  * Perform a copy from a memory mapped device (whose devinfo pointer is devi)
2188  * separately mapped at devaddr in the kernel to a kernel buffer at kaddr.
2189  */
2190 /*ARGSUSED*/
2191 int
2192 e_ddi_copyfromdev(dev_info_t *devi,
2193     off_t off, const void *devaddr, void *kaddr, size_t len)
2194 {
2195 	bcopy(devaddr, kaddr, len);
2196 	return (0);
2197 }
2198 
2199 /*
2200  * Perform a copy to a memory mapped device (whose devinfo pointer is devi)
2201  * separately mapped at devaddr in the kernel from a kernel buffer at kaddr.
2202  */
2203 /*ARGSUSED*/
2204 int
2205 e_ddi_copytodev(dev_info_t *devi,
2206     off_t off, const void *kaddr, void *devaddr, size_t len)
2207 {
2208 	bcopy(kaddr, devaddr, len);
2209 	return (0);
2210 }
2211 
2212 
2213 static int
2214 poke_mem(peekpoke_ctlops_t *in_args)
2215 {
2216 	int err = DDI_SUCCESS;
2217 	on_trap_data_t otd;
2218 
2219 	/* Set up protected environment. */
2220 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
2221 		switch (in_args->size) {
2222 		case sizeof (uint8_t):
2223 			*(uint8_t *)(in_args->dev_addr) =
2224 			    *(uint8_t *)in_args->host_addr;
2225 			break;
2226 
2227 		case sizeof (uint16_t):
2228 			*(uint16_t *)(in_args->dev_addr) =
2229 			    *(uint16_t *)in_args->host_addr;
2230 			break;
2231 
2232 		case sizeof (uint32_t):
2233 			*(uint32_t *)(in_args->dev_addr) =
2234 			    *(uint32_t *)in_args->host_addr;
2235 			break;
2236 
2237 		case sizeof (uint64_t):
2238 			*(uint64_t *)(in_args->dev_addr) =
2239 			    *(uint64_t *)in_args->host_addr;
2240 			break;
2241 
2242 		default:
2243 			err = DDI_FAILURE;
2244 			break;
2245 		}
2246 	} else
2247 		err = DDI_FAILURE;
2248 
2249 	/* Take down protected environment. */
2250 	no_trap();
2251 
2252 	return (err);
2253 }
2254 
2255 
2256 static int
2257 peek_mem(peekpoke_ctlops_t *in_args)
2258 {
2259 	int err = DDI_SUCCESS;
2260 	on_trap_data_t otd;
2261 
2262 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
2263 		switch (in_args->size) {
2264 		case sizeof (uint8_t):
2265 			*(uint8_t *)in_args->host_addr =
2266 			    *(uint8_t *)in_args->dev_addr;
2267 			break;
2268 
2269 		case sizeof (uint16_t):
2270 			*(uint16_t *)in_args->host_addr =
2271 			    *(uint16_t *)in_args->dev_addr;
2272 			break;
2273 
2274 		case sizeof (uint32_t):
2275 			*(uint32_t *)in_args->host_addr =
2276 			    *(uint32_t *)in_args->dev_addr;
2277 			break;
2278 
2279 		case sizeof (uint64_t):
2280 			*(uint64_t *)in_args->host_addr =
2281 			    *(uint64_t *)in_args->dev_addr;
2282 			break;
2283 
2284 		default:
2285 			err = DDI_FAILURE;
2286 			break;
2287 		}
2288 	} else
2289 		err = DDI_FAILURE;
2290 
2291 	no_trap();
2292 	return (err);
2293 }
2294 
2295 
2296 /*
2297  * This is called only to process peek/poke when the DIP is NULL.
2298  * Assume that this is for memory, as nexi take care of device safe accesses.
2299  */
2300 int
2301 peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args)
2302 {
2303 	return (cmd == DDI_CTLOPS_PEEK ? peek_mem(in_args) : poke_mem(in_args));
2304 }
2305 
2306 /*
2307  * we've just done a cautious put/get. Check if it was successful by
2308  * calling pci_ereport_post() on all puts and for any gets that return -1
2309  */
2310 static int
2311 pci_peekpoke_check_fma(dev_info_t *dip, void *arg, ddi_ctl_enum_t ctlop)
2312 {
2313 	int	rval = DDI_SUCCESS;
2314 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
2315 	ddi_fm_error_t de;
2316 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
2317 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
2318 	int check_err = 0;
2319 	int repcount = in_args->repcount;
2320 
2321 	if (ctlop == DDI_CTLOPS_POKE &&
2322 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC)
2323 		return (DDI_SUCCESS);
2324 
2325 	if (ctlop == DDI_CTLOPS_PEEK &&
2326 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC) {
2327 		for (; repcount; repcount--) {
2328 			switch (in_args->size) {
2329 			case sizeof (uint8_t):
2330 				if (*(uint8_t *)in_args->host_addr == 0xff)
2331 					check_err = 1;
2332 				break;
2333 			case sizeof (uint16_t):
2334 				if (*(uint16_t *)in_args->host_addr == 0xffff)
2335 					check_err = 1;
2336 				break;
2337 			case sizeof (uint32_t):
2338 				if (*(uint32_t *)in_args->host_addr ==
2339 				    0xffffffff)
2340 					check_err = 1;
2341 				break;
2342 			case sizeof (uint64_t):
2343 				if (*(uint64_t *)in_args->host_addr ==
2344 				    0xffffffffffffffff)
2345 					check_err = 1;
2346 				break;
2347 			}
2348 		}
2349 		if (check_err == 0)
2350 			return (DDI_SUCCESS);
2351 	}
2352 	/*
2353 	 * for a cautious put or get or a non-cautious get that returned -1 call
2354 	 * io framework to see if there really was an error
2355 	 */
2356 	bzero(&de, sizeof (ddi_fm_error_t));
2357 	de.fme_version = DDI_FME_VERSION;
2358 	de.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
2359 	if (hdlp->ah_acc.devacc_attr_access == DDI_CAUTIOUS_ACC) {
2360 		de.fme_flag = DDI_FM_ERR_EXPECTED;
2361 		de.fme_acc_handle = in_args->handle;
2362 	} else if (hdlp->ah_acc.devacc_attr_access == DDI_DEFAULT_ACC) {
2363 		/*
2364 		 * We only get here with DDI_DEFAULT_ACC for config space gets.
2365 		 * Non-hardened drivers may be probing the hardware and
2366 		 * expecting -1 returned. So need to treat errors on
2367 		 * DDI_DEFAULT_ACC as DDI_FM_ERR_EXPECTED.
2368 		 */
2369 		de.fme_flag = DDI_FM_ERR_EXPECTED;
2370 		de.fme_acc_handle = in_args->handle;
2371 	} else {
2372 		/*
2373 		 * Hardened driver doing protected accesses shouldn't
2374 		 * get errors unless there's a hardware problem. Treat
2375 		 * as nonfatal if there's an error, but set UNEXPECTED
2376 		 * so we raise ereports on any errors and potentially
2377 		 * fault the device
2378 		 */
2379 		de.fme_flag = DDI_FM_ERR_UNEXPECTED;
2380 	}
2381 	pci_ereport_post(dip, &de, NULL);
2382 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
2383 	    de.fme_status != DDI_FM_OK) {
2384 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
2385 		rval = DDI_FAILURE;
2386 		errp->err_ena = de.fme_ena;
2387 		errp->err_expected = de.fme_flag;
2388 		errp->err_status = DDI_FM_NONFATAL;
2389 	}
2390 	return (rval);
2391 }
2392 
2393 /*
2394  * pci_peekpoke_check_nofma() is for when an error occurs on a register access
2395  * during pci_ereport_post(). We can't call pci_ereport_post() again or we'd
2396  * recurse, so assume all puts are OK and gets have failed if they return -1
2397  */
2398 static int
2399 pci_peekpoke_check_nofma(void *arg, ddi_ctl_enum_t ctlop)
2400 {
2401 	int rval = DDI_SUCCESS;
2402 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
2403 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
2404 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
2405 	int repcount = in_args->repcount;
2406 
2407 	if (ctlop == DDI_CTLOPS_POKE)
2408 		return (rval);
2409 
2410 	for (; repcount; repcount--) {
2411 		switch (in_args->size) {
2412 		case sizeof (uint8_t):
2413 			if (*(uint8_t *)in_args->host_addr == 0xff)
2414 				rval = DDI_FAILURE;
2415 			break;
2416 		case sizeof (uint16_t):
2417 			if (*(uint16_t *)in_args->host_addr == 0xffff)
2418 				rval = DDI_FAILURE;
2419 			break;
2420 		case sizeof (uint32_t):
2421 			if (*(uint32_t *)in_args->host_addr == 0xffffffff)
2422 				rval = DDI_FAILURE;
2423 			break;
2424 		case sizeof (uint64_t):
2425 			if (*(uint64_t *)in_args->host_addr ==
2426 			    0xffffffffffffffff)
2427 				rval = DDI_FAILURE;
2428 			break;
2429 		}
2430 	}
2431 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
2432 	    rval == DDI_FAILURE) {
2433 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
2434 		errp->err_ena = fm_ena_generate(0, FM_ENA_FMT1);
2435 		errp->err_expected = DDI_FM_ERR_UNEXPECTED;
2436 		errp->err_status = DDI_FM_NONFATAL;
2437 	}
2438 	return (rval);
2439 }
2440 
2441 int
2442 pci_peekpoke_check(dev_info_t *dip, dev_info_t *rdip,
2443 	ddi_ctl_enum_t ctlop, void *arg, void *result,
2444 	int (*handler)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
2445 	void *), kmutex_t *err_mutexp, kmutex_t *peek_poke_mutexp)
2446 {
2447 	int rval;
2448 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
2449 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
2450 
2451 	/*
2452 	 * this function only supports cautious accesses, not peeks/pokes
2453 	 * which don't have a handle
2454 	 */
2455 	if (hp == NULL)
2456 		return (DDI_FAILURE);
2457 
2458 	if (hp->ahi_acc_attr & DDI_ACCATTR_CONFIG_SPACE) {
2459 		if (!mutex_tryenter(err_mutexp)) {
2460 			/*
2461 			 * As this may be a recursive call from within
2462 			 * pci_ereport_post() we can't wait for the mutexes.
2463 			 * Fortunately we know someone is already calling
2464 			 * pci_ereport_post() which will handle the error bits
2465 			 * for us, and as this is a config space access we can
2466 			 * just do the access and check return value for -1
2467 			 * using pci_peekpoke_check_nofma().
2468 			 */
2469 			rval = handler(dip, rdip, ctlop, arg, result);
2470 			if (rval == DDI_SUCCESS)
2471 				rval = pci_peekpoke_check_nofma(arg, ctlop);
2472 			return (rval);
2473 		}
2474 		/*
2475 		 * This can't be a recursive call. Drop the err_mutex and get
2476 		 * both mutexes in the right order. If an error hasn't already
2477 		 * been detected by the ontrap code, use pci_peekpoke_check_fma
2478 		 * which will call pci_ereport_post() to check error status.
2479 		 */
2480 		mutex_exit(err_mutexp);
2481 	}
2482 	mutex_enter(peek_poke_mutexp);
2483 	rval = handler(dip, rdip, ctlop, arg, result);
2484 	if (rval == DDI_SUCCESS) {
2485 		mutex_enter(err_mutexp);
2486 		rval = pci_peekpoke_check_fma(dip, arg, ctlop);
2487 		mutex_exit(err_mutexp);
2488 	}
2489 	mutex_exit(peek_poke_mutexp);
2490 	return (rval);
2491 }
2492 
2493 void
2494 impl_setup_ddi(void)
2495 {
2496 	dev_info_t *xdip, *isa_dip;
2497 	rd_existing_t rd_mem_prop;
2498 	int err;
2499 
2500 	ndi_devi_alloc_sleep(ddi_root_node(), "ramdisk",
2501 	    (pnode_t)DEVI_SID_NODEID, &xdip);
2502 
2503 	(void) BOP_GETPROP(bootops,
2504 	    "ramdisk_start", (void *)&ramdisk_start);
2505 	(void) BOP_GETPROP(bootops,
2506 	    "ramdisk_end", (void *)&ramdisk_end);
2507 
2508 #ifdef __xpv
2509 	ramdisk_start -= ONE_GIG;
2510 	ramdisk_end -= ONE_GIG;
2511 #endif
2512 	rd_mem_prop.phys = ramdisk_start;
2513 	rd_mem_prop.size = ramdisk_end - ramdisk_start + 1;
2514 
2515 	(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, xdip,
2516 	    RD_EXISTING_PROP_NAME, (uchar_t *)&rd_mem_prop,
2517 	    sizeof (rd_mem_prop));
2518 	err = ndi_devi_bind_driver(xdip, 0);
2519 	ASSERT(err == 0);
2520 
2521 	/* isa node */
2522 	ndi_devi_alloc_sleep(ddi_root_node(), "isa",
2523 	    (pnode_t)DEVI_SID_NODEID, &isa_dip);
2524 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
2525 	    "device_type", "isa");
2526 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
2527 	    "bus-type", "isa");
2528 	(void) ndi_devi_bind_driver(isa_dip, 0);
2529 
2530 	/*
2531 	 * Read in the properties from the boot.
2532 	 */
2533 	get_boot_properties();
2534 
2535 	/* do bus dependent probes. */
2536 	impl_bus_initialprobe();
2537 
2538 	/* not framebuffer should be enumerated, if present */
2539 	get_vga_properties();
2540 }
2541 
2542 dev_t
2543 getrootdev(void)
2544 {
2545 	/*
2546 	 * Precedence given to rootdev if set in /etc/system
2547 	 */
2548 	if (root_is_svm) {
2549 		return (ddi_pathname_to_dev_t(svm_bootpath));
2550 	}
2551 
2552 	/*
2553 	 * Usually rootfs.bo_name is initialized by the
2554 	 * the bootpath property from bootenv.rc, but
2555 	 * defaults to "/ramdisk:a" otherwise.
2556 	 */
2557 	return (ddi_pathname_to_dev_t(rootfs.bo_name));
2558 }
2559 
2560 static struct bus_probe {
2561 	struct bus_probe *next;
2562 	void (*probe)(int);
2563 } *bus_probes;
2564 
2565 void
2566 impl_bus_add_probe(void (*func)(int))
2567 {
2568 	struct bus_probe *probe;
2569 
2570 	probe = kmem_alloc(sizeof (*probe), KM_SLEEP);
2571 	probe->next = bus_probes;
2572 	probe->probe = func;
2573 	bus_probes = probe;
2574 }
2575 
2576 /*ARGSUSED*/
2577 void
2578 impl_bus_delete_probe(void (*func)(int))
2579 {
2580 	struct bus_probe *prev = NULL;
2581 	struct bus_probe *probe = bus_probes;
2582 
2583 	while (probe) {
2584 		if (probe->probe == func)
2585 			break;
2586 		prev = probe;
2587 		probe = probe->next;
2588 	}
2589 
2590 	if (probe == NULL)
2591 		return;
2592 
2593 	if (prev)
2594 		prev->next = probe->next;
2595 	else
2596 		bus_probes = probe->next;
2597 
2598 	kmem_free(probe, sizeof (struct bus_probe));
2599 }
2600 
2601 /*
2602  * impl_bus_initialprobe
2603  *	Modload the prom simulator, then let it probe to verify existence
2604  *	and type of PCI support.
2605  */
2606 static void
2607 impl_bus_initialprobe(void)
2608 {
2609 	struct bus_probe *probe;
2610 
2611 	/* load modules to install bus probes */
2612 #if defined(__xpv)
2613 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
2614 		if (modload("misc", "pci_autoconfig") < 0) {
2615 			panic("failed to load misc/pci_autoconfig");
2616 		}
2617 	}
2618 	(void) modload("misc", "xpv_autoconfig");
2619 #else
2620 	if (modload("misc", "pci_autoconfig") < 0) {
2621 		panic("failed to load misc/pci_autoconfig");
2622 	}
2623 #endif
2624 
2625 	probe = bus_probes;
2626 	while (probe) {
2627 		/* run the probe functions */
2628 		(*probe->probe)(0);
2629 		probe = probe->next;
2630 	}
2631 }
2632 
2633 /*
2634  * impl_bus_reprobe
2635  *	Reprogram devices not set up by firmware.
2636  */
2637 static void
2638 impl_bus_reprobe(void)
2639 {
2640 	struct bus_probe *probe;
2641 
2642 	probe = bus_probes;
2643 	while (probe) {
2644 		/* run the probe function */
2645 		(*probe->probe)(1);
2646 		probe = probe->next;
2647 	}
2648 }
2649 
2650 
2651 /*
2652  * The following functions ready a cautious request to go up to the nexus
2653  * driver.  It is up to the nexus driver to decide how to process the request.
2654  * It may choose to call i_ddi_do_caut_get/put in this file, or do it
2655  * differently.
2656  */
2657 
2658 static void
2659 i_ddi_caut_getput_ctlops(ddi_acc_impl_t *hp, uint64_t host_addr,
2660     uint64_t dev_addr, size_t size, size_t repcount, uint_t flags,
2661     ddi_ctl_enum_t cmd)
2662 {
2663 	peekpoke_ctlops_t	cautacc_ctlops_arg;
2664 
2665 	cautacc_ctlops_arg.size = size;
2666 	cautacc_ctlops_arg.dev_addr = dev_addr;
2667 	cautacc_ctlops_arg.host_addr = host_addr;
2668 	cautacc_ctlops_arg.handle = (ddi_acc_handle_t)hp;
2669 	cautacc_ctlops_arg.repcount = repcount;
2670 	cautacc_ctlops_arg.flags = flags;
2671 
2672 	(void) ddi_ctlops(hp->ahi_common.ah_dip, hp->ahi_common.ah_dip, cmd,
2673 	    &cautacc_ctlops_arg, NULL);
2674 }
2675 
2676 uint8_t
2677 i_ddi_caut_get8(ddi_acc_impl_t *hp, uint8_t *addr)
2678 {
2679 	uint8_t value;
2680 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2681 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_PEEK);
2682 
2683 	return (value);
2684 }
2685 
2686 uint16_t
2687 i_ddi_caut_get16(ddi_acc_impl_t *hp, uint16_t *addr)
2688 {
2689 	uint16_t value;
2690 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2691 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_PEEK);
2692 
2693 	return (value);
2694 }
2695 
2696 uint32_t
2697 i_ddi_caut_get32(ddi_acc_impl_t *hp, uint32_t *addr)
2698 {
2699 	uint32_t value;
2700 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2701 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_PEEK);
2702 
2703 	return (value);
2704 }
2705 
2706 uint64_t
2707 i_ddi_caut_get64(ddi_acc_impl_t *hp, uint64_t *addr)
2708 {
2709 	uint64_t value;
2710 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2711 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_PEEK);
2712 
2713 	return (value);
2714 }
2715 
2716 void
2717 i_ddi_caut_put8(ddi_acc_impl_t *hp, uint8_t *addr, uint8_t value)
2718 {
2719 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2720 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_POKE);
2721 }
2722 
2723 void
2724 i_ddi_caut_put16(ddi_acc_impl_t *hp, uint16_t *addr, uint16_t value)
2725 {
2726 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2727 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_POKE);
2728 }
2729 
2730 void
2731 i_ddi_caut_put32(ddi_acc_impl_t *hp, uint32_t *addr, uint32_t value)
2732 {
2733 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2734 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_POKE);
2735 }
2736 
2737 void
2738 i_ddi_caut_put64(ddi_acc_impl_t *hp, uint64_t *addr, uint64_t value)
2739 {
2740 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
2741 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_POKE);
2742 }
2743 
2744 void
2745 i_ddi_caut_rep_get8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
2746 	size_t repcount, uint_t flags)
2747 {
2748 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2749 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_PEEK);
2750 }
2751 
2752 void
2753 i_ddi_caut_rep_get16(ddi_acc_impl_t *hp, uint16_t *host_addr,
2754     uint16_t *dev_addr, size_t repcount, uint_t flags)
2755 {
2756 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2757 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_PEEK);
2758 }
2759 
2760 void
2761 i_ddi_caut_rep_get32(ddi_acc_impl_t *hp, uint32_t *host_addr,
2762     uint32_t *dev_addr, size_t repcount, uint_t flags)
2763 {
2764 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2765 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_PEEK);
2766 }
2767 
2768 void
2769 i_ddi_caut_rep_get64(ddi_acc_impl_t *hp, uint64_t *host_addr,
2770     uint64_t *dev_addr, size_t repcount, uint_t flags)
2771 {
2772 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2773 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_PEEK);
2774 }
2775 
2776 void
2777 i_ddi_caut_rep_put8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
2778 	size_t repcount, uint_t flags)
2779 {
2780 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2781 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_POKE);
2782 }
2783 
2784 void
2785 i_ddi_caut_rep_put16(ddi_acc_impl_t *hp, uint16_t *host_addr,
2786     uint16_t *dev_addr, size_t repcount, uint_t flags)
2787 {
2788 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2789 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_POKE);
2790 }
2791 
2792 void
2793 i_ddi_caut_rep_put32(ddi_acc_impl_t *hp, uint32_t *host_addr,
2794     uint32_t *dev_addr, size_t repcount, uint_t flags)
2795 {
2796 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2797 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_POKE);
2798 }
2799 
2800 void
2801 i_ddi_caut_rep_put64(ddi_acc_impl_t *hp, uint64_t *host_addr,
2802     uint64_t *dev_addr, size_t repcount, uint_t flags)
2803 {
2804 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
2805 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_POKE);
2806 }
2807