xref: /openbmc/linux/drivers/gpu/drm/drm_cache.c (revision 2d1a8a48)
1673a394bSEric Anholt /**************************************************************************
2673a394bSEric Anholt  *
3673a394bSEric Anholt  * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
4673a394bSEric Anholt  * All Rights Reserved.
5673a394bSEric Anholt  *
6673a394bSEric Anholt  * Permission is hereby granted, free of charge, to any person obtaining a
7673a394bSEric Anholt  * copy of this software and associated documentation files (the
8673a394bSEric Anholt  * "Software"), to deal in the Software without restriction, including
9673a394bSEric Anholt  * without limitation the rights to use, copy, modify, merge, publish,
10673a394bSEric Anholt  * distribute, sub license, and/or sell copies of the Software, and to
11673a394bSEric Anholt  * permit persons to whom the Software is furnished to do so, subject to
12673a394bSEric Anholt  * the following conditions:
13673a394bSEric Anholt  *
14673a394bSEric Anholt  * The above copyright notice and this permission notice (including the
15673a394bSEric Anholt  * next paragraph) shall be included in all copies or substantial portions
16673a394bSEric Anholt  * of the Software.
17673a394bSEric Anholt  *
18673a394bSEric Anholt  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19673a394bSEric Anholt  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20673a394bSEric Anholt  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21673a394bSEric Anholt  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22673a394bSEric Anholt  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23673a394bSEric Anholt  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24673a394bSEric Anholt  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25673a394bSEric Anholt  *
26673a394bSEric Anholt  **************************************************************************/
27673a394bSEric Anholt /*
28673a394bSEric Anholt  * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
29673a394bSEric Anholt  */
30673a394bSEric Anholt 
312d1a8a48SPaul Gortmaker #include <linux/export.h>
32673a394bSEric Anholt #include "drmP.h"
33673a394bSEric Anholt 
34673a394bSEric Anholt #if defined(CONFIG_X86)
35673a394bSEric Anholt static void
36673a394bSEric Anholt drm_clflush_page(struct page *page)
37673a394bSEric Anholt {
38673a394bSEric Anholt 	uint8_t *page_virtual;
39673a394bSEric Anholt 	unsigned int i;
40673a394bSEric Anholt 
41673a394bSEric Anholt 	if (unlikely(page == NULL))
42673a394bSEric Anholt 		return;
43673a394bSEric Anholt 
44673a394bSEric Anholt 	page_virtual = kmap_atomic(page, KM_USER0);
45673a394bSEric Anholt 	for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
46673a394bSEric Anholt 		clflush(page_virtual + i);
47673a394bSEric Anholt 	kunmap_atomic(page_virtual, KM_USER0);
48673a394bSEric Anholt }
49673a394bSEric Anholt 
50c9c97b8cSDave Airlie static void drm_cache_flush_clflush(struct page *pages[],
51c9c97b8cSDave Airlie 				    unsigned long num_pages)
52c9c97b8cSDave Airlie {
53c9c97b8cSDave Airlie 	unsigned long i;
54c9c97b8cSDave Airlie 
55c9c97b8cSDave Airlie 	mb();
56c9c97b8cSDave Airlie 	for (i = 0; i < num_pages; i++)
57c9c97b8cSDave Airlie 		drm_clflush_page(*pages++);
58c9c97b8cSDave Airlie 	mb();
59c9c97b8cSDave Airlie }
60c9c97b8cSDave Airlie 
61c9c97b8cSDave Airlie static void
62c9c97b8cSDave Airlie drm_clflush_ipi_handler(void *null)
63c9c97b8cSDave Airlie {
64c9c97b8cSDave Airlie 	wbinvd();
65c9c97b8cSDave Airlie }
66c9c97b8cSDave Airlie #endif
67ed017d9fSDave Airlie 
68673a394bSEric Anholt void
69673a394bSEric Anholt drm_clflush_pages(struct page *pages[], unsigned long num_pages)
70673a394bSEric Anholt {
71673a394bSEric Anholt 
72673a394bSEric Anholt #if defined(CONFIG_X86)
73673a394bSEric Anholt 	if (cpu_has_clflush) {
74c9c97b8cSDave Airlie 		drm_cache_flush_clflush(pages, num_pages);
75673a394bSEric Anholt 		return;
76673a394bSEric Anholt 	}
77673a394bSEric Anholt 
78c9c97b8cSDave Airlie 	if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
79c9c97b8cSDave Airlie 		printk(KERN_ERR "Timed out waiting for cache flush.\n");
80c9c97b8cSDave Airlie 
81c9c97b8cSDave Airlie #elif defined(__powerpc__)
82c9c97b8cSDave Airlie 	unsigned long i;
83c9c97b8cSDave Airlie 	for (i = 0; i < num_pages; i++) {
84c9c97b8cSDave Airlie 		struct page *page = pages[i];
85c9c97b8cSDave Airlie 		void *page_virtual;
86c9c97b8cSDave Airlie 
87c9c97b8cSDave Airlie 		if (unlikely(page == NULL))
88c9c97b8cSDave Airlie 			continue;
89c9c97b8cSDave Airlie 
90c9c97b8cSDave Airlie 		page_virtual = kmap_atomic(page, KM_USER0);
91c9c97b8cSDave Airlie 		flush_dcache_range((unsigned long)page_virtual,
92c9c97b8cSDave Airlie 				   (unsigned long)page_virtual + PAGE_SIZE);
93c9c97b8cSDave Airlie 		kunmap_atomic(page_virtual, KM_USER0);
94c9c97b8cSDave Airlie 	}
95c9c97b8cSDave Airlie #else
96ed017d9fSDave Airlie 	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
97ed017d9fSDave Airlie 	WARN_ON_ONCE(1);
98e0f0754fSDave Airlie #endif
99673a394bSEric Anholt }
100673a394bSEric Anholt EXPORT_SYMBOL(drm_clflush_pages);
101