xref: /titanic_51/usr/src/uts/i86pc/io/immu.c (revision bca70f54b47e1896668cd05d64cdf448a9582d74)
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  * Portions Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2009, Intel Corporation.
27  * All rights reserved.
28  */
29 
30 /*
31  * Intel IOMMU implementation
32  * This file contains Intel IOMMU code exported
33  * to the rest of the system and code that deals
34  * with the Intel IOMMU as a whole.
35  */
36 
37 #include <sys/conf.h>
38 #include <sys/modctl.h>
39 #include <sys/pci.h>
40 #include <sys/pci_impl.h>
41 #include <sys/sysmacros.h>
42 #include <sys/ddi.h>
43 #include <sys/ddidmareq.h>
44 #include <sys/ddi_impldefs.h>
45 #include <sys/ddifm.h>
46 #include <sys/sunndi.h>
47 #include <sys/debug.h>
48 #include <sys/fm/protocol.h>
49 #include <sys/note.h>
50 #include <sys/apic.h>
51 #include <vm/hat_i86.h>
52 #include <sys/smp_impldefs.h>
53 #include <sys/spl.h>
54 #include <sys/archsystm.h>
55 #include <sys/x86_archext.h>
56 #include <sys/rootnex.h>
57 #include <sys/avl.h>
58 #include <sys/bootconf.h>
59 #include <sys/bootinfo.h>
60 #include <sys/atomic.h>
61 #include <sys/immu.h>
62 /* ########################### Globals and tunables ######################## */
63 /*
64  * Global switches (boolean) that can be toggled either via boot options
65  * or via /etc/system or kmdb
66  */
67 
68 /* Various features */
69 boolean_t immu_enable = B_FALSE;
70 boolean_t immu_dvma_enable = B_TRUE;
71 
72 /* accessed in other files so not static */
73 boolean_t immu_gfxdvma_enable = B_TRUE;
74 boolean_t immu_intrmap_enable = B_FALSE;
75 boolean_t immu_qinv_enable = B_FALSE;
76 
77 /* various quirks that need working around */
78 
79 /* XXX We always map page 0 read/write for now */
80 boolean_t immu_quirk_usbpage0 = B_TRUE;
81 boolean_t immu_quirk_usbrmrr = B_TRUE;
82 boolean_t immu_quirk_usbfullpa;
83 boolean_t immu_quirk_mobile4;
84 
85 /* debug messages */
86 boolean_t immu_dmar_print;
87 
88 /* Tunables */
89 int64_t immu_flush_gran = 5;
90 
91 /* ############  END OPTIONS section ################ */
92 
93 /*
94  * Global used internally by Intel IOMMU code
95  */
96 dev_info_t *root_devinfo;
97 kmutex_t immu_lock;
98 list_t immu_list;
99 void *immu_pgtable_cache;
100 boolean_t immu_setup;
101 boolean_t immu_running;
102 boolean_t immu_quiesced;
103 
104 /* ######################## END Globals and tunables ###################### */
105 /* Globals used only in this file */
106 static char **black_array;
107 static uint_t nblacks;
108 /* ###################### Utility routines ############################# */
109 
110 /*
111  * Check if the device has mobile 4 chipset
112  */
113 static int
114 check_mobile4(dev_info_t *dip, void *arg)
115 {
116 	_NOTE(ARGUNUSED(arg));
117 	int vendor, device;
118 	int *ip = (int *)arg;
119 
120 	ASSERT(arg);
121 
122 	vendor = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
123 	    "vendor-id", -1);
124 	device = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
125 	    "device-id", -1);
126 
127 	if (vendor == 0x8086 && device == 0x2a40) {
128 		*ip = B_TRUE;
129 		ddi_err(DER_NOTE, dip, "IMMU: Mobile 4 chipset detected. "
130 		    "Force setting IOMMU write buffer");
131 		return (DDI_WALK_TERMINATE);
132 	} else {
133 		return (DDI_WALK_CONTINUE);
134 	}
135 }
136 
137 static void
138 map_bios_rsvd_mem(dev_info_t *dip)
139 {
140 	struct memlist *mp;
141 	int e;
142 
143 	memlist_read_lock();
144 
145 	mp = bios_rsvd;
146 	while (mp != NULL) {
147 		memrng_t mrng = {0};
148 
149 		ddi_err(DER_LOG, dip, "IMMU: Mapping BIOS rsvd range "
150 		    "[0x%" PRIx64 " - 0x%"PRIx64 "]\n", mp->ml_address,
151 		    mp->ml_address + mp->ml_size);
152 
153 		mrng.mrng_start = IMMU_ROUNDOWN(mp->ml_address);
154 		mrng.mrng_npages = IMMU_ROUNDUP(mp->ml_size) / IMMU_PAGESIZE;
155 
156 		e = immu_dvma_map(NULL, NULL, &mrng, 0, dip, IMMU_FLAGS_MEMRNG);
157 		ASSERT(e == DDI_DMA_MAPPED || e == DDI_DMA_USE_PHYSICAL);
158 
159 		mp = mp->ml_next;
160 	}
161 
162 	memlist_read_unlock();
163 }
164 
165 
166 /*
167  * Check if the driver requests physical mapping
168  */
169 /*ARGSUSED*/
170 static void
171 check_physical(dev_info_t *dip, void *arg)
172 {
173 	char *val;
174 
175 	/*
176 	 * Check for the DVMA unity mapping property on the device
177 	 */
178 	val = NULL;
179 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
180 	    DDI_PROP_DONTPASS, DDI_DVMA_MAPTYPE_PROP, &val) == DDI_SUCCESS) {
181 		ASSERT(val);
182 		if (strcmp(val, DDI_DVMA_MAPTYPE_UNITY) != 0) {
183 			ddi_err(DER_WARN, dip, "%s value \"%s\" is not valid",
184 			    DDI_DVMA_MAPTYPE_PROP, val);
185 		} else {
186 			int e;
187 
188 			ddi_err(DER_NOTE, dip,
189 			    "Using unity DVMA mapping for device");
190 			e = immu_dvma_map(NULL, NULL, NULL, 0, dip,
191 			    IMMU_FLAGS_UNITY);
192 			/* for unity mode, map will return USE_PHYSICAL */
193 			ASSERT(e == DDI_DMA_USE_PHYSICAL);
194 		}
195 		ddi_prop_free(val);
196 	}
197 }
198 
199 /*
200  * Check if the device is USB controller
201  */
202 /*ARGSUSED*/
203 static void
204 check_usb(dev_info_t *dip, void *arg)
205 {
206 	const char *drv = ddi_driver_name(dip);
207 
208 	if (drv == NULL ||
209 	    (strcmp(drv, "uhci") != 0 && strcmp(drv, "ohci") != 0 &&
210 	    strcmp(drv, "ehci") != 0)) {
211 		return;
212 	}
213 
214 	/* This must come first since it does unity mapping */
215 	if (immu_quirk_usbfullpa == B_TRUE) {
216 		int e;
217 		ddi_err(DER_NOTE, dip, "Applying USB FULL PA quirk");
218 		e = immu_dvma_map(NULL, NULL, NULL, 0, dip, IMMU_FLAGS_UNITY);
219 		/* for unity mode, map will return USE_PHYSICAL */
220 		ASSERT(e == DDI_DMA_USE_PHYSICAL);
221 	}
222 
223 	if (immu_quirk_usbrmrr == B_TRUE) {
224 		ddi_err(DER_LOG, dip, "Applying USB RMRR quirk");
225 		map_bios_rsvd_mem(dip);
226 	}
227 }
228 
229 /*
230  * Check if the device is a LPC device
231  */
232 /*ARGSUSED*/
233 static void
234 check_lpc(dev_info_t *dip, void *arg)
235 {
236 	immu_devi_t *immu_devi;
237 
238 	immu_devi = immu_devi_get(dip);
239 	ASSERT(immu_devi);
240 	if (immu_devi->imd_lpc == B_TRUE) {
241 		ddi_err(DER_LOG, dip, "IMMU: Found LPC device");
242 		/* This will put the immu_devi on the LPC "specials" list */
243 		(void) immu_dvma_get_immu(dip, IMMU_FLAGS_SLEEP);
244 	}
245 }
246 
247 /*
248  * Check if the device is a GFX device
249  */
250 /*ARGSUSED*/
251 static void
252 check_gfx(dev_info_t *dip, void *arg)
253 {
254 	immu_devi_t *immu_devi;
255 	int e;
256 
257 	immu_devi = immu_devi_get(dip);
258 	ASSERT(immu_devi);
259 	if (immu_devi->imd_display == B_TRUE) {
260 		ddi_err(DER_LOG, dip, "IMMU: Found GFX device");
261 		/* This will put the immu_devi on the GFX "specials" list */
262 		(void) immu_dvma_get_immu(dip, IMMU_FLAGS_SLEEP);
263 		e = immu_dvma_map(NULL, NULL, NULL, 0, dip, IMMU_FLAGS_UNITY);
264 		/* for unity mode, map will return USE_PHYSICAL */
265 		ASSERT(e == DDI_DMA_USE_PHYSICAL);
266 	}
267 }
268 
269 static void
270 walk_tree(int (*f)(dev_info_t *, void *), void *arg)
271 {
272 	int count;
273 
274 	ndi_devi_enter(root_devinfo, &count);
275 	ddi_walk_devs(ddi_get_child(root_devinfo), f, arg);
276 	ndi_devi_exit(root_devinfo, count);
277 }
278 
279 static int
280 check_pre_setup_quirks(dev_info_t *dip, void *arg)
281 {
282 	/* just 1 check right now */
283 	return (check_mobile4(dip, arg));
284 }
285 
286 static int
287 check_pre_startup_quirks(dev_info_t *dip, void *arg)
288 {
289 	if (immu_devi_set(dip, IMMU_FLAGS_SLEEP) != DDI_SUCCESS) {
290 		ddi_err(DER_PANIC, dip, "Failed to get immu_devi");
291 	}
292 
293 	check_gfx(dip, arg);
294 
295 	check_lpc(dip, arg);
296 
297 	check_usb(dip, arg);
298 
299 	check_physical(dip, arg);
300 
301 	return (DDI_WALK_CONTINUE);
302 }
303 
304 static void
305 pre_setup_quirks(void)
306 {
307 	walk_tree(check_pre_setup_quirks, &immu_quirk_mobile4);
308 }
309 
310 static void
311 pre_startup_quirks(void)
312 {
313 	walk_tree(check_pre_startup_quirks, NULL);
314 
315 	immu_dmar_rmrr_map();
316 }
317 
318 /*
319  * get_bootopt()
320  * 	check a boot option  (always a boolean)
321  */
322 static void
323 get_bootopt(char *bopt, boolean_t *kvar)
324 {
325 	char *val = NULL;
326 
327 	ASSERT(bopt);
328 	ASSERT(kvar);
329 
330 	/*
331 	 * All boot options set at the GRUB menu become
332 	 * properties on the rootnex.
333 	 */
334 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, root_devinfo,
335 	    DDI_PROP_DONTPASS, bopt, &val) == DDI_SUCCESS) {
336 		ASSERT(val);
337 		if (strcmp(val, "true") == 0) {
338 			*kvar = B_TRUE;
339 		} else if (strcmp(val, "false") == 0) {
340 			*kvar = B_FALSE;
341 		} else {
342 			ddi_err(DER_WARN, NULL, "boot option %s=\"%s\" ",
343 			    "is not set to true or false. Ignoring option.",
344 			    bopt, val);
345 		}
346 		ddi_prop_free(val);
347 	}
348 }
349 
350 static void
351 get_tunables(char *bopt, int64_t *ivar)
352 {
353 	int64_t	*iarray;
354 	uint_t n;
355 
356 	/*
357 	 * Check the rootnex.conf property
358 	 * Fake up a dev_t since searching the global
359 	 * property list needs it
360 	 */
361 	if (ddi_prop_lookup_int64_array(
362 	    makedevice(ddi_name_to_major("rootnex"), 0), root_devinfo,
363 	    DDI_PROP_DONTPASS | DDI_PROP_ROOTNEX_GLOBAL, bopt,
364 	    &iarray, &n) != DDI_PROP_SUCCESS) {
365 		return;
366 	}
367 
368 	if (n != 1) {
369 		ddi_err(DER_WARN, NULL, "More than one value specified for "
370 		    "%s property. Ignoring and using default",
371 		    "immu-flush-gran");
372 		ddi_prop_free(iarray);
373 		return;
374 	}
375 
376 	if (iarray[0] < 0) {
377 		ddi_err(DER_WARN, NULL, "Negative value specified for "
378 		    "%s property. Inoring and Using default value",
379 		    "immu-flush-gran");
380 		ddi_prop_free(iarray);
381 		return;
382 	}
383 
384 	*ivar = iarray[0];
385 
386 	ddi_prop_free(iarray);
387 }
388 
389 static void
390 read_boot_options(void)
391 {
392 	/* enable/disable options */
393 	get_bootopt("immu-enable", &immu_enable);
394 	get_bootopt("immu-dvma-enable", &immu_dvma_enable);
395 	get_bootopt("immu-gfxdvma-enable", &immu_gfxdvma_enable);
396 	get_bootopt("immu-intrmap-enable", &immu_intrmap_enable);
397 	get_bootopt("immu-qinv-enable", &immu_qinv_enable);
398 
399 	/* workaround switches */
400 	get_bootopt("immu-quirk-usbpage0", &immu_quirk_usbpage0);
401 	get_bootopt("immu-quirk-usbfullpa", &immu_quirk_usbfullpa);
402 	get_bootopt("immu-quirk-usbrmrr", &immu_quirk_usbrmrr);
403 
404 	/* debug printing */
405 	get_bootopt("immu-dmar-print", &immu_dmar_print);
406 
407 	/* get tunables */
408 	get_tunables("immu-flush-gran", &immu_flush_gran);
409 }
410 
411 /*
412  * Note, this will not catch hardware not enumerated
413  * in early boot
414  */
415 static boolean_t
416 blacklisted_driver(void)
417 {
418 	char **strptr;
419 	int i;
420 	major_t maj;
421 
422 	ASSERT((black_array == NULL) ^ (nblacks != 0));
423 
424 	/* need at least 2 strings */
425 	if (nblacks < 2) {
426 		return (B_FALSE);
427 	}
428 
429 	for (i = 0; nblacks - i > 1; i++) {
430 		strptr = &black_array[i];
431 		if (strcmp(*strptr++, "DRIVER") == 0) {
432 			if ((maj = ddi_name_to_major(*strptr++))
433 			    != DDI_MAJOR_T_NONE) {
434 				/* is there hardware bound to this drvr */
435 				if (devnamesp[maj].dn_head != NULL) {
436 					return (B_TRUE);
437 				}
438 			}
439 			i += 1;   /* for loop adds 1, so add only 1 here */
440 		}
441 	}
442 
443 	return (B_FALSE);
444 }
445 
446 static boolean_t
447 blacklisted_smbios(void)
448 {
449 	id_t smid;
450 	smbios_hdl_t *smhdl;
451 	smbios_info_t sminf;
452 	smbios_system_t smsys;
453 	char *mfg, *product, *version;
454 	char **strptr;
455 	int i;
456 
457 	ASSERT((black_array == NULL) ^ (nblacks != 0));
458 
459 	/* need at least 4 strings for this setting */
460 	if (nblacks < 4) {
461 		return (B_FALSE);
462 	}
463 
464 	smhdl = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
465 	if (smhdl == NULL ||
466 	    (smid = smbios_info_system(smhdl, &smsys)) == SMB_ERR ||
467 	    smbios_info_common(smhdl, smid, &sminf) == SMB_ERR) {
468 		return (B_FALSE);
469 	}
470 
471 	mfg = (char *)sminf.smbi_manufacturer;
472 	product = (char *)sminf.smbi_product;
473 	version = (char *)sminf.smbi_version;
474 
475 	ddi_err(DER_CONT, NULL, "?System SMBIOS information:\n");
476 	ddi_err(DER_CONT, NULL, "?Manufacturer = <%s>\n", mfg);
477 	ddi_err(DER_CONT, NULL, "?Product = <%s>\n", product);
478 	ddi_err(DER_CONT, NULL, "?Version = <%s>\n", version);
479 
480 	for (i = 0; nblacks - i > 3; i++) {
481 		strptr = &black_array[i];
482 		if (strcmp(*strptr++, "SMBIOS") == 0) {
483 			if (strcmp(*strptr++, mfg) == 0 &&
484 			    ((char *)strptr == '\0' ||
485 			    strcmp(*strptr++, product) == 0) &&
486 			    ((char *)strptr == '\0' ||
487 			    strcmp(*strptr++, version) == 0)) {
488 				return (B_TRUE);
489 			}
490 			i += 3;
491 		}
492 	}
493 
494 	return (B_FALSE);
495 }
496 
497 static boolean_t
498 blacklisted_acpi(void)
499 {
500 	ASSERT((black_array == NULL) ^ (nblacks != 0));
501 	if (nblacks == 0) {
502 		return (B_FALSE);
503 	}
504 
505 	return (immu_dmar_blacklisted(black_array, nblacks));
506 }
507 
508 /*
509  * Check if system is blacklisted by Intel IOMMU driver
510  * i.e. should Intel IOMMU be disabled on this system
511  * Currently a system can be blacklistd based on the
512  * following bases:
513  *
514  * 1. DMAR ACPI table information.
515  *    This information includes things like
516  *    manufacturer and revision number. If rootnex.conf
517  *    has matching info set in its blacklist property
518  *    then Intel IOMMu will be disabled
519  *
520  * 2. SMBIOS information
521  *
522  * 3. Driver installed - useful if a particular
523  *    driver or hardware is toxic if Intel IOMMU
524  *    is turned on.
525  */
526 
527 static void
528 blacklist_setup(void)
529 {
530 	char **string_array;
531 	uint_t nstrings;
532 
533 	/*
534 	 * Check the rootnex.conf blacklist property.
535 	 * Fake up a dev_t since searching the global
536 	 * property list needs it
537 	 */
538 	if (ddi_prop_lookup_string_array(
539 	    makedevice(ddi_name_to_major("rootnex"), 0), root_devinfo,
540 	    DDI_PROP_DONTPASS | DDI_PROP_ROOTNEX_GLOBAL, "immu-blacklist",
541 	    &string_array, &nstrings) != DDI_PROP_SUCCESS) {
542 		return;
543 	}
544 
545 	/* smallest blacklist criteria works with multiples of 2 */
546 	if (nstrings % 2 != 0) {
547 		ddi_err(DER_WARN, NULL, "Invalid IOMMU blacklist "
548 		    "rootnex.conf: number of strings must be a "
549 		    "multiple of 2");
550 		ddi_prop_free(string_array);
551 		return;
552 	}
553 
554 	black_array = string_array;
555 	nblacks = nstrings;
556 }
557 
558 static void
559 blacklist_destroy(void)
560 {
561 	if (black_array) {
562 		ddi_prop_free(black_array);
563 		black_array = NULL;
564 		nblacks = 0;
565 	}
566 
567 	ASSERT(black_array == NULL);
568 	ASSERT(nblacks == 0);
569 }
570 
571 
572 /*
573  * Now set all the fields in the order they are defined
574  * We do this only as a defensive-coding practice, it is
575  * not a correctness issue.
576  */
577 static void *
578 immu_state_alloc(int seg, void *dmar_unit)
579 {
580 	immu_t *immu;
581 
582 	dmar_unit = immu_dmar_walk_units(seg, dmar_unit);
583 	if (dmar_unit == NULL) {
584 		/* No more IOMMUs in this segment */
585 		return (NULL);
586 	}
587 
588 	immu = kmem_zalloc(sizeof (immu_t), KM_SLEEP);
589 
590 	mutex_init(&(immu->immu_lock), NULL, MUTEX_DRIVER, NULL);
591 
592 	mutex_enter(&(immu->immu_lock));
593 
594 	immu->immu_dmar_unit = dmar_unit;
595 	immu->immu_name = ddi_strdup(immu_dmar_unit_name(dmar_unit),
596 	    KM_SLEEP);
597 	immu->immu_dip = immu_dmar_unit_dip(dmar_unit);
598 
599 	/*
600 	 * the immu_intr_lock mutex is grabbed by the IOMMU
601 	 * unit's interrupt handler so we need to use an
602 	 * interrupt cookie for the mutex
603 	 */
604 	mutex_init(&(immu->immu_intr_lock), NULL, MUTEX_DRIVER,
605 	    (void *)ipltospl(IMMU_INTR_IPL));
606 
607 	/* IOMMU regs related */
608 	mutex_init(&(immu->immu_regs_lock), NULL, MUTEX_DEFAULT, NULL);
609 	cv_init(&(immu->immu_regs_cv), NULL, CV_DEFAULT, NULL);
610 	immu->immu_regs_busy = B_FALSE;
611 
612 	/* DVMA related */
613 	immu->immu_dvma_coherent = B_FALSE;
614 
615 	/* DVMA context related */
616 	rw_init(&(immu->immu_ctx_rwlock), NULL, RW_DEFAULT, NULL);
617 
618 	/* DVMA domain related */
619 	list_create(&(immu->immu_domain_list), sizeof (domain_t),
620 	    offsetof(domain_t, dom_immu_node));
621 
622 	/* DVMA special device lists */
623 	immu->immu_dvma_gfx_only = B_FALSE;
624 	list_create(&(immu->immu_dvma_lpc_list), sizeof (immu_devi_t),
625 	    offsetof(immu_devi_t, imd_spc_node));
626 	list_create(&(immu->immu_dvma_gfx_list), sizeof (immu_devi_t),
627 	    offsetof(immu_devi_t, imd_spc_node));
628 
629 	/* interrupt remapping related */
630 	mutex_init(&(immu->immu_intrmap_lock), NULL, MUTEX_DEFAULT, NULL);
631 
632 	/* qinv related */
633 	mutex_init(&(immu->immu_qinv_lock), NULL, MUTEX_DEFAULT, NULL);
634 
635 	/*
636 	 * insert this immu unit into the system-wide list
637 	 */
638 	list_insert_tail(&immu_list, immu);
639 
640 	mutex_exit(&(immu->immu_lock));
641 
642 	ddi_err(DER_LOG, immu->immu_dip, "IMMU: unit setup");
643 
644 	immu_dmar_set_immu(dmar_unit, immu);
645 
646 	return (dmar_unit);
647 }
648 
649 static void
650 immu_subsystems_setup(void)
651 {
652 	int seg;
653 	void *unit_hdl;
654 
655 	ddi_err(DER_VERB, NULL,
656 	    "Creating state structures for Intel IOMMU units\n");
657 
658 	ASSERT(immu_setup == B_FALSE);
659 	ASSERT(immu_running == B_FALSE);
660 
661 	mutex_init(&immu_lock, NULL, MUTEX_DEFAULT, NULL);
662 	list_create(&immu_list, sizeof (immu_t), offsetof(immu_t, immu_node));
663 
664 	mutex_enter(&immu_lock);
665 
666 	ASSERT(immu_pgtable_cache == NULL);
667 
668 	immu_pgtable_cache = kmem_cache_create("immu_pgtable_cache",
669 	    sizeof (pgtable_t), 0,
670 	    pgtable_ctor, pgtable_dtor, NULL, NULL, NULL, 0);
671 
672 	unit_hdl = NULL;
673 	for (seg = 0; seg < IMMU_MAXSEG; seg++) {
674 		while (unit_hdl = immu_state_alloc(seg, unit_hdl)) {
675 			;
676 		}
677 	}
678 
679 	immu_regs_setup(&immu_list);	/* subsequent code needs this first */
680 	immu_dvma_setup(&immu_list);
681 	immu_intrmap_setup(&immu_list);
682 	immu_qinv_setup(&immu_list);
683 
684 	mutex_exit(&immu_lock);
685 }
686 
687 /*
688  * immu_subsystems_startup()
689  * 	startup all units that were setup
690  */
691 static void
692 immu_subsystems_startup(void)
693 {
694 	immu_t *immu;
695 
696 	mutex_enter(&immu_lock);
697 
698 	ASSERT(immu_setup == B_TRUE);
699 	ASSERT(immu_running == B_FALSE);
700 
701 	immu_dmar_startup();
702 
703 	immu = list_head(&immu_list);
704 	for (; immu; immu = list_next(&immu_list, immu)) {
705 
706 		mutex_enter(&(immu->immu_lock));
707 
708 		immu_intr_register(immu);
709 		immu_dvma_startup(immu);
710 		immu_intrmap_startup(immu);
711 		immu_qinv_startup(immu);
712 
713 		/*
714 		 * Set IOMMU unit's regs to do
715 		 * the actual startup. This will
716 		 * set immu->immu_running  field
717 		 * if the unit is successfully
718 		 * started
719 		 */
720 		immu_regs_startup(immu);
721 
722 		mutex_exit(&(immu->immu_lock));
723 	}
724 
725 	mutex_exit(&immu_lock);
726 }
727 
728 /* ##################  Intel IOMMU internal interfaces ###################### */
729 
730 /*
731  * Internal interfaces for IOMMU code (i.e. not exported to rootnex
732  * or rest of system)
733  */
734 
735 /*
736  * ddip can be NULL, in which case we walk up until we find the root dip
737  * NOTE: We never visit the root dip since its not a hardware node
738  */
739 int
740 immu_walk_ancestor(
741 	dev_info_t *rdip,
742 	dev_info_t *ddip,
743 	int (*func)(dev_info_t *, void *arg),
744 	void *arg,
745 	int *lvlp,
746 	immu_flags_t immu_flags)
747 {
748 	dev_info_t *pdip;
749 	int level;
750 	int error = DDI_SUCCESS;
751 
752 	ASSERT(root_devinfo);
753 	ASSERT(rdip);
754 	ASSERT(rdip != root_devinfo);
755 	ASSERT(func);
756 
757 	/* ddip and immu can be NULL */
758 
759 	/* Hold rdip so that branch is not detached */
760 	ndi_hold_devi(rdip);
761 	for (pdip = rdip, level = 1; pdip && pdip != root_devinfo;
762 	    pdip = ddi_get_parent(pdip), level++) {
763 
764 		if (immu_devi_set(pdip, immu_flags) != DDI_SUCCESS) {
765 			error = DDI_FAILURE;
766 			break;
767 		}
768 		if (func(pdip, arg) == DDI_WALK_TERMINATE) {
769 			break;
770 		}
771 		if (immu_flags & IMMU_FLAGS_DONTPASS) {
772 			break;
773 		}
774 		if (pdip == ddip) {
775 			break;
776 		}
777 	}
778 
779 	ndi_rele_devi(rdip);
780 
781 	if (lvlp)
782 		*lvlp = level;
783 
784 	return (error);
785 }
786 
787 /* ########################  Intel IOMMU entry points ####################### */
788 /*
789  * immu_init()
790  *	called from rootnex_attach(). setup but don't startup the Intel IOMMU
791  *      This is the first function called in Intel IOMMU code
792  */
793 void
794 immu_init(void)
795 {
796 	char *phony_reg = "A thing of beauty is a joy forever";
797 
798 	/* Set some global shorthands that are needed by all of IOMMU code */
799 	ASSERT(root_devinfo == NULL);
800 	root_devinfo = ddi_root_node();
801 
802 	/*
803 	 * Intel IOMMU only supported only if MMU(CPU) page size is ==
804 	 * IOMMU pages size.
805 	 */
806 	/*LINTED*/
807 	if (MMU_PAGESIZE != IMMU_PAGESIZE) {
808 		ddi_err(DER_WARN, NULL,
809 		    "MMU page size (%d) is not equal to\n"
810 		    "IOMMU page size (%d). "
811 		    "Disabling Intel IOMMU. ",
812 		    MMU_PAGESIZE, IMMU_PAGESIZE);
813 		immu_enable = B_FALSE;
814 		return;
815 	}
816 
817 	/*
818 	 * retrieve the Intel IOMMU boot options.
819 	 * Do this before parsing immu ACPI table
820 	 * as a boot option could potentially affect
821 	 * ACPI parsing.
822 	 */
823 	ddi_err(DER_CONT, NULL, "?Reading Intel IOMMU boot options\n");
824 	read_boot_options();
825 
826 	/*
827 	 * Check the IOMMU enable boot-option first.
828 	 * This is so that we can skip parsing the ACPI table
829 	 * if necessary because that may cause problems in
830 	 * systems with buggy BIOS or ACPI tables
831 	 */
832 	if (immu_enable == B_FALSE) {
833 		return;
834 	}
835 
836 	/*
837 	 * Next, check if the system even has an Intel IOMMU
838 	 * We use the presence or absence of the IOMMU ACPI
839 	 * table to detect Intel IOMMU.
840 	 */
841 	if (immu_dmar_setup() != DDI_SUCCESS) {
842 		immu_enable = B_FALSE;
843 		return;
844 	}
845 
846 	/*
847 	 * Check blacklists
848 	 */
849 	blacklist_setup();
850 
851 	if (blacklisted_smbios() == B_TRUE) {
852 		blacklist_destroy();
853 		immu_enable = B_FALSE;
854 		return;
855 	}
856 
857 	if (blacklisted_driver() == B_TRUE) {
858 		blacklist_destroy();
859 		immu_enable = B_FALSE;
860 		return;
861 	}
862 
863 	/*
864 	 * Read the "raw" DMAR ACPI table to get information
865 	 * and convert into a form we can use.
866 	 */
867 	if (immu_dmar_parse() != DDI_SUCCESS) {
868 		blacklist_destroy();
869 		immu_enable = B_FALSE;
870 		return;
871 	}
872 
873 	/*
874 	 * now that we have processed the ACPI table
875 	 * check if we need to blacklist this system
876 	 * based on ACPI info
877 	 */
878 	if (blacklisted_acpi() == B_TRUE) {
879 		immu_dmar_destroy();
880 		blacklist_destroy();
881 		immu_enable = B_FALSE;
882 		return;
883 	}
884 
885 	blacklist_destroy();
886 
887 	/*
888 	 * Check if system has HW quirks.
889 	 */
890 	pre_setup_quirks();
891 
892 	/* Now do the rest of the setup */
893 	immu_subsystems_setup();
894 
895 	/*
896 	 * Now that the IMMU is setup, create a phony
897 	 * reg prop so that suspend/resume works
898 	 */
899 	if (ddi_prop_update_byte_array(DDI_DEV_T_NONE, root_devinfo, "reg",
900 	    (uchar_t *)phony_reg, strlen(phony_reg) + 1) != DDI_PROP_SUCCESS) {
901 		ddi_err(DER_PANIC, NULL, "Failed to create reg prop for "
902 		    "rootnex node");
903 		/*NOTREACHED*/
904 	}
905 
906 	immu_setup = B_TRUE;
907 }
908 
909 /*
910  * immu_startup()
911  * 	called directly by boot code to startup
912  *      all units of the IOMMU
913  */
914 void
915 immu_startup(void)
916 {
917 	/*
918 	 * If IOMMU is disabled, do nothing
919 	 */
920 	if (immu_enable == B_FALSE) {
921 		return;
922 	}
923 
924 	if (immu_setup == B_FALSE) {
925 		ddi_err(DER_WARN, NULL, "Intel IOMMU not setup, "
926 		    "skipping IOMU startup");
927 		return;
928 	}
929 
930 	pre_startup_quirks();
931 
932 	ddi_err(DER_CONT, NULL,
933 	    "?Starting Intel IOMMU (dmar) units...\n");
934 
935 	immu_subsystems_startup();
936 
937 	immu_running = B_TRUE;
938 }
939 
940 /*
941  * immu_map_sgl()
942  * 	called from rootnex_coredma_bindhdl() when Intel
943  *	IOMMU is enabled to build DVMA cookies and map them.
944  */
945 int
946 immu_map_sgl(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
947     int prealloc_count, dev_info_t *rdip)
948 {
949 	if (immu_running == B_FALSE) {
950 		return (DDI_DMA_USE_PHYSICAL);
951 	}
952 
953 	return (immu_dvma_map(hp, dmareq, NULL, prealloc_count, rdip,
954 	    IMMU_FLAGS_DMAHDL));
955 }
956 
957 /*
958  * immu_unmap_sgl()
959  * 	called from rootnex_coredma_unbindhdl(), to unmap DVMA
960  * 	cookies and free them
961  */
962 int
963 immu_unmap_sgl(ddi_dma_impl_t *hp, dev_info_t *rdip)
964 {
965 	if (immu_running == B_FALSE) {
966 		return (DDI_DMA_USE_PHYSICAL);
967 	}
968 
969 	return (immu_dvma_unmap(hp, rdip));
970 }
971 
972 /*
973  * Hook to notify IOMMU code of device tree changes
974  */
975 void
976 immu_device_tree_changed(void)
977 {
978 	if (immu_setup == B_FALSE) {
979 		return;
980 	}
981 
982 	ddi_err(DER_WARN, NULL, "Intel IOMMU currently "
983 	    "does not use device tree updates");
984 }
985 
986 /*
987  * Hook to notify IOMMU code of memory changes
988  */
989 void
990 immu_physmem_update(uint64_t addr, uint64_t size)
991 {
992 	if (immu_setup == B_FALSE) {
993 		return;
994 	}
995 	immu_dvma_physmem_update(addr, size);
996 }
997 
998 /*
999  * immu_quiesce()
1000  * 	quiesce all units that are running
1001  */
1002 int
1003 immu_quiesce(void)
1004 {
1005 	immu_t *immu;
1006 	int ret = DDI_SUCCESS;
1007 
1008 	mutex_enter(&immu_lock);
1009 
1010 	if (immu_running == B_FALSE)
1011 		return (DDI_SUCCESS);
1012 
1013 	ASSERT(immu_setup == B_TRUE);
1014 
1015 	immu = list_head(&immu_list);
1016 	for (; immu; immu = list_next(&immu_list, immu)) {
1017 
1018 		/* if immu is not running, we dont quiesce */
1019 		if (immu->immu_regs_running == B_FALSE)
1020 			continue;
1021 
1022 		/* flush caches */
1023 		rw_enter(&(immu->immu_ctx_rwlock), RW_WRITER);
1024 		immu_regs_context_flush(immu, 0, 0, 0, CONTEXT_GLOBAL);
1025 		rw_exit(&(immu->immu_ctx_rwlock));
1026 		immu_regs_iotlb_flush(immu, 0, 0, 0, 0, IOTLB_GLOBAL);
1027 		immu_regs_wbf_flush(immu);
1028 
1029 		mutex_enter(&(immu->immu_lock));
1030 
1031 		/*
1032 		 * Set IOMMU unit's regs to do
1033 		 * the actual shutdown.
1034 		 */
1035 		immu_regs_shutdown(immu);
1036 		immu_regs_suspend(immu);
1037 
1038 		/* if immu is still running, we failed */
1039 		if (immu->immu_regs_running == B_TRUE)
1040 			ret = DDI_FAILURE;
1041 		else
1042 			immu->immu_regs_quiesced = B_TRUE;
1043 
1044 		mutex_exit(&(immu->immu_lock));
1045 	}
1046 	mutex_exit(&immu_lock);
1047 
1048 	if (ret == DDI_SUCCESS) {
1049 		immu_running = B_FALSE;
1050 		immu_quiesced = B_TRUE;
1051 	}
1052 
1053 	return (ret);
1054 }
1055 
1056 /*
1057  * immu_unquiesce()
1058  * 	unquiesce all units
1059  */
1060 int
1061 immu_unquiesce(void)
1062 {
1063 	immu_t *immu;
1064 	int ret = DDI_SUCCESS;
1065 
1066 	mutex_enter(&immu_lock);
1067 
1068 	if (immu_quiesced == B_FALSE)
1069 		return (DDI_SUCCESS);
1070 
1071 	ASSERT(immu_setup == B_TRUE);
1072 	ASSERT(immu_running == B_FALSE);
1073 
1074 	immu = list_head(&immu_list);
1075 	for (; immu; immu = list_next(&immu_list, immu)) {
1076 
1077 		mutex_enter(&(immu->immu_lock));
1078 
1079 		/* if immu was not quiesced, i.e was not running before */
1080 		if (immu->immu_regs_quiesced == B_FALSE) {
1081 			mutex_exit(&(immu->immu_lock));
1082 			continue;
1083 		}
1084 
1085 		if (immu_regs_resume(immu) != DDI_SUCCESS) {
1086 			ret = DDI_FAILURE;
1087 			mutex_exit(&(immu->immu_lock));
1088 			continue;
1089 		}
1090 
1091 		/* flush caches before unquiesce */
1092 		rw_enter(&(immu->immu_ctx_rwlock), RW_WRITER);
1093 		immu_regs_context_flush(immu, 0, 0, 0, CONTEXT_GLOBAL);
1094 		rw_exit(&(immu->immu_ctx_rwlock));
1095 		immu_regs_iotlb_flush(immu, 0, 0, 0, 0, IOTLB_GLOBAL);
1096 
1097 		/*
1098 		 * Set IOMMU unit's regs to do
1099 		 * the actual startup. This will
1100 		 * set immu->immu_regs_running  field
1101 		 * if the unit is successfully
1102 		 * started
1103 		 */
1104 		immu_regs_startup(immu);
1105 
1106 		if (immu->immu_regs_running == B_FALSE) {
1107 			ret = DDI_FAILURE;
1108 		} else {
1109 			immu_quiesced = B_TRUE;
1110 			immu_running = B_TRUE;
1111 			immu->immu_regs_quiesced = B_FALSE;
1112 		}
1113 
1114 		mutex_exit(&(immu->immu_lock));
1115 	}
1116 
1117 	mutex_exit(&immu_lock);
1118 
1119 	return (ret);
1120 }
1121 
1122 /* ##############  END Intel IOMMU entry points ################## */
1123