1 /*- 2 * Copyright (c) 2014 The FreeBSD Foundation 3 * 4 * This software was developed by Semihalf under 5 * the sponsorship of the FreeBSD Foundation. 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 33 #include <machine/armreg.h> 34 #include <machine/atomic.h> 35 36 #include <stand.h> 37 #include <efi.h> 38 39 #include "bootstrap.h" 40 #include "cache.h" 41 42 static long cache_flags; 43 #define CACHE_FLAG_DIC_OFF (1<<0) 44 #define CACHE_FLAG_IDC_OFF (1<<1) 45 46 static bool 47 get_cache_dic(uint64_t ctr) 48 { 49 if ((cache_flags & CACHE_FLAG_DIC_OFF) != 0) { 50 return (false); 51 } 52 53 return (CTR_DIC_VAL(ctr) != 0); 54 } 55 56 static bool 57 get_cache_idc(uint64_t ctr) 58 { 59 if ((cache_flags & CACHE_FLAG_IDC_OFF) != 0) { 60 return (false); 61 } 62 63 return (CTR_IDC_VAL(ctr) != 0); 64 } 65 66 static unsigned int 67 get_dcache_line_size(uint64_t ctr) 68 { 69 unsigned int dcl_size; 70 71 /* 72 * Relevant field [19:16] is LOG2 73 * of the number of words in DCache line 74 */ 75 dcl_size = CTR_DLINE_SIZE(ctr); 76 77 /* Size of word shifted by cache line size */ 78 return (sizeof(int) << dcl_size); 79 } 80 81 void 82 cpu_flush_dcache(const void *ptr, size_t len) 83 { 84 uint64_t cl_size, ctr; 85 vm_offset_t addr, end; 86 87 /* Accessible from all security levels */ 88 ctr = READ_SPECIALREG(ctr_el0); 89 90 if (get_cache_idc(ctr)) { 91 dsb(ishst); 92 } else { 93 cl_size = get_dcache_line_size(ctr); 94 95 /* Calculate end address to clean */ 96 end = (vm_offset_t)ptr + (vm_offset_t)len; 97 /* Align start address to cache line */ 98 addr = (vm_offset_t)ptr; 99 addr = rounddown2(addr, cl_size); 100 101 for (; addr < end; addr += cl_size) 102 __asm __volatile("dc civac, %0" : : "r" (addr) : 103 "memory"); 104 /* Full system DSB */ 105 dsb(ish); 106 } 107 } 108 109 void 110 cpu_inval_icache(void) 111 { 112 uint64_t ctr; 113 114 /* Accessible from all security levels */ 115 ctr = READ_SPECIALREG(ctr_el0); 116 117 if (get_cache_dic(ctr)) { 118 isb(); 119 } else { 120 __asm __volatile( 121 "ic ialluis \n" 122 "dsb ish \n" 123 "isb \n" 124 : : : "memory"); 125 } 126 } 127 128 static int 129 command_cache_flags(int argc, char *argv[]) 130 { 131 char *cp; 132 long new_flags; 133 134 if (argc == 3) { 135 if (strcmp(argv[1], "set") == 0) { 136 new_flags = strtol(argv[2], &cp, 0); 137 if (cp[0] != '\0') { 138 printf("Invalid flags\n"); 139 } else { 140 printf("Setting cache flags to %#lx\n", 141 new_flags); 142 cache_flags = new_flags; 143 return (CMD_OK); 144 } 145 } 146 } 147 148 printf("usage: cache_flags set <value>\n"); 149 return (CMD_ERROR); 150 } 151 COMMAND_SET(cache_flags, "cache_flags", "Set cache flags", command_cache_flags); 152