blob: cafff7ca71c72df658a84a04b368796401e695f5 [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
1019static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
1020{
1021 switch (type) {
1022 case GUC_ISR_LOG_BUFFER:
1023 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
1024 case GUC_DPC_LOG_BUFFER:
1025 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
1026 case GUC_CRASH_DUMP_LOG_BUFFER:
1027 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
1028 default:
1029 MISSING_CASE(type);
1030 }
1031
1032 return 0;
1033}
1034
1035static void guc_read_update_log_buffer(struct intel_guc *guc)
1036{
1037 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
1038 struct guc_log_buffer_state log_buf_state_local;
1039 unsigned int buffer_size, write_offset;
1040 enum guc_log_buffer_type type;
1041 void *src_data, *dst_data;
1042
1043 if (WARN_ON(!guc->log.buf_addr))
1044 return;
1045
1046 /* Get the pointer to shared GuC log buffer */
1047 log_buf_state = src_data = guc->log.buf_addr;
1048
1049 /* Get the pointer to local buffer to store the logs */
1050 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
1051
1052 /* Actual logs are present from the 2nd page */
1053 src_data += PAGE_SIZE;
1054 dst_data += PAGE_SIZE;
1055
1056 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
1057 /* Make a copy of the state structure, inside GuC log buffer
1058 * (which is uncached mapped), on the stack to avoid reading
1059 * from it multiple times.
1060 */
1061 memcpy(&log_buf_state_local, log_buf_state,
1062 sizeof(struct guc_log_buffer_state));
1063 buffer_size = guc_get_log_buffer_size(type);
1064 write_offset = log_buf_state_local.sampled_write_ptr;
1065
1066 /* Update the state of shared log buffer */
1067 log_buf_state->read_ptr = write_offset;
1068 log_buf_state->flush_to_file = 0;
1069 log_buf_state++;
1070
1071 if (unlikely(!log_buf_snapshot_state))
1072 continue;
1073
1074 /* First copy the state structure in snapshot buffer */
1075 memcpy(log_buf_snapshot_state, &log_buf_state_local,
1076 sizeof(struct guc_log_buffer_state));
1077
1078 /* The write pointer could have been updated by GuC firmware,
1079 * after sending the flush interrupt to Host, for consistency
1080 * set write pointer value to same value of sampled_write_ptr
1081 * in the snapshot buffer.
1082 */
1083 log_buf_snapshot_state->write_ptr = write_offset;
1084 log_buf_snapshot_state++;
1085
1086 /* Now copy the actual logs. */
1087 memcpy(dst_data, src_data, buffer_size);
1088
1089 src_data += buffer_size;
1090 dst_data += buffer_size;
1091
1092 /* FIXME: invalidate/flush for log buffer needed */
1093 }
1094
1095 if (log_buf_snapshot_state)
1096 guc_move_to_next_buf(guc);
Akash Goelf8240832016-10-12 21:54:34 +05301097 else {
1098 /* Used rate limited to avoid deluge of messages, logs might be
1099 * getting consumed by User at a slow rate.
1100 */
1101 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
1102 }
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301103}
1104
1105static void guc_capture_logs_work(struct work_struct *work)
1106{
1107 struct drm_i915_private *dev_priv =
1108 container_of(work, struct drm_i915_private, guc.log.flush_work);
1109
1110 i915_guc_capture_logs(dev_priv);
1111}
1112
1113static void guc_log_cleanup(struct intel_guc *guc)
1114{
1115 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1116
1117 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1118
1119 /* First disable the flush interrupt */
1120 gen9_disable_guc_interrupts(dev_priv);
1121
1122 if (guc->log.flush_wq)
1123 destroy_workqueue(guc->log.flush_wq);
1124
1125 guc->log.flush_wq = NULL;
1126
Akash Goelf8240832016-10-12 21:54:34 +05301127 if (guc->log.relay_chan)
1128 guc_log_remove_relay_file(guc);
1129
1130 guc->log.relay_chan = NULL;
1131
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301132 if (guc->log.buf_addr)
1133 i915_gem_object_unpin_map(guc->log.vma->obj);
1134
1135 guc->log.buf_addr = NULL;
1136}
1137
1138static int guc_log_create_extras(struct intel_guc *guc)
1139{
1140 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1141 void *vaddr;
1142 int ret;
1143
1144 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1145
1146 /* Nothing to do */
1147 if (i915.guc_log_level < 0)
1148 return 0;
1149
1150 if (!guc->log.buf_addr) {
1151 /* Create a vmalloc mapping of log buffer pages */
1152 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WB);
1153 if (IS_ERR(vaddr)) {
1154 ret = PTR_ERR(vaddr);
1155 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
1156 return ret;
1157 }
1158
1159 guc->log.buf_addr = vaddr;
1160 }
1161
1162 if (!guc->log.flush_wq) {
1163 INIT_WORK(&guc->log.flush_work, guc_capture_logs_work);
1164
1165 /* Need a dedicated wq to process log buffer flush interrupts
1166 * from GuC without much delay so as to avoid any loss of logs.
1167 */
1168 guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log", WQ_HIGHPRI);
1169 if (guc->log.flush_wq == NULL) {
1170 DRM_ERROR("Couldn't allocate the wq for GuC logging\n");
1171 return -ENOMEM;
1172 }
1173 }
1174
1175 return 0;
1176}
1177
Dave Gordon7a9347f2016-09-12 21:19:37 +01001178static void guc_log_create(struct intel_guc *guc)
Alex Dai4c7e77f2015-08-12 15:43:40 +01001179{
Chris Wilson8b797af2016-08-15 10:48:51 +01001180 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001181 unsigned long offset;
1182 uint32_t size, flags;
1183
Alex Dai4c7e77f2015-08-12 15:43:40 +01001184 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
1185 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
1186
1187 /* The first page is to save log buffer state. Allocate one
1188 * extra page for others in case for overlap */
1189 size = (1 + GUC_LOG_DPC_PAGES + 1 +
1190 GUC_LOG_ISR_PAGES + 1 +
1191 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
1192
Akash Goeld6b40b42016-10-12 21:54:29 +05301193 vma = guc->log.vma;
Chris Wilson8b797af2016-08-15 10:48:51 +01001194 if (!vma) {
1195 vma = guc_allocate_vma(guc, size);
1196 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +01001197 /* logging will be off */
1198 i915.guc_log_level = -1;
1199 return;
1200 }
1201
Akash Goeld6b40b42016-10-12 21:54:29 +05301202 guc->log.vma = vma;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301203
1204 if (guc_log_create_extras(guc)) {
1205 guc_log_cleanup(guc);
1206 i915_vma_unpin_and_release(&guc->log.vma);
1207 i915.guc_log_level = -1;
1208 return;
1209 }
Alex Dai4c7e77f2015-08-12 15:43:40 +01001210 }
1211
1212 /* each allocated unit is a page */
1213 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
1214 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
1215 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
1216 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
1217
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001218 offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
Akash Goeld6b40b42016-10-12 21:54:29 +05301219 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001220}
1221
Akash Goelf8240832016-10-12 21:54:34 +05301222static int guc_log_late_setup(struct intel_guc *guc)
1223{
1224 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1225 int ret;
1226
1227 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1228
1229 if (i915.guc_log_level < 0)
1230 return -EINVAL;
1231
1232 /* If log_level was set as -1 at boot time, then setup needed to
1233 * handle log buffer flush interrupts would not have been done yet,
1234 * so do that now.
1235 */
1236 ret = guc_log_create_extras(guc);
1237 if (ret)
1238 goto err;
1239
1240 ret = guc_log_create_relay_file(guc);
1241 if (ret)
1242 goto err;
1243
1244 return 0;
1245err:
1246 guc_log_cleanup(guc);
1247 /* logging will remain off */
1248 i915.guc_log_level = -1;
1249 return ret;
1250}
1251
Dave Gordon7a9347f2016-09-12 21:19:37 +01001252static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -08001253{
1254 struct guc_policy *policy;
1255 u32 p, i;
1256
1257 policies->dpc_promote_time = 500000;
1258 policies->max_num_work_items = POLICY_MAX_NUM_WI;
1259
1260 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -08001261 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -08001262 policy = &policies->policy[p][i];
1263
1264 policy->execution_quantum = 1000000;
1265 policy->preemption_time = 500000;
1266 policy->fault_time = 250000;
1267 policy->policy_flags = 0;
1268 }
1269 }
1270
1271 policies->is_valid = 1;
1272}
1273
Dave Gordon7a9347f2016-09-12 21:19:37 +01001274static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -08001275{
1276 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +01001277 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -08001278 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -08001279 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -08001280 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001281 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301282 enum intel_engine_id id;
Alex Dai68371a92015-12-18 12:00:09 -08001283 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001284 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -08001285
1286 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -08001287 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
1288 sizeof(struct guc_mmio_reg_state) +
1289 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -08001290
Chris Wilson8b797af2016-08-15 10:48:51 +01001291 vma = guc->ads_vma;
1292 if (!vma) {
1293 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
1294 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -08001295 return;
1296
Chris Wilson8b797af2016-08-15 10:48:51 +01001297 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -08001298 }
1299
Chris Wilson8b797af2016-08-15 10:48:51 +01001300 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -08001301 ads = kmap(page);
1302
1303 /*
1304 * The GuC requires a "Golden Context" when it reinitialises
1305 * engines after a reset. Here we use the Render ring default
1306 * context, which must already exist and be pinned in the GGTT,
1307 * so its address won't change after we've told the GuC where
1308 * to find it.
1309 */
Akash Goel3b3f1652016-10-13 22:44:48 +05301310 engine = dev_priv->engine[RCS];
Chris Wilson57e88532016-08-15 10:48:57 +01001311 ads->golden_context_lrca = engine->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -08001312
Akash Goel3b3f1652016-10-13 22:44:48 +05301313 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001314 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -08001315
Alex Dai463704d2015-12-18 12:00:10 -08001316 /* GuC scheduling policies */
1317 policies = (void *)ads + sizeof(struct guc_ads);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001318 guc_policies_init(policies);
Alex Dai463704d2015-12-18 12:00:10 -08001319
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001320 ads->scheduler_policies =
1321 i915_ggtt_offset(vma) + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -08001322
Alex Dai5c148e02015-12-18 12:00:11 -08001323 /* MMIO reg state */
1324 reg_state = (void *)policies + sizeof(struct guc_policies);
1325
Akash Goel3b3f1652016-10-13 22:44:48 +05301326 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001327 reg_state->mmio_white_list[engine->guc_id].mmio_start =
1328 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -08001329
1330 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001331 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -08001332 }
1333
1334 ads->reg_state_addr = ads->scheduler_policies +
1335 sizeof(struct guc_policies);
1336
1337 ads->reg_state_buffer = ads->reg_state_addr +
1338 sizeof(struct guc_mmio_reg_state);
1339
Alex Dai68371a92015-12-18 12:00:09 -08001340 kunmap(page);
1341}
1342
Alex Daibac427f2015-08-12 15:43:39 +01001343/*
1344 * Set up the memory resources to be shared with the GuC. At this point,
1345 * we require just one object that can be mapped through the GGTT.
1346 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001347int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001348{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001349 const size_t ctxsize = sizeof(struct guc_context_desc);
1350 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1351 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001352 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001353 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +01001354
Dave Gordon29fb72c2016-06-07 09:14:50 +01001355 /* Wipe bitmap & delete client in case of reinitialisation */
1356 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001357 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001358
Alex Daibac427f2015-08-12 15:43:39 +01001359 if (!i915.enable_guc_submission)
1360 return 0; /* not enabled */
1361
Chris Wilson8b797af2016-08-15 10:48:51 +01001362 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001363 return 0; /* already allocated */
1364
Dave Gordon7a9347f2016-09-12 21:19:37 +01001365 vma = guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001366 if (IS_ERR(vma))
1367 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001368
Chris Wilson8b797af2016-08-15 10:48:51 +01001369 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001370 ida_init(&guc->ctx_ids);
Akash Goel5dd79892016-10-12 21:54:35 +05301371 mutex_init(&guc->action_lock);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001372 guc_log_create(guc);
1373 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001374
Alex Daibac427f2015-08-12 15:43:39 +01001375 return 0;
1376}
1377
Dave Gordonbeffa512016-06-10 18:29:26 +01001378int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001379{
Dave Gordon44a28b12015-08-12 15:43:41 +01001380 struct intel_guc *guc = &dev_priv->guc;
Akash Goel3b3f1652016-10-13 22:44:48 +05301381 struct drm_i915_gem_request *request;
Dave Gordon44a28b12015-08-12 15:43:41 +01001382 struct i915_guc_client *client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001383 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301384 enum intel_engine_id id;
Dave Gordon44a28b12015-08-12 15:43:41 +01001385
1386 /* client for execbuf submission */
Dave Gordon0daf5562016-06-10 18:29:25 +01001387 client = guc_client_alloc(dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +01001388 INTEL_INFO(dev_priv)->ring_mask,
Chris Wilson0ca5fa32016-05-24 14:53:40 +01001389 GUC_CTX_PRIORITY_KMD_NORMAL,
1390 dev_priv->kernel_context);
Dave Gordon44a28b12015-08-12 15:43:41 +01001391 if (!client) {
Dave Gordon535b2f52016-08-18 18:17:23 +01001392 DRM_ERROR("Failed to create normal GuC client!\n");
Dave Gordon44a28b12015-08-12 15:43:41 +01001393 return -ENOMEM;
1394 }
1395
1396 guc->execbuf_client = client;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001397 host2guc_sample_forcewake(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +01001398 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001399
Chris Wilsonddd66c52016-08-02 22:50:31 +01001400 /* Take over from manual control of ELSP (execlists) */
Akash Goel3b3f1652016-10-13 22:44:48 +05301401 for_each_engine(engine, dev_priv, id) {
Chris Wilsonddd66c52016-08-02 22:50:31 +01001402 engine->submit_request = i915_guc_submit;
1403
Chris Wilson821ed7d2016-09-09 14:11:53 +01001404 /* Replay the current set of previously submitted requests */
Chris Wilsondadd4812016-09-09 14:11:57 +01001405 list_for_each_entry(request, &engine->request_list, link) {
1406 client->wq_rsvd += sizeof(struct guc_wq_item);
Chris Wilson5590af32016-09-09 14:11:54 +01001407 if (i915_sw_fence_done(&request->submit))
1408 i915_guc_submit(request);
Chris Wilsondadd4812016-09-09 14:11:57 +01001409 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001410 }
1411
Dave Gordon44a28b12015-08-12 15:43:41 +01001412 return 0;
1413}
1414
Dave Gordonbeffa512016-06-10 18:29:26 +01001415void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001416{
Dave Gordon44a28b12015-08-12 15:43:41 +01001417 struct intel_guc *guc = &dev_priv->guc;
1418
Chris Wilsonddd66c52016-08-02 22:50:31 +01001419 if (!guc->execbuf_client)
1420 return;
1421
Chris Wilsonddd66c52016-08-02 22:50:31 +01001422 /* Revert back to manual ELSP submission */
1423 intel_execlists_enable_submission(dev_priv);
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +01001424
1425 guc_client_free(dev_priv, guc->execbuf_client);
1426 guc->execbuf_client = NULL;
Dave Gordon44a28b12015-08-12 15:43:41 +01001427}
1428
Dave Gordonbeffa512016-06-10 18:29:26 +01001429void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001430{
Alex Daibac427f2015-08-12 15:43:39 +01001431 struct intel_guc *guc = &dev_priv->guc;
1432
Chris Wilson19880c42016-08-15 10:49:05 +01001433 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301434 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001435
Chris Wilson8b797af2016-08-15 10:48:51 +01001436 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001437 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001438 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001439}
Alex Daia1c41992015-09-30 09:46:37 -07001440
1441/**
1442 * intel_guc_suspend() - notify GuC entering suspend state
1443 * @dev: drm device
1444 */
1445int intel_guc_suspend(struct drm_device *dev)
1446{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001447 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001448 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001449 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001450 u32 data[3];
1451
Dave Gordonfce91f22016-05-20 11:42:42 +01001452 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001453 return 0;
1454
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301455 gen9_disable_guc_interrupts(dev_priv);
1456
Dave Gordoned54c1a2016-01-19 19:02:54 +00001457 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001458
1459 data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
1460 /* any value greater than GUC_POWER_D0 */
1461 data[1] = GUC_POWER_D1;
1462 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001463 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001464
1465 return host2guc_action(guc, data, ARRAY_SIZE(data));
1466}
1467
1468
1469/**
1470 * intel_guc_resume() - notify GuC resuming from suspend state
1471 * @dev: drm device
1472 */
1473int intel_guc_resume(struct drm_device *dev)
1474{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001475 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001476 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001477 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001478 u32 data[3];
1479
Dave Gordonfce91f22016-05-20 11:42:42 +01001480 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001481 return 0;
1482
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301483 if (i915.guc_log_level >= 0)
1484 gen9_enable_guc_interrupts(dev_priv);
1485
Dave Gordoned54c1a2016-01-19 19:02:54 +00001486 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001487
1488 data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
1489 data[1] = GUC_POWER_D0;
1490 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001491 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001492
1493 return host2guc_action(guc, data, ARRAY_SIZE(data));
1494}
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301495
1496void i915_guc_capture_logs(struct drm_i915_private *dev_priv)
1497{
1498 guc_read_update_log_buffer(&dev_priv->guc);
1499
1500 /* Generally device is expected to be active only at this
1501 * time, so get/put should be really quick.
1502 */
1503 intel_runtime_pm_get(dev_priv);
1504 host2guc_logbuffer_flush_complete(&dev_priv->guc);
1505 intel_runtime_pm_put(dev_priv);
1506}
Akash Goelf8240832016-10-12 21:54:34 +05301507
1508void i915_guc_unregister(struct drm_i915_private *dev_priv)
1509{
1510 if (!i915.enable_guc_submission)
1511 return;
1512
1513 mutex_lock(&dev_priv->drm.struct_mutex);
1514 guc_log_cleanup(&dev_priv->guc);
1515 mutex_unlock(&dev_priv->drm.struct_mutex);
1516}
1517
1518void i915_guc_register(struct drm_i915_private *dev_priv)
1519{
1520 if (!i915.enable_guc_submission)
1521 return;
1522
1523 mutex_lock(&dev_priv->drm.struct_mutex);
1524 guc_log_late_setup(&dev_priv->guc);
1525 mutex_unlock(&dev_priv->drm.struct_mutex);
1526}