blob: 4f0f173f9754dc99e6350c1b40a2e3ff12f51470 [file] [log] [blame]
Alex Daibac427f2015-08-12 15:43:39 +01001/*
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 */
24#include <linux/firmware.h>
25#include <linux/circ_buf.h>
26#include "i915_drv.h"
27#include "intel_guc.h"
28
29/**
Alex Daifeda33e2015-10-19 16:10:54 -070030 * DOC: GuC-based command submission
Dave Gordon44a28b12015-08-12 15:43:41 +010031 *
32 * i915_guc_client:
33 * We use the term client to avoid confusion with contexts. A i915_guc_client is
34 * equivalent to GuC object guc_context_desc. This context descriptor is
35 * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
36 * and workqueue for it. Also the process descriptor (guc_process_desc), which
37 * is mapped to client space. So the client can write Work Item then ring the
38 * doorbell.
39 *
40 * To simplify the implementation, we allocate one gem object that contains all
41 * pages for doorbell, process descriptor and workqueue.
42 *
43 * The Scratch registers:
44 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
45 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
46 * triggers an interrupt on the GuC via another register write (0xC4C8).
47 * Firmware writes a success/fail code back to the action register after
48 * processes the request. The kernel driver polls waiting for this update and
49 * then proceeds.
50 * See host2guc_action()
51 *
52 * Doorbells:
53 * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
54 * mapped into process space.
55 *
56 * Work Items:
57 * There are several types of work items that the host may place into a
58 * workqueue, each with its own requirements and limitations. Currently only
59 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
60 * represents in-order queue. The kernel driver packs ring tail pointer and an
61 * ELSP context descriptor dword into Work Item.
62 * See guc_add_workqueue_item()
63 *
64 */
65
66/*
67 * Read GuC command/status register (SOFT_SCRATCH_0)
68 * Return true if it contains a response rather than a command
69 */
70static inline bool host2guc_action_response(struct drm_i915_private *dev_priv,
71 u32 *status)
72{
73 u32 val = I915_READ(SOFT_SCRATCH(0));
74 *status = val;
75 return GUC2HOST_IS_RESPONSE(val);
76}
77
78static int host2guc_action(struct intel_guc *guc, u32 *data, u32 len)
79{
80 struct drm_i915_private *dev_priv = guc_to_i915(guc);
81 u32 status;
82 int i;
83 int ret;
84
85 if (WARN_ON(len < 1 || len > 15))
86 return -EINVAL;
87
88 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Dave Gordon44a28b12015-08-12 15:43:41 +010089
90 dev_priv->guc.action_count += 1;
91 dev_priv->guc.action_cmd = data[0];
92
93 for (i = 0; i < len; i++)
94 I915_WRITE(SOFT_SCRATCH(i), data[i]);
95
96 POSTING_READ(SOFT_SCRATCH(i - 1));
97
98 I915_WRITE(HOST2GUC_INTERRUPT, HOST2GUC_TRIGGER);
99
Dave Gordonab0e4552016-07-06 15:30:11 +0100100 /*
101 * Fast commands should complete in less than 10us, so sample quickly
102 * up to that length of time, then switch to a slower sleep-wait loop.
103 * No HOST2GUC command should ever take longer than 10ms.
104 */
105 ret = wait_for_us(host2guc_action_response(dev_priv, &status), 10);
106 if (ret)
107 ret = wait_for(host2guc_action_response(dev_priv, &status), 10);
Dave Gordon44a28b12015-08-12 15:43:41 +0100108 if (status != GUC2HOST_STATUS_SUCCESS) {
109 /*
110 * Either the GuC explicitly returned an error (which
111 * we convert to -EIO here) or no response at all was
112 * received within the timeout limit (-ETIMEDOUT)
113 */
114 if (ret != -ETIMEDOUT)
115 ret = -EIO;
116
117 DRM_ERROR("GUC: host2guc action 0x%X failed. ret=%d "
118 "status=0x%08X response=0x%08X\n",
119 data[0], ret, status,
120 I915_READ(SOFT_SCRATCH(15)));
121
122 dev_priv->guc.action_fail += 1;
123 dev_priv->guc.action_err = ret;
124 }
125 dev_priv->guc.action_status = status;
126
Dave Gordon44a28b12015-08-12 15:43:41 +0100127 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
128
129 return ret;
130}
131
132/*
133 * Tell the GuC to allocate or deallocate a specific doorbell
134 */
135
136static int host2guc_allocate_doorbell(struct intel_guc *guc,
137 struct i915_guc_client *client)
138{
139 u32 data[2];
140
141 data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL;
142 data[1] = client->ctx_index;
143
144 return host2guc_action(guc, data, 2);
145}
146
147static int host2guc_release_doorbell(struct intel_guc *guc,
148 struct i915_guc_client *client)
149{
150 u32 data[2];
151
152 data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL;
153 data[1] = client->ctx_index;
154
155 return host2guc_action(guc, data, 2);
156}
157
Alex Daif5d3c3e2015-08-18 14:34:47 -0700158static int host2guc_sample_forcewake(struct intel_guc *guc,
159 struct i915_guc_client *client)
160{
161 struct drm_i915_private *dev_priv = guc_to_i915(guc);
162 u32 data[2];
163
164 data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
Alex Dai93f25312015-09-25 11:46:56 -0700165 /* WaRsDisableCoarsePowerGating:skl,bxt */
Tvrtko Ursulin61251512016-06-21 15:07:14 +0100166 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Alex Dai93f25312015-09-25 11:46:56 -0700167 data[1] = 0;
168 else
169 /* bit 0 and 1 are for Render and Media domain separately */
170 data[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
Alex Daif5d3c3e2015-08-18 14:34:47 -0700171
Alex Dai93f25312015-09-25 11:46:56 -0700172 return host2guc_action(guc, data, ARRAY_SIZE(data));
Alex Daif5d3c3e2015-08-18 14:34:47 -0700173}
174
Dave Gordon44a28b12015-08-12 15:43:41 +0100175/*
176 * Initialise, update, or clear doorbell data shared with the GuC
177 *
178 * These functions modify shared data and so need access to the mapped
179 * client object which contains the page being used for the doorbell
180 */
181
Dave Gordona6674292016-06-13 17:57:32 +0100182static int guc_update_doorbell_id(struct intel_guc *guc,
183 struct i915_guc_client *client,
184 u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100185{
Chris Wilson8b797af2016-08-15 10:48:51 +0100186 struct sg_table *sg = guc->ctx_pool_vma->pages;
Dave Gordona6674292016-06-13 17:57:32 +0100187 void *doorbell_bitmap = guc->doorbell_bitmap;
Dave Gordon44a28b12015-08-12 15:43:41 +0100188 struct guc_doorbell_info *doorbell;
Dave Gordona6674292016-06-13 17:57:32 +0100189 struct guc_context_desc desc;
190 size_t len;
Dave Gordon44a28b12015-08-12 15:43:41 +0100191
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100192 doorbell = client->client_base + client->doorbell_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100193
Dave Gordona6674292016-06-13 17:57:32 +0100194 if (client->doorbell_id != GUC_INVALID_DOORBELL_ID &&
195 test_bit(client->doorbell_id, doorbell_bitmap)) {
196 /* Deactivate the old doorbell */
197 doorbell->db_status = GUC_DOORBELL_DISABLED;
198 (void)host2guc_release_doorbell(guc, client);
199 __clear_bit(client->doorbell_id, doorbell_bitmap);
200 }
201
202 /* Update the GuC's idea of the doorbell ID */
203 len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
204 sizeof(desc) * client->ctx_index);
205 if (len != sizeof(desc))
206 return -EFAULT;
207 desc.db_id = new_id;
208 len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
209 sizeof(desc) * client->ctx_index);
210 if (len != sizeof(desc))
211 return -EFAULT;
212
213 client->doorbell_id = new_id;
214 if (new_id == GUC_INVALID_DOORBELL_ID)
215 return 0;
216
217 /* Activate the new doorbell */
218 __set_bit(new_id, doorbell_bitmap);
Dave Gordon44a28b12015-08-12 15:43:41 +0100219 doorbell->cookie = 0;
Dave Gordona6674292016-06-13 17:57:32 +0100220 doorbell->db_status = GUC_DOORBELL_ENABLED;
221 return host2guc_allocate_doorbell(guc, client);
222}
223
224static int guc_init_doorbell(struct intel_guc *guc,
225 struct i915_guc_client *client,
226 uint16_t db_id)
227{
228 return guc_update_doorbell_id(guc, client, db_id);
Dave Gordon44a28b12015-08-12 15:43:41 +0100229}
230
Dave Gordon44a28b12015-08-12 15:43:41 +0100231static void guc_disable_doorbell(struct intel_guc *guc,
232 struct i915_guc_client *client)
233{
Dave Gordona6674292016-06-13 17:57:32 +0100234 (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
Dave Gordon44a28b12015-08-12 15:43:41 +0100235
Dave Gordon44a28b12015-08-12 15:43:41 +0100236 /* XXX: wait for any interrupts */
237 /* XXX: wait for workqueue to drain */
238}
239
Dave Gordonf10d69a2016-06-13 17:57:33 +0100240static uint16_t
241select_doorbell_register(struct intel_guc *guc, uint32_t priority)
242{
243 /*
244 * The bitmap tracks which doorbell registers are currently in use.
245 * It is split into two halves; the first half is used for normal
246 * priority contexts, the second half for high-priority ones.
247 * Note that logically higher priorities are numerically less than
248 * normal ones, so the test below means "is it high-priority?"
249 */
250 const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
251 const uint16_t half = GUC_MAX_DOORBELLS / 2;
252 const uint16_t start = hi_pri ? half : 0;
253 const uint16_t end = start + half;
254 uint16_t id;
255
256 id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
257 if (id == end)
258 id = GUC_INVALID_DOORBELL_ID;
259
260 DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
261 hi_pri ? "high" : "normal", id);
262
263 return id;
264}
265
Dave Gordon44a28b12015-08-12 15:43:41 +0100266/*
267 * Select, assign and relase doorbell cachelines
268 *
269 * These functions track which doorbell cachelines are in use.
270 * The data they manipulate is protected by the host2guc lock.
271 */
272
273static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
274{
275 const uint32_t cacheline_size = cache_line_size();
276 uint32_t offset;
277
Dave Gordon44a28b12015-08-12 15:43:41 +0100278 /* Doorbell uses a single cache line within a page */
279 offset = offset_in_page(guc->db_cacheline);
280
281 /* Moving to next cache line to reduce contention */
282 guc->db_cacheline += cacheline_size;
283
Dave Gordon44a28b12015-08-12 15:43:41 +0100284 DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
285 offset, guc->db_cacheline, cacheline_size);
286
287 return offset;
288}
289
Dave Gordon44a28b12015-08-12 15:43:41 +0100290/*
291 * Initialise the process descriptor shared with the GuC firmware.
292 */
293static void guc_init_proc_desc(struct intel_guc *guc,
294 struct i915_guc_client *client)
295{
296 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100297
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100298 desc = client->client_base + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100299
300 memset(desc, 0, sizeof(*desc));
301
302 /*
303 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
304 * space for ring3 clients (set them as in mmap_ioctl) or kernel
305 * space for kernel clients (map on demand instead? May make debug
306 * easier to have it mapped).
307 */
308 desc->wq_base_addr = 0;
309 desc->db_base_addr = 0;
310
311 desc->context_id = client->ctx_index;
312 desc->wq_size_bytes = client->wq_size;
313 desc->wq_status = WQ_STATUS_ACTIVE;
314 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100315}
316
317/*
318 * Initialise/clear the context descriptor shared with the GuC firmware.
319 *
320 * This descriptor tells the GuC where (in GGTT space) to find the important
321 * data structures relating to this client (doorbell, process descriptor,
322 * write queue, etc).
323 */
324
325static void guc_init_ctx_desc(struct intel_guc *guc,
326 struct i915_guc_client *client)
327{
Alex Dai397097b2016-01-23 11:58:14 -0800328 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000329 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100330 struct i915_gem_context *ctx = client->owner;
Dave Gordon44a28b12015-08-12 15:43:41 +0100331 struct guc_context_desc desc;
332 struct sg_table *sg;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100333 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100334
335 memset(&desc, 0, sizeof(desc));
336
337 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
338 desc.context_id = client->ctx_index;
339 desc.priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100340 desc.db_id = client->doorbell_id;
341
Dave Gordone02757d2016-08-09 15:19:21 +0100342 for_each_engine_masked(engine, dev_priv, client->engines) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100343 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100344 uint32_t guc_engine_id = engine->guc_id;
345 struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100346 struct drm_i915_gem_object *obj;
Alex Daid1675192015-08-12 15:43:43 +0100347
348 /* TODO: We have a design issue to be solved here. Only when we
349 * receive the first batch, we know which engine is used by the
350 * user. But here GuC expects the lrc and ring to be pinned. It
351 * is not an issue for default context, which is the only one
352 * for now who owns a GuC client. But for future owner of GuC
353 * client, need to make sure lrc is pinned prior to enter here.
354 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100355 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100356 break; /* XXX: continue? */
357
Chris Wilson9021ad02016-05-24 14:53:37 +0100358 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100359
360 /* The state page is after PPHWSP */
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100361 gfx_addr = ce->state->node.start;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100362 lrc->ring_lcra = gfx_addr + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100363 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100364 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100365
Chris Wilsondca33ec2016-08-02 22:50:20 +0100366 obj = ce->ring->obj;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100367 gfx_addr = i915_gem_obj_ggtt_offset(obj);
Alex Daid1675192015-08-12 15:43:43 +0100368
Dave Gordon86e06cc2016-04-19 16:08:36 +0100369 lrc->ring_begin = gfx_addr;
370 lrc->ring_end = gfx_addr + obj->base.size - 1;
371 lrc->ring_next_free_location = gfx_addr;
Alex Daid1675192015-08-12 15:43:43 +0100372 lrc->ring_current_tail_pointer_value = 0;
373
Dave Gordonc18468c2016-08-09 15:19:22 +0100374 desc.engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100375 }
376
Dave Gordone02757d2016-08-09 15:19:21 +0100377 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
378 client->engines, desc.engines_used);
Alex Daid1675192015-08-12 15:43:43 +0100379 WARN_ON(desc.engines_used == 0);
380
Dave Gordon44a28b12015-08-12 15:43:41 +0100381 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100382 * The doorbell, process descriptor, and workqueue are all parts
383 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100384 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100385 gfx_addr = client->vma->node.start;
386 desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100387 client->doorbell_offset;
388 desc.db_trigger_cpu = (uintptr_t)client->client_base +
389 client->doorbell_offset;
390 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
391 desc.process_desc = gfx_addr + client->proc_desc_offset;
392 desc.wq_addr = gfx_addr + client->wq_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100393 desc.wq_size = client->wq_size;
394
395 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100396 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100397 * IsKMDCreatedContext client
398 */
399 desc.desc_private = (uintptr_t)client;
400
401 /* Pool context is pinned already */
Chris Wilson8b797af2016-08-15 10:48:51 +0100402 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100403 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
404 sizeof(desc) * client->ctx_index);
405}
406
407static void guc_fini_ctx_desc(struct intel_guc *guc,
408 struct i915_guc_client *client)
409{
410 struct guc_context_desc desc;
411 struct sg_table *sg;
412
413 memset(&desc, 0, sizeof(desc));
414
Chris Wilson8b797af2016-08-15 10:48:51 +0100415 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100416 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
417 sizeof(desc) * client->ctx_index);
418}
419
Dave Gordon7c2c2702016-05-13 15:36:32 +0100420/**
421 * i915_guc_wq_check_space() - check that the GuC can accept a request
422 * @request: request associated with the commands
423 *
424 * Return: 0 if space is available
425 * -EAGAIN if space is not currently available
426 *
427 * This function must be called (and must return 0) before a request
428 * is submitted to the GuC via i915_guc_submit() below. Once a result
429 * of 0 has been returned, it remains valid until (but only until)
430 * the next call to submit().
431 *
432 * This precheck allows the caller to determine in advance that space
433 * will be available for the next submission before committing resources
434 * to it, and helps avoid late failures with complicated recovery paths.
435 */
436int i915_guc_wq_check_space(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100437{
Dave Gordon551aaec2016-05-13 15:36:33 +0100438 const size_t wqi_size = sizeof(struct guc_wq_item);
Dave Gordon7c2c2702016-05-13 15:36:32 +0100439 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100440 struct guc_process_desc *desc;
Dave Gordon551aaec2016-05-13 15:36:33 +0100441 u32 freespace;
Dave Gordon44a28b12015-08-12 15:43:41 +0100442
Dave Gordon7c2c2702016-05-13 15:36:32 +0100443 GEM_BUG_ON(gc == NULL);
Alex Daia7e02192015-12-16 11:45:55 -0800444
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100445 desc = gc->client_base + gc->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100446
Dave Gordon551aaec2016-05-13 15:36:33 +0100447 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
448 if (likely(freespace >= wqi_size))
449 return 0;
Alex Dai5a843302015-12-02 16:56:29 -0800450
Dave Gordon551aaec2016-05-13 15:36:33 +0100451 gc->no_wq_space += 1;
Dave Gordon44a28b12015-08-12 15:43:41 +0100452
Dave Gordon551aaec2016-05-13 15:36:33 +0100453 return -EAGAIN;
Dave Gordon44a28b12015-08-12 15:43:41 +0100454}
455
Dave Gordon0a31afb2016-05-13 15:36:34 +0100456static void guc_add_workqueue_item(struct i915_guc_client *gc,
457 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100458{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100459 /* wqi_len is in DWords, and does not include the one-word header */
460 const size_t wqi_size = sizeof(struct guc_wq_item);
461 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100462 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100463 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100464 struct guc_wq_item *wqi;
465 void *base;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100466 u32 freespace, tail, wq_off, wq_page;
Dave Gordon44a28b12015-08-12 15:43:41 +0100467
Alex Daia5916e82016-04-19 16:08:35 +0100468 desc = gc->client_base + gc->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800469
Dave Gordon0a31afb2016-05-13 15:36:34 +0100470 /* Free space is guaranteed, see i915_guc_wq_check_space() above */
471 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
472 GEM_BUG_ON(freespace < wqi_size);
473
474 /* The GuC firmware wants the tail index in QWords, not bytes */
475 tail = rq->tail;
476 GEM_BUG_ON(tail & 7);
477 tail >>= 3;
478 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100479
480 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
481 * should not have the case where structure wqi is across page, neither
482 * wrapped to the beginning. This simplifies the implementation below.
483 *
484 * XXX: if not the case, we need save data to a temp wqi and copy it to
485 * workqueue buffer dw by dw.
486 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100487 BUILD_BUG_ON(wqi_size != 16);
Dave Gordon44a28b12015-08-12 15:43:41 +0100488
Dave Gordon0a31afb2016-05-13 15:36:34 +0100489 /* postincrement WQ tail for next time */
490 wq_off = gc->wq_tail;
491 gc->wq_tail += wqi_size;
492 gc->wq_tail &= gc->wq_size - 1;
493 GEM_BUG_ON(wq_off & (wqi_size - 1));
494
495 /* WQ starts from the page after doorbell / process_desc */
496 wq_page = (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT;
Dave Gordon44a28b12015-08-12 15:43:41 +0100497 wq_off &= PAGE_SIZE - 1;
Chris Wilson8b797af2016-08-15 10:48:51 +0100498 base = kmap_atomic(i915_gem_object_get_page(gc->vma->obj, wq_page));
Dave Gordon44a28b12015-08-12 15:43:41 +0100499 wqi = (struct guc_wq_item *)((char *)base + wq_off);
500
Dave Gordon0a31afb2016-05-13 15:36:34 +0100501 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100502 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100503 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100504 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100505 WQ_NO_WCFLUSH_WAIT;
506
507 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100508 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100509
Dave Gordon44a28b12015-08-12 15:43:41 +0100510 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson04769652016-07-20 09:21:11 +0100511 wqi->fence_id = rq->fence.seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100512
513 kunmap_atomic(base);
Dave Gordon44a28b12015-08-12 15:43:41 +0100514}
515
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100516static int guc_ring_doorbell(struct i915_guc_client *gc)
517{
518 struct guc_process_desc *desc;
519 union guc_doorbell_qw db_cmp, db_exc, db_ret;
520 union guc_doorbell_qw *db;
521 int attempt = 2, ret = -EAGAIN;
522
523 desc = gc->client_base + gc->proc_desc_offset;
524
525 /* Update the tail so it is visible to GuC */
526 desc->tail = gc->wq_tail;
527
528 /* current cookie */
529 db_cmp.db_status = GUC_DOORBELL_ENABLED;
530 db_cmp.cookie = gc->cookie;
531
532 /* cookie to be updated */
533 db_exc.db_status = GUC_DOORBELL_ENABLED;
534 db_exc.cookie = gc->cookie + 1;
535 if (db_exc.cookie == 0)
536 db_exc.cookie = 1;
537
538 /* pointer of current doorbell cacheline */
539 db = gc->client_base + gc->doorbell_offset;
540
541 while (attempt--) {
542 /* lets ring the doorbell */
543 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
544 db_cmp.value_qw, db_exc.value_qw);
545
546 /* if the exchange was successfully executed */
547 if (db_ret.value_qw == db_cmp.value_qw) {
548 /* db was successfully rung */
549 gc->cookie = db_exc.cookie;
550 ret = 0;
551 break;
552 }
553
554 /* XXX: doorbell was lost and need to acquire it again */
555 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
556 break;
557
558 DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n",
559 db_cmp.cookie, db_ret.cookie);
560
561 /* update the cookie to newly read cookie from GuC */
562 db_cmp.cookie = db_ret.cookie;
563 db_exc.cookie = db_ret.cookie + 1;
564 if (db_exc.cookie == 0)
565 db_exc.cookie = 1;
566 }
567
568 return ret;
569}
570
Dave Gordon44a28b12015-08-12 15:43:41 +0100571/**
572 * i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700573 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100574 *
Dave Gordon7c2c2702016-05-13 15:36:32 +0100575 * Return: 0 on success, otherwise an errno.
576 * (Note: nonzero really shouldn't happen!)
577 *
578 * The caller must have already called i915_guc_wq_check_space() above
579 * with a result of 0 (success) since the last request submission. This
580 * guarantees that there is space in the work queue for the new request,
581 * so enqueuing the item cannot fail.
582 *
583 * Bad Things Will Happen if the caller violates this protocol e.g. calls
584 * submit() when check() says there's no space, or calls submit() multiple
585 * times with no intervening check().
586 *
587 * The only error here arises if the doorbell hardware isn't functioning
588 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100589 */
Chris Wilsonddd66c52016-08-02 22:50:31 +0100590static void i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100591{
Dave Gordon0b63bb12016-06-20 15:18:07 +0100592 unsigned int engine_id = rq->engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100593 struct intel_guc *guc = &rq->i915->guc;
594 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100595 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100596
Dave Gordon0a31afb2016-05-13 15:36:34 +0100597 guc_add_workqueue_item(client, rq);
598 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100599
Alex Dai397097b2016-01-23 11:58:14 -0800600 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100601 client->retcode = b_ret;
602 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100603 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100604
Alex Dai397097b2016-01-23 11:58:14 -0800605 guc->submissions[engine_id] += 1;
Chris Wilson04769652016-07-20 09:21:11 +0100606 guc->last_seqno[engine_id] = rq->fence.seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100607}
608
609/*
610 * Everything below here is concerned with setup & teardown, and is
611 * therefore not part of the somewhat time-critical batch-submission
612 * path of i915_guc_submit() above.
613 */
614
615/**
Chris Wilson8b797af2016-08-15 10:48:51 +0100616 * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
617 * @guc: the guc
618 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100619 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100620 * This is a wrapper to create an object for use with the GuC. In order to
621 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
622 * both some backing storage and a range inside the Global GTT. We must pin
623 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
624 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100625 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100626 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100627 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100628static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100629{
Chris Wilson8b797af2016-08-15 10:48:51 +0100630 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100631 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100632 struct i915_vma *vma;
633 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100634
Chris Wilson91c8a322016-07-05 10:40:23 +0100635 obj = i915_gem_object_create(&dev_priv->drm, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100636 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100637 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100638
Chris Wilson8b797af2016-08-15 10:48:51 +0100639 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
640 if (IS_ERR(vma))
641 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100642
Chris Wilson8b797af2016-08-15 10:48:51 +0100643 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
644 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
645 if (ret) {
646 vma = ERR_PTR(ret);
647 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100648 }
649
650 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
651 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
652
Chris Wilson8b797af2016-08-15 10:48:51 +0100653 return vma;
654
655err:
656 i915_gem_object_put(obj);
657 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100658}
659
660/**
Chris Wilson8b797af2016-08-15 10:48:51 +0100661 * guc_release_vma() - Release gem object allocated for GuC usage
662 * @vma: gem obj to be released
Ville Syrjälä81fd8742015-11-25 16:21:30 +0200663 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100664static void guc_release_vma(struct i915_vma *vma)
Alex Daibac427f2015-08-12 15:43:39 +0100665{
Chris Wilson8b797af2016-08-15 10:48:51 +0100666 if (!vma)
Alex Daibac427f2015-08-12 15:43:39 +0100667 return;
668
Chris Wilson8b797af2016-08-15 10:48:51 +0100669 i915_vma_unpin(vma);
670 i915_vma_put(vma);
Alex Daibac427f2015-08-12 15:43:39 +0100671}
672
Dave Gordon0daf5562016-06-10 18:29:25 +0100673static void
674guc_client_free(struct drm_i915_private *dev_priv,
675 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100676{
Dave Gordon44a28b12015-08-12 15:43:41 +0100677 struct intel_guc *guc = &dev_priv->guc;
678
679 if (!client)
680 return;
681
Dave Gordon44a28b12015-08-12 15:43:41 +0100682 /*
683 * XXX: wait for any outstanding submissions before freeing memory.
684 * Be sure to drop any locks
685 */
686
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100687 if (client->client_base) {
688 /*
Dave Gordona6674292016-06-13 17:57:32 +0100689 * If we got as far as setting up a doorbell, make sure we
690 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100691 */
Dave Gordona6674292016-06-13 17:57:32 +0100692 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100693
694 kunmap(kmap_to_page(client->client_base));
695 }
696
Chris Wilson8b797af2016-08-15 10:48:51 +0100697 guc_release_vma(client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100698
699 if (client->ctx_index != GUC_INVALID_CTX_ID) {
700 guc_fini_ctx_desc(guc, client);
701 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
702 }
703
704 kfree(client);
705}
706
Dave Gordon84b7f882016-08-09 15:19:20 +0100707/* Check that a doorbell register is in the expected state */
708static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
709{
710 struct drm_i915_private *dev_priv = guc_to_i915(guc);
711 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
712 uint32_t value = I915_READ(drbreg);
713 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
714 bool expected = test_bit(db_id, guc->doorbell_bitmap);
715
716 if (enabled == expected)
717 return true;
718
719 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
720 db_id, drbreg.reg, value,
721 expected ? "active" : "inactive");
722
723 return false;
724}
725
Dave Gordon4d757872016-06-13 17:57:34 +0100726/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100727 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100728 * in turn, to ensure that all doorbell h/w is (re)initialised.
729 */
730static void guc_init_doorbell_hw(struct intel_guc *guc)
731{
Dave Gordon4d757872016-06-13 17:57:34 +0100732 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100733 uint16_t db_id;
734 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100735
Dave Gordon84b7f882016-08-09 15:19:20 +0100736 /* Save client's original doorbell selection */
Dave Gordon4d757872016-06-13 17:57:34 +0100737 db_id = client->doorbell_id;
738
739 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100740 /* Skip if doorbell is OK */
741 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100742 continue;
743
Dave Gordon4d757872016-06-13 17:57:34 +0100744 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100745 if (err)
746 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
747 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100748 }
749
750 /* Restore to original value */
751 err = guc_update_doorbell_id(guc, client, db_id);
752 if (err)
753 DRM_ERROR("Failed to restore doorbell to %d, err %d\n",
754 db_id, err);
755
Dave Gordon84b7f882016-08-09 15:19:20 +0100756 /* Read back & verify all doorbell registers */
757 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
758 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100759}
760
Dave Gordon44a28b12015-08-12 15:43:41 +0100761/**
762 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100763 * @dev_priv: driver private data structure
Dave Gordon44a28b12015-08-12 15:43:41 +0100764 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
765 * The kernel client to replace ExecList submission is created with
766 * NORMAL priority. Priority of a client for scheduler can be HIGH,
767 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700768 * @ctx: the context that owns the client (we use the default render
769 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100770 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100771 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100772 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100773static struct i915_guc_client *
774guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100775 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100776 uint32_t priority,
777 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100778{
779 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100780 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100781 struct i915_vma *vma;
Dave Gordona6674292016-06-13 17:57:32 +0100782 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100783
784 client = kzalloc(sizeof(*client), GFP_KERNEL);
785 if (!client)
786 return NULL;
787
Alex Daid1675192015-08-12 15:43:43 +0100788 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100789 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100790 client->engines = engines;
791 client->priority = priority;
792 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100793
794 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
795 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
796 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
797 client->ctx_index = GUC_INVALID_CTX_ID;
798 goto err;
799 }
800
801 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100802 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
803 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100804 goto err;
805
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100806 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100807 client->vma = vma;
808 client->client_base = kmap(i915_vma_first_page(vma));
Dave Gordon44a28b12015-08-12 15:43:41 +0100809 client->wq_offset = GUC_DB_SIZE;
810 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100811
Dave Gordonf10d69a2016-06-13 17:57:33 +0100812 db_id = select_doorbell_register(guc, client->priority);
813 if (db_id == GUC_INVALID_DOORBELL_ID)
814 /* XXX: evict a doorbell instead? */
815 goto err;
816
Dave Gordon44a28b12015-08-12 15:43:41 +0100817 client->doorbell_offset = select_doorbell_cacheline(guc);
818
819 /*
820 * Since the doorbell only requires a single cacheline, we can save
821 * space by putting the application process descriptor in the same
822 * page. Use the half of the page that doesn't include the doorbell.
823 */
824 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
825 client->proc_desc_offset = 0;
826 else
827 client->proc_desc_offset = (GUC_DB_SIZE / 2);
828
Dave Gordon44a28b12015-08-12 15:43:41 +0100829 guc_init_proc_desc(guc, client);
830 guc_init_ctx_desc(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100831 if (guc_init_doorbell(guc, client, db_id))
Dave Gordon44a28b12015-08-12 15:43:41 +0100832 goto err;
833
Dave Gordone02757d2016-08-09 15:19:21 +0100834 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
835 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100836 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
837 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100838
839 return client;
840
841err:
842 DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
843
Dave Gordon0daf5562016-06-10 18:29:25 +0100844 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100845 return NULL;
846}
847
Alex Dai4c7e77f2015-08-12 15:43:40 +0100848static void guc_create_log(struct intel_guc *guc)
849{
Chris Wilson8b797af2016-08-15 10:48:51 +0100850 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +0100851 unsigned long offset;
852 uint32_t size, flags;
853
854 if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
855 return;
856
857 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
858 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
859
860 /* The first page is to save log buffer state. Allocate one
861 * extra page for others in case for overlap */
862 size = (1 + GUC_LOG_DPC_PAGES + 1 +
863 GUC_LOG_ISR_PAGES + 1 +
864 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
865
Chris Wilson8b797af2016-08-15 10:48:51 +0100866 vma = guc->log_vma;
867 if (!vma) {
868 vma = guc_allocate_vma(guc, size);
869 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +0100870 /* logging will be off */
871 i915.guc_log_level = -1;
872 return;
873 }
874
Chris Wilson8b797af2016-08-15 10:48:51 +0100875 guc->log_vma = vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +0100876 }
877
878 /* each allocated unit is a page */
879 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
880 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
881 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
882 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
883
Chris Wilson8b797af2016-08-15 10:48:51 +0100884 offset = vma->node.start >> PAGE_SHIFT; /* in pages */
Alex Dai4c7e77f2015-08-12 15:43:40 +0100885 guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
886}
887
Alex Dai463704d2015-12-18 12:00:10 -0800888static void init_guc_policies(struct guc_policies *policies)
889{
890 struct guc_policy *policy;
891 u32 p, i;
892
893 policies->dpc_promote_time = 500000;
894 policies->max_num_work_items = POLICY_MAX_NUM_WI;
895
896 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -0800897 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -0800898 policy = &policies->policy[p][i];
899
900 policy->execution_quantum = 1000000;
901 policy->preemption_time = 500000;
902 policy->fault_time = 250000;
903 policy->policy_flags = 0;
904 }
905 }
906
907 policies->is_valid = 1;
908}
909
Alex Dai68371a92015-12-18 12:00:09 -0800910static void guc_create_ads(struct intel_guc *guc)
911{
912 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +0100913 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -0800914 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -0800915 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -0800916 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000917 struct intel_engine_cs *engine;
Alex Dai68371a92015-12-18 12:00:09 -0800918 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +0000919 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -0800920
921 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -0800922 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
923 sizeof(struct guc_mmio_reg_state) +
924 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -0800925
Chris Wilson8b797af2016-08-15 10:48:51 +0100926 vma = guc->ads_vma;
927 if (!vma) {
928 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
929 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -0800930 return;
931
Chris Wilson8b797af2016-08-15 10:48:51 +0100932 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -0800933 }
934
Chris Wilson8b797af2016-08-15 10:48:51 +0100935 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -0800936 ads = kmap(page);
937
938 /*
939 * The GuC requires a "Golden Context" when it reinitialises
940 * engines after a reset. Here we use the Render ring default
941 * context, which must already exist and be pinned in the GGTT,
942 * so its address won't change after we've told the GuC where
943 * to find it.
944 */
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000945 engine = &dev_priv->engine[RCS];
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000946 ads->golden_context_lrca = engine->status_page.gfx_addr;
Alex Dai68371a92015-12-18 12:00:09 -0800947
Dave Gordonb4ac5af2016-03-24 11:20:38 +0000948 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000949 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -0800950
Alex Dai463704d2015-12-18 12:00:10 -0800951 /* GuC scheduling policies */
952 policies = (void *)ads + sizeof(struct guc_ads);
953 init_guc_policies(policies);
954
Chris Wilson8b797af2016-08-15 10:48:51 +0100955 ads->scheduler_policies = vma->node.start + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -0800956
Alex Dai5c148e02015-12-18 12:00:11 -0800957 /* MMIO reg state */
958 reg_state = (void *)policies + sizeof(struct guc_policies);
959
Dave Gordonb4ac5af2016-03-24 11:20:38 +0000960 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000961 reg_state->mmio_white_list[engine->guc_id].mmio_start =
962 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -0800963
964 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000965 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -0800966 }
967
968 ads->reg_state_addr = ads->scheduler_policies +
969 sizeof(struct guc_policies);
970
971 ads->reg_state_buffer = ads->reg_state_addr +
972 sizeof(struct guc_mmio_reg_state);
973
Alex Dai68371a92015-12-18 12:00:09 -0800974 kunmap(page);
975}
976
Alex Daibac427f2015-08-12 15:43:39 +0100977/*
978 * Set up the memory resources to be shared with the GuC. At this point,
979 * we require just one object that can be mapped through the GGTT.
980 */
Dave Gordonbeffa512016-06-10 18:29:26 +0100981int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +0100982{
Alex Daibac427f2015-08-12 15:43:39 +0100983 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100984 struct i915_vma *vma;
985 u32 size;
Alex Daibac427f2015-08-12 15:43:39 +0100986
Dave Gordon29fb72c2016-06-07 09:14:50 +0100987 /* Wipe bitmap & delete client in case of reinitialisation */
988 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +0100989 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +0100990
Alex Daibac427f2015-08-12 15:43:39 +0100991 if (!i915.enable_guc_submission)
992 return 0; /* not enabled */
993
Chris Wilson8b797af2016-08-15 10:48:51 +0100994 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +0100995 return 0; /* already allocated */
996
Chris Wilson8b797af2016-08-15 10:48:51 +0100997 size = PAGE_ALIGN(GUC_MAX_GPU_CONTEXTS*sizeof(struct guc_context_desc));
998 vma = guc_allocate_vma(guc, size);
999 if (IS_ERR(vma))
1000 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001001
Chris Wilson8b797af2016-08-15 10:48:51 +01001002 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001003 ida_init(&guc->ctx_ids);
Alex Dai4c7e77f2015-08-12 15:43:40 +01001004 guc_create_log(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001005 guc_create_ads(guc);
1006
Alex Daibac427f2015-08-12 15:43:39 +01001007 return 0;
1008}
1009
Dave Gordonbeffa512016-06-10 18:29:26 +01001010int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001011{
Dave Gordon44a28b12015-08-12 15:43:41 +01001012 struct intel_guc *guc = &dev_priv->guc;
1013 struct i915_guc_client *client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001014 struct intel_engine_cs *engine;
Dave Gordon44a28b12015-08-12 15:43:41 +01001015
1016 /* client for execbuf submission */
Dave Gordon0daf5562016-06-10 18:29:25 +01001017 client = guc_client_alloc(dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +01001018 INTEL_INFO(dev_priv)->ring_mask,
Chris Wilson0ca5fa32016-05-24 14:53:40 +01001019 GUC_CTX_PRIORITY_KMD_NORMAL,
1020 dev_priv->kernel_context);
Dave Gordon44a28b12015-08-12 15:43:41 +01001021 if (!client) {
1022 DRM_ERROR("Failed to create execbuf guc_client\n");
1023 return -ENOMEM;
1024 }
1025
1026 guc->execbuf_client = client;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001027 host2guc_sample_forcewake(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +01001028 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001029
Chris Wilsonddd66c52016-08-02 22:50:31 +01001030 /* Take over from manual control of ELSP (execlists) */
1031 for_each_engine(engine, dev_priv)
1032 engine->submit_request = i915_guc_submit;
1033
Dave Gordon44a28b12015-08-12 15:43:41 +01001034 return 0;
1035}
1036
Dave Gordonbeffa512016-06-10 18:29:26 +01001037void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001038{
Dave Gordon44a28b12015-08-12 15:43:41 +01001039 struct intel_guc *guc = &dev_priv->guc;
1040
Chris Wilsonddd66c52016-08-02 22:50:31 +01001041 if (!guc->execbuf_client)
1042 return;
1043
Chris Wilsonddd66c52016-08-02 22:50:31 +01001044 /* Revert back to manual ELSP submission */
1045 intel_execlists_enable_submission(dev_priv);
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +01001046
1047 guc_client_free(dev_priv, guc->execbuf_client);
1048 guc->execbuf_client = NULL;
Dave Gordon44a28b12015-08-12 15:43:41 +01001049}
1050
Dave Gordonbeffa512016-06-10 18:29:26 +01001051void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001052{
Alex Daibac427f2015-08-12 15:43:39 +01001053 struct intel_guc *guc = &dev_priv->guc;
1054
Chris Wilson8b797af2016-08-15 10:48:51 +01001055 guc_release_vma(fetch_and_zero(&guc->ads_vma));
1056 guc_release_vma(fetch_and_zero(&guc->log_vma));
Alex Dai68371a92015-12-18 12:00:09 -08001057
Chris Wilson8b797af2016-08-15 10:48:51 +01001058 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001059 ida_destroy(&guc->ctx_ids);
Chris Wilson8b797af2016-08-15 10:48:51 +01001060 guc_release_vma(fetch_and_zero(&guc->ctx_pool_vma));
Alex Daibac427f2015-08-12 15:43:39 +01001061}
Alex Daia1c41992015-09-30 09:46:37 -07001062
1063/**
1064 * intel_guc_suspend() - notify GuC entering suspend state
1065 * @dev: drm device
1066 */
1067int intel_guc_suspend(struct drm_device *dev)
1068{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001069 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001070 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001071 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001072 u32 data[3];
1073
Dave Gordonfce91f22016-05-20 11:42:42 +01001074 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001075 return 0;
1076
Dave Gordoned54c1a2016-01-19 19:02:54 +00001077 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001078
1079 data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
1080 /* any value greater than GUC_POWER_D0 */
1081 data[1] = GUC_POWER_D1;
1082 /* first page is shared data with GuC */
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001083 data[2] = ctx->engine[RCS].state->node.start;
Alex Daia1c41992015-09-30 09:46:37 -07001084
1085 return host2guc_action(guc, data, ARRAY_SIZE(data));
1086}
1087
1088
1089/**
1090 * intel_guc_resume() - notify GuC resuming from suspend state
1091 * @dev: drm device
1092 */
1093int intel_guc_resume(struct drm_device *dev)
1094{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001095 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001096 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001097 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001098 u32 data[3];
1099
Dave Gordonfce91f22016-05-20 11:42:42 +01001100 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001101 return 0;
1102
Dave Gordoned54c1a2016-01-19 19:02:54 +00001103 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001104
1105 data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
1106 data[1] = GUC_POWER_D0;
1107 /* first page is shared data with GuC */
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001108 data[2] = ctx->engine[RCS].state->node.start;
Alex Daia1c41992015-09-30 09:46:37 -07001109
1110 return host2guc_action(guc, data, ARRAY_SIZE(data));
1111}