Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: mvmebus.c,v 1.20 2018/09/03 16:29:32 riastradh Exp $	*/

/*-
 * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Steve C. Woodford.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mvmebus.c,v 1.20 2018/09/03 16:29:32 riastradh Exp $");

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

#include <sys/cpu.h>
#include <sys/bus.h>

#include <dev/vme/vmereg.h>
#include <dev/vme/vmevar.h>

#include <dev/mvme/mvmebus.h>

#ifdef DIAGNOSTIC
int	mvmebus_dummy_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
	    bus_size_t, int, bus_dmamap_t *);
void	mvmebus_dummy_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
int	mvmebus_dummy_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
	    bus_size_t, bus_dma_segment_t *, int, int *, int);
void	mvmebus_dummy_dmamem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
#endif

#ifdef DEBUG
static const char *mvmebus_mod_string(vme_addr_t, vme_size_t,
	    vme_am_t, vme_datasize_t);
#endif

static void mvmebus_offboard_ram(struct mvmebus_softc *);
static int mvmebus_dmamap_load_common(struct mvmebus_softc *, bus_dmamap_t);

vme_am_t	_mvmebus_am_cap[] = {
	MVMEBUS_AM_CAP_BLKD64 | MVMEBUS_AM_CAP_USER,
	MVMEBUS_AM_CAP_DATA   | MVMEBUS_AM_CAP_USER,
	MVMEBUS_AM_CAP_PROG   | MVMEBUS_AM_CAP_USER,
	MVMEBUS_AM_CAP_BLK    | MVMEBUS_AM_CAP_USER,
	MVMEBUS_AM_CAP_BLKD64 | MVMEBUS_AM_CAP_SUPER,
	MVMEBUS_AM_CAP_DATA   | MVMEBUS_AM_CAP_SUPER,
	MVMEBUS_AM_CAP_PROG   | MVMEBUS_AM_CAP_SUPER,
	MVMEBUS_AM_CAP_BLK    | MVMEBUS_AM_CAP_SUPER
};

const char *mvmebus_irq_name[] = {
	"vmeirq0", "vmeirq1", "vmeirq2", "vmeirq3",
	"vmeirq4", "vmeirq5", "vmeirq6", "vmeirq7"
};

extern phys_ram_seg_t mem_clusters[0];
extern int mem_cluster_cnt;


static void
mvmebus_offboard_ram(struct mvmebus_softc *sc)
{
	struct mvmebus_range *svr, *mvr;
	vme_addr_t start, end, size;
	int i;

	/*
	 * If we have any offboard RAM (i.e. a VMEbus RAM board) then
	 * we need to record its details since it's effectively another
	 * VMEbus slave image as far as we're concerned.
	 * The chip-specific backend will have reserved sc->sc_slaves[0]
	 * for exactly this purpose.
	 */
	svr = sc->sc_slaves;
	if (mem_cluster_cnt < 2) {
		svr->vr_am = MVMEBUS_AM_DISABLED;
		return;
	}

	start = mem_clusters[1].start;
	size = mem_clusters[1].size - 1;
	end = start + size;

	/*
	 * Figure out which VMEbus master image the RAM is
	 * visible through. This will tell us the address
	 * modifier and datasizes it uses, as well as allowing
	 * us to calculate its `real' VMEbus address.
	 *
	 * XXX FIXME: This is broken if the RAM is mapped through
	 * a translated address space. For example, on mvme167 it's
	 * perfectly legal to set up the following A32 mapping:
	 *
	 *  vr_locaddr  == 0x80000000
	 *  vr_vmestart == 0x10000000
	 *  vr_vmeend   == 0x10ffffff
	 *
	 * In this case, RAM at VMEbus address 0x10800000 will appear at local
	 * address 0x80800000, but we need to set the slave vr_vmestart to
	 * 0x10800000.
	 */
	for (i = 0, mvr = sc->sc_masters; i < sc->sc_nmasters; i++, mvr++) {
		vme_addr_t vstart = mvr->vr_locstart + mvr->vr_vmestart;

		if (start >= vstart &&
		    end <= vstart + (mvr->vr_vmeend - mvr->vr_vmestart))
			break;
	}
	if (i == sc->sc_nmasters) {
		svr->vr_am = MVMEBUS_AM_DISABLED;
#ifdef DEBUG
		printf("%s: No VMEbus master mapping for offboard RAM!\n",
		    device_xname(sc->sc_dev));
#endif
		return;
	}

	svr->vr_locstart = start;
	svr->vr_vmestart = start & mvr->vr_mask;
	svr->vr_vmeend = svr->vr_vmestart + size;
	svr->vr_datasize = mvr->vr_datasize;
	svr->vr_mask = mvr->vr_mask;
	svr->vr_am = mvr->vr_am & VME_AM_ADRSIZEMASK;
	svr->vr_am |= MVMEBUS_AM_CAP_DATA  | MVMEBUS_AM_CAP_PROG |
		      MVMEBUS_AM_CAP_SUPER | MVMEBUS_AM_CAP_USER;
}

