[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / drivers / char / drm / drm_agpsupport.c
blob8d94c0b5fa44f00c0859b3c63e1103d5876bc6bc
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 */
9 /*
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
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:
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.
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.
34 #include "drmP.h"
35 #include <linux/module.h>
37 #if __OS_HAS_AGP
39 /**
40 * AGP information ioctl.
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.
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.
51 int drm_agp_info(struct inode *inode, struct file *filp,
52 unsigned int cmd, unsigned long arg)
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;
59 if (!dev->agp || !dev->agp->acquired)
60 return -EINVAL;
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;
73 if (copy_to_user((drm_agp_info_t __user *)arg, &info, sizeof(info)))
74 return -EFAULT;
75 return 0;
78 /**
79 * Acquire the AGP device (ioctl).
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.
87 * Verifies the AGP device hasn't been acquired before and calls
88 * agp_acquire().
90 int drm_agp_acquire(struct inode *inode, struct file *filp,
91 unsigned int cmd, unsigned long arg)
93 drm_file_t *priv = filp->private_data;
94 drm_device_t *dev = priv->head->dev;
96 if (!dev->agp)
97 return -ENODEV;
98 if (dev->agp->acquired)
99 return -EBUSY;
100 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
101 return -ENODEV;
102 dev->agp->acquired = 1;
103 return 0;
107 * Release the AGP device (ioctl).
109 * \param inode device inode.
110 * \param filp file pointer.
111 * \param cmd command.
112 * \param arg user argument.
113 * \return zero on success or a negative number on failure.
115 * Verifies the AGP device has been acquired and calls agp_backend_release().
117 int drm_agp_release(struct inode *inode, struct file *filp,
118 unsigned int cmd, unsigned long arg)
120 drm_file_t *priv = filp->private_data;
121 drm_device_t *dev = priv->head->dev;
123 if (!dev->agp || !dev->agp->acquired)
124 return -EINVAL;
125 agp_backend_release(dev->agp->bridge);
126 dev->agp->acquired = 0;
127 return 0;
132 * Release the AGP device.
134 * Calls agp_backend_release().
136 void drm_agp_do_release(drm_device_t *dev)
138 agp_backend_release(dev->agp->bridge);
142 * Enable the AGP bus.
144 * \param inode device inode.
145 * \param filp file pointer.
146 * \param cmd command.
147 * \param arg pointer to a drm_agp_mode structure.
148 * \return zero on success or a negative number on failure.
150 * Verifies the AGP device has been acquired but not enabled, and calls
151 * agp_enable().
153 int drm_agp_enable(struct inode *inode, struct file *filp,
154 unsigned int cmd, unsigned long arg)
156 drm_file_t *priv = filp->private_data;
157 drm_device_t *dev = priv->head->dev;
158 drm_agp_mode_t mode;
160 if (!dev->agp || !dev->agp->acquired)
161 return -EINVAL;
163 if (copy_from_user(&mode, (drm_agp_mode_t __user *)arg, sizeof(mode)))
164 return -EFAULT;
166 dev->agp->mode = mode.mode;
167 agp_enable(dev->agp->bridge, mode.mode);
168 dev->agp->base = dev->agp->agp_info.aper_base;
169 dev->agp->enabled = 1;
170 return 0;
174 * Allocate AGP memory.
176 * \param inode device inode.
177 * \param filp file pointer.
178 * \param cmd command.
179 * \param arg pointer to a drm_agp_buffer structure.
180 * \return zero on success or a negative number on failure.
182 * Verifies the AGP device is present and has been acquired, allocates the
183 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
185 int drm_agp_alloc(struct inode *inode, struct file *filp,
186 unsigned int cmd, unsigned long arg)
188 drm_file_t *priv = filp->private_data;
189 drm_device_t *dev = priv->head->dev;
190 drm_agp_buffer_t request;
191 drm_agp_mem_t *entry;
192 DRM_AGP_MEM *memory;
193 unsigned long pages;
194 u32 type;
195 drm_agp_buffer_t __user *argp = (void __user *)arg;
197 if (!dev->agp || !dev->agp->acquired)
198 return -EINVAL;
199 if (copy_from_user(&request, argp, sizeof(request)))
200 return -EFAULT;
201 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
202 return -ENOMEM;
204 memset(entry, 0, sizeof(*entry));
206 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
207 type = (u32) request.type;
209 if (!(memory = drm_alloc_agp(dev->agp->bridge, pages, type))) {
210 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
211 return -ENOMEM;
214 entry->handle = (unsigned long)memory->key + 1;
215 entry->memory = memory;
216 entry->bound = 0;
217 entry->pages = pages;
218 entry->prev = NULL;
219 entry->next = dev->agp->memory;
220 if (dev->agp->memory)
221 dev->agp->memory->prev = entry;
222 dev->agp->memory = entry;
224 request.handle = entry->handle;
225 request.physical = memory->physical;
227 if (copy_to_user(argp, &request, sizeof(request))) {
228 dev->agp->memory = entry->next;
229 dev->agp->memory->prev = NULL;
230 drm_free_agp(memory, pages);
231 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
232 return -EFAULT;
234 return 0;
238 * Search for the AGP memory entry associated with a handle.
240 * \param dev DRM device structure.
241 * \param handle AGP memory handle.
242 * \return pointer to the drm_agp_mem structure associated with \p handle.
244 * Walks through drm_agp_head::memory until finding a matching handle.
246 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
247 unsigned long handle)
249 drm_agp_mem_t *entry;
251 for (entry = dev->agp->memory; entry; entry = entry->next) {
252 if (entry->handle == handle)
253 return entry;
255 return NULL;
259 * Unbind AGP memory from the GATT (ioctl).
261 * \param inode device inode.
262 * \param filp file pointer.
263 * \param cmd command.
264 * \param arg pointer to a drm_agp_binding structure.
265 * \return zero on success or a negative number on failure.
267 * Verifies the AGP device is present and acquired, looks-up the AGP memory
268 * entry and passes it to the unbind_agp() function.
270 int drm_agp_unbind(struct inode *inode, struct file *filp,
271 unsigned int cmd, unsigned long arg)
273 drm_file_t *priv = filp->private_data;
274 drm_device_t *dev = priv->head->dev;
275 drm_agp_binding_t request;
276 drm_agp_mem_t *entry;
277 int ret;
279 if (!dev->agp || !dev->agp->acquired)
280 return -EINVAL;
281 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
282 return -EFAULT;
283 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
284 return -EINVAL;
285 if (!entry->bound)
286 return -EINVAL;
287 ret = drm_unbind_agp(entry->memory);
288 if (ret == 0)
289 entry->bound = 0;
290 return ret;
294 * Bind AGP memory into the GATT (ioctl)
296 * \param inode device inode.
297 * \param filp file pointer.
298 * \param cmd command.
299 * \param arg pointer to a drm_agp_binding structure.
300 * \return zero on success or a negative number on failure.
302 * Verifies the AGP device is present and has been acquired and that no memory
303 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
304 * it to bind_agp() function.
306 int drm_agp_bind(struct inode *inode, struct file *filp,
307 unsigned int cmd, unsigned long arg)
309 drm_file_t *priv = filp->private_data;
310 drm_device_t *dev = priv->head->dev;
311 drm_agp_binding_t request;
312 drm_agp_mem_t *entry;
313 int retcode;
314 int page;
316 if (!dev->agp || !dev->agp->acquired)
317 return -EINVAL;
318 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
319 return -EFAULT;
320 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
321 return -EINVAL;
322 if (entry->bound)
323 return -EINVAL;
324 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
325 if ((retcode = drm_bind_agp(entry->memory, page)))
326 return retcode;
327 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
328 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
329 dev->agp->base, entry->bound);
330 return 0;
334 * Free AGP memory (ioctl).
336 * \param inode device inode.
337 * \param filp file pointer.
338 * \param cmd command.
339 * \param arg pointer to a drm_agp_buffer structure.
340 * \return zero on success or a negative number on failure.
342 * Verifies the AGP device is present and has been acquired and looks up the
343 * AGP memory entry. If the memory it's currently bound, unbind it via
344 * unbind_agp(). Frees it via free_agp() as well as the entry itself
345 * and unlinks from the doubly linked list it's inserted in.
347 int drm_agp_free(struct inode *inode, struct file *filp,
348 unsigned int cmd, unsigned long arg)
350 drm_file_t *priv = filp->private_data;
351 drm_device_t *dev = priv->head->dev;
352 drm_agp_buffer_t request;
353 drm_agp_mem_t *entry;
355 if (!dev->agp || !dev->agp->acquired)
356 return -EINVAL;
357 if (copy_from_user(&request, (drm_agp_buffer_t __user *)arg, sizeof(request)))
358 return -EFAULT;
359 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
360 return -EINVAL;
361 if (entry->bound)
362 drm_unbind_agp(entry->memory);
364 if (entry->prev)
365 entry->prev->next = entry->next;
366 else
367 dev->agp->memory = entry->next;
369 if (entry->next)
370 entry->next->prev = entry->prev;
372 drm_free_agp(entry->memory, entry->pages);
373 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
374 return 0;
378 * Initialize the AGP resources.
380 * \return pointer to a drm_agp_head structure.
383 drm_agp_head_t *drm_agp_init(drm_device_t *dev)
385 drm_agp_head_t *head = NULL;
387 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
388 return NULL;
389 memset((void *)head, 0, sizeof(*head));
390 head->bridge = agp_find_bridge(dev->pdev);
391 if (!head->bridge) {
392 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
393 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
394 return NULL;
396 agp_copy_info(head->bridge, &head->agp_info);
397 agp_backend_release(head->bridge);
398 } else {
399 agp_copy_info(head->bridge, &head->agp_info);
401 if (head->agp_info.chipset == NOT_SUPPORTED) {
402 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
403 return NULL;
405 head->memory = NULL;
406 #if LINUX_VERSION_CODE <= 0x020408
407 head->cant_use_aperture = 0;
408 head->page_mask = ~(0xfff);
409 #else
410 head->cant_use_aperture = head->agp_info.cant_use_aperture;
411 head->page_mask = head->agp_info.page_mask;
412 #endif
414 return head;
417 /** Calls agp_allocate_memory() */
418 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type)
420 return agp_allocate_memory(bridge, pages, type);
423 /** Calls agp_free_memory() */
424 int drm_agp_free_memory(DRM_AGP_MEM *handle)
426 if (!handle)
427 return 0;
428 agp_free_memory(handle);
429 return 1;
432 /** Calls agp_bind_memory() */
433 int drm_agp_bind_memory(DRM_AGP_MEM *handle, off_t start)
435 if (!handle)
436 return -EINVAL;
437 return agp_bind_memory(handle, start);
440 /** Calls agp_unbind_memory() */
441 int drm_agp_unbind_memory(DRM_AGP_MEM *handle)
443 if (!handle)
444 return -EINVAL;
445 return agp_unbind_memory(handle);
448 #endif /* __OS_HAS_AGP */