xref: /titanic_41/usr/src/uts/i86pc/os/ddi_impl.c (revision 260921a4509dd7666c6613f413be409b66181642)
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 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/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 	if (attr->dma_attr_addr_lo > mmu_ptob((uint64_t)ddiphysmin))
1317 		return (NULL);
1318 
1319 	align = MAX(align, hdrsize);
1320 	ASSERT((align & (align - 1)) == 0);
1321 
1322 	/*
1323 	 * All of our allocators guarantee 16-byte alignment, so we don't
1324 	 * need to reserve additional space for the header.
1325 	 * To simplify picking the correct kmem_io_cache, we round up to
1326 	 * a multiple of KA_ALIGN.
1327 	 */
1328 	rsize = P2ROUNDUP_TYPED(size + align, KA_ALIGN, size_t);
1329 
1330 	if (physcontig && rsize > PAGESIZE) {
1331 		if (addr = contig_alloc(size, attr, align, cansleep)) {
1332 			if (!putctgas(addr, size))
1333 				contig_free(addr, size);
1334 			else
1335 				return (addr);
1336 		}
1337 		return (NULL);
1338 	}
1339 
1340 	a = kmem_io_index(attr->dma_attr_addr_hi);
1341 
1342 	if (rsize > PAGESIZE) {
1343 		vmp = kmem_io[a].kmem_io_arena;
1344 		raddr = vmem_alloc(vmp, rsize,
1345 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
1346 	} else {
1347 		c = highbit((rsize >> KA_ALIGN_SHIFT) - 1);
1348 		cp = kmem_io[a].kmem_io_cache[c];
1349 		raddr = kmem_cache_alloc(cp, (cansleep) ? KM_SLEEP :
1350 		    KM_NOSLEEP);
1351 	}
1352 
1353 	if (raddr == NULL) {
1354 		int	na;
1355 
1356 		ASSERT(cansleep == 0);
1357 		if (rsize > PAGESIZE)
1358 			return (NULL);
1359 		/*
1360 		 * System does not have memory in the requested range.
1361 		 * Try smaller kmem io ranges and larger cache sizes
1362 		 * to see if there might be memory available in
1363 		 * these other caches.
1364 		 */
1365 
1366 		for (na = kmem_io_index_next(a); na >= 0;
1367 		    na = kmem_io_index_next(na)) {
1368 			ASSERT(kmem_io[na].kmem_io_arena);
1369 			cp = kmem_io[na].kmem_io_cache[c];
1370 			raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
1371 			if (raddr)
1372 				goto kallocdone;
1373 		}
1374 		/* now try the larger kmem io cache sizes */
1375 		for (na = a; na >= 0; na = kmem_io_index_next(na)) {
1376 			for (i = c + 1; i < KA_NCACHE; i++) {
1377 				cp = kmem_io[na].kmem_io_cache[i];
1378 				raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
1379 				if (raddr)
1380 					goto kallocdone;
1381 			}
1382 		}
1383 		return (NULL);
1384 	}
1385 
1386 kallocdone:
1387 	ASSERT(!P2CROSS((uintptr_t)raddr, (uintptr_t)raddr + rsize - 1,
1388 	    PAGESIZE) || rsize > PAGESIZE);
1389 
1390 	addr = (size_t *)P2ROUNDUP((uintptr_t)raddr + hdrsize, align);
1391 	ASSERT((uintptr_t)addr + size - (uintptr_t)raddr <= rsize);
1392 
1393 	addr[-4] = (size_t)cp;
1394 	addr[-3] = (size_t)vmp;
1395 	addr[-2] = (size_t)raddr;
1396 	addr[-1] = rsize;
1397 
1398 	return (addr);
1399 }
1400 
1401 static void
1402 kfreea(void *addr)
1403 {
1404 	size_t		size;
1405 
1406 	if (!((uintptr_t)addr & PAGEOFFSET) && (size = getctgsz(addr))) {
1407 		contig_free(addr, size);
1408 	} else {
1409 		size_t	*saddr = addr;
1410 		if (saddr[-4] == 0)
1411 			vmem_free((vmem_t *)saddr[-3], (void *)saddr[-2],
1412 				saddr[-1]);
1413 		else
1414 			kmem_cache_free((kmem_cache_t *)saddr[-4],
1415 				(void *)saddr[-2]);
1416 	}
1417 }
1418 
1419 /*
1420  * This should actually be called i_ddi_dma_mem_alloc. There should
1421  * also be an i_ddi_pio_mem_alloc. i_ddi_dma_mem_alloc should call
1422  * through the device tree with the DDI_CTLOPS_DMA_ALIGN ctl ops to
1423  * get alignment requirements for DMA memory. i_ddi_pio_mem_alloc
1424  * should use DDI_CTLOPS_PIO_ALIGN. Since we only have i_ddi_mem_alloc
1425  * so far which is used for both, DMA and PIO, we have to use the DMA
1426  * ctl ops to make everybody happy.
1427  */
1428 /*ARGSUSED*/
1429 int
1430 i_ddi_mem_alloc(dev_info_t *dip, ddi_dma_attr_t *attr,
1431 	size_t length, int cansleep, int streaming,
1432 	ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
1433 	size_t *real_length, ddi_acc_hdl_t *ap)
1434 {
1435 	caddr_t a;
1436 	int iomin;
1437 	ddi_acc_impl_t *iap;
1438 	int physcontig = 0;
1439 	pgcnt_t npages;
1440 	pgcnt_t minctg;
1441 
1442 	/*
1443 	 * Check legality of arguments
1444 	 */
1445 	if (length == 0 || kaddrp == NULL || attr == NULL) {
1446 		return (DDI_FAILURE);
1447 	}
1448 	if (attr->dma_attr_minxfer == 0 || attr->dma_attr_align == 0 ||
1449 		(attr->dma_attr_align & (attr->dma_attr_align - 1)) ||
1450 		(attr->dma_attr_minxfer & (attr->dma_attr_minxfer - 1))) {
1451 			return (DDI_FAILURE);
1452 	}
1453 
1454 	/*
1455 	 * figure out most restrictive alignment requirement
1456 	 */
1457 	iomin = attr->dma_attr_minxfer;
1458 	iomin = maxbit(iomin, attr->dma_attr_align);
1459 	if (iomin == 0)
1460 		return (DDI_FAILURE);
1461 
1462 	ASSERT((iomin & (iomin - 1)) == 0);
1463 
1464 
1465 	/*
1466 	 * Determine if we need to satisfy the request for physically
1467 	 * contiguous memory or alignments larger than pagesize.
1468 	 */
1469 	npages = btopr(length + attr->dma_attr_align);
1470 	minctg = howmany(npages, attr->dma_attr_sgllen);
1471 
1472 	if (minctg > 1) {
1473 		uint64_t pfnseg = attr->dma_attr_seg >> PAGESHIFT;
1474 		/*
1475 		 * verify that the minimum contig requirement for the
1476 		 * actual length does not cross segment boundary.
1477 		 */
1478 		length = P2ROUNDUP_TYPED(length, attr->dma_attr_minxfer,
1479 		    size_t);
1480 		npages = btopr(length);
1481 		minctg = howmany(npages, attr->dma_attr_sgllen);
1482 		if (minctg > pfnseg + 1)
1483 			return (DDI_FAILURE);
1484 		physcontig = 1;
1485 	} else {
1486 		length = P2ROUNDUP_TYPED(length, iomin, size_t);
1487 	}
1488 
1489 	/*
1490 	 * Allocate the requested amount from the system.
1491 	 */
1492 	a = kalloca(length, iomin, cansleep, physcontig, attr);
1493 
1494 	if ((*kaddrp = a) == NULL)
1495 		return (DDI_FAILURE);
1496 
1497 	if (real_length) {
1498 		*real_length = length;
1499 	}
1500 	if (ap) {
1501 		/*
1502 		 * initialize access handle
1503 		 */
1504 		iap = (ddi_acc_impl_t *)ap->ah_platform_private;
1505 		iap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR;
1506 		impl_acc_hdl_init(ap);
1507 	}
1508 	return (DDI_SUCCESS);
1509 }
1510 
1511 /*
1512  * covert old DMA limits structure to DMA attribute structure
1513  * and continue
1514  */
1515 int
1516 i_ddi_mem_alloc_lim(dev_info_t *dip, ddi_dma_lim_t *limits,
1517 	size_t length, int cansleep, int streaming,
1518 	ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
1519 	uint_t *real_length, ddi_acc_hdl_t *ap)
1520 {
1521 	ddi_dma_attr_t dma_attr, *attrp;
1522 	size_t rlen;
1523 	int ret;
1524 
1525 	if (limits == NULL) {
1526 		return (DDI_FAILURE);
1527 	}
1528 
1529 	/*
1530 	 * set up DMA attribute structure to pass to i_ddi_mem_alloc()
1531 	 */
1532 	attrp = &dma_attr;
1533 	attrp->dma_attr_version = DMA_ATTR_V0;
1534 	attrp->dma_attr_addr_lo = (uint64_t)limits->dlim_addr_lo;
1535 	attrp->dma_attr_addr_hi = (uint64_t)limits->dlim_addr_hi;
1536 	attrp->dma_attr_count_max = (uint64_t)limits->dlim_ctreg_max;
1537 	attrp->dma_attr_align = 1;
1538 	attrp->dma_attr_burstsizes = (uint_t)limits->dlim_burstsizes;
1539 	attrp->dma_attr_minxfer = (uint32_t)limits->dlim_minxfer;
1540 	attrp->dma_attr_maxxfer = (uint64_t)limits->dlim_reqsize;
1541 	attrp->dma_attr_seg = (uint64_t)limits->dlim_adreg_max;
1542 	attrp->dma_attr_sgllen = limits->dlim_sgllen;
1543 	attrp->dma_attr_granular = (uint32_t)limits->dlim_granular;
1544 	attrp->dma_attr_flags = 0;
1545 
1546 	ret = i_ddi_mem_alloc(dip, attrp, length, cansleep, streaming,
1547 			accattrp, kaddrp, &rlen, ap);
1548 	if (ret == DDI_SUCCESS) {
1549 		if (real_length)
1550 			*real_length = (uint_t)rlen;
1551 	}
1552 	return (ret);
1553 }
1554 
1555 /* ARGSUSED */
1556 void
1557 i_ddi_mem_free(caddr_t kaddr, int stream)
1558 {
1559 	kfreea(kaddr);
1560 }
1561 
1562 /*
1563  * Access Barriers
1564  *
1565  */
1566 /*ARGSUSED*/
1567 int
1568 i_ddi_ontrap(ddi_acc_handle_t hp)
1569 {
1570 	return (DDI_FAILURE);
1571 }
1572 
1573 /*ARGSUSED*/
1574 void
1575 i_ddi_notrap(ddi_acc_handle_t hp)
1576 {
1577 }
1578 
1579 
1580 /*
1581  * Misc Functions
1582  */
1583 
1584 /*
1585  * Implementation instance override functions
1586  *
1587  * No override on i86pc
1588  */
1589 /*ARGSUSED*/
1590 uint_t
1591 impl_assign_instance(dev_info_t *dip)
1592 {
1593 	return ((uint_t)-1);
1594 }
1595 
1596 /*ARGSUSED*/
1597 int
1598 impl_keep_instance(dev_info_t *dip)
1599 {
1600 	return (DDI_FAILURE);
1601 }
1602 
1603 /*ARGSUSED*/
1604 int
1605 impl_free_instance(dev_info_t *dip)
1606 {
1607 	return (DDI_FAILURE);
1608 }
1609 
1610 /*ARGSUSED*/
1611 int
1612 impl_check_cpu(dev_info_t *devi)
1613 {
1614 	return (DDI_SUCCESS);
1615 }
1616 
1617 /*
1618  * Referenced in common/cpr_driver.c: Power off machine.
1619  * Don't know how to power off i86pc.
1620  */
1621 void
1622 arch_power_down()
1623 {}
1624 
1625 /*
1626  * Copy name to property_name, since name
1627  * is in the low address range below kernelbase.
1628  */
1629 static void
1630 copy_boot_str(const char *boot_str, char *kern_str, int len)
1631 {
1632 	int i = 0;
1633 
1634 	while (i < len - 1 && boot_str[i] != '\0') {
1635 		kern_str[i] = boot_str[i];
1636 		i++;
1637 	}
1638 
1639 	kern_str[i] = 0;	/* null terminate */
1640 	if (boot_str[i] != '\0')
1641 		cmn_err(CE_WARN,
1642 		    "boot property string is truncated to %s", kern_str);
1643 }
1644 
1645 static void
1646 get_boot_properties(void)
1647 {
1648 	extern char hw_provider[];
1649 	dev_info_t *devi;
1650 	char *name;
1651 	int length;
1652 	char property_name[50], property_val[50];
1653 	void *bop_staging_area;
1654 
1655 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP);
1656 
1657 	/*
1658 	 * Import "root" properties from the boot.
1659 	 *
1660 	 * We do this by invoking BOP_NEXTPROP until the list
1661 	 * is completely copied in.
1662 	 */
1663 
1664 	devi = ddi_root_node();
1665 	for (name = BOP_NEXTPROP(bootops, "");		/* get first */
1666 	    name;					/* NULL => DONE */
1667 	    name = BOP_NEXTPROP(bootops, name)) {	/* get next */
1668 
1669 		/* copy string to memory above kernelbase */
1670 		copy_boot_str(name, property_name, 50);
1671 
1672 		/*
1673 		 * Skip vga properties. They will be picked up later
1674 		 * by get_vga_properties.
1675 		 */
1676 		if (strcmp(property_name, "display-edif-block") == 0 ||
1677 		    strcmp(property_name, "display-edif-id") == 0) {
1678 			continue;
1679 		}
1680 
1681 		length = BOP_GETPROPLEN(bootops, property_name);
1682 		if (length == 0)
1683 			continue;
1684 		if (length > MMU_PAGESIZE) {
1685 			cmn_err(CE_NOTE,
1686 			    "boot property %s longer than 0x%x, ignored\n",
1687 			    property_name, MMU_PAGESIZE);
1688 			continue;
1689 		}
1690 		BOP_GETPROP(bootops, property_name, bop_staging_area);
1691 
1692 		/*
1693 		 * special properties:
1694 		 * si-machine, si-hw-provider
1695 		 *	goes to kernel data structures.
1696 		 * bios-boot-device and stdout
1697 		 *	goes to hardware property list so it may show up
1698 		 *	in the prtconf -vp output. This is needed by
1699 		 *	Install/Upgrade. Once we fix install upgrade,
1700 		 *	this can be taken out.
1701 		 */
1702 		if (strcmp(name, "si-machine") == 0) {
1703 			(void) strncpy(utsname.machine, bop_staging_area,
1704 			    SYS_NMLN);
1705 			utsname.machine[SYS_NMLN - 1] = (char)NULL;
1706 		} else if (strcmp(name, "si-hw-provider") == 0) {
1707 			(void) strncpy(hw_provider, bop_staging_area, SYS_NMLN);
1708 			hw_provider[SYS_NMLN - 1] = (char)NULL;
1709 		} else if (strcmp(name, "bios-boot-device") == 0) {
1710 			copy_boot_str(bop_staging_area, property_val, 50);
1711 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, devi,
1712 			    property_name, property_val);
1713 		} else if (strcmp(name, "stdout") == 0) {
1714 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi,
1715 			    property_name, *((int *)bop_staging_area));
1716 		} else {
1717 			/* Property type unknown, use old prop interface */
1718 			(void) e_ddi_prop_create(DDI_DEV_T_NONE, devi,
1719 			    DDI_PROP_CANSLEEP, property_name, bop_staging_area,
1720 			    length);
1721 		}
1722 	}
1723 
1724 	kmem_free(bop_staging_area, MMU_PAGESIZE);
1725 }
1726 
1727 static void
1728 get_vga_properties(void)
1729 {
1730 	dev_info_t *devi;
1731 	major_t major;
1732 	char *name;
1733 	int length;
1734 	char property_val[50];
1735 	void *bop_staging_area;
1736 
1737 	major = ddi_name_to_major("vgatext");
1738 	if (major == (major_t)-1)
1739 		return;
1740 	devi = devnamesp[major].dn_head;
1741 	if (devi == NULL)
1742 		return;
1743 
1744 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
1745 
1746 	/*
1747 	 * Import "vga" properties from the boot.
1748 	 */
1749 	name = "display-edif-block";
1750 	length = BOP_GETPROPLEN(bootops, name);
1751 	if (length > 0 && length < MMU_PAGESIZE) {
1752 		BOP_GETPROP(bootops, name, bop_staging_area);
1753 		(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
1754 		    devi, name, bop_staging_area, length);
1755 	}
1756 
1757 	/*
1758 	 * kdmconfig is also looking for display-type and
1759 	 * video-adapter-type. We default to color and svga.
1760 	 *
1761 	 * Could it be "monochrome", "vga"?
1762 	 * Nah, you've got to come to the 21st century...
1763 	 * And you can set monitor type manually in kdmconfig
1764 	 * if you are really an old junky.
1765 	 */
1766 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1767 	    devi, "display-type", "color");
1768 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1769 	    devi, "video-adapter-type", "svga");
1770 
1771 	name = "display-edif-id";
1772 	length = BOP_GETPROPLEN(bootops, name);
1773 	if (length > 0 && length < MMU_PAGESIZE) {
1774 		BOP_GETPROP(bootops, name, bop_staging_area);
1775 		copy_boot_str(bop_staging_area, property_val, length);
1776 		(void) ndi_prop_update_string(DDI_DEV_T_NONE,
1777 		    devi, name, property_val);
1778 	}
1779 
1780 	kmem_free(bop_staging_area, MMU_PAGESIZE);
1781 }
1782 
1783 
1784 /*
1785  * This is temporary, but absolutely necessary.  If we are being
1786  * booted with a device tree created by the DevConf project's bootconf
1787  * program, then we have device information nodes that reflect
1788  * reality.  At this point in time in the Solaris release schedule, the
1789  * kernel drivers aren't prepared for reality.  They still depend on their
1790  * own ad-hoc interpretations of the properties created when their .conf
1791  * files were interpreted. These drivers use an "ignore-hardware-nodes"
1792  * property to prevent them from using the nodes passed up from the bootconf
1793  * device tree.
1794  *
1795  * Trying to assemble root file system drivers as we are booting from
1796  * devconf will fail if the kernel driver is basing its name_addr's on the
1797  * psuedo-node device info while the bootpath passed up from bootconf is using
1798  * reality-based name_addrs.  We help the boot along in this case by
1799  * looking at the pre-bootconf bootpath and determining if we would have
1800  * successfully matched if that had been the bootpath we had chosen.
1801  *
1802  * Note that we only even perform this extra check if we've booted
1803  * using bootconf's 1275 compliant bootpath, this is the boot device, and
1804  * we're trying to match the name_addr specified in the 1275 bootpath.
1805  */
1806 
1807 #define	MAXCOMPONENTLEN	32
1808 
1809 int
1810 x86_old_bootpath_name_addr_match(dev_info_t *cdip, char *caddr, char *naddr)
1811 {
1812 	/*
1813 	 *  There are multiple criteria to be met before we can even
1814 	 *  consider allowing a name_addr match here.
1815 	 *
1816 	 *  1) We must have been booted such that the bootconf program
1817 	 *	created device tree nodes and properties.  This can be
1818 	 *	determined by examining the 'bootpath' property.  This
1819 	 *	property will be a non-null string iff bootconf was
1820 	 *	involved in the boot.
1821 	 *
1822 	 *  2) The module that we want to match must be the boot device.
1823 	 *
1824 	 *  3) The instance of the module we are thinking of letting be
1825 	 *	our match must be ignoring hardware nodes.
1826 	 *
1827 	 *  4) The name_addr we want to match must be the name_addr
1828 	 *	specified in the 1275 bootpath.
1829 	 */
1830 	static char bootdev_module[MAXCOMPONENTLEN];
1831 	static char bootdev_oldmod[MAXCOMPONENTLEN];
1832 	static char bootdev_newaddr[MAXCOMPONENTLEN];
1833 	static char bootdev_oldaddr[MAXCOMPONENTLEN];
1834 	static int  quickexit;
1835 
1836 	char *daddr;
1837 	int dlen;
1838 
1839 	char	*lkupname;
1840 	int	rv = DDI_FAILURE;
1841 
1842 	if ((ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
1843 		"devconf-addr", (caddr_t)&daddr, &dlen) == DDI_PROP_SUCCESS) &&
1844 	    (ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
1845 		"ignore-hardware-nodes", -1) != -1)) {
1846 		if (strcmp(daddr, caddr) == 0) {
1847 			return (DDI_SUCCESS);
1848 		}
1849 	}
1850 
1851 	if (quickexit)
1852 		return (rv);
1853 
1854 	if (bootdev_module[0] == '\0') {
1855 		char *addrp, *eoaddrp;
1856 		char *busp, *modp, *atp;
1857 		char *bp1275, *bp;
1858 		int  bp1275len, bplen;
1859 
1860 		bp1275 = bp = addrp = eoaddrp = busp = modp = atp = NULL;
1861 
1862 		if (ddi_getlongprop(DDI_DEV_T_ANY,
1863 		    ddi_root_node(), 0, "bootpath",
1864 		    (caddr_t)&bp1275, &bp1275len) != DDI_PROP_SUCCESS ||
1865 		    bp1275len <= 1) {
1866 			/*
1867 			 * We didn't boot from bootconf so we never need to
1868 			 * do any special matches.
1869 			 */
1870 			quickexit = 1;
1871 			if (bp1275)
1872 				kmem_free(bp1275, bp1275len);
1873 			return (rv);
1874 		}
1875 
1876 		if (ddi_getlongprop(DDI_DEV_T_ANY,
1877 		    ddi_root_node(), 0, "boot-path",
1878 		    (caddr_t)&bp, &bplen) != DDI_PROP_SUCCESS || bplen <= 1) {
1879 			/*
1880 			 * No fallback position for matching. This is
1881 			 * certainly unexpected, but we'll handle it
1882 			 * just in case.
1883 			 */
1884 			quickexit = 1;
1885 			kmem_free(bp1275, bp1275len);
1886 			if (bp)
1887 				kmem_free(bp, bplen);
1888 			return (rv);
1889 		}
1890 
1891 		/*
1892 		 *  Determine boot device module and 1275 name_addr
1893 		 *
1894 		 *  bootpath assumed to be of the form /bus/module@name_addr
1895 		 */
1896 		if (busp = strchr(bp1275, '/')) {
1897 			if (modp = strchr(busp + 1, '/')) {
1898 				if (atp = strchr(modp + 1, '@')) {
1899 					*atp = '\0';
1900 					addrp = atp + 1;
1901 					if (eoaddrp = strchr(addrp, '/'))
1902 						*eoaddrp = '\0';
1903 				}
1904 			}
1905 		}
1906 
1907 		if (modp && addrp) {
1908 			(void) strncpy(bootdev_module, modp + 1,
1909 			    MAXCOMPONENTLEN);
1910 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
1911 
1912 			(void) strncpy(bootdev_newaddr, addrp, MAXCOMPONENTLEN);
1913 			bootdev_newaddr[MAXCOMPONENTLEN - 1] = '\0';
1914 		} else {
1915 			quickexit = 1;
1916 			kmem_free(bp1275, bp1275len);
1917 			kmem_free(bp, bplen);
1918 			return (rv);
1919 		}
1920 
1921 		/*
1922 		 *  Determine fallback name_addr
1923 		 *
1924 		 *  10/3/96 - Also save fallback module name because it
1925 		 *  might actually be different than the current module
1926 		 *  name.  E.G., ISA pnp drivers have new names.
1927 		 *
1928 		 *  bootpath assumed to be of the form /bus/module@name_addr
1929 		 */
1930 		addrp = NULL;
1931 		if (busp = strchr(bp, '/')) {
1932 			if (modp = strchr(busp + 1, '/')) {
1933 				if (atp = strchr(modp + 1, '@')) {
1934 					*atp = '\0';
1935 					addrp = atp + 1;
1936 					if (eoaddrp = strchr(addrp, '/'))
1937 						*eoaddrp = '\0';
1938 				}
1939 			}
1940 		}
1941 
1942 		if (modp && addrp) {
1943 			(void) strncpy(bootdev_oldmod, modp + 1,
1944 			    MAXCOMPONENTLEN);
1945 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
1946 
1947 			(void) strncpy(bootdev_oldaddr, addrp, MAXCOMPONENTLEN);
1948 			bootdev_oldaddr[MAXCOMPONENTLEN - 1] = '\0';
1949 		}
1950 
1951 		/* Free up the bootpath storage now that we're done with it. */
1952 		kmem_free(bp1275, bp1275len);
1953 		kmem_free(bp, bplen);
1954 
1955 		if (bootdev_oldaddr[0] == '\0') {
1956 			quickexit = 1;
1957 			return (rv);
1958 		}
1959 	}
1960 
1961 	if (((lkupname = ddi_get_name(cdip)) != NULL) &&
1962 	    (strcmp(bootdev_module, lkupname) == 0 ||
1963 	    strcmp(bootdev_oldmod, lkupname) == 0) &&
1964 	    ((ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
1965 		"ignore-hardware-nodes", -1) != -1) ||
1966 		ignore_hardware_nodes) &&
1967 	    strcmp(bootdev_newaddr, caddr) == 0 &&
1968 	    strcmp(bootdev_oldaddr, naddr) == 0) {
1969 		rv = DDI_SUCCESS;
1970 	}
1971 
1972 	return (rv);
1973 }
1974 
1975 /*
1976  * Perform a copy from a memory mapped device (whose devinfo pointer is devi)
1977  * separately mapped at devaddr in the kernel to a kernel buffer at kaddr.
1978  */
1979 /*ARGSUSED*/
1980 int
1981 e_ddi_copyfromdev(dev_info_t *devi,
1982     off_t off, const void *devaddr, void *kaddr, size_t len)
1983 {
1984 	bcopy(devaddr, kaddr, len);
1985 	return (0);
1986 }
1987 
1988 /*
1989  * Perform a copy to a memory mapped device (whose devinfo pointer is devi)
1990  * separately mapped at devaddr in the kernel from a kernel buffer at kaddr.
1991  */
1992 /*ARGSUSED*/
1993 int
1994 e_ddi_copytodev(dev_info_t *devi,
1995     off_t off, const void *kaddr, void *devaddr, size_t len)
1996 {
1997 	bcopy(kaddr, devaddr, len);
1998 	return (0);
1999 }
2000 
2001 
2002 static int
2003 poke_mem(peekpoke_ctlops_t *in_args)
2004 {
2005 	int err = DDI_SUCCESS;
2006 	on_trap_data_t otd;
2007 
2008 	/* Set up protected environment. */
2009 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
2010 		switch (in_args->size) {
2011 		case sizeof (uint8_t):
2012 			*(uint8_t *)(in_args->dev_addr) =
2013 			    *(uint8_t *)in_args->host_addr;
2014 			break;
2015 
2016 		case sizeof (uint16_t):
2017 			*(uint16_t *)(in_args->dev_addr) =
2018 			    *(uint16_t *)in_args->host_addr;
2019 			break;
2020 
2021 		case sizeof (uint32_t):
2022 			*(uint32_t *)(in_args->dev_addr) =
2023 			    *(uint32_t *)in_args->host_addr;
2024 			break;
2025 
2026 		case sizeof (uint64_t):
2027 			*(uint64_t *)(in_args->dev_addr) =
2028 			    *(uint64_t *)in_args->host_addr;
2029 			break;
2030 
2031 		default:
2032 			err = DDI_FAILURE;
2033 			break;
2034 		}
2035 	} else
2036 		err = DDI_FAILURE;
2037 
2038 	/* Take down protected environment. */
2039 	no_trap();
2040 
2041 	return (err);
2042 }
2043 
2044 
2045 static int
2046 peek_mem(peekpoke_ctlops_t *in_args)
2047 {
2048 	int err = DDI_SUCCESS;
2049 	on_trap_data_t otd;
2050 
2051 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
2052 		switch (in_args->size) {
2053 		case sizeof (uint8_t):
2054 			*(uint8_t *)in_args->host_addr =
2055 			    *(uint8_t *)in_args->dev_addr;
2056 			break;
2057 
2058 		case sizeof (uint16_t):
2059 			*(uint16_t *)in_args->host_addr =
2060 			    *(uint16_t *)in_args->dev_addr;
2061 			break;
2062 
2063 		case sizeof (uint32_t):
2064 			*(uint32_t *)in_args->host_addr =
2065 			    *(uint32_t *)in_args->dev_addr;
2066 			break;
2067 
2068 		case sizeof (uint64_t):
2069 			*(uint64_t *)in_args->host_addr =
2070 			    *(uint64_t *)in_args->dev_addr;
2071 			break;
2072 
2073 		default:
2074 			err = DDI_FAILURE;
2075 			break;
2076 		}
2077 	} else
2078 		err = DDI_FAILURE;
2079 
2080 	no_trap();
2081 	return (err);
2082 }
2083 
2084 
2085 /*
2086  * This is called only to process peek/poke when the DIP is NULL.
2087  * Assume that this is for memory, as nexi take care of device safe accesses.
2088  */
2089 int
2090 peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args)
2091 {
2092 	return (cmd == DDI_CTLOPS_PEEK ? peek_mem(in_args) : poke_mem(in_args));
2093 }
2094 
2095 void
2096 impl_setup_ddi(void)
2097 {
2098 	dev_info_t *xdip, *isa_dip;
2099 	rd_existing_t rd_mem_prop;
2100 	int err;
2101 
2102 	ndi_devi_alloc_sleep(ddi_root_node(), "ramdisk",
2103 	    (pnode_t)DEVI_SID_NODEID, &xdip);
2104 
2105 	(void) BOP_GETPROP(bootops,
2106 	    "ramdisk_start", (void *)&ramdisk_start);
2107 	(void) BOP_GETPROP(bootops,
2108 	    "ramdisk_end", (void *)&ramdisk_end);
2109 
2110 	rd_mem_prop.phys = ramdisk_start;
2111 	rd_mem_prop.size = ramdisk_end - ramdisk_start + 1;
2112 
2113 	(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, xdip,
2114 	    RD_EXISTING_PROP_NAME, (uchar_t *)&rd_mem_prop,
2115 	    sizeof (rd_mem_prop));
2116 	err = ndi_devi_bind_driver(xdip, 0);
2117 	ASSERT(err == 0);
2118 
2119 	/* isa node */
2120 	ndi_devi_alloc_sleep(ddi_root_node(), "isa",
2121 	    (pnode_t)DEVI_SID_NODEID, &isa_dip);
2122 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
2123 	    "device_type", "isa");
2124 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
2125 	    "bus-type", "isa");
2126 	(void) ndi_devi_bind_driver(isa_dip, 0);
2127 
2128 	/*
2129 	 * Read in the properties from the boot.
2130 	 */
2131 	get_boot_properties();
2132 
2133 	/* do bus dependent probes. */
2134 	impl_bus_initialprobe();
2135 
2136 	/* not framebuffer should be enumerated, if present */
2137 	get_vga_properties();
2138 }
2139 
2140 dev_t
2141 getrootdev(void)
2142 {
2143 	/*
2144 	 * Precedence given to rootdev if set in /etc/system
2145 	 */
2146 	if (root_is_svm) {
2147 		return (ddi_pathname_to_dev_t(svm_bootpath));
2148 	}
2149 
2150 	/*
2151 	 * Usually rootfs.bo_name is initialized by the
2152 	 * the bootpath property from bootenv.rc, but
2153 	 * defaults to "/ramdisk:a" otherwise.
2154 	 */
2155 	return (ddi_pathname_to_dev_t(rootfs.bo_name));
2156 }
2157 
2158 static struct bus_probe {
2159 	struct bus_probe *next;
2160 	void (*probe)(int);
2161 } *bus_probes;
2162 
2163 void
2164 impl_bus_add_probe(void (*func)(int))
2165 {
2166 	struct bus_probe *probe;
2167 
2168 	probe = kmem_alloc(sizeof (*probe), KM_SLEEP);
2169 	probe->next = bus_probes;
2170 	probe->probe = func;
2171 	bus_probes = probe;
2172 }
2173 
2174 /*ARGSUSED*/
2175 void
2176 impl_bus_delete_probe(void (*func)(int))
2177 {
2178 	struct bus_probe *prev = NULL;
2179 	struct bus_probe *probe = bus_probes;
2180 
2181 	while (probe) {
2182 		if (probe->probe == func)
2183 			break;
2184 		prev = probe;
2185 		probe = probe->next;
2186 	}
2187 
2188 	if (probe == NULL)
2189 		return;
2190 
2191 	if (prev)
2192 		prev->next = probe->next;
2193 	else
2194 		bus_probes = probe->next;
2195 
2196 	kmem_free(probe, sizeof (struct bus_probe));
2197 }
2198 
2199 /*
2200  * impl_bus_initialprobe
2201  *	Modload the prom simulator, then let it probe to verify existence
2202  *	and type of PCI support.
2203  */
2204 static void
2205 impl_bus_initialprobe(void)
2206 {
2207 	struct bus_probe *probe;
2208 
2209 	/* load modules to install bus probes */
2210 	if (modload("misc", "pci_autoconfig") < 0) {
2211 		cmn_err(CE_PANIC, "failed to load misc/pci_autoconfig");
2212 	}
2213 
2214 	probe = bus_probes;
2215 	while (probe) {
2216 		/* run the probe function */
2217 		(*probe->probe)(0);
2218 		probe = probe->next;
2219 	}
2220 }
2221 
2222 /*
2223  * impl_bus_reprobe
2224  *	Reprogram devices not set up by firmware.
2225  */
2226 static void
2227 impl_bus_reprobe(void)
2228 {
2229 	struct bus_probe *probe;
2230 
2231 	probe = bus_probes;
2232 	while (probe) {
2233 		/* run the probe function */
2234 		(*probe->probe)(1);
2235 		probe = probe->next;
2236 	}
2237 }
2238