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