summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/io/iommulib.c
blob: f3a78bded91c38438e6cbc68ec71392b05bd439b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
 */

#pragma ident	"@(#)iommulib.c	1.6	08/09/07 SMI"

#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/errno.h>
#include <sys/modctl.h>
#include <sys/iommulib.h>

/* ******** Type definitions private to this file  ********************** */

/* 1 per IOMMU unit. There may be more than one per dip */
typedef struct iommulib_unit {
	kmutex_t ilu_lock;
	uint64_t ilu_ref;
	uint32_t ilu_unitid;
	dev_info_t *ilu_dip;
	iommulib_ops_t *ilu_ops;
	void* ilu_data;
	struct iommulib_unit *ilu_next;
	struct iommulib_unit *ilu_prev;
	iommulib_nexhandle_t ilu_nex;
} iommulib_unit_t;

typedef struct iommulib_nex {
	dev_info_t *nex_dip;
	iommulib_nexops_t nex_ops;
	struct iommulib_nex *nex_next;
	struct iommulib_nex *nex_prev;
	uint_t nex_ref;
} iommulib_nex_t;

/* *********  Globals ************************ */

/* For IOMMU drivers */
smbios_hdl_t *iommulib_smbios;

/* IOMMU side: Following data protected by lock */
static kmutex_t iommulib_lock;
static iommulib_unit_t   *iommulib_list;
static uint64_t iommulib_unit_ids = 0;
static uint64_t iommulib_num_units = 0;

/* rootnex side data */

static kmutex_t iommulib_nexus_lock;
static iommulib_nex_t *iommulib_nexus_list;

/* can be set atomically without lock */
static volatile uint32_t iommulib_fini;

/* debug flag */
static int iommulib_debug;

/*
 * Module linkage information for the kernel.
 */
static struct modlmisc modlmisc = {
	&mod_miscops, "IOMMU library module"
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlmisc, NULL
};

int
_init(void)
{
	return (mod_install(&modlinkage));
}

