2 * Copyright (c) 2016 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /* xDMA memcpy test driver. */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/resource.h>
48 #include <machine/bus.h>
50 #include <dev/xdma/xdma.h>
52 #include <dev/fdt/fdt_common.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
57 * To use this test add a compatible node to your dts, e.g.
60 * compatible = "freebsd,xdma-test";
62 * dmas = <&dma 0 0 0xffffffff>;
67 struct xdmatest_softc {
69 xdma_controller_t *xdma;
70 xdma_channel_t *xchan;
72 struct intr_config_hook config_intrhook;
78 bus_dma_tag_t src_dma_tag;
79 bus_dmamap_t src_dma_map;
80 bus_dma_tag_t dst_dma_tag;
81 bus_dmamap_t dst_dma_map;
85 struct xdma_request req;
88 static int xdmatest_probe(device_t dev);
89 static int xdmatest_attach(device_t dev);
90 static int xdmatest_detach(device_t dev);
93 xdmatest_intr(void *arg)
95 struct xdmatest_softc *sc;
103 mtx_unlock(&sc->mtx);
109 xdmatest_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
116 addr = (bus_addr_t*)arg;
117 *addr = segs[0].ds_addr;
121 xdmatest_alloc_test_memory(struct xdmatest_softc *sc)
125 sc->len = (0x1000000 - 8); /* 16mb */
130 err = bus_dma_tag_create(
131 bus_get_dma_tag(sc->dev),
132 1024, 0, /* alignment, boundary */
133 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
134 BUS_SPACE_MAXADDR, /* highaddr */
135 NULL, NULL, /* filter, filterarg */
136 sc->len, 1, /* maxsize, nsegments*/
137 sc->len, 0, /* maxsegsize, flags */
138 NULL, NULL, /* lockfunc, lockarg */
141 device_printf(sc->dev,
142 "%s: Can't create bus_dma tag.\n", __func__);
146 err = bus_dmamem_alloc(sc->src_dma_tag, (void **)&sc->src,
147 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &sc->src_dma_map);
149 device_printf(sc->dev,
150 "%s: Can't allocate memory.\n", __func__);
154 err = bus_dmamap_load(sc->src_dma_tag, sc->src_dma_map, sc->src,
155 sc->len, xdmatest_dmamap_cb, &sc->src_phys, BUS_DMA_WAITOK);
157 device_printf(sc->dev,
158 "%s: Can't load DMA map.\n", __func__);
162 /* Destination memory. */
164 err = bus_dma_tag_create(
165 bus_get_dma_tag(sc->dev),
166 1024, 0, /* alignment, boundary */
167 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
168 BUS_SPACE_MAXADDR, /* highaddr */
169 NULL, NULL, /* filter, filterarg */
170 sc->len, 1, /* maxsize, nsegments*/
171 sc->len, 0, /* maxsegsize, flags */
172 NULL, NULL, /* lockfunc, lockarg */
175 device_printf(sc->dev,
176 "%s: Can't create bus_dma tag.\n", __func__);
180 err = bus_dmamem_alloc(sc->dst_dma_tag, (void **)&sc->dst,
181 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &sc->dst_dma_map);
183 device_printf(sc->dev,
184 "%s: Can't allocate memory.\n", __func__);
188 err = bus_dmamap_load(sc->dst_dma_tag, sc->dst_dma_map, sc->dst,
189 sc->len, xdmatest_dmamap_cb, &sc->dst_phys, BUS_DMA_WAITOK);
191 device_printf(sc->dev,
192 "%s: Can't load DMA map.\n", __func__);
200 xdmatest_test(struct xdmatest_softc *sc)
205 /* Get xDMA controller. */
206 sc->xdma = xdma_ofw_get(sc->dev, "test");
207 if (sc->xdma == NULL) {
208 device_printf(sc->dev, "Can't find xDMA controller.\n");
212 /* Alloc xDMA virtual channel. */
213 sc->xchan = xdma_channel_alloc(sc->xdma);
214 if (sc->xchan == NULL) {
215 device_printf(sc->dev, "Can't alloc virtual DMA channel.\n");
219 /* Setup callback. */
220 err = xdma_setup_intr(sc->xchan, xdmatest_intr, sc, &sc->ih);
222 device_printf(sc->dev, "Can't setup xDMA interrupt handler.\n");
226 /* We are going to fill memory. */
227 bus_dmamap_sync(sc->src_dma_tag, sc->src_dma_map, BUS_DMASYNC_PREWRITE);
228 bus_dmamap_sync(sc->dst_dma_tag, sc->dst_dma_map, BUS_DMASYNC_PREWRITE);
231 for (i = 0; i < sc->len; i++) {
232 sc->src[i] = (i & 0xff);
236 sc->req.type = XR_TYPE_PHYS_ADDR;
237 sc->req.direction = XDMA_MEM_TO_MEM;
238 sc->req.src_addr = sc->src_phys;
239 sc->req.dst_addr = sc->dst_phys;
240 sc->req.src_width = 4;
241 sc->req.dst_width = 4;
242 sc->req.block_len = sc->len;
243 sc->req.block_num = 1;
245 err = xdma_request(sc->xchan, sc->src_phys, sc->dst_phys, sc->len);
247 device_printf(sc->dev, "Can't configure virtual channel.\n");
251 /* Start operation. */
252 xdma_begin(sc->xchan);
258 xdmatest_verify(struct xdmatest_softc *sc)
263 /* We have memory updated by DMA controller. */
264 bus_dmamap_sync(sc->src_dma_tag, sc->src_dma_map, BUS_DMASYNC_POSTREAD);
265 bus_dmamap_sync(sc->dst_dma_tag, sc->dst_dma_map, BUS_DMASYNC_POSTWRITE);
267 for (i = 0; i < sc->len; i++) {
268 if (sc->dst[i] != sc->src[i]) {
269 device_printf(sc->dev,
270 "%s: Test failed: iter %d\n", __func__, i);
275 err = xdma_channel_free(sc->xchan);
277 device_printf(sc->dev,
278 "%s: Test failed: can't deallocate channel.\n", __func__);
282 err = xdma_put(sc->xdma);
284 device_printf(sc->dev,
285 "%s: Test failed: can't deallocate xDMA.\n", __func__);
293 xdmatest_worker(void *arg)
295 struct xdmatest_softc *sc;
301 device_printf(sc->dev, "Worker %d started.\n",
302 device_get_unit(sc->dev));
309 if (xdmatest_test(sc) != 0) {
310 mtx_unlock(&sc->mtx);
311 device_printf(sc->dev,
312 "%s: Test failed.\n", __func__);
319 mtx_sleep(sc, &sc->mtx, 0, "xdmatest_wait", hz);
320 } while (timeout-- && sc->done == 0);
323 err = xdmatest_verify(sc);
325 /* Test succeeded. */
326 mtx_unlock(&sc->mtx);
331 mtx_unlock(&sc->mtx);
332 device_printf(sc->dev,
333 "%s: Test failed.\n", __func__);
339 xdmatest_delayed_attach(void *arg)
341 struct xdmatest_softc *sc;
345 if (kproc_create(xdmatest_worker, (void *)sc, &sc->newp, 0, 0,
346 "xdmatest_worker") != 0) {
347 device_printf(sc->dev,
348 "%s: Failed to create worker thread.\n", __func__);
351 config_intrhook_disestablish(&sc->config_intrhook);
355 xdmatest_probe(device_t dev)
358 if (!ofw_bus_status_okay(dev))
361 if (!ofw_bus_is_compatible(dev, "freebsd,xdma-test"))
364 device_set_desc(dev, "xDMA test driver");
366 return (BUS_PROBE_DEFAULT);
370 xdmatest_attach(device_t dev)
372 struct xdmatest_softc *sc;
375 sc = device_get_softc(dev);
378 mtx_init(&sc->mtx, device_get_nameunit(dev), "xdmatest", MTX_DEF);
380 /* Allocate test memory */
381 err = xdmatest_alloc_test_memory(sc);
383 device_printf(sc->dev, "Can't allocate test memory.\n");
387 /* We'll run test later, but before / mount. */
388 sc->config_intrhook.ich_func = xdmatest_delayed_attach;
389 sc->config_intrhook.ich_arg = sc;
390 if (config_intrhook_establish(&sc->config_intrhook) != 0)
391 device_printf(dev, "config_intrhook_establish failed\n");
397 xdmatest_detach(device_t dev)
399 struct xdmatest_softc *sc;
401 sc = device_get_softc(dev);
403 bus_dmamap_unload(sc->src_dma_tag, sc->src_dma_map);
404 bus_dmamem_free(sc->src_dma_tag, sc->src, sc->src_dma_map);
405 bus_dma_tag_destroy(sc->src_dma_tag);
407 bus_dmamap_unload(sc->dst_dma_tag, sc->dst_dma_map);
408 bus_dmamem_free(sc->dst_dma_tag, sc->dst, sc->dst_dma_map);
409 bus_dma_tag_destroy(sc->dst_dma_tag);
414 static device_method_t xdmatest_methods[] = {
415 /* Device interface */
416 DEVMETHOD(device_probe, xdmatest_probe),
417 DEVMETHOD(device_attach, xdmatest_attach),
418 DEVMETHOD(device_detach, xdmatest_detach),
423 static driver_t xdmatest_driver = {
426 sizeof(struct xdmatest_softc),
429 static devclass_t xdmatest_devclass;
431 DRIVER_MODULE(xdmatest, simplebus, xdmatest_driver, xdmatest_devclass, 0, 0);