summaryrefslogtreecommitdiffstats
path: root/sys/sparc64/pci/psycho.c
blob: d07af28dfe731f619570d10979b49adecd45d76d (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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
/*
 * Copyright (c) 1999, 2000 Matthew R. Green
 * All rights reserved.
 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: NetBSD: psycho.c,v 1.35 2001/09/10 16:17:06 eeh Exp
 *
 * $FreeBSD$
 */

/*
 * Support for `psycho' and `psycho+' UPA to PCI bridge and 
 * UltraSPARC IIi and IIe `sabre' PCI controllers.
 */

#include "opt_psycho.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/malloc.h>

#include <ofw/openfirm.h>
#include <ofw/ofw_pci.h>

#include <machine/bus.h>
#include <machine/cache.h>
#include <machine/iommureg.h>
#include <machine/bus_common.h>
#include <machine/frame.h>
#include <machine/intr_machdep.h>
#include <machine/nexusvar.h>
#include <machine/ofw_upa.h>
#include <machine/resource.h>

#include <sys/rman.h>

#include <machine/iommuvar.h>

#include <pci/pcivar.h>
#include <pci/pcireg.h>

#include <sparc64/pci/ofw_pci.h>
#include <sparc64/pci/psychoreg.h>
#include <sparc64/pci/psychovar.h>

#include "pcib_if.h"
#include "sparcbus_if.h"

static void psycho_get_ranges(phandle_t, struct upa_ranges **, int *);
static void psycho_set_intr(struct psycho_softc *, int, device_t, u_long *,
    int, driver_intr_t);
static int psycho_find_intrmap(struct psycho_softc *, int, u_long **,
    u_long **, u_long *);
static void psycho_intr_stub(void *);
#ifdef PSYCHO_STRAY
static void psycho_intr_stray(void *);
#endif
static bus_space_tag_t psycho_alloc_bus_tag(struct psycho_softc *, int);


/* Interrupt handlers */
static void psycho_ue(void *);
static void psycho_ce(void *);
static void psycho_bus_a(void *);
static void psycho_bus_b(void *);
static void psycho_powerfail(void *);
#ifdef PSYCHO_MAP_WAKEUP
static void psycho_wakeup(void *);
#endif

/* IOMMU support */
static void psycho_iommu_init(struct psycho_softc *, int);

/*
 * bus space and bus dma support for UltraSPARC `psycho'.  note that most
 * of the bus dma support is provided by the iommu dvma controller.
 */
static int psycho_dmamem_alloc(bus_dma_tag_t, void **, int, bus_dmamap_t *);
static void psycho_dmamem_free(bus_dma_tag_t, void *, bus_dmamap_t);
static int psycho_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
    bus_dmamap_callback_t *, void *, int);
static void psycho_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
static void psycho_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);

/*
 * autoconfiguration
 */
static int psycho_probe(device_t);
static int psycho_attach(device_t);
static int psycho_read_ivar(device_t, device_t, int, u_long *);
static int psycho_setup_intr(device_t, device_t, struct resource *, int,
    driver_intr_t *, void *, void **);
static int psycho_teardown_intr(device_t, device_t, struct resource *, void *);
static struct resource *psycho_alloc_resource(device_t, device_t, int, int *,
    u_long, u_long, u_long, u_int);
static int psycho_activate_resource(device_t, device_t, int, int,
    struct resource *);
static int psycho_deactivate_resource(device_t, device_t, int, int,
    struct resource *);
static int psycho_release_resource(device_t, device_t, int, int,
    struct resource *);
static int psycho_maxslots(device_t);
static u_int32_t psycho_read_config(device_t, u_int, u_int, u_int, u_int, int);
static void psycho_write_config(device_t, u_int, u_int, u_int, u_int, u_int32_t,
    int);
static int psycho_route_interrupt(device_t, device_t, int);
static int psycho_intr_pending(device_t, int);
static bus_space_handle_t psycho_get_bus_handle(device_t dev, enum sbbt_id id,
    bus_space_handle_t childhdl, bus_space_tag_t *tag);

static device_method_t psycho_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		psycho_probe),
	DEVMETHOD(device_attach,	psycho_attach),

	/* Bus interface */
	DEVMETHOD(bus_print_child,	bus_generic_print_child),
	DEVMETHOD(bus_read_ivar,	psycho_read_ivar),
	DEVMETHOD(bus_setup_intr, 	psycho_setup_intr),
	DEVMETHOD(bus_teardown_intr,	psycho_teardown_intr),
	DEVMETHOD(bus_alloc_resource,	psycho_alloc_resource),
	DEVMETHOD(bus_activate_resource,	psycho_activate_resource),
	DEVMETHOD(bus_deactivate_resource,	psycho_deactivate_resource),
	DEVMETHOD(bus_release_resource,	psycho_release_resource),

	/* pcib interface */
	DEVMETHOD(pcib_maxslots,	psycho_maxslots),
	DEVMETHOD(pcib_read_config,	psycho_read_config),
	DEVMETHOD(pcib_write_config,	psycho_write_config),
	DEVMETHOD(pcib_route_interrupt,	psycho_route_interrupt),

	/* sparcbus interface */
	DEVMETHOD(sparcbus_intr_pending,	psycho_intr_pending),
	DEVMETHOD(sparcbus_get_bus_handle,	psycho_get_bus_handle),

	{ 0, 0 }
};

