blob: 913d873589728f3f841aaae202525e65bd04184e [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 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
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000353 spin_lock(&client->wq_lock);
354 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 }
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000363 spin_unlock(&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 Wilson5ba89902016-10-07 07:53:27 +0100368void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
369{
370 const size_t wqi_size = sizeof(struct guc_wq_item);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000371 struct i915_guc_client *client = request->i915->guc.execbuf_client;
Chris Wilson5ba89902016-10-07 07:53:27 +0100372
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000373 GEM_BUG_ON(READ_ONCE(client->wq_rsvd) < wqi_size);
Chris Wilson5ba89902016-10-07 07:53:27 +0100374
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000375 spin_lock(&client->wq_lock);
376 client->wq_rsvd -= wqi_size;
377 spin_unlock(&client->wq_lock);
Chris Wilson5ba89902016-10-07 07:53:27 +0100378}
379
Dave Gordon7a9347f2016-09-12 21:19:37 +0100380/* Construct a Work Item and append it to the GuC's Work Queue */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000381static void guc_wq_item_append(struct i915_guc_client *client,
Dave Gordon7a9347f2016-09-12 21:19:37 +0100382 struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100383{
Dave Gordon0a31afb2016-05-13 15:36:34 +0100384 /* wqi_len is in DWords, and does not include the one-word header */
385 const size_t wqi_size = sizeof(struct guc_wq_item);
386 const u32 wqi_len = wqi_size/sizeof(u32) - 1;
Dave Gordonc18468c2016-08-09 15:19:22 +0100387 struct intel_engine_cs *engine = rq->engine;
Alex Daia5916e82016-04-19 16:08:35 +0100388 struct guc_process_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100389 struct guc_wq_item *wqi;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000390 u32 freespace, tail, wq_off;
Dave Gordon44a28b12015-08-12 15:43:41 +0100391
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000392 desc = client->vaddr + client->proc_desc_offset;
Alex Daia7e02192015-12-16 11:45:55 -0800393
Dave Gordon7a9347f2016-09-12 21:19:37 +0100394 /* Free space is guaranteed, see i915_guc_wq_reserve() above */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000395 freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
Dave Gordon0a31afb2016-05-13 15:36:34 +0100396 GEM_BUG_ON(freespace < wqi_size);
397
398 /* The GuC firmware wants the tail index in QWords, not bytes */
399 tail = rq->tail;
400 GEM_BUG_ON(tail & 7);
401 tail >>= 3;
402 GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
Dave Gordon44a28b12015-08-12 15:43:41 +0100403
404 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
405 * should not have the case where structure wqi is across page, neither
406 * wrapped to the beginning. This simplifies the implementation below.
407 *
408 * XXX: if not the case, we need save data to a temp wqi and copy it to
409 * workqueue buffer dw by dw.
410 */
Dave Gordon0a31afb2016-05-13 15:36:34 +0100411 BUILD_BUG_ON(wqi_size != 16);
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000412 GEM_BUG_ON(client->wq_rsvd < wqi_size);
Dave Gordon44a28b12015-08-12 15:43:41 +0100413
Dave Gordon0a31afb2016-05-13 15:36:34 +0100414 /* postincrement WQ tail for next time */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000415 wq_off = client->wq_tail;
Chris Wilsondadd4812016-09-09 14:11:57 +0100416 GEM_BUG_ON(wq_off & (wqi_size - 1));
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000417 client->wq_tail += wqi_size;
418 client->wq_tail &= client->wq_size - 1;
419 client->wq_rsvd -= wqi_size;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100420
421 /* WQ starts from the page after doorbell / process_desc */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000422 wqi = client->vaddr + wq_off + GUC_DB_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100423
Dave Gordon0a31afb2016-05-13 15:36:34 +0100424 /* Now fill in the 4-word work queue item */
Dave Gordon44a28b12015-08-12 15:43:41 +0100425 wqi->header = WQ_TYPE_INORDER |
Dave Gordon0a31afb2016-05-13 15:36:34 +0100426 (wqi_len << WQ_LEN_SHIFT) |
Dave Gordonc18468c2016-08-09 15:19:22 +0100427 (engine->guc_id << WQ_TARGET_SHIFT) |
Dave Gordon44a28b12015-08-12 15:43:41 +0100428 WQ_NO_WCFLUSH_WAIT;
429
430 /* The GuC wants only the low-order word of the context descriptor */
Dave Gordonc18468c2016-08-09 15:19:22 +0100431 wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
Dave Gordon44a28b12015-08-12 15:43:41 +0100432
Dave Gordon44a28b12015-08-12 15:43:41 +0100433 wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
Chris Wilson65e47602016-10-28 13:58:49 +0100434 wqi->fence_id = rq->global_seqno;
Dave Gordon44a28b12015-08-12 15:43:41 +0100435}
436
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000437static int guc_ring_doorbell(struct i915_guc_client *client)
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100438{
439 struct guc_process_desc *desc;
440 union guc_doorbell_qw db_cmp, db_exc, db_ret;
441 union guc_doorbell_qw *db;
442 int attempt = 2, ret = -EAGAIN;
443
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000444 desc = client->vaddr + client->proc_desc_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100445
446 /* Update the tail so it is visible to GuC */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000447 desc->tail = client->wq_tail;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100448
449 /* current cookie */
450 db_cmp.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000451 db_cmp.cookie = client->doorbell_cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100452
453 /* cookie to be updated */
454 db_exc.db_status = GUC_DOORBELL_ENABLED;
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000455 db_exc.cookie = client->doorbell_cookie + 1;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100456 if (db_exc.cookie == 0)
457 db_exc.cookie = 1;
458
459 /* pointer of current doorbell cacheline */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000460 db = client->vaddr + client->doorbell_offset;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100461
462 while (attempt--) {
463 /* lets ring the doorbell */
464 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
465 db_cmp.value_qw, db_exc.value_qw);
466
467 /* if the exchange was successfully executed */
468 if (db_ret.value_qw == db_cmp.value_qw) {
469 /* db was successfully rung */
Michal Wajdeczko776594d2016-12-15 19:53:21 +0000470 client->doorbell_cookie = db_exc.cookie;
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100471 ret = 0;
472 break;
473 }
474
475 /* XXX: doorbell was lost and need to acquire it again */
476 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
477 break;
478
Dave Gordon535b2f52016-08-18 18:17:23 +0100479 DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
480 db_cmp.cookie, db_ret.cookie);
Dave Gordon10d2c3e2016-06-13 17:57:31 +0100481
482 /* update the cookie to newly read cookie from GuC */
483 db_cmp.cookie = db_ret.cookie;
484 db_exc.cookie = db_ret.cookie + 1;
485 if (db_exc.cookie == 0)
486 db_exc.cookie = 1;
487 }
488
489 return ret;
490}
491
Dave Gordon44a28b12015-08-12 15:43:41 +0100492/**
Chris Wilson34ba5a82016-11-29 12:10:24 +0000493 * __i915_guc_submit() - Submit commands through GuC
Alex Daifeda33e2015-10-19 16:10:54 -0700494 * @rq: request associated with the commands
Dave Gordon44a28b12015-08-12 15:43:41 +0100495 *
Dave Gordon7a9347f2016-09-12 21:19:37 +0100496 * The caller must have already called i915_guc_wq_reserve() above with
497 * a result of 0 (success), guaranteeing that there is space in the work
498 * queue for the new request, so enqueuing the item cannot fail.
Dave Gordon7c2c2702016-05-13 15:36:32 +0100499 *
500 * Bad Things Will Happen if the caller violates this protocol e.g. calls
Dave Gordon7a9347f2016-09-12 21:19:37 +0100501 * submit() when _reserve() says there's no space, or calls _submit()
502 * a different number of times from (successful) calls to _reserve().
Dave Gordon7c2c2702016-05-13 15:36:32 +0100503 *
504 * The only error here arises if the doorbell hardware isn't functioning
505 * as expected, which really shouln't happen.
Dave Gordon44a28b12015-08-12 15:43:41 +0100506 */
Chris Wilson34ba5a82016-11-29 12:10:24 +0000507static void __i915_guc_submit(struct drm_i915_gem_request *rq)
Dave Gordon44a28b12015-08-12 15:43:41 +0100508{
Akash Goeled4596ea2016-10-25 22:05:23 +0530509 struct drm_i915_private *dev_priv = rq->i915;
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000510 struct intel_engine_cs *engine = rq->engine;
511 unsigned int engine_id = engine->id;
Dave Gordon7c2c2702016-05-13 15:36:32 +0100512 struct intel_guc *guc = &rq->i915->guc;
513 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100514 int b_ret;
Dave Gordon44a28b12015-08-12 15:43:41 +0100515
Chris Wilsondadd4812016-09-09 14:11:57 +0100516 spin_lock(&client->wq_lock);
Dave Gordon7a9347f2016-09-12 21:19:37 +0100517 guc_wq_item_append(client, rq);
Akash Goeled4596ea2016-10-25 22:05:23 +0530518
519 /* WA to flush out the pending GMADR writes to ring buffer. */
520 if (i915_vma_is_map_and_fenceable(rq->ring->vma))
521 POSTING_READ_FW(GUC_STATUS);
522
Dave Gordon0a31afb2016-05-13 15:36:34 +0100523 b_ret = guc_ring_doorbell(client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100524
Alex Dai397097b2016-01-23 11:58:14 -0800525 client->submissions[engine_id] += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100526 client->retcode = b_ret;
527 if (b_ret)
Dave Gordon44a28b12015-08-12 15:43:41 +0100528 client->b_fail += 1;
Dave Gordon0a31afb2016-05-13 15:36:34 +0100529
Alex Dai397097b2016-01-23 11:58:14 -0800530 guc->submissions[engine_id] += 1;
Chris Wilson65e47602016-10-28 13:58:49 +0100531 guc->last_seqno[engine_id] = rq->global_seqno;
Chris Wilsondadd4812016-09-09 14:11:57 +0100532 spin_unlock(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100533}
534
Chris Wilson34ba5a82016-11-29 12:10:24 +0000535static void i915_guc_submit(struct drm_i915_gem_request *rq)
536{
Chris Wilson34ba5a82016-11-29 12:10:24 +0000537 i915_gem_request_submit(rq);
538 __i915_guc_submit(rq);
539}
540
Dave Gordon44a28b12015-08-12 15:43:41 +0100541/*
542 * Everything below here is concerned with setup & teardown, and is
543 * therefore not part of the somewhat time-critical batch-submission
544 * path of i915_guc_submit() above.
545 */
546
547/**
Chris Wilson8b797af2016-08-15 10:48:51 +0100548 * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
549 * @guc: the guc
550 * @size: size of area to allocate (both virtual space and memory)
Alex Daibac427f2015-08-12 15:43:39 +0100551 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100552 * This is a wrapper to create an object for use with the GuC. In order to
553 * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
554 * both some backing storage and a range inside the Global GTT. We must pin
555 * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
556 * range is reserved inside GuC.
Alex Daibac427f2015-08-12 15:43:39 +0100557 *
Chris Wilson8b797af2016-08-15 10:48:51 +0100558 * Return: A i915_vma if successful, otherwise an ERR_PTR.
Alex Daibac427f2015-08-12 15:43:39 +0100559 */
Chris Wilson8b797af2016-08-15 10:48:51 +0100560static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
Alex Daibac427f2015-08-12 15:43:39 +0100561{
Chris Wilson8b797af2016-08-15 10:48:51 +0100562 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Alex Daibac427f2015-08-12 15:43:39 +0100563 struct drm_i915_gem_object *obj;
Chris Wilson8b797af2016-08-15 10:48:51 +0100564 struct i915_vma *vma;
565 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100566
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000567 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100568 if (IS_ERR(obj))
Chris Wilson8b797af2016-08-15 10:48:51 +0100569 return ERR_CAST(obj);
Alex Daibac427f2015-08-12 15:43:39 +0100570
Chris Wilson8b797af2016-08-15 10:48:51 +0100571 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
572 if (IS_ERR(vma))
573 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100574
Chris Wilson8b797af2016-08-15 10:48:51 +0100575 ret = i915_vma_pin(vma, 0, PAGE_SIZE,
576 PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
577 if (ret) {
578 vma = ERR_PTR(ret);
579 goto err;
Alex Daibac427f2015-08-12 15:43:39 +0100580 }
581
Chris Wilson8b797af2016-08-15 10:48:51 +0100582 return vma;
583
584err:
585 i915_gem_object_put(obj);
586 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100587}
588
Dave Gordon0daf5562016-06-10 18:29:25 +0100589static void
590guc_client_free(struct drm_i915_private *dev_priv,
591 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100592{
Dave Gordon44a28b12015-08-12 15:43:41 +0100593 struct intel_guc *guc = &dev_priv->guc;
594
595 if (!client)
596 return;
597
Dave Gordon44a28b12015-08-12 15:43:41 +0100598 /*
599 * XXX: wait for any outstanding submissions before freeing memory.
600 * Be sure to drop any locks
601 */
602
Chris Wilson72aa0d82016-11-02 17:50:47 +0000603 if (client->vaddr) {
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100604 /*
Dave Gordona6674292016-06-13 17:57:32 +0100605 * If we got as far as setting up a doorbell, make sure we
606 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100607 */
Dave Gordona6674292016-06-13 17:57:32 +0100608 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100609
Chris Wilson72aa0d82016-11-02 17:50:47 +0000610 i915_gem_object_unpin_map(client->vma->obj);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100611 }
612
Chris Wilson19880c42016-08-15 10:49:05 +0100613 i915_vma_unpin_and_release(&client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100614
615 if (client->ctx_index != GUC_INVALID_CTX_ID) {
Dave Gordon7a9347f2016-09-12 21:19:37 +0100616 guc_ctx_desc_fini(guc, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100617 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
618 }
619
620 kfree(client);
621}
622
Dave Gordon84b7f882016-08-09 15:19:20 +0100623/* Check that a doorbell register is in the expected state */
624static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
625{
626 struct drm_i915_private *dev_priv = guc_to_i915(guc);
627 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
628 uint32_t value = I915_READ(drbreg);
629 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
630 bool expected = test_bit(db_id, guc->doorbell_bitmap);
631
632 if (enabled == expected)
633 return true;
634
635 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
636 db_id, drbreg.reg, value,
637 expected ? "active" : "inactive");
638
639 return false;
640}
641
Dave Gordon4d757872016-06-13 17:57:34 +0100642/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100643 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100644 * in turn, to ensure that all doorbell h/w is (re)initialised.
645 */
646static void guc_init_doorbell_hw(struct intel_guc *guc)
647{
Dave Gordon4d757872016-06-13 17:57:34 +0100648 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100649 uint16_t db_id;
650 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100651
Chris Wilson4d357af2016-11-29 12:10:23 +0000652 guc_disable_doorbell(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +0100653
654 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100655 /* Skip if doorbell is OK */
656 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100657 continue;
658
Dave Gordon4d757872016-06-13 17:57:34 +0100659 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100660 if (err)
661 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
662 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100663 }
664
Chris Wilson4d357af2016-11-29 12:10:23 +0000665 db_id = select_doorbell_register(guc, client->priority);
666 WARN_ON(db_id == GUC_INVALID_DOORBELL_ID);
667
Dave Gordon4d757872016-06-13 17:57:34 +0100668 err = guc_update_doorbell_id(guc, client, db_id);
669 if (err)
Dave Gordon535b2f52016-08-18 18:17:23 +0100670 DRM_WARN("Failed to restore doorbell to %d, err %d\n",
671 db_id, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100672
Dave Gordon84b7f882016-08-09 15:19:20 +0100673 /* Read back & verify all doorbell registers */
674 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
675 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100676}
677
Dave Gordon44a28b12015-08-12 15:43:41 +0100678/**
679 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100680 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100681 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100682 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
683 * The kernel client to replace ExecList submission is created with
684 * NORMAL priority. Priority of a client for scheduler can be HIGH,
685 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700686 * @ctx: the context that owns the client (we use the default render
687 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100688 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100689 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100690 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100691static struct i915_guc_client *
692guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100693 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100694 uint32_t priority,
695 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100696{
697 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100698 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100699 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000700 void *vaddr;
Dave Gordona6674292016-06-13 17:57:32 +0100701 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100702
703 client = kzalloc(sizeof(*client), GFP_KERNEL);
704 if (!client)
705 return NULL;
706
Alex Daid1675192015-08-12 15:43:43 +0100707 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100708 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100709 client->engines = engines;
710 client->priority = priority;
711 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100712
713 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
714 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
715 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
716 client->ctx_index = GUC_INVALID_CTX_ID;
717 goto err;
718 }
719
720 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100721 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
722 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100723 goto err;
724
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100725 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100726 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000727
728 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
729 if (IS_ERR(vaddr))
730 goto err;
731
732 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100733
734 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100735 client->wq_offset = GUC_DB_SIZE;
736 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100737
Dave Gordonf10d69a2016-06-13 17:57:33 +0100738 db_id = select_doorbell_register(guc, client->priority);
739 if (db_id == GUC_INVALID_DOORBELL_ID)
740 /* XXX: evict a doorbell instead? */
741 goto err;
742
Dave Gordon44a28b12015-08-12 15:43:41 +0100743 client->doorbell_offset = select_doorbell_cacheline(guc);
744
745 /*
746 * Since the doorbell only requires a single cacheline, we can save
747 * space by putting the application process descriptor in the same
748 * page. Use the half of the page that doesn't include the doorbell.
749 */
750 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
751 client->proc_desc_offset = 0;
752 else
753 client->proc_desc_offset = (GUC_DB_SIZE / 2);
754
Dave Gordon7a9347f2016-09-12 21:19:37 +0100755 guc_proc_desc_init(guc, client);
756 guc_ctx_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000757
758 /* For runtime client allocation we need to enable the doorbell. Not
759 * required yet for the static execbuf_client as this special kernel
760 * client is enabled from i915_guc_submission_enable().
761 *
762 * guc_update_doorbell_id(guc, client, db_id);
763 */
Dave Gordon44a28b12015-08-12 15:43:41 +0100764
Dave Gordone02757d2016-08-09 15:19:21 +0100765 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
766 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100767 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
768 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100769
770 return client;
771
772err:
Dave Gordon0daf5562016-06-10 18:29:25 +0100773 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100774 return NULL;
775}
776
Akash Goelf8240832016-10-12 21:54:34 +0530777/*
778 * Sub buffer switch callback. Called whenever relay has to switch to a new
779 * sub buffer, relay stays on the same sub buffer if 0 is returned.
780 */
781static int subbuf_start_callback(struct rchan_buf *buf,
782 void *subbuf,
783 void *prev_subbuf,
784 size_t prev_padding)
785{
786 /* Use no-overwrite mode by default, where relay will stop accepting
787 * new data if there are no empty sub buffers left.
788 * There is no strict synchronization enforced by relay between Consumer
789 * and Producer. In overwrite mode, there is a possibility of getting
790 * inconsistent/garbled data, the producer could be writing on to the
791 * same sub buffer from which Consumer is reading. This can't be avoided
792 * unless Consumer is fast enough and can always run in tandem with
793 * Producer.
794 */
795 if (relay_buf_full(buf))
796 return 0;
797
798 return 1;
799}
800
801/*
802 * file_create() callback. Creates relay file in debugfs.
803 */
804static struct dentry *create_buf_file_callback(const char *filename,
805 struct dentry *parent,
806 umode_t mode,
807 struct rchan_buf *buf,
808 int *is_global)
809{
810 struct dentry *buf_file;
811
Akash Goelf8240832016-10-12 21:54:34 +0530812 /* This to enable the use of a single buffer for the relay channel and
813 * correspondingly have a single file exposed to User, through which
814 * it can collect the logs in order without any post-processing.
Akash Goel1e6b8b02016-10-12 21:54:43 +0530815 * Need to set 'is_global' even if parent is NULL for early logging.
Akash Goelf8240832016-10-12 21:54:34 +0530816 */
817 *is_global = 1;
818
Akash Goel1e6b8b02016-10-12 21:54:43 +0530819 if (!parent)
820 return NULL;
821
Akash Goelf8240832016-10-12 21:54:34 +0530822 /* Not using the channel filename passed as an argument, since for each
823 * channel relay appends the corresponding CPU number to the filename
824 * passed in relay_open(). This should be fine as relay just needs a
825 * dentry of the file associated with the channel buffer and that file's
826 * name need not be same as the filename passed as an argument.
827 */
828 buf_file = debugfs_create_file("guc_log", mode,
829 parent, buf, &relay_file_operations);
830 return buf_file;
831}
832
833/*
834 * file_remove() default callback. Removes relay file in debugfs.
835 */
836static int remove_buf_file_callback(struct dentry *dentry)
837{
838 debugfs_remove(dentry);
839 return 0;
840}
841
842/* relay channel callbacks */
843static struct rchan_callbacks relay_callbacks = {
844 .subbuf_start = subbuf_start_callback,
845 .create_buf_file = create_buf_file_callback,
846 .remove_buf_file = remove_buf_file_callback,
847};
848
849static void guc_log_remove_relay_file(struct intel_guc *guc)
850{
851 relay_close(guc->log.relay_chan);
852}
853
Akash Goel1e6b8b02016-10-12 21:54:43 +0530854static int guc_log_create_relay_channel(struct intel_guc *guc)
Akash Goelf8240832016-10-12 21:54:34 +0530855{
856 struct drm_i915_private *dev_priv = guc_to_i915(guc);
857 struct rchan *guc_log_relay_chan;
Akash Goelf8240832016-10-12 21:54:34 +0530858 size_t n_subbufs, subbuf_size;
859
Akash Goel1e6b8b02016-10-12 21:54:43 +0530860 /* Keep the size of sub buffers same as shared log buffer */
861 subbuf_size = guc->log.vma->obj->base.size;
862
863 /* Store up to 8 snapshots, which is large enough to buffer sufficient
864 * boot time logs and provides enough leeway to User, in terms of
865 * latency, for consuming the logs from relay. Also doesn't take
866 * up too much memory.
867 */
868 n_subbufs = 8;
869
870 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
871 n_subbufs, &relay_callbacks, dev_priv);
872 if (!guc_log_relay_chan) {
873 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
874 return -ENOMEM;
875 }
876
877 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
878 guc->log.relay_chan = guc_log_relay_chan;
879 return 0;
880}
881
882static int guc_log_create_relay_file(struct intel_guc *guc)
883{
884 struct drm_i915_private *dev_priv = guc_to_i915(guc);
885 struct dentry *log_dir;
886 int ret;
887
Akash Goelf8240832016-10-12 21:54:34 +0530888 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
889 log_dir = dev_priv->drm.primary->debugfs_root;
890
891 /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
892 * not mounted and so can't create the relay file.
893 * The relay API seems to fit well with debugfs only, for availing relay
894 * there are 3 requirements which can be met for debugfs file only in a
895 * straightforward/clean manner :-
896 * i) Need the associated dentry pointer of the file, while opening the
897 * relay channel.
898 * ii) Should be able to use 'relay_file_operations' fops for the file.
899 * iii) Set the 'i_private' field of file's inode to the pointer of
900 * relay channel buffer.
901 */
902 if (!log_dir) {
903 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
904 return -ENODEV;
905 }
906
Akash Goel1e6b8b02016-10-12 21:54:43 +0530907 ret = relay_late_setup_files(guc->log.relay_chan, "guc_log", log_dir);
908 if (ret) {
909 DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
910 return ret;
Akash Goelf8240832016-10-12 21:54:34 +0530911 }
912
Akash Goelf8240832016-10-12 21:54:34 +0530913 return 0;
914}
915
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530916static void guc_move_to_next_buf(struct intel_guc *guc)
917{
Akash Goelf8240832016-10-12 21:54:34 +0530918 /* Make sure the updates made in the sub buffer are visible when
919 * Consumer sees the following update to offset inside the sub buffer.
920 */
921 smp_wmb();
922
923 /* All data has been written, so now move the offset of sub buffer. */
924 relay_reserve(guc->log.relay_chan, guc->log.vma->obj->base.size);
925
926 /* Switch to the next sub buffer */
927 relay_flush(guc->log.relay_chan);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530928}
929
930static void *guc_get_write_buffer(struct intel_guc *guc)
931{
Akash Goelf8240832016-10-12 21:54:34 +0530932 if (!guc->log.relay_chan)
933 return NULL;
934
935 /* Just get the base address of a new sub buffer and copy data into it
936 * ourselves. NULL will be returned in no-overwrite mode, if all sub
937 * buffers are full. Could have used the relay_write() to indirectly
938 * copy the data, but that would have been bit convoluted, as we need to
939 * write to only certain locations inside a sub buffer which cannot be
940 * done without using relay_reserve() along with relay_write(). So its
941 * better to use relay_reserve() alone.
942 */
943 return relay_reserve(guc->log.relay_chan, 0);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530944}
945
Akash Goel5aa1ee42016-10-12 21:54:36 +0530946static bool
947guc_check_log_buf_overflow(struct intel_guc *guc,
948 enum guc_log_buffer_type type, unsigned int full_cnt)
949{
950 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
951 bool overflow = false;
952
953 if (full_cnt != prev_full_cnt) {
954 overflow = true;
955
956 guc->log.prev_overflow_count[type] = full_cnt;
957 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
958
959 if (full_cnt < prev_full_cnt) {
960 /* buffer_full_cnt is a 4 bit counter */
961 guc->log.total_overflow_count[type] += 16;
962 }
963 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
964 }
965
966 return overflow;
967}
968
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530969static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
970{
971 switch (type) {
972 case GUC_ISR_LOG_BUFFER:
973 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
974 case GUC_DPC_LOG_BUFFER:
975 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
976 case GUC_CRASH_DUMP_LOG_BUFFER:
977 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
978 default:
979 MISSING_CASE(type);
980 }
981
982 return 0;
983}
984
985static void guc_read_update_log_buffer(struct intel_guc *guc)
986{
Akash Goel6941f3c2016-10-12 21:54:37 +0530987 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530988 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
989 struct guc_log_buffer_state log_buf_state_local;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530990 enum guc_log_buffer_type type;
991 void *src_data, *dst_data;
Akash Goel6941f3c2016-10-12 21:54:37 +0530992 bool new_overflow;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530993
994 if (WARN_ON(!guc->log.buf_addr))
995 return;
996
997 /* Get the pointer to shared GuC log buffer */
998 log_buf_state = src_data = guc->log.buf_addr;
999
1000 /* Get the pointer to local buffer to store the logs */
1001 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
1002
1003 /* Actual logs are present from the 2nd page */
1004 src_data += PAGE_SIZE;
1005 dst_data += PAGE_SIZE;
1006
1007 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
1008 /* Make a copy of the state structure, inside GuC log buffer
1009 * (which is uncached mapped), on the stack to avoid reading
1010 * from it multiple times.
1011 */
1012 memcpy(&log_buf_state_local, log_buf_state,
1013 sizeof(struct guc_log_buffer_state));
1014 buffer_size = guc_get_log_buffer_size(type);
Akash Goel6941f3c2016-10-12 21:54:37 +05301015 read_offset = log_buf_state_local.read_ptr;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301016 write_offset = log_buf_state_local.sampled_write_ptr;
Akash Goel5aa1ee42016-10-12 21:54:36 +05301017 full_cnt = log_buf_state_local.buffer_full_cnt;
1018
1019 /* Bookkeeping stuff */
1020 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
Akash Goel6941f3c2016-10-12 21:54:37 +05301021 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301022
1023 /* Update the state of shared log buffer */
1024 log_buf_state->read_ptr = write_offset;
1025 log_buf_state->flush_to_file = 0;
1026 log_buf_state++;
1027
1028 if (unlikely(!log_buf_snapshot_state))
1029 continue;
1030
1031 /* First copy the state structure in snapshot buffer */
1032 memcpy(log_buf_snapshot_state, &log_buf_state_local,
1033 sizeof(struct guc_log_buffer_state));
1034
1035 /* The write pointer could have been updated by GuC firmware,
1036 * after sending the flush interrupt to Host, for consistency
1037 * set write pointer value to same value of sampled_write_ptr
1038 * in the snapshot buffer.
1039 */
1040 log_buf_snapshot_state->write_ptr = write_offset;
1041 log_buf_snapshot_state++;
1042
1043 /* Now copy the actual logs. */
Akash Goel6941f3c2016-10-12 21:54:37 +05301044 if (unlikely(new_overflow)) {
1045 /* copy the whole buffer in case of overflow */
1046 read_offset = 0;
1047 write_offset = buffer_size;
1048 } else if (unlikely((read_offset > buffer_size) ||
1049 (write_offset > buffer_size))) {
1050 DRM_ERROR("invalid log buffer state\n");
1051 /* copy whole buffer as offsets are unreliable */
1052 read_offset = 0;
1053 write_offset = buffer_size;
1054 }
1055
1056 /* Just copy the newly written data */
1057 if (read_offset > write_offset) {
Akash Goel71706592016-10-12 21:54:42 +05301058 i915_memcpy_from_wc(dst_data, src_data, write_offset);
Akash Goel6941f3c2016-10-12 21:54:37 +05301059 bytes_to_copy = buffer_size - read_offset;
1060 } else {
1061 bytes_to_copy = write_offset - read_offset;
1062 }
Akash Goel71706592016-10-12 21:54:42 +05301063 i915_memcpy_from_wc(dst_data + read_offset,
1064 src_data + read_offset, bytes_to_copy);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301065
1066 src_data += buffer_size;
1067 dst_data += buffer_size;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301068 }
1069
1070 if (log_buf_snapshot_state)
1071 guc_move_to_next_buf(guc);
Akash Goelf8240832016-10-12 21:54:34 +05301072 else {
1073 /* Used rate limited to avoid deluge of messages, logs might be
1074 * getting consumed by User at a slow rate.
1075 */
1076 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
Akash Goel5aa1ee42016-10-12 21:54:36 +05301077 guc->log.capture_miss_count++;
Akash Goelf8240832016-10-12 21:54:34 +05301078 }
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301079}
1080
1081static void guc_capture_logs_work(struct work_struct *work)
1082{
1083 struct drm_i915_private *dev_priv =
1084 container_of(work, struct drm_i915_private, guc.log.flush_work);
1085
1086 i915_guc_capture_logs(dev_priv);
1087}
1088
1089static void guc_log_cleanup(struct intel_guc *guc)
1090{
1091 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1092
1093 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1094
1095 /* First disable the flush interrupt */
1096 gen9_disable_guc_interrupts(dev_priv);
1097
1098 if (guc->log.flush_wq)
1099 destroy_workqueue(guc->log.flush_wq);
1100
1101 guc->log.flush_wq = NULL;
1102
Akash Goelf8240832016-10-12 21:54:34 +05301103 if (guc->log.relay_chan)
1104 guc_log_remove_relay_file(guc);
1105
1106 guc->log.relay_chan = NULL;
1107
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301108 if (guc->log.buf_addr)
1109 i915_gem_object_unpin_map(guc->log.vma->obj);
1110
1111 guc->log.buf_addr = NULL;
1112}
1113
1114static int guc_log_create_extras(struct intel_guc *guc)
1115{
1116 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1117 void *vaddr;
1118 int ret;
1119
1120 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1121
1122 /* Nothing to do */
1123 if (i915.guc_log_level < 0)
1124 return 0;
1125
1126 if (!guc->log.buf_addr) {
Akash Goel71706592016-10-12 21:54:42 +05301127 /* Create a WC (Uncached for read) vmalloc mapping of log
1128 * buffer pages, so that we can directly get the data
1129 * (up-to-date) from memory.
1130 */
1131 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301132 if (IS_ERR(vaddr)) {
1133 ret = PTR_ERR(vaddr);
1134 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
1135 return ret;
1136 }
1137
1138 guc->log.buf_addr = vaddr;
1139 }
1140
Akash Goel1e6b8b02016-10-12 21:54:43 +05301141 if (!guc->log.relay_chan) {
1142 /* Create a relay channel, so that we have buffers for storing
1143 * the GuC firmware logs, the channel will be linked with a file
1144 * later on when debugfs is registered.
1145 */
1146 ret = guc_log_create_relay_channel(guc);
1147 if (ret)
1148 return ret;
1149 }
1150
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301151 if (!guc->log.flush_wq) {
1152 INIT_WORK(&guc->log.flush_work, guc_capture_logs_work);
1153
Akash Goel7ef54de2016-10-12 21:54:44 +05301154 /*
1155 * GuC log buffer flush work item has to do register access to
1156 * send the ack to GuC and this work item, if not synced before
1157 * suspend, can potentially get executed after the GFX device is
1158 * suspended.
1159 * By marking the WQ as freezable, we don't have to bother about
1160 * flushing of this work item from the suspend hooks, the pending
1161 * work item if any will be either executed before the suspend
1162 * or scheduled later on resume. This way the handling of work
1163 * item can be kept same between system suspend & rpm suspend.
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301164 */
Akash Goel7ef54de2016-10-12 21:54:44 +05301165 guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log",
1166 WQ_HIGHPRI | WQ_FREEZABLE);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301167 if (guc->log.flush_wq == NULL) {
1168 DRM_ERROR("Couldn't allocate the wq for GuC logging\n");
1169 return -ENOMEM;
1170 }
1171 }
1172
1173 return 0;
1174}
1175
Dave Gordon7a9347f2016-09-12 21:19:37 +01001176static void guc_log_create(struct intel_guc *guc)
Alex Dai4c7e77f2015-08-12 15:43:40 +01001177{
Chris Wilson8b797af2016-08-15 10:48:51 +01001178 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001179 unsigned long offset;
1180 uint32_t size, flags;
1181
Alex Dai4c7e77f2015-08-12 15:43:40 +01001182 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
1183 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
1184
1185 /* The first page is to save log buffer state. Allocate one
1186 * extra page for others in case for overlap */
1187 size = (1 + GUC_LOG_DPC_PAGES + 1 +
1188 GUC_LOG_ISR_PAGES + 1 +
1189 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
1190
Akash Goeld6b40b42016-10-12 21:54:29 +05301191 vma = guc->log.vma;
Chris Wilson8b797af2016-08-15 10:48:51 +01001192 if (!vma) {
Akash Goel71706592016-10-12 21:54:42 +05301193 /* We require SSE 4.1 for fast reads from the GuC log buffer and
1194 * it should be present on the chipsets supporting GuC based
1195 * submisssions.
1196 */
Chris Wilsonc4d3ae62017-01-06 15:20:09 +00001197 if (WARN_ON(!i915_has_memcpy_from_wc())) {
Akash Goel71706592016-10-12 21:54:42 +05301198 /* logging will not be enabled */
1199 i915.guc_log_level = -1;
1200 return;
1201 }
1202
Chris Wilson8b797af2016-08-15 10:48:51 +01001203 vma = guc_allocate_vma(guc, size);
1204 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +01001205 /* logging will be off */
1206 i915.guc_log_level = -1;
1207 return;
1208 }
1209
Akash Goeld6b40b42016-10-12 21:54:29 +05301210 guc->log.vma = vma;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301211
1212 if (guc_log_create_extras(guc)) {
1213 guc_log_cleanup(guc);
1214 i915_vma_unpin_and_release(&guc->log.vma);
1215 i915.guc_log_level = -1;
1216 return;
1217 }
Alex Dai4c7e77f2015-08-12 15:43:40 +01001218 }
1219
1220 /* each allocated unit is a page */
1221 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
1222 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
1223 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
1224 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
1225
Chris Wilson4741da92016-12-24 19:31:46 +00001226 offset = guc_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
Akash Goeld6b40b42016-10-12 21:54:29 +05301227 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001228}
1229
Akash Goelf8240832016-10-12 21:54:34 +05301230static int guc_log_late_setup(struct intel_guc *guc)
1231{
1232 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1233 int ret;
1234
1235 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1236
1237 if (i915.guc_log_level < 0)
1238 return -EINVAL;
1239
1240 /* If log_level was set as -1 at boot time, then setup needed to
1241 * handle log buffer flush interrupts would not have been done yet,
1242 * so do that now.
1243 */
1244 ret = guc_log_create_extras(guc);
1245 if (ret)
1246 goto err;
1247
1248 ret = guc_log_create_relay_file(guc);
1249 if (ret)
1250 goto err;
1251
1252 return 0;
1253err:
1254 guc_log_cleanup(guc);
1255 /* logging will remain off */
1256 i915.guc_log_level = -1;
1257 return ret;
1258}
1259
Dave Gordon7a9347f2016-09-12 21:19:37 +01001260static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -08001261{
1262 struct guc_policy *policy;
1263 u32 p, i;
1264
1265 policies->dpc_promote_time = 500000;
1266 policies->max_num_work_items = POLICY_MAX_NUM_WI;
1267
1268 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -08001269 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -08001270 policy = &policies->policy[p][i];
1271
1272 policy->execution_quantum = 1000000;
1273 policy->preemption_time = 500000;
1274 policy->fault_time = 250000;
1275 policy->policy_flags = 0;
1276 }
1277 }
1278
1279 policies->is_valid = 1;
1280}
1281
Dave Gordon7a9347f2016-09-12 21:19:37 +01001282static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -08001283{
1284 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +01001285 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -08001286 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -08001287 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -08001288 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001289 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301290 enum intel_engine_id id;
Alex Dai68371a92015-12-18 12:00:09 -08001291 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001292 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -08001293
1294 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -08001295 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
1296 sizeof(struct guc_mmio_reg_state) +
1297 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -08001298
Chris Wilson8b797af2016-08-15 10:48:51 +01001299 vma = guc->ads_vma;
1300 if (!vma) {
1301 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
1302 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -08001303 return;
1304
Chris Wilson8b797af2016-08-15 10:48:51 +01001305 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -08001306 }
1307
Chris Wilson8b797af2016-08-15 10:48:51 +01001308 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -08001309 ads = kmap(page);
1310
1311 /*
1312 * The GuC requires a "Golden Context" when it reinitialises
1313 * engines after a reset. Here we use the Render ring default
1314 * context, which must already exist and be pinned in the GGTT,
1315 * so its address won't change after we've told the GuC where
1316 * to find it.
1317 */
Akash Goel3b3f1652016-10-13 22:44:48 +05301318 engine = dev_priv->engine[RCS];
Chris Wilson57e88532016-08-15 10:48:57 +01001319 ads->golden_context_lrca = engine->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -08001320
Akash Goel3b3f1652016-10-13 22:44:48 +05301321 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001322 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -08001323
Alex Dai463704d2015-12-18 12:00:10 -08001324 /* GuC scheduling policies */
1325 policies = (void *)ads + sizeof(struct guc_ads);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001326 guc_policies_init(policies);
Alex Dai463704d2015-12-18 12:00:10 -08001327
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001328 ads->scheduler_policies =
Chris Wilson4741da92016-12-24 19:31:46 +00001329 guc_ggtt_offset(vma) + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -08001330
Alex Dai5c148e02015-12-18 12:00:11 -08001331 /* MMIO reg state */
1332 reg_state = (void *)policies + sizeof(struct guc_policies);
1333
Akash Goel3b3f1652016-10-13 22:44:48 +05301334 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001335 reg_state->mmio_white_list[engine->guc_id].mmio_start =
1336 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -08001337
1338 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001339 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -08001340 }
1341
1342 ads->reg_state_addr = ads->scheduler_policies +
1343 sizeof(struct guc_policies);
1344
1345 ads->reg_state_buffer = ads->reg_state_addr +
1346 sizeof(struct guc_mmio_reg_state);
1347
Alex Dai68371a92015-12-18 12:00:09 -08001348 kunmap(page);
1349}
1350
Alex Daibac427f2015-08-12 15:43:39 +01001351/*
1352 * Set up the memory resources to be shared with the GuC. At this point,
1353 * we require just one object that can be mapped through the GGTT.
1354 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001355int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001356{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001357 const size_t ctxsize = sizeof(struct guc_context_desc);
1358 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1359 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001360 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001361 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +01001362
Chris Wilson4d357af2016-11-29 12:10:23 +00001363 if (!HAS_GUC_SCHED(dev_priv))
1364 return 0;
1365
Dave Gordon29fb72c2016-06-07 09:14:50 +01001366 /* Wipe bitmap & delete client in case of reinitialisation */
1367 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001368 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001369
Alex Daibac427f2015-08-12 15:43:39 +01001370 if (!i915.enable_guc_submission)
1371 return 0; /* not enabled */
1372
Chris Wilson8b797af2016-08-15 10:48:51 +01001373 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001374 return 0; /* already allocated */
1375
Dave Gordon7a9347f2016-09-12 21:19:37 +01001376 vma = guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001377 if (IS_ERR(vma))
1378 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001379
Chris Wilson8b797af2016-08-15 10:48:51 +01001380 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001381 ida_init(&guc->ctx_ids);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001382 guc_log_create(guc);
1383 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001384
Chris Wilson4d357af2016-11-29 12:10:23 +00001385 guc->execbuf_client = guc_client_alloc(dev_priv,
1386 INTEL_INFO(dev_priv)->ring_mask,
1387 GUC_CTX_PRIORITY_KMD_NORMAL,
1388 dev_priv->kernel_context);
1389 if (!guc->execbuf_client) {
1390 DRM_ERROR("Failed to create GuC client for execbuf!\n");
1391 goto err;
1392 }
1393
Alex Daibac427f2015-08-12 15:43:39 +01001394 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +00001395
1396err:
1397 i915_guc_submission_fini(dev_priv);
1398 return -ENOMEM;
1399}
1400
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001401static void guc_reset_wq(struct i915_guc_client *client)
Chris Wilson4d357af2016-11-29 12:10:23 +00001402{
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001403 struct guc_process_desc *desc = client->vaddr +
1404 client->proc_desc_offset;
Chris Wilson4d357af2016-11-29 12:10:23 +00001405
1406 desc->head = 0;
1407 desc->tail = 0;
1408
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001409 client->wq_tail = 0;
Alex Daibac427f2015-08-12 15:43:39 +01001410}
1411
Dave Gordonbeffa512016-06-10 18:29:26 +01001412int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001413{
Dave Gordon44a28b12015-08-12 15:43:41 +01001414 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001415 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001416 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301417 enum intel_engine_id id;
Dave Gordon44a28b12015-08-12 15:43:41 +01001418
Chris Wilson4d357af2016-11-29 12:10:23 +00001419 if (!client)
1420 return -ENODEV;
Dave Gordon44a28b12015-08-12 15:43:41 +01001421
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001422 intel_guc_sample_forcewake(guc);
Chris Wilson4d357af2016-11-29 12:10:23 +00001423
1424 guc_reset_wq(client);
Dave Gordon4d757872016-06-13 17:57:34 +01001425 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001426
Chris Wilsonddd66c52016-08-02 22:50:31 +01001427 /* Take over from manual control of ELSP (execlists) */
Akash Goel3b3f1652016-10-13 22:44:48 +05301428 for_each_engine(engine, dev_priv, id) {
Chris Wilson4d357af2016-11-29 12:10:23 +00001429 struct drm_i915_gem_request *rq;
1430
Chris Wilsonddd66c52016-08-02 22:50:31 +01001431 engine->submit_request = i915_guc_submit;
Chris Wilson20311bd2016-11-14 20:41:03 +00001432 engine->schedule = NULL;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001433
Chris Wilson821ed7d2016-09-09 14:11:53 +01001434 /* Replay the current set of previously submitted requests */
Chris Wilson4d357af2016-11-29 12:10:23 +00001435 list_for_each_entry(rq, &engine->timeline->requests, link) {
Chris Wilsondadd4812016-09-09 14:11:57 +01001436 client->wq_rsvd += sizeof(struct guc_wq_item);
Chris Wilson34ba5a82016-11-29 12:10:24 +00001437 __i915_guc_submit(rq);
Chris Wilsondadd4812016-09-09 14:11:57 +01001438 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001439 }
1440
Dave Gordon44a28b12015-08-12 15:43:41 +01001441 return 0;
1442}
1443
Dave Gordonbeffa512016-06-10 18:29:26 +01001444void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001445{
Dave Gordon44a28b12015-08-12 15:43:41 +01001446 struct intel_guc *guc = &dev_priv->guc;
1447
Chris Wilsonddd66c52016-08-02 22:50:31 +01001448 if (!guc->execbuf_client)
1449 return;
1450
Chris Wilsonddd66c52016-08-02 22:50:31 +01001451 /* Revert back to manual ELSP submission */
1452 intel_execlists_enable_submission(dev_priv);
Dave Gordon44a28b12015-08-12 15:43:41 +01001453}
1454
Dave Gordonbeffa512016-06-10 18:29:26 +01001455void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001456{
Alex Daibac427f2015-08-12 15:43:39 +01001457 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001458 struct i915_guc_client *client;
1459
1460 client = fetch_and_zero(&guc->execbuf_client);
1461 if (!client)
1462 return;
1463
1464 guc_client_free(dev_priv, client);
Alex Daibac427f2015-08-12 15:43:39 +01001465
Chris Wilson19880c42016-08-15 10:49:05 +01001466 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301467 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001468
Chris Wilson8b797af2016-08-15 10:48:51 +01001469 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001470 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001471 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001472}
Alex Daia1c41992015-09-30 09:46:37 -07001473
1474/**
1475 * intel_guc_suspend() - notify GuC entering suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001476 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001477 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001478int intel_guc_suspend(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001479{
Alex Daia1c41992015-09-30 09:46:37 -07001480 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001481 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001482 u32 data[3];
1483
Dave Gordonfce91f22016-05-20 11:42:42 +01001484 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001485 return 0;
1486
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301487 gen9_disable_guc_interrupts(dev_priv);
1488
Dave Gordoned54c1a2016-01-19 19:02:54 +00001489 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001490
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001491 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001492 /* any value greater than GUC_POWER_D0 */
1493 data[1] = GUC_POWER_D1;
1494 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001495 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001496
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001497 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001498}
1499
1500
1501/**
1502 * intel_guc_resume() - notify GuC resuming from suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001503 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001504 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001505int intel_guc_resume(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001506{
Alex Daia1c41992015-09-30 09:46:37 -07001507 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001508 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001509 u32 data[3];
1510
Dave Gordonfce91f22016-05-20 11:42:42 +01001511 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001512 return 0;
1513
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301514 if (i915.guc_log_level >= 0)
1515 gen9_enable_guc_interrupts(dev_priv);
1516
Dave Gordoned54c1a2016-01-19 19:02:54 +00001517 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001518
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001519 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001520 data[1] = GUC_POWER_D0;
1521 /* first page is shared data with GuC */
Chris Wilson4741da92016-12-24 19:31:46 +00001522 data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001523
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001524 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001525}
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301526
1527void i915_guc_capture_logs(struct drm_i915_private *dev_priv)
1528{
1529 guc_read_update_log_buffer(&dev_priv->guc);
1530
1531 /* Generally device is expected to be active only at this
1532 * time, so get/put should be really quick.
1533 */
1534 intel_runtime_pm_get(dev_priv);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001535 intel_guc_log_flush_complete(&dev_priv->guc);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301536 intel_runtime_pm_put(dev_priv);
1537}
Akash Goelf8240832016-10-12 21:54:34 +05301538
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301539void i915_guc_flush_logs(struct drm_i915_private *dev_priv)
1540{
1541 if (!i915.enable_guc_submission || (i915.guc_log_level < 0))
1542 return;
1543
1544 /* First disable the interrupts, will be renabled afterwards */
1545 gen9_disable_guc_interrupts(dev_priv);
1546
1547 /* Before initiating the forceful flush, wait for any pending/ongoing
1548 * flush to complete otherwise forceful flush may not actually happen.
1549 */
1550 flush_work(&dev_priv->guc.log.flush_work);
1551
1552 /* Ask GuC to update the log buffer state */
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001553 intel_guc_log_flush(&dev_priv->guc);
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301554
1555 /* GuC would have updated log buffer by now, so capture it */
1556 i915_guc_capture_logs(dev_priv);
1557}
1558
Akash Goelf8240832016-10-12 21:54:34 +05301559void i915_guc_unregister(struct drm_i915_private *dev_priv)
1560{
1561 if (!i915.enable_guc_submission)
1562 return;
1563
1564 mutex_lock(&dev_priv->drm.struct_mutex);
1565 guc_log_cleanup(&dev_priv->guc);
1566 mutex_unlock(&dev_priv->drm.struct_mutex);
1567}
1568
1569void i915_guc_register(struct drm_i915_private *dev_priv)
1570{
1571 if (!i915.enable_guc_submission)
1572 return;
1573
1574 mutex_lock(&dev_priv->drm.struct_mutex);
1575 guc_log_late_setup(&dev_priv->guc);
1576 mutex_unlock(&dev_priv->drm.struct_mutex);
1577}
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301578
1579int i915_guc_log_control(struct drm_i915_private *dev_priv, u64 control_val)
1580{
1581 union guc_log_control log_param;
1582 int ret;
1583
1584 log_param.value = control_val;
1585
1586 if (log_param.verbosity < GUC_LOG_VERBOSITY_MIN ||
1587 log_param.verbosity > GUC_LOG_VERBOSITY_MAX)
1588 return -EINVAL;
1589
1590 /* This combination doesn't make sense & won't have any effect */
1591 if (!log_param.logging_enabled && (i915.guc_log_level < 0))
1592 return 0;
1593
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001594 ret = intel_guc_log_control(&dev_priv->guc, log_param.value);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301595 if (ret < 0) {
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001596 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301597 return ret;
1598 }
1599
1600 i915.guc_log_level = log_param.verbosity;
1601
1602 /* If log_level was set as -1 at boot time, then the relay channel file
1603 * wouldn't have been created by now and interrupts also would not have
1604 * been enabled.
1605 */
1606 if (!dev_priv->guc.log.relay_chan) {
1607 ret = guc_log_late_setup(&dev_priv->guc);
1608 if (!ret)
1609 gen9_enable_guc_interrupts(dev_priv);
1610 } else if (!log_param.logging_enabled) {
1611 /* Once logging is disabled, GuC won't generate logs & send an
1612 * interrupt. But there could be some data in the log buffer
1613 * which is yet to be captured. So request GuC to update the log
1614 * buffer state and then collect the left over logs.
1615 */
1616 i915_guc_flush_logs(dev_priv);
1617
1618 /* As logging is disabled, update log level to reflect that */
1619 i915.guc_log_level = -1;
1620 } else {
1621 /* In case interrupts were disabled, enable them now */
1622 gen9_enable_guc_interrupts(dev_priv);
1623 }
1624
1625 return ret;
1626}