int
_fini(void)
{
	mutex_enter(&iommulib_lock);
	if (iommulib_list != NULL || iommulib_nexus_list != NULL) {
		mutex_exit(&iommulib_lock);
		return (EBUSY);
	}
	iommulib_fini = 1;

	mutex_exit(&iommulib_lock);
	return (mod_remove(&modlinkage));
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

/*
 * Routines with iommulib_iommu_* are invoked from the
 * IOMMU driver.
 * Routines with iommulib_nex* are invoked from the
 * nexus driver (typically rootnex)
 */

int
iommulib_nexus_register(dev_info_t *dip, iommulib_nexops_t *nexops,
    iommulib_nexhandle_t *handle)
{
	iommulib_nex_t *nexp;
	int instance = ddi_get_instance(dip);
	const char *driver = ddi_driver_name(dip);
	dev_info_t *pdip = ddi_get_parent(dip);
	const char *f = "iommulib_nexus_register";

	ASSERT(nexops);
	ASSERT(handle);

	*handle = NULL;

	/*
	 * Root node is never busy held
	 */
	if (dip != ddi_root_node() && (i_ddi_node_state(dip) < DS_PROBED ||
	    !DEVI_BUSY_OWNED(pdip))) {
		cmn_err(CE_WARN, "%s: NEXUS devinfo node not in DS_PROBED "
		    "or busy held for nexops vector (%p). Failing registration",
		    f, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_vers != IOMMU_NEXOPS_VERSION) {
		cmn_err(CE_WARN, "%s: %s%d: Invalid IOMMULIB nexops version "
		    "in nexops vector (%p). Failing NEXUS registration",
		    f, driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	ASSERT(nexops->nops_data == NULL);

	if (nexops->nops_id == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL ID field. "
		    "Failing registration for nexops vector: %p",
		    f, driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_allochdl == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_allochdl op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_freehdl == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_freehdl op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_bindhdl == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_bindhdl op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_sync == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_sync op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_reset_cookies == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_reset_cookies op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_get_cookies == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_get_cookies op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_set_cookies == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_set_cookies op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_clear_cookies == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_clear_cookies op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_get_sleep_flags == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_get_sleep_flags op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dma_win == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dma_win op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dmahdl_setprivate == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dmahdl_setprivate op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	if (nexops->nops_dmahdl_getprivate == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL nops_dmahdl_getprivate op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)nexops);
		return (DDI_FAILURE);
	}

	nexp = kmem_zalloc(sizeof (iommulib_nex_t), KM_SLEEP);

	mutex_enter(&iommulib_lock);
	if (iommulib_fini == 1) {
		mutex_exit(&iommulib_lock);
		cmn_err(CE_WARN, "%s: IOMMULIB unloading. "
		    "Failing NEXUS register.", f);
		kmem_free(nexp, sizeof (iommulib_nex_t));
		return (DDI_FAILURE);
	}

	/*
	 * fini/register race conditions have been handled. Now create the
	 * nexus struct
	 */
	ndi_hold_devi(dip);
	nexp->nex_dip = dip;
	nexp->nex_ops = *nexops;

	mutex_enter(&iommulib_nexus_lock);
	nexp->nex_next = iommulib_nexus_list;
	iommulib_nexus_list = nexp;
	nexp->nex_prev = NULL;

	if (nexp->nex_next != NULL)
		nexp->nex_next->nex_prev = nexp;

	nexp->nex_ref = 0;

	/*
	 * The nexus device won't be controlled by an IOMMU.
	 */
	DEVI(dip)->devi_iommulib_handle = IOMMU_HANDLE_UNUSED;

	DEVI(dip)->devi_iommulib_nex_handle = nexp;

	mutex_exit(&iommulib_nexus_lock);
	mutex_exit(&iommulib_lock);

	cmn_err(CE_NOTE, "!%s: %s%d: Succesfully registered NEXUS %s "
	    "nexops=%p", f, driver, instance, ddi_node_name(dip),
	    (void *)nexops);

	*handle = nexp;

	return (DDI_SUCCESS);
}

int
iommulib_nexus_unregister(iommulib_nexhandle_t handle)
{
	dev_info_t *dip;
	int instance;
	const char *driver;
	iommulib_nex_t *nexp = (iommulib_nex_t *)handle;
	const char *f = "iommulib_nexus_unregister";

	ASSERT(nexp);

	if (nexp->nex_ref != 0)
		return (DDI_FAILURE);

	mutex_enter(&iommulib_nexus_lock);

	dip = nexp->nex_dip;
	driver = ddi_driver_name(dip);
	instance = ddi_get_instance(dip);

	/* A future enhancement would be to add ref-counts */

	if (nexp->nex_prev == NULL) {
		iommulib_nexus_list = nexp->nex_next;
	} else {
		nexp->nex_prev->nex_next = nexp->nex_next;
	}

	if (nexp->nex_next != NULL)
		nexp->nex_next->nex_prev = nexp->nex_prev;

	mutex_exit(&iommulib_nexus_lock);

	kmem_free(nexp, sizeof (iommulib_nex_t));

	cmn_err(CE_NOTE, "!%s: %s%d: NEXUS (%s) handle successfully "
	    "unregistered from IOMMULIB", f, driver, instance,
	    ddi_node_name(dip));

	ndi_rele_devi(dip);

	return (DDI_SUCCESS);
}

int
iommulib_iommu_register(dev_info_t *dip, iommulib_ops_t *ops,
    iommulib_handle_t *handle)
{
	const char *vendor;
	iommulib_unit_t *unitp;
	int instance = ddi_get_instance(dip);
	const char *driver = ddi_driver_name(dip);
	const char *f = "iommulib_register";

	ASSERT(ops);
	ASSERT(handle);

	if (ops->ilops_vers != IOMMU_OPS_VERSION) {
		cmn_err(CE_WARN, "%s: %s%d: Invalid IOMMULIB ops version "
		    "in ops vector (%p). Failing registration", f, driver,
		    instance, (void *)ops);
		return (DDI_FAILURE);
	}

	switch (ops->ilops_vendor) {
	case AMD_IOMMU:
		vendor = "AMD";
		break;
	case INTEL_IOMMU:
		vendor = "Intel";
		break;
	case INVALID_VENDOR:
		cmn_err(CE_WARN, "%s: %s%d: vendor field (%x) not initialized. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, ops->ilops_vendor, (void *)ops);
		return (DDI_FAILURE);
	default:
		cmn_err(CE_WARN, "%s: %s%d: Invalid vendor field (%x). "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, ops->ilops_vendor, (void *)ops);
		return (DDI_FAILURE);
	}

	cmn_err(CE_NOTE, "!%s: %s%d: Detected IOMMU registration from vendor"
	    " %s", f, driver, instance, vendor);

	if (ops->ilops_data == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL IOMMU data field. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	if (ops->ilops_id == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL ID field. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	if (ops->ilops_probe == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL probe op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	if (ops->ilops_dma_allochdl == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL dma_allochdl op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	if (ops->ilops_dma_freehdl == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL dma_freehdl op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	if (ops->ilops_dma_bindhdl == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL dma_bindhdl op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	if (ops->ilops_dma_sync == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL dma_sync op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	if (ops->ilops_dma_win == NULL) {
		cmn_err(CE_WARN, "%s: %s%d: NULL dma_win op. "
		    "Failing registration for ops vector: %p", f,
		    driver, instance, (void *)ops);
		return (DDI_FAILURE);
	}

	unitp = kmem_zalloc(sizeof (iommulib_unit_t), KM_SLEEP);
	mutex_enter(&iommulib_lock);
	if (iommulib_fini == 1) {
		mutex_exit(&iommulib_lock);
		cmn_err(CE_WARN, "%s: IOMMULIB unloading. Failing register.",
		    f);
		kmem_free(unitp, sizeof (iommulib_unit_t));
		return (DDI_FAILURE);
	}

	/*
	 * fini/register race conditions have been handled. Now create the
	 * IOMMU unit
	 */
	mutex_init(&unitp->ilu_lock, NULL, MUTEX_DEFAULT, NULL);

	mutex_enter(&unitp->ilu_lock);
	unitp->ilu_unitid = ++iommulib_unit_ids;
	unitp->ilu_ref = 0;
	ndi_hold_devi(dip);
	unitp->ilu_dip = dip;
	unitp->ilu_ops = ops;
	unitp->ilu_data = ops->ilops_data;

	unitp->ilu_next = iommulib_list;
	iommulib_list = unitp;
	unitp->ilu_prev = NULL;
	if (unitp->ilu_next)
		unitp->ilu_next->ilu_prev = unitp;

	/*
	 * The IOMMU device itself is not controlled by an IOMMU.
	 */
	DEVI(dip)->devi_iommulib_handle = IOMMU_HANDLE_UNUSED;

	mutex_exit(&unitp->ilu_lock);

	iommulib_num_units++;

	*handle = unitp;

	mutex_exit(&iommulib_lock);

	cmn_err(CE_NOTE, "!%s: %s%d: Succesfully registered IOMMU unit "
	    "from vendor=%s, ops=%p, data=%p, IOMMULIB unitid=%u",
	    f, driver, instance, vendor, (void *)ops, (void *)unitp->ilu_data,
	    unitp->ilu_unitid);

	return (DDI_SUCCESS);
}

int
iommulib_iommu_unregister(iommulib_handle_t handle)
{
	uint32_t unitid;
	dev_info_t *dip;
	int instance;
	const char *driver;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;
	const char *f = "iommulib_unregister";

	ASSERT(unitp);

	mutex_enter(&iommulib_lock);
	mutex_enter(&unitp->ilu_lock);

	unitid = unitp->ilu_unitid;
	dip = unitp->ilu_dip;
	driver = ddi_driver_name(dip);
	instance = ddi_get_instance(dip);

	if (unitp->ilu_ref != 0) {
		mutex_exit(&unitp->ilu_lock);
		mutex_exit(&iommulib_lock);
		cmn_err(CE_WARN, "%s: %s%d: IOMMULIB handle is busy. Cannot "
		    "unregister IOMMULIB unitid %u",
		    f, driver, instance, unitid);
		return (DDI_FAILURE);
	}
	unitp->ilu_unitid = 0;
	ASSERT(unitp->ilu_ref == 0);

	if (unitp->ilu_prev == NULL) {
		iommulib_list = unitp->ilu_next;
		unitp->ilu_next->ilu_prev = NULL;
	} else {
		unitp->ilu_prev->ilu_next = unitp->ilu_next;
		unitp->ilu_next->ilu_prev = unitp->ilu_prev;
	}

	iommulib_num_units--;

	mutex_exit(&unitp->ilu_lock);

	mutex_destroy(&unitp->ilu_lock);
	kmem_free(unitp, sizeof (iommulib_unit_t));

	mutex_exit(&iommulib_lock);

	cmn_err(CE_WARN, "%s: %s%d: IOMMULIB handle (unitid=%u) successfully "
	    "unregistered", f, driver, instance, unitid);

	ndi_rele_devi(dip);

	return (DDI_SUCCESS);
}

int
iommulib_nex_open(dev_info_t *dip, dev_info_t *rdip)
{
	iommulib_unit_t *unitp;
	int instance = ddi_get_instance(rdip);
	const char *driver = ddi_driver_name(rdip);
	const char *f = "iommulib_nex_open";

	ASSERT(DEVI(dip)->devi_iommulib_nex_handle != NULL);
	ASSERT(DEVI(rdip)->devi_iommulib_handle == NULL);

	/* prevent use of IOMMU for AMD IOMMU's DMA */
	if (strcmp(driver, "amd_iommu") == 0) {
		DEVI(rdip)->devi_iommulib_handle = IOMMU_HANDLE_UNUSED;
		return (DDI_ENOTSUP);
	}

	/*
	 * Use the probe entry point to determine in a hardware specific
	 * manner whether this dip is controlled by an IOMMU. If yes,
	 * return the handle corresponding to the IOMMU unit.
	 */

	mutex_enter(&iommulib_lock);
	for (unitp = iommulib_list; unitp; unitp = unitp->ilu_next) {
		if (unitp->ilu_ops->ilops_probe(unitp, rdip) == DDI_SUCCESS)
			break;
	}

	if (unitp == NULL) {
		mutex_exit(&iommulib_lock);
		if (iommulib_debug) {
			char *buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
			cmn_err(CE_WARN, "%s: %s%d: devinfo node (%p): is not "
			    "controlled by an IOMMU: path=%s", f, driver,
			    instance, (void *)rdip, ddi_pathname(rdip, buf));
			kmem_free(buf, MAXPATHLEN);
		}
		DEVI(rdip)->devi_iommulib_handle = IOMMU_HANDLE_UNUSED;
		return (DDI_ENOTSUP);
	}

	mutex_enter(&unitp->ilu_lock);
	unitp->ilu_nex = DEVI(dip)->devi_iommulib_nex_handle;
	unitp->ilu_ref++;
	DEVI(rdip)->devi_iommulib_handle = unitp;
	mutex_exit(&unitp->ilu_lock);
	mutex_exit(&iommulib_lock);

	atomic_inc_uint(&DEVI(dip)->devi_iommulib_nex_handle->nex_ref);

	return (DDI_SUCCESS);
}

void
iommulib_nex_close(dev_info_t *rdip)
{
	iommulib_unit_t *unitp;
	const char *driver;
	int instance;
	uint32_t unitid;
	iommulib_nex_t *nexp;
	const char *f = "iommulib_nex_close";

	ASSERT(IOMMU_USED(rdip));

	unitp = DEVI(rdip)->devi_iommulib_handle;

	mutex_enter(&iommulib_lock);
	mutex_enter(&unitp->ilu_lock);

	nexp = (iommulib_nex_t *)unitp->ilu_nex;
	DEVI(rdip)->devi_iommulib_handle = NULL;

	unitid = unitp->ilu_unitid;
	driver = ddi_driver_name(unitp->ilu_dip);
	instance = ddi_get_instance(unitp->ilu_dip);

	unitp->ilu_ref--;
	mutex_exit(&unitp->ilu_lock);
	mutex_exit(&iommulib_lock);

	atomic_dec_uint(&nexp->nex_ref);

	if (iommulib_debug) {
		char *buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
		(void) ddi_pathname(rdip, buf);
		cmn_err(CE_NOTE, "%s: %s%d: closing IOMMU for dip (%p), "
		    "unitid=%u rdip path = %s", f, driver, instance,
		    (void *)rdip, unitid, buf);
		kmem_free(buf, MAXPATHLEN);
	}
}

int
iommulib_nexdma_allochdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_attr_t *attr, int (*waitfp)(caddr_t),
    caddr_t arg, ddi_dma_handle_t *dma_handlep)
{
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	/* No need to grab lock - the handle is reference counted */
	return (unitp->ilu_ops->ilops_dma_allochdl(handle, dip, rdip,
	    attr, waitfp, arg, dma_handlep));
}

int
iommulib_nexdma_freehdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t dma_handle)
{
	int error;
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	/* No need to grab lock - the handle is reference counted */
	error = unitp->ilu_ops->ilops_dma_freehdl(handle, dip,
	    rdip, dma_handle);

	return (error);
}

int
iommulib_nexdma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t dma_handle, struct ddi_dma_req *dmareq,
    ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	/* No need to grab lock - the handle is reference counted */
	return (unitp->ilu_ops->ilops_dma_bindhdl(handle, dip, rdip, dma_handle,
	    dmareq, cookiep, ccountp));
}

int
iommulib_nexdma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t dma_handle)
{
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	/* No need to grab lock - the handle is reference counted */
	return (unitp->ilu_ops->ilops_dma_unbindhdl(handle, dip, rdip,
	    dma_handle));
}

int
iommulib_nexdma_sync(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t dma_handle, off_t off, size_t len,
    uint_t cache_flags)
{
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	/* No need to grab lock - the handle is reference counted */
	return (unitp->ilu_ops->ilops_dma_sync(handle, dip, rdip, dma_handle,
	    off, len, cache_flags));
}

int
iommulib_nexdma_win(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t dma_handle, uint_t win, off_t *offp, size_t *lenp,
    ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	/* No need to grab lock - the handle is reference counted */
	return (unitp->ilu_ops->ilops_dma_win(handle, dip, rdip, dma_handle,
	    win, offp, lenp, cookiep, ccountp));
}

int
iommulib_nexdma_mapobject(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t dma_handle, struct ddi_dma_req *dmareq,
    ddi_dma_obj_t *dmao)
{
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	return (unitp->ilu_ops->ilops_dma_mapobject(handle, dip, rdip,
	    dma_handle, dmareq, dmao));
}

int
iommulib_nexdma_unmapobject(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t dma_handle, ddi_dma_obj_t *dmao)
{
	iommulib_handle_t handle = DEVI(rdip)->devi_iommulib_handle;
	iommulib_unit_t *unitp = (iommulib_unit_t *)handle;

	return (unitp->ilu_ops->ilops_dma_unmapobject(handle, dip, rdip,
	    dma_handle, dmao));
}

/* Utility routines invoked by IOMMU drivers */
int
iommulib_iommu_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
    ddi_dma_handle_t *handlep)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_allochdl(dip, rdip, attr, waitfp, arg,
	    handlep));
}

int
iommulib_iommu_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	ASSERT(nexops);
	return (nexops->nops_dma_freehdl(dip, rdip, handle));
}

int
iommulib_iommu_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
    ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_bindhdl(dip, rdip, handle, dmareq,
	    cookiep, ccountp));
}

int
iommulib_iommu_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_unbindhdl(dip, rdip, handle));
}

