blob: 3e20fe2be8111d7f24264eb58d2c532b87783430 [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);
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
582 /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
583 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
584
Chris Wilson8b797af2016-08-15 10:48:51 +0100585 return vma;
586
587err:
588 i915_gem_object_put(obj);
589 return vma;
Alex Daibac427f2015-08-12 15:43:39 +0100590}
591
Dave Gordon0daf5562016-06-10 18:29:25 +0100592static void
593guc_client_free(struct drm_i915_private *dev_priv,
594 struct i915_guc_client *client)
Dave Gordon44a28b12015-08-12 15:43:41 +0100595{
Dave Gordon44a28b12015-08-12 15:43:41 +0100596 struct intel_guc *guc = &dev_priv->guc;
597
598 if (!client)
599 return;
600
Dave Gordon44a28b12015-08-12 15:43:41 +0100601 /*
602 * XXX: wait for any outstanding submissions before freeing memory.
603 * Be sure to drop any locks
604 */
605
Chris Wilson72aa0d82016-11-02 17:50:47 +0000606 if (client->vaddr) {
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100607 /*
Dave Gordona6674292016-06-13 17:57:32 +0100608 * If we got as far as setting up a doorbell, make sure we
609 * shut it down before unmapping & deallocating the memory.
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100610 */
Dave Gordona6674292016-06-13 17:57:32 +0100611 guc_disable_doorbell(guc, client);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100612
Chris Wilson72aa0d82016-11-02 17:50:47 +0000613 i915_gem_object_unpin_map(client->vma->obj);
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100614 }
615
Chris Wilson19880c42016-08-15 10:49:05 +0100616 i915_vma_unpin_and_release(&client->vma);
Dave Gordon44a28b12015-08-12 15:43:41 +0100617
618 if (client->ctx_index != GUC_INVALID_CTX_ID) {
Dave Gordon7a9347f2016-09-12 21:19:37 +0100619 guc_ctx_desc_fini(guc, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100620 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
621 }
622
623 kfree(client);
624}
625
Dave Gordon84b7f882016-08-09 15:19:20 +0100626/* Check that a doorbell register is in the expected state */
627static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
628{
629 struct drm_i915_private *dev_priv = guc_to_i915(guc);
630 i915_reg_t drbreg = GEN8_DRBREGL(db_id);
631 uint32_t value = I915_READ(drbreg);
632 bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
633 bool expected = test_bit(db_id, guc->doorbell_bitmap);
634
635 if (enabled == expected)
636 return true;
637
638 DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
639 db_id, drbreg.reg, value,
640 expected ? "active" : "inactive");
641
642 return false;
643}
644
Dave Gordon4d757872016-06-13 17:57:34 +0100645/*
Dave Gordon8888cd02016-08-09 15:19:19 +0100646 * Borrow the first client to set up & tear down each unused doorbell
Dave Gordon4d757872016-06-13 17:57:34 +0100647 * in turn, to ensure that all doorbell h/w is (re)initialised.
648 */
649static void guc_init_doorbell_hw(struct intel_guc *guc)
650{
Dave Gordon4d757872016-06-13 17:57:34 +0100651 struct i915_guc_client *client = guc->execbuf_client;
Dave Gordon84b7f882016-08-09 15:19:20 +0100652 uint16_t db_id;
653 int i, err;
Dave Gordon4d757872016-06-13 17:57:34 +0100654
Chris Wilson4d357af2016-11-29 12:10:23 +0000655 guc_disable_doorbell(guc, client);
Dave Gordon4d757872016-06-13 17:57:34 +0100656
657 for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
Dave Gordon84b7f882016-08-09 15:19:20 +0100658 /* Skip if doorbell is OK */
659 if (guc_doorbell_check(guc, i))
Dave Gordon8888cd02016-08-09 15:19:19 +0100660 continue;
661
Dave Gordon4d757872016-06-13 17:57:34 +0100662 err = guc_update_doorbell_id(guc, client, i);
Dave Gordon84b7f882016-08-09 15:19:20 +0100663 if (err)
664 DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
665 i, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100666 }
667
Chris Wilson4d357af2016-11-29 12:10:23 +0000668 db_id = select_doorbell_register(guc, client->priority);
669 WARN_ON(db_id == GUC_INVALID_DOORBELL_ID);
670
Dave Gordon4d757872016-06-13 17:57:34 +0100671 err = guc_update_doorbell_id(guc, client, db_id);
672 if (err)
Dave Gordon535b2f52016-08-18 18:17:23 +0100673 DRM_WARN("Failed to restore doorbell to %d, err %d\n",
674 db_id, err);
Dave Gordon4d757872016-06-13 17:57:34 +0100675
Dave Gordon84b7f882016-08-09 15:19:20 +0100676 /* Read back & verify all doorbell registers */
677 for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
678 (void)guc_doorbell_check(guc, i);
Dave Gordon4d757872016-06-13 17:57:34 +0100679}
680
Dave Gordon44a28b12015-08-12 15:43:41 +0100681/**
682 * guc_client_alloc() - Allocate an i915_guc_client
Dave Gordon0daf5562016-06-10 18:29:25 +0100683 * @dev_priv: driver private data structure
Chris Wilsonceae5312016-08-17 13:42:42 +0100684 * @engines: The set of engines to enable for this client
Dave Gordon44a28b12015-08-12 15:43:41 +0100685 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
686 * The kernel client to replace ExecList submission is created with
687 * NORMAL priority. Priority of a client for scheduler can be HIGH,
688 * while a preemption context can use CRITICAL.
Alex Daifeda33e2015-10-19 16:10:54 -0700689 * @ctx: the context that owns the client (we use the default render
690 * context)
Dave Gordon44a28b12015-08-12 15:43:41 +0100691 *
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100692 * Return: An i915_guc_client object if success, else NULL.
Dave Gordon44a28b12015-08-12 15:43:41 +0100693 */
Dave Gordon0daf5562016-06-10 18:29:25 +0100694static struct i915_guc_client *
695guc_client_alloc(struct drm_i915_private *dev_priv,
Dave Gordone02757d2016-08-09 15:19:21 +0100696 uint32_t engines,
Dave Gordon0daf5562016-06-10 18:29:25 +0100697 uint32_t priority,
698 struct i915_gem_context *ctx)
Dave Gordon44a28b12015-08-12 15:43:41 +0100699{
700 struct i915_guc_client *client;
Dave Gordon44a28b12015-08-12 15:43:41 +0100701 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +0100702 struct i915_vma *vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000703 void *vaddr;
Dave Gordona6674292016-06-13 17:57:32 +0100704 uint16_t db_id;
Dave Gordon44a28b12015-08-12 15:43:41 +0100705
706 client = kzalloc(sizeof(*client), GFP_KERNEL);
707 if (!client)
708 return NULL;
709
Alex Daid1675192015-08-12 15:43:43 +0100710 client->owner = ctx;
Dave Gordon44a28b12015-08-12 15:43:41 +0100711 client->guc = guc;
Dave Gordone02757d2016-08-09 15:19:21 +0100712 client->engines = engines;
713 client->priority = priority;
714 client->doorbell_id = GUC_INVALID_DOORBELL_ID;
Dave Gordon44a28b12015-08-12 15:43:41 +0100715
716 client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
717 GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
718 if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
719 client->ctx_index = GUC_INVALID_CTX_ID;
720 goto err;
721 }
722
723 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100724 vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
725 if (IS_ERR(vma))
Dave Gordon44a28b12015-08-12 15:43:41 +0100726 goto err;
727
Dave Gordon0d92a6a2016-04-19 16:08:34 +0100728 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
Chris Wilson8b797af2016-08-15 10:48:51 +0100729 client->vma = vma;
Chris Wilson72aa0d82016-11-02 17:50:47 +0000730
731 vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
732 if (IS_ERR(vaddr))
733 goto err;
734
735 client->vaddr = vaddr;
Chris Wilsondadd4812016-09-09 14:11:57 +0100736
737 spin_lock_init(&client->wq_lock);
Dave Gordon44a28b12015-08-12 15:43:41 +0100738 client->wq_offset = GUC_DB_SIZE;
739 client->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100740
Dave Gordonf10d69a2016-06-13 17:57:33 +0100741 db_id = select_doorbell_register(guc, client->priority);
742 if (db_id == GUC_INVALID_DOORBELL_ID)
743 /* XXX: evict a doorbell instead? */
744 goto err;
745
Dave Gordon44a28b12015-08-12 15:43:41 +0100746 client->doorbell_offset = select_doorbell_cacheline(guc);
747
748 /*
749 * Since the doorbell only requires a single cacheline, we can save
750 * space by putting the application process descriptor in the same
751 * page. Use the half of the page that doesn't include the doorbell.
752 */
753 if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
754 client->proc_desc_offset = 0;
755 else
756 client->proc_desc_offset = (GUC_DB_SIZE / 2);
757
Dave Gordon7a9347f2016-09-12 21:19:37 +0100758 guc_proc_desc_init(guc, client);
759 guc_ctx_desc_init(guc, client);
Chris Wilson4d357af2016-11-29 12:10:23 +0000760
761 /* For runtime client allocation we need to enable the doorbell. Not
762 * required yet for the static execbuf_client as this special kernel
763 * client is enabled from i915_guc_submission_enable().
764 *
765 * guc_update_doorbell_id(guc, client, db_id);
766 */
Dave Gordon44a28b12015-08-12 15:43:41 +0100767
Dave Gordone02757d2016-08-09 15:19:21 +0100768 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
769 priority, client, client->engines, client->ctx_index);
Dave Gordona6674292016-06-13 17:57:32 +0100770 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
771 client->doorbell_id, client->doorbell_offset);
Dave Gordon44a28b12015-08-12 15:43:41 +0100772
773 return client;
774
775err:
Dave Gordon0daf5562016-06-10 18:29:25 +0100776 guc_client_free(dev_priv, client);
Dave Gordon44a28b12015-08-12 15:43:41 +0100777 return NULL;
778}
779
Akash Goelf8240832016-10-12 21:54:34 +0530780/*
781 * Sub buffer switch callback. Called whenever relay has to switch to a new
782 * sub buffer, relay stays on the same sub buffer if 0 is returned.
783 */
784static int subbuf_start_callback(struct rchan_buf *buf,
785 void *subbuf,
786 void *prev_subbuf,
787 size_t prev_padding)
788{
789 /* Use no-overwrite mode by default, where relay will stop accepting
790 * new data if there are no empty sub buffers left.
791 * There is no strict synchronization enforced by relay between Consumer
792 * and Producer. In overwrite mode, there is a possibility of getting
793 * inconsistent/garbled data, the producer could be writing on to the
794 * same sub buffer from which Consumer is reading. This can't be avoided
795 * unless Consumer is fast enough and can always run in tandem with
796 * Producer.
797 */
798 if (relay_buf_full(buf))
799 return 0;
800
801 return 1;
802}
803
804/*
805 * file_create() callback. Creates relay file in debugfs.
806 */
807static struct dentry *create_buf_file_callback(const char *filename,
808 struct dentry *parent,
809 umode_t mode,
810 struct rchan_buf *buf,
811 int *is_global)
812{
813 struct dentry *buf_file;
814
Akash Goelf8240832016-10-12 21:54:34 +0530815 /* This to enable the use of a single buffer for the relay channel and
816 * correspondingly have a single file exposed to User, through which
817 * it can collect the logs in order without any post-processing.
Akash Goel1e6b8b02016-10-12 21:54:43 +0530818 * Need to set 'is_global' even if parent is NULL for early logging.
Akash Goelf8240832016-10-12 21:54:34 +0530819 */
820 *is_global = 1;
821
Akash Goel1e6b8b02016-10-12 21:54:43 +0530822 if (!parent)
823 return NULL;
824
Akash Goelf8240832016-10-12 21:54:34 +0530825 /* Not using the channel filename passed as an argument, since for each
826 * channel relay appends the corresponding CPU number to the filename
827 * passed in relay_open(). This should be fine as relay just needs a
828 * dentry of the file associated with the channel buffer and that file's
829 * name need not be same as the filename passed as an argument.
830 */
831 buf_file = debugfs_create_file("guc_log", mode,
832 parent, buf, &relay_file_operations);
833 return buf_file;
834}
835
836/*
837 * file_remove() default callback. Removes relay file in debugfs.
838 */
839static int remove_buf_file_callback(struct dentry *dentry)
840{
841 debugfs_remove(dentry);
842 return 0;
843}
844
845/* relay channel callbacks */
846static struct rchan_callbacks relay_callbacks = {
847 .subbuf_start = subbuf_start_callback,
848 .create_buf_file = create_buf_file_callback,
849 .remove_buf_file = remove_buf_file_callback,
850};
851
852static void guc_log_remove_relay_file(struct intel_guc *guc)
853{
854 relay_close(guc->log.relay_chan);
855}
856
Akash Goel1e6b8b02016-10-12 21:54:43 +0530857static int guc_log_create_relay_channel(struct intel_guc *guc)
Akash Goelf8240832016-10-12 21:54:34 +0530858{
859 struct drm_i915_private *dev_priv = guc_to_i915(guc);
860 struct rchan *guc_log_relay_chan;
Akash Goelf8240832016-10-12 21:54:34 +0530861 size_t n_subbufs, subbuf_size;
862
Akash Goel1e6b8b02016-10-12 21:54:43 +0530863 /* Keep the size of sub buffers same as shared log buffer */
864 subbuf_size = guc->log.vma->obj->base.size;
865
866 /* Store up to 8 snapshots, which is large enough to buffer sufficient
867 * boot time logs and provides enough leeway to User, in terms of
868 * latency, for consuming the logs from relay. Also doesn't take
869 * up too much memory.
870 */
871 n_subbufs = 8;
872
873 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
874 n_subbufs, &relay_callbacks, dev_priv);
875 if (!guc_log_relay_chan) {
876 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
877 return -ENOMEM;
878 }
879
880 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
881 guc->log.relay_chan = guc_log_relay_chan;
882 return 0;
883}
884
885static int guc_log_create_relay_file(struct intel_guc *guc)
886{
887 struct drm_i915_private *dev_priv = guc_to_i915(guc);
888 struct dentry *log_dir;
889 int ret;
890
Akash Goelf8240832016-10-12 21:54:34 +0530891 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
892 log_dir = dev_priv->drm.primary->debugfs_root;
893
894 /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
895 * not mounted and so can't create the relay file.
896 * The relay API seems to fit well with debugfs only, for availing relay
897 * there are 3 requirements which can be met for debugfs file only in a
898 * straightforward/clean manner :-
899 * i) Need the associated dentry pointer of the file, while opening the
900 * relay channel.
901 * ii) Should be able to use 'relay_file_operations' fops for the file.
902 * iii) Set the 'i_private' field of file's inode to the pointer of
903 * relay channel buffer.
904 */
905 if (!log_dir) {
906 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
907 return -ENODEV;
908 }
909
Akash Goel1e6b8b02016-10-12 21:54:43 +0530910 ret = relay_late_setup_files(guc->log.relay_chan, "guc_log", log_dir);
911 if (ret) {
912 DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
913 return ret;
Akash Goelf8240832016-10-12 21:54:34 +0530914 }
915
Akash Goelf8240832016-10-12 21:54:34 +0530916 return 0;
917}
918
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530919static void guc_move_to_next_buf(struct intel_guc *guc)
920{
Akash Goelf8240832016-10-12 21:54:34 +0530921 /* Make sure the updates made in the sub buffer are visible when
922 * Consumer sees the following update to offset inside the sub buffer.
923 */
924 smp_wmb();
925
926 /* All data has been written, so now move the offset of sub buffer. */
927 relay_reserve(guc->log.relay_chan, guc->log.vma->obj->base.size);
928
929 /* Switch to the next sub buffer */
930 relay_flush(guc->log.relay_chan);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530931}
932
933static void *guc_get_write_buffer(struct intel_guc *guc)
934{
Akash Goelf8240832016-10-12 21:54:34 +0530935 if (!guc->log.relay_chan)
936 return NULL;
937
938 /* Just get the base address of a new sub buffer and copy data into it
939 * ourselves. NULL will be returned in no-overwrite mode, if all sub
940 * buffers are full. Could have used the relay_write() to indirectly
941 * copy the data, but that would have been bit convoluted, as we need to
942 * write to only certain locations inside a sub buffer which cannot be
943 * done without using relay_reserve() along with relay_write(). So its
944 * better to use relay_reserve() alone.
945 */
946 return relay_reserve(guc->log.relay_chan, 0);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530947}
948
Akash Goel5aa1ee42016-10-12 21:54:36 +0530949static bool
950guc_check_log_buf_overflow(struct intel_guc *guc,
951 enum guc_log_buffer_type type, unsigned int full_cnt)
952{
953 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
954 bool overflow = false;
955
956 if (full_cnt != prev_full_cnt) {
957 overflow = true;
958
959 guc->log.prev_overflow_count[type] = full_cnt;
960 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
961
962 if (full_cnt < prev_full_cnt) {
963 /* buffer_full_cnt is a 4 bit counter */
964 guc->log.total_overflow_count[type] += 16;
965 }
966 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
967 }
968
969 return overflow;
970}
971
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530972static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
973{
974 switch (type) {
975 case GUC_ISR_LOG_BUFFER:
976 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
977 case GUC_DPC_LOG_BUFFER:
978 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
979 case GUC_CRASH_DUMP_LOG_BUFFER:
980 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
981 default:
982 MISSING_CASE(type);
983 }
984
985 return 0;
986}
987
988static void guc_read_update_log_buffer(struct intel_guc *guc)
989{
Akash Goel6941f3c2016-10-12 21:54:37 +0530990 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530991 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
992 struct guc_log_buffer_state log_buf_state_local;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530993 enum guc_log_buffer_type type;
994 void *src_data, *dst_data;
Akash Goel6941f3c2016-10-12 21:54:37 +0530995 bool new_overflow;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +0530996
997 if (WARN_ON(!guc->log.buf_addr))
998 return;
999
1000 /* Get the pointer to shared GuC log buffer */
1001 log_buf_state = src_data = guc->log.buf_addr;
1002
1003 /* Get the pointer to local buffer to store the logs */
1004 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
1005
1006 /* Actual logs are present from the 2nd page */
1007 src_data += PAGE_SIZE;
1008 dst_data += PAGE_SIZE;
1009
1010 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
1011 /* Make a copy of the state structure, inside GuC log buffer
1012 * (which is uncached mapped), on the stack to avoid reading
1013 * from it multiple times.
1014 */
1015 memcpy(&log_buf_state_local, log_buf_state,
1016 sizeof(struct guc_log_buffer_state));
1017 buffer_size = guc_get_log_buffer_size(type);
Akash Goel6941f3c2016-10-12 21:54:37 +05301018 read_offset = log_buf_state_local.read_ptr;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301019 write_offset = log_buf_state_local.sampled_write_ptr;
Akash Goel5aa1ee42016-10-12 21:54:36 +05301020 full_cnt = log_buf_state_local.buffer_full_cnt;
1021
1022 /* Bookkeeping stuff */
1023 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
Akash Goel6941f3c2016-10-12 21:54:37 +05301024 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301025
1026 /* Update the state of shared log buffer */
1027 log_buf_state->read_ptr = write_offset;
1028 log_buf_state->flush_to_file = 0;
1029 log_buf_state++;
1030
1031 if (unlikely(!log_buf_snapshot_state))
1032 continue;
1033
1034 /* First copy the state structure in snapshot buffer */
1035 memcpy(log_buf_snapshot_state, &log_buf_state_local,
1036 sizeof(struct guc_log_buffer_state));
1037
1038 /* The write pointer could have been updated by GuC firmware,
1039 * after sending the flush interrupt to Host, for consistency
1040 * set write pointer value to same value of sampled_write_ptr
1041 * in the snapshot buffer.
1042 */
1043 log_buf_snapshot_state->write_ptr = write_offset;
1044 log_buf_snapshot_state++;
1045
1046 /* Now copy the actual logs. */
Akash Goel6941f3c2016-10-12 21:54:37 +05301047 if (unlikely(new_overflow)) {
1048 /* copy the whole buffer in case of overflow */
1049 read_offset = 0;
1050 write_offset = buffer_size;
1051 } else if (unlikely((read_offset > buffer_size) ||
1052 (write_offset > buffer_size))) {
1053 DRM_ERROR("invalid log buffer state\n");
1054 /* copy whole buffer as offsets are unreliable */
1055 read_offset = 0;
1056 write_offset = buffer_size;
1057 }
1058
1059 /* Just copy the newly written data */
1060 if (read_offset > write_offset) {
Akash Goel71706592016-10-12 21:54:42 +05301061 i915_memcpy_from_wc(dst_data, src_data, write_offset);
Akash Goel6941f3c2016-10-12 21:54:37 +05301062 bytes_to_copy = buffer_size - read_offset;
1063 } else {
1064 bytes_to_copy = write_offset - read_offset;
1065 }
Akash Goel71706592016-10-12 21:54:42 +05301066 i915_memcpy_from_wc(dst_data + read_offset,
1067 src_data + read_offset, bytes_to_copy);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301068
1069 src_data += buffer_size;
1070 dst_data += buffer_size;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301071 }
1072
1073 if (log_buf_snapshot_state)
1074 guc_move_to_next_buf(guc);
Akash Goelf8240832016-10-12 21:54:34 +05301075 else {
1076 /* Used rate limited to avoid deluge of messages, logs might be
1077 * getting consumed by User at a slow rate.
1078 */
1079 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
Akash Goel5aa1ee42016-10-12 21:54:36 +05301080 guc->log.capture_miss_count++;
Akash Goelf8240832016-10-12 21:54:34 +05301081 }
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301082}
1083
1084static void guc_capture_logs_work(struct work_struct *work)
1085{
1086 struct drm_i915_private *dev_priv =
1087 container_of(work, struct drm_i915_private, guc.log.flush_work);
1088
1089 i915_guc_capture_logs(dev_priv);
1090}
1091
1092static void guc_log_cleanup(struct intel_guc *guc)
1093{
1094 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1095
1096 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1097
1098 /* First disable the flush interrupt */
1099 gen9_disable_guc_interrupts(dev_priv);
1100
1101 if (guc->log.flush_wq)
1102 destroy_workqueue(guc->log.flush_wq);
1103
1104 guc->log.flush_wq = NULL;
1105
Akash Goelf8240832016-10-12 21:54:34 +05301106 if (guc->log.relay_chan)
1107 guc_log_remove_relay_file(guc);
1108
1109 guc->log.relay_chan = NULL;
1110
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301111 if (guc->log.buf_addr)
1112 i915_gem_object_unpin_map(guc->log.vma->obj);
1113
1114 guc->log.buf_addr = NULL;
1115}
1116
1117static int guc_log_create_extras(struct intel_guc *guc)
1118{
1119 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1120 void *vaddr;
1121 int ret;
1122
1123 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1124
1125 /* Nothing to do */
1126 if (i915.guc_log_level < 0)
1127 return 0;
1128
1129 if (!guc->log.buf_addr) {
Akash Goel71706592016-10-12 21:54:42 +05301130 /* Create a WC (Uncached for read) vmalloc mapping of log
1131 * buffer pages, so that we can directly get the data
1132 * (up-to-date) from memory.
1133 */
1134 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301135 if (IS_ERR(vaddr)) {
1136 ret = PTR_ERR(vaddr);
1137 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
1138 return ret;
1139 }
1140
1141 guc->log.buf_addr = vaddr;
1142 }
1143
Akash Goel1e6b8b02016-10-12 21:54:43 +05301144 if (!guc->log.relay_chan) {
1145 /* Create a relay channel, so that we have buffers for storing
1146 * the GuC firmware logs, the channel will be linked with a file
1147 * later on when debugfs is registered.
1148 */
1149 ret = guc_log_create_relay_channel(guc);
1150 if (ret)
1151 return ret;
1152 }
1153
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301154 if (!guc->log.flush_wq) {
1155 INIT_WORK(&guc->log.flush_work, guc_capture_logs_work);
1156
Akash Goel7ef54de2016-10-12 21:54:44 +05301157 /*
1158 * GuC log buffer flush work item has to do register access to
1159 * send the ack to GuC and this work item, if not synced before
1160 * suspend, can potentially get executed after the GFX device is
1161 * suspended.
1162 * By marking the WQ as freezable, we don't have to bother about
1163 * flushing of this work item from the suspend hooks, the pending
1164 * work item if any will be either executed before the suspend
1165 * or scheduled later on resume. This way the handling of work
1166 * item can be kept same between system suspend & rpm suspend.
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301167 */
Akash Goel7ef54de2016-10-12 21:54:44 +05301168 guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log",
1169 WQ_HIGHPRI | WQ_FREEZABLE);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301170 if (guc->log.flush_wq == NULL) {
1171 DRM_ERROR("Couldn't allocate the wq for GuC logging\n");
1172 return -ENOMEM;
1173 }
1174 }
1175
1176 return 0;
1177}
1178
Dave Gordon7a9347f2016-09-12 21:19:37 +01001179static void guc_log_create(struct intel_guc *guc)
Alex Dai4c7e77f2015-08-12 15:43:40 +01001180{
Chris Wilson8b797af2016-08-15 10:48:51 +01001181 struct i915_vma *vma;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001182 unsigned long offset;
1183 uint32_t size, flags;
1184
Alex Dai4c7e77f2015-08-12 15:43:40 +01001185 if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
1186 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
1187
1188 /* The first page is to save log buffer state. Allocate one
1189 * extra page for others in case for overlap */
1190 size = (1 + GUC_LOG_DPC_PAGES + 1 +
1191 GUC_LOG_ISR_PAGES + 1 +
1192 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
1193
Akash Goeld6b40b42016-10-12 21:54:29 +05301194 vma = guc->log.vma;
Chris Wilson8b797af2016-08-15 10:48:51 +01001195 if (!vma) {
Akash Goel71706592016-10-12 21:54:42 +05301196 /* We require SSE 4.1 for fast reads from the GuC log buffer and
1197 * it should be present on the chipsets supporting GuC based
1198 * submisssions.
1199 */
1200 if (WARN_ON(!i915_memcpy_from_wc(NULL, NULL, 0))) {
1201 /* logging will not be enabled */
1202 i915.guc_log_level = -1;
1203 return;
1204 }
1205
Chris Wilson8b797af2016-08-15 10:48:51 +01001206 vma = guc_allocate_vma(guc, size);
1207 if (IS_ERR(vma)) {
Alex Dai4c7e77f2015-08-12 15:43:40 +01001208 /* logging will be off */
1209 i915.guc_log_level = -1;
1210 return;
1211 }
1212
Akash Goeld6b40b42016-10-12 21:54:29 +05301213 guc->log.vma = vma;
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301214
1215 if (guc_log_create_extras(guc)) {
1216 guc_log_cleanup(guc);
1217 i915_vma_unpin_and_release(&guc->log.vma);
1218 i915.guc_log_level = -1;
1219 return;
1220 }
Alex Dai4c7e77f2015-08-12 15:43:40 +01001221 }
1222
1223 /* each allocated unit is a page */
1224 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
1225 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
1226 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
1227 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
1228
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001229 offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
Akash Goeld6b40b42016-10-12 21:54:29 +05301230 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
Alex Dai4c7e77f2015-08-12 15:43:40 +01001231}
1232
Akash Goelf8240832016-10-12 21:54:34 +05301233static int guc_log_late_setup(struct intel_guc *guc)
1234{
1235 struct drm_i915_private *dev_priv = guc_to_i915(guc);
1236 int ret;
1237
1238 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1239
1240 if (i915.guc_log_level < 0)
1241 return -EINVAL;
1242
1243 /* If log_level was set as -1 at boot time, then setup needed to
1244 * handle log buffer flush interrupts would not have been done yet,
1245 * so do that now.
1246 */
1247 ret = guc_log_create_extras(guc);
1248 if (ret)
1249 goto err;
1250
1251 ret = guc_log_create_relay_file(guc);
1252 if (ret)
1253 goto err;
1254
1255 return 0;
1256err:
1257 guc_log_cleanup(guc);
1258 /* logging will remain off */
1259 i915.guc_log_level = -1;
1260 return ret;
1261}
1262
Dave Gordon7a9347f2016-09-12 21:19:37 +01001263static void guc_policies_init(struct guc_policies *policies)
Alex Dai463704d2015-12-18 12:00:10 -08001264{
1265 struct guc_policy *policy;
1266 u32 p, i;
1267
1268 policies->dpc_promote_time = 500000;
1269 policies->max_num_work_items = POLICY_MAX_NUM_WI;
1270
1271 for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
Alex Dai397097b2016-01-23 11:58:14 -08001272 for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
Alex Dai463704d2015-12-18 12:00:10 -08001273 policy = &policies->policy[p][i];
1274
1275 policy->execution_quantum = 1000000;
1276 policy->preemption_time = 500000;
1277 policy->fault_time = 250000;
1278 policy->policy_flags = 0;
1279 }
1280 }
1281
1282 policies->is_valid = 1;
1283}
1284
Dave Gordon7a9347f2016-09-12 21:19:37 +01001285static void guc_addon_create(struct intel_guc *guc)
Alex Dai68371a92015-12-18 12:00:09 -08001286{
1287 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Chris Wilson8b797af2016-08-15 10:48:51 +01001288 struct i915_vma *vma;
Alex Dai68371a92015-12-18 12:00:09 -08001289 struct guc_ads *ads;
Alex Dai463704d2015-12-18 12:00:10 -08001290 struct guc_policies *policies;
Alex Dai5c148e02015-12-18 12:00:11 -08001291 struct guc_mmio_reg_state *reg_state;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001292 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301293 enum intel_engine_id id;
Alex Dai68371a92015-12-18 12:00:09 -08001294 struct page *page;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001295 u32 size;
Alex Dai68371a92015-12-18 12:00:09 -08001296
1297 /* The ads obj includes the struct itself and buffers passed to GuC */
Alex Dai5c148e02015-12-18 12:00:11 -08001298 size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
1299 sizeof(struct guc_mmio_reg_state) +
1300 GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
Alex Dai68371a92015-12-18 12:00:09 -08001301
Chris Wilson8b797af2016-08-15 10:48:51 +01001302 vma = guc->ads_vma;
1303 if (!vma) {
1304 vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
1305 if (IS_ERR(vma))
Alex Dai68371a92015-12-18 12:00:09 -08001306 return;
1307
Chris Wilson8b797af2016-08-15 10:48:51 +01001308 guc->ads_vma = vma;
Alex Dai68371a92015-12-18 12:00:09 -08001309 }
1310
Chris Wilson8b797af2016-08-15 10:48:51 +01001311 page = i915_vma_first_page(vma);
Alex Dai68371a92015-12-18 12:00:09 -08001312 ads = kmap(page);
1313
1314 /*
1315 * The GuC requires a "Golden Context" when it reinitialises
1316 * engines after a reset. Here we use the Render ring default
1317 * context, which must already exist and be pinned in the GGTT,
1318 * so its address won't change after we've told the GuC where
1319 * to find it.
1320 */
Akash Goel3b3f1652016-10-13 22:44:48 +05301321 engine = dev_priv->engine[RCS];
Chris Wilson57e88532016-08-15 10:48:57 +01001322 ads->golden_context_lrca = engine->status_page.ggtt_offset;
Alex Dai68371a92015-12-18 12:00:09 -08001323
Akash Goel3b3f1652016-10-13 22:44:48 +05301324 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001325 ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
Alex Dai68371a92015-12-18 12:00:09 -08001326
Alex Dai463704d2015-12-18 12:00:10 -08001327 /* GuC scheduling policies */
1328 policies = (void *)ads + sizeof(struct guc_ads);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001329 guc_policies_init(policies);
Alex Dai463704d2015-12-18 12:00:10 -08001330
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001331 ads->scheduler_policies =
1332 i915_ggtt_offset(vma) + sizeof(struct guc_ads);
Alex Dai463704d2015-12-18 12:00:10 -08001333
Alex Dai5c148e02015-12-18 12:00:11 -08001334 /* MMIO reg state */
1335 reg_state = (void *)policies + sizeof(struct guc_policies);
1336
Akash Goel3b3f1652016-10-13 22:44:48 +05301337 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001338 reg_state->mmio_white_list[engine->guc_id].mmio_start =
1339 engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
Alex Dai5c148e02015-12-18 12:00:11 -08001340
1341 /* Nothing to be saved or restored for now. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001342 reg_state->mmio_white_list[engine->guc_id].count = 0;
Alex Dai5c148e02015-12-18 12:00:11 -08001343 }
1344
1345 ads->reg_state_addr = ads->scheduler_policies +
1346 sizeof(struct guc_policies);
1347
1348 ads->reg_state_buffer = ads->reg_state_addr +
1349 sizeof(struct guc_mmio_reg_state);
1350
Alex Dai68371a92015-12-18 12:00:09 -08001351 kunmap(page);
1352}
1353
Alex Daibac427f2015-08-12 15:43:39 +01001354/*
1355 * Set up the memory resources to be shared with the GuC. At this point,
1356 * we require just one object that can be mapped through the GGTT.
1357 */
Dave Gordonbeffa512016-06-10 18:29:26 +01001358int i915_guc_submission_init(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001359{
Dave Gordon7a9347f2016-09-12 21:19:37 +01001360 const size_t ctxsize = sizeof(struct guc_context_desc);
1361 const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
1362 const size_t gemsize = round_up(poolsize, PAGE_SIZE);
Alex Daibac427f2015-08-12 15:43:39 +01001363 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson8b797af2016-08-15 10:48:51 +01001364 struct i915_vma *vma;
Alex Daibac427f2015-08-12 15:43:39 +01001365
Chris Wilson4d357af2016-11-29 12:10:23 +00001366 if (!HAS_GUC_SCHED(dev_priv))
1367 return 0;
1368
Dave Gordon29fb72c2016-06-07 09:14:50 +01001369 /* Wipe bitmap & delete client in case of reinitialisation */
1370 bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
Dave Gordonbeffa512016-06-10 18:29:26 +01001371 i915_guc_submission_disable(dev_priv);
Dave Gordon29fb72c2016-06-07 09:14:50 +01001372
Alex Daibac427f2015-08-12 15:43:39 +01001373 if (!i915.enable_guc_submission)
1374 return 0; /* not enabled */
1375
Chris Wilson8b797af2016-08-15 10:48:51 +01001376 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001377 return 0; /* already allocated */
1378
Dave Gordon7a9347f2016-09-12 21:19:37 +01001379 vma = guc_allocate_vma(guc, gemsize);
Chris Wilson8b797af2016-08-15 10:48:51 +01001380 if (IS_ERR(vma))
1381 return PTR_ERR(vma);
Alex Daibac427f2015-08-12 15:43:39 +01001382
Chris Wilson8b797af2016-08-15 10:48:51 +01001383 guc->ctx_pool_vma = vma;
Alex Daibac427f2015-08-12 15:43:39 +01001384 ida_init(&guc->ctx_ids);
Dave Gordon7a9347f2016-09-12 21:19:37 +01001385 guc_log_create(guc);
1386 guc_addon_create(guc);
Alex Dai68371a92015-12-18 12:00:09 -08001387
Chris Wilson4d357af2016-11-29 12:10:23 +00001388 guc->execbuf_client = guc_client_alloc(dev_priv,
1389 INTEL_INFO(dev_priv)->ring_mask,
1390 GUC_CTX_PRIORITY_KMD_NORMAL,
1391 dev_priv->kernel_context);
1392 if (!guc->execbuf_client) {
1393 DRM_ERROR("Failed to create GuC client for execbuf!\n");
1394 goto err;
1395 }
1396
Alex Daibac427f2015-08-12 15:43:39 +01001397 return 0;
Chris Wilson4d357af2016-11-29 12:10:23 +00001398
1399err:
1400 i915_guc_submission_fini(dev_priv);
1401 return -ENOMEM;
1402}
1403
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001404static void guc_reset_wq(struct i915_guc_client *client)
Chris Wilson4d357af2016-11-29 12:10:23 +00001405{
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001406 struct guc_process_desc *desc = client->vaddr +
1407 client->proc_desc_offset;
Chris Wilson4d357af2016-11-29 12:10:23 +00001408
1409 desc->head = 0;
1410 desc->tail = 0;
1411
Michal Wajdeczko776594d2016-12-15 19:53:21 +00001412 client->wq_tail = 0;
Alex Daibac427f2015-08-12 15:43:39 +01001413}
1414
Dave Gordonbeffa512016-06-10 18:29:26 +01001415int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001416{
Dave Gordon44a28b12015-08-12 15:43:41 +01001417 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001418 struct i915_guc_client *client = guc->execbuf_client;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001419 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301420 enum intel_engine_id id;
Dave Gordon44a28b12015-08-12 15:43:41 +01001421
Chris Wilson4d357af2016-11-29 12:10:23 +00001422 if (!client)
1423 return -ENODEV;
Dave Gordon44a28b12015-08-12 15:43:41 +01001424
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001425 intel_guc_sample_forcewake(guc);
Chris Wilson4d357af2016-11-29 12:10:23 +00001426
1427 guc_reset_wq(client);
Dave Gordon4d757872016-06-13 17:57:34 +01001428 guc_init_doorbell_hw(guc);
Alex Daif5d3c3e2015-08-18 14:34:47 -07001429
Chris Wilsonddd66c52016-08-02 22:50:31 +01001430 /* Take over from manual control of ELSP (execlists) */
Akash Goel3b3f1652016-10-13 22:44:48 +05301431 for_each_engine(engine, dev_priv, id) {
Chris Wilson4d357af2016-11-29 12:10:23 +00001432 struct drm_i915_gem_request *rq;
1433
Chris Wilsonddd66c52016-08-02 22:50:31 +01001434 engine->submit_request = i915_guc_submit;
Chris Wilson20311bd2016-11-14 20:41:03 +00001435 engine->schedule = NULL;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001436
Chris Wilson821ed7d2016-09-09 14:11:53 +01001437 /* Replay the current set of previously submitted requests */
Chris Wilson4d357af2016-11-29 12:10:23 +00001438 list_for_each_entry(rq, &engine->timeline->requests, link) {
Chris Wilsondadd4812016-09-09 14:11:57 +01001439 client->wq_rsvd += sizeof(struct guc_wq_item);
Chris Wilson34ba5a82016-11-29 12:10:24 +00001440 __i915_guc_submit(rq);
Chris Wilsondadd4812016-09-09 14:11:57 +01001441 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001442 }
1443
Dave Gordon44a28b12015-08-12 15:43:41 +01001444 return 0;
1445}
1446
Dave Gordonbeffa512016-06-10 18:29:26 +01001447void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
Dave Gordon44a28b12015-08-12 15:43:41 +01001448{
Dave Gordon44a28b12015-08-12 15:43:41 +01001449 struct intel_guc *guc = &dev_priv->guc;
1450
Chris Wilsonddd66c52016-08-02 22:50:31 +01001451 if (!guc->execbuf_client)
1452 return;
1453
Chris Wilsonddd66c52016-08-02 22:50:31 +01001454 /* Revert back to manual ELSP submission */
1455 intel_execlists_enable_submission(dev_priv);
Dave Gordon44a28b12015-08-12 15:43:41 +01001456}
1457
Dave Gordonbeffa512016-06-10 18:29:26 +01001458void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
Alex Daibac427f2015-08-12 15:43:39 +01001459{
Alex Daibac427f2015-08-12 15:43:39 +01001460 struct intel_guc *guc = &dev_priv->guc;
Chris Wilson4d357af2016-11-29 12:10:23 +00001461 struct i915_guc_client *client;
1462
1463 client = fetch_and_zero(&guc->execbuf_client);
1464 if (!client)
1465 return;
1466
1467 guc_client_free(dev_priv, client);
Alex Daibac427f2015-08-12 15:43:39 +01001468
Chris Wilson19880c42016-08-15 10:49:05 +01001469 i915_vma_unpin_and_release(&guc->ads_vma);
Akash Goeld6b40b42016-10-12 21:54:29 +05301470 i915_vma_unpin_and_release(&guc->log.vma);
Alex Dai68371a92015-12-18 12:00:09 -08001471
Chris Wilson8b797af2016-08-15 10:48:51 +01001472 if (guc->ctx_pool_vma)
Alex Daibac427f2015-08-12 15:43:39 +01001473 ida_destroy(&guc->ctx_ids);
Chris Wilson19880c42016-08-15 10:49:05 +01001474 i915_vma_unpin_and_release(&guc->ctx_pool_vma);
Alex Daibac427f2015-08-12 15:43:39 +01001475}
Alex Daia1c41992015-09-30 09:46:37 -07001476
1477/**
1478 * intel_guc_suspend() - notify GuC entering suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001479 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001480 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001481int intel_guc_suspend(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001482{
Alex Daia1c41992015-09-30 09:46:37 -07001483 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001484 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001485 u32 data[3];
1486
Dave Gordonfce91f22016-05-20 11:42:42 +01001487 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001488 return 0;
1489
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301490 gen9_disable_guc_interrupts(dev_priv);
1491
Dave Gordoned54c1a2016-01-19 19:02:54 +00001492 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001493
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001494 data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001495 /* any value greater than GUC_POWER_D0 */
1496 data[1] = GUC_POWER_D1;
1497 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001498 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001499
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001500 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001501}
1502
1503
1504/**
1505 * intel_guc_resume() - notify GuC resuming from suspend state
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001506 * @dev_priv: i915 device private
Alex Daia1c41992015-09-30 09:46:37 -07001507 */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001508int intel_guc_resume(struct drm_i915_private *dev_priv)
Alex Daia1c41992015-09-30 09:46:37 -07001509{
Alex Daia1c41992015-09-30 09:46:37 -07001510 struct intel_guc *guc = &dev_priv->guc;
Chris Wilsone2efd132016-05-24 14:53:34 +01001511 struct i915_gem_context *ctx;
Alex Daia1c41992015-09-30 09:46:37 -07001512 u32 data[3];
1513
Dave Gordonfce91f22016-05-20 11:42:42 +01001514 if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
Alex Daia1c41992015-09-30 09:46:37 -07001515 return 0;
1516
Sagar Arun Kamble26705e22016-10-12 21:54:31 +05301517 if (i915.guc_log_level >= 0)
1518 gen9_enable_guc_interrupts(dev_priv);
1519
Dave Gordoned54c1a2016-01-19 19:02:54 +00001520 ctx = dev_priv->kernel_context;
Alex Daia1c41992015-09-30 09:46:37 -07001521
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001522 data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
Alex Daia1c41992015-09-30 09:46:37 -07001523 data[1] = GUC_POWER_D0;
1524 /* first page is shared data with GuC */
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001525 data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
Alex Daia1c41992015-09-30 09:46:37 -07001526
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001527 return intel_guc_send(guc, data, ARRAY_SIZE(data));
Alex Daia1c41992015-09-30 09:46:37 -07001528}
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301529
1530void i915_guc_capture_logs(struct drm_i915_private *dev_priv)
1531{
1532 guc_read_update_log_buffer(&dev_priv->guc);
1533
1534 /* Generally device is expected to be active only at this
1535 * time, so get/put should be really quick.
1536 */
1537 intel_runtime_pm_get(dev_priv);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001538 intel_guc_log_flush_complete(&dev_priv->guc);
Sagar Arun Kamble4100b2a2016-10-12 21:54:32 +05301539 intel_runtime_pm_put(dev_priv);
1540}
Akash Goelf8240832016-10-12 21:54:34 +05301541
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301542void i915_guc_flush_logs(struct drm_i915_private *dev_priv)
1543{
1544 if (!i915.enable_guc_submission || (i915.guc_log_level < 0))
1545 return;
1546
1547 /* First disable the interrupts, will be renabled afterwards */
1548 gen9_disable_guc_interrupts(dev_priv);
1549
1550 /* Before initiating the forceful flush, wait for any pending/ongoing
1551 * flush to complete otherwise forceful flush may not actually happen.
1552 */
1553 flush_work(&dev_priv->guc.log.flush_work);
1554
1555 /* Ask GuC to update the log buffer state */
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001556 intel_guc_log_flush(&dev_priv->guc);
Sagar Arun Kamble896a0cb2016-10-12 21:54:40 +05301557
1558 /* GuC would have updated log buffer by now, so capture it */
1559 i915_guc_capture_logs(dev_priv);
1560}
1561
Akash Goelf8240832016-10-12 21:54:34 +05301562void i915_guc_unregister(struct drm_i915_private *dev_priv)
1563{
1564 if (!i915.enable_guc_submission)
1565 return;
1566
1567 mutex_lock(&dev_priv->drm.struct_mutex);
1568 guc_log_cleanup(&dev_priv->guc);
1569 mutex_unlock(&dev_priv->drm.struct_mutex);
1570}
1571
1572void i915_guc_register(struct drm_i915_private *dev_priv)
1573{
1574 if (!i915.enable_guc_submission)
1575 return;
1576
1577 mutex_lock(&dev_priv->drm.struct_mutex);
1578 guc_log_late_setup(&dev_priv->guc);
1579 mutex_unlock(&dev_priv->drm.struct_mutex);
1580}
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301581
1582int i915_guc_log_control(struct drm_i915_private *dev_priv, u64 control_val)
1583{
1584 union guc_log_control log_param;
1585 int ret;
1586
1587 log_param.value = control_val;
1588
1589 if (log_param.verbosity < GUC_LOG_VERBOSITY_MIN ||
1590 log_param.verbosity > GUC_LOG_VERBOSITY_MAX)
1591 return -EINVAL;
1592
1593 /* This combination doesn't make sense & won't have any effect */
1594 if (!log_param.logging_enabled && (i915.guc_log_level < 0))
1595 return 0;
1596
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001597 ret = intel_guc_log_control(&dev_priv->guc, log_param.value);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301598 if (ret < 0) {
Arkadiusz Hilera80bc452016-11-25 18:59:34 +01001599 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
Sagar Arun Kamble685534e2016-10-12 21:54:41 +05301600 return ret;
1601 }
1602
1603 i915.guc_log_level = log_param.verbosity;
1604
1605 /* If log_level was set as -1 at boot time, then the relay channel file
1606 * wouldn't have been created by now and interrupts also would not have
1607 * been enabled.
1608 */
1609 if (!dev_priv->guc.log.relay_chan) {
1610 ret = guc_log_late_setup(&dev_priv->guc);
1611 if (!ret)
1612 gen9_enable_guc_interrupts(dev_priv);
1613 } else if (!log_param.logging_enabled) {
1614 /* Once logging is disabled, GuC won't generate logs & send an
1615 * interrupt. But there could be some data in the log buffer
1616 * which is yet to be captured. So request GuC to update the log
1617 * buffer state and then collect the left over logs.
1618 */
1619 i915_guc_flush_logs(dev_priv);
1620
1621 /* As logging is disabled, update log level to reflect that */
1622 i915.guc_log_level = -1;
1623 } else {
1624 /* In case interrupts were disabled, enable them now */
1625 gen9_enable_guc_interrupts(dev_priv);
1626 }
1627
1628 return ret;
1629}