Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/i915/guc: Check the locking status of GuC WOPCM registers

GuC WOPCM registers are write-once registers. Current driver code accesses
these registers without checking the accessibility to these registers which
will lead to unpredictable driver behaviors if these registers were touch
by other components (such as faulty BIOS code).

This patch moves the GuC WOPCM registers updating code into intel_wopcm.c
and adds check before and after the update to GuC WOPCM registers so that
we can make sure the driver is in a known state after writing to these
write-once registers.

v6:
- Made sure module reloading won't bug the kernel while doing
locking status checking

v7:
- Fixed patch format issues

v8:
- Fixed coding style issue on register lock bit macro definition (Sagar)

v9:
- Avoided to use redundant !! to cast uint to bool (Chris)
- Return error code instead of GEM_BUG_ON for locked with invalid register
values case (Sagar)
- Updated guc_wopcm_hw_init to use guc_wopcm as first parameter (Michal)
- Added code to set and validate the HuC_LOADING_AGENT_GUC bit in GuC
WOPCM offset register based on the presence of HuC firmware (Michal)
- Use bit fields instead of macros for GuC WOPCM flags (Michal)

v10:
- Refined variable names, removed redundant comments (Joonas)
- Introduced lockable_reg to handle the write once register write and
propagate the write error to caller (Joonas)
- Used lockable_reg abstraction to avoid locking bit check on generic
i915_reg_t (Michal)
- Added log message for error paths (Michal)
- Removed hw_updated flag and only relies on real hardware status

v11:
- Replaced lockable_reg with simplified function (Michal)
- Used new macros for locking bits of WOPCM size/offset registers instead
of using BIT(0) directly (Michal)
- use intel_wopcm_init_hw() called from intel_gem_init_hw() to do GuC
WOPCM register setup instead of calling from intel_uc_init_hw() (Michal)

v12:
- Updated function kernel-doc to align with code changes (Michal)
- Updated code to use wopcm pointer directly (Michal)

v13:
- Updated the ordering of s-o-b/cc/r-b tags (Sagar)

BSpec: 10875, 10833

Signed-off-by: Jackie Li <yaodong.li@intel.com>
Cc: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Reviewed-by: Michal Wajdeczko <michal.wajdeczko@intel.com> (v11)
Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com> (v12)
Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Signed-off-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/1520987574-19351-5-git-send-email-yaodong.li@intel.com

authored by

Jackie Li and committed by
Joonas Lahtinen
f08e2035 96c83d35

