xref: /dflybsd-src/sys/dev/virtual/hyperv/hyperv_busdma.c (revision 82b778547fb437620f0453271b4db5c69a9980b4)
1*82b77854SSepherosa Ziehau /*-
2*82b77854SSepherosa Ziehau  * Copyright (c) 2016 Microsoft Corp.
3*82b77854SSepherosa Ziehau  * All rights reserved.
4*82b77854SSepherosa Ziehau  *
5*82b77854SSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
6*82b77854SSepherosa Ziehau  * modification, are permitted provided that the following conditions
7*82b77854SSepherosa Ziehau  * are met:
8*82b77854SSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
9*82b77854SSepherosa Ziehau  *    notice unmodified, this list of conditions, and the following
10*82b77854SSepherosa Ziehau  *    disclaimer.
11*82b77854SSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
12*82b77854SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in the
13*82b77854SSepherosa Ziehau  *    documentation and/or other materials provided with the distribution.
14*82b77854SSepherosa Ziehau  *
15*82b77854SSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*82b77854SSepherosa Ziehau  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*82b77854SSepherosa Ziehau  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*82b77854SSepherosa Ziehau  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*82b77854SSepherosa Ziehau  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*82b77854SSepherosa Ziehau  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*82b77854SSepherosa Ziehau  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*82b77854SSepherosa Ziehau  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*82b77854SSepherosa Ziehau  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*82b77854SSepherosa Ziehau  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*82b77854SSepherosa Ziehau  */
26*82b77854SSepherosa Ziehau 
27*82b77854SSepherosa Ziehau #include <sys/param.h>
28*82b77854SSepherosa Ziehau #include <sys/systm.h>
29*82b77854SSepherosa Ziehau #include <sys/bus.h>
30*82b77854SSepherosa Ziehau 
31*82b77854SSepherosa Ziehau #include <dev/virtual/hyperv/hyperv_busdma.h>
32*82b77854SSepherosa Ziehau 
33*82b77854SSepherosa Ziehau void
hyperv_dma_map_paddr(void * arg,bus_dma_segment_t * segs,int nseg,int error)34*82b77854SSepherosa Ziehau hyperv_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
35*82b77854SSepherosa Ziehau {
36*82b77854SSepherosa Ziehau 	bus_addr_t *paddr = arg;
37*82b77854SSepherosa Ziehau 
38*82b77854SSepherosa Ziehau 	if (error)
39*82b77854SSepherosa Ziehau 		return;
40*82b77854SSepherosa Ziehau 
41*82b77854SSepherosa Ziehau 	KASSERT(nseg == 1, ("too many segments %d!", nseg));
42*82b77854SSepherosa Ziehau 	*paddr = segs->ds_addr;
43*82b77854SSepherosa Ziehau }
44*82b77854SSepherosa Ziehau 
45*82b77854SSepherosa Ziehau void *
hyperv_dmamem_alloc(bus_dma_tag_t parent_dtag,bus_size_t alignment,bus_addr_t boundary,bus_size_t size,struct hyperv_dma * dma,int flags)46*82b77854SSepherosa Ziehau hyperv_dmamem_alloc(bus_dma_tag_t parent_dtag, bus_size_t alignment,
47*82b77854SSepherosa Ziehau     bus_addr_t boundary, bus_size_t size, struct hyperv_dma *dma, int flags)
48*82b77854SSepherosa Ziehau {
49*82b77854SSepherosa Ziehau 	bus_dmamem_t dmem;
50*82b77854SSepherosa Ziehau 	int error;
51*82b77854SSepherosa Ziehau 
52*82b77854SSepherosa Ziehau 	error = bus_dmamem_coherent(parent_dtag, alignment, boundary,
53*82b77854SSepherosa Ziehau 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, size, flags, &dmem);
54*82b77854SSepherosa Ziehau 	if (error)
55*82b77854SSepherosa Ziehau 		return NULL;
56*82b77854SSepherosa Ziehau 
57*82b77854SSepherosa Ziehau 	dma->hv_dtag = dmem.dmem_tag;
58*82b77854SSepherosa Ziehau 	dma->hv_dmap = dmem.dmem_map;
59*82b77854SSepherosa Ziehau 	dma->hv_paddr = dmem.dmem_busaddr;
60*82b77854SSepherosa Ziehau 	return dmem.dmem_addr;
61*82b77854SSepherosa Ziehau }
62*82b77854SSepherosa Ziehau 
63*82b77854SSepherosa Ziehau void
hyperv_dmamem_free(struct hyperv_dma * dma,void * ptr)64*82b77854SSepherosa Ziehau hyperv_dmamem_free(struct hyperv_dma *dma, void *ptr)
65*82b77854SSepherosa Ziehau {
66*82b77854SSepherosa Ziehau 	bus_dmamap_unload(dma->hv_dtag, dma->hv_dmap);
67*82b77854SSepherosa Ziehau 	bus_dmamem_free(dma->hv_dtag, ptr, dma->hv_dmap);
68*82b77854SSepherosa Ziehau 	bus_dma_tag_destroy(dma->hv_dtag);
69*82b77854SSepherosa Ziehau }
70