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