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