static driver_t psycho_driver = {
	"pcib",
	psycho_methods,
	sizeof(struct psycho_softc),
};

static devclass_t psycho_devclass;

DRIVER_MODULE(psycho, nexus, psycho_driver, psycho_devclass, 0, 0);

static int psycho_ndevs;
static struct psycho_softc *psycho_softcs[4];

struct psycho_clr {
	u_long	*pci_clr;		/* clear register */
	driver_intr_t	*pci_handler;	/* handler to call */
	void	*pci_arg;		/* argument for the handler */
	void	*pci_cookie;		/* interrupt cookie of parent bus */
};

/*
 * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
 * single PCI bus and does not have a streaming buffer.  It often has an APB
 * (advanced PCI bridge) connected to it, which was designed specifically for
 * the IIi.  The APB let's the IIi handle two independednt PCI buses, and
 * appears as two "simba"'s underneath the sabre.
 *
 * "psycho" and "psycho+" is a dual UPA to PCI bridge.  It sits on the UPA bus
 * and manages two PCI buses.  "psycho" has two 64-bit 33MHz buses, while
 * "psycho+" controls both a 64-bit 33Mhz and a 64-bit 66Mhz PCI bus.  You
 * will usually find a "psycho+" since I don't think the original "psycho"
 * ever shipped, and if it did it would be in the U30.  
 *
 * Each "psycho" PCI bus appears as a separate OFW node, but since they are
 * both part of the same IC, they only have a single register space.  As such,
 * they need to be configured together, even though the autoconfiguration will
 * attach them separately.
 *
 * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
 * as pci1 and pci2, although they have been implemented with other PCI bus
 * numbers on some machines.
 *
 * On UltraII machines, there can be any number of "psycho+" ICs, each
 * providing two PCI buses.  
 *
 *
 * XXXX The psycho/sabre node has an `interrupts' attribute.  They contain
 * the values of the following interrupts in this order:
 *
 * PCI Bus Error	(30)
 * DMA UE		(2e)
 * DMA CE		(2f)
 * Power Fail		(25)
 *
 * We really should attach handlers for each.
 */
#define	OFW_PCI_TYPE		"pci"
#define OFW_SABRE_MODEL		"SUNW,sabre"
#define OFW_SABRE_COMPAT	"pci108e,a001"
#define OFW_SIMBA_MODEL		"SUNW,simba"
#define OFW_PSYCHO_MODEL	"SUNW,psycho"

static int
psycho_probe(device_t dev)
{
	phandle_t node;
	char *dtype, *model;
	static char compat[32];

	node = nexus_get_node(dev);
	if (OF_getprop(node, "compatible", compat, sizeof(compat)) == -1)
		compat[0] = '\0';

	dtype = nexus_get_device_type(dev);
	model = nexus_get_model(dev);
	/* match on a type of "pci" and a sabre or a psycho */
	if (nexus_get_reg(dev) != NULL && dtype != NULL &&
	    strcmp(dtype, OFW_PCI_TYPE) == 0 &&
	    ((model != NULL && (strcmp(model, OFW_SABRE_MODEL) == 0 ||
	      strcmp(model, OFW_PSYCHO_MODEL) == 0)) ||
	      strcmp(compat, OFW_SABRE_COMPAT) == 0)) {
		device_set_desc(dev, "U2P UPA-PCI bridge");
		return (0);
	}

	return (ENXIO);
}

/*
 * SUNW,psycho initialisation ..
 *	- find the per-psycho registers
 *	- figure out the IGN.
 *	- find our partner psycho
 *	- configure ourselves
 *	- bus range, bus, 
 *	- interrupt map,
 *	- setup the chipsets.
 *	- if we're the first of the pair, initialise the IOMMU, otherwise
 *	  just copy it's tags and addresses.
 */
