1300478Ssephe/*-
2300478Ssephe * Copyright (c) 2016 Microsoft Corp.
3300478Ssephe * All rights reserved.
4300478Ssephe *
5300478Ssephe * Redistribution and use in source and binary forms, with or without
6300478Ssephe * modification, are permitted provided that the following conditions
7300478Ssephe * are met:
8300478Ssephe * 1. Redistributions of source code must retain the above copyright
9300478Ssephe *    notice unmodified, this list of conditions, and the following
10300478Ssephe *    disclaimer.
11300478Ssephe * 2. Redistributions in binary form must reproduce the above copyright
12300478Ssephe *    notice, this list of conditions and the following disclaimer in the
13300478Ssephe *    documentation and/or other materials provided with the distribution.
14300478Ssephe *
15300478Ssephe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16300478Ssephe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17300478Ssephe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18300478Ssephe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19300478Ssephe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20300478Ssephe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21300478Ssephe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22300478Ssephe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23300478Ssephe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24300478Ssephe * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25300478Ssephe */
26300478Ssephe
27300478Ssephe#include <sys/cdefs.h>
28300478Ssephe__FBSDID("$FreeBSD$");
29300478Ssephe
30300478Ssephe#include <sys/param.h>
31300478Ssephe#include <sys/systm.h>
32300478Ssephe#include <sys/bus.h>
33300478Ssephe
34300478Ssephe#include <machine/bus.h>
35300478Ssephe
36300478Ssephe#include <dev/hyperv/include/hyperv_busdma.h>
37300478Ssephe
38300568Ssephe#define HYPERV_DMA_MASK	(BUS_DMA_WAITOK | BUS_DMA_NOWAIT | BUS_DMA_ZERO)
39300478Ssephe
40300478Ssephevoid
41300478Ssephehyperv_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
42300478Ssephe{
43300478Ssephe	bus_addr_t *paddr = arg;
44300478Ssephe
45300478Ssephe	if (error)
46300478Ssephe		return;
47300478Ssephe
48300478Ssephe	KASSERT(nseg == 1, ("too many segments %d!", nseg));
49300478Ssephe	*paddr = segs->ds_addr;
50300478Ssephe}
51300478Ssephe
52300478Ssephevoid *
53300478Ssephehyperv_dmamem_alloc(bus_dma_tag_t parent_dtag, bus_size_t alignment,
54300478Ssephe    bus_addr_t boundary, bus_size_t size, struct hyperv_dma *dma, int flags)
55300478Ssephe{
56300478Ssephe	void *ret;
57300478Ssephe	int error;
58300478Ssephe
59300478Ssephe	error = bus_dma_tag_create(parent_dtag, /* parent */
60300478Ssephe	    alignment,		/* alignment */
61300478Ssephe	    boundary,		/* boundary */
62300478Ssephe	    BUS_SPACE_MAXADDR,	/* lowaddr */
63300478Ssephe	    BUS_SPACE_MAXADDR,	/* highaddr */
64300478Ssephe	    NULL, NULL,		/* filter, filterarg */
65300478Ssephe	    size,		/* maxsize */
66300478Ssephe	    1,			/* nsegments */
67300478Ssephe	    size,		/* maxsegsize */
68300478Ssephe	    0,			/* flags */
69300478Ssephe	    NULL,		/* lockfunc */
70300478Ssephe	    NULL,		/* lockfuncarg */
71300478Ssephe	    &dma->hv_dtag);
72300478Ssephe	if (error)
73300478Ssephe		return NULL;
74300478Ssephe
75300478Ssephe	error = bus_dmamem_alloc(dma->hv_dtag, &ret,
76300568Ssephe	    (flags & HYPERV_DMA_MASK) | BUS_DMA_COHERENT, &dma->hv_dmap);
77300478Ssephe	if (error) {
78300478Ssephe		bus_dma_tag_destroy(dma->hv_dtag);
79300478Ssephe		return NULL;
80300478Ssephe	}
81300478Ssephe
82300478Ssephe	error = bus_dmamap_load(dma->hv_dtag, dma->hv_dmap, ret, size,
83300478Ssephe	    hyperv_dma_map_paddr, &dma->hv_paddr, BUS_DMA_NOWAIT);
84300478Ssephe	if (error) {
85300478Ssephe		bus_dmamem_free(dma->hv_dtag, ret, dma->hv_dmap);
86300478Ssephe		bus_dma_tag_destroy(dma->hv_dtag);
87300478Ssephe		return NULL;
88300478Ssephe	}
89300478Ssephe	return ret;
90300478Ssephe}
91300478Ssephe
92300478Ssephevoid
93300478Ssephehyperv_dmamem_free(struct hyperv_dma *dma, void *ptr)
94300478Ssephe{
95300478Ssephe	bus_dmamap_unload(dma->hv_dtag, dma->hv_dmap);
96300478Ssephe	bus_dmamem_free(dma->hv_dtag, ptr, dma->hv_dmap);
97300478Ssephe	bus_dma_tag_destroy(dma->hv_dtag);
98300478Ssephe}
99