drm/i915/guc: move guc_ring_doorbell() nearer to callsite
[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
100 /* No HOST2GUC command should take longer than 10ms */
101 ret = wait_for_atomic(host2guc_action_response(dev_priv, &status), 10);
102 if (status != GUC2HOST_STATUS_SUCCESS) {
103 /*
104 * Either the GuC explicitly returned an error (which
105 * we convert to -EIO here) or no response at all was
106 * received within the timeout limit (-ETIMEDOUT)
107 */
108 if (ret != -ETIMEDOUT)
109 ret = -EIO;
110
111 DRM_ERROR("GUC: host2guc action 0x%X failed. ret=%d "
112 "status=0x%08X response=0x%08X\n",
113 data[0], ret, status,
114 I915_READ(SOFT_SCRATCH(15)));
115
116 dev_priv->guc.action_fail += 1;
117 dev_priv->guc.action_err = ret;
118 }
119 dev_priv->guc.action_status = status;
120
44a28b1d
DG
121 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
122
123 return ret;
124}
125
126/*
127 * Tell the GuC to allocate or deallocate a specific doorbell
128 */
129
130static int host2guc_allocate_doorbell(struct intel_guc *guc,
131 struct i915_guc_client *client)
132{
133 u32 data[2];
134
135 data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL;
136 data[1] = client->ctx_index;
137
138 return host2guc_action(guc, data, 2);
139}
140
141static int host2guc_release_doorbell(struct intel_guc *guc,
142 struct i915_guc_client *client)
143{
144 u32 data[2];
145
146 data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL;
147 data[1] = client->ctx_index;
148
149 return host2guc_action(guc, data, 2);
150}
151
f5d3c3ea
AD
152static int host2guc_sample_forcewake(struct intel_guc *guc,
153 struct i915_guc_client *client)
154{
155 struct drm_i915_private *dev_priv = guc_to_i915(guc);
93f25318 156 struct drm_device *dev = dev_priv->dev;
f5d3c3ea
AD
157 u32 data[2];
158
159 data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
93f25318 160 /* WaRsDisableCoarsePowerGating:skl,bxt */
dc97997a 161 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev))
93f25318
AD
162 data[1] = 0;
163 else
164 /* bit 0 and 1 are for Render and Media domain separately */
165 data[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
166
167 return host2guc_action(guc, data, ARRAY_SIZE(data));
f5d3c3ea
AD
168}
169
44a28b1d
DG
170/*
171 * Initialise, update, or clear doorbell data shared with the GuC
172 *
173 * These functions modify shared data and so need access to the mapped
174 * client object which contains the page being used for the doorbell
175 */
176
177static void guc_init_doorbell(struct intel_guc *guc,
178 struct i915_guc_client *client)
179{
180 struct guc_doorbell_info *doorbell;
44a28b1d 181
0d92a6a4 182 doorbell = client->client_base + client->doorbell_offset;
44a28b1d 183
0d92a6a4 184 doorbell->db_status = GUC_DOORBELL_ENABLED;
44a28b1d 185 doorbell->cookie = 0;
44a28b1d
DG
186}
187
44a28b1d
DG
188static void guc_disable_doorbell(struct intel_guc *guc,
189 struct i915_guc_client *client)
190{
191 struct drm_i915_private *dev_priv = guc_to_i915(guc);
192 struct guc_doorbell_info *doorbell;
f0f59a00 193 i915_reg_t drbreg = GEN8_DRBREGL(client->doorbell_id);
44a28b1d
DG
194 int value;
195
0d92a6a4 196 doorbell = client->client_base + client->doorbell_offset;
44a28b1d 197
0d92a6a4 198 doorbell->db_status = GUC_DOORBELL_DISABLED;
44a28b1d 199
44a28b1d
DG
200 value = I915_READ(drbreg);
201 WARN_ON((value & GEN8_DRB_VALID) != 0);
202
44a28b1d
DG
203 /* XXX: wait for any interrupts */
204 /* XXX: wait for workqueue to drain */
205}
206
207/*
208 * Select, assign and relase doorbell cachelines
209 *
210 * These functions track which doorbell cachelines are in use.
211 * The data they manipulate is protected by the host2guc lock.
212 */
213
214static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
215{
216 const uint32_t cacheline_size = cache_line_size();
217 uint32_t offset;
218
44a28b1d
DG
219 /* Doorbell uses a single cache line within a page */
220 offset = offset_in_page(guc->db_cacheline);
221
222 /* Moving to next cache line to reduce contention */
223 guc->db_cacheline += cacheline_size;
224
44a28b1d
DG
225 DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
226 offset, guc->db_cacheline, cacheline_size);
227
228 return offset;
229}
230
231static uint16_t assign_doorbell(struct intel_guc *guc, uint32_t priority)
232{
233 /*
234 * The bitmap is split into two halves; the first half is used for
235 * normal priority contexts, the second half for high-priority ones.
236 * Note that logically higher priorities are numerically less than
237 * normal ones, so the test below means "is it high-priority?"
238 */
239 const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
240 const uint16_t half = GUC_MAX_DOORBELLS / 2;
241 const uint16_t start = hi_pri ? half : 0;
242 const uint16_t end = start + half;
243 uint16_t id;
244
44a28b1d
DG
245 id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
246 if (id == end)
247 id = GUC_INVALID_DOORBELL_ID;
248 else
072a21cf 249 __set_bit(id, guc->doorbell_bitmap);
44a28b1d
DG
250
251 DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
252 hi_pri ? "high" : "normal", id);
253
254 return id;
255}
256
257static void release_doorbell(struct intel_guc *guc, uint16_t id)
258{
072a21cf 259 __clear_bit(id, guc->doorbell_bitmap);
44a28b1d
DG
260}
261
262/*
263 * Initialise the process descriptor shared with the GuC firmware.
264 */
265static void guc_init_proc_desc(struct intel_guc *guc,
266 struct i915_guc_client *client)
267{
268 struct guc_process_desc *desc;
44a28b1d 269
0d92a6a4 270 desc = client->client_base + client->proc_desc_offset;
44a28b1d
DG
271
272 memset(desc, 0, sizeof(*desc));
273
274 /*
275 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
276 * space for ring3 clients (set them as in mmap_ioctl) or kernel
277 * space for kernel clients (map on demand instead? May make debug
278 * easier to have it mapped).
279 */
280 desc->wq_base_addr = 0;
281 desc->db_base_addr = 0;
282
283 desc->context_id = client->ctx_index;
284 desc->wq_size_bytes = client->wq_size;
285 desc->wq_status = WQ_STATUS_ACTIVE;
286 desc->priority = client->priority;
44a28b1d
DG
287}
288
289/*
290 * Initialise/clear the context descriptor shared with the GuC firmware.
291 *
292 * This descriptor tells the GuC where (in GGTT space) to find the important
293 * data structures relating to this client (doorbell, process descriptor,
294 * write queue, etc).
295 */
296
297static void guc_init_ctx_desc(struct intel_guc *guc,
298 struct i915_guc_client *client)
299{
86e06cc0 300 struct drm_i915_gem_object *client_obj = client->client_obj;
397097b0 301 struct drm_i915_private *dev_priv = guc_to_i915(guc);
e2f80391 302 struct intel_engine_cs *engine;
e2efd130 303 struct i915_gem_context *ctx = client->owner;
44a28b1d
DG
304 struct guc_context_desc desc;
305 struct sg_table *sg;
86e06cc0 306 u32 gfx_addr;
44a28b1d
DG
307
308 memset(&desc, 0, sizeof(desc));
309
310 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
311 desc.context_id = client->ctx_index;
312 desc.priority = client->priority;
44a28b1d
DG
313 desc.db_id = client->doorbell_id;
314
9021ad03
CW
315 for_each_engine(engine, dev_priv) {
316 struct intel_context *ce = &ctx->engine[engine->id];
e2f80391 317 struct guc_execlist_context *lrc = &desc.lrc[engine->guc_id];
d1675198 318 struct drm_i915_gem_object *obj;
d1675198
AD
319
320 /* TODO: We have a design issue to be solved here. Only when we
321 * receive the first batch, we know which engine is used by the
322 * user. But here GuC expects the lrc and ring to be pinned. It
323 * is not an issue for default context, which is the only one
324 * for now who owns a GuC client. But for future owner of GuC
325 * client, need to make sure lrc is pinned prior to enter here.
326 */
9021ad03 327 if (!ce->state)
d1675198
AD
328 break; /* XXX: continue? */
329
9021ad03 330 lrc->context_desc = lower_32_bits(ce->lrc_desc);
d1675198
AD
331
332 /* The state page is after PPHWSP */
9021ad03 333 gfx_addr = i915_gem_obj_ggtt_offset(ce->state);
86e06cc0 334 lrc->ring_lcra = gfx_addr + LRC_STATE_PN * PAGE_SIZE;
d1675198 335 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
e2f80391 336 (engine->guc_id << GUC_ELC_ENGINE_OFFSET);
d1675198 337
9021ad03 338 obj = ce->ringbuf->obj;
86e06cc0 339 gfx_addr = i915_gem_obj_ggtt_offset(obj);
d1675198 340
86e06cc0
DG
341 lrc->ring_begin = gfx_addr;
342 lrc->ring_end = gfx_addr + obj->base.size - 1;
343 lrc->ring_next_free_location = gfx_addr;
d1675198
AD
344 lrc->ring_current_tail_pointer_value = 0;
345
e2f80391 346 desc.engines_used |= (1 << engine->guc_id);
d1675198
AD
347 }
348
349 WARN_ON(desc.engines_used == 0);
350
44a28b1d 351 /*
86e06cc0
DG
352 * The doorbell, process descriptor, and workqueue are all parts
353 * of the client object, which the GuC will reference via the GGTT
44a28b1d 354 */
86e06cc0
DG
355 gfx_addr = i915_gem_obj_ggtt_offset(client_obj);
356 desc.db_trigger_phy = sg_dma_address(client_obj->pages->sgl) +
357 client->doorbell_offset;
358 desc.db_trigger_cpu = (uintptr_t)client->client_base +
359 client->doorbell_offset;
360 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
361 desc.process_desc = gfx_addr + client->proc_desc_offset;
362 desc.wq_addr = gfx_addr + client->wq_offset;
44a28b1d
DG
363 desc.wq_size = client->wq_size;
364
365 /*
e2efd130 366 * XXX: Take LRCs from an existing context if this is not an
44a28b1d
DG
367 * IsKMDCreatedContext client
368 */
369 desc.desc_private = (uintptr_t)client;
370
371 /* Pool context is pinned already */
372 sg = guc->ctx_pool_obj->pages;
373 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
374 sizeof(desc) * client->ctx_index);
375}
376
377static void guc_fini_ctx_desc(struct intel_guc *guc,
378 struct i915_guc_client *client)
379{
380 struct guc_context_desc desc;
381 struct sg_table *sg;
382
383 memset(&desc, 0, sizeof(desc));
384
385 sg = guc->ctx_pool_obj->pages;
386 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
387 sizeof(desc) * client->ctx_index);
388}
389
7c2c270d
DG
390/**
391 * i915_guc_wq_check_space() - check that the GuC can accept a request
392 * @request: request associated with the commands
393 *
394 * Return: 0 if space is available
395 * -EAGAIN if space is not currently available
396 *
397 * This function must be called (and must return 0) before a request
398 * is submitted to the GuC via i915_guc_submit() below. Once a result
399 * of 0 has been returned, it remains valid until (but only until)
400 * the next call to submit().
401 *
402 * This precheck allows the caller to determine in advance that space
403 * will be available for the next submission before committing resources
404 * to it, and helps avoid late failures with complicated recovery paths.
405 */
406int i915_guc_wq_check_space(struct drm_i915_gem_request *request)
44a28b1d 407{
551aaecd 408 const size_t wqi_size = sizeof(struct guc_wq_item);
7c2c270d 409 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
44a28b1d 410 struct guc_process_desc *desc;
551aaecd 411 u32 freespace;
44a28b1d 412
7c2c270d 413 GEM_BUG_ON(gc == NULL);
a7e02199 414
0d92a6a4 415 desc = gc->client_base + gc->proc_desc_offset;
44a28b1d 416
551aaecd
DG
417 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
418 if (likely(freespace >= wqi_size))
419 return 0;
5a843307 420
551aaecd 421 gc->no_wq_space += 1;
44a28b1d 422
551aaecd 423 return -EAGAIN;
44a28b1d
DG
424}
425
0a31afbc
DG
426static void guc_add_workqueue_item(struct i915_guc_client *gc,
427 struct drm_i915_gem_request *rq)
44a28b1d 428{
0a31afbc
DG
429 /* wqi_len is in DWords, and does not include the one-word header */
430 const size_t wqi_size = sizeof(struct guc_wq_item);
431 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
a5916e8f 432 struct guc_process_desc *desc;
44a28b1d
DG
433 struct guc_wq_item *wqi;
434 void *base;
0a31afbc 435 u32 freespace, tail, wq_off, wq_page;
a7e02199 436
a5916e8f 437 desc = gc->client_base + gc->proc_desc_offset;
44a28b1d 438
0a31afbc
DG
439 /* Free space is guaranteed, see i915_guc_wq_check_space() above */
440 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
441 GEM_BUG_ON(freespace < wqi_size);
442
443 /* The GuC firmware wants the tail index in QWords, not bytes */
444 tail = rq->tail;
445 GEM_BUG_ON(tail & 7);
446 tail >>= 3;
447 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
44a28b1d
DG
448
449 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
450 * should not have the case where structure wqi is across page, neither
451 * wrapped to the beginning. This simplifies the implementation below.
452 *
453 * XXX: if not the case, we need save data to a temp wqi and copy it to
454 * workqueue buffer dw by dw.
455 */
0a31afbc 456 BUILD_BUG_ON(wqi_size != 16);
44a28b1d 457
0a31afbc
DG
458 /* postincrement WQ tail for next time */
459 wq_off = gc->wq_tail;
460 gc->wq_tail += wqi_size;
461 gc->wq_tail &= gc->wq_size - 1;
462 GEM_BUG_ON(wq_off & (wqi_size - 1));
463
464 /* WQ starts from the page after doorbell / process_desc */
465 wq_page = (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT;
44a28b1d 466 wq_off &= PAGE_SIZE - 1;
0a31afbc 467 base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, wq_page));
44a28b1d
DG
468 wqi = (struct guc_wq_item *)((char *)base + wq_off);
469
0a31afbc 470 /* Now fill in the 4-word work queue item */
44a28b1d 471 wqi->header = WQ_TYPE_INORDER |
0a31afbc 472 (wqi_len << WQ_LEN_SHIFT) |
4a570db5 473 (rq->engine->guc_id << WQ_TARGET_SHIFT) |
44a28b1d
DG
474 WQ_NO_WCFLUSH_WAIT;
475
476 /* The GuC wants only the low-order word of the context descriptor */
4a570db5
TU
477 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx,
478 rq->engine);
44a28b1d 479
44a28b1d 480 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
0a31afbc 481 wqi->fence_id = rq->seqno;
44a28b1d
DG
482
483 kunmap_atomic(base);
44a28b1d
DG
484}
485
10d2c3e2
DG
486static int guc_ring_doorbell(struct i915_guc_client *gc)
487{
488 struct guc_process_desc *desc;
489 union guc_doorbell_qw db_cmp, db_exc, db_ret;
490 union guc_doorbell_qw *db;
491 int attempt = 2, ret = -EAGAIN;
492
493 desc = gc->client_base + gc->proc_desc_offset;
494
495 /* Update the tail so it is visible to GuC */
496 desc->tail = gc->wq_tail;
497
498 /* current cookie */
499 db_cmp.db_status = GUC_DOORBELL_ENABLED;
500 db_cmp.cookie = gc->cookie;
501
502 /* cookie to be updated */
503 db_exc.db_status = GUC_DOORBELL_ENABLED;
504 db_exc.cookie = gc->cookie + 1;
505 if (db_exc.cookie == 0)
506 db_exc.cookie = 1;
507
508 /* pointer of current doorbell cacheline */
509 db = gc->client_base + gc->doorbell_offset;
510
511 while (attempt--) {
512 /* lets ring the doorbell */
513 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
514 db_cmp.value_qw, db_exc.value_qw);
515
516 /* if the exchange was successfully executed */
517 if (db_ret.value_qw == db_cmp.value_qw) {
518 /* db was successfully rung */
519 gc->cookie = db_exc.cookie;
520 ret = 0;
521 break;
522 }
523
524 /* XXX: doorbell was lost and need to acquire it again */
525 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
526 break;
527
528 DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n",
529 db_cmp.cookie, db_ret.cookie);
530
531 /* update the cookie to newly read cookie from GuC */
532 db_cmp.cookie = db_ret.cookie;
533 db_exc.cookie = db_ret.cookie + 1;
534 if (db_exc.cookie == 0)
535 db_exc.cookie = 1;
536 }
537
538 return ret;
539}
540
44a28b1d
DG
541/**
542 * i915_guc_submit() - Submit commands through GuC
feda33ef 543 * @rq: request associated with the commands
44a28b1d 544 *
7c2c270d
DG
545 * Return: 0 on success, otherwise an errno.
546 * (Note: nonzero really shouldn't happen!)
547 *
548 * The caller must have already called i915_guc_wq_check_space() above
549 * with a result of 0 (success) since the last request submission. This
550 * guarantees that there is space in the work queue for the new request,
551 * so enqueuing the item cannot fail.
552 *
553 * Bad Things Will Happen if the caller violates this protocol e.g. calls
554 * submit() when check() says there's no space, or calls submit() multiple
555 * times with no intervening check().
556 *
557 * The only error here arises if the doorbell hardware isn't functioning
558 * as expected, which really shouln't happen.
44a28b1d 559 */
7c2c270d 560int i915_guc_submit(struct drm_i915_gem_request *rq)
44a28b1d 561{
4a570db5 562 unsigned int engine_id = rq->engine->guc_id;
7c2c270d
DG
563 struct intel_guc *guc = &rq->i915->guc;
564 struct i915_guc_client *client = guc->execbuf_client;
0a31afbc 565 int b_ret;
44a28b1d 566
0a31afbc
DG
567 guc_add_workqueue_item(client, rq);
568 b_ret = guc_ring_doorbell(client);
44a28b1d 569
397097b0 570 client->submissions[engine_id] += 1;
0a31afbc
DG
571 client->retcode = b_ret;
572 if (b_ret)
44a28b1d 573 client->b_fail += 1;
0a31afbc 574
397097b0
AD
575 guc->submissions[engine_id] += 1;
576 guc->last_seqno[engine_id] = rq->seqno;
44a28b1d 577
0a31afbc 578 return b_ret;
44a28b1d
DG
579}
580
581/*
582 * Everything below here is concerned with setup & teardown, and is
583 * therefore not part of the somewhat time-critical batch-submission
584 * path of i915_guc_submit() above.
585 */
586
bac427f8
AD
587/**
588 * gem_allocate_guc_obj() - Allocate gem object for GuC usage
0daf556c 589 * @dev_priv: driver private data structure
bac427f8
AD
590 * @size: size of object
591 *
592 * This is a wrapper to create a gem obj. In order to use it inside GuC, the
593 * object needs to be pinned lifetime. Also we must pin it to gtt space other
594 * than [0, GUC_WOPCM_TOP) because this range is reserved inside GuC.
595 *
596 * Return: A drm_i915_gem_object if successful, otherwise NULL.
597 */
0daf556c
DG
598static struct drm_i915_gem_object *
599gem_allocate_guc_obj(struct drm_i915_private *dev_priv, u32 size)
bac427f8 600{
bac427f8
AD
601 struct drm_i915_gem_object *obj;
602
0daf556c 603 obj = i915_gem_object_create(dev_priv->dev, size);
fe3db79b 604 if (IS_ERR(obj))
bac427f8
AD
605 return NULL;
606
607 if (i915_gem_object_get_pages(obj)) {
608 drm_gem_object_unreference(&obj->base);
609 return NULL;
610 }
611
612 if (i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
613 PIN_OFFSET_BIAS | GUC_WOPCM_TOP)) {
614 drm_gem_object_unreference(&obj->base);
615 return NULL;
616 }
617
618 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
619 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
620
621 return obj;
622}
623
624/**
625 * gem_release_guc_obj() - Release gem object allocated for GuC usage
626 * @obj: gem obj to be released
81fd874e 627 */
bac427f8
AD
628static void gem_release_guc_obj(struct drm_i915_gem_object *obj)
629{
630 if (!obj)
631 return;
632
633 if (i915_gem_obj_is_pinned(obj))
634 i915_gem_object_ggtt_unpin(obj);
635
636 drm_gem_object_unreference(&obj->base);
637}
638
0daf556c
DG
639static void
640guc_client_free(struct drm_i915_private *dev_priv,
641 struct i915_guc_client *client)
44a28b1d 642{
44a28b1d
DG
643 struct intel_guc *guc = &dev_priv->guc;
644
645 if (!client)
646 return;
647
44a28b1d
DG
648 /*
649 * XXX: wait for any outstanding submissions before freeing memory.
650 * Be sure to drop any locks
651 */
652
0d92a6a4 653 if (client->client_base) {
29fb72c7
DG
654 uint16_t db_id = client->doorbell_id;
655
0d92a6a4
DG
656 /*
657 * If we got as far as setting up a doorbell, make sure
658 * we shut it down before unmapping & deallocating the
659 * memory. So first disable the doorbell, then tell the
660 * GuC that we've finished with it, finally deallocate
661 * it in our bitmap
662 */
29fb72c7 663 if (db_id != GUC_INVALID_DOORBELL_ID) {
0d92a6a4 664 guc_disable_doorbell(guc, client);
29fb72c7
DG
665 if (test_bit(db_id, guc->doorbell_bitmap))
666 host2guc_release_doorbell(guc, client);
667 release_doorbell(guc, db_id);
0d92a6a4
DG
668 }
669
670 kunmap(kmap_to_page(client->client_base));
671 }
672
44a28b1d
DG
673 gem_release_guc_obj(client->client_obj);
674
675 if (client->ctx_index != GUC_INVALID_CTX_ID) {
676 guc_fini_ctx_desc(guc, client);
677 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
678 }
679
680 kfree(client);
681}
682
683/**
684 * guc_client_alloc() - Allocate an i915_guc_client
0daf556c 685 * @dev_priv: driver private data structure
44a28b1d
DG
686 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
687 * The kernel client to replace ExecList submission is created with
688 * NORMAL priority. Priority of a client for scheduler can be HIGH,
689 * while a preemption context can use CRITICAL.
feda33ef
AD
690 * @ctx: the context that owns the client (we use the default render
691 * context)
44a28b1d 692 *
0d92a6a4 693 * Return: An i915_guc_client object if success, else NULL.
44a28b1d 694 */
0daf556c
DG
695static struct i915_guc_client *
696guc_client_alloc(struct drm_i915_private *dev_priv,
697 uint32_t priority,
698 struct i915_gem_context *ctx)
44a28b1d
DG
699{
700 struct i915_guc_client *client;
44a28b1d
DG
701 struct intel_guc *guc = &dev_priv->guc;
702 struct drm_i915_gem_object *obj;
703
704 client = kzalloc(sizeof(*client), GFP_KERNEL);
705 if (!client)
706 return NULL;
707
708 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
709 client->priority = priority;
d1675198 710 client->owner = ctx;
44a28b1d
DG
711 client->guc = guc;
712
713 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
714 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
715 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
716 client->ctx_index = GUC_INVALID_CTX_ID;
717 goto err;
718 }
719
720 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
0daf556c 721 obj = gem_allocate_guc_obj(dev_priv, GUC_DB_SIZE + GUC_WQ_SIZE);
44a28b1d
DG
722 if (!obj)
723 goto err;
724
0d92a6a4 725 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
44a28b1d 726 client->client_obj = obj;
0d92a6a4 727 client->client_base = kmap(i915_gem_object_get_page(obj, 0));
44a28b1d
DG
728 client->wq_offset = GUC_DB_SIZE;
729 client->wq_size = GUC_WQ_SIZE;
44a28b1d
DG
730
731 client->doorbell_offset = select_doorbell_cacheline(guc);
732
733 /*
734 * Since the doorbell only requires a single cacheline, we can save
735 * space by putting the application process descriptor in the same
736 * page. Use the half of the page that doesn't include the doorbell.
737 */
738 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
739 client->proc_desc_offset = 0;
740 else
741 client->proc_desc_offset = (GUC_DB_SIZE / 2);
742
743 client->doorbell_id = assign_doorbell(guc, client->priority);
744 if (client->doorbell_id == GUC_INVALID_DOORBELL_ID)
745 /* XXX: evict a doorbell instead */
746 goto err;
747
748 guc_init_proc_desc(guc, client);
749 guc_init_ctx_desc(guc, client);
750 guc_init_doorbell(guc, client);
751
752 /* XXX: Any cache flushes needed? General domain mgmt calls? */
753
754 if (host2guc_allocate_doorbell(guc, client))
755 goto err;
756
757 DRM_DEBUG_DRIVER("new priority %u client %p: ctx_index %u db_id %u\n",
758 priority, client, client->ctx_index, client->doorbell_id);
759
760 return client;
761
762err:
763 DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
764
0daf556c 765 guc_client_free(dev_priv, client);
44a28b1d
DG
766 return NULL;
767}
768
4c7e77fc
AD
769static void guc_create_log(struct intel_guc *guc)
770{
771 struct drm_i915_private *dev_priv = guc_to_i915(guc);
772 struct drm_i915_gem_object *obj;
773 unsigned long offset;
774 uint32_t size, flags;
775
776 if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
777 return;
778
779 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
780 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
781
782 /* The first page is to save log buffer state. Allocate one
783 * extra page for others in case for overlap */
784 size = (1 + GUC_LOG_DPC_PAGES + 1 +
785 GUC_LOG_ISR_PAGES + 1 +
786 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
787
788 obj = guc->log_obj;
789 if (!obj) {
0daf556c 790 obj = gem_allocate_guc_obj(dev_priv, size);
4c7e77fc
AD
791 if (!obj) {
792 /* logging will be off */
793 i915.guc_log_level = -1;
794 return;
795 }
796
797 guc->log_obj = obj;
798 }
799
800 /* each allocated unit is a page */
801 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
802 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
803 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
804 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
805
806 offset = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; /* in pages */
807 guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
808}
809
463704d0
AD
810static void init_guc_policies(struct guc_policies *policies)
811{
812 struct guc_policy *policy;
813 u32 p, i;
814
815 policies->dpc_promote_time = 500000;
816 policies->max_num_work_items = POLICY_MAX_NUM_WI;
817
818 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
397097b0 819 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
463704d0
AD
820 policy = &policies->policy[p][i];
821
822 policy->execution_quantum = 1000000;
823 policy->preemption_time = 500000;
824 policy->fault_time = 250000;
825 policy->policy_flags = 0;
826 }
827 }
828
829 policies->is_valid = 1;
830}
831
68371a95
AD
832static void guc_create_ads(struct intel_guc *guc)
833{
834 struct drm_i915_private *dev_priv = guc_to_i915(guc);
835 struct drm_i915_gem_object *obj;
836 struct guc_ads *ads;
463704d0 837 struct guc_policies *policies;
5c148e04 838 struct guc_mmio_reg_state *reg_state;
e2f80391 839 struct intel_engine_cs *engine;
68371a95 840 struct page *page;
b4ac5afc 841 u32 size;
68371a95
AD
842
843 /* The ads obj includes the struct itself and buffers passed to GuC */
5c148e04
AD
844 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
845 sizeof(struct guc_mmio_reg_state) +
846 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
68371a95
AD
847
848 obj = guc->ads_obj;
849 if (!obj) {
0daf556c 850 obj = gem_allocate_guc_obj(dev_priv, PAGE_ALIGN(size));
68371a95
AD
851 if (!obj)
852 return;
853
854 guc->ads_obj = obj;
855 }
856
857 page = i915_gem_object_get_page(obj, 0);
858 ads = kmap(page);
859
860 /*
861 * The GuC requires a "Golden Context" when it reinitialises
862 * engines after a reset. Here we use the Render ring default
863 * context, which must already exist and be pinned in the GGTT,
864 * so its address won't change after we've told the GuC where
865 * to find it.
866 */
4a570db5 867 engine = &dev_priv->engine[RCS];
e2f80391 868 ads->golden_context_lrca = engine->status_page.gfx_addr;
68371a95 869
b4ac5afc 870 for_each_engine(engine, dev_priv)
e2f80391 871 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
68371a95 872
463704d0
AD
873 /* GuC scheduling policies */
874 policies = (void *)ads + sizeof(struct guc_ads);
875 init_guc_policies(policies);
876
877 ads->scheduler_policies = i915_gem_obj_ggtt_offset(obj) +
878 sizeof(struct guc_ads);
879
5c148e04
AD
880 /* MMIO reg state */
881 reg_state = (void *)policies + sizeof(struct guc_policies);
882
b4ac5afc 883 for_each_engine(engine, dev_priv) {
e2f80391
TU
884 reg_state->mmio_white_list[engine->guc_id].mmio_start =
885 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
5c148e04
AD
886
887 /* Nothing to be saved or restored for now. */
e2f80391 888 reg_state->mmio_white_list[engine->guc_id].count = 0;
5c148e04
AD
889 }
890
891 ads->reg_state_addr = ads->scheduler_policies +
892 sizeof(struct guc_policies);
893
894 ads->reg_state_buffer = ads->reg_state_addr +
895 sizeof(struct guc_mmio_reg_state);
896
68371a95
AD
897 kunmap(page);
898}
899
bac427f8
AD
900/*
901 * Set up the memory resources to be shared with the GuC. At this point,
902 * we require just one object that can be mapped through the GGTT.
903 */
beffa517 904int i915_guc_submission_init(struct drm_i915_private *dev_priv)
bac427f8 905{
bac427f8
AD
906 const size_t ctxsize = sizeof(struct guc_context_desc);
907 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
908 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
909 struct intel_guc *guc = &dev_priv->guc;
910
29fb72c7
DG
911 /* Wipe bitmap & delete client in case of reinitialisation */
912 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
beffa517 913 i915_guc_submission_disable(dev_priv);
29fb72c7 914
bac427f8
AD
915 if (!i915.enable_guc_submission)
916 return 0; /* not enabled */
917
918 if (guc->ctx_pool_obj)
919 return 0; /* already allocated */
920
0daf556c 921 guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv, gemsize);
bac427f8
AD
922 if (!guc->ctx_pool_obj)
923 return -ENOMEM;
924
925 ida_init(&guc->ctx_ids);
4c7e77fc 926 guc_create_log(guc);
68371a95
AD
927 guc_create_ads(guc);
928
bac427f8
AD
929 return 0;
930}
931
beffa517 932int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
44a28b1d 933{
44a28b1d
DG
934 struct intel_guc *guc = &dev_priv->guc;
935 struct i915_guc_client *client;
936
937 /* client for execbuf submission */
0daf556c 938 client = guc_client_alloc(dev_priv,
0ca5fa3a
CW
939 GUC_CTX_PRIORITY_KMD_NORMAL,
940 dev_priv->kernel_context);
44a28b1d
DG
941 if (!client) {
942 DRM_ERROR("Failed to create execbuf guc_client\n");
943 return -ENOMEM;
944 }
945
946 guc->execbuf_client = client;
f5d3c3ea
AD
947
948 host2guc_sample_forcewake(guc, client);
949
44a28b1d
DG
950 return 0;
951}
952
beffa517 953void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
44a28b1d 954{
44a28b1d
DG
955 struct intel_guc *guc = &dev_priv->guc;
956
0daf556c 957 guc_client_free(dev_priv, guc->execbuf_client);
44a28b1d
DG
958 guc->execbuf_client = NULL;
959}
960
beffa517 961void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
bac427f8 962{
bac427f8
AD
963 struct intel_guc *guc = &dev_priv->guc;
964
68371a95
AD
965 gem_release_guc_obj(dev_priv->guc.ads_obj);
966 guc->ads_obj = NULL;
967
4c7e77fc
AD
968 gem_release_guc_obj(dev_priv->guc.log_obj);
969 guc->log_obj = NULL;
970
bac427f8
AD
971 if (guc->ctx_pool_obj)
972 ida_destroy(&guc->ctx_ids);
973 gem_release_guc_obj(guc->ctx_pool_obj);
974 guc->ctx_pool_obj = NULL;
975}
a1c41994
AD
976
977/**
978 * intel_guc_suspend() - notify GuC entering suspend state
979 * @dev: drm device
980 */
981int intel_guc_suspend(struct drm_device *dev)
982{
983 struct drm_i915_private *dev_priv = dev->dev_private;
984 struct intel_guc *guc = &dev_priv->guc;
e2efd130 985 struct i915_gem_context *ctx;
a1c41994
AD
986 u32 data[3];
987
fce91f22 988 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
a1c41994
AD
989 return 0;
990
ed54c1a1 991 ctx = dev_priv->kernel_context;
a1c41994
AD
992
993 data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
994 /* any value greater than GUC_POWER_D0 */
995 data[1] = GUC_POWER_D1;
996 /* first page is shared data with GuC */
997 data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
998
999 return host2guc_action(guc, data, ARRAY_SIZE(data));
1000}
1001
1002
1003/**
1004 * intel_guc_resume() - notify GuC resuming from suspend state
1005 * @dev: drm device
1006 */
1007int intel_guc_resume(struct drm_device *dev)
1008{
1009 struct drm_i915_private *dev_priv = dev->dev_private;
1010 struct intel_guc *guc = &dev_priv->guc;
e2efd130 1011 struct i915_gem_context *ctx;
a1c41994
AD
1012 u32 data[3];
1013
fce91f22 1014 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
a1c41994
AD
1015 return 0;
1016
ed54c1a1 1017 ctx = dev_priv->kernel_context;
a1c41994
AD
1018
1019 data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
1020 data[1] = GUC_POWER_D0;
1021 /* first page is shared data with GuC */
1022 data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
1023
1024 return host2guc_action(guc, data, ARRAY_SIZE(data));
1025}
This page took 0.146786 seconds and 5 git commands to generate.