summaryrefslogtreecommitdiff
path: root/linux-core/drm_ttm.c
diff options
context:
space:
mode:
authorNian Wu <nian@graphics.(none)>2007-02-25 17:06:13 -0800
committerNian Wu <nian@graphics.(none)>2007-02-25 17:06:13 -0800
commitdf2fc3ec62d6799a3266cfb18b1279a126892f44 (patch)
tree94d39b9172bce8c93cb035e40d12e635dfaeb12f /linux-core/drm_ttm.c
parent80095ffe01efe79983c2124ecc99ce979d7ac6a9 (diff)
parent9d8ba2d0d479f53b996c0a0e366acfee52daab3b (diff)
Merge git://proxy01.pd.intel.com:9419/git/mesa/drm into crestline
Diffstat (limited to 'linux-core/drm_ttm.c')
-rw-r--r--linux-core/drm_ttm.c296
1 files changed, 60 insertions, 236 deletions
diff --git a/linux-core/drm_ttm.c b/linux-core/drm_ttm.c
index c17c41cb..e67719e2 100644
--- a/linux-core/drm_ttm.c
+++ b/linux-core/drm_ttm.c
@@ -1,6 +1,6 @@
/**************************************************************************
*
- * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
+ * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -11,6 +11,10 @@
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
@@ -18,13 +22,11 @@
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- *
*
**************************************************************************/
+/*
+ * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
+ */
#include "drmP.h"
@@ -33,18 +35,17 @@ static void drm_ttm_ipi_handler(void *null)
flush_agp_cache();
}
-static void drm_ttm_cache_flush(void)
+static void drm_ttm_cache_flush(void)
{
if (on_each_cpu(drm_ttm_ipi_handler, NULL, 1, 1) != 0)
DRM_ERROR("Timed out waiting for drm cache flush.\n");
}
-
/*
* Use kmalloc if possible. Otherwise fall back to vmalloc.
*/
-static void ttm_alloc_pages(drm_ttm_t *ttm)
+static void ttm_alloc_pages(drm_ttm_t * ttm)
{
unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
ttm->pages = NULL;
@@ -65,7 +66,7 @@ static void ttm_alloc_pages(drm_ttm_t *ttm)
}
}
-static void ttm_free_pages(drm_ttm_t *ttm)
+static void ttm_free_pages(drm_ttm_t * ttm)
{
unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
@@ -79,27 +80,24 @@ static void ttm_free_pages(drm_ttm_t *ttm)
ttm->pages = NULL;
}
-/*
- * Unmap all vma pages from vmas mapping this ttm.
- */
-
-static int unmap_vma_pages(drm_ttm_t * ttm)
+static struct page *drm_ttm_alloc_page(void)
{
- drm_device_t *dev = ttm->dev;
- loff_t offset = ((loff_t) ttm->mapping_offset) << PAGE_SHIFT;
- loff_t holelen = ((loff_t) ttm->num_pages) << PAGE_SHIFT;
+ struct page *page;
-#ifdef DRM_ODD_MM_COMPAT
- int ret;
- ret = drm_ttm_lock_mm(ttm);
- if (ret)
- return ret;
-#endif
- unmap_mapping_range(dev->dev_mapping, offset, holelen, 1);
-#ifdef DRM_ODD_MM_COMPAT
- drm_ttm_finish_unmap(ttm);
+ if (drm_alloc_memctl(PAGE_SIZE)) {
+ return NULL;
+ }
+ page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32);
+ if (!page) {
+ drm_free_memctl(PAGE_SIZE);
+ return NULL;
+ }
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
+ SetPageLocked(page);
+#else
+ SetPageReserved(page);
#endif
- return 0;
+ return page;
}
/*
@@ -116,7 +114,7 @@ static int drm_set_caching(drm_ttm_t * ttm, int noncached)
if ((ttm->page_flags & DRM_TTM_PAGE_UNCACHED) == noncached)
return 0;
- if (noncached)
+ if (noncached)
drm_ttm_cache_flush();
for (i = 0; i < ttm->num_pages; ++i) {
@@ -135,7 +133,7 @@ static int drm_set_caching(drm_ttm_t * ttm, int noncached)
if (do_tlbflush)
flush_agp_mappings();
- DRM_MASK_VAL(ttm->page_flags, DRM_TTM_PAGE_UNCACHED, noncached);
+ DRM_FLAG_MASKED(ttm->page_flags, noncached, DRM_TTM_PAGE_UNCACHED);
return 0;
}
@@ -154,18 +152,6 @@ int drm_destroy_ttm(drm_ttm_t * ttm)
if (!ttm)
return 0;
- if (atomic_read(&ttm->vma_count) > 0) {
- ttm->destroy = 1;
- DRM_ERROR("VMAs are still alive. Skipping destruction.\n");
- return -EBUSY;
- }
-
- DRM_DEBUG("Destroying a ttm\n");
-
-#ifdef DRM_TTM_ODD_COMPAT
- BUG_ON(!list_empty(&ttm->vma_list));
- BUG_ON(!list_empty(&ttm->p_mm_list));
-#endif
be = ttm->be;
if (be) {
be->destroy(be);
@@ -193,11 +179,6 @@ int drm_destroy_ttm(drm_ttm_t * ttm)
DRM_ERROR("Erroneous map count. "
"Leaking page mappings.\n");
}
-
- /*
- * End debugging.
- */
-
__free_page(*cur_page);
drm_free_memctl(PAGE_SIZE);
--bm->cur_pages;
@@ -210,37 +191,36 @@ int drm_destroy_ttm(drm_ttm_t * ttm)
return 0;
}
+struct page *drm_ttm_get_page(drm_ttm_t * ttm, int index)
+{
+ struct page *p;
+ drm_buffer_manager_t *bm = &ttm->dev->bm;
+
+ p = ttm->pages[index];
+ if (!p) {
+ p = drm_ttm_alloc_page();
+ if (!p)
+ return NULL;
+ ttm->pages[index] = p;
+ ++bm->cur_pages;
+ }
+ return p;
+}
+
static int drm_ttm_populate(drm_ttm_t * ttm)
{
struct page *page;
unsigned long i;
- drm_buffer_manager_t *bm;
drm_ttm_backend_t *be;
if (ttm->state != ttm_unpopulated)
return 0;
- bm = &ttm->dev->bm;
be = ttm->be;
for (i = 0; i < ttm->num_pages; ++i) {
- page = ttm->pages[i];
- if (!page) {
- if (drm_alloc_memctl(PAGE_SIZE)) {
- return -ENOMEM;
- }
- page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32);
- if (!page) {
- drm_free_memctl(PAGE_SIZE);
- return -ENOMEM;
- }
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
- SetPageLocked(page);
-#else
- SetPageReserved(page);
-#endif
- ttm->pages[i] = page;
- ++bm->cur_pages;
- }
+ page = drm_ttm_get_page(ttm, i);
+ if (!page)
+ return -ENOMEM;
}
be->populate(be, ttm->num_pages, ttm->pages);
ttm->state = ttm_unbound;
@@ -251,7 +231,7 @@ static int drm_ttm_populate(drm_ttm_t * ttm)
* Initialize a ttm.
*/
-static drm_ttm_t *drm_init_ttm(struct drm_device *dev, unsigned long size)
+drm_ttm_t *drm_ttm_init(struct drm_device * dev, unsigned long size)
{
drm_bo_driver_t *bo_driver = dev->driver->bo_driver;
drm_ttm_t *ttm;
@@ -263,11 +243,6 @@ static drm_ttm_t *drm_init_ttm(struct drm_device *dev, unsigned long size)
if (!ttm)
return NULL;
-#ifdef DRM_ODD_MM_COMPAT
- INIT_LIST_HEAD(&ttm->p_mm_list);
- INIT_LIST_HEAD(&ttm->vma_list);
-#endif
-
ttm->dev = dev;
atomic_set(&ttm->vma_count, 0);
@@ -300,29 +275,20 @@ static drm_ttm_t *drm_init_ttm(struct drm_device *dev, unsigned long size)
* Unbind a ttm region from the aperture.
*/
-int drm_evict_ttm(drm_ttm_t * ttm)
+void drm_ttm_evict(drm_ttm_t * ttm)
{
drm_ttm_backend_t *be = ttm->be;
int ret;
- switch (ttm->state) {
- case ttm_bound:
- if (be->needs_ub_cache_adjust(be)) {
- ret = unmap_vma_pages(ttm);
- if (ret) {
- return ret;
- }
- }
- be->unbind(be);
- break;
- default:
- break;
+ if (ttm->state == ttm_bound) {
+ ret = be->unbind(be);
+ BUG_ON(ret);
}
+
ttm->state = ttm_evicted;
- return 0;
}
-void drm_fixup_ttm_caching(drm_ttm_t * ttm)
+void drm_ttm_fixup_caching(drm_ttm_t * ttm)
{
if (ttm->state == ttm_evicted) {
@@ -334,18 +300,12 @@ void drm_fixup_ttm_caching(drm_ttm_t * ttm)
}
}
-int drm_unbind_ttm(drm_ttm_t * ttm)
+void drm_ttm_unbind(drm_ttm_t * ttm)
{
- int ret = 0;
-
if (ttm->state == ttm_bound)
- ret = drm_evict_ttm(ttm);
-
- if (ret)
- return ret;
+ drm_ttm_evict(ttm);
- drm_fixup_ttm_caching(ttm);
- return 0;
+ drm_ttm_fixup_caching(ttm);
}
int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset)
@@ -364,26 +324,13 @@ int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset)
ret = drm_ttm_populate(ttm);
if (ret)
return ret;
- if (ttm->state == ttm_unbound && !cached) {
- ret = unmap_vma_pages(ttm);
- if (ret)
- return ret;
+ if (ttm->state == ttm_unbound && !cached) {
drm_set_caching(ttm, DRM_TTM_PAGE_UNCACHED);
}
-#ifdef DRM_ODD_MM_COMPAT
- else if (ttm->state == ttm_evicted && !cached) {
- ret = drm_ttm_lock_mm(ttm);
- if (ret)
- return ret;
- }
-#endif
+
if ((ret = be->bind(be, aper_offset, cached))) {
ttm->state = ttm_evicted;
-#ifdef DRM_ODD_MM_COMPAT
- if (be->needs_ub_cache_adjust(be))
- drm_ttm_unlock_mm(ttm);
-#endif
DRM_ERROR("Couldn't bind backend.\n");
return ret;
}
@@ -391,130 +338,7 @@ int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset)
ttm->aper_offset = aper_offset;
ttm->state = ttm_bound;
-#ifdef DRM_ODD_MM_COMPAT
- if (be->needs_ub_cache_adjust(be)) {
- ret = drm_ttm_remap_bound(ttm);
- if (ret)
- return ret;
- }
-#endif
-
return 0;
}
-/*
- * dev->struct_mutex locked.
- */
-static void drm_ttm_object_remove(drm_device_t * dev, drm_ttm_object_t * object)
-{
- drm_map_list_t *list = &object->map_list;
- drm_local_map_t *map;
-
- if (list->user_token)
- drm_ht_remove_item(&dev->map_hash, &list->hash);
-
- if (list->file_offset_node) {
- drm_mm_put_block(list->file_offset_node);
- list->file_offset_node = NULL;
- }
-
- map = list->map;
-
- if (map) {
- drm_ttm_t *ttm = (drm_ttm_t *) map->offset;
- if (ttm) {
- if (drm_destroy_ttm(ttm) != -EBUSY) {
- drm_ctl_free(map, sizeof(*map), DRM_MEM_TTM);
- }
- } else {
- drm_ctl_free(map, sizeof(*map), DRM_MEM_TTM);
- }
- }
-
- drm_ctl_free(object, sizeof(*object), DRM_MEM_TTM);
-}
-
-void drm_ttm_object_deref_locked(drm_device_t * dev, drm_ttm_object_t * to)
-{
- if (atomic_dec_and_test(&to->usage)) {
- drm_ttm_object_remove(dev, to);
- }
-}
-
-void drm_ttm_object_deref_unlocked(drm_device_t * dev, drm_ttm_object_t * to)
-{
- if (atomic_dec_and_test(&to->usage)) {
- mutex_lock(&dev->struct_mutex);
- if (atomic_read(&to->usage) == 0)
- drm_ttm_object_remove(dev, to);
- mutex_unlock(&dev->struct_mutex);
- }
-}
-
-/*
- * Create a ttm and add it to the drm book-keeping.
- * dev->struct_mutex locked.
- */
-
-int drm_ttm_object_create(drm_device_t * dev, unsigned long size,
- uint32_t flags, drm_ttm_object_t ** ttm_object)
-{
- drm_ttm_object_t *object;
- drm_map_list_t *list;
- drm_local_map_t *map;
- drm_ttm_t *ttm;
-
- object = drm_ctl_calloc(1, sizeof(*object), DRM_MEM_TTM);
- if (!object)
- return -ENOMEM;
- object->flags = flags;
- list = &object->map_list;
-
- list->map = drm_ctl_calloc(1, sizeof(*map), DRM_MEM_TTM);
- if (!list->map) {
- drm_ttm_object_remove(dev, object);
- return -ENOMEM;
- }
- map = list->map;
-
- ttm = drm_init_ttm(dev, size);
- if (!ttm) {
- DRM_ERROR("Could not create ttm\n");
- drm_ttm_object_remove(dev, object);
- return -ENOMEM;
- }
-
- map->offset = (unsigned long)ttm;
- map->type = _DRM_TTM;
- map->flags = _DRM_REMOVABLE;
- map->size = ttm->num_pages * PAGE_SIZE;
- map->handle = (void *)object;
-
- /*
- * Add a one-page "hole" to the block size to avoid the mm subsystem
- * merging vmas.
- * FIXME: Is this really needed?
- */
-
- list->file_offset_node = drm_mm_search_free(&dev->offset_manager,
- ttm->num_pages + 1, 0, 0);
- if (!list->file_offset_node) {
- drm_ttm_object_remove(dev, object);
- return -ENOMEM;
- }
- list->file_offset_node = drm_mm_get_block(list->file_offset_node,
- ttm->num_pages + 1, 0);
-
- list->hash.key = list->file_offset_node->start;
-
- if (drm_ht_insert_item(&dev->map_hash, &list->hash)) {
- drm_ttm_object_remove(dev, object);
- return -ENOMEM;
- }
-
- list->user_token = ((drm_u64_t) list->hash.key) << PAGE_SHIFT;
- ttm->mapping_offset = list->hash.key;
- atomic_set(&object->usage, 1);
- *ttm_object = object;
- return 0;
-}
+EXPORT_SYMBOL(drm_bind_ttm);