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