void
iommulib_iommu_dma_reset_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	nexops->nops_dma_reset_cookies(dip, handle);
}

int
iommulib_iommu_dma_get_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
    ddi_dma_cookie_t **cookiepp, uint_t *ccountp)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_get_cookies(dip, handle, cookiepp, ccountp));
}

int
iommulib_iommu_dma_set_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
    ddi_dma_cookie_t *cookiep, uint_t ccount)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_set_cookies(dip, handle, cookiep, ccount));
}

int
iommulib_iommu_dma_clear_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_clear_cookies(dip, handle));
}

int
iommulib_iommu_dma_get_sleep_flags(dev_info_t *dip, ddi_dma_handle_t handle)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_get_sleep_flags(handle));
}

int
iommulib_iommu_dma_sync(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, off_t off, size_t len, uint_t cache_flags)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_sync(dip, rdip, handle, off, len,
	    cache_flags));
}

int
iommulib_iommu_dma_win(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, uint_t win, off_t *offp, size_t *lenp,
    ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dma_win(dip, rdip, handle, win, offp, lenp,
	    cookiep, ccountp));
}

int
iommulib_iommu_dmahdl_setprivate(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, void *priv)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dmahdl_setprivate(dip, rdip, handle, priv));
}

void *
iommulib_iommu_dmahdl_getprivate(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	iommulib_nexops_t *nexops;

	nexops = &DEVI(dip)->devi_iommulib_nex_handle->nex_ops;
	return (nexops->nops_dmahdl_getprivate(dip, rdip, handle));
}