void
mvmebus_attach(struct mvmebus_softc *sc)
{
	struct vmebus_attach_args vaa;
	int i;

	/* Zap the IRQ reference counts */
	for (i = 0; i < 8; i++)
		sc->sc_irqref[i] = 0;

	/* If there's offboard RAM, get its VMEbus slave attributes */
	mvmebus_offboard_ram(sc);

#ifdef DEBUG
	for (i = 0; i < sc->sc_nmasters; i++) {
		struct mvmebus_range *vr = &sc->sc_masters[i];
		if (vr->vr_am == MVMEBUS_AM_DISABLED) {
			printf("%s: Master#%d: disabled\n",
			    device_xname(sc->sc_dev), i);
			continue;
		}
		printf("%s: Master#%d: 0x%08lx -> %s\n",
		    device_xname(sc->sc_dev), i,
		    vr->vr_locstart + (vr->vr_vmestart & vr->vr_mask),
		    mvmebus_mod_string(vr->vr_vmestart,
			(vr->vr_vmeend - vr->vr_vmestart) + 1,
			vr->vr_am, vr->vr_datasize));
	}

	for (i = 0; i < sc->sc_nslaves; i++) {
		struct mvmebus_range *vr = &sc->sc_slaves[i];
		if (vr->vr_am == MVMEBUS_AM_DISABLED) {
			printf("%s:  Slave#%d: disabled\n",
			    device_xname(sc->sc_dev), i);
			continue;
		}
		printf("%s:  Slave#%d: 0x%08lx -> %s\n",
		    device_xname(sc->sc_dev), i, vr->vr_locstart,
		    mvmebus_mod_string(vr->vr_vmestart,
			(vr->vr_vmeend - vr->vr_vmestart) + 1,
			vr->vr_am, vr->vr_datasize));
	}
#endif

	sc->sc_vct.cookie = sc;
	sc->sc_vct.vct_probe = mvmebus_probe;
	sc->sc_vct.vct_map = mvmebus_map;
	sc->sc_vct.vct_unmap = mvmebus_unmap;
	sc->sc_vct.vct_int_map = mvmebus_intmap;
	sc->sc_vct.vct_int_evcnt = mvmebus_intr_evcnt;
	sc->sc_vct.vct_int_establish = mvmebus_intr_establish;
	sc->sc_vct.vct_int_disestablish = mvmebus_intr_disestablish;
	sc->sc_vct.vct_dmamap_create = mvmebus_dmamap_create;
	sc->sc_vct.vct_dmamap_destroy = mvmebus_dmamap_destroy;
	sc->sc_vct.vct_dmamem_alloc = mvmebus_dmamem_alloc;
	sc->sc_vct.vct_dmamem_free = mvmebus_dmamem_free;

	sc->sc_mvmedmat._cookie = sc;
	sc->sc_mvmedmat._dmamap_load = mvmebus_dmamap_load;
	sc->sc_mvmedmat._dmamap_load_mbuf = mvmebus_dmamap_load_mbuf;
	sc->sc_mvmedmat._dmamap_load_uio = mvmebus_dmamap_load_uio;
	sc->sc_mvmedmat._dmamap_load_raw = mvmebus_dmamap_load_raw;
	sc->sc_mvmedmat._dmamap_unload = mvmebus_dmamap_unload;
	sc->sc_mvmedmat._dmamap_sync = mvmebus_dmamap_sync;
	sc->sc_mvmedmat._dmamem_map = mvmebus_dmamem_map;
	sc->sc_mvmedmat._dmamem_unmap = mvmebus_dmamem_unmap;
	sc->sc_mvmedmat._dmamem_mmap = mvmebus_dmamem_mmap;

#ifdef DIAGNOSTIC
	sc->sc_mvmedmat._dmamap_create = mvmebus_dummy_dmamap_create;
	sc->sc_mvmedmat._dmamap_destroy = mvmebus_dummy_dmamap_destroy;
	sc->sc_mvmedmat._dmamem_alloc = mvmebus_dummy_dmamem_alloc;
	sc->sc_mvmedmat._dmamem_free = mvmebus_dummy_dmamem_free;
#else
	sc->sc_mvmedmat._dmamap_create = NULL;
	sc->sc_mvmedmat._dmamap_destroy = NULL;
	sc->sc_mvmedmat._dmamem_alloc = NULL;
	sc->sc_mvmedmat._dmamem_free = NULL;
#endif

	vaa.va_vct = &sc->sc_vct;
	vaa.va_bdt = &sc->sc_mvmedmat;
	vaa.va_slaveconfig = NULL;

	config_found(sc->sc_dev, &vaa, 0);
}

