2 * \file drm_agpsupport.c
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->head->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
88 * agp_backend_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;
99 if (dev->agp->acquired)
102 if (dev->agp->cant_use_aperture)
105 if ((retcode = agp_backend_acquire()))
107 dev->agp->acquired = 1;
112 * Release the AGP device (ioctl).
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.
120 * Verifies the AGP device has been acquired and calls agp_backend_release().
122 int drm_agp_release(struct inode *inode, struct file *filp,
123 unsigned int cmd, unsigned long arg)
125 drm_file_t *priv = filp->private_data;
126 drm_device_t *dev = priv->head->dev;
128 if (!dev->agp || !dev->agp->acquired)
130 agp_backend_release();
131 dev->agp->acquired = 0;
137 * Release the AGP device.
139 * Calls agp_backend_release().
141 void drm_agp_do_release(void)
143 agp_backend_release();
147 * Enable the AGP bus.
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.
155 * Verifies the AGP device has been acquired but not enabled, and calls
158 int drm_agp_enable(struct inode *inode, struct file *filp,
159 unsigned int cmd, unsigned long arg)
161 drm_file_t *priv = filp->private_data;
162 drm_device_t *dev = priv->head->dev;
165 if (!dev->agp || !dev->agp->acquired)
168 if (copy_from_user(&mode, (drm_agp_mode_t __user *) arg, sizeof(mode)))
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;
179 * Allocate AGP memory.
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.
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.
190 int drm_agp_alloc(struct inode *inode, struct file *filp,
191 unsigned int cmd, unsigned long arg)
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;
200 drm_agp_buffer_t __user *argp = (void __user *)arg;
202 if (!dev->agp || !dev->agp->acquired)
204 if (copy_from_user(&request, argp, sizeof(request)))
206 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
209 memset(entry, 0, sizeof(*entry));
211 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
212 type = (u32) request.type;
214 if (!(memory = drm_alloc_agp(pages, type))) {
215 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
219 entry->handle = (unsigned long)memory->key + 1;
220 entry->memory = memory;
222 entry->pages = pages;
224 entry->next = dev->agp->memory;
225 if (dev->agp->memory)
226 dev->agp->memory->prev = entry;
227 dev->agp->memory = entry;
229 request.handle = entry->handle;
230 request.physical = memory->physical;
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);
243 * Search for the AGP memory entry associated with a handle.
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.
249 * Walks through drm_agp_head::memory until finding a matching handle.
251 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t * dev,
252 unsigned long handle)
254 drm_agp_mem_t *entry;
256 for (entry = dev->agp->memory; entry; entry = entry->next) {
257 if (entry->handle == handle)
264 * Unbind AGP memory from the GATT (ioctl).
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.
272 * Verifies the AGP device is present and acquired, looks-up the AGP memory
273 * entry and passes it to the unbind_agp() function.
275 int drm_agp_unbind(struct inode *inode, struct file *filp,
276 unsigned int cmd, unsigned long arg)
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;
284 if (!dev->agp || !dev->agp->acquired)
287 (&request, (drm_agp_binding_t __user *) arg, sizeof(request)))
289 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
293 ret = drm_unbind_agp(entry->memory);
300 * Bind AGP memory into the GATT (ioctl)
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.
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.
312 int drm_agp_bind(struct inode *inode, struct file *filp,
313 unsigned int cmd, unsigned long arg)
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;
322 if (!dev->agp || !dev->agp->acquired)
325 (&request, (drm_agp_binding_t __user *) arg, sizeof(request)))
327 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
331 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
332 if ((retcode = drm_bind_agp(entry->memory, page)))
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);
341 * Free AGP memory (ioctl).
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.
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.
354 int drm_agp_free(struct inode *inode, struct file *filp,
355 unsigned int cmd, unsigned long arg)
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;
362 if (!dev->agp || !dev->agp->acquired)
365 (&request, (drm_agp_buffer_t __user *) arg, sizeof(request)))
367 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
370 drm_unbind_agp(entry->memory);
373 entry->prev->next = entry->next;
375 dev->agp->memory = entry->next;
378 entry->next->prev = entry->prev;
380 drm_free_agp(entry->memory, entry->pages);
381 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
386 * Initialize the AGP resources.
388 * \return pointer to a drm_agp_head structure.
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
394 drm_agp_head_t *drm_agp_init(void)
396 drm_agp_head_t *head = NULL;
398 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
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);
407 head->cant_use_aperture = head->agp_info.cant_use_aperture;
408 head->page_mask = head->agp_info.page_mask;
412 /** Calls agp_allocate_memory() */
413 DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type)
415 return agp_allocate_memory(pages, type);
418 /** Calls agp_free_memory() */
419 int drm_agp_free_memory(DRM_AGP_MEM * handle)
423 agp_free_memory(handle);
427 /** Calls agp_bind_memory() */
428 int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
432 return agp_bind_memory(handle, start);
435 /** Calls agp_unbind_memory() */
436 int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
440 return agp_unbind_memory(handle);
443 #endif /* __OS_HAS_AGP */