intel_wopcm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. *
  4. * Copyright © 2017-2018 Intel Corporation
  5. */
  6. #include "intel_wopcm.h"
  7. #include "i915_drv.h"
  8. /**
  9. * DOC: WOPCM Layout
  10. *
  11. * The layout of the WOPCM will be fixed after writing to GuC WOPCM size and
  12. * offset registers whose values are calculated and determined by HuC/GuC
  13. * firmware size and set of hardware requirements/restrictions as shown below:
  14. *
  15. * ::
  16. *
  17. * +=========> +====================+ <== WOPCM Top
  18. * ^ | HW contexts RSVD |
  19. * | +===> +====================+ <== GuC WOPCM Top
  20. * | ^ | |
  21. * | | | |
  22. * | | | |
  23. * | GuC | |
  24. * | WOPCM | |
  25. * | Size +--------------------+
  26. * WOPCM | | GuC FW RSVD |
  27. * | | +--------------------+
  28. * | | | GuC Stack RSVD |
  29. * | | +------------------- +
  30. * | v | GuC WOPCM RSVD |
  31. * | +===> +====================+ <== GuC WOPCM base
  32. * | | WOPCM RSVD |
  33. * | +------------------- + <== HuC Firmware Top
  34. * v | HuC FW |
  35. * +=========> +====================+ <== WOPCM Base
  36. *
  37. * GuC accessible WOPCM starts at GuC WOPCM base and ends at GuC WOPCM top.
  38. * The top part of the WOPCM is reserved for hardware contexts (e.g. RC6
  39. * context).
  40. */
  41. /* Default WOPCM size 1MB. */
  42. #define GEN9_WOPCM_SIZE (1024 * 1024)
  43. /* 16KB WOPCM (RSVD WOPCM) is reserved from HuC firmware top. */
  44. #define WOPCM_RESERVED_SIZE (16 * 1024)
  45. /* 16KB reserved at the beginning of GuC WOPCM. */
  46. #define GUC_WOPCM_RESERVED (16 * 1024)
  47. /* 8KB from GUC_WOPCM_RESERVED is reserved for GuC stack. */
  48. #define GUC_WOPCM_STACK_RESERVED (8 * 1024)
  49. /* GuC WOPCM Offset value needs to be aligned to 16KB. */
  50. #define GUC_WOPCM_OFFSET_ALIGNMENT (1UL << GUC_WOPCM_OFFSET_SHIFT)
  51. /* 24KB at the end of WOPCM is reserved for RC6 CTX on BXT. */
  52. #define BXT_WOPCM_RC6_CTX_RESERVED (24 * 1024)
  53. /* 36KB WOPCM reserved at the end of WOPCM on CNL. */
  54. #define CNL_WOPCM_HW_CTX_RESERVED (36 * 1024)
  55. /* 128KB from GUC_WOPCM_RESERVED is reserved for FW on Gen9. */
  56. #define GEN9_GUC_FW_RESERVED (128 * 1024)
  57. #define GEN9_GUC_WOPCM_OFFSET (GUC_WOPCM_RESERVED + GEN9_GUC_FW_RESERVED)
  58. /**
  59. * intel_wopcm_init_early() - Early initialization of the WOPCM.
  60. * @wopcm: pointer to intel_wopcm.
  61. *
  62. * Setup the size of WOPCM which will be used by later on WOPCM partitioning.
  63. */
  64. void intel_wopcm_init_early(struct intel_wopcm *wopcm)
  65. {
  66. wopcm->size = GEN9_WOPCM_SIZE;
  67. DRM_DEBUG_DRIVER("WOPCM size: %uKiB\n", wopcm->size / 1024);
  68. }
  69. static inline u32 context_reserved_size(struct drm_i915_private *i915)
  70. {
  71. if (IS_GEN9_LP(i915))
  72. return BXT_WOPCM_RC6_CTX_RESERVED;
  73. else if (INTEL_GEN(i915) >= 10)
  74. return CNL_WOPCM_HW_CTX_RESERVED;
  75. else
  76. return 0;
  77. }
  78. static inline int gen9_check_dword_gap(u32 guc_wopcm_base, u32 guc_wopcm_size)
  79. {
  80. u32 offset;
  81. /*
  82. * GuC WOPCM size shall be at least a dword larger than the offset from
  83. * WOPCM base (GuC WOPCM offset from WOPCM base + GEN9_GUC_WOPCM_OFFSET)
  84. * due to hardware limitation on Gen9.
  85. */
  86. offset = guc_wopcm_base + GEN9_GUC_WOPCM_OFFSET;
  87. if (offset > guc_wopcm_size ||
  88. (guc_wopcm_size - offset) < sizeof(u32)) {
  89. DRM_ERROR("GuC WOPCM size %uKiB is too small. %uKiB needed.\n",
  90. guc_wopcm_size / 1024,
  91. (u32)(offset + sizeof(u32)) / 1024);
  92. return -E2BIG;
  93. }
  94. return 0;
  95. }
  96. static inline int gen9_check_huc_fw_fits(u32 guc_wopcm_size, u32 huc_fw_size)
  97. {
  98. /*
  99. * On Gen9 & CNL A0, hardware requires the total available GuC WOPCM
  100. * size to be larger than or equal to HuC firmware size. Otherwise,
  101. * firmware uploading would fail.
  102. */
  103. if (huc_fw_size > guc_wopcm_size - GUC_WOPCM_RESERVED) {
  104. DRM_ERROR("HuC FW (%uKiB) won't fit in GuC WOPCM (%uKiB).\n",
  105. huc_fw_size / 1024,
  106. (guc_wopcm_size - GUC_WOPCM_RESERVED) / 1024);
  107. return -E2BIG;
  108. }
  109. return 0;
  110. }
  111. static inline int check_hw_restriction(struct drm_i915_private *i915,
  112. u32 guc_wopcm_base, u32 guc_wopcm_size,
  113. u32 huc_fw_size)
  114. {
  115. int err = 0;
  116. if (IS_GEN9(i915))
  117. err = gen9_check_dword_gap(guc_wopcm_base, guc_wopcm_size);
  118. if (!err &&
  119. (IS_GEN9(i915) || IS_CNL_REVID(i915, CNL_REVID_A0, CNL_REVID_A0)))
  120. err = gen9_check_huc_fw_fits(guc_wopcm_size, huc_fw_size);
  121. return err;
  122. }
  123. /**
  124. * intel_wopcm_init() - Initialize the WOPCM structure.
  125. * @wopcm: pointer to intel_wopcm.
  126. *
  127. * This function will partition WOPCM space based on GuC and HuC firmware sizes
  128. * and will allocate max remaining for use by GuC. This function will also
  129. * enforce platform dependent hardware restrictions on GuC WOPCM offset and
  130. * size. It will fail the WOPCM init if any of these checks were failed, so that
  131. * the following GuC firmware uploading would be aborted.
  132. *
  133. * Return: 0 on success, non-zero error code on failure.
  134. */
  135. int intel_wopcm_init(struct intel_wopcm *wopcm)
  136. {
  137. struct drm_i915_private *i915 = wopcm_to_i915(wopcm);
  138. u32 guc_fw_size = intel_uc_fw_get_upload_size(&i915->guc.fw);
  139. u32 huc_fw_size = intel_uc_fw_get_upload_size(&i915->huc.fw);
  140. u32 ctx_rsvd = context_reserved_size(i915);
  141. u32 guc_wopcm_base;
  142. u32 guc_wopcm_size;
  143. u32 guc_wopcm_rsvd;
  144. int err;
  145. if (!USES_GUC(dev_priv))
  146. return 0;
  147. GEM_BUG_ON(!wopcm->size);
  148. if (i915_inject_load_failure())
  149. return -E2BIG;
  150. if (guc_fw_size >= wopcm->size) {
  151. DRM_ERROR("GuC FW (%uKiB) is too big to fit in WOPCM.",
  152. guc_fw_size / 1024);
  153. return -E2BIG;
  154. }
  155. if (huc_fw_size >= wopcm->size) {
  156. DRM_ERROR("HuC FW (%uKiB) is too big to fit in WOPCM.",
  157. huc_fw_size / 1024);
  158. return -E2BIG;
  159. }
  160. guc_wopcm_base = ALIGN(huc_fw_size + WOPCM_RESERVED_SIZE,
  161. GUC_WOPCM_OFFSET_ALIGNMENT);
  162. if ((guc_wopcm_base + ctx_rsvd) >= wopcm->size) {
  163. DRM_ERROR("GuC WOPCM base (%uKiB) is too big.\n",
  164. guc_wopcm_base / 1024);
  165. return -E2BIG;
  166. }
  167. guc_wopcm_size = wopcm->size - guc_wopcm_base - ctx_rsvd;
  168. guc_wopcm_size &= GUC_WOPCM_SIZE_MASK;
  169. DRM_DEBUG_DRIVER("Calculated GuC WOPCM Region: [%uKiB, %uKiB)\n",
  170. guc_wopcm_base / 1024, guc_wopcm_size / 1024);
  171. guc_wopcm_rsvd = GUC_WOPCM_RESERVED + GUC_WOPCM_STACK_RESERVED;
  172. if ((guc_fw_size + guc_wopcm_rsvd) > guc_wopcm_size) {
  173. DRM_ERROR("Need %uKiB WOPCM for GuC, %uKiB available.\n",
  174. (guc_fw_size + guc_wopcm_rsvd) / 1024,
  175. guc_wopcm_size / 1024);
  176. return -E2BIG;
  177. }
  178. err = check_hw_restriction(i915, guc_wopcm_base, guc_wopcm_size,
  179. huc_fw_size);
  180. if (err)
  181. return err;
  182. wopcm->guc.base = guc_wopcm_base;
  183. wopcm->guc.size = guc_wopcm_size;
  184. return 0;
  185. }
  186. static inline int write_and_verify(struct drm_i915_private *dev_priv,
  187. i915_reg_t reg, u32 val, u32 mask,
  188. u32 locked_bit)
  189. {
  190. u32 reg_val;
  191. GEM_BUG_ON(val & ~mask);
  192. I915_WRITE(reg, val);
  193. reg_val = I915_READ(reg);
  194. return (reg_val & mask) != (val | locked_bit) ? -EIO : 0;
  195. }
  196. /**
  197. * intel_wopcm_init_hw() - Setup GuC WOPCM registers.
  198. * @wopcm: pointer to intel_wopcm.
  199. *
  200. * Setup the GuC WOPCM size and offset registers with the calculated values. It
  201. * will verify the register values to make sure the registers are locked with
  202. * correct values.
  203. *
  204. * Return: 0 on success. -EIO if registers were locked with incorrect values.
  205. */
  206. int intel_wopcm_init_hw(struct intel_wopcm *wopcm)
  207. {
  208. struct drm_i915_private *dev_priv = wopcm_to_i915(wopcm);
  209. u32 huc_agent;
  210. u32 mask;
  211. int err;
  212. if (!USES_GUC(dev_priv))
  213. return 0;
  214. GEM_BUG_ON(!HAS_GUC(dev_priv));
  215. GEM_BUG_ON(!wopcm->guc.size);
  216. GEM_BUG_ON(!wopcm->guc.base);
  217. err = write_and_verify(dev_priv, GUC_WOPCM_SIZE, wopcm->guc.size,
  218. GUC_WOPCM_SIZE_MASK | GUC_WOPCM_SIZE_LOCKED,
  219. GUC_WOPCM_SIZE_LOCKED);
  220. if (err)
  221. goto err_out;
  222. huc_agent = USES_HUC(dev_priv) ? HUC_LOADING_AGENT_GUC : 0;
  223. mask = GUC_WOPCM_OFFSET_MASK | GUC_WOPCM_OFFSET_VALID | huc_agent;
  224. err = write_and_verify(dev_priv, DMA_GUC_WOPCM_OFFSET,
  225. wopcm->guc.base | huc_agent, mask,
  226. GUC_WOPCM_OFFSET_VALID);
  227. if (err)
  228. goto err_out;
  229. return 0;
  230. err_out:
  231. DRM_ERROR("Failed to init WOPCM registers:\n");
  232. DRM_ERROR("DMA_GUC_WOPCM_OFFSET=%#x\n",
  233. I915_READ(DMA_GUC_WOPCM_OFFSET));
  234. DRM_ERROR("GUC_WOPCM_SIZE=%#x\n", I915_READ(GUC_WOPCM_SIZE));
  235. return err;
  236. }