blob: 141ed9df3d5ca83648053b76de187ac9ebe980f6 [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
Michal Wajdeczko9f436c42017-10-04 18:13:40 +000025#include <linux/circ_buf.h>
Chris Wilson31de7352017-03-16 12:56:18 +000026#include <trace/events/dma_fence.h>
27
Michal Wajdeczko9f436c42017-10-04 18:13:40 +000028#include "i915_guc_submission.h"
29#include "i915_drv.h"
30
Alex Daibac427f2015-08-12 15:43:39 +010031/**
Alex Daifeda33e2015-10-19 16:10:54 -070032 * DOC: GuC-based command submission
Dave Gordon44a28b12015-08-12 15:43:41 +010033 *
Oscar Mateo0d768122017-03-22 10:39:50 -070034 * GuC client:
35 * A i915_guc_client refers to a submission path through GuC. Currently, there
36 * is only one of these (the execbuf_client) and this one is charged with all
37 * submissions to the GuC. This struct is the owner of a doorbell, a process
38 * descriptor and a workqueue (all of them inside a single gem object that
39 * contains all required pages for these elements).
Dave Gordon44a28b12015-08-12 15:43:41 +010040 *
Oscar Mateob09935a2017-03-22 10:39:53 -070041 * GuC stage descriptor:
Oscar Mateo0d768122017-03-22 10:39:50 -070042 * During initialization, the driver allocates a static pool of 1024 such
43 * descriptors, and shares them with the GuC.
44 * Currently, there exists a 1:1 mapping between a i915_guc_client and a
Oscar Mateob09935a2017-03-22 10:39:53 -070045 * guc_stage_desc (via the client's stage_id), so effectively only one
46 * gets used. This stage descriptor lets the GuC know about the doorbell,
47 * workqueue and process descriptor. Theoretically, it also lets the GuC
48 * know about our HW contexts (context ID, etc...), but we actually
Oscar Mateo0d768122017-03-22 10:39:50 -070049 * employ a kind of submission where the GuC uses the LRCA sent via the work
Oscar Mateob09935a2017-03-22 10:39:53 -070050 * item instead (the single guc_stage_desc associated to execbuf client
Oscar Mateo0d768122017-03-22 10:39:50 -070051 * contains information about the default kernel context only, but this is
52 * essentially unused). This is called a "proxy" submission.
Dave Gordon44a28b12015-08-12 15:43:41 +010053 *
54 * The Scratch registers:
55 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
56 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
57 * triggers an interrupt on the GuC via another register write (0xC4C8).
58 * Firmware writes a success/fail code back to the action register after
59 * processes the request. The kernel driver polls waiting for this update and
60 * then proceeds.
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010061 * See intel_guc_send()
Dave Gordon44a28b12015-08-12 15:43:41 +010062 *
63 * Doorbells:
64 * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
65 * mapped into process space.
66 *
67 * Work Items:
68 * There are several types of work items that the host may place into a
69 * workqueue, each with its own requirements and limitations. Currently only
70 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
71 * represents in-order queue. The kernel driver packs ring tail pointer and an
72 * ELSP context descriptor dword into Work Item.
Dave Gordon7a9347f2016-09-12 21:19:37 +010073 * See guc_wq_item_append()
Dave Gordon44a28b12015-08-12 15:43:41 +010074 *
Oscar Mateo0704df22017-03-22 10:39:47 -070075 * ADS:
76 * The Additional Data Struct (ADS) has pointers for different buffers used by
77 * the GuC. One single gem object contains the ADS struct itself (guc_ads), the
78 * scheduling policies (guc_policies), a structure describing a collection of
79 * register sets (guc_mmio_reg_state) and some extra pages for the GuC to save
80 * its internal state for sleep.
81 *
Dave Gordon44a28b12015-08-12 15:43:41 +010082 */
83
Joonas Lahtinenabddffd2017-03-22 10:39:44 -070084static inline bool is_high_priority(struct i915_guc_client* client)
85{
Oscar Mateob09935a2017-03-22 10:39:53 -070086 return client->priority <= GUC_CLIENT_PRIORITY_HIGH;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -070087}
88
89static int __reserve_doorbell(struct i915_guc_client *client)
90{
91 unsigned long offset;
92 unsigned long end;
93 u16 id;
94
95 GEM_BUG_ON(client->doorbell_id != GUC_DOORBELL_INVALID);
96
97 /*
98 * The bitmap tracks which doorbell registers are currently in use.
99 * It is split into two halves; the first half is used for normal
100 * priority contexts, the second half for high-priority ones.
101 */
102 offset = 0;
103 end = GUC_NUM_DOORBELLS/2;
104 if (is_high_priority(client)) {
105 offset = end;
106 end += offset;
107 }
108
Michel Thierry7f1ea2a2017-05-30 17:05:46 -0700109 id = find_next_zero_bit(client->guc->doorbell_bitmap, end, offset);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700110 if (id == end)
111 return -ENOSPC;
112
113 __set_bit(id, client->guc->doorbell_bitmap);
114 client->doorbell_id = id;
115 DRM_DEBUG_DRIVER("client %u (high prio=%s) reserved doorbell: %d\n",
Oscar Mateob09935a2017-03-22 10:39:53 -0700116 client->stage_id, yesno(is_high_priority(client)),
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700117 id);
118 return 0;
119}
120
121static void __unreserve_doorbell(struct i915_guc_client *client)
122{
123 GEM_BUG_ON(client->doorbell_id == GUC_DOORBELL_INVALID);
124
125 __clear_bit(client->doorbell_id, client->guc->doorbell_bitmap);
126 client->doorbell_id = GUC_DOORBELL_INVALID;
127}
128
Dave Gordon44a28b12015-08-12 15:43:41 +0100129/*
Dave Gordon44a28b12015-08-12 15:43:41 +0100130 * Tell the GuC to allocate or deallocate a specific doorbell
131 */
132
Oscar Mateob09935a2017-03-22 10:39:53 -0700133static int __guc_allocate_doorbell(struct intel_guc *guc, u32 stage_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100134{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100135 u32 action[] = {
136 INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
Oscar Mateob09935a2017-03-22 10:39:53 -0700137 stage_id
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100138 };
Dave Gordon44a28b12015-08-12 15:43:41 +0100139
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100140 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Dave Gordon44a28b12015-08-12 15:43:41 +0100141}
142
Oscar Mateob09935a2017-03-22 10:39:53 -0700143static int __guc_deallocate_doorbell(struct intel_guc *guc, u32 stage_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100144{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100145 u32 action[] = {
146 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
Oscar Mateob09935a2017-03-22 10:39:53 -0700147 stage_id
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100148 };
Dave Gordon44a28b12015-08-12 15:43:41 +0100149
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100150 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Sagar Arun Kamble685534e2016-10-12 21:54:41 +0530151}
152
Oscar Mateob09935a2017-03-22 10:39:53 -0700153static struct guc_stage_desc *__get_stage_desc(struct i915_guc_client *client)
Oscar Mateo73b05532017-03-22 10:39:45 -0700154{
Oscar Mateob09935a2017-03-22 10:39:53 -0700155 struct guc_stage_desc *base = client->guc->stage_desc_pool_vaddr;
Oscar Mateo73b05532017-03-22 10:39:45 -0700156
Oscar Mateob09935a2017-03-22 10:39:53 -0700157 return &base[client->stage_id];
Oscar Mateo73b05532017-03-22 10:39:45 -0700158}
159
Dave Gordon44a28b12015-08-12 15:43:41 +0100160/*
161 * Initialise, update, or clear doorbell data shared with the GuC
162 *
163 * These functions modify shared data and so need access to the mapped
164 * client object which contains the page being used for the doorbell
165 */
166
Oscar Mateo397fce82017-03-22 10:39:52 -0700167static void __update_doorbell_desc(struct i915_guc_client *client, u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100168{
Oscar Mateob09935a2017-03-22 10:39:53 -0700169 struct guc_stage_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100170
Dave Gordona6674292016-06-13 17:57:32 +0100171 /* Update the GuC's idea of the doorbell ID */
Oscar Mateob09935a2017-03-22 10:39:53 -0700172 desc = __get_stage_desc(client);
Oscar Mateo73b05532017-03-22 10:39:45 -0700173 desc->db_id = new_id;
Dave Gordona6674292016-06-13 17:57:32 +0100174}
175
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700176static struct guc_doorbell_info *__get_doorbell(struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100177{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700178 return client->vaddr + client->doorbell_offset;
179}
180
181static bool has_doorbell(struct i915_guc_client *client)
182{
183 if (client->doorbell_id == GUC_DOORBELL_INVALID)
184 return false;
185
186 return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
187}
188
189static int __create_doorbell(struct i915_guc_client *client)
190{
191 struct guc_doorbell_info *doorbell;
192 int err;
193
194 doorbell = __get_doorbell(client);
195 doorbell->db_status = GUC_DOORBELL_ENABLED;
Michał Winiarski59db36c2017-09-14 12:51:23 +0200196 doorbell->cookie = 0;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700197
Oscar Mateob09935a2017-03-22 10:39:53 -0700198 err = __guc_allocate_doorbell(client->guc, client->stage_id);
Michał Winiarski59db36c2017-09-14 12:51:23 +0200199 if (err)
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700200 doorbell->db_status = GUC_DOORBELL_DISABLED;
Michał Winiarski59db36c2017-09-14 12:51:23 +0200201
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700202 return err;
203}
204
205static int __destroy_doorbell(struct i915_guc_client *client)
206{
Oscar Mateoed2ec71f2017-03-22 10:39:51 -0700207 struct drm_i915_private *dev_priv = guc_to_i915(client->guc);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700208 struct guc_doorbell_info *doorbell;
Oscar Mateoed2ec71f2017-03-22 10:39:51 -0700209 u16 db_id = client->doorbell_id;
210
211 GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700212
213 doorbell = __get_doorbell(client);
214 doorbell->db_status = GUC_DOORBELL_DISABLED;
215 doorbell->cookie = 0;
216
Oscar Mateoed2ec71f2017-03-22 10:39:51 -0700217 /* Doorbell release flow requires that we wait for GEN8_DRB_VALID bit
218 * to go to zero after updating db_status before we call the GuC to
219 * release the doorbell */
220 if (wait_for_us(!(I915_READ(GEN8_DRBREGL(db_id)) & GEN8_DRB_VALID), 10))
221 WARN_ONCE(true, "Doorbell never became invalid after disable\n");
222
Oscar Mateob09935a2017-03-22 10:39:53 -0700223 return __guc_deallocate_doorbell(client->guc, client->stage_id);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700224}
225
Oscar Mateo397fce82017-03-22 10:39:52 -0700226static int create_doorbell(struct i915_guc_client *client)
227{
228 int ret;
229
230 ret = __reserve_doorbell(client);
231 if (ret)
232 return ret;
233
234 __update_doorbell_desc(client, client->doorbell_id);
235
236 ret = __create_doorbell(client);
237 if (ret)
238 goto err;
239
240 return 0;
241
242err:
243 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
244 __unreserve_doorbell(client);
245 return ret;
246}
247
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700248static int destroy_doorbell(struct i915_guc_client *client)
249{
250 int err;
251
252 GEM_BUG_ON(!has_doorbell(client));
Dave Gordon44a28b12015-08-12 15:43:41 +0100253
Dave Gordon44a28b12015-08-12 15:43:41 +0100254 /* XXX: wait for any interrupts */
255 /* XXX: wait for workqueue to drain */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700256
257 err = __destroy_doorbell(client);
258 if (err)
259 return err;
260
261 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
262
263 __unreserve_doorbell(client);
264
265 return 0;
Dave Gordon44a28b12015-08-12 15:43:41 +0100266}
267
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700268static unsigned long __select_cacheline(struct intel_guc* guc)
Dave Gordonf10d69a2016-06-13 17:57:33 +0100269{
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700270 unsigned long offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100271
Dave Gordon44a28b12015-08-12 15:43:41 +0100272 /* Doorbell uses a single cache line within a page */
273 offset = offset_in_page(guc->db_cacheline);
274
275 /* Moving to next cache line to reduce contention */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700276 guc->db_cacheline += cache_line_size();
Dave Gordon44a28b12015-08-12 15:43:41 +0100277
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700278 DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
279 offset, guc->db_cacheline, cache_line_size());
Dave Gordon44a28b12015-08-12 15:43:41 +0100280 return offset;
281}
282
Chris Wilsonbd00e732017-03-23 23:00:00 +0000283static inline struct guc_process_desc *
284__get_process_desc(struct i915_guc_client *client)
285{
286 return client->vaddr + client->proc_desc_offset;
287}
288
Dave Gordon44a28b12015-08-12 15:43:41 +0100289/*
290 * Initialise the process descriptor shared with the GuC firmware.
291 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100292static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100293 struct i915_guc_client *client)
294{
295 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100296
Chris Wilsonbd00e732017-03-23 23:00:00 +0000297 desc = memset(__get_process_desc(client), 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100298
299 /*
300 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
301 * space for ring3 clients (set them as in mmap_ioctl) or kernel
302 * space for kernel clients (map on demand instead? May make debug
303 * easier to have it mapped).
304 */
305 desc->wq_base_addr = 0;
306 desc->db_base_addr = 0;
307
Oscar Mateob09935a2017-03-22 10:39:53 -0700308 desc->stage_id = client->stage_id;
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200309 desc->wq_size_bytes = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100310 desc->wq_status = WQ_STATUS_ACTIVE;
311 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100312}
313
314/*
Oscar Mateob09935a2017-03-22 10:39:53 -0700315 * Initialise/clear the stage descriptor shared with the GuC firmware.
Dave Gordon44a28b12015-08-12 15:43:41 +0100316 *
317 * This descriptor tells the GuC where (in GGTT space) to find the important
318 * data structures relating to this client (doorbell, process descriptor,
319 * write queue, etc).
320 */
Oscar Mateob09935a2017-03-22 10:39:53 -0700321static void guc_stage_desc_init(struct intel_guc *guc,
322 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100323{
Alex Dai397097b2016-01-23 11:58:14 -0800324 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000325 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100326 struct i915_gem_context *ctx = client->owner;
Oscar Mateob09935a2017-03-22 10:39:53 -0700327 struct guc_stage_desc *desc;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100328 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100329 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100330
Oscar Mateob09935a2017-03-22 10:39:53 -0700331 desc = __get_stage_desc(client);
Oscar Mateo73b05532017-03-22 10:39:45 -0700332 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100333
Oscar Mateob09935a2017-03-22 10:39:53 -0700334 desc->attribute = GUC_STAGE_DESC_ATTR_ACTIVE | GUC_STAGE_DESC_ATTR_KERNEL;
335 desc->stage_id = client->stage_id;
Oscar Mateo73b05532017-03-22 10:39:45 -0700336 desc->priority = client->priority;
337 desc->db_id = client->doorbell_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100338
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100339 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100340 struct intel_context *ce = &ctx->engine[engine->id];
Joonas Lahtinenfaf65482017-10-06 11:49:40 +0300341 u32 guc_engine_id = engine->guc_id;
Oscar Mateo73b05532017-03-22 10:39:45 -0700342 struct guc_execlist_context *lrc = &desc->lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100343
344 /* TODO: We have a design issue to be solved here. Only when we
345 * receive the first batch, we know which engine is used by the
346 * user. But here GuC expects the lrc and ring to be pinned. It
347 * is not an issue for default context, which is the only one
348 * for now who owns a GuC client. But for future owner of GuC
349 * client, need to make sure lrc is pinned prior to enter here.
350 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100351 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100352 break; /* XXX: continue? */
353
Oscar Mateo0d768122017-03-22 10:39:50 -0700354 /*
Oscar Mateob09935a2017-03-22 10:39:53 -0700355 * XXX: When this is a GUC_STAGE_DESC_ATTR_KERNEL client (proxy
Oscar Mateo0d768122017-03-22 10:39:50 -0700356 * submission or, in other words, not using a direct submission
357 * model) the KMD's LRCA is not used for any work submission.
358 * Instead, the GuC uses the LRCA of the user mode context (see
359 * guc_wq_item_append below).
360 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100361 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100362
363 /* The state page is after PPHWSP */
Oscar Mateo0d768122017-03-22 10:39:50 -0700364 lrc->ring_lrca =
Chris Wilson4741da92016-12-24 19:31:46 +0000365 guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Oscar Mateob09935a2017-03-22 10:39:53 -0700366
367 /* XXX: In direct submission, the GuC wants the HW context id
368 * here. In proxy submission, it wants the stage id */
369 lrc->context_id = (client->stage_id << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100370 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100371
Chris Wilson4741da92016-12-24 19:31:46 +0000372 lrc->ring_begin = guc_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100373 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
374 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100375 lrc->ring_current_tail_pointer_value = 0;
376
Oscar Mateo73b05532017-03-22 10:39:45 -0700377 desc->engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100378 }
379
Dave Gordone02757d2016-08-09 15:19:21 +0100380 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
Oscar Mateo73b05532017-03-22 10:39:45 -0700381 client->engines, desc->engines_used);
382 WARN_ON(desc->engines_used == 0);
Alex Daid1675192015-08-12 15:43:43 +0100383
Dave Gordon44a28b12015-08-12 15:43:41 +0100384 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100385 * The doorbell, process descriptor, and workqueue are all parts
386 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100387 */
Chris Wilson4741da92016-12-24 19:31:46 +0000388 gfx_addr = guc_ggtt_offset(client->vma);
Oscar Mateo73b05532017-03-22 10:39:45 -0700389 desc->db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100390 client->doorbell_offset;
Michal Wajdeczkobb8920f2017-10-06 13:08:44 +0000391 desc->db_trigger_cpu = ptr_to_u64(__get_doorbell(client));
Oscar Mateo73b05532017-03-22 10:39:45 -0700392 desc->db_trigger_uk = gfx_addr + client->doorbell_offset;
393 desc->process_desc = gfx_addr + client->proc_desc_offset;
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200394 desc->wq_addr = gfx_addr + GUC_DB_SIZE;
395 desc->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100396
Michal Wajdeczkobb8920f2017-10-06 13:08:44 +0000397 desc->desc_private = ptr_to_u64(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100398}
399
Oscar Mateob09935a2017-03-22 10:39:53 -0700400static void guc_stage_desc_fini(struct intel_guc *guc,
401 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100402{
Oscar Mateob09935a2017-03-22 10:39:53 -0700403 struct guc_stage_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100404
Oscar Mateob09935a2017-03-22 10:39:53 -0700405 desc = __get_stage_desc(client);
Oscar Mateo73b05532017-03-22 10:39:45 -0700406 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100407}
408
Dave Gordon7a9347f2016-09-12 21:19:37 +0100409/* Construct a Work Item and append it to the GuC's Work Queue */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000410static void guc_wq_item_append(struct i915_guc_client *client,
Dave Gordon7a9347f2016-09-12 21:19:37 +0100411 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100412{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100413 /* wqi_len is in DWords, and does not include the one-word header */
414 const size_t wqi_size = sizeof(struct guc_wq_item);
Oscar Mateoada8c412017-09-12 14:36:37 -0700415 const u32 wqi_len = wqi_size / sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100416 struct intel_engine_cs *engine = rq->engine;
Oscar Mateoada8c412017-09-12 14:36:37 -0700417 struct i915_gem_context *ctx = rq->ctx;
Chris Wilsonbd00e732017-03-23 23:00:00 +0000418 struct guc_process_desc *desc = __get_process_desc(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100419 struct guc_wq_item *wqi;
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200420 u32 ring_tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100421
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200422 lockdep_assert_held(&client->wq_lock);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100423
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200424 ring_tail = intel_ring_set_tail(rq->ring, rq->tail) / sizeof(u64);
425 GEM_BUG_ON(ring_tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100426
427 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
428 * should not have the case where structure wqi is across page, neither
429 * wrapped to the beginning. This simplifies the implementation below.
430 *
431 * XXX: if not the case, we need save data to a temp wqi and copy it to
432 * workqueue buffer dw by dw.
433 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100434 BUILD_BUG_ON(wqi_size != 16);
Dave Gordon44a28b12015-08-12 15:43:41 +0100435
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200436 /* Free space is guaranteed. */
437 wq_off = READ_ONCE(desc->tail);
438 GEM_BUG_ON(CIRC_SPACE(wq_off, READ_ONCE(desc->head),
439 GUC_WQ_SIZE) < wqi_size);
Chris Wilsondadd4812016-09-09 14:11:57 +0100440 GEM_BUG_ON(wq_off & (wqi_size - 1));
Dave Gordon0a31afb2016-05-13 15:36:34 +0100441
442 /* WQ starts from the page after doorbell / process_desc */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000443 wqi = client->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100444
Dave Gordon0a31afb2016-05-13 15:36:34 +0100445 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100446 wqi->header = WQ_TYPE_INORDER |
Oscar Mateoada8c412017-09-12 14:36:37 -0700447 (wqi_len << WQ_LEN_SHIFT) |
448 (engine->guc_id << WQ_TARGET_SHIFT) |
449 WQ_NO_WCFLUSH_WAIT;
Dave Gordon44a28b12015-08-12 15:43:41 +0100450
Oscar Mateoada8c412017-09-12 14:36:37 -0700451 wqi->context_desc = lower_32_bits(intel_lr_context_descriptor(ctx, engine));
Dave Gordon44a28b12015-08-12 15:43:41 +0100452
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200453 wqi->submit_element_info = ring_tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100454 wqi->fence_id = rq->global_seqno;
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200455
456 /* Postincrement WQ tail for next time. */
457 WRITE_ONCE(desc->tail, (wq_off + wqi_size) & (GUC_WQ_SIZE - 1));
Dave Gordon44a28b12015-08-12 15:43:41 +0100458}
459
Oscar Mateo397fce82017-03-22 10:39:52 -0700460static void guc_reset_wq(struct i915_guc_client *client)
461{
Chris Wilsonbd00e732017-03-23 23:00:00 +0000462 struct guc_process_desc *desc = __get_process_desc(client);
Oscar Mateo397fce82017-03-22 10:39:52 -0700463
464 desc->head = 0;
465 desc->tail = 0;
Oscar Mateo397fce82017-03-22 10:39:52 -0700466}
467
Michał Winiarski59db36c2017-09-14 12:51:23 +0200468static void guc_ring_doorbell(struct i915_guc_client *client)
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100469{
Michał Winiarski59db36c2017-09-14 12:51:23 +0200470 struct guc_doorbell_info *db;
471 u32 cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100472
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200473 lockdep_assert_held(&client->wq_lock);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100474
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100475 /* pointer of current doorbell cacheline */
Michał Winiarski59db36c2017-09-14 12:51:23 +0200476 db = __get_doorbell(client);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100477
Michał Winiarski59db36c2017-09-14 12:51:23 +0200478 /* we're not expecting the doorbell cookie to change behind our back */
479 cookie = READ_ONCE(db->cookie);
480 WARN_ON_ONCE(xchg(&db->cookie, cookie + 1) != cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100481
Michał Winiarski59db36c2017-09-14 12:51:23 +0200482 /* XXX: doorbell was lost and need to acquire it again */
483 GEM_BUG_ON(db->db_status != GUC_DOORBELL_ENABLED);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100484}
485
Dave Gordon44a28b12015-08-12 15:43:41 +0100486/**
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200487 * i915_guc_submit() - Submit commands through GuC
488 * @engine: engine associated with the commands
Dave Gordon7c2c2702016-05-13 15:36:32 +0100489 *
490 * The only error here arises if the doorbell hardware isn't functioning
491 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100492 */
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200493static void i915_guc_submit(struct intel_engine_cs *engine)
Dave Gordon44a28b12015-08-12 15:43:41 +0100494{
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200495 struct drm_i915_private *dev_priv = engine->i915;
496 struct intel_guc *guc = &dev_priv->guc;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100497 struct i915_guc_client *client = guc->execbuf_client;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300498 struct intel_engine_execlists * const execlists = &engine->execlists;
499 struct execlist_port *port = execlists->port;
500 const unsigned int engine_id = engine->id;
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200501 unsigned int n;
Dave Gordon44a28b12015-08-12 15:43:41 +0100502
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300503 for (n = 0; n < execlists_num_ports(execlists); n++) {
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200504 struct drm_i915_gem_request *rq;
505 unsigned int count;
Akash Goeled4596ea2016-10-25 22:05:23 +0530506
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200507 rq = port_unpack(&port[n], &count);
508 if (rq && count == 0) {
509 port_set(&port[n], port_pack(rq, ++count));
Chris Wilson0c335182017-02-28 11:28:03 +0000510
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200511 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
512 POSTING_READ_FW(GUC_STATUS);
Dave Gordon44a28b12015-08-12 15:43:41 +0100513
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200514 spin_lock(&client->wq_lock);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100515
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200516 guc_wq_item_append(client, rq);
Michał Winiarski59db36c2017-09-14 12:51:23 +0200517 guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100518
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200519 client->submissions[engine_id] += 1;
520
521 spin_unlock(&client->wq_lock);
522 }
523 }
Chris Wilson34ba5a82016-11-29 12:10:24 +0000524}
525
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100526static void port_assign(struct execlist_port *port,
527 struct drm_i915_gem_request *rq)
528{
529 GEM_BUG_ON(rq == port_request(port));
530
531 if (port_isset(port))
532 i915_gem_request_put(port_request(port));
533
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200534 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100535}
536
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200537static void i915_guc_dequeue(struct intel_engine_cs *engine)
Chris Wilson31de7352017-03-16 12:56:18 +0000538{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300539 struct intel_engine_execlists * const execlists = &engine->execlists;
540 struct execlist_port *port = execlists->port;
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200541 struct drm_i915_gem_request *last = NULL;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300542 const struct execlist_port * const last_port =
543 &execlists->port[execlists->port_mask];
Chris Wilson31de7352017-03-16 12:56:18 +0000544 bool submit = false;
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200545 struct rb_node *rb;
546
547 if (port_isset(port))
548 port++;
Chris Wilson31de7352017-03-16 12:56:18 +0000549
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000550 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300551 rb = execlists->first;
552 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilson31de7352017-03-16 12:56:18 +0000553 while (rb) {
Chris Wilson6c067572017-05-17 13:10:03 +0100554 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
555 struct drm_i915_gem_request *rq, *rn;
Chris Wilson31de7352017-03-16 12:56:18 +0000556
Chris Wilson6c067572017-05-17 13:10:03 +0100557 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
558 if (last && rq->ctx != last->ctx) {
Mika Kuoppala76e70082017-09-22 15:43:07 +0300559 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100560 __list_del_many(&p->requests,
561 &rq->priotree.link);
562 goto done;
563 }
Chris Wilson31de7352017-03-16 12:56:18 +0000564
Michał Winiarskif63078a2017-05-23 12:23:59 +0200565 if (submit)
566 port_assign(port, last);
Chris Wilson6c067572017-05-17 13:10:03 +0100567 port++;
568 }
569
570 INIT_LIST_HEAD(&rq->priotree.link);
571 rq->priotree.priority = INT_MAX;
572
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200573 __i915_gem_request_submit(rq);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300574 trace_i915_gem_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100575 last = rq;
576 submit = true;
Chris Wilson31de7352017-03-16 12:56:18 +0000577 }
578
579 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300580 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100581 INIT_LIST_HEAD(&p->requests);
582 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100583 kmem_cache_free(engine->i915->priorities, p);
Chris Wilson31de7352017-03-16 12:56:18 +0000584 }
Chris Wilson6c067572017-05-17 13:10:03 +0100585done:
Mika Kuoppalab620e872017-09-22 15:43:03 +0300586 execlists->first = rb;
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200587 if (submit) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100588 port_assign(port, last);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100589 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200590 i915_guc_submit(engine);
591 }
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000592 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson31de7352017-03-16 12:56:18 +0000593}
594
595static void i915_guc_irq_handler(unsigned long data)
596{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300597 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300598 struct intel_engine_execlists * const execlists = &engine->execlists;
599 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300600 const struct execlist_port * const last_port =
601 &execlists->port[execlists->port_mask];
Chris Wilson31de7352017-03-16 12:56:18 +0000602 struct drm_i915_gem_request *rq;
Chris Wilson31de7352017-03-16 12:56:18 +0000603
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200604 rq = port_request(&port[0]);
605 while (rq && i915_gem_request_completed(rq)) {
606 trace_i915_gem_request_out(rq);
607 i915_gem_request_put(rq);
608
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300609 execlists_port_complete(execlists, port);
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200610
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100611 rq = port_request(&port[0]);
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200612 }
Chris Wilson4a118ec2017-10-23 22:32:36 +0100613 if (!rq)
614 execlists_clear_active(execlists, EXECLISTS_ACTIVE_USER);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100615
Mika Kuoppala76e70082017-09-22 15:43:07 +0300616 if (!port_isset(last_port))
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200617 i915_guc_dequeue(engine);
Chris Wilson31de7352017-03-16 12:56:18 +0000618}
619
Dave Gordon44a28b12015-08-12 15:43:41 +0100620/*
621 * Everything below here is concerned with setup & teardown, and is
622 * therefore not part of the somewhat time-critical batch-submission
623 * path of i915_guc_submit() above.
624 */
625
Dave Gordon84b7f882016-08-09 15:19:20 +0100626/* Check that a doorbell register is in the expected state */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700627static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
Dave Gordon84b7f882016-08-09 15:19:20 +0100628{
629 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700630 u32 drbregl;
631 bool valid;
Dave Gordon84b7f882016-08-09 15:19:20 +0100632
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700633 GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
634
635 drbregl = I915_READ(GEN8_DRBREGL(db_id));
636 valid = drbregl & GEN8_DRB_VALID;
637
638 if (test_bit(db_id, guc->doorbell_bitmap) == valid)
Dave Gordon84b7f882016-08-09 15:19:20 +0100639 return true;
640
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700641 DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
642 db_id, drbregl, yesno(valid));
Dave Gordon84b7f882016-08-09 15:19:20 +0100643
644 return false;
645}
646
Dave Gordon4d757872016-06-13 17:57:34 +0100647/*
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700648 * If the GuC thinks that the doorbell is unassigned (e.g. because we reset and
649 * reloaded the GuC FW) we can use this function to tell the GuC to reassign the
650 * doorbell to the rightful owner.
651 */
652static int __reset_doorbell(struct i915_guc_client* client, u16 db_id)
653{
654 int err;
655
Oscar Mateo397fce82017-03-22 10:39:52 -0700656 __update_doorbell_desc(client, db_id);
657 err = __create_doorbell(client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700658 if (!err)
659 err = __destroy_doorbell(client);
660
661 return err;
662}
663
664/*
Oscar Mateo397fce82017-03-22 10:39:52 -0700665 * Set up & tear down each unused doorbell in turn, to ensure that all doorbell
666 * HW is (re)initialised. For that end, we might have to borrow the first
667 * client. Also, tell GuC about all the doorbells in use by all clients.
668 * We do this because the KMD, the GuC and the doorbell HW can easily go out of
669 * sync (e.g. we can reset the GuC, but not the doorbel HW).
Dave Gordon4d757872016-06-13 17:57:34 +0100670 */
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700671static int guc_init_doorbell_hw(struct intel_guc *guc)
Dave Gordon4d757872016-06-13 17:57:34 +0100672{
Dave Gordon4d757872016-06-13 17:57:34 +0100673 struct i915_guc_client *client = guc->execbuf_client;
Oscar Mateo397fce82017-03-22 10:39:52 -0700674 bool recreate_first_client = false;
675 u16 db_id;
676 int ret;
Dave Gordon4d757872016-06-13 17:57:34 +0100677
Oscar Mateo397fce82017-03-22 10:39:52 -0700678 /* For unused doorbells, make sure they are disabled */
679 for_each_clear_bit(db_id, guc->doorbell_bitmap, GUC_NUM_DOORBELLS) {
680 if (doorbell_ok(guc, db_id))
Dave Gordon8888cd02016-08-09 15:19:19 +0100681 continue;
682
Oscar Mateo397fce82017-03-22 10:39:52 -0700683 if (has_doorbell(client)) {
684 /* Borrow execbuf_client (we will recreate it later) */
685 destroy_doorbell(client);
686 recreate_first_client = true;
687 }
688
689 ret = __reset_doorbell(client, db_id);
690 WARN(ret, "Doorbell %u reset failed, err %d\n", db_id, ret);
Dave Gordon4d757872016-06-13 17:57:34 +0100691 }
692
Oscar Mateo397fce82017-03-22 10:39:52 -0700693 if (recreate_first_client) {
694 ret = __reserve_doorbell(client);
695 if (unlikely(ret)) {
696 DRM_ERROR("Couldn't re-reserve first client db: %d\n", ret);
697 return ret;
698 }
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700699
Oscar Mateo397fce82017-03-22 10:39:52 -0700700 __update_doorbell_desc(client, client->doorbell_id);
701 }
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700702
Oscar Mateo397fce82017-03-22 10:39:52 -0700703 /* Now for every client (and not only execbuf_client) make sure their
704 * doorbells are known by the GuC */
705 //for (client = client_list; client != NULL; client = client->next)
706 {
707 ret = __create_doorbell(client);
708 if (ret) {
709 DRM_ERROR("Couldn't recreate client %u doorbell: %d\n",
Oscar Mateob09935a2017-03-22 10:39:53 -0700710 client->stage_id, ret);
Oscar Mateo397fce82017-03-22 10:39:52 -0700711 return ret;
712 }
713 }
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700714
Oscar Mateo397fce82017-03-22 10:39:52 -0700715 /* Read back & verify all (used & unused) doorbell registers */
716 for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
717 WARN_ON(!doorbell_ok(guc, db_id));
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700718
719 return 0;
Dave Gordon4d757872016-06-13 17:57:34 +0100720}
721
Dave Gordon44a28b12015-08-12 15:43:41 +0100722/**
723 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100724 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100725 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100726 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
727 * The kernel client to replace ExecList submission is created with
728 * NORMAL priority. Priority of a client for scheduler can be HIGH,
729 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700730 * @ctx: the context that owns the client (we use the default render
731 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100732 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100733 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100734 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100735static struct i915_guc_client *
736guc_client_alloc(struct drm_i915_private *dev_priv,
Joonas Lahtinenfaf65482017-10-06 11:49:40 +0300737 u32 engines,
738 u32 priority,
Dave Gordon0daf5562016-06-10 18:29:25 +0100739 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100740{
741 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100742 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100743 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000744 void *vaddr;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700745 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100746
747 client = kzalloc(sizeof(*client), GFP_KERNEL);
748 if (!client)
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700749 return ERR_PTR(-ENOMEM);
Dave Gordon44a28b12015-08-12 15:43:41 +0100750
Dave Gordon44a28b12015-08-12 15:43:41 +0100751 client->guc = guc;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700752 client->owner = ctx;
Dave Gordone02757d2016-08-09 15:19:21 +0100753 client->engines = engines;
754 client->priority = priority;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700755 client->doorbell_id = GUC_DOORBELL_INVALID;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700756 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100757
Oscar Mateob09935a2017-03-22 10:39:53 -0700758 ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700759 GFP_KERNEL);
760 if (ret < 0)
761 goto err_client;
762
Oscar Mateob09935a2017-03-22 10:39:53 -0700763 client->stage_id = ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100764
765 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000766 vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700767 if (IS_ERR(vma)) {
768 ret = PTR_ERR(vma);
769 goto err_id;
770 }
Dave Gordon44a28b12015-08-12 15:43:41 +0100771
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100772 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100773 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000774
775 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700776 if (IS_ERR(vaddr)) {
777 ret = PTR_ERR(vaddr);
778 goto err_vma;
779 }
Chris Wilson72aa0d82016-11-02 17:50:47 +0000780 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100781
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700782 client->doorbell_offset = __select_cacheline(guc);
Dave Gordon44a28b12015-08-12 15:43:41 +0100783
784 /*
785 * Since the doorbell only requires a single cacheline, we can save
786 * space by putting the application process descriptor in the same
787 * page. Use the half of the page that doesn't include the doorbell.
788 */
789 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
790 client->proc_desc_offset = 0;
791 else
792 client->proc_desc_offset = (GUC_DB_SIZE / 2);
793
Dave Gordon7a9347f2016-09-12 21:19:37 +0100794 guc_proc_desc_init(guc, client);
Oscar Mateob09935a2017-03-22 10:39:53 -0700795 guc_stage_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000796
Oscar Mateo397fce82017-03-22 10:39:52 -0700797 ret = create_doorbell(client);
798 if (ret)
799 goto err_vaddr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100800
Oscar Mateob09935a2017-03-22 10:39:53 -0700801 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
802 priority, client, client->engines, client->stage_id);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700803 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
804 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100805
806 return client;
Oscar Mateo397fce82017-03-22 10:39:52 -0700807
808err_vaddr:
809 i915_gem_object_unpin_map(client->vma->obj);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700810err_vma:
811 i915_vma_unpin_and_release(&client->vma);
812err_id:
Oscar Mateob09935a2017-03-22 10:39:53 -0700813 ida_simple_remove(&guc->stage_ids, client->stage_id);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700814err_client:
815 kfree(client);
Joonas Lahtinenabddffd2017-03-22 10:39:44 -0700816 return ERR_PTR(ret);
Dave Gordon44a28b12015-08-12 15:43:41 +0100817}
818
Oscar Mateo397fce82017-03-22 10:39:52 -0700819static void guc_client_free(struct i915_guc_client *client)
820{
821 /*
822 * XXX: wait for any outstanding submissions before freeing memory.
823 * Be sure to drop any locks
824 */
825
826 /* FIXME: in many cases, by the time we get here the GuC has been
827 * reset, so we cannot destroy the doorbell properly. Ignore the
828 * error message for now */
829 destroy_doorbell(client);
Oscar Mateob09935a2017-03-22 10:39:53 -0700830 guc_stage_desc_fini(client->guc, client);
Oscar Mateo397fce82017-03-22 10:39:52 -0700831 i915_gem_object_unpin_map(client->vma->obj);
832 i915_vma_unpin_and_release(&client->vma);
Oscar Mateob09935a2017-03-22 10:39:53 -0700833 ida_simple_remove(&client->guc->stage_ids, client->stage_id);
Oscar Mateo397fce82017-03-22 10:39:52 -0700834 kfree(client);
835}
836
Oscar Mateoe9eb8032017-09-12 14:36:35 -0700837static void guc_policy_init(struct guc_policy *policy)
838{
839 policy->execution_quantum = POLICY_DEFAULT_EXECUTION_QUANTUM_US;
840 policy->preemption_time = POLICY_DEFAULT_PREEMPTION_TIME_US;
841 policy->fault_time = POLICY_DEFAULT_FAULT_TIME_US;
842 policy->policy_flags = 0;
843}
844
Dave Gordon7a9347f2016-09-12 21:19:37 +0100845static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -0800846{
847 struct guc_policy *policy;
848 u32 p, i;
849
Oscar Mateoe9eb8032017-09-12 14:36:35 -0700850 policies->dpc_promote_time = POLICY_DEFAULT_DPC_PROMOTE_TIME_US;
Alex Dai463704d2015-12-18 12:00:10 -0800851 policies->max_num_work_items = POLICY_MAX_NUM_WI;
852
Oscar Mateob09935a2017-03-22 10:39:53 -0700853 for (p = 0; p < GUC_CLIENT_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -0800854 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -0800855 policy = &policies->policy[p][i];
856
Oscar Mateoe9eb8032017-09-12 14:36:35 -0700857 guc_policy_init(policy);
Alex Dai463704d2015-12-18 12:00:10 -0800858 }
859 }
860
861 policies->is_valid = 1;
862}
863
Michel Thierrya922c0c2017-09-13 09:56:01 +0100864/*
865 * The first 80 dwords of the register state context, containing the
866 * execlists and ppgtt registers.
867 */
868#define LR_HW_CONTEXT_SIZE (80 * sizeof(u32))
869
Oscar Mateo0704df22017-03-22 10:39:47 -0700870static int guc_ads_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -0800871{
872 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +0100873 struct i915_vma *vma;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000874 struct page *page;
875 /* The ads obj includes the struct itself and buffers passed to GuC */
876 struct {
877 struct guc_ads ads;
878 struct guc_policies policies;
879 struct guc_mmio_reg_state reg_state;
880 u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
881 } __packed *blob;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000882 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530883 enum intel_engine_id id;
Michel Thierrya922c0c2017-09-13 09:56:01 +0100884 const u32 skipped_offset = LRC_HEADER_PAGES * PAGE_SIZE;
885 const u32 skipped_size = LRC_PPHWSP_SZ * PAGE_SIZE + LR_HW_CONTEXT_SIZE;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000886 u32 base;
Alex Dai68371a92015-12-18 12:00:09 -0800887
Oscar Mateo3950bf32017-03-22 10:39:46 -0700888 GEM_BUG_ON(guc->ads_vma);
Alex Dai68371a92015-12-18 12:00:09 -0800889
Oscar Mateo3950bf32017-03-22 10:39:46 -0700890 vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
891 if (IS_ERR(vma))
892 return PTR_ERR(vma);
893
894 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -0800895
Chris Wilson8b797af2016-08-15 10:48:51 +0100896 page = i915_vma_first_page(vma);
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000897 blob = kmap(page);
898
899 /* GuC scheduling policies */
900 guc_policies_init(&blob->policies);
901
902 /* MMIO reg state */
903 for_each_engine(engine, dev_priv, id) {
Oscar Mateo35815ea2017-03-22 10:39:54 -0700904 blob->reg_state.white_list[engine->guc_id].mmio_start =
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000905 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
906
907 /* Nothing to be saved or restored for now. */
Oscar Mateo35815ea2017-03-22 10:39:54 -0700908 blob->reg_state.white_list[engine->guc_id].count = 0;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000909 }
Alex Dai68371a92015-12-18 12:00:09 -0800910
911 /*
912 * The GuC requires a "Golden Context" when it reinitialises
913 * engines after a reset. Here we use the Render ring default
914 * context, which must already exist and be pinned in the GGTT,
915 * so its address won't change after we've told the GuC where
Michel Thierrya922c0c2017-09-13 09:56:01 +0100916 * to find it. Note that we have to skip our header (1 page),
917 * because our GuC shared data is there.
Alex Dai68371a92015-12-18 12:00:09 -0800918 */
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000919 blob->ads.golden_context_lrca =
Michel Thierrya922c0c2017-09-13 09:56:01 +0100920 guc_ggtt_offset(dev_priv->kernel_context->engine[RCS].state) + skipped_offset;
Alex Dai68371a92015-12-18 12:00:09 -0800921
Michel Thierrya922c0c2017-09-13 09:56:01 +0100922 /*
923 * The GuC expects us to exclude the portion of the context image that
924 * it skips from the size it is to read. It starts reading from after
925 * the execlist context (so skipping the first page [PPHWSP] and 80
926 * dwords). Weird guc is weird.
927 */
Akash Goel3b3f1652016-10-13 22:44:48 +0530928 for_each_engine(engine, dev_priv, id)
Michel Thierrya922c0c2017-09-13 09:56:01 +0100929 blob->ads.eng_state_size[engine->guc_id] = engine->context_size - skipped_size;
Alex Dai68371a92015-12-18 12:00:09 -0800930
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000931 base = guc_ggtt_offset(vma);
932 blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
933 blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
934 blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
Alex Dai5c148e02015-12-18 12:00:11 -0800935
Alex Dai68371a92015-12-18 12:00:09 -0800936 kunmap(page);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700937
938 return 0;
939}
940
Oscar Mateo0704df22017-03-22 10:39:47 -0700941static void guc_ads_destroy(struct intel_guc *guc)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700942{
943 i915_vma_unpin_and_release(&guc->ads_vma);
Alex Dai68371a92015-12-18 12:00:09 -0800944}
945
Alex Daibac427f2015-08-12 15:43:39 +0100946/*
Oscar Mateo397fce82017-03-22 10:39:52 -0700947 * Set up the memory resources to be shared with the GuC (via the GGTT)
948 * at firmware loading time.
Alex Daibac427f2015-08-12 15:43:39 +0100949 */
Dave Gordonbeffa512016-06-10 18:29:26 +0100950int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +0100951{
Alex Daibac427f2015-08-12 15:43:39 +0100952 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100953 struct i915_vma *vma;
Oscar Mateo73b05532017-03-22 10:39:45 -0700954 void *vaddr;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700955 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100956
Oscar Mateob09935a2017-03-22 10:39:53 -0700957 if (guc->stage_desc_pool)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700958 return 0;
Alex Daibac427f2015-08-12 15:43:39 +0100959
Oscar Mateob09935a2017-03-22 10:39:53 -0700960 vma = intel_guc_allocate_vma(guc,
961 PAGE_ALIGN(sizeof(struct guc_stage_desc) *
962 GUC_MAX_STAGE_DESCRIPTORS));
Chris Wilson8b797af2016-08-15 10:48:51 +0100963 if (IS_ERR(vma))
964 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +0100965
Oscar Mateob09935a2017-03-22 10:39:53 -0700966 guc->stage_desc_pool = vma;
Oscar Mateo73b05532017-03-22 10:39:45 -0700967
Oscar Mateob09935a2017-03-22 10:39:53 -0700968 vaddr = i915_gem_object_pin_map(guc->stage_desc_pool->obj, I915_MAP_WB);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700969 if (IS_ERR(vaddr)) {
970 ret = PTR_ERR(vaddr);
971 goto err_vma;
972 }
Oscar Mateo73b05532017-03-22 10:39:45 -0700973
Oscar Mateob09935a2017-03-22 10:39:53 -0700974 guc->stage_desc_pool_vaddr = vaddr;
Oscar Mateo73b05532017-03-22 10:39:45 -0700975
Oscar Mateo3950bf32017-03-22 10:39:46 -0700976 ret = intel_guc_log_create(guc);
977 if (ret < 0)
978 goto err_vaddr;
979
Oscar Mateo0704df22017-03-22 10:39:47 -0700980 ret = guc_ads_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700981 if (ret < 0)
982 goto err_log;
983
Oscar Mateob09935a2017-03-22 10:39:53 -0700984 ida_init(&guc->stage_ids);
Alex Dai68371a92015-12-18 12:00:09 -0800985
Alex Daibac427f2015-08-12 15:43:39 +0100986 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +0000987
Oscar Mateo3950bf32017-03-22 10:39:46 -0700988err_log:
989 intel_guc_log_destroy(guc);
990err_vaddr:
Oscar Mateob09935a2017-03-22 10:39:53 -0700991 i915_gem_object_unpin_map(guc->stage_desc_pool->obj);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700992err_vma:
Oscar Mateob09935a2017-03-22 10:39:53 -0700993 i915_vma_unpin_and_release(&guc->stage_desc_pool);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700994 return ret;
995}
996
997void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
998{
999 struct intel_guc *guc = &dev_priv->guc;
1000
Oscar Mateob09935a2017-03-22 10:39:53 -07001001 ida_destroy(&guc->stage_ids);
Oscar Mateo0704df22017-03-22 10:39:47 -07001002 guc_ads_destroy(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -07001003 intel_guc_log_destroy(guc);
Oscar Mateob09935a2017-03-22 10:39:53 -07001004 i915_gem_object_unpin_map(guc->stage_desc_pool->obj);
1005 i915_vma_unpin_and_release(&guc->stage_desc_pool);
Chris Wilson4d357af2016-11-29 12:10:23 +00001006}
1007
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001008static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
1009{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01001010 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001011 struct intel_engine_cs *engine;
1012 enum intel_engine_id id;
1013 int irqs;
1014
1015 /* tell all command streamers to forward interrupts (but not vblank) to GuC */
1016 irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
1017 for_each_engine(engine, dev_priv, id)
1018 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1019
1020 /* route USER_INTERRUPT to Host, all others are sent to GuC. */
1021 irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
1022 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
1023 /* These three registers have the same bit definitions */
1024 I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
1025 I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
1026 I915_WRITE(GUC_WD_VECS_IER, ~irqs);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301027
1028 /*
1029 * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
1030 * (unmasked) PM interrupts to the GuC. All other bits of this
1031 * register *disable* generation of a specific interrupt.
1032 *
1033 * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
1034 * writing to the PM interrupt mask register, i.e. interrupts
1035 * that must not be disabled.
1036 *
1037 * If the GuC is handling these interrupts, then we must not let
1038 * the PM code disable ANY interrupt that the GuC is expecting.
1039 * So for each ENABLED (0) bit in this register, we must SET the
1040 * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
1041 * GuC needs ARAT expired interrupt unmasked hence it is set in
1042 * pm_intrmsk_mbz.
1043 *
1044 * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1045 * result in the register bit being left SET!
1046 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01001047 rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
1048 rps->pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001049}
1050
Oscar Mateo618ef002017-03-22 10:39:55 -07001051static void guc_interrupts_release(struct drm_i915_private *dev_priv)
1052{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01001053 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Oscar Mateo618ef002017-03-22 10:39:55 -07001054 struct intel_engine_cs *engine;
1055 enum intel_engine_id id;
1056 int irqs;
1057
1058 /*
1059 * tell all command streamers NOT to forward interrupts or vblank
1060 * to GuC.
1061 */
1062 irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
1063 irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
1064 for_each_engine(engine, dev_priv, id)
1065 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1066
1067 /* route all GT interrupts to the host */
1068 I915_WRITE(GUC_BCS_RCS_IER, 0);
1069 I915_WRITE(GUC_VCS2_VCS1_IER, 0);
1070 I915_WRITE(GUC_WD_VECS_IER, 0);
1071
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01001072 rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
1073 rps->pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
Oscar Mateo618ef002017-03-22 10:39:55 -07001074}
1075
Chris Wilsonbcbd5c32017-10-25 15:39:42 +01001076static void i915_guc_submission_park(struct intel_engine_cs *engine)
1077{
1078 intel_engine_unpin_breadcrumbs_irq(engine);
1079}
1080
1081static void i915_guc_submission_unpark(struct intel_engine_cs *engine)
1082{
1083 intel_engine_pin_breadcrumbs_irq(engine);
1084}
1085
Dave Gordonbeffa512016-06-10 18:29:26 +01001086int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001087{
Dave Gordon44a28b12015-08-12 15:43:41 +01001088 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001089 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001090 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301091 enum intel_engine_id id;
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001092 int err;
Dave Gordon44a28b12015-08-12 15:43:41 +01001093
Michał Winiarski85e2fe62017-09-14 10:32:13 +02001094 /*
1095 * We're using GuC work items for submitting work through GuC. Since
1096 * we're coalescing multiple requests from a single context into a
1097 * single work item prior to assigning it to execlist_port, we can
1098 * never have more work items than the total number of ports (for all
1099 * engines). The GuC firmware is controlling the HEAD of work queue,
1100 * and it is guaranteed that it will remove the work item from the
1101 * queue before our request is completed.
1102 */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001103 BUILD_BUG_ON(ARRAY_SIZE(engine->execlists.port) *
Michał Winiarski85e2fe62017-09-14 10:32:13 +02001104 sizeof(struct guc_wq_item) *
1105 I915_NUM_ENGINES > GUC_WQ_SIZE);
1106
Oscar Mateo397fce82017-03-22 10:39:52 -07001107 if (!client) {
1108 client = guc_client_alloc(dev_priv,
1109 INTEL_INFO(dev_priv)->ring_mask,
Oscar Mateob09935a2017-03-22 10:39:53 -07001110 GUC_CLIENT_PRIORITY_KMD_NORMAL,
Oscar Mateo397fce82017-03-22 10:39:52 -07001111 dev_priv->kernel_context);
1112 if (IS_ERR(client)) {
1113 DRM_ERROR("Failed to create GuC client for execbuf!\n");
1114 return PTR_ERR(client);
1115 }
1116
1117 guc->execbuf_client = client;
1118 }
Dave Gordon44a28b12015-08-12 15:43:41 +01001119
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001120 err = intel_guc_sample_forcewake(guc);
1121 if (err)
Oscar Mateo397fce82017-03-22 10:39:52 -07001122 goto err_execbuf_client;
Chris Wilson4d357af2016-11-29 12:10:23 +00001123
1124 guc_reset_wq(client);
Oscar Mateo397fce82017-03-22 10:39:52 -07001125
Joonas Lahtinenabddffd2017-03-22 10:39:44 -07001126 err = guc_init_doorbell_hw(guc);
1127 if (err)
Oscar Mateo397fce82017-03-22 10:39:52 -07001128 goto err_execbuf_client;
Alex Daif5d3c3e2015-08-18 14:34:47 -07001129
Chris Wilsonddd66c52016-08-02 22:50:31 +01001130 /* Take over from manual control of ELSP (execlists) */
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001131 guc_interrupts_capture(dev_priv);
1132
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001133 for_each_engine(engine, dev_priv, id) {
Mika Kuoppalab620e872017-09-22 15:43:03 +03001134 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson31de7352017-03-16 12:56:18 +00001135 /* The tasklet was initialised by execlists, and may be in
1136 * a state of flux (across a reset) and so we just want to
1137 * take over the callback without changing any other state
1138 * in the tasklet.
1139 */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001140 execlists->irq_tasklet.func = i915_guc_irq_handler;
Chris Wilson31de7352017-03-16 12:56:18 +00001141 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001142 tasklet_schedule(&execlists->irq_tasklet);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +01001143
1144 engine->park = i915_guc_submission_park;
1145 engine->unpark = i915_guc_submission_unpark;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001146 }
1147
Dave Gordon44a28b12015-08-12 15:43:41 +01001148 return 0;
Oscar Mateo397fce82017-03-22 10:39:52 -07001149
1150err_execbuf_client:
1151 guc_client_free(guc->execbuf_client);
1152 guc->execbuf_client = NULL;
1153 return err;
Dave Gordon44a28b12015-08-12 15:43:41 +01001154}
1155
Dave Gordonbeffa512016-06-10 18:29:26 +01001156void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001157{
Dave Gordon44a28b12015-08-12 15:43:41 +01001158 struct intel_guc *guc = &dev_priv->guc;
1159
Chris Wilsonbcbd5c32017-10-25 15:39:42 +01001160 GEM_BUG_ON(dev_priv->gt.awake); /* GT should be parked first */
1161
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301162 guc_interrupts_release(dev_priv);
1163
Chris Wilsonddd66c52016-08-02 22:50:31 +01001164 /* Revert back to manual ELSP submission */
Chris Wilsonff44ad52017-03-16 17:13:03 +00001165 intel_engines_reset_default_submission(dev_priv);
Oscar Mateo397fce82017-03-22 10:39:52 -07001166
1167 guc_client_free(guc->execbuf_client);
1168 guc->execbuf_client = NULL;
Dave Gordon44a28b12015-08-12 15:43:41 +01001169}