]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_pci.c
This commit was generated by cvs2svn to compensate for changes in r172314,
[FreeBSD/FreeBSD.git] / sys / dev / drm / 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 <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "dev/drm/drmP.h"
35
36 /**********************************************************************/
37 /** \name PCI memory */
38 /*@{*/
39
40 #if defined(__FreeBSD__)
41 static void
42 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
43 {
44         drm_dma_handle_t *dmah = arg;
45
46         if (error != 0)
47                 return;
48
49         KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
50         dmah->busaddr = segs[0].ds_addr;
51 }
52 #endif
53
54 /**
55  * \brief Allocate a physically contiguous DMA-accessible consistent 
56  * memory block.
57  */
58 drm_dma_handle_t *
59 drm_pci_alloc(drm_device_t *dev, size_t size, size_t align, dma_addr_t maxaddr)
60 {
61         drm_dma_handle_t *dmah;
62         int ret;
63
64         /* Need power-of-two alignment, so fail the allocation if it isn't. */
65         if ((align & (align - 1)) != 0) {
66                 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
67                     (int)align);
68                 return NULL;
69         }
70
71         dmah = malloc(sizeof(drm_dma_handle_t), M_DRM, M_ZERO | M_NOWAIT);
72         if (dmah == NULL)
73                 return NULL;
74
75 #ifdef __FreeBSD__
76         ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
77             maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
78             NULL, NULL, /* filtfunc, filtfuncargs */
79             size, 1, size, /* maxsize, nsegs, maxsegsize */
80             BUS_DMA_ALLOCNOW, NULL, NULL, /* flags, lockfunc, lockfuncargs */
81             &dmah->tag);
82         if (ret != 0) {
83                 free(dmah, M_DRM);
84                 return NULL;
85         }
86
87         ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, BUS_DMA_NOWAIT,
88             &dmah->map);
89         if (ret != 0) {
90                 bus_dma_tag_destroy(dmah->tag);
91                 free(dmah, M_DRM);
92                 return NULL;
93         }
94
95         ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
96             drm_pci_busdma_callback, dmah, 0);
97         if (ret != 0) {
98                 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
99                 bus_dma_tag_destroy(dmah->tag);
100                 free(dmah, M_DRM);
101                 return NULL;
102         }
103 #elif defined(__NetBSD__)
104         ret = bus_dmamem_alloc(dev->dma_tag, size, align, PAGE_SIZE,
105             &dmah->seg, 1, &nsegs, BUS_DMA_NOWAIT);
106         if ((ret != 0) || (nsegs != 1)) {
107                 free(dmah, M_DRM);
108                 return NULL;
109         }
110
111         ret = bus_dmamem_map(dev->dma_tag, &dmah->seg, 1, size, &dmah->addr,
112             BUS_DMA_NOWAIT);
113         if (ret != 0) {
114                 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
115                 free(dmah, M_DRM);
116                 return NULL;
117         }
118
119         dmah->dmaaddr = h->seg.ds_addr;
120 #endif
121
122         return dmah;
123 }
124
125 /**
126  * \brief Free a DMA-accessible consistent memory block.
127  */
128 void
129 drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah)
130 {
131         if (dmah == NULL)
132                 return;
133
134 #if defined(__FreeBSD__)
135         bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
136         bus_dma_tag_destroy(dmah->tag);
137 #elif defined(__NetBSD__)
138         bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
139 #endif
140
141         free(dmah, M_DRM);
142 }
143
144 /*@}*/