int
mvmebus_map(void *vsc, vme_addr_t vmeaddr, vme_size_t len, vme_am_t am, vme_datasize_t datasize, vme_swap_t swap, bus_space_tag_t *tag, bus_space_handle_t *handle, vme_mapresc_t *resc)
{
	struct mvmebus_softc *sc;
	struct mvmebus_mapresc *mr;
	struct mvmebus_range *vr;
	vme_addr_t end;
	vme_am_t cap, as;
	paddr_t paddr;
	int rv, i;

	sc = vsc;
	end = (vmeaddr + len) - 1;
	paddr = 0;
	vr = sc->sc_masters;
	cap = MVMEBUS_AM2CAP(am);
	as = am & VME_AM_ADRSIZEMASK;

	for (i = 0; i < sc->sc_nmasters && paddr == 0; i++, vr++) {
		if (vr->vr_am == MVMEBUS_AM_DISABLED)
			continue;

		if (cap == (vr->vr_am & cap) &&
		    as == (vr->vr_am & VME_AM_ADRSIZEMASK) &&
		    datasize <= vr->vr_datasize &&
		    vmeaddr >= vr->vr_vmestart && end < vr->vr_vmeend)
			paddr = vr->vr_locstart + (vmeaddr & vr->vr_mask);
	}
	if (paddr == 0)
		return (ENOMEM);

	rv = bus_space_map(sc->sc_bust, paddr, len, 0, handle);
	if (rv != 0)
		return (rv);

	/* Allocate space for the resource tag */
	if ((mr = malloc(sizeof(*mr), M_DEVBUF, M_NOWAIT)) == NULL) {
		bus_space_unmap(sc->sc_bust, *handle, len);
		return (ENOMEM);
	}

	/* Record the range's details */
	mr->mr_am = am;
	mr->mr_datasize = datasize;
	mr->mr_addr = vmeaddr;
	mr->mr_size = len;
	mr->mr_handle = *handle;
	mr->mr_range = i;

	*tag = sc->sc_bust;
	*resc = (vme_mapresc_t *) mr;

	return (0);
}

/* ARGSUSED */
void
mvmebus_unmap(void *vsc, vme_mapresc_t resc)
{
	struct mvmebus_softc *sc = vsc;
	struct mvmebus_mapresc *mr = (struct mvmebus_mapresc *) resc;

	bus_space_unmap(sc->sc_bust, mr->mr_handle, mr->mr_size);

	free(mr, M_DEVBUF);
}

