]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_scatter.h
This commit was generated by cvs2svn to compensate for changes in r96295,
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_scatter.h
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
3  *
4  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
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:
13  *
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
16  * Software.
17  *
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.
25  *
26  * Authors:
27  *   Gareth Hughes <gareth@valinux.com>
28  *
29  * $FreeBSD$
30  */
31
32 #define __NO_VERSION__
33 #include <linux/config.h>
34 #include <linux/vmalloc.h>
35 #include "dev/drm/drmP.h"
36
37 #define DEBUG_SCATTER 0
38
39 void DRM(sg_cleanup)( drm_sg_mem_t *entry )
40 {
41         struct page *page;
42         int i;
43
44         for ( i = 0 ; i < entry->pages ; i++ ) {
45                 page = entry->pagelist[i];
46                 if ( page )
47                         ClearPageReserved( page );
48         }
49
50         vfree( entry->virtual );
51
52         DRM(free)( entry->busaddr,
53                    entry->pages * sizeof(*entry->busaddr),
54                    DRM_MEM_PAGES );
55         DRM(free)( entry->pagelist,
56                    entry->pages * sizeof(*entry->pagelist),
57                    DRM_MEM_PAGES );
58         DRM(free)( entry,
59                    sizeof(*entry),
60                    DRM_MEM_SGLISTS );
61 }
62
63 int DRM(sg_alloc)( struct inode *inode, struct file *filp,
64                    unsigned int cmd, unsigned long arg )
65 {
66         drm_file_t *priv = filp->private_data;
67         drm_device_t *dev = priv->dev;
68         drm_scatter_gather_t request;
69         drm_sg_mem_t *entry;
70         unsigned long pages, i, j;
71         pgd_t *pgd;
72         pmd_t *pmd;
73         pte_t *pte;
74
75         DRM_DEBUG( "%s\n", __FUNCTION__ );
76
77         if ( dev->sg )
78                 return DRM_OS_ERR(EINVAL);
79
80         if ( copy_from_user( &request,
81                              (drm_scatter_gather_t *)arg,
82                              sizeof(request) ) )
83                 return DRM_OS_ERR(EFAULT);
84
85         entry = DRM(alloc)( sizeof(*entry), DRM_MEM_SGLISTS );
86         if ( !entry )
87                 return DRM_OS_ERR(ENOMEM);
88
89         memset( entry, 0, sizeof(*entry) );
90
91         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
92         DRM_DEBUG( "sg size=%ld pages=%ld\n", request.size, pages );
93
94         entry->pages = pages;
95         entry->pagelist = DRM(alloc)( pages * sizeof(*entry->pagelist),
96                                      DRM_MEM_PAGES );
97         if ( !entry->pagelist ) {
98                 DRM(free)( entry, sizeof(*entry), DRM_MEM_SGLISTS );
99                 return DRM_OS_ERR(ENOMEM);
100         }
101
102         entry->busaddr = DRM(alloc)( pages * sizeof(*entry->busaddr),
103                                      DRM_MEM_PAGES );
104         if ( !entry->busaddr ) {
105                 DRM(free)( entry->pagelist,
106                            entry->pages * sizeof(*entry->pagelist),
107                            DRM_MEM_PAGES );
108                 DRM(free)( entry,
109                            sizeof(*entry),
110                            DRM_MEM_SGLISTS );
111                 return DRM_OS_ERR(ENOMEM);
112         }
113         memset( (void *)entry->busaddr, 0, pages * sizeof(*entry->busaddr) );
114
115         entry->virtual = vmalloc_32( pages << PAGE_SHIFT );
116         if ( !entry->virtual ) {
117                 DRM(free)( entry->busaddr,
118                            entry->pages * sizeof(*entry->busaddr),
119                            DRM_MEM_PAGES );
120                 DRM(free)( entry->pagelist,
121                            entry->pages * sizeof(*entry->pagelist),
122                            DRM_MEM_PAGES );
123                 DRM(free)( entry,
124                            sizeof(*entry),
125                            DRM_MEM_SGLISTS );
126                 return DRM_OS_ERR(ENOMEM);
127         }
128
129         /* This also forces the mapping of COW pages, so our page list
130          * will be valid.  Please don't remove it...
131          */
132         memset( entry->virtual, 0, pages << PAGE_SHIFT );
133
134         entry->handle = (unsigned long)entry->virtual;
135
136         DRM_DEBUG( "sg alloc handle  = %08lx\n", entry->handle );
137         DRM_DEBUG( "sg alloc virtual = %p\n", entry->virtual );
138
139         for ( i = entry->handle, j = 0 ; j < pages ; i += PAGE_SIZE, j++ ) {
140                 pgd = pgd_offset_k( i );
141                 if ( !pgd_present( *pgd ) )
142                         goto failed;
143
144                 pmd = pmd_offset( pgd, i );
145                 if ( !pmd_present( *pmd ) )
146                         goto failed;
147
148                 pte = pte_offset( pmd, i );
149                 if ( !pte_present( *pte ) )
150                         goto failed;
151
152                 entry->pagelist[j] = pte_page( *pte );
153
154                 SetPageReserved( entry->pagelist[j] );
155         }
156
157         request.handle = entry->handle;
158
159         if ( copy_to_user( (drm_scatter_gather_t *)arg,
160                            &request,
161                            sizeof(request) ) ) {
162                 DRM(sg_cleanup)( entry );
163                 return DRM_OS_ERR(EFAULT);
164         }
165
166         dev->sg = entry;
167
168 #if DEBUG_SCATTER
169         /* Verify that each page points to its virtual address, and vice
170          * versa.
171          */
172         {
173         int error = 0;
174
175         for ( i = 0 ; i < pages ; i++ ) {
176                 unsigned long *tmp;
177
178                 tmp = page_address( entry->pagelist[i] );
179                 for ( j = 0 ;
180                       j < PAGE_SIZE / sizeof(unsigned long) ;
181                       j++, tmp++ ) {
182                         *tmp = 0xcafebabe;
183                 }
184                 tmp = (unsigned long *)((u8 *)entry->virtual +
185                                         (PAGE_SIZE * i));
186                 for( j = 0 ;
187                      j < PAGE_SIZE / sizeof(unsigned long) ;
188                      j++, tmp++ ) {
189                         if ( *tmp != 0xcafebabe && error == 0 ) {
190                                 error = 1;
191                                 DRM_ERROR( "Scatter allocation error, "
192                                            "pagelist does not match "
193                                            "virtual mapping\n" );
194                         }
195                 }
196                 tmp = page_address( entry->pagelist[i] );
197                 for(j = 0 ;
198                     j < PAGE_SIZE / sizeof(unsigned long) ;
199                     j++, tmp++) {
200                         *tmp = 0;
201                 }
202         }
203         if (error == 0)
204                 DRM_ERROR( "Scatter allocation matches pagelist\n" );
205         }
206 #endif
207
208         return 0;
209
210  failed:
211         DRM(sg_cleanup)( entry );
212         return DRM_OS_ERR(ENOMEM);
213 }
214
215 int DRM(sg_free)( struct inode *inode, struct file *filp,
216                  unsigned int cmd, unsigned long arg )
217 {
218         drm_file_t *priv = filp->private_data;
219         drm_device_t *dev = priv->dev;
220         drm_scatter_gather_t request;
221         drm_sg_mem_t *entry;
222
223         if ( copy_from_user( &request,
224                              (drm_scatter_gather_t *)arg,
225                              sizeof(request) ) )
226                 return DRM_OS_ERR(EFAULT);
227
228         entry = dev->sg;
229         dev->sg = NULL;
230
231         if ( !entry || entry->handle != request.handle )
232                 return DRM_OS_ERR(EINVAL);
233
234         DRM_DEBUG( "sg free virtual  = %p\n", entry->virtual );
235
236         DRM(sg_cleanup)( entry );
237
238         return 0;
239 }