drm/i915: context basic create & destroy
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / drm / i915 / i915_gem_context.c
CommitLineData
254f965c
BW
1/*
2 * Copyright © 2011-2012 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 * Ben Widawsky <ben@bwidawsk.net>
25 *
26 */
27
28/*
29 * This file implements HW context support. On gen5+ a HW context consists of an
30 * opaque GPU object which is referenced at times of context saves and restores.
31 * With RC6 enabled, the context is also referenced as the GPU enters and exists
32 * from RC6 (GPU has it's own internal power context, except on gen5). Though
33 * something like a context does exist for the media ring, the code only
34 * supports contexts for the render ring.
35 *
36 * In software, there is a distinction between contexts created by the user,
37 * and the default HW context. The default HW context is used by GPU clients
38 * that do not request setup of their own hardware context. The default
39 * context's state is never restored to help prevent programming errors. This
40 * would happen if a client ran and piggy-backed off another clients GPU state.
41 * The default context only exists to give the GPU some offset to load as the
42 * current to invoke a save of the context we actually care about. In fact, the
43 * code could likely be constructed, albeit in a more complicated fashion, to
44 * never use the default context, though that limits the driver's ability to
45 * swap out, and/or destroy other contexts.
46 *
47 * All other contexts are created as a request by the GPU client. These contexts
48 * store GPU state, and thus allow GPU clients to not re-emit state (and
49 * potentially query certain state) at any time. The kernel driver makes
50 * certain that the appropriate commands are inserted.
51 *
52 * The context life cycle is semi-complicated in that context BOs may live
53 * longer than the context itself because of the way the hardware, and object
54 * tracking works. Below is a very crude representation of the state machine
55 * describing the context life.
56 * refcount pincount active
57 * S0: initial state 0 0 0
58 * S1: context created 1 0 0
59 * S2: context is currently running 2 1 X
60 * S3: GPU referenced, but not current 2 0 1
61 * S4: context is current, but destroyed 1 1 0
62 * S5: like S3, but destroyed 1 0 1
63 *
64 * The most common (but not all) transitions:
65 * S0->S1: client creates a context
66 * S1->S2: client submits execbuf with context
67 * S2->S3: other clients submits execbuf with context
68 * S3->S1: context object was retired
69 * S3->S2: clients submits another execbuf
70 * S2->S4: context destroy called with current context
71 * S3->S5->S0: destroy path
72 * S4->S5->S0: destroy path on current context
73 *
74 * There are two confusing terms used above:
75 * The "current context" means the context which is currently running on the
76 * GPU. The GPU has loaded it's state already and has stored away the gtt
77 * offset of the BO. The GPU is not actively referencing the data at this
78 * offset, but it will on the next context switch. The only way to avoid this
79 * is to do a GPU reset.
80 *
81 * An "active context' is one which was previously the "current context" and is
82 * on the active list waiting for the next context switch to occur. Until this
83 * happens, the object must remain at the same gtt offset. It is therefore
84 * possible to destroy a context, but it is still active.
85 *
86 */
87
88#include "drmP.h"
89#include "i915_drm.h"
90#include "i915_drv.h"
91
40521054
BW
92/* This is a HW constraint. The value below is the largest known requirement
93 * I've seen in a spec to date, and that was a workaround for a non-shipping
94 * part. It should be safe to decrease this, but it's more future proof as is.
95 */
96#define CONTEXT_ALIGN (64<<10)
97
98static struct i915_hw_context *
99i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id);
100
254f965c
BW
101static int get_context_size(struct drm_device *dev)
102{
103 struct drm_i915_private *dev_priv = dev->dev_private;
104 int ret;
105 u32 reg;
106
107 switch (INTEL_INFO(dev)->gen) {
108 case 6:
109 reg = I915_READ(CXT_SIZE);
110 ret = GEN6_CXT_TOTAL_SIZE(reg) * 64;
111 break;
112 case 7:
113 reg = I915_READ(GEN7_CTX_SIZE);
114 ret = GEN7_CTX_TOTAL_SIZE(reg) * 64;
115 break;
116 default:
117 BUG();
118 }
119
120 return ret;
121}
122
40521054
BW
123static void do_destroy(struct i915_hw_context *ctx)
124{
125 struct drm_device *dev = ctx->obj->base.dev;
126 struct drm_i915_private *dev_priv = dev->dev_private;
127
128 if (ctx->file_priv)
129 idr_remove(&ctx->file_priv->context_idr, ctx->id);
130 else
131 BUG_ON(ctx != dev_priv->ring[RCS].default_context);
132
133 drm_gem_object_unreference(&ctx->obj->base);
134 kfree(ctx);
135}
136
137static int
138create_hw_context(struct drm_device *dev,
139 struct drm_i915_file_private *file_priv,
140 struct i915_hw_context **ctx_out)
141{
142 struct drm_i915_private *dev_priv = dev->dev_private;
143 int ret, id;
144
145 *ctx_out = kzalloc(sizeof(struct drm_i915_file_private), GFP_KERNEL);
146 if (*ctx_out == NULL)
147 return -ENOMEM;
148
149 (*ctx_out)->obj = i915_gem_alloc_object(dev,
150 dev_priv->hw_context_size);
151 if ((*ctx_out)->obj == NULL) {
152 kfree(*ctx_out);
153 DRM_DEBUG_DRIVER("Context object allocated failed\n");
154 return -ENOMEM;
155 }
156
157 /* The ring associated with the context object is handled by the normal
158 * object tracking code. We give an initial ring value simple to pass an
159 * assertion in the context switch code.
160 */
161 (*ctx_out)->ring = &dev_priv->ring[RCS];
162
163 /* Default context will never have a file_priv */
164 if (file_priv == NULL)
165 return 0;
166
167 (*ctx_out)->file_priv = file_priv;
168
169again:
170 if (idr_pre_get(&file_priv->context_idr, GFP_KERNEL) == 0) {
171 ret = -ENOMEM;
172 DRM_DEBUG_DRIVER("idr allocation failed\n");
173 goto err_out;
174 }
175
176 ret = idr_get_new_above(&file_priv->context_idr, *ctx_out,
177 DEFAULT_CONTEXT_ID + 1, &id);
178 if (ret == 0)
179 (*ctx_out)->id = id;
180
181 if (ret == -EAGAIN)
182 goto again;
183 else if (ret)
184 goto err_out;
185
186 return 0;
187
188err_out:
189 do_destroy(*ctx_out);
190 return ret;
191}
192
254f965c
BW
193/**
194 * The default context needs to exist per ring that uses contexts. It stores the
195 * context state of the GPU for applications that don't utilize HW contexts, as
196 * well as an idle case.
197 */
198static int create_default_context(struct drm_i915_private *dev_priv)
199{
40521054
BW
200 struct i915_hw_context *ctx;
201 int ret;
202
203 BUG_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
204
205 ret = create_hw_context(dev_priv->dev, NULL,
206 &dev_priv->ring[RCS].default_context);
207 if (ret)
208 return ret;
209
210 /* We may need to do things with the shrinker which require us to
211 * immediately switch back to the default context. This can cause a
212 * problem as pinning the default context also requires GTT space which
213 * may not be available. To avoid this we always pin the
214 * default context.
215 */
216 ctx = dev_priv->ring[RCS].default_context;
217 ret = i915_gem_object_pin(ctx->obj, CONTEXT_ALIGN, false);
218 if (ret) {
219 do_destroy(ctx);
220 return ret;
221 }
222
223 return ret;
254f965c
BW
224}
225
226void i915_gem_context_init(struct drm_device *dev)
227{
228 struct drm_i915_private *dev_priv = dev->dev_private;
229 uint32_t ctx_size;
230
231 if (!HAS_HW_CONTEXTS(dev))
232 return;
233
234 /* If called from reset, or thaw... we've been here already */
40521054
BW
235 if (dev_priv->hw_contexts_disabled ||
236 dev_priv->ring[RCS].default_context)
254f965c
BW
237 return;
238
239 ctx_size = get_context_size(dev);
240 dev_priv->hw_context_size = get_context_size(dev);
241 dev_priv->hw_context_size = round_up(dev_priv->hw_context_size, 4096);
242
243 if (ctx_size <= 0 || ctx_size > (1<<20)) {
244 dev_priv->hw_contexts_disabled = true;
245 return;
246 }
247
248 if (create_default_context(dev_priv)) {
249 dev_priv->hw_contexts_disabled = true;
250 return;
251 }
252
253 DRM_DEBUG_DRIVER("HW context support initialized\n");
254}
255
256void i915_gem_context_fini(struct drm_device *dev)
257{
258 struct drm_i915_private *dev_priv = dev->dev_private;
259
260 if (dev_priv->hw_contexts_disabled)
261 return;
40521054
BW
262
263 i915_gem_object_unpin(dev_priv->ring[RCS].default_context->obj);
264
265 do_destroy(dev_priv->ring[RCS].default_context);
254f965c
BW
266}
267
268void i915_gem_context_open(struct drm_device *dev, struct drm_file *file)
269{
270 struct drm_i915_private *dev_priv = dev->dev_private;
40521054 271 struct drm_i915_file_private *file_priv = file->driver_priv;
254f965c
BW
272
273 if (dev_priv->hw_contexts_disabled)
274 return;
40521054
BW
275
276 idr_init(&file_priv->context_idr);
277}
278
279static int context_idr_cleanup(int id, void *p, void *data)
280{
281 struct drm_file *file = (struct drm_file *)data;
282 struct drm_i915_file_private *file_priv = file->driver_priv;
283 struct i915_hw_context *ctx;
284
285 BUG_ON(id == DEFAULT_CONTEXT_ID);
286 ctx = i915_gem_context_get(file_priv, id);
287 if (WARN_ON(ctx == NULL))
288 return -ENXIO;
289
290 do_destroy(ctx);
291
292 return 0;
254f965c
BW
293}
294
295void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
296{
297 struct drm_i915_private *dev_priv = dev->dev_private;
40521054 298 struct drm_i915_file_private *file_priv = file->driver_priv;
254f965c
BW
299
300 if (dev_priv->hw_contexts_disabled)
301 return;
40521054
BW
302
303 mutex_lock(&dev->struct_mutex);
304 idr_for_each(&file_priv->context_idr, context_idr_cleanup, file);
305 idr_destroy(&file_priv->context_idr);
306 mutex_unlock(&dev->struct_mutex);
307}
308
309static __used struct i915_hw_context *
310i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id)
311{
312 return (struct i915_hw_context *)idr_find(&file_priv->context_idr, id);
254f965c 313}