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.
34 #define __NO_VERSION__
36 #include <linux/module.h>
41 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
42 #define DRM_AGP_PUT inter_module_put("drm_agp")
45 * Pointer to the drm_agp_t structure made available by the agpgart module.
47 static const drm_agp_t *drm_agp = NULL;
50 * AGP information ioctl.
52 * \param inode device inode.
53 * \param filp file pointer.
55 * \param arg pointer to a (output) drm_agp_info structure.
56 * \return zero on success or a negative number on failure.
58 * Verifies the AGP device has been initialized and acquired and fills in the
59 * drm_agp_info structure with the information in drm_agp_head::agp_info.
61 int DRM(agp_info)(struct inode *inode, struct file *filp,
62 unsigned int cmd, unsigned long arg)
64 drm_file_t *priv = filp->private_data;
65 drm_device_t *dev = priv->dev;
69 if (!dev->agp || !dev->agp->acquired || !drm_agp->copy_info)
72 kern = &dev->agp->agp_info;
73 info.agp_version_major = kern->version.major;
74 info.agp_version_minor = kern->version.minor;
75 info.mode = kern->mode;
76 info.aperture_base = kern->aper_base;
77 info.aperture_size = kern->aper_size * 1024 * 1024;
78 info.memory_allowed = kern->max_memory << PAGE_SHIFT;
79 info.memory_used = kern->current_memory << PAGE_SHIFT;
80 info.id_vendor = kern->device->vendor;
81 info.id_device = kern->device->device;
83 if (copy_to_user((drm_agp_info_t *)arg, &info, sizeof(info)))
89 * Acquire the AGP device (ioctl).
91 * \param inode device inode.
92 * \param filp file pointer.
94 * \param arg user argument.
95 * \return zero on success or a negative number on failure.
97 * Verifies the AGP device hasn't been acquired before and calls
100 int DRM(agp_acquire)(struct inode *inode, struct file *filp,
101 unsigned int cmd, unsigned long arg)
103 drm_file_t *priv = filp->private_data;
104 drm_device_t *dev = priv->dev;
109 if (dev->agp->acquired)
111 if (!drm_agp->acquire)
114 if ( dev->agp->cant_use_aperture )
117 if ((retcode = drm_agp->acquire()))
119 dev->agp->acquired = 1;
124 * Release the AGP device (ioctl).
126 * \param inode device inode.
127 * \param filp file pointer.
128 * \param cmd command.
129 * \param arg user argument.
130 * \return zero on success or a negative number on failure.
132 * Verifies the AGP device has been acquired and calls drm_agp->release().
134 int DRM(agp_release)(struct inode *inode, struct file *filp,
135 unsigned int cmd, unsigned long arg)
137 drm_file_t *priv = filp->private_data;
138 drm_device_t *dev = priv->dev;
140 if (!dev->agp || !dev->agp->acquired || !drm_agp->release)
143 dev->agp->acquired = 0;
149 * Release the AGP device.
151 * Calls drm_agp->release().
153 void DRM(agp_do_release)(void)
155 if (drm_agp->release)
160 * Enable the AGP bus.
162 * \param inode device inode.
163 * \param filp file pointer.
164 * \param cmd command.
165 * \param arg pointer to a drm_agp_mode structure.
166 * \return zero on success or a negative number on failure.
168 * Verifies the AGP device has been acquired but not enabled, and calls
171 int DRM(agp_enable)(struct inode *inode, struct file *filp,
172 unsigned int cmd, unsigned long arg)
174 drm_file_t *priv = filp->private_data;
175 drm_device_t *dev = priv->dev;
178 if (!dev->agp || !dev->agp->acquired || !drm_agp->enable)
181 if (copy_from_user(&mode, (drm_agp_mode_t *)arg, sizeof(mode)))
184 dev->agp->mode = mode.mode;
185 drm_agp->enable(mode.mode);
186 dev->agp->base = dev->agp->agp_info.aper_base;
187 dev->agp->enabled = 1;
192 * Allocate AGP memory.
194 * \param inode device inode.
195 * \param filp file pointer.
196 * \param cmd command.
197 * \param arg pointer to a drm_agp_buffer structure.
198 * \return zero on success or a negative number on failure.
200 * Verifies the AGP device is present and has been acquired, allocates the
201 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
203 int DRM(agp_alloc)(struct inode *inode, struct file *filp,
204 unsigned int cmd, unsigned long arg)
206 drm_file_t *priv = filp->private_data;
207 drm_device_t *dev = priv->dev;
208 drm_agp_buffer_t request;
209 drm_agp_mem_t *entry;
214 if (!dev->agp || !dev->agp->acquired)
216 if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
218 if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
221 memset(entry, 0, sizeof(*entry));
223 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
224 type = (u32) request.type;
226 if (!(memory = DRM(alloc_agp)(pages, type))) {
227 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
231 entry->handle = (unsigned long)memory->key + 1;
232 entry->memory = memory;
234 entry->pages = pages;
236 entry->next = dev->agp->memory;
237 if (dev->agp->memory)
238 dev->agp->memory->prev = entry;
239 dev->agp->memory = entry;
241 request.handle = entry->handle;
242 request.physical = memory->physical;
244 if (copy_to_user((drm_agp_buffer_t *)arg, &request, sizeof(request))) {
245 dev->agp->memory = entry->next;
246 dev->agp->memory->prev = NULL;
247 DRM(free_agp)(memory, pages);
248 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
255 * Search for the AGP memory entry associated with a handle.
257 * \param dev DRM device structure.
258 * \param handle AGP memory handle.
259 * \return pointer to the drm_agp_mem structure associated with \p handle.
261 * Walks through drm_agp_head::memory until finding a matching handle.
263 static drm_agp_mem_t *DRM(agp_lookup_entry)(drm_device_t *dev,
264 unsigned long handle)
266 drm_agp_mem_t *entry;
268 for (entry = dev->agp->memory; entry; entry = entry->next) {
269 if (entry->handle == handle)
276 * Unbind AGP memory from the GATT (ioctl).
278 * \param inode device inode.
279 * \param filp file pointer.
280 * \param cmd command.
281 * \param arg pointer to a drm_agp_binding structure.
282 * \return zero on success or a negative number on failure.
284 * Verifies the AGP device is present and acquired, looks-up the AGP memory
285 * entry and passes it to the unbind_agp() function.
287 int DRM(agp_unbind)(struct inode *inode, struct file *filp,
288 unsigned int cmd, unsigned long arg)
290 drm_file_t *priv = filp->private_data;
291 drm_device_t *dev = priv->dev;
292 drm_agp_binding_t request;
293 drm_agp_mem_t *entry;
296 if (!dev->agp || !dev->agp->acquired)
298 if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
300 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
304 ret = DRM(unbind_agp)(entry->memory);
311 * Bind AGP memory into the GATT (ioctl)
313 * \param inode device inode.
314 * \param filp file pointer.
315 * \param cmd command.
316 * \param arg pointer to a drm_agp_binding structure.
317 * \return zero on success or a negative number on failure.
319 * Verifies the AGP device is present and has been acquired and that no memory
320 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
321 * it to bind_agp() function.
323 int DRM(agp_bind)(struct inode *inode, struct file *filp,
324 unsigned int cmd, unsigned long arg)
326 drm_file_t *priv = filp->private_data;
327 drm_device_t *dev = priv->dev;
328 drm_agp_binding_t request;
329 drm_agp_mem_t *entry;
333 if (!dev->agp || !dev->agp->acquired || !drm_agp->bind_memory)
335 if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
337 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
341 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
342 if ((retcode = DRM(bind_agp)(entry->memory, page)))
344 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
345 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
346 dev->agp->base, entry->bound);
351 * Free AGP memory (ioctl).
353 * \param inode device inode.
354 * \param filp file pointer.
355 * \param cmd command.
356 * \param arg pointer to a drm_agp_buffer structure.
357 * \return zero on success or a negative number on failure.
359 * Verifies the AGP device is present and has been acquired and looks up the
360 * AGP memory entry. If the memory it's currently bound, unbind it via
361 * unbind_agp(). Frees it via free_agp() as well as the entry itself
362 * and unlinks from the doubly linked list it's inserted in.
364 int DRM(agp_free)(struct inode *inode, struct file *filp,
365 unsigned int cmd, unsigned long arg)
367 drm_file_t *priv = filp->private_data;
368 drm_device_t *dev = priv->dev;
369 drm_agp_buffer_t request;
370 drm_agp_mem_t *entry;
372 if (!dev->agp || !dev->agp->acquired)
374 if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
376 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
379 DRM(unbind_agp)(entry->memory);
382 entry->prev->next = entry->next;
384 dev->agp->memory = entry->next;
387 entry->next->prev = entry->prev;
389 DRM(free_agp)(entry->memory, entry->pages);
390 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
395 * Initialize the AGP resources.
397 * \return pointer to a drm_agp_head structure.
399 * Gets the drm_agp_t structure which is made available by the agpgart module
400 * via the inter_module_* functions. Creates and initializes a drm_agp_head
403 drm_agp_head_t *DRM(agp_init)(void)
405 drm_agp_head_t *head = NULL;
407 drm_agp = DRM_AGP_GET;
409 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
411 memset((void *)head, 0, sizeof(*head));
412 drm_agp->copy_info(&head->agp_info);
413 if (head->agp_info.chipset == NOT_SUPPORTED) {
414 DRM(free)(head, sizeof(*head), DRM_MEM_AGPLISTS);
418 #if LINUX_VERSION_CODE <= 0x020408
419 head->cant_use_aperture = 0;
420 head->page_mask = ~(0xfff);
422 head->cant_use_aperture = head->agp_info.cant_use_aperture;
423 head->page_mask = head->agp_info.page_mask;
430 * Free the AGP resources.
432 * Releases the pointer in ::drm_agp.
434 void DRM(agp_uninit)(void)
440 /** Calls drm_agp->allocate_memory() */
441 DRM_AGP_MEM *DRM(agp_allocate_memory)(size_t pages, u32 type)
443 if (!drm_agp->allocate_memory)
445 return drm_agp->allocate_memory(pages, type);
448 /** Calls drm_agp->free_memory() */
449 int DRM(agp_free_memory)(DRM_AGP_MEM *handle)
451 if (!handle || !drm_agp->free_memory)
453 drm_agp->free_memory(handle);
457 /** Calls drm_agp->bind_memory() */
458 int DRM(agp_bind_memory)(DRM_AGP_MEM *handle, off_t start)
460 if (!handle || !drm_agp->bind_memory)
462 return drm_agp->bind_memory(handle, start);
465 /** Calls drm_agp->unbind_memory() */
466 int DRM(agp_unbind_memory)(DRM_AGP_MEM *handle)
468 if (!handle || !drm_agp->unbind_memory)
470 return drm_agp->unbind_memory(handle);
473 #endif /* __REALLY_HAVE_AGP */