+74 -5
+6
drivers/gpu/drm/i915/i915_gem.c
··· 5137 5137 goto out; 5138 5138 } 5139 5139 5140 + ret = intel_wopcm_init_hw(&dev_priv->wopcm); 5141 + if (ret) { 5142 + DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); 5143 + goto out; 5144 + } 5145 + 5140 5146 /* We can't enable contexts until all firmware is loaded */ 5141 5147 ret = intel_uc_init_hw(dev_priv); 5142 5148 if (ret) {
+3
drivers/gpu/drm/i915/intel_guc_reg.h
··· 66 66 #define UOS_MOVE (1<<4) 67 67 #define START_DMA (1<<0) 68 68 #define DMA_GUC_WOPCM_OFFSET _MMIO(0xc340) 69 + #define GUC_WOPCM_OFFSET_VALID (1<<0) 69 70 #define HUC_LOADING_AGENT_VCR (0<<1) 70 71 #define HUC_LOADING_AGENT_GUC (1<<1) 71 72 #define GUC_WOPCM_OFFSET_SHIFT 14 73 + #define GUC_WOPCM_OFFSET_MASK (0x3ffff << GUC_WOPCM_OFFSET_SHIFT) 72 74 #define GUC_MAX_IDLE_COUNT _MMIO(0xC3E4) 73 75 74 76 #define HUC_STATUS2 _MMIO(0xD3B0) 75 77 #define HUC_FW_VERIFIED (1<<7) 76 78 77 79 #define GUC_WOPCM_SIZE _MMIO(0xc050) 80 + #define GUC_WOPCM_SIZE_LOCKED (1<<0) 78 81 #define GUC_WOPCM_SIZE_SHIFT 12 79 82 #define GUC_WOPCM_SIZE_MASK (0xfffff << GUC_WOPCM_SIZE_SHIFT) 80 83
-5
drivers/gpu/drm/i915/intel_uc.c
··· 367 367 368 368 gen9_reset_guc_interrupts(dev_priv); 369 369 370 - /* init WOPCM */ 371 - I915_WRITE(GUC_WOPCM_SIZE, dev_priv->wopcm.guc.size); 372 - I915_WRITE(DMA_GUC_WOPCM_OFFSET, 373 - dev_priv->wopcm.guc.base | HUC_LOADING_AGENT_GUC); 374 - 375 370 /* WaEnableuKernelHeaderValidFix:skl */ 376 371 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */ 377 372 if (IS_GEN9(dev_priv))
+64
drivers/gpu/drm/i915/intel_wopcm.c
··· 207 207 208 208 return 0; 209 209 } 210 + 211 + static inline int write_and_verify(struct drm_i915_private *dev_priv, 212 + i915_reg_t reg, u32 val, u32 mask, 213 + u32 locked_bit) 214 + { 215 + u32 reg_val; 216 + 217 + GEM_BUG_ON(val & ~mask); 218 + 219 + I915_WRITE(reg, val); 220 + 221 + reg_val = I915_READ(reg); 222 + 223 + return (reg_val & mask) != (val | locked_bit) ? -EIO : 0; 224 + } 225 + 226 + /** 227 + * intel_wopcm_init_hw() - Setup GuC WOPCM registers. 228 + * @wopcm: pointer to intel_wopcm. 229 + * 230 + * Setup the GuC WOPCM size and offset registers with the calculated values. It 231 + * will verify the register values to make sure the registers are locked with 232 + * correct values. 233 + * 234 + * Return: 0 on success. -EIO if registers were locked with incorrect values. 235 + */ 236 + int intel_wopcm_init_hw(struct intel_wopcm *wopcm) 237 + { 238 + struct drm_i915_private *dev_priv = wopcm_to_i915(wopcm); 239 + u32 huc_agent; 240 + u32 mask; 241 + int err; 242 + 243 + if (!USES_GUC(dev_priv)) 244 + return 0; 245 + 246 + GEM_BUG_ON(!HAS_GUC(dev_priv)); 247 + GEM_BUG_ON(!wopcm->guc.size); 248 + GEM_BUG_ON(!wopcm->guc.base); 249 + 250 + err = write_and_verify(dev_priv, GUC_WOPCM_SIZE, wopcm->guc.size, 251 + GUC_WOPCM_SIZE_MASK | GUC_WOPCM_SIZE_LOCKED, 252 + GUC_WOPCM_SIZE_LOCKED); 253 + if (err) 254 + goto err_out; 255 + 256 + huc_agent = USES_HUC(dev_priv) ? HUC_LOADING_AGENT_GUC : 0; 257 + mask = GUC_WOPCM_OFFSET_MASK | GUC_WOPCM_OFFSET_VALID | huc_agent; 258 + err = write_and_verify(dev_priv, DMA_GUC_WOPCM_OFFSET, 259 + wopcm->guc.base | huc_agent, mask, 260 + GUC_WOPCM_OFFSET_VALID); 261 + if (err) 262 + goto err_out; 263 + 264 + return 0; 265 + 266 + err_out: 267 + DRM_ERROR("Failed to init WOPCM registers:\n"); 268 + DRM_ERROR("DMA_GUC_WOPCM_OFFSET=%#x\n", 269 + I915_READ(DMA_GUC_WOPCM_OFFSET)); 270 + DRM_ERROR("GUC_WOPCM_SIZE=%#x\n", I915_READ(GUC_WOPCM_SIZE)); 271 + 272 + return err; 273 + }
+1
drivers/gpu/drm/i915/intel_wopcm.h
··· 26 26 27 27 void intel_wopcm_init_early(struct intel_wopcm *wopcm); 28 28 int intel_wopcm_init(struct intel_wopcm *wopcm); 29 + int intel_wopcm_init_hw(struct intel_wopcm *wopcm); 29 30 30 31 #endif