blob: 7fb0077aedcd08e822027d016e3b45b5bd9a2664 [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>
Akash Goelf8240832016-10-12 21:54:34 +053026#include <linux/debugfs.h>
27#include <linux/relay.h>
Alex Daibac427f2015-08-12 15:43:39 +010028#include "i915_drv.h"
29#include "intel_guc.h"
30
31/**
Alex Daifeda33e2015-10-19 16:10:54 -070032 * DOC: GuC-based command submission
Dave Gordon44a28b12015-08-12 15:43:41 +010033 *
34 * i915_guc_client:
35 * We use the term client to avoid confusion with contexts. A i915_guc_client is
36 * equivalent to GuC object guc_context_desc. This context descriptor is
37 * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
38 * and workqueue for it. Also the process descriptor (guc_process_desc), which
39 * is mapped to client space. So the client can write Work Item then ring the
40 * doorbell.
41 *
42 * To simplify the implementation, we allocate one gem object that contains all
43 * pages for doorbell, process descriptor and workqueue.
44 *
45 * The Scratch registers:
46 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
47 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
48 * triggers an interrupt on the GuC via another register write (0xC4C8).
49 * Firmware writes a success/fail code back to the action register after
50 * processes the request. The kernel driver polls waiting for this update and
51 * then proceeds.
52 * See host2guc_action()
53 *
54 * Doorbells:
55 * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
56 * mapped into process space.
57 *
58 * Work Items:
59 * There are several types of work items that the host may place into a
60 * workqueue, each with its own requirements and limitations. Currently only
61 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
62 * represents in-order queue. The kernel driver packs ring tail pointer and an
63 * ELSP context descriptor dword into Work Item.
Dave Gordon7a9347f2016-09-12 21:19:37 +010064 * See guc_wq_item_append()
Dave Gordon44a28b12015-08-12 15:43:41 +010065 *
66 */
67
68/*
69 * Read GuC command/status register (SOFT_SCRATCH_0)
70 * Return true if it contains a response rather than a command
71 */
72static inline bool host2guc_action_response(struct drm_i915_private *dev_priv,
73 u32 *status)
74{
75 u32 val = I915_READ(SOFT_SCRATCH(0));
76 *status = val;
77 return GUC2HOST_IS_RESPONSE(val);
78}
79
80static int host2guc_action(struct intel_guc *guc, u32 *data, u32 len)
81{
82 struct drm_i915_private *dev_priv = guc_to_i915(guc);
83 u32 status;
84 int i;
85 int ret;
86
87 if (WARN_ON(len < 1 || len > 15))
88 return -EINVAL;
89
Akash Goel5dd79892016-10-12 21:54:35 +053090 mutex_lock(&guc->action_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +010091 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Dave Gordon44a28b12015-08-12 15:43:41 +010092
93 dev_priv->guc.action_count += 1;
94 dev_priv->guc.action_cmd = data[0];
95
96 for (i = 0; i < len; i++)
97 I915_WRITE(SOFT_SCRATCH(i), data[i]);
98
99 POSTING_READ(SOFT_SCRATCH(i - 1));
100
101 I915_WRITE(HOST2GUC_INTERRUPT, HOST2GUC_TRIGGER);
102
Dave Gordonab0e4552016-07-06 15:30:11 +0100103 /*
104 * Fast commands should complete in less than 10us, so sample quickly
105 * up to that length of time, then switch to a slower sleep-wait loop.
106 * No HOST2GUC command should ever take longer than 10ms.
107 */
108 ret = wait_for_us(host2guc_action_response(dev_priv, &status), 10);
109 if (ret)
110 ret = wait_for(host2guc_action_response(dev_priv, &status), 10);
Dave Gordon44a28b12015-08-12 15:43:41 +0100111 if (status != GUC2HOST_STATUS_SUCCESS) {
112 /*
113 * Either the GuC explicitly returned an error (which
114 * we convert to -EIO here) or no response at all was
115 * received within the timeout limit (-ETIMEDOUT)
116 */
117 if (ret != -ETIMEDOUT)
118 ret = -EIO;
119
Dave Gordon535b2f52016-08-18 18:17:23 +0100120 DRM_WARN("Action 0x%X failed; ret=%d status=0x%08X response=0x%08X\n",
121 data[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
Dave Gordon44a28b12015-08-12 15:43:41 +0100122
123 dev_priv->guc.action_fail += 1;
124 dev_priv->guc.action_err = ret;
125 }
126 dev_priv->guc.action_status = status;
127
Dave Gordon44a28b12015-08-12 15:43:41 +0100128 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Akash Goel5dd79892016-10-12 21:54:35 +0530129 mutex_unlock(&guc->action_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100130
131 return ret;
132}
133
134/*
135 * Tell the GuC to allocate or deallocate a specific doorbell
136 */
137
138static int host2guc_allocate_doorbell(struct intel_guc *guc,
139 struct i915_guc_client *client)
140{
141 u32 data[2];
142
143 data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL;
144 data[1] = client->ctx_index;
145
146 return host2guc_action(guc, data, 2);
147}
148
149static int host2guc_release_doorbell(struct intel_guc *guc,
150 struct i915_guc_client *client)
151{
152 u32 data[2];
153
154 data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL;
155 data[1] = client->ctx_index;
156
157 return host2guc_action(guc, data, 2);
158}
159
Alex Daif5d3c3e2015-08-18 14:34:47 -0700160static int host2guc_sample_forcewake(struct intel_guc *guc,
161 struct i915_guc_client *client)
162{
163 struct drm_i915_private *dev_priv = guc_to_i915(guc);
164 u32 data[2];
165
166 data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
Alex Dai93f25312015-09-25 11:46:56 -0700167 /* WaRsDisableCoarsePowerGating:skl,bxt */
Tvrtko Ursulin61251512016-06-21 15:07:14 +0100168 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Alex Dai93f25312015-09-25 11:46:56 -0700169 data[1] = 0;
170 else
171 /* bit 0 and 1 are for Render and Media domain separately */
172 data[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
Alex Daif5d3c3e2015-08-18 14:34:47 -0700173
Alex Dai93f25312015-09-25 11:46:56 -0700174 return host2guc_action(guc, data, ARRAY_SIZE(data));
Alex Daif5d3c3e2015-08-18 14:34:47 -0700175}
176
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530177static int host2guc_logbuffer_flush_complete(struct intel_guc *guc)
178{
179 u32 data[1];
180
181 data[0] = HOST2GUC_ACTION_LOG_BUFFER_FILE_FLUSH_COMPLETE;
182
183 return host2guc_action(guc, data, 1);
184}
185
Dave Gordon44a28b12015-08-12 15:43:41 +0100186/*
187 * Initialise, update, or clear doorbell data shared with the GuC
188 *
189 * These functions modify shared data and so need access to the mapped
190 * client object which contains the page being used for the doorbell
191 */
192
Dave Gordona6674292016-06-13 17:57:32 +0100193static int guc_update_doorbell_id(struct intel_guc *guc,
194 struct i915_guc_client *client,
195 u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100196{
Chris Wilson8b797af2016-08-15 10:48:51 +0100197 struct sg_table *sg = guc->ctx_pool_vma->pages;
Dave Gordona6674292016-06-13 17:57:32 +0100198 void *doorbell_bitmap = guc->doorbell_bitmap;
Dave Gordon44a28b12015-08-12 15:43:41 +0100199 struct guc_doorbell_info *doorbell;
Dave Gordona6674292016-06-13 17:57:32 +0100200 struct guc_context_desc desc;
201 size_t len;
Dave Gordon44a28b12015-08-12 15:43:41 +0100202
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100203 doorbell = client->client_base + client->doorbell_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100204
Dave Gordona6674292016-06-13 17:57:32 +0100205 if (client->doorbell_id != GUC_INVALID_DOORBELL_ID &&
206 test_bit(client->doorbell_id, doorbell_bitmap)) {
207 /* Deactivate the old doorbell */
208 doorbell->db_status = GUC_DOORBELL_DISABLED;
209 (void)host2guc_release_doorbell(guc, client);
210 __clear_bit(client->doorbell_id, doorbell_bitmap);
211 }
212
213 /* Update the GuC's idea of the doorbell ID */
214 len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
215 sizeof(desc) * client->ctx_index);
216 if (len != sizeof(desc))
217 return -EFAULT;
218 desc.db_id = new_id;
219 len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
220 sizeof(desc) * client->ctx_index);
221 if (len != sizeof(desc))
222 return -EFAULT;
223
224 client->doorbell_id = new_id;
225 if (new_id == GUC_INVALID_DOORBELL_ID)
226 return 0;
227
228 /* Activate the new doorbell */
229 __set_bit(new_id, doorbell_bitmap);
Dave Gordon44a28b12015-08-12 15:43:41 +0100230 doorbell->cookie = 0;
Dave Gordona6674292016-06-13 17:57:32 +0100231 doorbell->db_status = GUC_DOORBELL_ENABLED;
232 return host2guc_allocate_doorbell(guc, client);
233}
234
235static int guc_init_doorbell(struct intel_guc *guc,
236 struct i915_guc_client *client,
237 uint16_t db_id)
238{
239 return guc_update_doorbell_id(guc, client, db_id);
Dave Gordon44a28b12015-08-12 15:43:41 +0100240}
241
Dave Gordon44a28b12015-08-12 15:43:41 +0100242static void guc_disable_doorbell(struct intel_guc *guc,
243 struct i915_guc_client *client)
244{
Dave Gordona6674292016-06-13 17:57:32 +0100245 (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
Dave Gordon44a28b12015-08-12 15:43:41 +0100246
Dave Gordon44a28b12015-08-12 15:43:41 +0100247 /* XXX: wait for any interrupts */
248 /* XXX: wait for workqueue to drain */
249}
250
Dave Gordonf10d69a2016-06-13 17:57:33 +0100251static uint16_t
252select_doorbell_register(struct intel_guc *guc, uint32_t priority)
253{
254 /*
255 * The bitmap tracks which doorbell registers are currently in use.
256 * It is split into two halves; the first half is used for normal
257 * priority contexts, the second half for high-priority ones.
258 * Note that logically higher priorities are numerically less than
259 * normal ones, so the test below means "is it high-priority?"
260 */
261 const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
262 const uint16_t half = GUC_MAX_DOORBELLS / 2;
263 const uint16_t start = hi_pri ? half : 0;
264 const uint16_t end = start + half;
265 uint16_t id;
266
267 id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
268 if (id == end)
269 id = GUC_INVALID_DOORBELL_ID;
270
271 DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
272 hi_pri ? "high" : "normal", id);
273
274 return id;
275}
276
Dave Gordon44a28b12015-08-12 15:43:41 +0100277/*
278 * Select, assign and relase doorbell cachelines
279 *
280 * These functions track which doorbell cachelines are in use.
281 * The data they manipulate is protected by the host2guc lock.
282 */
283
284static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
285{
286 const uint32_t cacheline_size = cache_line_size();
287 uint32_t offset;
288
Dave Gordon44a28b12015-08-12 15:43:41 +0100289 /* Doorbell uses a single cache line within a page */
290 offset = offset_in_page(guc->db_cacheline);
291
292 /* Moving to next cache line to reduce contention */
293 guc->db_cacheline += cacheline_size;
294
Dave Gordon44a28b12015-08-12 15:43:41 +0100295 DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
296 offset, guc->db_cacheline, cacheline_size);
297
298 return offset;
299}
300
Dave Gordon44a28b12015-08-12 15:43:41 +0100301/*
302 * Initialise the process descriptor shared with the GuC firmware.
303 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100304static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100305 struct i915_guc_client *client)
306{
307 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100308
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100309 desc = client->client_base + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100310
311 memset(desc, 0, sizeof(*desc));
312
313 /*
314 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
315 * space for ring3 clients (set them as in mmap_ioctl) or kernel
316 * space for kernel clients (map on demand instead? May make debug
317 * easier to have it mapped).
318 */
319 desc->wq_base_addr = 0;
320 desc->db_base_addr = 0;
321
322 desc->context_id = client->ctx_index;
323 desc->wq_size_bytes = client->wq_size;
324 desc->wq_status = WQ_STATUS_ACTIVE;
325 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100326}
327
328/*
329 * Initialise/clear the context descriptor shared with the GuC firmware.
330 *
331 * This descriptor tells the GuC where (in GGTT space) to find the important
332 * data structures relating to this client (doorbell, process descriptor,
333 * write queue, etc).
334 */
335
Dave Gordon7a9347f2016-09-12 21:19:37 +0100336static void guc_ctx_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100337 struct i915_guc_client *client)
338{
Alex Dai397097b2016-01-23 11:58:14 -0800339 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000340 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100341 struct i915_gem_context *ctx = client->owner;
Dave Gordon44a28b12015-08-12 15:43:41 +0100342 struct guc_context_desc desc;
343 struct sg_table *sg;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100344 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100345 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100346
347 memset(&desc, 0, sizeof(desc));
348
349 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
350 desc.context_id = client->ctx_index;
351 desc.priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100352 desc.db_id = client->doorbell_id;
353
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100354 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100355 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100356 uint32_t guc_engine_id = engine->guc_id;
357 struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100358
359 /* TODO: We have a design issue to be solved here. Only when we
360 * receive the first batch, we know which engine is used by the
361 * user. But here GuC expects the lrc and ring to be pinned. It
362 * is not an issue for default context, which is the only one
363 * for now who owns a GuC client. But for future owner of GuC
364 * client, need to make sure lrc is pinned prior to enter here.
365 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100366 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100367 break; /* XXX: continue? */
368
Chris Wilson9021ad02016-05-24 14:53:37 +0100369 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100370
371 /* The state page is after PPHWSP */
Chris Wilson57e88532016-08-15 10:48:57 +0100372 lrc->ring_lcra =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100373 i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100374 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100375 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100376
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100377 lrc->ring_begin = i915_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100378 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
379 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100380 lrc->ring_current_tail_pointer_value = 0;
381
Dave Gordonc18468c2016-08-09 15:19:22 +0100382 desc.engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100383 }
384
Dave Gordone02757d2016-08-09 15:19:21 +0100385 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
386 client->engines, desc.engines_used);
Alex Daid1675192015-08-12 15:43:43 +0100387 WARN_ON(desc.engines_used == 0);
388
Dave Gordon44a28b12015-08-12 15:43:41 +0100389 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100390 * The doorbell, process descriptor, and workqueue are all parts
391 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100392 */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100393 gfx_addr = i915_ggtt_offset(client->vma);
Chris Wilson8b797af2016-08-15 10:48:51 +0100394 desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100395 client->doorbell_offset;
396 desc.db_trigger_cpu = (uintptr_t)client->client_base +
397 client->doorbell_offset;
398 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
399 desc.process_desc = gfx_addr + client->proc_desc_offset;
400 desc.wq_addr = gfx_addr + client->wq_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100401 desc.wq_size = client->wq_size;
402
403 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100404 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100405 * IsKMDCreatedContext client
406 */
407 desc.desc_private = (uintptr_t)client;
408
409 /* Pool context is pinned already */
Chris Wilson8b797af2016-08-15 10:48:51 +0100410 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100411 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
412 sizeof(desc) * client->ctx_index);
413}
414
Dave Gordon7a9347f2016-09-12 21:19:37 +0100415static void guc_ctx_desc_fini(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100416 struct i915_guc_client *client)
417{
418 struct guc_context_desc desc;
419 struct sg_table *sg;
420
421 memset(&desc, 0, sizeof(desc));
422
Chris Wilson8b797af2016-08-15 10:48:51 +0100423 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100424 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
425 sizeof(desc) * client->ctx_index);
426}
427
Dave Gordon7c2c2702016-05-13 15:36:32 +0100428/**
Dave Gordon7a9347f2016-09-12 21:19:37 +0100429 * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
Dave Gordon7c2c2702016-05-13 15:36:32 +0100430 * @request: request associated with the commands
431 *
432 * Return: 0 if space is available
433 * -EAGAIN if space is not currently available
434 *
435 * This function must be called (and must return 0) before a request
436 * is submitted to the GuC via i915_guc_submit() below. Once a result
Dave Gordon7a9347f2016-09-12 21:19:37 +0100437 * of 0 has been returned, it must be balanced by a corresponding
438 * call to submit().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100439 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100440 * Reservation allows the caller to determine in advance that space
Dave Gordon7c2c2702016-05-13 15:36:32 +0100441 * will be available for the next submission before committing resources
442 * to it, and helps avoid late failures with complicated recovery paths.
443 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100444int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100445{
Dave Gordon551aaec2016-05-13 15:36:33 +0100446 const size_t wqi_size = sizeof(struct guc_wq_item);
Dave Gordon7c2c2702016-05-13 15:36:32 +0100447 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
Chris Wilsondadd4812016-09-09 14:11:57 +0100448 struct guc_process_desc *desc = gc->client_base + gc->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100449 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100450 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100451
Chris Wilsondadd4812016-09-09 14:11:57 +0100452 spin_lock(&gc->wq_lock);
Dave Gordon551aaec2016-05-13 15:36:33 +0100453 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
Chris Wilsondadd4812016-09-09 14:11:57 +0100454 freespace -= gc->wq_rsvd;
455 if (likely(freespace >= wqi_size)) {
456 gc->wq_rsvd += wqi_size;
457 ret = 0;
458 } else {
459 gc->no_wq_space++;
460 ret = -EAGAIN;
461 }
462 spin_unlock(&gc->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800463
Chris Wilsondadd4812016-09-09 14:11:57 +0100464 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100465}
466
Chris Wilson5ba89902016-10-07 07:53:27 +0100467void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
468{
469 const size_t wqi_size = sizeof(struct guc_wq_item);
470 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
471
472 GEM_BUG_ON(READ_ONCE(gc->wq_rsvd) < wqi_size);
473
474 spin_lock(&gc->wq_lock);
475 gc->wq_rsvd -= wqi_size;
476 spin_unlock(&gc->wq_lock);
477}
478
Dave Gordon7a9347f2016-09-12 21:19:37 +0100479/* Construct a Work Item and append it to the GuC's Work Queue */
480static void guc_wq_item_append(struct i915_guc_client *gc,
481 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100482{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100483 /* wqi_len is in DWords, and does not include the one-word header */
484 const size_t wqi_size = sizeof(struct guc_wq_item);
485 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100486 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100487 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100488 struct guc_wq_item *wqi;
489 void *base;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100490 u32 freespace, tail, wq_off, wq_page;
Dave Gordon44a28b12015-08-12 15:43:41 +0100491
Alex Daia5916e82016-04-19 16:08:35 +0100492 desc = gc->client_base + gc->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800493
Dave Gordon7a9347f2016-09-12 21:19:37 +0100494 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100495 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
496 GEM_BUG_ON(freespace < wqi_size);
497
498 /* The GuC firmware wants the tail index in QWords, not bytes */
499 tail = rq->tail;
500 GEM_BUG_ON(tail & 7);
501 tail >>= 3;
502 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100503
504 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
505 * should not have the case where structure wqi is across page, neither
506 * wrapped to the beginning. This simplifies the implementation below.
507 *
508 * XXX: if not the case, we need save data to a temp wqi and copy it to
509 * workqueue buffer dw by dw.
510 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100511 BUILD_BUG_ON(wqi_size != 16);
Chris Wilsondadd4812016-09-09 14:11:57 +0100512 GEM_BUG_ON(gc->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100513
Dave Gordon0a31afb2016-05-13 15:36:34 +0100514 /* postincrement WQ tail for next time */
515 wq_off = gc->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100516 GEM_BUG_ON(wq_off & (wqi_size - 1));
Dave Gordon0a31afb2016-05-13 15:36:34 +0100517 gc->wq_tail += wqi_size;
518 gc->wq_tail &= gc->wq_size - 1;
Chris Wilsondadd4812016-09-09 14:11:57 +0100519 gc->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100520
521 /* WQ starts from the page after doorbell / process_desc */
522 wq_page = (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT;
Dave Gordon44a28b12015-08-12 15:43:41 +0100523 wq_off &= PAGE_SIZE - 1;
Chris Wilson8b797af2016-08-15 10:48:51 +0100524 base = kmap_atomic(i915_gem_object_get_page(gc->vma->obj, wq_page));
Dave Gordon44a28b12015-08-12 15:43:41 +0100525 wqi = (struct guc_wq_item *)((char *)base + wq_off);
526
Dave Gordon0a31afb2016-05-13 15:36:34 +0100527 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100528 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100529 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100530 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100531 WQ_NO_WCFLUSH_WAIT;
532
533 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100534 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100535
Dave Gordon44a28b12015-08-12 15:43:41 +0100536 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson04769652016-07-20 09:21:11 +0100537 wqi->fence_id = rq->fence.seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100538
539 kunmap_atomic(base);
Dave Gordon44a28b12015-08-12 15:43:41 +0100540}
541
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100542static int guc_ring_doorbell(struct i915_guc_client *gc)
543{
544 struct guc_process_desc *desc;
545 union guc_doorbell_qw db_cmp, db_exc, db_ret;
546 union guc_doorbell_qw *db;
547 int attempt = 2, ret = -EAGAIN;
548
549 desc = gc->client_base + gc->proc_desc_offset;
550
551 /* Update the tail so it is visible to GuC */
552 desc->tail = gc->wq_tail;
553
554 /* current cookie */
555 db_cmp.db_status = GUC_DOORBELL_ENABLED;
556 db_cmp.cookie = gc->cookie;
557
558 /* cookie to be updated */
559 db_exc.db_status = GUC_DOORBELL_ENABLED;
560 db_exc.cookie = gc->cookie + 1;
561 if (db_exc.cookie == 0)
562 db_exc.cookie = 1;
563
564 /* pointer of current doorbell cacheline */
565 db = gc->client_base + gc->doorbell_offset;
566
567 while (attempt--) {
568 /* lets ring the doorbell */
569 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
570 db_cmp.value_qw, db_exc.value_qw);
571
572 /* if the exchange was successfully executed */
573 if (db_ret.value_qw == db_cmp.value_qw) {
574 /* db was successfully rung */
575 gc->cookie = db_exc.cookie;
576 ret = 0;
577 break;
578 }
579
580 /* XXX: doorbell was lost and need to acquire it again */
581 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
582 break;
583
Dave Gordon535b2f52016-08-18 18:17:23 +0100584 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
585 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100586
587 /* update the cookie to newly read cookie from GuC */
588 db_cmp.cookie = db_ret.cookie;
589 db_exc.cookie = db_ret.cookie + 1;
590 if (db_exc.cookie == 0)
591 db_exc.cookie = 1;
592 }
593
594 return ret;
595}
596
Dave Gordon44a28b12015-08-12 15:43:41 +0100597/**
598 * i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700599 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100600 *
Dave Gordon7c2c2702016-05-13 15:36:32 +0100601 * Return: 0 on success, otherwise an errno.
602 * (Note: nonzero really shouldn't happen!)
603 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100604 * The caller must have already called i915_guc_wq_reserve() above with
605 * a result of 0 (success), guaranteeing that there is space in the work
606 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100607 *
608 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100609 * submit() when _reserve() says there's no space, or calls _submit()
610 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100611 *
612 * The only error here arises if the doorbell hardware isn't functioning
613 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100614 */
Chris Wilsonddd66c52016-08-02 22:50:31 +0100615static void i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100616{
Dave Gordon0b63bb12016-06-20 15:18:07 +0100617 unsigned int engine_id = rq->engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100618 struct intel_guc *guc = &rq->i915->guc;
619 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100620 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100621
Chris Wilsondadd4812016-09-09 14:11:57 +0100622 spin_lock(&client->wq_lock);
Dave Gordon7a9347f2016-09-12 21:19:37 +0100623 guc_wq_item_append(client, rq);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100624 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100625
Alex Dai397097b2016-01-23 11:58:14 -0800626 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100627 client->retcode = b_ret;
628 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100629 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100630
Alex Dai397097b2016-01-23 11:58:14 -0800631 guc->submissions[engine_id] += 1;
Chris Wilson04769652016-07-20 09:21:11 +0100632 guc->last_seqno[engine_id] = rq->fence.seqno;
Chris Wilsondadd4812016-09-09 14:11:57 +0100633 spin_unlock(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100634}
635
636/*
637 * Everything below here is concerned with setup & teardown, and is
638 * therefore not part of the somewhat time-critical batch-submission
639 * path of i915_guc_submit() above.
640 */
641
642/**
Chris Wilson8b797af2016-08-15 10:48:51 +0100643 * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
644 * @guc: the guc
645 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100646 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100647 * This is a wrapper to create an object for use with the GuC. In order to
648 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
649 * both some backing storage and a range inside the Global GTT. We must pin
650 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
651 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100652 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100653 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100654 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100655static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100656{
Chris Wilson8b797af2016-08-15 10:48:51 +0100657 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100658 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100659 struct i915_vma *vma;
660 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100661
Chris Wilson91c8a322016-07-05 10:40:23 +0100662 obj = i915_gem_object_create(&dev_priv->drm, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100663 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100664 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100665
Chris Wilson8b797af2016-08-15 10:48:51 +0100666 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
667 if (IS_ERR(vma))
668 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100669
Chris Wilson8b797af2016-08-15 10:48:51 +0100670 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
671 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
672 if (ret) {
673 vma = ERR_PTR(ret);
674 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100675 }
676
677 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
678 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
679
Chris Wilson8b797af2016-08-15 10:48:51 +0100680 return vma;
681
682err:
683 i915_gem_object_put(obj);
684 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100685}
686
Dave Gordon0daf5562016-06-10 18:29:25 +0100687static void
688guc_client_free(struct drm_i915_private *dev_priv,
689 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100690{
Dave Gordon44a28b12015-08-12 15:43:41 +0100691 struct intel_guc *guc = &dev_priv->guc;
692
693 if (!client)
694 return;
695
Dave Gordon44a28b12015-08-12 15:43:41 +0100696 /*
697 * XXX: wait for any outstanding submissions before freeing memory.
698 * Be sure to drop any locks
699 */
700
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100701 if (client->client_base) {
702 /*
Dave Gordona6674292016-06-13 17:57:32 +0100703 * If we got as far as setting up a doorbell, make sure we
704 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100705 */
Dave Gordona6674292016-06-13 17:57:32 +0100706 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100707
708 kunmap(kmap_to_page(client->client_base));
709 }
710
Chris Wilson19880c42016-08-15 10:49:05 +0100711 i915_vma_unpin_and_release(&client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100712
713 if (client->ctx_index != GUC_INVALID_CTX_ID) {
Dave Gordon7a9347f2016-09-12 21:19:37 +0100714 guc_ctx_desc_fini(guc, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100715 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
716 }
717
718 kfree(client);
719}
720
Dave Gordon84b7f882016-08-09 15:19:20 +0100721/* Check that a doorbell register is in the expected state */
722static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
723{
724 struct drm_i915_private *dev_priv = guc_to_i915(guc);
725 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
726 uint32_t value = I915_READ(drbreg);
727 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
728 bool expected = test_bit(db_id, guc->doorbell_bitmap);
729
730 if (enabled == expected)
731 return true;
732
733 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
734 db_id, drbreg.reg, value,
735 expected ? "active" : "inactive");
736
737 return false;
738}
739
Dave Gordon4d757872016-06-13 17:57:34 +0100740/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100741 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100742 * in turn, to ensure that all doorbell h/w is (re)initialised.
743 */
744static void guc_init_doorbell_hw(struct intel_guc *guc)
745{
Dave Gordon4d757872016-06-13 17:57:34 +0100746 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100747 uint16_t db_id;
748 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100749
Dave Gordon84b7f882016-08-09 15:19:20 +0100750 /* Save client's original doorbell selection */
Dave Gordon4d757872016-06-13 17:57:34 +0100751 db_id = client->doorbell_id;
752
753 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100754 /* Skip if doorbell is OK */
755 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100756 continue;
757
Dave Gordon4d757872016-06-13 17:57:34 +0100758 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100759 if (err)
760 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
761 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100762 }
763
764 /* Restore to original value */
765 err = guc_update_doorbell_id(guc, client, db_id);
766 if (err)
Dave Gordon535b2f52016-08-18 18:17:23 +0100767 DRM_WARN("Failed to restore doorbell to %d, err %d\n",
768 db_id, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100769
Dave Gordon84b7f882016-08-09 15:19:20 +0100770 /* Read back & verify all doorbell registers */
771 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
772 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100773}
774
Dave Gordon44a28b12015-08-12 15:43:41 +0100775/**
776 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100777 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100778 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100779 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
780 * The kernel client to replace ExecList submission is created with
781 * NORMAL priority. Priority of a client for scheduler can be HIGH,
782 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700783 * @ctx: the context that owns the client (we use the default render
784 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100785 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100786 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100787 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100788static struct i915_guc_client *
789guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100790 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100791 uint32_t priority,
792 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100793{
794 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100795 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100796 struct i915_vma *vma;
Dave Gordona6674292016-06-13 17:57:32 +0100797 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100798
799 client = kzalloc(sizeof(*client), GFP_KERNEL);
800 if (!client)
801 return NULL;
802
Alex Daid1675192015-08-12 15:43:43 +0100803 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100804 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100805 client->engines = engines;
806 client->priority = priority;
807 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100808
809 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
810 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
811 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
812 client->ctx_index = GUC_INVALID_CTX_ID;
813 goto err;
814 }
815
816 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100817 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
818 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100819 goto err;
820
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100821 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100822 client->vma = vma;
823 client->client_base = kmap(i915_vma_first_page(vma));
Chris Wilsondadd4812016-09-09 14:11:57 +0100824
825 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100826 client->wq_offset = GUC_DB_SIZE;
827 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100828
Dave Gordonf10d69a2016-06-13 17:57:33 +0100829 db_id = select_doorbell_register(guc, client->priority);
830 if (db_id == GUC_INVALID_DOORBELL_ID)
831 /* XXX: evict a doorbell instead? */
832 goto err;
833
Dave Gordon44a28b12015-08-12 15:43:41 +0100834 client->doorbell_offset = select_doorbell_cacheline(guc);
835
836 /*
837 * Since the doorbell only requires a single cacheline, we can save
838 * space by putting the application process descriptor in the same
839 * page. Use the half of the page that doesn't include the doorbell.
840 */
841 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
842 client->proc_desc_offset = 0;
843 else
844 client->proc_desc_offset = (GUC_DB_SIZE / 2);
845
Dave Gordon7a9347f2016-09-12 21:19:37 +0100846 guc_proc_desc_init(guc, client);
847 guc_ctx_desc_init(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100848 if (guc_init_doorbell(guc, client, db_id))
Dave Gordon44a28b12015-08-12 15:43:41 +0100849 goto err;
850
Dave Gordone02757d2016-08-09 15:19:21 +0100851 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
852 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100853 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
854 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100855
856 return client;
857
858err:
Dave Gordon0daf5562016-06-10 18:29:25 +0100859 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100860 return NULL;
861}
862
Akash Goelf8240832016-10-12 21:54:34 +0530863/*
864 * Sub buffer switch callback. Called whenever relay has to switch to a new
865 * sub buffer, relay stays on the same sub buffer if 0 is returned.
866 */
867static int subbuf_start_callback(struct rchan_buf *buf,
868 void *subbuf,
869 void *prev_subbuf,
870 size_t prev_padding)
871{
872 /* Use no-overwrite mode by default, where relay will stop accepting
873 * new data if there are no empty sub buffers left.
874 * There is no strict synchronization enforced by relay between Consumer
875 * and Producer. In overwrite mode, there is a possibility of getting
876 * inconsistent/garbled data, the producer could be writing on to the
877 * same sub buffer from which Consumer is reading. This can't be avoided
878 * unless Consumer is fast enough and can always run in tandem with
879 * Producer.
880 */
881 if (relay_buf_full(buf))
882 return 0;
883
884 return 1;
885}
886
887/*
888 * file_create() callback. Creates relay file in debugfs.
889 */
890static struct dentry *create_buf_file_callback(const char *filename,
891 struct dentry *parent,
892 umode_t mode,
893 struct rchan_buf *buf,
894 int *is_global)
895{
896 struct dentry *buf_file;
897
898 if (!parent)
899 return NULL;
900
901 /* This to enable the use of a single buffer for the relay channel and
902 * correspondingly have a single file exposed to User, through which
903 * it can collect the logs in order without any post-processing.
904 */
905 *is_global = 1;
906
907 /* Not using the channel filename passed as an argument, since for each
908 * channel relay appends the corresponding CPU number to the filename
909 * passed in relay_open(). This should be fine as relay just needs a
910 * dentry of the file associated with the channel buffer and that file's
911 * name need not be same as the filename passed as an argument.
912 */
913 buf_file = debugfs_create_file("guc_log", mode,
914 parent, buf, &relay_file_operations);
915 return buf_file;
916}
917
918/*
919 * file_remove() default callback. Removes relay file in debugfs.
920 */
921static int remove_buf_file_callback(struct dentry *dentry)
922{
923 debugfs_remove(dentry);
924 return 0;
925}
926
927/* relay channel callbacks */
928static struct rchan_callbacks relay_callbacks = {
929 .subbuf_start = subbuf_start_callback,
930 .create_buf_file = create_buf_file_callback,
931 .remove_buf_file = remove_buf_file_callback,
932};
933
934static void guc_log_remove_relay_file(struct intel_guc *guc)
935{
936 relay_close(guc->log.relay_chan);
937}
938
939static int guc_log_create_relay_file(struct intel_guc *guc)
940{
941 struct drm_i915_private *dev_priv = guc_to_i915(guc);
942 struct rchan *guc_log_relay_chan;
943 struct dentry *log_dir;
944 size_t n_subbufs, subbuf_size;
945
946 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
947 log_dir = dev_priv->drm.primary->debugfs_root;
948
949 /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
950 * not mounted and so can't create the relay file.
951 * The relay API seems to fit well with debugfs only, for availing relay
952 * there are 3 requirements which can be met for debugfs file only in a
953 * straightforward/clean manner :-
954 * i) Need the associated dentry pointer of the file, while opening the
955 * relay channel.
956 * ii) Should be able to use 'relay_file_operations' fops for the file.
957 * iii) Set the 'i_private' field of file's inode to the pointer of
958 * relay channel buffer.
959 */
960 if (!log_dir) {
961 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
962 return -ENODEV;
963 }
964
965 /* Keep the size of sub buffers same as shared log buffer */
966 subbuf_size = guc->log.vma->obj->base.size;
967
968 /* Store up to 8 snapshots, which is large enough to buffer sufficient
969 * boot time logs and provides enough leeway to User, in terms of
970 * latency, for consuming the logs from relay. Also doesn't take
971 * up too much memory.
972 */
973 n_subbufs = 8;
974
975 guc_log_relay_chan = relay_open("guc_log", log_dir, subbuf_size,
976 n_subbufs, &relay_callbacks, dev_priv);
977 if (!guc_log_relay_chan) {
978 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
979 return -ENOMEM;
980 }
981
982 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
983 /* FIXME: Cover the update under a lock ? */
984 guc->log.relay_chan = guc_log_relay_chan;
985 return 0;
986}
987
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530988static void guc_move_to_next_buf(struct intel_guc *guc)
989{
Akash Goelf8240832016-10-12 21:54:34 +0530990 /* Make sure the updates made in the sub buffer are visible when
991 * Consumer sees the following update to offset inside the sub buffer.
992 */
993 smp_wmb();
994
995 /* All data has been written, so now move the offset of sub buffer. */
996 relay_reserve(guc->log.relay_chan, guc->log.vma->obj->base.size);
997
998 /* Switch to the next sub buffer */
999 relay_flush(guc->log.relay_chan);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301000}
1001
1002static void *guc_get_write_buffer(struct intel_guc *guc)
1003{
Akash Goelf8240832016-10-12 21:54:34 +05301004 /* FIXME: Cover the check under a lock ? */
1005 if (!guc->log.relay_chan)
1006 return NULL;
1007
1008 /* Just get the base address of a new sub buffer and copy data into it
1009 * ourselves. NULL will be returned in no-overwrite mode, if all sub
1010 * buffers are full. Could have used the relay_write() to indirectly
1011 * copy the data, but that would have been bit convoluted, as we need to
1012 * write to only certain locations inside a sub buffer which cannot be
1013 * done without using relay_reserve() along with relay_write(). So its
1014 * better to use relay_reserve() alone.
1015 */
1016 return relay_reserve(guc->log.relay_chan, 0);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301017}
1018
Akash Goel5aa1ee42016-10-12 21:54:36 +05301019static bool
1020guc_check_log_buf_overflow(struct intel_guc *guc,
1021 enum guc_log_buffer_type type, unsigned int full_cnt)
1022{
1023 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
1024 bool overflow = false;
1025
1026 if (full_cnt != prev_full_cnt) {
1027 overflow = true;
1028
1029 guc->log.prev_overflow_count[type] = full_cnt;
1030 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
1031
1032 if (full_cnt < prev_full_cnt) {
1033 /* buffer_full_cnt is a 4 bit counter */
1034 guc->log.total_overflow_count[type] += 16;
1035 }
1036 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
1037 }
1038
1039 return overflow;
1040}
1041
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301042static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
1043{
1044 switch (type) {
1045 case GUC_ISR_LOG_BUFFER:
1046 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
1047 case GUC_DPC_LOG_BUFFER:
1048 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
1049 case GUC_CRASH_DUMP_LOG_BUFFER:
1050 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
1051 default:
1052 MISSING_CASE(type);
1053 }
1054
1055 return 0;
1056}
1057
1058static void guc_read_update_log_buffer(struct intel_guc *guc)
1059{
Akash Goel6941f3c2016-10-12 21:54:37 +05301060 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301061 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
1062 struct guc_log_buffer_state log_buf_state_local;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301063 enum guc_log_buffer_type type;
1064 void *src_data, *dst_data;
Akash Goel6941f3c2016-10-12 21:54:37 +05301065 bool new_overflow;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301066
1067 if (WARN_ON(!guc->log.buf_addr))
1068 return;
1069
1070 /* Get the pointer to shared GuC log buffer */
1071 log_buf_state = src_data = guc->log.buf_addr;
1072
1073 /* Get the pointer to local buffer to store the logs */
1074 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
1075
1076 /* Actual logs are present from the 2nd page */
1077 src_data += PAGE_SIZE;
1078 dst_data += PAGE_SIZE;
1079
1080 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
1081 /* Make a copy of the state structure, inside GuC log buffer
1082 * (which is uncached mapped), on the stack to avoid reading
1083 * from it multiple times.
1084 */
1085 memcpy(&log_buf_state_local, log_buf_state,
1086 sizeof(struct guc_log_buffer_state));
1087 buffer_size = guc_get_log_buffer_size(type);
Akash Goel6941f3c2016-10-12 21:54:37 +05301088 read_offset = log_buf_state_local.read_ptr;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301089 write_offset = log_buf_state_local.sampled_write_ptr;
Akash Goel5aa1ee42016-10-12 21:54:36 +05301090 full_cnt = log_buf_state_local.buffer_full_cnt;
1091
1092 /* Bookkeeping stuff */
1093 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
Akash Goel6941f3c2016-10-12 21:54:37 +05301094 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301095
1096 /* Update the state of shared log buffer */
1097 log_buf_state->read_ptr = write_offset;
1098 log_buf_state->flush_to_file = 0;
1099 log_buf_state++;
1100
1101 if (unlikely(!log_buf_snapshot_state))
1102 continue;
1103
1104 /* First copy the state structure in snapshot buffer */
1105 memcpy(log_buf_snapshot_state, &log_buf_state_local,
1106 sizeof(struct guc_log_buffer_state));
1107
1108 /* The write pointer could have been updated by GuC firmware,
1109 * after sending the flush interrupt to Host, for consistency
1110 * set write pointer value to same value of sampled_write_ptr
1111 * in the snapshot buffer.
1112 */
1113 log_buf_snapshot_state->write_ptr = write_offset;
1114 log_buf_snapshot_state++;
1115
1116 /* Now copy the actual logs. */
Akash Goel6941f3c2016-10-12 21:54:37 +05301117 if (unlikely(new_overflow)) {
1118 /* copy the whole buffer in case of overflow */
1119 read_offset = 0;
1120 write_offset = buffer_size;
1121 } else if (unlikely((read_offset > buffer_size) ||
1122 (write_offset > buffer_size))) {
1123 DRM_ERROR("invalid log buffer state\n");
1124 /* copy whole buffer as offsets are unreliable */
1125 read_offset = 0;
1126 write_offset = buffer_size;
1127 }
1128
1129 /* Just copy the newly written data */
1130 if (read_offset > write_offset) {
1131 memcpy(dst_data, src_data, write_offset);
1132 bytes_to_copy = buffer_size - read_offset;
1133 } else {
1134 bytes_to_copy = write_offset - read_offset;
1135 }
1136 memcpy(dst_data + read_offset,
1137 src_data + read_offset, bytes_to_copy);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301138
1139 src_data += buffer_size;
1140 dst_data += buffer_size;
1141
1142 /* FIXME: invalidate/flush for log buffer needed */
1143 }
1144
1145 if (log_buf_snapshot_state)
1146 guc_move_to_next_buf(guc);
Akash Goelf8240832016-10-12 21:54:34 +05301147 else {
1148 /* Used rate limited to avoid deluge of messages, logs might be
1149 * getting consumed by User at a slow rate.
1150 */
1151 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
Akash Goel5aa1ee42016-10-12 21:54:36 +05301152 guc->log.capture_miss_count++;
Akash Goelf8240832016-10-12 21:54:34 +05301153 }
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301154}
1155
1156static void guc_capture_logs_work(struct work_struct *work)
1157{
1158 struct drm_i915_private *dev_priv =
1159 container_of(work, struct drm_i915_private, guc.log.flush_work);
1160
1161 i915_guc_capture_logs(dev_priv);
1162}
1163
1164static void guc_log_cleanup(struct intel_guc *guc)
1165{
1166 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1167
1168 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1169
1170 /* First disable the flush interrupt */
1171 gen9_disable_guc_interrupts(dev_priv);
1172
1173 if (guc->log.flush_wq)
1174 destroy_workqueue(guc->log.flush_wq);
1175
1176 guc->log.flush_wq = NULL;
1177
Akash Goelf8240832016-10-12 21:54:34 +05301178 if (guc->log.relay_chan)
1179 guc_log_remove_relay_file(guc);
1180
1181 guc->log.relay_chan = NULL;
1182
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301183 if (guc->log.buf_addr)
1184 i915_gem_object_unpin_map(guc->log.vma->obj);
1185
1186 guc->log.buf_addr = NULL;
1187}
1188
1189static int guc_log_create_extras(struct intel_guc *guc)
1190{
1191 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1192 void *vaddr;
1193 int ret;
1194
1195 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1196
1197 /* Nothing to do */
1198 if (i915.guc_log_level < 0)
1199 return 0;
1200
1201 if (!guc->log.buf_addr) {
1202 /* Create a vmalloc mapping of log buffer pages */
1203 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WB);
1204 if (IS_ERR(vaddr)) {
1205 ret = PTR_ERR(vaddr);
1206 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
1207 return ret;
1208 }
1209
1210 guc->log.buf_addr = vaddr;
1211 }
1212
1213 if (!guc->log.flush_wq) {
1214 INIT_WORK(&guc->log.flush_work, guc_capture_logs_work);
1215
1216 /* Need a dedicated wq to process log buffer flush interrupts
1217 * from GuC without much delay so as to avoid any loss of logs.
1218 */
1219 guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log", WQ_HIGHPRI);
1220 if (guc->log.flush_wq == NULL) {
1221 DRM_ERROR("Couldn't allocate the wq for GuC logging\n");
1222 return -ENOMEM;
1223 }
1224 }
1225
1226 return 0;
1227}
1228
Dave Gordon7a9347f2016-09-12 21:19:37 +01001229static void guc_log_create(struct intel_guc *guc)
Alex Dai4c7e77f2015-08-12 15:43:40 +01001230{
Chris Wilson8b797af2016-08-15 10:48:51 +01001231 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001232 unsigned long offset;
1233 uint32_t size, flags;
1234
Alex Dai4c7e77f2015-08-12 15:43:40 +01001235 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
1236 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
1237
1238 /* The first page is to save log buffer state. Allocate one
1239 * extra page for others in case for overlap */
1240 size = (1 + GUC_LOG_DPC_PAGES + 1 +
1241 GUC_LOG_ISR_PAGES + 1 +
1242 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
1243
Akash Goeld6b40b42016-10-12 21:54:29 +05301244 vma = guc->log.vma;
Chris Wilson8b797af2016-08-15 10:48:51 +01001245 if (!vma) {
1246 vma = guc_allocate_vma(guc, size);
1247 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +01001248 /* logging will be off */
1249 i915.guc_log_level = -1;
1250 return;
1251 }
1252
Akash Goeld6b40b42016-10-12 21:54:29 +05301253 guc->log.vma = vma;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301254
1255 if (guc_log_create_extras(guc)) {
1256 guc_log_cleanup(guc);
1257 i915_vma_unpin_and_release(&guc->log.vma);
1258 i915.guc_log_level = -1;
1259 return;
1260 }
Alex Dai4c7e77f2015-08-12 15:43:40 +01001261 }
1262
1263 /* each allocated unit is a page */
1264 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
1265 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
1266 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
1267 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
1268
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001269 offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
Akash Goeld6b40b42016-10-12 21:54:29 +05301270 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001271}
1272
Akash Goelf8240832016-10-12 21:54:34 +05301273static int guc_log_late_setup(struct intel_guc *guc)
1274{
1275 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1276 int ret;
1277
1278 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1279
1280 if (i915.guc_log_level < 0)
1281 return -EINVAL;
1282
1283 /* If log_level was set as -1 at boot time, then setup needed to
1284 * handle log buffer flush interrupts would not have been done yet,
1285 * so do that now.
1286 */
1287 ret = guc_log_create_extras(guc);
1288 if (ret)
1289 goto err;
1290
1291 ret = guc_log_create_relay_file(guc);
1292 if (ret)
1293 goto err;
1294
1295 return 0;
1296err:
1297 guc_log_cleanup(guc);
1298 /* logging will remain off */
1299 i915.guc_log_level = -1;
1300 return ret;
1301}
1302
Dave Gordon7a9347f2016-09-12 21:19:37 +01001303static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -08001304{
1305 struct guc_policy *policy;
1306 u32 p, i;
1307
1308 policies->dpc_promote_time = 500000;
1309 policies->max_num_work_items = POLICY_MAX_NUM_WI;
1310
1311 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -08001312 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -08001313 policy = &policies->policy[p][i];
1314
1315 policy->execution_quantum = 1000000;
1316 policy->preemption_time = 500000;
1317 policy->fault_time = 250000;
1318 policy->policy_flags = 0;
1319 }
1320 }
1321
1322 policies->is_valid = 1;
1323}
1324
Dave Gordon7a9347f2016-09-12 21:19:37 +01001325static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -08001326{
1327 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +01001328 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -08001329 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -08001330 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -08001331 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001332 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301333 enum intel_engine_id id;
Alex Dai68371a92015-12-18 12:00:09 -08001334 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001335 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -08001336
1337 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -08001338 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
1339 sizeof(struct guc_mmio_reg_state) +
1340 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -08001341
Chris Wilson8b797af2016-08-15 10:48:51 +01001342 vma = guc->ads_vma;
1343 if (!vma) {
1344 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
1345 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -08001346 return;
1347
Chris Wilson8b797af2016-08-15 10:48:51 +01001348 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -08001349 }
1350
Chris Wilson8b797af2016-08-15 10:48:51 +01001351 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -08001352 ads = kmap(page);
1353
1354 /*
1355 * The GuC requires a "Golden Context" when it reinitialises
1356 * engines after a reset. Here we use the Render ring default
1357 * context, which must already exist and be pinned in the GGTT,
1358 * so its address won't change after we've told the GuC where
1359 * to find it.
1360 */
Akash Goel3b3f1652016-10-13 22:44:48 +05301361 engine = dev_priv->engine[RCS];
Chris Wilson57e88532016-08-15 10:48:57 +01001362 ads->golden_context_lrca = engine->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -08001363
Akash Goel3b3f1652016-10-13 22:44:48 +05301364 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001365 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -08001366
Alex Dai463704d2015-12-18 12:00:10 -08001367 /* GuC scheduling policies */
1368 policies = (void *)ads + sizeof(struct guc_ads);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001369 guc_policies_init(policies);
Alex Dai463704d2015-12-18 12:00:10 -08001370
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001371 ads->scheduler_policies =
1372 i915_ggtt_offset(vma) + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -08001373
Alex Dai5c148e02015-12-18 12:00:11 -08001374 /* MMIO reg state */
1375 reg_state = (void *)policies + sizeof(struct guc_policies);
1376
Akash Goel3b3f1652016-10-13 22:44:48 +05301377 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001378 reg_state->mmio_white_list[engine->guc_id].mmio_start =
1379 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -08001380
1381 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001382 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -08001383 }
1384
1385 ads->reg_state_addr = ads->scheduler_policies +
1386 sizeof(struct guc_policies);
1387
1388 ads->reg_state_buffer = ads->reg_state_addr +
1389 sizeof(struct guc_mmio_reg_state);
1390
Alex Dai68371a92015-12-18 12:00:09 -08001391 kunmap(page);
1392}
1393
Alex Daibac427f2015-08-12 15:43:39 +01001394/*
1395 * Set up the memory resources to be shared with the GuC. At this point,
1396 * we require just one object that can be mapped through the GGTT.
1397 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001398int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001399{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001400 const size_t ctxsize = sizeof(struct guc_context_desc);
1401 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1402 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001403 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001404 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +01001405
Dave Gordon29fb72c2016-06-07 09:14:50 +01001406 /* Wipe bitmap & delete client in case of reinitialisation */
1407 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001408 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001409
Alex Daibac427f2015-08-12 15:43:39 +01001410 if (!i915.enable_guc_submission)
1411 return 0; /* not enabled */
1412
Chris Wilson8b797af2016-08-15 10:48:51 +01001413 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001414 return 0; /* already allocated */
1415
Dave Gordon7a9347f2016-09-12 21:19:37 +01001416 vma = guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001417 if (IS_ERR(vma))
1418 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001419
Chris Wilson8b797af2016-08-15 10:48:51 +01001420 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001421 ida_init(&guc->ctx_ids);
Akash Goel5dd79892016-10-12 21:54:35 +05301422 mutex_init(&guc->action_lock);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001423 guc_log_create(guc);
1424 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001425
Alex Daibac427f2015-08-12 15:43:39 +01001426 return 0;
1427}
1428
Dave Gordonbeffa512016-06-10 18:29:26 +01001429int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001430{
Dave Gordon44a28b12015-08-12 15:43:41 +01001431 struct intel_guc *guc = &dev_priv->guc;
Akash Goel3b3f1652016-10-13 22:44:48 +05301432 struct drm_i915_gem_request *request;
Dave Gordon44a28b12015-08-12 15:43:41 +01001433 struct i915_guc_client *client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001434 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301435 enum intel_engine_id id;
Dave Gordon44a28b12015-08-12 15:43:41 +01001436
1437 /* client for execbuf submission */
Dave Gordon0daf5562016-06-10 18:29:25 +01001438 client = guc_client_alloc(dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +01001439 INTEL_INFO(dev_priv)->ring_mask,
Chris Wilson0ca5fa32016-05-24 14:53:40 +01001440 GUC_CTX_PRIORITY_KMD_NORMAL,
1441 dev_priv->kernel_context);
Dave Gordon44a28b12015-08-12 15:43:41 +01001442 if (!client) {
Dave Gordon535b2f52016-08-18 18:17:23 +01001443 DRM_ERROR("Failed to create normal GuC client!\n");
Dave Gordon44a28b12015-08-12 15:43:41 +01001444 return -ENOMEM;
1445 }
1446
1447 guc->execbuf_client = client;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001448 host2guc_sample_forcewake(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +01001449 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001450
Chris Wilsonddd66c52016-08-02 22:50:31 +01001451 /* Take over from manual control of ELSP (execlists) */
Akash Goel3b3f1652016-10-13 22:44:48 +05301452 for_each_engine(engine, dev_priv, id) {
Chris Wilsonddd66c52016-08-02 22:50:31 +01001453 engine->submit_request = i915_guc_submit;
1454
Chris Wilson821ed7d2016-09-09 14:11:53 +01001455 /* Replay the current set of previously submitted requests */
Chris Wilsondadd4812016-09-09 14:11:57 +01001456 list_for_each_entry(request, &engine->request_list, link) {
1457 client->wq_rsvd += sizeof(struct guc_wq_item);
Chris Wilson5590af32016-09-09 14:11:54 +01001458 if (i915_sw_fence_done(&request->submit))
1459 i915_guc_submit(request);
Chris Wilsondadd4812016-09-09 14:11:57 +01001460 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001461 }
1462
Dave Gordon44a28b12015-08-12 15:43:41 +01001463 return 0;
1464}
1465
Dave Gordonbeffa512016-06-10 18:29:26 +01001466void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001467{
Dave Gordon44a28b12015-08-12 15:43:41 +01001468 struct intel_guc *guc = &dev_priv->guc;
1469
Chris Wilsonddd66c52016-08-02 22:50:31 +01001470 if (!guc->execbuf_client)
1471 return;
1472
Chris Wilsonddd66c52016-08-02 22:50:31 +01001473 /* Revert back to manual ELSP submission */
1474 intel_execlists_enable_submission(dev_priv);
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +01001475
1476 guc_client_free(dev_priv, guc->execbuf_client);
1477 guc->execbuf_client = NULL;
Dave Gordon44a28b12015-08-12 15:43:41 +01001478}
1479
Dave Gordonbeffa512016-06-10 18:29:26 +01001480void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001481{
Alex Daibac427f2015-08-12 15:43:39 +01001482 struct intel_guc *guc = &dev_priv->guc;
1483
Chris Wilson19880c42016-08-15 10:49:05 +01001484 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301485 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001486
Chris Wilson8b797af2016-08-15 10:48:51 +01001487 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001488 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001489 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001490}
Alex Daia1c41992015-09-30 09:46:37 -07001491
1492/**
1493 * intel_guc_suspend() - notify GuC entering suspend state
1494 * @dev: drm device
1495 */
1496int intel_guc_suspend(struct drm_device *dev)
1497{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001498 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001499 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001500 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001501 u32 data[3];
1502
Dave Gordonfce91f22016-05-20 11:42:42 +01001503 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001504 return 0;
1505
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301506 gen9_disable_guc_interrupts(dev_priv);
1507
Dave Gordoned54c1a2016-01-19 19:02:54 +00001508 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001509
1510 data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
1511 /* any value greater than GUC_POWER_D0 */
1512 data[1] = GUC_POWER_D1;
1513 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001514 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001515
1516 return host2guc_action(guc, data, ARRAY_SIZE(data));
1517}
1518
1519
1520/**
1521 * intel_guc_resume() - notify GuC resuming from suspend state
1522 * @dev: drm device
1523 */
1524int intel_guc_resume(struct drm_device *dev)
1525{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001526 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001527 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001528 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001529 u32 data[3];
1530
Dave Gordonfce91f22016-05-20 11:42:42 +01001531 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001532 return 0;
1533
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301534 if (i915.guc_log_level >= 0)
1535 gen9_enable_guc_interrupts(dev_priv);
1536
Dave Gordoned54c1a2016-01-19 19:02:54 +00001537 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001538
1539 data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
1540 data[1] = GUC_POWER_D0;
1541 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001542 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001543
1544 return host2guc_action(guc, data, ARRAY_SIZE(data));
1545}
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301546
1547void i915_guc_capture_logs(struct drm_i915_private *dev_priv)
1548{
1549 guc_read_update_log_buffer(&dev_priv->guc);
1550
1551 /* Generally device is expected to be active only at this
1552 * time, so get/put should be really quick.
1553 */
1554 intel_runtime_pm_get(dev_priv);
1555 host2guc_logbuffer_flush_complete(&dev_priv->guc);
1556 intel_runtime_pm_put(dev_priv);
1557}
Akash Goelf8240832016-10-12 21:54:34 +05301558
1559void i915_guc_unregister(struct drm_i915_private *dev_priv)
1560{
1561 if (!i915.enable_guc_submission)
1562 return;
1563
1564 mutex_lock(&dev_priv->drm.struct_mutex);
1565 guc_log_cleanup(&dev_priv->guc);
1566 mutex_unlock(&dev_priv->drm.struct_mutex);
1567}
1568
1569void i915_guc_register(struct drm_i915_private *dev_priv)
1570{
1571 if (!i915.enable_guc_submission)
1572 return;
1573
1574 mutex_lock(&dev_priv->drm.struct_mutex);
1575 guc_log_late_setup(&dev_priv->guc);
1576 mutex_unlock(&dev_priv->drm.struct_mutex);
1577}