xref: /freebsd/stand/arm64/libarm64/cache.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
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 
38 #include "bootstrap.h"
39 #include "cache.h"
40 
41 static long cache_flags;
42 #define	CACHE_FLAG_DIC_OFF	(1<<0)
43 #define	CACHE_FLAG_IDC_OFF	(1<<1)
44 
45 static bool
46 get_cache_dic(uint64_t ctr)
47 {
48 	if ((cache_flags & CACHE_FLAG_DIC_OFF) != 0) {
49 		return (false);
50 	}
51 
52 	return (CTR_DIC_VAL(ctr) != 0);
53 }
54 
55 static bool
56 get_cache_idc(uint64_t ctr)
57 {
58 	if ((cache_flags & CACHE_FLAG_IDC_OFF) != 0) {
59 		return (false);
60 	}
61 
62 	return (CTR_IDC_VAL(ctr) != 0);
63 }
64 
65 static unsigned int
66 get_dcache_line_size(uint64_t ctr)
67 {
68 	unsigned int dcl_size;
69 
70 	/*
71 	 * Relevant field [19:16] is LOG2
72 	 * of the number of words in DCache line
73 	 */
74 	dcl_size = CTR_DLINE_SIZE(ctr);
75 
76 	/* Size of word shifted by cache line size */
77 	return (sizeof(int) << dcl_size);
78 }
79 
80 void
81 cpu_flush_dcache(const void *ptr, size_t len)
82 {
83 	uint64_t cl_size, ctr;
84 	vm_offset_t addr, end;
85 
86 	/* Accessible from all security levels */
87 	ctr = READ_SPECIALREG(ctr_el0);
88 
89 	if (get_cache_idc(ctr)) {
90 		dsb(ishst);
91 	} else {
92 		cl_size = get_dcache_line_size(ctr);
93 
94 		/* Calculate end address to clean */
95 		end = (vm_offset_t)ptr + (vm_offset_t)len;
96 		/* Align start address to cache line */
97 		addr = (vm_offset_t)ptr;
98 		addr = rounddown2(addr, cl_size);
99 
100 		for (; addr < end; addr += cl_size)
101 			__asm __volatile("dc	civac, %0" : : "r" (addr) :
102 			    "memory");
103 		/* Full system DSB */
104 		dsb(ish);
105 	}
106 }
107 
108 void
109 cpu_inval_icache(void)
110 {
111 	uint64_t ctr;
112 
113 	/* Accessible from all security levels */
114 	ctr = READ_SPECIALREG(ctr_el0);
115 
116 	if (get_cache_dic(ctr)) {
117 		isb();
118 	} else {
119 		__asm __volatile(
120 		    "ic		ialluis	\n"
121 		    "dsb	ish	\n"
122 		    "isb		\n"
123 		    : : : "memory");
124 	}
125 }
126 
127 static int
128 command_cache_flags(int argc, char *argv[])
129 {
130 	char *cp;
131 	long new_flags;
132 
133 	if (argc == 3) {
134 		if (strcmp(argv[1], "set") == 0) {
135 			new_flags = strtol(argv[2], &cp, 0);
136 			if (cp[0] != '\0') {
137 				printf("Invalid flags\n");
138 			} else {
139 				printf("Setting cache flags to %#lx\n",
140 				    new_flags);
141 				cache_flags = new_flags;
142 				return (CMD_OK);
143 			}
144 		}
145 	}
146 
147 	printf("usage: cache_flags set <value>\n");
148 	return (CMD_ERROR);
149 }
150 COMMAND_SET(cache_flags, "cache_flags", "Set cache flags", command_cache_flags);
151