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