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