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