136108Sjb/*- 250472Speter * Copyright (c) 2009 Robert C. Noland III <rnoland@FreeBSD.org> 336108Sjb * All Rights Reserved. 436108Sjb * 543818Swes * Permission is hereby granted, free of charge, to any person obtaining a 643818Swes * copy of this software and associated documentation files (the "Software"), 750177Shoek * to deal in the Software without restriction, including without limitation 843818Swes * the rights to use, copy, modify, merge, publish, distribute, sublicense, 943818Swes * and/or sell copies of the Software, and to permit persons to whom the 1043818Swes * Software is furnished to do so, subject to the following conditions: 1143818Swes * 1243818Swes * The above copyright notice and this permission notice (including the next 1350177Shoek * paragraph) shall be included in all copies or substantial portions of the 1443818Swes * Software. 1543818Swes * 1643818Swes * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1750177Shoek * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18199250Sed * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19220154Sed * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20220154Sed * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 2143818Swes * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 2243818Swes * DEALINGS IN THE SOFTWARE. 2350177Shoek */ 2450177Shoek 2543818Swes#include <sys/cdefs.h> 2636108Sjb__FBSDID("$FreeBSD$"); 2736108Sjb 2850177Shoek/** @file drm_scatter.c 2950177Shoek * Allocation of memory for scatter-gather mappings by the graphics chip. 3056805Sobrien * The memory allocated here is then made into an aperture in the card 3136108Sjb * by mapping the pages into the GART. 32199250Sed */ 3336108Sjb 34199250Sed#include <dev/drm2/drmP.h> 35199250Sed 36199250Sedint 37199250Seddrm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather *request) 38199250Sed{ 39199250Sed struct drm_sg_mem *entry; 40199250Sed vm_size_t size; 41170088Sdougb vm_pindex_t pindex; 4236108Sjb 4336108Sjb if (dev->sg) 44267243Snwhitehorn return EINVAL; 45180487Sed 46180487Sed DRM_DEBUG("request size=%ld\n", request->size); 47180487Sed 48121468Ssimokawa entry = malloc(sizeof(*entry), DRM_MEM_DRIVER, M_WAITOK | M_ZERO); 49121468Ssimokawa 50 size = round_page(request->size); 51 entry->pages = OFF_TO_IDX(size); 52 entry->busaddr = malloc(entry->pages * sizeof(*entry->busaddr), 53 DRM_MEM_SGLISTS, M_WAITOK | M_ZERO); 54 55 entry->vaddr = kmem_alloc_attr(kernel_arena, size, M_WAITOK | M_ZERO, 56 0, BUS_SPACE_MAXADDR_32BIT, VM_MEMATTR_WRITE_COMBINING); 57 if (entry->vaddr == 0) { 58 drm_sg_cleanup(entry); 59 return (ENOMEM); 60 } 61 62 for(pindex = 0; pindex < entry->pages; pindex++) { 63 entry->busaddr[pindex] = 64 vtophys(entry->vaddr + IDX_TO_OFF(pindex)); 65 } 66 67 DRM_LOCK(dev); 68 if (dev->sg) { 69 DRM_UNLOCK(dev); 70 drm_sg_cleanup(entry); 71 return (EINVAL); 72 } 73 dev->sg = entry; 74 DRM_UNLOCK(dev); 75 76 request->handle = entry->vaddr; 77 78 DRM_DEBUG("allocated %ju pages @ 0x%08zx, contents=%08lx\n", 79 entry->pages, entry->vaddr, *(unsigned long *)entry->vaddr); 80 81 return (0); 82} 83 84int 85drm_sg_alloc_ioctl(struct drm_device *dev, void *data, 86 struct drm_file *file_priv) 87{ 88 struct drm_scatter_gather *request = data; 89 90 DRM_DEBUG("\n"); 91 92 return (drm_sg_alloc(dev, request)); 93} 94 95void 96drm_sg_cleanup(struct drm_sg_mem *entry) 97{ 98 if (entry == NULL) 99 return; 100 101 if (entry->vaddr != 0) 102 kmem_free(kernel_arena, entry->vaddr, IDX_TO_OFF(entry->pages)); 103 104 free(entry->busaddr, DRM_MEM_SGLISTS); 105 free(entry, DRM_MEM_DRIVER); 106 107 return; 108} 109 110int 111drm_sg_free(struct drm_device *dev, void *data, struct drm_file *file_priv) 112{ 113 struct drm_scatter_gather *request = data; 114 struct drm_sg_mem *entry; 115 116 DRM_LOCK(dev); 117 entry = dev->sg; 118 dev->sg = NULL; 119 DRM_UNLOCK(dev); 120 121 if (!entry || entry->vaddr != request->handle) 122 return (EINVAL); 123 124 DRM_DEBUG("free 0x%zx\n", entry->vaddr); 125 126 drm_sg_cleanup(entry); 127 128 return (0); 129} 130