1 /* SPDX-License-Identifier: MIT */ 2 /* 3 * Copyright © 2014-2019 Intel Corporation 4 */ 5 6 #ifndef _INTEL_UC_FW_H_ 7 #define _INTEL_UC_FW_H_ 8 9 #include <linux/types.h> 10 #include "intel_uc_fw_abi.h" 11 #include "intel_device_info.h" 12 #include "i915_gem.h" 13 #include "i915_vma.h" 14 15 struct drm_printer; 16 struct drm_i915_private; 17 struct intel_gt; 18 19 /* Home of GuC, HuC and DMC firmwares */ 20 #define INTEL_UC_FIRMWARE_URL "https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/tree/i915" 21 22 /* 23 * +------------+---------------------------------------------------+ 24 * | PHASE | FIRMWARE STATUS TRANSITIONS | 25 * +============+===================================================+ 26 * | | UNINITIALIZED | 27 * +------------+- / | \ -+ 28 * | | DISABLED <--/ | \--> NOT_SUPPORTED | 29 * | init_early | V | 30 * | | SELECTED | 31 * +------------+- / | \ -+ 32 * | | MISSING <--/ | \--> ERROR | 33 * | fetch | V | 34 * | | AVAILABLE | 35 * +------------+- | \ -+ 36 * | | | \--> INIT FAIL | 37 * | init | V | 38 * | | /------> LOADABLE <----<-----------\ | 39 * +------------+- \ / \ \ \ -+ 40 * | | LOAD FAIL <--< \--> TRANSFERRED \ | 41 * | upload | \ / \ / | 42 * | | \---------/ \--> RUNNING | 43 * +------------+---------------------------------------------------+ 44 */ 45 46 enum intel_uc_fw_status { 47 INTEL_UC_FIRMWARE_NOT_SUPPORTED = -1, /* no uc HW */ 48 INTEL_UC_FIRMWARE_UNINITIALIZED = 0, /* used to catch checks done too early */ 49 INTEL_UC_FIRMWARE_DISABLED, /* disabled */ 50 INTEL_UC_FIRMWARE_SELECTED, /* selected the blob we want to load */ 51 INTEL_UC_FIRMWARE_MISSING, /* blob not found on the system */ 52 INTEL_UC_FIRMWARE_ERROR, /* invalid format or version */ 53 INTEL_UC_FIRMWARE_AVAILABLE, /* blob found and copied in mem */ 54 INTEL_UC_FIRMWARE_INIT_FAIL, /* failed to prepare fw objects for load */ 55 INTEL_UC_FIRMWARE_LOADABLE, /* all fw-required objects are ready */ 56 INTEL_UC_FIRMWARE_LOAD_FAIL, /* failed to xfer or init/auth the fw */ 57 INTEL_UC_FIRMWARE_TRANSFERRED, /* dma xfer done */ 58 INTEL_UC_FIRMWARE_RUNNING /* init/auth done */ 59 }; 60 61 enum intel_uc_fw_type { 62 INTEL_UC_FW_TYPE_GUC = 0, 63 INTEL_UC_FW_TYPE_HUC 64 }; 65 #define INTEL_UC_FW_NUM_TYPES 2 66 67 /* 68 * This structure encapsulates all the data needed during the process 69 * of fetching, caching, and loading the firmware image into the uC. 70 */ 71 struct intel_uc_fw { 72 enum intel_uc_fw_type type; 73 union { 74 const enum intel_uc_fw_status status; 75 enum intel_uc_fw_status __status; /* no accidental overwrites */ 76 }; 77 const char *path; 78 bool user_overridden; 79 size_t size; 80 struct drm_i915_gem_object *obj; 81 /** 82 * @dummy: A vma used in binding the uc fw to ggtt. We can't define this 83 * vma on the stack as it can lead to a stack overflow, so we define it 84 * here. Safe to have 1 copy per uc fw because the binding is single 85 * threaded as it done during driver load (inherently single threaded) 86 * or during a GT reset (mutex guarantees single threaded). 87 */ 88 struct i915_vma_resource dummy; 89 struct i915_vma *rsa_data; 90 91 /* 92 * The firmware build process will generate a version header file with major and 93 * minor version defined. The versions are built into CSS header of firmware. 94 * i915 kernel driver set the minimal firmware version required per platform. 95 */ 96 u16 major_ver_wanted; 97 u16 minor_ver_wanted; 98 u16 major_ver_found; 99 u16 minor_ver_found; 100 101 u32 rsa_size; 102 u32 ucode_size; 103 104 u32 private_data_size; 105 106 bool loaded_via_gsc; 107 }; 108 109 #ifdef CONFIG_DRM_I915_DEBUG_GUC 110 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw, 111 enum intel_uc_fw_status status); 112 #else 113 static inline void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw, 114 enum intel_uc_fw_status status) 115 { 116 uc_fw->__status = status; 117 } 118 #endif 119 120 static inline 121 const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status) 122 { 123 switch (status) { 124 case INTEL_UC_FIRMWARE_NOT_SUPPORTED: 125 return "N/A"; 126 case INTEL_UC_FIRMWARE_UNINITIALIZED: 127 return "UNINITIALIZED"; 128 case INTEL_UC_FIRMWARE_DISABLED: 129 return "DISABLED"; 130 case INTEL_UC_FIRMWARE_SELECTED: 131 return "SELECTED"; 132 case INTEL_UC_FIRMWARE_MISSING: 133 return "MISSING"; 134 case INTEL_UC_FIRMWARE_ERROR: 135 return "ERROR"; 136 case INTEL_UC_FIRMWARE_AVAILABLE: 137 return "AVAILABLE"; 138 case INTEL_UC_FIRMWARE_INIT_FAIL: 139 return "INIT FAIL"; 140 case INTEL_UC_FIRMWARE_LOADABLE: 141 return "LOADABLE"; 142 case INTEL_UC_FIRMWARE_LOAD_FAIL: 143 return "LOAD FAIL"; 144 case INTEL_UC_FIRMWARE_TRANSFERRED: 145 return "TRANSFERRED"; 146 case INTEL_UC_FIRMWARE_RUNNING: 147 return "RUNNING"; 148 } 149 return "<invalid>"; 150 } 151 152 static inline int intel_uc_fw_status_to_error(enum intel_uc_fw_status status) 153 { 154 switch (status) { 155 case INTEL_UC_FIRMWARE_NOT_SUPPORTED: 156 return -ENODEV; 157 case INTEL_UC_FIRMWARE_UNINITIALIZED: 158 return -EACCES; 159 case INTEL_UC_FIRMWARE_DISABLED: 160 return -EPERM; 161 case INTEL_UC_FIRMWARE_MISSING: 162 return -ENOENT; 163 case INTEL_UC_FIRMWARE_ERROR: 164 return -ENOEXEC; 165 case INTEL_UC_FIRMWARE_INIT_FAIL: 166 case INTEL_UC_FIRMWARE_LOAD_FAIL: 167 return -EIO; 168 case INTEL_UC_FIRMWARE_SELECTED: 169 return -ESTALE; 170 case INTEL_UC_FIRMWARE_AVAILABLE: 171 case INTEL_UC_FIRMWARE_LOADABLE: 172 case INTEL_UC_FIRMWARE_TRANSFERRED: 173 case INTEL_UC_FIRMWARE_RUNNING: 174 return 0; 175 } 176 return -EINVAL; 177 } 178 179 static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type) 180 { 181 switch (type) { 182 case INTEL_UC_FW_TYPE_GUC: 183 return "GuC"; 184 case INTEL_UC_FW_TYPE_HUC: 185 return "HuC"; 186 } 187 return "uC"; 188 } 189 190 static inline enum intel_uc_fw_status 191 __intel_uc_fw_status(struct intel_uc_fw *uc_fw) 192 { 193 /* shouldn't call this before checking hw/blob availability */ 194 GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED); 195 return uc_fw->status; 196 } 197 198 static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw) 199 { 200 return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED; 201 } 202 203 static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw) 204 { 205 return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED; 206 } 207 208 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw) 209 { 210 return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE; 211 } 212 213 static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw) 214 { 215 return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE; 216 } 217 218 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw) 219 { 220 return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED; 221 } 222 223 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw) 224 { 225 return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING; 226 } 227 228 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw) 229 { 230 return uc_fw->user_overridden; 231 } 232 233 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw) 234 { 235 if (intel_uc_fw_is_loaded(uc_fw)) 236 intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE); 237 } 238 239 static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw) 240 { 241 return sizeof(struct uc_css_header) + uc_fw->ucode_size; 242 } 243 244 /** 245 * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded. 246 * @uc_fw: uC firmware. 247 * 248 * Get the size of the firmware and header that will be uploaded to WOPCM. 249 * 250 * Return: Upload firmware size, or zero on firmware fetch failure. 251 */ 252 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw) 253 { 254 if (!intel_uc_fw_is_available(uc_fw)) 255 return 0; 256 257 return __intel_uc_fw_get_upload_size(uc_fw); 258 } 259 260 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw, 261 enum intel_uc_fw_type type); 262 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw); 263 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw); 264 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags); 265 int intel_uc_fw_init(struct intel_uc_fw *uc_fw); 266 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw); 267 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len); 268 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p); 269 270 #endif 271