19d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala/*
29d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * Copyright © 2014 Intel Corporation
39d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala *
49d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * Permission is hereby granted, free of charge, to any person obtaining a
59d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * copy of this software and associated documentation files (the "Software"),
69d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * to deal in the Software without restriction, including without limitation
79d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * the rights to use, copy, modify, merge, publish, distribute, sublicense,
89d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * and/or sell copies of the Software, and to permit persons to whom the
99d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * Software is furnished to do so, subject to the following conditions:
109d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala *
119d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * The above copyright notice and this permission notice (including the next
129d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * paragraph) shall be included in all copies or substantial portions of the
139d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * Software.
149d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala *
159d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
169d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
179d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
189d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
199d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
209d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
219d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * IN THE SOFTWARE.
229d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala *
239d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala * Authors:
249d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala *    Mika Kuoppala <mika.kuoppala@intel.com>
259d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala *
269d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala */
279d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
289d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala#include "i915_drv.h"
299d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala#include "intel_renderstate.h"
309d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
319d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppalastatic const struct intel_renderstate_rodata *
329d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppalarender_state_get_rodata(struct drm_device *dev, const int gen)
339d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala{
349d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	switch (gen) {
359d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	case 6:
369d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		return &gen6_null_state;
379d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	case 7:
389d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		return &gen7_null_state;
399d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	case 8:
409d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		return &gen8_null_state;
419d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	}
429d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
439d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	return NULL;
449d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala}
459d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
461ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilsonstatic int render_state_init(struct render_state *so, struct drm_device *dev)
479d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala{
489d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	int ret;
499d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
501ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	so->gen = INTEL_INFO(dev)->gen;
511ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	so->rodata = render_state_get_rodata(dev, so->gen);
521ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	if (so->rodata == NULL)
531ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		return 0;
541ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
551ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	if (so->rodata->batch_items * 4 > 4096)
569d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		return -EINVAL;
579d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
581ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	so->obj = i915_gem_alloc_object(dev, 4096);
591ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	if (so->obj == NULL)
601ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		return -ENOMEM;
611ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
621ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	ret = i915_gem_obj_ggtt_pin(so->obj, 4096, 0);
631ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	if (ret)
641ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		goto free_gem;
651ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
661ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	so->ggtt_offset = i915_gem_obj_ggtt_offset(so->obj);
671ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	return 0;
681ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
691ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilsonfree_gem:
701ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	drm_gem_object_unreference(&so->obj->base);
711ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	return ret;
721ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson}
731ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
741ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilsonstatic int render_state_setup(struct render_state *so)
751ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson{
761ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	const struct intel_renderstate_rodata *rodata = so->rodata;
771ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	unsigned int i = 0, reloc_index = 0;
781ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	struct page *page;
791ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	u32 *d;
801ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	int ret;
811ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
829d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	ret = i915_gem_object_set_to_cpu_domain(so->obj, true);
839d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	if (ret)
849d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		return ret;
859d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
861ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	page = sg_page(so->obj->pages->sgl);
871ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	d = kmap(page);
881ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
899d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	while (i < rodata->batch_items) {
909d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		u32 s = rodata->batch[i];
919d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
921ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		if (i * 4  == rodata->reloc[reloc_index]) {
931ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson			u64 r = s + so->ggtt_offset;
941ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson			s = lower_32_bits(r);
951ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson			if (so->gen >= 8) {
969d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala				if (i + 1 >= rodata->batch_items ||
979d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala				    rodata->batch[i + 1] != 0)
989d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala					return -EINVAL;
999d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1001ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson				d[i++] = s;
1011ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson				s = upper_32_bits(r);
1029d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala			}
1039d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1049d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala			reloc_index++;
1059d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		}
1069d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1071ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		d[i++] = s;
1089d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	}
1091ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	kunmap(page);
1109d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1119d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	ret = i915_gem_object_set_to_gtt_domain(so->obj, false);
1129d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	if (ret)
1139d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		return ret;
1149d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1151ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	if (rodata->reloc[reloc_index] != -1) {
1161ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		DRM_ERROR("only %d relocs resolved\n", reloc_index);
1179d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		return -EINVAL;
1189d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	}
1199d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1209d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	return 0;
1219d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala}
1229d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
123564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateovoid i915_gem_render_state_fini(struct render_state *so)
1241ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson{
1251ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	i915_gem_object_ggtt_unpin(so->obj);
1261ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	drm_gem_object_unreference(&so->obj->base);
1271ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson}
1281ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson
129564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateoint i915_gem_render_state_prepare(struct intel_engine_cs *ring,
130564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo				  struct render_state *so)
1319d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala{
1329d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	int ret;
1339d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
13446470fc932ac8a0e8317a220b3f4ea4ed903338eMika Kuoppala	if (WARN_ON(ring->id != RCS))
13546470fc932ac8a0e8317a220b3f4ea4ed903338eMika Kuoppala		return -ENOENT;
13646470fc932ac8a0e8317a220b3f4ea4ed903338eMika Kuoppala
137564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	ret = render_state_init(so, ring->dev);
1381ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	if (ret)
1391ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		return ret;
1409d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
141564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	if (so->rodata == NULL)
1421ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson		return 0;
1439d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
144564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	ret = render_state_setup(so);
145564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	if (ret) {
146564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo		i915_gem_render_state_fini(so);
147564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo		return ret;
148564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	}
149564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo
150564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	return 0;
151564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo}
152564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo
153564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateoint i915_gem_render_state_init(struct intel_engine_cs *ring)
154564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo{
155564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	struct render_state so;
156564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	int ret;
157564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo
158564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	ret = i915_gem_render_state_prepare(ring, &so);
1599d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	if (ret)
160564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo		return ret;
161564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo
162564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	if (so.rodata == NULL)
163564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo		return 0;
1649d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1659d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	ret = ring->dispatch_execbuffer(ring,
1661ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson					so.ggtt_offset,
1671ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson					so.rodata->batch_items * 4,
1689d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala					I915_DISPATCH_SECURE);
1699d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	if (ret)
1709d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala		goto out;
1719d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1721ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
1739d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala
1741ce826d436f33c8fc0cf8b51d213b496ea73e0a6Chris Wilson	ret = __i915_add_request(ring, NULL, so.obj, NULL);
17546470fc932ac8a0e8317a220b3f4ea4ed903338eMika Kuoppala	/* __i915_add_request moves object to inactive if it fails */
1769d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppalaout:
177564ddb2fae4da01406a24e5a763e601dc21c23d7Oscar Mateo	i915_gem_render_state_fini(&so);
1789d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala	return ret;
1799d0a6fa6c5e618bd978d625a215dc4a240ba3b3cMika Kuoppala}
180