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 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
41 #define DRM_AGP_PUT inter_module_put("drm_agp")
44 * Pointer to the drm_agp_t structure made available by the agpgart module.
46 static const drm_agp_t *drm_agp = NULL;
49 * AGP information ioctl.
51 * \param inode device inode.
52 * \param filp file pointer.
54 * \param arg pointer to a (output) drm_agp_info structure.
55 * \return zero on success or a negative number on failure.
57 * Verifies the AGP device has been initialized and acquired and fills in the
58 * drm_agp_info structure with the information in drm_agp_head::agp_info.
60 int DRM(agp_info)(struct inode *inode, struct file *filp,
61 unsigned int cmd, unsigned long arg)
63 drm_file_t *priv = filp->private_data;
64 drm_device_t *dev = priv->dev;
68 if (!dev->agp || !dev->agp->acquired || !drm_agp->copy_info)
71 kern = &dev->agp->agp_info;
72 info.agp_version_major = kern->version.major;
73 info.agp_version_minor = kern->version.minor;
74 info.mode = kern->mode;
75 info.aperture_base = kern->aper_base;
76 info.aperture_size = kern->aper_size * 1024 * 1024;
77 info.memory_allowed = kern->max_memory << PAGE_SHIFT;
78 info.memory_used = kern->current_memory << PAGE_SHIFT;
79 info.id_vendor = kern->device->vendor;
80 info.id_device = kern->device->device;
82 if (copy_to_user((drm_agp_info_t *)arg, &info, sizeof(info)))
88 * Acquire the AGP device (ioctl).
90 * \param inode device inode.
91 * \param filp file pointer.
93 * \param arg user argument.
94 * \return zero on success or a negative number on failure.
96 * Verifies the AGP device hasn't been acquired before and calls
99 int DRM(agp_acquire)(struct inode *inode, struct file *filp,
100 unsigned int cmd, unsigned long arg)
102 drm_file_t *priv = filp->private_data;
103 drm_device_t *dev = priv->dev;
106 if (!dev->agp || dev->agp->acquired || !drm_agp->acquire)
108 if ((retcode = drm_agp->acquire()))
110 dev->agp->acquired = 1;
115 * Release the AGP device (ioctl).
117 * \param inode device inode.
118 * \param filp file pointer.
119 * \param cmd command.
120 * \param arg user argument.
121 * \return zero on success or a negative number on failure.
123 * Verifies the AGP device has been acquired and calls drm_agp->release().
125 int DRM(agp_release)(struct inode *inode, struct file *filp,
126 unsigned int cmd, unsigned long arg)
128 drm_file_t *priv = filp->private_data;
129 drm_device_t *dev = priv->dev;
131 if (!dev->agp || !dev->agp->acquired || !drm_agp->release)
134 dev->agp->acquired = 0;
140 * Release the AGP device.
142 * Calls drm_agp->release().
144 void DRM(agp_do_release)(void)
146 if (drm_agp->release)
151 * Enable the AGP bus.
153 * \param inode device inode.
154 * \param filp file pointer.
155 * \param cmd command.
156 * \param arg pointer to a drm_agp_mode structure.
157 * \return zero on success or a negative number on failure.
159 * Verifies the AGP device has been acquired but not enabled, and calls
162 int DRM(agp_enable)(struct inode *inode, struct file *filp,
163 unsigned int cmd, unsigned long arg)
165 drm_file_t *priv = filp->private_data;
166 drm_device_t *dev = priv->dev;
169 if (!dev->agp || !dev->agp->acquired || !drm_agp->enable)
172 if (copy_from_user(&mode, (drm_agp_mode_t *)arg, sizeof(mode)))
175 dev->agp->mode = mode.mode;
176 drm_agp->enable(mode.mode);
177 dev->agp->base = dev->agp->agp_info.aper_base;
178 dev->agp->enabled = 1;
183 * Allocate AGP memory.
185 * \param inode device inode.
186 * \param filp file pointer.
187 * \param cmd command.
188 * \param arg pointer to a drm_agp_buffer structure.
189 * \return zero on success or a negative number on failure.
191 * Verifies the AGP device is present and has been acquired, allocates the
192 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
194 int DRM(agp_alloc)(struct inode *inode, struct file *filp,
195 unsigned int cmd, unsigned long arg)
197 drm_file_t *priv = filp->private_data;
198 drm_device_t *dev = priv->dev;
199 drm_agp_buffer_t request;
200 drm_agp_mem_t *entry;
205 if (!dev->agp || !dev->agp->acquired)
207 if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
209 if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
212 memset(entry, 0, sizeof(*entry));
214 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
215 type = (u32) request.type;
217 if (!(memory = DRM(alloc_agp)(pages, type))) {
218 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
222 entry->handle = (unsigned long)memory->key + 1;
223 entry->memory = memory;
225 entry->pages = pages;
227 entry->next = dev->agp->memory;
228 if (dev->agp->memory)
229 dev->agp->memory->prev = entry;
230 dev->agp->memory = entry;
232 request.handle = entry->handle;
233 request.physical = memory->physical;
235 if (copy_to_user((drm_agp_buffer_t *)arg, &request, sizeof(request))) {
236 dev->agp->memory = entry->next;
237 dev->agp->memory->prev = NULL;
238 DRM(free_agp)(memory, pages);
239 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
246 * Search for the AGP memory entry associated with a handle.
248 * \param dev DRM device structure.
249 * \param handle AGP memory handle.
250 * \return pointer to the drm_agp_mem structure associated with \p handle.
252 * Walks through drm_agp_head::memory until finding a matching handle.
254 static drm_agp_mem_t *DRM(agp_lookup_entry)(drm_device_t *dev,
255 unsigned long handle)
257 drm_agp_mem_t *entry;
259 for (entry = dev->agp->memory; entry; entry = entry->next) {
260 if (entry->handle == handle)
267 * Unbind AGP memory from the GATT (ioctl).
269 * \param inode device inode.
270 * \param filp file pointer.
271 * \param cmd command.
272 * \param arg pointer to a drm_agp_binding structure.
273 * \return zero on success or a negative number on failure.
275 * Verifies the AGP device is present and acquired, looks-up the AGP memory
276 * entry and passes it to the unbind_agp() function.
278 int DRM(agp_unbind)(struct inode *inode, struct file *filp,
279 unsigned int cmd, unsigned long arg)
281 drm_file_t *priv = filp->private_data;
282 drm_device_t *dev = priv->dev;
283 drm_agp_binding_t request;
284 drm_agp_mem_t *entry;
287 if (!dev->agp || !dev->agp->acquired)
289 if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
291 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
295 ret = DRM(unbind_agp)(entry->memory);
302 * Bind AGP memory into the GATT (ioctl)
304 * \param inode device inode.
305 * \param filp file pointer.
306 * \param cmd command.
307 * \param arg pointer to a drm_agp_binding structure.
308 * \return zero on success or a negative number on failure.
310 * Verifies the AGP device is present and has been acquired and that no memory
311 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
312 * it to bind_agp() function.
314 int DRM(agp_bind)(struct inode *inode, struct file *filp,
315 unsigned int cmd, unsigned long arg)
317 drm_file_t *priv = filp->private_data;
318 drm_device_t *dev = priv->dev;
319 drm_agp_binding_t request;
320 drm_agp_mem_t *entry;
324 if (!dev->agp || !dev->agp->acquired || !drm_agp->bind_memory)
326 if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
328 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
332 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
333 if ((retcode = DRM(bind_agp)(entry->memory, page)))
335 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
336 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
337 dev->agp->base, entry->bound);
342 * Free AGP memory (ioctl).
344 * \param inode device inode.
345 * \param filp file pointer.
346 * \param cmd command.
347 * \param arg pointer to a drm_agp_buffer structure.
348 * \return zero on success or a negative number on failure.
350 * Verifies the AGP device is present and has been acquired and looks up the
351 * AGP memory entry. If the memory it's currently bound, unbind it via
352 * unbind_agp(). Frees it via free_agp() as well as the entry itself
353 * and unlinks from the doubly linked list it's inserted in.
355 int DRM(agp_free)(struct inode *inode, struct file *filp,
356 unsigned int cmd, unsigned long arg)
358 drm_file_t *priv = filp->private_data;
359 drm_device_t *dev = priv->dev;
360 drm_agp_buffer_t request;
361 drm_agp_mem_t *entry;
363 if (!dev->agp || !dev->agp->acquired)
365 if (copy_from_user(&request, (drm_agp_buffer_t *)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 drm_agp = DRM_AGP_GET;
400 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
402 memset((void *)head, 0, sizeof(*head));
403 drm_agp->copy_info(&head->agp_info);
404 if (head->agp_info.chipset == NOT_SUPPORTED) {
405 DRM(free)(head, sizeof(*head), DRM_MEM_AGPLISTS);
409 #if LINUX_VERSION_CODE <= 0x020408
410 head->cant_use_aperture = 0;
411 head->page_mask = ~(0xfff);
413 head->cant_use_aperture = head->agp_info.cant_use_aperture;
414 head->page_mask = head->agp_info.page_mask;
421 * Free the AGP resources.
423 * Releases the pointer in ::drm_agp.
425 void DRM(agp_uninit)(void)
431 /** Calls drm_agp->allocate_memory() */
432 DRM_AGP_MEM *DRM(agp_allocate_memory)(size_t pages, u32 type)
434 if (!drm_agp->allocate_memory)
436 return drm_agp->allocate_memory(pages, type);
439 /** Calls drm_agp->free_memory() */
440 int DRM(agp_free_memory)(DRM_AGP_MEM *handle)
442 if (!handle || !drm_agp->free_memory)
444 drm_agp->free_memory(handle);
448 /** Calls drm_agp->bind_memory() */
449 int DRM(agp_bind_memory)(DRM_AGP_MEM *handle, off_t start)
451 if (!handle || !drm_agp->bind_memory)
453 return drm_agp->bind_memory(handle, start);
456 /** Calls drm_agp->unbind_memory() */
457 int DRM(agp_unbind_memory)(DRM_AGP_MEM *handle)
459 if (!handle || !drm_agp->unbind_memory)
461 return drm_agp->unbind_memory(handle);
464 #endif /* __REALLY_HAVE_AGP */