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