int
iommulib_iommu_getunitid(iommulib_handle_t handle, uint64_t *unitidp)
{
	iommulib_unit_t *unitp;
	uint64_t unitid;

	unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);
	ASSERT(unitidp);

	mutex_enter(&unitp->ilu_lock);
	unitid = unitp->ilu_unitid;
	mutex_exit(&unitp->ilu_lock);

	ASSERT(unitid > 0);
	*unitidp = (uint64_t)unitid;

	return (DDI_SUCCESS);
}

dev_info_t *
iommulib_iommu_getdip(iommulib_handle_t handle)
{
	iommulib_unit_t *unitp;
	dev_info_t *dip;

	unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	mutex_enter(&unitp->ilu_lock);
	dip = unitp->ilu_dip;
	ASSERT(dip);
	ndi_hold_devi(dip);
	mutex_exit(&unitp->ilu_lock);

	return (dip);
}

iommulib_ops_t *
iommulib_iommu_getops(iommulib_handle_t handle)
{
	iommulib_unit_t *unitp;
	iommulib_ops_t *ops;

	unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	mutex_enter(&unitp->ilu_lock);
	ops = unitp->ilu_ops;
	mutex_exit(&unitp->ilu_lock);

	ASSERT(ops);

	return (ops);
}

void *
iommulib_iommu_getdata(iommulib_handle_t handle)
{
	iommulib_unit_t *unitp;
	void *data;

	unitp = (iommulib_unit_t *)handle;

	ASSERT(unitp);

	mutex_enter(&unitp->ilu_lock);
	data = unitp->ilu_data;
	mutex_exit(&unitp->ilu_lock);

	ASSERT(data);

	return (data);
}