1 /* drm_scatter.h -- IOCTLs to manage scatter/gather memory -*- linux-c -*-
2 * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com
4 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 * Gareth Hughes <gareth@valinux.com>
32 #define __NO_VERSION__
33 #include <linux/config.h>
34 #include <linux/vmalloc.h>
35 #include "dev/drm/drmP.h"
37 #define DEBUG_SCATTER 0
39 void DRM(sg_cleanup)( drm_sg_mem_t *entry )
44 for ( i = 0 ; i < entry->pages ; i++ ) {
45 page = entry->pagelist[i];
47 ClearPageReserved( page );
50 vfree( entry->virtual );
52 DRM(free)( entry->busaddr,
53 entry->pages * sizeof(*entry->busaddr),
55 DRM(free)( entry->pagelist,
56 entry->pages * sizeof(*entry->pagelist),
63 int DRM(sg_alloc)( struct inode *inode, struct file *filp,
64 unsigned int cmd, unsigned long arg )
66 drm_file_t *priv = filp->private_data;
67 drm_device_t *dev = priv->dev;
68 drm_scatter_gather_t request;
70 unsigned long pages, i, j;
75 DRM_DEBUG( "%s\n", __func__ );
78 return DRM_OS_ERR(EINVAL);
80 if ( copy_from_user( &request,
81 (drm_scatter_gather_t *)arg,
83 return DRM_OS_ERR(EFAULT);
85 entry = DRM(alloc)( sizeof(*entry), DRM_MEM_SGLISTS );
87 return DRM_OS_ERR(ENOMEM);
89 memset( entry, 0, sizeof(*entry) );
91 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
92 DRM_DEBUG( "sg size=%ld pages=%ld\n", request.size, pages );
95 entry->pagelist = DRM(alloc)( pages * sizeof(*entry->pagelist),
97 if ( !entry->pagelist ) {
98 DRM(free)( entry, sizeof(*entry), DRM_MEM_SGLISTS );
99 return DRM_OS_ERR(ENOMEM);
102 entry->busaddr = DRM(alloc)( pages * sizeof(*entry->busaddr),
104 if ( !entry->busaddr ) {
105 DRM(free)( entry->pagelist,
106 entry->pages * sizeof(*entry->pagelist),
111 return DRM_OS_ERR(ENOMEM);
113 memset( (void *)entry->busaddr, 0, pages * sizeof(*entry->busaddr) );
115 entry->virtual = vmalloc_32( pages << PAGE_SHIFT );
116 if ( !entry->virtual ) {
117 DRM(free)( entry->busaddr,
118 entry->pages * sizeof(*entry->busaddr),
120 DRM(free)( entry->pagelist,
121 entry->pages * sizeof(*entry->pagelist),
126 return DRM_OS_ERR(ENOMEM);
129 /* This also forces the mapping of COW pages, so our page list
130 * will be valid. Please don't remove it...
132 memset( entry->virtual, 0, pages << PAGE_SHIFT );
134 entry->handle = (unsigned long)entry->virtual;
136 DRM_DEBUG( "sg alloc handle = %08lx\n", entry->handle );
137 DRM_DEBUG( "sg alloc virtual = %p\n", entry->virtual );
139 for ( i = entry->handle, j = 0 ; j < pages ; i += PAGE_SIZE, j++ ) {
140 pgd = pgd_offset_k( i );
141 if ( !pgd_present( *pgd ) )
144 pmd = pmd_offset( pgd, i );
145 if ( !pmd_present( *pmd ) )
148 pte = pte_offset( pmd, i );
149 if ( !pte_present( *pte ) )
152 entry->pagelist[j] = pte_page( *pte );
154 SetPageReserved( entry->pagelist[j] );
157 request.handle = entry->handle;
159 if ( copy_to_user( (drm_scatter_gather_t *)arg,
161 sizeof(request) ) ) {
162 DRM(sg_cleanup)( entry );
163 return DRM_OS_ERR(EFAULT);
169 /* Verify that each page points to its virtual address, and vice
175 for ( i = 0 ; i < pages ; i++ ) {
178 tmp = page_address( entry->pagelist[i] );
180 j < PAGE_SIZE / sizeof(unsigned long) ;
184 tmp = (unsigned long *)((u8 *)entry->virtual +
187 j < PAGE_SIZE / sizeof(unsigned long) ;
189 if ( *tmp != 0xcafebabe && error == 0 ) {
191 DRM_ERROR( "Scatter allocation error, "
192 "pagelist does not match "
193 "virtual mapping\n" );
196 tmp = page_address( entry->pagelist[i] );
198 j < PAGE_SIZE / sizeof(unsigned long) ;
204 DRM_ERROR( "Scatter allocation matches pagelist\n" );
211 DRM(sg_cleanup)( entry );
212 return DRM_OS_ERR(ENOMEM);
215 int DRM(sg_free)( struct inode *inode, struct file *filp,
216 unsigned int cmd, unsigned long arg )
218 drm_file_t *priv = filp->private_data;
219 drm_device_t *dev = priv->dev;
220 drm_scatter_gather_t request;
223 if ( copy_from_user( &request,
224 (drm_scatter_gather_t *)arg,
226 return DRM_OS_ERR(EFAULT);
231 if ( !entry || entry->handle != request.handle )
232 return DRM_OS_ERR(EINVAL);
234 DRM_DEBUG( "sg free virtual = %p\n", entry->virtual );
236 DRM(sg_cleanup)( entry );