intel_ring.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #ifndef INTEL_RING_H
  6. #define INTEL_RING_H
  7. #include "i915_gem.h" /* GEM_BUG_ON */
  8. #include "i915_request.h"
  9. #include "intel_ring_types.h"
  10. struct intel_engine_cs;
  11. struct intel_ring *
  12. intel_engine_create_ring(struct intel_engine_cs *engine, int size);
  13. u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords);
  14. unsigned int intel_ring_update_space(struct intel_ring *ring);
  15. void __intel_ring_pin(struct intel_ring *ring);
  16. int intel_ring_pin(struct intel_ring *ring, struct i915_gem_ww_ctx *ww);
  17. void intel_ring_unpin(struct intel_ring *ring);
  18. void intel_ring_reset(struct intel_ring *ring, u32 tail);
  19. void intel_ring_free(struct kref *ref);
  20. static inline struct intel_ring *intel_ring_get(struct intel_ring *ring)
  21. {
  22. kref_get(&ring->ref);
  23. return ring;
  24. }
  25. static inline void intel_ring_put(struct intel_ring *ring)
  26. {
  27. kref_put(&ring->ref, intel_ring_free);
  28. }
  29. static inline void intel_ring_advance(struct i915_request *rq, u32 *cs)
  30. {
  31. /* Dummy function.
  32. *
  33. * This serves as a placeholder in the code so that the reader
  34. * can compare against the preceding intel_ring_begin() and
  35. * check that the number of dwords emitted matches the space
  36. * reserved for the command packet (i.e. the value passed to
  37. * intel_ring_begin()).
  38. */
  39. GEM_BUG_ON((rq->ring->vaddr + rq->ring->emit) != cs);
  40. GEM_BUG_ON(!IS_ALIGNED(rq->ring->emit, 8)); /* RING_TAIL qword align */
  41. }
  42. static inline u32 intel_ring_wrap(const struct intel_ring *ring, u32 pos)
  43. {
  44. return pos & (ring->size - 1);
  45. }
  46. static inline int intel_ring_direction(const struct intel_ring *ring,
  47. u32 next, u32 prev)
  48. {
  49. typecheck(typeof(ring->size), next);
  50. typecheck(typeof(ring->size), prev);
  51. return (next - prev) << ring->wrap;
  52. }
  53. static inline bool
  54. intel_ring_offset_valid(const struct intel_ring *ring,
  55. unsigned int pos)
  56. {
  57. if (pos & -ring->size) /* must be strictly within the ring */
  58. return false;
  59. if (!IS_ALIGNED(pos, 8)) /* must be qword aligned */
  60. return false;
  61. return true;
  62. }
  63. static inline u32 intel_ring_offset(const struct i915_request *rq, void *addr)
  64. {
  65. /* Don't write ring->size (equivalent to 0) as that hangs some GPUs. */
  66. u32 offset = addr - rq->ring->vaddr;
  67. GEM_BUG_ON(offset > rq->ring->size);
  68. return intel_ring_wrap(rq->ring, offset);
  69. }
  70. static inline void
  71. assert_ring_tail_valid(const struct intel_ring *ring, unsigned int tail)
  72. {
  73. unsigned int head = READ_ONCE(ring->head);
  74. GEM_BUG_ON(!intel_ring_offset_valid(ring, tail));
  75. /*
  76. * "Ring Buffer Use"
  77. * Gen2 BSpec "1. Programming Environment" / 1.4.4.6
  78. * Gen3 BSpec "1c Memory Interface Functions" / 2.3.4.5
  79. * Gen4+ BSpec "1c Memory Interface and Command Stream" / 5.3.4.5
  80. * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
  81. * same cacheline, the Head Pointer must not be greater than the Tail
  82. * Pointer."
  83. *
  84. * We use ring->head as the last known location of the actual RING_HEAD,
  85. * it may have advanced but in the worst case it is equally the same
  86. * as ring->head and so we should never program RING_TAIL to advance
  87. * into the same cacheline as ring->head.
  88. */
  89. #define cacheline(a) round_down(a, CACHELINE_BYTES)
  90. GEM_BUG_ON(cacheline(tail) == cacheline(head) && tail < head);
  91. #undef cacheline
  92. }
  93. static inline unsigned int
  94. intel_ring_set_tail(struct intel_ring *ring, unsigned int tail)
  95. {
  96. /* Whilst writes to the tail are strictly order, there is no
  97. * serialisation between readers and the writers. The tail may be
  98. * read by i915_request_retire() just as it is being updated
  99. * by execlists, as although the breadcrumb is complete, the context
  100. * switch hasn't been seen.
  101. */
  102. assert_ring_tail_valid(ring, tail);
  103. ring->tail = tail;
  104. return tail;
  105. }
  106. static inline unsigned int
  107. __intel_ring_space(unsigned int head, unsigned int tail, unsigned int size)
  108. {
  109. /*
  110. * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
  111. * same cacheline, the Head Pointer must not be greater than the Tail
  112. * Pointer."
  113. */
  114. GEM_BUG_ON(!is_power_of_2(size));
  115. return (head - tail - CACHELINE_BYTES) & (size - 1);
  116. }
  117. #endif /* INTEL_RING_H */