blob: 832ac9e45801370f9ec444221d0ae4968b6959fc [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
67/*
Dave Gordon44a28b12015-08-12 15:43:41 +010068 * Tell the GuC to allocate or deallocate a specific doorbell
69 */
70
Arkadiusz Hilera80bc452016-11-25 18:59:34 +010071static int guc_allocate_doorbell(struct intel_guc *guc,
72 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +010073{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010074 u32 action[] = {
75 INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
76 client->ctx_index
77 };
Dave Gordon44a28b12015-08-12 15:43:41 +010078
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010079 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Dave Gordon44a28b12015-08-12 15:43:41 +010080}
81
Arkadiusz Hilera80bc452016-11-25 18:59:34 +010082static int guc_release_doorbell(struct intel_guc *guc,
83 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +010084{
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010085 u32 action[] = {
86 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
87 client->ctx_index
88 };
Dave Gordon44a28b12015-08-12 15:43:41 +010089
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010090 return intel_guc_send(guc, action, ARRAY_SIZE(action));
Sagar Arun Kamble685534e2016-10-12 21:54:41 +053091}
92
Dave Gordon44a28b12015-08-12 15:43:41 +010093/*
94 * Initialise, update, or clear doorbell data shared with the GuC
95 *
96 * These functions modify shared data and so need access to the mapped
97 * client object which contains the page being used for the doorbell
98 */
99
Dave Gordona6674292016-06-13 17:57:32 +0100100static int guc_update_doorbell_id(struct intel_guc *guc,
101 struct i915_guc_client *client,
102 u16 new_id)
Dave Gordon44a28b12015-08-12 15:43:41 +0100103{
Chris Wilson8b797af2016-08-15 10:48:51 +0100104 struct sg_table *sg = guc->ctx_pool_vma->pages;
Dave Gordona6674292016-06-13 17:57:32 +0100105 void *doorbell_bitmap = guc->doorbell_bitmap;
Dave Gordon44a28b12015-08-12 15:43:41 +0100106 struct guc_doorbell_info *doorbell;
Dave Gordona6674292016-06-13 17:57:32 +0100107 struct guc_context_desc desc;
108 size_t len;
Dave Gordon44a28b12015-08-12 15:43:41 +0100109
Chris Wilson72aa0d82016-11-02 17:50:47 +0000110 doorbell = client->vaddr + client->doorbell_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100111
Dave Gordona6674292016-06-13 17:57:32 +0100112 if (client->doorbell_id != GUC_INVALID_DOORBELL_ID &&
113 test_bit(client->doorbell_id, doorbell_bitmap)) {
114 /* Deactivate the old doorbell */
115 doorbell->db_status = GUC_DOORBELL_DISABLED;
Arkadiusz Hilera80bc452016-11-25 18:59:34 +0100116 (void)guc_release_doorbell(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100117 __clear_bit(client->doorbell_id, doorbell_bitmap);
118 }
119
120 /* Update the GuC's idea of the doorbell ID */
121 len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
122 sizeof(desc) * client->ctx_index);
123 if (len != sizeof(desc))
124 return -EFAULT;
125 desc.db_id = new_id;
126 len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
127 sizeof(desc) * client->ctx_index);
128 if (len != sizeof(desc))
129 return -EFAULT;
130
131 client->doorbell_id = new_id;
132 if (new_id == GUC_INVALID_DOORBELL_ID)
133 return 0;
134
135 /* Activate the new doorbell */
136 __set_bit(new_id, doorbell_bitmap);
Dave Gordona6674292016-06-13 17:57:32 +0100137 doorbell->db_status = GUC_DOORBELL_ENABLED;
Chris Wilson597bdc82016-11-29 12:10:22 +0000138 doorbell->cookie = client->doorbell_cookie;
Arkadiusz Hilera80bc452016-11-25 18:59:34 +0100139 return guc_allocate_doorbell(guc, client);
Dave Gordona6674292016-06-13 17:57:32 +0100140}
141
Dave Gordon44a28b12015-08-12 15:43:41 +0100142static void guc_disable_doorbell(struct intel_guc *guc,
143 struct i915_guc_client *client)
144{
Dave Gordona6674292016-06-13 17:57:32 +0100145 (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
Dave Gordon44a28b12015-08-12 15:43:41 +0100146
Dave Gordon44a28b12015-08-12 15:43:41 +0100147 /* XXX: wait for any interrupts */
148 /* XXX: wait for workqueue to drain */
149}
150
Dave Gordonf10d69a2016-06-13 17:57:33 +0100151static uint16_t
152select_doorbell_register(struct intel_guc *guc, uint32_t priority)
153{
154 /*
155 * The bitmap tracks which doorbell registers are currently in use.
156 * It is split into two halves; the first half is used for normal
157 * priority contexts, the second half for high-priority ones.
158 * Note that logically higher priorities are numerically less than
159 * normal ones, so the test below means "is it high-priority?"
160 */
161 const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
162 const uint16_t half = GUC_MAX_DOORBELLS / 2;
163 const uint16_t start = hi_pri ? half : 0;
164 const uint16_t end = start + half;
165 uint16_t id;
166
167 id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
168 if (id == end)
169 id = GUC_INVALID_DOORBELL_ID;
170
171 DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
172 hi_pri ? "high" : "normal", id);
173
174 return id;
175}
176
Dave Gordon44a28b12015-08-12 15:43:41 +0100177/*
178 * Select, assign and relase doorbell cachelines
179 *
180 * These functions track which doorbell cachelines are in use.
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100181 * The data they manipulate is protected by the intel_guc_send lock.
Dave Gordon44a28b12015-08-12 15:43:41 +0100182 */
183
184static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
185{
186 const uint32_t cacheline_size = cache_line_size();
187 uint32_t offset;
188
Dave Gordon44a28b12015-08-12 15:43:41 +0100189 /* Doorbell uses a single cache line within a page */
190 offset = offset_in_page(guc->db_cacheline);
191
192 /* Moving to next cache line to reduce contention */
193 guc->db_cacheline += cacheline_size;
194
Dave Gordon44a28b12015-08-12 15:43:41 +0100195 DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
196 offset, guc->db_cacheline, cacheline_size);
197
198 return offset;
199}
200
Dave Gordon44a28b12015-08-12 15:43:41 +0100201/*
202 * Initialise the process descriptor shared with the GuC firmware.
203 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100204static void guc_proc_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100205 struct i915_guc_client *client)
206{
207 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100208
Chris Wilson72aa0d82016-11-02 17:50:47 +0000209 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100210
211 memset(desc, 0, sizeof(*desc));
212
213 /*
214 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
215 * space for ring3 clients (set them as in mmap_ioctl) or kernel
216 * space for kernel clients (map on demand instead? May make debug
217 * easier to have it mapped).
218 */
219 desc->wq_base_addr = 0;
220 desc->db_base_addr = 0;
221
222 desc->context_id = client->ctx_index;
223 desc->wq_size_bytes = client->wq_size;
224 desc->wq_status = WQ_STATUS_ACTIVE;
225 desc->priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100226}
227
228/*
229 * Initialise/clear the context descriptor shared with the GuC firmware.
230 *
231 * This descriptor tells the GuC where (in GGTT space) to find the important
232 * data structures relating to this client (doorbell, process descriptor,
233 * write queue, etc).
234 */
235
Dave Gordon7a9347f2016-09-12 21:19:37 +0100236static void guc_ctx_desc_init(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100237 struct i915_guc_client *client)
238{
Alex Dai397097b2016-01-23 11:58:14 -0800239 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000240 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +0100241 struct i915_gem_context *ctx = client->owner;
Dave Gordon44a28b12015-08-12 15:43:41 +0100242 struct guc_context_desc desc;
243 struct sg_table *sg;
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100244 unsigned int tmp;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100245 u32 gfx_addr;
Dave Gordon44a28b12015-08-12 15:43:41 +0100246
247 memset(&desc, 0, sizeof(desc));
248
249 desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
250 desc.context_id = client->ctx_index;
251 desc.priority = client->priority;
Dave Gordon44a28b12015-08-12 15:43:41 +0100252 desc.db_id = client->doorbell_id;
253
Chris Wilsonbafb0fc2016-08-27 08:54:01 +0100254 for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
Chris Wilson9021ad02016-05-24 14:53:37 +0100255 struct intel_context *ce = &ctx->engine[engine->id];
Dave Gordonc18468c2016-08-09 15:19:22 +0100256 uint32_t guc_engine_id = engine->guc_id;
257 struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
Alex Daid1675192015-08-12 15:43:43 +0100258
259 /* TODO: We have a design issue to be solved here. Only when we
260 * receive the first batch, we know which engine is used by the
261 * user. But here GuC expects the lrc and ring to be pinned. It
262 * is not an issue for default context, which is the only one
263 * for now who owns a GuC client. But for future owner of GuC
264 * client, need to make sure lrc is pinned prior to enter here.
265 */
Chris Wilson9021ad02016-05-24 14:53:37 +0100266 if (!ce->state)
Alex Daid1675192015-08-12 15:43:43 +0100267 break; /* XXX: continue? */
268
Chris Wilson9021ad02016-05-24 14:53:37 +0100269 lrc->context_desc = lower_32_bits(ce->lrc_desc);
Alex Daid1675192015-08-12 15:43:43 +0100270
271 /* The state page is after PPHWSP */
Chris Wilson57e88532016-08-15 10:48:57 +0100272 lrc->ring_lcra =
Chris Wilson4741da92016-12-24 19:31:46 +0000273 guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +0100274 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100275 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
Alex Daid1675192015-08-12 15:43:43 +0100276
Chris Wilson4741da92016-12-24 19:31:46 +0000277 lrc->ring_begin = guc_ggtt_offset(ce->ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +0100278 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
279 lrc->ring_next_free_location = lrc->ring_begin;
Alex Daid1675192015-08-12 15:43:43 +0100280 lrc->ring_current_tail_pointer_value = 0;
281
Dave Gordonc18468c2016-08-09 15:19:22 +0100282 desc.engines_used |= (1 << guc_engine_id);
Alex Daid1675192015-08-12 15:43:43 +0100283 }
284
Dave Gordone02757d2016-08-09 15:19:21 +0100285 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
286 client->engines, desc.engines_used);
Alex Daid1675192015-08-12 15:43:43 +0100287 WARN_ON(desc.engines_used == 0);
288
Dave Gordon44a28b12015-08-12 15:43:41 +0100289 /*
Dave Gordon86e06cc2016-04-19 16:08:36 +0100290 * The doorbell, process descriptor, and workqueue are all parts
291 * of the client object, which the GuC will reference via the GGTT
Dave Gordon44a28b12015-08-12 15:43:41 +0100292 */
Chris Wilson4741da92016-12-24 19:31:46 +0000293 gfx_addr = guc_ggtt_offset(client->vma);
Chris Wilson8b797af2016-08-15 10:48:51 +0100294 desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
Dave Gordon86e06cc2016-04-19 16:08:36 +0100295 client->doorbell_offset;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000296 desc.db_trigger_cpu =
297 (uintptr_t)client->vaddr + client->doorbell_offset;
Dave Gordon86e06cc2016-04-19 16:08:36 +0100298 desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
299 desc.process_desc = gfx_addr + client->proc_desc_offset;
300 desc.wq_addr = gfx_addr + client->wq_offset;
Dave Gordon44a28b12015-08-12 15:43:41 +0100301 desc.wq_size = client->wq_size;
302
303 /*
Chris Wilsone2efd132016-05-24 14:53:34 +0100304 * XXX: Take LRCs from an existing context if this is not an
Dave Gordon44a28b12015-08-12 15:43:41 +0100305 * IsKMDCreatedContext client
306 */
307 desc.desc_private = (uintptr_t)client;
308
309 /* Pool context is pinned already */
Chris Wilson8b797af2016-08-15 10:48:51 +0100310 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100311 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
312 sizeof(desc) * client->ctx_index);
313}
314
Dave Gordon7a9347f2016-09-12 21:19:37 +0100315static void guc_ctx_desc_fini(struct intel_guc *guc,
Dave Gordon44a28b12015-08-12 15:43:41 +0100316 struct i915_guc_client *client)
317{
318 struct guc_context_desc desc;
319 struct sg_table *sg;
320
321 memset(&desc, 0, sizeof(desc));
322
Chris Wilson8b797af2016-08-15 10:48:51 +0100323 sg = guc->ctx_pool_vma->pages;
Dave Gordon44a28b12015-08-12 15:43:41 +0100324 sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
325 sizeof(desc) * client->ctx_index);
326}
327
Dave Gordon7c2c2702016-05-13 15:36:32 +0100328/**
Dave Gordon7a9347f2016-09-12 21:19:37 +0100329 * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
Dave Gordon7c2c2702016-05-13 15:36:32 +0100330 * @request: request associated with the commands
331 *
332 * Return: 0 if space is available
333 * -EAGAIN if space is not currently available
334 *
335 * This function must be called (and must return 0) before a request
336 * is submitted to the GuC via i915_guc_submit() below. Once a result
Dave Gordon7a9347f2016-09-12 21:19:37 +0100337 * of 0 has been returned, it must be balanced by a corresponding
338 * call to submit().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100339 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100340 * Reservation allows the caller to determine in advance that space
Dave Gordon7c2c2702016-05-13 15:36:32 +0100341 * will be available for the next submission before committing resources
342 * to it, and helps avoid late failures with complicated recovery paths.
343 */
Dave Gordon7a9347f2016-09-12 21:19:37 +0100344int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
Dave Gordon44a28b12015-08-12 15:43:41 +0100345{
Dave Gordon551aaec2016-05-13 15:36:33 +0100346 const size_t wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000347 struct i915_guc_client *client = request->i915->guc.execbuf_client;
348 struct guc_process_desc *desc = client->vaddr +
349 client->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100350 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100351 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100352
Chris Wilson349ab912017-02-28 11:28:02 +0000353 spin_lock_irq(&client->wq_lock);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000354 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
355 freespace -= client->wq_rsvd;
Chris Wilsondadd4812016-09-09 14:11:57 +0100356 if (likely(freespace >= wqi_size)) {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000357 client->wq_rsvd += wqi_size;
Chris Wilsondadd4812016-09-09 14:11:57 +0100358 ret = 0;
359 } else {
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000360 client->no_wq_space++;
Chris Wilsondadd4812016-09-09 14:11:57 +0100361 ret = -EAGAIN;
362 }
Chris Wilson349ab912017-02-28 11:28:02 +0000363 spin_unlock_irq(&client->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800364
Chris Wilsondadd4812016-09-09 14:11:57 +0100365 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100366}
367
Chris Wilson349ab912017-02-28 11:28:02 +0000368static void guc_client_update_wq_rsvd(struct i915_guc_client *client, int size)
369{
370 unsigned long flags;
371
372 spin_lock_irqsave(&client->wq_lock, flags);
373 client->wq_rsvd += size;
374 spin_unlock_irqrestore(&client->wq_lock, flags);
375}
376
Chris Wilson5ba89902016-10-07 07:53:27 +0100377void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
378{
Chris Wilson349ab912017-02-28 11:28:02 +0000379 const int wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000380 struct i915_guc_client *client = request->i915->guc.execbuf_client;
Chris Wilson5ba89902016-10-07 07:53:27 +0100381
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000382 GEM_BUG_ON(READ_ONCE(client->wq_rsvd) < wqi_size);
Chris Wilson349ab912017-02-28 11:28:02 +0000383 guc_client_update_wq_rsvd(client, -wqi_size);
Chris Wilson5ba89902016-10-07 07:53:27 +0100384}
385
Dave Gordon7a9347f2016-09-12 21:19:37 +0100386/* Construct a Work Item and append it to the GuC's Work Queue */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000387static void guc_wq_item_append(struct i915_guc_client *client,
Dave Gordon7a9347f2016-09-12 21:19:37 +0100388 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100389{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100390 /* wqi_len is in DWords, and does not include the one-word header */
391 const size_t wqi_size = sizeof(struct guc_wq_item);
392 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100393 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100394 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100395 struct guc_wq_item *wqi;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000396 u32 freespace, tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100397
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000398 desc = client->vaddr + client->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800399
Dave Gordon7a9347f2016-09-12 21:19:37 +0100400 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000401 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100402 GEM_BUG_ON(freespace < wqi_size);
403
404 /* The GuC firmware wants the tail index in QWords, not bytes */
405 tail = rq->tail;
406 GEM_BUG_ON(tail & 7);
407 tail >>= 3;
408 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100409
410 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
411 * should not have the case where structure wqi is across page, neither
412 * wrapped to the beginning. This simplifies the implementation below.
413 *
414 * XXX: if not the case, we need save data to a temp wqi and copy it to
415 * workqueue buffer dw by dw.
416 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100417 BUILD_BUG_ON(wqi_size != 16);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000418 GEM_BUG_ON(client->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100419
Dave Gordon0a31afb2016-05-13 15:36:34 +0100420 /* postincrement WQ tail for next time */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000421 wq_off = client->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100422 GEM_BUG_ON(wq_off & (wqi_size - 1));
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000423 client->wq_tail += wqi_size;
424 client->wq_tail &= client->wq_size - 1;
425 client->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100426
427 /* WQ starts from the page after doorbell / process_desc */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000428 wqi = client->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100429
Dave Gordon0a31afb2016-05-13 15:36:34 +0100430 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100431 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100432 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100433 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100434 WQ_NO_WCFLUSH_WAIT;
435
436 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100437 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100438
Dave Gordon44a28b12015-08-12 15:43:41 +0100439 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100440 wqi->fence_id = rq->global_seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100441}
442
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000443static int guc_ring_doorbell(struct i915_guc_client *client)
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100444{
445 struct guc_process_desc *desc;
446 union guc_doorbell_qw db_cmp, db_exc, db_ret;
447 union guc_doorbell_qw *db;
448 int attempt = 2, ret = -EAGAIN;
449
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000450 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100451
452 /* Update the tail so it is visible to GuC */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000453 desc->tail = client->wq_tail;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100454
455 /* current cookie */
456 db_cmp.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000457 db_cmp.cookie = client->doorbell_cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100458
459 /* cookie to be updated */
460 db_exc.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000461 db_exc.cookie = client->doorbell_cookie + 1;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100462 if (db_exc.cookie == 0)
463 db_exc.cookie = 1;
464
465 /* pointer of current doorbell cacheline */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000466 db = client->vaddr + client->doorbell_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100467
468 while (attempt--) {
469 /* lets ring the doorbell */
470 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
471 db_cmp.value_qw, db_exc.value_qw);
472
473 /* if the exchange was successfully executed */
474 if (db_ret.value_qw == db_cmp.value_qw) {
475 /* db was successfully rung */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000476 client->doorbell_cookie = db_exc.cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100477 ret = 0;
478 break;
479 }
480
481 /* XXX: doorbell was lost and need to acquire it again */
482 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
483 break;
484
Dave Gordon535b2f52016-08-18 18:17:23 +0100485 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
486 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100487
488 /* update the cookie to newly read cookie from GuC */
489 db_cmp.cookie = db_ret.cookie;
490 db_exc.cookie = db_ret.cookie + 1;
491 if (db_exc.cookie == 0)
492 db_exc.cookie = 1;
493 }
494
495 return ret;
496}
497
Dave Gordon44a28b12015-08-12 15:43:41 +0100498/**
Chris Wilson34ba5a82016-11-29 12:10:24 +0000499 * __i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700500 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100501 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100502 * The caller must have already called i915_guc_wq_reserve() above with
503 * a result of 0 (success), guaranteeing that there is space in the work
504 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100505 *
506 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100507 * submit() when _reserve() says there's no space, or calls _submit()
508 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100509 *
510 * The only error here arises if the doorbell hardware isn't functioning
511 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100512 */
Chris Wilson34ba5a82016-11-29 12:10:24 +0000513static void __i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100514{
Akash Goeled4596ea2016-10-25 22:05:23 +0530515 struct drm_i915_private *dev_priv = rq->i915;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000516 struct intel_engine_cs *engine = rq->engine;
517 unsigned int engine_id = engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100518 struct intel_guc *guc = &rq->i915->guc;
519 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilson25afdf892017-03-02 14:53:23 +0000520 unsigned long flags;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100521 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100522
Akash Goeled4596ea2016-10-25 22:05:23 +0530523 /* WA to flush out the pending GMADR writes to ring buffer. */
524 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
525 POSTING_READ_FW(GUC_STATUS);
526
Chris Wilson25afdf892017-03-02 14:53:23 +0000527 spin_lock_irqsave(&client->wq_lock, flags);
Chris Wilson0c335182017-02-28 11:28:03 +0000528
529 guc_wq_item_append(client, rq);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100530 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100531
Alex Dai397097b2016-01-23 11:58:14 -0800532 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100533 client->retcode = b_ret;
534 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100535 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100536
Alex Dai397097b2016-01-23 11:58:14 -0800537 guc->submissions[engine_id] += 1;
Chris Wilson65e47602016-10-28 13:58:49 +0100538 guc->last_seqno[engine_id] = rq->global_seqno;
Chris Wilson0c335182017-02-28 11:28:03 +0000539
Chris Wilson25afdf892017-03-02 14:53:23 +0000540 spin_unlock_irqrestore(&client->wq_lock, flags);
Dave Gordon44a28b12015-08-12 15:43:41 +0100541}
542
Chris Wilson34ba5a82016-11-29 12:10:24 +0000543static void i915_guc_submit(struct drm_i915_gem_request *rq)
544{
Chris Wilson31de7352017-03-16 12:56:18 +0000545 __i915_gem_request_submit(rq);
Chris Wilson34ba5a82016-11-29 12:10:24 +0000546 __i915_guc_submit(rq);
547}
548
Chris Wilson31de7352017-03-16 12:56:18 +0000549static void nested_enable_signaling(struct drm_i915_gem_request *rq)
550{
551 /* If we use dma_fence_enable_sw_signaling() directly, lockdep
552 * detects an ordering issue between the fence lockclass and the
553 * global_timeline. This circular dependency can only occur via 2
554 * different fences (but same fence lockclass), so we use the nesting
555 * annotation here to prevent the warn, equivalent to the nesting
556 * inside i915_gem_request_submit() for when we also enable the
557 * signaler.
558 */
559
560 if (test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
561 &rq->fence.flags))
562 return;
563
564 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags));
565 trace_dma_fence_enable_signal(&rq->fence);
566
567 spin_lock_nested(&rq->lock, SINGLE_DEPTH_NESTING);
568 intel_engine_enable_signaling(rq);
569 spin_unlock(&rq->lock);
570}
571
572static bool i915_guc_dequeue(struct intel_engine_cs *engine)
573{
574 struct execlist_port *port = engine->execlist_port;
575 struct drm_i915_gem_request *last = port[0].request;
576 unsigned long flags;
577 struct rb_node *rb;
578 bool submit = false;
579
Chris Wilson6c943de2017-03-17 12:07:16 +0000580 /* After execlist_first is updated, the tasklet will be rescheduled.
581 *
582 * If we are currently running (inside the tasklet) and a third
583 * party queues a request and so updates engine->execlist_first under
584 * the spinlock (which we have elided), it will atomically set the
585 * TASKLET_SCHED flag causing the us to be re-executed and pick up
586 * the change in state (the update to TASKLET_SCHED incurs a memory
587 * barrier making this cross-cpu checking safe).
588 */
589 if (!READ_ONCE(engine->execlist_first))
590 return false;
591
Chris Wilson31de7352017-03-16 12:56:18 +0000592 spin_lock_irqsave(&engine->timeline->lock, flags);
593 rb = engine->execlist_first;
594 while (rb) {
595 struct drm_i915_gem_request *rq =
596 rb_entry(rb, typeof(*rq), priotree.node);
597
598 if (last && rq->ctx != last->ctx) {
599 if (port != engine->execlist_port)
600 break;
601
602 i915_gem_request_assign(&port->request, last);
603 nested_enable_signaling(last);
604 port++;
605 }
606
607 rb = rb_next(rb);
608 rb_erase(&rq->priotree.node, &engine->execlist_queue);
609 RB_CLEAR_NODE(&rq->priotree.node);
610 rq->priotree.priority = INT_MAX;
611
612 trace_i915_gem_request_in(rq, port - engine->execlist_port);
613 i915_guc_submit(rq);
614 last = rq;
615 submit = true;
616 }
617 if (submit) {
618 i915_gem_request_assign(&port->request, last);
619 nested_enable_signaling(last);
620 engine->execlist_first = rb;
621 }
622 spin_unlock_irqrestore(&engine->timeline->lock, flags);
623
624 return submit;
625}
626
627static void i915_guc_irq_handler(unsigned long data)
628{
629 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
630 struct execlist_port *port = engine->execlist_port;
631 struct drm_i915_gem_request *rq;
632 bool submit;
633
634 do {
635 rq = port[0].request;
636 while (rq && i915_gem_request_completed(rq)) {
637 trace_i915_gem_request_out(rq);
638 i915_gem_request_put(rq);
639 port[0].request = port[1].request;
640 port[1].request = NULL;
641 rq = port[0].request;
642 }
643
644 submit = false;
645 if (!port[1].request)
646 submit = i915_guc_dequeue(engine);
647 } while (submit);
648}
649
Dave Gordon44a28b12015-08-12 15:43:41 +0100650/*
651 * Everything below here is concerned with setup & teardown, and is
652 * therefore not part of the somewhat time-critical batch-submission
653 * path of i915_guc_submit() above.
654 */
655
656/**
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000657 * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
Chris Wilson8b797af2016-08-15 10:48:51 +0100658 * @guc: the guc
659 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100660 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100661 * This is a wrapper to create an object for use with the GuC. In order to
662 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
663 * both some backing storage and a range inside the Global GTT. We must pin
664 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
665 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100666 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100667 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100668 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000669struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100670{
Chris Wilson8b797af2016-08-15 10:48:51 +0100671 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100672 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100673 struct i915_vma *vma;
674 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100675
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000676 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100677 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100678 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100679
Chris Wilsona01cb372017-01-16 15:21:30 +0000680 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson8b797af2016-08-15 10:48:51 +0100681 if (IS_ERR(vma))
682 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100683
Chris Wilson8b797af2016-08-15 10:48:51 +0100684 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
685 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
686 if (ret) {
687 vma = ERR_PTR(ret);
688 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100689 }
690
Chris Wilson8b797af2016-08-15 10:48:51 +0100691 return vma;
692
693err:
694 i915_gem_object_put(obj);
695 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100696}
697
Dave Gordon0daf5562016-06-10 18:29:25 +0100698static void
699guc_client_free(struct drm_i915_private *dev_priv,
700 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100701{
Dave Gordon44a28b12015-08-12 15:43:41 +0100702 struct intel_guc *guc = &dev_priv->guc;
703
704 if (!client)
705 return;
706
Dave Gordon44a28b12015-08-12 15:43:41 +0100707 /*
708 * XXX: wait for any outstanding submissions before freeing memory.
709 * Be sure to drop any locks
710 */
711
Chris Wilson72aa0d82016-11-02 17:50:47 +0000712 if (client->vaddr) {
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100713 /*
Dave Gordona6674292016-06-13 17:57:32 +0100714 * If we got as far as setting up a doorbell, make sure we
715 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100716 */
Dave Gordona6674292016-06-13 17:57:32 +0100717 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100718
Chris Wilson72aa0d82016-11-02 17:50:47 +0000719 i915_gem_object_unpin_map(client->vma->obj);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100720 }
721
Chris Wilson19880c42016-08-15 10:49:05 +0100722 i915_vma_unpin_and_release(&client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100723
724 if (client->ctx_index != GUC_INVALID_CTX_ID) {
Dave Gordon7a9347f2016-09-12 21:19:37 +0100725 guc_ctx_desc_fini(guc, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100726 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
727 }
728
729 kfree(client);
730}
731
Dave Gordon84b7f882016-08-09 15:19:20 +0100732/* Check that a doorbell register is in the expected state */
733static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
734{
735 struct drm_i915_private *dev_priv = guc_to_i915(guc);
736 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
737 uint32_t value = I915_READ(drbreg);
738 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
739 bool expected = test_bit(db_id, guc->doorbell_bitmap);
740
741 if (enabled == expected)
742 return true;
743
744 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
745 db_id, drbreg.reg, value,
746 expected ? "active" : "inactive");
747
748 return false;
749}
750
Dave Gordon4d757872016-06-13 17:57:34 +0100751/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100752 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100753 * in turn, to ensure that all doorbell h/w is (re)initialised.
754 */
755static void guc_init_doorbell_hw(struct intel_guc *guc)
756{
Dave Gordon4d757872016-06-13 17:57:34 +0100757 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100758 uint16_t db_id;
759 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100760
Chris Wilson4d357af2016-11-29 12:10:23 +0000761 guc_disable_doorbell(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +0100762
763 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100764 /* Skip if doorbell is OK */
765 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100766 continue;
767
Dave Gordon4d757872016-06-13 17:57:34 +0100768 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100769 if (err)
770 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
771 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100772 }
773
Chris Wilson4d357af2016-11-29 12:10:23 +0000774 db_id = select_doorbell_register(guc, client->priority);
775 WARN_ON(db_id == GUC_INVALID_DOORBELL_ID);
776
Dave Gordon4d757872016-06-13 17:57:34 +0100777 err = guc_update_doorbell_id(guc, client, db_id);
778 if (err)
Dave Gordon535b2f52016-08-18 18:17:23 +0100779 DRM_WARN("Failed to restore doorbell to %d, err %d\n",
780 db_id, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100781
Dave Gordon84b7f882016-08-09 15:19:20 +0100782 /* Read back & verify all doorbell registers */
783 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
784 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100785}
786
Dave Gordon44a28b12015-08-12 15:43:41 +0100787/**
788 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100789 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100790 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100791 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
792 * The kernel client to replace ExecList submission is created with
793 * NORMAL priority. Priority of a client for scheduler can be HIGH,
794 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700795 * @ctx: the context that owns the client (we use the default render
796 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100797 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100798 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100799 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100800static struct i915_guc_client *
801guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100802 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100803 uint32_t priority,
804 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100805{
806 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100807 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100808 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000809 void *vaddr;
Dave Gordona6674292016-06-13 17:57:32 +0100810 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100811
812 client = kzalloc(sizeof(*client), GFP_KERNEL);
813 if (!client)
814 return NULL;
815
Alex Daid1675192015-08-12 15:43:43 +0100816 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100817 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100818 client->engines = engines;
819 client->priority = priority;
820 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100821
822 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
823 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
824 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
825 client->ctx_index = GUC_INVALID_CTX_ID;
826 goto err;
827 }
828
829 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000830 vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
Chris Wilson8b797af2016-08-15 10:48:51 +0100831 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100832 goto err;
833
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100834 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100835 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000836
837 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
838 if (IS_ERR(vaddr))
839 goto err;
840
841 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100842
843 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100844 client->wq_offset = GUC_DB_SIZE;
845 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100846
Dave Gordonf10d69a2016-06-13 17:57:33 +0100847 db_id = select_doorbell_register(guc, client->priority);
848 if (db_id == GUC_INVALID_DOORBELL_ID)
849 /* XXX: evict a doorbell instead? */
850 goto err;
851
Dave Gordon44a28b12015-08-12 15:43:41 +0100852 client->doorbell_offset = select_doorbell_cacheline(guc);
853
854 /*
855 * Since the doorbell only requires a single cacheline, we can save
856 * space by putting the application process descriptor in the same
857 * page. Use the half of the page that doesn't include the doorbell.
858 */
859 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
860 client->proc_desc_offset = 0;
861 else
862 client->proc_desc_offset = (GUC_DB_SIZE / 2);
863
Dave Gordon7a9347f2016-09-12 21:19:37 +0100864 guc_proc_desc_init(guc, client);
865 guc_ctx_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000866
867 /* For runtime client allocation we need to enable the doorbell. Not
868 * required yet for the static execbuf_client as this special kernel
869 * client is enabled from i915_guc_submission_enable().
870 *
871 * guc_update_doorbell_id(guc, client, db_id);
872 */
Dave Gordon44a28b12015-08-12 15:43:41 +0100873
Dave Gordone02757d2016-08-09 15:19:21 +0100874 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
875 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100876 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
877 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100878
879 return client;
880
881err:
Dave Gordon0daf5562016-06-10 18:29:25 +0100882 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100883 return NULL;
884}
885
Akash Goelf8240832016-10-12 21:54:34 +0530886
Akash Goelf8240832016-10-12 21:54:34 +0530887
Dave Gordon7a9347f2016-09-12 21:19:37 +0100888static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -0800889{
890 struct guc_policy *policy;
891 u32 p, i;
892
893 policies->dpc_promote_time = 500000;
894 policies->max_num_work_items = POLICY_MAX_NUM_WI;
895
896 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -0800897 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -0800898 policy = &policies->policy[p][i];
899
900 policy->execution_quantum = 1000000;
901 policy->preemption_time = 500000;
902 policy->fault_time = 250000;
903 policy->policy_flags = 0;
904 }
905 }
906
907 policies->is_valid = 1;
908}
909
Dave Gordon7a9347f2016-09-12 21:19:37 +0100910static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -0800911{
912 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +0100913 struct i915_vma *vma;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000914 struct page *page;
915 /* The ads obj includes the struct itself and buffers passed to GuC */
916 struct {
917 struct guc_ads ads;
918 struct guc_policies policies;
919 struct guc_mmio_reg_state reg_state;
920 u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
921 } __packed *blob;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000922 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530923 enum intel_engine_id id;
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000924 u32 base;
Alex Dai68371a92015-12-18 12:00:09 -0800925
Chris Wilson8b797af2016-08-15 10:48:51 +0100926 vma = guc->ads_vma;
927 if (!vma) {
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000928 vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
Chris Wilson8b797af2016-08-15 10:48:51 +0100929 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -0800930 return;
931
Chris Wilson8b797af2016-08-15 10:48:51 +0100932 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -0800933 }
934
Chris Wilson8b797af2016-08-15 10:48:51 +0100935 page = i915_vma_first_page(vma);
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000936 blob = kmap(page);
937
938 /* GuC scheduling policies */
939 guc_policies_init(&blob->policies);
940
941 /* MMIO reg state */
942 for_each_engine(engine, dev_priv, id) {
943 blob->reg_state.mmio_white_list[engine->guc_id].mmio_start =
944 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
945
946 /* Nothing to be saved or restored for now. */
947 blob->reg_state.mmio_white_list[engine->guc_id].count = 0;
948 }
Alex Dai68371a92015-12-18 12:00:09 -0800949
950 /*
951 * The GuC requires a "Golden Context" when it reinitialises
952 * engines after a reset. Here we use the Render ring default
953 * context, which must already exist and be pinned in the GGTT,
954 * so its address won't change after we've told the GuC where
955 * to find it.
956 */
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000957 blob->ads.golden_context_lrca =
958 dev_priv->engine[RCS]->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -0800959
Akash Goel3b3f1652016-10-13 22:44:48 +0530960 for_each_engine(engine, dev_priv, id)
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000961 blob->ads.eng_state_size[engine->guc_id] =
962 intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -0800963
Michal Wajdeczko16f11f42017-03-14 13:33:09 +0000964 base = guc_ggtt_offset(vma);
965 blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
966 blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
967 blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
Alex Dai5c148e02015-12-18 12:00:11 -0800968
Alex Dai68371a92015-12-18 12:00:09 -0800969 kunmap(page);
970}
971
Alex Daibac427f2015-08-12 15:43:39 +0100972/*
973 * Set up the memory resources to be shared with the GuC. At this point,
974 * we require just one object that can be mapped through the GGTT.
975 */
Dave Gordonbeffa512016-06-10 18:29:26 +0100976int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +0100977{
Dave Gordon7a9347f2016-09-12 21:19:37 +0100978 const size_t ctxsize = sizeof(struct guc_context_desc);
979 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
980 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +0100981 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100982 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +0100983
Chris Wilson4d357af2016-11-29 12:10:23 +0000984 if (!HAS_GUC_SCHED(dev_priv))
985 return 0;
986
Dave Gordon29fb72c2016-06-07 09:14:50 +0100987 /* Wipe bitmap & delete client in case of reinitialisation */
988 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +0100989 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +0100990
Alex Daibac427f2015-08-12 15:43:39 +0100991 if (!i915.enable_guc_submission)
992 return 0; /* not enabled */
993
Chris Wilson8b797af2016-08-15 10:48:51 +0100994 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +0100995 return 0; /* already allocated */
996
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000997 vma = intel_guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +0100998 if (IS_ERR(vma))
999 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001000
Chris Wilson8b797af2016-08-15 10:48:51 +01001001 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001002 ida_init(&guc->ctx_ids);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001003 intel_guc_log_create(guc);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001004 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001005
Chris Wilson4d357af2016-11-29 12:10:23 +00001006 guc->execbuf_client = guc_client_alloc(dev_priv,
1007 INTEL_INFO(dev_priv)->ring_mask,
1008 GUC_CTX_PRIORITY_KMD_NORMAL,
1009 dev_priv->kernel_context);
1010 if (!guc->execbuf_client) {
1011 DRM_ERROR("Failed to create GuC client for execbuf!\n");
1012 goto err;
1013 }
1014
Alex Daibac427f2015-08-12 15:43:39 +01001015 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +00001016
1017err:
1018 i915_guc_submission_fini(dev_priv);
1019 return -ENOMEM;
1020}
1021
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001022static void guc_reset_wq(struct i915_guc_client *client)
Chris Wilson4d357af2016-11-29 12:10:23 +00001023{
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001024 struct guc_process_desc *desc = client->vaddr +
1025 client->proc_desc_offset;
Chris Wilson4d357af2016-11-29 12:10:23 +00001026
1027 desc->head = 0;
1028 desc->tail = 0;
1029
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001030 client->wq_tail = 0;
Alex Daibac427f2015-08-12 15:43:39 +01001031}
1032
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001033static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
1034{
1035 struct intel_engine_cs *engine;
1036 enum intel_engine_id id;
1037 int irqs;
1038
1039 /* tell all command streamers to forward interrupts (but not vblank) to GuC */
1040 irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
1041 for_each_engine(engine, dev_priv, id)
1042 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1043
1044 /* route USER_INTERRUPT to Host, all others are sent to GuC. */
1045 irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
1046 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
1047 /* These three registers have the same bit definitions */
1048 I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
1049 I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
1050 I915_WRITE(GUC_WD_VECS_IER, ~irqs);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301051
1052 /*
1053 * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
1054 * (unmasked) PM interrupts to the GuC. All other bits of this
1055 * register *disable* generation of a specific interrupt.
1056 *
1057 * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
1058 * writing to the PM interrupt mask register, i.e. interrupts
1059 * that must not be disabled.
1060 *
1061 * If the GuC is handling these interrupts, then we must not let
1062 * the PM code disable ANY interrupt that the GuC is expecting.
1063 * So for each ENABLED (0) bit in this register, we must SET the
1064 * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
1065 * GuC needs ARAT expired interrupt unmasked hence it is set in
1066 * pm_intrmsk_mbz.
1067 *
1068 * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1069 * result in the register bit being left SET!
1070 */
1071 dev_priv->rps.pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
Chris Wilson655d49e2017-03-12 13:27:45 +00001072 dev_priv->rps.pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001073}
1074
Dave Gordonbeffa512016-06-10 18:29:26 +01001075int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001076{
Dave Gordon44a28b12015-08-12 15:43:41 +01001077 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001078 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001079 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301080 enum intel_engine_id id;
Dave Gordon44a28b12015-08-12 15:43:41 +01001081
Chris Wilson4d357af2016-11-29 12:10:23 +00001082 if (!client)
1083 return -ENODEV;
Dave Gordon44a28b12015-08-12 15:43:41 +01001084
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001085 intel_guc_sample_forcewake(guc);
Chris Wilson4d357af2016-11-29 12:10:23 +00001086
1087 guc_reset_wq(client);
Dave Gordon4d757872016-06-13 17:57:34 +01001088 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001089
Chris Wilsonddd66c52016-08-02 22:50:31 +01001090 /* Take over from manual control of ELSP (execlists) */
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001091 guc_interrupts_capture(dev_priv);
1092
Tvrtko Ursulincbf4b772017-03-09 13:20:04 +00001093 for_each_engine(engine, dev_priv, id) {
Chris Wilson349ab912017-02-28 11:28:02 +00001094 const int wqi_size = sizeof(struct guc_wq_item);
Chris Wilson4d357af2016-11-29 12:10:23 +00001095 struct drm_i915_gem_request *rq;
1096
Chris Wilson31de7352017-03-16 12:56:18 +00001097 /* The tasklet was initialised by execlists, and may be in
1098 * a state of flux (across a reset) and so we just want to
1099 * take over the callback without changing any other state
1100 * in the tasklet.
1101 */
1102 engine->irq_tasklet.func = i915_guc_irq_handler;
1103 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1104
1105 /* Replay the current set of previously submitted requests */
Chris Wilson349ab912017-02-28 11:28:02 +00001106 spin_lock_irq(&engine->timeline->lock);
Chris Wilson4d357af2016-11-29 12:10:23 +00001107 list_for_each_entry(rq, &engine->timeline->requests, link) {
Chris Wilson349ab912017-02-28 11:28:02 +00001108 guc_client_update_wq_rsvd(client, wqi_size);
Chris Wilson34ba5a82016-11-29 12:10:24 +00001109 __i915_guc_submit(rq);
Chris Wilsondadd4812016-09-09 14:11:57 +01001110 }
Chris Wilson349ab912017-02-28 11:28:02 +00001111 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001112 }
1113
Dave Gordon44a28b12015-08-12 15:43:41 +01001114 return 0;
1115}
1116
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301117static void guc_interrupts_release(struct drm_i915_private *dev_priv)
1118{
1119 struct intel_engine_cs *engine;
1120 enum intel_engine_id id;
1121 int irqs;
1122
1123 /*
1124 * tell all command streamers NOT to forward interrupts or vblank
1125 * to GuC.
1126 */
1127 irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
1128 irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
1129 for_each_engine(engine, dev_priv, id)
1130 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1131
1132 /* route all GT interrupts to the host */
1133 I915_WRITE(GUC_BCS_RCS_IER, 0);
1134 I915_WRITE(GUC_VCS2_VCS1_IER, 0);
1135 I915_WRITE(GUC_WD_VECS_IER, 0);
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301136
Chris Wilson655d49e2017-03-12 13:27:45 +00001137 dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
Sagar Arun Kamble1f3b1fd2017-03-11 08:07:01 +05301138 dev_priv->rps.pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301139}
1140
Dave Gordonbeffa512016-06-10 18:29:26 +01001141void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001142{
Dave Gordon44a28b12015-08-12 15:43:41 +01001143 struct intel_guc *guc = &dev_priv->guc;
1144
Sagar Arun Kamble7762ebb2017-03-11 08:06:59 +05301145 guc_interrupts_release(dev_priv);
1146
Chris Wilsonddd66c52016-08-02 22:50:31 +01001147 if (!guc->execbuf_client)
1148 return;
1149
Chris Wilsonddd66c52016-08-02 22:50:31 +01001150 /* Revert back to manual ELSP submission */
Chris Wilsonff44ad52017-03-16 17:13:03 +00001151 intel_engines_reset_default_submission(dev_priv);
Dave Gordon44a28b12015-08-12 15:43:41 +01001152}
1153
Dave Gordonbeffa512016-06-10 18:29:26 +01001154void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001155{
Alex Daibac427f2015-08-12 15:43:39 +01001156 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001157 struct i915_guc_client *client;
1158
1159 client = fetch_and_zero(&guc->execbuf_client);
1160 if (!client)
1161 return;
1162
1163 guc_client_free(dev_priv, client);
Alex Daibac427f2015-08-12 15:43:39 +01001164
Chris Wilson19880c42016-08-15 10:49:05 +01001165 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301166 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001167
Chris Wilson8b797af2016-08-15 10:48:51 +01001168 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001169 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001170 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001171}
Alex Daia1c41992015-09-30 09:46:37 -07001172
1173/**
1174 * intel_guc_suspend() - notify GuC entering suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001175 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001176 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001177int intel_guc_suspend(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001178{
Alex Daia1c41992015-09-30 09:46:37 -07001179 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001180 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001181 u32 data[3];
1182
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001183 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001184 return 0;
1185
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301186 gen9_disable_guc_interrupts(dev_priv);
1187
Dave Gordoned54c1a2016-01-19 19:02:54 +00001188 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001189
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001190 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001191 /* any value greater than GUC_POWER_D0 */
1192 data[1] = GUC_POWER_D1;
1193 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001194 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001195
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001196 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001197}
1198
1199
1200/**
1201 * intel_guc_resume() - notify GuC resuming from suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001202 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001203 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001204int intel_guc_resume(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001205{
Alex Daia1c41992015-09-30 09:46:37 -07001206 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001207 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001208 u32 data[3];
1209
Anusha Srivatsadb0a0912017-01-13 17:17:04 -08001210 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001211 return 0;
1212
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301213 if (i915.guc_log_level >= 0)
1214 gen9_enable_guc_interrupts(dev_priv);
1215
Dave Gordoned54c1a2016-01-19 19:02:54 +00001216 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001217
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001218 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001219 data[1] = GUC_POWER_D0;
1220 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001221 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001222
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001223 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001224}
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301225
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301226