blob: dd879f0b077d6b05ff7f762a0bcb9dced761a32f [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 */
Alex Daibac427f2015-08-12 15:43:39 +010024#include <linux/circ_buf.h>
Akash Goelf8240832016-10-12 21:54:34 +053025#include <linux/debugfs.h>
26#include <linux/relay.h>
Alex Daibac427f2015-08-12 15:43:39 +010027#include "i915_drv.h"
Arkadiusz Hiler8c4f24f2016-11-25 18:59:33 +010028#include "intel_uc.h"
Alex Daibac427f2015-08-12 15:43:39 +010029
30/**
Alex Daifeda33e2015-10-19 16:10:54 -070031 * DOC: GuC-based command submission
Dave Gordon44a28b12015-08-12 15:43:41 +010032 *
33 * i915_guc_client:
34 * We use the term client to avoid confusion with contexts. A i915_guc_client is
35 * equivalent to GuC object guc_context_desc. This context descriptor is
36 * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
37 * and workqueue for it. Also the process descriptor (guc_process_desc), which
38 * is mapped to client space. So the client can write Work Item then ring the
39 * doorbell.
40 *
41 * To simplify the implementation, we allocate one gem object that contains all
42 * pages for doorbell, process descriptor and workqueue.
43 *
44 * The Scratch registers:
45 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
46 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
47 * triggers an interrupt on the GuC via another register write (0xC4C8).
48 * Firmware writes a success/fail code back to the action register after
49 * processes the request. The kernel driver polls waiting for this update and
50 * then proceeds.
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010051 * See intel_guc_send()
Dave Gordon44a28b12015-08-12 15:43:41 +010052 *
53 * Doorbells:
54 * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
55 * mapped into process space.
56 *
57 * Work Items:
58 * There are several types of work items that the host may place into a
59 * workqueue, each with its own requirements and limitations. Currently only
60 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
61 * represents in-order queue. The kernel driver packs ring tail pointer and an
62 * ELSP context descriptor dword into Work Item.
Dave Gordon7a9347f2016-09-12 21:19:37 +010063 * See guc_wq_item_append()
Dave Gordon44a28b12015-08-12 15:43:41 +010064 *
65 */
66
67/*
Dave Gordon44a28b12015-08-12 15:43:41 +010068 * Tell the GuC to allocate or deallocate a specific doorbell
69 */
70
Arkadiusz Hilera80bc452016-11-25 18:59:34 +010071static int guc_allocate_doorbell(struct intel_guc *guc,
72 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +010073{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010074 u32 action[] = {
75 INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
76 client->ctx_index
77 };
Dave Gordon44a28b12015-08-12 15:43:41 +010078
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010079 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Dave Gordon44a28b12015-08-12 15:43:41 +010080}
81
Arkadiusz Hilera80bc452016-11-25 18:59:34 +010082static int guc_release_doorbell(struct intel_guc *guc,
83 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +010084{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010085 u32 action[] = {
86 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
87 client->ctx_index
88 };
Dave Gordon44a28b12015-08-12 15:43:41 +010089
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010090 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Sagar Arun Kamble685534e2016-10-12 21:54:41 +053091}
92
Dave Gordon44a28b12015-08-12 15:43:41 +010093/*
94 * Initialise, update, or clear doorbell data shared with the GuC
95 *
96 * These functions modify shared data and so need access to the mapped
97 * client object which contains the page being used for the doorbell
98 */
99
Dave Gordona6674292016-06-13 17:57:32 +0100100static int guc_update_doorbell_id(struct intel_guc *guc,
101 struct i915_guc_client *client,
102 u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100103{
Chris Wilson8b797af2016-08-15 10:48:51 +0100104 struct sg_table *sg = guc->ctx_pool_vma->pages;
Dave Gordona6674292016-06-13 17:57:32 +0100105 void *doorbell_bitmap = guc->doorbell_bitmap;
Dave Gordon44a28b12015-08-12 15:43:41 +0100106 struct guc_doorbell_info *doorbell;
Dave Gordona6674292016-06-13 17:57:32 +0100107 struct guc_context_desc desc;
108 size_t len;
Dave Gordon44a28b12015-08-12 15:43:41 +0100109
Chris Wilson72aa0d82016-11-02 17:50:47 +0000110 doorbell = client->vaddr + client->doorbell_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100111
Dave Gordona6674292016-06-13 17:57:32 +0100112 if (client->doorbell_id != GUC_INVALID_DOORBELL_ID &&
113 test_bit(client->doorbell_id, doorbell_bitmap)) {
114 /* Deactivate the old doorbell */
115 doorbell->db_status = GUC_DOORBELL_DISABLED;
Arkadiusz Hilera80bc452016-11-25 18:59:34 +0100116 (void)guc_release_doorbell(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100117 __clear_bit(client->doorbell_id, doorbell_bitmap);
118 }
119
120 /* Update the GuC's idea of the doorbell ID */
121 len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
122 sizeof(desc) * client->ctx_index);
123 if (len != sizeof(desc))
124 return -EFAULT;
125 desc.db_id = new_id;
126 len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
127 sizeof(desc) * client->ctx_index);
128 if (len != sizeof(desc))
129 return -EFAULT;
130
131 client->doorbell_id = new_id;
132 if (new_id == GUC_INVALID_DOORBELL_ID)
133 return 0;
134
135 /* Activate the new doorbell */
136 __set_bit(new_id, doorbell_bitmap);
Dave Gordona6674292016-06-13 17:57:32 +0100137 doorbell->db_status = GUC_DOORBELL_ENABLED;
Chris Wilson597bdc82016-11-29 12:10:22 +0000138 doorbell->cookie = client->doorbell_cookie;
Arkadiusz Hilera80bc452016-11-25 18:59:34 +0100139 return guc_allocate_doorbell(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100140}
141
142static int guc_init_doorbell(struct intel_guc *guc,
143 struct i915_guc_client *client,
144 uint16_t db_id)
145{
146 return guc_update_doorbell_id(guc, client, db_id);
Dave Gordon44a28b12015-08-12 15:43:41 +0100147}
148
Dave Gordon44a28b12015-08-12 15:43:41 +0100149static void guc_disable_doorbell(struct intel_guc *guc,
150 struct i915_guc_client *client)
151{
Dave Gordona6674292016-06-13 17:57:32 +0100152 (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
Dave Gordon44a28b12015-08-12 15:43:41 +0100153
Dave Gordon44a28b12015-08-12 15:43:41 +0100154 /* XXX: wait for any interrupts */
155 /* XXX: wait for workqueue to drain */
156}
157
Dave Gordonf10d69a2016-06-13 17:57:33 +0100158static uint16_t
159select_doorbell_register(struct intel_guc *guc, uint32_t priority)
160{
161 /*
162 * The bitmap tracks which doorbell registers are currently in use.
163 * It is split into two halves; the first half is used for normal
164 * priority contexts, the second half for high-priority ones.
165 * Note that logically higher priorities are numerically less than
166 * normal ones, so the test below means "is it high-priority?"
167 */
168 const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
169 const uint16_t half = GUC_MAX_DOORBELLS / 2;
170 const uint16_t start = hi_pri ? half : 0;
171 const uint16_t end = start + half;
172 uint16_t id;
173
174 id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
175 if (id == end)
176 id = GUC_INVALID_DOORBELL_ID;
177
178 DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
179 hi_pri ? "high" : "normal", id);
180
181 return id;
182}
183
Dave Gordon44a28b12015-08-12 15:43:41 +0100184/*
185 * Select, assign and relase doorbell cachelines
186 *
187 * These functions track which doorbell cachelines are in use.
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100188 * The data they manipulate is protected by the intel_guc_send lock.
Dave Gordon44a28b12015-08-12 15:43:41 +0100189 */
190
191static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
192{
193 const uint32_t cacheline_size = cache_line_size();
194 uint32_t offset;
195
Dave Gordon44a28b12015-08-12 15:43:41 +0100196 /* Doorbell uses a single cache line within a page */
197 offset = offset_in_page(guc->db_cacheline);
198
199 /* Moving to next cache line to reduce contention */
200 guc->db_cacheline += cacheline_size;
201
Dave Gordon44a28b12015-08-12 15:43:41 +0100202 DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
203 offset, guc->db_cacheline, cacheline_size);
204
205 return offset;
206}
207
Dave Gordon44a28b12015-08-12 15:43:41 +0100208/*
209 * Initialise the process descriptor shared with the GuC firmware.
210 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100211static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100212 struct i915_guc_client *client)
213{
214 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100215
Chris Wilson72aa0d82016-11-02 17:50:47 +0000216 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100217
218 memset(desc, 0, sizeof(*desc));
219
220 /*
221 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
222 * space for ring3 clients (set them as in mmap_ioctl) or kernel
223 * space for kernel clients (map on demand instead? May make debug
224 * easier to have it mapped).
225 */
226 desc->wq_base_addr = 0;
227 desc->db_base_addr = 0;
228
229 desc->context_id = client->ctx_index;
230 desc->wq_size_bytes = client->wq_size;
231 desc->wq_status = WQ_STATUS_ACTIVE;
232 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100233}
234
235/*
236 * Initialise/clear the context descriptor shared with the GuC firmware.
237 *
238 * This descriptor tells the GuC where (in GGTT space) to find the important
239 * data structures relating to this client (doorbell, process descriptor,
240 * write queue, etc).
241 */
242
Dave Gordon7a9347f2016-09-12 21:19:37 +0100243static void guc_ctx_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100244 struct i915_guc_client *client)
245{
Alex Dai397097b2016-01-23 11:58:14 -0800246 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000247 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100248 struct i915_gem_context *ctx = client->owner;
Dave Gordon44a28b12015-08-12 15:43:41 +0100249 struct guc_context_desc desc;
250 struct sg_table *sg;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100251 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100252 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100253
254 memset(&desc, 0, sizeof(desc));
255
256 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
257 desc.context_id = client->ctx_index;
258 desc.priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100259 desc.db_id = client->doorbell_id;
260
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100261 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100262 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100263 uint32_t guc_engine_id = engine->guc_id;
264 struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100265
266 /* TODO: We have a design issue to be solved here. Only when we
267 * receive the first batch, we know which engine is used by the
268 * user. But here GuC expects the lrc and ring to be pinned. It
269 * is not an issue for default context, which is the only one
270 * for now who owns a GuC client. But for future owner of GuC
271 * client, need to make sure lrc is pinned prior to enter here.
272 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100273 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100274 break; /* XXX: continue? */
275
Chris Wilson9021ad02016-05-24 14:53:37 +0100276 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100277
278 /* The state page is after PPHWSP */
Chris Wilson57e88532016-08-15 10:48:57 +0100279 lrc->ring_lcra =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100280 i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100281 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100282 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100283
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100284 lrc->ring_begin = i915_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100285 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
286 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100287 lrc->ring_current_tail_pointer_value = 0;
288
Dave Gordonc18468c2016-08-09 15:19:22 +0100289 desc.engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100290 }
291
Dave Gordone02757d2016-08-09 15:19:21 +0100292 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
293 client->engines, desc.engines_used);
Alex Daid1675192015-08-12 15:43:43 +0100294 WARN_ON(desc.engines_used == 0);
295
Dave Gordon44a28b12015-08-12 15:43:41 +0100296 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100297 * The doorbell, process descriptor, and workqueue are all parts
298 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100299 */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100300 gfx_addr = i915_ggtt_offset(client->vma);
Chris Wilson8b797af2016-08-15 10:48:51 +0100301 desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100302 client->doorbell_offset;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000303 desc.db_trigger_cpu =
304 (uintptr_t)client->vaddr + client->doorbell_offset;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100305 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
306 desc.process_desc = gfx_addr + client->proc_desc_offset;
307 desc.wq_addr = gfx_addr + client->wq_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100308 desc.wq_size = client->wq_size;
309
310 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100311 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100312 * IsKMDCreatedContext client
313 */
314 desc.desc_private = (uintptr_t)client;
315
316 /* Pool context is pinned already */
Chris Wilson8b797af2016-08-15 10:48:51 +0100317 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100318 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
319 sizeof(desc) * client->ctx_index);
320}
321
Dave Gordon7a9347f2016-09-12 21:19:37 +0100322static void guc_ctx_desc_fini(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100323 struct i915_guc_client *client)
324{
325 struct guc_context_desc desc;
326 struct sg_table *sg;
327
328 memset(&desc, 0, sizeof(desc));
329
Chris Wilson8b797af2016-08-15 10:48:51 +0100330 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100331 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
332 sizeof(desc) * client->ctx_index);
333}
334
Dave Gordon7c2c2702016-05-13 15:36:32 +0100335/**
Dave Gordon7a9347f2016-09-12 21:19:37 +0100336 * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
Dave Gordon7c2c2702016-05-13 15:36:32 +0100337 * @request: request associated with the commands
338 *
339 * Return: 0 if space is available
340 * -EAGAIN if space is not currently available
341 *
342 * This function must be called (and must return 0) before a request
343 * is submitted to the GuC via i915_guc_submit() below. Once a result
Dave Gordon7a9347f2016-09-12 21:19:37 +0100344 * of 0 has been returned, it must be balanced by a corresponding
345 * call to submit().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100346 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100347 * Reservation allows the caller to determine in advance that space
Dave Gordon7c2c2702016-05-13 15:36:32 +0100348 * will be available for the next submission before committing resources
349 * to it, and helps avoid late failures with complicated recovery paths.
350 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100351int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100352{
Dave Gordon551aaec2016-05-13 15:36:33 +0100353 const size_t wqi_size = sizeof(struct guc_wq_item);
Dave Gordon7c2c2702016-05-13 15:36:32 +0100354 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000355 struct guc_process_desc *desc = gc->vaddr + gc->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100356 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100357 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100358
Chris Wilsondadd4812016-09-09 14:11:57 +0100359 spin_lock(&gc->wq_lock);
Dave Gordon551aaec2016-05-13 15:36:33 +0100360 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
Chris Wilsondadd4812016-09-09 14:11:57 +0100361 freespace -= gc->wq_rsvd;
362 if (likely(freespace >= wqi_size)) {
363 gc->wq_rsvd += wqi_size;
364 ret = 0;
365 } else {
366 gc->no_wq_space++;
367 ret = -EAGAIN;
368 }
369 spin_unlock(&gc->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800370
Chris Wilsondadd4812016-09-09 14:11:57 +0100371 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100372}
373
Chris Wilson5ba89902016-10-07 07:53:27 +0100374void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
375{
376 const size_t wqi_size = sizeof(struct guc_wq_item);
377 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
378
379 GEM_BUG_ON(READ_ONCE(gc->wq_rsvd) < wqi_size);
380
381 spin_lock(&gc->wq_lock);
382 gc->wq_rsvd -= wqi_size;
383 spin_unlock(&gc->wq_lock);
384}
385
Dave Gordon7a9347f2016-09-12 21:19:37 +0100386/* Construct a Work Item and append it to the GuC's Work Queue */
387static void guc_wq_item_append(struct i915_guc_client *gc,
388 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100389{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100390 /* wqi_len is in DWords, and does not include the one-word header */
391 const size_t wqi_size = sizeof(struct guc_wq_item);
392 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100393 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100394 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100395 struct guc_wq_item *wqi;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000396 u32 freespace, tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100397
Chris Wilson72aa0d82016-11-02 17:50:47 +0000398 desc = gc->vaddr + gc->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800399
Dave Gordon7a9347f2016-09-12 21:19:37 +0100400 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100401 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
402 GEM_BUG_ON(freespace < wqi_size);
403
404 /* The GuC firmware wants the tail index in QWords, not bytes */
405 tail = rq->tail;
406 GEM_BUG_ON(tail & 7);
407 tail >>= 3;
408 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100409
410 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
411 * should not have the case where structure wqi is across page, neither
412 * wrapped to the beginning. This simplifies the implementation below.
413 *
414 * XXX: if not the case, we need save data to a temp wqi and copy it to
415 * workqueue buffer dw by dw.
416 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100417 BUILD_BUG_ON(wqi_size != 16);
Chris Wilsondadd4812016-09-09 14:11:57 +0100418 GEM_BUG_ON(gc->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100419
Dave Gordon0a31afb2016-05-13 15:36:34 +0100420 /* postincrement WQ tail for next time */
421 wq_off = gc->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100422 GEM_BUG_ON(wq_off & (wqi_size - 1));
Dave Gordon0a31afb2016-05-13 15:36:34 +0100423 gc->wq_tail += wqi_size;
424 gc->wq_tail &= gc->wq_size - 1;
Chris Wilsondadd4812016-09-09 14:11:57 +0100425 gc->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100426
427 /* WQ starts from the page after doorbell / process_desc */
Chris Wilson72aa0d82016-11-02 17:50:47 +0000428 wqi = gc->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100429
Dave Gordon0a31afb2016-05-13 15:36:34 +0100430 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100431 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100432 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100433 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100434 WQ_NO_WCFLUSH_WAIT;
435
436 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100437 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100438
Dave Gordon44a28b12015-08-12 15:43:41 +0100439 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100440 wqi->fence_id = rq->global_seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100441}
442
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100443static int guc_ring_doorbell(struct i915_guc_client *gc)
444{
445 struct guc_process_desc *desc;
446 union guc_doorbell_qw db_cmp, db_exc, db_ret;
447 union guc_doorbell_qw *db;
448 int attempt = 2, ret = -EAGAIN;
449
Chris Wilson72aa0d82016-11-02 17:50:47 +0000450 desc = gc->vaddr + gc->proc_desc_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100451
452 /* Update the tail so it is visible to GuC */
453 desc->tail = gc->wq_tail;
454
455 /* current cookie */
456 db_cmp.db_status = GUC_DOORBELL_ENABLED;
Chris Wilson357248b2016-11-29 12:10:21 +0000457 db_cmp.cookie = gc->doorbell_cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100458
459 /* cookie to be updated */
460 db_exc.db_status = GUC_DOORBELL_ENABLED;
Chris Wilson357248b2016-11-29 12:10:21 +0000461 db_exc.cookie = gc->doorbell_cookie + 1;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100462 if (db_exc.cookie == 0)
463 db_exc.cookie = 1;
464
465 /* pointer of current doorbell cacheline */
Chris Wilson72aa0d82016-11-02 17:50:47 +0000466 db = gc->vaddr + gc->doorbell_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100467
468 while (attempt--) {
469 /* lets ring the doorbell */
470 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
471 db_cmp.value_qw, db_exc.value_qw);
472
473 /* if the exchange was successfully executed */
474 if (db_ret.value_qw == db_cmp.value_qw) {
475 /* db was successfully rung */
Chris Wilson357248b2016-11-29 12:10:21 +0000476 gc->doorbell_cookie = db_exc.cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100477 ret = 0;
478 break;
479 }
480
481 /* XXX: doorbell was lost and need to acquire it again */
482 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
483 break;
484
Dave Gordon535b2f52016-08-18 18:17:23 +0100485 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
486 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100487
488 /* update the cookie to newly read cookie from GuC */
489 db_cmp.cookie = db_ret.cookie;
490 db_exc.cookie = db_ret.cookie + 1;
491 if (db_exc.cookie == 0)
492 db_exc.cookie = 1;
493 }
494
495 return ret;
496}
497
Dave Gordon44a28b12015-08-12 15:43:41 +0100498/**
499 * i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700500 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100501 *
Dave Gordon7c2c2702016-05-13 15:36:32 +0100502 * Return: 0 on success, otherwise an errno.
503 * (Note: nonzero really shouldn't happen!)
504 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100505 * The caller must have already called i915_guc_wq_reserve() above with
506 * a result of 0 (success), guaranteeing that there is space in the work
507 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100508 *
509 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100510 * submit() when _reserve() says there's no space, or calls _submit()
511 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100512 *
513 * The only error here arises if the doorbell hardware isn't functioning
514 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100515 */
Chris Wilsonddd66c52016-08-02 22:50:31 +0100516static void i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100517{
Akash Goeled4596ea2016-10-25 22:05:23 +0530518 struct drm_i915_private *dev_priv = rq->i915;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000519 struct intel_engine_cs *engine = rq->engine;
520 unsigned int engine_id = engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100521 struct intel_guc *guc = &rq->i915->guc;
522 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100523 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100524
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000525 /* We keep the previous context alive until we retire the following
526 * request. This ensures that any the context object is still pinned
527 * for any residual writes the HW makes into it on the context switch
528 * into the next object following the breadcrumb. Otherwise, we may
529 * retire the context too early.
530 */
531 rq->previous_context = engine->last_context;
532 engine->last_context = rq->ctx;
533
534 i915_gem_request_submit(rq);
535
Chris Wilsondadd4812016-09-09 14:11:57 +0100536 spin_lock(&client->wq_lock);
Dave Gordon7a9347f2016-09-12 21:19:37 +0100537 guc_wq_item_append(client, rq);
Akash Goeled4596ea2016-10-25 22:05:23 +0530538
539 /* WA to flush out the pending GMADR writes to ring buffer. */
540 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
541 POSTING_READ_FW(GUC_STATUS);
542
Dave Gordon0a31afb2016-05-13 15:36:34 +0100543 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100544
Alex Dai397097b2016-01-23 11:58:14 -0800545 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100546 client->retcode = b_ret;
547 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100548 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100549
Alex Dai397097b2016-01-23 11:58:14 -0800550 guc->submissions[engine_id] += 1;
Chris Wilson65e47602016-10-28 13:58:49 +0100551 guc->last_seqno[engine_id] = rq->global_seqno;
Chris Wilsondadd4812016-09-09 14:11:57 +0100552 spin_unlock(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100553}
554
555/*
556 * Everything below here is concerned with setup & teardown, and is
557 * therefore not part of the somewhat time-critical batch-submission
558 * path of i915_guc_submit() above.
559 */
560
561/**
Chris Wilson8b797af2016-08-15 10:48:51 +0100562 * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
563 * @guc: the guc
564 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100565 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100566 * This is a wrapper to create an object for use with the GuC. In order to
567 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
568 * both some backing storage and a range inside the Global GTT. We must pin
569 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
570 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100571 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100572 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100573 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100574static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100575{
Chris Wilson8b797af2016-08-15 10:48:51 +0100576 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100577 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100578 struct i915_vma *vma;
579 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100580
Chris Wilson91c8a322016-07-05 10:40:23 +0100581 obj = i915_gem_object_create(&dev_priv->drm, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100582 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100583 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100584
Chris Wilson8b797af2016-08-15 10:48:51 +0100585 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
586 if (IS_ERR(vma))
587 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100588
Chris Wilson8b797af2016-08-15 10:48:51 +0100589 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
590 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
591 if (ret) {
592 vma = ERR_PTR(ret);
593 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100594 }
595
596 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
597 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
598
Chris Wilson8b797af2016-08-15 10:48:51 +0100599 return vma;
600
601err:
602 i915_gem_object_put(obj);
603 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100604}
605
Dave Gordon0daf5562016-06-10 18:29:25 +0100606static void
607guc_client_free(struct drm_i915_private *dev_priv,
608 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100609{
Dave Gordon44a28b12015-08-12 15:43:41 +0100610 struct intel_guc *guc = &dev_priv->guc;
611
612 if (!client)
613 return;
614
Dave Gordon44a28b12015-08-12 15:43:41 +0100615 /*
616 * XXX: wait for any outstanding submissions before freeing memory.
617 * Be sure to drop any locks
618 */
619
Chris Wilson72aa0d82016-11-02 17:50:47 +0000620 if (client->vaddr) {
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100621 /*
Dave Gordona6674292016-06-13 17:57:32 +0100622 * If we got as far as setting up a doorbell, make sure we
623 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100624 */
Dave Gordona6674292016-06-13 17:57:32 +0100625 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100626
Chris Wilson72aa0d82016-11-02 17:50:47 +0000627 i915_gem_object_unpin_map(client->vma->obj);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100628 }
629
Chris Wilson19880c42016-08-15 10:49:05 +0100630 i915_vma_unpin_and_release(&client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100631
632 if (client->ctx_index != GUC_INVALID_CTX_ID) {
Dave Gordon7a9347f2016-09-12 21:19:37 +0100633 guc_ctx_desc_fini(guc, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100634 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
635 }
636
637 kfree(client);
638}
639
Dave Gordon84b7f882016-08-09 15:19:20 +0100640/* Check that a doorbell register is in the expected state */
641static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
642{
643 struct drm_i915_private *dev_priv = guc_to_i915(guc);
644 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
645 uint32_t value = I915_READ(drbreg);
646 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
647 bool expected = test_bit(db_id, guc->doorbell_bitmap);
648
649 if (enabled == expected)
650 return true;
651
652 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
653 db_id, drbreg.reg, value,
654 expected ? "active" : "inactive");
655
656 return false;
657}
658
Dave Gordon4d757872016-06-13 17:57:34 +0100659/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100660 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100661 * in turn, to ensure that all doorbell h/w is (re)initialised.
662 */
663static void guc_init_doorbell_hw(struct intel_guc *guc)
664{
Dave Gordon4d757872016-06-13 17:57:34 +0100665 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100666 uint16_t db_id;
667 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100668
Dave Gordon84b7f882016-08-09 15:19:20 +0100669 /* Save client's original doorbell selection */
Dave Gordon4d757872016-06-13 17:57:34 +0100670 db_id = client->doorbell_id;
671
672 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100673 /* Skip if doorbell is OK */
674 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100675 continue;
676
Dave Gordon4d757872016-06-13 17:57:34 +0100677 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100678 if (err)
679 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
680 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100681 }
682
683 /* Restore to original value */
684 err = guc_update_doorbell_id(guc, client, db_id);
685 if (err)
Dave Gordon535b2f52016-08-18 18:17:23 +0100686 DRM_WARN("Failed to restore doorbell to %d, err %d\n",
687 db_id, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100688
Dave Gordon84b7f882016-08-09 15:19:20 +0100689 /* Read back & verify all doorbell registers */
690 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
691 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100692}
693
Dave Gordon44a28b12015-08-12 15:43:41 +0100694/**
695 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100696 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100697 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100698 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
699 * The kernel client to replace ExecList submission is created with
700 * NORMAL priority. Priority of a client for scheduler can be HIGH,
701 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700702 * @ctx: the context that owns the client (we use the default render
703 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100704 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100705 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100706 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100707static struct i915_guc_client *
708guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100709 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100710 uint32_t priority,
711 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100712{
713 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100714 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100715 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000716 void *vaddr;
Dave Gordona6674292016-06-13 17:57:32 +0100717 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100718
719 client = kzalloc(sizeof(*client), GFP_KERNEL);
720 if (!client)
721 return NULL;
722
Alex Daid1675192015-08-12 15:43:43 +0100723 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100724 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100725 client->engines = engines;
726 client->priority = priority;
727 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100728
729 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
730 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
731 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
732 client->ctx_index = GUC_INVALID_CTX_ID;
733 goto err;
734 }
735
736 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100737 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
738 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100739 goto err;
740
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100741 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100742 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000743
744 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
745 if (IS_ERR(vaddr))
746 goto err;
747
748 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100749
750 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100751 client->wq_offset = GUC_DB_SIZE;
752 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100753
Dave Gordonf10d69a2016-06-13 17:57:33 +0100754 db_id = select_doorbell_register(guc, client->priority);
755 if (db_id == GUC_INVALID_DOORBELL_ID)
756 /* XXX: evict a doorbell instead? */
757 goto err;
758
Dave Gordon44a28b12015-08-12 15:43:41 +0100759 client->doorbell_offset = select_doorbell_cacheline(guc);
760
761 /*
762 * Since the doorbell only requires a single cacheline, we can save
763 * space by putting the application process descriptor in the same
764 * page. Use the half of the page that doesn't include the doorbell.
765 */
766 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
767 client->proc_desc_offset = 0;
768 else
769 client->proc_desc_offset = (GUC_DB_SIZE / 2);
770
Dave Gordon7a9347f2016-09-12 21:19:37 +0100771 guc_proc_desc_init(guc, client);
772 guc_ctx_desc_init(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100773 if (guc_init_doorbell(guc, client, db_id))
Dave Gordon44a28b12015-08-12 15:43:41 +0100774 goto err;
775
Dave Gordone02757d2016-08-09 15:19:21 +0100776 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
777 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100778 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
779 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100780
781 return client;
782
783err:
Dave Gordon0daf5562016-06-10 18:29:25 +0100784 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100785 return NULL;
786}
787
Akash Goelf8240832016-10-12 21:54:34 +0530788/*
789 * Sub buffer switch callback. Called whenever relay has to switch to a new
790 * sub buffer, relay stays on the same sub buffer if 0 is returned.
791 */
792static int subbuf_start_callback(struct rchan_buf *buf,
793 void *subbuf,
794 void *prev_subbuf,
795 size_t prev_padding)
796{
797 /* Use no-overwrite mode by default, where relay will stop accepting
798 * new data if there are no empty sub buffers left.
799 * There is no strict synchronization enforced by relay between Consumer
800 * and Producer. In overwrite mode, there is a possibility of getting
801 * inconsistent/garbled data, the producer could be writing on to the
802 * same sub buffer from which Consumer is reading. This can't be avoided
803 * unless Consumer is fast enough and can always run in tandem with
804 * Producer.
805 */
806 if (relay_buf_full(buf))
807 return 0;
808
809 return 1;
810}
811
812/*
813 * file_create() callback. Creates relay file in debugfs.
814 */
815static struct dentry *create_buf_file_callback(const char *filename,
816 struct dentry *parent,
817 umode_t mode,
818 struct rchan_buf *buf,
819 int *is_global)
820{
821 struct dentry *buf_file;
822
Akash Goelf8240832016-10-12 21:54:34 +0530823 /* This to enable the use of a single buffer for the relay channel and
824 * correspondingly have a single file exposed to User, through which
825 * it can collect the logs in order without any post-processing.
Akash Goel1e6b8b02016-10-12 21:54:43 +0530826 * Need to set 'is_global' even if parent is NULL for early logging.
Akash Goelf8240832016-10-12 21:54:34 +0530827 */
828 *is_global = 1;
829
Akash Goel1e6b8b02016-10-12 21:54:43 +0530830 if (!parent)
831 return NULL;
832
Akash Goelf8240832016-10-12 21:54:34 +0530833 /* Not using the channel filename passed as an argument, since for each
834 * channel relay appends the corresponding CPU number to the filename
835 * passed in relay_open(). This should be fine as relay just needs a
836 * dentry of the file associated with the channel buffer and that file's
837 * name need not be same as the filename passed as an argument.
838 */
839 buf_file = debugfs_create_file("guc_log", mode,
840 parent, buf, &relay_file_operations);
841 return buf_file;
842}
843
844/*
845 * file_remove() default callback. Removes relay file in debugfs.
846 */
847static int remove_buf_file_callback(struct dentry *dentry)
848{
849 debugfs_remove(dentry);
850 return 0;
851}
852
853/* relay channel callbacks */
854static struct rchan_callbacks relay_callbacks = {
855 .subbuf_start = subbuf_start_callback,
856 .create_buf_file = create_buf_file_callback,
857 .remove_buf_file = remove_buf_file_callback,
858};
859
860static void guc_log_remove_relay_file(struct intel_guc *guc)
861{
862 relay_close(guc->log.relay_chan);
863}
864
Akash Goel1e6b8b02016-10-12 21:54:43 +0530865static int guc_log_create_relay_channel(struct intel_guc *guc)
Akash Goelf8240832016-10-12 21:54:34 +0530866{
867 struct drm_i915_private *dev_priv = guc_to_i915(guc);
868 struct rchan *guc_log_relay_chan;
Akash Goelf8240832016-10-12 21:54:34 +0530869 size_t n_subbufs, subbuf_size;
870
Akash Goel1e6b8b02016-10-12 21:54:43 +0530871 /* Keep the size of sub buffers same as shared log buffer */
872 subbuf_size = guc->log.vma->obj->base.size;
873
874 /* Store up to 8 snapshots, which is large enough to buffer sufficient
875 * boot time logs and provides enough leeway to User, in terms of
876 * latency, for consuming the logs from relay. Also doesn't take
877 * up too much memory.
878 */
879 n_subbufs = 8;
880
881 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
882 n_subbufs, &relay_callbacks, dev_priv);
883 if (!guc_log_relay_chan) {
884 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
885 return -ENOMEM;
886 }
887
888 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
889 guc->log.relay_chan = guc_log_relay_chan;
890 return 0;
891}
892
893static int guc_log_create_relay_file(struct intel_guc *guc)
894{
895 struct drm_i915_private *dev_priv = guc_to_i915(guc);
896 struct dentry *log_dir;
897 int ret;
898
Akash Goelf8240832016-10-12 21:54:34 +0530899 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
900 log_dir = dev_priv->drm.primary->debugfs_root;
901
902 /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
903 * not mounted and so can't create the relay file.
904 * The relay API seems to fit well with debugfs only, for availing relay
905 * there are 3 requirements which can be met for debugfs file only in a
906 * straightforward/clean manner :-
907 * i) Need the associated dentry pointer of the file, while opening the
908 * relay channel.
909 * ii) Should be able to use 'relay_file_operations' fops for the file.
910 * iii) Set the 'i_private' field of file's inode to the pointer of
911 * relay channel buffer.
912 */
913 if (!log_dir) {
914 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
915 return -ENODEV;
916 }
917
Akash Goel1e6b8b02016-10-12 21:54:43 +0530918 ret = relay_late_setup_files(guc->log.relay_chan, "guc_log", log_dir);
919 if (ret) {
920 DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
921 return ret;
Akash Goelf8240832016-10-12 21:54:34 +0530922 }
923
Akash Goelf8240832016-10-12 21:54:34 +0530924 return 0;
925}
926
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530927static void guc_move_to_next_buf(struct intel_guc *guc)
928{
Akash Goelf8240832016-10-12 21:54:34 +0530929 /* Make sure the updates made in the sub buffer are visible when
930 * Consumer sees the following update to offset inside the sub buffer.
931 */
932 smp_wmb();
933
934 /* All data has been written, so now move the offset of sub buffer. */
935 relay_reserve(guc->log.relay_chan, guc->log.vma->obj->base.size);
936
937 /* Switch to the next sub buffer */
938 relay_flush(guc->log.relay_chan);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530939}
940
941static void *guc_get_write_buffer(struct intel_guc *guc)
942{
Akash Goelf8240832016-10-12 21:54:34 +0530943 if (!guc->log.relay_chan)
944 return NULL;
945
946 /* Just get the base address of a new sub buffer and copy data into it
947 * ourselves. NULL will be returned in no-overwrite mode, if all sub
948 * buffers are full. Could have used the relay_write() to indirectly
949 * copy the data, but that would have been bit convoluted, as we need to
950 * write to only certain locations inside a sub buffer which cannot be
951 * done without using relay_reserve() along with relay_write(). So its
952 * better to use relay_reserve() alone.
953 */
954 return relay_reserve(guc->log.relay_chan, 0);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530955}
956
Akash Goel5aa1ee42016-10-12 21:54:36 +0530957static bool
958guc_check_log_buf_overflow(struct intel_guc *guc,
959 enum guc_log_buffer_type type, unsigned int full_cnt)
960{
961 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
962 bool overflow = false;
963
964 if (full_cnt != prev_full_cnt) {
965 overflow = true;
966
967 guc->log.prev_overflow_count[type] = full_cnt;
968 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
969
970 if (full_cnt < prev_full_cnt) {
971 /* buffer_full_cnt is a 4 bit counter */
972 guc->log.total_overflow_count[type] += 16;
973 }
974 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
975 }
976
977 return overflow;
978}
979
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530980static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
981{
982 switch (type) {
983 case GUC_ISR_LOG_BUFFER:
984 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
985 case GUC_DPC_LOG_BUFFER:
986 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
987 case GUC_CRASH_DUMP_LOG_BUFFER:
988 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
989 default:
990 MISSING_CASE(type);
991 }
992
993 return 0;
994}
995
996static void guc_read_update_log_buffer(struct intel_guc *guc)
997{
Akash Goel6941f3c2016-10-12 21:54:37 +0530998 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530999 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
1000 struct guc_log_buffer_state log_buf_state_local;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301001 enum guc_log_buffer_type type;
1002 void *src_data, *dst_data;
Akash Goel6941f3c2016-10-12 21:54:37 +05301003 bool new_overflow;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301004
1005 if (WARN_ON(!guc->log.buf_addr))
1006 return;
1007
1008 /* Get the pointer to shared GuC log buffer */
1009 log_buf_state = src_data = guc->log.buf_addr;
1010
1011 /* Get the pointer to local buffer to store the logs */
1012 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
1013
1014 /* Actual logs are present from the 2nd page */
1015 src_data += PAGE_SIZE;
1016 dst_data += PAGE_SIZE;
1017
1018 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
1019 /* Make a copy of the state structure, inside GuC log buffer
1020 * (which is uncached mapped), on the stack to avoid reading
1021 * from it multiple times.
1022 */
1023 memcpy(&log_buf_state_local, log_buf_state,
1024 sizeof(struct guc_log_buffer_state));
1025 buffer_size = guc_get_log_buffer_size(type);
Akash Goel6941f3c2016-10-12 21:54:37 +05301026 read_offset = log_buf_state_local.read_ptr;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301027 write_offset = log_buf_state_local.sampled_write_ptr;
Akash Goel5aa1ee42016-10-12 21:54:36 +05301028 full_cnt = log_buf_state_local.buffer_full_cnt;
1029
1030 /* Bookkeeping stuff */
1031 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
Akash Goel6941f3c2016-10-12 21:54:37 +05301032 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301033
1034 /* Update the state of shared log buffer */
1035 log_buf_state->read_ptr = write_offset;
1036 log_buf_state->flush_to_file = 0;
1037 log_buf_state++;
1038
1039 if (unlikely(!log_buf_snapshot_state))
1040 continue;
1041
1042 /* First copy the state structure in snapshot buffer */
1043 memcpy(log_buf_snapshot_state, &log_buf_state_local,
1044 sizeof(struct guc_log_buffer_state));
1045
1046 /* The write pointer could have been updated by GuC firmware,
1047 * after sending the flush interrupt to Host, for consistency
1048 * set write pointer value to same value of sampled_write_ptr
1049 * in the snapshot buffer.
1050 */
1051 log_buf_snapshot_state->write_ptr = write_offset;
1052 log_buf_snapshot_state++;
1053
1054 /* Now copy the actual logs. */
Akash Goel6941f3c2016-10-12 21:54:37 +05301055 if (unlikely(new_overflow)) {
1056 /* copy the whole buffer in case of overflow */
1057 read_offset = 0;
1058 write_offset = buffer_size;
1059 } else if (unlikely((read_offset > buffer_size) ||
1060 (write_offset > buffer_size))) {
1061 DRM_ERROR("invalid log buffer state\n");
1062 /* copy whole buffer as offsets are unreliable */
1063 read_offset = 0;
1064 write_offset = buffer_size;
1065 }
1066
1067 /* Just copy the newly written data */
1068 if (read_offset > write_offset) {
Akash Goel71706592016-10-12 21:54:42 +05301069 i915_memcpy_from_wc(dst_data, src_data, write_offset);
Akash Goel6941f3c2016-10-12 21:54:37 +05301070 bytes_to_copy = buffer_size - read_offset;
1071 } else {
1072 bytes_to_copy = write_offset - read_offset;
1073 }
Akash Goel71706592016-10-12 21:54:42 +05301074 i915_memcpy_from_wc(dst_data + read_offset,
1075 src_data + read_offset, bytes_to_copy);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301076
1077 src_data += buffer_size;
1078 dst_data += buffer_size;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301079 }
1080
1081 if (log_buf_snapshot_state)
1082 guc_move_to_next_buf(guc);
Akash Goelf8240832016-10-12 21:54:34 +05301083 else {
1084 /* Used rate limited to avoid deluge of messages, logs might be
1085 * getting consumed by User at a slow rate.
1086 */
1087 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
Akash Goel5aa1ee42016-10-12 21:54:36 +05301088 guc->log.capture_miss_count++;
Akash Goelf8240832016-10-12 21:54:34 +05301089 }
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301090}
1091
1092static void guc_capture_logs_work(struct work_struct *work)
1093{
1094 struct drm_i915_private *dev_priv =
1095 container_of(work, struct drm_i915_private, guc.log.flush_work);
1096
1097 i915_guc_capture_logs(dev_priv);
1098}
1099
1100static void guc_log_cleanup(struct intel_guc *guc)
1101{
1102 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1103
1104 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1105
1106 /* First disable the flush interrupt */
1107 gen9_disable_guc_interrupts(dev_priv);
1108
1109 if (guc->log.flush_wq)
1110 destroy_workqueue(guc->log.flush_wq);
1111
1112 guc->log.flush_wq = NULL;
1113
Akash Goelf8240832016-10-12 21:54:34 +05301114 if (guc->log.relay_chan)
1115 guc_log_remove_relay_file(guc);
1116
1117 guc->log.relay_chan = NULL;
1118
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301119 if (guc->log.buf_addr)
1120 i915_gem_object_unpin_map(guc->log.vma->obj);
1121
1122 guc->log.buf_addr = NULL;
1123}
1124
1125static int guc_log_create_extras(struct intel_guc *guc)
1126{
1127 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1128 void *vaddr;
1129 int ret;
1130
1131 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1132
1133 /* Nothing to do */
1134 if (i915.guc_log_level < 0)
1135 return 0;
1136
1137 if (!guc->log.buf_addr) {
Akash Goel71706592016-10-12 21:54:42 +05301138 /* Create a WC (Uncached for read) vmalloc mapping of log
1139 * buffer pages, so that we can directly get the data
1140 * (up-to-date) from memory.
1141 */
1142 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301143 if (IS_ERR(vaddr)) {
1144 ret = PTR_ERR(vaddr);
1145 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
1146 return ret;
1147 }
1148
1149 guc->log.buf_addr = vaddr;
1150 }
1151
Akash Goel1e6b8b02016-10-12 21:54:43 +05301152 if (!guc->log.relay_chan) {
1153 /* Create a relay channel, so that we have buffers for storing
1154 * the GuC firmware logs, the channel will be linked with a file
1155 * later on when debugfs is registered.
1156 */
1157 ret = guc_log_create_relay_channel(guc);
1158 if (ret)
1159 return ret;
1160 }
1161
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301162 if (!guc->log.flush_wq) {
1163 INIT_WORK(&guc->log.flush_work, guc_capture_logs_work);
1164
Akash Goel7ef54de2016-10-12 21:54:44 +05301165 /*
1166 * GuC log buffer flush work item has to do register access to
1167 * send the ack to GuC and this work item, if not synced before
1168 * suspend, can potentially get executed after the GFX device is
1169 * suspended.
1170 * By marking the WQ as freezable, we don't have to bother about
1171 * flushing of this work item from the suspend hooks, the pending
1172 * work item if any will be either executed before the suspend
1173 * or scheduled later on resume. This way the handling of work
1174 * item can be kept same between system suspend & rpm suspend.
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301175 */
Akash Goel7ef54de2016-10-12 21:54:44 +05301176 guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log",
1177 WQ_HIGHPRI | WQ_FREEZABLE);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301178 if (guc->log.flush_wq == NULL) {
1179 DRM_ERROR("Couldn't allocate the wq for GuC logging\n");
1180 return -ENOMEM;
1181 }
1182 }
1183
1184 return 0;
1185}
1186
Dave Gordon7a9347f2016-09-12 21:19:37 +01001187static void guc_log_create(struct intel_guc *guc)
Alex Dai4c7e77f2015-08-12 15:43:40 +01001188{
Chris Wilson8b797af2016-08-15 10:48:51 +01001189 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001190 unsigned long offset;
1191 uint32_t size, flags;
1192
Alex Dai4c7e77f2015-08-12 15:43:40 +01001193 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
1194 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
1195
1196 /* The first page is to save log buffer state. Allocate one
1197 * extra page for others in case for overlap */
1198 size = (1 + GUC_LOG_DPC_PAGES + 1 +
1199 GUC_LOG_ISR_PAGES + 1 +
1200 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
1201
Akash Goeld6b40b42016-10-12 21:54:29 +05301202 vma = guc->log.vma;
Chris Wilson8b797af2016-08-15 10:48:51 +01001203 if (!vma) {
Akash Goel71706592016-10-12 21:54:42 +05301204 /* We require SSE 4.1 for fast reads from the GuC log buffer and
1205 * it should be present on the chipsets supporting GuC based
1206 * submisssions.
1207 */
1208 if (WARN_ON(!i915_memcpy_from_wc(NULL, NULL, 0))) {
1209 /* logging will not be enabled */
1210 i915.guc_log_level = -1;
1211 return;
1212 }
1213
Chris Wilson8b797af2016-08-15 10:48:51 +01001214 vma = guc_allocate_vma(guc, size);
1215 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +01001216 /* logging will be off */
1217 i915.guc_log_level = -1;
1218 return;
1219 }
1220
Akash Goeld6b40b42016-10-12 21:54:29 +05301221 guc->log.vma = vma;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301222
1223 if (guc_log_create_extras(guc)) {
1224 guc_log_cleanup(guc);
1225 i915_vma_unpin_and_release(&guc->log.vma);
1226 i915.guc_log_level = -1;
1227 return;
1228 }
Alex Dai4c7e77f2015-08-12 15:43:40 +01001229 }
1230
1231 /* each allocated unit is a page */
1232 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
1233 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
1234 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
1235 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
1236
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001237 offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
Akash Goeld6b40b42016-10-12 21:54:29 +05301238 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001239}
1240
Akash Goelf8240832016-10-12 21:54:34 +05301241static int guc_log_late_setup(struct intel_guc *guc)
1242{
1243 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1244 int ret;
1245
1246 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1247
1248 if (i915.guc_log_level < 0)
1249 return -EINVAL;
1250
1251 /* If log_level was set as -1 at boot time, then setup needed to
1252 * handle log buffer flush interrupts would not have been done yet,
1253 * so do that now.
1254 */
1255 ret = guc_log_create_extras(guc);
1256 if (ret)
1257 goto err;
1258
1259 ret = guc_log_create_relay_file(guc);
1260 if (ret)
1261 goto err;
1262
1263 return 0;
1264err:
1265 guc_log_cleanup(guc);
1266 /* logging will remain off */
1267 i915.guc_log_level = -1;
1268 return ret;
1269}
1270
Dave Gordon7a9347f2016-09-12 21:19:37 +01001271static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -08001272{
1273 struct guc_policy *policy;
1274 u32 p, i;
1275
1276 policies->dpc_promote_time = 500000;
1277 policies->max_num_work_items = POLICY_MAX_NUM_WI;
1278
1279 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -08001280 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -08001281 policy = &policies->policy[p][i];
1282
1283 policy->execution_quantum = 1000000;
1284 policy->preemption_time = 500000;
1285 policy->fault_time = 250000;
1286 policy->policy_flags = 0;
1287 }
1288 }
1289
1290 policies->is_valid = 1;
1291}
1292
Dave Gordon7a9347f2016-09-12 21:19:37 +01001293static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -08001294{
1295 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +01001296 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -08001297 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -08001298 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -08001299 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001300 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301301 enum intel_engine_id id;
Alex Dai68371a92015-12-18 12:00:09 -08001302 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001303 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -08001304
1305 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -08001306 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
1307 sizeof(struct guc_mmio_reg_state) +
1308 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -08001309
Chris Wilson8b797af2016-08-15 10:48:51 +01001310 vma = guc->ads_vma;
1311 if (!vma) {
1312 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
1313 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -08001314 return;
1315
Chris Wilson8b797af2016-08-15 10:48:51 +01001316 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -08001317 }
1318
Chris Wilson8b797af2016-08-15 10:48:51 +01001319 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -08001320 ads = kmap(page);
1321
1322 /*
1323 * The GuC requires a "Golden Context" when it reinitialises
1324 * engines after a reset. Here we use the Render ring default
1325 * context, which must already exist and be pinned in the GGTT,
1326 * so its address won't change after we've told the GuC where
1327 * to find it.
1328 */
Akash Goel3b3f1652016-10-13 22:44:48 +05301329 engine = dev_priv->engine[RCS];
Chris Wilson57e88532016-08-15 10:48:57 +01001330 ads->golden_context_lrca = engine->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -08001331
Akash Goel3b3f1652016-10-13 22:44:48 +05301332 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001333 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -08001334
Alex Dai463704d2015-12-18 12:00:10 -08001335 /* GuC scheduling policies */
1336 policies = (void *)ads + sizeof(struct guc_ads);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001337 guc_policies_init(policies);
Alex Dai463704d2015-12-18 12:00:10 -08001338
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001339 ads->scheduler_policies =
1340 i915_ggtt_offset(vma) + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -08001341
Alex Dai5c148e02015-12-18 12:00:11 -08001342 /* MMIO reg state */
1343 reg_state = (void *)policies + sizeof(struct guc_policies);
1344
Akash Goel3b3f1652016-10-13 22:44:48 +05301345 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001346 reg_state->mmio_white_list[engine->guc_id].mmio_start =
1347 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -08001348
1349 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001350 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -08001351 }
1352
1353 ads->reg_state_addr = ads->scheduler_policies +
1354 sizeof(struct guc_policies);
1355
1356 ads->reg_state_buffer = ads->reg_state_addr +
1357 sizeof(struct guc_mmio_reg_state);
1358
Alex Dai68371a92015-12-18 12:00:09 -08001359 kunmap(page);
1360}
1361
Alex Daibac427f2015-08-12 15:43:39 +01001362/*
1363 * Set up the memory resources to be shared with the GuC. At this point,
1364 * we require just one object that can be mapped through the GGTT.
1365 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001366int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001367{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001368 const size_t ctxsize = sizeof(struct guc_context_desc);
1369 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1370 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001371 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001372 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +01001373
Dave Gordon29fb72c2016-06-07 09:14:50 +01001374 /* Wipe bitmap & delete client in case of reinitialisation */
1375 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001376 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001377
Alex Daibac427f2015-08-12 15:43:39 +01001378 if (!i915.enable_guc_submission)
1379 return 0; /* not enabled */
1380
Chris Wilson8b797af2016-08-15 10:48:51 +01001381 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001382 return 0; /* already allocated */
1383
Dave Gordon7a9347f2016-09-12 21:19:37 +01001384 vma = guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001385 if (IS_ERR(vma))
1386 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001387
Chris Wilson8b797af2016-08-15 10:48:51 +01001388 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001389 ida_init(&guc->ctx_ids);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001390 guc_log_create(guc);
1391 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001392
Alex Daibac427f2015-08-12 15:43:39 +01001393 return 0;
1394}
1395
Dave Gordonbeffa512016-06-10 18:29:26 +01001396int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001397{
Dave Gordon44a28b12015-08-12 15:43:41 +01001398 struct intel_guc *guc = &dev_priv->guc;
Akash Goel3b3f1652016-10-13 22:44:48 +05301399 struct drm_i915_gem_request *request;
Dave Gordon44a28b12015-08-12 15:43:41 +01001400 struct i915_guc_client *client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001401 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301402 enum intel_engine_id id;
Dave Gordon44a28b12015-08-12 15:43:41 +01001403
1404 /* client for execbuf submission */
Dave Gordon0daf5562016-06-10 18:29:25 +01001405 client = guc_client_alloc(dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +01001406 INTEL_INFO(dev_priv)->ring_mask,
Chris Wilson0ca5fa32016-05-24 14:53:40 +01001407 GUC_CTX_PRIORITY_KMD_NORMAL,
1408 dev_priv->kernel_context);
Dave Gordon44a28b12015-08-12 15:43:41 +01001409 if (!client) {
Dave Gordon535b2f52016-08-18 18:17:23 +01001410 DRM_ERROR("Failed to create normal GuC client!\n");
Dave Gordon44a28b12015-08-12 15:43:41 +01001411 return -ENOMEM;
1412 }
1413
1414 guc->execbuf_client = client;
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001415 intel_guc_sample_forcewake(guc);
Dave Gordon4d757872016-06-13 17:57:34 +01001416 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001417
Chris Wilsonddd66c52016-08-02 22:50:31 +01001418 /* Take over from manual control of ELSP (execlists) */
Akash Goel3b3f1652016-10-13 22:44:48 +05301419 for_each_engine(engine, dev_priv, id) {
Chris Wilsonddd66c52016-08-02 22:50:31 +01001420 engine->submit_request = i915_guc_submit;
Chris Wilson20311bd2016-11-14 20:41:03 +00001421 engine->schedule = NULL;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001422
Chris Wilson821ed7d2016-09-09 14:11:53 +01001423 /* Replay the current set of previously submitted requests */
Chris Wilson73cb9702016-10-28 13:58:46 +01001424 list_for_each_entry(request,
1425 &engine->timeline->requests, link) {
Chris Wilsondadd4812016-09-09 14:11:57 +01001426 client->wq_rsvd += sizeof(struct guc_wq_item);
Chris Wilson5590af32016-09-09 14:11:54 +01001427 if (i915_sw_fence_done(&request->submit))
1428 i915_guc_submit(request);
Chris Wilsondadd4812016-09-09 14:11:57 +01001429 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001430 }
1431
Dave Gordon44a28b12015-08-12 15:43:41 +01001432 return 0;
1433}
1434
Dave Gordonbeffa512016-06-10 18:29:26 +01001435void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001436{
Dave Gordon44a28b12015-08-12 15:43:41 +01001437 struct intel_guc *guc = &dev_priv->guc;
1438
Chris Wilsonddd66c52016-08-02 22:50:31 +01001439 if (!guc->execbuf_client)
1440 return;
1441
Chris Wilsonddd66c52016-08-02 22:50:31 +01001442 /* Revert back to manual ELSP submission */
1443 intel_execlists_enable_submission(dev_priv);
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +01001444
1445 guc_client_free(dev_priv, guc->execbuf_client);
1446 guc->execbuf_client = NULL;
Dave Gordon44a28b12015-08-12 15:43:41 +01001447}
1448
Dave Gordonbeffa512016-06-10 18:29:26 +01001449void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001450{
Alex Daibac427f2015-08-12 15:43:39 +01001451 struct intel_guc *guc = &dev_priv->guc;
1452
Chris Wilson19880c42016-08-15 10:49:05 +01001453 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301454 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001455
Chris Wilson8b797af2016-08-15 10:48:51 +01001456 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001457 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001458 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001459}
Alex Daia1c41992015-09-30 09:46:37 -07001460
1461/**
1462 * intel_guc_suspend() - notify GuC entering suspend state
1463 * @dev: drm device
1464 */
1465int intel_guc_suspend(struct drm_device *dev)
1466{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001467 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001468 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001469 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001470 u32 data[3];
1471
Dave Gordonfce91f22016-05-20 11:42:42 +01001472 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001473 return 0;
1474
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301475 gen9_disable_guc_interrupts(dev_priv);
1476
Dave Gordoned54c1a2016-01-19 19:02:54 +00001477 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001478
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001479 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001480 /* any value greater than GUC_POWER_D0 */
1481 data[1] = GUC_POWER_D1;
1482 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001483 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001484
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001485 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001486}
1487
1488
1489/**
1490 * intel_guc_resume() - notify GuC resuming from suspend state
1491 * @dev: drm device
1492 */
1493int intel_guc_resume(struct drm_device *dev)
1494{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001495 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001496 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001497 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001498 u32 data[3];
1499
Dave Gordonfce91f22016-05-20 11:42:42 +01001500 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001501 return 0;
1502
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301503 if (i915.guc_log_level >= 0)
1504 gen9_enable_guc_interrupts(dev_priv);
1505
Dave Gordoned54c1a2016-01-19 19:02:54 +00001506 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001507
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001508 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001509 data[1] = GUC_POWER_D0;
1510 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001511 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001512
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001513 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001514}
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301515
1516void i915_guc_capture_logs(struct drm_i915_private *dev_priv)
1517{
1518 guc_read_update_log_buffer(&dev_priv->guc);
1519
1520 /* Generally device is expected to be active only at this
1521 * time, so get/put should be really quick.
1522 */
1523 intel_runtime_pm_get(dev_priv);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001524 intel_guc_log_flush_complete(&dev_priv->guc);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301525 intel_runtime_pm_put(dev_priv);
1526}
Akash Goelf8240832016-10-12 21:54:34 +05301527
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301528void i915_guc_flush_logs(struct drm_i915_private *dev_priv)
1529{
1530 if (!i915.enable_guc_submission || (i915.guc_log_level < 0))
1531 return;
1532
1533 /* First disable the interrupts, will be renabled afterwards */
1534 gen9_disable_guc_interrupts(dev_priv);
1535
1536 /* Before initiating the forceful flush, wait for any pending/ongoing
1537 * flush to complete otherwise forceful flush may not actually happen.
1538 */
1539 flush_work(&dev_priv->guc.log.flush_work);
1540
1541 /* Ask GuC to update the log buffer state */
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001542 intel_guc_log_flush(&dev_priv->guc);
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301543
1544 /* GuC would have updated log buffer by now, so capture it */
1545 i915_guc_capture_logs(dev_priv);
1546}
1547
Akash Goelf8240832016-10-12 21:54:34 +05301548void i915_guc_unregister(struct drm_i915_private *dev_priv)
1549{
1550 if (!i915.enable_guc_submission)
1551 return;
1552
1553 mutex_lock(&dev_priv->drm.struct_mutex);
1554 guc_log_cleanup(&dev_priv->guc);
1555 mutex_unlock(&dev_priv->drm.struct_mutex);
1556}
1557
1558void i915_guc_register(struct drm_i915_private *dev_priv)
1559{
1560 if (!i915.enable_guc_submission)
1561 return;
1562
1563 mutex_lock(&dev_priv->drm.struct_mutex);
1564 guc_log_late_setup(&dev_priv->guc);
1565 mutex_unlock(&dev_priv->drm.struct_mutex);
1566}
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301567
1568int i915_guc_log_control(struct drm_i915_private *dev_priv, u64 control_val)
1569{
1570 union guc_log_control log_param;
1571 int ret;
1572
1573 log_param.value = control_val;
1574
1575 if (log_param.verbosity < GUC_LOG_VERBOSITY_MIN ||
1576 log_param.verbosity > GUC_LOG_VERBOSITY_MAX)
1577 return -EINVAL;
1578
1579 /* This combination doesn't make sense & won't have any effect */
1580 if (!log_param.logging_enabled && (i915.guc_log_level < 0))
1581 return 0;
1582
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001583 ret = intel_guc_log_control(&dev_priv->guc, log_param.value);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301584 if (ret < 0) {
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001585 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301586 return ret;
1587 }
1588
1589 i915.guc_log_level = log_param.verbosity;
1590
1591 /* If log_level was set as -1 at boot time, then the relay channel file
1592 * wouldn't have been created by now and interrupts also would not have
1593 * been enabled.
1594 */
1595 if (!dev_priv->guc.log.relay_chan) {
1596 ret = guc_log_late_setup(&dev_priv->guc);
1597 if (!ret)
1598 gen9_enable_guc_interrupts(dev_priv);
1599 } else if (!log_param.logging_enabled) {
1600 /* Once logging is disabled, GuC won't generate logs & send an
1601 * interrupt. But there could be some data in the log buffer
1602 * which is yet to be captured. So request GuC to update the log
1603 * buffer state and then collect the left over logs.
1604 */
1605 i915_guc_flush_logs(dev_priv);
1606
1607 /* As logging is disabled, update log level to reflect that */
1608 i915.guc_log_level = -1;
1609 } else {
1610 /* In case interrupts were disabled, enable them now */
1611 gen9_enable_guc_interrupts(dev_priv);
1612 }
1613
1614 return ret;
1615}