1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2021 Intel Corporation 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/moduleparam.h> 8 #include <linux/slab.h> 9 #include <linux/string.h> 10 11 #include "i915_driver.h" 12 #include "i915_drv.h" 13 #include "i915_mitigations.h" 14 15 static unsigned long mitigations __read_mostly = ~0UL; 16 17 enum { 18 CLEAR_RESIDUALS = 0, 19 }; 20 21 static const char * const names[] = { 22 [CLEAR_RESIDUALS] = "residuals", 23 }; 24 25 bool i915_mitigate_clear_residuals(void) 26 { 27 return READ_ONCE(mitigations) & BIT(CLEAR_RESIDUALS); 28 } 29 30 #ifdef __linux__ 31 32 static int mitigations_set(const char *val, const struct kernel_param *kp) 33 { 34 unsigned long new = ~0UL; 35 char *str, *sep, *tok; 36 bool first = true; 37 int err = 0; 38 39 BUILD_BUG_ON(ARRAY_SIZE(names) >= BITS_PER_TYPE(mitigations)); 40 41 str = kstrdup(val, GFP_KERNEL); 42 if (!str) 43 return -ENOMEM; 44 45 for (sep = str; (tok = strsep(&sep, ","));) { 46 bool enable = true; 47 int i; 48 49 /* Be tolerant of leading/trailing whitespace */ 50 tok = strim(tok); 51 52 if (first) { 53 first = false; 54 55 if (!strcmp(tok, "auto")) 56 continue; 57 58 new = 0; 59 if (!strcmp(tok, "off")) 60 continue; 61 } 62 63 if (*tok == '!') { 64 enable = !enable; 65 tok++; 66 } 67 68 if (!strncmp(tok, "no", 2)) { 69 enable = !enable; 70 tok += 2; 71 } 72 73 if (*tok == '\0') 74 continue; 75 76 for (i = 0; i < ARRAY_SIZE(names); i++) { 77 if (!strcmp(tok, names[i])) { 78 if (enable) 79 new |= BIT(i); 80 else 81 new &= ~BIT(i); 82 break; 83 } 84 } 85 if (i == ARRAY_SIZE(names)) { 86 pr_err("Bad \"%s.mitigations=%s\", '%s' is unknown\n", 87 DRIVER_NAME, val, tok); 88 err = -EINVAL; 89 break; 90 } 91 } 92 kfree(str); 93 if (err) 94 return err; 95 96 WRITE_ONCE(mitigations, new); 97 return 0; 98 } 99 100 static int mitigations_get(char *buffer, const struct kernel_param *kp) 101 { 102 unsigned long local = READ_ONCE(mitigations); 103 int count, i; 104 bool enable; 105 106 if (!local) 107 return scnprintf(buffer, PAGE_SIZE, "%s\n", "off"); 108 109 if (local & BIT(BITS_PER_LONG - 1)) { 110 count = scnprintf(buffer, PAGE_SIZE, "%s,", "auto"); 111 enable = false; 112 } else { 113 enable = true; 114 count = 0; 115 } 116 117 for (i = 0; i < ARRAY_SIZE(names); i++) { 118 if ((local & BIT(i)) != enable) 119 continue; 120 121 count += scnprintf(buffer + count, PAGE_SIZE - count, 122 "%s%s,", enable ? "" : "!", names[i]); 123 } 124 125 buffer[count - 1] = '\n'; 126 return count; 127 } 128 129 static const struct kernel_param_ops ops = { 130 .set = mitigations_set, 131 .get = mitigations_get, 132 }; 133 134 module_param_cb_unsafe(mitigations, &ops, NULL, 0600); 135 MODULE_PARM_DESC(mitigations, 136 "Selectively enable security mitigations for all Intel® GPUs in the system.\n" 137 "\n" 138 " auto -- enables all mitigations required for the platform [default]\n" 139 " off -- disables all mitigations\n" 140 "\n" 141 "Individual mitigations can be enabled by passing a comma-separated string,\n" 142 "e.g. mitigations=residuals to enable only clearing residuals or\n" 143 "mitigations=auto,noresiduals to disable only the clear residual mitigation.\n" 144 "Either '!' or 'no' may be used to switch from enabling the mitigation to\n" 145 "disabling it.\n" 146 "\n" 147 "Active mitigations for Ivybridge, Baytrail, Haswell:\n" 148 " residuals -- clear all thread-local registers between contexts" 149 ); 150 151 #endif /* __linux__ */ 152