Home | History | Annotate | Line # | Download | only in drm
drm_cache.c revision 1.2
      1  1.2  riastrad /*	$NetBSD: drm_cache.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /**************************************************************************
      4  1.1  riastrad  *
      5  1.1  riastrad  * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
      6  1.1  riastrad  * All Rights Reserved.
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the
     10  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
     11  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     12  1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     13  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     14  1.1  riastrad  * the following conditions:
     15  1.1  riastrad  *
     16  1.1  riastrad  * The above copyright notice and this permission notice (including the
     17  1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     18  1.1  riastrad  * of the Software.
     19  1.1  riastrad  *
     20  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     23  1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     24  1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     25  1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     26  1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     27  1.1  riastrad  *
     28  1.1  riastrad  **************************************************************************/
     29  1.1  riastrad /*
     30  1.1  riastrad  * Authors: Thomas Hellstrm <thomas-at-tungstengraphics-dot-com>
     31  1.1  riastrad  */
     32  1.1  riastrad 
     33  1.2  riastrad #include <sys/cdefs.h>
     34  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_cache.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
     35  1.2  riastrad 
     36  1.1  riastrad #include <linux/export.h>
     37  1.1  riastrad #include <drm/drmP.h>
     38  1.1  riastrad 
     39  1.1  riastrad #if defined(CONFIG_X86)
     40  1.2  riastrad #include <asm/smp.h>
     41  1.2  riastrad 
     42  1.2  riastrad /*
     43  1.2  riastrad  * clflushopt is an unordered instruction which needs fencing with mfence or
     44  1.2  riastrad  * sfence to avoid ordering issues.  For drm_clflush_page this fencing happens
     45  1.2  riastrad  * in the caller.
     46  1.2  riastrad  */
     47  1.1  riastrad static void
     48  1.1  riastrad drm_clflush_page(struct page *page)
     49  1.1  riastrad {
     50  1.1  riastrad 	uint8_t *page_virtual;
     51  1.1  riastrad 	unsigned int i;
     52  1.1  riastrad 	const int size = boot_cpu_data.x86_clflush_size;
     53  1.1  riastrad 
     54  1.1  riastrad 	if (unlikely(page == NULL))
     55  1.1  riastrad 		return;
     56  1.1  riastrad 
     57  1.1  riastrad 	page_virtual = kmap_atomic(page);
     58  1.1  riastrad 	for (i = 0; i < PAGE_SIZE; i += size)
     59  1.2  riastrad 		clflushopt(page_virtual + i);
     60  1.1  riastrad 	kunmap_atomic(page_virtual);
     61  1.1  riastrad }
     62  1.1  riastrad 
     63  1.1  riastrad static void drm_cache_flush_clflush(struct page *pages[],
     64  1.1  riastrad 				    unsigned long num_pages)
     65  1.1  riastrad {
     66  1.1  riastrad 	unsigned long i;
     67  1.1  riastrad 
     68  1.1  riastrad 	mb();
     69  1.1  riastrad 	for (i = 0; i < num_pages; i++)
     70  1.1  riastrad 		drm_clflush_page(*pages++);
     71  1.1  riastrad 	mb();
     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.2  riastrad 	if (wbinvd_on_all_cpus())
     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.2  riastrad 		struct sg_page_iter sg_iter;
    115  1.1  riastrad 
    116  1.1  riastrad 		mb();
    117  1.2  riastrad 		for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
    118  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.2  riastrad 	if (wbinvd_on_all_cpus())
    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.2  riastrad drm_clflush_virt_range(void *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.2  riastrad 		const int size = boot_cpu_data.x86_clflush_size;
    139  1.2  riastrad 		void *end = addr + length;
    140  1.2  riastrad 		addr = (void *)(((unsigned long)addr) & -size);
    141  1.1  riastrad 		mb();
    142  1.2  riastrad 		for (; addr < end; addr += size)
    143  1.2  riastrad 			clflushopt(addr);
    144  1.2  riastrad 		clflushopt(end - 1); /* force serialisation */
    145  1.1  riastrad 		mb();
    146  1.1  riastrad 		return;
    147  1.1  riastrad 	}
    148  1.1  riastrad 
    149  1.2  riastrad 	if (wbinvd_on_all_cpus())
    150  1.1  riastrad 		printk(KERN_ERR "Timed out waiting for cache flush.\n");
    151  1.1  riastrad #else
    152  1.1  riastrad 	printk(KERN_ERR "Architecture has no drm_cache.c support\n");
    153  1.1  riastrad 	WARN_ON_ONCE(1);
    154  1.1  riastrad #endif
    155  1.1  riastrad }
    156  1.1  riastrad EXPORT_SYMBOL(drm_clflush_virt_range);
    157