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) {
502 return ((backend->flags & DRM_BE_FLAG_BOUND_CACHED) ? 0 : 1);
506 static int drm_agp_populate(struct drm_ttm_backend *backend, unsigned long num_pages,
507 struct page **pages) {
509 struct drm_agp_ttm_backend *agp_be =
510 container_of(backend, struct drm_agp_ttm_backend, backend);
511 struct page **cur_page, **last_page = pages + num_pages;
514 if (drm_alloc_memctl(num_pages * sizeof(void *)))
517 DRM_DEBUG("drm_agp_populate_ttm\n");
518 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
519 mem = drm_agp_allocate_memory(num_pages, AGP_USER_MEMORY);
521 mem = drm_agp_allocate_memory(agp_be->bridge, num_pages, AGP_USER_MEMORY);
524 drm_free_memctl(num_pages *sizeof(void *));
528 DRM_DEBUG("Current page count is %ld\n", (long) mem->page_count);
530 for (cur_page = pages; cur_page < last_page; ++cur_page) {
531 mem->memory[mem->page_count++] = phys_to_gart(page_to_phys(*cur_page));
537 static int drm_agp_bind_ttm(struct drm_ttm_backend *backend,
538 struct drm_bo_mem_reg *bo_mem)
540 struct drm_agp_ttm_backend *agp_be =
541 container_of(backend, struct drm_agp_ttm_backend, backend);
542 DRM_AGP_MEM *mem = agp_be->mem;
545 DRM_DEBUG("drm_agp_bind_ttm\n");
546 mem->is_flushed = TRUE;
547 mem->type = (bo_mem->flags & DRM_BO_FLAG_CACHED) ? AGP_USER_CACHED_MEMORY :
549 ret = drm_agp_bind_memory(mem, bo_mem->mm_node->start);
551 DRM_ERROR("AGP Bind memory failed\n");
553 DRM_FLAG_MASKED(backend->flags, (bo_mem->flags & DRM_BO_FLAG_CACHED) ?
554 DRM_BE_FLAG_BOUND_CACHED : 0,
555 DRM_BE_FLAG_BOUND_CACHED);
559 static int drm_agp_unbind_ttm(struct drm_ttm_backend *backend) {
561 struct drm_agp_ttm_backend *agp_be =
562 container_of(backend, struct drm_agp_ttm_backend, backend);
564 DRM_DEBUG("drm_agp_unbind_ttm\n");
565 if (agp_be->mem->is_bound)
566 return drm_agp_unbind_memory(agp_be->mem);
571 static void drm_agp_clear_ttm(struct drm_ttm_backend *backend) {
573 struct drm_agp_ttm_backend *agp_be =
574 container_of(backend, struct drm_agp_ttm_backend, backend);
575 DRM_AGP_MEM *mem = agp_be->mem;
577 DRM_DEBUG("drm_agp_clear_ttm\n");
579 unsigned long num_pages = mem->page_count;
580 backend->func->unbind(backend);
581 agp_free_memory(mem);
582 drm_free_memctl(num_pages *sizeof(void *));
587 static void drm_agp_destroy_ttm(struct drm_ttm_backend *backend) {
589 struct drm_agp_ttm_backend *agp_be;
592 DRM_DEBUG("drm_agp_destroy_ttm\n");
593 agp_be = container_of(backend, struct drm_agp_ttm_backend, backend);
596 backend->func->clear(backend);
598 drm_ctl_free(agp_be, sizeof(*agp_be), DRM_MEM_TTM);
603 static struct drm_ttm_backend_func agp_ttm_backend =
605 .needs_ub_cache_adjust = drm_agp_needs_unbind_cache_adjust,
606 .populate = drm_agp_populate,
607 .clear = drm_agp_clear_ttm,
608 .bind = drm_agp_bind_ttm,
609 .unbind = drm_agp_unbind_ttm,
610 .destroy = drm_agp_destroy_ttm,
613 struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev)
616 struct drm_agp_ttm_backend *agp_be;
617 struct agp_kern_info *info;
620 DRM_ERROR("AGP is not initialized.\n");
623 info = &dev->agp->agp_info;
625 if (info->version.major != AGP_REQUIRED_MAJOR ||
626 info->version.minor < AGP_REQUIRED_MINOR) {
627 DRM_ERROR("Wrong agpgart version %d.%d\n"
628 "\tYou need at least version %d.%d.\n",
637 agp_be = drm_ctl_calloc(1, sizeof(*agp_be), DRM_MEM_TTM);
643 agp_be->bridge = dev->agp->bridge;
644 agp_be->populated = FALSE;
645 agp_be->backend.func = &agp_ttm_backend;
646 // agp_be->backend.mem_type = DRM_BO_MEM_TT;
647 agp_be->backend.dev = dev;
649 return &agp_be->backend;
651 EXPORT_SYMBOL(drm_agp_init_ttm);
653 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
654 void drm_agp_flush_chipset(struct drm_device *dev)
656 agp_flush_chipset(dev->agp->bridge);
658 EXPORT_SYMBOL(drm_agp_flush_chipset);
661 #endif /* __OS_HAS_AGP */