Home | History | Annotate | Line # | Download | only in drm
drm_scatter.c revision 1.7
      1 /*	$NetBSD: drm_scatter.c,v 1.7 2021/12/19 09:50:42 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.7 2021/12/19 09:50:42 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_drv.h>
     44 
     45 #include "../dist/drm/drm_internal.h"
     46 #include "../dist/drm/drm_legacy.h"
     47 
     48 #if IS_ENABLED(CONFIG_DRM_LEGACY)
     49 static int	drm_sg_alloc_mem(struct drm_device *, size_t,
     50 		    struct drm_sg_mem **);
     51 static void	drm_sg_free_mem(struct drm_device *, struct drm_sg_mem *);
     52 
     53 int
     54 drm_legacy_sg_alloc(struct drm_device *dev, void *data,
     55     struct drm_file *file __unused)
     56 {
     57 	struct drm_scatter_gather *const request = data;
     58 	struct drm_sg_mem *sg = NULL;
     59 	int error;
     60 
     61 	/*
     62 	 * XXX Should not hold this mutex during drm_sg_alloc.  For
     63 	 * now, we'll just use non-blocking allocations.
     64 	 */
     65 	KASSERT(mutex_is_locked(&drm_global_mutex));
     66 
     67 	/*
     68 	 * Sanity-check the inputs.
     69 	 */
     70 	if (!drm_core_check_feature(dev, DRIVER_SG))
     71 		return -ENODEV;
     72 	if (dev->sg != NULL)
     73 		return -EBUSY;
     74 	if (request->size > 0xffffffffUL)
     75 		return -ENOMEM;
     76 
     77 	/*
     78 	 * Do the allocation.
     79 	 *
     80 	 * XXX Would it be safe to drop drm_global_mutex here to avoid
     81 	 * holding it during allocation?
     82 	 */
     83 	error = drm_sg_alloc_mem(dev, request->size, &sg);
     84 	if (error)
     85 		return error;
     86 
     87 	/* Set it up to be the device's scatter-gather memory.  */
     88 	dev->sg = sg;
     89 
     90 	/* Success!  */
     91 	request->handle = sg->handle;
     92 	return 0;
     93 }
     94 
     95 int
     96 drm_legacy_sg_free(struct drm_device *dev, void *data, struct drm_file *file)
     97 {
     98 	struct drm_scatter_gather *const request = data;
     99 	struct drm_sg_mem *sg;
    100 
    101 	KASSERT(mutex_is_locked(&drm_global_mutex));
    102 
    103 	/*
    104 	 * Sanity-check the inputs.
    105 	 */
    106 	if (!drm_core_check_feature(dev, DRIVER_SG))
    107 		return -ENODEV;
    108 
    109 	sg = dev->sg;
    110 	if (sg == NULL)
    111 		return -ENXIO;
    112 	if (sg->handle != request->handle)
    113 		return -EINVAL;
    114 
    115 	/* Remove dev->sg.  */
    116 	dev->sg = NULL;
    117 
    118 	/* Free it.  */
    119 	drm_sg_free_mem(dev, sg);
    120 
    121 	/* Success!  */
    122 	return 0;
    123 }
    124 
    125 void
    126 drm_legacy_sg_cleanup(struct drm_device *dev)
    127 {
    128 
    129 	if (!drm_core_check_feature(dev, DRIVER_SG))
    130 		return;
    131 	if (dev->sg == NULL)
    132 		return;
    133 	if (drm_core_check_feature(dev, DRIVER_MODESET))
    134 		return;
    135 
    136 	drm_sg_free_mem(dev, dev->sg);
    137 }
    138 
    139 static int
    140 drm_sg_alloc_mem(struct drm_device *dev, size_t size, struct drm_sg_mem **sgp)
    141 {
    142 	int nsegs;
    143 	int error;
    144 
    145 	KASSERT(drm_core_check_feature(dev, DRIVER_SG));
    146 
    147 	KASSERT(size <= (size_t)0xffffffffUL); /* XXX 32-bit sizes only?  */
    148 	const size_t nbytes = PAGE_ALIGN(size);
    149 	const size_t npages = nbytes >> PAGE_SHIFT;
    150 	KASSERT(npages <= (size_t)INT_MAX);
    151 
    152 	/*
    153 	 * Allocate a drm_sg_mem record.
    154 	 */
    155 	struct drm_sg_mem *const sg =
    156 	    kmem_zalloc(offsetof(struct drm_sg_mem, sg_segs[npages]),
    157 		KM_NOSLEEP);
    158 	if (sg == NULL)
    159 		return -ENOMEM;
    160 	sg->sg_tag = dev->dmat;
    161 	sg->sg_nsegs_max = (unsigned int)npages;
    162 
    163 	/*
    164 	 * Allocate the requested amount of DMA-safe memory.
    165 	 */
    166 	KASSERT(sg->sg_nsegs_max <= (unsigned int)INT_MAX);
    167 	/* XXX errno NetBSD->Linux */
    168 	error = -bus_dmamem_alloc(sg->sg_tag, nbytes, PAGE_SIZE, 0,
    169 	    sg->sg_segs, (int)sg->sg_nsegs_max, &nsegs, BUS_DMA_NOWAIT);
    170 	if (error)
    171 		goto fail0;
    172 	KASSERT(0 <= nsegs);
    173 	sg->sg_nsegs = (unsigned int)nsegs;
    174 
    175 	/*
    176 	 * XXX Old drm passed DRM_BUS_NOWAIT below but BUS_DMA_WAITOK
    177 	 * above.  WTF?
    178 	 */
    179 
    180 	/*
    181 	 * Map the DMA-safe memory into kernel virtual address space.
    182 	 */
    183 	/* XXX errno NetBSD->Linux */
    184 	error = -bus_dmamem_map(sg->sg_tag, sg->sg_segs, nsegs, nbytes,
    185 	    &sg->virtual,
    186 	    (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
    187 	if (error)
    188 		goto fail1;
    189 	sg->sg_size = nbytes;
    190 
    191 	/*
    192 	 * Create a map for DMA transfers.
    193 	 */
    194 	/* XXX errno NetBSD->Linux */
    195 	error = -bus_dmamap_create(sg->sg_tag, nbytes, nsegs, nbytes, 0,
    196 	    BUS_DMA_NOWAIT, &sg->sg_map);
    197 	if (error)
    198 		goto fail2;
    199 
    200 	/*
    201 	 * Load the kva buffer into the map for DMA transfers.
    202 	 */
    203 	/* XXX errno NetBSD->Linux */
    204 	error = -bus_dmamap_load(sg->sg_tag, sg->sg_map, sg->virtual, nbytes,
    205 	    NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
    206 	if (error)
    207 		goto fail3;
    208 
    209 	/*
    210 	 * Use the kernel virtual address as the userland handle.
    211 	 *
    212 	 * XXX This leaks some information about kernel virtual
    213 	 * addresses to userland; should we use an idr instead?
    214 	 */
    215 	sg->handle = (unsigned long)(uintptr_t)sg->virtual;
    216 	KASSERT(sg->virtual == (void *)(uintptr_t)sg->handle);
    217 
    218 	/* Success!  */
    219 	*sgp = sg;
    220 	return 0;
    221 
    222 fail3:	bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
    223 fail2:	bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
    224 fail1:	KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
    225 	bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
    226 fail0:	sg->sg_tag = NULL;	/* XXX paranoia */
    227 	kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
    228 	return error;
    229 }
    230 
    231 static void
    232 drm_sg_free_mem(struct drm_device *dev, struct drm_sg_mem *sg)
    233 {
    234 
    235 	bus_dmamap_unload(sg->sg_tag, sg->sg_map);
    236 	bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
    237 	bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
    238 	KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
    239 	bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
    240 	sg->sg_tag = NULL;	/* XXX paranoia */
    241 	kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
    242 }
    243 #endif
    244