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.
24 * $FreeBSD: src/sys/dev/drm2/drm_scatter.c,v 1.1 2012/05/22 11:07:44 kib Exp $
27 /** @file drm_scatter.c
28 * Allocation of memory for scatter-gather mappings by the graphics chip.
29 * The memory allocated here is then made into an aperture in the card
30 * by mapping the pages into the GART.
36 drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather *request)
38 struct drm_sg_mem *entry;
45 DRM_DEBUG("request size=%ld\n", request->size);
47 entry = kmalloc(sizeof(*entry), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
49 size = round_page(request->size);
50 entry->pages = OFF_TO_IDX(size);
51 entry->busaddr = kmalloc(entry->pages * sizeof(*entry->busaddr),
52 DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
54 entry->vaddr = kmem_alloc_attr(&kernel_map, size, M_WAITOK | M_ZERO,
55 0, BUS_SPACE_MAXADDR_32BIT, VM_MEMATTR_WRITE_COMBINING);
56 if (entry->vaddr == 0) {
57 drm_sg_cleanup(entry);
61 for(pindex = 0; pindex < entry->pages; pindex++) {
62 entry->busaddr[pindex] =
63 vtophys(entry->vaddr + IDX_TO_OFF(pindex));
69 drm_sg_cleanup(entry);
75 request->handle = entry->vaddr;
77 DRM_DEBUG("allocated %ju pages @ 0x%08jx, contents=%08lx\n",
78 entry->pages, (uintmax_t)entry->vaddr,
79 *(unsigned long *)entry->vaddr);
85 drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
86 struct drm_file *file_priv)
88 struct drm_scatter_gather *request = data;
92 return (drm_sg_alloc(dev, request));
96 drm_sg_cleanup(struct drm_sg_mem *entry)
101 if (entry->vaddr != 0)
102 kmem_free(&kernel_map, entry->vaddr, IDX_TO_OFF(entry->pages));
104 drm_free(entry->busaddr, DRM_MEM_SGLISTS);
105 drm_free(entry, DRM_MEM_DRIVER);
111 drm_sg_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
113 struct drm_scatter_gather *request = data;
114 struct drm_sg_mem *entry;
121 if (!entry || entry->vaddr != request->handle)
124 DRM_DEBUG("free 0x%jx\n", (uintmax_t)entry->vaddr);
126 drm_sg_cleanup(entry);