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