intel: Fix type correctly this time
[mesa.git] / src / gallium / winsys / drm / intel / gem / intel_be_batchbuffer.c
1
2 #include "intel_be_batchbuffer.h"
3 #include "intel_be_context.h"
4 #include "intel_be_device.h"
5 #include "intel_be_fence.h"
6 #include <errno.h>
7
8 #include "util/u_memory.h"
9
10 struct intel_be_batchbuffer *
11 intel_be_batchbuffer_alloc(struct intel_be_context *intel)
12 {
13 struct intel_be_batchbuffer *batch = CALLOC_STRUCT(intel_be_batchbuffer);
14
15
16 batch->base.buffer = NULL;
17 batch->base.winsys = &intel->base;
18 batch->base.map = NULL;
19 batch->base.ptr = NULL;
20 batch->base.size = 0;
21 batch->base.actual_size = intel->device->max_batch_size;
22 batch->base.relocs = 0;
23 batch->base.max_relocs = INTEL_DEFAULT_RELOCS;
24
25 batch->base.map = malloc(batch->base.actual_size);
26 memset(batch->base.map, 0, batch->base.actual_size);
27
28 batch->base.ptr = batch->base.map;
29
30 intel_be_batchbuffer_reset(batch);
31
32 return batch;
33 }
34
35 void
36 intel_be_batchbuffer_reset(struct intel_be_batchbuffer *batch)
37 {
38 struct intel_be_context *intel = intel_be_context(batch->base.winsys);
39 struct intel_be_device *dev = intel->device;
40
41 if (batch->bo)
42 drm_intel_bo_unreference(batch->bo);
43
44 memset(batch->base.map, 0, batch->base.actual_size);
45 batch->base.ptr = batch->base.map;
46 batch->base.size = batch->base.actual_size - BATCH_RESERVED;
47
48 batch->base.relocs = 0;
49 batch->base.max_relocs = INTEL_DEFAULT_RELOCS;
50
51 batch->bo = drm_intel_bo_alloc(dev->pools.gem,
52 "gallium3d_batch_buffer",
53 batch->base.actual_size, 0);
54 }
55
56 int
57 intel_be_offset_relocation(struct intel_be_batchbuffer *batch,
58 unsigned pre_add,
59 drm_intel_bo *bo,
60 uint32_t read_domains,
61 uint32_t write_domain)
62 {
63 unsigned offset;
64 int ret = 0;
65
66 assert(batch->base.relocs < batch->base.max_relocs);
67
68 offset = (unsigned)(batch->base.ptr - batch->base.map);
69
70 ret = drm_intel_bo_emit_reloc(batch->bo, offset,
71 bo, pre_add,
72 read_domains,
73 write_domain);
74
75 ((uint32_t*)batch->base.ptr)[0] = bo->offset + pre_add;
76 batch->base.ptr += 4;
77
78 if (!ret)
79 batch->base.relocs++;
80
81 return ret;
82 }
83
84 void
85 intel_be_batchbuffer_flush(struct intel_be_batchbuffer *batch,
86 struct intel_be_fence **fence)
87 {
88 struct i915_batchbuffer *i915 = &batch->base;
89 unsigned used = 0;
90 int ret = 0;
91
92 assert(i915_batchbuffer_space(i915) >= 0);
93
94 used = batch->base.ptr - batch->base.map;
95 assert((used & 3) == 0);
96
97 if (used & 4) {
98 ((uint32_t *) batch->base.ptr)[0] = ((0<<29)|(4<<23)); // MI_FLUSH;
99 ((uint32_t *) batch->base.ptr)[1] = 0;
100 ((uint32_t *) batch->base.ptr)[2] = (0xA<<23); // MI_BATCH_BUFFER_END;
101 batch->base.ptr += 12;
102 } else {
103 ((uint32_t *) batch->base.ptr)[0] = ((0<<29)|(4<<23)); // MI_FLUSH;
104 ((uint32_t *) batch->base.ptr)[1] = (0xA<<23); // MI_BATCH_BUFFER_END;
105 batch->base.ptr += 8;
106 }
107
108 debug_printf("%s\n", __FUNCTION__);
109
110 used = batch->base.ptr - batch->base.map;
111
112 debug_printf(" - subdata\n");
113 drm_intel_bo_subdata(batch->bo, 0, used, batch->base.map);
114 debug_printf(" - exec\n");
115 ret = drm_intel_bo_exec(batch->bo, used, NULL, 0, 0);
116 debug_printf(" - waiting\n");
117 drm_intel_bo_wait_rendering(batch->bo);
118 debug_printf(" - done\n");
119
120 assert(ret == 0);
121
122 intel_be_batchbuffer_reset(batch);
123
124 if (fence) {
125 if (*fence)
126 intel_be_fence_unreference(*fence);
127
128 (*fence) = CALLOC_STRUCT(intel_be_fence);
129 (*fence)->refcount = 1;
130 (*fence)->bo = NULL;
131 }
132 }
133
134 void
135 intel_be_batchbuffer_finish(struct intel_be_batchbuffer *batch)
136 {
137
138 }
139
140 void
141 intel_be_batchbuffer_free(struct intel_be_batchbuffer *batch)
142 {
143 if (batch->bo)
144 drm_intel_bo_unreference(batch->bo);
145
146 free(batch->base.map);
147 free(batch);
148 }