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