1 1.9 riastrad /* $NetBSD: intel_csr.c,v 1.9 2021/12/27 11:06:49 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2014 Intel Corporation 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice (including the next 14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 15 1.1 riastrad * Software. 16 1.1 riastrad * 17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 1.1 riastrad * IN THE SOFTWARE. 24 1.1 riastrad * 25 1.1 riastrad */ 26 1.7 riastrad 27 1.1 riastrad #include <sys/cdefs.h> 28 1.9 riastrad __KERNEL_RCSID(0, "$NetBSD: intel_csr.c,v 1.9 2021/12/27 11:06:49 riastradh Exp $"); 29 1.1 riastrad 30 1.1 riastrad #include <linux/firmware.h> 31 1.7 riastrad 32 1.1 riastrad #include "i915_drv.h" 33 1.1 riastrad #include "i915_reg.h" 34 1.7 riastrad #include "intel_csr.h" 35 1.1 riastrad 36 1.1 riastrad /** 37 1.1 riastrad * DOC: csr support for dmc 38 1.1 riastrad * 39 1.1 riastrad * Display Context Save and Restore (CSR) firmware support added from gen9 40 1.1 riastrad * onwards to drive newly added DMC (Display microcontroller) in display 41 1.1 riastrad * engine to save and restore the state of display engine when it enter into 42 1.1 riastrad * low-power state and comes back to normal. 43 1.1 riastrad */ 44 1.1 riastrad 45 1.7 riastrad #define GEN12_CSR_MAX_FW_SIZE ICL_CSR_MAX_FW_SIZE 46 1.7 riastrad 47 1.7 riastrad #define TGL_CSR_PATH "i915/tgl_dmc_ver2_04.bin" 48 1.7 riastrad #define TGL_CSR_VERSION_REQUIRED CSR_VERSION(2, 4) 49 1.7 riastrad #define TGL_CSR_MAX_FW_SIZE 0x6000 50 1.7 riastrad MODULE_FIRMWARE(TGL_CSR_PATH); 51 1.7 riastrad 52 1.7 riastrad #define ICL_CSR_PATH "i915/icl_dmc_ver1_09.bin" 53 1.7 riastrad #define ICL_CSR_VERSION_REQUIRED CSR_VERSION(1, 9) 54 1.7 riastrad #define ICL_CSR_MAX_FW_SIZE 0x6000 55 1.7 riastrad MODULE_FIRMWARE(ICL_CSR_PATH); 56 1.7 riastrad 57 1.7 riastrad #define CNL_CSR_PATH "i915/cnl_dmc_ver1_07.bin" 58 1.7 riastrad #define CNL_CSR_VERSION_REQUIRED CSR_VERSION(1, 7) 59 1.7 riastrad #define CNL_CSR_MAX_FW_SIZE GLK_CSR_MAX_FW_SIZE 60 1.7 riastrad MODULE_FIRMWARE(CNL_CSR_PATH); 61 1.7 riastrad 62 1.7 riastrad #define GLK_CSR_PATH "i915/glk_dmc_ver1_04.bin" 63 1.7 riastrad #define GLK_CSR_VERSION_REQUIRED CSR_VERSION(1, 4) 64 1.7 riastrad #define GLK_CSR_MAX_FW_SIZE 0x4000 65 1.7 riastrad MODULE_FIRMWARE(GLK_CSR_PATH); 66 1.7 riastrad 67 1.7 riastrad #define KBL_CSR_PATH "i915/kbl_dmc_ver1_04.bin" 68 1.7 riastrad #define KBL_CSR_VERSION_REQUIRED CSR_VERSION(1, 4) 69 1.7 riastrad #define KBL_CSR_MAX_FW_SIZE BXT_CSR_MAX_FW_SIZE 70 1.7 riastrad MODULE_FIRMWARE(KBL_CSR_PATH); 71 1.7 riastrad 72 1.7 riastrad #define SKL_CSR_PATH "i915/skl_dmc_ver1_27.bin" 73 1.7 riastrad #define SKL_CSR_VERSION_REQUIRED CSR_VERSION(1, 27) 74 1.7 riastrad #define SKL_CSR_MAX_FW_SIZE BXT_CSR_MAX_FW_SIZE 75 1.7 riastrad MODULE_FIRMWARE(SKL_CSR_PATH); 76 1.7 riastrad 77 1.7 riastrad #define BXT_CSR_PATH "i915/bxt_dmc_ver1_07.bin" 78 1.7 riastrad #define BXT_CSR_VERSION_REQUIRED CSR_VERSION(1, 7) 79 1.7 riastrad #define BXT_CSR_MAX_FW_SIZE 0x3000 80 1.7 riastrad MODULE_FIRMWARE(BXT_CSR_PATH); 81 1.1 riastrad 82 1.1 riastrad #define CSR_DEFAULT_FW_OFFSET 0xFFFFFFFF 83 1.7 riastrad #define PACKAGE_MAX_FW_INFO_ENTRIES 20 84 1.7 riastrad #define PACKAGE_V2_MAX_FW_INFO_ENTRIES 32 85 1.7 riastrad #define DMC_V1_MAX_MMIO_COUNT 8 86 1.7 riastrad #define DMC_V3_MAX_MMIO_COUNT 20 87 1.1 riastrad 88 1.1 riastrad struct intel_css_header { 89 1.1 riastrad /* 0x09 for DMC */ 90 1.7 riastrad u32 module_type; 91 1.1 riastrad 92 1.1 riastrad /* Includes the DMC specific header in dwords */ 93 1.7 riastrad u32 header_len; 94 1.1 riastrad 95 1.1 riastrad /* always value would be 0x10000 */ 96 1.7 riastrad u32 header_ver; 97 1.1 riastrad 98 1.1 riastrad /* Not used */ 99 1.7 riastrad u32 module_id; 100 1.1 riastrad 101 1.1 riastrad /* Not used */ 102 1.7 riastrad u32 module_vendor; 103 1.1 riastrad 104 1.1 riastrad /* in YYYYMMDD format */ 105 1.7 riastrad u32 date; 106 1.1 riastrad 107 1.1 riastrad /* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */ 108 1.7 riastrad u32 size; 109 1.1 riastrad 110 1.1 riastrad /* Not used */ 111 1.7 riastrad u32 key_size; 112 1.1 riastrad 113 1.1 riastrad /* Not used */ 114 1.7 riastrad u32 modulus_size; 115 1.1 riastrad 116 1.1 riastrad /* Not used */ 117 1.7 riastrad u32 exponent_size; 118 1.1 riastrad 119 1.1 riastrad /* Not used */ 120 1.7 riastrad u32 reserved1[12]; 121 1.1 riastrad 122 1.1 riastrad /* Major Minor */ 123 1.7 riastrad u32 version; 124 1.1 riastrad 125 1.1 riastrad /* Not used */ 126 1.7 riastrad u32 reserved2[8]; 127 1.1 riastrad 128 1.1 riastrad /* Not used */ 129 1.7 riastrad u32 kernel_header_info; 130 1.1 riastrad } __packed; 131 1.1 riastrad 132 1.1 riastrad struct intel_fw_info { 133 1.7 riastrad u8 reserved1; 134 1.7 riastrad 135 1.7 riastrad /* reserved on package_header version 1, must be 0 on version 2 */ 136 1.7 riastrad u8 dmc_id; 137 1.1 riastrad 138 1.1 riastrad /* Stepping (A, B, C, ..., *). * is a wildcard */ 139 1.1 riastrad char stepping; 140 1.1 riastrad 141 1.1 riastrad /* Sub-stepping (0, 1, ..., *). * is a wildcard */ 142 1.1 riastrad char substepping; 143 1.1 riastrad 144 1.7 riastrad u32 offset; 145 1.7 riastrad u32 reserved2; 146 1.1 riastrad } __packed; 147 1.1 riastrad 148 1.1 riastrad struct intel_package_header { 149 1.1 riastrad /* DMC container header length in dwords */ 150 1.7 riastrad u8 header_len; 151 1.1 riastrad 152 1.7 riastrad /* 0x01, 0x02 */ 153 1.7 riastrad u8 header_ver; 154 1.1 riastrad 155 1.7 riastrad u8 reserved[10]; 156 1.1 riastrad 157 1.1 riastrad /* Number of valid entries in the FWInfo array below */ 158 1.7 riastrad u32 num_entries; 159 1.1 riastrad } __packed; 160 1.1 riastrad 161 1.7 riastrad struct intel_dmc_header_base { 162 1.1 riastrad /* always value would be 0x40403E3E */ 163 1.7 riastrad u32 signature; 164 1.1 riastrad 165 1.1 riastrad /* DMC binary header length */ 166 1.7 riastrad u8 header_len; 167 1.1 riastrad 168 1.1 riastrad /* 0x01 */ 169 1.7 riastrad u8 header_ver; 170 1.1 riastrad 171 1.1 riastrad /* Reserved */ 172 1.7 riastrad u16 dmcc_ver; 173 1.1 riastrad 174 1.1 riastrad /* Major, Minor */ 175 1.7 riastrad u32 project; 176 1.1 riastrad 177 1.1 riastrad /* Firmware program size (excluding header) in dwords */ 178 1.7 riastrad u32 fw_size; 179 1.1 riastrad 180 1.1 riastrad /* Major Minor version */ 181 1.7 riastrad u32 fw_version; 182 1.7 riastrad } __packed; 183 1.7 riastrad 184 1.7 riastrad struct intel_dmc_header_v1 { 185 1.7 riastrad struct intel_dmc_header_base base; 186 1.1 riastrad 187 1.1 riastrad /* Number of valid MMIO cycles present. */ 188 1.7 riastrad u32 mmio_count; 189 1.1 riastrad 190 1.1 riastrad /* MMIO address */ 191 1.7 riastrad u32 mmioaddr[DMC_V1_MAX_MMIO_COUNT]; 192 1.1 riastrad 193 1.1 riastrad /* MMIO data */ 194 1.7 riastrad u32 mmiodata[DMC_V1_MAX_MMIO_COUNT]; 195 1.1 riastrad 196 1.1 riastrad /* FW filename */ 197 1.7 riastrad char dfile[32]; 198 1.1 riastrad 199 1.7 riastrad u32 reserved1[2]; 200 1.7 riastrad } __packed; 201 1.7 riastrad 202 1.7 riastrad struct intel_dmc_header_v3 { 203 1.7 riastrad struct intel_dmc_header_base base; 204 1.7 riastrad 205 1.7 riastrad /* DMC RAM start MMIO address */ 206 1.7 riastrad u32 start_mmioaddr; 207 1.7 riastrad 208 1.7 riastrad u32 reserved[9]; 209 1.7 riastrad 210 1.7 riastrad /* FW filename */ 211 1.7 riastrad char dfile[32]; 212 1.7 riastrad 213 1.7 riastrad /* Number of valid MMIO cycles present. */ 214 1.7 riastrad u32 mmio_count; 215 1.7 riastrad 216 1.7 riastrad /* MMIO address */ 217 1.7 riastrad u32 mmioaddr[DMC_V3_MAX_MMIO_COUNT]; 218 1.7 riastrad 219 1.7 riastrad /* MMIO data */ 220 1.7 riastrad u32 mmiodata[DMC_V3_MAX_MMIO_COUNT]; 221 1.1 riastrad } __packed; 222 1.1 riastrad 223 1.1 riastrad struct stepping_info { 224 1.1 riastrad char stepping; 225 1.1 riastrad char substepping; 226 1.1 riastrad }; 227 1.1 riastrad 228 1.5 mrg /* 229 1.5 mrg * Kabylake derivated from Skylake H0, so SKL H0 230 1.5 mrg * is the right firmware for KBL A0 (revid 0). 231 1.5 mrg */ 232 1.8 riastrad static const struct stepping_info kbl_stepping_info[] __unused = { 233 1.5 mrg {'H', '0'}, {'I', '0'} 234 1.5 mrg }; 235 1.5 mrg 236 1.1 riastrad static const struct stepping_info skl_stepping_info[] = { 237 1.7 riastrad {'A', '0'}, {'B', '0'}, {'C', '0'}, 238 1.7 riastrad {'D', '0'}, {'E', '0'}, {'F', '0'}, 239 1.7 riastrad {'G', '0'}, {'H', '0'}, {'I', '0'}, 240 1.7 riastrad {'J', '0'}, {'K', '0'} 241 1.1 riastrad }; 242 1.1 riastrad 243 1.7 riastrad static const struct stepping_info bxt_stepping_info[] = { 244 1.1 riastrad {'A', '0'}, {'A', '1'}, {'A', '2'}, 245 1.1 riastrad {'B', '0'}, {'B', '1'}, {'B', '2'} 246 1.1 riastrad }; 247 1.1 riastrad 248 1.7 riastrad static const struct stepping_info icl_stepping_info[] = { 249 1.7 riastrad {'A', '0'}, {'A', '1'}, {'A', '2'}, 250 1.7 riastrad {'B', '0'}, {'B', '2'}, 251 1.7 riastrad {'C', '0'} 252 1.7 riastrad }; 253 1.7 riastrad 254 1.7 riastrad static const struct stepping_info no_stepping_info = { '*', '*' }; 255 1.7 riastrad 256 1.7 riastrad static const struct stepping_info * 257 1.7 riastrad intel_get_stepping_info(struct drm_i915_private *dev_priv) 258 1.1 riastrad { 259 1.7 riastrad const struct stepping_info *si; 260 1.7 riastrad unsigned int size; 261 1.7 riastrad 262 1.7 riastrad if (IS_ICELAKE(dev_priv)) { 263 1.7 riastrad size = ARRAY_SIZE(icl_stepping_info); 264 1.7 riastrad si = icl_stepping_info; 265 1.7 riastrad } else if (IS_SKYLAKE(dev_priv)) { 266 1.7 riastrad size = ARRAY_SIZE(skl_stepping_info); 267 1.7 riastrad si = skl_stepping_info; 268 1.7 riastrad } else if (IS_BROXTON(dev_priv)) { 269 1.7 riastrad size = ARRAY_SIZE(bxt_stepping_info); 270 1.7 riastrad si = bxt_stepping_info; 271 1.7 riastrad } else { 272 1.7 riastrad size = 0; 273 1.7 riastrad si = NULL; 274 1.7 riastrad } 275 1.7 riastrad 276 1.7 riastrad if (INTEL_REVID(dev_priv) < size) 277 1.7 riastrad return si + INTEL_REVID(dev_priv); 278 1.7 riastrad 279 1.7 riastrad return &no_stepping_info; 280 1.1 riastrad } 281 1.1 riastrad 282 1.7 riastrad static void gen9_set_dc_state_debugmask(struct drm_i915_private *dev_priv) 283 1.1 riastrad { 284 1.7 riastrad u32 val, mask; 285 1.1 riastrad 286 1.7 riastrad mask = DC_STATE_DEBUG_MASK_MEMORY_UP; 287 1.1 riastrad 288 1.7 riastrad if (IS_GEN9_LP(dev_priv)) 289 1.7 riastrad mask |= DC_STATE_DEBUG_MASK_CORES; 290 1.1 riastrad 291 1.7 riastrad /* The below bit doesn't need to be cleared ever afterwards */ 292 1.7 riastrad val = I915_READ(DC_STATE_DEBUG); 293 1.7 riastrad if ((val & mask) != mask) { 294 1.7 riastrad val |= mask; 295 1.7 riastrad I915_WRITE(DC_STATE_DEBUG, val); 296 1.7 riastrad POSTING_READ(DC_STATE_DEBUG); 297 1.7 riastrad } 298 1.1 riastrad } 299 1.1 riastrad 300 1.1 riastrad /** 301 1.1 riastrad * intel_csr_load_program() - write the firmware from memory to register. 302 1.7 riastrad * @dev_priv: i915 drm device. 303 1.1 riastrad * 304 1.1 riastrad * CSR firmware is read from a .bin file and kept in internal memory one time. 305 1.1 riastrad * Everytime display comes back from low power state this function is called to 306 1.1 riastrad * copy the firmware from internal memory to registers. 307 1.1 riastrad */ 308 1.7 riastrad void intel_csr_load_program(struct drm_i915_private *dev_priv) 309 1.1 riastrad { 310 1.1 riastrad u32 *payload = dev_priv->csr.dmc_payload; 311 1.7 riastrad u32 i, fw_size; 312 1.1 riastrad 313 1.7 riastrad if (!HAS_CSR(dev_priv)) { 314 1.1 riastrad DRM_ERROR("No CSR support available for this platform\n"); 315 1.1 riastrad return; 316 1.1 riastrad } 317 1.1 riastrad 318 1.7 riastrad if (!dev_priv->csr.dmc_payload) { 319 1.7 riastrad DRM_ERROR("Tried to program CSR with empty payload\n"); 320 1.1 riastrad return; 321 1.7 riastrad } 322 1.1 riastrad 323 1.1 riastrad fw_size = dev_priv->csr.dmc_fw_size; 324 1.7 riastrad assert_rpm_wakelock_held(&dev_priv->runtime_pm); 325 1.7 riastrad 326 1.7 riastrad preempt_disable(); 327 1.7 riastrad 328 1.1 riastrad for (i = 0; i < fw_size; i++) 329 1.7 riastrad I915_WRITE_FW(CSR_PROGRAM(i), payload[i]); 330 1.7 riastrad 331 1.7 riastrad preempt_enable(); 332 1.1 riastrad 333 1.1 riastrad for (i = 0; i < dev_priv->csr.mmio_count; i++) { 334 1.1 riastrad I915_WRITE(dev_priv->csr.mmioaddr[i], 335 1.7 riastrad dev_priv->csr.mmiodata[i]); 336 1.1 riastrad } 337 1.1 riastrad 338 1.7 riastrad dev_priv->csr.dc_state = 0; 339 1.7 riastrad 340 1.7 riastrad gen9_set_dc_state_debugmask(dev_priv); 341 1.1 riastrad } 342 1.1 riastrad 343 1.7 riastrad /* 344 1.7 riastrad * Search fw_info table for dmc_offset to find firmware binary: num_entries is 345 1.7 riastrad * already sanitized. 346 1.7 riastrad */ 347 1.7 riastrad static u32 find_dmc_fw_offset(const struct intel_fw_info *fw_info, 348 1.7 riastrad unsigned int num_entries, 349 1.7 riastrad const struct stepping_info *si, 350 1.7 riastrad u8 package_ver) 351 1.1 riastrad { 352 1.7 riastrad u32 dmc_offset = CSR_DEFAULT_FW_OFFSET; 353 1.7 riastrad unsigned int i; 354 1.7 riastrad 355 1.7 riastrad for (i = 0; i < num_entries; i++) { 356 1.7 riastrad if (package_ver > 1 && fw_info[i].dmc_id != 0) 357 1.7 riastrad continue; 358 1.7 riastrad 359 1.7 riastrad if (fw_info[i].substepping == '*' && 360 1.7 riastrad si->stepping == fw_info[i].stepping) { 361 1.7 riastrad dmc_offset = fw_info[i].offset; 362 1.7 riastrad break; 363 1.7 riastrad } 364 1.7 riastrad 365 1.7 riastrad if (si->stepping == fw_info[i].stepping && 366 1.7 riastrad si->substepping == fw_info[i].substepping) { 367 1.7 riastrad dmc_offset = fw_info[i].offset; 368 1.7 riastrad break; 369 1.7 riastrad } 370 1.1 riastrad 371 1.7 riastrad if (fw_info[i].stepping == '*' && 372 1.7 riastrad fw_info[i].substepping == '*') { 373 1.7 riastrad /* 374 1.7 riastrad * In theory we should stop the search as generic 375 1.7 riastrad * entries should always come after the more specific 376 1.7 riastrad * ones, but let's continue to make sure to work even 377 1.7 riastrad * with "broken" firmwares. If we don't find a more 378 1.7 riastrad * specific one, then we use this entry 379 1.7 riastrad */ 380 1.7 riastrad dmc_offset = fw_info[i].offset; 381 1.7 riastrad } 382 1.1 riastrad } 383 1.1 riastrad 384 1.7 riastrad return dmc_offset; 385 1.7 riastrad } 386 1.7 riastrad 387 1.7 riastrad static u32 parse_csr_fw_dmc(struct intel_csr *csr, 388 1.7 riastrad const struct intel_dmc_header_base *dmc_header, 389 1.7 riastrad size_t rem_size) 390 1.7 riastrad { 391 1.7 riastrad unsigned int header_len_bytes, dmc_header_size, payload_size, i; 392 1.7 riastrad const u32 *mmioaddr, *mmiodata; 393 1.7 riastrad u32 mmio_count, mmio_count_max; 394 1.8 riastrad const u8 *payload; 395 1.7 riastrad 396 1.7 riastrad BUILD_BUG_ON(ARRAY_SIZE(csr->mmioaddr) < DMC_V3_MAX_MMIO_COUNT || 397 1.7 riastrad ARRAY_SIZE(csr->mmioaddr) < DMC_V1_MAX_MMIO_COUNT); 398 1.7 riastrad 399 1.7 riastrad /* 400 1.7 riastrad * Check if we can access common fields, we will checkc again below 401 1.7 riastrad * after we have read the version 402 1.7 riastrad */ 403 1.7 riastrad if (rem_size < sizeof(struct intel_dmc_header_base)) 404 1.7 riastrad goto error_truncated; 405 1.1 riastrad 406 1.7 riastrad /* Cope with small differences between v1 and v3 */ 407 1.7 riastrad if (dmc_header->header_ver == 3) { 408 1.7 riastrad const struct intel_dmc_header_v3 *v3 = 409 1.7 riastrad (const struct intel_dmc_header_v3 *)dmc_header; 410 1.7 riastrad 411 1.7 riastrad if (rem_size < sizeof(struct intel_dmc_header_v3)) 412 1.7 riastrad goto error_truncated; 413 1.7 riastrad 414 1.7 riastrad mmioaddr = v3->mmioaddr; 415 1.7 riastrad mmiodata = v3->mmiodata; 416 1.7 riastrad mmio_count = v3->mmio_count; 417 1.7 riastrad mmio_count_max = DMC_V3_MAX_MMIO_COUNT; 418 1.7 riastrad /* header_len is in dwords */ 419 1.7 riastrad header_len_bytes = dmc_header->header_len * 4; 420 1.7 riastrad dmc_header_size = sizeof(*v3); 421 1.7 riastrad } else if (dmc_header->header_ver == 1) { 422 1.7 riastrad const struct intel_dmc_header_v1 *v1 = 423 1.7 riastrad (const struct intel_dmc_header_v1 *)dmc_header; 424 1.7 riastrad 425 1.7 riastrad if (rem_size < sizeof(struct intel_dmc_header_v1)) 426 1.7 riastrad goto error_truncated; 427 1.7 riastrad 428 1.7 riastrad mmioaddr = v1->mmioaddr; 429 1.7 riastrad mmiodata = v1->mmiodata; 430 1.7 riastrad mmio_count = v1->mmio_count; 431 1.7 riastrad mmio_count_max = DMC_V1_MAX_MMIO_COUNT; 432 1.7 riastrad header_len_bytes = dmc_header->header_len; 433 1.7 riastrad dmc_header_size = sizeof(*v1); 434 1.7 riastrad } else { 435 1.7 riastrad DRM_ERROR("Unknown DMC fw header version: %u\n", 436 1.7 riastrad dmc_header->header_ver); 437 1.7 riastrad return 0; 438 1.7 riastrad } 439 1.7 riastrad 440 1.7 riastrad if (header_len_bytes != dmc_header_size) { 441 1.7 riastrad DRM_ERROR("DMC firmware has wrong dmc header length " 442 1.7 riastrad "(%u bytes)\n", header_len_bytes); 443 1.7 riastrad return 0; 444 1.1 riastrad } 445 1.1 riastrad 446 1.7 riastrad /* Cache the dmc header info. */ 447 1.7 riastrad if (mmio_count > mmio_count_max) { 448 1.7 riastrad DRM_ERROR("DMC firmware has wrong mmio count %u\n", mmio_count); 449 1.7 riastrad return 0; 450 1.1 riastrad } 451 1.1 riastrad 452 1.7 riastrad for (i = 0; i < mmio_count; i++) { 453 1.7 riastrad if (mmioaddr[i] < CSR_MMIO_START_RANGE || 454 1.7 riastrad mmioaddr[i] > CSR_MMIO_END_RANGE) { 455 1.7 riastrad DRM_ERROR("DMC firmware has wrong mmio address 0x%x\n", 456 1.7 riastrad mmioaddr[i]); 457 1.7 riastrad return 0; 458 1.1 riastrad } 459 1.7 riastrad csr->mmioaddr[i] = _MMIO(mmioaddr[i]); 460 1.7 riastrad csr->mmiodata[i] = mmiodata[i]; 461 1.1 riastrad } 462 1.7 riastrad csr->mmio_count = mmio_count; 463 1.7 riastrad 464 1.7 riastrad rem_size -= header_len_bytes; 465 1.1 riastrad 466 1.1 riastrad /* fw_size is in dwords, so multiplied by 4 to convert into bytes. */ 467 1.7 riastrad payload_size = dmc_header->fw_size * 4; 468 1.7 riastrad if (rem_size < payload_size) 469 1.7 riastrad goto error_truncated; 470 1.7 riastrad 471 1.7 riastrad if (payload_size > csr->max_fw_size) { 472 1.7 riastrad DRM_ERROR("DMC FW too big (%u bytes)\n", payload_size); 473 1.7 riastrad return 0; 474 1.1 riastrad } 475 1.1 riastrad csr->dmc_fw_size = dmc_header->fw_size; 476 1.1 riastrad 477 1.7 riastrad csr->dmc_payload = kmalloc(payload_size, GFP_KERNEL); 478 1.1 riastrad if (!csr->dmc_payload) { 479 1.1 riastrad DRM_ERROR("Memory allocation failed for dmc payload\n"); 480 1.7 riastrad return 0; 481 1.7 riastrad } 482 1.7 riastrad 483 1.8 riastrad payload = (const u8 *)(dmc_header) + header_len_bytes; 484 1.7 riastrad memcpy(csr->dmc_payload, payload, payload_size); 485 1.7 riastrad 486 1.7 riastrad return header_len_bytes + payload_size; 487 1.7 riastrad 488 1.7 riastrad error_truncated: 489 1.7 riastrad DRM_ERROR("Truncated DMC firmware, refusing.\n"); 490 1.7 riastrad return 0; 491 1.7 riastrad } 492 1.7 riastrad 493 1.7 riastrad static u32 494 1.7 riastrad parse_csr_fw_package(struct intel_csr *csr, 495 1.7 riastrad const struct intel_package_header *package_header, 496 1.7 riastrad const struct stepping_info *si, 497 1.7 riastrad size_t rem_size) 498 1.7 riastrad { 499 1.7 riastrad u32 package_size = sizeof(struct intel_package_header); 500 1.7 riastrad u32 num_entries, max_entries, dmc_offset; 501 1.7 riastrad const struct intel_fw_info *fw_info; 502 1.7 riastrad 503 1.7 riastrad if (rem_size < package_size) 504 1.7 riastrad goto error_truncated; 505 1.7 riastrad 506 1.7 riastrad if (package_header->header_ver == 1) { 507 1.7 riastrad max_entries = PACKAGE_MAX_FW_INFO_ENTRIES; 508 1.7 riastrad } else if (package_header->header_ver == 2) { 509 1.7 riastrad max_entries = PACKAGE_V2_MAX_FW_INFO_ENTRIES; 510 1.7 riastrad } else { 511 1.7 riastrad DRM_ERROR("DMC firmware has unknown header version %u\n", 512 1.7 riastrad package_header->header_ver); 513 1.7 riastrad return 0; 514 1.7 riastrad } 515 1.7 riastrad 516 1.7 riastrad /* 517 1.7 riastrad * We should always have space for max_entries, 518 1.7 riastrad * even if not all are used 519 1.7 riastrad */ 520 1.7 riastrad package_size += max_entries * sizeof(struct intel_fw_info); 521 1.7 riastrad if (rem_size < package_size) 522 1.7 riastrad goto error_truncated; 523 1.7 riastrad 524 1.7 riastrad if (package_header->header_len * 4 != package_size) { 525 1.7 riastrad DRM_ERROR("DMC firmware has wrong package header length " 526 1.7 riastrad "(%u bytes)\n", package_size); 527 1.7 riastrad return 0; 528 1.7 riastrad } 529 1.7 riastrad 530 1.7 riastrad num_entries = package_header->num_entries; 531 1.7 riastrad if (WARN_ON(package_header->num_entries > max_entries)) 532 1.7 riastrad num_entries = max_entries; 533 1.7 riastrad 534 1.7 riastrad fw_info = (const struct intel_fw_info *) 535 1.8 riastrad ((const u8 *)package_header + sizeof(*package_header)); 536 1.7 riastrad dmc_offset = find_dmc_fw_offset(fw_info, num_entries, si, 537 1.7 riastrad package_header->header_ver); 538 1.7 riastrad if (dmc_offset == CSR_DEFAULT_FW_OFFSET) { 539 1.7 riastrad DRM_ERROR("DMC firmware not supported for %c stepping\n", 540 1.7 riastrad si->stepping); 541 1.7 riastrad return 0; 542 1.1 riastrad } 543 1.1 riastrad 544 1.7 riastrad /* dmc_offset is in dwords */ 545 1.7 riastrad return package_size + dmc_offset * 4; 546 1.1 riastrad 547 1.7 riastrad error_truncated: 548 1.7 riastrad DRM_ERROR("Truncated DMC firmware, refusing.\n"); 549 1.7 riastrad return 0; 550 1.7 riastrad } 551 1.7 riastrad 552 1.7 riastrad /* Return number of bytes parsed or 0 on error */ 553 1.7 riastrad static u32 parse_csr_fw_css(struct intel_csr *csr, 554 1.7 riastrad struct intel_css_header *css_header, 555 1.7 riastrad size_t rem_size) 556 1.7 riastrad { 557 1.7 riastrad if (rem_size < sizeof(struct intel_css_header)) { 558 1.7 riastrad DRM_ERROR("Truncated DMC firmware, refusing.\n"); 559 1.7 riastrad return 0; 560 1.7 riastrad } 561 1.7 riastrad 562 1.7 riastrad if (sizeof(struct intel_css_header) != 563 1.7 riastrad (css_header->header_len * 4)) { 564 1.7 riastrad DRM_ERROR("DMC firmware has wrong CSS header length " 565 1.7 riastrad "(%u bytes)\n", 566 1.7 riastrad (css_header->header_len * 4)); 567 1.7 riastrad return 0; 568 1.7 riastrad } 569 1.7 riastrad 570 1.7 riastrad if (csr->required_version && 571 1.7 riastrad css_header->version != csr->required_version) { 572 1.7 riastrad DRM_INFO("Refusing to load DMC firmware v%u.%u," 573 1.7 riastrad " please use v%u.%u\n", 574 1.7 riastrad CSR_VERSION_MAJOR(css_header->version), 575 1.7 riastrad CSR_VERSION_MINOR(css_header->version), 576 1.7 riastrad CSR_VERSION_MAJOR(csr->required_version), 577 1.7 riastrad CSR_VERSION_MINOR(csr->required_version)); 578 1.7 riastrad return 0; 579 1.7 riastrad } 580 1.7 riastrad 581 1.7 riastrad csr->version = css_header->version; 582 1.7 riastrad 583 1.7 riastrad return sizeof(struct intel_css_header); 584 1.7 riastrad } 585 1.7 riastrad 586 1.7 riastrad static void parse_csr_fw(struct drm_i915_private *dev_priv, 587 1.7 riastrad const struct firmware *fw) 588 1.7 riastrad { 589 1.7 riastrad struct intel_css_header *css_header; 590 1.7 riastrad struct intel_package_header *package_header; 591 1.7 riastrad struct intel_dmc_header_base *dmc_header; 592 1.7 riastrad struct intel_csr *csr = &dev_priv->csr; 593 1.7 riastrad const struct stepping_info *si = intel_get_stepping_info(dev_priv); 594 1.7 riastrad u32 readcount = 0; 595 1.7 riastrad u32 r; 596 1.7 riastrad 597 1.7 riastrad if (!fw) 598 1.7 riastrad return; 599 1.7 riastrad 600 1.7 riastrad /* Extract CSS Header information */ 601 1.7 riastrad css_header = (struct intel_css_header *)fw->data; 602 1.7 riastrad r = parse_csr_fw_css(csr, css_header, fw->size); 603 1.7 riastrad if (!r) 604 1.7 riastrad return; 605 1.7 riastrad 606 1.7 riastrad readcount += r; 607 1.7 riastrad 608 1.7 riastrad /* Extract Package Header information */ 609 1.7 riastrad package_header = (struct intel_package_header *)&fw->data[readcount]; 610 1.7 riastrad r = parse_csr_fw_package(csr, package_header, si, fw->size - readcount); 611 1.7 riastrad if (!r) 612 1.7 riastrad return; 613 1.7 riastrad 614 1.7 riastrad readcount += r; 615 1.7 riastrad 616 1.7 riastrad /* Extract dmc_header information */ 617 1.7 riastrad dmc_header = (struct intel_dmc_header_base *)&fw->data[readcount]; 618 1.7 riastrad parse_csr_fw_dmc(csr, dmc_header, fw->size - readcount); 619 1.7 riastrad } 620 1.7 riastrad 621 1.7 riastrad static void intel_csr_runtime_pm_get(struct drm_i915_private *dev_priv) 622 1.7 riastrad { 623 1.7 riastrad WARN_ON(dev_priv->csr.wakeref); 624 1.7 riastrad dev_priv->csr.wakeref = 625 1.7 riastrad intel_display_power_get(dev_priv, POWER_DOMAIN_INIT); 626 1.7 riastrad } 627 1.7 riastrad 628 1.7 riastrad static void intel_csr_runtime_pm_put(struct drm_i915_private *dev_priv) 629 1.7 riastrad { 630 1.7 riastrad intel_wakeref_t wakeref __maybe_unused = 631 1.7 riastrad fetch_and_zero(&dev_priv->csr.wakeref); 632 1.7 riastrad 633 1.7 riastrad intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref); 634 1.7 riastrad } 635 1.7 riastrad 636 1.7 riastrad static void csr_load_work_fn(struct work_struct *work) 637 1.7 riastrad { 638 1.7 riastrad struct drm_i915_private *dev_priv; 639 1.7 riastrad struct intel_csr *csr; 640 1.7 riastrad const struct firmware *fw = NULL; 641 1.7 riastrad 642 1.7 riastrad dev_priv = container_of(work, typeof(*dev_priv), csr.work); 643 1.7 riastrad csr = &dev_priv->csr; 644 1.7 riastrad 645 1.8 riastrad request_firmware(&fw, dev_priv->csr.fw_path, 646 1.8 riastrad pci_dev_dev(dev_priv->drm.pdev)); 647 1.7 riastrad parse_csr_fw(dev_priv, fw); 648 1.7 riastrad 649 1.7 riastrad if (dev_priv->csr.dmc_payload) { 650 1.7 riastrad intel_csr_load_program(dev_priv); 651 1.7 riastrad intel_csr_runtime_pm_put(dev_priv); 652 1.7 riastrad 653 1.7 riastrad DRM_INFO("Finished loading DMC firmware %s (v%u.%u)\n", 654 1.7 riastrad dev_priv->csr.fw_path, 655 1.7 riastrad CSR_VERSION_MAJOR(csr->version), 656 1.7 riastrad CSR_VERSION_MINOR(csr->version)); 657 1.7 riastrad } else { 658 1.7 riastrad dev_notice(dev_priv->drm.dev, 659 1.7 riastrad "Failed to load DMC firmware %s." 660 1.7 riastrad " Disabling runtime power management.\n", 661 1.7 riastrad csr->fw_path); 662 1.9 riastrad #ifndef __NetBSD__ 663 1.7 riastrad dev_notice(dev_priv->drm.dev, "DMC firmware homepage: %s", 664 1.7 riastrad INTEL_UC_FIRMWARE_URL); 665 1.9 riastrad #endif 666 1.7 riastrad } 667 1.1 riastrad 668 1.1 riastrad release_firmware(fw); 669 1.1 riastrad } 670 1.1 riastrad 671 1.1 riastrad /** 672 1.1 riastrad * intel_csr_ucode_init() - initialize the firmware loading. 673 1.7 riastrad * @dev_priv: i915 drm device. 674 1.1 riastrad * 675 1.1 riastrad * This function is called at the time of loading the display driver to read 676 1.1 riastrad * firmware from a .bin file and copied into a internal memory. 677 1.1 riastrad */ 678 1.7 riastrad void intel_csr_ucode_init(struct drm_i915_private *dev_priv) 679 1.1 riastrad { 680 1.1 riastrad struct intel_csr *csr = &dev_priv->csr; 681 1.1 riastrad 682 1.7 riastrad INIT_WORK(&dev_priv->csr.work, csr_load_work_fn); 683 1.7 riastrad 684 1.7 riastrad if (!HAS_CSR(dev_priv)) 685 1.1 riastrad return; 686 1.1 riastrad 687 1.7 riastrad /* 688 1.7 riastrad * Obtain a runtime pm reference, until CSR is loaded, to avoid entering 689 1.7 riastrad * runtime-suspend. 690 1.7 riastrad * 691 1.7 riastrad * On error, we return with the rpm wakeref held to prevent runtime 692 1.7 riastrad * suspend as runtime suspend *requires* a working CSR for whatever 693 1.7 riastrad * reason. 694 1.7 riastrad */ 695 1.7 riastrad intel_csr_runtime_pm_get(dev_priv); 696 1.7 riastrad 697 1.7 riastrad if (INTEL_GEN(dev_priv) >= 12) { 698 1.7 riastrad csr->fw_path = TGL_CSR_PATH; 699 1.7 riastrad csr->required_version = TGL_CSR_VERSION_REQUIRED; 700 1.7 riastrad /* Allow to load fw via parameter using the last known size */ 701 1.7 riastrad csr->max_fw_size = GEN12_CSR_MAX_FW_SIZE; 702 1.7 riastrad } else if (IS_GEN(dev_priv, 11)) { 703 1.7 riastrad csr->fw_path = ICL_CSR_PATH; 704 1.7 riastrad csr->required_version = ICL_CSR_VERSION_REQUIRED; 705 1.7 riastrad csr->max_fw_size = ICL_CSR_MAX_FW_SIZE; 706 1.7 riastrad } else if (IS_CANNONLAKE(dev_priv)) { 707 1.7 riastrad csr->fw_path = CNL_CSR_PATH; 708 1.7 riastrad csr->required_version = CNL_CSR_VERSION_REQUIRED; 709 1.7 riastrad csr->max_fw_size = CNL_CSR_MAX_FW_SIZE; 710 1.7 riastrad } else if (IS_GEMINILAKE(dev_priv)) { 711 1.7 riastrad csr->fw_path = GLK_CSR_PATH; 712 1.7 riastrad csr->required_version = GLK_CSR_VERSION_REQUIRED; 713 1.7 riastrad csr->max_fw_size = GLK_CSR_MAX_FW_SIZE; 714 1.7 riastrad } else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) { 715 1.7 riastrad csr->fw_path = KBL_CSR_PATH; 716 1.7 riastrad csr->required_version = KBL_CSR_VERSION_REQUIRED; 717 1.7 riastrad csr->max_fw_size = KBL_CSR_MAX_FW_SIZE; 718 1.7 riastrad } else if (IS_SKYLAKE(dev_priv)) { 719 1.7 riastrad csr->fw_path = SKL_CSR_PATH; 720 1.7 riastrad csr->required_version = SKL_CSR_VERSION_REQUIRED; 721 1.7 riastrad csr->max_fw_size = SKL_CSR_MAX_FW_SIZE; 722 1.7 riastrad } else if (IS_BROXTON(dev_priv)) { 723 1.7 riastrad csr->fw_path = BXT_CSR_PATH; 724 1.7 riastrad csr->required_version = BXT_CSR_VERSION_REQUIRED; 725 1.7 riastrad csr->max_fw_size = BXT_CSR_MAX_FW_SIZE; 726 1.7 riastrad } 727 1.7 riastrad 728 1.7 riastrad if (i915_modparams.dmc_firmware_path) { 729 1.7 riastrad if (strlen(i915_modparams.dmc_firmware_path) == 0) { 730 1.7 riastrad csr->fw_path = NULL; 731 1.7 riastrad DRM_INFO("Disabling CSR firmware and runtime PM\n"); 732 1.7 riastrad return; 733 1.7 riastrad } 734 1.7 riastrad 735 1.7 riastrad csr->fw_path = i915_modparams.dmc_firmware_path; 736 1.7 riastrad /* Bypass version check for firmware override. */ 737 1.7 riastrad csr->required_version = 0; 738 1.7 riastrad } 739 1.7 riastrad 740 1.7 riastrad if (csr->fw_path == NULL) { 741 1.7 riastrad DRM_DEBUG_KMS("No known CSR firmware for platform, disabling runtime PM\n"); 742 1.1 riastrad return; 743 1.1 riastrad } 744 1.1 riastrad 745 1.1 riastrad DRM_DEBUG_KMS("Loading %s\n", csr->fw_path); 746 1.7 riastrad schedule_work(&dev_priv->csr.work); 747 1.7 riastrad } 748 1.7 riastrad 749 1.7 riastrad /** 750 1.7 riastrad * intel_csr_ucode_suspend() - prepare CSR firmware before system suspend 751 1.7 riastrad * @dev_priv: i915 drm device 752 1.7 riastrad * 753 1.7 riastrad * Prepare the DMC firmware before entering system suspend. This includes 754 1.7 riastrad * flushing pending work items and releasing any resources acquired during 755 1.7 riastrad * init. 756 1.7 riastrad */ 757 1.7 riastrad void intel_csr_ucode_suspend(struct drm_i915_private *dev_priv) 758 1.7 riastrad { 759 1.7 riastrad if (!HAS_CSR(dev_priv)) 760 1.7 riastrad return; 761 1.7 riastrad 762 1.7 riastrad flush_work(&dev_priv->csr.work); 763 1.7 riastrad 764 1.7 riastrad /* Drop the reference held in case DMC isn't loaded. */ 765 1.7 riastrad if (!dev_priv->csr.dmc_payload) 766 1.7 riastrad intel_csr_runtime_pm_put(dev_priv); 767 1.7 riastrad } 768 1.7 riastrad 769 1.7 riastrad /** 770 1.7 riastrad * intel_csr_ucode_resume() - init CSR firmware during system resume 771 1.7 riastrad * @dev_priv: i915 drm device 772 1.7 riastrad * 773 1.7 riastrad * Reinitialize the DMC firmware during system resume, reacquiring any 774 1.7 riastrad * resources released in intel_csr_ucode_suspend(). 775 1.7 riastrad */ 776 1.7 riastrad void intel_csr_ucode_resume(struct drm_i915_private *dev_priv) 777 1.7 riastrad { 778 1.7 riastrad if (!HAS_CSR(dev_priv)) 779 1.7 riastrad return; 780 1.1 riastrad 781 1.1 riastrad /* 782 1.7 riastrad * Reacquire the reference to keep RPM disabled in case DMC isn't 783 1.7 riastrad * loaded. 784 1.1 riastrad */ 785 1.7 riastrad if (!dev_priv->csr.dmc_payload) 786 1.7 riastrad intel_csr_runtime_pm_get(dev_priv); 787 1.1 riastrad } 788 1.1 riastrad 789 1.1 riastrad /** 790 1.1 riastrad * intel_csr_ucode_fini() - unload the CSR firmware. 791 1.7 riastrad * @dev_priv: i915 drm device. 792 1.1 riastrad * 793 1.7 riastrad * Firmmware unloading includes freeing the internal memory and reset the 794 1.1 riastrad * firmware loading status. 795 1.1 riastrad */ 796 1.7 riastrad void intel_csr_ucode_fini(struct drm_i915_private *dev_priv) 797 1.1 riastrad { 798 1.7 riastrad if (!HAS_CSR(dev_priv)) 799 1.7 riastrad return; 800 1.1 riastrad 801 1.7 riastrad intel_csr_ucode_suspend(dev_priv); 802 1.7 riastrad WARN_ON(dev_priv->csr.wakeref); 803 1.1 riastrad 804 1.1 riastrad kfree(dev_priv->csr.dmc_payload); 805 1.1 riastrad } 806