intel_engine_user.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #include <linux/list.h>
  6. #include <linux/list_sort.h>
  7. #include <linux/llist.h>
  8. #include <drm/drm_print.h>
  9. #include "i915_drv.h"
  10. #include "intel_engine.h"
  11. #include "intel_engine_user.h"
  12. #include "intel_gt.h"
  13. #include "uc/intel_guc_submission.h"
  14. struct intel_engine_cs *
  15. intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
  16. {
  17. struct rb_node *p = i915->uabi_engines.rb_node;
  18. while (p) {
  19. struct intel_engine_cs *it =
  20. rb_entry(p, typeof(*it), uabi_node);
  21. if (class < it->uabi_class)
  22. p = p->rb_left;
  23. else if (class > it->uabi_class ||
  24. instance > it->uabi_instance)
  25. p = p->rb_right;
  26. else if (instance < it->uabi_instance)
  27. p = p->rb_left;
  28. else
  29. return it;
  30. }
  31. return NULL;
  32. }
  33. void intel_engine_add_user(struct intel_engine_cs *engine)
  34. {
  35. llist_add(&engine->uabi_llist, &engine->i915->uabi_engines_llist);
  36. }
  37. #define I915_NO_UABI_CLASS ((u16)(-1))
  38. static const u16 uabi_classes[] = {
  39. [RENDER_CLASS] = I915_ENGINE_CLASS_RENDER,
  40. [COPY_ENGINE_CLASS] = I915_ENGINE_CLASS_COPY,
  41. [VIDEO_DECODE_CLASS] = I915_ENGINE_CLASS_VIDEO,
  42. [VIDEO_ENHANCEMENT_CLASS] = I915_ENGINE_CLASS_VIDEO_ENHANCE,
  43. [COMPUTE_CLASS] = I915_ENGINE_CLASS_COMPUTE,
  44. [OTHER_CLASS] = I915_NO_UABI_CLASS, /* Not exposed to users, no uabi class. */
  45. };
  46. static int engine_cmp(void *priv, const struct list_head *A,
  47. const struct list_head *B)
  48. {
  49. const struct intel_engine_cs *a =
  50. container_of(A, typeof(*a), uabi_list);
  51. const struct intel_engine_cs *b =
  52. container_of(B, typeof(*b), uabi_list);
  53. if (uabi_classes[a->class] < uabi_classes[b->class])
  54. return -1;
  55. if (uabi_classes[a->class] > uabi_classes[b->class])
  56. return 1;
  57. if (a->instance < b->instance)
  58. return -1;
  59. if (a->instance > b->instance)
  60. return 1;
  61. return 0;
  62. }
  63. static struct llist_node *get_engines(struct drm_i915_private *i915)
  64. {
  65. return llist_del_all(&i915->uabi_engines_llist);
  66. }
  67. static void sort_engines(struct drm_i915_private *i915,
  68. struct list_head *engines)
  69. {
  70. struct llist_node *pos, *next;
  71. llist_for_each_safe(pos, next, get_engines(i915)) {
  72. struct intel_engine_cs *engine =
  73. container_of(pos, typeof(*engine), uabi_llist);
  74. list_add(&engine->uabi_list, engines);
  75. }
  76. list_sort(NULL, engines, engine_cmp);
  77. }
  78. static void set_scheduler_caps(struct drm_i915_private *i915)
  79. {
  80. static const struct {
  81. u8 engine;
  82. u8 sched;
  83. } map[] = {
  84. #define MAP(x, y) { ilog2(I915_ENGINE_##x), ilog2(I915_SCHEDULER_CAP_##y) }
  85. MAP(HAS_PREEMPTION, PREEMPTION),
  86. MAP(HAS_SEMAPHORES, SEMAPHORES),
  87. MAP(SUPPORTS_STATS, ENGINE_BUSY_STATS),
  88. #undef MAP
  89. };
  90. struct intel_engine_cs *engine;
  91. u32 enabled, disabled;
  92. enabled = 0;
  93. disabled = 0;
  94. for_each_uabi_engine(engine, i915) { /* all engines must agree! */
  95. int i;
  96. if (engine->sched_engine->schedule)
  97. enabled |= (I915_SCHEDULER_CAP_ENABLED |
  98. I915_SCHEDULER_CAP_PRIORITY);
  99. else
  100. disabled |= (I915_SCHEDULER_CAP_ENABLED |
  101. I915_SCHEDULER_CAP_PRIORITY);
  102. if (intel_uc_uses_guc_submission(&engine->gt->uc))
  103. enabled |= I915_SCHEDULER_CAP_STATIC_PRIORITY_MAP;
  104. for (i = 0; i < ARRAY_SIZE(map); i++) {
  105. if (engine->flags & BIT(map[i].engine))
  106. enabled |= BIT(map[i].sched);
  107. else
  108. disabled |= BIT(map[i].sched);
  109. }
  110. }
  111. i915->caps.scheduler = enabled & ~disabled;
  112. if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED))
  113. i915->caps.scheduler = 0;
  114. }
  115. const char *intel_engine_class_repr(u8 class)
  116. {
  117. static const char * const uabi_names[] = {
  118. [RENDER_CLASS] = "rcs",
  119. [COPY_ENGINE_CLASS] = "bcs",
  120. [VIDEO_DECODE_CLASS] = "vcs",
  121. [VIDEO_ENHANCEMENT_CLASS] = "vecs",
  122. [OTHER_CLASS] = "other",
  123. [COMPUTE_CLASS] = "ccs",
  124. };
  125. if (class >= ARRAY_SIZE(uabi_names) || !uabi_names[class])
  126. return "xxx";
  127. return uabi_names[class];
  128. }
  129. struct legacy_ring {
  130. struct intel_gt *gt;
  131. u8 class;
  132. u8 instance;
  133. };
  134. static int legacy_ring_idx(const struct legacy_ring *ring)
  135. {
  136. static const struct {
  137. u8 base, max;
  138. } map[] = {
  139. [RENDER_CLASS] = { RCS0, 1 },
  140. [COPY_ENGINE_CLASS] = { BCS0, 1 },
  141. [VIDEO_DECODE_CLASS] = { VCS0, I915_MAX_VCS },
  142. [VIDEO_ENHANCEMENT_CLASS] = { VECS0, I915_MAX_VECS },
  143. [COMPUTE_CLASS] = { CCS0, I915_MAX_CCS },
  144. };
  145. if (GEM_DEBUG_WARN_ON(ring->class >= ARRAY_SIZE(map)))
  146. return INVALID_ENGINE;
  147. if (GEM_DEBUG_WARN_ON(ring->instance >= map[ring->class].max))
  148. return INVALID_ENGINE;
  149. return map[ring->class].base + ring->instance;
  150. }
  151. static void add_legacy_ring(struct legacy_ring *ring,
  152. struct intel_engine_cs *engine)
  153. {
  154. if (engine->gt != ring->gt || engine->class != ring->class) {
  155. ring->gt = engine->gt;
  156. ring->class = engine->class;
  157. ring->instance = 0;
  158. }
  159. engine->legacy_idx = legacy_ring_idx(ring);
  160. if (engine->legacy_idx != INVALID_ENGINE)
  161. ring->instance++;
  162. }
  163. static void engine_rename(struct intel_engine_cs *engine, const char *name, u16 instance)
  164. {
  165. char old[sizeof(engine->name)];
  166. memcpy(old, engine->name, sizeof(engine->name));
  167. scnprintf(engine->name, sizeof(engine->name), "%s%u", name, instance);
  168. drm_dbg(&engine->i915->drm, "renamed %s to %s\n", old, engine->name);
  169. }
  170. void intel_engines_driver_register(struct drm_i915_private *i915)
  171. {
  172. u16 name_instance, other_instance = 0;
  173. struct legacy_ring ring = {};
  174. struct list_head *it, *next;
  175. struct rb_node **p, *prev;
  176. LIST_HEAD(engines);
  177. sort_engines(i915, &engines);
  178. prev = NULL;
  179. p = &i915->uabi_engines.rb_node;
  180. list_for_each_safe(it, next, &engines) {
  181. struct intel_engine_cs *engine =
  182. container_of(it, typeof(*engine), uabi_list);
  183. if (intel_gt_has_unrecoverable_error(engine->gt))
  184. continue; /* ignore incomplete engines */
  185. GEM_BUG_ON(engine->class >= ARRAY_SIZE(uabi_classes));
  186. engine->uabi_class = uabi_classes[engine->class];
  187. if (engine->uabi_class == I915_NO_UABI_CLASS) {
  188. name_instance = other_instance++;
  189. } else {
  190. GEM_BUG_ON(engine->uabi_class >=
  191. ARRAY_SIZE(i915->engine_uabi_class_count));
  192. name_instance =
  193. i915->engine_uabi_class_count[engine->uabi_class]++;
  194. }
  195. engine->uabi_instance = name_instance;
  196. /*
  197. * Replace the internal name with the final user and log facing
  198. * name.
  199. */
  200. engine_rename(engine,
  201. intel_engine_class_repr(engine->class),
  202. name_instance);
  203. if (engine->uabi_class == I915_NO_UABI_CLASS)
  204. continue;
  205. rb_link_node(&engine->uabi_node, prev, p);
  206. rb_insert_color(&engine->uabi_node, &i915->uabi_engines);
  207. GEM_BUG_ON(intel_engine_lookup_user(i915,
  208. engine->uabi_class,
  209. engine->uabi_instance) != engine);
  210. /* Fix up the mapping to match default execbuf::user_map[] */
  211. add_legacy_ring(&ring, engine);
  212. prev = &engine->uabi_node;
  213. p = &prev->rb_right;
  214. }
  215. if (IS_ENABLED(CONFIG_DRM_I915_SELFTESTS) &&
  216. IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
  217. struct intel_engine_cs *engine;
  218. unsigned int isolation;
  219. int class, inst;
  220. int errors = 0;
  221. for (class = 0; class < ARRAY_SIZE(i915->engine_uabi_class_count); class++) {
  222. for (inst = 0; inst < i915->engine_uabi_class_count[class]; inst++) {
  223. engine = intel_engine_lookup_user(i915,
  224. class, inst);
  225. if (!engine) {
  226. pr_err("UABI engine not found for { class:%d, instance:%d }\n",
  227. class, inst);
  228. errors++;
  229. continue;
  230. }
  231. if (engine->uabi_class != class ||
  232. engine->uabi_instance != inst) {
  233. pr_err("Wrong UABI engine:%s { class:%d, instance:%d } found for { class:%d, instance:%d }\n",
  234. engine->name,
  235. engine->uabi_class,
  236. engine->uabi_instance,
  237. class, inst);
  238. errors++;
  239. continue;
  240. }
  241. }
  242. }
  243. /*
  244. * Make sure that classes with multiple engine instances all
  245. * share the same basic configuration.
  246. */
  247. isolation = intel_engines_has_context_isolation(i915);
  248. for_each_uabi_engine(engine, i915) {
  249. unsigned int bit = BIT(engine->uabi_class);
  250. unsigned int expected = engine->default_state ? bit : 0;
  251. if ((isolation & bit) != expected) {
  252. pr_err("mismatching default context state for class %d on engine %s\n",
  253. engine->uabi_class, engine->name);
  254. errors++;
  255. }
  256. }
  257. if (drm_WARN(&i915->drm, errors,
  258. "Invalid UABI engine mapping found"))
  259. i915->uabi_engines = RB_ROOT;
  260. }
  261. set_scheduler_caps(i915);
  262. }
  263. unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
  264. {
  265. struct intel_engine_cs *engine;
  266. unsigned int which;
  267. which = 0;
  268. for_each_uabi_engine(engine, i915)
  269. if (engine->default_state)
  270. which |= BIT(engine->uabi_class);
  271. return which;
  272. }