static int
psycho_attach(device_t dev)
{
	struct psycho_softc *sc;
	struct psycho_softc *osc = NULL;
	struct psycho_softc *asc;
	struct ofw_nexus_reg *reg;
	char compat[32];
	char *model;
	phandle_t node;
	u_int64_t csr;
	u_long pci_ctl;
	int psycho_br[2];
	int n, i, nreg;
#if defined(PSYCHO_DEBUG) || defined(PSYCHO_STRAY)
	u_long *map, *clr;
#endif

	bootverbose = 1;
	node = nexus_get_node(dev);
	sc = device_get_softc(dev);
	if (OF_getprop(node, "compatible", compat, sizeof(compat)) == -1)
		compat[0] = '\0';

	sc->sc_node = node;
	sc->sc_dev = dev;
	sc->sc_bustag = nexus_get_bustag(dev);
	sc->sc_dmatag = nexus_get_dmatag(dev);

	/*
	 * call the model-specific initialisation routine.
	 */
	model = nexus_get_model(dev);
	if ((model != NULL &&
	     strcmp(model, OFW_SABRE_MODEL) == 0) ||
	    strcmp(compat, OFW_SABRE_COMPAT) == 0) {
		sc->sc_mode = PSYCHO_MODE_SABRE;
		if (model == NULL)
			model = "sabre";
	} else if (model != NULL &&
	    strcmp(model, OFW_PSYCHO_MODEL) == 0)
		sc->sc_mode = PSYCHO_MODE_PSYCHO;
	else
		panic("psycho_attach: unknown model!");

	/*
	 * The psycho gets three register banks:
	 * (0) per-PBM configuration and status registers
	 * (1) per-PBM PCI configuration space, containing only the
	 *     PBM 256-byte PCI header
	 * (2) the shared psycho configuration registers (struct psychoreg)
	 *
	 * XXX use the prom address for the psycho registers?  we do so far.
	 */
	reg = nexus_get_reg(dev);
	nreg = nexus_get_nreg(dev);
	/* Register layouts are different.  stuupid. */
	if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
		sc->sc_basepaddr = (vm_offset_t)reg[2].or_paddr;

		if (nreg <= 2) {
			panic("psycho_attach: %d not enough registers", nreg);
		}
		if (sparc64_bus_mem_map(UPA_BUS_SPACE, reg[2].or_paddr,
		    reg[2].or_len, 0, NULL, (void **)&sc->sc_regs))
			panic("psycho_attach: cannot map regs");
		pci_ctl = reg[0].or_paddr;
	} else {
		sc->sc_basepaddr = (vm_offset_t)reg[0].or_paddr;

		if (nreg <= 0) {
			panic("psycho_attach: %d not enough registers", nreg);
		}
		if (sparc64_bus_mem_map(UPA_BUS_SPACE, reg[0].or_paddr,
		    reg[0].or_len, 0, NULL, (void **)&sc->sc_regs))
			panic("psycho_attach: cannot map regs");
		pci_ctl = reg[0].or_paddr +
		    offsetof(struct psychoreg, psy_pcictl[0]);
	}

	csr = sc->sc_regs->psy_csr;
	sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
		sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;

	device_printf(dev, "%s: impl %d, version %d: ign %x ",
		model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
		sc->sc_ign);

	/*
	 * Match other psycho's that are already configured against
	 * the base physical address. This will be the same for a
	 * pair of devices that share register space.
	 */
	for (n = 0; n < psycho_ndevs && n < sizeof(psycho_softcs) /
	     sizeof(psycho_softcs[0]); n++) {
		asc = (struct psycho_softc *)psycho_softcs[n];

		if (asc == NULL || asc == sc)
			/* This entry is not there or it is me */
			continue;

		if (asc->sc_basepaddr != sc->sc_basepaddr)
			/* This is an unrelated psycho */
			continue;

		/* Found partner */
		osc = asc;
		break;
	}

	/*
	 * Setup the PCI control register
	 */
	csr = bus_space_read_8(sc->sc_bustag,
	    (bus_space_handle_t)pci_ctl, offsetof(struct pci_ctl, pci_csr));
	csr |= PCICTL_MRLM |
	    PCICTL_ARB_PARK |
	    PCICTL_ERRINTEN |
	    PCICTL_4ENABLE;
	csr &= ~(PCICTL_SERR |
	    PCICTL_CPU_PRIO |
	    PCICTL_ARB_PRIO |
	    PCICTL_RTRYWAIT);
	bus_space_write_8(sc->sc_bustag,
	    (bus_space_handle_t)pci_ctl, offsetof(struct pci_ctl, pci_csr),
	    csr);

	/* grab the psycho ranges */
	psycho_get_ranges(sc->sc_node, &sc->sc_range, &sc->sc_nrange);

	/* get the bus-range for the psycho */
	n = OF_getprop(node, "bus-range", (void *)psycho_br, sizeof(psycho_br));
	if (n == -1)
		panic("could not get psycho bus-range");
	if (n != sizeof(psycho_br))
		panic("broken psycho bus-range (%d)", n);

	printf("bus range %u to %u; PCI bus %d\n", psycho_br[0], psycho_br[1],
	    psycho_br[0]);

	sc->sc_busno = psycho_br[0];

	/* Initialize memory and i/o rmans */
	sc->sc_io_rman.rm_type = RMAN_ARRAY;
	sc->sc_io_rman.rm_descr = "Psycho PCI I/O Ports";
	if (rman_init(&sc->sc_io_rman) != 0 ||
	    rman_manage_region(&sc->sc_io_rman, 0, PSYCHO_IO_SIZE) != 0)
		panic("psycho_probe: failed to set up i/o rman");
	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
	sc->sc_mem_rman.rm_descr = "Psycho PCI Memory";
	if (rman_init(&sc->sc_mem_rman) != 0 ||
	    rman_manage_region(&sc->sc_mem_rman, 0, PSYCHO_MEM_SIZE) != 0)
		panic("psycho_probe: failed to set up memory rman");
	/*
	 * Find the addresses of the various bus spaces.
	 * There should not be multiple ones of one kind.
	 * The physical start addresses of the ranges are the configuration,
	 * memory and IO handles.
	 */
	for (n = 0; n < sc->sc_nrange; n++) {
		i = UPA_RANGE_CS(&sc->sc_range[n]);
		if (sc->sc_bh[i] != 0)
			panic("psycho_attach: duplicate range for space %d", i);
		sc->sc_bh[i] = UPA_RANGE_PHYS(&sc->sc_range[n]);
	}
	/*
	 * Check that all needed handles are present. The PCI_CS_MEM64 one is
	 * not currently used.
	 */
	for (n = 0; n < 3; n++) {
		if (sc->sc_bh[n] == 0)
			panic("psycho_attach: range %d missing", n);
	}

	/* allocate our tags */
	sc->sc_memt = psycho_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
	sc->sc_iot = psycho_alloc_bus_tag(sc, PCI_IO_BUS_SPACE);
	sc->sc_cfgt = psycho_alloc_bus_tag(sc, PCI_CONFIG_BUS_SPACE);
	if (bus_dma_tag_create(sc->sc_dmatag, 8, 1, 0, 0x3ffffffff, NULL, NULL,
	    0x3ffffffff, 0xff, 0xffffffff, 0, &sc->sc_dmat) != 0)
		panic("bus_dma_tag_create failed");
	/* Customize the tag */
	sc->sc_dmat->cookie = sc;
	sc->sc_dmat->dmamem_alloc = psycho_dmamem_alloc;
	sc->sc_dmat->dmamem_free = psycho_dmamem_free;
	sc->sc_dmat->dmamap_load = psycho_dmamap_load;
	sc->sc_dmat->dmamap_unload = psycho_dmamap_unload;
	sc->sc_dmat->dmamap_sync = psycho_dmamap_sync;
	/* XXX: register as root dma tag (kluge). */
	sparc64_root_dma_tag = sc->sc_dmat;

	if ((sc->sc_nintrmap = OF_getprop_alloc(sc->sc_node, "interrupt-map",
	    sizeof(*sc->sc_intrmap), (void **)&sc->sc_intrmap)) == -1 ||
	    OF_getprop(sc->sc_node, "interrupt-map-mask", &sc->sc_intrmapmsk,
		sizeof(sc->sc_intrmapmsk)) == -1) {
		if (sc->sc_intrmap != NULL) {
			free(sc->sc_intrmap, M_OFWPROP);
			sc->sc_intrmap = NULL;
		}
	}

	/* Register the softc, this is needed for paired psychos. */
	if (psycho_ndevs < sizeof(psycho_softcs) / sizeof(psycho_softcs[0]))
		psycho_softcs[psycho_ndevs] = sc;
	else
		device_printf(dev, "XXX: bump the number of psycho_softcs");
	psycho_ndevs++;
	/*
	 * And finally, if we're a sabre or the first of a pair of psycho's to
	 * arrive here, start up the IOMMU and get a config space tag.
	 */
	if (osc == NULL) {
		/*
		 * Establish handlers for interesting interrupts....
		 *
		 * XXX We need to remember these and remove this to support
		 * hotplug on the UPA/FHC bus.
		 *
		 * XXX Not all controllers have these, but installing them
		 * is better than trying to sort through this mess.
		 */
		psycho_set_intr(sc, 0, dev, &sc->sc_regs->ue_int_map,
		    INTR_TYPE_MISC | INTR_FAST, psycho_ue);
		psycho_set_intr(sc, 1, dev, &sc->sc_regs->ce_int_map,
		    INTR_TYPE_MISC, psycho_ce);
		psycho_set_intr(sc, 2, dev,
		    &sc->sc_regs->pciaerr_int_map, INTR_TYPE_MISC | INTR_FAST,
		    psycho_bus_a);
		psycho_set_intr(sc, 3, dev,
		    &sc->sc_regs->pciberr_int_map, INTR_TYPE_MISC | INTR_FAST,
		    psycho_bus_b);
		psycho_set_intr(sc, 4, dev, &sc->sc_regs->power_int_map,
		    INTR_TYPE_MISC | INTR_FAST, psycho_powerfail);
#ifdef PSYCHO_MAP_WAKEUP
		/*
		 * On some models, this is mapped to the same interrupt as
		 * pciberr by default, so leave it alone for now since
		 * psycho_wakeup() doesn't do anything useful anyway.
		 */
		psycho_set_intr(sc, 5, dev, &sc->sc_regs->pwrmgt_int_map,
		    INTR_TYPE_MISC, psycho_wakeup);
#endif /* PSYCHO_MAP_WAKEUP */

		/*
		 * Setup IOMMU and PCI configuration if we're the first
		 * of a pair of psycho's to arrive here.
		 *
		 * We should calculate a TSB size based on amount of RAM
		 * and number of bus controllers and number an type of
		 * child devices.
		 *
		 * For the moment, 32KB should be more than enough.
		 */
		psycho_iommu_init(sc, 2);
	} else {
		/* Just copy IOMMU state, config tag and address */
		sc->sc_is = osc->sc_is;
	}

	/*
	 * Enable all interrupts, clear all interrupt states, and install an
	 * interrupt handler for OBIO interrupts, which can be ISA ones
	 * (to frob the interrupt clear registers).
	 * This aids the debugging of interrupt routing problems, and is needed
	 * for isa drivers that use isa_irq_pending (otherwise the registers
	 * will never be cleared).
	 */
