Home | History | Annotate | Line # | Download | only in drm
drm_cache.c revision 1.1.1.2
      1      1.1  riastrad /**************************************************************************
      2      1.1  riastrad  *
      3      1.1  riastrad  * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
      4      1.1  riastrad  * All Rights Reserved.
      5      1.1  riastrad  *
      6      1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7      1.1  riastrad  * copy of this software and associated documentation files (the
      8      1.1  riastrad  * "Software"), to deal in the Software without restriction, including
      9      1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     10      1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     11      1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     12      1.1  riastrad  * the following conditions:
     13      1.1  riastrad  *
     14      1.1  riastrad  * The above copyright notice and this permission notice (including the
     15      1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     16      1.1  riastrad  * of the Software.
     17      1.1  riastrad  *
     18      1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19      1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20      1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     21      1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     22      1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     23      1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     24      1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     25      1.1  riastrad  *
     26      1.1  riastrad  **************************************************************************/
     27      1.1  riastrad /*
     28      1.1  riastrad  * Authors: Thomas Hellstrm <thomas-at-tungstengraphics-dot-com>
     29      1.1  riastrad  */
     30      1.1  riastrad 
     31      1.1  riastrad #include <linux/export.h>
     32      1.1  riastrad #include <drm/drmP.h>
     33      1.1  riastrad 
     34      1.1  riastrad #if defined(CONFIG_X86)
     35  1.1.1.2  riastrad 
     36  1.1.1.2  riastrad /*
     37  1.1.1.2  riastrad  * clflushopt is an unordered instruction which needs fencing with mfence or
     38  1.1.1.2  riastrad  * sfence to avoid ordering issues.  For drm_clflush_page this fencing happens
     39  1.1.1.2  riastrad  * in the caller.
     40  1.1.1.2  riastrad  */
     41      1.1  riastrad static void
     42      1.1  riastrad drm_clflush_page(struct page *page)
     43      1.1  riastrad {
     44      1.1  riastrad 	uint8_t *page_virtual;
     45      1.1  riastrad 	unsigned int i;
     46      1.1  riastrad 	const int size = boot_cpu_data.x86_clflush_size;
     47      1.1  riastrad 
     48      1.1  riastrad 	if (unlikely(page == NULL))
     49      1.1  riastrad 		return;
     50      1.1  riastrad 
     51      1.1  riastrad 	page_virtual = kmap_atomic(page);
     52      1.1  riastrad 	for (i = 0; i < PAGE_SIZE; i += size)
     53  1.1.1.2  riastrad 		clflushopt(page_virtual + i);
     54      1.1  riastrad 	kunmap_atomic(page_virtual);
     55      1.1  riastrad }
     56      1.1  riastrad 
     57      1.1  riastrad static void drm_cache_flush_clflush(struct page *pages[],
     58      1.1  riastrad 				    unsigned long num_pages)
     59      1.1  riastrad {
     60      1.1  riastrad 	unsigned long i;
     61      1.1  riastrad 
     62      1.1  riastrad 	mb();
     63      1.1  riastrad 	for (i = 0; i < num_pages; i++)
     64      1.1  riastrad 		drm_clflush_page(*pages++);
     65      1.1  riastrad 	mb();
     66      1.1  riastrad }
     67      1.1  riastrad 
     68      1.1  riastrad static void
     69      1.1  riastrad drm_clflush_ipi_handler(void *null)
     70      1.1  riastrad {
     71      1.1  riastrad 	wbinvd();
     72      1.1  riastrad }
     73      1.1  riastrad #endif
     74      1.1  riastrad 
     75      1.1  riastrad void
     76      1.1  riastrad drm_clflush_pages(struct page *pages[], unsigned long num_pages)
     77      1.1  riastrad {
     78      1.1  riastrad 
     79      1.1  riastrad #if defined(CONFIG_X86)
     80      1.1  riastrad 	if (cpu_has_clflush) {
     81      1.1  riastrad 		drm_cache_flush_clflush(pages, num_pages);
     82      1.1  riastrad 		return;
     83      1.1  riastrad 	}
     84      1.1  riastrad 
     85      1.1  riastrad 	if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
     86      1.1  riastrad 		printk(KERN_ERR "Timed out waiting for cache flush.\n");
     87      1.1  riastrad 
     88      1.1  riastrad #elif defined(__powerpc__)
     89      1.1  riastrad 	unsigned long i;
     90      1.1  riastrad 	for (i = 0; i < num_pages; i++) {
     91      1.1  riastrad 		struct page *page = pages[i];
     92      1.1  riastrad 		void *page_virtual;
     93      1.1  riastrad 
     94      1.1  riastrad 		if (unlikely(page == NULL))
     95      1.1  riastrad 			continue;
     96      1.1  riastrad 
     97      1.1  riastrad 		page_virtual = kmap_atomic(page);
     98      1.1  riastrad 		flush_dcache_range((unsigned long)page_virtual,
     99      1.1  riastrad 				   (unsigned long)page_virtual + PAGE_SIZE);
    100      1.1  riastrad 		kunmap_atomic(page_virtual);
    101      1.1  riastrad 	}
    102      1.1  riastrad #else
    103      1.1  riastrad 	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
    104      1.1  riastrad 	WARN_ON_ONCE(1);
    105      1.1  riastrad #endif
    106      1.1  riastrad }
    107      1.1  riastrad EXPORT_SYMBOL(drm_clflush_pages);
    108      1.1  riastrad 
    109      1.1  riastrad void
    110      1.1  riastrad drm_clflush_sg(struct sg_table *st)
    111      1.1  riastrad {
    112      1.1  riastrad #if defined(CONFIG_X86)
    113      1.1  riastrad 	if (cpu_has_clflush) {
    114  1.1.1.2  riastrad 		struct sg_page_iter sg_iter;
    115      1.1  riastrad 
    116      1.1  riastrad 		mb();
    117  1.1.1.2  riastrad 		for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
    118  1.1.1.2  riastrad 			drm_clflush_page(sg_page_iter_page(&sg_iter));
    119      1.1  riastrad 		mb();
    120      1.1  riastrad 
    121      1.1  riastrad 		return;
    122      1.1  riastrad 	}
    123      1.1  riastrad 
    124      1.1  riastrad 	if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
    125      1.1  riastrad 		printk(KERN_ERR "Timed out waiting for cache flush.\n");
    126      1.1  riastrad #else
    127      1.1  riastrad 	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
    128      1.1  riastrad 	WARN_ON_ONCE(1);
    129      1.1  riastrad #endif
    130      1.1  riastrad }
    131      1.1  riastrad EXPORT_SYMBOL(drm_clflush_sg);
    132      1.1  riastrad 
    133      1.1  riastrad void
    134      1.1  riastrad drm_clflush_virt_range(char *addr, unsigned long length)
    135      1.1  riastrad {
    136      1.1  riastrad #if defined(CONFIG_X86)
    137      1.1  riastrad 	if (cpu_has_clflush) {
    138      1.1  riastrad 		char *end = addr + length;
    139      1.1  riastrad 		mb();
    140      1.1  riastrad 		for (; addr < end; addr += boot_cpu_data.x86_clflush_size)
    141      1.1  riastrad 			clflush(addr);
    142  1.1.1.2  riastrad 		clflushopt(end - 1);
    143      1.1  riastrad 		mb();
    144      1.1  riastrad 		return;
    145      1.1  riastrad 	}
    146      1.1  riastrad 
    147      1.1  riastrad 	if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
    148      1.1  riastrad 		printk(KERN_ERR "Timed out waiting for cache flush.\n");
    149      1.1  riastrad #else
    150      1.1  riastrad 	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
    151      1.1  riastrad 	WARN_ON_ONCE(1);
    152      1.1  riastrad #endif
    153      1.1  riastrad }
    154      1.1  riastrad EXPORT_SYMBOL(drm_clflush_virt_range);
    155