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