#if defined(PSYCHO_DEBUG) || defined(PSYCHO_STRAY)
	for (map = &sc->sc_regs->pcia0_int_map,
	     clr = &sc->sc_regs->pcia0_int_clr[0], n = 0;
	     map <= &sc->sc_regs->pcib3_int_map; map++, clr += 4, n++) {
#ifdef PSYCHO_DEBUG
		device_printf(dev, "intr map (pci) %d: %lx\n", n, *map);
#endif
		*map &= ~INTMAP_V;
		membar(StoreStore);
		for (i = 0; i < 4; i++)
			clr[i] = 0;
		membar(StoreStore);
		*map |= INTMAP_V;
	}
	for (map = &sc->sc_regs->scsi_int_map, n = 0,
	     clr = &sc->sc_regs->scsi_int_clr, n = 0;
	     map <= &sc->sc_regs->ffb1_int_map; map++, clr++, n++) {
#ifdef PSYCHO_DEBUG
		device_printf(dev, "intr map (obio) %d: %lx, clr: %p\n", n,
		    *map, clr);
#endif
		*map &= ~INTMAP_V;
		membar(StoreStore);
		*clr = 0;
#ifdef PSYCHO_STRAY
		/*
		 * This can cause interrupt storms, and is therefore disabled
		 * by default.
		 * XXX: use intr_setup() to not confuse higher level code
		 */
		if (INTVEC(*map) != 0x7e6 && INTVEC(*map) != 0x7e7 &&
		    INTVEC(*map) != 0)
		intr_setup(PIL_LOW, intr_dequeue, INTVEC(*map), psycho_intr_stray,
		    (void *)clr);
#endif
		membar(StoreStore);
		*map |= INTMAP_V;
	}
