drm_cache.c 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/**************************************************************************
 *
 * 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
 * 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 Hellström <thomas-at-tungstengraphics-dot-com>
 */

31
#include <linux/export.h>
32
#include <drm/drmP.h>
33 34

#if defined(CONFIG_X86)
B
Ben Widawsky 已提交
35
#include <asm/smp.h>
36 37 38 39 40 41

/*
 * clflushopt is an unordered instruction which needs fencing with mfence or
 * sfence to avoid ordering issues.  For drm_clflush_page this fencing happens
 * in the caller.
 */
42 43 44 45 46
static void
drm_clflush_page(struct page *page)
{
	uint8_t *page_virtual;
	unsigned int i;
D
Dave Airlie 已提交
47
	const int size = boot_cpu_data.x86_clflush_size;
48 49 50 51

	if (unlikely(page == NULL))
		return;

52
	page_virtual = kmap_atomic(page);
D
Dave Airlie 已提交
53
	for (i = 0; i < PAGE_SIZE; i += size)
54
		clflushopt(page_virtual + i);
55
	kunmap_atomic(page_virtual);
56 57
}

58 59 60 61 62 63 64 65 66 67 68
static void drm_cache_flush_clflush(struct page *pages[],
				    unsigned long num_pages)
{
	unsigned long i;

	mb();
	for (i = 0; i < num_pages; i++)
		drm_clflush_page(*pages++);
	mb();
}
#endif
69

70 71 72 73 74
void
drm_clflush_pages(struct page *pages[], unsigned long num_pages)
{

#if defined(CONFIG_X86)
75
	if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
76
		drm_cache_flush_clflush(pages, num_pages);
77 78 79
		return;
	}

B
Ben Widawsky 已提交
80
	if (wbinvd_on_all_cpus())
81 82 83 84 85 86 87 88 89 90 91
		printk(KERN_ERR "Timed out waiting for cache flush.\n");

#elif defined(__powerpc__)
	unsigned long i;
	for (i = 0; i < num_pages; i++) {
		struct page *page = pages[i];
		void *page_virtual;

		if (unlikely(page == NULL))
			continue;

92
		page_virtual = kmap_atomic(page);
93 94
		flush_dcache_range((unsigned long)page_virtual,
				   (unsigned long)page_virtual + PAGE_SIZE);
95
		kunmap_atomic(page_virtual);
96 97
	}
#else
98 99
	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
	WARN_ON_ONCE(1);
D
Dave Airlie 已提交
100
#endif
101 102
}
EXPORT_SYMBOL(drm_clflush_pages);
103

104 105 106 107
void
drm_clflush_sg(struct sg_table *st)
{
#if defined(CONFIG_X86)
108
	if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
109
		struct sg_page_iter sg_iter;
110 111

		mb();
112
		for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
113
			drm_clflush_page(sg_page_iter_page(&sg_iter));
114 115 116 117 118
		mb();

		return;
	}

B
Ben Widawsky 已提交
119
	if (wbinvd_on_all_cpus())
120 121 122 123 124 125 126 127
		printk(KERN_ERR "Timed out waiting for cache flush.\n");
#else
	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
	WARN_ON_ONCE(1);
#endif
}
EXPORT_SYMBOL(drm_clflush_sg);

128
void
129
drm_clflush_virt_range(void *addr, unsigned long length)
130 131
{
#if defined(CONFIG_X86)
132
	if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
133
		const int size = boot_cpu_data.x86_clflush_size;
134
		void *end = addr + length;
135
		addr = (void *)(((unsigned long)addr) & -size);
136
		mb();
137
		for (; addr < end; addr += size)
138
			clflushopt(addr);
139
		clflushopt(end - 1); /* force serialisation */
140 141 142 143
		mb();
		return;
	}

B
Ben Widawsky 已提交
144
	if (wbinvd_on_all_cpus())
145 146 147 148 149 150 151
		printk(KERN_ERR "Timed out waiting for cache flush.\n");
#else
	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
	WARN_ON_ONCE(1);
#endif
}
EXPORT_SYMBOL(drm_clflush_virt_range);