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>
36 #include "drm_legacy.h"
38 #include <dev/agp/agpreg.h>
39 #include <bus/pci/pcireg.h>
42 * Get AGP information.
44 * \param inode device inode.
45 * \param file_priv DRM file private.
47 * \param arg pointer to a (output) drm_agp_info structure.
48 * \return zero on success or a negative number on failure.
50 * Verifies the AGP device has been initialized and acquired and fills in the
51 * drm_agp_info structure with the information in drm_agp_head::agp_info.
53 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
55 struct agp_info *kern;
57 if (!dev->agp || !dev->agp->acquired)
60 kern = &dev->agp->agp_info;
61 agp_get_info(dev->agp->agpdev, kern);
62 info->agp_version_major = 1;
63 info->agp_version_minor = 0;
64 info->mode = kern->ai_mode;
65 info->aperture_base = kern->ai_aperture_base;
66 info->aperture_size = kern->ai_aperture_size;
67 info->memory_allowed = kern->ai_memory_allowed;
68 info->memory_used = kern->ai_memory_used;
69 info->id_vendor = kern->ai_devid & 0xffff;
70 info->id_device = kern->ai_devid >> 16;
75 EXPORT_SYMBOL(drm_agp_info);
77 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
78 struct drm_file *file_priv)
80 struct drm_agp_info info;
83 err = drm_agp_info(dev, &info);
87 *(struct drm_agp_info *) data = info;
92 * Acquire the AGP device.
94 * \param dev DRM device that is to acquire AGP.
95 * \return zero on success or a negative number on failure.
97 * Verifies the AGP device hasn't been acquired before and calls
98 * \c agp_backend_acquire.
100 int drm_agp_acquire(struct drm_device * dev)
104 if (!dev->agp || dev->agp->acquired)
107 retcode = -agp_acquire(dev->agp->agpdev);
111 dev->agp->acquired = 1;
115 EXPORT_SYMBOL(drm_agp_acquire);
118 * Acquire the AGP device (ioctl).
120 * \param inode device inode.
121 * \param file_priv DRM file private.
122 * \param cmd command.
123 * \param arg user argument.
124 * \return zero on success or a negative number on failure.
126 * Verifies the AGP device hasn't been acquired before and calls
127 * \c agp_backend_acquire.
129 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
130 struct drm_file *file_priv)
132 return drm_agp_acquire(dev);
136 * Release the AGP device.
138 * \param dev DRM device that is to release AGP.
139 * \return zero on success or a negative number on failure.
141 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
143 int drm_agp_release(struct drm_device * dev)
145 if (!dev->agp || !dev->agp->acquired)
147 agp_release(dev->agp->agpdev);
148 dev->agp->acquired = 0;
151 EXPORT_SYMBOL(drm_agp_release);
153 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
154 struct drm_file *file_priv)
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(struct drm_device * dev, struct drm_agp_mode mode)
171 if (!dev->agp || !dev->agp->acquired)
174 dev->agp->mode = mode.mode;
175 agp_enable(dev->agp->agpdev, mode.mode);
176 dev->agp->enabled = 1;
180 EXPORT_SYMBOL(drm_agp_enable);
182 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
183 struct drm_file *file_priv)
185 struct drm_agp_mode mode;
187 mode = *(struct drm_agp_mode *) data;
189 return drm_agp_enable(dev, mode);
192 static void *drm_agp_allocate_memory(size_t pages, u32 type)
196 agpdev = agp_find_device();
200 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
204 * Allocate AGP memory.
206 * \param inode device inode.
207 * \param file_priv file private pointer.
208 * \param cmd command.
209 * \param arg pointer to a drm_agp_buffer structure.
210 * \return zero on success or a negative number on failure.
212 * Verifies the AGP device is present and has been acquired, allocates the
213 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
215 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
217 struct drm_agp_mem *entry;
221 struct agp_memory_info info;
223 if (!dev->agp || !dev->agp->acquired)
226 entry = kmalloc(sizeof(*entry), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
230 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
231 type = (u_int32_t) request->type;
233 handle = drm_agp_allocate_memory(pages, type);
234 if (handle == NULL) {
239 entry->handle = handle;
241 entry->pages = pages;
243 entry->next = dev->agp->memory;
244 if (dev->agp->memory)
245 dev->agp->memory->prev = entry;
246 dev->agp->memory = entry;
248 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
250 request->handle = (unsigned long) entry->handle;
251 request->physical = info.ami_physical;
255 EXPORT_SYMBOL(drm_agp_alloc);
258 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
259 struct drm_file *file_priv)
261 struct drm_agp_buffer request;
264 request = *(struct drm_agp_buffer *) data;
266 retcode = drm_agp_alloc(dev, &request);
268 *(struct drm_agp_buffer *) data = request;
274 * Search for the AGP memory entry associated with a handle.
276 * \param dev DRM device structure.
277 * \param handle AGP memory handle.
278 * \return pointer to the drm_agp_mem structure associated with \p handle.
280 * Walks through drm_agp_head::memory until finding a matching handle.
282 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
285 struct drm_agp_mem *entry;
287 for (entry = dev->agp->memory; entry; entry = entry->next) {
288 if (entry->handle == handle)
294 static int drm_agp_unbind_memory(void *handle)
298 agpdev = agp_find_device();
299 if (!agpdev || !handle)
302 return -agp_unbind_memory(agpdev, handle);
306 * Unbind AGP memory from the GATT (ioctl).
308 * \param inode device inode.
309 * \param file_priv DRM file private.
310 * \param cmd command.
311 * \param arg pointer to a drm_agp_binding structure.
312 * \return zero on success or a negative number on failure.
314 * Verifies the AGP device is present and acquired, looks-up the AGP memory
315 * entry and passes it to the unbind_agp() function.
317 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
319 struct drm_agp_mem *entry;
322 if (!dev->agp || !dev->agp->acquired)
324 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
325 if (entry == NULL || !entry->bound)
327 ret = drm_agp_unbind_memory(entry->handle);
332 EXPORT_SYMBOL(drm_agp_unbind);
335 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
336 struct drm_file *file_priv)
338 struct drm_agp_binding request;
340 request = *(struct drm_agp_binding *) data;
342 return drm_agp_unbind(dev, &request);
345 static int drm_agp_bind_memory(void *handle, off_t start)
349 agpdev = agp_find_device();
350 if (!agpdev || !handle)
353 return -agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
357 * Bind AGP memory into the GATT (ioctl)
359 * \param inode device inode.
360 * \param file_priv DRM file private.
361 * \param cmd command.
362 * \param arg pointer to a drm_agp_binding structure.
363 * \return zero on success or a negative number on failure.
365 * Verifies the AGP device is present and has been acquired and that no memory
366 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
367 * it to bind_agp() function.
369 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
371 struct drm_agp_mem *entry;
375 if (!dev->agp || !dev->agp->acquired)
378 DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE);
380 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
381 if (entry == NULL || entry->bound)
384 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
386 retcode = drm_agp_bind_memory(entry->handle, page);
388 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
392 EXPORT_SYMBOL(drm_agp_bind);
395 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
396 struct drm_file *file_priv)
398 struct drm_agp_binding request;
400 request = *(struct drm_agp_binding *) data;
402 return drm_agp_bind(dev, &request);
405 static int drm_agp_free_memory(void *handle)
409 agpdev = agp_find_device();
410 if (!agpdev || !handle)
413 agp_free_memory(agpdev, handle);
418 * Free AGP memory (ioctl).
420 * \param inode device inode.
421 * \param file_priv DRM file private.
422 * \param cmd command.
423 * \param arg pointer to a drm_agp_buffer structure.
424 * \return zero on success or a negative number on failure.
426 * Verifies the AGP device is present and has been acquired and looks up the
427 * AGP memory entry. If the memory it's currently bound, unbind it via
428 * unbind_agp(). Frees it via free_agp() as well as the entry itself
429 * and unlinks from the doubly linked list it's inserted in.
431 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
433 struct drm_agp_mem *entry;
435 if (!dev->agp || !dev->agp->acquired)
437 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
442 entry->prev->next = entry->next;
444 dev->agp->memory = entry->next;
446 entry->next->prev = entry->prev;
449 drm_agp_unbind_memory(entry->handle);
451 drm_agp_free_memory(entry->handle);
455 EXPORT_SYMBOL(drm_agp_free);
459 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
460 struct drm_file *file_priv)
462 struct drm_agp_buffer request;
464 request = *(struct drm_agp_buffer *) data;
466 return drm_agp_free(dev, &request);
470 * Initialize the AGP resources.
472 * \return pointer to a drm_agp_head structure.
474 * Gets the drm_agp_t structure which is made available by the agpgart module
475 * via the inter_module_* functions. Creates and initializes a drm_agp_head
478 * Note that final cleanup of the kmalloced structure is directly done in
479 * drm_pci_agp_destroy.
481 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
484 struct drm_agp_head *head = NULL;
485 int agp_available = 1;
487 agpdev = agp_find_device();
491 DRM_DEBUG("agp_available = %d\n", agp_available);
494 head = kmalloc(sizeof(*head), M_DRM,
495 M_WAITOK | M_NULLOK | M_ZERO);
498 head->agpdev = agpdev;
499 agp_get_info(agpdev, &head->agp_info);
500 head->base = head->agp_info.ai_aperture_base;
502 DRM_INFO("AGP at 0x%08lx %dMB\n",
503 (long)head->agp_info.ai_aperture_base,
504 (int)(head->agp_info.ai_aperture_size >> 20));
510 * drm_legacy_agp_clear - Clear AGP resource list
513 * Iterate over all AGP resources and remove them. But keep the AGP head
514 * intact so it can still be used. It is safe to call this if AGP is disabled or
515 * was already removed.
517 * If DRIVER_MODESET is active, nothing is done to protect the modesetting
518 * resources from getting destroyed. Drivers are responsible of cleaning them up
519 * during device shutdown.
521 void drm_legacy_agp_clear(struct drm_device *dev)
523 struct drm_agp_mem *entry, *nexte;
527 if (drm_core_check_feature(dev, DRIVER_MODESET))
530 /* Remove AGP resources, but leave dev->agp intact until
531 * drm_unload is called.
533 for (entry = dev->agp->memory; entry; entry = nexte) {
536 drm_agp_unbind_memory(entry->handle);
537 drm_agp_free_memory(entry->handle);
540 dev->agp->memory = NULL;
542 if (dev->agp->acquired)
543 drm_agp_release(dev);
545 dev->agp->acquired = 0;
546 dev->agp->enabled = 0;