patch-2_6_7-vs1_9_1_12
[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 ((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 *)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
209         if (!dev->agp || !dev->agp->acquired)
210                 return -EINVAL;
211         if (copy_from_user(&request, (drm_agp_buffer_t *)arg, 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((drm_agp_buffer_t *)arg, &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 *)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 *)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 *)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 /* __REALLY_HAVE_AGP */