i915_initial_plane.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2021 Intel Corporation
  4. */
  5. #include <drm/drm_print.h>
  6. #include <drm/intel/display_parent_interface.h>
  7. #include "display/intel_crtc.h"
  8. #include "display/intel_display_types.h"
  9. #include "display/intel_fb.h"
  10. #include "gem/i915_gem_lmem.h"
  11. #include "gem/i915_gem_region.h"
  12. #include "i915_drv.h"
  13. #include "i915_initial_plane.h"
  14. static void i915_initial_plane_vblank_wait(struct drm_crtc *crtc)
  15. {
  16. intel_crtc_wait_for_next_vblank(to_intel_crtc(crtc));
  17. }
  18. static enum intel_memory_type
  19. initial_plane_memory_type(struct drm_i915_private *i915)
  20. {
  21. if (IS_DGFX(i915))
  22. return INTEL_MEMORY_LOCAL;
  23. else if (HAS_LMEMBAR_SMEM_STOLEN(i915))
  24. return INTEL_MEMORY_STOLEN_LOCAL;
  25. else
  26. return INTEL_MEMORY_STOLEN_SYSTEM;
  27. }
  28. static bool
  29. initial_plane_phys(struct drm_i915_private *i915,
  30. struct intel_initial_plane_config *plane_config)
  31. {
  32. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  33. struct intel_memory_region *mem;
  34. enum intel_memory_type mem_type;
  35. bool is_present, is_local;
  36. dma_addr_t dma_addr;
  37. u32 base;
  38. mem_type = initial_plane_memory_type(i915);
  39. mem = intel_memory_region_by_type(i915, mem_type);
  40. if (!mem) {
  41. drm_dbg_kms(&i915->drm,
  42. "Initial plane memory region (type %s) not initialized\n",
  43. intel_memory_type_str(mem_type));
  44. return false;
  45. }
  46. base = round_down(plane_config->base, I915_GTT_MIN_ALIGNMENT);
  47. dma_addr = intel_ggtt_read_entry(&ggtt->vm, base, &is_present, &is_local);
  48. if (!is_present) {
  49. drm_err(&i915->drm, "Initial plane FB PTE not present\n");
  50. return false;
  51. }
  52. if (intel_memory_type_is_local(mem->type) != is_local) {
  53. drm_err(&i915->drm, "Initial plane FB PTE unsuitable for %s\n",
  54. mem->region.name);
  55. return false;
  56. }
  57. if (dma_addr < mem->region.start || dma_addr > mem->region.end) {
  58. drm_err(&i915->drm,
  59. "Initial plane programming using invalid range, dma_addr=%pa (%s [%pa-%pa])\n",
  60. &dma_addr, mem->region.name, &mem->region.start, &mem->region.end);
  61. return false;
  62. }
  63. drm_dbg(&i915->drm, "Using dma_addr=%pa, based on initial plane programming\n",
  64. &dma_addr);
  65. plane_config->phys_base = dma_addr - mem->region.start;
  66. plane_config->mem = mem;
  67. return true;
  68. }
  69. static struct i915_vma *
  70. initial_plane_vma(struct drm_i915_private *i915,
  71. struct intel_initial_plane_config *plane_config)
  72. {
  73. struct intel_memory_region *mem;
  74. struct drm_i915_gem_object *obj;
  75. struct drm_mm_node orig_mm = {};
  76. struct i915_vma *vma;
  77. resource_size_t phys_base;
  78. unsigned int tiling;
  79. u32 base, size;
  80. u64 pinctl;
  81. if (plane_config->size == 0)
  82. return NULL;
  83. if (!initial_plane_phys(i915, plane_config))
  84. return NULL;
  85. phys_base = plane_config->phys_base;
  86. mem = plane_config->mem;
  87. base = round_down(plane_config->base, I915_GTT_MIN_ALIGNMENT);
  88. size = round_up(plane_config->base + plane_config->size,
  89. mem->min_page_size);
  90. size -= base;
  91. /*
  92. * If the FB is too big, just don't use it since fbdev is not very
  93. * important and we should probably use that space with FBC or other
  94. * features.
  95. */
  96. if (IS_ENABLED(CONFIG_FRAMEBUFFER_CONSOLE) &&
  97. mem == i915->mm.stolen_region &&
  98. size * 2 > i915->dsm.usable_size) {
  99. drm_dbg_kms(&i915->drm, "Initial FB size exceeds half of stolen, discarding\n");
  100. return NULL;
  101. }
  102. obj = i915_gem_object_create_region_at(mem, phys_base, size,
  103. I915_BO_ALLOC_USER |
  104. I915_BO_PREALLOC);
  105. if (IS_ERR(obj)) {
  106. drm_dbg_kms(&i915->drm, "Failed to preallocate initial FB in %s\n",
  107. mem->region.name);
  108. return NULL;
  109. }
  110. /*
  111. * Mark it WT ahead of time to avoid changing the
  112. * cache_level during fbdev initialization. The
  113. * unbind there would get stuck waiting for rcu.
  114. */
  115. i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
  116. I915_CACHE_WT : I915_CACHE_NONE);
  117. tiling = intel_fb_modifier_to_tiling(plane_config->fb->base.modifier);
  118. switch (tiling) {
  119. case I915_TILING_NONE:
  120. break;
  121. case I915_TILING_X:
  122. case I915_TILING_Y:
  123. obj->tiling_and_stride =
  124. plane_config->fb->base.pitches[0] |
  125. tiling;
  126. break;
  127. default:
  128. MISSING_CASE(tiling);
  129. goto err_obj;
  130. }
  131. /*
  132. * MTL GOP likes to place the framebuffer high up in ggtt,
  133. * which can cause problems for ggtt_reserve_guc_top().
  134. * Try to pin it to a low ggtt address instead to avoid that.
  135. */
  136. base = 0;
  137. if (base != plane_config->base) {
  138. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  139. int ret;
  140. /*
  141. * Make sure the original and new locations
  142. * can't overlap. That would corrupt the original
  143. * PTEs which are still being used for scanout.
  144. */
  145. ret = i915_gem_gtt_reserve(&ggtt->vm, NULL, &orig_mm,
  146. size, plane_config->base,
  147. I915_COLOR_UNEVICTABLE, PIN_NOEVICT);
  148. if (ret)
  149. goto err_obj;
  150. }
  151. vma = i915_vma_instance(obj, &to_gt(i915)->ggtt->vm, NULL);
  152. if (IS_ERR(vma))
  153. goto err_obj;
  154. retry:
  155. pinctl = PIN_GLOBAL | PIN_OFFSET_FIXED | base;
  156. if (!i915_gem_object_is_lmem(obj))
  157. pinctl |= PIN_MAPPABLE;
  158. if (i915_vma_pin(vma, 0, 0, pinctl)) {
  159. if (drm_mm_node_allocated(&orig_mm)) {
  160. drm_mm_remove_node(&orig_mm);
  161. /*
  162. * Try again, but this time pin
  163. * it to its original location.
  164. */
  165. base = plane_config->base;
  166. goto retry;
  167. }
  168. goto err_obj;
  169. }
  170. if (i915_gem_object_is_tiled(obj) &&
  171. !i915_vma_is_map_and_fenceable(vma))
  172. goto err_obj;
  173. if (drm_mm_node_allocated(&orig_mm))
  174. drm_mm_remove_node(&orig_mm);
  175. drm_dbg_kms(&i915->drm,
  176. "Initial plane fb bound to 0x%x in the ggtt (original 0x%x)\n",
  177. i915_ggtt_offset(vma), plane_config->base);
  178. return vma;
  179. err_obj:
  180. if (drm_mm_node_allocated(&orig_mm))
  181. drm_mm_remove_node(&orig_mm);
  182. i915_gem_object_put(obj);
  183. return NULL;
  184. }
  185. static struct drm_gem_object *
  186. i915_alloc_initial_plane_obj(struct drm_device *drm,
  187. struct intel_initial_plane_config *plane_config)
  188. {
  189. struct drm_i915_private *i915 = to_i915(drm);
  190. struct drm_mode_fb_cmd2 mode_cmd = {};
  191. struct drm_framebuffer *fb = &plane_config->fb->base;
  192. struct i915_vma *vma;
  193. vma = initial_plane_vma(i915, plane_config);
  194. if (!vma)
  195. return NULL;
  196. mode_cmd.pixel_format = fb->format->format;
  197. mode_cmd.width = fb->width;
  198. mode_cmd.height = fb->height;
  199. mode_cmd.pitches[0] = fb->pitches[0];
  200. mode_cmd.modifier[0] = fb->modifier;
  201. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  202. if (intel_framebuffer_init(to_intel_framebuffer(fb),
  203. intel_bo_to_drm_bo(vma->obj),
  204. fb->format, &mode_cmd)) {
  205. drm_dbg_kms(&i915->drm, "intel fb init failed\n");
  206. goto err_vma;
  207. }
  208. plane_config->vma = vma;
  209. return intel_bo_to_drm_bo(vma->obj);
  210. err_vma:
  211. i915_vma_put(vma);
  212. return NULL;
  213. }
  214. static int
  215. i915_initial_plane_setup(struct drm_plane_state *_plane_state,
  216. struct intel_initial_plane_config *plane_config,
  217. struct drm_framebuffer *fb,
  218. struct i915_vma *vma)
  219. {
  220. struct intel_plane_state *plane_state = to_intel_plane_state(_plane_state);
  221. struct drm_i915_private *dev_priv = to_i915(_plane_state->plane->dev);
  222. __i915_vma_pin(vma);
  223. plane_state->ggtt_vma = i915_vma_get(vma);
  224. if (intel_plane_uses_fence(plane_state) &&
  225. i915_vma_pin_fence(vma) == 0 && vma->fence)
  226. plane_state->flags |= PLANE_HAS_FENCE;
  227. plane_state->surf = i915_ggtt_offset(plane_state->ggtt_vma);
  228. if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
  229. dev_priv->preserve_bios_swizzle = true;
  230. return 0;
  231. }
  232. static void i915_plane_config_fini(struct intel_initial_plane_config *plane_config)
  233. {
  234. if (plane_config->vma)
  235. i915_vma_put(plane_config->vma);
  236. }
  237. const struct intel_display_initial_plane_interface i915_display_initial_plane_interface = {
  238. .vblank_wait = i915_initial_plane_vblank_wait,
  239. .alloc_obj = i915_alloc_initial_plane_obj,
  240. .setup = i915_initial_plane_setup,
  241. .config_fini = i915_plane_config_fini,
  242. };