OSDN Git Service

Merge branch 'nouveau-1'
[android-x86/external-libdrm.git] / bsd-core / drm_pci.c
1 /**
2  * \file drm_pci.h
3  * \brief PCI consistent, DMA-accessible memory functions.
4  *
5  * \author Eric Anholt <anholt@FreeBSD.org>
6  */
7
8 /*-
9  * Copyright 2003 Eric Anholt.
10  * All Rights Reserved.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the next
20  * paragraph) shall be included in all copies or substantial portions of the
21  * Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
26  * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29  */
30
31 #include "drmP.h"
32
33 /**********************************************************************/
34 /** \name PCI memory */
35 /*@{*/
36
37 #if defined(__FreeBSD__)
38 static void
39 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
40 {
41         drm_dma_handle_t *dmah = arg;
42
43         if (error != 0)
44                 return;
45
46         KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
47         dmah->busaddr = segs[0].ds_addr;
48 }
49 #endif
50
51 /**
52  * \brief Allocate a physically contiguous DMA-accessible consistent 
53  * memory block.
54  */
55 drm_dma_handle_t *
56 drm_pci_alloc(drm_device_t *dev, size_t size, size_t align, dma_addr_t maxaddr)
57 {
58         drm_dma_handle_t *dmah;
59         int ret;
60
61         /* Need power-of-two alignment, so fail the allocation if it isn't. */
62         if ((align & (align - 1)) != 0) {
63                 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
64                     (int)align);
65                 return NULL;
66         }
67
68         dmah = malloc(sizeof(drm_dma_handle_t), M_DRM, M_ZERO | M_NOWAIT);
69         if (dmah == NULL)
70                 return NULL;
71
72 #ifdef __FreeBSD__
73         ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
74             maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
75             NULL, NULL, /* filtfunc, filtfuncargs */
76             size, 1, size, /* maxsize, nsegs, maxsegsize */
77             BUS_DMA_ALLOCNOW, NULL, NULL, /* flags, lockfunc, lockfuncargs */
78             &dmah->tag);
79         if (ret != 0) {
80                 free(dmah, M_DRM);
81                 return NULL;
82         }
83
84         ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, BUS_DMA_NOWAIT,
85             &dmah->map);
86         if (ret != 0) {
87                 bus_dma_tag_destroy(dmah->tag);
88                 free(dmah, M_DRM);
89                 return NULL;
90         }
91
92         ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
93             drm_pci_busdma_callback, dmah, 0);
94         if (ret != 0) {
95                 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
96                 bus_dma_tag_destroy(dmah->tag);
97                 free(dmah, M_DRM);
98                 return NULL;
99         }
100 #elif defined(__NetBSD__)
101         ret = bus_dmamem_alloc(dev->dma_tag, size, align, PAGE_SIZE,
102             &dmah->seg, 1, &nsegs, BUS_DMA_NOWAIT);
103         if ((ret != 0) || (nsegs != 1)) {
104                 free(dmah, M_DRM);
105                 return NULL;
106         }
107
108         ret = bus_dmamem_map(dev->dma_tag, &dmah->seg, 1, size, &dmah->addr,
109             BUS_DMA_NOWAIT);
110         if (ret != 0) {
111                 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
112                 free(dmah, M_DRM);
113                 return NULL;
114         }
115
116         dmah->dmaaddr = h->seg.ds_addr;
117 #endif
118
119         return dmah;
120 }
121
122 /**
123  * \brief Free a DMA-accessible consistent memory block.
124  */
125 void
126 drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah)
127 {
128         if (dmah == NULL)
129                 return;
130
131 #if defined(__FreeBSD__)
132         bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
133         bus_dma_tag_destroy(dmah->tag);
134 #elif defined(__NetBSD__)
135         bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
136 #endif
137
138         free(dmah, M_DRM);
139 }
140
141 /*@}*/