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 * AGP information ioctl.
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(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->dev;
59 if (!dev->agp || !dev->agp->acquired)
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)))
79 * Acquire the AGP device (ioctl).
81 * \param inode device inode.
82 * \param filp file pointer.
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
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->dev;
99 if (dev->agp->acquired)
101 if ((retcode = agp_backend_acquire()))
103 dev->agp->acquired = 1;
108 * Release the AGP device (ioctl).
110 * \param inode device inode.
111 * \param filp file pointer.
112 * \param cmd command.
113 * \param arg user argument.
114 * \return zero on success or a negative number on failure.
116 * Verifies the AGP device has been acquired and calls agp_backend_release().
118 int drm_agp_release(struct inode *inode, struct file *filp,
119 unsigned int cmd, unsigned long arg)
121 drm_file_t *priv = filp->private_data;
122 drm_device_t *dev = priv->dev;
124 if (!dev->agp || !dev->agp->acquired)
126 agp_backend_release();
127 dev->agp->acquired = 0;
133 * Release the AGP device.
135 * Calls agp_backend_release().
137 void drm_agp_do_release(void)
139 agp_backend_release();
143 * Enable the AGP bus.
145 * \param inode device inode.
146 * \param filp file pointer.
147 * \param cmd command.
148 * \param arg pointer to a drm_agp_mode structure.
149 * \return zero on success or a negative number on failure.
151 * Verifies the AGP device has been acquired but not enabled, and calls
154 int drm_agp_enable(struct inode *inode, struct file *filp,
155 unsigned int cmd, unsigned long arg)
157 drm_file_t *priv = filp->private_data;
158 drm_device_t *dev = priv->dev;
161 if (!dev->agp || !dev->agp->acquired)
164 if (copy_from_user(&mode, (drm_agp_mode_t __user *)arg, sizeof(mode)))
167 dev->agp->mode = mode.mode;
168 agp_enable(mode.mode);
169 dev->agp->base = dev->agp->agp_info.aper_base;
170 dev->agp->enabled = 1;
175 * Allocate AGP memory.
177 * \param inode device inode.
178 * \param filp file pointer.
179 * \param cmd command.
180 * \param arg pointer to a drm_agp_buffer structure.
181 * \return zero on success or a negative number on failure.
183 * Verifies the AGP device is present and has been acquired, allocates the
184 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
186 int drm_agp_alloc(struct inode *inode, struct file *filp,
187 unsigned int cmd, unsigned long arg)
189 drm_file_t *priv = filp->private_data;
190 drm_device_t *dev = priv->dev;
191 drm_agp_buffer_t request;
192 drm_agp_mem_t *entry;
196 drm_agp_buffer_t __user *argp = (void __user *)arg;
198 if (!dev->agp || !dev->agp->acquired)
200 if (copy_from_user(&request, argp, sizeof(request)))
202 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
205 memset(entry, 0, sizeof(*entry));
207 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
208 type = (u32) request.type;
210 if (!(memory = drm_alloc_agp(pages, type))) {
211 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
215 entry->handle = (unsigned long)memory->key + 1;
216 entry->memory = memory;
218 entry->pages = pages;
220 entry->next = dev->agp->memory;
221 if (dev->agp->memory)
222 dev->agp->memory->prev = entry;
223 dev->agp->memory = entry;
225 request.handle = entry->handle;
226 request.physical = memory->physical;
228 if (copy_to_user(argp, &request, sizeof(request))) {
229 dev->agp->memory = entry->next;
230 dev->agp->memory->prev = NULL;
231 drm_free_agp(memory, pages);
232 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
239 * Search for the AGP memory entry associated with a handle.
241 * \param dev DRM device structure.
242 * \param handle AGP memory handle.
243 * \return pointer to the drm_agp_mem structure associated with \p handle.
245 * Walks through drm_agp_head::memory until finding a matching handle.
247 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
248 unsigned long handle)
250 drm_agp_mem_t *entry;
252 for (entry = dev->agp->memory; entry; entry = entry->next) {
253 if (entry->handle == handle)
260 * Unbind AGP memory from the GATT (ioctl).
262 * \param inode device inode.
263 * \param filp file pointer.
264 * \param cmd command.
265 * \param arg pointer to a drm_agp_binding structure.
266 * \return zero on success or a negative number on failure.
268 * Verifies the AGP device is present and acquired, looks-up the AGP memory
269 * entry and passes it to the unbind_agp() function.
271 int drm_agp_unbind(struct inode *inode, struct file *filp,
272 unsigned int cmd, unsigned long arg)
274 drm_file_t *priv = filp->private_data;
275 drm_device_t *dev = priv->dev;
276 drm_agp_binding_t request;
277 drm_agp_mem_t *entry;
280 if (!dev->agp || !dev->agp->acquired)
282 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
284 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
288 ret = drm_unbind_agp(entry->memory);
295 * Bind AGP memory into the GATT (ioctl)
297 * \param inode device inode.
298 * \param filp file pointer.
299 * \param cmd command.
300 * \param arg pointer to a drm_agp_binding structure.
301 * \return zero on success or a negative number on failure.
303 * Verifies the AGP device is present and has been acquired and that no memory
304 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
305 * it to bind_agp() function.
307 int drm_agp_bind(struct inode *inode, struct file *filp,
308 unsigned int cmd, unsigned long arg)
310 drm_file_t *priv = filp->private_data;
311 drm_device_t *dev = priv->dev;
312 drm_agp_binding_t request;
313 drm_agp_mem_t *entry;
317 if (!dev->agp || !dev->agp->acquired)
319 if (copy_from_user(&request, (drm_agp_binding_t __user *)arg, sizeof(request)))
321 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
325 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
326 if ((retcode = drm_bind_agp(entry->memory, page)))
328 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
329 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
330 dev->agp->base, entry->bound);
335 * Free AGP memory (ioctl).
337 * \param inode device inode.
338 * \param filp file pointer.
339 * \param cmd command.
340 * \param arg pointer to a drm_agp_buffer structure.
341 * \return zero on success or a negative number on failure.
343 * Verifies the AGP device is present and has been acquired and looks up the
344 * AGP memory entry. If the memory it's currently bound, unbind it via
345 * unbind_agp(). Frees it via free_agp() as well as the entry itself
346 * and unlinks from the doubly linked list it's inserted in.
348 int drm_agp_free(struct inode *inode, struct file *filp,
349 unsigned int cmd, unsigned long arg)
351 drm_file_t *priv = filp->private_data;
352 drm_device_t *dev = priv->dev;
353 drm_agp_buffer_t request;
354 drm_agp_mem_t *entry;
356 if (!dev->agp || !dev->agp->acquired)
358 if (copy_from_user(&request, (drm_agp_buffer_t __user *)arg, sizeof(request)))
360 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
363 drm_unbind_agp(entry->memory);
366 entry->prev->next = entry->next;
368 dev->agp->memory = entry->next;
371 entry->next->prev = entry->prev;
373 drm_free_agp(entry->memory, entry->pages);
374 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
379 * Initialize the AGP resources.
381 * \return pointer to a drm_agp_head structure.
384 drm_agp_head_t *drm_agp_init(void)
386 drm_agp_head_t *head = NULL;
388 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
390 memset((void *)head, 0, sizeof(*head));
391 agp_copy_info(&head->agp_info);
392 if (head->agp_info.chipset == NOT_SUPPORTED) {
393 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
397 #if LINUX_VERSION_CODE <= 0x020408
398 head->cant_use_aperture = 0;
399 head->page_mask = ~(0xfff);
401 head->cant_use_aperture = head->agp_info.cant_use_aperture;
402 head->page_mask = head->agp_info.page_mask;
408 /** Calls agp_allocate_memory() */
409 DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type)
411 return agp_allocate_memory(pages, type);
414 /** Calls agp_free_memory() */
415 int drm_agp_free_memory(DRM_AGP_MEM *handle)
419 agp_free_memory(handle);
423 /** Calls agp_bind_memory() */
424 int drm_agp_bind_memory(DRM_AGP_MEM *handle, off_t start)
428 return agp_bind_memory(handle, start);
431 /** Calls agp_unbind_memory() */
432 int drm_agp_unbind_memory(DRM_AGP_MEM *handle)
436 return agp_unbind_memory(handle);
439 #endif /* __OS_HAS_AGP */