blob: 43358e18d34cb3f5f770bec71a95644dd225ff32 [file] [log] [blame]
Alex Daibac427f2015-08-12 15:43:39 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24#include <linux/firmware.h>
25#include <linux/circ_buf.h>
26#include "i915_drv.h"
27#include "intel_guc.h"
28
29/**
Alex Daifeda33e2015-10-19 16:10:54 -070030 * DOC: GuC-based command submission
Dave Gordon44a28b12015-08-12 15:43:41 +010031 *
32 * i915_guc_client:
33 * We use the term client to avoid confusion with contexts. A i915_guc_client is
34 * equivalent to GuC object guc_context_desc. This context descriptor is
35 * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
36 * and workqueue for it. Also the process descriptor (guc_process_desc), which
37 * is mapped to client space. So the client can write Work Item then ring the
38 * doorbell.
39 *
40 * To simplify the implementation, we allocate one gem object that contains all
41 * pages for doorbell, process descriptor and workqueue.
42 *
43 * The Scratch registers:
44 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
45 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
46 * triggers an interrupt on the GuC via another register write (0xC4C8).
47 * Firmware writes a success/fail code back to the action register after
48 * processes the request. The kernel driver polls waiting for this update and
49 * then proceeds.
50 * See host2guc_action()
51 *
52 * Doorbells:
53 * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
54 * mapped into process space.
55 *
56 * Work Items:
57 * There are several types of work items that the host may place into a
58 * workqueue, each with its own requirements and limitations. Currently only
59 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
60 * represents in-order queue. The kernel driver packs ring tail pointer and an
61 * ELSP context descriptor dword into Work Item.
Dave Gordon7a9347f2016-09-12 21:19:37 +010062 * See guc_wq_item_append()
Dave Gordon44a28b12015-08-12 15:43:41 +010063 *
64 */
65
66/*
67 * Read GuC command/status register (SOFT_SCRATCH_0)
68 * Return true if it contains a response rather than a command
69 */
70static inline bool host2guc_action_response(struct drm_i915_private *dev_priv,
71 u32 *status)
72{
73 u32 val = I915_READ(SOFT_SCRATCH(0));
74 *status = val;
75 return GUC2HOST_IS_RESPONSE(val);
76}
77
78static int host2guc_action(struct intel_guc *guc, u32 *data, u32 len)
79{
80 struct drm_i915_private *dev_priv = guc_to_i915(guc);
81 u32 status;
82 int i;
83 int ret;
84
85 if (WARN_ON(len < 1 || len > 15))
86 return -EINVAL;
87
88 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Dave Gordon44a28b12015-08-12 15:43:41 +010089
90 dev_priv->guc.action_count += 1;
91 dev_priv->guc.action_cmd = data[0];
92
93 for (i = 0; i < len; i++)
94 I915_WRITE(SOFT_SCRATCH(i), data[i]);
95
96 POSTING_READ(SOFT_SCRATCH(i - 1));
97
98 I915_WRITE(HOST2GUC_INTERRUPT, HOST2GUC_TRIGGER);
99
Dave Gordonab0e4552016-07-06 15:30:11 +0100100 /*
101 * Fast commands should complete in less than 10us, so sample quickly
102 * up to that length of time, then switch to a slower sleep-wait loop.
103 * No HOST2GUC command should ever take longer than 10ms.
104 */
105 ret = wait_for_us(host2guc_action_response(dev_priv, &status), 10);
106 if (ret)
107 ret = wait_for(host2guc_action_response(dev_priv, &status), 10);
Dave Gordon44a28b12015-08-12 15:43:41 +0100108 if (status != GUC2HOST_STATUS_SUCCESS) {
109 /*
110 * Either the GuC explicitly returned an error (which
111 * we convert to -EIO here) or no response at all was
112 * received within the timeout limit (-ETIMEDOUT)
113 */
114 if (ret != -ETIMEDOUT)
115 ret = -EIO;
116
Dave Gordon535b2f52016-08-18 18:17:23 +0100117 DRM_WARN("Action 0x%X failed; ret=%d status=0x%08X response=0x%08X\n",
118 data[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
Dave Gordon44a28b12015-08-12 15:43:41 +0100119
120 dev_priv->guc.action_fail += 1;
121 dev_priv->guc.action_err = ret;
122 }
123 dev_priv->guc.action_status = status;
124
Dave Gordon44a28b12015-08-12 15:43:41 +0100125 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
126
127 return ret;
128}
129
130/*
131 * Tell the GuC to allocate or deallocate a specific doorbell
132 */
133
134static int host2guc_allocate_doorbell(struct intel_guc *guc,
135 struct i915_guc_client *client)
136{
137 u32 data[2];
138
139 data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL;
140 data[1] = client->ctx_index;
141
142 return host2guc_action(guc, data, 2);
143}
144
145static int host2guc_release_doorbell(struct intel_guc *guc,
146 struct i915_guc_client *client)
147{
148 u32 data[2];
149
150 data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL;
151 data[1] = client->ctx_index;
152
153 return host2guc_action(guc, data, 2);
154}
155
Alex Daif5d3c3e2015-08-18 14:34:47 -0700156static int host2guc_sample_forcewake(struct intel_guc *guc,
157 struct i915_guc_client *client)
158{
159 struct drm_i915_private *dev_priv = guc_to_i915(guc);
160 u32 data[2];
161
162 data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
Alex Dai93f25312015-09-25 11:46:56 -0700163 /* WaRsDisableCoarsePowerGating:skl,bxt */
Tvrtko Ursulin61251512016-06-21 15:07:14 +0100164 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Alex Dai93f25312015-09-25 11:46:56 -0700165 data[1] = 0;
166 else
167 /* bit 0 and 1 are for Render and Media domain separately */
168 data[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
Alex Daif5d3c3e2015-08-18 14:34:47 -0700169
Alex Dai93f25312015-09-25 11:46:56 -0700170 return host2guc_action(guc, data, ARRAY_SIZE(data));
Alex Daif5d3c3e2015-08-18 14:34:47 -0700171}
172
Dave Gordon44a28b12015-08-12 15:43:41 +0100173/*
174 * Initialise, update, or clear doorbell data shared with the GuC
175 *
176 * These functions modify shared data and so need access to the mapped
177 * client object which contains the page being used for the doorbell
178 */
179
Dave Gordona6674292016-06-13 17:57:32 +0100180static int guc_update_doorbell_id(struct intel_guc *guc,
181 struct i915_guc_client *client,
182 u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100183{
Chris Wilson8b797af2016-08-15 10:48:51 +0100184 struct sg_table *sg = guc->ctx_pool_vma->pages;
Dave Gordona6674292016-06-13 17:57:32 +0100185 void *doorbell_bitmap = guc->doorbell_bitmap;
Dave Gordon44a28b12015-08-12 15:43:41 +0100186 struct guc_doorbell_info *doorbell;
Dave Gordona6674292016-06-13 17:57:32 +0100187 struct guc_context_desc desc;
188 size_t len;
Dave Gordon44a28b12015-08-12 15:43:41 +0100189
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100190 doorbell = client->client_base + client->doorbell_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100191
Dave Gordona6674292016-06-13 17:57:32 +0100192 if (client->doorbell_id != GUC_INVALID_DOORBELL_ID &&
193 test_bit(client->doorbell_id, doorbell_bitmap)) {
194 /* Deactivate the old doorbell */
195 doorbell->db_status = GUC_DOORBELL_DISABLED;
196 (void)host2guc_release_doorbell(guc, client);
197 __clear_bit(client->doorbell_id, doorbell_bitmap);
198 }
199
200 /* Update the GuC's idea of the doorbell ID */
201 len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
202 sizeof(desc) * client->ctx_index);
203 if (len != sizeof(desc))
204 return -EFAULT;
205 desc.db_id = new_id;
206 len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
207 sizeof(desc) * client->ctx_index);
208 if (len != sizeof(desc))
209 return -EFAULT;
210
211 client->doorbell_id = new_id;
212 if (new_id == GUC_INVALID_DOORBELL_ID)
213 return 0;
214
215 /* Activate the new doorbell */
216 __set_bit(new_id, doorbell_bitmap);
Dave Gordon44a28b12015-08-12 15:43:41 +0100217 doorbell->cookie = 0;
Dave Gordona6674292016-06-13 17:57:32 +0100218 doorbell->db_status = GUC_DOORBELL_ENABLED;
219 return host2guc_allocate_doorbell(guc, client);
220}
221
222static int guc_init_doorbell(struct intel_guc *guc,
223 struct i915_guc_client *client,
224 uint16_t db_id)
225{
226 return guc_update_doorbell_id(guc, client, db_id);
Dave Gordon44a28b12015-08-12 15:43:41 +0100227}
228
Dave Gordon44a28b12015-08-12 15:43:41 +0100229static void guc_disable_doorbell(struct intel_guc *guc,
230 struct i915_guc_client *client)
231{
Dave Gordona6674292016-06-13 17:57:32 +0100232 (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
Dave Gordon44a28b12015-08-12 15:43:41 +0100233
Dave Gordon44a28b12015-08-12 15:43:41 +0100234 /* XXX: wait for any interrupts */
235 /* XXX: wait for workqueue to drain */
236}
237
Dave Gordonf10d69a2016-06-13 17:57:33 +0100238static uint16_t
239select_doorbell_register(struct intel_guc *guc, uint32_t priority)
240{
241 /*
242 * The bitmap tracks which doorbell registers are currently in use.
243 * It is split into two halves; the first half is used for normal
244 * priority contexts, the second half for high-priority ones.
245 * Note that logically higher priorities are numerically less than
246 * normal ones, so the test below means "is it high-priority?"
247 */
248 const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
249 const uint16_t half = GUC_MAX_DOORBELLS / 2;
250 const uint16_t start = hi_pri ? half : 0;
251 const uint16_t end = start + half;
252 uint16_t id;
253
254 id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
255 if (id == end)
256 id = GUC_INVALID_DOORBELL_ID;
257
258 DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
259 hi_pri ? "high" : "normal", id);
260
261 return id;
262}
263
Dave Gordon44a28b12015-08-12 15:43:41 +0100264/*
265 * Select, assign and relase doorbell cachelines
266 *
267 * These functions track which doorbell cachelines are in use.
268 * The data they manipulate is protected by the host2guc lock.
269 */
270
271static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
272{
273 const uint32_t cacheline_size = cache_line_size();
274 uint32_t offset;
275
Dave Gordon44a28b12015-08-12 15:43:41 +0100276 /* Doorbell uses a single cache line within a page */
277 offset = offset_in_page(guc->db_cacheline);
278
279 /* Moving to next cache line to reduce contention */
280 guc->db_cacheline += cacheline_size;
281
Dave Gordon44a28b12015-08-12 15:43:41 +0100282 DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
283 offset, guc->db_cacheline, cacheline_size);
284
285 return offset;
286}
287
Dave Gordon44a28b12015-08-12 15:43:41 +0100288/*
289 * Initialise the process descriptor shared with the GuC firmware.
290 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100291static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100292 struct i915_guc_client *client)
293{
294 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100295
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100296 desc = client->client_base + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100297
298 memset(desc, 0, sizeof(*desc));
299
300 /*
301 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
302 * space for ring3 clients (set them as in mmap_ioctl) or kernel
303 * space for kernel clients (map on demand instead? May make debug
304 * easier to have it mapped).
305 */
306 desc->wq_base_addr = 0;
307 desc->db_base_addr = 0;
308
309 desc->context_id = client->ctx_index;
310 desc->wq_size_bytes = client->wq_size;
311 desc->wq_status = WQ_STATUS_ACTIVE;
312 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100313}
314
315/*
316 * Initialise/clear the context descriptor shared with the GuC firmware.
317 *
318 * This descriptor tells the GuC where (in GGTT space) to find the important
319 * data structures relating to this client (doorbell, process descriptor,
320 * write queue, etc).
321 */
322
Dave Gordon7a9347f2016-09-12 21:19:37 +0100323static void guc_ctx_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100324 struct i915_guc_client *client)
325{
Alex Dai397097b2016-01-23 11:58:14 -0800326 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000327 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100328 struct i915_gem_context *ctx = client->owner;
Dave Gordon44a28b12015-08-12 15:43:41 +0100329 struct guc_context_desc desc;
330 struct sg_table *sg;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100331 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100332 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100333
334 memset(&desc, 0, sizeof(desc));
335
336 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
337 desc.context_id = client->ctx_index;
338 desc.priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100339 desc.db_id = client->doorbell_id;
340
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100341 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100342 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100343 uint32_t guc_engine_id = engine->guc_id;
344 struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100345
346 /* TODO: We have a design issue to be solved here. Only when we
347 * receive the first batch, we know which engine is used by the
348 * user. But here GuC expects the lrc and ring to be pinned. It
349 * is not an issue for default context, which is the only one
350 * for now who owns a GuC client. But for future owner of GuC
351 * client, need to make sure lrc is pinned prior to enter here.
352 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100353 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100354 break; /* XXX: continue? */
355
Chris Wilson9021ad02016-05-24 14:53:37 +0100356 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100357
358 /* The state page is after PPHWSP */
Chris Wilson57e88532016-08-15 10:48:57 +0100359 lrc->ring_lcra =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100360 i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100361 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100362 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100363
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100364 lrc->ring_begin = i915_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100365 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
366 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100367 lrc->ring_current_tail_pointer_value = 0;
368
Dave Gordonc18468c2016-08-09 15:19:22 +0100369 desc.engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100370 }
371
Dave Gordone02757d2016-08-09 15:19:21 +0100372 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
373 client->engines, desc.engines_used);
Alex Daid1675192015-08-12 15:43:43 +0100374 WARN_ON(desc.engines_used == 0);
375
Dave Gordon44a28b12015-08-12 15:43:41 +0100376 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100377 * The doorbell, process descriptor, and workqueue are all parts
378 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100379 */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100380 gfx_addr = i915_ggtt_offset(client->vma);
Chris Wilson8b797af2016-08-15 10:48:51 +0100381 desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100382 client->doorbell_offset;
383 desc.db_trigger_cpu = (uintptr_t)client->client_base +
384 client->doorbell_offset;
385 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
386 desc.process_desc = gfx_addr + client->proc_desc_offset;
387 desc.wq_addr = gfx_addr + client->wq_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100388 desc.wq_size = client->wq_size;
389
390 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100391 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100392 * IsKMDCreatedContext client
393 */
394 desc.desc_private = (uintptr_t)client;
395
396 /* Pool context is pinned already */
Chris Wilson8b797af2016-08-15 10:48:51 +0100397 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100398 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
399 sizeof(desc) * client->ctx_index);
400}
401
Dave Gordon7a9347f2016-09-12 21:19:37 +0100402static void guc_ctx_desc_fini(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100403 struct i915_guc_client *client)
404{
405 struct guc_context_desc desc;
406 struct sg_table *sg;
407
408 memset(&desc, 0, sizeof(desc));
409
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 Gordon7c2c2702016-05-13 15:36:32 +0100415/**
Dave Gordon7a9347f2016-09-12 21:19:37 +0100416 * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
Dave Gordon7c2c2702016-05-13 15:36:32 +0100417 * @request: request associated with the commands
418 *
419 * Return: 0 if space is available
420 * -EAGAIN if space is not currently available
421 *
422 * This function must be called (and must return 0) before a request
423 * is submitted to the GuC via i915_guc_submit() below. Once a result
Dave Gordon7a9347f2016-09-12 21:19:37 +0100424 * of 0 has been returned, it must be balanced by a corresponding
425 * call to submit().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100426 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100427 * Reservation allows the caller to determine in advance that space
Dave Gordon7c2c2702016-05-13 15:36:32 +0100428 * will be available for the next submission before committing resources
429 * to it, and helps avoid late failures with complicated recovery paths.
430 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100431int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100432{
Dave Gordon551aaec2016-05-13 15:36:33 +0100433 const size_t wqi_size = sizeof(struct guc_wq_item);
Dave Gordon7c2c2702016-05-13 15:36:32 +0100434 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
Chris Wilsondadd4812016-09-09 14:11:57 +0100435 struct guc_process_desc *desc = gc->client_base + gc->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100436 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100437 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100438
Chris Wilsondadd4812016-09-09 14:11:57 +0100439 spin_lock(&gc->wq_lock);
Dave Gordon551aaec2016-05-13 15:36:33 +0100440 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
Chris Wilsondadd4812016-09-09 14:11:57 +0100441 freespace -= gc->wq_rsvd;
442 if (likely(freespace >= wqi_size)) {
443 gc->wq_rsvd += wqi_size;
444 ret = 0;
445 } else {
446 gc->no_wq_space++;
447 ret = -EAGAIN;
448 }
449 spin_unlock(&gc->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800450
Chris Wilsondadd4812016-09-09 14:11:57 +0100451 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100452}
453
Dave Gordon7a9347f2016-09-12 21:19:37 +0100454/* Construct a Work Item and append it to the GuC's Work Queue */
455static void guc_wq_item_append(struct i915_guc_client *gc,
456 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100457{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100458 /* wqi_len is in DWords, and does not include the one-word header */
459 const size_t wqi_size = sizeof(struct guc_wq_item);
460 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100461 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100462 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100463 struct guc_wq_item *wqi;
464 void *base;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100465 u32 freespace, tail, wq_off, wq_page;
Dave Gordon44a28b12015-08-12 15:43:41 +0100466
Alex Daia5916e82016-04-19 16:08:35 +0100467 desc = gc->client_base + gc->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800468
Dave Gordon7a9347f2016-09-12 21:19:37 +0100469 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100470 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
471 GEM_BUG_ON(freespace < wqi_size);
472
473 /* The GuC firmware wants the tail index in QWords, not bytes */
474 tail = rq->tail;
475 GEM_BUG_ON(tail & 7);
476 tail >>= 3;
477 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100478
479 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
480 * should not have the case where structure wqi is across page, neither
481 * wrapped to the beginning. This simplifies the implementation below.
482 *
483 * XXX: if not the case, we need save data to a temp wqi and copy it to
484 * workqueue buffer dw by dw.
485 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100486 BUILD_BUG_ON(wqi_size != 16);
Chris Wilsondadd4812016-09-09 14:11:57 +0100487 GEM_BUG_ON(gc->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100488
Dave Gordon0a31afb2016-05-13 15:36:34 +0100489 /* postincrement WQ tail for next time */
490 wq_off = gc->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100491 GEM_BUG_ON(wq_off & (wqi_size - 1));
Dave Gordon0a31afb2016-05-13 15:36:34 +0100492 gc->wq_tail += wqi_size;
493 gc->wq_tail &= gc->wq_size - 1;
Chris Wilsondadd4812016-09-09 14:11:57 +0100494 gc->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100495
496 /* WQ starts from the page after doorbell / process_desc */
497 wq_page = (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT;
Dave Gordon44a28b12015-08-12 15:43:41 +0100498 wq_off &= PAGE_SIZE - 1;
Chris Wilson8b797af2016-08-15 10:48:51 +0100499 base = kmap_atomic(i915_gem_object_get_page(gc->vma->obj, wq_page));
Dave Gordon44a28b12015-08-12 15:43:41 +0100500 wqi = (struct guc_wq_item *)((char *)base + wq_off);
501
Dave Gordon0a31afb2016-05-13 15:36:34 +0100502 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100503 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100504 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100505 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100506 WQ_NO_WCFLUSH_WAIT;
507
508 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100509 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100510
Dave Gordon44a28b12015-08-12 15:43:41 +0100511 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson04769652016-07-20 09:21:11 +0100512 wqi->fence_id = rq->fence.seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100513
514 kunmap_atomic(base);
Dave Gordon44a28b12015-08-12 15:43:41 +0100515}
516
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100517static int guc_ring_doorbell(struct i915_guc_client *gc)
518{
519 struct guc_process_desc *desc;
520 union guc_doorbell_qw db_cmp, db_exc, db_ret;
521 union guc_doorbell_qw *db;
522 int attempt = 2, ret = -EAGAIN;
523
524 desc = gc->client_base + gc->proc_desc_offset;
525
526 /* Update the tail so it is visible to GuC */
527 desc->tail = gc->wq_tail;
528
529 /* current cookie */
530 db_cmp.db_status = GUC_DOORBELL_ENABLED;
531 db_cmp.cookie = gc->cookie;
532
533 /* cookie to be updated */
534 db_exc.db_status = GUC_DOORBELL_ENABLED;
535 db_exc.cookie = gc->cookie + 1;
536 if (db_exc.cookie == 0)
537 db_exc.cookie = 1;
538
539 /* pointer of current doorbell cacheline */
540 db = gc->client_base + gc->doorbell_offset;
541
542 while (attempt--) {
543 /* lets ring the doorbell */
544 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
545 db_cmp.value_qw, db_exc.value_qw);
546
547 /* if the exchange was successfully executed */
548 if (db_ret.value_qw == db_cmp.value_qw) {
549 /* db was successfully rung */
550 gc->cookie = db_exc.cookie;
551 ret = 0;
552 break;
553 }
554
555 /* XXX: doorbell was lost and need to acquire it again */
556 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
557 break;
558
Dave Gordon535b2f52016-08-18 18:17:23 +0100559 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
560 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100561
562 /* update the cookie to newly read cookie from GuC */
563 db_cmp.cookie = db_ret.cookie;
564 db_exc.cookie = db_ret.cookie + 1;
565 if (db_exc.cookie == 0)
566 db_exc.cookie = 1;
567 }
568
569 return ret;
570}
571
Dave Gordon44a28b12015-08-12 15:43:41 +0100572/**
573 * i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700574 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100575 *
Dave Gordon7c2c2702016-05-13 15:36:32 +0100576 * Return: 0 on success, otherwise an errno.
577 * (Note: nonzero really shouldn't happen!)
578 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100579 * The caller must have already called i915_guc_wq_reserve() above with
580 * a result of 0 (success), guaranteeing that there is space in the work
581 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100582 *
583 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100584 * submit() when _reserve() says there's no space, or calls _submit()
585 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100586 *
587 * The only error here arises if the doorbell hardware isn't functioning
588 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100589 */
Chris Wilsonddd66c52016-08-02 22:50:31 +0100590static void i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100591{
Dave Gordon0b63bb12016-06-20 15:18:07 +0100592 unsigned int engine_id = rq->engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100593 struct intel_guc *guc = &rq->i915->guc;
594 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100595 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100596
Chris Wilsondadd4812016-09-09 14:11:57 +0100597 spin_lock(&client->wq_lock);
Dave Gordon7a9347f2016-09-12 21:19:37 +0100598 guc_wq_item_append(client, rq);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100599 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100600
Alex Dai397097b2016-01-23 11:58:14 -0800601 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100602 client->retcode = b_ret;
603 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100604 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100605
Alex Dai397097b2016-01-23 11:58:14 -0800606 guc->submissions[engine_id] += 1;
Chris Wilson04769652016-07-20 09:21:11 +0100607 guc->last_seqno[engine_id] = rq->fence.seqno;
Chris Wilsondadd4812016-09-09 14:11:57 +0100608 spin_unlock(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100609}
610
611/*
612 * Everything below here is concerned with setup & teardown, and is
613 * therefore not part of the somewhat time-critical batch-submission
614 * path of i915_guc_submit() above.
615 */
616
617/**
Chris Wilson8b797af2016-08-15 10:48:51 +0100618 * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
619 * @guc: the guc
620 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100621 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100622 * This is a wrapper to create an object for use with the GuC. In order to
623 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
624 * both some backing storage and a range inside the Global GTT. We must pin
625 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
626 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100627 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100628 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100629 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100630static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100631{
Chris Wilson8b797af2016-08-15 10:48:51 +0100632 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100633 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100634 struct i915_vma *vma;
635 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100636
Chris Wilson91c8a322016-07-05 10:40:23 +0100637 obj = i915_gem_object_create(&dev_priv->drm, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100638 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100639 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100640
Chris Wilson8b797af2016-08-15 10:48:51 +0100641 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
642 if (IS_ERR(vma))
643 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100644
Chris Wilson8b797af2016-08-15 10:48:51 +0100645 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
646 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
647 if (ret) {
648 vma = ERR_PTR(ret);
649 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100650 }
651
652 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
653 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
654
Chris Wilson8b797af2016-08-15 10:48:51 +0100655 return vma;
656
657err:
658 i915_gem_object_put(obj);
659 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100660}
661
Dave Gordon0daf5562016-06-10 18:29:25 +0100662static void
663guc_client_free(struct drm_i915_private *dev_priv,
664 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100665{
Dave Gordon44a28b12015-08-12 15:43:41 +0100666 struct intel_guc *guc = &dev_priv->guc;
667
668 if (!client)
669 return;
670
Dave Gordon44a28b12015-08-12 15:43:41 +0100671 /*
672 * XXX: wait for any outstanding submissions before freeing memory.
673 * Be sure to drop any locks
674 */
675
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100676 if (client->client_base) {
677 /*
Dave Gordona6674292016-06-13 17:57:32 +0100678 * If we got as far as setting up a doorbell, make sure we
679 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100680 */
Dave Gordona6674292016-06-13 17:57:32 +0100681 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100682
683 kunmap(kmap_to_page(client->client_base));
684 }
685
Chris Wilson19880c42016-08-15 10:49:05 +0100686 i915_vma_unpin_and_release(&client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100687
688 if (client->ctx_index != GUC_INVALID_CTX_ID) {
Dave Gordon7a9347f2016-09-12 21:19:37 +0100689 guc_ctx_desc_fini(guc, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100690 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
691 }
692
693 kfree(client);
694}
695
Dave Gordon84b7f882016-08-09 15:19:20 +0100696/* Check that a doorbell register is in the expected state */
697static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
698{
699 struct drm_i915_private *dev_priv = guc_to_i915(guc);
700 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
701 uint32_t value = I915_READ(drbreg);
702 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
703 bool expected = test_bit(db_id, guc->doorbell_bitmap);
704
705 if (enabled == expected)
706 return true;
707
708 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
709 db_id, drbreg.reg, value,
710 expected ? "active" : "inactive");
711
712 return false;
713}
714
Dave Gordon4d757872016-06-13 17:57:34 +0100715/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100716 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100717 * in turn, to ensure that all doorbell h/w is (re)initialised.
718 */
719static void guc_init_doorbell_hw(struct intel_guc *guc)
720{
Dave Gordon4d757872016-06-13 17:57:34 +0100721 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100722 uint16_t db_id;
723 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100724
Dave Gordon84b7f882016-08-09 15:19:20 +0100725 /* Save client's original doorbell selection */
Dave Gordon4d757872016-06-13 17:57:34 +0100726 db_id = client->doorbell_id;
727
728 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100729 /* Skip if doorbell is OK */
730 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100731 continue;
732
Dave Gordon4d757872016-06-13 17:57:34 +0100733 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100734 if (err)
735 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
736 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100737 }
738
739 /* Restore to original value */
740 err = guc_update_doorbell_id(guc, client, db_id);
741 if (err)
Dave Gordon535b2f52016-08-18 18:17:23 +0100742 DRM_WARN("Failed to restore doorbell to %d, err %d\n",
743 db_id, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100744
Dave Gordon84b7f882016-08-09 15:19:20 +0100745 /* Read back & verify all doorbell registers */
746 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
747 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100748}
749
Dave Gordon44a28b12015-08-12 15:43:41 +0100750/**
751 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100752 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100753 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100754 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
755 * The kernel client to replace ExecList submission is created with
756 * NORMAL priority. Priority of a client for scheduler can be HIGH,
757 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700758 * @ctx: the context that owns the client (we use the default render
759 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100760 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100761 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100762 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100763static struct i915_guc_client *
764guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100765 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100766 uint32_t priority,
767 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100768{
769 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100770 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100771 struct i915_vma *vma;
Dave Gordona6674292016-06-13 17:57:32 +0100772 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100773
774 client = kzalloc(sizeof(*client), GFP_KERNEL);
775 if (!client)
776 return NULL;
777
Alex Daid1675192015-08-12 15:43:43 +0100778 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100779 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100780 client->engines = engines;
781 client->priority = priority;
782 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100783
784 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
785 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
786 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
787 client->ctx_index = GUC_INVALID_CTX_ID;
788 goto err;
789 }
790
791 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100792 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
793 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100794 goto err;
795
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100796 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100797 client->vma = vma;
798 client->client_base = kmap(i915_vma_first_page(vma));
Chris Wilsondadd4812016-09-09 14:11:57 +0100799
800 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100801 client->wq_offset = GUC_DB_SIZE;
802 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100803
Dave Gordonf10d69a2016-06-13 17:57:33 +0100804 db_id = select_doorbell_register(guc, client->priority);
805 if (db_id == GUC_INVALID_DOORBELL_ID)
806 /* XXX: evict a doorbell instead? */
807 goto err;
808
Dave Gordon44a28b12015-08-12 15:43:41 +0100809 client->doorbell_offset = select_doorbell_cacheline(guc);
810
811 /*
812 * Since the doorbell only requires a single cacheline, we can save
813 * space by putting the application process descriptor in the same
814 * page. Use the half of the page that doesn't include the doorbell.
815 */
816 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
817 client->proc_desc_offset = 0;
818 else
819 client->proc_desc_offset = (GUC_DB_SIZE / 2);
820
Dave Gordon7a9347f2016-09-12 21:19:37 +0100821 guc_proc_desc_init(guc, client);
822 guc_ctx_desc_init(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100823 if (guc_init_doorbell(guc, client, db_id))
Dave Gordon44a28b12015-08-12 15:43:41 +0100824 goto err;
825
Dave Gordone02757d2016-08-09 15:19:21 +0100826 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
827 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100828 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
829 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100830
831 return client;
832
833err:
Dave Gordon0daf5562016-06-10 18:29:25 +0100834 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100835 return NULL;
836}
837
Dave Gordon7a9347f2016-09-12 21:19:37 +0100838static void guc_log_create(struct intel_guc *guc)
Alex Dai4c7e77f2015-08-12 15:43:40 +0100839{
Chris Wilson8b797af2016-08-15 10:48:51 +0100840 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +0100841 unsigned long offset;
842 uint32_t size, flags;
843
844 if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
845 return;
846
847 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
848 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
849
850 /* The first page is to save log buffer state. Allocate one
851 * extra page for others in case for overlap */
852 size = (1 + GUC_LOG_DPC_PAGES + 1 +
853 GUC_LOG_ISR_PAGES + 1 +
854 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
855
Chris Wilson8b797af2016-08-15 10:48:51 +0100856 vma = guc->log_vma;
857 if (!vma) {
858 vma = guc_allocate_vma(guc, size);
859 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +0100860 /* logging will be off */
861 i915.guc_log_level = -1;
862 return;
863 }
864
Chris Wilson8b797af2016-08-15 10:48:51 +0100865 guc->log_vma = vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +0100866 }
867
868 /* each allocated unit is a page */
869 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
870 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
871 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
872 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
873
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100874 offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
Alex Dai4c7e77f2015-08-12 15:43:40 +0100875 guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
876}
877
Dave Gordon7a9347f2016-09-12 21:19:37 +0100878static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -0800879{
880 struct guc_policy *policy;
881 u32 p, i;
882
883 policies->dpc_promote_time = 500000;
884 policies->max_num_work_items = POLICY_MAX_NUM_WI;
885
886 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -0800887 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -0800888 policy = &policies->policy[p][i];
889
890 policy->execution_quantum = 1000000;
891 policy->preemption_time = 500000;
892 policy->fault_time = 250000;
893 policy->policy_flags = 0;
894 }
895 }
896
897 policies->is_valid = 1;
898}
899
Dave Gordon7a9347f2016-09-12 21:19:37 +0100900static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -0800901{
902 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +0100903 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -0800904 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -0800905 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -0800906 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000907 struct intel_engine_cs *engine;
Alex Dai68371a92015-12-18 12:00:09 -0800908 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +0000909 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -0800910
911 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -0800912 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
913 sizeof(struct guc_mmio_reg_state) +
914 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -0800915
Chris Wilson8b797af2016-08-15 10:48:51 +0100916 vma = guc->ads_vma;
917 if (!vma) {
918 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
919 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -0800920 return;
921
Chris Wilson8b797af2016-08-15 10:48:51 +0100922 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -0800923 }
924
Chris Wilson8b797af2016-08-15 10:48:51 +0100925 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -0800926 ads = kmap(page);
927
928 /*
929 * The GuC requires a "Golden Context" when it reinitialises
930 * engines after a reset. Here we use the Render ring default
931 * context, which must already exist and be pinned in the GGTT,
932 * so its address won't change after we've told the GuC where
933 * to find it.
934 */
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000935 engine = &dev_priv->engine[RCS];
Chris Wilson57e88532016-08-15 10:48:57 +0100936 ads->golden_context_lrca = engine->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -0800937
Dave Gordonb4ac5af2016-03-24 11:20:38 +0000938 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000939 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -0800940
Alex Dai463704d2015-12-18 12:00:10 -0800941 /* GuC scheduling policies */
942 policies = (void *)ads + sizeof(struct guc_ads);
Dave Gordon7a9347f2016-09-12 21:19:37 +0100943 guc_policies_init(policies);
Alex Dai463704d2015-12-18 12:00:10 -0800944
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100945 ads->scheduler_policies =
946 i915_ggtt_offset(vma) + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -0800947
Alex Dai5c148e02015-12-18 12:00:11 -0800948 /* MMIO reg state */
949 reg_state = (void *)policies + sizeof(struct guc_policies);
950
Dave Gordonb4ac5af2016-03-24 11:20:38 +0000951 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000952 reg_state->mmio_white_list[engine->guc_id].mmio_start =
953 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -0800954
955 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000956 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -0800957 }
958
959 ads->reg_state_addr = ads->scheduler_policies +
960 sizeof(struct guc_policies);
961
962 ads->reg_state_buffer = ads->reg_state_addr +
963 sizeof(struct guc_mmio_reg_state);
964
Alex Dai68371a92015-12-18 12:00:09 -0800965 kunmap(page);
966}
967
Alex Daibac427f2015-08-12 15:43:39 +0100968/*
969 * Set up the memory resources to be shared with the GuC. At this point,
970 * we require just one object that can be mapped through the GGTT.
971 */
Dave Gordonbeffa512016-06-10 18:29:26 +0100972int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +0100973{
Dave Gordon7a9347f2016-09-12 21:19:37 +0100974 const size_t ctxsize = sizeof(struct guc_context_desc);
975 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
976 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +0100977 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100978 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +0100979
Dave Gordon29fb72c2016-06-07 09:14:50 +0100980 /* Wipe bitmap & delete client in case of reinitialisation */
981 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +0100982 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +0100983
Alex Daibac427f2015-08-12 15:43:39 +0100984 if (!i915.enable_guc_submission)
985 return 0; /* not enabled */
986
Chris Wilson8b797af2016-08-15 10:48:51 +0100987 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +0100988 return 0; /* already allocated */
989
Dave Gordon7a9347f2016-09-12 21:19:37 +0100990 vma = guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +0100991 if (IS_ERR(vma))
992 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +0100993
Chris Wilson8b797af2016-08-15 10:48:51 +0100994 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +0100995 ida_init(&guc->ctx_ids);
Dave Gordon7a9347f2016-09-12 21:19:37 +0100996 guc_log_create(guc);
997 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -0800998
Alex Daibac427f2015-08-12 15:43:39 +0100999 return 0;
1000}
1001
Dave Gordonbeffa512016-06-10 18:29:26 +01001002int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001003{
Dave Gordon44a28b12015-08-12 15:43:41 +01001004 struct intel_guc *guc = &dev_priv->guc;
1005 struct i915_guc_client *client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001006 struct intel_engine_cs *engine;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001007 struct drm_i915_gem_request *request;
Dave Gordon44a28b12015-08-12 15:43:41 +01001008
1009 /* client for execbuf submission */
Dave Gordon0daf5562016-06-10 18:29:25 +01001010 client = guc_client_alloc(dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +01001011 INTEL_INFO(dev_priv)->ring_mask,
Chris Wilson0ca5fa32016-05-24 14:53:40 +01001012 GUC_CTX_PRIORITY_KMD_NORMAL,
1013 dev_priv->kernel_context);
Dave Gordon44a28b12015-08-12 15:43:41 +01001014 if (!client) {
Dave Gordon535b2f52016-08-18 18:17:23 +01001015 DRM_ERROR("Failed to create normal GuC client!\n");
Dave Gordon44a28b12015-08-12 15:43:41 +01001016 return -ENOMEM;
1017 }
1018
1019 guc->execbuf_client = client;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001020 host2guc_sample_forcewake(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +01001021 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001022
Chris Wilsonddd66c52016-08-02 22:50:31 +01001023 /* Take over from manual control of ELSP (execlists) */
Chris Wilson821ed7d2016-09-09 14:11:53 +01001024 for_each_engine(engine, dev_priv) {
Chris Wilsonddd66c52016-08-02 22:50:31 +01001025 engine->submit_request = i915_guc_submit;
1026
Chris Wilson821ed7d2016-09-09 14:11:53 +01001027 /* Replay the current set of previously submitted requests */
Chris Wilsondadd4812016-09-09 14:11:57 +01001028 list_for_each_entry(request, &engine->request_list, link) {
1029 client->wq_rsvd += sizeof(struct guc_wq_item);
Chris Wilson5590af32016-09-09 14:11:54 +01001030 if (i915_sw_fence_done(&request->submit))
1031 i915_guc_submit(request);
Chris Wilsondadd4812016-09-09 14:11:57 +01001032 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001033 }
1034
Dave Gordon44a28b12015-08-12 15:43:41 +01001035 return 0;
1036}
1037
Dave Gordonbeffa512016-06-10 18:29:26 +01001038void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001039{
Dave Gordon44a28b12015-08-12 15:43:41 +01001040 struct intel_guc *guc = &dev_priv->guc;
1041
Chris Wilsonddd66c52016-08-02 22:50:31 +01001042 if (!guc->execbuf_client)
1043 return;
1044
Chris Wilsonddd66c52016-08-02 22:50:31 +01001045 /* Revert back to manual ELSP submission */
1046 intel_execlists_enable_submission(dev_priv);
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +01001047
1048 guc_client_free(dev_priv, guc->execbuf_client);
1049 guc->execbuf_client = NULL;
Dave Gordon44a28b12015-08-12 15:43:41 +01001050}
1051
Dave Gordonbeffa512016-06-10 18:29:26 +01001052void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001053{
Alex Daibac427f2015-08-12 15:43:39 +01001054 struct intel_guc *guc = &dev_priv->guc;
1055
Chris Wilson19880c42016-08-15 10:49:05 +01001056 i915_vma_unpin_and_release(&guc->ads_vma);
1057 i915_vma_unpin_and_release(&guc->log_vma);
Alex Dai68371a92015-12-18 12:00:09 -08001058
Chris Wilson8b797af2016-08-15 10:48:51 +01001059 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001060 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001061 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001062}
Alex Daia1c41992015-09-30 09:46:37 -07001063
1064/**
1065 * intel_guc_suspend() - notify GuC entering suspend state
1066 * @dev: drm device
1067 */
1068int intel_guc_suspend(struct drm_device *dev)
1069{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001070 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001071 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001072 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001073 u32 data[3];
1074
Dave Gordonfce91f22016-05-20 11:42:42 +01001075 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001076 return 0;
1077
Dave Gordoned54c1a2016-01-19 19:02:54 +00001078 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001079
1080 data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
1081 /* any value greater than GUC_POWER_D0 */
1082 data[1] = GUC_POWER_D1;
1083 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001084 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001085
1086 return host2guc_action(guc, data, ARRAY_SIZE(data));
1087}
1088
1089
1090/**
1091 * intel_guc_resume() - notify GuC resuming from suspend state
1092 * @dev: drm device
1093 */
1094int intel_guc_resume(struct drm_device *dev)
1095{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001096 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001097 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001098 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001099 u32 data[3];
1100
Dave Gordonfce91f22016-05-20 11:42:42 +01001101 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001102 return 0;
1103
Dave Gordoned54c1a2016-01-19 19:02:54 +00001104 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001105
1106 data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
1107 data[1] = GUC_POWER_D0;
1108 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001109 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001110
1111 return host2guc_action(guc, data, ARRAY_SIZE(data));
1112}