int
mvmebus_probe(void *vsc, vme_addr_t vmeaddr, vme_size_t len, vme_am_t am, vme_datasize_t datasize, int (*callback)(void *, bus_space_tag_t, bus_space_handle_t), void *arg)
{
	bus_space_tag_t tag;
	bus_space_handle_t handle;
	vme_mapresc_t resc;
	vme_size_t offs;
	int rv;

	/* Get a temporary mapping to the VMEbus range */
	rv = mvmebus_map(vsc, vmeaddr, len, am, datasize, 0,
	    &tag, &handle, &resc);
	if (rv)
		return (rv);

	if (callback)
		rv = (*callback) (arg, tag, handle);
	else
		for (offs = 0; offs < len && rv == 0;) {
			switch (datasize) {
			case VME_D8:
				rv = bus_space_peek_1(tag, handle, offs, NULL);
				offs += 1;
				break;

			case VME_D16:
				rv = bus_space_peek_2(tag, handle, offs, NULL);
				offs += 2;
				break;

			case VME_D32:
				rv = bus_space_peek_4(tag, handle, offs, NULL);
				offs += 4;
				break;
			}
		}

	mvmebus_unmap(vsc, resc);

	return (rv);
}

/* ARGSUSED */
int
mvmebus_intmap(void *vsc, int level, int vector, vme_intr_handle_t *handlep)
{

	if (level < 1 || level > 7 || vector < 0x80 || vector > 0xff)
		return (EINVAL);

	/* This is rather gross */
	*handlep = (void *) (int) ((level << 8) | vector);
	return (0);
}

/* ARGSUSED */
const struct evcnt *
mvmebus_intr_evcnt(void *vsc, vme_intr_handle_t handle)
{
	struct mvmebus_softc *sc = vsc;

	return (&sc->sc_evcnt[(((int) handle) >> 8) - 1]);
}

void *
mvmebus_intr_establish(void *vsc, vme_intr_handle_t handle, int prior, int (*func)(void *), void *arg)
{
	struct mvmebus_softc *sc;
	int level, vector, first;

	sc = vsc;

	/* Extract the interrupt's level and vector */
	level = ((int) handle) >> 8;
	vector = ((int) handle) & 0xff;

#ifdef DIAGNOSTIC
	if (vector < 0 || vector > 0xff) {
		printf("%s: Illegal vector offset: 0x%x\n",
		    device_xname(sc->sc_dev), vector);
		panic("mvmebus_intr_establish");
	}
	if (level < 1 || level > 7) {
		printf("%s: Illegal interrupt level: %d\n",
		    device_xname(sc->sc_dev), level);
		panic("mvmebus_intr_establish");
	}
#endif

	first = (sc->sc_irqref[level]++ == 0);

	(*sc->sc_intr_establish)(sc->sc_chip, prior, level, vector, first,
	    func, arg, &sc->sc_evcnt[level - 1]);

	return ((void *) handle);
}

void
mvmebus_intr_disestablish(void *vsc, vme_intr_handle_t handle)
{
	struct mvmebus_softc *sc;
	int level, vector, last;

	sc = vsc;

	/* Extract the interrupt's level and vector */
	level = ((int) handle) >> 8;
	vector = ((int) handle) & 0xff;

#ifdef DIAGNOSTIC
	if (vector < 0 || vector > 0xff) {
		printf("%s: Illegal vector offset: 0x%x\n",
		    device_xname(sc->sc_dev), vector);
		panic("mvmebus_intr_disestablish");
	}
	if (level < 1 || level > 7) {
		printf("%s: Illegal interrupt level: %d\n",
		    device_xname(sc->sc_dev), level);
		panic("mvmebus_intr_disestablish");
	}
	if (sc->sc_irqref[level] == 0) {
		printf("%s: VMEirq#%d: Reference count already zero!\n",
		    device_xname(sc->sc_dev), level);
		panic("mvmebus_intr_disestablish");
	}
#endif

	last = (--(sc->sc_irqref[level]) == 0);

	(*sc->sc_intr_disestablish)(sc->sc_chip, level, vector, last,
	    &sc->sc_evcnt[level - 1]);
}

#ifdef DIAGNOSTIC
/* ARGSUSED */
int
mvmebus_dummy_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegs, bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
{

	panic("Must use vme_dmamap_create() in place of bus_dmamap_create()");
	return (0);	/* Shutup the compiler */
}

