summaryrefslogtreecommitdiff
path: root/sys/dev/ieee1394/fwdma.c
blob: 09300fdc76bd4910ad7ff6aa633dc5bd1c10ac7b (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
/*	$NetBSD: fwdma.c,v 1.18 2021/07/24 21:31:37 andvar Exp $	*/
/*-
 * Copyright (c) 2003
 * 	Hidetoshi Shimokawa. 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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *
 *	This product includes software developed by Hidetoshi Shimokawa.
 *
 * 4. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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: fwdma.c,v 1.18 2021/07/24 21:31:37 andvar Exp $");
#if defined(__FreeBSD__)
__FBSDID("$FreeBSD: src/sys/dev/firewire/fwdma.c,v 1.9 2007/06/06 14:31:36 simokawa Exp $");
#endif

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/select.h>

#include <machine/vmparam.h>

#include <dev/ieee1394/firewire.h>
#include <dev/ieee1394/firewirereg.h>
#include <dev/ieee1394/fwdma.h>

#define BUS_SPACE_MAXSIZE_32BIT		0xffffffff


void *
fwdma_malloc(device_t dev, bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
	     bus_size_t size, int alignment, int flags)
{
	bus_dma_segment_t segs;
	int nsegs;
	int err;
	void *v_addr;

	err = bus_dmamem_alloc(dmat, size, alignment, 0, &segs, 1,
	    &nsegs, flags);
	if (err) {
		aprint_error_dev(dev, "DMA memory allocation failed %d\n", err);
		return NULL;
	}

	err = bus_dmamem_map(dmat, &segs, nsegs, size, &v_addr, flags);
	if (err) {
		aprint_error_dev(dev, "DMA memory map failed %d\n", err);
		bus_dmamem_free(dmat, &segs, nsegs);
		return NULL;
	}

	if (*dmamap == NULL) {
		err = bus_dmamap_create(dmat, size, nsegs,
		    BUS_SPACE_MAXSIZE_32BIT, 0, flags, dmamap);
		if (err) {
			aprint_error_dev(dev,
			    "DMA map create failed %d\n", err);
			bus_dmamem_unmap(dmat, v_addr, size);
			bus_dmamem_free(dmat, &segs, nsegs);
			return NULL;
		}
	}

	err = bus_dmamap_load(dmat, *dmamap, v_addr, size, NULL, flags);
	if (err != 0) {
		aprint_error_dev(dev, "DMA map load failed %d\n", err);
		bus_dmamap_destroy(dmat, *dmamap);
		bus_dmamem_unmap(dmat, v_addr, size);
		bus_dmamem_free(dmat, &segs, nsegs);
		return NULL;
	}

	return v_addr;
}

void
fwdma_free(bus_dma_tag_t dmat, bus_dmamap_t dmamap, void *vaddr)
{
	bus_dma_segment_t *segs;

	/* XXX we shouldn't pass around the segs in the dmamap */
	const bus_size_t mapsize = dmamap->dm_mapsize;
	const int nsegs = dmamap->dm_nsegs;
	const size_t segssz = sizeof(bus_dma_segment_t) * nsegs;
	segs = kmem_alloc(segssz, KM_SLEEP);
	memcpy(segs, dmamap->dm_segs, segssz);
	
	bus_dmamap_unload(dmat, dmamap);
	bus_dmamem_unmap(dmat, vaddr, mapsize);
	bus_dmamem_free(dmat, segs, nsegs);
	bus_dmamap_destroy(dmat, dmamap);
	
	kmem_free(segs, segssz);
}


void *
fwdma_alloc_setup(device_t dev, bus_dma_tag_t dmat, bus_size_t size,
		  struct fwdma_alloc *dma, int alignment, int flags)
{

	dma->v_addr =
	    fwdma_malloc(dev, dmat, &dma->dma_map, size, alignment, flags);
	if (dma->v_addr != NULL) {
		dma->dma_tag = dmat;
		dma->bus_addr = dma->dma_map->dm_segs[0].ds_addr;
	}
	return dma->v_addr;
}

/*
 * Allocate multisegment dma buffers
 * each segment size is equal to ssize except last segment.
 */
struct fwdma_alloc_multi *
fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment, int esize, int n,
		      int flags)
{
	struct fwdma_alloc_multi *am;
	struct fwdma_seg *seg;
	bus_size_t ssize;
	size_t size;
	int nseg;

	if (esize > PAGE_SIZE) {
		/* round up to PAGE_SIZE */
		esize = ssize = roundup2(esize, PAGE_SIZE);
		nseg = n;
	} else {
		/* allocate PAGE_SIZE segment for small elements */
		ssize = rounddown(PAGE_SIZE, esize);
		nseg = howmany(n, ssize / esize);
	}
	size = sizeof(struct fwdma_alloc_multi) +
	    sizeof(struct fwdma_seg) * nseg;
	am = (struct fwdma_alloc_multi *)malloc(size, M_FW, M_WAITOK | M_ZERO);
	if (am == NULL) {
		aprint_error_dev(fc->dev, "malloc failed\n");
		return NULL;
	}
	am->ssize = ssize;
	am->esize = esize;
	am->nseg = 0;
	am->dma_tag = fc->dmat;

	for (seg = am->seg; nseg--; seg++) {
		seg->v_addr = fwdma_malloc(fc->dev, am->dma_tag, &seg->dma_map,
		    ssize, alignment, flags);
		if (seg->v_addr == NULL) {
			aprint_error_dev(fc->dev, "malloc_size failed %d\n",
			    am->nseg);
			fwdma_free_multiseg(am);
			return NULL;
		}
		seg->bus_addr = seg->dma_map->dm_segs[0].ds_addr;
		am->nseg++;
	}
	return am;
}

void
fwdma_free_multiseg(struct fwdma_alloc_multi *am)
{
	struct fwdma_seg *seg;

	for (seg = am->seg; am->nseg--; seg++)
		fwdma_free(am->dma_tag, seg->dma_map, seg->v_addr);
	free(am, M_FW);
}