drm/i915/guc: replace assign_doorbell() with select_doorbell_register()
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_guc_submission.c
... / ...
CommitLineData
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
29/**
30 * DOC: GuC-based command submission
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);
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
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
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);
156 struct drm_device *dev = dev_priv->dev;
157 u32 data[2];
158
159 data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
160 /* WaRsDisableCoarsePowerGating:skl,bxt */
161 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev))
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));
168}
169
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 int guc_update_doorbell_id(struct intel_guc *guc,
178 struct i915_guc_client *client,
179 u16 new_id)
180{
181 struct sg_table *sg = guc->ctx_pool_obj->pages;
182 void *doorbell_bitmap = guc->doorbell_bitmap;
183 struct guc_doorbell_info *doorbell;
184 struct guc_context_desc desc;
185 size_t len;
186
187 doorbell = client->client_base + client->doorbell_offset;
188
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);
214 doorbell->cookie = 0;
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);
224}
225
226static void guc_disable_doorbell(struct intel_guc *guc,
227 struct i915_guc_client *client)
228{
229 (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
230
231 /* XXX: wait for any interrupts */
232 /* XXX: wait for workqueue to drain */
233}
234
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
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
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
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
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;
292
293 desc = client->client_base + client->proc_desc_offset;
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;
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{
323 struct drm_i915_gem_object *client_obj = client->client_obj;
324 struct drm_i915_private *dev_priv = guc_to_i915(guc);
325 struct intel_engine_cs *engine;
326 struct i915_gem_context *ctx = client->owner;
327 struct guc_context_desc desc;
328 struct sg_table *sg;
329 u32 gfx_addr;
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;
336 desc.db_id = client->doorbell_id;
337
338 for_each_engine(engine, dev_priv) {
339 struct intel_context *ce = &ctx->engine[engine->id];
340 struct guc_execlist_context *lrc = &desc.lrc[engine->guc_id];
341 struct drm_i915_gem_object *obj;
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 */
350 if (!ce->state)
351 break; /* XXX: continue? */
352
353 lrc->context_desc = lower_32_bits(ce->lrc_desc);
354
355 /* The state page is after PPHWSP */
356 gfx_addr = i915_gem_obj_ggtt_offset(ce->state);
357 lrc->ring_lcra = gfx_addr + LRC_STATE_PN * PAGE_SIZE;
358 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
359 (engine->guc_id << GUC_ELC_ENGINE_OFFSET);
360
361 obj = ce->ringbuf->obj;
362 gfx_addr = i915_gem_obj_ggtt_offset(obj);
363
364 lrc->ring_begin = gfx_addr;
365 lrc->ring_end = gfx_addr + obj->base.size - 1;
366 lrc->ring_next_free_location = gfx_addr;
367 lrc->ring_current_tail_pointer_value = 0;
368
369 desc.engines_used |= (1 << engine->guc_id);
370 }
371
372 WARN_ON(desc.engines_used == 0);
373
374 /*
375 * The doorbell, process descriptor, and workqueue are all parts
376 * of the client object, which the GuC will reference via the GGTT
377 */
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;
386 desc.wq_size = client->wq_size;
387
388 /*
389 * XXX: Take LRCs from an existing context if this is not an
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
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)
430{
431 const size_t wqi_size = sizeof(struct guc_wq_item);
432 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
433 struct guc_process_desc *desc;
434 u32 freespace;
435
436 GEM_BUG_ON(gc == NULL);
437
438 desc = gc->client_base + gc->proc_desc_offset;
439
440 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
441 if (likely(freespace >= wqi_size))
442 return 0;
443
444 gc->no_wq_space += 1;
445
446 return -EAGAIN;
447}
448
449static void guc_add_workqueue_item(struct i915_guc_client *gc,
450 struct drm_i915_gem_request *rq)
451{
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;
455 struct guc_process_desc *desc;
456 struct guc_wq_item *wqi;
457 void *base;
458 u32 freespace, tail, wq_off, wq_page;
459
460 desc = gc->client_base + gc->proc_desc_offset;
461
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);
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 */
479 BUILD_BUG_ON(wqi_size != 16);
480
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;
489 wq_off &= PAGE_SIZE - 1;
490 base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, wq_page));
491 wqi = (struct guc_wq_item *)((char *)base + wq_off);
492
493 /* Now fill in the 4-word work queue item */
494 wqi->header = WQ_TYPE_INORDER |
495 (wqi_len << WQ_LEN_SHIFT) |
496 (rq->engine->guc_id << WQ_TARGET_SHIFT) |
497 WQ_NO_WCFLUSH_WAIT;
498
499 /* The GuC wants only the low-order word of the context descriptor */
500 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx,
501 rq->engine);
502
503 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
504 wqi->fence_id = rq->seqno;
505
506 kunmap_atomic(base);
507}
508
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
564/**
565 * i915_guc_submit() - Submit commands through GuC
566 * @rq: request associated with the commands
567 *
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.
582 */
583int i915_guc_submit(struct drm_i915_gem_request *rq)
584{
585 unsigned int engine_id = rq->engine->guc_id;
586 struct intel_guc *guc = &rq->i915->guc;
587 struct i915_guc_client *client = guc->execbuf_client;
588 int b_ret;
589
590 guc_add_workqueue_item(client, rq);
591 b_ret = guc_ring_doorbell(client);
592
593 client->submissions[engine_id] += 1;
594 client->retcode = b_ret;
595 if (b_ret)
596 client->b_fail += 1;
597
598 guc->submissions[engine_id] += 1;
599 guc->last_seqno[engine_id] = rq->seqno;
600
601 return b_ret;
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
610/**
611 * gem_allocate_guc_obj() - Allocate gem object for GuC usage
612 * @dev_priv: driver private data structure
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 */
621static struct drm_i915_gem_object *
622gem_allocate_guc_obj(struct drm_i915_private *dev_priv, u32 size)
623{
624 struct drm_i915_gem_object *obj;
625
626 obj = i915_gem_object_create(dev_priv->dev, size);
627 if (IS_ERR(obj))
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
650 */
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
662static void
663guc_client_free(struct drm_i915_private *dev_priv,
664 struct i915_guc_client *client)
665{
666 struct intel_guc *guc = &dev_priv->guc;
667
668 if (!client)
669 return;
670
671 /*
672 * XXX: wait for any outstanding submissions before freeing memory.
673 * Be sure to drop any locks
674 */
675
676 if (client->client_base) {
677 /*
678 * If we got as far as setting up a doorbell, make sure we
679 * shut it down before unmapping & deallocating the memory.
680 */
681 guc_disable_doorbell(guc, client);
682
683 kunmap(kmap_to_page(client->client_base));
684 }
685
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
698 * @dev_priv: driver private data structure
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.
703 * @ctx: the context that owns the client (we use the default render
704 * context)
705 *
706 * Return: An i915_guc_client object if success, else NULL.
707 */
708static struct i915_guc_client *
709guc_client_alloc(struct drm_i915_private *dev_priv,
710 uint32_t priority,
711 struct i915_gem_context *ctx)
712{
713 struct i915_guc_client *client;
714 struct intel_guc *guc = &dev_priv->guc;
715 struct drm_i915_gem_object *obj;
716 uint16_t db_id;
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;
724 client->owner = ctx;
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. */
735 obj = gem_allocate_guc_obj(dev_priv, GUC_DB_SIZE + GUC_WQ_SIZE);
736 if (!obj)
737 goto err;
738
739 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
740 client->client_obj = obj;
741 client->client_base = kmap(i915_gem_object_get_page(obj, 0));
742 client->wq_offset = GUC_DB_SIZE;
743 client->wq_size = GUC_WQ_SIZE;
744
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
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
762 guc_init_proc_desc(guc, client);
763 guc_init_ctx_desc(guc, client);
764 if (guc_init_doorbell(guc, client, db_id))
765 goto err;
766
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);
771
772 return client;
773
774err:
775 DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
776
777 guc_client_free(dev_priv, client);
778 return NULL;
779}
780
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) {
802 obj = gem_allocate_guc_obj(dev_priv, size);
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
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++) {
831 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
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
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;
849 struct guc_policies *policies;
850 struct guc_mmio_reg_state *reg_state;
851 struct intel_engine_cs *engine;
852 struct page *page;
853 u32 size;
854
855 /* The ads obj includes the struct itself and buffers passed to GuC */
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;
859
860 obj = guc->ads_obj;
861 if (!obj) {
862 obj = gem_allocate_guc_obj(dev_priv, PAGE_ALIGN(size));
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 */
879 engine = &dev_priv->engine[RCS];
880 ads->golden_context_lrca = engine->status_page.gfx_addr;
881
882 for_each_engine(engine, dev_priv)
883 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
884
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
892 /* MMIO reg state */
893 reg_state = (void *)policies + sizeof(struct guc_policies);
894
895 for_each_engine(engine, dev_priv) {
896 reg_state->mmio_white_list[engine->guc_id].mmio_start =
897 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
898
899 /* Nothing to be saved or restored for now. */
900 reg_state->mmio_white_list[engine->guc_id].count = 0;
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
909 kunmap(page);
910}
911
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 */
916int i915_guc_submission_init(struct drm_i915_private *dev_priv)
917{
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
923 /* Wipe bitmap & delete client in case of reinitialisation */
924 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
925 i915_guc_submission_disable(dev_priv);
926
927 if (!i915.enable_guc_submission)
928 return 0; /* not enabled */
929
930 if (guc->ctx_pool_obj)
931 return 0; /* already allocated */
932
933 guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv, gemsize);
934 if (!guc->ctx_pool_obj)
935 return -ENOMEM;
936
937 ida_init(&guc->ctx_ids);
938 guc_create_log(guc);
939 guc_create_ads(guc);
940
941 return 0;
942}
943
944int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
945{
946 struct intel_guc *guc = &dev_priv->guc;
947 struct i915_guc_client *client;
948
949 /* client for execbuf submission */
950 client = guc_client_alloc(dev_priv,
951 GUC_CTX_PRIORITY_KMD_NORMAL,
952 dev_priv->kernel_context);
953 if (!client) {
954 DRM_ERROR("Failed to create execbuf guc_client\n");
955 return -ENOMEM;
956 }
957
958 guc->execbuf_client = client;
959
960 host2guc_sample_forcewake(guc, client);
961
962 return 0;
963}
964
965void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
966{
967 struct intel_guc *guc = &dev_priv->guc;
968
969 guc_client_free(dev_priv, guc->execbuf_client);
970 guc->execbuf_client = NULL;
971}
972
973void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
974{
975 struct intel_guc *guc = &dev_priv->guc;
976
977 gem_release_guc_obj(dev_priv->guc.ads_obj);
978 guc->ads_obj = NULL;
979
980 gem_release_guc_obj(dev_priv->guc.log_obj);
981 guc->log_obj = NULL;
982
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}
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;
997 struct i915_gem_context *ctx;
998 u32 data[3];
999
1000 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
1001 return 0;
1002
1003 ctx = dev_priv->kernel_context;
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;
1023 struct i915_gem_context *ctx;
1024 u32 data[3];
1025
1026 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
1027 return 0;
1028
1029 ctx = dev_priv->kernel_context;
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.026999 seconds and 5 git commands to generate.