xref: /freebsd/sys/arm64/arm64/cpu_feat.c (revision ac4fa5838bb33f0c3ba05fce02d41164bd84a560)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2024 Arm Ltd
5  *
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/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 
32 #include <machine/cpu.h>
33 #include <machine/cpu_feat.h>
34 
35 /* TODO: Make this a list if we ever grow a callback other than smccc_errata */
36 static cpu_feat_errata_check_fn cpu_feat_check_cb = NULL;
37 
38 void
enable_cpu_feat(uint32_t stage)39 enable_cpu_feat(uint32_t stage)
40 {
41 	struct cpu_feat **featp, *feat;
42 	uint32_t midr;
43 	u_int errata_count, *errata_list;
44 	cpu_feat_errata errata_status;
45 
46 	MPASS((stage & ~CPU_FEAT_STAGE_MASK) == 0);
47 
48 	midr = get_midr();
49 	SET_FOREACH(featp, cpu_feat_set) {
50 		feat = *featp;
51 
52 		/* Run the enablement code at the correct stage of boot */
53 		if ((feat->feat_flags & CPU_FEAT_STAGE_MASK) != stage)
54 			continue;
55 
56 		/* If the feature is system wide run on a single CPU */
57 		if ((feat->feat_flags & CPU_FEAT_SCOPE_MASK)==CPU_FEAT_SYSTEM &&
58 		    PCPU_GET(cpuid) != 0)
59 			continue;
60 
61 		if (feat->feat_check != NULL && !feat->feat_check(feat, midr))
62 			continue;
63 
64 		/*
65 		 * Check if the feature has any errata that may need a
66 		 * workaround applied (or it is to install the workaround for
67 		 * known errata.
68 		 */
69 		errata_status = ERRATA_NONE;
70 		errata_list = NULL;
71 		errata_count = 0;
72 		if (feat->feat_has_errata != NULL) {
73 			if (feat->feat_has_errata(feat, midr, &errata_list,
74 			    &errata_count)) {
75 				/* Assume we are affected */
76 				errata_status = ERRATA_AFFECTED;
77 			}
78 		}
79 
80 		if (errata_status == ERRATA_AFFECTED &&
81 		    cpu_feat_check_cb != NULL) {
82 			for (int i = 0; i < errata_count; i++) {
83 				cpu_feat_errata new_status;
84 
85 				/* Check if affected by this erratum */
86 				new_status = cpu_feat_check_cb(feat,
87 				    errata_list[i]);
88 				if (new_status != ERRATA_UNKNOWN) {
89 					errata_status = new_status;
90 					errata_list = &errata_list[i];
91 					errata_count = 1;
92 					break;
93 				}
94 			}
95 		}
96 
97 		/* Shouldn't be possible */
98 		MPASS(errata_status != ERRATA_UNKNOWN);
99 
100 		feat->feat_enable(feat, errata_status, errata_list,
101 		    errata_count);
102 	}
103 }
104 
105 static void
enable_cpu_feat_after_dev(void * dummy __unused)106 enable_cpu_feat_after_dev(void *dummy __unused)
107 {
108 	MPASS(PCPU_GET(cpuid) == 0);
109 	enable_cpu_feat(CPU_FEAT_AFTER_DEV);
110 }
111 SYSINIT(enable_cpu_feat_after_dev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
112     enable_cpu_feat_after_dev, NULL);
113 
114 void
cpu_feat_register_errata_check(cpu_feat_errata_check_fn cb)115 cpu_feat_register_errata_check(cpu_feat_errata_check_fn cb)
116 {
117 	MPASS(cpu_feat_check_cb == NULL);
118 	cpu_feat_check_cb = cb;
119 }
120