1*407b0b05Sriastradh /* $NetBSD: drm_scatter.c,v 1.9 2021/12/19 12:30:05 riastradh Exp $ */
26cb10275Sriastradh
36cb10275Sriastradh /*-
46cb10275Sriastradh * Copyright (c) 2013 The NetBSD Foundation, Inc.
56cb10275Sriastradh * All rights reserved.
66cb10275Sriastradh *
76cb10275Sriastradh * This code is derived from software contributed to The NetBSD Foundation
86cb10275Sriastradh * by Taylor R. Campbell.
96cb10275Sriastradh *
106cb10275Sriastradh * Redistribution and use in source and binary forms, with or without
116cb10275Sriastradh * modification, are permitted provided that the following conditions
126cb10275Sriastradh * are met:
136cb10275Sriastradh * 1. Redistributions of source code must retain the above copyright
146cb10275Sriastradh * notice, this list of conditions and the following disclaimer.
156cb10275Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
166cb10275Sriastradh * notice, this list of conditions and the following disclaimer in the
176cb10275Sriastradh * documentation and/or other materials provided with the distribution.
186cb10275Sriastradh *
196cb10275Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206cb10275Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216cb10275Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226cb10275Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236cb10275Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246cb10275Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256cb10275Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266cb10275Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276cb10275Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286cb10275Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296cb10275Sriastradh * POSSIBILITY OF SUCH DAMAGE.
306cb10275Sriastradh */
316cb10275Sriastradh
326cb10275Sriastradh #include <sys/cdefs.h>
33*407b0b05Sriastradh __KERNEL_RCSID(0, "$NetBSD: drm_scatter.c,v 1.9 2021/12/19 12:30:05 riastradh Exp $");
346cb10275Sriastradh
356cb10275Sriastradh #include <sys/types.h>
366cb10275Sriastradh #include <sys/bus.h>
376cb10275Sriastradh #include <sys/errno.h>
386cb10275Sriastradh #include <sys/systm.h>
396cb10275Sriastradh
40*407b0b05Sriastradh #include <linux/mm.h>
416cb10275Sriastradh #include <linux/slab.h>
426cb10275Sriastradh
43f21b21b0Sriastradh #include <drm/drm_device.h>
44429b039cSriastradh #include <drm/drm_drv.h>
453f26b5c5Sriastradh
463f26b5c5Sriastradh #include "../dist/drm/drm_internal.h"
47e09bf153Sriastradh #include "../dist/drm/drm_legacy.h"
486cb10275Sriastradh
49429b039cSriastradh #if IS_ENABLED(CONFIG_DRM_LEGACY)
506cb10275Sriastradh static int drm_sg_alloc_mem(struct drm_device *, size_t,
516cb10275Sriastradh struct drm_sg_mem **);
5277b5597aSriastradh static void drm_sg_free_mem(struct drm_device *, struct drm_sg_mem *);
536cb10275Sriastradh
546cb10275Sriastradh int
drm_legacy_sg_alloc(struct drm_device * dev,void * data,struct drm_file * file __unused)55e09bf153Sriastradh drm_legacy_sg_alloc(struct drm_device *dev, void *data,
566cb10275Sriastradh struct drm_file *file __unused)
576cb10275Sriastradh {
586cb10275Sriastradh struct drm_scatter_gather *const request = data;
595299ceaaSmrg struct drm_sg_mem *sg = NULL;
606cb10275Sriastradh int error;
616cb10275Sriastradh
626cb10275Sriastradh /*
636cb10275Sriastradh * XXX Should not hold this mutex during drm_sg_alloc. For
646cb10275Sriastradh * now, we'll just use non-blocking allocations.
656cb10275Sriastradh */
666cb10275Sriastradh KASSERT(mutex_is_locked(&drm_global_mutex));
676cb10275Sriastradh
686cb10275Sriastradh /*
696cb10275Sriastradh * Sanity-check the inputs.
706cb10275Sriastradh */
716cb10275Sriastradh if (!drm_core_check_feature(dev, DRIVER_SG))
726cb10275Sriastradh return -ENODEV;
736cb10275Sriastradh if (dev->sg != NULL)
746cb10275Sriastradh return -EBUSY;
756cb10275Sriastradh if (request->size > 0xffffffffUL)
766cb10275Sriastradh return -ENOMEM;
776cb10275Sriastradh
786cb10275Sriastradh /*
796cb10275Sriastradh * Do the allocation.
806cb10275Sriastradh *
816cb10275Sriastradh * XXX Would it be safe to drop drm_global_mutex here to avoid
826cb10275Sriastradh * holding it during allocation?
836cb10275Sriastradh */
846cb10275Sriastradh error = drm_sg_alloc_mem(dev, request->size, &sg);
856cb10275Sriastradh if (error)
866cb10275Sriastradh return error;
876cb10275Sriastradh
886cb10275Sriastradh /* Set it up to be the device's scatter-gather memory. */
896cb10275Sriastradh dev->sg = sg;
906cb10275Sriastradh
916cb10275Sriastradh /* Success! */
926cb10275Sriastradh request->handle = sg->handle;
936cb10275Sriastradh return 0;
946cb10275Sriastradh }
956cb10275Sriastradh
966cb10275Sriastradh int
drm_legacy_sg_free(struct drm_device * dev,void * data,struct drm_file * file)97e09bf153Sriastradh drm_legacy_sg_free(struct drm_device *dev, void *data, struct drm_file *file)
986cb10275Sriastradh {
996cb10275Sriastradh struct drm_scatter_gather *const request = data;
1006cb10275Sriastradh struct drm_sg_mem *sg;
1016cb10275Sriastradh
1026cb10275Sriastradh KASSERT(mutex_is_locked(&drm_global_mutex));
1036cb10275Sriastradh
1046cb10275Sriastradh /*
1056cb10275Sriastradh * Sanity-check the inputs.
1066cb10275Sriastradh */
1076cb10275Sriastradh if (!drm_core_check_feature(dev, DRIVER_SG))
1086cb10275Sriastradh return -ENODEV;
1096cb10275Sriastradh
1106cb10275Sriastradh sg = dev->sg;
1116cb10275Sriastradh if (sg == NULL)
1126cb10275Sriastradh return -ENXIO;
1136cb10275Sriastradh if (sg->handle != request->handle)
1146cb10275Sriastradh return -EINVAL;
1156cb10275Sriastradh
1166cb10275Sriastradh /* Remove dev->sg. */
1176cb10275Sriastradh dev->sg = NULL;
1186cb10275Sriastradh
11977b5597aSriastradh /* Free it. */
12077b5597aSriastradh drm_sg_free_mem(dev, sg);
12177b5597aSriastradh
1226cb10275Sriastradh /* Success! */
1236cb10275Sriastradh return 0;
1246cb10275Sriastradh }
1256cb10275Sriastradh
12677b5597aSriastradh void
drm_legacy_sg_cleanup(struct drm_device * dev)12777b5597aSriastradh drm_legacy_sg_cleanup(struct drm_device *dev)
12877b5597aSriastradh {
12977b5597aSriastradh
13077b5597aSriastradh if (!drm_core_check_feature(dev, DRIVER_SG))
13177b5597aSriastradh return;
13277b5597aSriastradh if (dev->sg == NULL)
13377b5597aSriastradh return;
13477b5597aSriastradh if (drm_core_check_feature(dev, DRIVER_MODESET))
13577b5597aSriastradh return;
13677b5597aSriastradh
13777b5597aSriastradh drm_sg_free_mem(dev, dev->sg);
13877b5597aSriastradh }
13977b5597aSriastradh
1406cb10275Sriastradh static int
drm_sg_alloc_mem(struct drm_device * dev,size_t size,struct drm_sg_mem ** sgp)1416cb10275Sriastradh drm_sg_alloc_mem(struct drm_device *dev, size_t size, struct drm_sg_mem **sgp)
1426cb10275Sriastradh {
1436cb10275Sriastradh int nsegs;
1446cb10275Sriastradh int error;
1456cb10275Sriastradh
1466cb10275Sriastradh KASSERT(drm_core_check_feature(dev, DRIVER_SG));
1476cb10275Sriastradh
1486cb10275Sriastradh KASSERT(size <= (size_t)0xffffffffUL); /* XXX 32-bit sizes only? */
1496cb10275Sriastradh const size_t nbytes = PAGE_ALIGN(size);
1506cb10275Sriastradh const size_t npages = nbytes >> PAGE_SHIFT;
1516cb10275Sriastradh KASSERT(npages <= (size_t)INT_MAX);
1526cb10275Sriastradh
1536cb10275Sriastradh /*
1546cb10275Sriastradh * Allocate a drm_sg_mem record.
1556cb10275Sriastradh */
1566cb10275Sriastradh struct drm_sg_mem *const sg =
1576cb10275Sriastradh kmem_zalloc(offsetof(struct drm_sg_mem, sg_segs[npages]),
1586cb10275Sriastradh KM_NOSLEEP);
1596cb10275Sriastradh if (sg == NULL)
1606cb10275Sriastradh return -ENOMEM;
1616cb10275Sriastradh sg->sg_tag = dev->dmat;
1626cb10275Sriastradh sg->sg_nsegs_max = (unsigned int)npages;
1636cb10275Sriastradh
1646cb10275Sriastradh /*
1656cb10275Sriastradh * Allocate the requested amount of DMA-safe memory.
1666cb10275Sriastradh */
1676cb10275Sriastradh KASSERT(sg->sg_nsegs_max <= (unsigned int)INT_MAX);
1686cb10275Sriastradh /* XXX errno NetBSD->Linux */
1696cb10275Sriastradh error = -bus_dmamem_alloc(sg->sg_tag, nbytes, PAGE_SIZE, 0,
1706cb10275Sriastradh sg->sg_segs, (int)sg->sg_nsegs_max, &nsegs, BUS_DMA_NOWAIT);
1716cb10275Sriastradh if (error)
1726cb10275Sriastradh goto fail0;
1736cb10275Sriastradh KASSERT(0 <= nsegs);
1746cb10275Sriastradh sg->sg_nsegs = (unsigned int)nsegs;
1756cb10275Sriastradh
1766cb10275Sriastradh /*
1776cb10275Sriastradh * XXX Old drm passed DRM_BUS_NOWAIT below but BUS_DMA_WAITOK
1786cb10275Sriastradh * above. WTF?
1796cb10275Sriastradh */
1806cb10275Sriastradh
1816cb10275Sriastradh /*
1826cb10275Sriastradh * Map the DMA-safe memory into kernel virtual address space.
1836cb10275Sriastradh */
1846cb10275Sriastradh /* XXX errno NetBSD->Linux */
1856cb10275Sriastradh error = -bus_dmamem_map(sg->sg_tag, sg->sg_segs, nsegs, nbytes,
1866cb10275Sriastradh &sg->virtual,
1876cb10275Sriastradh (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
1886cb10275Sriastradh if (error)
1896cb10275Sriastradh goto fail1;
1906cb10275Sriastradh sg->sg_size = nbytes;
1916cb10275Sriastradh
1926cb10275Sriastradh /*
1936cb10275Sriastradh * Create a map for DMA transfers.
1946cb10275Sriastradh */
1956cb10275Sriastradh /* XXX errno NetBSD->Linux */
1966cb10275Sriastradh error = -bus_dmamap_create(sg->sg_tag, nbytes, nsegs, nbytes, 0,
1976cb10275Sriastradh BUS_DMA_NOWAIT, &sg->sg_map);
1986cb10275Sriastradh if (error)
1996cb10275Sriastradh goto fail2;
2006cb10275Sriastradh
2016cb10275Sriastradh /*
2026cb10275Sriastradh * Load the kva buffer into the map for DMA transfers.
2036cb10275Sriastradh */
2046cb10275Sriastradh /* XXX errno NetBSD->Linux */
2056cb10275Sriastradh error = -bus_dmamap_load(sg->sg_tag, sg->sg_map, sg->virtual, nbytes,
2066cb10275Sriastradh NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
2076cb10275Sriastradh if (error)
2086cb10275Sriastradh goto fail3;
2096cb10275Sriastradh
2106cb10275Sriastradh /*
2116cb10275Sriastradh * Use the kernel virtual address as the userland handle.
2126cb10275Sriastradh *
2136cb10275Sriastradh * XXX This leaks some information about kernel virtual
2146cb10275Sriastradh * addresses to userland; should we use an idr instead?
2156cb10275Sriastradh */
2166cb10275Sriastradh sg->handle = (unsigned long)(uintptr_t)sg->virtual;
2176cb10275Sriastradh KASSERT(sg->virtual == (void *)(uintptr_t)sg->handle);
2186cb10275Sriastradh
2196cb10275Sriastradh /* Success! */
2206cb10275Sriastradh *sgp = sg;
2216cb10275Sriastradh return 0;
2226cb10275Sriastradh
2236cb10275Sriastradh fail3: bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
2246cb10275Sriastradh fail2: bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
2256cb10275Sriastradh fail1: KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
2266cb10275Sriastradh bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
2276cb10275Sriastradh fail0: sg->sg_tag = NULL; /* XXX paranoia */
22877b5597aSriastradh kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
2296cb10275Sriastradh return error;
2306cb10275Sriastradh }
2316cb10275Sriastradh
23277b5597aSriastradh static void
drm_sg_free_mem(struct drm_device * dev,struct drm_sg_mem * sg)23377b5597aSriastradh drm_sg_free_mem(struct drm_device *dev, struct drm_sg_mem *sg)
2346cb10275Sriastradh {
2356cb10275Sriastradh
2366cb10275Sriastradh bus_dmamap_unload(sg->sg_tag, sg->sg_map);
2376cb10275Sriastradh bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
2386cb10275Sriastradh bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
2396cb10275Sriastradh KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
2406cb10275Sriastradh bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
2416cb10275Sriastradh sg->sg_tag = NULL; /* XXX paranoia */
2426cb10275Sriastradh kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
2436cb10275Sriastradh }
244429b039cSriastradh #endif
245