blob: 58413803ba3c7be755048f243847d7925e1aa3ee [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>
Akash Goelf8240832016-10-12 21:54:34 +053025#include <linux/debugfs.h>
26#include <linux/relay.h>
Alex Daibac427f2015-08-12 15:43:39 +010027#include "i915_drv.h"
Arkadiusz Hiler8c4f24f2016-11-25 18:59:33 +010028#include "intel_uc.h"
Alex Daibac427f2015-08-12 15:43:39 +010029
30/**
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 Wilsonbde13eb2016-08-15 10:49:07 +0100273 i915_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 Wilsonbde13eb2016-08-15 10:49:07 +0100277 lrc->ring_begin = i915_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 Wilsonbde13eb2016-08-15 10:49:07 +0100293 gfx_addr = i915_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);
Dave Gordon7c2c2702016-05-13 15:36:32 +0100347 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000348 struct guc_process_desc *desc = gc->vaddr + gc->proc_desc_offset;
Dave Gordon551aaec2016-05-13 15:36:33 +0100349 u32 freespace;
Chris Wilsondadd4812016-09-09 14:11:57 +0100350 int ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100351
Chris Wilsondadd4812016-09-09 14:11:57 +0100352 spin_lock(&gc->wq_lock);
Dave Gordon551aaec2016-05-13 15:36:33 +0100353 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
Chris Wilsondadd4812016-09-09 14:11:57 +0100354 freespace -= gc->wq_rsvd;
355 if (likely(freespace >= wqi_size)) {
356 gc->wq_rsvd += wqi_size;
357 ret = 0;
358 } else {
359 gc->no_wq_space++;
360 ret = -EAGAIN;
361 }
362 spin_unlock(&gc->wq_lock);
Alex Dai5a843302015-12-02 16:56:29 -0800363
Chris Wilsondadd4812016-09-09 14:11:57 +0100364 return ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100365}
366
Chris Wilson5ba89902016-10-07 07:53:27 +0100367void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
368{
369 const size_t wqi_size = sizeof(struct guc_wq_item);
370 struct i915_guc_client *gc = request->i915->guc.execbuf_client;
371
372 GEM_BUG_ON(READ_ONCE(gc->wq_rsvd) < wqi_size);
373
374 spin_lock(&gc->wq_lock);
375 gc->wq_rsvd -= wqi_size;
376 spin_unlock(&gc->wq_lock);
377}
378
Dave Gordon7a9347f2016-09-12 21:19:37 +0100379/* Construct a Work Item and append it to the GuC's Work Queue */
380static void guc_wq_item_append(struct i915_guc_client *gc,
381 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100382{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100383 /* wqi_len is in DWords, and does not include the one-word header */
384 const size_t wqi_size = sizeof(struct guc_wq_item);
385 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100386 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100387 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100388 struct guc_wq_item *wqi;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000389 u32 freespace, tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100390
Chris Wilson72aa0d82016-11-02 17:50:47 +0000391 desc = gc->vaddr + gc->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800392
Dave Gordon7a9347f2016-09-12 21:19:37 +0100393 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100394 freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
395 GEM_BUG_ON(freespace < wqi_size);
396
397 /* The GuC firmware wants the tail index in QWords, not bytes */
398 tail = rq->tail;
399 GEM_BUG_ON(tail & 7);
400 tail >>= 3;
401 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100402
403 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
404 * should not have the case where structure wqi is across page, neither
405 * wrapped to the beginning. This simplifies the implementation below.
406 *
407 * XXX: if not the case, we need save data to a temp wqi and copy it to
408 * workqueue buffer dw by dw.
409 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100410 BUILD_BUG_ON(wqi_size != 16);
Chris Wilsondadd4812016-09-09 14:11:57 +0100411 GEM_BUG_ON(gc->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100412
Dave Gordon0a31afb2016-05-13 15:36:34 +0100413 /* postincrement WQ tail for next time */
414 wq_off = gc->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100415 GEM_BUG_ON(wq_off & (wqi_size - 1));
Dave Gordon0a31afb2016-05-13 15:36:34 +0100416 gc->wq_tail += wqi_size;
417 gc->wq_tail &= gc->wq_size - 1;
Chris Wilsondadd4812016-09-09 14:11:57 +0100418 gc->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100419
420 /* WQ starts from the page after doorbell / process_desc */
Chris Wilson72aa0d82016-11-02 17:50:47 +0000421 wqi = gc->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100422
Dave Gordon0a31afb2016-05-13 15:36:34 +0100423 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100424 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100425 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100426 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100427 WQ_NO_WCFLUSH_WAIT;
428
429 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100430 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100431
Dave Gordon44a28b12015-08-12 15:43:41 +0100432 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100433 wqi->fence_id = rq->global_seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100434}
435
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100436static int guc_ring_doorbell(struct i915_guc_client *gc)
437{
438 struct guc_process_desc *desc;
439 union guc_doorbell_qw db_cmp, db_exc, db_ret;
440 union guc_doorbell_qw *db;
441 int attempt = 2, ret = -EAGAIN;
442
Chris Wilson72aa0d82016-11-02 17:50:47 +0000443 desc = gc->vaddr + gc->proc_desc_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100444
445 /* Update the tail so it is visible to GuC */
446 desc->tail = gc->wq_tail;
447
448 /* current cookie */
449 db_cmp.db_status = GUC_DOORBELL_ENABLED;
Chris Wilson357248b2016-11-29 12:10:21 +0000450 db_cmp.cookie = gc->doorbell_cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100451
452 /* cookie to be updated */
453 db_exc.db_status = GUC_DOORBELL_ENABLED;
Chris Wilson357248b2016-11-29 12:10:21 +0000454 db_exc.cookie = gc->doorbell_cookie + 1;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100455 if (db_exc.cookie == 0)
456 db_exc.cookie = 1;
457
458 /* pointer of current doorbell cacheline */
Chris Wilson72aa0d82016-11-02 17:50:47 +0000459 db = gc->vaddr + gc->doorbell_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100460
461 while (attempt--) {
462 /* lets ring the doorbell */
463 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
464 db_cmp.value_qw, db_exc.value_qw);
465
466 /* if the exchange was successfully executed */
467 if (db_ret.value_qw == db_cmp.value_qw) {
468 /* db was successfully rung */
Chris Wilson357248b2016-11-29 12:10:21 +0000469 gc->doorbell_cookie = db_exc.cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100470 ret = 0;
471 break;
472 }
473
474 /* XXX: doorbell was lost and need to acquire it again */
475 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
476 break;
477
Dave Gordon535b2f52016-08-18 18:17:23 +0100478 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
479 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100480
481 /* update the cookie to newly read cookie from GuC */
482 db_cmp.cookie = db_ret.cookie;
483 db_exc.cookie = db_ret.cookie + 1;
484 if (db_exc.cookie == 0)
485 db_exc.cookie = 1;
486 }
487
488 return ret;
489}
490
Dave Gordon44a28b12015-08-12 15:43:41 +0100491/**
Chris Wilson34ba5a82016-11-29 12:10:24 +0000492 * __i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700493 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100494 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100495 * The caller must have already called i915_guc_wq_reserve() above with
496 * a result of 0 (success), guaranteeing that there is space in the work
497 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100498 *
499 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100500 * submit() when _reserve() says there's no space, or calls _submit()
501 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100502 *
503 * The only error here arises if the doorbell hardware isn't functioning
504 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100505 */
Chris Wilson34ba5a82016-11-29 12:10:24 +0000506static void __i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100507{
Akash Goeled4596ea2016-10-25 22:05:23 +0530508 struct drm_i915_private *dev_priv = rq->i915;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000509 struct intel_engine_cs *engine = rq->engine;
510 unsigned int engine_id = engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100511 struct intel_guc *guc = &rq->i915->guc;
512 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100513 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100514
Chris Wilsondadd4812016-09-09 14:11:57 +0100515 spin_lock(&client->wq_lock);
Dave Gordon7a9347f2016-09-12 21:19:37 +0100516 guc_wq_item_append(client, rq);
Akash Goeled4596ea2016-10-25 22:05:23 +0530517
518 /* WA to flush out the pending GMADR writes to ring buffer. */
519 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
520 POSTING_READ_FW(GUC_STATUS);
521
Dave Gordon0a31afb2016-05-13 15:36:34 +0100522 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100523
Alex Dai397097b2016-01-23 11:58:14 -0800524 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100525 client->retcode = b_ret;
526 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100527 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100528
Alex Dai397097b2016-01-23 11:58:14 -0800529 guc->submissions[engine_id] += 1;
Chris Wilson65e47602016-10-28 13:58:49 +0100530 guc->last_seqno[engine_id] = rq->global_seqno;
Chris Wilsondadd4812016-09-09 14:11:57 +0100531 spin_unlock(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100532}
533
Chris Wilson34ba5a82016-11-29 12:10:24 +0000534static void i915_guc_submit(struct drm_i915_gem_request *rq)
535{
536 struct intel_engine_cs *engine = rq->engine;
537
538 /* We keep the previous context alive until we retire the following
539 * request. This ensures that any the context object is still pinned
540 * for any residual writes the HW makes into it on the context switch
541 * into the next object following the breadcrumb. Otherwise, we may
542 * retire the context too early.
543 */
544 rq->previous_context = engine->last_context;
545 engine->last_context = rq->ctx;
546
547 i915_gem_request_submit(rq);
548 __i915_guc_submit(rq);
549}
550
Dave Gordon44a28b12015-08-12 15:43:41 +0100551/*
552 * Everything below here is concerned with setup & teardown, and is
553 * therefore not part of the somewhat time-critical batch-submission
554 * path of i915_guc_submit() above.
555 */
556
557/**
Chris Wilson8b797af2016-08-15 10:48:51 +0100558 * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
559 * @guc: the guc
560 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100561 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100562 * This is a wrapper to create an object for use with the GuC. In order to
563 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
564 * both some backing storage and a range inside the Global GTT. We must pin
565 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
566 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100567 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100568 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100569 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100570static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100571{
Chris Wilson8b797af2016-08-15 10:48:51 +0100572 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100573 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100574 struct i915_vma *vma;
575 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100576
Chris Wilson91c8a322016-07-05 10:40:23 +0100577 obj = i915_gem_object_create(&dev_priv->drm, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100578 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100579 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100580
Chris Wilson8b797af2016-08-15 10:48:51 +0100581 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
582 if (IS_ERR(vma))
583 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100584
Chris Wilson8b797af2016-08-15 10:48:51 +0100585 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
586 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
587 if (ret) {
588 vma = ERR_PTR(ret);
589 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100590 }
591
592 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
593 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
594
Chris Wilson8b797af2016-08-15 10:48:51 +0100595 return vma;
596
597err:
598 i915_gem_object_put(obj);
599 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100600}
601
Dave Gordon0daf5562016-06-10 18:29:25 +0100602static void
603guc_client_free(struct drm_i915_private *dev_priv,
604 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100605{
Dave Gordon44a28b12015-08-12 15:43:41 +0100606 struct intel_guc *guc = &dev_priv->guc;
607
608 if (!client)
609 return;
610
Dave Gordon44a28b12015-08-12 15:43:41 +0100611 /*
612 * XXX: wait for any outstanding submissions before freeing memory.
613 * Be sure to drop any locks
614 */
615
Chris Wilson72aa0d82016-11-02 17:50:47 +0000616 if (client->vaddr) {
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100617 /*
Dave Gordona6674292016-06-13 17:57:32 +0100618 * If we got as far as setting up a doorbell, make sure we
619 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100620 */
Dave Gordona6674292016-06-13 17:57:32 +0100621 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100622
Chris Wilson72aa0d82016-11-02 17:50:47 +0000623 i915_gem_object_unpin_map(client->vma->obj);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100624 }
625
Chris Wilson19880c42016-08-15 10:49:05 +0100626 i915_vma_unpin_and_release(&client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100627
628 if (client->ctx_index != GUC_INVALID_CTX_ID) {
Dave Gordon7a9347f2016-09-12 21:19:37 +0100629 guc_ctx_desc_fini(guc, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100630 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
631 }
632
633 kfree(client);
634}
635
Dave Gordon84b7f882016-08-09 15:19:20 +0100636/* Check that a doorbell register is in the expected state */
637static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
638{
639 struct drm_i915_private *dev_priv = guc_to_i915(guc);
640 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
641 uint32_t value = I915_READ(drbreg);
642 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
643 bool expected = test_bit(db_id, guc->doorbell_bitmap);
644
645 if (enabled == expected)
646 return true;
647
648 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
649 db_id, drbreg.reg, value,
650 expected ? "active" : "inactive");
651
652 return false;
653}
654
Dave Gordon4d757872016-06-13 17:57:34 +0100655/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100656 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100657 * in turn, to ensure that all doorbell h/w is (re)initialised.
658 */
659static void guc_init_doorbell_hw(struct intel_guc *guc)
660{
Dave Gordon4d757872016-06-13 17:57:34 +0100661 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100662 uint16_t db_id;
663 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100664
Chris Wilson4d357af2016-11-29 12:10:23 +0000665 guc_disable_doorbell(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +0100666
667 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100668 /* Skip if doorbell is OK */
669 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100670 continue;
671
Dave Gordon4d757872016-06-13 17:57:34 +0100672 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100673 if (err)
674 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
675 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100676 }
677
Chris Wilson4d357af2016-11-29 12:10:23 +0000678 db_id = select_doorbell_register(guc, client->priority);
679 WARN_ON(db_id == GUC_INVALID_DOORBELL_ID);
680
Dave Gordon4d757872016-06-13 17:57:34 +0100681 err = guc_update_doorbell_id(guc, client, db_id);
682 if (err)
Dave Gordon535b2f52016-08-18 18:17:23 +0100683 DRM_WARN("Failed to restore doorbell to %d, err %d\n",
684 db_id, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100685
Dave Gordon84b7f882016-08-09 15:19:20 +0100686 /* Read back & verify all doorbell registers */
687 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
688 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100689}
690
Dave Gordon44a28b12015-08-12 15:43:41 +0100691/**
692 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100693 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100694 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100695 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
696 * The kernel client to replace ExecList submission is created with
697 * NORMAL priority. Priority of a client for scheduler can be HIGH,
698 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700699 * @ctx: the context that owns the client (we use the default render
700 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100701 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100702 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100703 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100704static struct i915_guc_client *
705guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100706 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100707 uint32_t priority,
708 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100709{
710 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100711 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100712 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000713 void *vaddr;
Dave Gordona6674292016-06-13 17:57:32 +0100714 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100715
716 client = kzalloc(sizeof(*client), GFP_KERNEL);
717 if (!client)
718 return NULL;
719
Alex Daid1675192015-08-12 15:43:43 +0100720 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100721 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100722 client->engines = engines;
723 client->priority = priority;
724 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100725
726 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
727 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
728 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
729 client->ctx_index = GUC_INVALID_CTX_ID;
730 goto err;
731 }
732
733 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100734 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
735 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100736 goto err;
737
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100738 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100739 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000740
741 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
742 if (IS_ERR(vaddr))
743 goto err;
744
745 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100746
747 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100748 client->wq_offset = GUC_DB_SIZE;
749 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100750
Dave Gordonf10d69a2016-06-13 17:57:33 +0100751 db_id = select_doorbell_register(guc, client->priority);
752 if (db_id == GUC_INVALID_DOORBELL_ID)
753 /* XXX: evict a doorbell instead? */
754 goto err;
755
Dave Gordon44a28b12015-08-12 15:43:41 +0100756 client->doorbell_offset = select_doorbell_cacheline(guc);
757
758 /*
759 * Since the doorbell only requires a single cacheline, we can save
760 * space by putting the application process descriptor in the same
761 * page. Use the half of the page that doesn't include the doorbell.
762 */
763 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
764 client->proc_desc_offset = 0;
765 else
766 client->proc_desc_offset = (GUC_DB_SIZE / 2);
767
Dave Gordon7a9347f2016-09-12 21:19:37 +0100768 guc_proc_desc_init(guc, client);
769 guc_ctx_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000770
771 /* For runtime client allocation we need to enable the doorbell. Not
772 * required yet for the static execbuf_client as this special kernel
773 * client is enabled from i915_guc_submission_enable().
774 *
775 * guc_update_doorbell_id(guc, client, db_id);
776 */
Dave Gordon44a28b12015-08-12 15:43:41 +0100777
Dave Gordone02757d2016-08-09 15:19:21 +0100778 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
779 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100780 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
781 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100782
783 return client;
784
785err:
Dave Gordon0daf5562016-06-10 18:29:25 +0100786 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100787 return NULL;
788}
789
Akash Goelf8240832016-10-12 21:54:34 +0530790/*
791 * Sub buffer switch callback. Called whenever relay has to switch to a new
792 * sub buffer, relay stays on the same sub buffer if 0 is returned.
793 */
794static int subbuf_start_callback(struct rchan_buf *buf,
795 void *subbuf,
796 void *prev_subbuf,
797 size_t prev_padding)
798{
799 /* Use no-overwrite mode by default, where relay will stop accepting
800 * new data if there are no empty sub buffers left.
801 * There is no strict synchronization enforced by relay between Consumer
802 * and Producer. In overwrite mode, there is a possibility of getting
803 * inconsistent/garbled data, the producer could be writing on to the
804 * same sub buffer from which Consumer is reading. This can't be avoided
805 * unless Consumer is fast enough and can always run in tandem with
806 * Producer.
807 */
808 if (relay_buf_full(buf))
809 return 0;
810
811 return 1;
812}
813
814/*
815 * file_create() callback. Creates relay file in debugfs.
816 */
817static struct dentry *create_buf_file_callback(const char *filename,
818 struct dentry *parent,
819 umode_t mode,
820 struct rchan_buf *buf,
821 int *is_global)
822{
823 struct dentry *buf_file;
824
Akash Goelf8240832016-10-12 21:54:34 +0530825 /* This to enable the use of a single buffer for the relay channel and
826 * correspondingly have a single file exposed to User, through which
827 * it can collect the logs in order without any post-processing.
Akash Goel1e6b8b02016-10-12 21:54:43 +0530828 * Need to set 'is_global' even if parent is NULL for early logging.
Akash Goelf8240832016-10-12 21:54:34 +0530829 */
830 *is_global = 1;
831
Akash Goel1e6b8b02016-10-12 21:54:43 +0530832 if (!parent)
833 return NULL;
834
Akash Goelf8240832016-10-12 21:54:34 +0530835 /* Not using the channel filename passed as an argument, since for each
836 * channel relay appends the corresponding CPU number to the filename
837 * passed in relay_open(). This should be fine as relay just needs a
838 * dentry of the file associated with the channel buffer and that file's
839 * name need not be same as the filename passed as an argument.
840 */
841 buf_file = debugfs_create_file("guc_log", mode,
842 parent, buf, &relay_file_operations);
843 return buf_file;
844}
845
846/*
847 * file_remove() default callback. Removes relay file in debugfs.
848 */
849static int remove_buf_file_callback(struct dentry *dentry)
850{
851 debugfs_remove(dentry);
852 return 0;
853}
854
855/* relay channel callbacks */
856static struct rchan_callbacks relay_callbacks = {
857 .subbuf_start = subbuf_start_callback,
858 .create_buf_file = create_buf_file_callback,
859 .remove_buf_file = remove_buf_file_callback,
860};
861
862static void guc_log_remove_relay_file(struct intel_guc *guc)
863{
864 relay_close(guc->log.relay_chan);
865}
866
Akash Goel1e6b8b02016-10-12 21:54:43 +0530867static int guc_log_create_relay_channel(struct intel_guc *guc)
Akash Goelf8240832016-10-12 21:54:34 +0530868{
869 struct drm_i915_private *dev_priv = guc_to_i915(guc);
870 struct rchan *guc_log_relay_chan;
Akash Goelf8240832016-10-12 21:54:34 +0530871 size_t n_subbufs, subbuf_size;
872
Akash Goel1e6b8b02016-10-12 21:54:43 +0530873 /* Keep the size of sub buffers same as shared log buffer */
874 subbuf_size = guc->log.vma->obj->base.size;
875
876 /* Store up to 8 snapshots, which is large enough to buffer sufficient
877 * boot time logs and provides enough leeway to User, in terms of
878 * latency, for consuming the logs from relay. Also doesn't take
879 * up too much memory.
880 */
881 n_subbufs = 8;
882
883 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
884 n_subbufs, &relay_callbacks, dev_priv);
885 if (!guc_log_relay_chan) {
886 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
887 return -ENOMEM;
888 }
889
890 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
891 guc->log.relay_chan = guc_log_relay_chan;
892 return 0;
893}
894
895static int guc_log_create_relay_file(struct intel_guc *guc)
896{
897 struct drm_i915_private *dev_priv = guc_to_i915(guc);
898 struct dentry *log_dir;
899 int ret;
900
Akash Goelf8240832016-10-12 21:54:34 +0530901 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
902 log_dir = dev_priv->drm.primary->debugfs_root;
903
904 /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
905 * not mounted and so can't create the relay file.
906 * The relay API seems to fit well with debugfs only, for availing relay
907 * there are 3 requirements which can be met for debugfs file only in a
908 * straightforward/clean manner :-
909 * i) Need the associated dentry pointer of the file, while opening the
910 * relay channel.
911 * ii) Should be able to use 'relay_file_operations' fops for the file.
912 * iii) Set the 'i_private' field of file's inode to the pointer of
913 * relay channel buffer.
914 */
915 if (!log_dir) {
916 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
917 return -ENODEV;
918 }
919
Akash Goel1e6b8b02016-10-12 21:54:43 +0530920 ret = relay_late_setup_files(guc->log.relay_chan, "guc_log", log_dir);
921 if (ret) {
922 DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
923 return ret;
Akash Goelf8240832016-10-12 21:54:34 +0530924 }
925
Akash Goelf8240832016-10-12 21:54:34 +0530926 return 0;
927}
928
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530929static void guc_move_to_next_buf(struct intel_guc *guc)
930{
Akash Goelf8240832016-10-12 21:54:34 +0530931 /* Make sure the updates made in the sub buffer are visible when
932 * Consumer sees the following update to offset inside the sub buffer.
933 */
934 smp_wmb();
935
936 /* All data has been written, so now move the offset of sub buffer. */
937 relay_reserve(guc->log.relay_chan, guc->log.vma->obj->base.size);
938
939 /* Switch to the next sub buffer */
940 relay_flush(guc->log.relay_chan);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530941}
942
943static void *guc_get_write_buffer(struct intel_guc *guc)
944{
Akash Goelf8240832016-10-12 21:54:34 +0530945 if (!guc->log.relay_chan)
946 return NULL;
947
948 /* Just get the base address of a new sub buffer and copy data into it
949 * ourselves. NULL will be returned in no-overwrite mode, if all sub
950 * buffers are full. Could have used the relay_write() to indirectly
951 * copy the data, but that would have been bit convoluted, as we need to
952 * write to only certain locations inside a sub buffer which cannot be
953 * done without using relay_reserve() along with relay_write(). So its
954 * better to use relay_reserve() alone.
955 */
956 return relay_reserve(guc->log.relay_chan, 0);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530957}
958
Akash Goel5aa1ee42016-10-12 21:54:36 +0530959static bool
960guc_check_log_buf_overflow(struct intel_guc *guc,
961 enum guc_log_buffer_type type, unsigned int full_cnt)
962{
963 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
964 bool overflow = false;
965
966 if (full_cnt != prev_full_cnt) {
967 overflow = true;
968
969 guc->log.prev_overflow_count[type] = full_cnt;
970 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
971
972 if (full_cnt < prev_full_cnt) {
973 /* buffer_full_cnt is a 4 bit counter */
974 guc->log.total_overflow_count[type] += 16;
975 }
976 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
977 }
978
979 return overflow;
980}
981
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530982static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
983{
984 switch (type) {
985 case GUC_ISR_LOG_BUFFER:
986 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
987 case GUC_DPC_LOG_BUFFER:
988 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
989 case GUC_CRASH_DUMP_LOG_BUFFER:
990 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
991 default:
992 MISSING_CASE(type);
993 }
994
995 return 0;
996}
997
998static void guc_read_update_log_buffer(struct intel_guc *guc)
999{
Akash Goel6941f3c2016-10-12 21:54:37 +05301000 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301001 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
1002 struct guc_log_buffer_state log_buf_state_local;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301003 enum guc_log_buffer_type type;
1004 void *src_data, *dst_data;
Akash Goel6941f3c2016-10-12 21:54:37 +05301005 bool new_overflow;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301006
1007 if (WARN_ON(!guc->log.buf_addr))
1008 return;
1009
1010 /* Get the pointer to shared GuC log buffer */
1011 log_buf_state = src_data = guc->log.buf_addr;
1012
1013 /* Get the pointer to local buffer to store the logs */
1014 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
1015
1016 /* Actual logs are present from the 2nd page */
1017 src_data += PAGE_SIZE;
1018 dst_data += PAGE_SIZE;
1019
1020 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
1021 /* Make a copy of the state structure, inside GuC log buffer
1022 * (which is uncached mapped), on the stack to avoid reading
1023 * from it multiple times.
1024 */
1025 memcpy(&log_buf_state_local, log_buf_state,
1026 sizeof(struct guc_log_buffer_state));
1027 buffer_size = guc_get_log_buffer_size(type);
Akash Goel6941f3c2016-10-12 21:54:37 +05301028 read_offset = log_buf_state_local.read_ptr;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301029 write_offset = log_buf_state_local.sampled_write_ptr;
Akash Goel5aa1ee42016-10-12 21:54:36 +05301030 full_cnt = log_buf_state_local.buffer_full_cnt;
1031
1032 /* Bookkeeping stuff */
1033 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
Akash Goel6941f3c2016-10-12 21:54:37 +05301034 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301035
1036 /* Update the state of shared log buffer */
1037 log_buf_state->read_ptr = write_offset;
1038 log_buf_state->flush_to_file = 0;
1039 log_buf_state++;
1040
1041 if (unlikely(!log_buf_snapshot_state))
1042 continue;
1043
1044 /* First copy the state structure in snapshot buffer */
1045 memcpy(log_buf_snapshot_state, &log_buf_state_local,
1046 sizeof(struct guc_log_buffer_state));
1047
1048 /* The write pointer could have been updated by GuC firmware,
1049 * after sending the flush interrupt to Host, for consistency
1050 * set write pointer value to same value of sampled_write_ptr
1051 * in the snapshot buffer.
1052 */
1053 log_buf_snapshot_state->write_ptr = write_offset;
1054 log_buf_snapshot_state++;
1055
1056 /* Now copy the actual logs. */
Akash Goel6941f3c2016-10-12 21:54:37 +05301057 if (unlikely(new_overflow)) {
1058 /* copy the whole buffer in case of overflow */
1059 read_offset = 0;
1060 write_offset = buffer_size;
1061 } else if (unlikely((read_offset > buffer_size) ||
1062 (write_offset > buffer_size))) {
1063 DRM_ERROR("invalid log buffer state\n");
1064 /* copy whole buffer as offsets are unreliable */
1065 read_offset = 0;
1066 write_offset = buffer_size;
1067 }
1068
1069 /* Just copy the newly written data */
1070 if (read_offset > write_offset) {
Akash Goel71706592016-10-12 21:54:42 +05301071 i915_memcpy_from_wc(dst_data, src_data, write_offset);
Akash Goel6941f3c2016-10-12 21:54:37 +05301072 bytes_to_copy = buffer_size - read_offset;
1073 } else {
1074 bytes_to_copy = write_offset - read_offset;
1075 }
Akash Goel71706592016-10-12 21:54:42 +05301076 i915_memcpy_from_wc(dst_data + read_offset,
1077 src_data + read_offset, bytes_to_copy);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301078
1079 src_data += buffer_size;
1080 dst_data += buffer_size;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301081 }
1082
1083 if (log_buf_snapshot_state)
1084 guc_move_to_next_buf(guc);
Akash Goelf8240832016-10-12 21:54:34 +05301085 else {
1086 /* Used rate limited to avoid deluge of messages, logs might be
1087 * getting consumed by User at a slow rate.
1088 */
1089 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
Akash Goel5aa1ee42016-10-12 21:54:36 +05301090 guc->log.capture_miss_count++;
Akash Goelf8240832016-10-12 21:54:34 +05301091 }
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301092}
1093
1094static void guc_capture_logs_work(struct work_struct *work)
1095{
1096 struct drm_i915_private *dev_priv =
1097 container_of(work, struct drm_i915_private, guc.log.flush_work);
1098
1099 i915_guc_capture_logs(dev_priv);
1100}
1101
1102static void guc_log_cleanup(struct intel_guc *guc)
1103{
1104 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1105
1106 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1107
1108 /* First disable the flush interrupt */
1109 gen9_disable_guc_interrupts(dev_priv);
1110
1111 if (guc->log.flush_wq)
1112 destroy_workqueue(guc->log.flush_wq);
1113
1114 guc->log.flush_wq = NULL;
1115
Akash Goelf8240832016-10-12 21:54:34 +05301116 if (guc->log.relay_chan)
1117 guc_log_remove_relay_file(guc);
1118
1119 guc->log.relay_chan = NULL;
1120
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301121 if (guc->log.buf_addr)
1122 i915_gem_object_unpin_map(guc->log.vma->obj);
1123
1124 guc->log.buf_addr = NULL;
1125}
1126
1127static int guc_log_create_extras(struct intel_guc *guc)
1128{
1129 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1130 void *vaddr;
1131 int ret;
1132
1133 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1134
1135 /* Nothing to do */
1136 if (i915.guc_log_level < 0)
1137 return 0;
1138
1139 if (!guc->log.buf_addr) {
Akash Goel71706592016-10-12 21:54:42 +05301140 /* Create a WC (Uncached for read) vmalloc mapping of log
1141 * buffer pages, so that we can directly get the data
1142 * (up-to-date) from memory.
1143 */
1144 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301145 if (IS_ERR(vaddr)) {
1146 ret = PTR_ERR(vaddr);
1147 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
1148 return ret;
1149 }
1150
1151 guc->log.buf_addr = vaddr;
1152 }
1153
Akash Goel1e6b8b02016-10-12 21:54:43 +05301154 if (!guc->log.relay_chan) {
1155 /* Create a relay channel, so that we have buffers for storing
1156 * the GuC firmware logs, the channel will be linked with a file
1157 * later on when debugfs is registered.
1158 */
1159 ret = guc_log_create_relay_channel(guc);
1160 if (ret)
1161 return ret;
1162 }
1163
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301164 if (!guc->log.flush_wq) {
1165 INIT_WORK(&guc->log.flush_work, guc_capture_logs_work);
1166
Akash Goel7ef54de2016-10-12 21:54:44 +05301167 /*
1168 * GuC log buffer flush work item has to do register access to
1169 * send the ack to GuC and this work item, if not synced before
1170 * suspend, can potentially get executed after the GFX device is
1171 * suspended.
1172 * By marking the WQ as freezable, we don't have to bother about
1173 * flushing of this work item from the suspend hooks, the pending
1174 * work item if any will be either executed before the suspend
1175 * or scheduled later on resume. This way the handling of work
1176 * item can be kept same between system suspend & rpm suspend.
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301177 */
Akash Goel7ef54de2016-10-12 21:54:44 +05301178 guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log",
1179 WQ_HIGHPRI | WQ_FREEZABLE);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301180 if (guc->log.flush_wq == NULL) {
1181 DRM_ERROR("Couldn't allocate the wq for GuC logging\n");
1182 return -ENOMEM;
1183 }
1184 }
1185
1186 return 0;
1187}
1188
Dave Gordon7a9347f2016-09-12 21:19:37 +01001189static void guc_log_create(struct intel_guc *guc)
Alex Dai4c7e77f2015-08-12 15:43:40 +01001190{
Chris Wilson8b797af2016-08-15 10:48:51 +01001191 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001192 unsigned long offset;
1193 uint32_t size, flags;
1194
Alex Dai4c7e77f2015-08-12 15:43:40 +01001195 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
1196 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
1197
1198 /* The first page is to save log buffer state. Allocate one
1199 * extra page for others in case for overlap */
1200 size = (1 + GUC_LOG_DPC_PAGES + 1 +
1201 GUC_LOG_ISR_PAGES + 1 +
1202 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
1203
Akash Goeld6b40b42016-10-12 21:54:29 +05301204 vma = guc->log.vma;
Chris Wilson8b797af2016-08-15 10:48:51 +01001205 if (!vma) {
Akash Goel71706592016-10-12 21:54:42 +05301206 /* We require SSE 4.1 for fast reads from the GuC log buffer and
1207 * it should be present on the chipsets supporting GuC based
1208 * submisssions.
1209 */
1210 if (WARN_ON(!i915_memcpy_from_wc(NULL, NULL, 0))) {
1211 /* logging will not be enabled */
1212 i915.guc_log_level = -1;
1213 return;
1214 }
1215
Chris Wilson8b797af2016-08-15 10:48:51 +01001216 vma = guc_allocate_vma(guc, size);
1217 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +01001218 /* logging will be off */
1219 i915.guc_log_level = -1;
1220 return;
1221 }
1222
Akash Goeld6b40b42016-10-12 21:54:29 +05301223 guc->log.vma = vma;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301224
1225 if (guc_log_create_extras(guc)) {
1226 guc_log_cleanup(guc);
1227 i915_vma_unpin_and_release(&guc->log.vma);
1228 i915.guc_log_level = -1;
1229 return;
1230 }
Alex Dai4c7e77f2015-08-12 15:43:40 +01001231 }
1232
1233 /* each allocated unit is a page */
1234 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
1235 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
1236 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
1237 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
1238
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001239 offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
Akash Goeld6b40b42016-10-12 21:54:29 +05301240 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001241}
1242
Akash Goelf8240832016-10-12 21:54:34 +05301243static int guc_log_late_setup(struct intel_guc *guc)
1244{
1245 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1246 int ret;
1247
1248 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1249
1250 if (i915.guc_log_level < 0)
1251 return -EINVAL;
1252
1253 /* If log_level was set as -1 at boot time, then setup needed to
1254 * handle log buffer flush interrupts would not have been done yet,
1255 * so do that now.
1256 */
1257 ret = guc_log_create_extras(guc);
1258 if (ret)
1259 goto err;
1260
1261 ret = guc_log_create_relay_file(guc);
1262 if (ret)
1263 goto err;
1264
1265 return 0;
1266err:
1267 guc_log_cleanup(guc);
1268 /* logging will remain off */
1269 i915.guc_log_level = -1;
1270 return ret;
1271}
1272
Dave Gordon7a9347f2016-09-12 21:19:37 +01001273static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -08001274{
1275 struct guc_policy *policy;
1276 u32 p, i;
1277
1278 policies->dpc_promote_time = 500000;
1279 policies->max_num_work_items = POLICY_MAX_NUM_WI;
1280
1281 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -08001282 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -08001283 policy = &policies->policy[p][i];
1284
1285 policy->execution_quantum = 1000000;
1286 policy->preemption_time = 500000;
1287 policy->fault_time = 250000;
1288 policy->policy_flags = 0;
1289 }
1290 }
1291
1292 policies->is_valid = 1;
1293}
1294
Dave Gordon7a9347f2016-09-12 21:19:37 +01001295static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -08001296{
1297 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +01001298 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -08001299 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -08001300 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -08001301 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001302 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301303 enum intel_engine_id id;
Alex Dai68371a92015-12-18 12:00:09 -08001304 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001305 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -08001306
1307 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -08001308 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
1309 sizeof(struct guc_mmio_reg_state) +
1310 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -08001311
Chris Wilson8b797af2016-08-15 10:48:51 +01001312 vma = guc->ads_vma;
1313 if (!vma) {
1314 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
1315 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -08001316 return;
1317
Chris Wilson8b797af2016-08-15 10:48:51 +01001318 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -08001319 }
1320
Chris Wilson8b797af2016-08-15 10:48:51 +01001321 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -08001322 ads = kmap(page);
1323
1324 /*
1325 * The GuC requires a "Golden Context" when it reinitialises
1326 * engines after a reset. Here we use the Render ring default
1327 * context, which must already exist and be pinned in the GGTT,
1328 * so its address won't change after we've told the GuC where
1329 * to find it.
1330 */
Akash Goel3b3f1652016-10-13 22:44:48 +05301331 engine = dev_priv->engine[RCS];
Chris Wilson57e88532016-08-15 10:48:57 +01001332 ads->golden_context_lrca = engine->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -08001333
Akash Goel3b3f1652016-10-13 22:44:48 +05301334 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001335 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -08001336
Alex Dai463704d2015-12-18 12:00:10 -08001337 /* GuC scheduling policies */
1338 policies = (void *)ads + sizeof(struct guc_ads);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001339 guc_policies_init(policies);
Alex Dai463704d2015-12-18 12:00:10 -08001340
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001341 ads->scheduler_policies =
1342 i915_ggtt_offset(vma) + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -08001343
Alex Dai5c148e02015-12-18 12:00:11 -08001344 /* MMIO reg state */
1345 reg_state = (void *)policies + sizeof(struct guc_policies);
1346
Akash Goel3b3f1652016-10-13 22:44:48 +05301347 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001348 reg_state->mmio_white_list[engine->guc_id].mmio_start =
1349 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -08001350
1351 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001352 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -08001353 }
1354
1355 ads->reg_state_addr = ads->scheduler_policies +
1356 sizeof(struct guc_policies);
1357
1358 ads->reg_state_buffer = ads->reg_state_addr +
1359 sizeof(struct guc_mmio_reg_state);
1360
Alex Dai68371a92015-12-18 12:00:09 -08001361 kunmap(page);
1362}
1363
Alex Daibac427f2015-08-12 15:43:39 +01001364/*
1365 * Set up the memory resources to be shared with the GuC. At this point,
1366 * we require just one object that can be mapped through the GGTT.
1367 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001368int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001369{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001370 const size_t ctxsize = sizeof(struct guc_context_desc);
1371 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1372 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001373 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001374 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +01001375
Chris Wilson4d357af2016-11-29 12:10:23 +00001376 if (!HAS_GUC_SCHED(dev_priv))
1377 return 0;
1378
Dave Gordon29fb72c2016-06-07 09:14:50 +01001379 /* Wipe bitmap & delete client in case of reinitialisation */
1380 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001381 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001382
Alex Daibac427f2015-08-12 15:43:39 +01001383 if (!i915.enable_guc_submission)
1384 return 0; /* not enabled */
1385
Chris Wilson8b797af2016-08-15 10:48:51 +01001386 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001387 return 0; /* already allocated */
1388
Dave Gordon7a9347f2016-09-12 21:19:37 +01001389 vma = guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001390 if (IS_ERR(vma))
1391 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001392
Chris Wilson8b797af2016-08-15 10:48:51 +01001393 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001394 ida_init(&guc->ctx_ids);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001395 guc_log_create(guc);
1396 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001397
Chris Wilson4d357af2016-11-29 12:10:23 +00001398 guc->execbuf_client = guc_client_alloc(dev_priv,
1399 INTEL_INFO(dev_priv)->ring_mask,
1400 GUC_CTX_PRIORITY_KMD_NORMAL,
1401 dev_priv->kernel_context);
1402 if (!guc->execbuf_client) {
1403 DRM_ERROR("Failed to create GuC client for execbuf!\n");
1404 goto err;
1405 }
1406
Alex Daibac427f2015-08-12 15:43:39 +01001407 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +00001408
1409err:
1410 i915_guc_submission_fini(dev_priv);
1411 return -ENOMEM;
1412}
1413
1414static void guc_reset_wq(struct i915_guc_client *gc)
1415{
1416 struct guc_process_desc *desc = gc->vaddr + gc->proc_desc_offset;
1417
1418 desc->head = 0;
1419 desc->tail = 0;
1420
1421 gc->wq_tail = 0;
Alex Daibac427f2015-08-12 15:43:39 +01001422}
1423
Dave Gordonbeffa512016-06-10 18:29:26 +01001424int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001425{
Dave Gordon44a28b12015-08-12 15:43:41 +01001426 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001427 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001428 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301429 enum intel_engine_id id;
Dave Gordon44a28b12015-08-12 15:43:41 +01001430
Chris Wilson4d357af2016-11-29 12:10:23 +00001431 if (!client)
1432 return -ENODEV;
Dave Gordon44a28b12015-08-12 15:43:41 +01001433
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001434 intel_guc_sample_forcewake(guc);
Chris Wilson4d357af2016-11-29 12:10:23 +00001435
1436 guc_reset_wq(client);
Dave Gordon4d757872016-06-13 17:57:34 +01001437 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001438
Chris Wilsonddd66c52016-08-02 22:50:31 +01001439 /* Take over from manual control of ELSP (execlists) */
Akash Goel3b3f1652016-10-13 22:44:48 +05301440 for_each_engine(engine, dev_priv, id) {
Chris Wilson4d357af2016-11-29 12:10:23 +00001441 struct drm_i915_gem_request *rq;
1442
Chris Wilsonddd66c52016-08-02 22:50:31 +01001443 engine->submit_request = i915_guc_submit;
Chris Wilson20311bd2016-11-14 20:41:03 +00001444 engine->schedule = NULL;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001445
Chris Wilson821ed7d2016-09-09 14:11:53 +01001446 /* Replay the current set of previously submitted requests */
Chris Wilson4d357af2016-11-29 12:10:23 +00001447 list_for_each_entry(rq, &engine->timeline->requests, link) {
Chris Wilsondadd4812016-09-09 14:11:57 +01001448 client->wq_rsvd += sizeof(struct guc_wq_item);
Chris Wilson34ba5a82016-11-29 12:10:24 +00001449 __i915_guc_submit(rq);
Chris Wilsondadd4812016-09-09 14:11:57 +01001450 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001451 }
1452
Dave Gordon44a28b12015-08-12 15:43:41 +01001453 return 0;
1454}
1455
Dave Gordonbeffa512016-06-10 18:29:26 +01001456void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001457{
Dave Gordon44a28b12015-08-12 15:43:41 +01001458 struct intel_guc *guc = &dev_priv->guc;
1459
Chris Wilsonddd66c52016-08-02 22:50:31 +01001460 if (!guc->execbuf_client)
1461 return;
1462
Chris Wilsonddd66c52016-08-02 22:50:31 +01001463 /* Revert back to manual ELSP submission */
1464 intel_execlists_enable_submission(dev_priv);
Dave Gordon44a28b12015-08-12 15:43:41 +01001465}
1466
Dave Gordonbeffa512016-06-10 18:29:26 +01001467void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001468{
Alex Daibac427f2015-08-12 15:43:39 +01001469 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001470 struct i915_guc_client *client;
1471
1472 client = fetch_and_zero(&guc->execbuf_client);
1473 if (!client)
1474 return;
1475
1476 guc_client_free(dev_priv, client);
Alex Daibac427f2015-08-12 15:43:39 +01001477
Chris Wilson19880c42016-08-15 10:49:05 +01001478 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301479 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001480
Chris Wilson8b797af2016-08-15 10:48:51 +01001481 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001482 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001483 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001484}
Alex Daia1c41992015-09-30 09:46:37 -07001485
1486/**
1487 * intel_guc_suspend() - notify GuC entering suspend state
1488 * @dev: drm device
1489 */
1490int intel_guc_suspend(struct drm_device *dev)
1491{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001492 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001493 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001494 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001495 u32 data[3];
1496
Dave Gordonfce91f22016-05-20 11:42:42 +01001497 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001498 return 0;
1499
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301500 gen9_disable_guc_interrupts(dev_priv);
1501
Dave Gordoned54c1a2016-01-19 19:02:54 +00001502 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001503
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001504 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001505 /* any value greater than GUC_POWER_D0 */
1506 data[1] = GUC_POWER_D1;
1507 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001508 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001509
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001510 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001511}
1512
1513
1514/**
1515 * intel_guc_resume() - notify GuC resuming from suspend state
1516 * @dev: drm device
1517 */
1518int intel_guc_resume(struct drm_device *dev)
1519{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001520 struct drm_i915_private *dev_priv = to_i915(dev);
Alex Daia1c41992015-09-30 09:46:37 -07001521 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001522 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001523 u32 data[3];
1524
Dave Gordonfce91f22016-05-20 11:42:42 +01001525 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001526 return 0;
1527
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301528 if (i915.guc_log_level >= 0)
1529 gen9_enable_guc_interrupts(dev_priv);
1530
Dave Gordoned54c1a2016-01-19 19:02:54 +00001531 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001532
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001533 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001534 data[1] = GUC_POWER_D0;
1535 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001536 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001537
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001538 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001539}
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301540
1541void i915_guc_capture_logs(struct drm_i915_private *dev_priv)
1542{
1543 guc_read_update_log_buffer(&dev_priv->guc);
1544
1545 /* Generally device is expected to be active only at this
1546 * time, so get/put should be really quick.
1547 */
1548 intel_runtime_pm_get(dev_priv);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001549 intel_guc_log_flush_complete(&dev_priv->guc);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301550 intel_runtime_pm_put(dev_priv);
1551}
Akash Goelf8240832016-10-12 21:54:34 +05301552
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301553void i915_guc_flush_logs(struct drm_i915_private *dev_priv)
1554{
1555 if (!i915.enable_guc_submission || (i915.guc_log_level < 0))
1556 return;
1557
1558 /* First disable the interrupts, will be renabled afterwards */
1559 gen9_disable_guc_interrupts(dev_priv);
1560
1561 /* Before initiating the forceful flush, wait for any pending/ongoing
1562 * flush to complete otherwise forceful flush may not actually happen.
1563 */
1564 flush_work(&dev_priv->guc.log.flush_work);
1565
1566 /* Ask GuC to update the log buffer state */
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001567 intel_guc_log_flush(&dev_priv->guc);
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301568
1569 /* GuC would have updated log buffer by now, so capture it */
1570 i915_guc_capture_logs(dev_priv);
1571}
1572
Akash Goelf8240832016-10-12 21:54:34 +05301573void i915_guc_unregister(struct drm_i915_private *dev_priv)
1574{
1575 if (!i915.enable_guc_submission)
1576 return;
1577
1578 mutex_lock(&dev_priv->drm.struct_mutex);
1579 guc_log_cleanup(&dev_priv->guc);
1580 mutex_unlock(&dev_priv->drm.struct_mutex);
1581}
1582
1583void i915_guc_register(struct drm_i915_private *dev_priv)
1584{
1585 if (!i915.enable_guc_submission)
1586 return;
1587
1588 mutex_lock(&dev_priv->drm.struct_mutex);
1589 guc_log_late_setup(&dev_priv->guc);
1590 mutex_unlock(&dev_priv->drm.struct_mutex);
1591}
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301592
1593int i915_guc_log_control(struct drm_i915_private *dev_priv, u64 control_val)
1594{
1595 union guc_log_control log_param;
1596 int ret;
1597
1598 log_param.value = control_val;
1599
1600 if (log_param.verbosity < GUC_LOG_VERBOSITY_MIN ||
1601 log_param.verbosity > GUC_LOG_VERBOSITY_MAX)
1602 return -EINVAL;
1603
1604 /* This combination doesn't make sense & won't have any effect */
1605 if (!log_param.logging_enabled && (i915.guc_log_level < 0))
1606 return 0;
1607
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001608 ret = intel_guc_log_control(&dev_priv->guc, log_param.value);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301609 if (ret < 0) {
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001610 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301611 return ret;
1612 }
1613
1614 i915.guc_log_level = log_param.verbosity;
1615
1616 /* If log_level was set as -1 at boot time, then the relay channel file
1617 * wouldn't have been created by now and interrupts also would not have
1618 * been enabled.
1619 */
1620 if (!dev_priv->guc.log.relay_chan) {
1621 ret = guc_log_late_setup(&dev_priv->guc);
1622 if (!ret)
1623 gen9_enable_guc_interrupts(dev_priv);
1624 } else if (!log_param.logging_enabled) {
1625 /* Once logging is disabled, GuC won't generate logs & send an
1626 * interrupt. But there could be some data in the log buffer
1627 * which is yet to be captured. So request GuC to update the log
1628 * buffer state and then collect the left over logs.
1629 */
1630 i915_guc_flush_logs(dev_priv);
1631
1632 /* As logging is disabled, update log level to reflect that */
1633 i915.guc_log_level = -1;
1634 } else {
1635 /* In case interrupts were disabled, enable them now */
1636 gen9_enable_guc_interrupts(dev_priv);
1637 }
1638
1639 return ret;
1640}