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