drm/i915: s/_TRANSA_CHICKEN/TRANS_CHICKEN(PIPE_A)/
[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
DG
29/**
30 * DOC: GuC Client
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 spin_lock(&dev_priv->guc.host2guc_lock);
90
91 dev_priv->guc.action_count += 1;
92 dev_priv->guc.action_cmd = data[0];
93
94 for (i = 0; i < len; i++)
95 I915_WRITE(SOFT_SCRATCH(i), data[i]);
96
97 POSTING_READ(SOFT_SCRATCH(i - 1));
98
99 I915_WRITE(HOST2GUC_INTERRUPT, HOST2GUC_TRIGGER);
100
101 /* No HOST2GUC command should take longer than 10ms */
102 ret = wait_for_atomic(host2guc_action_response(dev_priv, &status), 10);
103 if (status != GUC2HOST_STATUS_SUCCESS) {
104 /*
105 * Either the GuC explicitly returned an error (which
106 * we convert to -EIO here) or no response at all was
107 * received within the timeout limit (-ETIMEDOUT)
108 */
109 if (ret != -ETIMEDOUT)
110 ret = -EIO;
111
112 DRM_ERROR("GUC: host2guc action 0x%X failed. ret=%d "
113 "status=0x%08X response=0x%08X\n",
114 data[0], ret, status,
115 I915_READ(SOFT_SCRATCH(15)));
116
117 dev_priv->guc.action_fail += 1;
118 dev_priv->guc.action_err = ret;
119 }
120 dev_priv->guc.action_status = status;
121
122 spin_unlock(&dev_priv->guc.host2guc_lock);
123 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
124
125 return ret;
126}
127
128/*
129 * Tell the GuC to allocate or deallocate a specific doorbell
130 */
131
132static int host2guc_allocate_doorbell(struct intel_guc *guc,
133 struct i915_guc_client *client)
134{
135 u32 data[2];
136
137 data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL;
138 data[1] = client->ctx_index;
139
140 return host2guc_action(guc, data, 2);
141}
142
143static int host2guc_release_doorbell(struct intel_guc *guc,
144 struct i915_guc_client *client)
145{
146 u32 data[2];
147
148 data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL;
149 data[1] = client->ctx_index;
150
151 return host2guc_action(guc, data, 2);
152}
153
f5d3c3ea
AD
154static int host2guc_sample_forcewake(struct intel_guc *guc,
155 struct i915_guc_client *client)
156{
157 struct drm_i915_private *dev_priv = guc_to_i915(guc);
158 u32 data[2];
159
160 data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
161 data[1] = (intel_enable_rc6(dev_priv->dev)) ? 1 : 0;
162
163 return host2guc_action(guc, data, 2);
164}
165
44a28b1d
DG
166/*
167 * Initialise, update, or clear doorbell data shared with the GuC
168 *
169 * These functions modify shared data and so need access to the mapped
170 * client object which contains the page being used for the doorbell
171 */
172
173static void guc_init_doorbell(struct intel_guc *guc,
174 struct i915_guc_client *client)
175{
176 struct guc_doorbell_info *doorbell;
177 void *base;
178
179 base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
180 doorbell = base + client->doorbell_offset;
181
182 doorbell->db_status = 1;
183 doorbell->cookie = 0;
184
185 kunmap_atomic(base);
186}
187
188static int guc_ring_doorbell(struct i915_guc_client *gc)
189{
190 struct guc_process_desc *desc;
191 union guc_doorbell_qw db_cmp, db_exc, db_ret;
192 union guc_doorbell_qw *db;
193 void *base;
194 int attempt = 2, ret = -EAGAIN;
195
196 base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, 0));
197 desc = base + gc->proc_desc_offset;
198
199 /* Update the tail so it is visible to GuC */
200 desc->tail = gc->wq_tail;
201
202 /* current cookie */
203 db_cmp.db_status = GUC_DOORBELL_ENABLED;
204 db_cmp.cookie = gc->cookie;
205
206 /* cookie to be updated */
207 db_exc.db_status = GUC_DOORBELL_ENABLED;
208 db_exc.cookie = gc->cookie + 1;
209 if (db_exc.cookie == 0)
210 db_exc.cookie = 1;
211
212 /* pointer of current doorbell cacheline */
213 db = base + gc->doorbell_offset;
214
215 while (attempt--) {
216 /* lets ring the doorbell */
217 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
218 db_cmp.value_qw, db_exc.value_qw);
219
220 /* if the exchange was successfully executed */
221 if (db_ret.value_qw == db_cmp.value_qw) {
222 /* db was successfully rung */
223 gc->cookie = db_exc.cookie;
224 ret = 0;
225 break;
226 }
227
228 /* XXX: doorbell was lost and need to acquire it again */
229 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
230 break;
231
232 DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n",
233 db_cmp.cookie, db_ret.cookie);
234
235 /* update the cookie to newly read cookie from GuC */
236 db_cmp.cookie = db_ret.cookie;
237 db_exc.cookie = db_ret.cookie + 1;
238 if (db_exc.cookie == 0)
239 db_exc.cookie = 1;
240 }
241
242 kunmap_atomic(base);
243 return ret;
244}
245
246static void guc_disable_doorbell(struct intel_guc *guc,
247 struct i915_guc_client *client)
248{
249 struct drm_i915_private *dev_priv = guc_to_i915(guc);
250 struct guc_doorbell_info *doorbell;
251 void *base;
252 int drbreg = GEN8_DRBREGL(client->doorbell_id);
253 int value;
254
255 base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
256 doorbell = base + client->doorbell_offset;
257
258 doorbell->db_status = 0;
259
260 kunmap_atomic(base);
261
262 I915_WRITE(drbreg, I915_READ(drbreg) & ~GEN8_DRB_VALID);
263
264 value = I915_READ(drbreg);
265 WARN_ON((value & GEN8_DRB_VALID) != 0);
266
267 I915_WRITE(GEN8_DRBREGU(client->doorbell_id), 0);
268 I915_WRITE(drbreg, 0);
269
270 /* XXX: wait for any interrupts */
271 /* XXX: wait for workqueue to drain */
272}
273
274/*
275 * Select, assign and relase doorbell cachelines
276 *
277 * These functions track which doorbell cachelines are in use.
278 * The data they manipulate is protected by the host2guc lock.
279 */
280
281static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
282{
283 const uint32_t cacheline_size = cache_line_size();
284 uint32_t offset;
285
286 spin_lock(&guc->host2guc_lock);
287
288 /* Doorbell uses a single cache line within a page */
289 offset = offset_in_page(guc->db_cacheline);
290
291 /* Moving to next cache line to reduce contention */
292 guc->db_cacheline += cacheline_size;
293
294 spin_unlock(&guc->host2guc_lock);
295
296 DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
297 offset, guc->db_cacheline, cacheline_size);
298
299 return offset;
300}
301
302static uint16_t assign_doorbell(struct intel_guc *guc, uint32_t priority)
303{
304 /*
305 * The bitmap is split into two halves; the first half is used for
306 * normal priority contexts, the second half for high-priority ones.
307 * Note that logically higher priorities are numerically less than
308 * normal ones, so the test below means "is it high-priority?"
309 */
310 const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
311 const uint16_t half = GUC_MAX_DOORBELLS / 2;
312 const uint16_t start = hi_pri ? half : 0;
313 const uint16_t end = start + half;
314 uint16_t id;
315
316 spin_lock(&guc->host2guc_lock);
317 id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
318 if (id == end)
319 id = GUC_INVALID_DOORBELL_ID;
320 else
321 bitmap_set(guc->doorbell_bitmap, id, 1);
322 spin_unlock(&guc->host2guc_lock);
323
324 DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
325 hi_pri ? "high" : "normal", id);
326
327 return id;
328}
329
330static void release_doorbell(struct intel_guc *guc, uint16_t id)
331{
332 spin_lock(&guc->host2guc_lock);
333 bitmap_clear(guc->doorbell_bitmap, id, 1);
334 spin_unlock(&guc->host2guc_lock);
335}
336
337/*
338 * Initialise the process descriptor shared with the GuC firmware.
339 */
340static void guc_init_proc_desc(struct intel_guc *guc,
341 struct i915_guc_client *client)
342{
343 struct guc_process_desc *desc;
344 void *base;
345
346 base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
347 desc = base + client->proc_desc_offset;
348
349 memset(desc, 0, sizeof(*desc));
350
351 /*
352 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
353 * space for ring3 clients (set them as in mmap_ioctl) or kernel
354 * space for kernel clients (map on demand instead? May make debug
355 * easier to have it mapped).
356 */
357 desc->wq_base_addr = 0;
358 desc->db_base_addr = 0;
359
360 desc->context_id = client->ctx_index;
361 desc->wq_size_bytes = client->wq_size;
362 desc->wq_status = WQ_STATUS_ACTIVE;
363 desc->priority = client->priority;
364
365 kunmap_atomic(base);
366}
367
368/*
369 * Initialise/clear the context descriptor shared with the GuC firmware.
370 *
371 * This descriptor tells the GuC where (in GGTT space) to find the important
372 * data structures relating to this client (doorbell, process descriptor,
373 * write queue, etc).
374 */
375
376static void guc_init_ctx_desc(struct intel_guc *guc,
377 struct i915_guc_client *client)
378{
d1675198 379 struct intel_context *ctx = client->owner;
44a28b1d
DG
380 struct guc_context_desc desc;
381 struct sg_table *sg;
d1675198 382 int i;
44a28b1d
DG
383
384 memset(&desc, 0, sizeof(desc));
385
386 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
387 desc.context_id = client->ctx_index;
388 desc.priority = client->priority;
44a28b1d
DG
389 desc.db_id = client->doorbell_id;
390
d1675198
AD
391 for (i = 0; i < I915_NUM_RINGS; i++) {
392 struct guc_execlist_context *lrc = &desc.lrc[i];
393 struct intel_ringbuffer *ringbuf = ctx->engine[i].ringbuf;
394 struct intel_engine_cs *ring;
395 struct drm_i915_gem_object *obj;
396 uint64_t ctx_desc;
397
398 /* TODO: We have a design issue to be solved here. Only when we
399 * receive the first batch, we know which engine is used by the
400 * user. But here GuC expects the lrc and ring to be pinned. It
401 * is not an issue for default context, which is the only one
402 * for now who owns a GuC client. But for future owner of GuC
403 * client, need to make sure lrc is pinned prior to enter here.
404 */
405 obj = ctx->engine[i].state;
406 if (!obj)
407 break; /* XXX: continue? */
408
409 ring = ringbuf->ring;
410 ctx_desc = intel_lr_context_descriptor(ctx, ring);
411 lrc->context_desc = (u32)ctx_desc;
412
413 /* The state page is after PPHWSP */
414 lrc->ring_lcra = i915_gem_obj_ggtt_offset(obj) +
415 LRC_STATE_PN * PAGE_SIZE;
416 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
417 (ring->id << GUC_ELC_ENGINE_OFFSET);
418
419 obj = ringbuf->obj;
420
421 lrc->ring_begin = i915_gem_obj_ggtt_offset(obj);
422 lrc->ring_end = lrc->ring_begin + obj->base.size - 1;
423 lrc->ring_next_free_location = lrc->ring_begin;
424 lrc->ring_current_tail_pointer_value = 0;
425
426 desc.engines_used |= (1 << ring->id);
427 }
428
429 WARN_ON(desc.engines_used == 0);
430
44a28b1d
DG
431 /*
432 * The CPU address is only needed at certain points, so kmap_atomic on
433 * demand instead of storing it in the ctx descriptor.
434 * XXX: May make debug easier to have it mapped
435 */
436 desc.db_trigger_cpu = 0;
437 desc.db_trigger_uk = client->doorbell_offset +
438 i915_gem_obj_ggtt_offset(client->client_obj);
439 desc.db_trigger_phy = client->doorbell_offset +
440 sg_dma_address(client->client_obj->pages->sgl);
441
442 desc.process_desc = client->proc_desc_offset +
443 i915_gem_obj_ggtt_offset(client->client_obj);
444
445 desc.wq_addr = client->wq_offset +
446 i915_gem_obj_ggtt_offset(client->client_obj);
447
448 desc.wq_size = client->wq_size;
449
450 /*
451 * XXX: Take LRCs from an existing intel_context if this is not an
452 * IsKMDCreatedContext client
453 */
454 desc.desc_private = (uintptr_t)client;
455
456 /* Pool context is pinned already */
457 sg = guc->ctx_pool_obj->pages;
458 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
459 sizeof(desc) * client->ctx_index);
460}
461
462static void guc_fini_ctx_desc(struct intel_guc *guc,
463 struct i915_guc_client *client)
464{
465 struct guc_context_desc desc;
466 struct sg_table *sg;
467
468 memset(&desc, 0, sizeof(desc));
469
470 sg = guc->ctx_pool_obj->pages;
471 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
472 sizeof(desc) * client->ctx_index);
473}
474
475/* Get valid workqueue item and return it back to offset */
476static int guc_get_workqueue_space(struct i915_guc_client *gc, u32 *offset)
477{
478 struct guc_process_desc *desc;
479 void *base;
480 u32 size = sizeof(struct guc_wq_item);
481 int ret = 0, timeout_counter = 200;
482
483 base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, 0));
484 desc = base + gc->proc_desc_offset;
485
486 while (timeout_counter-- > 0) {
487 ret = wait_for_atomic(CIRC_SPACE(gc->wq_tail, desc->head,
488 gc->wq_size) >= size, 1);
489
490 if (!ret) {
491 *offset = gc->wq_tail;
492
493 /* advance the tail for next workqueue item */
494 gc->wq_tail += size;
495 gc->wq_tail &= gc->wq_size - 1;
496
497 /* this will break the loop */
498 timeout_counter = 0;
499 }
500 };
501
502 kunmap_atomic(base);
503
504 return ret;
505}
506
507static int guc_add_workqueue_item(struct i915_guc_client *gc,
508 struct drm_i915_gem_request *rq)
509{
510 enum intel_ring_id ring_id = rq->ring->id;
511 struct guc_wq_item *wqi;
512 void *base;
513 u32 tail, wq_len, wq_off = 0;
514 int ret;
515
516 ret = guc_get_workqueue_space(gc, &wq_off);
517 if (ret)
518 return ret;
519
520 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
521 * should not have the case where structure wqi is across page, neither
522 * wrapped to the beginning. This simplifies the implementation below.
523 *
524 * XXX: if not the case, we need save data to a temp wqi and copy it to
525 * workqueue buffer dw by dw.
526 */
527 WARN_ON(sizeof(struct guc_wq_item) != 16);
528 WARN_ON(wq_off & 3);
529
530 /* wq starts from the page after doorbell / process_desc */
531 base = kmap_atomic(i915_gem_object_get_page(gc->client_obj,
532 (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT));
533 wq_off &= PAGE_SIZE - 1;
534 wqi = (struct guc_wq_item *)((char *)base + wq_off);
535
536 /* len does not include the header */
537 wq_len = sizeof(struct guc_wq_item) / sizeof(u32) - 1;
538 wqi->header = WQ_TYPE_INORDER |
539 (wq_len << WQ_LEN_SHIFT) |
540 (ring_id << WQ_TARGET_SHIFT) |
541 WQ_NO_WCFLUSH_WAIT;
542
543 /* The GuC wants only the low-order word of the context descriptor */
544 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, rq->ring);
545
546 /* The GuC firmware wants the tail index in QWords, not bytes */
547 tail = rq->ringbuf->tail >> 3;
548 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
549 wqi->fence_id = 0; /*XXX: what fence to be here */
550
551 kunmap_atomic(base);
552
553 return 0;
554}
555
d1675198
AD
556#define CTX_RING_BUFFER_START 0x08
557
558/* Update the ringbuffer pointer in a saved context image */
559static void lr_context_update(struct drm_i915_gem_request *rq)
560{
561 enum intel_ring_id ring_id = rq->ring->id;
562 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring_id].state;
563 struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
564 struct page *page;
565 uint32_t *reg_state;
566
567 BUG_ON(!ctx_obj);
568 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
569 WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
570
571 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
572 reg_state = kmap_atomic(page);
573
574 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
575
576 kunmap_atomic(reg_state);
577}
578
44a28b1d
DG
579/**
580 * i915_guc_submit() - Submit commands through GuC
581 * @client: the guc client where commands will go through
582 * @ctx: LRC where commands come from
583 * @ring: HW engine that will excute the commands
584 *
585 * Return: 0 if succeed
586 */
587int i915_guc_submit(struct i915_guc_client *client,
588 struct drm_i915_gem_request *rq)
589{
590 struct intel_guc *guc = client->guc;
591 enum intel_ring_id ring_id = rq->ring->id;
592 unsigned long flags;
593 int q_ret, b_ret;
594
d1675198
AD
595 /* Need this because of the deferred pin ctx and ring */
596 /* Shall we move this right after ring is pinned? */
597 lr_context_update(rq);
598
44a28b1d
DG
599 spin_lock_irqsave(&client->wq_lock, flags);
600
601 q_ret = guc_add_workqueue_item(client, rq);
602 if (q_ret == 0)
603 b_ret = guc_ring_doorbell(client);
604
605 client->submissions[ring_id] += 1;
606 if (q_ret) {
607 client->q_fail += 1;
608 client->retcode = q_ret;
609 } else if (b_ret) {
610 client->b_fail += 1;
611 client->retcode = q_ret = b_ret;
612 } else {
613 client->retcode = 0;
614 }
615 spin_unlock_irqrestore(&client->wq_lock, flags);
616
617 spin_lock(&guc->host2guc_lock);
618 guc->submissions[ring_id] += 1;
619 guc->last_seqno[ring_id] = rq->seqno;
620 spin_unlock(&guc->host2guc_lock);
621
622 return q_ret;
623}
624
625/*
626 * Everything below here is concerned with setup & teardown, and is
627 * therefore not part of the somewhat time-critical batch-submission
628 * path of i915_guc_submit() above.
629 */
630
bac427f8
AD
631/**
632 * gem_allocate_guc_obj() - Allocate gem object for GuC usage
633 * @dev: drm device
634 * @size: size of object
635 *
636 * This is a wrapper to create a gem obj. In order to use it inside GuC, the
637 * object needs to be pinned lifetime. Also we must pin it to gtt space other
638 * than [0, GUC_WOPCM_TOP) because this range is reserved inside GuC.
639 *
640 * Return: A drm_i915_gem_object if successful, otherwise NULL.
641 */
642static struct drm_i915_gem_object *gem_allocate_guc_obj(struct drm_device *dev,
643 u32 size)
644{
645 struct drm_i915_private *dev_priv = dev->dev_private;
646 struct drm_i915_gem_object *obj;
647
648 obj = i915_gem_alloc_object(dev, size);
649 if (!obj)
650 return NULL;
651
652 if (i915_gem_object_get_pages(obj)) {
653 drm_gem_object_unreference(&obj->base);
654 return NULL;
655 }
656
657 if (i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
658 PIN_OFFSET_BIAS | GUC_WOPCM_TOP)) {
659 drm_gem_object_unreference(&obj->base);
660 return NULL;
661 }
662
663 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
664 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
665
666 return obj;
667}
668
669/**
670 * gem_release_guc_obj() - Release gem object allocated for GuC usage
671 * @obj: gem obj to be released
672 */
673static void gem_release_guc_obj(struct drm_i915_gem_object *obj)
674{
675 if (!obj)
676 return;
677
678 if (i915_gem_obj_is_pinned(obj))
679 i915_gem_object_ggtt_unpin(obj);
680
681 drm_gem_object_unreference(&obj->base);
682}
683
44a28b1d
DG
684static void guc_client_free(struct drm_device *dev,
685 struct i915_guc_client *client)
686{
687 struct drm_i915_private *dev_priv = dev->dev_private;
688 struct intel_guc *guc = &dev_priv->guc;
689
690 if (!client)
691 return;
692
693 if (client->doorbell_id != GUC_INVALID_DOORBELL_ID) {
694 /*
695 * First disable the doorbell, then tell the GuC we've
696 * finished with it, finally deallocate it in our bitmap
697 */
698 guc_disable_doorbell(guc, client);
699 host2guc_release_doorbell(guc, client);
700 release_doorbell(guc, client->doorbell_id);
701 }
702
703 /*
704 * XXX: wait for any outstanding submissions before freeing memory.
705 * Be sure to drop any locks
706 */
707
708 gem_release_guc_obj(client->client_obj);
709
710 if (client->ctx_index != GUC_INVALID_CTX_ID) {
711 guc_fini_ctx_desc(guc, client);
712 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
713 }
714
715 kfree(client);
716}
717
718/**
719 * guc_client_alloc() - Allocate an i915_guc_client
720 * @dev: drm device
721 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
722 * The kernel client to replace ExecList submission is created with
723 * NORMAL priority. Priority of a client for scheduler can be HIGH,
724 * while a preemption context can use CRITICAL.
d1675198 725 * @ctx the context to own the client (we use the default render context)
44a28b1d
DG
726 *
727 * Return: An i915_guc_client object if success.
728 */
729static struct i915_guc_client *guc_client_alloc(struct drm_device *dev,
d1675198
AD
730 uint32_t priority,
731 struct intel_context *ctx)
44a28b1d
DG
732{
733 struct i915_guc_client *client;
734 struct drm_i915_private *dev_priv = dev->dev_private;
735 struct intel_guc *guc = &dev_priv->guc;
736 struct drm_i915_gem_object *obj;
737
738 client = kzalloc(sizeof(*client), GFP_KERNEL);
739 if (!client)
740 return NULL;
741
742 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
743 client->priority = priority;
d1675198 744 client->owner = ctx;
44a28b1d
DG
745 client->guc = guc;
746
747 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
748 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
749 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
750 client->ctx_index = GUC_INVALID_CTX_ID;
751 goto err;
752 }
753
754 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
755 obj = gem_allocate_guc_obj(dev, GUC_DB_SIZE + GUC_WQ_SIZE);
756 if (!obj)
757 goto err;
758
759 client->client_obj = obj;
760 client->wq_offset = GUC_DB_SIZE;
761 client->wq_size = GUC_WQ_SIZE;
762 spin_lock_init(&client->wq_lock);
763
764 client->doorbell_offset = select_doorbell_cacheline(guc);
765
766 /*
767 * Since the doorbell only requires a single cacheline, we can save
768 * space by putting the application process descriptor in the same
769 * page. Use the half of the page that doesn't include the doorbell.
770 */
771 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
772 client->proc_desc_offset = 0;
773 else
774 client->proc_desc_offset = (GUC_DB_SIZE / 2);
775
776 client->doorbell_id = assign_doorbell(guc, client->priority);
777 if (client->doorbell_id == GUC_INVALID_DOORBELL_ID)
778 /* XXX: evict a doorbell instead */
779 goto err;
780
781 guc_init_proc_desc(guc, client);
782 guc_init_ctx_desc(guc, client);
783 guc_init_doorbell(guc, client);
784
785 /* XXX: Any cache flushes needed? General domain mgmt calls? */
786
787 if (host2guc_allocate_doorbell(guc, client))
788 goto err;
789
790 DRM_DEBUG_DRIVER("new priority %u client %p: ctx_index %u db_id %u\n",
791 priority, client, client->ctx_index, client->doorbell_id);
792
793 return client;
794
795err:
796 DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
797
798 guc_client_free(dev, client);
799 return NULL;
800}
801
4c7e77fc
AD
802static void guc_create_log(struct intel_guc *guc)
803{
804 struct drm_i915_private *dev_priv = guc_to_i915(guc);
805 struct drm_i915_gem_object *obj;
806 unsigned long offset;
807 uint32_t size, flags;
808
809 if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
810 return;
811
812 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
813 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
814
815 /* The first page is to save log buffer state. Allocate one
816 * extra page for others in case for overlap */
817 size = (1 + GUC_LOG_DPC_PAGES + 1 +
818 GUC_LOG_ISR_PAGES + 1 +
819 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
820
821 obj = guc->log_obj;
822 if (!obj) {
823 obj = gem_allocate_guc_obj(dev_priv->dev, size);
824 if (!obj) {
825 /* logging will be off */
826 i915.guc_log_level = -1;
827 return;
828 }
829
830 guc->log_obj = obj;
831 }
832
833 /* each allocated unit is a page */
834 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
835 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
836 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
837 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
838
839 offset = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; /* in pages */
840 guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
841}
842
bac427f8
AD
843/*
844 * Set up the memory resources to be shared with the GuC. At this point,
845 * we require just one object that can be mapped through the GGTT.
846 */
847int i915_guc_submission_init(struct drm_device *dev)
848{
849 struct drm_i915_private *dev_priv = dev->dev_private;
850 const size_t ctxsize = sizeof(struct guc_context_desc);
851 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
852 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
853 struct intel_guc *guc = &dev_priv->guc;
854
855 if (!i915.enable_guc_submission)
856 return 0; /* not enabled */
857
858 if (guc->ctx_pool_obj)
859 return 0; /* already allocated */
860
861 guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv->dev, gemsize);
862 if (!guc->ctx_pool_obj)
863 return -ENOMEM;
864
44a28b1d
DG
865 spin_lock_init(&dev_priv->guc.host2guc_lock);
866
bac427f8
AD
867 ida_init(&guc->ctx_ids);
868
4c7e77fc
AD
869 guc_create_log(guc);
870
bac427f8
AD
871 return 0;
872}
873
44a28b1d
DG
874int i915_guc_submission_enable(struct drm_device *dev)
875{
876 struct drm_i915_private *dev_priv = dev->dev_private;
877 struct intel_guc *guc = &dev_priv->guc;
d1675198 878 struct intel_context *ctx = dev_priv->ring[RCS].default_context;
44a28b1d
DG
879 struct i915_guc_client *client;
880
881 /* client for execbuf submission */
d1675198 882 client = guc_client_alloc(dev, GUC_CTX_PRIORITY_KMD_NORMAL, ctx);
44a28b1d
DG
883 if (!client) {
884 DRM_ERROR("Failed to create execbuf guc_client\n");
885 return -ENOMEM;
886 }
887
888 guc->execbuf_client = client;
f5d3c3ea
AD
889
890 host2guc_sample_forcewake(guc, client);
891
44a28b1d
DG
892 return 0;
893}
894
895void i915_guc_submission_disable(struct drm_device *dev)
896{
897 struct drm_i915_private *dev_priv = dev->dev_private;
898 struct intel_guc *guc = &dev_priv->guc;
899
900 guc_client_free(dev, guc->execbuf_client);
901 guc->execbuf_client = NULL;
902}
903
bac427f8
AD
904void i915_guc_submission_fini(struct drm_device *dev)
905{
906 struct drm_i915_private *dev_priv = dev->dev_private;
907 struct intel_guc *guc = &dev_priv->guc;
908
4c7e77fc
AD
909 gem_release_guc_obj(dev_priv->guc.log_obj);
910 guc->log_obj = NULL;
911
bac427f8
AD
912 if (guc->ctx_pool_obj)
913 ida_destroy(&guc->ctx_ids);
914 gem_release_guc_obj(guc->ctx_pool_obj);
915 guc->ctx_pool_obj = NULL;
916}
This page took 0.076183 seconds and 5 git commands to generate.