blob: 9ba69276754bea3b45debe45399cc8ade183893d [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
Oscar Mateo73b05532017-03-22 10:39:45 -0700136static struct guc_context_desc *__get_context_desc(struct i915_guc_client *client)
137{
138 struct guc_context_desc *base = client->guc->ctx_pool_vaddr;
139
140 return &base[client->ctx_index];
141}
142
Dave Gordon44a28b12015-08-12 15:43:41 +0100143/*
144 * Initialise, update, or clear doorbell data shared with the GuC
145 *
146 * These functions modify shared data and so need access to the mapped
147 * client object which contains the page being used for the doorbell
148 */
149
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700150static int __update_doorbell_desc(struct i915_guc_client *client, u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100151{
Oscar Mateo73b05532017-03-22 10:39:45 -0700152 struct guc_context_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100153
Dave Gordona6674292016-06-13 17:57:32 +0100154 /* Update the GuC's idea of the doorbell ID */
Oscar Mateo73b05532017-03-22 10:39:45 -0700155 desc = __get_context_desc(client);
156 desc->db_id = new_id;
Dave Gordona6674292016-06-13 17:57:32 +0100157
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700158 return 0;
Dave Gordona6674292016-06-13 17:57:32 +0100159}
160
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700161static struct guc_doorbell_info *__get_doorbell(struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100162{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700163 return client->vaddr + client->doorbell_offset;
164}
165
166static bool has_doorbell(struct i915_guc_client *client)
167{
168 if (client->doorbell_id == GUC_DOORBELL_INVALID)
169 return false;
170
171 return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
172}
173
174static int __create_doorbell(struct i915_guc_client *client)
175{
176 struct guc_doorbell_info *doorbell;
177 int err;
178
179 doorbell = __get_doorbell(client);
180 doorbell->db_status = GUC_DOORBELL_ENABLED;
181 doorbell->cookie = client->doorbell_cookie;
182
183 err = __guc_allocate_doorbell(client->guc, client->ctx_index);
184 if (err) {
185 doorbell->db_status = GUC_DOORBELL_DISABLED;
186 doorbell->cookie = 0;
187 }
188 return err;
189}
190
191static int __destroy_doorbell(struct i915_guc_client *client)
192{
193 struct guc_doorbell_info *doorbell;
194
195 doorbell = __get_doorbell(client);
196 doorbell->db_status = GUC_DOORBELL_DISABLED;
197 doorbell->cookie = 0;
198
199 return __guc_deallocate_doorbell(client->guc, client->ctx_index);
200}
201
202static int destroy_doorbell(struct i915_guc_client *client)
203{
204 int err;
205
206 GEM_BUG_ON(!has_doorbell(client));
Dave Gordon44a28b12015-08-12 15:43:41 +0100207
Dave Gordon44a28b12015-08-12 15:43:41 +0100208 /* XXX: wait for any interrupts */
209 /* XXX: wait for workqueue to drain */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700210
211 err = __destroy_doorbell(client);
212 if (err)
213 return err;
214
215 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
216
217 __unreserve_doorbell(client);
218
219 return 0;
Dave Gordon44a28b12015-08-12 15:43:41 +0100220}
221
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700222static unsigned long __select_cacheline(struct intel_guc* guc)
Dave Gordonf10d69a2016-06-13 17:57:33 +0100223{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700224 unsigned long offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100225
Dave Gordon44a28b12015-08-12 15:43:41 +0100226 /* Doorbell uses a single cache line within a page */
227 offset = offset_in_page(guc->db_cacheline);
228
229 /* Moving to next cache line to reduce contention */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700230 guc->db_cacheline += cache_line_size();
Dave Gordon44a28b12015-08-12 15:43:41 +0100231
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700232 DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
233 offset, guc->db_cacheline, cache_line_size());
Dave Gordon44a28b12015-08-12 15:43:41 +0100234 return offset;
235}
236
Dave Gordon44a28b12015-08-12 15:43:41 +0100237/*
238 * Initialise the process descriptor shared with the GuC firmware.
239 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100240static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100241 struct i915_guc_client *client)
242{
243 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100244
Chris Wilson72aa0d82016-11-02 17:50:47 +0000245 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100246
247 memset(desc, 0, sizeof(*desc));
248
249 /*
250 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
251 * space for ring3 clients (set them as in mmap_ioctl) or kernel
252 * space for kernel clients (map on demand instead? May make debug
253 * easier to have it mapped).
254 */
255 desc->wq_base_addr = 0;
256 desc->db_base_addr = 0;
257
258 desc->context_id = client->ctx_index;
259 desc->wq_size_bytes = client->wq_size;
260 desc->wq_status = WQ_STATUS_ACTIVE;
261 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100262}
263
264/*
265 * Initialise/clear the context descriptor shared with the GuC firmware.
266 *
267 * This descriptor tells the GuC where (in GGTT space) to find the important
268 * data structures relating to this client (doorbell, process descriptor,
269 * write queue, etc).
270 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100271static void guc_ctx_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100272 struct i915_guc_client *client)
273{
Alex Dai397097b2016-01-23 11:58:14 -0800274 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000275 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100276 struct i915_gem_context *ctx = client->owner;
Oscar Mateo73b05532017-03-22 10:39:45 -0700277 struct guc_context_desc *desc;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100278 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100279 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100280
Oscar Mateo73b05532017-03-22 10:39:45 -0700281 desc = __get_context_desc(client);
282 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100283
Oscar Mateo73b05532017-03-22 10:39:45 -0700284 desc->attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
285 desc->context_id = client->ctx_index;
286 desc->priority = client->priority;
287 desc->db_id = client->doorbell_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100288
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100289 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100290 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100291 uint32_t guc_engine_id = engine->guc_id;
Oscar Mateo73b05532017-03-22 10:39:45 -0700292 struct guc_execlist_context *lrc = &desc->lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100293
294 /* TODO: We have a design issue to be solved here. Only when we
295 * receive the first batch, we know which engine is used by the
296 * user. But here GuC expects the lrc and ring to be pinned. It
297 * is not an issue for default context, which is the only one
298 * for now who owns a GuC client. But for future owner of GuC
299 * client, need to make sure lrc is pinned prior to enter here.
300 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100301 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100302 break; /* XXX: continue? */
303
Chris Wilson9021ad02016-05-24 14:53:37 +0100304 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100305
306 /* The state page is after PPHWSP */
Chris Wilson57e88532016-08-15 10:48:57 +0100307 lrc->ring_lcra =
Chris Wilson4741da92016-12-24 19:31:46 +0000308 guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100309 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100310 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100311
Chris Wilson4741da92016-12-24 19:31:46 +0000312 lrc->ring_begin = guc_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100313 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
314 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100315 lrc->ring_current_tail_pointer_value = 0;
316
Oscar Mateo73b05532017-03-22 10:39:45 -0700317 desc->engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100318 }
319
Dave Gordone02757d2016-08-09 15:19:21 +0100320 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
Oscar Mateo73b05532017-03-22 10:39:45 -0700321 client->engines, desc->engines_used);
322 WARN_ON(desc->engines_used == 0);
Alex Daid1675192015-08-12 15:43:43 +0100323
Dave Gordon44a28b12015-08-12 15:43:41 +0100324 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100325 * The doorbell, process descriptor, and workqueue are all parts
326 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100327 */
Chris Wilson4741da92016-12-24 19:31:46 +0000328 gfx_addr = guc_ggtt_offset(client->vma);
Oscar Mateo73b05532017-03-22 10:39:45 -0700329 desc->db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100330 client->doorbell_offset;
Oscar Mateo73b05532017-03-22 10:39:45 -0700331 desc->db_trigger_cpu = (uintptr_t)__get_doorbell(client);
332 desc->db_trigger_uk = gfx_addr + client->doorbell_offset;
333 desc->process_desc = gfx_addr + client->proc_desc_offset;
334 desc->wq_addr = gfx_addr + client->wq_offset;
335 desc->wq_size = client->wq_size;
Dave Gordon44a28b12015-08-12 15:43:41 +0100336
337 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100338 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100339 * IsKMDCreatedContext client
340 */
Oscar Mateo73b05532017-03-22 10:39:45 -0700341 desc->desc_private = (uintptr_t)client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100342}
343
Dave Gordon7a9347f2016-09-12 21:19:37 +0100344static void guc_ctx_desc_fini(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100345 struct i915_guc_client *client)
346{
Oscar Mateo73b05532017-03-22 10:39:45 -0700347 struct guc_context_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100348
Oscar Mateo73b05532017-03-22 10:39:45 -0700349 desc = __get_context_desc(client);
350 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100351}
352
Dave Gordon7c2c2702016-05-13 15:36:32 +0100353/**
Dave Gordon7a9347f2016-09-12 21:19:37 +0100354 * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
Dave Gordon7c2c2702016-05-13 15:36:32 +0100355 * @request: request associated with the commands
356 *
357 * Return: 0 if space is available
358 * -EAGAIN if space is not currently available
359 *
360 * This function must be called (and must return 0) before a request
361 * is submitted to the GuC via i915_guc_submit() below. Once a result
Dave Gordon7a9347f2016-09-12 21:19:37 +0100362 * of 0 has been returned, it must be balanced by a corresponding
363 * call to submit().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100364 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100365 * Reservation allows the caller to determine in advance that space
Dave Gordon7c2c2702016-05-13 15:36:32 +0100366 * will be available for the next submission before committing resources
367 * to it, and helps avoid late failures with complicated recovery paths.
368 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100369int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100370{
Dave Gordon551aaec2016-05-13 15:36:33 +0100371 const size_t wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000372 struct i915_guc_client *client = request->i915->guc.execbuf_client;
373 struct guc_process_desc *desc = client->vaddr +
374 client->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100375 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100376 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100377
Chris Wilson349ab912017-02-28 11:28:02 +0000378 spin_lock_irq(&client->wq_lock);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000379 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
380 freespace -= client->wq_rsvd;
Chris Wilsondadd4812016-09-09 14:11:57 +0100381 if (likely(freespace >= wqi_size)) {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000382 client->wq_rsvd += wqi_size;
Chris Wilsondadd4812016-09-09 14:11:57 +0100383 ret = 0;
384 } else {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000385 client->no_wq_space++;
Chris Wilsondadd4812016-09-09 14:11:57 +0100386 ret = -EAGAIN;
387 }
Chris Wilson349ab912017-02-28 11:28:02 +0000388 spin_unlock_irq(&client->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800389
Chris Wilsondadd4812016-09-09 14:11:57 +0100390 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100391}
392
Chris Wilson349ab912017-02-28 11:28:02 +0000393static void guc_client_update_wq_rsvd(struct i915_guc_client *client, int size)
394{
395 unsigned long flags;
396
397 spin_lock_irqsave(&client->wq_lock, flags);
398 client->wq_rsvd += size;
399 spin_unlock_irqrestore(&client->wq_lock, flags);
400}
401
Chris Wilson5ba89902016-10-07 07:53:27 +0100402void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
403{
Chris Wilson349ab912017-02-28 11:28:02 +0000404 const int wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000405 struct i915_guc_client *client = request->i915->guc.execbuf_client;
Chris Wilson5ba89902016-10-07 07:53:27 +0100406
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000407 GEM_BUG_ON(READ_ONCE(client->wq_rsvd) < wqi_size);
Chris Wilson349ab912017-02-28 11:28:02 +0000408 guc_client_update_wq_rsvd(client, -wqi_size);
Chris Wilson5ba89902016-10-07 07:53:27 +0100409}
410
Dave Gordon7a9347f2016-09-12 21:19:37 +0100411/* Construct a Work Item and append it to the GuC's Work Queue */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000412static void guc_wq_item_append(struct i915_guc_client *client,
Dave Gordon7a9347f2016-09-12 21:19:37 +0100413 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100414{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100415 /* wqi_len is in DWords, and does not include the one-word header */
416 const size_t wqi_size = sizeof(struct guc_wq_item);
417 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100418 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100419 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100420 struct guc_wq_item *wqi;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000421 u32 freespace, tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100422
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000423 desc = client->vaddr + client->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800424
Dave Gordon7a9347f2016-09-12 21:19:37 +0100425 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000426 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100427 GEM_BUG_ON(freespace < wqi_size);
428
429 /* The GuC firmware wants the tail index in QWords, not bytes */
430 tail = rq->tail;
431 GEM_BUG_ON(tail & 7);
432 tail >>= 3;
433 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100434
435 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
436 * should not have the case where structure wqi is across page, neither
437 * wrapped to the beginning. This simplifies the implementation below.
438 *
439 * XXX: if not the case, we need save data to a temp wqi and copy it to
440 * workqueue buffer dw by dw.
441 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100442 BUILD_BUG_ON(wqi_size != 16);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000443 GEM_BUG_ON(client->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100444
Dave Gordon0a31afb2016-05-13 15:36:34 +0100445 /* postincrement WQ tail for next time */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000446 wq_off = client->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100447 GEM_BUG_ON(wq_off & (wqi_size - 1));
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000448 client->wq_tail += wqi_size;
449 client->wq_tail &= client->wq_size - 1;
450 client->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100451
452 /* WQ starts from the page after doorbell / process_desc */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000453 wqi = client->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100454
Dave Gordon0a31afb2016-05-13 15:36:34 +0100455 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100456 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100457 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100458 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100459 WQ_NO_WCFLUSH_WAIT;
460
461 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100462 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100463
Dave Gordon44a28b12015-08-12 15:43:41 +0100464 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100465 wqi->fence_id = rq->global_seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100466}
467
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000468static int guc_ring_doorbell(struct i915_guc_client *client)
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100469{
470 struct guc_process_desc *desc;
471 union guc_doorbell_qw db_cmp, db_exc, db_ret;
472 union guc_doorbell_qw *db;
473 int attempt = 2, ret = -EAGAIN;
474
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000475 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100476
477 /* Update the tail so it is visible to GuC */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000478 desc->tail = client->wq_tail;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100479
480 /* current cookie */
481 db_cmp.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000482 db_cmp.cookie = client->doorbell_cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100483
484 /* cookie to be updated */
485 db_exc.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000486 db_exc.cookie = client->doorbell_cookie + 1;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100487 if (db_exc.cookie == 0)
488 db_exc.cookie = 1;
489
490 /* pointer of current doorbell cacheline */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700491 db = (union guc_doorbell_qw *)__get_doorbell(client);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100492
493 while (attempt--) {
494 /* lets ring the doorbell */
495 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
496 db_cmp.value_qw, db_exc.value_qw);
497
498 /* if the exchange was successfully executed */
499 if (db_ret.value_qw == db_cmp.value_qw) {
500 /* db was successfully rung */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000501 client->doorbell_cookie = db_exc.cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100502 ret = 0;
503 break;
504 }
505
506 /* XXX: doorbell was lost and need to acquire it again */
507 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
508 break;
509
Dave Gordon535b2f52016-08-18 18:17:23 +0100510 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
511 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100512
513 /* update the cookie to newly read cookie from GuC */
514 db_cmp.cookie = db_ret.cookie;
515 db_exc.cookie = db_ret.cookie + 1;
516 if (db_exc.cookie == 0)
517 db_exc.cookie = 1;
518 }
519
520 return ret;
521}
522
Dave Gordon44a28b12015-08-12 15:43:41 +0100523/**
Chris Wilson34ba5a82016-11-29 12:10:24 +0000524 * __i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700525 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100526 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100527 * The caller must have already called i915_guc_wq_reserve() above with
528 * a result of 0 (success), guaranteeing that there is space in the work
529 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100530 *
531 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100532 * submit() when _reserve() says there's no space, or calls _submit()
533 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100534 *
535 * The only error here arises if the doorbell hardware isn't functioning
536 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100537 */
Chris Wilson34ba5a82016-11-29 12:10:24 +0000538static void __i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100539{
Akash Goeled4596ea2016-10-25 22:05:23 +0530540 struct drm_i915_private *dev_priv = rq->i915;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000541 struct intel_engine_cs *engine = rq->engine;
542 unsigned int engine_id = engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100543 struct intel_guc *guc = &rq->i915->guc;
544 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilson25afdf892017-03-02 14:53:23 +0000545 unsigned long flags;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100546 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100547
Akash Goeled4596ea2016-10-25 22:05:23 +0530548 /* WA to flush out the pending GMADR writes to ring buffer. */
549 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
550 POSTING_READ_FW(GUC_STATUS);
551
Chris Wilson25afdf892017-03-02 14:53:23 +0000552 spin_lock_irqsave(&client->wq_lock, flags);
Chris Wilson0c335182017-02-28 11:28:03 +0000553
554 guc_wq_item_append(client, rq);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100555 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100556
Alex Dai397097b2016-01-23 11:58:14 -0800557 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100558 client->retcode = b_ret;
559 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100560 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100561
Alex Dai397097b2016-01-23 11:58:14 -0800562 guc->submissions[engine_id] += 1;
Chris Wilson65e47602016-10-28 13:58:49 +0100563 guc->last_seqno[engine_id] = rq->global_seqno;
Chris Wilson0c335182017-02-28 11:28:03 +0000564
Chris Wilson25afdf892017-03-02 14:53:23 +0000565 spin_unlock_irqrestore(&client->wq_lock, flags);
Dave Gordon44a28b12015-08-12 15:43:41 +0100566}
567
Chris Wilson34ba5a82016-11-29 12:10:24 +0000568static void i915_guc_submit(struct drm_i915_gem_request *rq)
569{
Chris Wilson31de7352017-03-16 12:56:18 +0000570 __i915_gem_request_submit(rq);
Chris Wilson34ba5a82016-11-29 12:10:24 +0000571 __i915_guc_submit(rq);
572}
573
Chris Wilson31de7352017-03-16 12:56:18 +0000574static void nested_enable_signaling(struct drm_i915_gem_request *rq)
575{
576 /* If we use dma_fence_enable_sw_signaling() directly, lockdep
577 * detects an ordering issue between the fence lockclass and the
578 * global_timeline. This circular dependency can only occur via 2
579 * different fences (but same fence lockclass), so we use the nesting
580 * annotation here to prevent the warn, equivalent to the nesting
581 * inside i915_gem_request_submit() for when we also enable the
582 * signaler.
583 */
584
585 if (test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
586 &rq->fence.flags))
587 return;
588
589 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags));
590 trace_dma_fence_enable_signal(&rq->fence);
591
592 spin_lock_nested(&rq->lock, SINGLE_DEPTH_NESTING);
593 intel_engine_enable_signaling(rq);
594 spin_unlock(&rq->lock);
595}
596
597static bool i915_guc_dequeue(struct intel_engine_cs *engine)
598{
599 struct execlist_port *port = engine->execlist_port;
600 struct drm_i915_gem_request *last = port[0].request;
Chris Wilson31de7352017-03-16 12:56:18 +0000601 struct rb_node *rb;
602 bool submit = false;
603
Chris Wilson6c943de2017-03-17 12:07:16 +0000604 /* After execlist_first is updated, the tasklet will be rescheduled.
605 *
606 * If we are currently running (inside the tasklet) and a third
607 * party queues a request and so updates engine->execlist_first under
608 * the spinlock (which we have elided), it will atomically set the
609 * TASKLET_SCHED flag causing the us to be re-executed and pick up
610 * the change in state (the update to TASKLET_SCHED incurs a memory
611 * barrier making this cross-cpu checking safe).
612 */
613 if (!READ_ONCE(engine->execlist_first))
614 return false;
615
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000616 spin_lock_irq(&engine->timeline->lock);
Chris Wilson31de7352017-03-16 12:56:18 +0000617 rb = engine->execlist_first;
618 while (rb) {
619 struct drm_i915_gem_request *rq =
620 rb_entry(rb, typeof(*rq), priotree.node);
621
622 if (last && rq->ctx != last->ctx) {
623 if (port != engine->execlist_port)
624 break;
625
626 i915_gem_request_assign(&port->request, last);
627 nested_enable_signaling(last);
628 port++;
629 }
630
631 rb = rb_next(rb);
632 rb_erase(&rq->priotree.node, &engine->execlist_queue);
633 RB_CLEAR_NODE(&rq->priotree.node);
634 rq->priotree.priority = INT_MAX;
635
Chris Wilson31de7352017-03-16 12:56:18 +0000636 i915_guc_submit(rq);
Tvrtko Ursulin66e303e2017-03-20 13:25:56 +0000637 trace_i915_gem_request_in(rq, port - engine->execlist_port);
Chris Wilson31de7352017-03-16 12:56:18 +0000638 last = rq;
639 submit = true;
640 }
641 if (submit) {
642 i915_gem_request_assign(&port->request, last);
643 nested_enable_signaling(last);
644 engine->execlist_first = rb;
645 }
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000646 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson31de7352017-03-16 12:56:18 +0000647
648 return submit;
649}
650
651static void i915_guc_irq_handler(unsigned long data)
652{
653 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
654 struct execlist_port *port = engine->execlist_port;
655 struct drm_i915_gem_request *rq;
656 bool submit;
657
658 do {
659 rq = port[0].request;
660 while (rq && i915_gem_request_completed(rq)) {
661 trace_i915_gem_request_out(rq);
662 i915_gem_request_put(rq);
663 port[0].request = port[1].request;
664 port[1].request = NULL;
665 rq = port[0].request;
666 }
667
668 submit = false;
669 if (!port[1].request)
670 submit = i915_guc_dequeue(engine);
671 } while (submit);
672}
673
Dave Gordon44a28b12015-08-12 15:43:41 +0100674/*
675 * Everything below here is concerned with setup & teardown, and is
676 * therefore not part of the somewhat time-critical batch-submission
677 * path of i915_guc_submit() above.
678 */
679
680/**
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000681 * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
Chris Wilson8b797af2016-08-15 10:48:51 +0100682 * @guc: the guc
683 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100684 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100685 * This is a wrapper to create an object for use with the GuC. In order to
686 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
687 * both some backing storage and a range inside the Global GTT. We must pin
688 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
689 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100690 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100691 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100692 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000693struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100694{
Chris Wilson8b797af2016-08-15 10:48:51 +0100695 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100696 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100697 struct i915_vma *vma;
698 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100699
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000700 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100701 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100702 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100703
Chris Wilsona01cb372017-01-16 15:21:30 +0000704 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson8b797af2016-08-15 10:48:51 +0100705 if (IS_ERR(vma))
706 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100707
Chris Wilson8b797af2016-08-15 10:48:51 +0100708 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
709 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
710 if (ret) {
711 vma = ERR_PTR(ret);
712 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100713 }
714
Chris Wilson8b797af2016-08-15 10:48:51 +0100715 return vma;
716
717err:
718 i915_gem_object_put(obj);
719 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100720}
721
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700722static void guc_client_free(struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100723{
Dave Gordon44a28b12015-08-12 15:43:41 +0100724 /*
725 * XXX: wait for any outstanding submissions before freeing memory.
726 * Be sure to drop any locks
727 */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700728 guc_ctx_desc_fini(client->guc, client);
729 i915_gem_object_unpin_map(client->vma->obj);
Chris Wilson19880c42016-08-15 10:49:05 +0100730 i915_vma_unpin_and_release(&client->vma);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700731 ida_simple_remove(&client->guc->ctx_ids, client->ctx_index);
Dave Gordon44a28b12015-08-12 15:43:41 +0100732 kfree(client);
733}
734
Dave Gordon84b7f882016-08-09 15:19:20 +0100735/* Check that a doorbell register is in the expected state */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700736static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
Dave Gordon84b7f882016-08-09 15:19:20 +0100737{
738 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700739 u32 drbregl;
740 bool valid;
Dave Gordon84b7f882016-08-09 15:19:20 +0100741
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700742 GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
743
744 drbregl = I915_READ(GEN8_DRBREGL(db_id));
745 valid = drbregl & GEN8_DRB_VALID;
746
747 if (test_bit(db_id, guc->doorbell_bitmap) == valid)
Dave Gordon84b7f882016-08-09 15:19:20 +0100748 return true;
749
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700750 DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
751 db_id, drbregl, yesno(valid));
Dave Gordon84b7f882016-08-09 15:19:20 +0100752
753 return false;
754}
755
Dave Gordon4d757872016-06-13 17:57:34 +0100756/*
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700757 * If the GuC thinks that the doorbell is unassigned (e.g. because we reset and
758 * reloaded the GuC FW) we can use this function to tell the GuC to reassign the
759 * doorbell to the rightful owner.
760 */
761static int __reset_doorbell(struct i915_guc_client* client, u16 db_id)
762{
763 int err;
764
765 err = __update_doorbell_desc(client, db_id);
766 if (!err)
767 err = __create_doorbell(client);
768 if (!err)
769 err = __destroy_doorbell(client);
770
771 return err;
772}
773
774/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100775 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100776 * in turn, to ensure that all doorbell h/w is (re)initialised.
777 */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700778static int guc_init_doorbell_hw(struct intel_guc *guc)
Dave Gordon4d757872016-06-13 17:57:34 +0100779{
Dave Gordon4d757872016-06-13 17:57:34 +0100780 struct i915_guc_client *client = guc->execbuf_client;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700781 int err;
782 int i;
Dave Gordon4d757872016-06-13 17:57:34 +0100783
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700784 if (has_doorbell(client))
785 destroy_doorbell(client);
Dave Gordon4d757872016-06-13 17:57:34 +0100786
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700787 for (i = 0; i < GUC_NUM_DOORBELLS; ++i) {
788 if (doorbell_ok(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100789 continue;
790
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700791 err = __reset_doorbell(client, i);
792 WARN(err, "Doorbell %d reset failed, err %d\n", i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100793 }
794
Dave Gordon84b7f882016-08-09 15:19:20 +0100795 /* Read back & verify all doorbell registers */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700796 for (i = 0; i < GUC_NUM_DOORBELLS; ++i)
797 WARN_ON(!doorbell_ok(guc, i));
798
799 err = __reserve_doorbell(client);
800 if (err)
801 return err;
802
803 err = __update_doorbell_desc(client, client->doorbell_id);
804 if (err)
805 goto err_reserve;
806
807 err = __create_doorbell(client);
808 if (err)
809 goto err_update;
810
811 return 0;
812err_reserve:
813 __unreserve_doorbell(client);
814err_update:
815 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
816 return err;
Dave Gordon4d757872016-06-13 17:57:34 +0100817}
818
Dave Gordon44a28b12015-08-12 15:43:41 +0100819/**
820 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100821 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100822 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100823 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
824 * The kernel client to replace ExecList submission is created with
825 * NORMAL priority. Priority of a client for scheduler can be HIGH,
826 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700827 * @ctx: the context that owns the client (we use the default render
828 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100829 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100830 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100831 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100832static struct i915_guc_client *
833guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100834 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100835 uint32_t priority,
836 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100837{
838 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100839 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100840 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000841 void *vaddr;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700842 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100843
844 client = kzalloc(sizeof(*client), GFP_KERNEL);
845 if (!client)
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700846 return ERR_PTR(-ENOMEM);
Dave Gordon44a28b12015-08-12 15:43:41 +0100847
Dave Gordon44a28b12015-08-12 15:43:41 +0100848 client->guc = guc;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700849 client->owner = ctx;
Dave Gordone02757d2016-08-09 15:19:21 +0100850 client->engines = engines;
851 client->priority = priority;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700852 client->doorbell_id = GUC_DOORBELL_INVALID;
853 client->wq_offset = GUC_DB_SIZE;
854 client->wq_size = GUC_WQ_SIZE;
855 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100856
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700857 ret = ida_simple_get(&guc->ctx_ids, 0, GUC_MAX_GPU_CONTEXTS,
858 GFP_KERNEL);
859 if (ret < 0)
860 goto err_client;
861
862 client->ctx_index = ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100863
864 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000865 vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700866 if (IS_ERR(vma)) {
867 ret = PTR_ERR(vma);
868 goto err_id;
869 }
Dave Gordon44a28b12015-08-12 15:43:41 +0100870
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100871 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100872 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000873
874 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700875 if (IS_ERR(vaddr)) {
876 ret = PTR_ERR(vaddr);
877 goto err_vma;
878 }
Chris Wilson72aa0d82016-11-02 17:50:47 +0000879 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100880
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700881 client->doorbell_offset = __select_cacheline(guc);
Dave Gordon44a28b12015-08-12 15:43:41 +0100882
883 /*
884 * Since the doorbell only requires a single cacheline, we can save
885 * space by putting the application process descriptor in the same
886 * page. Use the half of the page that doesn't include the doorbell.
887 */
888 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
889 client->proc_desc_offset = 0;
890 else
891 client->proc_desc_offset = (GUC_DB_SIZE / 2);
892
Dave Gordon7a9347f2016-09-12 21:19:37 +0100893 guc_proc_desc_init(guc, client);
894 guc_ctx_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000895
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700896 /* FIXME: Runtime client allocation (which currently we don't do) will
897 * require that the doorbell gets created now. The static execbuf_client
898 * is now getting its doorbell later (on submission enable) but maybe we
899 * also want to reorder things in the future so that we don't have to
900 * special case the doorbell creation */
Dave Gordon44a28b12015-08-12 15:43:41 +0100901
Dave Gordone02757d2016-08-09 15:19:21 +0100902 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700903 priority, client, client->engines, client->ctx_index);
904 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
905 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100906
907 return client;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700908err_vma:
909 i915_vma_unpin_and_release(&client->vma);
910err_id:
911 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
912err_client:
913 kfree(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100914
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700915 return ERR_PTR(ret);
Dave Gordon44a28b12015-08-12 15:43:41 +0100916}
917
Dave Gordon7a9347f2016-09-12 21:19:37 +0100918static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -0800919{
920 struct guc_policy *policy;
921 u32 p, i;
922
923 policies->dpc_promote_time = 500000;
924 policies->max_num_work_items = POLICY_MAX_NUM_WI;
925
926 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -0800927 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -0800928 policy = &policies->policy[p][i];
929
930 policy->execution_quantum = 1000000;
931 policy->preemption_time = 500000;
932 policy->fault_time = 250000;
933 policy->policy_flags = 0;
934 }
935 }
936
937 policies->is_valid = 1;
938}
939
Dave Gordon7a9347f2016-09-12 21:19:37 +0100940static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -0800941{
942 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +0100943 struct i915_vma *vma;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000944 struct page *page;
945 /* The ads obj includes the struct itself and buffers passed to GuC */
946 struct {
947 struct guc_ads ads;
948 struct guc_policies policies;
949 struct guc_mmio_reg_state reg_state;
950 u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
951 } __packed *blob;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000952 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530953 enum intel_engine_id id;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000954 u32 base;
Alex Dai68371a92015-12-18 12:00:09 -0800955
Chris Wilson8b797af2016-08-15 10:48:51 +0100956 vma = guc->ads_vma;
957 if (!vma) {
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000958 vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
Chris Wilson8b797af2016-08-15 10:48:51 +0100959 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -0800960 return;
961
Chris Wilson8b797af2016-08-15 10:48:51 +0100962 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -0800963 }
964
Chris Wilson8b797af2016-08-15 10:48:51 +0100965 page = i915_vma_first_page(vma);
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000966 blob = kmap(page);
967
968 /* GuC scheduling policies */
969 guc_policies_init(&blob->policies);
970
971 /* MMIO reg state */
972 for_each_engine(engine, dev_priv, id) {
973 blob->reg_state.mmio_white_list[engine->guc_id].mmio_start =
974 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
975
976 /* Nothing to be saved or restored for now. */
977 blob->reg_state.mmio_white_list[engine->guc_id].count = 0;
978 }
Alex Dai68371a92015-12-18 12:00:09 -0800979
980 /*
981 * The GuC requires a "Golden Context" when it reinitialises
982 * engines after a reset. Here we use the Render ring default
983 * context, which must already exist and be pinned in the GGTT,
984 * so its address won't change after we've told the GuC where
985 * to find it.
986 */
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000987 blob->ads.golden_context_lrca =
988 dev_priv->engine[RCS]->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -0800989
Akash Goel3b3f1652016-10-13 22:44:48 +0530990 for_each_engine(engine, dev_priv, id)
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000991 blob->ads.eng_state_size[engine->guc_id] =
992 intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -0800993
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000994 base = guc_ggtt_offset(vma);
995 blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
996 blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
997 blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
Alex Dai5c148e02015-12-18 12:00:11 -0800998
Alex Dai68371a92015-12-18 12:00:09 -0800999 kunmap(page);
1000}
1001
Alex Daibac427f2015-08-12 15:43:39 +01001002/*
1003 * Set up the memory resources to be shared with the GuC. At this point,
1004 * we require just one object that can be mapped through the GGTT.
1005 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001006int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001007{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001008 const size_t ctxsize = sizeof(struct guc_context_desc);
1009 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1010 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001011 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001012 struct i915_vma *vma;
Oscar Mateo73b05532017-03-22 10:39:45 -07001013 void *vaddr;
Alex Daibac427f2015-08-12 15:43:39 +01001014
Chris Wilson4d357af2016-11-29 12:10:23 +00001015 if (!HAS_GUC_SCHED(dev_priv))
1016 return 0;
1017
Dave Gordon29fb72c2016-06-07 09:14:50 +01001018 /* Wipe bitmap & delete client in case of reinitialisation */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001019 bitmap_clear(guc->doorbell_bitmap, 0, GUC_NUM_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001020 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001021
Alex Daibac427f2015-08-12 15:43:39 +01001022 if (!i915.enable_guc_submission)
1023 return 0; /* not enabled */
1024
Oscar Mateo73b05532017-03-22 10:39:45 -07001025 if (guc->ctx_pool)
Alex Daibac427f2015-08-12 15:43:39 +01001026 return 0; /* already allocated */
1027
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001028 vma = intel_guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001029 if (IS_ERR(vma))
1030 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001031
Oscar Mateo73b05532017-03-22 10:39:45 -07001032 guc->ctx_pool = vma;
1033
1034 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
1035 if (IS_ERR(vaddr))
1036 goto err;
1037
1038 guc->ctx_pool_vaddr = vaddr;
1039
Alex Daibac427f2015-08-12 15:43:39 +01001040 ida_init(&guc->ctx_ids);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001041 intel_guc_log_create(guc);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001042 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001043
Chris Wilson4d357af2016-11-29 12:10:23 +00001044 guc->execbuf_client = guc_client_alloc(dev_priv,
1045 INTEL_INFO(dev_priv)->ring_mask,
1046 GUC_CTX_PRIORITY_KMD_NORMAL,
1047 dev_priv->kernel_context);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001048 if (IS_ERR(guc->execbuf_client)) {
Chris Wilson4d357af2016-11-29 12:10:23 +00001049 DRM_ERROR("Failed to create GuC client for execbuf!\n");
1050 goto err;
1051 }
1052
Alex Daibac427f2015-08-12 15:43:39 +01001053 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +00001054
1055err:
1056 i915_guc_submission_fini(dev_priv);
1057 return -ENOMEM;
1058}
1059
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001060static void guc_reset_wq(struct i915_guc_client *client)
Chris Wilson4d357af2016-11-29 12:10:23 +00001061{
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001062 struct guc_process_desc *desc = client->vaddr +
1063 client->proc_desc_offset;
Chris Wilson4d357af2016-11-29 12:10:23 +00001064
1065 desc->head = 0;
1066 desc->tail = 0;
1067
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001068 client->wq_tail = 0;
Alex Daibac427f2015-08-12 15:43:39 +01001069}
1070
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001071static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
1072{
1073 struct intel_engine_cs *engine;
1074 enum intel_engine_id id;
1075 int irqs;
1076
1077 /* tell all command streamers to forward interrupts (but not vblank) to GuC */
1078 irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
1079 for_each_engine(engine, dev_priv, id)
1080 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1081
1082 /* route USER_INTERRUPT to Host, all others are sent to GuC. */
1083 irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
1084 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
1085 /* These three registers have the same bit definitions */
1086 I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
1087 I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
1088 I915_WRITE(GUC_WD_VECS_IER, ~irqs);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301089
1090 /*
1091 * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
1092 * (unmasked) PM interrupts to the GuC. All other bits of this
1093 * register *disable* generation of a specific interrupt.
1094 *
1095 * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
1096 * writing to the PM interrupt mask register, i.e. interrupts
1097 * that must not be disabled.
1098 *
1099 * If the GuC is handling these interrupts, then we must not let
1100 * the PM code disable ANY interrupt that the GuC is expecting.
1101 * So for each ENABLED (0) bit in this register, we must SET the
1102 * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
1103 * GuC needs ARAT expired interrupt unmasked hence it is set in
1104 * pm_intrmsk_mbz.
1105 *
1106 * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1107 * result in the register bit being left SET!
1108 */
1109 dev_priv->rps.pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
Chris Wilson655d49e2017-03-12 13:27:45 +00001110 dev_priv->rps.pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001111}
1112
Dave Gordonbeffa512016-06-10 18:29:26 +01001113int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001114{
Dave Gordon44a28b12015-08-12 15:43:41 +01001115 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001116 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001117 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301118 enum intel_engine_id id;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001119 int err;
Dave Gordon44a28b12015-08-12 15:43:41 +01001120
Chris Wilson4d357af2016-11-29 12:10:23 +00001121 if (!client)
1122 return -ENODEV;
Dave Gordon44a28b12015-08-12 15:43:41 +01001123
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001124 err = intel_guc_sample_forcewake(guc);
1125 if (err)
1126 return err;
Chris Wilson4d357af2016-11-29 12:10:23 +00001127
1128 guc_reset_wq(client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001129 err = guc_init_doorbell_hw(guc);
1130 if (err)
1131 return err;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001132
Chris Wilsonddd66c52016-08-02 22:50:31 +01001133 /* Take over from manual control of ELSP (execlists) */
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001134 guc_interrupts_capture(dev_priv);
1135
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001136 for_each_engine(engine, dev_priv, id) {
Chris Wilson349ab912017-02-28 11:28:02 +00001137 const int wqi_size = sizeof(struct guc_wq_item);
Chris Wilson4d357af2016-11-29 12:10:23 +00001138 struct drm_i915_gem_request *rq;
1139
Chris Wilson31de7352017-03-16 12:56:18 +00001140 /* The tasklet was initialised by execlists, and may be in
1141 * a state of flux (across a reset) and so we just want to
1142 * take over the callback without changing any other state
1143 * in the tasklet.
1144 */
1145 engine->irq_tasklet.func = i915_guc_irq_handler;
1146 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1147
1148 /* Replay the current set of previously submitted requests */
Chris Wilson349ab912017-02-28 11:28:02 +00001149 spin_lock_irq(&engine->timeline->lock);
Chris Wilson4d357af2016-11-29 12:10:23 +00001150 list_for_each_entry(rq, &engine->timeline->requests, link) {
Chris Wilson349ab912017-02-28 11:28:02 +00001151 guc_client_update_wq_rsvd(client, wqi_size);
Chris Wilson34ba5a82016-11-29 12:10:24 +00001152 __i915_guc_submit(rq);
Chris Wilsondadd4812016-09-09 14:11:57 +01001153 }
Chris Wilson349ab912017-02-28 11:28:02 +00001154 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001155 }
1156
Dave Gordon44a28b12015-08-12 15:43:41 +01001157 return 0;
1158}
1159
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301160static void guc_interrupts_release(struct drm_i915_private *dev_priv)
1161{
1162 struct intel_engine_cs *engine;
1163 enum intel_engine_id id;
1164 int irqs;
1165
1166 /*
1167 * tell all command streamers NOT to forward interrupts or vblank
1168 * to GuC.
1169 */
1170 irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
1171 irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
1172 for_each_engine(engine, dev_priv, id)
1173 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1174
1175 /* route all GT interrupts to the host */
1176 I915_WRITE(GUC_BCS_RCS_IER, 0);
1177 I915_WRITE(GUC_VCS2_VCS1_IER, 0);
1178 I915_WRITE(GUC_WD_VECS_IER, 0);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301179
Chris Wilson655d49e2017-03-12 13:27:45 +00001180 dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301181 dev_priv->rps.pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301182}
1183
Dave Gordonbeffa512016-06-10 18:29:26 +01001184void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001185{
Dave Gordon44a28b12015-08-12 15:43:41 +01001186 struct intel_guc *guc = &dev_priv->guc;
1187
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301188 guc_interrupts_release(dev_priv);
1189
Chris Wilsonddd66c52016-08-02 22:50:31 +01001190 if (!guc->execbuf_client)
1191 return;
1192
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001193 /* FIXME: in many cases, by the time we get here the GuC has been
1194 * reset, so we cannot destroy the doorbell properly. Ignore the
1195 * error message for now */
1196 destroy_doorbell(guc->execbuf_client);
1197
Chris Wilsonddd66c52016-08-02 22:50:31 +01001198 /* Revert back to manual ELSP submission */
Chris Wilsonff44ad52017-03-16 17:13:03 +00001199 intel_engines_reset_default_submission(dev_priv);
Dave Gordon44a28b12015-08-12 15:43:41 +01001200}
1201
Dave Gordonbeffa512016-06-10 18:29:26 +01001202void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001203{
Alex Daibac427f2015-08-12 15:43:39 +01001204 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001205 struct i915_guc_client *client;
1206
1207 client = fetch_and_zero(&guc->execbuf_client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001208 if (client && !IS_ERR(client))
1209 guc_client_free(client);
Alex Daibac427f2015-08-12 15:43:39 +01001210
Chris Wilson19880c42016-08-15 10:49:05 +01001211 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301212 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001213
Oscar Mateo73b05532017-03-22 10:39:45 -07001214 if (guc->ctx_pool_vaddr) {
Alex Daibac427f2015-08-12 15:43:39 +01001215 ida_destroy(&guc->ctx_ids);
Oscar Mateo73b05532017-03-22 10:39:45 -07001216 i915_gem_object_unpin_map(guc->ctx_pool->obj);
1217 }
1218
1219 i915_vma_unpin_and_release(&guc->ctx_pool);
Alex Daibac427f2015-08-12 15:43:39 +01001220}
Alex Daia1c41992015-09-30 09:46:37 -07001221
1222/**
1223 * intel_guc_suspend() - notify GuC entering suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001224 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001225 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001226int intel_guc_suspend(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001227{
Alex Daia1c41992015-09-30 09:46:37 -07001228 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001229 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001230 u32 data[3];
1231
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001232 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001233 return 0;
1234
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301235 gen9_disable_guc_interrupts(dev_priv);
1236
Dave Gordoned54c1a2016-01-19 19:02:54 +00001237 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001238
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001239 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001240 /* any value greater than GUC_POWER_D0 */
1241 data[1] = GUC_POWER_D1;
1242 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001243 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001244
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001245 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001246}
1247
1248
1249/**
1250 * intel_guc_resume() - notify GuC resuming from suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001251 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001252 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001253int intel_guc_resume(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001254{
Alex Daia1c41992015-09-30 09:46:37 -07001255 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001256 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001257 u32 data[3];
1258
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001259 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001260 return 0;
1261
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301262 if (i915.guc_log_level >= 0)
1263 gen9_enable_guc_interrupts(dev_priv);
1264
Dave Gordoned54c1a2016-01-19 19:02:54 +00001265 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001266
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001267 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001268 data[1] = GUC_POWER_D0;
1269 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001270 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001271
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001272 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001273}