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 filp file pointer.
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(drm_device_t * dev, drm_agp_info_t *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 inode *inode, struct file *filp,
74 unsigned int cmd, unsigned long arg)
76 drm_file_t *priv = filp->private_data;
77 drm_device_t *dev = priv->head->dev;
81 err = drm_agp_info(dev, &info);
85 if (copy_to_user((drm_agp_info_t __user *) arg, &info, sizeof(info)))
91 * Acquire the AGP device.
93 * \param dev DRM device that is to acquire AGP.
94 * \return zero on success or a negative number on failure.
96 * Verifies the AGP device hasn't been acquired before and calls
97 * \c agp_backend_acquire.
99 int drm_agp_acquire(drm_device_t * dev)
101 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
107 if (dev->agp->acquired)
110 if (dev->agp->cant_use_aperture)
113 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
114 if ((retcode = agp_backend_acquire()))
117 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
121 dev->agp->acquired = 1;
124 EXPORT_SYMBOL(drm_agp_acquire);
127 * Acquire the AGP device (ioctl).
129 * \param inode device inode.
130 * \param filp file pointer.
131 * \param cmd command.
132 * \param arg user argument.
133 * \return zero on success or a negative number on failure.
135 * Verifies the AGP device hasn't been acquired before and calls
136 * \c agp_backend_acquire.
138 int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp,
139 unsigned int cmd, unsigned long arg)
141 drm_file_t *priv = filp->private_data;
143 return drm_agp_acquire( (drm_device_t *) priv->head->dev );
147 * Release the AGP device.
149 * \param dev DRM device that is to release AGP.
150 * \return zero on success or a negative number on failure.
152 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
154 int drm_agp_release(drm_device_t *dev)
156 if (!dev->agp || !dev->agp->acquired)
158 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
159 agp_backend_release();
161 agp_backend_release(dev->agp->bridge);
163 dev->agp->acquired = 0;
167 EXPORT_SYMBOL(drm_agp_release);
169 int drm_agp_release_ioctl(struct inode *inode, struct file *filp,
170 unsigned int cmd, unsigned long arg)
172 drm_file_t *priv = filp->private_data;
173 drm_device_t *dev = priv->head->dev;
175 return drm_agp_release(dev);
179 * Enable the AGP bus.
181 * \param dev DRM device that has previously acquired AGP.
182 * \param mode Requested AGP mode.
183 * \return zero on success or a negative number on failure.
185 * Verifies the AGP device has been acquired but not enabled, and calls
188 int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode)
190 if (!dev->agp || !dev->agp->acquired)
193 dev->agp->mode = mode.mode;
194 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
195 agp_enable(mode.mode);
197 agp_enable(dev->agp->bridge, mode.mode);
199 dev->agp->base = dev->agp->agp_info.aper_base;
200 dev->agp->enabled = 1;
203 EXPORT_SYMBOL(drm_agp_enable);
205 int drm_agp_enable_ioctl(struct inode *inode, struct file *filp,
206 unsigned int cmd, unsigned long arg)
208 drm_file_t *priv = filp->private_data;
209 drm_device_t *dev = priv->head->dev;
213 if (copy_from_user(&mode, (drm_agp_mode_t __user *) arg, sizeof(mode)))
216 return drm_agp_enable(dev, mode);
220 * Allocate AGP memory.
222 * \param inode device inode.
223 * \param filp file pointer.
224 * \param cmd command.
225 * \param arg pointer to a drm_agp_buffer structure.
226 * \return zero on success or a negative number on failure.
228 * Verifies the AGP device is present and has been acquired, allocates the
229 * memory via alloc_agp() and creates a drm_agp_mem entry for it.
231 int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request)
233 drm_agp_mem_t *entry;
238 if (!dev->agp || !dev->agp->acquired)
240 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
243 memset(entry, 0, sizeof(*entry));
245 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
246 type = (u32) request->type;
247 if (!(memory = drm_alloc_agp(dev, pages, type))) {
248 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
252 entry->handle = (unsigned long)memory->key + 1;
253 entry->memory = memory;
255 entry->pages = pages;
257 entry->next = dev->agp->memory;
258 if (dev->agp->memory)
259 dev->agp->memory->prev = entry;
260 dev->agp->memory = entry;
262 request->handle = entry->handle;
263 request->physical = memory->physical;
267 EXPORT_SYMBOL(drm_agp_alloc);
270 int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp,
271 unsigned int cmd, unsigned long arg)
273 drm_file_t *priv = filp->private_data;
274 drm_device_t *dev = priv->head->dev;
275 drm_agp_buffer_t request;
276 drm_agp_buffer_t __user *argp = (void __user *)arg;
279 if (copy_from_user(&request, argp, sizeof(request)))
282 err = drm_agp_alloc(dev, &request);
286 if (copy_to_user(argp, &request, sizeof(request))) {
287 drm_agp_mem_t *entry = dev->agp->memory;
289 dev->agp->memory = entry->next;
290 dev->agp->memory->prev = NULL;
291 drm_free_agp(entry->memory, entry->pages);
292 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
300 * Search for the AGP memory entry associated with a handle.
302 * \param dev DRM device structure.
303 * \param handle AGP memory handle.
304 * \return pointer to the drm_agp_mem structure associated with \p handle.
306 * Walks through drm_agp_head::memory until finding a matching handle.
308 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t * dev,
309 unsigned long handle)
311 drm_agp_mem_t *entry;
313 for (entry = dev->agp->memory; entry; entry = entry->next) {
314 if (entry->handle == handle)
321 * Unbind AGP memory from the GATT (ioctl).
323 * \param inode device inode.
324 * \param filp file pointer.
325 * \param cmd command.
326 * \param arg pointer to a drm_agp_binding structure.
327 * \return zero on success or a negative number on failure.
329 * Verifies the AGP device is present and acquired, looks-up the AGP memory
330 * entry and passes it to the unbind_agp() function.
332 int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request)
334 drm_agp_mem_t *entry;
337 if (!dev->agp || !dev->agp->acquired)
339 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
343 ret = drm_unbind_agp(entry->memory);
348 EXPORT_SYMBOL(drm_agp_unbind);
351 int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp,
352 unsigned int cmd, unsigned long arg)
354 drm_file_t *priv = filp->private_data;
355 drm_device_t *dev = priv->head->dev;
356 drm_agp_binding_t request;
359 (&request, (drm_agp_binding_t __user *) arg, sizeof(request)))
362 return drm_agp_unbind(dev, &request);
367 * Bind AGP memory into the GATT (ioctl)
369 * \param inode device inode.
370 * \param filp file pointer.
371 * \param cmd command.
372 * \param arg pointer to a drm_agp_binding structure.
373 * \return zero on success or a negative number on failure.
375 * Verifies the AGP device is present and has been acquired and that no memory
376 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
377 * it to bind_agp() function.
379 int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request)
381 drm_agp_mem_t *entry;
385 if (!dev->agp || !dev->agp->acquired)
387 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
391 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
392 if ((retcode = drm_bind_agp(entry->memory, page)))
394 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
395 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
396 dev->agp->base, entry->bound);
399 EXPORT_SYMBOL(drm_agp_bind);
402 int drm_agp_bind_ioctl(struct inode *inode, struct file *filp,
403 unsigned int cmd, unsigned long arg)
405 drm_file_t *priv = filp->private_data;
406 drm_device_t *dev = priv->head->dev;
407 drm_agp_binding_t request;
410 (&request, (drm_agp_binding_t __user *) arg, sizeof(request)))
413 return drm_agp_bind(dev, &request);
418 * Free AGP memory (ioctl).
420 * \param inode device inode.
421 * \param filp file pointer.
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(drm_device_t *dev, drm_agp_buffer_t *request)
433 drm_agp_mem_t *entry;
435 if (!dev->agp || !dev->agp->acquired)
437 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
440 drm_unbind_agp(entry->memory);
443 entry->prev->next = entry->next;
445 dev->agp->memory = entry->next;
448 entry->next->prev = entry->prev;
450 drm_free_agp(entry->memory, entry->pages);
451 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
454 EXPORT_SYMBOL(drm_agp_free);
458 int drm_agp_free_ioctl(struct inode *inode, struct file *filp,
459 unsigned int cmd, unsigned long arg)
461 drm_file_t *priv = filp->private_data;
462 drm_device_t *dev = priv->head->dev;
463 drm_agp_buffer_t request;
466 (&request, (drm_agp_buffer_t __user *) arg, sizeof(request)))
469 return drm_agp_free(dev, &request);
474 * Initialize the AGP resources.
476 * \return pointer to a drm_agp_head structure.
478 * Gets the drm_agp_t structure which is made available by the agpgart module
479 * via the inter_module_* functions. Creates and initializes a drm_agp_head
482 drm_agp_head_t *drm_agp_init(drm_device_t *dev)
484 drm_agp_head_t *head = NULL;
486 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
488 memset((void *)head, 0, sizeof(*head));
490 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
491 agp_copy_info(&head->agp_info);
493 head->bridge = agp_find_bridge(dev->pdev);
495 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
496 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
499 agp_copy_info(head->bridge, &head->agp_info);
500 agp_backend_release(head->bridge);
502 agp_copy_info(head->bridge, &head->agp_info);
505 if (head->agp_info.chipset == NOT_SUPPORTED) {
506 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
510 head->cant_use_aperture = head->agp_info.cant_use_aperture;
511 head->page_mask = head->agp_info.page_mask;
515 /** Calls agp_allocate_memory() */
516 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
517 DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type)
519 return agp_allocate_memory(pages, type);
522 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge,
523 size_t pages, u32 type)
525 return agp_allocate_memory(bridge, pages, type);
529 /** Calls agp_free_memory() */
530 int drm_agp_free_memory(DRM_AGP_MEM * handle)
534 agp_free_memory(handle);
538 /** Calls agp_bind_memory() */
539 int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
543 return agp_bind_memory(handle, start);
545 EXPORT_SYMBOL(drm_agp_bind_memory);
547 /** Calls agp_unbind_memory() */
548 int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
552 return agp_unbind_memory(handle);
558 * AGP ttm backend interface.
561 static int drm_agp_needs_unbind_cache_adjust(drm_ttm_backend_t *backend) {
562 return ((backend->flags & DRM_BE_FLAG_BOUND_CACHED) ? 1 : 0);
566 static int drm_agp_populate(drm_ttm_backend_t *backend, unsigned long num_pages,
567 struct page **pages) {
569 drm_agp_ttm_priv *agp_priv = (drm_agp_ttm_priv *) backend->private;
570 struct page **cur_page, **last_page = pages + num_pages;
573 DRM_DEBUG("drm_agp_populate_ttm\n");
574 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
575 mem = drm_agp_allocate_memory(num_pages, agp_priv->alloc_type);
577 mem = drm_agp_allocate_memory(agp_priv->bridge, num_pages, agp_priv->alloc_type);
582 DRM_DEBUG("Current page count is %ld\n", (long) mem->page_count);
584 for (cur_page = pages; cur_page < last_page; ++cur_page) {
585 mem->memory[mem->page_count++] = phys_to_gart(page_to_phys(*cur_page));
591 static int drm_agp_bind_ttm(drm_ttm_backend_t *backend,
592 unsigned long offset,
595 drm_agp_ttm_priv *agp_priv = (drm_agp_ttm_priv *) backend->private;
596 DRM_AGP_MEM *mem = agp_priv->mem;
599 DRM_DEBUG("drm_agp_bind_ttm\n");
600 DRM_MASK_VAL(backend->flags, DRM_BE_FLAG_BOUND_CACHED,
601 (cached) ? DRM_BE_FLAG_BOUND_CACHED : 0);
602 mem->is_flushed = FALSE;
603 mem->type = (cached) ? agp_priv->cached_type : agp_priv->uncached_type;
604 ret = drm_agp_bind_memory(mem, offset);
606 DRM_ERROR("AGP Bind memory failed\n");
611 static int drm_agp_unbind_ttm(drm_ttm_backend_t *backend) {
613 drm_agp_ttm_priv *agp_priv = (drm_agp_ttm_priv *) backend->private;
615 DRM_DEBUG("drm_agp_unbind_ttm\n");
616 if (agp_priv->mem->is_bound)
617 return drm_agp_unbind_memory(agp_priv->mem);
622 static void drm_agp_clear_ttm(drm_ttm_backend_t *backend) {
624 drm_agp_ttm_priv *agp_priv = (drm_agp_ttm_priv *) backend->private;
625 DRM_AGP_MEM *mem = agp_priv->mem;
627 DRM_DEBUG("drm_agp_clear_ttm\n");
629 backend->unbind(backend);
630 agp_free_memory(mem);
633 agp_priv->mem = NULL;
636 static void drm_agp_destroy_ttm(drm_ttm_backend_t *backend) {
638 drm_agp_ttm_priv *agp_priv;
641 DRM_DEBUG("drm_agp_destroy_ttm\n");
642 agp_priv = (drm_agp_ttm_priv *) backend->private;
645 backend->clear(backend);
647 drm_free(agp_priv, sizeof(*agp_priv), DRM_MEM_MAPPINGS);
649 if (backend->flags & DRM_BE_FLAG_NEEDS_FREE)
650 drm_free(backend, sizeof(*backend), DRM_MEM_MAPPINGS);
655 drm_ttm_backend_t *drm_agp_init_ttm(struct drm_device *dev,
656 drm_ttm_backend_t *backend,
658 unsigned cached_type,
659 unsigned uncached_type) {
661 drm_ttm_backend_t *agp_be;
662 drm_agp_ttm_priv *agp_priv;
664 agp_be = (backend != NULL) ? backend:
665 drm_calloc(1, sizeof(*agp_be), DRM_MEM_MAPPINGS);
670 agp_priv = drm_calloc(1, sizeof(agp_priv), DRM_MEM_MAPPINGS);
673 drm_free(agp_be, sizeof(*agp_be), DRM_MEM_MAPPINGS);
677 agp_priv->mem = NULL;
678 agp_priv->alloc_type = alloc_type;
679 agp_priv->cached_type = cached_type;
680 agp_priv->uncached_type = uncached_type;
681 agp_priv->bridge = dev->agp->bridge;
682 agp_priv->populated = FALSE;
683 agp_be->aperture_base = dev->agp->agp_info.aper_base;
684 agp_be->private = (void *) agp_priv;
685 agp_be->needs_ub_cache_adjust = drm_agp_needs_unbind_cache_adjust;
686 agp_be->populate = drm_agp_populate;
687 agp_be->clear = drm_agp_clear_ttm;
688 agp_be->bind = drm_agp_bind_ttm;
689 agp_be->unbind = drm_agp_unbind_ttm;
690 agp_be->destroy = drm_agp_destroy_ttm;
691 DRM_MASK_VAL(agp_be->flags, DRM_BE_FLAG_NEEDS_FREE,
692 (backend == NULL) ? DRM_BE_FLAG_NEEDS_FREE : 0);
693 DRM_MASK_VAL(agp_be->flags, DRM_BE_FLAG_CBA,
694 (dev->agp->cant_use_aperture) ? DRM_BE_FLAG_CBA : 0);
695 agp_be->drm_map_type = _DRM_AGP;
698 EXPORT_SYMBOL(drm_agp_init_ttm);
700 #endif /* __OS_HAS_AGP */