Home | History | Annotate | Line # | Download | only in booke
booke_cache.c revision 1.2.6.1
      1      1.2    matt /*-
      2      1.2    matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      3      1.2    matt  * All rights reserved.
      4      1.2    matt  *
      5      1.2    matt  * This code is derived from software contributed to The NetBSD Foundation
      6      1.2    matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      7      1.2    matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      8      1.2    matt  *
      9      1.2    matt  * This material is based upon work supported by the Defense Advanced Research
     10      1.2    matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     11      1.2    matt  * Contract No. N66001-09-C-2073.
     12      1.2    matt  * Approved for Public Release, Distribution Unlimited
     13      1.2    matt  *
     14      1.2    matt  * Redistribution and use in source and binary forms, with or without
     15      1.2    matt  * modification, are permitted provided that the following conditions
     16      1.2    matt  * are met:
     17      1.2    matt  * 1. Redistributions of source code must retain the above copyright
     18      1.2    matt  *    notice, this list of conditions and the following disclaimer.
     19      1.2    matt  * 2. Redistributions in binary form must reproduce the above copyright
     20      1.2    matt  *    notice, this list of conditions and the following disclaimer in the
     21      1.2    matt  *    documentation and/or other materials provided with the distribution.
     22      1.2    matt  *
     23      1.2    matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24      1.2    matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25      1.2    matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26      1.2    matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27      1.2    matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28      1.2    matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29      1.2    matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30      1.2    matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31      1.2    matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32      1.2    matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33      1.2    matt  * POSSIBILITY OF SUCH DAMAGE.
     34      1.2    matt  */
     35      1.2    matt /*
     36      1.2    matt  *
     37      1.2    matt  */
     38      1.2    matt #include <sys/cdefs.h>
     39      1.2    matt 
     40  1.2.6.1  cherry __KERNEL_RCSID(0, "$NetBSD: booke_cache.c,v 1.2.6.1 2011/06/23 14:19:27 cherry Exp $");
     41      1.2    matt 
     42      1.2    matt #include <sys/param.h>
     43      1.2    matt #include <sys/cpu.h>
     44  1.2.6.1  cherry #include <sys/atomic.h>
     45      1.2    matt 
     46  1.2.6.1  cherry enum cache_op { OP_DCBF, OP_DCBST, OP_DCBI, OP_DCBZ, OP_DCBA, OP_ICBI };
     47      1.2    matt 
     48      1.2    matt static void inline
     49      1.2    matt dcbf(vaddr_t va, vsize_t off)
     50      1.2    matt {
     51      1.2    matt 	__asm volatile("dcbf\t%0,%1" : : "b" (va), "r" (off));
     52      1.2    matt }
     53      1.2    matt 
     54      1.2    matt static void inline
     55      1.2    matt dcbst(vaddr_t va, vsize_t off)
     56      1.2    matt {
     57      1.2    matt 	__asm volatile("dcbst\t%0,%1" : : "b" (va), "r" (off));
     58      1.2    matt }
     59      1.2    matt 
     60      1.2    matt static void inline
     61      1.2    matt dcbi(vaddr_t va, vsize_t off)
     62      1.2    matt {
     63      1.2    matt 	__asm volatile("dcbi\t%0,%1" : : "b" (va), "r" (off));
     64      1.2    matt }
     65      1.2    matt 
     66      1.2    matt static void inline
     67      1.2    matt dcbz(vaddr_t va, vsize_t off)
     68      1.2    matt {
     69      1.2    matt 	__asm volatile("dcbz\t%0,%1" : : "b" (va), "r" (off));
     70      1.2    matt }
     71      1.2    matt 
     72      1.2    matt static void inline
     73      1.2    matt dcba(vaddr_t va, vsize_t off)
     74      1.2    matt {
     75      1.2    matt 	__asm volatile("dcba\t%0,%1" : : "b" (va), "r" (off));
     76      1.2    matt }
     77      1.2    matt 
     78      1.2    matt static void inline
     79      1.2    matt icbi(vaddr_t va, vsize_t off)
     80      1.2    matt {
     81      1.2    matt 	__asm volatile("icbi\t%0,%1" : : "b" (va), "r" (off));
     82      1.2    matt }
     83      1.2    matt 
     84      1.2    matt static inline void
     85  1.2.6.1  cherry cache_op(vaddr_t va, vsize_t len, vsize_t line_size, enum cache_op op)
     86      1.2    matt {
     87      1.2    matt 	KASSERT(line_size > 0);
     88      1.2    matt 
     89      1.2    matt 	if (len == 0)
     90      1.2    matt 		return;
     91      1.2    matt 
     92      1.2    matt 	/* Make sure we flush all cache lines */
     93      1.2    matt 	len += va & (line_size - 1);
     94      1.2    matt 	va &= -line_size;
     95      1.2    matt 
     96  1.2.6.1  cherry 	for (vsize_t i = 0; i < len; i += line_size) {
     97  1.2.6.1  cherry 		switch (op) {
     98  1.2.6.1  cherry 		case OP_DCBF: dcbf(va, i); break;
     99  1.2.6.1  cherry 		case OP_DCBST: dcbst(va, i); break;
    100  1.2.6.1  cherry 		case OP_DCBI: dcbi(va, i); break;
    101  1.2.6.1  cherry 		case OP_DCBZ: dcbz(va, i); break;
    102  1.2.6.1  cherry 		case OP_DCBA: dcba(va, i); break;
    103  1.2.6.1  cherry 		case OP_ICBI: icbi(va, i); break;
    104  1.2.6.1  cherry 		}
    105  1.2.6.1  cherry 	}
    106  1.2.6.1  cherry 	if (op != OP_ICBI)
    107  1.2.6.1  cherry 		membar_producer();
    108      1.2    matt }
    109      1.2    matt 
    110      1.2    matt void
    111      1.2    matt dcache_wb_page(vaddr_t va)
    112      1.2    matt {
    113  1.2.6.1  cherry 	cache_op(va, PAGE_SIZE, curcpu()->ci_ci.dcache_line_size, OP_DCBST);
    114      1.2    matt }
    115      1.2    matt 
    116      1.2    matt void
    117      1.2    matt dcache_wbinv_page(vaddr_t va)
    118      1.2    matt {
    119  1.2.6.1  cherry 	cache_op(va, PAGE_SIZE, curcpu()->ci_ci.dcache_line_size, OP_DCBF);
    120      1.2    matt }
    121      1.2    matt 
    122      1.2    matt void
    123      1.2    matt dcache_inv_page(vaddr_t va)
    124      1.2    matt {
    125  1.2.6.1  cherry 	cache_op(va, PAGE_SIZE, curcpu()->ci_ci.dcache_line_size, OP_DCBI);
    126      1.2    matt }
    127      1.2    matt 
    128      1.2    matt void
    129      1.2    matt dcache_zero_page(vaddr_t va)
    130      1.2    matt {
    131  1.2.6.1  cherry 	cache_op(va, PAGE_SIZE, curcpu()->ci_ci.dcache_line_size, OP_DCBZ);
    132      1.2    matt }
    133      1.2    matt 
    134      1.2    matt void
    135      1.2    matt icache_inv_page(vaddr_t va)
    136      1.2    matt {
    137  1.2.6.1  cherry 	membar_sync();
    138  1.2.6.1  cherry 	cache_op(va, PAGE_SIZE, curcpu()->ci_ci.icache_line_size, OP_ICBI);
    139  1.2.6.1  cherry 	membar_sync();
    140      1.2    matt 	/* synchronizing instruction will be the rfi to user mode */
    141      1.2    matt }
    142      1.2    matt 
    143      1.2    matt void
    144      1.2    matt dcache_wb(vaddr_t va, vsize_t len)
    145      1.2    matt {
    146  1.2.6.1  cherry 	cache_op(va, len, curcpu()->ci_ci.dcache_line_size, OP_DCBST);
    147      1.2    matt }
    148      1.2    matt 
    149      1.2    matt void
    150      1.2    matt dcache_wbinv(vaddr_t va, vsize_t len)
    151      1.2    matt {
    152  1.2.6.1  cherry 	cache_op(va, len, curcpu()->ci_ci.dcache_line_size, OP_DCBF);
    153      1.2    matt }
    154      1.2    matt 
    155      1.2    matt void
    156      1.2    matt dcache_inv(vaddr_t va, vsize_t len)
    157      1.2    matt {
    158  1.2.6.1  cherry 	cache_op(va, len, curcpu()->ci_ci.dcache_line_size, OP_DCBI);
    159      1.2    matt }
    160      1.2    matt 
    161      1.2    matt void
    162      1.2    matt icache_inv(vaddr_t va, vsize_t len)
    163      1.2    matt {
    164  1.2.6.1  cherry 	membar_sync();
    165  1.2.6.1  cherry 	cache_op(va, len, curcpu()->ci_ci.icache_line_size, OP_ICBI);
    166  1.2.6.1  cherry 	membar_sync();
    167      1.2    matt }
    168