Sync DRM code with FreeBSD trunk rev 190433.
[dragonfly.git] / sys / dev / drm / drm_pci.c
blobce7ed369b7edca8438a364fb3db718e78e112204
1 /*-
2 * Copyright 2003 Eric Anholt.
3 * All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 /**
25 * \file drm_pci.h
26 * \brief PCI consistent, DMA-accessible memory allocation.
28 * \author Eric Anholt <anholt@FreeBSD.org>
31 #include "dev/drm/drmP.h"
33 /**********************************************************************/
34 /** \name PCI memory */
35 /*@{*/
37 static void
38 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
40 drm_dma_handle_t *dmah = arg;
42 if (error != 0)
43 return;
45 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
46 dmah->busaddr = segs[0].ds_addr;
49 /**
50 * \brief Allocate a physically contiguous DMA-accessible consistent
51 * memory block.
53 drm_dma_handle_t *
54 drm_pci_alloc(struct drm_device *dev, size_t size,
55 size_t align, dma_addr_t maxaddr)
57 drm_dma_handle_t *dmah;
58 int ret;
60 /* Need power-of-two alignment, so fail the allocation if it isn't. */
61 if ((align & (align - 1)) != 0) {
62 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
63 (int)align);
64 return NULL;
67 dmah = malloc(sizeof(drm_dma_handle_t), DRM_MEM_DMA, M_ZERO | M_NOWAIT);
68 if (dmah == NULL)
69 return NULL;
71 #if 0 /* HT XXX XXX XXX */
72 /* Make sure we aren't holding locks here */
73 mtx_assert(&dev->dev_lock, MA_NOTOWNED);
74 if (mtx_owned(&dev->dev_lock))
75 DRM_ERROR("called while holding dev_lock\n");
76 mtx_assert(&dev->dma_lock, MA_NOTOWNED);
77 if (mtx_owned(&dev->dma_lock))
78 DRM_ERROR("called while holding dma_lock\n");
79 #endif
81 ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
82 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
83 NULL, NULL, /* filtfunc, filtfuncargs */
84 size, 1, size, /* maxsize, nsegs, maxsegsize */
85 0, /* flags */
86 &dmah->tag);
87 if (ret != 0) {
88 free(dmah, DRM_MEM_DMA);
89 return NULL;
92 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
93 BUS_DMA_WAITOK | BUS_DMA_ZERO, &dmah->map);
94 if (ret != 0) {
95 bus_dma_tag_destroy(dmah->tag);
96 free(dmah, DRM_MEM_DMA);
97 return NULL;
100 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
101 drm_pci_busdma_callback, dmah, BUS_DMA_NOWAIT);
102 if (ret != 0) {
103 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
104 bus_dma_tag_destroy(dmah->tag);
105 free(dmah, DRM_MEM_DMA);
106 return NULL;
109 return dmah;
113 * \brief Free a DMA-accessible consistent memory block.
115 void
116 drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah)
118 if (dmah == NULL)
119 return;
121 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
122 bus_dma_tag_destroy(dmah->tag);
124 free(dmah, DRM_MEM_DMA);
127 /*@}*/