Merge branch 'akpm' (patches from Andrew)
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / gt / intel_ring.h
blobea2839d9e044546957ea19133139c025375d9055
1 /*
2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
5 */
7 #ifndef INTEL_RING_H
8 #define INTEL_RING_H
10 #include "i915_gem.h" /* GEM_BUG_ON */
11 #include "i915_request.h"
12 #include "intel_ring_types.h"
14 struct intel_engine_cs;
16 struct intel_ring *
17 intel_engine_create_ring(struct intel_engine_cs *engine, int size);
19 u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords);
20 int intel_ring_cacheline_align(struct i915_request *rq);
22 unsigned int intel_ring_update_space(struct intel_ring *ring);
24 int intel_ring_pin(struct intel_ring *ring);
25 void intel_ring_unpin(struct intel_ring *ring);
26 void intel_ring_reset(struct intel_ring *ring, u32 tail);
28 void intel_ring_free(struct kref *ref);
30 static inline struct intel_ring *intel_ring_get(struct intel_ring *ring)
32 kref_get(&ring->ref);
33 return ring;
36 static inline void intel_ring_put(struct intel_ring *ring)
38 kref_put(&ring->ref, intel_ring_free);
41 static inline void intel_ring_advance(struct i915_request *rq, u32 *cs)
43 /* Dummy function.
45 * This serves as a placeholder in the code so that the reader
46 * can compare against the preceding intel_ring_begin() and
47 * check that the number of dwords emitted matches the space
48 * reserved for the command packet (i.e. the value passed to
49 * intel_ring_begin()).
51 GEM_BUG_ON((rq->ring->vaddr + rq->ring->emit) != cs);
54 static inline u32 intel_ring_wrap(const struct intel_ring *ring, u32 pos)
56 return pos & (ring->size - 1);
59 static inline bool
60 intel_ring_offset_valid(const struct intel_ring *ring,
61 unsigned int pos)
63 if (pos & -ring->size) /* must be strictly within the ring */
64 return false;
66 if (!IS_ALIGNED(pos, 8)) /* must be qword aligned */
67 return false;
69 return true;
72 static inline u32 intel_ring_offset(const struct i915_request *rq, void *addr)
74 /* Don't write ring->size (equivalent to 0) as that hangs some GPUs. */
75 u32 offset = addr - rq->ring->vaddr;
76 GEM_BUG_ON(offset > rq->ring->size);
77 return intel_ring_wrap(rq->ring, offset);
80 static inline void
81 assert_ring_tail_valid(const struct intel_ring *ring, unsigned int tail)
83 GEM_BUG_ON(!intel_ring_offset_valid(ring, tail));
86 * "Ring Buffer Use"
87 * Gen2 BSpec "1. Programming Environment" / 1.4.4.6
88 * Gen3 BSpec "1c Memory Interface Functions" / 2.3.4.5
89 * Gen4+ BSpec "1c Memory Interface and Command Stream" / 5.3.4.5
90 * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
91 * same cacheline, the Head Pointer must not be greater than the Tail
92 * Pointer."
94 * We use ring->head as the last known location of the actual RING_HEAD,
95 * it may have advanced but in the worst case it is equally the same
96 * as ring->head and so we should never program RING_TAIL to advance
97 * into the same cacheline as ring->head.
99 #define cacheline(a) round_down(a, CACHELINE_BYTES)
100 GEM_BUG_ON(cacheline(tail) == cacheline(ring->head) &&
101 tail < ring->head);
102 #undef cacheline
105 static inline unsigned int
106 intel_ring_set_tail(struct intel_ring *ring, unsigned int tail)
108 /* Whilst writes to the tail are strictly order, there is no
109 * serialisation between readers and the writers. The tail may be
110 * read by i915_request_retire() just as it is being updated
111 * by execlists, as although the breadcrumb is complete, the context
112 * switch hasn't been seen.
114 assert_ring_tail_valid(ring, tail);
115 ring->tail = tail;
116 return tail;
119 static inline unsigned int
120 __intel_ring_space(unsigned int head, unsigned int tail, unsigned int size)
123 * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
124 * same cacheline, the Head Pointer must not be greater than the Tail
125 * Pointer."
127 GEM_BUG_ON(!is_power_of_2(size));
128 return (head - tail - CACHELINE_BYTES) & (size - 1);
131 #endif /* INTEL_RING_H */