Home | History | Annotate | Line # | Download | only in drm
drm_cache.c revision 1.8.16.2
      1  1.8.16.2  pgoyette /*	$NetBSD: drm_cache.c,v 1.8.16.2 2019/01/26 22:00:24 pgoyette Exp $	*/
      2       1.2  riastrad 
      3       1.2  riastrad /*-
      4       1.2  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5       1.2  riastrad  * All rights reserved.
      6       1.2  riastrad  *
      7       1.2  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8       1.2  riastrad  * by Taylor R. Campbell.
      9       1.2  riastrad  *
     10       1.2  riastrad  * Redistribution and use in source and binary forms, with or without
     11       1.2  riastrad  * modification, are permitted provided that the following conditions
     12       1.2  riastrad  * are met:
     13       1.2  riastrad  * 1. Redistributions of source code must retain the above copyright
     14       1.2  riastrad  *    notice, this list of conditions and the following disclaimer.
     15       1.2  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17       1.2  riastrad  *    documentation and/or other materials provided with the distribution.
     18       1.2  riastrad  *
     19       1.2  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.2  riastrad  */
     31       1.2  riastrad 
     32       1.2  riastrad #include <sys/cdefs.h>
     33  1.8.16.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: drm_cache.c,v 1.8.16.2 2019/01/26 22:00:24 pgoyette Exp $");
     34       1.2  riastrad 
     35       1.8  jmcneill #include <sys/param.h>
     36       1.2  riastrad #include <sys/types.h>
     37       1.2  riastrad #include <sys/xcall.h>
     38       1.2  riastrad 
     39       1.2  riastrad #include <uvm/uvm_extern.h>
     40       1.2  riastrad 
     41       1.2  riastrad #include <linux/mm_types.h>
     42       1.2  riastrad 
     43       1.2  riastrad #include <drm/drmP.h>
     44       1.2  riastrad 
     45  1.8.16.2  pgoyette #if !defined(__arm__) && !defined(__aarch64__)
     46       1.8  jmcneill #define DRM_CLFLUSH	1
     47       1.8  jmcneill #endif
     48       1.8  jmcneill 
     49       1.8  jmcneill #if defined(DRM_CLFLUSH)
     50       1.2  riastrad static bool		drm_md_clflush_finegrained_p(void);
     51       1.2  riastrad static void		drm_md_clflush_all(void);
     52  1.8.16.1  pgoyette static void		drm_md_clflush_begin(void);
     53  1.8.16.1  pgoyette static void		drm_md_clflush_commit(void);
     54       1.2  riastrad static void		drm_md_clflush_page(struct page *);
     55       1.2  riastrad static void		drm_md_clflush_virt_range(const void *, size_t);
     56       1.8  jmcneill #endif
     57       1.2  riastrad 
     58       1.2  riastrad void
     59       1.3  riastrad drm_clflush_pages(struct page **pages, unsigned long npages)
     60       1.3  riastrad {
     61       1.8  jmcneill #if defined(DRM_CLFLUSH)
     62       1.3  riastrad 	if (drm_md_clflush_finegrained_p()) {
     63  1.8.16.1  pgoyette 		drm_md_clflush_begin();
     64       1.3  riastrad 		while (npages--)
     65       1.3  riastrad 			drm_md_clflush_page(pages[npages]);
     66  1.8.16.1  pgoyette 		drm_md_clflush_commit();
     67       1.3  riastrad 	} else {
     68       1.3  riastrad 		drm_md_clflush_all();
     69       1.3  riastrad 	}
     70       1.8  jmcneill #endif
     71       1.3  riastrad }
     72       1.3  riastrad 
     73       1.3  riastrad void
     74       1.2  riastrad drm_clflush_pglist(struct pglist *list)
     75       1.2  riastrad {
     76       1.8  jmcneill #if defined(DRM_CLFLUSH)
     77       1.2  riastrad 	if (drm_md_clflush_finegrained_p()) {
     78       1.2  riastrad 		struct vm_page *page;
     79       1.2  riastrad 
     80  1.8.16.1  pgoyette 		drm_md_clflush_begin();
     81       1.2  riastrad 		TAILQ_FOREACH(page, list, pageq.queue)
     82       1.2  riastrad 			drm_md_clflush_page(container_of(page, struct page,
     83       1.2  riastrad 				p_vmp));
     84  1.8.16.1  pgoyette 		drm_md_clflush_commit();
     85       1.2  riastrad 	} else {
     86       1.2  riastrad 		drm_md_clflush_all();
     87       1.2  riastrad 	}
     88       1.8  jmcneill #endif
     89       1.2  riastrad }
     90       1.2  riastrad 
     91       1.2  riastrad void
     92       1.2  riastrad drm_clflush_page(struct page *page)
     93       1.2  riastrad {
     94       1.8  jmcneill #if defined(DRM_CLFLUSH)
     95  1.8.16.1  pgoyette 	if (drm_md_clflush_finegrained_p()) {
     96  1.8.16.1  pgoyette 		drm_md_clflush_begin();
     97       1.2  riastrad 		drm_md_clflush_page(page);
     98  1.8.16.1  pgoyette 		drm_md_clflush_commit();
     99  1.8.16.1  pgoyette 	} else {
    100       1.2  riastrad 		drm_md_clflush_all();
    101  1.8.16.1  pgoyette 	}
    102       1.8  jmcneill #endif
    103       1.2  riastrad }
    104       1.2  riastrad 
    105       1.2  riastrad void
    106       1.2  riastrad drm_clflush_virt_range(const void *vaddr, size_t nbytes)
    107       1.2  riastrad {
    108       1.8  jmcneill #if defined(DRM_CLFLUSH)
    109  1.8.16.1  pgoyette 	if (drm_md_clflush_finegrained_p()) {
    110  1.8.16.1  pgoyette 		drm_md_clflush_begin();
    111       1.2  riastrad 		drm_md_clflush_virt_range(vaddr, nbytes);
    112  1.8.16.1  pgoyette 		drm_md_clflush_commit();
    113  1.8.16.1  pgoyette 	} else {
    114       1.2  riastrad 		drm_md_clflush_all();
    115  1.8.16.1  pgoyette 	}
    116       1.8  jmcneill #endif
    117       1.2  riastrad }
    118       1.2  riastrad 
    119       1.2  riastrad #if defined(__i386__) || defined(__x86_64__)
    120       1.2  riastrad 
    121       1.7  jmcneill #include <machine/cpufunc.h>
    122       1.7  jmcneill 
    123       1.2  riastrad static bool
    124       1.2  riastrad drm_md_clflush_finegrained_p(void)
    125       1.2  riastrad {
    126       1.2  riastrad 	return ISSET(cpu_info_primary.ci_feat_val[0], CPUID_CFLUSH);
    127       1.2  riastrad }
    128       1.2  riastrad 
    129       1.2  riastrad static void
    130  1.8.16.1  pgoyette drm_x86_clflush_xc(void *arg0 __unused, void *arg1 __unused)
    131       1.2  riastrad {
    132  1.8.16.1  pgoyette 	wbinvd();
    133       1.2  riastrad }
    134       1.2  riastrad 
    135  1.8.16.1  pgoyette static void
    136  1.8.16.1  pgoyette drm_md_clflush_all(void)
    137       1.2  riastrad {
    138  1.8.16.1  pgoyette 	xc_wait(xc_broadcast(0, &drm_x86_clflush_xc, NULL, NULL));
    139       1.2  riastrad }
    140       1.2  riastrad 
    141       1.2  riastrad static void
    142  1.8.16.1  pgoyette drm_md_clflush_begin(void)
    143       1.2  riastrad {
    144  1.8.16.1  pgoyette 	/* Support for CLFLUSH implies support for MFENCE.  */
    145  1.8.16.1  pgoyette 	x86_mfence();
    146  1.8.16.1  pgoyette }
    147  1.8.16.1  pgoyette 
    148  1.8.16.1  pgoyette static void
    149  1.8.16.1  pgoyette drm_md_clflush_commit(void)
    150  1.8.16.1  pgoyette {
    151  1.8.16.1  pgoyette 	x86_mfence();
    152  1.8.16.1  pgoyette }
    153  1.8.16.1  pgoyette 
    154  1.8.16.1  pgoyette static void
    155  1.8.16.1  pgoyette drm_md_clflush_page(struct page *page)
    156  1.8.16.1  pgoyette {
    157  1.8.16.1  pgoyette 	void *const vaddr = kmap_atomic(page);
    158  1.8.16.1  pgoyette 
    159  1.8.16.1  pgoyette 	drm_md_clflush_virt_range(vaddr, PAGE_SIZE);
    160  1.8.16.1  pgoyette 
    161  1.8.16.1  pgoyette 	kunmap_atomic(vaddr);
    162  1.8.16.1  pgoyette }
    163  1.8.16.1  pgoyette 
    164  1.8.16.1  pgoyette static void
    165  1.8.16.1  pgoyette drm_md_clflush_virt_range(const void *ptr, size_t nbytes)
    166  1.8.16.1  pgoyette {
    167  1.8.16.1  pgoyette 	const unsigned clflush_size = cpu_info_primary.ci_cflush_lsize;
    168  1.8.16.1  pgoyette 	const vaddr_t vaddr = (vaddr_t)ptr;
    169  1.8.16.1  pgoyette 	const vaddr_t start = rounddown(vaddr, clflush_size);
    170  1.8.16.1  pgoyette 	const vaddr_t end = roundup(vaddr + nbytes, clflush_size);
    171  1.8.16.1  pgoyette 	vaddr_t va;
    172  1.8.16.1  pgoyette 
    173  1.8.16.1  pgoyette 	for (va = start; va < end; va += clflush_size)
    174  1.8.16.1  pgoyette 		asm volatile ("clflush %0" : : "m" (*(const char *)va));
    175  1.8.16.1  pgoyette }
    176  1.8.16.1  pgoyette 
    177  1.8.16.1  pgoyette #elif defined(__sparc__) || defined(__sparc64__)
    178  1.8.16.1  pgoyette 
    179  1.8.16.1  pgoyette #ifdef __sparc64__
    180  1.8.16.1  pgoyette #include <sparc64/sparc64/cache.h>
    181  1.8.16.1  pgoyette #else
    182  1.8.16.1  pgoyette #include <sparc/sparc/cache.h>
    183  1.8.16.1  pgoyette #endif
    184  1.8.16.1  pgoyette 
    185  1.8.16.1  pgoyette static bool
    186  1.8.16.1  pgoyette drm_md_clflush_finegrained_p(void)
    187  1.8.16.1  pgoyette {
    188  1.8.16.1  pgoyette 	return true;
    189       1.2  riastrad }
    190       1.2  riastrad 
    191       1.2  riastrad static void
    192       1.2  riastrad drm_md_clflush_all(void)
    193       1.2  riastrad {
    194  1.8.16.1  pgoyette 	panic("don't know how to flush entire cache on sparc64");
    195  1.8.16.1  pgoyette }
    196  1.8.16.1  pgoyette 
    197  1.8.16.1  pgoyette static void
    198  1.8.16.1  pgoyette drm_md_clflush_begin(void)
    199  1.8.16.1  pgoyette {
    200  1.8.16.1  pgoyette 	membar_Sync();		/* unsure if needed */
    201  1.8.16.1  pgoyette }
    202  1.8.16.1  pgoyette 
    203  1.8.16.1  pgoyette static void
    204  1.8.16.1  pgoyette drm_md_clflush_commit(void)
    205  1.8.16.1  pgoyette {
    206  1.8.16.1  pgoyette 	membar_Sync();		/* unsure if needed */
    207  1.8.16.1  pgoyette }
    208  1.8.16.1  pgoyette 
    209  1.8.16.1  pgoyette static void
    210  1.8.16.1  pgoyette drm_md_clflush_page(struct page *page)
    211  1.8.16.1  pgoyette {
    212  1.8.16.1  pgoyette #ifdef __sparc64__
    213  1.8.16.1  pgoyette 	paddr_t pa = VM_PAGE_TO_PHYS(&page->p_vmp);
    214  1.8.16.1  pgoyette 
    215  1.8.16.1  pgoyette 	cache_flush_phys(pa, PAGE_SIZE, 0);
    216  1.8.16.1  pgoyette #else
    217  1.8.16.1  pgoyette 	void *const vaddr = kmap_atomic(page);
    218  1.8.16.1  pgoyette 
    219  1.8.16.1  pgoyette 	cache_flush(vaddr, PAGE_SIZE);
    220  1.8.16.1  pgoyette 
    221  1.8.16.1  pgoyette 	kunmap_atomic(vaddr);
    222  1.8.16.1  pgoyette #endif
    223  1.8.16.1  pgoyette }
    224  1.8.16.1  pgoyette 
    225  1.8.16.1  pgoyette static void
    226  1.8.16.1  pgoyette drm_md_clflush_virt_range(const void *ptr, size_t nbytes)
    227  1.8.16.1  pgoyette {
    228  1.8.16.1  pgoyette #ifdef __sparc64__
    229  1.8.16.1  pgoyette 	/* XXX Mega-kludge -- doesn't seem to be a way to flush by vaddr.  */
    230  1.8.16.1  pgoyette 	blast_dcache();
    231  1.8.16.1  pgoyette #else
    232  1.8.16.1  pgoyette 	cache_flush(ptr, nbytes);
    233  1.8.16.1  pgoyette #endif
    234  1.8.16.1  pgoyette }
    235  1.8.16.1  pgoyette 
    236  1.8.16.1  pgoyette #elif defined(__powerpc__)
    237  1.8.16.1  pgoyette 
    238  1.8.16.1  pgoyette static bool
    239  1.8.16.1  pgoyette drm_md_clflush_finegrained_p(void)
    240  1.8.16.1  pgoyette {
    241  1.8.16.1  pgoyette 	return true;
    242  1.8.16.1  pgoyette }
    243  1.8.16.1  pgoyette 
    244  1.8.16.1  pgoyette static void
    245  1.8.16.1  pgoyette drm_md_clflush_all(void)
    246  1.8.16.1  pgoyette {
    247  1.8.16.1  pgoyette 	panic("don't know how to flush entire cache on powerpc");
    248  1.8.16.1  pgoyette }
    249  1.8.16.1  pgoyette 
    250  1.8.16.1  pgoyette static void
    251  1.8.16.1  pgoyette drm_md_clflush_begin(void)
    252  1.8.16.1  pgoyette {
    253  1.8.16.1  pgoyette }
    254  1.8.16.1  pgoyette 
    255  1.8.16.1  pgoyette static void
    256  1.8.16.1  pgoyette drm_md_clflush_commit(void)
    257  1.8.16.1  pgoyette {
    258  1.8.16.1  pgoyette 	asm volatile ("sync" ::: "memory");
    259       1.2  riastrad }
    260       1.2  riastrad 
    261       1.2  riastrad static void
    262       1.2  riastrad drm_md_clflush_page(struct page *page)
    263       1.2  riastrad {
    264       1.2  riastrad 	void *const vaddr = kmap_atomic(page);
    265       1.2  riastrad 
    266       1.2  riastrad 	drm_md_clflush_virt_range(vaddr, PAGE_SIZE);
    267       1.2  riastrad 
    268       1.2  riastrad 	kunmap_atomic(vaddr);
    269       1.2  riastrad }
    270       1.2  riastrad 
    271       1.2  riastrad static void
    272  1.8.16.1  pgoyette drm_md_clflush_virt_range(const void *ptr, size_t nbytes)
    273       1.2  riastrad {
    274  1.8.16.1  pgoyette 	const unsigned dcsize = curcpu()->ci_ci.dcache_line_size;
    275  1.8.16.1  pgoyette 	vaddr_t va = (vaddr_t)ptr;
    276  1.8.16.1  pgoyette 	vaddr_t start = rounddown(va, dcsize);
    277  1.8.16.1  pgoyette 	vaddr_t end = roundup(va + nbytes, dcsize);
    278  1.8.16.1  pgoyette 	vsize_t len = end - start;
    279  1.8.16.1  pgoyette 	vsize_t off;
    280       1.2  riastrad 
    281  1.8.16.1  pgoyette 	for (off = 0; off < len; off += dcsize)
    282  1.8.16.1  pgoyette 		asm volatile ("dcbf\t%0,%1" : : "b"(start), "r"(off));
    283       1.2  riastrad }
    284       1.2  riastrad 
    285  1.8.16.1  pgoyette #endif
    286