drm/i915: Consolidate i915_vma_unpin_and_release()
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_guc_submission.c
CommitLineData
bac427f8
AD
1/*
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
44a28b1d 29/**
feda33ef 30 * DOC: GuC-based command submission
44a28b1d
DG
31 *
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);
44a28b1d
DG
89
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
ab0e455b
DG
100 /*
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);
44a28b1d
DG
108 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
44a28b1d
DG
127 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
f5d3c3ea
AD
158static 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;
93f25318 165 /* WaRsDisableCoarsePowerGating:skl,bxt */
61251512 166 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
93f25318
AD
167 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;
171
172 return host2guc_action(guc, data, ARRAY_SIZE(data));
f5d3c3ea
AD
173}
174
44a28b1d
DG
175/*
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
a667429b
DG
182static int guc_update_doorbell_id(struct intel_guc *guc,
183 struct i915_guc_client *client,
184 u16 new_id)
44a28b1d 185{
8b797af1 186 struct sg_table *sg = guc->ctx_pool_vma->pages;
a667429b 187 void *doorbell_bitmap = guc->doorbell_bitmap;
44a28b1d 188 struct guc_doorbell_info *doorbell;
a667429b
DG
189 struct guc_context_desc desc;
190 size_t len;
44a28b1d 191
0d92a6a4 192 doorbell = client->client_base + client->doorbell_offset;
44a28b1d 193
a667429b
DG
194 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);
44a28b1d 219 doorbell->cookie = 0;
a667429b
DG
220 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);
44a28b1d
DG
229}
230
44a28b1d
DG
231static void guc_disable_doorbell(struct intel_guc *guc,
232 struct i915_guc_client *client)
233{
a667429b 234 (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
44a28b1d 235
44a28b1d
DG
236 /* XXX: wait for any interrupts */
237 /* XXX: wait for workqueue to drain */
238}
239
f10d69a7
DG
240static 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
44a28b1d
DG
266/*
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
44a28b1d
DG
278 /* 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
44a28b1d
DG
284 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
44a28b1d
DG
290/*
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;
44a28b1d 297
0d92a6a4 298 desc = client->client_base + client->proc_desc_offset;
44a28b1d
DG
299
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;
44a28b1d
DG
315}
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{
397097b0 328 struct drm_i915_private *dev_priv = guc_to_i915(guc);
e2f80391 329 struct intel_engine_cs *engine;
e2efd130 330 struct i915_gem_context *ctx = client->owner;
44a28b1d
DG
331 struct guc_context_desc desc;
332 struct sg_table *sg;
86e06cc0 333 u32 gfx_addr;
44a28b1d
DG
334
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;
44a28b1d
DG
340 desc.db_id = client->doorbell_id;
341
e02757d9 342 for_each_engine_masked(engine, dev_priv, client->engines) {
9021ad03 343 struct intel_context *ce = &ctx->engine[engine->id];
c18468c4
DG
344 uint32_t guc_engine_id = engine->guc_id;
345 struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
d1675198
AD
346
347 /* TODO: We have a design issue to be solved here. Only when we
348 * receive the first batch, we know which engine is used by the
349 * user. But here GuC expects the lrc and ring to be pinned. It
350 * is not an issue for default context, which is the only one
351 * for now who owns a GuC client. But for future owner of GuC
352 * client, need to make sure lrc is pinned prior to enter here.
353 */
9021ad03 354 if (!ce->state)
d1675198
AD
355 break; /* XXX: continue? */
356
9021ad03 357 lrc->context_desc = lower_32_bits(ce->lrc_desc);
d1675198
AD
358
359 /* The state page is after PPHWSP */
57e88531
CW
360 lrc->ring_lcra =
361 ce->state->node.start + LRC_STATE_PN * PAGE_SIZE;
d1675198 362 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
c18468c4 363 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
d1675198 364
57e88531
CW
365 lrc->ring_begin = ce->ring->vma->node.start;
366 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
367 lrc->ring_next_free_location = lrc->ring_begin;
d1675198
AD
368 lrc->ring_current_tail_pointer_value = 0;
369
c18468c4 370 desc.engines_used |= (1 << guc_engine_id);
d1675198
AD
371 }
372
e02757d9
DG
373 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
374 client->engines, desc.engines_used);
d1675198
AD
375 WARN_ON(desc.engines_used == 0);
376
44a28b1d 377 /*
86e06cc0
DG
378 * The doorbell, process descriptor, and workqueue are all parts
379 * of the client object, which the GuC will reference via the GGTT
44a28b1d 380 */
8b797af1
CW
381 gfx_addr = client->vma->node.start;
382 desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
86e06cc0
DG
383 client->doorbell_offset;
384 desc.db_trigger_cpu = (uintptr_t)client->client_base +
385 client->doorbell_offset;
386 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
387 desc.process_desc = gfx_addr + client->proc_desc_offset;
388 desc.wq_addr = gfx_addr + client->wq_offset;
44a28b1d
DG
389 desc.wq_size = client->wq_size;
390
391 /*
e2efd130 392 * XXX: Take LRCs from an existing context if this is not an
44a28b1d
DG
393 * IsKMDCreatedContext client
394 */
395 desc.desc_private = (uintptr_t)client;
396
397 /* Pool context is pinned already */
8b797af1 398 sg = guc->ctx_pool_vma->pages;
44a28b1d
DG
399 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
400 sizeof(desc) * client->ctx_index);
401}
402
403static void guc_fini_ctx_desc(struct intel_guc *guc,
404 struct i915_guc_client *client)
405{
406 struct guc_context_desc desc;
407 struct sg_table *sg;
408
409 memset(&desc, 0, sizeof(desc));
410
8b797af1 411 sg = guc->ctx_pool_vma->pages;
44a28b1d
DG
412 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
413 sizeof(desc) * client->ctx_index);
414}
415
7c2c270d
DG
416/**
417 * i915_guc_wq_check_space() - check that the GuC can accept a request
418 * @request: request associated with the commands
419 *
420 * Return: 0 if space is available
421 * -EAGAIN if space is not currently available
422 *
423 * This function must be called (and must return 0) before a request
424 * is submitted to the GuC via i915_guc_submit() below. Once a result
425 * of 0 has been returned, it remains valid until (but only until)
426 * the next call to submit().
427 *
428 * This precheck allows the caller to determine in advance that space
429 * will be available for the next submission before committing resources
430 * to it, and helps avoid late failures with complicated recovery paths.
431 */
432int i915_guc_wq_check_space(struct drm_i915_gem_request *request)
44a28b1d 433{
551aaecd 434 const size_t wqi_size = sizeof(struct guc_wq_item);
7c2c270d 435 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
44a28b1d 436 struct guc_process_desc *desc;
551aaecd 437 u32 freespace;
44a28b1d 438
7c2c270d 439 GEM_BUG_ON(gc == NULL);
a7e02199 440
0d92a6a4 441 desc = gc->client_base + gc->proc_desc_offset;
44a28b1d 442
551aaecd
DG
443 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
444 if (likely(freespace >= wqi_size))
445 return 0;
5a843307 446
551aaecd 447 gc->no_wq_space += 1;
44a28b1d 448
551aaecd 449 return -EAGAIN;
44a28b1d
DG
450}
451
0a31afbc
DG
452static void guc_add_workqueue_item(struct i915_guc_client *gc,
453 struct drm_i915_gem_request *rq)
44a28b1d 454{
0a31afbc
DG
455 /* wqi_len is in DWords, and does not include the one-word header */
456 const size_t wqi_size = sizeof(struct guc_wq_item);
457 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
c18468c4 458 struct intel_engine_cs *engine = rq->engine;
a5916e8f 459 struct guc_process_desc *desc;
44a28b1d
DG
460 struct guc_wq_item *wqi;
461 void *base;
0a31afbc 462 u32 freespace, tail, wq_off, wq_page;
a7e02199 463
a5916e8f 464 desc = gc->client_base + gc->proc_desc_offset;
44a28b1d 465
0a31afbc
DG
466 /* Free space is guaranteed, see i915_guc_wq_check_space() above */
467 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
468 GEM_BUG_ON(freespace < wqi_size);
469
470 /* The GuC firmware wants the tail index in QWords, not bytes */
471 tail = rq->tail;
472 GEM_BUG_ON(tail & 7);
473 tail >>= 3;
474 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
44a28b1d
DG
475
476 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
477 * should not have the case where structure wqi is across page, neither
478 * wrapped to the beginning. This simplifies the implementation below.
479 *
480 * XXX: if not the case, we need save data to a temp wqi and copy it to
481 * workqueue buffer dw by dw.
482 */
0a31afbc 483 BUILD_BUG_ON(wqi_size != 16);
44a28b1d 484
0a31afbc
DG
485 /* postincrement WQ tail for next time */
486 wq_off = gc->wq_tail;
487 gc->wq_tail += wqi_size;
488 gc->wq_tail &= gc->wq_size - 1;
489 GEM_BUG_ON(wq_off & (wqi_size - 1));
490
491 /* WQ starts from the page after doorbell / process_desc */
492 wq_page = (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT;
44a28b1d 493 wq_off &= PAGE_SIZE - 1;
8b797af1 494 base = kmap_atomic(i915_gem_object_get_page(gc->vma->obj, wq_page));
44a28b1d
DG
495 wqi = (struct guc_wq_item *)((char *)base + wq_off);
496
0a31afbc 497 /* Now fill in the 4-word work queue item */
44a28b1d 498 wqi->header = WQ_TYPE_INORDER |
0a31afbc 499 (wqi_len << WQ_LEN_SHIFT) |
c18468c4 500 (engine->guc_id << WQ_TARGET_SHIFT) |
44a28b1d
DG
501 WQ_NO_WCFLUSH_WAIT;
502
503 /* The GuC wants only the low-order word of the context descriptor */
c18468c4 504 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
44a28b1d 505
44a28b1d 506 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
04769652 507 wqi->fence_id = rq->fence.seqno;
44a28b1d
DG
508
509 kunmap_atomic(base);
44a28b1d
DG
510}
511
10d2c3e2
DG
512static int guc_ring_doorbell(struct i915_guc_client *gc)
513{
514 struct guc_process_desc *desc;
515 union guc_doorbell_qw db_cmp, db_exc, db_ret;
516 union guc_doorbell_qw *db;
517 int attempt = 2, ret = -EAGAIN;
518
519 desc = gc->client_base + gc->proc_desc_offset;
520
521 /* Update the tail so it is visible to GuC */
522 desc->tail = gc->wq_tail;
523
524 /* current cookie */
525 db_cmp.db_status = GUC_DOORBELL_ENABLED;
526 db_cmp.cookie = gc->cookie;
527
528 /* cookie to be updated */
529 db_exc.db_status = GUC_DOORBELL_ENABLED;
530 db_exc.cookie = gc->cookie + 1;
531 if (db_exc.cookie == 0)
532 db_exc.cookie = 1;
533
534 /* pointer of current doorbell cacheline */
535 db = gc->client_base + gc->doorbell_offset;
536
537 while (attempt--) {
538 /* lets ring the doorbell */
539 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
540 db_cmp.value_qw, db_exc.value_qw);
541
542 /* if the exchange was successfully executed */
543 if (db_ret.value_qw == db_cmp.value_qw) {
544 /* db was successfully rung */
545 gc->cookie = db_exc.cookie;
546 ret = 0;
547 break;
548 }
549
550 /* XXX: doorbell was lost and need to acquire it again */
551 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
552 break;
553
554 DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n",
555 db_cmp.cookie, db_ret.cookie);
556
557 /* update the cookie to newly read cookie from GuC */
558 db_cmp.cookie = db_ret.cookie;
559 db_exc.cookie = db_ret.cookie + 1;
560 if (db_exc.cookie == 0)
561 db_exc.cookie = 1;
562 }
563
564 return ret;
565}
566
44a28b1d
DG
567/**
568 * i915_guc_submit() - Submit commands through GuC
feda33ef 569 * @rq: request associated with the commands
44a28b1d 570 *
7c2c270d
DG
571 * Return: 0 on success, otherwise an errno.
572 * (Note: nonzero really shouldn't happen!)
573 *
574 * The caller must have already called i915_guc_wq_check_space() above
575 * with a result of 0 (success) since the last request submission. This
576 * guarantees that there is space in the work queue for the new request,
577 * so enqueuing the item cannot fail.
578 *
579 * Bad Things Will Happen if the caller violates this protocol e.g. calls
580 * submit() when check() says there's no space, or calls submit() multiple
581 * times with no intervening check().
582 *
583 * The only error here arises if the doorbell hardware isn't functioning
584 * as expected, which really shouln't happen.
44a28b1d 585 */
ddd66c51 586static void i915_guc_submit(struct drm_i915_gem_request *rq)
44a28b1d 587{
0b63bb14 588 unsigned int engine_id = rq->engine->id;
7c2c270d
DG
589 struct intel_guc *guc = &rq->i915->guc;
590 struct i915_guc_client *client = guc->execbuf_client;
0a31afbc 591 int b_ret;
44a28b1d 592
0a31afbc
DG
593 guc_add_workqueue_item(client, rq);
594 b_ret = guc_ring_doorbell(client);
44a28b1d 595
397097b0 596 client->submissions[engine_id] += 1;
0a31afbc
DG
597 client->retcode = b_ret;
598 if (b_ret)
44a28b1d 599 client->b_fail += 1;
0a31afbc 600
397097b0 601 guc->submissions[engine_id] += 1;
04769652 602 guc->last_seqno[engine_id] = rq->fence.seqno;
44a28b1d
DG
603}
604
605/*
606 * Everything below here is concerned with setup & teardown, and is
607 * therefore not part of the somewhat time-critical batch-submission
608 * path of i915_guc_submit() above.
609 */
610
bac427f8 611/**
8b797af1
CW
612 * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
613 * @guc: the guc
614 * @size: size of area to allocate (both virtual space and memory)
bac427f8 615 *
8b797af1
CW
616 * This is a wrapper to create an object for use with the GuC. In order to
617 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
618 * both some backing storage and a range inside the Global GTT. We must pin
619 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
620 * range is reserved inside GuC.
bac427f8 621 *
8b797af1 622 * Return: A i915_vma if successful, otherwise an ERR_PTR.
bac427f8 623 */
8b797af1 624static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
bac427f8 625{
8b797af1 626 struct drm_i915_private *dev_priv = guc_to_i915(guc);
bac427f8 627 struct drm_i915_gem_object *obj;
8b797af1
CW
628 struct i915_vma *vma;
629 int ret;
bac427f8 630
91c8a326 631 obj = i915_gem_object_create(&dev_priv->drm, size);
fe3db79b 632 if (IS_ERR(obj))
8b797af1 633 return ERR_CAST(obj);
bac427f8 634
8b797af1
CW
635 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
636 if (IS_ERR(vma))
637 goto err;
bac427f8 638
8b797af1
CW
639 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
640 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
641 if (ret) {
642 vma = ERR_PTR(ret);
643 goto err;
bac427f8
AD
644 }
645
646 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
647 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
648
8b797af1
CW
649 return vma;
650
651err:
652 i915_gem_object_put(obj);
653 return vma;
bac427f8
AD
654}
655
0daf556c
DG
656static void
657guc_client_free(struct drm_i915_private *dev_priv,
658 struct i915_guc_client *client)
44a28b1d 659{
44a28b1d
DG
660 struct intel_guc *guc = &dev_priv->guc;
661
662 if (!client)
663 return;
664
44a28b1d
DG
665 /*
666 * XXX: wait for any outstanding submissions before freeing memory.
667 * Be sure to drop any locks
668 */
669
0d92a6a4
DG
670 if (client->client_base) {
671 /*
a667429b
DG
672 * If we got as far as setting up a doorbell, make sure we
673 * shut it down before unmapping & deallocating the memory.
0d92a6a4 674 */
a667429b 675 guc_disable_doorbell(guc, client);
0d92a6a4
DG
676
677 kunmap(kmap_to_page(client->client_base));
678 }
679
19880c4a 680 i915_vma_unpin_and_release(&client->vma);
44a28b1d
DG
681
682 if (client->ctx_index != GUC_INVALID_CTX_ID) {
683 guc_fini_ctx_desc(guc, client);
684 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
685 }
686
687 kfree(client);
688}
689
84b7f882
DG
690/* Check that a doorbell register is in the expected state */
691static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
692{
693 struct drm_i915_private *dev_priv = guc_to_i915(guc);
694 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
695 uint32_t value = I915_READ(drbreg);
696 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
697 bool expected = test_bit(db_id, guc->doorbell_bitmap);
698
699 if (enabled == expected)
700 return true;
701
702 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
703 db_id, drbreg.reg, value,
704 expected ? "active" : "inactive");
705
706 return false;
707}
708
4d75787b 709/*
8888cd01 710 * Borrow the first client to set up & tear down each unused doorbell
4d75787b
DG
711 * in turn, to ensure that all doorbell h/w is (re)initialised.
712 */
713static void guc_init_doorbell_hw(struct intel_guc *guc)
714{
4d75787b 715 struct i915_guc_client *client = guc->execbuf_client;
84b7f882
DG
716 uint16_t db_id;
717 int i, err;
4d75787b 718
84b7f882 719 /* Save client's original doorbell selection */
4d75787b
DG
720 db_id = client->doorbell_id;
721
722 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
84b7f882
DG
723 /* Skip if doorbell is OK */
724 if (guc_doorbell_check(guc, i))
8888cd01
DG
725 continue;
726
4d75787b 727 err = guc_update_doorbell_id(guc, client, i);
84b7f882
DG
728 if (err)
729 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
730 i, err);
4d75787b
DG
731 }
732
733 /* Restore to original value */
734 err = guc_update_doorbell_id(guc, client, db_id);
735 if (err)
736 DRM_ERROR("Failed to restore doorbell to %d, err %d\n",
737 db_id, err);
738
84b7f882
DG
739 /* Read back & verify all doorbell registers */
740 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
741 (void)guc_doorbell_check(guc, i);
4d75787b
DG
742}
743
44a28b1d
DG
744/**
745 * guc_client_alloc() - Allocate an i915_guc_client
0daf556c 746 * @dev_priv: driver private data structure
44a28b1d
DG
747 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
748 * The kernel client to replace ExecList submission is created with
749 * NORMAL priority. Priority of a client for scheduler can be HIGH,
750 * while a preemption context can use CRITICAL.
feda33ef
AD
751 * @ctx: the context that owns the client (we use the default render
752 * context)
44a28b1d 753 *
0d92a6a4 754 * Return: An i915_guc_client object if success, else NULL.
44a28b1d 755 */
0daf556c
DG
756static struct i915_guc_client *
757guc_client_alloc(struct drm_i915_private *dev_priv,
e02757d9 758 uint32_t engines,
0daf556c
DG
759 uint32_t priority,
760 struct i915_gem_context *ctx)
44a28b1d
DG
761{
762 struct i915_guc_client *client;
44a28b1d 763 struct intel_guc *guc = &dev_priv->guc;
8b797af1 764 struct i915_vma *vma;
a667429b 765 uint16_t db_id;
44a28b1d
DG
766
767 client = kzalloc(sizeof(*client), GFP_KERNEL);
768 if (!client)
769 return NULL;
770
d1675198 771 client->owner = ctx;
44a28b1d 772 client->guc = guc;
e02757d9
DG
773 client->engines = engines;
774 client->priority = priority;
775 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
44a28b1d
DG
776
777 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
778 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
779 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
780 client->ctx_index = GUC_INVALID_CTX_ID;
781 goto err;
782 }
783
784 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
8b797af1
CW
785 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
786 if (IS_ERR(vma))
44a28b1d
DG
787 goto err;
788
0d92a6a4 789 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
8b797af1
CW
790 client->vma = vma;
791 client->client_base = kmap(i915_vma_first_page(vma));
44a28b1d
DG
792 client->wq_offset = GUC_DB_SIZE;
793 client->wq_size = GUC_WQ_SIZE;
44a28b1d 794
f10d69a7
DG
795 db_id = select_doorbell_register(guc, client->priority);
796 if (db_id == GUC_INVALID_DOORBELL_ID)
797 /* XXX: evict a doorbell instead? */
798 goto err;
799
44a28b1d
DG
800 client->doorbell_offset = select_doorbell_cacheline(guc);
801
802 /*
803 * Since the doorbell only requires a single cacheline, we can save
804 * space by putting the application process descriptor in the same
805 * page. Use the half of the page that doesn't include the doorbell.
806 */
807 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
808 client->proc_desc_offset = 0;
809 else
810 client->proc_desc_offset = (GUC_DB_SIZE / 2);
811
44a28b1d
DG
812 guc_init_proc_desc(guc, client);
813 guc_init_ctx_desc(guc, client);
a667429b 814 if (guc_init_doorbell(guc, client, db_id))
44a28b1d
DG
815 goto err;
816
e02757d9
DG
817 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
818 priority, client, client->engines, client->ctx_index);
a667429b
DG
819 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
820 client->doorbell_id, client->doorbell_offset);
44a28b1d
DG
821
822 return client;
823
824err:
825 DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
826
0daf556c 827 guc_client_free(dev_priv, client);
44a28b1d
DG
828 return NULL;
829}
830
4c7e77fc
AD
831static void guc_create_log(struct intel_guc *guc)
832{
8b797af1 833 struct i915_vma *vma;
4c7e77fc
AD
834 unsigned long offset;
835 uint32_t size, flags;
836
837 if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
838 return;
839
840 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
841 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
842
843 /* The first page is to save log buffer state. Allocate one
844 * extra page for others in case for overlap */
845 size = (1 + GUC_LOG_DPC_PAGES + 1 +
846 GUC_LOG_ISR_PAGES + 1 +
847 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
848
8b797af1
CW
849 vma = guc->log_vma;
850 if (!vma) {
851 vma = guc_allocate_vma(guc, size);
852 if (IS_ERR(vma)) {
4c7e77fc
AD
853 /* logging will be off */
854 i915.guc_log_level = -1;
855 return;
856 }
857
8b797af1 858 guc->log_vma = vma;
4c7e77fc
AD
859 }
860
861 /* each allocated unit is a page */
862 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
863 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
864 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
865 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
866
8b797af1 867 offset = vma->node.start >> PAGE_SHIFT; /* in pages */
4c7e77fc
AD
868 guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
869}
870
463704d0
AD
871static void init_guc_policies(struct guc_policies *policies)
872{
873 struct guc_policy *policy;
874 u32 p, i;
875
876 policies->dpc_promote_time = 500000;
877 policies->max_num_work_items = POLICY_MAX_NUM_WI;
878
879 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
397097b0 880 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
463704d0
AD
881 policy = &policies->policy[p][i];
882
883 policy->execution_quantum = 1000000;
884 policy->preemption_time = 500000;
885 policy->fault_time = 250000;
886 policy->policy_flags = 0;
887 }
888 }
889
890 policies->is_valid = 1;
891}
892
68371a95
AD
893static void guc_create_ads(struct intel_guc *guc)
894{
895 struct drm_i915_private *dev_priv = guc_to_i915(guc);
8b797af1 896 struct i915_vma *vma;
68371a95 897 struct guc_ads *ads;
463704d0 898 struct guc_policies *policies;
5c148e04 899 struct guc_mmio_reg_state *reg_state;
e2f80391 900 struct intel_engine_cs *engine;
68371a95 901 struct page *page;
b4ac5afc 902 u32 size;
68371a95
AD
903
904 /* The ads obj includes the struct itself and buffers passed to GuC */
5c148e04
AD
905 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
906 sizeof(struct guc_mmio_reg_state) +
907 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
68371a95 908
8b797af1
CW
909 vma = guc->ads_vma;
910 if (!vma) {
911 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
912 if (IS_ERR(vma))
68371a95
AD
913 return;
914
8b797af1 915 guc->ads_vma = vma;
68371a95
AD
916 }
917
8b797af1 918 page = i915_vma_first_page(vma);
68371a95
AD
919 ads = kmap(page);
920
921 /*
922 * The GuC requires a "Golden Context" when it reinitialises
923 * engines after a reset. Here we use the Render ring default
924 * context, which must already exist and be pinned in the GGTT,
925 * so its address won't change after we've told the GuC where
926 * to find it.
927 */
4a570db5 928 engine = &dev_priv->engine[RCS];
57e88531 929 ads->golden_context_lrca = engine->status_page.ggtt_offset;
68371a95 930
b4ac5afc 931 for_each_engine(engine, dev_priv)
e2f80391 932 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
68371a95 933
463704d0
AD
934 /* GuC scheduling policies */
935 policies = (void *)ads + sizeof(struct guc_ads);
936 init_guc_policies(policies);
937
8b797af1 938 ads->scheduler_policies = vma->node.start + sizeof(struct guc_ads);
463704d0 939
5c148e04
AD
940 /* MMIO reg state */
941 reg_state = (void *)policies + sizeof(struct guc_policies);
942
b4ac5afc 943 for_each_engine(engine, dev_priv) {
e2f80391
TU
944 reg_state->mmio_white_list[engine->guc_id].mmio_start =
945 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
5c148e04
AD
946
947 /* Nothing to be saved or restored for now. */
e2f80391 948 reg_state->mmio_white_list[engine->guc_id].count = 0;
5c148e04
AD
949 }
950
951 ads->reg_state_addr = ads->scheduler_policies +
952 sizeof(struct guc_policies);
953
954 ads->reg_state_buffer = ads->reg_state_addr +
955 sizeof(struct guc_mmio_reg_state);
956
68371a95
AD
957 kunmap(page);
958}
959
bac427f8
AD
960/*
961 * Set up the memory resources to be shared with the GuC. At this point,
962 * we require just one object that can be mapped through the GGTT.
963 */
beffa517 964int i915_guc_submission_init(struct drm_i915_private *dev_priv)
bac427f8 965{
bac427f8 966 struct intel_guc *guc = &dev_priv->guc;
8b797af1
CW
967 struct i915_vma *vma;
968 u32 size;
bac427f8 969
29fb72c7
DG
970 /* Wipe bitmap & delete client in case of reinitialisation */
971 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
beffa517 972 i915_guc_submission_disable(dev_priv);
29fb72c7 973
bac427f8
AD
974 if (!i915.enable_guc_submission)
975 return 0; /* not enabled */
976
8b797af1 977 if (guc->ctx_pool_vma)
bac427f8
AD
978 return 0; /* already allocated */
979
8b797af1
CW
980 size = PAGE_ALIGN(GUC_MAX_GPU_CONTEXTS*sizeof(struct guc_context_desc));
981 vma = guc_allocate_vma(guc, size);
982 if (IS_ERR(vma))
983 return PTR_ERR(vma);
bac427f8 984
8b797af1 985 guc->ctx_pool_vma = vma;
bac427f8 986 ida_init(&guc->ctx_ids);
4c7e77fc 987 guc_create_log(guc);
68371a95
AD
988 guc_create_ads(guc);
989
bac427f8
AD
990 return 0;
991}
992
beffa517 993int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
44a28b1d 994{
44a28b1d
DG
995 struct intel_guc *guc = &dev_priv->guc;
996 struct i915_guc_client *client;
ddd66c51 997 struct intel_engine_cs *engine;
44a28b1d
DG
998
999 /* client for execbuf submission */
0daf556c 1000 client = guc_client_alloc(dev_priv,
e02757d9 1001 INTEL_INFO(dev_priv)->ring_mask,
0ca5fa3a
CW
1002 GUC_CTX_PRIORITY_KMD_NORMAL,
1003 dev_priv->kernel_context);
44a28b1d
DG
1004 if (!client) {
1005 DRM_ERROR("Failed to create execbuf guc_client\n");
1006 return -ENOMEM;
1007 }
1008
1009 guc->execbuf_client = client;
f5d3c3ea 1010 host2guc_sample_forcewake(guc, client);
4d75787b 1011 guc_init_doorbell_hw(guc);
f5d3c3ea 1012
ddd66c51
CW
1013 /* Take over from manual control of ELSP (execlists) */
1014 for_each_engine(engine, dev_priv)
1015 engine->submit_request = i915_guc_submit;
1016
44a28b1d
DG
1017 return 0;
1018}
1019
beffa517 1020void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
44a28b1d 1021{
44a28b1d
DG
1022 struct intel_guc *guc = &dev_priv->guc;
1023
ddd66c51
CW
1024 if (!guc->execbuf_client)
1025 return;
1026
ddd66c51
CW
1027 /* Revert back to manual ELSP submission */
1028 intel_execlists_enable_submission(dev_priv);
f4ea6bdd
CW
1029
1030 guc_client_free(dev_priv, guc->execbuf_client);
1031 guc->execbuf_client = NULL;
44a28b1d
DG
1032}
1033
beffa517 1034void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
bac427f8 1035{
bac427f8
AD
1036 struct intel_guc *guc = &dev_priv->guc;
1037
19880c4a
CW
1038 i915_vma_unpin_and_release(&guc->ads_vma);
1039 i915_vma_unpin_and_release(&guc->log_vma);
4c7e77fc 1040
8b797af1 1041 if (guc->ctx_pool_vma)
bac427f8 1042 ida_destroy(&guc->ctx_ids);
19880c4a 1043 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
bac427f8 1044}
a1c41994
AD
1045
1046/**
1047 * intel_guc_suspend() - notify GuC entering suspend state
1048 * @dev: drm device
1049 */
1050int intel_guc_suspend(struct drm_device *dev)
1051{
fac5e23e 1052 struct drm_i915_private *dev_priv = to_i915(dev);
a1c41994 1053 struct intel_guc *guc = &dev_priv->guc;
e2efd130 1054 struct i915_gem_context *ctx;
a1c41994
AD
1055 u32 data[3];
1056
fce91f22 1057 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
a1c41994
AD
1058 return 0;
1059
ed54c1a1 1060 ctx = dev_priv->kernel_context;
a1c41994
AD
1061
1062 data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
1063 /* any value greater than GUC_POWER_D0 */
1064 data[1] = GUC_POWER_D1;
1065 /* first page is shared data with GuC */
bf3783e5 1066 data[2] = ctx->engine[RCS].state->node.start;
a1c41994
AD
1067
1068 return host2guc_action(guc, data, ARRAY_SIZE(data));
1069}
1070
1071
1072/**
1073 * intel_guc_resume() - notify GuC resuming from suspend state
1074 * @dev: drm device
1075 */
1076int intel_guc_resume(struct drm_device *dev)
1077{
fac5e23e 1078 struct drm_i915_private *dev_priv = to_i915(dev);
a1c41994 1079 struct intel_guc *guc = &dev_priv->guc;
e2efd130 1080 struct i915_gem_context *ctx;
a1c41994
AD
1081 u32 data[3];
1082
fce91f22 1083 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
a1c41994
AD
1084 return 0;
1085
ed54c1a1 1086 ctx = dev_priv->kernel_context;
a1c41994
AD
1087
1088 data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
1089 data[1] = GUC_POWER_D0;
1090 /* first page is shared data with GuC */
bf3783e5 1091 data[2] = ctx->engine[RCS].state->node.start;
a1c41994
AD
1092
1093 return host2guc_action(guc, data, ARRAY_SIZE(data));
1094}
This page took 0.156378 seconds and 5 git commands to generate.