Merge branch 'drm-patches' of ssh://master.kernel.org/pub/scm/linux/kernel/git/airlie...
[sfrench/cifs-2.6.git] / drivers / char / drm / 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  * Get AGP information.
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 drm_device *dev, struct drm_agp_info *info)
52 {
53         DRM_AGP_KERN *kern;
54
55         if (!dev->agp || !dev->agp->acquired)
56                 return -EINVAL;
57
58         kern = &dev->agp->agp_info;
59         info->agp_version_major = kern->version.major;
60         info->agp_version_minor = kern->version.minor;
61         info->mode = kern->mode;
62         info->aperture_base = kern->aper_base;
63         info->aperture_size = kern->aper_size * 1024 * 1024;
64         info->memory_allowed = kern->max_memory << PAGE_SHIFT;
65         info->memory_used = kern->current_memory << PAGE_SHIFT;
66         info->id_vendor = kern->device->vendor;
67         info->id_device = kern->device->device;
68
69         return 0;
70 }
71
72 EXPORT_SYMBOL(drm_agp_info);
73
74 int drm_agp_info_ioctl(struct inode *inode, struct file *filp,
75                        unsigned int cmd, unsigned long arg)
76 {
77         struct drm_file *priv = filp->private_data;
78         struct drm_device *dev = priv->head->dev;
79         struct drm_agp_info info;
80         int err;
81
82         err = drm_agp_info(dev, &info);
83         if (err)
84                 return err;
85
86         if (copy_to_user((struct drm_agp_info __user *) arg, &info, sizeof(info)))
87                 return -EFAULT;
88         return 0;
89 }
90
91 /**
92  * Acquire the AGP device.
93  *
94  * \param dev DRM device that is to acquire AGP.
95  * \return zero on success or a negative number on failure.
96  *
97  * Verifies the AGP device hasn't been acquired before and calls
98  * \c agp_backend_acquire.
99  */
100 int drm_agp_acquire(struct drm_device * dev)
101 {
102         if (!dev->agp)
103                 return -ENODEV;
104         if (dev->agp->acquired)
105                 return -EBUSY;
106         if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
107                 return -ENODEV;
108         dev->agp->acquired = 1;
109         return 0;
110 }
111
112 EXPORT_SYMBOL(drm_agp_acquire);
113
114 /**
115  * Acquire 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 hasn't been acquired before and calls
124  * \c agp_backend_acquire.
125  */
126 int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp,
127                           unsigned int cmd, unsigned long arg)
128 {
129         struct drm_file *priv = filp->private_data;
130
131         return drm_agp_acquire((struct drm_device *) priv->head->dev);
132 }
133
134 /**
135  * Release the AGP device.
136  *
137  * \param dev DRM device that is to release AGP.
138  * \return zero on success or a negative number on failure.
139  *
140  * Verifies the AGP device has been acquired and calls \c agp_backend_release.
141  */
142 int drm_agp_release(struct drm_device * dev)
143 {
144         if (!dev->agp || !dev->agp->acquired)
145                 return -EINVAL;
146         agp_backend_release(dev->agp->bridge);
147         dev->agp->acquired = 0;
148         return 0;
149 }
150 EXPORT_SYMBOL(drm_agp_release);
151
152 int drm_agp_release_ioctl(struct inode *inode, struct file *filp,
153                           unsigned int cmd, unsigned long arg)
154 {
155         struct drm_file *priv = filp->private_data;
156         struct drm_device *dev = priv->head->dev;
157
158         return drm_agp_release(dev);
159 }
160
161 /**
162  * Enable the AGP bus.
163  *
164  * \param dev DRM device that has previously acquired AGP.
165  * \param mode Requested AGP mode.
166  * \return zero on success or a negative number on failure.
167  *
168  * Verifies the AGP device has been acquired but not enabled, and calls
169  * \c agp_enable.
170  */
171 int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
172 {
173         if (!dev->agp || !dev->agp->acquired)
174                 return -EINVAL;
175
176         dev->agp->mode = mode.mode;
177         agp_enable(dev->agp->bridge, mode.mode);
178         dev->agp->base = dev->agp->agp_info.aper_base;
179         dev->agp->enabled = 1;
180         return 0;
181 }
182
183 EXPORT_SYMBOL(drm_agp_enable);
184
185 int drm_agp_enable_ioctl(struct inode *inode, struct file *filp,
186                          unsigned int cmd, unsigned long arg)
187 {
188         struct drm_file *priv = filp->private_data;
189         struct drm_device *dev = priv->head->dev;
190         struct drm_agp_mode mode;
191
192         if (copy_from_user(&mode, (struct drm_agp_mode __user *) arg, sizeof(mode)))
193                 return -EFAULT;
194
195         return drm_agp_enable(dev, mode);
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 drm_device *dev, struct drm_agp_buffer *request)
211 {
212         struct drm_agp_mem *entry;
213         DRM_AGP_MEM *memory;
214         unsigned long pages;
215         u32 type;
216
217         if (!dev->agp || !dev->agp->acquired)
218                 return -EINVAL;
219         if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
220                 return -ENOMEM;
221
222         memset(entry, 0, sizeof(*entry));
223
224         pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
225         type = (u32) request->type;
226         if (!(memory = drm_alloc_agp(dev, pages, type))) {
227                 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
228                 return -ENOMEM;
229         }
230
231         entry->handle = (unsigned long)memory->key + 1;
232         entry->memory = memory;
233         entry->bound = 0;
234         entry->pages = pages;
235         list_add(&entry->head, &dev->agp->memory);
236
237         request->handle = entry->handle;
238         request->physical = memory->physical;
239
240         return 0;
241 }
242 EXPORT_SYMBOL(drm_agp_alloc);
243
244 int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp,
245                         unsigned int cmd, unsigned long arg)
246 {
247         struct drm_file *priv = filp->private_data;
248         struct drm_device *dev = priv->head->dev;
249         struct drm_agp_buffer request;
250         struct drm_agp_buffer __user *argp = (void __user *)arg;
251         int err;
252
253         if (copy_from_user(&request, argp, sizeof(request)))
254                 return -EFAULT;
255
256         err = drm_agp_alloc(dev, &request);
257         if (err)
258                 return err;
259
260         if (copy_to_user(argp, &request, sizeof(request))) {
261                 struct drm_agp_mem *entry;
262                 list_for_each_entry(entry, &dev->agp->memory, head) {
263                         if (entry->handle == request.handle)
264                                 break;
265                 }
266                 list_del(&entry->head);
267                 drm_free_agp(entry->memory, entry->pages);
268                 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
269                 return -EFAULT;
270         }
271
272         return 0;
273 }
274
275 /**
276  * Search for the AGP memory entry associated with a handle.
277  *
278  * \param dev DRM device structure.
279  * \param handle AGP memory handle.
280  * \return pointer to the drm_agp_mem structure associated with \p handle.
281  *
282  * Walks through drm_agp_head::memory until finding a matching handle.
283  */
284 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
285                                            unsigned long handle)
286 {
287         struct drm_agp_mem *entry;
288
289         list_for_each_entry(entry, &dev->agp->memory, head) {
290                 if (entry->handle == handle)
291                         return entry;
292         }
293         return NULL;
294 }
295
296 /**
297  * Unbind AGP memory from the GATT (ioctl).
298  *
299  * \param inode device inode.
300  * \param filp file pointer.
301  * \param cmd command.
302  * \param arg pointer to a drm_agp_binding structure.
303  * \return zero on success or a negative number on failure.
304  *
305  * Verifies the AGP device is present and acquired, looks-up the AGP memory
306  * entry and passes it to the unbind_agp() function.
307  */
308 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
309 {
310         struct drm_agp_mem *entry;
311         int ret;
312
313         if (!dev->agp || !dev->agp->acquired)
314                 return -EINVAL;
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 EXPORT_SYMBOL(drm_agp_unbind);
325
326 int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp,
327                          unsigned int cmd, unsigned long arg)
328 {
329         struct drm_file *priv = filp->private_data;
330         struct drm_device *dev = priv->head->dev;
331         struct drm_agp_binding request;
332
333         if (copy_from_user
334             (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
335                 return -EFAULT;
336
337         return drm_agp_unbind(dev, &request);
338 }
339
340 /**
341  * Bind AGP memory into the GATT (ioctl)
342  *
343  * \param inode device inode.
344  * \param filp file pointer.
345  * \param cmd command.
346  * \param arg pointer to a drm_agp_binding structure.
347  * \return zero on success or a negative number on failure.
348  *
349  * Verifies the AGP device is present and has been acquired and that no memory
350  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
351  * it to bind_agp() function.
352  */
353 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
354 {
355         struct drm_agp_mem *entry;
356         int retcode;
357         int page;
358
359         if (!dev->agp || !dev->agp->acquired)
360                 return -EINVAL;
361         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
362                 return -EINVAL;
363         if (entry->bound)
364                 return -EINVAL;
365         page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
366         if ((retcode = drm_bind_agp(entry->memory, page)))
367                 return retcode;
368         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
369         DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
370                   dev->agp->base, entry->bound);
371         return 0;
372 }
373 EXPORT_SYMBOL(drm_agp_bind);
374
375 int drm_agp_bind_ioctl(struct inode *inode, struct file *filp,
376                        unsigned int cmd, unsigned long arg)
377 {
378         struct drm_file *priv = filp->private_data;
379         struct drm_device *dev = priv->head->dev;
380         struct drm_agp_binding request;
381
382         if (copy_from_user
383             (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
384                 return -EFAULT;
385
386         return drm_agp_bind(dev, &request);
387 }
388
389 /**
390  * Free AGP memory (ioctl).
391  *
392  * \param inode device inode.
393  * \param filp file pointer.
394  * \param cmd command.
395  * \param arg pointer to a drm_agp_buffer structure.
396  * \return zero on success or a negative number on failure.
397  *
398  * Verifies the AGP device is present and has been acquired and looks up the
399  * AGP memory entry. If the memory it's currently bound, unbind it via
400  * unbind_agp(). Frees it via free_agp() as well as the entry itself
401  * and unlinks from the doubly linked list it's inserted in.
402  */
403 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
404 {
405         struct drm_agp_mem *entry;
406
407         if (!dev->agp || !dev->agp->acquired)
408                 return -EINVAL;
409         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
410                 return -EINVAL;
411         if (entry->bound)
412                 drm_unbind_agp(entry->memory);
413
414         list_del(&entry->head);
415
416         drm_free_agp(entry->memory, entry->pages);
417         drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
418         return 0;
419 }
420 EXPORT_SYMBOL(drm_agp_free);
421
422 int drm_agp_free_ioctl(struct inode *inode, struct file *filp,
423                        unsigned int cmd, unsigned long arg)
424 {
425         struct drm_file *priv = filp->private_data;
426         struct drm_device *dev = priv->head->dev;
427         struct drm_agp_buffer request;
428
429         if (copy_from_user
430             (&request, (struct drm_agp_buffer __user *) arg, sizeof(request)))
431                 return -EFAULT;
432
433         return drm_agp_free(dev, &request);
434 }
435
436 /**
437  * Initialize the AGP resources.
438  *
439  * \return pointer to a drm_agp_head structure.
440  *
441  * Gets the drm_agp_t structure which is made available by the agpgart module
442  * via the inter_module_* functions. Creates and initializes a drm_agp_head
443  * structure.
444  */
445 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
446 {
447         struct drm_agp_head *head = NULL;
448
449         if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
450                 return NULL;
451         memset((void *)head, 0, sizeof(*head));
452         head->bridge = agp_find_bridge(dev->pdev);
453         if (!head->bridge) {
454                 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
455                         drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
456                         return NULL;
457                 }
458                 agp_copy_info(head->bridge, &head->agp_info);
459                 agp_backend_release(head->bridge);
460         } else {
461                 agp_copy_info(head->bridge, &head->agp_info);
462         }
463         if (head->agp_info.chipset == NOT_SUPPORTED) {
464                 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
465                 return NULL;
466         }
467         INIT_LIST_HEAD(&head->memory);
468         head->cant_use_aperture = head->agp_info.cant_use_aperture;
469         head->page_mask = head->agp_info.page_mask;
470
471         return head;
472 }
473
474 /** Calls agp_allocate_memory() */
475 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge,
476                                      size_t pages, u32 type)
477 {
478         return agp_allocate_memory(bridge, pages, type);
479 }
480
481 /** Calls agp_free_memory() */
482 int drm_agp_free_memory(DRM_AGP_MEM * handle)
483 {
484         if (!handle)
485                 return 0;
486         agp_free_memory(handle);
487         return 1;
488 }
489
490 /** Calls agp_bind_memory() */
491 int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
492 {
493         if (!handle)
494                 return -EINVAL;
495         return agp_bind_memory(handle, start);
496 }
497
498 /** Calls agp_unbind_memory() */
499 int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
500 {
501         if (!handle)
502                 return -EINVAL;
503         return agp_unbind_memory(handle);
504 }
505
506 #endif                          /* __OS_HAS_AGP */