VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / char / drm / drm_agpsupport.h
1 /**
2  * \file drm_agpsupport.h 
3  * DRM support for AGP/GART backend
4  *    
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8
9 /*
10  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12  * All Rights Reserved.
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the "Software"),
16  * to deal in the Software without restriction, including without limitation
17  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18  * and/or sell copies of the Software, and to permit persons to whom the
19  * Software is furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice (including the next
22  * paragraph) shall be included in all copies or substantial portions of the
23  * Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31  * OTHER DEALINGS IN THE SOFTWARE.
32  */
33
34 #include "drmP.h"
35 #include <linux/module.h>
36
37 #if __REALLY_HAVE_AGP
38
39
40 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
41 #define DRM_AGP_PUT inter_module_put("drm_agp")
42
43 /**
44  * Pointer to the drm_agp_t structure made available by the agpgart module.
45  */
46 static const drm_agp_t *drm_agp = NULL;
47
48 /**
49  * AGP information ioctl.
50  *
51  * \param inode device inode.
52  * \param filp file pointer.
53  * \param cmd command.
54  * \param arg pointer to a (output) drm_agp_info structure.
55  * \return zero on success or a negative number on failure.
56  *
57  * Verifies the AGP device has been initialized and acquired and fills in the
58  * drm_agp_info structure with the information in drm_agp_head::agp_info.
59  */
60 int DRM(agp_info)(struct inode *inode, struct file *filp,
61                   unsigned int cmd, unsigned long arg)
62 {
63         drm_file_t       *priv   = filp->private_data;
64         drm_device_t     *dev    = priv->dev;
65         DRM_AGP_KERN     *kern;
66         drm_agp_info_t   info;
67
68         if (!dev->agp || !dev->agp->acquired || !drm_agp->copy_info)
69                 return -EINVAL;
70
71         kern                   = &dev->agp->agp_info;
72         info.agp_version_major = kern->version.major;
73         info.agp_version_minor = kern->version.minor;
74         info.mode              = kern->mode;
75         info.aperture_base     = kern->aper_base;
76         info.aperture_size     = kern->aper_size * 1024 * 1024;
77         info.memory_allowed    = kern->max_memory << PAGE_SHIFT;
78         info.memory_used       = kern->current_memory << PAGE_SHIFT;
79         info.id_vendor         = kern->device->vendor;
80         info.id_device         = kern->device->device;
81
82         if (copy_to_user((drm_agp_info_t __user *)arg, &info, sizeof(info)))
83                 return -EFAULT;
84         return 0;
85 }
86
87 /**
88  * Acquire the AGP device (ioctl).
89  *
90  * \param inode device inode.
91  * \param filp file pointer.
92  * \param cmd command.
93  * \param arg user argument.
94  * \return zero on success or a negative number on failure. 
95  *
96  * Verifies the AGP device hasn't been acquired before and calls
97  * drm_agp->acquire().
98  */
99 int DRM(agp_acquire)(struct inode *inode, struct file *filp,
100                      unsigned int cmd, unsigned long arg)
101 {
102         drm_file_t       *priv   = filp->private_data;
103         drm_device_t     *dev    = priv->dev;
104         int              retcode;
105
106         if (!dev->agp)
107                 return -ENODEV;
108         if (dev->agp->acquired)
109                 return -EBUSY;
110         if (!drm_agp->acquire)
111                 return -EINVAL;
112         if ((retcode = drm_agp->acquire()))
113                 return retcode;
114         dev->agp->acquired = 1;
115         return 0;
116 }
117
118 /**
119  * Release the AGP device (ioctl).
120  *
121  * \param inode device inode.
122  * \param filp file pointer.
123  * \param cmd command.
124  * \param arg user argument.
125  * \return zero on success or a negative number on failure.
126  *
127  * Verifies the AGP device has been acquired and calls drm_agp->release().
128  */
129 int DRM(agp_release)(struct inode *inode, struct file *filp,
130                      unsigned int cmd, unsigned long arg)
131 {
132         drm_file_t       *priv   = filp->private_data;
133         drm_device_t     *dev    = priv->dev;
134
135         if (!dev->agp || !dev->agp->acquired || !drm_agp->release)
136                 return -EINVAL;
137         drm_agp->release();
138         dev->agp->acquired = 0;
139         return 0;
140
141 }
142
143 /**
144  * Release the AGP device.
145  *
146  * Calls drm_agp->release().
147  */
148 void DRM(agp_do_release)(void)
149 {
150         if (drm_agp->release)
151                 drm_agp->release();
152 }
153
154 /**
155  * Enable the AGP bus.
156  * 
157  * \param inode device inode.
158  * \param filp file pointer.
159  * \param cmd command.
160  * \param arg pointer to a drm_agp_mode structure.
161  * \return zero on success or a negative number on failure.
162  *
163  * Verifies the AGP device has been acquired but not enabled, and calls
164  * drm_agp->enable().
165  */
166 int DRM(agp_enable)(struct inode *inode, struct file *filp,
167                     unsigned int cmd, unsigned long arg)
168 {
169         drm_file_t       *priv   = filp->private_data;
170         drm_device_t     *dev    = priv->dev;
171         drm_agp_mode_t   mode;
172
173         if (!dev->agp || !dev->agp->acquired || !drm_agp->enable)
174                 return -EINVAL;
175
176         if (copy_from_user(&mode, (drm_agp_mode_t __user *)arg, sizeof(mode)))
177                 return -EFAULT;
178
179         dev->agp->mode    = mode.mode;
180         drm_agp->enable(mode.mode);
181         dev->agp->base    = dev->agp->agp_info.aper_base;
182         dev->agp->enabled = 1;
183         return 0;
184 }
185
186 /**
187  * Allocate AGP memory.
188  *
189  * \param inode device inode.
190  * \param filp file pointer.
191  * \param cmd command.
192  * \param arg pointer to a drm_agp_buffer structure.
193  * \return zero on success or a negative number on failure.
194  * 
195  * Verifies the AGP device is present and has been acquired, allocates the
196  * memory via alloc_agp() and creates a drm_agp_mem entry for it.
197  */
198 int DRM(agp_alloc)(struct inode *inode, struct file *filp,
199                    unsigned int cmd, unsigned long arg)
200 {
201         drm_file_t       *priv   = filp->private_data;
202         drm_device_t     *dev    = priv->dev;
203         drm_agp_buffer_t request;
204         drm_agp_mem_t    *entry;
205         DRM_AGP_MEM      *memory;
206         unsigned long    pages;
207         u32              type;
208         drm_agp_buffer_t __user *argp = (void __user *)arg;
209
210         if (!dev->agp || !dev->agp->acquired)
211                 return -EINVAL;
212         if (copy_from_user(&request, argp, sizeof(request)))
213                 return -EFAULT;
214         if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
215                 return -ENOMEM;
216
217         memset(entry, 0, sizeof(*entry));
218
219         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
220         type = (u32) request.type;
221
222         if (!(memory = DRM(alloc_agp)(pages, type))) {
223                 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
224                 return -ENOMEM;
225         }
226
227         entry->handle    = (unsigned long)memory->key + 1;
228         entry->memory    = memory;
229         entry->bound     = 0;
230         entry->pages     = pages;
231         entry->prev      = NULL;
232         entry->next      = dev->agp->memory;
233         if (dev->agp->memory)
234                 dev->agp->memory->prev = entry;
235         dev->agp->memory = entry;
236
237         request.handle   = entry->handle;
238         request.physical = memory->physical;
239
240         if (copy_to_user(argp, &request, sizeof(request))) {
241                 dev->agp->memory       = entry->next;
242                 dev->agp->memory->prev = NULL;
243                 DRM(free_agp)(memory, pages);
244                 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
245                 return -EFAULT;
246         }
247         return 0;
248 }
249
250 /**
251  * Search for the AGP memory entry associated with a handle.
252  *
253  * \param dev DRM device structure.
254  * \param handle AGP memory handle.
255  * \return pointer to the drm_agp_mem structure associated with \p handle.
256  * 
257  * Walks through drm_agp_head::memory until finding a matching handle.
258  */
259 static drm_agp_mem_t *DRM(agp_lookup_entry)(drm_device_t *dev,
260                                             unsigned long handle)
261 {
262         drm_agp_mem_t *entry;
263
264         for (entry = dev->agp->memory; entry; entry = entry->next) {
265                 if (entry->handle == handle)
266                         return entry;
267         }
268         return NULL;
269 }
270
271 /**
272  * Unbind AGP memory from the GATT (ioctl).
273  *
274  * \param inode device inode.
275  * \param filp file pointer.
276  * \param cmd command.
277  * \param arg pointer to a drm_agp_binding structure.
278  * \return zero on success or a negative number on failure.
279  *
280  * Verifies the AGP device is present and acquired, looks-up the AGP memory
281  * entry and passes it to the unbind_agp() function.
282  */
283 int DRM(agp_unbind)(struct inode *inode, struct file *filp,
284                     unsigned int cmd, unsigned long arg)
285 {
286         drm_file_t        *priv  = filp->private_data;
287         drm_device_t      *dev   = priv->dev;
288         drm_agp_binding_t request;
289         drm_agp_mem_t     *entry;
290         int ret;
291
292         if (!dev->agp || !dev->agp->acquired)
293                 return -EINVAL;
294         if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
295                 return -EFAULT;
296         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
297                 return -EINVAL;
298         if (!entry->bound)
299                 return -EINVAL;
300         ret = DRM(unbind_agp)(entry->memory);
301         if (ret == 0)
302             entry->bound = 0;
303         return ret;
304 }
305
306 /**
307  * Bind AGP memory into the GATT (ioctl)
308  *
309  * \param inode device inode.
310  * \param filp file pointer.
311  * \param cmd command.
312  * \param arg pointer to a drm_agp_binding structure.
313  * \return zero on success or a negative number on failure.
314  *
315  * Verifies the AGP device is present and has been acquired and that no memory
316  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
317  * it to bind_agp() function.
318  */
319 int DRM(agp_bind)(struct inode *inode, struct file *filp,
320                   unsigned int cmd, unsigned long arg)
321 {
322         drm_file_t        *priv  = filp->private_data;
323         drm_device_t      *dev   = priv->dev;
324         drm_agp_binding_t request;
325         drm_agp_mem_t     *entry;
326         int               retcode;
327         int               page;
328
329         if (!dev->agp || !dev->agp->acquired || !drm_agp->bind_memory)
330                 return -EINVAL;
331         if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
332                 return -EFAULT;
333         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
334                 return -EINVAL;
335         if (entry->bound)
336                 return -EINVAL;
337         page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
338         if ((retcode = DRM(bind_agp)(entry->memory, page)))
339                 return retcode;
340         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
341         DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
342                   dev->agp->base, entry->bound);
343         return 0;
344 }
345
346 /**
347  * Free AGP memory (ioctl).
348  *
349  * \param inode device inode.
350  * \param filp file pointer.
351  * \param cmd command.
352  * \param arg pointer to a drm_agp_buffer structure.
353  * \return zero on success or a negative number on failure.
354  *
355  * Verifies the AGP device is present and has been acquired and looks up the
356  * AGP memory entry. If the memory it's currently bound, unbind it via
357  * unbind_agp(). Frees it via free_agp() as well as the entry itself
358  * and unlinks from the doubly linked list it's inserted in.
359  */
360 int DRM(agp_free)(struct inode *inode, struct file *filp,
361                   unsigned int cmd, unsigned long arg)
362 {
363         drm_file_t       *priv   = filp->private_data;
364         drm_device_t     *dev    = priv->dev;
365         drm_agp_buffer_t request;
366         drm_agp_mem_t    *entry;
367
368         if (!dev->agp || !dev->agp->acquired)
369                 return -EINVAL;
370         if (copy_from_user(&request, (drm_agp_buffer_t __user *)arg, sizeof(request)))
371                 return -EFAULT;
372         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
373                 return -EINVAL;
374         if (entry->bound)
375                 DRM(unbind_agp)(entry->memory);
376
377         if (entry->prev)
378                 entry->prev->next = entry->next;
379         else
380                 dev->agp->memory = entry->next;
381
382         if (entry->next)
383                 entry->next->prev = entry->prev;
384
385         DRM(free_agp)(entry->memory, entry->pages);
386         DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
387         return 0;
388 }
389
390 /**
391  * Initialize the AGP resources.
392  *
393  * \return pointer to a drm_agp_head structure.
394  *
395  * Gets the drm_agp_t structure which is made available by the agpgart module
396  * via the inter_module_* functions. Creates and initializes a drm_agp_head
397  * structure.
398  */
399 drm_agp_head_t *DRM(agp_init)(void)
400 {
401         drm_agp_head_t *head         = NULL;
402
403         drm_agp = DRM_AGP_GET;
404         if (drm_agp) {
405                 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
406                         return NULL;
407                 memset((void *)head, 0, sizeof(*head));
408                 drm_agp->copy_info(&head->agp_info);
409                 if (head->agp_info.chipset == NOT_SUPPORTED) {
410                         DRM(free)(head, sizeof(*head), DRM_MEM_AGPLISTS);
411                         return NULL;
412                 }
413                 head->memory = NULL;
414 #if LINUX_VERSION_CODE <= 0x020408
415                 head->cant_use_aperture = 0;
416                 head->page_mask = ~(0xfff);
417 #else
418                 head->cant_use_aperture = head->agp_info.cant_use_aperture;
419                 head->page_mask = head->agp_info.page_mask;
420 #endif
421         }
422         return head;
423 }
424
425 /**
426  * Free the AGP resources.
427  *
428  * Releases the pointer in ::drm_agp.
429  */
430 void DRM(agp_uninit)(void)
431 {
432         DRM_AGP_PUT;
433         drm_agp = NULL;
434 }
435
436 /** Calls drm_agp->allocate_memory() */
437 DRM_AGP_MEM *DRM(agp_allocate_memory)(size_t pages, u32 type)
438 {
439         if (!drm_agp->allocate_memory)
440                 return NULL;
441         return drm_agp->allocate_memory(pages, type);
442 }
443
444 /** Calls drm_agp->free_memory() */
445 int DRM(agp_free_memory)(DRM_AGP_MEM *handle)
446 {
447         if (!handle || !drm_agp->free_memory)
448                 return 0;
449         drm_agp->free_memory(handle);
450         return 1;
451 }
452
453 /** Calls drm_agp->bind_memory() */
454 int DRM(agp_bind_memory)(DRM_AGP_MEM *handle, off_t start)
455 {
456         if (!handle || !drm_agp->bind_memory)
457                 return -EINVAL;
458         return drm_agp->bind_memory(handle, start);
459 }
460
461 /** Calls drm_agp->unbind_memory() */
462 int DRM(agp_unbind_memory)(DRM_AGP_MEM *handle)
463 {
464         if (!handle || !drm_agp->unbind_memory)
465                 return -EINVAL;
466         return drm_agp->unbind_memory(handle);
467 }
468
469 #endif /* __REALLY_HAVE_AGP */