#endif

	/*
	 * Initialize the interrupt registers of all devices hanging from
	 * the host bridge directly or indirectly via PCI-PCI bridges.
	 * The MI code (and the PCI spec) assume that this is done during
	 * system initialization, however the firmware does not do this
	 * at least on some models, and we probably shouldn't trust that
	 * the firmware uses the same model as this driver if it does.
	 */
	ofw_pci_init_intr(dev, sc->sc_node, sc->sc_intrmap, sc->sc_nintrmap,
	    &sc->sc_intrmapmsk);
	
	device_add_child(dev, "pci", device_get_unit(dev));
	return (bus_generic_attach(dev));
}

static void
psycho_set_intr(struct psycho_softc *sc, int index,
    device_t dev, u_long *map, int iflags, driver_intr_t handler)
{
	int rid;

	sc->sc_irq[index] = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
	    INTVEC(*map), INTVEC(*map), 1, RF_ACTIVE);
	if (sc->sc_irq[index] == NULL)
		panic("psycho_set_intr: failed to get interupt");
	bus_setup_intr(dev, sc->sc_irq[index], INTR_TYPE_MISC | iflags,
	    handler, sc, &sc->sc_ihand[index]);
	*map |= INTMAP_V;
}

static int
psycho_find_intrmap(struct psycho_softc *sc, int ino, u_long **intrmapptr,
    u_long **intrclrptr, u_long *intrdiagptr)
{
	u_long *intrmap, *intrclr, diag;
	int found;

	found = 0;
	/* Hunt thru obio first */
	diag = sc->sc_regs->obio_int_diag;
	for (intrmap = &sc->sc_regs->scsi_int_map,
		 intrclr = &sc->sc_regs->scsi_int_clr;
	     intrmap <= &sc->sc_regs->ffb1_int_map;
	     intrmap++, intrclr++, diag >>= 2) {
		if (INTINO(*intrmap) == ino) {
			diag &= 2;
			found = 1;
			break;
		}
	}

	if (!found) {
		diag = sc->sc_regs->pci_int_diag;
		/* Now do PCI interrupts */
		for (intrmap = &sc->sc_regs->pcia0_int_map,
			 intrclr = &sc->sc_regs->pcia0_int_clr[0];
		     intrmap <= &sc->sc_regs->pcib3_int_map;
		     intrmap++, intrclr += 4, diag >>= 8) {
			if (((*intrmap ^ ino) & 0x3c) == 0) {
				intrclr += ino & 3;
				diag = (diag >> ((ino & 3) * 2)) & 2;
				found = 1;
				break;
			}
		}
	}
	if (intrmapptr != NULL)
		*intrmapptr = intrmap;
	if (intrclrptr != NULL)
		*intrclrptr = intrclr;
	if (intrdiagptr != NULL)
		*intrdiagptr = diag;
	return (found);
}

/* grovel the OBP for various psycho properties */
static void
psycho_get_ranges(phandle_t node, struct upa_ranges **rp, int *np)
{

	*np = OF_getprop_alloc(node, "ranges", sizeof(**rp), (void **)rp);
	if (*np == -1)
		panic("could not get psycho ranges");
}

/*
 * Interrupt handlers.
 */
static void
psycho_ue(void *arg)
{
	struct psycho_softc *sc = (struct psycho_softc *)arg;
	struct psychoreg *regs = sc->sc_regs;

	sc->sc_regs->ue_int_clr = 0;
	/* It's uncorrectable.  Dump the regs and panic. */
	panic("%s: uncorrectable DMA error AFAR %llx AFSR %llx\n",
		device_get_name(sc->sc_dev), 
		(long long)regs->psy_ue_afar, (long long)regs->psy_ue_afsr);
}

