2 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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 * Gareth Hughes <gareth@valinux.com>
26 * Eric Anholt <anholt@FreeBSD.org>
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 /** @file drm_scatter.c
34 * Allocation of memory for scatter-gather mappings by the graphics chip.
36 * The memory allocated here is then made into an aperture in the card
37 * by drm_ati_pcigart_init().
40 #include "dev/drm/drmP.h"
42 static void drm_sg_alloc_cb(void *arg, bus_dma_segment_t *segs,
43 int nsegs, int error);
46 drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather *request)
48 struct drm_sg_mem *entry;
49 struct drm_dma_handle *dmah;
56 entry = malloc(sizeof(*entry), DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
60 pages = round_page(request->size) / PAGE_SIZE;
61 DRM_DEBUG("sg size=%ld pages=%ld\n", request->size, pages);
65 entry->busaddr = malloc(pages * sizeof(*entry->busaddr), DRM_MEM_PAGES,
67 if (!entry->busaddr) {
68 free(entry, DRM_MEM_SGLISTS);
72 dmah = malloc(sizeof(struct drm_dma_handle), DRM_MEM_DMA,
75 free(entry->busaddr, DRM_MEM_PAGES);
76 free(entry, DRM_MEM_SGLISTS);
80 ret = bus_dma_tag_create(NULL, PAGE_SIZE, 0, /* tag, align, boundary */
81 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
82 NULL, NULL, /* filtfunc, filtfuncargs */
83 request->size, pages, /* maxsize, nsegs */
84 PAGE_SIZE, 0, /* maxsegsize, flags */
85 NULL, NULL, /* lockfunc, lockfuncargs */
88 free(dmah, DRM_MEM_DMA);
89 free(entry->busaddr, DRM_MEM_PAGES);
90 free(entry, DRM_MEM_SGLISTS);
94 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
95 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
97 bus_dma_tag_destroy(dmah->tag);
98 free(dmah, DRM_MEM_DMA);
99 free(entry->busaddr, DRM_MEM_PAGES);
100 free(entry, DRM_MEM_SGLISTS);
104 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr,
105 request->size, drm_sg_alloc_cb, entry, BUS_DMA_NOWAIT);
107 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
108 bus_dma_tag_destroy(dmah->tag);
109 free(dmah, DRM_MEM_DMA);
110 free(entry->busaddr, DRM_MEM_PAGES);
111 free(entry, DRM_MEM_SGLISTS);
116 entry->handle = (unsigned long)dmah->vaddr;
118 DRM_DEBUG("sg alloc handle = %08lx\n", entry->handle);
120 entry->virtual = (void *)entry->handle;
121 request->handle = entry->handle;
126 drm_sg_cleanup(entry);
136 drm_sg_alloc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
138 struct drm_sg_mem *entry = arg;
144 for(i = 0 ; i < nsegs ; i++) {
145 entry->busaddr[i] = segs[i].ds_addr;
150 drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
151 struct drm_file *file_priv)
153 struct drm_scatter_gather *request = data;
157 return drm_sg_alloc(dev, request);
161 drm_sg_cleanup(struct drm_sg_mem *entry)
163 struct drm_dma_handle *dmah = entry->dmah;
165 bus_dmamap_unload(dmah->tag, dmah->map);
166 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
167 bus_dma_tag_destroy(dmah->tag);
168 free(dmah, DRM_MEM_DMA);
169 free(entry->busaddr, DRM_MEM_PAGES);
170 free(entry, DRM_MEM_SGLISTS);
174 drm_sg_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
176 struct drm_scatter_gather *request = data;
177 struct drm_sg_mem *entry;
184 if (!entry || entry->handle != request->handle)
187 DRM_DEBUG("sg free virtual = 0x%lx\n", entry->handle);
189 drm_sg_cleanup(entry);