2 * \file drm_agpsupport.h
3 * DRM support for AGP/GART backend
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
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
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.
35 #include <linux/module.h>
40 * Get AGP information.
42 * \param inode device inode.
43 * \param filp file pointer.
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(drm_device_t *dev, drm_agp_info_t *info)
55 if (!dev->agp || !dev->agp->acquired)
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;
71 EXPORT_SYMBOL(drm_agp_info);
73 int drm_agp_info_ioctl(struct inode *inode, struct file *filp,
74 unsigned int cmd, unsigned long arg)
76 drm_file_t *priv = filp->private_data;
77 drm_device_t *dev = priv->head->dev;
81 err = drm_agp_info(dev, &info);
85 if (copy_to_user((drm_agp_info_t __user *) arg, &info, sizeof(info)))
91 * Acquire the AGP device.
93 * \param dev DRM device that is to acquire AGP
94 * \return zero on success or a negative number on failure.
96 * Verifies the AGP device hasn't been acquired before and calls
97 * \c agp_backend_acquire.
99 int drm_agp_acquire(drm_device_t *dev)
103 if (dev->agp->acquired)
105 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
107 dev->agp->acquired = 1;
110 EXPORT_SYMBOL(drm_agp_acquire);
113 * Acquire the AGP device (ioctl).
115 * \param inode device inode.
116 * \param filp file pointer.
117 * \param cmd command.
118 * \param arg user argument.
119 * \return zero on success or a negative number on failure.
121 * Verifies the AGP device hasn't been acquired before and calls
122 * \c agp_backend_acquire.
124 int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp,
125 unsigned int cmd, unsigned long arg)
127 drm_file_t *priv = filp->private_data;
129 return drm_agp_acquire( (drm_device_t *) priv->head->dev );
133 * Release the AGP device.
135 * \param dev DRM device that is to release AGP
136 * \return zero on success or a negative number on failure.
138 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
140 int drm_agp_release(drm_device_t *dev)
142 if (!dev->agp || !dev->agp->acquired)
144 agp_backend_release(dev->agp->bridge);
145 dev->agp->acquired = 0;
148 EXPORT_SYMBOL(drm_agp_release);
150 int drm_agp_release_ioctl(struct inode *inode, struct file *filp,
151 unsigned int cmd, unsigned long arg)
153 drm_file_t *priv = filp->private_data;
154 drm_device_t *dev = priv->head->dev;
156 return drm_agp_release(dev);
160 * Enable the AGP bus.
162 * \param dev DRM device that has previously acquired AGP.
163 * \param mode Requested AGP mode.
164 * \return zero on success or a negative number on failure.
166 * Verifies the AGP device has been acquired but not enabled, and calls
169 int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode)
171 if (!dev->agp || !dev->agp->acquired)
174 dev->agp->mode = mode.mode;
175 agp_enable(dev->agp->bridge, mode.mode);
176 dev->agp->base = dev->agp->agp_info.aper_base;
177 dev->agp->enabled = 1;
180 EXPORT_SYMBOL(drm_agp_enable);
182 int drm_agp_enable_ioctl(struct inode *inode, struct file *filp,
183 unsigned int cmd, unsigned long arg)
185 drm_file_t *priv = filp->private_data;
186 drm_device_t *dev = priv->head->dev;
190 if (copy_from_user(&mode, (drm_agp_mode_t __user *) arg, sizeof(mode)))
193 return drm_agp_enable(dev, mode);
197 * Allocate AGP memory.
199 * \param inode device inode.
200 * \param filp file pointer.
201 * \param cmd command.
202 * \param arg pointer to a drm_agp_buffer structure.
203 * \return zero on success or a negative number on failure.
205 * Verifies the AGP device is present and has been acquired, allocates the
206 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
208 int drm_agp_alloc(struct inode *inode, struct file *filp,
209 unsigned int cmd, unsigned long arg)
211 drm_file_t *priv = filp->private_data;
212 drm_device_t *dev = priv->head->dev;
213 drm_agp_buffer_t request;
214 drm_agp_mem_t *entry;
218 drm_agp_buffer_t __user *argp = (void __user *)arg;
220 if (!dev->agp || !dev->agp->acquired)
222 if (copy_from_user(&request, argp, sizeof(request)))
224 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
227 memset(entry, 0, sizeof(*entry));
229 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
230 type = (u32) request.type;
232 if (!(memory = drm_alloc_agp(dev, pages, type))) {
233 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
237 entry->handle = (unsigned long)memory->key + 1;
238 entry->memory = memory;
240 entry->pages = pages;
242 entry->next = dev->agp->memory;
243 if (dev->agp->memory)
244 dev->agp->memory->prev = entry;
245 dev->agp->memory = entry;
247 request.handle = entry->handle;
248 request.physical = memory->physical;
250 if (copy_to_user(argp, &request, sizeof(request))) {
251 dev->agp->memory = entry->next;
252 dev->agp->memory->prev = NULL;
253 drm_free_agp(memory, pages);
254 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
261 * Search for the AGP memory entry associated with a handle.
263 * \param dev DRM device structure.
264 * \param handle AGP memory handle.
265 * \return pointer to the drm_agp_mem structure associated with \p handle.
267 * Walks through drm_agp_head::memory until finding a matching handle.
269 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
270 unsigned long handle)
272 drm_agp_mem_t *entry;
274 for (entry = dev->agp->memory; entry; entry = entry->next) {
275 if (entry->handle == handle)
282 * Unbind AGP memory from the GATT (ioctl).
284 * \param inode device inode.
285 * \param filp file pointer.
286 * \param cmd command.
287 * \param arg pointer to a drm_agp_binding structure.
288 * \return zero on success or a negative number on failure.
290 * Verifies the AGP device is present and acquired, looks-up the AGP memory
291 * entry and passes it to the unbind_agp() function.
293 int drm_agp_unbind(struct inode *inode, struct file *filp,
294 unsigned int cmd, unsigned long arg)
296 drm_file_t *priv = filp->private_data;
297 drm_device_t *dev = priv->head->dev;
298 drm_agp_binding_t request;
299 drm_agp_mem_t *entry;
302 if (!dev->agp || !dev->agp->acquired)
304 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
306 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
310 ret = drm_unbind_agp(entry->memory);
317 * Bind AGP memory into the GATT (ioctl)
319 * \param inode device inode.
320 * \param filp file pointer.
321 * \param cmd command.
322 * \param arg pointer to a drm_agp_binding structure.
323 * \return zero on success or a negative number on failure.
325 * Verifies the AGP device is present and has been acquired and that no memory
326 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
327 * it to bind_agp() function.
329 int drm_agp_bind(struct inode *inode, struct file *filp,
330 unsigned int cmd, unsigned long arg)
332 drm_file_t *priv = filp->private_data;
333 drm_device_t *dev = priv->head->dev;
334 drm_agp_binding_t request;
335 drm_agp_mem_t *entry;
339 if (!dev->agp || !dev->agp->acquired)
341 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
343 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
347 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
348 if ((retcode = drm_bind_agp(entry->memory, page)))
350 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
351 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
352 dev->agp->base, entry->bound);
357 * Free AGP memory (ioctl).
359 * \param inode device inode.
360 * \param filp file pointer.
361 * \param cmd command.
362 * \param arg pointer to a drm_agp_buffer structure.
363 * \return zero on success or a negative number on failure.
365 * Verifies the AGP device is present and has been acquired and looks up the
366 * AGP memory entry. If the memory it's currently bound, unbind it via
367 * unbind_agp(). Frees it via free_agp() as well as the entry itself
368 * and unlinks from the doubly linked list it's inserted in.
370 int drm_agp_free(struct inode *inode, struct file *filp,
371 unsigned int cmd, unsigned long arg)
373 drm_file_t *priv = filp->private_data;
374 drm_device_t *dev = priv->head->dev;
375 drm_agp_buffer_t request;
376 drm_agp_mem_t *entry;
378 if (!dev->agp || !dev->agp->acquired)
380 if (copy_from_user(&request, (drm_agp_buffer_t __user *)arg, sizeof(request)))
382 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
385 drm_unbind_agp(entry->memory);
388 entry->prev->next = entry->next;
390 dev->agp->memory = entry->next;
393 entry->next->prev = entry->prev;
395 drm_free_agp(entry->memory, entry->pages);
396 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
401 * Initialize the AGP resources.
403 * \return pointer to a drm_agp_head structure.
406 drm_agp_head_t *drm_agp_init(drm_device_t *dev)
408 drm_agp_head_t *head = NULL;
410 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
412 memset((void *)head, 0, sizeof(*head));
413 head->bridge = agp_find_bridge(dev->pdev);
415 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
416 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
419 agp_copy_info(head->bridge, &head->agp_info);
420 agp_backend_release(head->bridge);
422 agp_copy_info(head->bridge, &head->agp_info);
424 if (head->agp_info.chipset == NOT_SUPPORTED) {
425 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
429 head->cant_use_aperture = head->agp_info.cant_use_aperture;
430 head->page_mask = head->agp_info.page_mask;
435 /** Calls agp_allocate_memory() */
436 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type)
438 return agp_allocate_memory(bridge, pages, type);
441 /** Calls agp_free_memory() */
442 int drm_agp_free_memory(DRM_AGP_MEM *handle)
446 agp_free_memory(handle);
450 /** Calls agp_bind_memory() */
451 int drm_agp_bind_memory(DRM_AGP_MEM *handle, off_t start)
455 return agp_bind_memory(handle, start);
457 EXPORT_SYMBOL(drm_agp_bind_memory);
459 /** Calls agp_unbind_memory() */
460 int drm_agp_unbind_memory(DRM_AGP_MEM *handle)
464 return agp_unbind_memory(handle);
467 #endif /* __OS_HAS_AGP */