More differentiated error codes for DRM(agp_acquire)
[platform/upstream/libdrm.git] / linux-core / 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 #define __NO_VERSION__
35 #include "drmP.h"
36 #include <linux/module.h>
37
38 #if __REALLY_HAVE_AGP
39
40
41 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
42 #define DRM_AGP_PUT inter_module_put("drm_agp")
43
44 /**
45  * Pointer to the drm_agp_t structure made available by the agpgart module.
46  */
47 static const drm_agp_t *drm_agp = NULL;
48
49 /**
50  * AGP information ioctl.
51  *
52  * \param inode device inode.
53  * \param filp file pointer.
54  * \param cmd command.
55  * \param arg pointer to a (output) drm_agp_info structure.
56  * \return zero on success or a negative number on failure.
57  *
58  * Verifies the AGP device has been initialized and acquired and fills in the
59  * drm_agp_info structure with the information in drm_agp_head::agp_info.
60  */
61 int DRM(agp_info)(struct inode *inode, struct file *filp,
62                   unsigned int cmd, unsigned long arg)
63 {
64         drm_file_t       *priv   = filp->private_data;
65         drm_device_t     *dev    = priv->dev;
66         DRM_AGP_KERN     *kern;
67         drm_agp_info_t   info;
68
69         if (!dev->agp || !dev->agp->acquired || !drm_agp->copy_info)
70                 return -EINVAL;
71
72         kern                   = &dev->agp->agp_info;
73         info.agp_version_major = kern->version.major;
74         info.agp_version_minor = kern->version.minor;
75         info.mode              = kern->mode;
76         info.aperture_base     = kern->aper_base;
77         info.aperture_size     = kern->aper_size * 1024 * 1024;
78         info.memory_allowed    = kern->max_memory << PAGE_SHIFT;
79         info.memory_used       = kern->current_memory << PAGE_SHIFT;
80         info.id_vendor         = kern->device->vendor;
81         info.id_device         = kern->device->device;
82
83         if (copy_to_user((drm_agp_info_t *)arg, &info, sizeof(info)))
84                 return -EFAULT;
85         return 0;
86 }
87
88 /**
89  * Acquire the AGP device (ioctl).
90  *
91  * \param inode device inode.
92  * \param filp file pointer.
93  * \param cmd command.
94  * \param arg user argument.
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  * drm_agp->acquire().
99  */
100 int DRM(agp_acquire)(struct inode *inode, struct file *filp,
101                      unsigned int cmd, unsigned long arg)
102 {
103         drm_file_t       *priv   = filp->private_data;
104         drm_device_t     *dev    = priv->dev;
105         int              retcode;
106
107         if (!dev->agp)
108                 return -ENODEV;
109         if (dev->agp->acquired)
110                 return -EBUSY;
111         if (!drm_agp->acquire)
112                 return -EINVAL;
113 #ifndef VMAP_4_ARGS
114         if ( dev->agp->cant_use_aperture )
115                 return -EINVAL;
116 #endif
117         if ((retcode = drm_agp->acquire()))
118                 return retcode;
119         dev->agp->acquired = 1;
120         return 0;
121 }
122
123 /**
124  * Release the AGP device (ioctl).
125  *
126  * \param inode device inode.
127  * \param filp file pointer.
128  * \param cmd command.
129  * \param arg user argument.
130  * \return zero on success or a negative number on failure.
131  *
132  * Verifies the AGP device has been acquired and calls drm_agp->release().
133  */
134 int DRM(agp_release)(struct inode *inode, struct file *filp,
135                      unsigned int cmd, unsigned long arg)
136 {
137         drm_file_t       *priv   = filp->private_data;
138         drm_device_t     *dev    = priv->dev;
139
140         if (!dev->agp || !dev->agp->acquired || !drm_agp->release)
141                 return -EINVAL;
142         drm_agp->release();
143         dev->agp->acquired = 0;
144         return 0;
145
146 }
147
148 /**
149  * Release the AGP device.
150  *
151  * Calls drm_agp->release().
152  */
153 void DRM(agp_do_release)(void)
154 {
155         if (drm_agp->release)
156                 drm_agp->release();
157 }
158
159 /**
160  * Enable the AGP bus.
161  * 
162  * \param inode device inode.
163  * \param filp file pointer.
164  * \param cmd command.
165  * \param arg pointer to a drm_agp_mode structure.
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  * drm_agp->enable().
170  */
171 int DRM(agp_enable)(struct inode *inode, struct file *filp,
172                     unsigned int cmd, unsigned long arg)
173 {
174         drm_file_t       *priv   = filp->private_data;
175         drm_device_t     *dev    = priv->dev;
176         drm_agp_mode_t   mode;
177
178         if (!dev->agp || !dev->agp->acquired || !drm_agp->enable)
179                 return -EINVAL;
180
181         if (copy_from_user(&mode, (drm_agp_mode_t *)arg, sizeof(mode)))
182                 return -EFAULT;
183
184         dev->agp->mode    = mode.mode;
185         drm_agp->enable(mode.mode);
186         dev->agp->base    = dev->agp->agp_info.aper_base;
187         dev->agp->enabled = 1;
188         return 0;
189 }
190
191 /**
192  * Allocate AGP memory.
193  *
194  * \param inode device inode.
195  * \param filp file pointer.
196  * \param cmd command.
197  * \param arg pointer to a drm_agp_buffer structure.
198  * \return zero on success or a negative number on failure.
199  * 
200  * Verifies the AGP device is present and has been acquired, allocates the
201  * memory via alloc_agp() and creates a drm_agp_mem entry for it.
202  */
203 int DRM(agp_alloc)(struct inode *inode, struct file *filp,
204                    unsigned int cmd, unsigned long arg)
205 {
206         drm_file_t       *priv   = filp->private_data;
207         drm_device_t     *dev    = priv->dev;
208         drm_agp_buffer_t request;
209         drm_agp_mem_t    *entry;
210         DRM_AGP_MEM      *memory;
211         unsigned long    pages;
212         u32              type;
213
214         if (!dev->agp || !dev->agp->acquired)
215                 return -EINVAL;
216         if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
217                 return -EFAULT;
218         if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
219                 return -ENOMEM;
220
221         memset(entry, 0, sizeof(*entry));
222
223         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
224         type = (u32) request.type;
225
226         if (!(memory = DRM(alloc_agp)(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         entry->prev      = NULL;
236         entry->next      = dev->agp->memory;
237         if (dev->agp->memory)
238                 dev->agp->memory->prev = entry;
239         dev->agp->memory = entry;
240
241         request.handle   = entry->handle;
242         request.physical = memory->physical;
243
244         if (copy_to_user((drm_agp_buffer_t *)arg, &request, sizeof(request))) {
245                 dev->agp->memory       = entry->next;
246                 dev->agp->memory->prev = NULL;
247                 DRM(free_agp)(memory, pages);
248                 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
249                 return -EFAULT;
250         }
251         return 0;
252 }
253
254 /**
255  * Search for the AGP memory entry associated with a handle.
256  *
257  * \param dev DRM device structure.
258  * \param handle AGP memory handle.
259  * \return pointer to the drm_agp_mem structure associated with \p handle.
260  * 
261  * Walks through drm_agp_head::memory until finding a matching handle.
262  */
263 static drm_agp_mem_t *DRM(agp_lookup_entry)(drm_device_t *dev,
264                                             unsigned long handle)
265 {
266         drm_agp_mem_t *entry;
267
268         for (entry = dev->agp->memory; entry; entry = entry->next) {
269                 if (entry->handle == handle)
270                         return entry;
271         }
272         return NULL;
273 }
274
275 /**
276  * Unbind AGP memory from the GATT (ioctl).
277  *
278  * \param inode device inode.
279  * \param filp file pointer.
280  * \param cmd command.
281  * \param arg pointer to a drm_agp_binding structure.
282  * \return zero on success or a negative number on failure.
283  *
284  * Verifies the AGP device is present and acquired, looks-up the AGP memory
285  * entry and passes it to the unbind_agp() function.
286  */
287 int DRM(agp_unbind)(struct inode *inode, struct file *filp,
288                     unsigned int cmd, unsigned long arg)
289 {
290         drm_file_t        *priv  = filp->private_data;
291         drm_device_t      *dev   = priv->dev;
292         drm_agp_binding_t request;
293         drm_agp_mem_t     *entry;
294         int ret;
295
296         if (!dev->agp || !dev->agp->acquired)
297                 return -EINVAL;
298         if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
299                 return -EFAULT;
300         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
301                 return -EINVAL;
302         if (!entry->bound)
303                 return -EINVAL;
304         ret = DRM(unbind_agp)(entry->memory);
305         if (ret == 0)
306             entry->bound = 0;
307         return ret;
308 }
309
310 /**
311  * Bind AGP memory into the GATT (ioctl)
312  *
313  * \param inode device inode.
314  * \param filp file pointer.
315  * \param cmd command.
316  * \param arg pointer to a drm_agp_binding structure.
317  * \return zero on success or a negative number on failure.
318  *
319  * Verifies the AGP device is present and has been acquired and that no memory
320  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
321  * it to bind_agp() function.
322  */
323 int DRM(agp_bind)(struct inode *inode, struct file *filp,
324                   unsigned int cmd, unsigned long arg)
325 {
326         drm_file_t        *priv  = filp->private_data;
327         drm_device_t      *dev   = priv->dev;
328         drm_agp_binding_t request;
329         drm_agp_mem_t     *entry;
330         int               retcode;
331         int               page;
332
333         if (!dev->agp || !dev->agp->acquired || !drm_agp->bind_memory)
334                 return -EINVAL;
335         if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
336                 return -EFAULT;
337         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
338                 return -EINVAL;
339         if (entry->bound)
340                 return -EINVAL;
341         page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
342         if ((retcode = DRM(bind_agp)(entry->memory, page)))
343                 return retcode;
344         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
345         DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
346                   dev->agp->base, entry->bound);
347         return 0;
348 }
349
350 /**
351  * Free AGP memory (ioctl).
352  *
353  * \param inode device inode.
354  * \param filp file pointer.
355  * \param cmd command.
356  * \param arg pointer to a drm_agp_buffer structure.
357  * \return zero on success or a negative number on failure.
358  *
359  * Verifies the AGP device is present and has been acquired and looks up the
360  * AGP memory entry. If the memory it's currently bound, unbind it via
361  * unbind_agp(). Frees it via free_agp() as well as the entry itself
362  * and unlinks from the doubly linked list it's inserted in.
363  */
364 int DRM(agp_free)(struct inode *inode, struct file *filp,
365                   unsigned int cmd, unsigned long arg)
366 {
367         drm_file_t       *priv   = filp->private_data;
368         drm_device_t     *dev    = priv->dev;
369         drm_agp_buffer_t request;
370         drm_agp_mem_t    *entry;
371
372         if (!dev->agp || !dev->agp->acquired)
373                 return -EINVAL;
374         if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
375                 return -EFAULT;
376         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
377                 return -EINVAL;
378         if (entry->bound)
379                 DRM(unbind_agp)(entry->memory);
380
381         if (entry->prev)
382                 entry->prev->next = entry->next;
383         else
384                 dev->agp->memory = entry->next;
385
386         if (entry->next)
387                 entry->next->prev = entry->prev;
388
389         DRM(free_agp)(entry->memory, entry->pages);
390         DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
391         return 0;
392 }
393
394 /**
395  * Initialize the AGP resources.
396  *
397  * \return pointer to a drm_agp_head structure.
398  *
399  * Gets the drm_agp_t structure which is made available by the agpgart module
400  * via the inter_module_* functions. Creates and initializes a drm_agp_head
401  * structure.
402  */
403 drm_agp_head_t *DRM(agp_init)(void)
404 {
405         drm_agp_head_t *head         = NULL;
406
407         drm_agp = DRM_AGP_GET;
408         if (drm_agp) {
409                 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
410                         return NULL;
411                 memset((void *)head, 0, sizeof(*head));
412                 drm_agp->copy_info(&head->agp_info);
413                 if (head->agp_info.chipset == NOT_SUPPORTED) {
414                         DRM(free)(head, sizeof(*head), DRM_MEM_AGPLISTS);
415                         return NULL;
416                 }
417                 head->memory = NULL;
418 #if LINUX_VERSION_CODE <= 0x020408
419                 head->cant_use_aperture = 0;
420                 head->page_mask = ~(0xfff);
421 #else
422                 head->cant_use_aperture = head->agp_info.cant_use_aperture;
423                 head->page_mask = head->agp_info.page_mask;
424 #endif
425         }
426         return head;
427 }
428
429 /**
430  * Free the AGP resources.
431  *
432  * Releases the pointer in ::drm_agp.
433  */
434 void DRM(agp_uninit)(void)
435 {
436         DRM_AGP_PUT;
437         drm_agp = NULL;
438 }
439
440 /** Calls drm_agp->allocate_memory() */
441 DRM_AGP_MEM *DRM(agp_allocate_memory)(size_t pages, u32 type)
442 {
443         if (!drm_agp->allocate_memory)
444                 return NULL;
445         return drm_agp->allocate_memory(pages, type);
446 }
447
448 /** Calls drm_agp->free_memory() */
449 int DRM(agp_free_memory)(DRM_AGP_MEM *handle)
450 {
451         if (!handle || !drm_agp->free_memory)
452                 return 0;
453         drm_agp->free_memory(handle);
454         return 1;
455 }
456
457 /** Calls drm_agp->bind_memory() */
458 int DRM(agp_bind_memory)(DRM_AGP_MEM *handle, off_t start)
459 {
460         if (!handle || !drm_agp->bind_memory)
461                 return -EINVAL;
462         return drm_agp->bind_memory(handle, start);
463 }
464
465 /** Calls drm_agp->unbind_memory() */
466 int DRM(agp_unbind_memory)(DRM_AGP_MEM *handle)
467 {
468         if (!handle || !drm_agp->unbind_memory)
469                 return -EINVAL;
470         return drm_agp->unbind_memory(handle);
471 }
472
473 #endif /* __REALLY_HAVE_AGP */