static void
psycho_ce(void *arg)
{
	struct psycho_softc *sc = (struct psycho_softc *)arg;
	struct psychoreg *regs = sc->sc_regs;

	sc->sc_regs->ce_int_clr = 0;
	/* It's correctable.  Dump the regs and continue. */
	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
		device_get_name(sc->sc_dev), 
		(long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
}

static void
psycho_bus_a(void *arg)
{
	struct psycho_softc *sc = (struct psycho_softc *)arg;
	struct psychoreg *regs = sc->sc_regs;

	sc->sc_regs->pciaerr_int_clr = 0;
	/* It's uncorrectable.  Dump the regs and panic. */
	panic("%s: PCI bus A error AFAR %lx AFSR %lx\n",
	    device_get_name(sc->sc_dev), 
	    regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr);
}

static void
psycho_bus_b(void *arg)
{
	struct psycho_softc *sc = (struct psycho_softc *)arg;
	struct psychoreg *regs = sc->sc_regs;

	sc->sc_regs->pciberr_int_clr = 0;
	/* It's uncorrectable.  Dump the regs and panic. */
	panic("%s: PCI bus B error AFAR %lx AFSR %lx\n",
	    device_get_name(sc->sc_dev),
	    regs->psy_pcictl[1].pci_afar, regs->psy_pcictl[1].pci_afsr);
}

static void
psycho_powerfail(void *arg)
{
	struct psycho_softc *sc = (struct psycho_softc *)arg;

	sc->sc_regs->power_int_clr = 0;
	/* We lost power.  Try to shut down NOW. */
	printf("Power Failure Detected: Shutting down NOW.\n");
	shutdown_nice(0);
}

#ifdef PSYCHO_MAP_WAKEUP
static void
psycho_wakeup(void *arg)
{
	struct psycho_softc *sc = (struct psycho_softc *)arg;

	sc->sc_regs->pwrmgt_int_clr = 0;
	/* Gee, we don't really have a framework to deal with this properly. */
	printf("%s: power management wakeup\n",	device_get_name(sc->sc_dev));
}
#endif /* PSYCHO_MAP_WAKEUP */

/* initialise the IOMMU... */
void
psycho_iommu_init(struct psycho_softc *sc, int tsbsize)
{
	char *name;
	struct iommu_state *is;
	u_int32_t iobase = -1;
	int *vdma = NULL;
	int nitem;

	is = malloc(sizeof(struct iommu_state), M_DEVBUF, M_NOWAIT);
	if (is == NULL)
		panic("psycho_iommu_init: malloc failed");

	sc->sc_is = is;

	/* punch in our copies */
	is->is_bustag = sc->sc_bustag;
	is->is_iommu = &sc->sc_regs->psy_iommu;
	is->is_dtag = &sc->sc_regs->tlb_tag_diag[0];
	is->is_ddram = &sc->sc_regs->tlb_data_diag[0];
	is->is_dqueue = &sc->sc_regs->iommu_queue_diag[0];
	is->is_dva = &sc->sc_regs->iommu_svadiag;
	is->is_dtcmp = &sc->sc_regs->iommu_tlb_comp_diag;

	if (OF_getproplen(sc->sc_node, "no-streaming-cache") < 0)
		is->is_sb = 0;
	else
		is->is_sb = &sc->sc_regs->psy_iommu_strbuf;

	/*
	 * Separate the men from the boys.  Get the `virtual-dma'
	 * property for sabre and use that to make sure the damn
	 * iommu works.
	 *
	 * We could query the `#virtual-dma-size-cells' and
	 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
	 */
	nitem = OF_getprop_alloc(sc->sc_node, "virtual-dma", sizeof(vdma),
	    (void **)&vdma);
	if (nitem > 0) {
		iobase = vdma[0];
		tsbsize = ffs(vdma[1]);
		if (tsbsize < 25 || tsbsize > 31 ||
		    (vdma[1] & ~(1 << (tsbsize - 1))) != 0) {
			printf("bogus tsb size %x, using 7\n", vdma[1]);
			tsbsize = 31;
		}
		tsbsize -= 24;
		free(vdma, M_OFWPROP);
	}

	/* give us a nice name.. */
	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
	if (name == 0)
		panic("couldn't malloc iommu name");
	snprintf(name, 32, "%s dvma", device_get_name(sc->sc_dev));

	iommu_init(name, is, tsbsize, iobase);
}

static int
psycho_maxslots(device_t dev)
{

	/*
	 * XXX: is this correct? At any rate, a number that is too high
	 * shouldn't do any harm, if only because of the way things are
	 * handled in psycho_read_config.
	 */
	return (31);
}

static u_int32_t
psycho_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
	int width)
{
	struct psycho_softc *sc;
	bus_space_handle_t bh;
	u_long offset = 0;
	u_int32_t r;

	/*
	 * The psycho bridge does not tolerate accesses to unconfigured PCI
	 * devices' or function's config space, so look up the device in the
	 * first, and if it is not present, return a value that will make the
	 * detection think that there is no device here. This is somehow ugly...
	 */
	if (ofw_pci_find_node(bus, slot, func) == 0)
		return (0xffffffff);
	sc = (struct psycho_softc *)device_get_softc(dev);
	offset = PSYCHO_CONF_OFF(bus, slot, func, reg);
	bh = sc->sc_bh[PCI_CS_CONFIG];
	switch (width) {
	case 1:
		r = bus_space_read_1(sc->sc_cfgt, bh, offset);
		break;
	case 2:
		r = bus_space_read_2(sc->sc_cfgt, bh, offset);
		break;
	case 4:
		r = bus_space_read_4(sc->sc_cfgt, bh, offset);
		break;
	default:
		panic("psycho_read_config: bad width");
	}
	return (r);
}

