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