blob: be88bbebd15557b2aa5b67147c9aa8b343f442b0 [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 *
65 */
66
Joonas Lahtinenabddffd2017-03-22 10:39:44 -070067static inline bool is_high_priority(struct i915_guc_client* client)
68{
69 return client->priority <= GUC_CTX_PRIORITY_HIGH;
70}
71
72static int __reserve_doorbell(struct i915_guc_client *client)
73{
74 unsigned long offset;
75 unsigned long end;
76 u16 id;
77
78 GEM_BUG_ON(client->doorbell_id != GUC_DOORBELL_INVALID);
79
80 /*
81 * The bitmap tracks which doorbell registers are currently in use.
82 * It is split into two halves; the first half is used for normal
83 * priority contexts, the second half for high-priority ones.
84 */
85 offset = 0;
86 end = GUC_NUM_DOORBELLS/2;
87 if (is_high_priority(client)) {
88 offset = end;
89 end += offset;
90 }
91
92 id = find_next_zero_bit(client->guc->doorbell_bitmap, offset, end);
93 if (id == end)
94 return -ENOSPC;
95
96 __set_bit(id, client->guc->doorbell_bitmap);
97 client->doorbell_id = id;
98 DRM_DEBUG_DRIVER("client %u (high prio=%s) reserved doorbell: %d\n",
99 client->ctx_index, yesno(is_high_priority(client)),
100 id);
101 return 0;
102}
103
104static void __unreserve_doorbell(struct i915_guc_client *client)
105{
106 GEM_BUG_ON(client->doorbell_id == GUC_DOORBELL_INVALID);
107
108 __clear_bit(client->doorbell_id, client->guc->doorbell_bitmap);
109 client->doorbell_id = GUC_DOORBELL_INVALID;
110}
111
Dave Gordon44a28b12015-08-12 15:43:41 +0100112/*
Dave Gordon44a28b12015-08-12 15:43:41 +0100113 * Tell the GuC to allocate or deallocate a specific doorbell
114 */
115
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700116static int __guc_allocate_doorbell(struct intel_guc *guc, u32 ctx_index)
Dave Gordon44a28b12015-08-12 15:43:41 +0100117{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100118 u32 action[] = {
119 INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700120 ctx_index
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100121 };
Dave Gordon44a28b12015-08-12 15:43:41 +0100122
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100123 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Dave Gordon44a28b12015-08-12 15:43:41 +0100124}
125
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700126static int __guc_deallocate_doorbell(struct intel_guc *guc, u32 ctx_index)
Dave Gordon44a28b12015-08-12 15:43:41 +0100127{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100128 u32 action[] = {
129 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700130 ctx_index
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100131 };
Dave Gordon44a28b12015-08-12 15:43:41 +0100132
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100133 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Sagar Arun Kamble685534e2016-10-12 21:54:41 +0530134}
135
Dave Gordon44a28b12015-08-12 15:43:41 +0100136/*
137 * Initialise, update, or clear doorbell data shared with the GuC
138 *
139 * These functions modify shared data and so need access to the mapped
140 * client object which contains the page being used for the doorbell
141 */
142
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700143static int __update_doorbell_desc(struct i915_guc_client *client, u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100144{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700145 struct sg_table *sg = client->guc->ctx_pool_vma->pages;
Dave Gordona6674292016-06-13 17:57:32 +0100146 struct guc_context_desc desc;
147 size_t len;
Dave Gordon44a28b12015-08-12 15:43:41 +0100148
Dave Gordona6674292016-06-13 17:57:32 +0100149 /* Update the GuC's idea of the doorbell ID */
150 len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700151 sizeof(desc) * client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100152 if (len != sizeof(desc))
153 return -EFAULT;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700154
Dave Gordona6674292016-06-13 17:57:32 +0100155 desc.db_id = new_id;
156 len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700157 sizeof(desc) * client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100158 if (len != sizeof(desc))
159 return -EFAULT;
160
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700161 return 0;
Dave Gordona6674292016-06-13 17:57:32 +0100162}
163
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700164static struct guc_doorbell_info *__get_doorbell(struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100165{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700166 return client->vaddr + client->doorbell_offset;
167}
168
169static bool has_doorbell(struct i915_guc_client *client)
170{
171 if (client->doorbell_id == GUC_DOORBELL_INVALID)
172 return false;
173
174 return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
175}
176
177static int __create_doorbell(struct i915_guc_client *client)
178{
179 struct guc_doorbell_info *doorbell;
180 int err;
181
182 doorbell = __get_doorbell(client);
183 doorbell->db_status = GUC_DOORBELL_ENABLED;
184 doorbell->cookie = client->doorbell_cookie;
185
186 err = __guc_allocate_doorbell(client->guc, client->ctx_index);
187 if (err) {
188 doorbell->db_status = GUC_DOORBELL_DISABLED;
189 doorbell->cookie = 0;
190 }
191 return err;
192}
193
194static int __destroy_doorbell(struct i915_guc_client *client)
195{
196 struct guc_doorbell_info *doorbell;
197
198 doorbell = __get_doorbell(client);
199 doorbell->db_status = GUC_DOORBELL_DISABLED;
200 doorbell->cookie = 0;
201
202 return __guc_deallocate_doorbell(client->guc, client->ctx_index);
203}
204
205static int destroy_doorbell(struct i915_guc_client *client)
206{
207 int err;
208
209 GEM_BUG_ON(!has_doorbell(client));
Dave Gordon44a28b12015-08-12 15:43:41 +0100210
Dave Gordon44a28b12015-08-12 15:43:41 +0100211 /* XXX: wait for any interrupts */
212 /* XXX: wait for workqueue to drain */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700213
214 err = __destroy_doorbell(client);
215 if (err)
216 return err;
217
218 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
219
220 __unreserve_doorbell(client);
221
222 return 0;
Dave Gordon44a28b12015-08-12 15:43:41 +0100223}
224
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700225static unsigned long __select_cacheline(struct intel_guc* guc)
Dave Gordonf10d69a2016-06-13 17:57:33 +0100226{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700227 unsigned long offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100228
Dave Gordon44a28b12015-08-12 15:43:41 +0100229 /* Doorbell uses a single cache line within a page */
230 offset = offset_in_page(guc->db_cacheline);
231
232 /* Moving to next cache line to reduce contention */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700233 guc->db_cacheline += cache_line_size();
Dave Gordon44a28b12015-08-12 15:43:41 +0100234
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700235 DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
236 offset, guc->db_cacheline, cache_line_size());
Dave Gordon44a28b12015-08-12 15:43:41 +0100237 return offset;
238}
239
Dave Gordon44a28b12015-08-12 15:43:41 +0100240/*
241 * Initialise the process descriptor shared with the GuC firmware.
242 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100243static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100244 struct i915_guc_client *client)
245{
246 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100247
Chris Wilson72aa0d82016-11-02 17:50:47 +0000248 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100249
250 memset(desc, 0, sizeof(*desc));
251
252 /*
253 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
254 * space for ring3 clients (set them as in mmap_ioctl) or kernel
255 * space for kernel clients (map on demand instead? May make debug
256 * easier to have it mapped).
257 */
258 desc->wq_base_addr = 0;
259 desc->db_base_addr = 0;
260
261 desc->context_id = client->ctx_index;
262 desc->wq_size_bytes = client->wq_size;
263 desc->wq_status = WQ_STATUS_ACTIVE;
264 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100265}
266
267/*
268 * Initialise/clear the context descriptor shared with the GuC firmware.
269 *
270 * This descriptor tells the GuC where (in GGTT space) to find the important
271 * data structures relating to this client (doorbell, process descriptor,
272 * write queue, etc).
273 */
274
Dave Gordon7a9347f2016-09-12 21:19:37 +0100275static void guc_ctx_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100276 struct i915_guc_client *client)
277{
Alex Dai397097b2016-01-23 11:58:14 -0800278 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000279 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100280 struct i915_gem_context *ctx = client->owner;
Dave Gordon44a28b12015-08-12 15:43:41 +0100281 struct guc_context_desc desc;
282 struct sg_table *sg;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100283 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100284 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100285
286 memset(&desc, 0, sizeof(desc));
287
288 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
289 desc.context_id = client->ctx_index;
290 desc.priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100291 desc.db_id = client->doorbell_id;
292
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100293 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100294 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100295 uint32_t guc_engine_id = engine->guc_id;
296 struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100297
298 /* TODO: We have a design issue to be solved here. Only when we
299 * receive the first batch, we know which engine is used by the
300 * user. But here GuC expects the lrc and ring to be pinned. It
301 * is not an issue for default context, which is the only one
302 * for now who owns a GuC client. But for future owner of GuC
303 * client, need to make sure lrc is pinned prior to enter here.
304 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100305 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100306 break; /* XXX: continue? */
307
Chris Wilson9021ad02016-05-24 14:53:37 +0100308 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100309
310 /* The state page is after PPHWSP */
Chris Wilson57e88532016-08-15 10:48:57 +0100311 lrc->ring_lcra =
Chris Wilson4741da92016-12-24 19:31:46 +0000312 guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100313 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100314 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100315
Chris Wilson4741da92016-12-24 19:31:46 +0000316 lrc->ring_begin = guc_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100317 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
318 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100319 lrc->ring_current_tail_pointer_value = 0;
320
Dave Gordonc18468c2016-08-09 15:19:22 +0100321 desc.engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100322 }
323
Dave Gordone02757d2016-08-09 15:19:21 +0100324 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
325 client->engines, desc.engines_used);
Alex Daid1675192015-08-12 15:43:43 +0100326 WARN_ON(desc.engines_used == 0);
327
Dave Gordon44a28b12015-08-12 15:43:41 +0100328 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100329 * The doorbell, process descriptor, and workqueue are all parts
330 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100331 */
Chris Wilson4741da92016-12-24 19:31:46 +0000332 gfx_addr = guc_ggtt_offset(client->vma);
Chris Wilson8b797af2016-08-15 10:48:51 +0100333 desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100334 client->doorbell_offset;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700335 desc.db_trigger_cpu = (uintptr_t)__get_doorbell(client);
Dave Gordon86e06cc2016-04-19 16:08:36 +0100336 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
337 desc.process_desc = gfx_addr + client->proc_desc_offset;
338 desc.wq_addr = gfx_addr + client->wq_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100339 desc.wq_size = client->wq_size;
340
341 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100342 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100343 * IsKMDCreatedContext client
344 */
345 desc.desc_private = (uintptr_t)client;
346
347 /* Pool context is pinned already */
Chris Wilson8b797af2016-08-15 10:48:51 +0100348 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100349 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
350 sizeof(desc) * client->ctx_index);
351}
352
Dave Gordon7a9347f2016-09-12 21:19:37 +0100353static void guc_ctx_desc_fini(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100354 struct i915_guc_client *client)
355{
356 struct guc_context_desc desc;
357 struct sg_table *sg;
358
359 memset(&desc, 0, sizeof(desc));
360
Chris Wilson8b797af2016-08-15 10:48:51 +0100361 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100362 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
363 sizeof(desc) * client->ctx_index);
364}
365
Dave Gordon7c2c2702016-05-13 15:36:32 +0100366/**
Dave Gordon7a9347f2016-09-12 21:19:37 +0100367 * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
Dave Gordon7c2c2702016-05-13 15:36:32 +0100368 * @request: request associated with the commands
369 *
370 * Return: 0 if space is available
371 * -EAGAIN if space is not currently available
372 *
373 * This function must be called (and must return 0) before a request
374 * is submitted to the GuC via i915_guc_submit() below. Once a result
Dave Gordon7a9347f2016-09-12 21:19:37 +0100375 * of 0 has been returned, it must be balanced by a corresponding
376 * call to submit().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100377 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100378 * Reservation allows the caller to determine in advance that space
Dave Gordon7c2c2702016-05-13 15:36:32 +0100379 * will be available for the next submission before committing resources
380 * to it, and helps avoid late failures with complicated recovery paths.
381 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100382int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100383{
Dave Gordon551aaec2016-05-13 15:36:33 +0100384 const size_t wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000385 struct i915_guc_client *client = request->i915->guc.execbuf_client;
386 struct guc_process_desc *desc = client->vaddr +
387 client->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100388 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100389 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100390
Chris Wilson349ab912017-02-28 11:28:02 +0000391 spin_lock_irq(&client->wq_lock);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000392 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
393 freespace -= client->wq_rsvd;
Chris Wilsondadd4812016-09-09 14:11:57 +0100394 if (likely(freespace >= wqi_size)) {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000395 client->wq_rsvd += wqi_size;
Chris Wilsondadd4812016-09-09 14:11:57 +0100396 ret = 0;
397 } else {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000398 client->no_wq_space++;
Chris Wilsondadd4812016-09-09 14:11:57 +0100399 ret = -EAGAIN;
400 }
Chris Wilson349ab912017-02-28 11:28:02 +0000401 spin_unlock_irq(&client->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800402
Chris Wilsondadd4812016-09-09 14:11:57 +0100403 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100404}
405
Chris Wilson349ab912017-02-28 11:28:02 +0000406static void guc_client_update_wq_rsvd(struct i915_guc_client *client, int size)
407{
408 unsigned long flags;
409
410 spin_lock_irqsave(&client->wq_lock, flags);
411 client->wq_rsvd += size;
412 spin_unlock_irqrestore(&client->wq_lock, flags);
413}
414
Chris Wilson5ba89902016-10-07 07:53:27 +0100415void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
416{
Chris Wilson349ab912017-02-28 11:28:02 +0000417 const int wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000418 struct i915_guc_client *client = request->i915->guc.execbuf_client;
Chris Wilson5ba89902016-10-07 07:53:27 +0100419
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000420 GEM_BUG_ON(READ_ONCE(client->wq_rsvd) < wqi_size);
Chris Wilson349ab912017-02-28 11:28:02 +0000421 guc_client_update_wq_rsvd(client, -wqi_size);
Chris Wilson5ba89902016-10-07 07:53:27 +0100422}
423
Dave Gordon7a9347f2016-09-12 21:19:37 +0100424/* Construct a Work Item and append it to the GuC's Work Queue */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000425static void guc_wq_item_append(struct i915_guc_client *client,
Dave Gordon7a9347f2016-09-12 21:19:37 +0100426 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100427{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100428 /* wqi_len is in DWords, and does not include the one-word header */
429 const size_t wqi_size = sizeof(struct guc_wq_item);
430 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100431 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100432 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100433 struct guc_wq_item *wqi;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000434 u32 freespace, tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100435
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000436 desc = client->vaddr + client->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800437
Dave Gordon7a9347f2016-09-12 21:19:37 +0100438 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000439 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100440 GEM_BUG_ON(freespace < wqi_size);
441
442 /* The GuC firmware wants the tail index in QWords, not bytes */
443 tail = rq->tail;
444 GEM_BUG_ON(tail & 7);
445 tail >>= 3;
446 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100447
448 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
449 * should not have the case where structure wqi is across page, neither
450 * wrapped to the beginning. This simplifies the implementation below.
451 *
452 * XXX: if not the case, we need save data to a temp wqi and copy it to
453 * workqueue buffer dw by dw.
454 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100455 BUILD_BUG_ON(wqi_size != 16);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000456 GEM_BUG_ON(client->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100457
Dave Gordon0a31afb2016-05-13 15:36:34 +0100458 /* postincrement WQ tail for next time */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000459 wq_off = client->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100460 GEM_BUG_ON(wq_off & (wqi_size - 1));
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000461 client->wq_tail += wqi_size;
462 client->wq_tail &= client->wq_size - 1;
463 client->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100464
465 /* WQ starts from the page after doorbell / process_desc */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000466 wqi = client->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100467
Dave Gordon0a31afb2016-05-13 15:36:34 +0100468 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100469 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100470 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100471 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100472 WQ_NO_WCFLUSH_WAIT;
473
474 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100475 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100476
Dave Gordon44a28b12015-08-12 15:43:41 +0100477 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100478 wqi->fence_id = rq->global_seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100479}
480
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000481static int guc_ring_doorbell(struct i915_guc_client *client)
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100482{
483 struct guc_process_desc *desc;
484 union guc_doorbell_qw db_cmp, db_exc, db_ret;
485 union guc_doorbell_qw *db;
486 int attempt = 2, ret = -EAGAIN;
487
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000488 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100489
490 /* Update the tail so it is visible to GuC */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000491 desc->tail = client->wq_tail;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100492
493 /* current cookie */
494 db_cmp.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000495 db_cmp.cookie = client->doorbell_cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100496
497 /* cookie to be updated */
498 db_exc.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000499 db_exc.cookie = client->doorbell_cookie + 1;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100500 if (db_exc.cookie == 0)
501 db_exc.cookie = 1;
502
503 /* pointer of current doorbell cacheline */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700504 db = (union guc_doorbell_qw *)__get_doorbell(client);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100505
506 while (attempt--) {
507 /* lets ring the doorbell */
508 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
509 db_cmp.value_qw, db_exc.value_qw);
510
511 /* if the exchange was successfully executed */
512 if (db_ret.value_qw == db_cmp.value_qw) {
513 /* db was successfully rung */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000514 client->doorbell_cookie = db_exc.cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100515 ret = 0;
516 break;
517 }
518
519 /* XXX: doorbell was lost and need to acquire it again */
520 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
521 break;
522
Dave Gordon535b2f52016-08-18 18:17:23 +0100523 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
524 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100525
526 /* update the cookie to newly read cookie from GuC */
527 db_cmp.cookie = db_ret.cookie;
528 db_exc.cookie = db_ret.cookie + 1;
529 if (db_exc.cookie == 0)
530 db_exc.cookie = 1;
531 }
532
533 return ret;
534}
535
Dave Gordon44a28b12015-08-12 15:43:41 +0100536/**
Chris Wilson34ba5a82016-11-29 12:10:24 +0000537 * __i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700538 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100539 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100540 * The caller must have already called i915_guc_wq_reserve() above with
541 * a result of 0 (success), guaranteeing that there is space in the work
542 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100543 *
544 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100545 * submit() when _reserve() says there's no space, or calls _submit()
546 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100547 *
548 * The only error here arises if the doorbell hardware isn't functioning
549 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100550 */
Chris Wilson34ba5a82016-11-29 12:10:24 +0000551static void __i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100552{
Akash Goeled4596ea2016-10-25 22:05:23 +0530553 struct drm_i915_private *dev_priv = rq->i915;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000554 struct intel_engine_cs *engine = rq->engine;
555 unsigned int engine_id = engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100556 struct intel_guc *guc = &rq->i915->guc;
557 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilson25afdf892017-03-02 14:53:23 +0000558 unsigned long flags;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100559 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100560
Akash Goeled4596ea2016-10-25 22:05:23 +0530561 /* WA to flush out the pending GMADR writes to ring buffer. */
562 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
563 POSTING_READ_FW(GUC_STATUS);
564
Chris Wilson25afdf892017-03-02 14:53:23 +0000565 spin_lock_irqsave(&client->wq_lock, flags);
Chris Wilson0c335182017-02-28 11:28:03 +0000566
567 guc_wq_item_append(client, rq);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100568 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100569
Alex Dai397097b2016-01-23 11:58:14 -0800570 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100571 client->retcode = b_ret;
572 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100573 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100574
Alex Dai397097b2016-01-23 11:58:14 -0800575 guc->submissions[engine_id] += 1;
Chris Wilson65e47602016-10-28 13:58:49 +0100576 guc->last_seqno[engine_id] = rq->global_seqno;
Chris Wilson0c335182017-02-28 11:28:03 +0000577
Chris Wilson25afdf892017-03-02 14:53:23 +0000578 spin_unlock_irqrestore(&client->wq_lock, flags);
Dave Gordon44a28b12015-08-12 15:43:41 +0100579}
580
Chris Wilson34ba5a82016-11-29 12:10:24 +0000581static void i915_guc_submit(struct drm_i915_gem_request *rq)
582{
Chris Wilson31de7352017-03-16 12:56:18 +0000583 __i915_gem_request_submit(rq);
Chris Wilson34ba5a82016-11-29 12:10:24 +0000584 __i915_guc_submit(rq);
585}
586
Chris Wilson31de7352017-03-16 12:56:18 +0000587static void nested_enable_signaling(struct drm_i915_gem_request *rq)
588{
589 /* If we use dma_fence_enable_sw_signaling() directly, lockdep
590 * detects an ordering issue between the fence lockclass and the
591 * global_timeline. This circular dependency can only occur via 2
592 * different fences (but same fence lockclass), so we use the nesting
593 * annotation here to prevent the warn, equivalent to the nesting
594 * inside i915_gem_request_submit() for when we also enable the
595 * signaler.
596 */
597
598 if (test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
599 &rq->fence.flags))
600 return;
601
602 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags));
603 trace_dma_fence_enable_signal(&rq->fence);
604
605 spin_lock_nested(&rq->lock, SINGLE_DEPTH_NESTING);
606 intel_engine_enable_signaling(rq);
607 spin_unlock(&rq->lock);
608}
609
610static bool i915_guc_dequeue(struct intel_engine_cs *engine)
611{
612 struct execlist_port *port = engine->execlist_port;
613 struct drm_i915_gem_request *last = port[0].request;
Chris Wilson31de7352017-03-16 12:56:18 +0000614 struct rb_node *rb;
615 bool submit = false;
616
Chris Wilson6c943de2017-03-17 12:07:16 +0000617 /* After execlist_first is updated, the tasklet will be rescheduled.
618 *
619 * If we are currently running (inside the tasklet) and a third
620 * party queues a request and so updates engine->execlist_first under
621 * the spinlock (which we have elided), it will atomically set the
622 * TASKLET_SCHED flag causing the us to be re-executed and pick up
623 * the change in state (the update to TASKLET_SCHED incurs a memory
624 * barrier making this cross-cpu checking safe).
625 */
626 if (!READ_ONCE(engine->execlist_first))
627 return false;
628
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000629 spin_lock_irq(&engine->timeline->lock);
Chris Wilson31de7352017-03-16 12:56:18 +0000630 rb = engine->execlist_first;
631 while (rb) {
632 struct drm_i915_gem_request *rq =
633 rb_entry(rb, typeof(*rq), priotree.node);
634
635 if (last && rq->ctx != last->ctx) {
636 if (port != engine->execlist_port)
637 break;
638
639 i915_gem_request_assign(&port->request, last);
640 nested_enable_signaling(last);
641 port++;
642 }
643
644 rb = rb_next(rb);
645 rb_erase(&rq->priotree.node, &engine->execlist_queue);
646 RB_CLEAR_NODE(&rq->priotree.node);
647 rq->priotree.priority = INT_MAX;
648
Chris Wilson31de7352017-03-16 12:56:18 +0000649 i915_guc_submit(rq);
Tvrtko Ursulin66e303e2017-03-20 13:25:56 +0000650 trace_i915_gem_request_in(rq, port - engine->execlist_port);
Chris Wilson31de7352017-03-16 12:56:18 +0000651 last = rq;
652 submit = true;
653 }
654 if (submit) {
655 i915_gem_request_assign(&port->request, last);
656 nested_enable_signaling(last);
657 engine->execlist_first = rb;
658 }
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000659 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson31de7352017-03-16 12:56:18 +0000660
661 return submit;
662}
663
664static void i915_guc_irq_handler(unsigned long data)
665{
666 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
667 struct execlist_port *port = engine->execlist_port;
668 struct drm_i915_gem_request *rq;
669 bool submit;
670
671 do {
672 rq = port[0].request;
673 while (rq && i915_gem_request_completed(rq)) {
674 trace_i915_gem_request_out(rq);
675 i915_gem_request_put(rq);
676 port[0].request = port[1].request;
677 port[1].request = NULL;
678 rq = port[0].request;
679 }
680
681 submit = false;
682 if (!port[1].request)
683 submit = i915_guc_dequeue(engine);
684 } while (submit);
685}
686
Dave Gordon44a28b12015-08-12 15:43:41 +0100687/*
688 * Everything below here is concerned with setup & teardown, and is
689 * therefore not part of the somewhat time-critical batch-submission
690 * path of i915_guc_submit() above.
691 */
692
693/**
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000694 * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
Chris Wilson8b797af2016-08-15 10:48:51 +0100695 * @guc: the guc
696 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100697 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100698 * This is a wrapper to create an object for use with the GuC. In order to
699 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
700 * both some backing storage and a range inside the Global GTT. We must pin
701 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
702 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100703 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100704 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100705 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000706struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100707{
Chris Wilson8b797af2016-08-15 10:48:51 +0100708 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100709 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100710 struct i915_vma *vma;
711 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100712
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000713 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100714 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100715 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100716
Chris Wilsona01cb372017-01-16 15:21:30 +0000717 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson8b797af2016-08-15 10:48:51 +0100718 if (IS_ERR(vma))
719 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100720
Chris Wilson8b797af2016-08-15 10:48:51 +0100721 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
722 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
723 if (ret) {
724 vma = ERR_PTR(ret);
725 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100726 }
727
Chris Wilson8b797af2016-08-15 10:48:51 +0100728 return vma;
729
730err:
731 i915_gem_object_put(obj);
732 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100733}
734
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700735static void guc_client_free(struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100736{
Dave Gordon44a28b12015-08-12 15:43:41 +0100737 /*
738 * XXX: wait for any outstanding submissions before freeing memory.
739 * Be sure to drop any locks
740 */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700741 guc_ctx_desc_fini(client->guc, client);
742 i915_gem_object_unpin_map(client->vma->obj);
Chris Wilson19880c42016-08-15 10:49:05 +0100743 i915_vma_unpin_and_release(&client->vma);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700744 ida_simple_remove(&client->guc->ctx_ids, client->ctx_index);
Dave Gordon44a28b12015-08-12 15:43:41 +0100745 kfree(client);
746}
747
Dave Gordon84b7f882016-08-09 15:19:20 +0100748/* Check that a doorbell register is in the expected state */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700749static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
Dave Gordon84b7f882016-08-09 15:19:20 +0100750{
751 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700752 u32 drbregl;
753 bool valid;
Dave Gordon84b7f882016-08-09 15:19:20 +0100754
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700755 GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
756
757 drbregl = I915_READ(GEN8_DRBREGL(db_id));
758 valid = drbregl & GEN8_DRB_VALID;
759
760 if (test_bit(db_id, guc->doorbell_bitmap) == valid)
Dave Gordon84b7f882016-08-09 15:19:20 +0100761 return true;
762
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700763 DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
764 db_id, drbregl, yesno(valid));
Dave Gordon84b7f882016-08-09 15:19:20 +0100765
766 return false;
767}
768
Dave Gordon4d757872016-06-13 17:57:34 +0100769/*
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700770 * If the GuC thinks that the doorbell is unassigned (e.g. because we reset and
771 * reloaded the GuC FW) we can use this function to tell the GuC to reassign the
772 * doorbell to the rightful owner.
773 */
774static int __reset_doorbell(struct i915_guc_client* client, u16 db_id)
775{
776 int err;
777
778 err = __update_doorbell_desc(client, db_id);
779 if (!err)
780 err = __create_doorbell(client);
781 if (!err)
782 err = __destroy_doorbell(client);
783
784 return err;
785}
786
787/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100788 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100789 * in turn, to ensure that all doorbell h/w is (re)initialised.
790 */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700791static int guc_init_doorbell_hw(struct intel_guc *guc)
Dave Gordon4d757872016-06-13 17:57:34 +0100792{
Dave Gordon4d757872016-06-13 17:57:34 +0100793 struct i915_guc_client *client = guc->execbuf_client;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700794 int err;
795 int i;
Dave Gordon4d757872016-06-13 17:57:34 +0100796
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700797 if (has_doorbell(client))
798 destroy_doorbell(client);
Dave Gordon4d757872016-06-13 17:57:34 +0100799
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700800 for (i = 0; i < GUC_NUM_DOORBELLS; ++i) {
801 if (doorbell_ok(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100802 continue;
803
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700804 err = __reset_doorbell(client, i);
805 WARN(err, "Doorbell %d reset failed, err %d\n", i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100806 }
807
Dave Gordon84b7f882016-08-09 15:19:20 +0100808 /* Read back & verify all doorbell registers */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700809 for (i = 0; i < GUC_NUM_DOORBELLS; ++i)
810 WARN_ON(!doorbell_ok(guc, i));
811
812 err = __reserve_doorbell(client);
813 if (err)
814 return err;
815
816 err = __update_doorbell_desc(client, client->doorbell_id);
817 if (err)
818 goto err_reserve;
819
820 err = __create_doorbell(client);
821 if (err)
822 goto err_update;
823
824 return 0;
825err_reserve:
826 __unreserve_doorbell(client);
827err_update:
828 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
829 return err;
Dave Gordon4d757872016-06-13 17:57:34 +0100830}
831
Dave Gordon44a28b12015-08-12 15:43:41 +0100832/**
833 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100834 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100835 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100836 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
837 * The kernel client to replace ExecList submission is created with
838 * NORMAL priority. Priority of a client for scheduler can be HIGH,
839 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700840 * @ctx: the context that owns the client (we use the default render
841 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100842 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100843 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100844 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100845static struct i915_guc_client *
846guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100847 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100848 uint32_t priority,
849 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100850{
851 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100852 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100853 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000854 void *vaddr;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700855 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100856
857 client = kzalloc(sizeof(*client), GFP_KERNEL);
858 if (!client)
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700859 return ERR_PTR(-ENOMEM);
Dave Gordon44a28b12015-08-12 15:43:41 +0100860
Dave Gordon44a28b12015-08-12 15:43:41 +0100861 client->guc = guc;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700862 client->owner = ctx;
Dave Gordone02757d2016-08-09 15:19:21 +0100863 client->engines = engines;
864 client->priority = priority;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700865 client->doorbell_id = GUC_DOORBELL_INVALID;
866 client->wq_offset = GUC_DB_SIZE;
867 client->wq_size = GUC_WQ_SIZE;
868 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100869
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700870 ret = ida_simple_get(&guc->ctx_ids, 0, GUC_MAX_GPU_CONTEXTS,
871 GFP_KERNEL);
872 if (ret < 0)
873 goto err_client;
874
875 client->ctx_index = ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100876
877 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000878 vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700879 if (IS_ERR(vma)) {
880 ret = PTR_ERR(vma);
881 goto err_id;
882 }
Dave Gordon44a28b12015-08-12 15:43:41 +0100883
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100884 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100885 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000886
887 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700888 if (IS_ERR(vaddr)) {
889 ret = PTR_ERR(vaddr);
890 goto err_vma;
891 }
Chris Wilson72aa0d82016-11-02 17:50:47 +0000892 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100893
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700894 client->doorbell_offset = __select_cacheline(guc);
Dave Gordon44a28b12015-08-12 15:43:41 +0100895
896 /*
897 * Since the doorbell only requires a single cacheline, we can save
898 * space by putting the application process descriptor in the same
899 * page. Use the half of the page that doesn't include the doorbell.
900 */
901 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
902 client->proc_desc_offset = 0;
903 else
904 client->proc_desc_offset = (GUC_DB_SIZE / 2);
905
Dave Gordon7a9347f2016-09-12 21:19:37 +0100906 guc_proc_desc_init(guc, client);
907 guc_ctx_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000908
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700909 /* FIXME: Runtime client allocation (which currently we don't do) will
910 * require that the doorbell gets created now. The static execbuf_client
911 * is now getting its doorbell later (on submission enable) but maybe we
912 * also want to reorder things in the future so that we don't have to
913 * special case the doorbell creation */
Dave Gordon44a28b12015-08-12 15:43:41 +0100914
Dave Gordone02757d2016-08-09 15:19:21 +0100915 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700916 priority, client, client->engines, client->ctx_index);
917 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
918 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100919
920 return client;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700921err_vma:
922 i915_vma_unpin_and_release(&client->vma);
923err_id:
924 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
925err_client:
926 kfree(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100927
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700928 return ERR_PTR(ret);
Dave Gordon44a28b12015-08-12 15:43:41 +0100929}
930
Dave Gordon7a9347f2016-09-12 21:19:37 +0100931static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -0800932{
933 struct guc_policy *policy;
934 u32 p, i;
935
936 policies->dpc_promote_time = 500000;
937 policies->max_num_work_items = POLICY_MAX_NUM_WI;
938
939 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -0800940 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -0800941 policy = &policies->policy[p][i];
942
943 policy->execution_quantum = 1000000;
944 policy->preemption_time = 500000;
945 policy->fault_time = 250000;
946 policy->policy_flags = 0;
947 }
948 }
949
950 policies->is_valid = 1;
951}
952
Dave Gordon7a9347f2016-09-12 21:19:37 +0100953static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -0800954{
955 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +0100956 struct i915_vma *vma;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000957 struct page *page;
958 /* The ads obj includes the struct itself and buffers passed to GuC */
959 struct {
960 struct guc_ads ads;
961 struct guc_policies policies;
962 struct guc_mmio_reg_state reg_state;
963 u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
964 } __packed *blob;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000965 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530966 enum intel_engine_id id;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000967 u32 base;
Alex Dai68371a92015-12-18 12:00:09 -0800968
Chris Wilson8b797af2016-08-15 10:48:51 +0100969 vma = guc->ads_vma;
970 if (!vma) {
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000971 vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
Chris Wilson8b797af2016-08-15 10:48:51 +0100972 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -0800973 return;
974
Chris Wilson8b797af2016-08-15 10:48:51 +0100975 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -0800976 }
977
Chris Wilson8b797af2016-08-15 10:48:51 +0100978 page = i915_vma_first_page(vma);
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000979 blob = kmap(page);
980
981 /* GuC scheduling policies */
982 guc_policies_init(&blob->policies);
983
984 /* MMIO reg state */
985 for_each_engine(engine, dev_priv, id) {
986 blob->reg_state.mmio_white_list[engine->guc_id].mmio_start =
987 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
988
989 /* Nothing to be saved or restored for now. */
990 blob->reg_state.mmio_white_list[engine->guc_id].count = 0;
991 }
Alex Dai68371a92015-12-18 12:00:09 -0800992
993 /*
994 * The GuC requires a "Golden Context" when it reinitialises
995 * engines after a reset. Here we use the Render ring default
996 * context, which must already exist and be pinned in the GGTT,
997 * so its address won't change after we've told the GuC where
998 * to find it.
999 */
Michal Wajdeczko16f11f42017-03-14 13:33:09 +00001000 blob->ads.golden_context_lrca =
1001 dev_priv->engine[RCS]->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -08001002
Akash Goel3b3f1652016-10-13 22:44:48 +05301003 for_each_engine(engine, dev_priv, id)
Michal Wajdeczko16f11f42017-03-14 13:33:09 +00001004 blob->ads.eng_state_size[engine->guc_id] =
1005 intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -08001006
Michal Wajdeczko16f11f42017-03-14 13:33:09 +00001007 base = guc_ggtt_offset(vma);
1008 blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
1009 blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
1010 blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
Alex Dai5c148e02015-12-18 12:00:11 -08001011
Alex Dai68371a92015-12-18 12:00:09 -08001012 kunmap(page);
1013}
1014
Alex Daibac427f2015-08-12 15:43:39 +01001015/*
1016 * Set up the memory resources to be shared with the GuC. At this point,
1017 * we require just one object that can be mapped through the GGTT.
1018 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001019int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001020{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001021 const size_t ctxsize = sizeof(struct guc_context_desc);
1022 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1023 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001024 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001025 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +01001026
Chris Wilson4d357af2016-11-29 12:10:23 +00001027 if (!HAS_GUC_SCHED(dev_priv))
1028 return 0;
1029
Dave Gordon29fb72c2016-06-07 09:14:50 +01001030 /* Wipe bitmap & delete client in case of reinitialisation */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001031 bitmap_clear(guc->doorbell_bitmap, 0, GUC_NUM_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001032 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001033
Alex Daibac427f2015-08-12 15:43:39 +01001034 if (!i915.enable_guc_submission)
1035 return 0; /* not enabled */
1036
Chris Wilson8b797af2016-08-15 10:48:51 +01001037 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001038 return 0; /* already allocated */
1039
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001040 vma = intel_guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001041 if (IS_ERR(vma))
1042 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001043
Chris Wilson8b797af2016-08-15 10:48:51 +01001044 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001045 ida_init(&guc->ctx_ids);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001046 intel_guc_log_create(guc);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001047 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001048
Chris Wilson4d357af2016-11-29 12:10:23 +00001049 guc->execbuf_client = guc_client_alloc(dev_priv,
1050 INTEL_INFO(dev_priv)->ring_mask,
1051 GUC_CTX_PRIORITY_KMD_NORMAL,
1052 dev_priv->kernel_context);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001053 if (IS_ERR(guc->execbuf_client)) {
Chris Wilson4d357af2016-11-29 12:10:23 +00001054 DRM_ERROR("Failed to create GuC client for execbuf!\n");
1055 goto err;
1056 }
1057
Alex Daibac427f2015-08-12 15:43:39 +01001058 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +00001059
1060err:
1061 i915_guc_submission_fini(dev_priv);
1062 return -ENOMEM;
1063}
1064
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001065static void guc_reset_wq(struct i915_guc_client *client)
Chris Wilson4d357af2016-11-29 12:10:23 +00001066{
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001067 struct guc_process_desc *desc = client->vaddr +
1068 client->proc_desc_offset;
Chris Wilson4d357af2016-11-29 12:10:23 +00001069
1070 desc->head = 0;
1071 desc->tail = 0;
1072
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001073 client->wq_tail = 0;
Alex Daibac427f2015-08-12 15:43:39 +01001074}
1075
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001076static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
1077{
1078 struct intel_engine_cs *engine;
1079 enum intel_engine_id id;
1080 int irqs;
1081
1082 /* tell all command streamers to forward interrupts (but not vblank) to GuC */
1083 irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
1084 for_each_engine(engine, dev_priv, id)
1085 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1086
1087 /* route USER_INTERRUPT to Host, all others are sent to GuC. */
1088 irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
1089 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
1090 /* These three registers have the same bit definitions */
1091 I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
1092 I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
1093 I915_WRITE(GUC_WD_VECS_IER, ~irqs);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301094
1095 /*
1096 * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
1097 * (unmasked) PM interrupts to the GuC. All other bits of this
1098 * register *disable* generation of a specific interrupt.
1099 *
1100 * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
1101 * writing to the PM interrupt mask register, i.e. interrupts
1102 * that must not be disabled.
1103 *
1104 * If the GuC is handling these interrupts, then we must not let
1105 * the PM code disable ANY interrupt that the GuC is expecting.
1106 * So for each ENABLED (0) bit in this register, we must SET the
1107 * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
1108 * GuC needs ARAT expired interrupt unmasked hence it is set in
1109 * pm_intrmsk_mbz.
1110 *
1111 * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1112 * result in the register bit being left SET!
1113 */
1114 dev_priv->rps.pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
Chris Wilson655d49e2017-03-12 13:27:45 +00001115 dev_priv->rps.pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001116}
1117
Dave Gordonbeffa512016-06-10 18:29:26 +01001118int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001119{
Dave Gordon44a28b12015-08-12 15:43:41 +01001120 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001121 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001122 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301123 enum intel_engine_id id;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001124 int err;
Dave Gordon44a28b12015-08-12 15:43:41 +01001125
Chris Wilson4d357af2016-11-29 12:10:23 +00001126 if (!client)
1127 return -ENODEV;
Dave Gordon44a28b12015-08-12 15:43:41 +01001128
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001129 err = intel_guc_sample_forcewake(guc);
1130 if (err)
1131 return err;
Chris Wilson4d357af2016-11-29 12:10:23 +00001132
1133 guc_reset_wq(client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001134 err = guc_init_doorbell_hw(guc);
1135 if (err)
1136 return err;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001137
Chris Wilsonddd66c52016-08-02 22:50:31 +01001138 /* Take over from manual control of ELSP (execlists) */
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001139 guc_interrupts_capture(dev_priv);
1140
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001141 for_each_engine(engine, dev_priv, id) {
Chris Wilson349ab912017-02-28 11:28:02 +00001142 const int wqi_size = sizeof(struct guc_wq_item);
Chris Wilson4d357af2016-11-29 12:10:23 +00001143 struct drm_i915_gem_request *rq;
1144
Chris Wilson31de7352017-03-16 12:56:18 +00001145 /* The tasklet was initialised by execlists, and may be in
1146 * a state of flux (across a reset) and so we just want to
1147 * take over the callback without changing any other state
1148 * in the tasklet.
1149 */
1150 engine->irq_tasklet.func = i915_guc_irq_handler;
1151 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1152
1153 /* Replay the current set of previously submitted requests */
Chris Wilson349ab912017-02-28 11:28:02 +00001154 spin_lock_irq(&engine->timeline->lock);
Chris Wilson4d357af2016-11-29 12:10:23 +00001155 list_for_each_entry(rq, &engine->timeline->requests, link) {
Chris Wilson349ab912017-02-28 11:28:02 +00001156 guc_client_update_wq_rsvd(client, wqi_size);
Chris Wilson34ba5a82016-11-29 12:10:24 +00001157 __i915_guc_submit(rq);
Chris Wilsondadd4812016-09-09 14:11:57 +01001158 }
Chris Wilson349ab912017-02-28 11:28:02 +00001159 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001160 }
1161
Dave Gordon44a28b12015-08-12 15:43:41 +01001162 return 0;
1163}
1164
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301165static void guc_interrupts_release(struct drm_i915_private *dev_priv)
1166{
1167 struct intel_engine_cs *engine;
1168 enum intel_engine_id id;
1169 int irqs;
1170
1171 /*
1172 * tell all command streamers NOT to forward interrupts or vblank
1173 * to GuC.
1174 */
1175 irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
1176 irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
1177 for_each_engine(engine, dev_priv, id)
1178 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1179
1180 /* route all GT interrupts to the host */
1181 I915_WRITE(GUC_BCS_RCS_IER, 0);
1182 I915_WRITE(GUC_VCS2_VCS1_IER, 0);
1183 I915_WRITE(GUC_WD_VECS_IER, 0);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301184
Chris Wilson655d49e2017-03-12 13:27:45 +00001185 dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301186 dev_priv->rps.pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301187}
1188
Dave Gordonbeffa512016-06-10 18:29:26 +01001189void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001190{
Dave Gordon44a28b12015-08-12 15:43:41 +01001191 struct intel_guc *guc = &dev_priv->guc;
1192
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301193 guc_interrupts_release(dev_priv);
1194
Chris Wilsonddd66c52016-08-02 22:50:31 +01001195 if (!guc->execbuf_client)
1196 return;
1197
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001198 /* FIXME: in many cases, by the time we get here the GuC has been
1199 * reset, so we cannot destroy the doorbell properly. Ignore the
1200 * error message for now */
1201 destroy_doorbell(guc->execbuf_client);
1202
Chris Wilsonddd66c52016-08-02 22:50:31 +01001203 /* Revert back to manual ELSP submission */
Chris Wilsonff44ad52017-03-16 17:13:03 +00001204 intel_engines_reset_default_submission(dev_priv);
Dave Gordon44a28b12015-08-12 15:43:41 +01001205}
1206
Dave Gordonbeffa512016-06-10 18:29:26 +01001207void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001208{
Alex Daibac427f2015-08-12 15:43:39 +01001209 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001210 struct i915_guc_client *client;
1211
1212 client = fetch_and_zero(&guc->execbuf_client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001213 if (client && !IS_ERR(client))
1214 guc_client_free(client);
Alex Daibac427f2015-08-12 15:43:39 +01001215
Chris Wilson19880c42016-08-15 10:49:05 +01001216 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301217 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001218
Chris Wilson8b797af2016-08-15 10:48:51 +01001219 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001220 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001221 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001222}
Alex Daia1c41992015-09-30 09:46:37 -07001223
1224/**
1225 * intel_guc_suspend() - notify GuC entering suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001226 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001227 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001228int intel_guc_suspend(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001229{
Alex Daia1c41992015-09-30 09:46:37 -07001230 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001231 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001232 u32 data[3];
1233
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001234 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001235 return 0;
1236
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301237 gen9_disable_guc_interrupts(dev_priv);
1238
Dave Gordoned54c1a2016-01-19 19:02:54 +00001239 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001240
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001241 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001242 /* any value greater than GUC_POWER_D0 */
1243 data[1] = GUC_POWER_D1;
1244 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001245 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001246
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001247 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001248}
1249
1250
1251/**
1252 * intel_guc_resume() - notify GuC resuming from suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001253 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001254 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001255int intel_guc_resume(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001256{
Alex Daia1c41992015-09-30 09:46:37 -07001257 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001258 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001259 u32 data[3];
1260
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001261 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001262 return 0;
1263
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301264 if (i915.guc_log_level >= 0)
1265 gen9_enable_guc_interrupts(dev_priv);
1266
Dave Gordoned54c1a2016-01-19 19:02:54 +00001267 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001268
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001269 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001270 data[1] = GUC_POWER_D0;
1271 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001272 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001273
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001274 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001275}