vserver 1.9.3
[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 __OS_HAS_AGP
38
39 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
40 #define DRM_AGP_PUT inter_module_put("drm_agp")
41
42 /**
43  * Pointer to the drm_agp_t structure made available by the agpgart module.
44  */
45 static const drm_agp_t *drm_agp = NULL;
46
47 /**
48  * AGP information ioctl.
49  *
50  * \param inode device inode.
51  * \param filp file pointer.
52  * \param cmd command.
53  * \param arg pointer to a (output) drm_agp_info structure.
54  * \return zero on success or a negative number on failure.
55  *
56  * Verifies the AGP device has been initialized and acquired and fills in the
57  * drm_agp_info structure with the information in drm_agp_head::agp_info.
58  */
59 int DRM(agp_info)(struct inode *inode, struct file *filp,
60                   unsigned int cmd, unsigned long arg)
61 {
62         drm_file_t       *priv   = filp->private_data;
63         drm_device_t     *dev    = priv->dev;
64         DRM_AGP_KERN     *kern;
65         drm_agp_info_t   info;
66
67         if (!dev->agp || !dev->agp->acquired || !drm_agp->copy_info)
68                 return -EINVAL;
69
70         kern                   = &dev->agp->agp_info;
71         info.agp_version_major = kern->version.major;
72         info.agp_version_minor = kern->version.minor;
73         info.mode              = kern->mode;
74         info.aperture_base     = kern->aper_base;
75         info.aperture_size     = kern->aper_size * 1024 * 1024;
76         info.memory_allowed    = kern->max_memory << PAGE_SHIFT;
77         info.memory_used       = kern->current_memory << PAGE_SHIFT;
78         info.id_vendor         = kern->device->vendor;
79         info.id_device         = kern->device->device;
80
81         if (copy_to_user((drm_agp_info_t __user *)arg, &info, sizeof(info)))
82                 return -EFAULT;
83         return 0;
84 }
85
86 /**
87  * Acquire the AGP device (ioctl).
88  *
89  * \param inode device inode.
90  * \param filp file pointer.
91  * \param cmd command.
92  * \param arg user argument.
93  * \return zero on success or a negative number on failure. 
94  *
95  * Verifies the AGP device hasn't been acquired before and calls
96  * drm_agp->acquire().
97  */
98 int DRM(agp_acquire)(struct inode *inode, struct file *filp,
99                      unsigned int cmd, unsigned long arg)
100 {
101         drm_file_t       *priv   = filp->private_data;
102         drm_device_t     *dev    = priv->dev;
103         int              retcode;
104
105         if (!dev->agp)
106                 return -ENODEV;
107         if (dev->agp->acquired)
108                 return -EBUSY;
109         if (!drm_agp->acquire)
110                 return -EINVAL;
111         if ((retcode = drm_agp->acquire()))
112                 return retcode;
113         dev->agp->acquired = 1;
114         return 0;
115 }
116
117 /**
118  * Release the AGP device (ioctl).
119  *
120  * \param inode device inode.
121  * \param filp file pointer.
122  * \param cmd command.
123  * \param arg user argument.
124  * \return zero on success or a negative number on failure.
125  *
126  * Verifies the AGP device has been acquired and calls drm_agp->release().
127  */
128 int DRM(agp_release)(struct inode *inode, struct file *filp,
129                      unsigned int cmd, unsigned long arg)
130 {
131         drm_file_t       *priv   = filp->private_data;
132         drm_device_t     *dev    = priv->dev;
133
134         if (!dev->agp || !dev->agp->acquired || !drm_agp->release)
135                 return -EINVAL;
136         drm_agp->release();
137         dev->agp->acquired = 0;
138         return 0;
139
140 }
141
142 /**
143  * Release the AGP device.
144  *
145  * Calls drm_agp->release().
146  */
147 void DRM(agp_do_release)(void)
148 {
149         if (drm_agp->release)
150                 drm_agp->release();
151 }
152
153 /**
154  * Enable the AGP bus.
155  * 
156  * \param inode device inode.
157  * \param filp file pointer.
158  * \param cmd command.
159  * \param arg pointer to a drm_agp_mode structure.
160  * \return zero on success or a negative number on failure.
161  *
162  * Verifies the AGP device has been acquired but not enabled, and calls
163  * drm_agp->enable().
164  */
165 int DRM(agp_enable)(struct inode *inode, struct file *filp,
166                     unsigned int cmd, unsigned long arg)
167 {
168         drm_file_t       *priv   = filp->private_data;
169         drm_device_t     *dev    = priv->dev;
170         drm_agp_mode_t   mode;
171
172         if (!dev->agp || !dev->agp->acquired || !drm_agp->enable)
173                 return -EINVAL;
174
175         if (copy_from_user(&mode, (drm_agp_mode_t __user *)arg, sizeof(mode)))
176                 return -EFAULT;
177
178         dev->agp->mode    = mode.mode;
179         drm_agp->enable(mode.mode);
180         dev->agp->base    = dev->agp->agp_info.aper_base;
181         dev->agp->enabled = 1;
182         return 0;
183 }
184
185 /**
186  * Allocate AGP memory.
187  *
188  * \param inode device inode.
189  * \param filp file pointer.
190  * \param cmd command.
191  * \param arg pointer to a drm_agp_buffer structure.
192  * \return zero on success or a negative number on failure.
193  * 
194  * Verifies the AGP device is present and has been acquired, allocates the
195  * memory via alloc_agp() and creates a drm_agp_mem entry for it.
196  */
197 int DRM(agp_alloc)(struct inode *inode, struct file *filp,
198                    unsigned int cmd, unsigned long arg)
199 {
200         drm_file_t       *priv   = filp->private_data;
201         drm_device_t     *dev    = priv->dev;
202         drm_agp_buffer_t request;
203         drm_agp_mem_t    *entry;
204         DRM_AGP_MEM      *memory;
205         unsigned long    pages;
206         u32              type;
207         drm_agp_buffer_t __user *argp = (void __user *)arg;
208
209         if (!dev->agp || !dev->agp->acquired)
210                 return -EINVAL;
211         if (copy_from_user(&request, argp, sizeof(request)))
212                 return -EFAULT;
213         if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
214                 return -ENOMEM;
215
216         memset(entry, 0, sizeof(*entry));
217
218         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
219         type = (u32) request.type;
220
221         if (!(memory = DRM(alloc_agp)(pages, type))) {
222                 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
223                 return -ENOMEM;
224         }
225
226         entry->handle    = (unsigned long)memory->key + 1;
227         entry->memory    = memory;
228         entry->bound     = 0;
229         entry->pages     = pages;
230         entry->prev      = NULL;
231         entry->next      = dev->agp->memory;
232         if (dev->agp->memory)
233                 dev->agp->memory->prev = entry;
234         dev->agp->memory = entry;
235
236         request.handle   = entry->handle;
237         request.physical = memory->physical;
238
239         if (copy_to_user(argp, &request, sizeof(request))) {
240                 dev->agp->memory       = entry->next;
241                 dev->agp->memory->prev = NULL;
242                 DRM(free_agp)(memory, pages);
243                 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
244                 return -EFAULT;
245         }
246         return 0;
247 }
248
249 /**
250  * Search for the AGP memory entry associated with a handle.
251  *
252  * \param dev DRM device structure.
253  * \param handle AGP memory handle.
254  * \return pointer to the drm_agp_mem structure associated with \p handle.
255  * 
256  * Walks through drm_agp_head::memory until finding a matching handle.
257  */
258 static drm_agp_mem_t *DRM(agp_lookup_entry)(drm_device_t *dev,
259                                             unsigned long handle)
260 {
261         drm_agp_mem_t *entry;
262
263         for (entry = dev->agp->memory; entry; entry = entry->next) {
264                 if (entry->handle == handle)
265                         return entry;
266         }
267         return NULL;
268 }
269
270 /**
271  * Unbind AGP memory from the GATT (ioctl).
272  *
273  * \param inode device inode.
274  * \param filp file pointer.
275  * \param cmd command.
276  * \param arg pointer to a drm_agp_binding structure.
277  * \return zero on success or a negative number on failure.
278  *
279  * Verifies the AGP device is present and acquired, looks-up the AGP memory
280  * entry and passes it to the unbind_agp() function.
281  */
282 int DRM(agp_unbind)(struct inode *inode, struct file *filp,
283                     unsigned int cmd, unsigned long arg)
284 {
285         drm_file_t        *priv  = filp->private_data;
286         drm_device_t      *dev   = priv->dev;
287         drm_agp_binding_t request;
288         drm_agp_mem_t     *entry;
289         int ret;
290
291         if (!dev->agp || !dev->agp->acquired)
292                 return -EINVAL;
293         if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
294                 return -EFAULT;
295         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
296                 return -EINVAL;
297         if (!entry->bound)
298                 return -EINVAL;
299         ret = DRM(unbind_agp)(entry->memory);
300         if (ret == 0)
301             entry->bound = 0;
302         return ret;
303 }
304
305 /**
306  * Bind AGP memory into the GATT (ioctl)
307  *
308  * \param inode device inode.
309  * \param filp file pointer.
310  * \param cmd command.
311  * \param arg pointer to a drm_agp_binding structure.
312  * \return zero on success or a negative number on failure.
313  *
314  * Verifies the AGP device is present and has been acquired and that no memory
315  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
316  * it to bind_agp() function.
317  */
318 int DRM(agp_bind)(struct inode *inode, struct file *filp,
319                   unsigned int cmd, unsigned long arg)
320 {
321         drm_file_t        *priv  = filp->private_data;
322         drm_device_t      *dev   = priv->dev;
323         drm_agp_binding_t request;
324         drm_agp_mem_t     *entry;
325         int               retcode;
326         int               page;
327
328         if (!dev->agp || !dev->agp->acquired || !drm_agp->bind_memory)
329                 return -EINVAL;
330         if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
331                 return -EFAULT;
332         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
333                 return -EINVAL;
334         if (entry->bound)
335                 return -EINVAL;
336         page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
337         if ((retcode = DRM(bind_agp)(entry->memory, page)))
338                 return retcode;
339         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
340         DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
341                   dev->agp->base, entry->bound);
342         return 0;
343 }
344
345 /**
346  * Free AGP memory (ioctl).
347  *
348  * \param inode device inode.
349  * \param filp file pointer.
350  * \param cmd command.
351  * \param arg pointer to a drm_agp_buffer structure.
352  * \return zero on success or a negative number on failure.
353  *
354  * Verifies the AGP device is present and has been acquired and looks up the
355  * AGP memory entry. If the memory it's currently bound, unbind it via
356  * unbind_agp(). Frees it via free_agp() as well as the entry itself
357  * and unlinks from the doubly linked list it's inserted in.
358  */
359 int DRM(agp_free)(struct inode *inode, struct file *filp,
360                   unsigned int cmd, unsigned long arg)
361 {
362         drm_file_t       *priv   = filp->private_data;
363         drm_device_t     *dev    = priv->dev;
364         drm_agp_buffer_t request;
365         drm_agp_mem_t    *entry;
366
367         if (!dev->agp || !dev->agp->acquired)
368                 return -EINVAL;
369         if (copy_from_user(&request, (drm_agp_buffer_t __user *)arg, sizeof(request)))
370                 return -EFAULT;
371         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
372                 return -EINVAL;
373         if (entry->bound)
374                 DRM(unbind_agp)(entry->memory);
375
376         if (entry->prev)
377                 entry->prev->next = entry->next;
378         else
379                 dev->agp->memory = entry->next;
380
381         if (entry->next)
382                 entry->next->prev = entry->prev;
383
384         DRM(free_agp)(entry->memory, entry->pages);
385         DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
386         return 0;
387 }
388
389 /**
390  * Initialize the AGP resources.
391  *
392  * \return pointer to a drm_agp_head structure.
393  *
394  * Gets the drm_agp_t structure which is made available by the agpgart module
395  * via the inter_module_* functions. Creates and initializes a drm_agp_head
396  * structure.
397  */
398 drm_agp_head_t *DRM(agp_init)(void)
399 {
400         drm_agp_head_t *head         = NULL;
401
402         drm_agp = DRM_AGP_GET;
403         if (drm_agp) {
404                 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
405                         return NULL;
406                 memset((void *)head, 0, sizeof(*head));
407                 drm_agp->copy_info(&head->agp_info);
408                 if (head->agp_info.chipset == NOT_SUPPORTED) {
409                         DRM(free)(head, sizeof(*head), DRM_MEM_AGPLISTS);
410                         return NULL;
411                 }
412                 head->memory = NULL;
413 #if LINUX_VERSION_CODE <= 0x020408
414                 head->cant_use_aperture = 0;
415                 head->page_mask = ~(0xfff);
416 #else
417                 head->cant_use_aperture = head->agp_info.cant_use_aperture;
418                 head->page_mask = head->agp_info.page_mask;
419 #endif
420         }
421         return head;
422 }
423
424 /**
425  * Free the AGP resources.
426  *
427  * Releases the pointer in ::drm_agp.
428  */
429 void DRM(agp_uninit)(void)
430 {
431         DRM_AGP_PUT;
432         drm_agp = NULL;
433 }
434
435 /** Calls drm_agp->allocate_memory() */
436 DRM_AGP_MEM *DRM(agp_allocate_memory)(size_t pages, u32 type)
437 {
438         if (!drm_agp->allocate_memory)
439                 return NULL;
440         return drm_agp->allocate_memory(pages, type);
441 }
442
443 /** Calls drm_agp->free_memory() */
444 int DRM(agp_free_memory)(DRM_AGP_MEM *handle)
445 {
446         if (!handle || !drm_agp->free_memory)
447                 return 0;
448         drm_agp->free_memory(handle);
449         return 1;
450 }
451
452 /** Calls drm_agp->bind_memory() */
453 int DRM(agp_bind_memory)(DRM_AGP_MEM *handle, off_t start)
454 {
455         if (!handle || !drm_agp->bind_memory)
456                 return -EINVAL;
457         return drm_agp->bind_memory(handle, start);
458 }
459
460 /** Calls drm_agp->unbind_memory() */
461 int DRM(agp_unbind_memory)(DRM_AGP_MEM *handle)
462 {
463         if (!handle || !drm_agp->unbind_memory)
464                 return -EINVAL;
465         return drm_agp->unbind_memory(handle);
466 }
467
468 #endif /* __OS_HAS_AGP */