/* ARGSUSED */
void
mvmebus_dummy_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
{

	panic("Must use vme_dmamap_destroy() in place of bus_dmamap_destroy()");
}
#endif

/* ARGSUSED */
int
mvmebus_dmamap_create(
	void *vsc,
	vme_size_t len,
	vme_am_t am,
	vme_datasize_t datasize,
	vme_swap_t swap,
	int nsegs,
	vme_size_t segsz,
	vme_addr_t bound,
	int flags,
	bus_dmamap_t *mapp)
{
	struct mvmebus_softc *sc = vsc;
	struct mvmebus_dmamap *vmap;
	struct mvmebus_range *vr;
	vme_am_t cap, as;
	int i, rv;

	cap = MVMEBUS_AM2CAP(am);
	as = am & VME_AM_ADRSIZEMASK;

	/*
	 * Verify that we even stand a chance of satisfying
	 * the VMEbus address space and datasize requested.
	 */
	for (i = 0, vr = sc->sc_slaves; i < sc->sc_nslaves; i++, vr++) {
		if (vr->vr_am == MVMEBUS_AM_DISABLED)
			continue;

		if (as == (vr->vr_am & VME_AM_ADRSIZEMASK) &&
		    cap == (vr->vr_am & cap) && datasize <= vr->vr_datasize &&
		    len <= (vr->vr_vmeend - vr->vr_vmestart))
			break;
	}

	if (i == sc->sc_nslaves)
		return (EINVAL);

	if ((vmap = malloc(sizeof(*vmap), M_DMAMAP,
	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
		return (ENOMEM);


	rv = bus_dmamap_create(sc->sc_dmat, len, nsegs, segsz,
	    bound, flags, mapp);
	if (rv != 0) {
		free(vmap, M_DMAMAP);
		return (rv);
	}

	vmap->vm_am = am;
	vmap->vm_datasize = datasize;
	vmap->vm_swap = swap;
	vmap->vm_slave = vr;

	(*mapp)->_dm_cookie = vmap;

	return (0);
}

void
mvmebus_dmamap_destroy(void *vsc, bus_dmamap_t map)
{
	struct mvmebus_softc *sc = vsc;

	free(map->_dm_cookie, M_DMAMAP);
	bus_dmamap_destroy(sc->sc_dmat, map);
}

static int
mvmebus_dmamap_load_common(struct mvmebus_softc *sc, bus_dmamap_t map)
{
	struct mvmebus_dmamap *vmap = map->_dm_cookie;
	struct mvmebus_range *vr = vmap->vm_slave;
	bus_dma_segment_t *ds;
	vme_am_t cap, am;
	int i;

	cap = MVMEBUS_AM2CAP(vmap->vm_am);
	am = vmap->vm_am & VME_AM_ADRSIZEMASK;

	/*
	 * Traverse the list of segments which make up this map, and
	 * convert the CPU-relative addresses therein to VMEbus addresses.
	 */
	for (ds = &map->dm_segs[0]; ds < &map->dm_segs[map->dm_nsegs]; ds++) {
		/*
		 * First, see if this map's slave image can access the
		 * segment, otherwise we have to waste time scanning all
		 * the slave images.
		 */
		vr = vmap->vm_slave;
		if (am == (vr->vr_am & VME_AM_ADRSIZEMASK) &&
		    cap == (vr->vr_am & cap) &&
		    vmap->vm_datasize <= vr->vr_datasize &&
		    ds->_ds_cpuaddr >= vr->vr_locstart &&
		    ds->ds_len <= (vr->vr_vmeend - vr->vr_vmestart))
			goto found;

		for (i = 0, vr = sc->sc_slaves; i < sc->sc_nslaves; i++, vr++) {
			if (vr->vr_am == MVMEBUS_AM_DISABLED)
				continue;

			/*
			 * Filter out any slave images which don't have the
			 * same VMEbus address modifier and datasize as
			 * this DMA map, and those which don't cover the
			 * physical address region containing the segment.
			 */
			if (vr != vmap->vm_slave &&
			    am == (vr->vr_am & VME_AM_ADRSIZEMASK) &&
			    cap == (vr->vr_am & cap) &&
			    vmap->vm_datasize <= vr->vr_datasize &&
			    ds->_ds_cpuaddr >= vr->vr_locstart &&
			    ds->ds_len <= (vr->vr_vmeend - vr->vr_vmestart))
				break;
		}

		/*
		 * Did we find an applicable slave image which covers this
		 * segment?
		 */
		if (i == sc->sc_nslaves) {
			/*
			 * XXX TODO:
			 *
			 * Bounce this segment via a bounce buffer allocated
			 * from this DMA map.
			 */
			printf("mvmebus_dmamap_load_common: bounce needed!\n");
			return (EINVAL);
		}

found:
		/*
		 * Generate the VMEbus address of this segment
		 */
		ds->ds_addr = (ds->_ds_cpuaddr - vr->vr_locstart) +
		    vr->vr_vmestart;
	}

	return (0);
}

int
mvmebus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
{
	struct mvmebus_softc *sc = t->_cookie;
	int rv;

	rv = bus_dmamap_load(sc->sc_dmat, map, buf, buflen, p, flags);
	if (rv != 0)
		return rv;

	return mvmebus_dmamap_load_common(sc, map);
}

int
mvmebus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *chain, int flags)
{
	struct mvmebus_softc *sc = t->_cookie;
	int rv;

	rv = bus_dmamap_load_mbuf(sc->sc_dmat, map, chain, flags);
	if (rv != 0)
		return rv;

	return mvmebus_dmamap_load_common(sc, map);
}

int
mvmebus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
{
	struct mvmebus_softc *sc = t->_cookie;
	int rv;

	rv = bus_dmamap_load_uio(sc->sc_dmat, map, uio, flags);
	if (rv != 0)
		return rv;

	return mvmebus_dmamap_load_common(sc, map);
}

int
mvmebus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
{
	struct mvmebus_softc *sc = t->_cookie;
	int rv;

	/*
	 * mvmebus_dmamem_alloc() will ensure that the physical memory
	 * backing these segments is 100% accessible in at least one
	 * of the board's VMEbus slave images.
	 */
	rv = bus_dmamap_load_raw(sc->sc_dmat, map, segs, nsegs, size, flags);
	if (rv != 0)
		return rv;

	return mvmebus_dmamap_load_common(sc, map);
}

void
mvmebus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
{
	struct mvmebus_softc *sc = t->_cookie;

	/* XXX Deal with bounce buffers */

	bus_dmamap_unload(sc->sc_dmat, map);
}

void
mvmebus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, bus_size_t len, int ops)
{
	struct mvmebus_softc *sc = t->_cookie;

	/* XXX Bounce buffers */

	bus_dmamap_sync(sc->sc_dmat, map, offset, len, ops);
}

#ifdef DIAGNOSTIC
/* ARGSUSED */
int
mvmebus_dummy_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t align, bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags)
{

	panic("Must use vme_dmamem_alloc() in place of bus_dmamem_alloc()");
}

