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