1 /*
2 * Copyright © 2020 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef IRIS_FINE_FENCE_DOT_H
25 #define IRIS_FINE_FENCE_DOT_H
26
27 #include <stdbool.h>
28 #include <stdint.h>
29
30 #include "iris_screen.h"
31 #include "iris_resource.h"
32
33 /**
34 * A lightweight sequence number fence.
35 *
36 * We emit PIPE_CONTROLs inside a batch (possibly in the middle)
37 * which update a monotonically increasing, 32-bit counter. We
38 * can then check if that moment has passed by either:
39 *
40 * 1. Checking on the CPU by snooping on the DWord via a coherent map
41 *
42 * 2. Blocking on the GPU with MI_SEMAPHORE_WAIT from a second batch
43 * (relying on mid-batch preemption to switch GPU execution to the
44 * batch that writes it).
45 */
46 struct iris_fine_fence {
47 struct pipe_reference reference;
48
49 /** Buffer where the seqno lives */
50 struct iris_state_ref ref;
51
52 /** Coherent CPU map of the buffer containing the seqno DWord. */
53 const uint32_t *map;
54
55 /**
56 * A drm_syncobj pointing which will be signaled at the end of the
57 * batch which writes this seqno. This can be used to block until
58 * the seqno has definitely passed (but may wait longer than necessary).
59 */
60 struct iris_syncobj *syncobj;
61
62 /**
63 * Sequence number expected to be written by the flush we inserted
64 * when creating this fence. The iris_fine_fence is 'signaled' when *@map
65 * (written by the flush on the GPU) is greater-than-or-equal to @seqno.
66 */
67 uint32_t seqno;
68 };
69
70 void iris_fine_fence_init(struct iris_batch *batch);
71
72 struct iris_fine_fence *iris_fine_fence_new(struct iris_batch *batch);
73
74 void iris_fine_fence_destroy(struct iris_screen *screen, struct iris_fine_fence *sq);
75
76 static inline void
iris_fine_fence_reference(struct iris_screen * screen,struct iris_fine_fence ** dst,struct iris_fine_fence * src)77 iris_fine_fence_reference(struct iris_screen *screen,
78 struct iris_fine_fence **dst,
79 struct iris_fine_fence *src)
80 {
81 if (pipe_reference(&(*dst)->reference, &src->reference))
82 iris_fine_fence_destroy(screen, *dst);
83
84 *dst = src;
85 }
86
87 /**
88 * Return true if this seqno has passed.
89 *
90 * NULL is considered signaled.
91 */
92 static inline bool
iris_fine_fence_signaled(const struct iris_fine_fence * sq)93 iris_fine_fence_signaled(const struct iris_fine_fence *sq)
94 {
95 return !sq || (READ_ONCE(*sq->map) >= sq->seqno);
96 }
97
98 #endif
99