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