1 /* $NetBSD: drm_scatter.c,v 1.5 2018/08/27 07:02:06 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Taylor R. Campbell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: drm_scatter.c,v 1.5 2018/08/27 07:02:06 riastradh Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/bus.h> 37 #include <sys/errno.h> 38 #include <sys/systm.h> 39 40 #include <linux/slab.h> 41 42 #include <drm/drmP.h> 43 #include <drm/drm_internal.h> 44 #include "../dist/drm/drm_legacy.h" 45 46 static int drm_sg_alloc_mem(struct drm_device *, size_t, 47 struct drm_sg_mem **); 48 static void drm_sg_free_mem(struct drm_device *, struct drm_sg_mem *); 49 50 int 51 drm_legacy_sg_alloc(struct drm_device *dev, void *data, 52 struct drm_file *file __unused) 53 { 54 struct drm_scatter_gather *const request = data; 55 struct drm_sg_mem *sg = NULL; 56 int error; 57 58 /* 59 * XXX Should not hold this mutex during drm_sg_alloc. For 60 * now, we'll just use non-blocking allocations. 61 */ 62 KASSERT(mutex_is_locked(&drm_global_mutex)); 63 64 /* 65 * Sanity-check the inputs. 66 */ 67 if (!drm_core_check_feature(dev, DRIVER_SG)) 68 return -ENODEV; 69 if (dev->sg != NULL) 70 return -EBUSY; 71 if (request->size > 0xffffffffUL) 72 return -ENOMEM; 73 74 /* 75 * Do the allocation. 76 * 77 * XXX Would it be safe to drop drm_global_mutex here to avoid 78 * holding it during allocation? 79 */ 80 error = drm_sg_alloc_mem(dev, request->size, &sg); 81 if (error) 82 return error; 83 84 /* Set it up to be the device's scatter-gather memory. */ 85 dev->sg = sg; 86 87 /* Success! */ 88 request->handle = sg->handle; 89 return 0; 90 } 91 92 int 93 drm_legacy_sg_free(struct drm_device *dev, void *data, struct drm_file *file) 94 { 95 struct drm_scatter_gather *const request = data; 96 struct drm_sg_mem *sg; 97 98 KASSERT(mutex_is_locked(&drm_global_mutex)); 99 100 /* 101 * Sanity-check the inputs. 102 */ 103 if (!drm_core_check_feature(dev, DRIVER_SG)) 104 return -ENODEV; 105 106 sg = dev->sg; 107 if (sg == NULL) 108 return -ENXIO; 109 if (sg->handle != request->handle) 110 return -EINVAL; 111 112 /* Remove dev->sg. */ 113 dev->sg = NULL; 114 115 /* Free it. */ 116 drm_sg_free_mem(dev, sg); 117 118 /* Success! */ 119 return 0; 120 } 121 122 void 123 drm_legacy_sg_cleanup(struct drm_device *dev) 124 { 125 126 if (!drm_core_check_feature(dev, DRIVER_SG)) 127 return; 128 if (dev->sg == NULL) 129 return; 130 if (drm_core_check_feature(dev, DRIVER_MODESET)) 131 return; 132 133 drm_sg_free_mem(dev, dev->sg); 134 } 135 136 static int 137 drm_sg_alloc_mem(struct drm_device *dev, size_t size, struct drm_sg_mem **sgp) 138 { 139 int nsegs; 140 int error; 141 142 KASSERT(drm_core_check_feature(dev, DRIVER_SG)); 143 144 KASSERT(size <= (size_t)0xffffffffUL); /* XXX 32-bit sizes only? */ 145 const size_t nbytes = PAGE_ALIGN(size); 146 const size_t npages = nbytes >> PAGE_SHIFT; 147 KASSERT(npages <= (size_t)INT_MAX); 148 149 /* 150 * Allocate a drm_sg_mem record. 151 */ 152 struct drm_sg_mem *const sg = 153 kmem_zalloc(offsetof(struct drm_sg_mem, sg_segs[npages]), 154 KM_NOSLEEP); 155 if (sg == NULL) 156 return -ENOMEM; 157 sg->sg_tag = dev->dmat; 158 sg->sg_nsegs_max = (unsigned int)npages; 159 160 /* 161 * Allocate the requested amount of DMA-safe memory. 162 */ 163 KASSERT(sg->sg_nsegs_max <= (unsigned int)INT_MAX); 164 /* XXX errno NetBSD->Linux */ 165 error = -bus_dmamem_alloc(sg->sg_tag, nbytes, PAGE_SIZE, 0, 166 sg->sg_segs, (int)sg->sg_nsegs_max, &nsegs, BUS_DMA_NOWAIT); 167 if (error) 168 goto fail0; 169 KASSERT(0 <= nsegs); 170 sg->sg_nsegs = (unsigned int)nsegs; 171 172 /* 173 * XXX Old drm passed DRM_BUS_NOWAIT below but BUS_DMA_WAITOK 174 * above. WTF? 175 */ 176 177 /* 178 * Map the DMA-safe memory into kernel virtual address space. 179 */ 180 /* XXX errno NetBSD->Linux */ 181 error = -bus_dmamem_map(sg->sg_tag, sg->sg_segs, nsegs, nbytes, 182 &sg->virtual, 183 (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE)); 184 if (error) 185 goto fail1; 186 sg->sg_size = nbytes; 187 188 /* 189 * Create a map for DMA transfers. 190 */ 191 /* XXX errno NetBSD->Linux */ 192 error = -bus_dmamap_create(sg->sg_tag, nbytes, nsegs, nbytes, 0, 193 BUS_DMA_NOWAIT, &sg->sg_map); 194 if (error) 195 goto fail2; 196 197 /* 198 * Load the kva buffer into the map for DMA transfers. 199 */ 200 /* XXX errno NetBSD->Linux */ 201 error = -bus_dmamap_load(sg->sg_tag, sg->sg_map, sg->virtual, nbytes, 202 NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE)); 203 if (error) 204 goto fail3; 205 206 /* 207 * Use the kernel virtual address as the userland handle. 208 * 209 * XXX This leaks some information about kernel virtual 210 * addresses to userland; should we use an idr instead? 211 */ 212 sg->handle = (unsigned long)(uintptr_t)sg->virtual; 213 KASSERT(sg->virtual == (void *)(uintptr_t)sg->handle); 214 215 /* Success! */ 216 *sgp = sg; 217 return 0; 218 219 fail3: bus_dmamap_destroy(sg->sg_tag, sg->sg_map); 220 fail2: bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size); 221 fail1: KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX); 222 bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs); 223 fail0: sg->sg_tag = NULL; /* XXX paranoia */ 224 kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max])); 225 return error; 226 } 227 228 static void 229 drm_sg_free_mem(struct drm_device *dev, struct drm_sg_mem *sg) 230 { 231 232 bus_dmamap_unload(sg->sg_tag, sg->sg_map); 233 bus_dmamap_destroy(sg->sg_tag, sg->sg_map); 234 bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size); 235 KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX); 236 bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs); 237 sg->sg_tag = NULL; /* XXX paranoia */ 238 kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max])); 239 } 240