intel_wopcm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2017-2019 Intel Corporation
  4. */
  5. #include "intel_wopcm.h"
  6. #include "i915_drv.h"
  7. /**
  8. * DOC: WOPCM Layout
  9. *
  10. * The layout of the WOPCM will be fixed after writing to GuC WOPCM size and
  11. * offset registers whose values are calculated and determined by HuC/GuC
  12. * firmware size and set of hardware requirements/restrictions as shown below:
  13. *
  14. * ::
  15. *
  16. * +=========> +====================+ <== WOPCM Top
  17. * ^ | HW contexts RSVD |
  18. * | +===> +====================+ <== GuC WOPCM Top
  19. * | ^ | |
  20. * | | | |
  21. * | | | |
  22. * | GuC | |
  23. * | WOPCM | |
  24. * | Size +--------------------+
  25. * WOPCM | | GuC FW RSVD |
  26. * | | +--------------------+
  27. * | | | GuC Stack RSVD |
  28. * | | +------------------- +
  29. * | v | GuC WOPCM RSVD |
  30. * | +===> +====================+ <== GuC WOPCM base
  31. * | | WOPCM RSVD |
  32. * | +------------------- + <== HuC Firmware Top
  33. * v | HuC FW |
  34. * +=========> +====================+ <== WOPCM Base
  35. *
  36. * GuC accessible WOPCM starts at GuC WOPCM base and ends at GuC WOPCM top.
  37. * The top part of the WOPCM is reserved for hardware contexts (e.g. RC6
  38. * context).
  39. */
  40. /* Default WOPCM size is 2MB from Gen11, 1MB on previous platforms */
  41. #define GEN11_WOPCM_SIZE SZ_2M
  42. #define GEN9_WOPCM_SIZE SZ_1M
  43. #define MAX_WOPCM_SIZE SZ_8M
  44. /* 16KB WOPCM (RSVD WOPCM) is reserved from HuC firmware top. */
  45. #define WOPCM_RESERVED_SIZE SZ_16K
  46. /* 16KB reserved at the beginning of GuC WOPCM. */
  47. #define GUC_WOPCM_RESERVED SZ_16K
  48. /* 8KB from GUC_WOPCM_RESERVED is reserved for GuC stack. */
  49. #define GUC_WOPCM_STACK_RESERVED SZ_8K
  50. /* GuC WOPCM Offset value needs to be aligned to 16KB. */
  51. #define GUC_WOPCM_OFFSET_ALIGNMENT (1UL << GUC_WOPCM_OFFSET_SHIFT)
  52. /* 24KB at the end of WOPCM is reserved for RC6 CTX on BXT. */
  53. #define BXT_WOPCM_RC6_CTX_RESERVED (SZ_16K + SZ_8K)
  54. /* 36KB WOPCM reserved at the end of WOPCM on ICL. */
  55. #define ICL_WOPCM_HW_CTX_RESERVED (SZ_32K + SZ_4K)
  56. /* 128KB from GUC_WOPCM_RESERVED is reserved for FW on Gen9. */
  57. #define GEN9_GUC_FW_RESERVED SZ_128K
  58. #define GEN9_GUC_WOPCM_OFFSET (GUC_WOPCM_RESERVED + GEN9_GUC_FW_RESERVED)
  59. static inline struct drm_i915_private *wopcm_to_i915(struct intel_wopcm *wopcm)
  60. {
  61. return container_of(wopcm, struct drm_i915_private, wopcm);
  62. }
  63. /**
  64. * intel_wopcm_init_early() - Early initialization of the WOPCM.
  65. * @wopcm: pointer to intel_wopcm.
  66. *
  67. * Setup the size of WOPCM which will be used by later on WOPCM partitioning.
  68. */
  69. void intel_wopcm_init_early(struct intel_wopcm *wopcm)
  70. {
  71. struct drm_i915_private *i915 = wopcm_to_i915(wopcm);
  72. if (!HAS_GT_UC(i915))
  73. return;
  74. if (GRAPHICS_VER(i915) >= 11)
  75. wopcm->size = GEN11_WOPCM_SIZE;
  76. else
  77. wopcm->size = GEN9_WOPCM_SIZE;
  78. drm_dbg(&i915->drm, "WOPCM: %uK\n", wopcm->size / 1024);
  79. }
  80. static u32 context_reserved_size(struct drm_i915_private *i915)
  81. {
  82. if (IS_GEN9_LP(i915))
  83. return BXT_WOPCM_RC6_CTX_RESERVED;
  84. else if (GRAPHICS_VER(i915) >= 11)
  85. return ICL_WOPCM_HW_CTX_RESERVED;
  86. else
  87. return 0;
  88. }
  89. static bool gen9_check_dword_gap(struct drm_i915_private *i915,
  90. u32 guc_wopcm_base, u32 guc_wopcm_size)
  91. {
  92. u32 offset;
  93. /*
  94. * GuC WOPCM size shall be at least a dword larger than the offset from
  95. * WOPCM base (GuC WOPCM offset from WOPCM base + GEN9_GUC_WOPCM_OFFSET)
  96. * due to hardware limitation on Gen9.
  97. */
  98. offset = guc_wopcm_base + GEN9_GUC_WOPCM_OFFSET;
  99. if (offset > guc_wopcm_size ||
  100. (guc_wopcm_size - offset) < sizeof(u32)) {
  101. drm_err(&i915->drm,
  102. "WOPCM: invalid GuC region size: %uK < %uK\n",
  103. guc_wopcm_size / SZ_1K,
  104. (u32)(offset + sizeof(u32)) / SZ_1K);
  105. return false;
  106. }
  107. return true;
  108. }
  109. static bool gen9_check_huc_fw_fits(struct drm_i915_private *i915,
  110. u32 guc_wopcm_size, u32 huc_fw_size)
  111. {
  112. /*
  113. * On Gen9, hardware requires the total available GuC WOPCM
  114. * size to be larger than or equal to HuC firmware size. Otherwise,
  115. * firmware uploading would fail.
  116. */
  117. if (huc_fw_size > guc_wopcm_size - GUC_WOPCM_RESERVED) {
  118. drm_err(&i915->drm, "WOPCM: no space for %s: %uK < %uK\n",
  119. intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_HUC),
  120. (guc_wopcm_size - GUC_WOPCM_RESERVED) / SZ_1K,
  121. huc_fw_size / 1024);
  122. return false;
  123. }
  124. return true;
  125. }
  126. static bool check_hw_restrictions(struct drm_i915_private *i915,
  127. u32 guc_wopcm_base, u32 guc_wopcm_size,
  128. u32 huc_fw_size)
  129. {
  130. if (GRAPHICS_VER(i915) == 9 && !gen9_check_dword_gap(i915, guc_wopcm_base,
  131. guc_wopcm_size))
  132. return false;
  133. if (GRAPHICS_VER(i915) == 9 &&
  134. !gen9_check_huc_fw_fits(i915, guc_wopcm_size, huc_fw_size))
  135. return false;
  136. return true;
  137. }
  138. static bool __check_layout(struct drm_i915_private *i915, u32 wopcm_size,
  139. u32 guc_wopcm_base, u32 guc_wopcm_size,
  140. u32 guc_fw_size, u32 huc_fw_size)
  141. {
  142. const u32 ctx_rsvd = context_reserved_size(i915);
  143. u32 size;
  144. size = wopcm_size - ctx_rsvd;
  145. if (unlikely(range_overflows(guc_wopcm_base, guc_wopcm_size, size))) {
  146. drm_err(&i915->drm,
  147. "WOPCM: invalid GuC region layout: %uK + %uK > %uK\n",
  148. guc_wopcm_base / SZ_1K, guc_wopcm_size / SZ_1K,
  149. size / SZ_1K);
  150. return false;
  151. }
  152. size = guc_fw_size + GUC_WOPCM_RESERVED + GUC_WOPCM_STACK_RESERVED;
  153. if (unlikely(guc_wopcm_size < size)) {
  154. drm_err(&i915->drm, "WOPCM: no space for %s: %uK < %uK\n",
  155. intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_GUC),
  156. guc_wopcm_size / SZ_1K, size / SZ_1K);
  157. return false;
  158. }
  159. size = huc_fw_size + WOPCM_RESERVED_SIZE;
  160. if (unlikely(guc_wopcm_base < size)) {
  161. drm_err(&i915->drm, "WOPCM: no space for %s: %uK < %uK\n",
  162. intel_uc_fw_type_repr(INTEL_UC_FW_TYPE_HUC),
  163. guc_wopcm_base / SZ_1K, size / SZ_1K);
  164. return false;
  165. }
  166. return check_hw_restrictions(i915, guc_wopcm_base, guc_wopcm_size,
  167. huc_fw_size);
  168. }
  169. static bool __wopcm_regs_locked(struct intel_uncore *uncore,
  170. u32 *guc_wopcm_base, u32 *guc_wopcm_size)
  171. {
  172. u32 reg_base = intel_uncore_read(uncore, DMA_GUC_WOPCM_OFFSET);
  173. u32 reg_size = intel_uncore_read(uncore, GUC_WOPCM_SIZE);
  174. if (!(reg_size & GUC_WOPCM_SIZE_LOCKED) ||
  175. !(reg_base & GUC_WOPCM_OFFSET_VALID))
  176. return false;
  177. *guc_wopcm_base = reg_base & GUC_WOPCM_OFFSET_MASK;
  178. *guc_wopcm_size = reg_size & GUC_WOPCM_SIZE_MASK;
  179. return true;
  180. }
  181. static bool __wopcm_regs_writable(struct intel_uncore *uncore)
  182. {
  183. if (!HAS_GUC_DEPRIVILEGE(uncore->i915))
  184. return true;
  185. return intel_uncore_read(uncore, GUC_SHIM_CONTROL2) & GUC_IS_PRIVILEGED;
  186. }
  187. /**
  188. * intel_wopcm_init() - Initialize the WOPCM structure.
  189. * @wopcm: pointer to intel_wopcm.
  190. *
  191. * This function will partition WOPCM space based on GuC and HuC firmware sizes
  192. * and will allocate max remaining for use by GuC. This function will also
  193. * enforce platform dependent hardware restrictions on GuC WOPCM offset and
  194. * size. It will fail the WOPCM init if any of these checks fail, so that the
  195. * following WOPCM registers setup and GuC firmware uploading would be aborted.
  196. */
  197. void intel_wopcm_init(struct intel_wopcm *wopcm)
  198. {
  199. struct drm_i915_private *i915 = wopcm_to_i915(wopcm);
  200. struct intel_gt *gt = to_gt(i915);
  201. u32 guc_fw_size = intel_uc_fw_get_upload_size(&gt->uc.guc.fw);
  202. u32 huc_fw_size = intel_uc_fw_get_upload_size(&gt->uc.huc.fw);
  203. u32 ctx_rsvd = context_reserved_size(i915);
  204. u32 wopcm_size = wopcm->size;
  205. u32 guc_wopcm_base;
  206. u32 guc_wopcm_size;
  207. if (!guc_fw_size)
  208. return;
  209. GEM_BUG_ON(!wopcm_size);
  210. GEM_BUG_ON(wopcm->guc.base);
  211. GEM_BUG_ON(wopcm->guc.size);
  212. GEM_BUG_ON(guc_fw_size >= wopcm_size);
  213. GEM_BUG_ON(huc_fw_size >= wopcm_size);
  214. GEM_BUG_ON(ctx_rsvd + WOPCM_RESERVED_SIZE >= wopcm_size);
  215. if (i915_inject_probe_failure(i915))
  216. return;
  217. if (__wopcm_regs_locked(gt->uncore, &guc_wopcm_base, &guc_wopcm_size)) {
  218. drm_dbg(&i915->drm, "GuC WOPCM is already locked [%uK, %uK)\n",
  219. guc_wopcm_base / SZ_1K, guc_wopcm_size / SZ_1K);
  220. /*
  221. * Note that to keep things simple (i.e. avoid different
  222. * defines per platform) our WOPCM math doesn't always use the
  223. * actual WOPCM size, but a value that is less or equal to it.
  224. * This is perfectly fine when i915 programs the registers, but
  225. * on platforms with GuC deprivilege the registers are not
  226. * writable from i915 and are instead pre-programmed by the
  227. * bios/IFWI, so there might be a mismatch of sizes.
  228. * Instead of handling the size difference, we trust that the
  229. * programmed values make sense and disable the relevant check
  230. * by using the maximum possible WOPCM size in the verification
  231. * math. In the extremely unlikely case that the registers
  232. * were pre-programmed with an invalid value, we will still
  233. * gracefully fail later during the GuC/HuC dma.
  234. */
  235. if (!__wopcm_regs_writable(gt->uncore))
  236. wopcm_size = MAX_WOPCM_SIZE;
  237. goto check;
  238. }
  239. /*
  240. * Aligned value of guc_wopcm_base will determine available WOPCM space
  241. * for HuC firmware and mandatory reserved area.
  242. */
  243. guc_wopcm_base = huc_fw_size + WOPCM_RESERVED_SIZE;
  244. guc_wopcm_base = ALIGN(guc_wopcm_base, GUC_WOPCM_OFFSET_ALIGNMENT);
  245. /*
  246. * Need to clamp guc_wopcm_base now to make sure the following math is
  247. * correct. Formal check of whole WOPCM layout will be done below.
  248. */
  249. guc_wopcm_base = min(guc_wopcm_base, wopcm_size - ctx_rsvd);
  250. /* Aligned remainings of usable WOPCM space can be assigned to GuC. */
  251. guc_wopcm_size = wopcm_size - ctx_rsvd - guc_wopcm_base;
  252. guc_wopcm_size &= GUC_WOPCM_SIZE_MASK;
  253. drm_dbg(&i915->drm, "Calculated GuC WOPCM [%uK, %uK)\n",
  254. guc_wopcm_base / SZ_1K, guc_wopcm_size / SZ_1K);
  255. check:
  256. if (__check_layout(i915, wopcm_size, guc_wopcm_base, guc_wopcm_size,
  257. guc_fw_size, huc_fw_size)) {
  258. wopcm->guc.base = guc_wopcm_base;
  259. wopcm->guc.size = guc_wopcm_size;
  260. GEM_BUG_ON(!wopcm->guc.base);
  261. GEM_BUG_ON(!wopcm->guc.size);
  262. }
  263. }