xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/drm_cache.c (revision 873b6c058bf3340036a7c418714d2f6169cdac35)
1*873b6c05Sriastradh /*	$NetBSD: drm_cache.c,v 1.4 2021/12/19 01:24:25 riastradh Exp $	*/
2efa246c0Sriastradh 
3fcd0cb28Sriastradh /**************************************************************************
4fcd0cb28Sriastradh  *
5fcd0cb28Sriastradh  * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
6fcd0cb28Sriastradh  * All Rights Reserved.
7fcd0cb28Sriastradh  *
8fcd0cb28Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
9fcd0cb28Sriastradh  * copy of this software and associated documentation files (the
10fcd0cb28Sriastradh  * "Software"), to deal in the Software without restriction, including
11fcd0cb28Sriastradh  * without limitation the rights to use, copy, modify, merge, publish,
12fcd0cb28Sriastradh  * distribute, sub license, and/or sell copies of the Software, and to
13fcd0cb28Sriastradh  * permit persons to whom the Software is furnished to do so, subject to
14fcd0cb28Sriastradh  * the following conditions:
15fcd0cb28Sriastradh  *
16fcd0cb28Sriastradh  * The above copyright notice and this permission notice (including the
17fcd0cb28Sriastradh  * next paragraph) shall be included in all copies or substantial portions
18fcd0cb28Sriastradh  * of the Software.
19fcd0cb28Sriastradh  *
20fcd0cb28Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21fcd0cb28Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22fcd0cb28Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23fcd0cb28Sriastradh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24fcd0cb28Sriastradh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25fcd0cb28Sriastradh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26fcd0cb28Sriastradh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27fcd0cb28Sriastradh  *
28fcd0cb28Sriastradh  **************************************************************************/
29fcd0cb28Sriastradh /*
30fcd0cb28Sriastradh  * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
31fcd0cb28Sriastradh  */
32fcd0cb28Sriastradh 
33efa246c0Sriastradh #include <sys/cdefs.h>
34*873b6c05Sriastradh __KERNEL_RCSID(0, "$NetBSD: drm_cache.c,v 1.4 2021/12/19 01:24:25 riastradh Exp $");
35efa246c0Sriastradh 
36fcd0cb28Sriastradh #include <linux/export.h>
3741ec0267Sriastradh #include <linux/highmem.h>
3841ec0267Sriastradh 
3941ec0267Sriastradh #include <drm/drm_cache.h>
40fcd0cb28Sriastradh 
41fcd0cb28Sriastradh #if defined(CONFIG_X86)
42efa246c0Sriastradh #include <asm/smp.h>
439d20d926Sriastradh 
449d20d926Sriastradh /*
459d20d926Sriastradh  * clflushopt is an unordered instruction which needs fencing with mfence or
469d20d926Sriastradh  * sfence to avoid ordering issues.  For drm_clflush_page this fencing happens
479d20d926Sriastradh  * in the caller.
489d20d926Sriastradh  */
49fcd0cb28Sriastradh static void
drm_clflush_page(struct page * page)50fcd0cb28Sriastradh drm_clflush_page(struct page *page)
51fcd0cb28Sriastradh {
52fcd0cb28Sriastradh 	uint8_t *page_virtual;
53fcd0cb28Sriastradh 	unsigned int i;
54fcd0cb28Sriastradh 	const int size = boot_cpu_data.x86_clflush_size;
55fcd0cb28Sriastradh 
56fcd0cb28Sriastradh 	if (unlikely(page == NULL))
57fcd0cb28Sriastradh 		return;
58fcd0cb28Sriastradh 
59fcd0cb28Sriastradh 	page_virtual = kmap_atomic(page);
60fcd0cb28Sriastradh 	for (i = 0; i < PAGE_SIZE; i += size)
619d20d926Sriastradh 		clflushopt(page_virtual + i);
62fcd0cb28Sriastradh 	kunmap_atomic(page_virtual);
63fcd0cb28Sriastradh }
64fcd0cb28Sriastradh 
drm_cache_flush_clflush(struct page * pages[],unsigned long num_pages)65fcd0cb28Sriastradh static void drm_cache_flush_clflush(struct page *pages[],
66fcd0cb28Sriastradh 				    unsigned long num_pages)
67fcd0cb28Sriastradh {
68fcd0cb28Sriastradh 	unsigned long i;
69fcd0cb28Sriastradh 
7041ec0267Sriastradh 	mb(); /*Full memory barrier used before so that CLFLUSH is ordered*/
71fcd0cb28Sriastradh 	for (i = 0; i < num_pages; i++)
72fcd0cb28Sriastradh 		drm_clflush_page(*pages++);
7341ec0267Sriastradh 	mb(); /*Also used after CLFLUSH so that all cache is flushed*/
74fcd0cb28Sriastradh }
75fcd0cb28Sriastradh #endif
76fcd0cb28Sriastradh 
7741ec0267Sriastradh /**
7841ec0267Sriastradh  * drm_clflush_pages - Flush dcache lines of a set of pages.
7941ec0267Sriastradh  * @pages: List of pages to be flushed.
8041ec0267Sriastradh  * @num_pages: Number of pages in the array.
8141ec0267Sriastradh  *
8241ec0267Sriastradh  * Flush every data cache line entry that points to an address belonging
8341ec0267Sriastradh  * to a page in the array.
8441ec0267Sriastradh  */
85fcd0cb28Sriastradh void
drm_clflush_pages(struct page * pages[],unsigned long num_pages)86fcd0cb28Sriastradh drm_clflush_pages(struct page *pages[], unsigned long num_pages)
87fcd0cb28Sriastradh {
88fcd0cb28Sriastradh 
89fcd0cb28Sriastradh #if defined(CONFIG_X86)
9041ec0267Sriastradh 	if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
91fcd0cb28Sriastradh 		drm_cache_flush_clflush(pages, num_pages);
92fcd0cb28Sriastradh 		return;
93fcd0cb28Sriastradh 	}
94fcd0cb28Sriastradh 
95efa246c0Sriastradh 	if (wbinvd_on_all_cpus())
9641ec0267Sriastradh 		pr_err("Timed out waiting for cache flush\n");
97fcd0cb28Sriastradh 
98fcd0cb28Sriastradh #elif defined(__powerpc__)
99fcd0cb28Sriastradh 	unsigned long i;
10041ec0267Sriastradh 
101fcd0cb28Sriastradh 	for (i = 0; i < num_pages; i++) {
102fcd0cb28Sriastradh 		struct page *page = pages[i];
103fcd0cb28Sriastradh 		void *page_virtual;
104fcd0cb28Sriastradh 
105fcd0cb28Sriastradh 		if (unlikely(page == NULL))
106fcd0cb28Sriastradh 			continue;
107fcd0cb28Sriastradh 
108fcd0cb28Sriastradh 		page_virtual = kmap_atomic(page);
109fcd0cb28Sriastradh 		flush_dcache_range((unsigned long)page_virtual,
110fcd0cb28Sriastradh 				   (unsigned long)page_virtual + PAGE_SIZE);
111fcd0cb28Sriastradh 		kunmap_atomic(page_virtual);
112fcd0cb28Sriastradh 	}
113fcd0cb28Sriastradh #else
11441ec0267Sriastradh 	pr_err("Architecture has no drm_cache.c support\n");
115fcd0cb28Sriastradh 	WARN_ON_ONCE(1);
116fcd0cb28Sriastradh #endif
117fcd0cb28Sriastradh }
118fcd0cb28Sriastradh EXPORT_SYMBOL(drm_clflush_pages);
119fcd0cb28Sriastradh 
12041ec0267Sriastradh /**
12141ec0267Sriastradh  * drm_clflush_sg - Flush dcache lines pointing to a scather-gather.
12241ec0267Sriastradh  * @st: struct sg_table.
12341ec0267Sriastradh  *
12441ec0267Sriastradh  * Flush every data cache line entry that points to an address in the
12541ec0267Sriastradh  * sg.
12641ec0267Sriastradh  */
127fcd0cb28Sriastradh void
drm_clflush_sg(struct sg_table * st)128fcd0cb28Sriastradh drm_clflush_sg(struct sg_table *st)
129fcd0cb28Sriastradh {
130fcd0cb28Sriastradh #if defined(CONFIG_X86)
13141ec0267Sriastradh 	if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
1329d20d926Sriastradh 		struct sg_page_iter sg_iter;
133fcd0cb28Sriastradh 
13441ec0267Sriastradh 		mb(); /*CLFLUSH is ordered only by using memory barriers*/
1359d20d926Sriastradh 		for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
1369d20d926Sriastradh 			drm_clflush_page(sg_page_iter_page(&sg_iter));
13741ec0267Sriastradh 		mb(); /*Make sure that all cache line entry is flushed*/
138fcd0cb28Sriastradh 
139fcd0cb28Sriastradh 		return;
140fcd0cb28Sriastradh 	}
141fcd0cb28Sriastradh 
142efa246c0Sriastradh 	if (wbinvd_on_all_cpus())
14341ec0267Sriastradh 		pr_err("Timed out waiting for cache flush\n");
144fcd0cb28Sriastradh #else
14541ec0267Sriastradh 	pr_err("Architecture has no drm_cache.c support\n");
146fcd0cb28Sriastradh 	WARN_ON_ONCE(1);
147fcd0cb28Sriastradh #endif
148fcd0cb28Sriastradh }
149fcd0cb28Sriastradh EXPORT_SYMBOL(drm_clflush_sg);
150fcd0cb28Sriastradh 
15141ec0267Sriastradh /**
15241ec0267Sriastradh  * drm_clflush_virt_range - Flush dcache lines of a region
15341ec0267Sriastradh  * @addr: Initial kernel memory address.
15441ec0267Sriastradh  * @length: Region size.
15541ec0267Sriastradh  *
15641ec0267Sriastradh  * Flush every data cache line entry that points to an address in the
15741ec0267Sriastradh  * region requested.
15841ec0267Sriastradh  */
159fcd0cb28Sriastradh void
drm_clflush_virt_range(void * addr,unsigned long length)160efa246c0Sriastradh drm_clflush_virt_range(void *addr, unsigned long length)
161fcd0cb28Sriastradh {
162fcd0cb28Sriastradh #if defined(CONFIG_X86)
16341ec0267Sriastradh 	if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
164*873b6c05Sriastradh #ifdef __NetBSD__
165*873b6c05Sriastradh 		const int size = cpu_info_primary.ci_cflush_lsize;
166*873b6c05Sriastradh #else
167efa246c0Sriastradh 		const int size = boot_cpu_data.x86_clflush_size;
168*873b6c05Sriastradh #endif
169efa246c0Sriastradh 		void *end = addr + length;
17041ec0267Sriastradh 
171efa246c0Sriastradh 		addr = (void *)(((unsigned long)addr) & -size);
17241ec0267Sriastradh 		mb(); /*CLFLUSH is only ordered with a full memory barrier*/
173efa246c0Sriastradh 		for (; addr < end; addr += size)
174efa246c0Sriastradh 			clflushopt(addr);
175efa246c0Sriastradh 		clflushopt(end - 1); /* force serialisation */
17641ec0267Sriastradh 		mb(); /*Ensure that evry data cache line entry is flushed*/
177fcd0cb28Sriastradh 		return;
178fcd0cb28Sriastradh 	}
179fcd0cb28Sriastradh 
180efa246c0Sriastradh 	if (wbinvd_on_all_cpus())
18141ec0267Sriastradh 		pr_err("Timed out waiting for cache flush\n");
182fcd0cb28Sriastradh #else
18341ec0267Sriastradh 	pr_err("Architecture has no drm_cache.c support\n");
184fcd0cb28Sriastradh 	WARN_ON_ONCE(1);
185fcd0cb28Sriastradh #endif
186fcd0cb28Sriastradh }
187fcd0cb28Sriastradh EXPORT_SYMBOL(drm_clflush_virt_range);
188