Commit | Line | Data |
---|---|---|
bac427f8 AD |
1 | /* |
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 | */ | |
24 | #include <linux/firmware.h> | |
25 | #include <linux/circ_buf.h> | |
26 | #include "i915_drv.h" | |
27 | #include "intel_guc.h" | |
28 | ||
44a28b1d | 29 | /** |
feda33ef | 30 | * DOC: GuC-based command submission |
44a28b1d DG |
31 | * |
32 | * i915_guc_client: | |
33 | * We use the term client to avoid confusion with contexts. A i915_guc_client is | |
34 | * equivalent to GuC object guc_context_desc. This context descriptor is | |
35 | * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell | |
36 | * and workqueue for it. Also the process descriptor (guc_process_desc), which | |
37 | * is mapped to client space. So the client can write Work Item then ring the | |
38 | * doorbell. | |
39 | * | |
40 | * To simplify the implementation, we allocate one gem object that contains all | |
41 | * pages for doorbell, process descriptor and workqueue. | |
42 | * | |
43 | * The Scratch registers: | |
44 | * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes | |
45 | * a value to the action register (SOFT_SCRATCH_0) along with any data. It then | |
46 | * triggers an interrupt on the GuC via another register write (0xC4C8). | |
47 | * Firmware writes a success/fail code back to the action register after | |
48 | * processes the request. The kernel driver polls waiting for this update and | |
49 | * then proceeds. | |
50 | * See host2guc_action() | |
51 | * | |
52 | * Doorbells: | |
53 | * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW) | |
54 | * mapped into process space. | |
55 | * | |
56 | * Work Items: | |
57 | * There are several types of work items that the host may place into a | |
58 | * workqueue, each with its own requirements and limitations. Currently only | |
59 | * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which | |
60 | * represents in-order queue. The kernel driver packs ring tail pointer and an | |
61 | * ELSP context descriptor dword into Work Item. | |
62 | * See guc_add_workqueue_item() | |
63 | * | |
64 | */ | |
65 | ||
66 | /* | |
67 | * Read GuC command/status register (SOFT_SCRATCH_0) | |
68 | * Return true if it contains a response rather than a command | |
69 | */ | |
70 | static inline bool host2guc_action_response(struct drm_i915_private *dev_priv, | |
71 | u32 *status) | |
72 | { | |
73 | u32 val = I915_READ(SOFT_SCRATCH(0)); | |
74 | *status = val; | |
75 | return GUC2HOST_IS_RESPONSE(val); | |
76 | } | |
77 | ||
78 | static int host2guc_action(struct intel_guc *guc, u32 *data, u32 len) | |
79 | { | |
80 | struct drm_i915_private *dev_priv = guc_to_i915(guc); | |
81 | u32 status; | |
82 | int i; | |
83 | int ret; | |
84 | ||
85 | if (WARN_ON(len < 1 || len > 15)) | |
86 | return -EINVAL; | |
87 | ||
88 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); | |
44a28b1d DG |
89 | |
90 | dev_priv->guc.action_count += 1; | |
91 | dev_priv->guc.action_cmd = data[0]; | |
92 | ||
93 | for (i = 0; i < len; i++) | |
94 | I915_WRITE(SOFT_SCRATCH(i), data[i]); | |
95 | ||
96 | POSTING_READ(SOFT_SCRATCH(i - 1)); | |
97 | ||
98 | I915_WRITE(HOST2GUC_INTERRUPT, HOST2GUC_TRIGGER); | |
99 | ||
100 | /* No HOST2GUC command should take longer than 10ms */ | |
101 | ret = wait_for_atomic(host2guc_action_response(dev_priv, &status), 10); | |
102 | if (status != GUC2HOST_STATUS_SUCCESS) { | |
103 | /* | |
104 | * Either the GuC explicitly returned an error (which | |
105 | * we convert to -EIO here) or no response at all was | |
106 | * received within the timeout limit (-ETIMEDOUT) | |
107 | */ | |
108 | if (ret != -ETIMEDOUT) | |
109 | ret = -EIO; | |
110 | ||
111 | DRM_ERROR("GUC: host2guc action 0x%X failed. ret=%d " | |
112 | "status=0x%08X response=0x%08X\n", | |
113 | data[0], ret, status, | |
114 | I915_READ(SOFT_SCRATCH(15))); | |
115 | ||
116 | dev_priv->guc.action_fail += 1; | |
117 | dev_priv->guc.action_err = ret; | |
118 | } | |
119 | dev_priv->guc.action_status = status; | |
120 | ||
44a28b1d DG |
121 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
122 | ||
123 | return ret; | |
124 | } | |
125 | ||
126 | /* | |
127 | * Tell the GuC to allocate or deallocate a specific doorbell | |
128 | */ | |
129 | ||
130 | static int host2guc_allocate_doorbell(struct intel_guc *guc, | |
131 | struct i915_guc_client *client) | |
132 | { | |
133 | u32 data[2]; | |
134 | ||
135 | data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL; | |
136 | data[1] = client->ctx_index; | |
137 | ||
138 | return host2guc_action(guc, data, 2); | |
139 | } | |
140 | ||
141 | static int host2guc_release_doorbell(struct intel_guc *guc, | |
142 | struct i915_guc_client *client) | |
143 | { | |
144 | u32 data[2]; | |
145 | ||
146 | data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL; | |
147 | data[1] = client->ctx_index; | |
148 | ||
149 | return host2guc_action(guc, data, 2); | |
150 | } | |
151 | ||
f5d3c3ea AD |
152 | static int host2guc_sample_forcewake(struct intel_guc *guc, |
153 | struct i915_guc_client *client) | |
154 | { | |
155 | struct drm_i915_private *dev_priv = guc_to_i915(guc); | |
93f25318 | 156 | struct drm_device *dev = dev_priv->dev; |
f5d3c3ea AD |
157 | u32 data[2]; |
158 | ||
159 | data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE; | |
93f25318 | 160 | /* WaRsDisableCoarsePowerGating:skl,bxt */ |
06e668ac MK |
161 | if (!intel_enable_rc6(dev) || |
162 | NEEDS_WaRsDisableCoarsePowerGating(dev)) | |
93f25318 AD |
163 | data[1] = 0; |
164 | else | |
165 | /* bit 0 and 1 are for Render and Media domain separately */ | |
166 | data[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA; | |
167 | ||
168 | return host2guc_action(guc, data, ARRAY_SIZE(data)); | |
f5d3c3ea AD |
169 | } |
170 | ||
44a28b1d DG |
171 | /* |
172 | * Initialise, update, or clear doorbell data shared with the GuC | |
173 | * | |
174 | * These functions modify shared data and so need access to the mapped | |
175 | * client object which contains the page being used for the doorbell | |
176 | */ | |
177 | ||
178 | static void guc_init_doorbell(struct intel_guc *guc, | |
179 | struct i915_guc_client *client) | |
180 | { | |
181 | struct guc_doorbell_info *doorbell; | |
44a28b1d | 182 | |
0d92a6a4 | 183 | doorbell = client->client_base + client->doorbell_offset; |
44a28b1d | 184 | |
0d92a6a4 | 185 | doorbell->db_status = GUC_DOORBELL_ENABLED; |
44a28b1d | 186 | doorbell->cookie = 0; |
44a28b1d DG |
187 | } |
188 | ||
189 | static int guc_ring_doorbell(struct i915_guc_client *gc) | |
190 | { | |
191 | struct guc_process_desc *desc; | |
192 | union guc_doorbell_qw db_cmp, db_exc, db_ret; | |
193 | union guc_doorbell_qw *db; | |
44a28b1d DG |
194 | int attempt = 2, ret = -EAGAIN; |
195 | ||
0d92a6a4 | 196 | desc = gc->client_base + gc->proc_desc_offset; |
44a28b1d DG |
197 | |
198 | /* Update the tail so it is visible to GuC */ | |
199 | desc->tail = gc->wq_tail; | |
200 | ||
201 | /* current cookie */ | |
202 | db_cmp.db_status = GUC_DOORBELL_ENABLED; | |
203 | db_cmp.cookie = gc->cookie; | |
204 | ||
205 | /* cookie to be updated */ | |
206 | db_exc.db_status = GUC_DOORBELL_ENABLED; | |
207 | db_exc.cookie = gc->cookie + 1; | |
208 | if (db_exc.cookie == 0) | |
209 | db_exc.cookie = 1; | |
210 | ||
211 | /* pointer of current doorbell cacheline */ | |
0d92a6a4 | 212 | db = gc->client_base + gc->doorbell_offset; |
44a28b1d DG |
213 | |
214 | while (attempt--) { | |
215 | /* lets ring the doorbell */ | |
216 | db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db, | |
217 | db_cmp.value_qw, db_exc.value_qw); | |
218 | ||
219 | /* if the exchange was successfully executed */ | |
220 | if (db_ret.value_qw == db_cmp.value_qw) { | |
221 | /* db was successfully rung */ | |
222 | gc->cookie = db_exc.cookie; | |
223 | ret = 0; | |
224 | break; | |
225 | } | |
226 | ||
227 | /* XXX: doorbell was lost and need to acquire it again */ | |
228 | if (db_ret.db_status == GUC_DOORBELL_DISABLED) | |
229 | break; | |
230 | ||
231 | DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n", | |
232 | db_cmp.cookie, db_ret.cookie); | |
233 | ||
234 | /* update the cookie to newly read cookie from GuC */ | |
235 | db_cmp.cookie = db_ret.cookie; | |
236 | db_exc.cookie = db_ret.cookie + 1; | |
237 | if (db_exc.cookie == 0) | |
238 | db_exc.cookie = 1; | |
239 | } | |
240 | ||
44a28b1d DG |
241 | return ret; |
242 | } | |
243 | ||
244 | static void guc_disable_doorbell(struct intel_guc *guc, | |
245 | struct i915_guc_client *client) | |
246 | { | |
247 | struct drm_i915_private *dev_priv = guc_to_i915(guc); | |
248 | struct guc_doorbell_info *doorbell; | |
f0f59a00 | 249 | i915_reg_t drbreg = GEN8_DRBREGL(client->doorbell_id); |
44a28b1d DG |
250 | int value; |
251 | ||
0d92a6a4 | 252 | doorbell = client->client_base + client->doorbell_offset; |
44a28b1d | 253 | |
0d92a6a4 | 254 | doorbell->db_status = GUC_DOORBELL_DISABLED; |
44a28b1d DG |
255 | |
256 | I915_WRITE(drbreg, I915_READ(drbreg) & ~GEN8_DRB_VALID); | |
257 | ||
258 | value = I915_READ(drbreg); | |
259 | WARN_ON((value & GEN8_DRB_VALID) != 0); | |
260 | ||
261 | I915_WRITE(GEN8_DRBREGU(client->doorbell_id), 0); | |
262 | I915_WRITE(drbreg, 0); | |
263 | ||
264 | /* XXX: wait for any interrupts */ | |
265 | /* XXX: wait for workqueue to drain */ | |
266 | } | |
267 | ||
268 | /* | |
269 | * Select, assign and relase doorbell cachelines | |
270 | * | |
271 | * These functions track which doorbell cachelines are in use. | |
272 | * The data they manipulate is protected by the host2guc lock. | |
273 | */ | |
274 | ||
275 | static uint32_t select_doorbell_cacheline(struct intel_guc *guc) | |
276 | { | |
277 | const uint32_t cacheline_size = cache_line_size(); | |
278 | uint32_t offset; | |
279 | ||
44a28b1d DG |
280 | /* Doorbell uses a single cache line within a page */ |
281 | offset = offset_in_page(guc->db_cacheline); | |
282 | ||
283 | /* Moving to next cache line to reduce contention */ | |
284 | guc->db_cacheline += cacheline_size; | |
285 | ||
44a28b1d DG |
286 | DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n", |
287 | offset, guc->db_cacheline, cacheline_size); | |
288 | ||
289 | return offset; | |
290 | } | |
291 | ||
292 | static uint16_t assign_doorbell(struct intel_guc *guc, uint32_t priority) | |
293 | { | |
294 | /* | |
295 | * The bitmap is split into two halves; the first half is used for | |
296 | * normal priority contexts, the second half for high-priority ones. | |
297 | * Note that logically higher priorities are numerically less than | |
298 | * normal ones, so the test below means "is it high-priority?" | |
299 | */ | |
300 | const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH); | |
301 | const uint16_t half = GUC_MAX_DOORBELLS / 2; | |
302 | const uint16_t start = hi_pri ? half : 0; | |
303 | const uint16_t end = start + half; | |
304 | uint16_t id; | |
305 | ||
44a28b1d DG |
306 | id = find_next_zero_bit(guc->doorbell_bitmap, end, start); |
307 | if (id == end) | |
308 | id = GUC_INVALID_DOORBELL_ID; | |
309 | else | |
310 | bitmap_set(guc->doorbell_bitmap, id, 1); | |
44a28b1d DG |
311 | |
312 | DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n", | |
313 | hi_pri ? "high" : "normal", id); | |
314 | ||
315 | return id; | |
316 | } | |
317 | ||
318 | static void release_doorbell(struct intel_guc *guc, uint16_t id) | |
319 | { | |
44a28b1d | 320 | bitmap_clear(guc->doorbell_bitmap, id, 1); |
44a28b1d DG |
321 | } |
322 | ||
323 | /* | |
324 | * Initialise the process descriptor shared with the GuC firmware. | |
325 | */ | |
326 | static void guc_init_proc_desc(struct intel_guc *guc, | |
327 | struct i915_guc_client *client) | |
328 | { | |
329 | struct guc_process_desc *desc; | |
44a28b1d | 330 | |
0d92a6a4 | 331 | desc = client->client_base + client->proc_desc_offset; |
44a28b1d DG |
332 | |
333 | memset(desc, 0, sizeof(*desc)); | |
334 | ||
335 | /* | |
336 | * XXX: pDoorbell and WQVBaseAddress are pointers in process address | |
337 | * space for ring3 clients (set them as in mmap_ioctl) or kernel | |
338 | * space for kernel clients (map on demand instead? May make debug | |
339 | * easier to have it mapped). | |
340 | */ | |
341 | desc->wq_base_addr = 0; | |
342 | desc->db_base_addr = 0; | |
343 | ||
344 | desc->context_id = client->ctx_index; | |
345 | desc->wq_size_bytes = client->wq_size; | |
346 | desc->wq_status = WQ_STATUS_ACTIVE; | |
347 | desc->priority = client->priority; | |
44a28b1d DG |
348 | } |
349 | ||
350 | /* | |
351 | * Initialise/clear the context descriptor shared with the GuC firmware. | |
352 | * | |
353 | * This descriptor tells the GuC where (in GGTT space) to find the important | |
354 | * data structures relating to this client (doorbell, process descriptor, | |
355 | * write queue, etc). | |
356 | */ | |
357 | ||
358 | static void guc_init_ctx_desc(struct intel_guc *guc, | |
359 | struct i915_guc_client *client) | |
360 | { | |
397097b0 | 361 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
e2f80391 | 362 | struct intel_engine_cs *engine; |
d1675198 | 363 | struct intel_context *ctx = client->owner; |
44a28b1d DG |
364 | struct guc_context_desc desc; |
365 | struct sg_table *sg; | |
c3232b18 | 366 | enum intel_engine_id id; |
44a28b1d DG |
367 | |
368 | memset(&desc, 0, sizeof(desc)); | |
369 | ||
370 | desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL; | |
371 | desc.context_id = client->ctx_index; | |
372 | desc.priority = client->priority; | |
44a28b1d DG |
373 | desc.db_id = client->doorbell_id; |
374 | ||
c3232b18 | 375 | for_each_engine_id(engine, dev_priv, id) { |
e2f80391 | 376 | struct guc_execlist_context *lrc = &desc.lrc[engine->guc_id]; |
d1675198 AD |
377 | struct drm_i915_gem_object *obj; |
378 | uint64_t ctx_desc; | |
379 | ||
380 | /* TODO: We have a design issue to be solved here. Only when we | |
381 | * receive the first batch, we know which engine is used by the | |
382 | * user. But here GuC expects the lrc and ring to be pinned. It | |
383 | * is not an issue for default context, which is the only one | |
384 | * for now who owns a GuC client. But for future owner of GuC | |
385 | * client, need to make sure lrc is pinned prior to enter here. | |
386 | */ | |
c3232b18 | 387 | obj = ctx->engine[id].state; |
d1675198 AD |
388 | if (!obj) |
389 | break; /* XXX: continue? */ | |
390 | ||
e2f80391 | 391 | ctx_desc = intel_lr_context_descriptor(ctx, engine); |
d1675198 AD |
392 | lrc->context_desc = (u32)ctx_desc; |
393 | ||
394 | /* The state page is after PPHWSP */ | |
395 | lrc->ring_lcra = i915_gem_obj_ggtt_offset(obj) + | |
396 | LRC_STATE_PN * PAGE_SIZE; | |
397 | lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) | | |
e2f80391 | 398 | (engine->guc_id << GUC_ELC_ENGINE_OFFSET); |
d1675198 | 399 | |
c3232b18 | 400 | obj = ctx->engine[id].ringbuf->obj; |
d1675198 AD |
401 | |
402 | lrc->ring_begin = i915_gem_obj_ggtt_offset(obj); | |
403 | lrc->ring_end = lrc->ring_begin + obj->base.size - 1; | |
404 | lrc->ring_next_free_location = lrc->ring_begin; | |
405 | lrc->ring_current_tail_pointer_value = 0; | |
406 | ||
e2f80391 | 407 | desc.engines_used |= (1 << engine->guc_id); |
d1675198 AD |
408 | } |
409 | ||
410 | WARN_ON(desc.engines_used == 0); | |
411 | ||
44a28b1d DG |
412 | /* |
413 | * The CPU address is only needed at certain points, so kmap_atomic on | |
414 | * demand instead of storing it in the ctx descriptor. | |
415 | * XXX: May make debug easier to have it mapped | |
416 | */ | |
417 | desc.db_trigger_cpu = 0; | |
418 | desc.db_trigger_uk = client->doorbell_offset + | |
419 | i915_gem_obj_ggtt_offset(client->client_obj); | |
420 | desc.db_trigger_phy = client->doorbell_offset + | |
421 | sg_dma_address(client->client_obj->pages->sgl); | |
422 | ||
423 | desc.process_desc = client->proc_desc_offset + | |
424 | i915_gem_obj_ggtt_offset(client->client_obj); | |
425 | ||
426 | desc.wq_addr = client->wq_offset + | |
427 | i915_gem_obj_ggtt_offset(client->client_obj); | |
428 | ||
429 | desc.wq_size = client->wq_size; | |
430 | ||
431 | /* | |
432 | * XXX: Take LRCs from an existing intel_context if this is not an | |
433 | * IsKMDCreatedContext client | |
434 | */ | |
435 | desc.desc_private = (uintptr_t)client; | |
436 | ||
437 | /* Pool context is pinned already */ | |
438 | sg = guc->ctx_pool_obj->pages; | |
439 | sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc), | |
440 | sizeof(desc) * client->ctx_index); | |
441 | } | |
442 | ||
443 | static void guc_fini_ctx_desc(struct intel_guc *guc, | |
444 | struct i915_guc_client *client) | |
445 | { | |
446 | struct guc_context_desc desc; | |
447 | struct sg_table *sg; | |
448 | ||
449 | memset(&desc, 0, sizeof(desc)); | |
450 | ||
451 | sg = guc->ctx_pool_obj->pages; | |
452 | sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc), | |
453 | sizeof(desc) * client->ctx_index); | |
454 | } | |
455 | ||
a7e02199 | 456 | int i915_guc_wq_check_space(struct i915_guc_client *gc) |
44a28b1d DG |
457 | { |
458 | struct guc_process_desc *desc; | |
44a28b1d | 459 | u32 size = sizeof(struct guc_wq_item); |
5a843307 | 460 | int ret = -ETIMEDOUT, timeout_counter = 200; |
44a28b1d | 461 | |
a7e02199 AD |
462 | if (!gc) |
463 | return 0; | |
464 | ||
0d92a6a4 | 465 | desc = gc->client_base + gc->proc_desc_offset; |
44a28b1d DG |
466 | |
467 | while (timeout_counter-- > 0) { | |
a5916e8f | 468 | if (CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size) >= size) { |
5a843307 | 469 | ret = 0; |
a7e02199 | 470 | break; |
44a28b1d | 471 | } |
5a843307 AD |
472 | |
473 | if (timeout_counter) | |
474 | usleep_range(1000, 2000); | |
44a28b1d DG |
475 | }; |
476 | ||
44a28b1d DG |
477 | return ret; |
478 | } | |
479 | ||
480 | static int guc_add_workqueue_item(struct i915_guc_client *gc, | |
481 | struct drm_i915_gem_request *rq) | |
482 | { | |
a5916e8f | 483 | struct guc_process_desc *desc; |
44a28b1d DG |
484 | struct guc_wq_item *wqi; |
485 | void *base; | |
a7e02199 AD |
486 | u32 tail, wq_len, wq_off, space; |
487 | ||
a5916e8f AD |
488 | desc = gc->client_base + gc->proc_desc_offset; |
489 | space = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size); | |
a7e02199 AD |
490 | if (WARN_ON(space < sizeof(struct guc_wq_item))) |
491 | return -ENOSPC; /* shouldn't happen */ | |
44a28b1d | 492 | |
a7e02199 AD |
493 | /* postincrement WQ tail for next time */ |
494 | wq_off = gc->wq_tail; | |
495 | gc->wq_tail += sizeof(struct guc_wq_item); | |
496 | gc->wq_tail &= gc->wq_size - 1; | |
44a28b1d DG |
497 | |
498 | /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we | |
499 | * should not have the case where structure wqi is across page, neither | |
500 | * wrapped to the beginning. This simplifies the implementation below. | |
501 | * | |
502 | * XXX: if not the case, we need save data to a temp wqi and copy it to | |
503 | * workqueue buffer dw by dw. | |
504 | */ | |
505 | WARN_ON(sizeof(struct guc_wq_item) != 16); | |
506 | WARN_ON(wq_off & 3); | |
507 | ||
508 | /* wq starts from the page after doorbell / process_desc */ | |
509 | base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, | |
510 | (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT)); | |
511 | wq_off &= PAGE_SIZE - 1; | |
512 | wqi = (struct guc_wq_item *)((char *)base + wq_off); | |
513 | ||
514 | /* len does not include the header */ | |
515 | wq_len = sizeof(struct guc_wq_item) / sizeof(u32) - 1; | |
516 | wqi->header = WQ_TYPE_INORDER | | |
517 | (wq_len << WQ_LEN_SHIFT) | | |
4a570db5 | 518 | (rq->engine->guc_id << WQ_TARGET_SHIFT) | |
44a28b1d DG |
519 | WQ_NO_WCFLUSH_WAIT; |
520 | ||
521 | /* The GuC wants only the low-order word of the context descriptor */ | |
4a570db5 TU |
522 | wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, |
523 | rq->engine); | |
44a28b1d DG |
524 | |
525 | /* The GuC firmware wants the tail index in QWords, not bytes */ | |
526 | tail = rq->ringbuf->tail >> 3; | |
527 | wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT; | |
528 | wqi->fence_id = 0; /*XXX: what fence to be here */ | |
529 | ||
530 | kunmap_atomic(base); | |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
535 | /** | |
536 | * i915_guc_submit() - Submit commands through GuC | |
537 | * @client: the guc client where commands will go through | |
feda33ef | 538 | * @rq: request associated with the commands |
44a28b1d DG |
539 | * |
540 | * Return: 0 if succeed | |
541 | */ | |
542 | int i915_guc_submit(struct i915_guc_client *client, | |
543 | struct drm_i915_gem_request *rq) | |
544 | { | |
545 | struct intel_guc *guc = client->guc; | |
4a570db5 | 546 | unsigned int engine_id = rq->engine->guc_id; |
44a28b1d DG |
547 | int q_ret, b_ret; |
548 | ||
44a28b1d DG |
549 | q_ret = guc_add_workqueue_item(client, rq); |
550 | if (q_ret == 0) | |
551 | b_ret = guc_ring_doorbell(client); | |
552 | ||
397097b0 | 553 | client->submissions[engine_id] += 1; |
44a28b1d DG |
554 | if (q_ret) { |
555 | client->q_fail += 1; | |
556 | client->retcode = q_ret; | |
557 | } else if (b_ret) { | |
558 | client->b_fail += 1; | |
559 | client->retcode = q_ret = b_ret; | |
560 | } else { | |
561 | client->retcode = 0; | |
562 | } | |
397097b0 AD |
563 | guc->submissions[engine_id] += 1; |
564 | guc->last_seqno[engine_id] = rq->seqno; | |
44a28b1d DG |
565 | |
566 | return q_ret; | |
567 | } | |
568 | ||
569 | /* | |
570 | * Everything below here is concerned with setup & teardown, and is | |
571 | * therefore not part of the somewhat time-critical batch-submission | |
572 | * path of i915_guc_submit() above. | |
573 | */ | |
574 | ||
bac427f8 AD |
575 | /** |
576 | * gem_allocate_guc_obj() - Allocate gem object for GuC usage | |
577 | * @dev: drm device | |
578 | * @size: size of object | |
579 | * | |
580 | * This is a wrapper to create a gem obj. In order to use it inside GuC, the | |
581 | * object needs to be pinned lifetime. Also we must pin it to gtt space other | |
582 | * than [0, GUC_WOPCM_TOP) because this range is reserved inside GuC. | |
583 | * | |
584 | * Return: A drm_i915_gem_object if successful, otherwise NULL. | |
585 | */ | |
586 | static struct drm_i915_gem_object *gem_allocate_guc_obj(struct drm_device *dev, | |
587 | u32 size) | |
588 | { | |
589 | struct drm_i915_private *dev_priv = dev->dev_private; | |
590 | struct drm_i915_gem_object *obj; | |
591 | ||
592 | obj = i915_gem_alloc_object(dev, size); | |
593 | if (!obj) | |
594 | return NULL; | |
595 | ||
596 | if (i915_gem_object_get_pages(obj)) { | |
597 | drm_gem_object_unreference(&obj->base); | |
598 | return NULL; | |
599 | } | |
600 | ||
601 | if (i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, | |
602 | PIN_OFFSET_BIAS | GUC_WOPCM_TOP)) { | |
603 | drm_gem_object_unreference(&obj->base); | |
604 | return NULL; | |
605 | } | |
606 | ||
607 | /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */ | |
608 | I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE); | |
609 | ||
610 | return obj; | |
611 | } | |
612 | ||
613 | /** | |
614 | * gem_release_guc_obj() - Release gem object allocated for GuC usage | |
615 | * @obj: gem obj to be released | |
81fd874e | 616 | */ |
bac427f8 AD |
617 | static void gem_release_guc_obj(struct drm_i915_gem_object *obj) |
618 | { | |
619 | if (!obj) | |
620 | return; | |
621 | ||
622 | if (i915_gem_obj_is_pinned(obj)) | |
623 | i915_gem_object_ggtt_unpin(obj); | |
624 | ||
625 | drm_gem_object_unreference(&obj->base); | |
626 | } | |
627 | ||
44a28b1d DG |
628 | static void guc_client_free(struct drm_device *dev, |
629 | struct i915_guc_client *client) | |
630 | { | |
631 | struct drm_i915_private *dev_priv = dev->dev_private; | |
632 | struct intel_guc *guc = &dev_priv->guc; | |
633 | ||
634 | if (!client) | |
635 | return; | |
636 | ||
44a28b1d DG |
637 | /* |
638 | * XXX: wait for any outstanding submissions before freeing memory. | |
639 | * Be sure to drop any locks | |
640 | */ | |
641 | ||
0d92a6a4 DG |
642 | if (client->client_base) { |
643 | /* | |
644 | * If we got as far as setting up a doorbell, make sure | |
645 | * we shut it down before unmapping & deallocating the | |
646 | * memory. So first disable the doorbell, then tell the | |
647 | * GuC that we've finished with it, finally deallocate | |
648 | * it in our bitmap | |
649 | */ | |
650 | if (client->doorbell_id != GUC_INVALID_DOORBELL_ID) { | |
651 | guc_disable_doorbell(guc, client); | |
652 | host2guc_release_doorbell(guc, client); | |
653 | release_doorbell(guc, client->doorbell_id); | |
654 | } | |
655 | ||
656 | kunmap(kmap_to_page(client->client_base)); | |
657 | } | |
658 | ||
44a28b1d DG |
659 | gem_release_guc_obj(client->client_obj); |
660 | ||
661 | if (client->ctx_index != GUC_INVALID_CTX_ID) { | |
662 | guc_fini_ctx_desc(guc, client); | |
663 | ida_simple_remove(&guc->ctx_ids, client->ctx_index); | |
664 | } | |
665 | ||
666 | kfree(client); | |
667 | } | |
668 | ||
669 | /** | |
670 | * guc_client_alloc() - Allocate an i915_guc_client | |
671 | * @dev: drm device | |
672 | * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW | |
673 | * The kernel client to replace ExecList submission is created with | |
674 | * NORMAL priority. Priority of a client for scheduler can be HIGH, | |
675 | * while a preemption context can use CRITICAL. | |
feda33ef AD |
676 | * @ctx: the context that owns the client (we use the default render |
677 | * context) | |
44a28b1d | 678 | * |
0d92a6a4 | 679 | * Return: An i915_guc_client object if success, else NULL. |
44a28b1d DG |
680 | */ |
681 | static struct i915_guc_client *guc_client_alloc(struct drm_device *dev, | |
d1675198 AD |
682 | uint32_t priority, |
683 | struct intel_context *ctx) | |
44a28b1d DG |
684 | { |
685 | struct i915_guc_client *client; | |
686 | struct drm_i915_private *dev_priv = dev->dev_private; | |
687 | struct intel_guc *guc = &dev_priv->guc; | |
688 | struct drm_i915_gem_object *obj; | |
689 | ||
690 | client = kzalloc(sizeof(*client), GFP_KERNEL); | |
691 | if (!client) | |
692 | return NULL; | |
693 | ||
694 | client->doorbell_id = GUC_INVALID_DOORBELL_ID; | |
695 | client->priority = priority; | |
d1675198 | 696 | client->owner = ctx; |
44a28b1d DG |
697 | client->guc = guc; |
698 | ||
699 | client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0, | |
700 | GUC_MAX_GPU_CONTEXTS, GFP_KERNEL); | |
701 | if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) { | |
702 | client->ctx_index = GUC_INVALID_CTX_ID; | |
703 | goto err; | |
704 | } | |
705 | ||
706 | /* The first page is doorbell/proc_desc. Two followed pages are wq. */ | |
707 | obj = gem_allocate_guc_obj(dev, GUC_DB_SIZE + GUC_WQ_SIZE); | |
708 | if (!obj) | |
709 | goto err; | |
710 | ||
0d92a6a4 | 711 | /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */ |
44a28b1d | 712 | client->client_obj = obj; |
0d92a6a4 | 713 | client->client_base = kmap(i915_gem_object_get_page(obj, 0)); |
44a28b1d DG |
714 | client->wq_offset = GUC_DB_SIZE; |
715 | client->wq_size = GUC_WQ_SIZE; | |
44a28b1d DG |
716 | |
717 | client->doorbell_offset = select_doorbell_cacheline(guc); | |
718 | ||
719 | /* | |
720 | * Since the doorbell only requires a single cacheline, we can save | |
721 | * space by putting the application process descriptor in the same | |
722 | * page. Use the half of the page that doesn't include the doorbell. | |
723 | */ | |
724 | if (client->doorbell_offset >= (GUC_DB_SIZE / 2)) | |
725 | client->proc_desc_offset = 0; | |
726 | else | |
727 | client->proc_desc_offset = (GUC_DB_SIZE / 2); | |
728 | ||
729 | client->doorbell_id = assign_doorbell(guc, client->priority); | |
730 | if (client->doorbell_id == GUC_INVALID_DOORBELL_ID) | |
731 | /* XXX: evict a doorbell instead */ | |
732 | goto err; | |
733 | ||
734 | guc_init_proc_desc(guc, client); | |
735 | guc_init_ctx_desc(guc, client); | |
736 | guc_init_doorbell(guc, client); | |
737 | ||
738 | /* XXX: Any cache flushes needed? General domain mgmt calls? */ | |
739 | ||
740 | if (host2guc_allocate_doorbell(guc, client)) | |
741 | goto err; | |
742 | ||
743 | DRM_DEBUG_DRIVER("new priority %u client %p: ctx_index %u db_id %u\n", | |
744 | priority, client, client->ctx_index, client->doorbell_id); | |
745 | ||
746 | return client; | |
747 | ||
748 | err: | |
749 | DRM_ERROR("FAILED to create priority %u GuC client!\n", priority); | |
750 | ||
751 | guc_client_free(dev, client); | |
752 | return NULL; | |
753 | } | |
754 | ||
4c7e77fc AD |
755 | static void guc_create_log(struct intel_guc *guc) |
756 | { | |
757 | struct drm_i915_private *dev_priv = guc_to_i915(guc); | |
758 | struct drm_i915_gem_object *obj; | |
759 | unsigned long offset; | |
760 | uint32_t size, flags; | |
761 | ||
762 | if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN) | |
763 | return; | |
764 | ||
765 | if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX) | |
766 | i915.guc_log_level = GUC_LOG_VERBOSITY_MAX; | |
767 | ||
768 | /* The first page is to save log buffer state. Allocate one | |
769 | * extra page for others in case for overlap */ | |
770 | size = (1 + GUC_LOG_DPC_PAGES + 1 + | |
771 | GUC_LOG_ISR_PAGES + 1 + | |
772 | GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT; | |
773 | ||
774 | obj = guc->log_obj; | |
775 | if (!obj) { | |
776 | obj = gem_allocate_guc_obj(dev_priv->dev, size); | |
777 | if (!obj) { | |
778 | /* logging will be off */ | |
779 | i915.guc_log_level = -1; | |
780 | return; | |
781 | } | |
782 | ||
783 | guc->log_obj = obj; | |
784 | } | |
785 | ||
786 | /* each allocated unit is a page */ | |
787 | flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL | | |
788 | (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) | | |
789 | (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) | | |
790 | (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT); | |
791 | ||
792 | offset = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; /* in pages */ | |
793 | guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags; | |
794 | } | |
795 | ||
463704d0 AD |
796 | static void init_guc_policies(struct guc_policies *policies) |
797 | { | |
798 | struct guc_policy *policy; | |
799 | u32 p, i; | |
800 | ||
801 | policies->dpc_promote_time = 500000; | |
802 | policies->max_num_work_items = POLICY_MAX_NUM_WI; | |
803 | ||
804 | for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) { | |
397097b0 | 805 | for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) { |
463704d0 AD |
806 | policy = &policies->policy[p][i]; |
807 | ||
808 | policy->execution_quantum = 1000000; | |
809 | policy->preemption_time = 500000; | |
810 | policy->fault_time = 250000; | |
811 | policy->policy_flags = 0; | |
812 | } | |
813 | } | |
814 | ||
815 | policies->is_valid = 1; | |
816 | } | |
817 | ||
68371a95 AD |
818 | static void guc_create_ads(struct intel_guc *guc) |
819 | { | |
820 | struct drm_i915_private *dev_priv = guc_to_i915(guc); | |
821 | struct drm_i915_gem_object *obj; | |
822 | struct guc_ads *ads; | |
463704d0 | 823 | struct guc_policies *policies; |
5c148e04 | 824 | struct guc_mmio_reg_state *reg_state; |
e2f80391 | 825 | struct intel_engine_cs *engine; |
68371a95 | 826 | struct page *page; |
b4ac5afc | 827 | u32 size; |
68371a95 AD |
828 | |
829 | /* The ads obj includes the struct itself and buffers passed to GuC */ | |
5c148e04 AD |
830 | size = sizeof(struct guc_ads) + sizeof(struct guc_policies) + |
831 | sizeof(struct guc_mmio_reg_state) + | |
832 | GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE; | |
68371a95 AD |
833 | |
834 | obj = guc->ads_obj; | |
835 | if (!obj) { | |
836 | obj = gem_allocate_guc_obj(dev_priv->dev, PAGE_ALIGN(size)); | |
837 | if (!obj) | |
838 | return; | |
839 | ||
840 | guc->ads_obj = obj; | |
841 | } | |
842 | ||
843 | page = i915_gem_object_get_page(obj, 0); | |
844 | ads = kmap(page); | |
845 | ||
846 | /* | |
847 | * The GuC requires a "Golden Context" when it reinitialises | |
848 | * engines after a reset. Here we use the Render ring default | |
849 | * context, which must already exist and be pinned in the GGTT, | |
850 | * so its address won't change after we've told the GuC where | |
851 | * to find it. | |
852 | */ | |
4a570db5 | 853 | engine = &dev_priv->engine[RCS]; |
e2f80391 | 854 | ads->golden_context_lrca = engine->status_page.gfx_addr; |
68371a95 | 855 | |
b4ac5afc | 856 | for_each_engine(engine, dev_priv) |
e2f80391 | 857 | ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine); |
68371a95 | 858 | |
463704d0 AD |
859 | /* GuC scheduling policies */ |
860 | policies = (void *)ads + sizeof(struct guc_ads); | |
861 | init_guc_policies(policies); | |
862 | ||
863 | ads->scheduler_policies = i915_gem_obj_ggtt_offset(obj) + | |
864 | sizeof(struct guc_ads); | |
865 | ||
5c148e04 AD |
866 | /* MMIO reg state */ |
867 | reg_state = (void *)policies + sizeof(struct guc_policies); | |
868 | ||
b4ac5afc | 869 | for_each_engine(engine, dev_priv) { |
e2f80391 TU |
870 | reg_state->mmio_white_list[engine->guc_id].mmio_start = |
871 | engine->mmio_base + GUC_MMIO_WHITE_LIST_START; | |
5c148e04 AD |
872 | |
873 | /* Nothing to be saved or restored for now. */ | |
e2f80391 | 874 | reg_state->mmio_white_list[engine->guc_id].count = 0; |
5c148e04 AD |
875 | } |
876 | ||
877 | ads->reg_state_addr = ads->scheduler_policies + | |
878 | sizeof(struct guc_policies); | |
879 | ||
880 | ads->reg_state_buffer = ads->reg_state_addr + | |
881 | sizeof(struct guc_mmio_reg_state); | |
882 | ||
68371a95 AD |
883 | kunmap(page); |
884 | } | |
885 | ||
bac427f8 AD |
886 | /* |
887 | * Set up the memory resources to be shared with the GuC. At this point, | |
888 | * we require just one object that can be mapped through the GGTT. | |
889 | */ | |
890 | int i915_guc_submission_init(struct drm_device *dev) | |
891 | { | |
892 | struct drm_i915_private *dev_priv = dev->dev_private; | |
893 | const size_t ctxsize = sizeof(struct guc_context_desc); | |
894 | const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize; | |
895 | const size_t gemsize = round_up(poolsize, PAGE_SIZE); | |
896 | struct intel_guc *guc = &dev_priv->guc; | |
897 | ||
898 | if (!i915.enable_guc_submission) | |
899 | return 0; /* not enabled */ | |
900 | ||
901 | if (guc->ctx_pool_obj) | |
902 | return 0; /* already allocated */ | |
903 | ||
904 | guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv->dev, gemsize); | |
905 | if (!guc->ctx_pool_obj) | |
906 | return -ENOMEM; | |
907 | ||
908 | ida_init(&guc->ctx_ids); | |
909 | ||
4c7e77fc AD |
910 | guc_create_log(guc); |
911 | ||
68371a95 AD |
912 | guc_create_ads(guc); |
913 | ||
bac427f8 AD |
914 | return 0; |
915 | } | |
916 | ||
44a28b1d DG |
917 | int i915_guc_submission_enable(struct drm_device *dev) |
918 | { | |
919 | struct drm_i915_private *dev_priv = dev->dev_private; | |
920 | struct intel_guc *guc = &dev_priv->guc; | |
ed54c1a1 | 921 | struct intel_context *ctx = dev_priv->kernel_context; |
44a28b1d DG |
922 | struct i915_guc_client *client; |
923 | ||
924 | /* client for execbuf submission */ | |
d1675198 | 925 | client = guc_client_alloc(dev, GUC_CTX_PRIORITY_KMD_NORMAL, ctx); |
44a28b1d DG |
926 | if (!client) { |
927 | DRM_ERROR("Failed to create execbuf guc_client\n"); | |
928 | return -ENOMEM; | |
929 | } | |
930 | ||
931 | guc->execbuf_client = client; | |
f5d3c3ea AD |
932 | |
933 | host2guc_sample_forcewake(guc, client); | |
934 | ||
44a28b1d DG |
935 | return 0; |
936 | } | |
937 | ||
938 | void i915_guc_submission_disable(struct drm_device *dev) | |
939 | { | |
940 | struct drm_i915_private *dev_priv = dev->dev_private; | |
941 | struct intel_guc *guc = &dev_priv->guc; | |
942 | ||
943 | guc_client_free(dev, guc->execbuf_client); | |
944 | guc->execbuf_client = NULL; | |
945 | } | |
946 | ||
bac427f8 AD |
947 | void i915_guc_submission_fini(struct drm_device *dev) |
948 | { | |
949 | struct drm_i915_private *dev_priv = dev->dev_private; | |
950 | struct intel_guc *guc = &dev_priv->guc; | |
951 | ||
68371a95 AD |
952 | gem_release_guc_obj(dev_priv->guc.ads_obj); |
953 | guc->ads_obj = NULL; | |
954 | ||
4c7e77fc AD |
955 | gem_release_guc_obj(dev_priv->guc.log_obj); |
956 | guc->log_obj = NULL; | |
957 | ||
bac427f8 AD |
958 | if (guc->ctx_pool_obj) |
959 | ida_destroy(&guc->ctx_ids); | |
960 | gem_release_guc_obj(guc->ctx_pool_obj); | |
961 | guc->ctx_pool_obj = NULL; | |
962 | } | |
a1c41994 AD |
963 | |
964 | /** | |
965 | * intel_guc_suspend() - notify GuC entering suspend state | |
966 | * @dev: drm device | |
967 | */ | |
968 | int intel_guc_suspend(struct drm_device *dev) | |
969 | { | |
970 | struct drm_i915_private *dev_priv = dev->dev_private; | |
971 | struct intel_guc *guc = &dev_priv->guc; | |
972 | struct intel_context *ctx; | |
973 | u32 data[3]; | |
974 | ||
975 | if (!i915.enable_guc_submission) | |
976 | return 0; | |
977 | ||
ed54c1a1 | 978 | ctx = dev_priv->kernel_context; |
a1c41994 AD |
979 | |
980 | data[0] = HOST2GUC_ACTION_ENTER_S_STATE; | |
981 | /* any value greater than GUC_POWER_D0 */ | |
982 | data[1] = GUC_POWER_D1; | |
983 | /* first page is shared data with GuC */ | |
984 | data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state); | |
985 | ||
986 | return host2guc_action(guc, data, ARRAY_SIZE(data)); | |
987 | } | |
988 | ||
989 | ||
990 | /** | |
991 | * intel_guc_resume() - notify GuC resuming from suspend state | |
992 | * @dev: drm device | |
993 | */ | |
994 | int intel_guc_resume(struct drm_device *dev) | |
995 | { | |
996 | struct drm_i915_private *dev_priv = dev->dev_private; | |
997 | struct intel_guc *guc = &dev_priv->guc; | |
998 | struct intel_context *ctx; | |
999 | u32 data[3]; | |
1000 | ||
1001 | if (!i915.enable_guc_submission) | |
1002 | return 0; | |
1003 | ||
ed54c1a1 | 1004 | ctx = dev_priv->kernel_context; |
a1c41994 AD |
1005 | |
1006 | data[0] = HOST2GUC_ACTION_EXIT_S_STATE; | |
1007 | data[1] = GUC_POWER_D0; | |
1008 | /* first page is shared data with GuC */ | |
1009 | data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state); | |
1010 | ||
1011 | return host2guc_action(guc, data, ARRAY_SIZE(data)); | |
1012 | } |