/* ARGSUSED */
void
mvmebus_dummy_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
{

	panic("Must use vme_dmamem_free() in place of bus_dmamem_free()");
}
#endif

/* ARGSUSED */
int
mvmebus_dmamem_alloc(void *vsc, vme_size_t len, vme_am_t am, vme_datasize_t datasize, vme_swap_t swap, bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags)
{
	extern paddr_t avail_start;
	struct mvmebus_softc *sc = vsc;
	struct mvmebus_range *vr;
	bus_addr_t low, high;
	bus_size_t bound;
	vme_am_t cap;
	int i;

	cap = MVMEBUS_AM2CAP(am);
	am &= VME_AM_ADRSIZEMASK;

	/*
	 * Find a slave mapping in the requested VMEbus address space.
	 */
	for (i = 0, vr = sc->sc_slaves; i < sc->sc_nslaves; i++, vr++) {
		if (vr->vr_am == MVMEBUS_AM_DISABLED)
			continue;

		if (i == 0 && (flags & BUS_DMA_ONBOARD_RAM) != 0)
			continue;

		if (am == (vr->vr_am & VME_AM_ADRSIZEMASK) &&
		    cap == (vr->vr_am & cap) && datasize <= vr->vr_datasize &&
		    len <= (vr->vr_vmeend - vr->vr_vmestart))
			break;
	}
	if (i == sc->sc_nslaves)
		return (EINVAL);

	/*
	 * Set up the constraints so we can allocate physical memory which
	 * is visible in the requested address space
	 */
	low = uimax(vr->vr_locstart, avail_start);
	high = vr->vr_locstart + (vr->vr_vmeend - vr->vr_vmestart) + 1;
	bound = (bus_size_t) vr->vr_mask + 1;

	/*
	 * Allocate physical memory.
	 *
	 * Note: This fills in the segments with CPU-relative physical
	 * addresses. A further call to bus_dmamap_load_raw() (with a
	 * DMA map which specifies the same VMEbus address space and
	 * constraints as the call to here) must be made. The segments
	 * of the DMA map will then contain VMEbus-relative physical
	 * addresses of the memory allocated here.
	 */
	return _bus_dmamem_alloc_common(sc->sc_dmat, low, high,
	    len, 0, bound, segs, nsegs, rsegs, flags);
}

