2 * Copyright 2003 Eric Anholt.
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
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.
23 * $FreeBSD: src/sys/dev/drm2/drm_pci.c,v 1.1 2012/05/22 11:07:44 kib Exp $
28 * \brief PCI consistent, DMA-accessible memory allocation.
30 * \author Eric Anholt <anholt@FreeBSD.org>
33 #include <dev/drm2/drmP.h>
35 /**********************************************************************/
36 /** \name PCI memory */
40 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
42 drm_dma_handle_t *dmah = arg;
47 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
48 dmah->busaddr = segs[0].ds_addr;
52 * \brief Allocate a physically contiguous DMA-accessible consistent
56 drm_pci_alloc(struct drm_device *dev, size_t size,
57 size_t align, dma_addr_t maxaddr)
59 drm_dma_handle_t *dmah;
62 /* Need power-of-two alignment, so fail the allocation if it isn't. */
63 if ((align & (align - 1)) != 0) {
64 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
69 dmah = kmalloc(sizeof(drm_dma_handle_t), DRM_MEM_DMA, M_ZERO | M_NOWAIT);
73 /* Make sure we aren't holding mutexes here */
74 /* FIXME: Is it ok to remove the next 3 lines ? */
75 /* mtx_assert(&dev->dma_lock, MA_NOTOWNED);
76 if (mtx_owned(&dev->dma_lock))
77 DRM_ERROR("called while holding dma_lock\n"); */
79 ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
80 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
81 NULL, NULL, /* filtfunc, filtfuncargs */
82 size, 1, size, /* maxsize, nsegs, maxsegsize */
86 free(dmah, DRM_MEM_DMA);
90 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
91 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
93 bus_dma_tag_destroy(dmah->tag);
94 free(dmah, DRM_MEM_DMA);
98 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
99 drm_pci_busdma_callback, dmah, BUS_DMA_NOWAIT);
101 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
102 bus_dma_tag_destroy(dmah->tag);
103 free(dmah, DRM_MEM_DMA);
111 * \brief Free a DMA-accessible consistent memory block.
114 drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah)
119 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
120 bus_dma_tag_destroy(dmah->tag);
122 free(dmah, DRM_MEM_DMA);