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 * Get AGP information.
42 * \param inode device inode.
43 * \param file_priv DRM file private.
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 drm_device *dev, struct drm_agp_info *info)
55 if (!dev->agp || !dev->agp->acquired)
58 kern = &dev->agp->agp_info;
59 info->agp_version_major = kern->version.major;
60 info->agp_version_minor = kern->version.minor;
61 info->mode = kern->mode;
62 info->aperture_base = kern->aper_base;
63 info->aperture_size = kern->aper_size * 1024 * 1024;
64 info->memory_allowed = kern->max_memory << PAGE_SHIFT;
65 info->memory_used = kern->current_memory << PAGE_SHIFT;
66 info->id_vendor = kern->device->vendor;
67 info->id_device = kern->device->device;
71 EXPORT_SYMBOL(drm_agp_info);
73 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
74 struct drm_file *file_priv)
76 struct drm_agp_info *info = data;
79 err = drm_agp_info(dev, info);
87 * Acquire the AGP device.
89 * \param dev DRM device that is to acquire AGP.
90 * \return zero on success or a negative number on failure.
92 * Verifies the AGP device hasn't been acquired before and calls
93 * \c agp_backend_acquire.
95 int drm_agp_acquire(struct drm_device * dev)
97 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
103 if (dev->agp->acquired)
105 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
106 if ((retcode = agp_backend_acquire()))
109 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
113 dev->agp->acquired = 1;
116 EXPORT_SYMBOL(drm_agp_acquire);
119 * Acquire the AGP device (ioctl).
121 * \param inode device inode.
122 * \param file_priv DRM file private.
123 * \param cmd command.
124 * \param arg user argument.
125 * \return zero on success or a negative number on failure.
127 * Verifies the AGP device hasn't been acquired before and calls
128 * \c agp_backend_acquire.
130 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
131 struct drm_file *file_priv)
133 return drm_agp_acquire((struct drm_device *) file_priv->head->dev);
137 * Release the AGP device.
139 * \param dev DRM device that is to release AGP.
140 * \return zero on success or a negative number on failure.
142 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
144 int drm_agp_release(struct drm_device *dev)
146 if (!dev->agp || !dev->agp->acquired)
148 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
149 agp_backend_release();
151 agp_backend_release(dev->agp->bridge);
153 dev->agp->acquired = 0;
157 EXPORT_SYMBOL(drm_agp_release);
159 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
160 struct drm_file *file_priv)
162 return drm_agp_release(dev);
166 * Enable the AGP bus.
168 * \param dev DRM device that has previously acquired AGP.
169 * \param mode Requested AGP mode.
170 * \return zero on success or a negative number on failure.
172 * Verifies the AGP device has been acquired but not enabled, and calls
175 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
177 if (!dev->agp || !dev->agp->acquired)
180 dev->agp->mode = mode.mode;
181 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
182 agp_enable(mode.mode);
184 agp_enable(dev->agp->bridge, mode.mode);
186 dev->agp->enabled = 1;
189 EXPORT_SYMBOL(drm_agp_enable);
191 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
192 struct drm_file *file_priv)
194 struct drm_agp_mode *mode = data;
196 return drm_agp_enable(dev, *mode);
200 * Allocate AGP memory.
202 * \param inode device inode.
203 * \param file_priv file private pointer.
204 * \param cmd command.
205 * \param arg pointer to a drm_agp_buffer structure.
206 * \return zero on success or a negative number on failure.
208 * Verifies the AGP device is present and has been acquired, allocates the
209 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
211 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
213 struct drm_agp_mem *entry;
218 if (!dev->agp || !dev->agp->acquired)
220 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
223 memset(entry, 0, sizeof(*entry));
225 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
226 type = (u32) request->type;
227 if (!(memory = drm_alloc_agp(dev, pages, type))) {
228 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
232 entry->handle = (unsigned long)memory->key + 1;
233 entry->memory = memory;
235 entry->pages = pages;
236 list_add(&entry->head, &dev->agp->memory);
238 request->handle = entry->handle;
239 request->physical = memory->physical;
243 EXPORT_SYMBOL(drm_agp_alloc);
246 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
247 struct drm_file *file_priv)
249 struct drm_agp_buffer *request = data;
251 return drm_agp_alloc(dev, request);
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 struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
264 unsigned long handle)
266 struct drm_agp_mem *entry;
268 list_for_each_entry(entry, &dev->agp->memory, head) {
269 if (entry->handle == handle)
276 * Unbind AGP memory from the GATT (ioctl).
278 * \param inode device inode.
279 * \param file_priv DRM file private.
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 drm_device *dev, struct drm_agp_binding *request)
289 struct drm_agp_mem *entry;
292 if (!dev->agp || !dev->agp->acquired)
294 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
298 ret = drm_unbind_agp(entry->memory);
303 EXPORT_SYMBOL(drm_agp_unbind);
306 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
307 struct drm_file *file_priv)
309 struct drm_agp_binding *request = data;
311 return drm_agp_unbind(dev, request);
316 * Bind AGP memory into the GATT (ioctl)
318 * \param inode device inode.
319 * \param file_priv DRM file private.
320 * \param cmd command.
321 * \param arg pointer to a drm_agp_binding structure.
322 * \return zero on success or a negative number on failure.
324 * Verifies the AGP device is present and has been acquired and that no memory
325 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
326 * it to bind_agp() function.
328 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
330 struct drm_agp_mem *entry;
334 if (!dev->agp || !dev->agp->acquired)
336 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
340 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
341 if ((retcode = drm_bind_agp(entry->memory, page)))
343 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
344 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
345 dev->agp->base, entry->bound);
348 EXPORT_SYMBOL(drm_agp_bind);
351 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
352 struct drm_file *file_priv)
354 struct drm_agp_binding *request = data;
356 return drm_agp_bind(dev, request);
361 * Free AGP memory (ioctl).
363 * \param inode device inode.
364 * \param file_priv DRM file private.
365 * \param cmd command.
366 * \param arg pointer to a drm_agp_buffer structure.
367 * \return zero on success or a negative number on failure.
369 * Verifies the AGP device is present and has been acquired and looks up the
370 * AGP memory entry. If the memory it's currently bound, unbind it via
371 * unbind_agp(). Frees it via free_agp() as well as the entry itself
372 * and unlinks from the doubly linked list it's inserted in.
374 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
376 struct drm_agp_mem *entry;
378 if (!dev->agp || !dev->agp->acquired)
380 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
383 drm_unbind_agp(entry->memory);
385 list_del(&entry->head);
387 drm_free_agp(entry->memory, entry->pages);
388 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
391 EXPORT_SYMBOL(drm_agp_free);
395 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
396 struct drm_file *file_priv)
398 struct drm_agp_buffer *request = data;
400 return drm_agp_free(dev, request);
405 * Initialize the AGP resources.
407 * \return pointer to a drm_agp_head structure.
409 * Gets the drm_agp_t structure which is made available by the agpgart module
410 * via the inter_module_* functions. Creates and initializes a drm_agp_head
413 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
415 struct drm_agp_head *head = NULL;
417 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
419 memset((void *)head, 0, sizeof(*head));
421 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
422 agp_copy_info(&head->agp_info);
424 head->bridge = agp_find_bridge(dev->pdev);
426 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
427 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
430 agp_copy_info(head->bridge, &head->agp_info);
431 agp_backend_release(head->bridge);
433 agp_copy_info(head->bridge, &head->agp_info);
436 if (head->agp_info.chipset == NOT_SUPPORTED) {
437 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
440 INIT_LIST_HEAD(&head->memory);
441 head->cant_use_aperture = head->agp_info.cant_use_aperture;
442 head->page_mask = head->agp_info.page_mask;
443 head->base = head->agp_info.aper_base;
447 /** Calls agp_allocate_memory() */
448 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
449 DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type)
451 return agp_allocate_memory(pages, type);
454 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge,
455 size_t pages, u32 type)
457 return agp_allocate_memory(bridge, pages, type);
461 /** Calls agp_free_memory() */
462 int drm_agp_free_memory(DRM_AGP_MEM * handle)
466 agp_free_memory(handle);
470 /** Calls agp_bind_memory() */
471 int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
475 return agp_bind_memory(handle, start);
477 EXPORT_SYMBOL(drm_agp_bind_memory);
479 /** Calls agp_unbind_memory() */
480 int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
484 return agp_unbind_memory(handle);
490 * AGP ttm backend interface.
493 #ifndef AGP_USER_TYPES
494 #define AGP_USER_TYPES (1 << 16)
495 #define AGP_USER_MEMORY (AGP_USER_TYPES)
496 #define AGP_USER_CACHED_MEMORY (AGP_USER_TYPES + 1)
498 #define AGP_REQUIRED_MAJOR 0
499 #define AGP_REQUIRED_MINOR 102
501 static int drm_agp_needs_unbind_cache_adjust(struct drm_ttm_backend *backend)
503 return ((backend->flags & DRM_BE_FLAG_BOUND_CACHED) ? 0 : 1);
507 static int drm_agp_populate(struct drm_ttm_backend *backend,
508 unsigned long num_pages, struct page **pages,
509 struct page *dummy_read_page)
511 struct drm_agp_ttm_backend *agp_be =
512 container_of(backend, struct drm_agp_ttm_backend, backend);
513 struct page **cur_page, **last_page = pages + num_pages;
515 int dummy_page_count = 0;
517 if (drm_alloc_memctl(num_pages * sizeof(void *)))
520 DRM_DEBUG("drm_agp_populate_ttm\n");
521 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
522 mem = drm_agp_allocate_memory(num_pages, AGP_USER_MEMORY);
524 mem = drm_agp_allocate_memory(agp_be->bridge, num_pages, AGP_USER_MEMORY);
527 drm_free_memctl(num_pages * sizeof(void *));
531 DRM_DEBUG("Current page count is %ld\n", (long) mem->page_count);
533 for (cur_page = pages; cur_page < last_page; ++cur_page) {
534 struct page *page = *cur_page;
536 page = dummy_read_page;
539 mem->memory[mem->page_count++] = phys_to_gart(page_to_phys(page));
541 if (dummy_page_count)
542 DRM_DEBUG("Mapped %d dummy pages\n", dummy_page_count);
547 static int drm_agp_bind_ttm(struct drm_ttm_backend *backend,
548 struct drm_bo_mem_reg *bo_mem)
550 struct drm_agp_ttm_backend *agp_be =
551 container_of(backend, struct drm_agp_ttm_backend, backend);
552 DRM_AGP_MEM *mem = agp_be->mem;
554 int snooped = (bo_mem->flags & DRM_BO_FLAG_CACHED) && !(bo_mem->flags & DRM_BO_FLAG_CACHED_MAPPED);
556 DRM_DEBUG("drm_agp_bind_ttm\n");
557 mem->is_flushed = TRUE;
558 mem->type = AGP_USER_MEMORY;
559 /* CACHED MAPPED implies not snooped memory */
561 mem->type = AGP_USER_CACHED_MEMORY;
563 ret = drm_agp_bind_memory(mem, bo_mem->mm_node->start);
565 DRM_ERROR("AGP Bind memory failed\n");
567 DRM_FLAG_MASKED(backend->flags, (bo_mem->flags & DRM_BO_FLAG_CACHED) ?
568 DRM_BE_FLAG_BOUND_CACHED : 0,
569 DRM_BE_FLAG_BOUND_CACHED);
573 static int drm_agp_unbind_ttm(struct drm_ttm_backend *backend)
575 struct drm_agp_ttm_backend *agp_be =
576 container_of(backend, struct drm_agp_ttm_backend, backend);
578 DRM_DEBUG("drm_agp_unbind_ttm\n");
579 if (agp_be->mem->is_bound)
580 return drm_agp_unbind_memory(agp_be->mem);
585 static void drm_agp_clear_ttm(struct drm_ttm_backend *backend)
587 struct drm_agp_ttm_backend *agp_be =
588 container_of(backend, struct drm_agp_ttm_backend, backend);
589 DRM_AGP_MEM *mem = agp_be->mem;
591 DRM_DEBUG("drm_agp_clear_ttm\n");
593 unsigned long num_pages = mem->page_count;
594 backend->func->unbind(backend);
595 agp_free_memory(mem);
596 drm_free_memctl(num_pages * sizeof(void *));
601 static void drm_agp_destroy_ttm(struct drm_ttm_backend *backend)
603 struct drm_agp_ttm_backend *agp_be;
606 DRM_DEBUG("drm_agp_destroy_ttm\n");
607 agp_be = container_of(backend, struct drm_agp_ttm_backend, backend);
610 backend->func->clear(backend);
611 drm_ctl_free(agp_be, sizeof(*agp_be), DRM_MEM_TTM);
616 static struct drm_ttm_backend_func agp_ttm_backend = {
617 .needs_ub_cache_adjust = drm_agp_needs_unbind_cache_adjust,
618 .populate = drm_agp_populate,
619 .clear = drm_agp_clear_ttm,
620 .bind = drm_agp_bind_ttm,
621 .unbind = drm_agp_unbind_ttm,
622 .destroy = drm_agp_destroy_ttm,
625 struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev)
628 struct drm_agp_ttm_backend *agp_be;
629 struct agp_kern_info *info;
632 DRM_ERROR("AGP is not initialized.\n");
635 info = &dev->agp->agp_info;
637 if (info->version.major != AGP_REQUIRED_MAJOR ||
638 info->version.minor < AGP_REQUIRED_MINOR) {
639 DRM_ERROR("Wrong agpgart version %d.%d\n"
640 "\tYou need at least version %d.%d.\n",
649 agp_be = drm_ctl_calloc(1, sizeof(*agp_be), DRM_MEM_TTM);
655 agp_be->bridge = dev->agp->bridge;
656 agp_be->populated = FALSE;
657 agp_be->backend.func = &agp_ttm_backend;
658 agp_be->backend.dev = dev;
660 return &agp_be->backend;
662 EXPORT_SYMBOL(drm_agp_init_ttm);
664 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
665 void drm_agp_chipset_flush(struct drm_device *dev)
667 agp_flush_chipset(dev->agp->bridge);
669 EXPORT_SYMBOL(drm_agp_flush_chipset);
672 #endif /* __OS_HAS_AGP */