void
mvmebus_dmamem_free(void *vsc, bus_dma_segment_t *segs, int nsegs)
{
	struct mvmebus_softc *sc = vsc;

	bus_dmamem_free(sc->sc_dmat, segs, nsegs);
}

int
mvmebus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size, void **kvap, int flags)
{
	struct mvmebus_softc *sc = t->_cookie;

	return bus_dmamem_map(sc->sc_dmat, segs, nsegs, size, kvap, flags);
}

void
mvmebus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
{
	struct mvmebus_softc *sc = t->_cookie;

	bus_dmamem_unmap(sc->sc_dmat, kva, size);
}

paddr_t
mvmebus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, off_t offset, int prot, int flags)
{
	struct mvmebus_softc *sc = t->_cookie;

	return bus_dmamem_mmap(sc->sc_dmat, segs, nsegs, offset, prot, flags);
}

#ifdef DEBUG
static const char *
mvmebus_mod_string(vme_addr_t addr, vme_size_t len, vme_am_t am, vme_datasize_t ds)
{
	static const char *mode[] = {"BLT64)", "DATA)", "PROG)", "BLT32)"};
	static const char *dsiz[] = {"(", "(D8,", "(D16,", "(D16-D8,",
	"(D32,", "(D32,D8,", "(D32-D16,", "(D32-D8,"};
	static const char *adrfmt[] = { "A32:%08x-%08x ", "USR:%08x-%08x ",
	    "A16:%04x-%04x ", "A24:%06x-%06x " };
	static char mstring[40];

	snprintf(mstring, sizeof(mstring),
	    adrfmt[(am & VME_AM_ADRSIZEMASK) >> VME_AM_ADRSIZESHIFT],
	    addr, addr + len - 1);
	strlcat(mstring, dsiz[ds & 0x7], sizeof(mstring));

	if (MVMEBUS_AM_HAS_CAP(am)) {
		if (am & MVMEBUS_AM_CAP_DATA)
			strlcat(mstring, "D", sizeof(mstring));
		if (am & MVMEBUS_AM_CAP_PROG)
			strlcat(mstring, "P", sizeof(mstring));
		if (am & MVMEBUS_AM_CAP_USER)
			strlcat(mstring, "U", sizeof(mstring));
		if (am & MVMEBUS_AM_CAP_SUPER)
			strlcat(mstring, "S", sizeof(mstring));
		if (am & MVMEBUS_AM_CAP_BLK)
			strlcat(mstring, "B", sizeof(mstring));
		if (am & MVMEBUS_AM_CAP_BLKD64)
			strlcat(mstring, "6", sizeof(mstring));
		strlcat(mstring, ")", sizeof(mstring));
	} else {
		strlcat(mstring, ((am & VME_AM_PRIVMASK) == VME_AM_USER) ?
		    "USER," : "SUPER,", sizeof(mstring));
		strlcat(mstring, mode[am & VME_AM_MODEMASK], sizeof(mstring));
	}

	return (mstring);
}
#endif