2 * Copyright (c) 2009 Robert C. Noland III <rnoland@FreeBSD.org>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 /** @file drm_scatter.c
29 * Allocation of memory for scatter-gather mappings by the graphics chip.
30 * The memory allocated here is then made into an aperture in the card
31 * by mapping the pages into the GART.
34 #include <dev/drm2/drmP.h>
36 #define DEBUG_SCATTER 0
38 static inline vm_offset_t drm_vmalloc_dma(vm_size_t size)
40 return kmem_alloc_attr(kernel_arena, size, M_NOWAIT | M_ZERO,
41 0, BUS_SPACE_MAXADDR_32BIT, VM_MEMATTR_WRITE_COMBINING);
44 void drm_sg_cleanup(struct drm_sg_mem * entry)
49 if (entry->vaddr != 0)
50 kmem_free(kernel_arena, entry->vaddr, IDX_TO_OFF(entry->pages));
52 free(entry->busaddr, DRM_MEM_SGLISTS);
53 free(entry, DRM_MEM_DRIVER);
56 int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request)
58 struct drm_sg_mem *entry;
64 if (!drm_core_check_feature(dev, DRIVER_SG))
70 entry = malloc(sizeof(*entry), DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
74 DRM_DEBUG("request size=%ld\n", request->size);
76 size = round_page(request->size);
77 entry->pages = OFF_TO_IDX(size);
78 entry->busaddr = malloc(entry->pages * sizeof(*entry->busaddr),
79 DRM_MEM_SGLISTS, M_NOWAIT | M_ZERO);
80 if (!entry->busaddr) {
81 free(entry, DRM_MEM_DRIVER);
85 entry->vaddr = drm_vmalloc_dma(size);
86 if (entry->vaddr == 0) {
87 free(entry->busaddr, DRM_MEM_DRIVER);
88 free(entry, DRM_MEM_DRIVER);
92 for (pindex = 0; pindex < entry->pages; pindex++) {
93 entry->busaddr[pindex] =
94 vtophys(entry->vaddr + IDX_TO_OFF(pindex));
97 request->handle = entry->vaddr;
101 DRM_DEBUG("allocated %ju pages @ 0x%08zx, contents=%08lx\n",
102 entry->pages, entry->vaddr, *(unsigned long *)entry->vaddr);
107 int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
108 struct drm_file *file_priv)
110 struct drm_scatter_gather *request = data;
112 return drm_sg_alloc(dev, request);
116 int drm_sg_free(struct drm_device *dev, void *data,
117 struct drm_file *file_priv)
119 struct drm_scatter_gather *request = data;
120 struct drm_sg_mem *entry;
122 if (!drm_core_check_feature(dev, DRIVER_SG))
128 if (!entry || entry->vaddr != request->handle)
131 DRM_DEBUG("free 0x%zx\n", entry->vaddr);
133 drm_sg_cleanup(entry);