blob: ab91dac15b5b0480d6cf3318eb3e3dbc6cef9009 [file] [log] [blame]
Alex Daibac427f2015-08-12 15:43:39 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
Alex Daibac427f2015-08-12 15:43:39 +010024#include <linux/circ_buf.h>
25#include "i915_drv.h"
Arkadiusz Hiler8c4f24f2016-11-25 18:59:33 +010026#include "intel_uc.h"
Alex Daibac427f2015-08-12 15:43:39 +010027
Chris Wilson31de7352017-03-16 12:56:18 +000028#include <trace/events/dma_fence.h>
29
Alex Daibac427f2015-08-12 15:43:39 +010030/**
Alex Daifeda33e2015-10-19 16:10:54 -070031 * DOC: GuC-based command submission
Dave Gordon44a28b12015-08-12 15:43:41 +010032 *
33 * i915_guc_client:
34 * We use the term client to avoid confusion with contexts. A i915_guc_client is
35 * equivalent to GuC object guc_context_desc. This context descriptor is
36 * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
37 * and workqueue for it. Also the process descriptor (guc_process_desc), which
38 * is mapped to client space. So the client can write Work Item then ring the
39 * doorbell.
40 *
41 * To simplify the implementation, we allocate one gem object that contains all
42 * pages for doorbell, process descriptor and workqueue.
43 *
44 * The Scratch registers:
45 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
46 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
47 * triggers an interrupt on the GuC via another register write (0xC4C8).
48 * Firmware writes a success/fail code back to the action register after
49 * processes the request. The kernel driver polls waiting for this update and
50 * then proceeds.
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010051 * See intel_guc_send()
Dave Gordon44a28b12015-08-12 15:43:41 +010052 *
53 * Doorbells:
54 * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
55 * mapped into process space.
56 *
57 * Work Items:
58 * There are several types of work items that the host may place into a
59 * workqueue, each with its own requirements and limitations. Currently only
60 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
61 * represents in-order queue. The kernel driver packs ring tail pointer and an
62 * ELSP context descriptor dword into Work Item.
Dave Gordon7a9347f2016-09-12 21:19:37 +010063 * See guc_wq_item_append()
Dave Gordon44a28b12015-08-12 15:43:41 +010064 *
Oscar Mateo0704df22017-03-22 10:39:47 -070065 * ADS:
66 * The Additional Data Struct (ADS) has pointers for different buffers used by
67 * the GuC. One single gem object contains the ADS struct itself (guc_ads), the
68 * scheduling policies (guc_policies), a structure describing a collection of
69 * register sets (guc_mmio_reg_state) and some extra pages for the GuC to save
70 * its internal state for sleep.
71 *
Dave Gordon44a28b12015-08-12 15:43:41 +010072 */
73
Joonas Lahtinenabddffd2017-03-22 10:39:44 -070074static inline bool is_high_priority(struct i915_guc_client* client)
75{
76 return client->priority <= GUC_CTX_PRIORITY_HIGH;
77}
78
79static int __reserve_doorbell(struct i915_guc_client *client)
80{
81 unsigned long offset;
82 unsigned long end;
83 u16 id;
84
85 GEM_BUG_ON(client->doorbell_id != GUC_DOORBELL_INVALID);
86
87 /*
88 * The bitmap tracks which doorbell registers are currently in use.
89 * It is split into two halves; the first half is used for normal
90 * priority contexts, the second half for high-priority ones.
91 */
92 offset = 0;
93 end = GUC_NUM_DOORBELLS/2;
94 if (is_high_priority(client)) {
95 offset = end;
96 end += offset;
97 }
98
99 id = find_next_zero_bit(client->guc->doorbell_bitmap, offset, end);
100 if (id == end)
101 return -ENOSPC;
102
103 __set_bit(id, client->guc->doorbell_bitmap);
104 client->doorbell_id = id;
105 DRM_DEBUG_DRIVER("client %u (high prio=%s) reserved doorbell: %d\n",
106 client->ctx_index, yesno(is_high_priority(client)),
107 id);
108 return 0;
109}
110
111static void __unreserve_doorbell(struct i915_guc_client *client)
112{
113 GEM_BUG_ON(client->doorbell_id == GUC_DOORBELL_INVALID);
114
115 __clear_bit(client->doorbell_id, client->guc->doorbell_bitmap);
116 client->doorbell_id = GUC_DOORBELL_INVALID;
117}
118
Dave Gordon44a28b12015-08-12 15:43:41 +0100119/*
Dave Gordon44a28b12015-08-12 15:43:41 +0100120 * Tell the GuC to allocate or deallocate a specific doorbell
121 */
122
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700123static int __guc_allocate_doorbell(struct intel_guc *guc, u32 ctx_index)
Dave Gordon44a28b12015-08-12 15:43:41 +0100124{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100125 u32 action[] = {
126 INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700127 ctx_index
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100128 };
Dave Gordon44a28b12015-08-12 15:43:41 +0100129
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100130 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Dave Gordon44a28b12015-08-12 15:43:41 +0100131}
132
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700133static int __guc_deallocate_doorbell(struct intel_guc *guc, u32 ctx_index)
Dave Gordon44a28b12015-08-12 15:43:41 +0100134{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100135 u32 action[] = {
136 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700137 ctx_index
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100138 };
Dave Gordon44a28b12015-08-12 15:43:41 +0100139
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100140 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Sagar Arun Kamble685534e2016-10-12 21:54:41 +0530141}
142
Oscar Mateo73b05532017-03-22 10:39:45 -0700143static struct guc_context_desc *__get_context_desc(struct i915_guc_client *client)
144{
145 struct guc_context_desc *base = client->guc->ctx_pool_vaddr;
146
147 return &base[client->ctx_index];
148}
149
Dave Gordon44a28b12015-08-12 15:43:41 +0100150/*
151 * Initialise, update, or clear doorbell data shared with the GuC
152 *
153 * These functions modify shared data and so need access to the mapped
154 * client object which contains the page being used for the doorbell
155 */
156
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700157static int __update_doorbell_desc(struct i915_guc_client *client, u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100158{
Oscar Mateo73b05532017-03-22 10:39:45 -0700159 struct guc_context_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100160
Dave Gordona6674292016-06-13 17:57:32 +0100161 /* Update the GuC's idea of the doorbell ID */
Oscar Mateo73b05532017-03-22 10:39:45 -0700162 desc = __get_context_desc(client);
163 desc->db_id = new_id;
Dave Gordona6674292016-06-13 17:57:32 +0100164
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700165 return 0;
Dave Gordona6674292016-06-13 17:57:32 +0100166}
167
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700168static struct guc_doorbell_info *__get_doorbell(struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100169{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700170 return client->vaddr + client->doorbell_offset;
171}
172
173static bool has_doorbell(struct i915_guc_client *client)
174{
175 if (client->doorbell_id == GUC_DOORBELL_INVALID)
176 return false;
177
178 return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
179}
180
181static int __create_doorbell(struct i915_guc_client *client)
182{
183 struct guc_doorbell_info *doorbell;
184 int err;
185
186 doorbell = __get_doorbell(client);
187 doorbell->db_status = GUC_DOORBELL_ENABLED;
188 doorbell->cookie = client->doorbell_cookie;
189
190 err = __guc_allocate_doorbell(client->guc, client->ctx_index);
191 if (err) {
192 doorbell->db_status = GUC_DOORBELL_DISABLED;
193 doorbell->cookie = 0;
194 }
195 return err;
196}
197
198static int __destroy_doorbell(struct i915_guc_client *client)
199{
200 struct guc_doorbell_info *doorbell;
201
202 doorbell = __get_doorbell(client);
203 doorbell->db_status = GUC_DOORBELL_DISABLED;
204 doorbell->cookie = 0;
205
206 return __guc_deallocate_doorbell(client->guc, client->ctx_index);
207}
208
209static int destroy_doorbell(struct i915_guc_client *client)
210{
211 int err;
212
213 GEM_BUG_ON(!has_doorbell(client));
Dave Gordon44a28b12015-08-12 15:43:41 +0100214
Dave Gordon44a28b12015-08-12 15:43:41 +0100215 /* XXX: wait for any interrupts */
216 /* XXX: wait for workqueue to drain */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700217
218 err = __destroy_doorbell(client);
219 if (err)
220 return err;
221
222 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
223
224 __unreserve_doorbell(client);
225
226 return 0;
Dave Gordon44a28b12015-08-12 15:43:41 +0100227}
228
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700229static unsigned long __select_cacheline(struct intel_guc* guc)
Dave Gordonf10d69a2016-06-13 17:57:33 +0100230{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700231 unsigned long offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100232
Dave Gordon44a28b12015-08-12 15:43:41 +0100233 /* Doorbell uses a single cache line within a page */
234 offset = offset_in_page(guc->db_cacheline);
235
236 /* Moving to next cache line to reduce contention */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700237 guc->db_cacheline += cache_line_size();
Dave Gordon44a28b12015-08-12 15:43:41 +0100238
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700239 DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
240 offset, guc->db_cacheline, cache_line_size());
Dave Gordon44a28b12015-08-12 15:43:41 +0100241 return offset;
242}
243
Dave Gordon44a28b12015-08-12 15:43:41 +0100244/*
245 * Initialise the process descriptor shared with the GuC firmware.
246 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100247static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100248 struct i915_guc_client *client)
249{
250 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100251
Chris Wilson72aa0d82016-11-02 17:50:47 +0000252 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100253
254 memset(desc, 0, sizeof(*desc));
255
256 /*
257 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
258 * space for ring3 clients (set them as in mmap_ioctl) or kernel
259 * space for kernel clients (map on demand instead? May make debug
260 * easier to have it mapped).
261 */
262 desc->wq_base_addr = 0;
263 desc->db_base_addr = 0;
264
265 desc->context_id = client->ctx_index;
266 desc->wq_size_bytes = client->wq_size;
267 desc->wq_status = WQ_STATUS_ACTIVE;
268 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100269}
270
271/*
272 * Initialise/clear the context descriptor shared with the GuC firmware.
273 *
274 * This descriptor tells the GuC where (in GGTT space) to find the important
275 * data structures relating to this client (doorbell, process descriptor,
276 * write queue, etc).
277 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100278static void guc_ctx_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100279 struct i915_guc_client *client)
280{
Alex Dai397097b2016-01-23 11:58:14 -0800281 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000282 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100283 struct i915_gem_context *ctx = client->owner;
Oscar Mateo73b05532017-03-22 10:39:45 -0700284 struct guc_context_desc *desc;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100285 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100286 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100287
Oscar Mateo73b05532017-03-22 10:39:45 -0700288 desc = __get_context_desc(client);
289 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100290
Oscar Mateo73b05532017-03-22 10:39:45 -0700291 desc->attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
292 desc->context_id = client->ctx_index;
293 desc->priority = client->priority;
294 desc->db_id = client->doorbell_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100295
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100296 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100297 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100298 uint32_t guc_engine_id = engine->guc_id;
Oscar Mateo73b05532017-03-22 10:39:45 -0700299 struct guc_execlist_context *lrc = &desc->lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100300
301 /* TODO: We have a design issue to be solved here. Only when we
302 * receive the first batch, we know which engine is used by the
303 * user. But here GuC expects the lrc and ring to be pinned. It
304 * is not an issue for default context, which is the only one
305 * for now who owns a GuC client. But for future owner of GuC
306 * client, need to make sure lrc is pinned prior to enter here.
307 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100308 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100309 break; /* XXX: continue? */
310
Chris Wilson9021ad02016-05-24 14:53:37 +0100311 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100312
313 /* The state page is after PPHWSP */
Chris Wilson57e88532016-08-15 10:48:57 +0100314 lrc->ring_lcra =
Chris Wilson4741da92016-12-24 19:31:46 +0000315 guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100316 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100317 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100318
Chris Wilson4741da92016-12-24 19:31:46 +0000319 lrc->ring_begin = guc_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100320 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
321 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100322 lrc->ring_current_tail_pointer_value = 0;
323
Oscar Mateo73b05532017-03-22 10:39:45 -0700324 desc->engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100325 }
326
Dave Gordone02757d2016-08-09 15:19:21 +0100327 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
Oscar Mateo73b05532017-03-22 10:39:45 -0700328 client->engines, desc->engines_used);
329 WARN_ON(desc->engines_used == 0);
Alex Daid1675192015-08-12 15:43:43 +0100330
Dave Gordon44a28b12015-08-12 15:43:41 +0100331 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100332 * The doorbell, process descriptor, and workqueue are all parts
333 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100334 */
Chris Wilson4741da92016-12-24 19:31:46 +0000335 gfx_addr = guc_ggtt_offset(client->vma);
Oscar Mateo73b05532017-03-22 10:39:45 -0700336 desc->db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100337 client->doorbell_offset;
Oscar Mateo73b05532017-03-22 10:39:45 -0700338 desc->db_trigger_cpu = (uintptr_t)__get_doorbell(client);
339 desc->db_trigger_uk = gfx_addr + client->doorbell_offset;
340 desc->process_desc = gfx_addr + client->proc_desc_offset;
341 desc->wq_addr = gfx_addr + client->wq_offset;
342 desc->wq_size = client->wq_size;
Dave Gordon44a28b12015-08-12 15:43:41 +0100343
344 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100345 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100346 * IsKMDCreatedContext client
347 */
Oscar Mateo73b05532017-03-22 10:39:45 -0700348 desc->desc_private = (uintptr_t)client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100349}
350
Dave Gordon7a9347f2016-09-12 21:19:37 +0100351static void guc_ctx_desc_fini(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100352 struct i915_guc_client *client)
353{
Oscar Mateo73b05532017-03-22 10:39:45 -0700354 struct guc_context_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100355
Oscar Mateo73b05532017-03-22 10:39:45 -0700356 desc = __get_context_desc(client);
357 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100358}
359
Dave Gordon7c2c2702016-05-13 15:36:32 +0100360/**
Dave Gordon7a9347f2016-09-12 21:19:37 +0100361 * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
Dave Gordon7c2c2702016-05-13 15:36:32 +0100362 * @request: request associated with the commands
363 *
364 * Return: 0 if space is available
365 * -EAGAIN if space is not currently available
366 *
367 * This function must be called (and must return 0) before a request
368 * is submitted to the GuC via i915_guc_submit() below. Once a result
Dave Gordon7a9347f2016-09-12 21:19:37 +0100369 * of 0 has been returned, it must be balanced by a corresponding
370 * call to submit().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100371 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100372 * Reservation allows the caller to determine in advance that space
Dave Gordon7c2c2702016-05-13 15:36:32 +0100373 * will be available for the next submission before committing resources
374 * to it, and helps avoid late failures with complicated recovery paths.
375 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100376int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100377{
Dave Gordon551aaec2016-05-13 15:36:33 +0100378 const size_t wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000379 struct i915_guc_client *client = request->i915->guc.execbuf_client;
380 struct guc_process_desc *desc = client->vaddr +
381 client->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100382 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100383 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100384
Chris Wilson349ab912017-02-28 11:28:02 +0000385 spin_lock_irq(&client->wq_lock);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000386 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
387 freespace -= client->wq_rsvd;
Chris Wilsondadd4812016-09-09 14:11:57 +0100388 if (likely(freespace >= wqi_size)) {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000389 client->wq_rsvd += wqi_size;
Chris Wilsondadd4812016-09-09 14:11:57 +0100390 ret = 0;
391 } else {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000392 client->no_wq_space++;
Chris Wilsondadd4812016-09-09 14:11:57 +0100393 ret = -EAGAIN;
394 }
Chris Wilson349ab912017-02-28 11:28:02 +0000395 spin_unlock_irq(&client->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800396
Chris Wilsondadd4812016-09-09 14:11:57 +0100397 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100398}
399
Chris Wilson349ab912017-02-28 11:28:02 +0000400static void guc_client_update_wq_rsvd(struct i915_guc_client *client, int size)
401{
402 unsigned long flags;
403
404 spin_lock_irqsave(&client->wq_lock, flags);
405 client->wq_rsvd += size;
406 spin_unlock_irqrestore(&client->wq_lock, flags);
407}
408
Chris Wilson5ba89902016-10-07 07:53:27 +0100409void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
410{
Chris Wilson349ab912017-02-28 11:28:02 +0000411 const int wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000412 struct i915_guc_client *client = request->i915->guc.execbuf_client;
Chris Wilson5ba89902016-10-07 07:53:27 +0100413
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000414 GEM_BUG_ON(READ_ONCE(client->wq_rsvd) < wqi_size);
Chris Wilson349ab912017-02-28 11:28:02 +0000415 guc_client_update_wq_rsvd(client, -wqi_size);
Chris Wilson5ba89902016-10-07 07:53:27 +0100416}
417
Dave Gordon7a9347f2016-09-12 21:19:37 +0100418/* Construct a Work Item and append it to the GuC's Work Queue */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000419static void guc_wq_item_append(struct i915_guc_client *client,
Dave Gordon7a9347f2016-09-12 21:19:37 +0100420 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100421{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100422 /* wqi_len is in DWords, and does not include the one-word header */
423 const size_t wqi_size = sizeof(struct guc_wq_item);
424 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100425 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100426 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100427 struct guc_wq_item *wqi;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000428 u32 freespace, tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100429
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000430 desc = client->vaddr + client->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800431
Dave Gordon7a9347f2016-09-12 21:19:37 +0100432 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000433 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100434 GEM_BUG_ON(freespace < wqi_size);
435
436 /* The GuC firmware wants the tail index in QWords, not bytes */
437 tail = rq->tail;
438 GEM_BUG_ON(tail & 7);
439 tail >>= 3;
440 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100441
442 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
443 * should not have the case where structure wqi is across page, neither
444 * wrapped to the beginning. This simplifies the implementation below.
445 *
446 * XXX: if not the case, we need save data to a temp wqi and copy it to
447 * workqueue buffer dw by dw.
448 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100449 BUILD_BUG_ON(wqi_size != 16);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000450 GEM_BUG_ON(client->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100451
Dave Gordon0a31afb2016-05-13 15:36:34 +0100452 /* postincrement WQ tail for next time */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000453 wq_off = client->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100454 GEM_BUG_ON(wq_off & (wqi_size - 1));
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000455 client->wq_tail += wqi_size;
456 client->wq_tail &= client->wq_size - 1;
457 client->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100458
459 /* WQ starts from the page after doorbell / process_desc */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000460 wqi = client->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100461
Dave Gordon0a31afb2016-05-13 15:36:34 +0100462 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100463 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100464 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100465 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100466 WQ_NO_WCFLUSH_WAIT;
467
468 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100469 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100470
Dave Gordon44a28b12015-08-12 15:43:41 +0100471 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100472 wqi->fence_id = rq->global_seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100473}
474
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000475static int guc_ring_doorbell(struct i915_guc_client *client)
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100476{
477 struct guc_process_desc *desc;
478 union guc_doorbell_qw db_cmp, db_exc, db_ret;
479 union guc_doorbell_qw *db;
480 int attempt = 2, ret = -EAGAIN;
481
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000482 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100483
484 /* Update the tail so it is visible to GuC */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000485 desc->tail = client->wq_tail;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100486
487 /* current cookie */
488 db_cmp.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000489 db_cmp.cookie = client->doorbell_cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100490
491 /* cookie to be updated */
492 db_exc.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000493 db_exc.cookie = client->doorbell_cookie + 1;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100494 if (db_exc.cookie == 0)
495 db_exc.cookie = 1;
496
497 /* pointer of current doorbell cacheline */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700498 db = (union guc_doorbell_qw *)__get_doorbell(client);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100499
500 while (attempt--) {
501 /* lets ring the doorbell */
502 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
503 db_cmp.value_qw, db_exc.value_qw);
504
505 /* if the exchange was successfully executed */
506 if (db_ret.value_qw == db_cmp.value_qw) {
507 /* db was successfully rung */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000508 client->doorbell_cookie = db_exc.cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100509 ret = 0;
510 break;
511 }
512
513 /* XXX: doorbell was lost and need to acquire it again */
514 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
515 break;
516
Dave Gordon535b2f52016-08-18 18:17:23 +0100517 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
518 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100519
520 /* update the cookie to newly read cookie from GuC */
521 db_cmp.cookie = db_ret.cookie;
522 db_exc.cookie = db_ret.cookie + 1;
523 if (db_exc.cookie == 0)
524 db_exc.cookie = 1;
525 }
526
527 return ret;
528}
529
Dave Gordon44a28b12015-08-12 15:43:41 +0100530/**
Chris Wilson34ba5a82016-11-29 12:10:24 +0000531 * __i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700532 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100533 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100534 * The caller must have already called i915_guc_wq_reserve() above with
535 * a result of 0 (success), guaranteeing that there is space in the work
536 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100537 *
538 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100539 * submit() when _reserve() says there's no space, or calls _submit()
540 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100541 *
542 * The only error here arises if the doorbell hardware isn't functioning
543 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100544 */
Chris Wilson34ba5a82016-11-29 12:10:24 +0000545static void __i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100546{
Akash Goeled4596ea2016-10-25 22:05:23 +0530547 struct drm_i915_private *dev_priv = rq->i915;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000548 struct intel_engine_cs *engine = rq->engine;
549 unsigned int engine_id = engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100550 struct intel_guc *guc = &rq->i915->guc;
551 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilson25afdf892017-03-02 14:53:23 +0000552 unsigned long flags;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100553 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100554
Akash Goeled4596ea2016-10-25 22:05:23 +0530555 /* WA to flush out the pending GMADR writes to ring buffer. */
556 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
557 POSTING_READ_FW(GUC_STATUS);
558
Chris Wilson25afdf892017-03-02 14:53:23 +0000559 spin_lock_irqsave(&client->wq_lock, flags);
Chris Wilson0c335182017-02-28 11:28:03 +0000560
561 guc_wq_item_append(client, rq);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100562 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100563
Alex Dai397097b2016-01-23 11:58:14 -0800564 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100565 client->retcode = b_ret;
566 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100567 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100568
Alex Dai397097b2016-01-23 11:58:14 -0800569 guc->submissions[engine_id] += 1;
Chris Wilson65e47602016-10-28 13:58:49 +0100570 guc->last_seqno[engine_id] = rq->global_seqno;
Chris Wilson0c335182017-02-28 11:28:03 +0000571
Chris Wilson25afdf892017-03-02 14:53:23 +0000572 spin_unlock_irqrestore(&client->wq_lock, flags);
Dave Gordon44a28b12015-08-12 15:43:41 +0100573}
574
Chris Wilson34ba5a82016-11-29 12:10:24 +0000575static void i915_guc_submit(struct drm_i915_gem_request *rq)
576{
Chris Wilson31de7352017-03-16 12:56:18 +0000577 __i915_gem_request_submit(rq);
Chris Wilson34ba5a82016-11-29 12:10:24 +0000578 __i915_guc_submit(rq);
579}
580
Chris Wilson31de7352017-03-16 12:56:18 +0000581static void nested_enable_signaling(struct drm_i915_gem_request *rq)
582{
583 /* If we use dma_fence_enable_sw_signaling() directly, lockdep
584 * detects an ordering issue between the fence lockclass and the
585 * global_timeline. This circular dependency can only occur via 2
586 * different fences (but same fence lockclass), so we use the nesting
587 * annotation here to prevent the warn, equivalent to the nesting
588 * inside i915_gem_request_submit() for when we also enable the
589 * signaler.
590 */
591
592 if (test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
593 &rq->fence.flags))
594 return;
595
596 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags));
597 trace_dma_fence_enable_signal(&rq->fence);
598
599 spin_lock_nested(&rq->lock, SINGLE_DEPTH_NESTING);
600 intel_engine_enable_signaling(rq);
601 spin_unlock(&rq->lock);
602}
603
604static bool i915_guc_dequeue(struct intel_engine_cs *engine)
605{
606 struct execlist_port *port = engine->execlist_port;
607 struct drm_i915_gem_request *last = port[0].request;
Chris Wilson31de7352017-03-16 12:56:18 +0000608 struct rb_node *rb;
609 bool submit = false;
610
Chris Wilson6c943de2017-03-17 12:07:16 +0000611 /* After execlist_first is updated, the tasklet will be rescheduled.
612 *
613 * If we are currently running (inside the tasklet) and a third
614 * party queues a request and so updates engine->execlist_first under
615 * the spinlock (which we have elided), it will atomically set the
616 * TASKLET_SCHED flag causing the us to be re-executed and pick up
617 * the change in state (the update to TASKLET_SCHED incurs a memory
618 * barrier making this cross-cpu checking safe).
619 */
620 if (!READ_ONCE(engine->execlist_first))
621 return false;
622
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000623 spin_lock_irq(&engine->timeline->lock);
Chris Wilson31de7352017-03-16 12:56:18 +0000624 rb = engine->execlist_first;
625 while (rb) {
626 struct drm_i915_gem_request *rq =
627 rb_entry(rb, typeof(*rq), priotree.node);
628
629 if (last && rq->ctx != last->ctx) {
630 if (port != engine->execlist_port)
631 break;
632
633 i915_gem_request_assign(&port->request, last);
634 nested_enable_signaling(last);
635 port++;
636 }
637
638 rb = rb_next(rb);
639 rb_erase(&rq->priotree.node, &engine->execlist_queue);
640 RB_CLEAR_NODE(&rq->priotree.node);
641 rq->priotree.priority = INT_MAX;
642
Chris Wilson31de7352017-03-16 12:56:18 +0000643 i915_guc_submit(rq);
Tvrtko Ursulin66e303e2017-03-20 13:25:56 +0000644 trace_i915_gem_request_in(rq, port - engine->execlist_port);
Chris Wilson31de7352017-03-16 12:56:18 +0000645 last = rq;
646 submit = true;
647 }
648 if (submit) {
649 i915_gem_request_assign(&port->request, last);
650 nested_enable_signaling(last);
651 engine->execlist_first = rb;
652 }
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000653 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson31de7352017-03-16 12:56:18 +0000654
655 return submit;
656}
657
658static void i915_guc_irq_handler(unsigned long data)
659{
660 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
661 struct execlist_port *port = engine->execlist_port;
662 struct drm_i915_gem_request *rq;
663 bool submit;
664
665 do {
666 rq = port[0].request;
667 while (rq && i915_gem_request_completed(rq)) {
668 trace_i915_gem_request_out(rq);
669 i915_gem_request_put(rq);
670 port[0].request = port[1].request;
671 port[1].request = NULL;
672 rq = port[0].request;
673 }
674
675 submit = false;
676 if (!port[1].request)
677 submit = i915_guc_dequeue(engine);
678 } while (submit);
679}
680
Dave Gordon44a28b12015-08-12 15:43:41 +0100681/*
682 * Everything below here is concerned with setup & teardown, and is
683 * therefore not part of the somewhat time-critical batch-submission
684 * path of i915_guc_submit() above.
685 */
686
687/**
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000688 * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
Chris Wilson8b797af2016-08-15 10:48:51 +0100689 * @guc: the guc
690 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100691 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100692 * This is a wrapper to create an object for use with the GuC. In order to
693 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
694 * both some backing storage and a range inside the Global GTT. We must pin
695 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
696 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100697 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100698 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100699 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000700struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100701{
Chris Wilson8b797af2016-08-15 10:48:51 +0100702 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100703 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100704 struct i915_vma *vma;
705 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100706
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000707 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100708 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100709 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100710
Chris Wilsona01cb372017-01-16 15:21:30 +0000711 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson8b797af2016-08-15 10:48:51 +0100712 if (IS_ERR(vma))
713 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100714
Chris Wilson8b797af2016-08-15 10:48:51 +0100715 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
716 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
717 if (ret) {
718 vma = ERR_PTR(ret);
719 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100720 }
721
Chris Wilson8b797af2016-08-15 10:48:51 +0100722 return vma;
723
724err:
725 i915_gem_object_put(obj);
726 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100727}
728
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700729static void guc_client_free(struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100730{
Dave Gordon44a28b12015-08-12 15:43:41 +0100731 /*
732 * XXX: wait for any outstanding submissions before freeing memory.
733 * Be sure to drop any locks
734 */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700735 guc_ctx_desc_fini(client->guc, client);
736 i915_gem_object_unpin_map(client->vma->obj);
Chris Wilson19880c42016-08-15 10:49:05 +0100737 i915_vma_unpin_and_release(&client->vma);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700738 ida_simple_remove(&client->guc->ctx_ids, client->ctx_index);
Dave Gordon44a28b12015-08-12 15:43:41 +0100739 kfree(client);
740}
741
Dave Gordon84b7f882016-08-09 15:19:20 +0100742/* Check that a doorbell register is in the expected state */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700743static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
Dave Gordon84b7f882016-08-09 15:19:20 +0100744{
745 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700746 u32 drbregl;
747 bool valid;
Dave Gordon84b7f882016-08-09 15:19:20 +0100748
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700749 GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
750
751 drbregl = I915_READ(GEN8_DRBREGL(db_id));
752 valid = drbregl & GEN8_DRB_VALID;
753
754 if (test_bit(db_id, guc->doorbell_bitmap) == valid)
Dave Gordon84b7f882016-08-09 15:19:20 +0100755 return true;
756
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700757 DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
758 db_id, drbregl, yesno(valid));
Dave Gordon84b7f882016-08-09 15:19:20 +0100759
760 return false;
761}
762
Dave Gordon4d757872016-06-13 17:57:34 +0100763/*
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700764 * If the GuC thinks that the doorbell is unassigned (e.g. because we reset and
765 * reloaded the GuC FW) we can use this function to tell the GuC to reassign the
766 * doorbell to the rightful owner.
767 */
768static int __reset_doorbell(struct i915_guc_client* client, u16 db_id)
769{
770 int err;
771
772 err = __update_doorbell_desc(client, db_id);
773 if (!err)
774 err = __create_doorbell(client);
775 if (!err)
776 err = __destroy_doorbell(client);
777
778 return err;
779}
780
781/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100782 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100783 * in turn, to ensure that all doorbell h/w is (re)initialised.
784 */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700785static int guc_init_doorbell_hw(struct intel_guc *guc)
Dave Gordon4d757872016-06-13 17:57:34 +0100786{
Dave Gordon4d757872016-06-13 17:57:34 +0100787 struct i915_guc_client *client = guc->execbuf_client;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700788 int err;
789 int i;
Dave Gordon4d757872016-06-13 17:57:34 +0100790
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700791 if (has_doorbell(client))
792 destroy_doorbell(client);
Dave Gordon4d757872016-06-13 17:57:34 +0100793
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700794 for (i = 0; i < GUC_NUM_DOORBELLS; ++i) {
795 if (doorbell_ok(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100796 continue;
797
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700798 err = __reset_doorbell(client, i);
799 WARN(err, "Doorbell %d reset failed, err %d\n", i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100800 }
801
Dave Gordon84b7f882016-08-09 15:19:20 +0100802 /* Read back & verify all doorbell registers */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700803 for (i = 0; i < GUC_NUM_DOORBELLS; ++i)
804 WARN_ON(!doorbell_ok(guc, i));
805
806 err = __reserve_doorbell(client);
807 if (err)
808 return err;
809
810 err = __update_doorbell_desc(client, client->doorbell_id);
811 if (err)
812 goto err_reserve;
813
814 err = __create_doorbell(client);
815 if (err)
816 goto err_update;
817
818 return 0;
819err_reserve:
820 __unreserve_doorbell(client);
821err_update:
822 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
823 return err;
Dave Gordon4d757872016-06-13 17:57:34 +0100824}
825
Dave Gordon44a28b12015-08-12 15:43:41 +0100826/**
827 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100828 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100829 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100830 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
831 * The kernel client to replace ExecList submission is created with
832 * NORMAL priority. Priority of a client for scheduler can be HIGH,
833 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700834 * @ctx: the context that owns the client (we use the default render
835 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100836 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100837 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100838 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100839static struct i915_guc_client *
840guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100841 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100842 uint32_t priority,
843 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100844{
845 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100846 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100847 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000848 void *vaddr;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700849 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100850
851 client = kzalloc(sizeof(*client), GFP_KERNEL);
852 if (!client)
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700853 return ERR_PTR(-ENOMEM);
Dave Gordon44a28b12015-08-12 15:43:41 +0100854
Dave Gordon44a28b12015-08-12 15:43:41 +0100855 client->guc = guc;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700856 client->owner = ctx;
Dave Gordone02757d2016-08-09 15:19:21 +0100857 client->engines = engines;
858 client->priority = priority;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700859 client->doorbell_id = GUC_DOORBELL_INVALID;
860 client->wq_offset = GUC_DB_SIZE;
861 client->wq_size = GUC_WQ_SIZE;
862 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100863
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700864 ret = ida_simple_get(&guc->ctx_ids, 0, GUC_MAX_GPU_CONTEXTS,
865 GFP_KERNEL);
866 if (ret < 0)
867 goto err_client;
868
869 client->ctx_index = ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100870
871 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000872 vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700873 if (IS_ERR(vma)) {
874 ret = PTR_ERR(vma);
875 goto err_id;
876 }
Dave Gordon44a28b12015-08-12 15:43:41 +0100877
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100878 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100879 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000880
881 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700882 if (IS_ERR(vaddr)) {
883 ret = PTR_ERR(vaddr);
884 goto err_vma;
885 }
Chris Wilson72aa0d82016-11-02 17:50:47 +0000886 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100887
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700888 client->doorbell_offset = __select_cacheline(guc);
Dave Gordon44a28b12015-08-12 15:43:41 +0100889
890 /*
891 * Since the doorbell only requires a single cacheline, we can save
892 * space by putting the application process descriptor in the same
893 * page. Use the half of the page that doesn't include the doorbell.
894 */
895 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
896 client->proc_desc_offset = 0;
897 else
898 client->proc_desc_offset = (GUC_DB_SIZE / 2);
899
Dave Gordon7a9347f2016-09-12 21:19:37 +0100900 guc_proc_desc_init(guc, client);
901 guc_ctx_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000902
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700903 /* FIXME: Runtime client allocation (which currently we don't do) will
904 * require that the doorbell gets created now. The static execbuf_client
905 * is now getting its doorbell later (on submission enable) but maybe we
906 * also want to reorder things in the future so that we don't have to
907 * special case the doorbell creation */
Dave Gordon44a28b12015-08-12 15:43:41 +0100908
Dave Gordone02757d2016-08-09 15:19:21 +0100909 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700910 priority, client, client->engines, client->ctx_index);
911 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
912 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100913
914 return client;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700915err_vma:
916 i915_vma_unpin_and_release(&client->vma);
917err_id:
918 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
919err_client:
920 kfree(client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700921 return ERR_PTR(ret);
Dave Gordon44a28b12015-08-12 15:43:41 +0100922}
923
Dave Gordon7a9347f2016-09-12 21:19:37 +0100924static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -0800925{
926 struct guc_policy *policy;
927 u32 p, i;
928
929 policies->dpc_promote_time = 500000;
930 policies->max_num_work_items = POLICY_MAX_NUM_WI;
931
932 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -0800933 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -0800934 policy = &policies->policy[p][i];
935
936 policy->execution_quantum = 1000000;
937 policy->preemption_time = 500000;
938 policy->fault_time = 250000;
939 policy->policy_flags = 0;
940 }
941 }
942
943 policies->is_valid = 1;
944}
945
Oscar Mateo0704df22017-03-22 10:39:47 -0700946static int guc_ads_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -0800947{
948 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +0100949 struct i915_vma *vma;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000950 struct page *page;
951 /* The ads obj includes the struct itself and buffers passed to GuC */
952 struct {
953 struct guc_ads ads;
954 struct guc_policies policies;
955 struct guc_mmio_reg_state reg_state;
956 u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
957 } __packed *blob;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000958 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530959 enum intel_engine_id id;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000960 u32 base;
Alex Dai68371a92015-12-18 12:00:09 -0800961
Oscar Mateo3950bf32017-03-22 10:39:46 -0700962 GEM_BUG_ON(guc->ads_vma);
Alex Dai68371a92015-12-18 12:00:09 -0800963
Oscar Mateo3950bf32017-03-22 10:39:46 -0700964 vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
965 if (IS_ERR(vma))
966 return PTR_ERR(vma);
967
968 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -0800969
Chris Wilson8b797af2016-08-15 10:48:51 +0100970 page = i915_vma_first_page(vma);
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000971 blob = kmap(page);
972
973 /* GuC scheduling policies */
974 guc_policies_init(&blob->policies);
975
976 /* MMIO reg state */
977 for_each_engine(engine, dev_priv, id) {
978 blob->reg_state.mmio_white_list[engine->guc_id].mmio_start =
979 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
980
981 /* Nothing to be saved or restored for now. */
982 blob->reg_state.mmio_white_list[engine->guc_id].count = 0;
983 }
Alex Dai68371a92015-12-18 12:00:09 -0800984
985 /*
986 * The GuC requires a "Golden Context" when it reinitialises
987 * engines after a reset. Here we use the Render ring default
988 * context, which must already exist and be pinned in the GGTT,
989 * so its address won't change after we've told the GuC where
990 * to find it.
991 */
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000992 blob->ads.golden_context_lrca =
993 dev_priv->engine[RCS]->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -0800994
Akash Goel3b3f1652016-10-13 22:44:48 +0530995 for_each_engine(engine, dev_priv, id)
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000996 blob->ads.eng_state_size[engine->guc_id] =
997 intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -0800998
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000999 base = guc_ggtt_offset(vma);
1000 blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
1001 blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
1002 blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
Alex Dai5c148e02015-12-18 12:00:11 -08001003
Alex Dai68371a92015-12-18 12:00:09 -08001004 kunmap(page);
Oscar Mateo3950bf32017-03-22 10:39:46 -07001005
1006 return 0;
1007}
1008
Oscar Mateo0704df22017-03-22 10:39:47 -07001009static void guc_ads_destroy(struct intel_guc *guc)
Oscar Mateo3950bf32017-03-22 10:39:46 -07001010{
1011 i915_vma_unpin_and_release(&guc->ads_vma);
Alex Dai68371a92015-12-18 12:00:09 -08001012}
1013
Alex Daibac427f2015-08-12 15:43:39 +01001014/*
1015 * Set up the memory resources to be shared with the GuC. At this point,
1016 * we require just one object that can be mapped through the GGTT.
1017 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001018int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001019{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001020 const size_t ctxsize = sizeof(struct guc_context_desc);
1021 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1022 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001023 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001024 struct i915_vma *vma;
Oscar Mateo73b05532017-03-22 10:39:45 -07001025 void *vaddr;
Oscar Mateo3950bf32017-03-22 10:39:46 -07001026 int ret;
Alex Daibac427f2015-08-12 15:43:39 +01001027
Chris Wilson4d357af2016-11-29 12:10:23 +00001028 if (!HAS_GUC_SCHED(dev_priv))
1029 return 0;
1030
Dave Gordon29fb72c2016-06-07 09:14:50 +01001031 /* Wipe bitmap & delete client in case of reinitialisation */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001032 bitmap_clear(guc->doorbell_bitmap, 0, GUC_NUM_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001033 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001034
Alex Daibac427f2015-08-12 15:43:39 +01001035 if (!i915.enable_guc_submission)
Oscar Mateo3950bf32017-03-22 10:39:46 -07001036 return 0;
Alex Daibac427f2015-08-12 15:43:39 +01001037
Oscar Mateo73b05532017-03-22 10:39:45 -07001038 if (guc->ctx_pool)
Oscar Mateo3950bf32017-03-22 10:39:46 -07001039 return 0;
Alex Daibac427f2015-08-12 15:43:39 +01001040
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001041 vma = intel_guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001042 if (IS_ERR(vma))
1043 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001044
Oscar Mateo73b05532017-03-22 10:39:45 -07001045 guc->ctx_pool = vma;
1046
Oscar Mateo3950bf32017-03-22 10:39:46 -07001047 vaddr = i915_gem_object_pin_map(guc->ctx_pool->obj, I915_MAP_WB);
1048 if (IS_ERR(vaddr)) {
1049 ret = PTR_ERR(vaddr);
1050 goto err_vma;
1051 }
Oscar Mateo73b05532017-03-22 10:39:45 -07001052
1053 guc->ctx_pool_vaddr = vaddr;
1054
Oscar Mateo3950bf32017-03-22 10:39:46 -07001055 ret = intel_guc_log_create(guc);
1056 if (ret < 0)
1057 goto err_vaddr;
1058
Oscar Mateo0704df22017-03-22 10:39:47 -07001059 ret = guc_ads_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -07001060 if (ret < 0)
1061 goto err_log;
1062
Alex Daibac427f2015-08-12 15:43:39 +01001063 ida_init(&guc->ctx_ids);
Alex Dai68371a92015-12-18 12:00:09 -08001064
Chris Wilson4d357af2016-11-29 12:10:23 +00001065 guc->execbuf_client = guc_client_alloc(dev_priv,
1066 INTEL_INFO(dev_priv)->ring_mask,
1067 GUC_CTX_PRIORITY_KMD_NORMAL,
1068 dev_priv->kernel_context);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001069 if (IS_ERR(guc->execbuf_client)) {
Chris Wilson4d357af2016-11-29 12:10:23 +00001070 DRM_ERROR("Failed to create GuC client for execbuf!\n");
Oscar Mateo3950bf32017-03-22 10:39:46 -07001071 ret = PTR_ERR(guc->execbuf_client);
1072 goto err_ads;
Chris Wilson4d357af2016-11-29 12:10:23 +00001073 }
1074
Alex Daibac427f2015-08-12 15:43:39 +01001075 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +00001076
Oscar Mateo3950bf32017-03-22 10:39:46 -07001077err_ads:
Oscar Mateo0704df22017-03-22 10:39:47 -07001078 guc_ads_destroy(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -07001079err_log:
1080 intel_guc_log_destroy(guc);
1081err_vaddr:
1082 i915_gem_object_unpin_map(guc->ctx_pool->obj);
1083err_vma:
1084 i915_vma_unpin_and_release(&guc->ctx_pool);
1085 return ret;
1086}
1087
1088void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
1089{
1090 struct intel_guc *guc = &dev_priv->guc;
1091
1092 if (!i915.enable_guc_submission)
1093 return 0;
1094
1095 guc_client_free(guc->execbuf_client);
1096 guc->execbuf_client = NULL;
1097 ida_destroy(&guc->ctx_ids);
Oscar Mateo0704df22017-03-22 10:39:47 -07001098 guc_ads_destroy(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -07001099 intel_guc_log_destroy(guc);
1100 i915_gem_object_unpin_map(guc->ctx_pool->obj);
1101 i915_vma_unpin_and_release(&guc->ctx_pool);
Chris Wilson4d357af2016-11-29 12:10:23 +00001102}
1103
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001104static void guc_reset_wq(struct i915_guc_client *client)
Chris Wilson4d357af2016-11-29 12:10:23 +00001105{
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001106 struct guc_process_desc *desc = client->vaddr +
1107 client->proc_desc_offset;
Chris Wilson4d357af2016-11-29 12:10:23 +00001108
1109 desc->head = 0;
1110 desc->tail = 0;
1111
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001112 client->wq_tail = 0;
Alex Daibac427f2015-08-12 15:43:39 +01001113}
1114
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001115static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
1116{
1117 struct intel_engine_cs *engine;
1118 enum intel_engine_id id;
1119 int irqs;
1120
1121 /* tell all command streamers to forward interrupts (but not vblank) to GuC */
1122 irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
1123 for_each_engine(engine, dev_priv, id)
1124 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1125
1126 /* route USER_INTERRUPT to Host, all others are sent to GuC. */
1127 irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
1128 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
1129 /* These three registers have the same bit definitions */
1130 I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
1131 I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
1132 I915_WRITE(GUC_WD_VECS_IER, ~irqs);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301133
1134 /*
1135 * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
1136 * (unmasked) PM interrupts to the GuC. All other bits of this
1137 * register *disable* generation of a specific interrupt.
1138 *
1139 * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
1140 * writing to the PM interrupt mask register, i.e. interrupts
1141 * that must not be disabled.
1142 *
1143 * If the GuC is handling these interrupts, then we must not let
1144 * the PM code disable ANY interrupt that the GuC is expecting.
1145 * So for each ENABLED (0) bit in this register, we must SET the
1146 * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
1147 * GuC needs ARAT expired interrupt unmasked hence it is set in
1148 * pm_intrmsk_mbz.
1149 *
1150 * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1151 * result in the register bit being left SET!
1152 */
1153 dev_priv->rps.pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
Chris Wilson655d49e2017-03-12 13:27:45 +00001154 dev_priv->rps.pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001155}
1156
Dave Gordonbeffa512016-06-10 18:29:26 +01001157int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001158{
Dave Gordon44a28b12015-08-12 15:43:41 +01001159 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001160 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001161 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301162 enum intel_engine_id id;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001163 int err;
Dave Gordon44a28b12015-08-12 15:43:41 +01001164
Chris Wilson4d357af2016-11-29 12:10:23 +00001165 if (!client)
1166 return -ENODEV;
Dave Gordon44a28b12015-08-12 15:43:41 +01001167
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001168 err = intel_guc_sample_forcewake(guc);
1169 if (err)
1170 return err;
Chris Wilson4d357af2016-11-29 12:10:23 +00001171
1172 guc_reset_wq(client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001173 err = guc_init_doorbell_hw(guc);
1174 if (err)
1175 return err;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001176
Chris Wilsonddd66c52016-08-02 22:50:31 +01001177 /* Take over from manual control of ELSP (execlists) */
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001178 guc_interrupts_capture(dev_priv);
1179
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001180 for_each_engine(engine, dev_priv, id) {
Chris Wilson349ab912017-02-28 11:28:02 +00001181 const int wqi_size = sizeof(struct guc_wq_item);
Chris Wilson4d357af2016-11-29 12:10:23 +00001182 struct drm_i915_gem_request *rq;
1183
Chris Wilson31de7352017-03-16 12:56:18 +00001184 /* The tasklet was initialised by execlists, and may be in
1185 * a state of flux (across a reset) and so we just want to
1186 * take over the callback without changing any other state
1187 * in the tasklet.
1188 */
1189 engine->irq_tasklet.func = i915_guc_irq_handler;
1190 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1191
1192 /* Replay the current set of previously submitted requests */
Chris Wilson349ab912017-02-28 11:28:02 +00001193 spin_lock_irq(&engine->timeline->lock);
Chris Wilson4d357af2016-11-29 12:10:23 +00001194 list_for_each_entry(rq, &engine->timeline->requests, link) {
Chris Wilson349ab912017-02-28 11:28:02 +00001195 guc_client_update_wq_rsvd(client, wqi_size);
Chris Wilson34ba5a82016-11-29 12:10:24 +00001196 __i915_guc_submit(rq);
Chris Wilsondadd4812016-09-09 14:11:57 +01001197 }
Chris Wilson349ab912017-02-28 11:28:02 +00001198 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001199 }
1200
Dave Gordon44a28b12015-08-12 15:43:41 +01001201 return 0;
1202}
1203
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301204static void guc_interrupts_release(struct drm_i915_private *dev_priv)
1205{
1206 struct intel_engine_cs *engine;
1207 enum intel_engine_id id;
1208 int irqs;
1209
1210 /*
1211 * tell all command streamers NOT to forward interrupts or vblank
1212 * to GuC.
1213 */
1214 irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
1215 irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
1216 for_each_engine(engine, dev_priv, id)
1217 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1218
1219 /* route all GT interrupts to the host */
1220 I915_WRITE(GUC_BCS_RCS_IER, 0);
1221 I915_WRITE(GUC_VCS2_VCS1_IER, 0);
1222 I915_WRITE(GUC_WD_VECS_IER, 0);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301223
Chris Wilson655d49e2017-03-12 13:27:45 +00001224 dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301225 dev_priv->rps.pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301226}
1227
Dave Gordonbeffa512016-06-10 18:29:26 +01001228void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001229{
Dave Gordon44a28b12015-08-12 15:43:41 +01001230 struct intel_guc *guc = &dev_priv->guc;
1231
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301232 guc_interrupts_release(dev_priv);
1233
Chris Wilsonddd66c52016-08-02 22:50:31 +01001234 if (!guc->execbuf_client)
1235 return;
1236
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001237 /* FIXME: in many cases, by the time we get here the GuC has been
1238 * reset, so we cannot destroy the doorbell properly. Ignore the
1239 * error message for now */
1240 destroy_doorbell(guc->execbuf_client);
1241
Chris Wilsonddd66c52016-08-02 22:50:31 +01001242 /* Revert back to manual ELSP submission */
Chris Wilsonff44ad52017-03-16 17:13:03 +00001243 intel_engines_reset_default_submission(dev_priv);
Dave Gordon44a28b12015-08-12 15:43:41 +01001244}
1245
Alex Daia1c41992015-09-30 09:46:37 -07001246/**
1247 * intel_guc_suspend() - notify GuC entering suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001248 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001249 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001250int intel_guc_suspend(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001251{
Alex Daia1c41992015-09-30 09:46:37 -07001252 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001253 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001254 u32 data[3];
1255
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001256 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001257 return 0;
1258
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301259 gen9_disable_guc_interrupts(dev_priv);
1260
Dave Gordoned54c1a2016-01-19 19:02:54 +00001261 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001262
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001263 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001264 /* any value greater than GUC_POWER_D0 */
1265 data[1] = GUC_POWER_D1;
1266 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001267 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001268
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001269 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001270}
1271
1272
1273/**
1274 * intel_guc_resume() - notify GuC resuming from suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001275 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001276 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001277int intel_guc_resume(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001278{
Alex Daia1c41992015-09-30 09:46:37 -07001279 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001280 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001281 u32 data[3];
1282
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001283 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001284 return 0;
1285
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301286 if (i915.guc_log_level >= 0)
1287 gen9_enable_guc_interrupts(dev_priv);
1288
Dave Gordoned54c1a2016-01-19 19:02:54 +00001289 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001290
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001291 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001292 data[1] = GUC_POWER_D0;
1293 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001294 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001295
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001296 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001297}