blob: e60be3f552a6b1aed14770bef853a1eab2ddf5aa [file] [log] [blame]
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +03001/*
2 * Copyright © 2014 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 * Authors:
24 * Mika Kuoppala <mika.kuoppala@intel.com>
25 *
26 */
27
28#include "i915_drv.h"
29#include "intel_renderstate.h"
30
Chris Wilson1ce826d2014-06-10 11:23:33 +010031struct render_state {
32 const struct intel_renderstate_rodata *rodata;
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030033 struct drm_i915_gem_object *obj;
Chris Wilson1ce826d2014-06-10 11:23:33 +010034 u64 ggtt_offset;
35 int gen;
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030036};
37
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030038static const struct intel_renderstate_rodata *
39render_state_get_rodata(struct drm_device *dev, const int gen)
40{
41 switch (gen) {
42 case 6:
43 return &gen6_null_state;
44 case 7:
45 return &gen7_null_state;
46 case 8:
47 return &gen8_null_state;
48 }
49
50 return NULL;
51}
52
Chris Wilson1ce826d2014-06-10 11:23:33 +010053static int render_state_init(struct render_state *so, struct drm_device *dev)
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030054{
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030055 int ret;
56
Chris Wilson1ce826d2014-06-10 11:23:33 +010057 so->gen = INTEL_INFO(dev)->gen;
58 so->rodata = render_state_get_rodata(dev, so->gen);
59 if (so->rodata == NULL)
60 return 0;
61
62 if (so->rodata->batch_items * 4 > 4096)
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030063 return -EINVAL;
64
Chris Wilson1ce826d2014-06-10 11:23:33 +010065 so->obj = i915_gem_alloc_object(dev, 4096);
66 if (so->obj == NULL)
67 return -ENOMEM;
68
69 ret = i915_gem_obj_ggtt_pin(so->obj, 4096, 0);
70 if (ret)
71 goto free_gem;
72
73 so->ggtt_offset = i915_gem_obj_ggtt_offset(so->obj);
74 return 0;
75
76free_gem:
77 drm_gem_object_unreference(&so->obj->base);
78 return ret;
79}
80
81static int render_state_setup(struct render_state *so)
82{
83 const struct intel_renderstate_rodata *rodata = so->rodata;
84 unsigned int i = 0, reloc_index = 0;
85 struct page *page;
86 u32 *d;
87 int ret;
88
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030089 ret = i915_gem_object_set_to_cpu_domain(so->obj, true);
90 if (ret)
91 return ret;
92
Chris Wilson1ce826d2014-06-10 11:23:33 +010093 page = sg_page(so->obj->pages->sgl);
94 d = kmap(page);
95
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +030096 while (i < rodata->batch_items) {
97 u32 s = rodata->batch[i];
98
Chris Wilson1ce826d2014-06-10 11:23:33 +010099 if (i * 4 == rodata->reloc[reloc_index]) {
100 u64 r = s + so->ggtt_offset;
101 s = lower_32_bits(r);
102 if (so->gen >= 8) {
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300103 if (i + 1 >= rodata->batch_items ||
104 rodata->batch[i + 1] != 0)
105 return -EINVAL;
106
Chris Wilson1ce826d2014-06-10 11:23:33 +0100107 d[i++] = s;
108 s = upper_32_bits(r);
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300109 }
110
111 reloc_index++;
112 }
113
Chris Wilson1ce826d2014-06-10 11:23:33 +0100114 d[i++] = s;
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300115 }
Chris Wilson1ce826d2014-06-10 11:23:33 +0100116 kunmap(page);
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300117
118 ret = i915_gem_object_set_to_gtt_domain(so->obj, false);
119 if (ret)
120 return ret;
121
Chris Wilson1ce826d2014-06-10 11:23:33 +0100122 if (rodata->reloc[reloc_index] != -1) {
123 DRM_ERROR("only %d relocs resolved\n", reloc_index);
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300124 return -EINVAL;
125 }
126
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300127 return 0;
128}
129
Chris Wilson1ce826d2014-06-10 11:23:33 +0100130static void render_state_fini(struct render_state *so)
131{
132 i915_gem_object_ggtt_unpin(so->obj);
133 drm_gem_object_unreference(&so->obj->base);
134}
135
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100136int i915_gem_render_state_init(struct intel_engine_cs *ring)
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300137{
Chris Wilson1ce826d2014-06-10 11:23:33 +0100138 struct render_state so;
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300139 int ret;
140
Mika Kuoppala46470fc92014-05-21 19:01:06 +0300141 if (WARN_ON(ring->id != RCS))
142 return -ENOENT;
143
Chris Wilson1ce826d2014-06-10 11:23:33 +0100144 ret = render_state_init(&so, ring->dev);
145 if (ret)
146 return ret;
147
148 if (so.rodata == NULL)
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300149 return 0;
150
Chris Wilson1ce826d2014-06-10 11:23:33 +0100151 ret = render_state_setup(&so);
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300152 if (ret)
153 goto out;
154
155 ret = ring->dispatch_execbuffer(ring,
Chris Wilson1ce826d2014-06-10 11:23:33 +0100156 so.ggtt_offset,
157 so.rodata->batch_items * 4,
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300158 I915_DISPATCH_SECURE);
159 if (ret)
160 goto out;
161
Chris Wilson1ce826d2014-06-10 11:23:33 +0100162 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300163
Chris Wilson1ce826d2014-06-10 11:23:33 +0100164 ret = __i915_add_request(ring, NULL, so.obj, NULL);
Mika Kuoppala46470fc92014-05-21 19:01:06 +0300165 /* __i915_add_request moves object to inactive if it fails */
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300166out:
Chris Wilson1ce826d2014-06-10 11:23:33 +0100167 render_state_fini(&so);
Mika Kuoppala9d0a6fa2014-05-14 17:02:16 +0300168 return ret;
169}