static void
psycho_write_config(device_t dev, u_int bus, u_int slot, u_int func,
	u_int reg, u_int32_t val, int width)
{
	struct psycho_softc *sc;
	bus_space_handle_t bh;
	u_long offset = 0;

	sc = (struct psycho_softc *)device_get_softc(dev);
	offset = PSYCHO_CONF_OFF(bus, slot, func, reg);
	bh = sc->sc_bh[PCI_CS_CONFIG];
	switch (width) {
	case 1:
		bus_space_write_1(sc->sc_cfgt, bh, offset, val);
		break;
	case 2:
		bus_space_write_2(sc->sc_cfgt, bh, offset, val);
		break;
	case 4:
		bus_space_write_4(sc->sc_cfgt, bh, offset, val);
		break;
	default:
		panic("psycho_write_config: bad width");
	}
}

static int
psycho_route_interrupt(device_t bus, device_t dev, int pin)
{

	/*
	 * Since we preinitialize all interrupt line registers, this should not
	 * happen for any built-in device.
	 * Devices on bridges that route interrupts cannot work now - the
	 * interrupt pin mappings are not known from the firmware...
	 */
	panic("psycho_route_interrupt");
}

static int
psycho_read_ivar(device_t dev, device_t child, int which, u_long *result)
{
	struct psycho_softc *sc;

	sc = (struct psycho_softc *)device_get_softc(dev);
	switch (which) {
	case PCIB_IVAR_BUS:
		*result = sc->sc_busno;
		return (0);
	}
	return (ENOENT);
}

/* Write to the correct clr register, and call the actual handler. */
static void
psycho_intr_stub(void *arg)
{
	struct psycho_clr *pc;

	pc = (struct psycho_clr *)arg;
	*pc->pci_clr = 0;
	pc->pci_handler(pc->pci_arg);
}

#ifdef PSYCHO_STRAY
/*
 * Write to the correct clr register and return. arg is the address of the clear
 * register to be used.
 * XXX: print a message?
 */
static void
psycho_intr_stray(void *arg)
{

	*((u_long *)arg) = 0;
}
#endif

static int
psycho_setup_intr(device_t dev, device_t child,
    struct resource *ires,  int flags, driver_intr_t *intr, void *arg,
    void **cookiep)
{
	struct psycho_softc *sc;
	struct psycho_clr *pc;
	u_long *intrmapptr, *intrclrptr;
	int ino;
	int error;
	long vec = rman_get_start(ires);

	sc = (struct psycho_softc *)device_get_softc(dev);
	pc = (struct psycho_clr *)
		malloc(sizeof(*pc), M_DEVBUF, M_NOWAIT);
	if (pc == NULL)
		return (NULL);

	/*
	 * Hunt through all the interrupt mapping regs to look for our
	 * interrupt vector.
	 *
	 * XXX We only compare INOs rather than IGNs since the firmware may
	 * not provide the IGN and the IGN is constant for all device on that
	 * PCI controller.  This could cause problems for the FFB/external
	 * interrupt which has a full vector that can be set arbitrarily.  
	 */
	ino = INTINO(vec);

	if (!psycho_find_intrmap(sc, ino, &intrmapptr, &intrclrptr, NULL)) {
		printf("Cannot find interrupt vector %lx\n", vec);
		free(pc, M_DEVBUF);
		return (NULL);
	}

#ifdef PSYCHO_DEBUG
	device_printf(dev, "psycho_setup_intr: INO %d, map %p, clr %p\n", ino,
	    intrmapptr, intrclrptr);
#endif
	pc->pci_arg = arg;
	pc->pci_handler = intr;
	pc->pci_clr = intrclrptr;
	/* Disable the interrupt while we fiddle with it */
	*intrmapptr &= ~INTMAP_V;
	membar(Sync);
	error = bus_setup_intr(dev, ires, flags, psycho_intr_stub, pc,
	    cookiep);
	if (error != 0) {
		free(pc, M_DEVBUF);
		return (error);
	}
	pc->pci_cookie = *cookiep;
	*cookiep = pc;
	
	/*
	 * Clear the interrupt, it might have been triggered before it was
	 * set up.
	 */
	*intrclrptr = 0;
	membar(StoreStore);
	/*
	 * Enable the interrupt now we have the handler installed.
	 * Read the current value as we can't change it besides the
	 * valid bit so so make sure only this bit is changed.
	 */
	*intrmapptr |= INTMAP_V;
	return (error);
}

static int
psycho_teardown_intr(device_t dev, device_t child,
    struct resource *vec, void *cookie)
{
	struct psycho_clr *pc;
	int error;

	pc = (struct psycho_clr *)cookie;
	error = bus_teardown_intr(dev, vec, pc->pci_cookie);
	/*
	 * Don't disable the interrupt for now, so that stray interupts get
	 * detected...
	 */
	if (error != 0)
		free(pc, M_DEVBUF);
	return (error);
}

