Release 4.7 drivers/gpu/drm/ttm/ttm_tt.c
  
  
/**************************************************************************
 *
 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * 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
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * 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.
 *
 **************************************************************************/
/*
 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
 */
#define pr_fmt(fmt) "[TTM] " fmt
#include <linux/sched.h>
#include <linux/highmem.h>
#include <linux/pagemap.h>
#include <linux/shmem_fs.h>
#include <linux/file.h>
#include <linux/swap.h>
#include <linux/slab.h>
#include <linux/export.h>
#include <drm/drm_cache.h>
#include <drm/drm_mem_util.h>
#include <drm/ttm/ttm_module.h>
#include <drm/ttm/ttm_bo_driver.h>
#include <drm/ttm/ttm_placement.h>
#include <drm/ttm/ttm_page_alloc.h>
/**
 * Allocates storage for pointers to the pages that back the ttm.
 */
static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
{
	ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 19 | 67.86% | 1 | 25.00% | 
| dave airlie | dave airlie | 5 | 17.86% | 1 | 25.00% | 
| konrad rzeszutek wilk | konrad rzeszutek wilk | 3 | 10.71% | 1 | 25.00% | 
| jerome glisse | jerome glisse | 1 | 3.57% | 1 | 25.00% | 
 | Total | 28 | 100.00% | 4 | 100.00% | 
static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
{
	ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages,
					  sizeof(*ttm->ttm.pages) +
					  sizeof(*ttm->dma_address) +
					  sizeof(*ttm->cpu_address));
	ttm->cpu_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
	ttm->dma_address = (void *) (ttm->cpu_address + ttm->ttm.num_pages);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| alexandre courbot | alexandre courbot | 51 | 54.26% | 1 | 25.00% | 
| jerome glisse | jerome glisse | 20 | 21.28% | 1 | 25.00% | 
| thomas hellstrom | thomas hellstrom | 14 | 14.89% | 1 | 25.00% | 
| konrad rzeszutek wilk | konrad rzeszutek wilk | 9 | 9.57% | 1 | 25.00% | 
 | Total | 94 | 100.00% | 4 | 100.00% | 
#ifdef CONFIG_X86
static inline int ttm_tt_set_page_caching(struct page *p,
					  enum ttm_caching_state c_old,
					  enum ttm_caching_state c_new)
{
	int ret = 0;
	if (PageHighMem(p))
		return 0;
	if (c_old != tt_cached) {
		/* p isn't in the default caching state, set it to
                 * writeback first to free its current memtype. */
		ret = set_pages_wb(p, 1);
		if (ret)
			return ret;
	}
	if (c_new == tt_wc)
		ret = set_memory_wc((unsigned long) page_address(p), 1);
	else if (c_new == tt_uncached)
		ret = set_pages_uc(p, 1);
	return ret;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 56 | 55.45% | 1 | 33.33% | 
| francisco jerez | francisco jerez | 45 | 44.55% | 2 | 66.67% | 
 | Total | 101 | 100.00% | 3 | 100.00% | 
#else /* CONFIG_X86 */
static inline int ttm_tt_set_page_caching(struct page *p,
					  enum ttm_caching_state c_old,
					  enum ttm_caching_state c_new)
{
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 18 | 78.26% | 1 | 50.00% | 
| francisco jerez | francisco jerez | 5 | 21.74% | 1 | 50.00% | 
 | Total | 23 | 100.00% | 2 | 100.00% | 
#endif /* CONFIG_X86 */
/*
 * Change caching policy for the linear kernel map
 * for range of pages in a ttm.
 */
static int ttm_tt_set_caching(struct ttm_tt *ttm,
			      enum ttm_caching_state c_state)
{
	int i, j;
	struct page *cur_page;
	int ret;
	if (ttm->caching_state == c_state)
		return 0;
	if (ttm->state == tt_unpopulated) {
		/* Change caching but don't populate */
		ttm->caching_state = c_state;
		return 0;
	}
	if (ttm->caching_state == tt_cached)
		drm_clflush_pages(ttm->pages, ttm->num_pages);
	for (i = 0; i < ttm->num_pages; ++i) {
		cur_page = ttm->pages[i];
		if (likely(cur_page != NULL)) {
			ret = ttm_tt_set_page_caching(cur_page,
						      ttm->caching_state,
						      c_state);
			if (unlikely(ret != 0))
				goto out_err;
		}
	}
	ttm->caching_state = c_state;
	return 0;
out_err:
	for (j = 0; j < i; ++j) {
		cur_page = ttm->pages[j];
		if (likely(cur_page != NULL)) {
			(void)ttm_tt_set_page_caching(cur_page, c_state,
						      ttm->caching_state);
		}
	}
	return ret;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 185 | 91.13% | 1 | 25.00% | 
| pauli nieminen | pauli nieminen | 11 | 5.42% | 1 | 25.00% | 
| francisco jerez | francisco jerez | 6 | 2.96% | 1 | 25.00% | 
| dave airlie | dave airlie | 1 | 0.49% | 1 | 25.00% | 
 | Total | 203 | 100.00% | 4 | 100.00% | 
int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
{
	enum ttm_caching_state state;
	if (placement & TTM_PL_FLAG_WC)
		state = tt_wc;
	else if (placement & TTM_PL_FLAG_UNCACHED)
		state = tt_uncached;
	else
		state = tt_cached;
	return ttm_tt_set_caching(ttm, state);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 51 | 100.00% | 1 | 100.00% | 
 | Total | 51 | 100.00% | 1 | 100.00% | 
EXPORT_SYMBOL(ttm_tt_set_placement_caching);
void ttm_tt_destroy(struct ttm_tt *ttm)
{
	if (unlikely(ttm == NULL))
		return;
	if (ttm->state == tt_bound) {
		ttm_tt_unbind(ttm);
	}
	if (ttm->state == tt_unbound)
		ttm_tt_unpopulate(ttm);
	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
	    ttm->swap_storage)
		fput(ttm->swap_storage);
	ttm->swap_storage = NULL;
	ttm->func->destroy(ttm);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 62 | 72.94% | 1 | 20.00% | 
| jerome glisse | jerome glisse | 19 | 22.35% | 2 | 40.00% | 
| ben skeggs | ben skeggs | 3 | 3.53% | 1 | 20.00% | 
| jan engelhardt | jan engelhardt | 1 | 1.18% | 1 | 20.00% | 
 | Total | 85 | 100.00% | 5 | 100.00% | 
int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
		unsigned long size, uint32_t page_flags,
		struct page *dummy_read_page)
{
	ttm->bdev = bdev;
	ttm->glob = bdev->glob;
	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
	ttm->caching_state = tt_cached;
	ttm->page_flags = page_flags;
	ttm->dummy_read_page = dummy_read_page;
	ttm->state = tt_unpopulated;
	ttm->swap_storage = NULL;
	ttm_tt_alloc_page_directory(ttm);
	if (!ttm->pages) {
		ttm_tt_destroy(ttm);
		pr_err("Failed allocating page table\n");
		return -ENOMEM;
	}
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 64 | 55.17% | 2 | 28.57% | 
| jerome glisse | jerome glisse | 45 | 38.79% | 3 | 42.86% | 
| konrad rzeszutek wilk | konrad rzeszutek wilk | 5 | 4.31% | 1 | 14.29% | 
| joe perches | joe perches | 2 | 1.72% | 1 | 14.29% | 
 | Total | 116 | 100.00% | 7 | 100.00% | 
EXPORT_SYMBOL(ttm_tt_init);
void ttm_tt_fini(struct ttm_tt *ttm)
{
	drm_free_large(ttm->pages);
	ttm->pages = NULL;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| jerome glisse | jerome glisse | 23 | 100.00% | 1 | 100.00% | 
 | Total | 23 | 100.00% | 1 | 100.00% | 
EXPORT_SYMBOL(ttm_tt_fini);
int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
		unsigned long size, uint32_t page_flags,
		struct page *dummy_read_page)
{
	struct ttm_tt *ttm = &ttm_dma->ttm;
	ttm->bdev = bdev;
	ttm->glob = bdev->glob;
	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
	ttm->caching_state = tt_cached;
	ttm->page_flags = page_flags;
	ttm->dummy_read_page = dummy_read_page;
	ttm->state = tt_unpopulated;
	ttm->swap_storage = NULL;
	INIT_LIST_HEAD(&ttm_dma->pages_list);
	ttm_dma_tt_alloc_page_directory(ttm_dma);
	if (!ttm->pages) {
		ttm_tt_destroy(ttm);
		pr_err("Failed allocating page table\n");
		return -ENOMEM;
	}
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| jerome glisse | jerome glisse | 107 | 79.85% | 3 | 60.00% | 
| thomas hellstrom | thomas hellstrom | 25 | 18.66% | 1 | 20.00% | 
| joe perches | joe perches | 2 | 1.49% | 1 | 20.00% | 
 | Total | 134 | 100.00% | 5 | 100.00% | 
EXPORT_SYMBOL(ttm_dma_tt_init);
void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
{
	struct ttm_tt *ttm = &ttm_dma->ttm;
	drm_free_large(ttm->pages);
	ttm->pages = NULL;
	ttm_dma->cpu_address = NULL;
	ttm_dma->dma_address = NULL;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| jerome glisse | jerome glisse | 42 | 93.33% | 1 | 50.00% | 
| alexandre courbot | alexandre courbot | 3 | 6.67% | 1 | 50.00% | 
 | Total | 45 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL(ttm_dma_tt_fini);
void ttm_tt_unbind(struct ttm_tt *ttm)
{
	int ret;
	if (ttm->state == tt_bound) {
		ret = ttm->func->unbind(ttm);
		BUG_ON(ret);
		ttm->state = tt_unbound;
	}
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 43 | 95.56% | 1 | 50.00% | 
| jerome glisse | jerome glisse | 2 | 4.44% | 1 | 50.00% | 
 | Total | 45 | 100.00% | 2 | 100.00% | 
int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
{
	int ret = 0;
	if (!ttm)
		return -EINVAL;
	if (ttm->state == tt_bound)
		return 0;
	ret = ttm->bdev->driver->ttm_tt_populate(ttm);
	if (ret)
		return ret;
	ret = ttm->func->bind(ttm, bo_mem);
	if (unlikely(ret != 0))
		return ret;
	ttm->state = tt_bound;
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 86 | 91.49% | 2 | 50.00% | 
| jerome glisse | jerome glisse | 8 | 8.51% | 2 | 50.00% | 
 | Total | 94 | 100.00% | 4 | 100.00% | 
EXPORT_SYMBOL(ttm_tt_bind);
int ttm_tt_swapin(struct ttm_tt *ttm)
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
	int i;
	int ret = -ENOMEM;
	swap_storage = ttm->swap_storage;
	BUG_ON(swap_storage == NULL);
	swap_space = file_inode(swap_storage)->i_mapping;
	for (i = 0; i < ttm->num_pages; ++i) {
		from_page = shmem_read_mapping_page(swap_space, i);
		if (IS_ERR(from_page)) {
			ret = PTR_ERR(from_page);
			goto out_err;
		}
		to_page = ttm->pages[i];
		if (unlikely(to_page == NULL))
			goto out_err;
		copy_highpage(to_page, from_page);
		put_page(from_page);
	}
	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
		fput(swap_storage);
	ttm->swap_storage = NULL;
	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
	return 0;
out_err:
	return ret;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 151 | 85.80% | 1 | 12.50% | 
| maarten maathuis | maarten maathuis | 13 | 7.39% | 1 | 12.50% | 
| jerome glisse | jerome glisse | 4 | 2.27% | 1 | 12.50% | 
| al viro | al viro | 3 | 1.70% | 1 | 12.50% | 
| akinobu mita | akinobu mita | 2 | 1.14% | 1 | 12.50% | 
| kirill a. shutemov | kirill a. shutemov | 1 | 0.57% | 1 | 12.50% | 
| hugh dickins | hugh dickins | 1 | 0.57% | 1 | 12.50% | 
| jan engelhardt | jan engelhardt | 1 | 0.57% | 1 | 12.50% | 
 | Total | 176 | 100.00% | 8 | 100.00% | 
int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
{
	struct address_space *swap_space;
	struct file *swap_storage;
	struct page *from_page;
	struct page *to_page;
	int i;
	int ret = -ENOMEM;
	BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
	BUG_ON(ttm->caching_state != tt_cached);
	if (!persistent_swap_storage) {
		swap_storage = shmem_file_setup("ttm swap",
						ttm->num_pages << PAGE_SHIFT,
						0);
		if (IS_ERR(swap_storage)) {
			pr_err("Failed allocating swap storage\n");
			return PTR_ERR(swap_storage);
		}
	} else
		swap_storage = persistent_swap_storage;
	swap_space = file_inode(swap_storage)->i_mapping;
	for (i = 0; i < ttm->num_pages; ++i) {
		from_page = ttm->pages[i];
		if (unlikely(from_page == NULL))
			continue;
		to_page = shmem_read_mapping_page(swap_space, i);
		if (IS_ERR(to_page)) {
			ret = PTR_ERR(to_page);
			goto out_err;
		}
		copy_highpage(to_page, from_page);
		set_page_dirty(to_page);
		mark_page_accessed(to_page);
		put_page(to_page);
	}
	ttm_tt_unpopulate(ttm);
	ttm->swap_storage = swap_storage;
	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
	if (persistent_swap_storage)
		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
	return 0;
out_err:
	if (!persistent_swap_storage)
		fput(swap_storage);
	return ret;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 216 | 84.71% | 1 | 11.11% | 
| maarten maathuis | maarten maathuis | 23 | 9.02% | 1 | 11.11% | 
| jan engelhardt | jan engelhardt | 6 | 2.35% | 1 | 11.11% | 
| al viro | al viro | 3 | 1.18% | 1 | 11.11% | 
| joe perches | joe perches | 2 | 0.78% | 1 | 11.11% | 
| akinobu mita | akinobu mita | 2 | 0.78% | 1 | 11.11% | 
| jerome glisse | jerome glisse | 1 | 0.39% | 1 | 11.11% | 
| kirill a. shutemov | kirill a. shutemov | 1 | 0.39% | 1 | 11.11% | 
| hugh dickins | hugh dickins | 1 | 0.39% | 1 | 11.11% | 
 | Total | 255 | 100.00% | 9 | 100.00% | 
static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
{
	pgoff_t i;
	struct page **page = ttm->pages;
	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
		return;
	for (i = 0; i < ttm->num_pages; ++i) {
		(*page)->mapping = NULL;
		(*page++)->index = 0;
	}
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 69 | 100.00% | 2 | 100.00% | 
 | Total | 69 | 100.00% | 2 | 100.00% | 
void ttm_tt_unpopulate(struct ttm_tt *ttm)
{
	if (ttm->state == tt_unpopulated)
		return;
	ttm_tt_clear_mapping(ttm);
	ttm->bdev->driver->ttm_tt_unpopulate(ttm);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 35 | 100.00% | 1 | 100.00% | 
 | Total | 35 | 100.00% | 1 | 100.00% | 
Overall Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| thomas hellstrom | thomas hellstrom | 1133 | 67.88% | 5 | 17.24% | 
| jerome glisse | jerome glisse | 291 | 17.44% | 4 | 13.79% | 
| francisco jerez | francisco jerez | 56 | 3.36% | 2 | 6.90% | 
| alexandre courbot | alexandre courbot | 54 | 3.24% | 1 | 3.45% | 
| maarten maathuis | maarten maathuis | 36 | 2.16% | 1 | 3.45% | 
| konrad rzeszutek wilk | konrad rzeszutek wilk | 17 | 1.02% | 2 | 6.90% | 
| dave airlie | dave airlie | 16 | 0.96% | 3 | 10.34% | 
| pauli nieminen | pauli nieminen | 13 | 0.78% | 1 | 3.45% | 
| joe perches | joe perches | 13 | 0.78% | 1 | 3.45% | 
| jan engelhardt | jan engelhardt | 8 | 0.48% | 1 | 3.45% | 
| al viro | al viro | 6 | 0.36% | 1 | 3.45% | 
| david howells | david howells | 6 | 0.36% | 1 | 3.45% | 
| hugh dickins | hugh dickins | 5 | 0.30% | 1 | 3.45% | 
| akinobu mita | akinobu mita | 4 | 0.24% | 1 | 3.45% | 
| tejun heo | tejun heo | 3 | 0.18% | 1 | 3.45% | 
| paul gortmaker | paul gortmaker | 3 | 0.18% | 1 | 3.45% | 
| ben skeggs | ben skeggs | 3 | 0.18% | 1 | 3.45% | 
| kirill a. shutemov | kirill a. shutemov | 2 | 0.12% | 1 | 3.45% | 
 | Total | 1669 | 100.00% | 29 | 100.00% | 
  
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.