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