static struct resource *
psycho_alloc_resource(device_t bus, device_t child, int type, int *rid,
    u_long start, u_long end, u_long count, u_int flags)
{
	struct psycho_softc *sc;
	struct resource *rv;
	struct rman *rm;
	bus_space_tag_t bt;
	bus_space_handle_t bh;
	int needactivate = flags & RF_ACTIVE;

	flags &= ~RF_ACTIVE;

	sc = (struct psycho_softc *)device_get_softc(bus);
	if (type == SYS_RES_IRQ) {
		/*
		 * XXX: Don't accept blank ranges for now, only single
		 * interrupts. The other case should not happen with the MI pci
		 * code...
		 * XXX: This may return a resource that is out of the range
		 * that was specified. Is this correct...?
		 */
		if (start != end)
			panic("psycho_alloc_resource: XXX: interrupt range");
		start = end |= sc->sc_ign;
		return (bus_alloc_resource(bus, type, rid, start, end,
		    count, flags));
	}
	switch (type) {
	case SYS_RES_MEMORY:
		rm = &sc->sc_mem_rman;
		bt = sc->sc_memt;
		bh = sc->sc_bh[PCI_CS_MEM32];
		break;
	case SYS_RES_IOPORT:
		rm = &sc->sc_io_rman;
		bt = sc->sc_iot;
		/* XXX: probably should use ranges property here. */
		bh = sc->sc_bh[PCI_CS_IO];
		break;
	default:
		return (NULL);
	}

	rv = rman_reserve_resource(rm, start, end, count, flags, child);
	if (rv == NULL)
		return (NULL);

	bh += rman_get_start(rv);
	rman_set_bustag(rv, bt);
	rman_set_bushandle(rv, bh); 

	if (needactivate) {
		if (bus_activate_resource(child, type, *rid, rv)) {
			rman_release_resource(rv);
			return (NULL);
		}
	}
	
	return (rv);
}

static int
psycho_activate_resource(device_t bus, device_t child, int type, int rid,
    struct resource *r)
{

	if (type == SYS_RES_IRQ)
		return (bus_activate_resource(bus, type, rid, r));
	return (rman_activate_resource(r));
}

static int
psycho_deactivate_resource(device_t bus, device_t child, int type, int rid,
    struct resource *r)
{
		
	if (type == SYS_RES_IRQ)
		return (bus_deactivate_resource(bus, type, rid, r));
	return (rman_deactivate_resource(r));
}

static int
psycho_release_resource(device_t bus, device_t child, int type, int rid,
		       struct resource *r)
{
	int error;

	if (type == SYS_RES_IRQ)
		return (bus_release_resource(bus, type, rid, r));
	if (rman_get_flags(r) & RF_ACTIVE) {
		error = bus_deactivate_resource(child, type, rid, r);
		if (error)
			return error;
	}
	return (rman_release_resource(r));
}

static int
psycho_intr_pending(device_t dev, int intr)
{
	struct psycho_softc *sc;
	u_long diag;

	sc = (struct psycho_softc *)device_get_softc(dev);
	if (!psycho_find_intrmap(sc, intr, NULL, NULL, &diag)) {
		printf("psycho_intr_pending: mapping not found for %d\n", intr);
		return (0);
	}
	return (diag != 0);
}

static bus_space_handle_t
psycho_get_bus_handle(device_t dev, enum sbbt_id id,
    bus_space_handle_t childhdl, bus_space_tag_t *tag)
{
	struct psycho_softc *sc;

	sc = (struct psycho_softc *)device_get_softc(dev);
	switch(id) {
	case SBBT_IO:
		*tag = sc->sc_iot;
		return (sc->sc_bh[PCI_CS_IO] + childhdl);
	case SBBT_MEM:
		*tag = sc->sc_memt;
		return (sc->sc_bh[PCI_CS_MEM32] + childhdl);
	default:
		panic("psycho_get_bus_handle: illegal space\n");
	}
}

/*
 * below here is bus space and bus dma support
 */
static bus_space_tag_t
psycho_alloc_bus_tag(struct psycho_softc *sc, int type)
{
	bus_space_tag_t bt;

	bt = (bus_space_tag_t)
		malloc(sizeof(struct bus_space_tag), M_DEVBUF, M_NOWAIT);
	if (bt == NULL)
		panic("could not allocate psycho bus tag");

	bzero(bt, sizeof *bt);
	bt->cookie = sc;
	bt->parent = sc->sc_bustag;
	bt->type = type;
	return (bt);
}

/*
 * hooks into the iommu dvma calls.
 */
static int
psycho_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags, bus_dmamap_t *mapp)
{
	struct psycho_softc *sc;

	sc = (struct psycho_softc *)dmat->cookie;
	return (iommu_dvmamem_alloc(dmat, sc->sc_is, vaddr, flags, mapp));
}

static void
psycho_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
{
	struct psycho_softc *sc;

	sc = (struct psycho_softc *)dmat->cookie;
	return (iommu_dvmamem_free(dmat, sc->sc_is, vaddr, map));
}

static int
psycho_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
    bus_size_t buflen, bus_dmamap_callback_t *callback, void *callback_arg,
    int flags)
{
	struct psycho_softc *sc;

	sc = (struct psycho_softc *)dmat->cookie;
	return (iommu_dvmamap_load(dmat, sc->sc_is, map, buf, buflen, callback,
	    callback_arg, flags));
}

static void
psycho_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
{
	struct psycho_softc *sc;

	sc = (struct psycho_softc *)dmat->cookie;
	iommu_dvmamap_unload(dmat, sc->sc_is, map);
}

static void
psycho_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map,
    bus_dmasync_op_t op)
{
	struct psycho_softc *sc;

	sc = (struct psycho_softc *)dmat->cookie;
	iommu_dvmamap_sync(dmat, sc->sc_is, map, op);
}
OpenPOWER on IntegriCloud