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