2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Mika Kuoppala <mika.kuoppala@intel.com>
29 #include "intel_renderstate.h"
31 static const struct intel_renderstate_rodata
*
32 render_state_get_rodata(struct drm_device
*dev
, const int gen
)
36 return &gen6_null_state
;
38 return &gen7_null_state
;
40 return &gen8_null_state
;
42 return &gen9_null_state
;
48 static int render_state_init(struct render_state
*so
, struct drm_device
*dev
)
52 so
->gen
= INTEL_INFO(dev
)->gen
;
53 so
->rodata
= render_state_get_rodata(dev
, so
->gen
);
54 if (so
->rodata
== NULL
)
57 if (so
->rodata
->batch_items
* 4 > 4096)
60 so
->obj
= i915_gem_alloc_object(dev
, 4096);
64 ret
= i915_gem_obj_ggtt_pin(so
->obj
, 4096, 0);
68 so
->ggtt_offset
= i915_gem_obj_ggtt_offset(so
->obj
);
72 drm_gem_object_unreference(&so
->obj
->base
);
77 * Macro to add commands to auxiliary batch.
78 * This macro only checks for page overflow before inserting the commands,
79 * this is sufficient as the null state generator makes the final batch
80 * with two passes to build command and state separately. At this point
81 * the size of both are known and it compacts them by relocating the state
82 * right after the commands taking care of aligment so we should sufficient
83 * space below them for adding new commands.
85 #define OUT_BATCH(batch, i, val) \
87 if (WARN_ON((i) >= PAGE_SIZE / sizeof(u32))) { \
91 (batch)[(i)++] = (val); \
94 static int render_state_setup(struct render_state
*so
)
96 const struct intel_renderstate_rodata
*rodata
= so
->rodata
;
97 unsigned int i
= 0, reloc_index
= 0;
102 ret
= i915_gem_object_set_to_cpu_domain(so
->obj
, true);
106 page
= sg_page(so
->obj
->pages
->sgl
);
109 while (i
< rodata
->batch_items
) {
110 u32 s
= rodata
->batch
[i
];
112 if (i
* 4 == rodata
->reloc
[reloc_index
]) {
113 u64 r
= s
+ so
->ggtt_offset
;
114 s
= lower_32_bits(r
);
116 if (i
+ 1 >= rodata
->batch_items
||
117 rodata
->batch
[i
+ 1] != 0) {
123 s
= upper_32_bits(r
);
132 while (i
% CACHELINE_DWORDS
)
133 OUT_BATCH(d
, i
, MI_NOOP
);
135 so
->aux_batch_offset
= i
* sizeof(u32
);
137 OUT_BATCH(d
, i
, MI_BATCH_BUFFER_END
);
138 so
->aux_batch_size
= (i
* sizeof(u32
)) - so
->aux_batch_offset
;
141 * Since we are sending length, we need to strictly conform to
142 * all requirements. For Gen2 this must be a multiple of 8.
144 so
->aux_batch_size
= ALIGN(so
->aux_batch_size
, 8);
148 ret
= i915_gem_object_set_to_gtt_domain(so
->obj
, false);
152 if (rodata
->reloc
[reloc_index
] != -1) {
153 DRM_ERROR("only %d relocs resolved\n", reloc_index
);
166 void i915_gem_render_state_fini(struct render_state
*so
)
168 i915_gem_object_ggtt_unpin(so
->obj
);
169 drm_gem_object_unreference(&so
->obj
->base
);
172 int i915_gem_render_state_prepare(struct intel_engine_cs
*ring
,
173 struct render_state
*so
)
177 if (WARN_ON(ring
->id
!= RCS
))
180 ret
= render_state_init(so
, ring
->dev
);
184 if (so
->rodata
== NULL
)
187 ret
= render_state_setup(so
);
189 i915_gem_render_state_fini(so
);
196 int i915_gem_render_state_init(struct drm_i915_gem_request
*req
)
198 struct render_state so
;
201 ret
= i915_gem_render_state_prepare(req
->ring
, &so
);
205 if (so
.rodata
== NULL
)
208 ret
= req
->ring
->dispatch_execbuffer(req
, so
.ggtt_offset
,
209 so
.rodata
->batch_items
* 4,
210 I915_DISPATCH_SECURE
);
214 if (so
.aux_batch_size
> 8) {
215 ret
= req
->ring
->dispatch_execbuffer(req
,
217 so
.aux_batch_offset
),
219 I915_DISPATCH_SECURE
);
224 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so
.obj
), req
);
227 i915_gem_render_state_fini(&so
);