Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 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 | */ |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 24 | #include <linux/circ_buf.h> |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/relay.h> |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 27 | #include "i915_drv.h" |
Arkadiusz Hiler | 8c4f24f | 2016-11-25 18:59:33 +0100 | [diff] [blame] | 28 | #include "intel_uc.h" |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
Alex Dai | feda33e | 2015-10-19 16:10:54 -0700 | [diff] [blame] | 31 | * DOC: GuC-based command submission |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 32 | * |
| 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 Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 51 | * See intel_guc_send() |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 52 | * |
| 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 Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 63 | * See guc_wq_item_append() |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 64 | * |
| 65 | */ |
| 66 | |
| 67 | /* |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 68 | * Tell the GuC to allocate or deallocate a specific doorbell |
| 69 | */ |
| 70 | |
Arkadiusz Hiler | a80bc45 | 2016-11-25 18:59:34 +0100 | [diff] [blame] | 71 | static int guc_allocate_doorbell(struct intel_guc *guc, |
| 72 | struct i915_guc_client *client) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 73 | { |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 74 | u32 action[] = { |
| 75 | INTEL_GUC_ACTION_ALLOCATE_DOORBELL, |
| 76 | client->ctx_index |
| 77 | }; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 78 | |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 79 | return intel_guc_send(guc, action, ARRAY_SIZE(action)); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Arkadiusz Hiler | a80bc45 | 2016-11-25 18:59:34 +0100 | [diff] [blame] | 82 | static int guc_release_doorbell(struct intel_guc *guc, |
| 83 | struct i915_guc_client *client) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 84 | { |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 85 | u32 action[] = { |
| 86 | INTEL_GUC_ACTION_DEALLOCATE_DOORBELL, |
| 87 | client->ctx_index |
| 88 | }; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 89 | |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 90 | return intel_guc_send(guc, action, ARRAY_SIZE(action)); |
Sagar Arun Kamble | 685534e | 2016-10-12 21:54:41 +0530 | [diff] [blame] | 91 | } |
| 92 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 93 | /* |
| 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 Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 100 | static int guc_update_doorbell_id(struct intel_guc *guc, |
| 101 | struct i915_guc_client *client, |
| 102 | u16 new_id) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 103 | { |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 104 | struct sg_table *sg = guc->ctx_pool_vma->pages; |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 105 | void *doorbell_bitmap = guc->doorbell_bitmap; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 106 | struct guc_doorbell_info *doorbell; |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 107 | struct guc_context_desc desc; |
| 108 | size_t len; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 109 | |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 110 | doorbell = client->vaddr + client->doorbell_offset; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 111 | |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 112 | 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 Hiler | a80bc45 | 2016-11-25 18:59:34 +0100 | [diff] [blame] | 116 | (void)guc_release_doorbell(guc, client); |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 117 | __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 Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 137 | doorbell->db_status = GUC_DOORBELL_ENABLED; |
Chris Wilson | 597bdc8 | 2016-11-29 12:10:22 +0000 | [diff] [blame] | 138 | doorbell->cookie = client->doorbell_cookie; |
Arkadiusz Hiler | a80bc45 | 2016-11-25 18:59:34 +0100 | [diff] [blame] | 139 | return guc_allocate_doorbell(guc, client); |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 142 | static void guc_disable_doorbell(struct intel_guc *guc, |
| 143 | struct i915_guc_client *client) |
| 144 | { |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 145 | (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 146 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 147 | /* XXX: wait for any interrupts */ |
| 148 | /* XXX: wait for workqueue to drain */ |
| 149 | } |
| 150 | |
Dave Gordon | f10d69a | 2016-06-13 17:57:33 +0100 | [diff] [blame] | 151 | static uint16_t |
| 152 | select_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 Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 177 | /* |
| 178 | * Select, assign and relase doorbell cachelines |
| 179 | * |
| 180 | * These functions track which doorbell cachelines are in use. |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 181 | * The data they manipulate is protected by the intel_guc_send lock. |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 182 | */ |
| 183 | |
| 184 | static 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 Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 189 | /* 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 Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 195 | 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 Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 201 | /* |
| 202 | * Initialise the process descriptor shared with the GuC firmware. |
| 203 | */ |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 204 | static void guc_proc_desc_init(struct intel_guc *guc, |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 205 | struct i915_guc_client *client) |
| 206 | { |
| 207 | struct guc_process_desc *desc; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 208 | |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 209 | desc = client->vaddr + client->proc_desc_offset; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 210 | |
| 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 Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 226 | } |
| 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 Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 236 | static void guc_ctx_desc_init(struct intel_guc *guc, |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 237 | struct i915_guc_client *client) |
| 238 | { |
Alex Dai | 397097b | 2016-01-23 11:58:14 -0800 | [diff] [blame] | 239 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 240 | struct intel_engine_cs *engine; |
Chris Wilson | e2efd13 | 2016-05-24 14:53:34 +0100 | [diff] [blame] | 241 | struct i915_gem_context *ctx = client->owner; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 242 | struct guc_context_desc desc; |
| 243 | struct sg_table *sg; |
Chris Wilson | bafb0fc | 2016-08-27 08:54:01 +0100 | [diff] [blame] | 244 | unsigned int tmp; |
Dave Gordon | 86e06cc | 2016-04-19 16:08:36 +0100 | [diff] [blame] | 245 | u32 gfx_addr; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 246 | |
| 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 Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 252 | desc.db_id = client->doorbell_id; |
| 253 | |
Chris Wilson | bafb0fc | 2016-08-27 08:54:01 +0100 | [diff] [blame] | 254 | for_each_engine_masked(engine, dev_priv, client->engines, tmp) { |
Chris Wilson | 9021ad0 | 2016-05-24 14:53:37 +0100 | [diff] [blame] | 255 | struct intel_context *ce = &ctx->engine[engine->id]; |
Dave Gordon | c18468c | 2016-08-09 15:19:22 +0100 | [diff] [blame] | 256 | uint32_t guc_engine_id = engine->guc_id; |
| 257 | struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id]; |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 258 | |
| 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 Wilson | 9021ad0 | 2016-05-24 14:53:37 +0100 | [diff] [blame] | 266 | if (!ce->state) |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 267 | break; /* XXX: continue? */ |
| 268 | |
Chris Wilson | 9021ad0 | 2016-05-24 14:53:37 +0100 | [diff] [blame] | 269 | lrc->context_desc = lower_32_bits(ce->lrc_desc); |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 270 | |
| 271 | /* The state page is after PPHWSP */ |
Chris Wilson | 57e8853 | 2016-08-15 10:48:57 +0100 | [diff] [blame] | 272 | lrc->ring_lcra = |
Chris Wilson | bde13eb | 2016-08-15 10:49:07 +0100 | [diff] [blame] | 273 | i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE; |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 274 | lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) | |
Dave Gordon | c18468c | 2016-08-09 15:19:22 +0100 | [diff] [blame] | 275 | (guc_engine_id << GUC_ELC_ENGINE_OFFSET); |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 276 | |
Chris Wilson | bde13eb | 2016-08-15 10:49:07 +0100 | [diff] [blame] | 277 | lrc->ring_begin = i915_ggtt_offset(ce->ring->vma); |
Chris Wilson | 57e8853 | 2016-08-15 10:48:57 +0100 | [diff] [blame] | 278 | lrc->ring_end = lrc->ring_begin + ce->ring->size - 1; |
| 279 | lrc->ring_next_free_location = lrc->ring_begin; |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 280 | lrc->ring_current_tail_pointer_value = 0; |
| 281 | |
Dave Gordon | c18468c | 2016-08-09 15:19:22 +0100 | [diff] [blame] | 282 | desc.engines_used |= (1 << guc_engine_id); |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Dave Gordon | e02757d | 2016-08-09 15:19:21 +0100 | [diff] [blame] | 285 | DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n", |
| 286 | client->engines, desc.engines_used); |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 287 | WARN_ON(desc.engines_used == 0); |
| 288 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 289 | /* |
Dave Gordon | 86e06cc | 2016-04-19 16:08:36 +0100 | [diff] [blame] | 290 | * The doorbell, process descriptor, and workqueue are all parts |
| 291 | * of the client object, which the GuC will reference via the GGTT |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 292 | */ |
Chris Wilson | bde13eb | 2016-08-15 10:49:07 +0100 | [diff] [blame] | 293 | gfx_addr = i915_ggtt_offset(client->vma); |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 294 | desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) + |
Dave Gordon | 86e06cc | 2016-04-19 16:08:36 +0100 | [diff] [blame] | 295 | client->doorbell_offset; |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 296 | desc.db_trigger_cpu = |
| 297 | (uintptr_t)client->vaddr + client->doorbell_offset; |
Dave Gordon | 86e06cc | 2016-04-19 16:08:36 +0100 | [diff] [blame] | 298 | 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 Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 301 | desc.wq_size = client->wq_size; |
| 302 | |
| 303 | /* |
Chris Wilson | e2efd13 | 2016-05-24 14:53:34 +0100 | [diff] [blame] | 304 | * XXX: Take LRCs from an existing context if this is not an |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 305 | * IsKMDCreatedContext client |
| 306 | */ |
| 307 | desc.desc_private = (uintptr_t)client; |
| 308 | |
| 309 | /* Pool context is pinned already */ |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 310 | sg = guc->ctx_pool_vma->pages; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 311 | sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc), |
| 312 | sizeof(desc) * client->ctx_index); |
| 313 | } |
| 314 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 315 | static void guc_ctx_desc_fini(struct intel_guc *guc, |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 316 | 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 Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 323 | sg = guc->ctx_pool_vma->pages; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 324 | sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc), |
| 325 | sizeof(desc) * client->ctx_index); |
| 326 | } |
| 327 | |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 328 | /** |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 329 | * i915_guc_wq_reserve() - reserve space in the GuC's workqueue |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 330 | * @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 Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 337 | * of 0 has been returned, it must be balanced by a corresponding |
| 338 | * call to submit(). |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 339 | * |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 340 | * Reservation allows the caller to determine in advance that space |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 341 | * will be available for the next submission before committing resources |
| 342 | * to it, and helps avoid late failures with complicated recovery paths. |
| 343 | */ |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 344 | int i915_guc_wq_reserve(struct drm_i915_gem_request *request) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 345 | { |
Dave Gordon | 551aaec | 2016-05-13 15:36:33 +0100 | [diff] [blame] | 346 | const size_t wqi_size = sizeof(struct guc_wq_item); |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 347 | struct i915_guc_client *gc = request->i915->guc.execbuf_client; |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 348 | struct guc_process_desc *desc = gc->vaddr + gc->proc_desc_offset; |
Dave Gordon | 551aaec | 2016-05-13 15:36:33 +0100 | [diff] [blame] | 349 | u32 freespace; |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 350 | int ret; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 351 | |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 352 | spin_lock(&gc->wq_lock); |
Dave Gordon | 551aaec | 2016-05-13 15:36:33 +0100 | [diff] [blame] | 353 | freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size); |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 354 | freespace -= gc->wq_rsvd; |
| 355 | if (likely(freespace >= wqi_size)) { |
| 356 | gc->wq_rsvd += wqi_size; |
| 357 | ret = 0; |
| 358 | } else { |
| 359 | gc->no_wq_space++; |
| 360 | ret = -EAGAIN; |
| 361 | } |
| 362 | spin_unlock(&gc->wq_lock); |
Alex Dai | 5a84330 | 2015-12-02 16:56:29 -0800 | [diff] [blame] | 363 | |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 364 | return ret; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Chris Wilson | 5ba8990 | 2016-10-07 07:53:27 +0100 | [diff] [blame] | 367 | void i915_guc_wq_unreserve(struct drm_i915_gem_request *request) |
| 368 | { |
| 369 | const size_t wqi_size = sizeof(struct guc_wq_item); |
| 370 | struct i915_guc_client *gc = request->i915->guc.execbuf_client; |
| 371 | |
| 372 | GEM_BUG_ON(READ_ONCE(gc->wq_rsvd) < wqi_size); |
| 373 | |
| 374 | spin_lock(&gc->wq_lock); |
| 375 | gc->wq_rsvd -= wqi_size; |
| 376 | spin_unlock(&gc->wq_lock); |
| 377 | } |
| 378 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 379 | /* Construct a Work Item and append it to the GuC's Work Queue */ |
| 380 | static void guc_wq_item_append(struct i915_guc_client *gc, |
| 381 | struct drm_i915_gem_request *rq) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 382 | { |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 383 | /* wqi_len is in DWords, and does not include the one-word header */ |
| 384 | const size_t wqi_size = sizeof(struct guc_wq_item); |
| 385 | const u32 wqi_len = wqi_size/sizeof(u32) - 1; |
Dave Gordon | c18468c | 2016-08-09 15:19:22 +0100 | [diff] [blame] | 386 | struct intel_engine_cs *engine = rq->engine; |
Alex Dai | a5916e8 | 2016-04-19 16:08:35 +0100 | [diff] [blame] | 387 | struct guc_process_desc *desc; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 388 | struct guc_wq_item *wqi; |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 389 | u32 freespace, tail, wq_off; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 390 | |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 391 | desc = gc->vaddr + gc->proc_desc_offset; |
Alex Dai | a7e0219 | 2015-12-16 11:45:55 -0800 | [diff] [blame] | 392 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 393 | /* Free space is guaranteed, see i915_guc_wq_reserve() above */ |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 394 | freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size); |
| 395 | GEM_BUG_ON(freespace < wqi_size); |
| 396 | |
| 397 | /* The GuC firmware wants the tail index in QWords, not bytes */ |
| 398 | tail = rq->tail; |
| 399 | GEM_BUG_ON(tail & 7); |
| 400 | tail >>= 3; |
| 401 | GEM_BUG_ON(tail > WQ_RING_TAIL_MAX); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 402 | |
| 403 | /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we |
| 404 | * should not have the case where structure wqi is across page, neither |
| 405 | * wrapped to the beginning. This simplifies the implementation below. |
| 406 | * |
| 407 | * XXX: if not the case, we need save data to a temp wqi and copy it to |
| 408 | * workqueue buffer dw by dw. |
| 409 | */ |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 410 | BUILD_BUG_ON(wqi_size != 16); |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 411 | GEM_BUG_ON(gc->wq_rsvd < wqi_size); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 412 | |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 413 | /* postincrement WQ tail for next time */ |
| 414 | wq_off = gc->wq_tail; |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 415 | GEM_BUG_ON(wq_off & (wqi_size - 1)); |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 416 | gc->wq_tail += wqi_size; |
| 417 | gc->wq_tail &= gc->wq_size - 1; |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 418 | gc->wq_rsvd -= wqi_size; |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 419 | |
| 420 | /* WQ starts from the page after doorbell / process_desc */ |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 421 | wqi = gc->vaddr + wq_off + GUC_DB_SIZE; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 422 | |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 423 | /* Now fill in the 4-word work queue item */ |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 424 | wqi->header = WQ_TYPE_INORDER | |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 425 | (wqi_len << WQ_LEN_SHIFT) | |
Dave Gordon | c18468c | 2016-08-09 15:19:22 +0100 | [diff] [blame] | 426 | (engine->guc_id << WQ_TARGET_SHIFT) | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 427 | WQ_NO_WCFLUSH_WAIT; |
| 428 | |
| 429 | /* The GuC wants only the low-order word of the context descriptor */ |
Dave Gordon | c18468c | 2016-08-09 15:19:22 +0100 | [diff] [blame] | 430 | wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 431 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 432 | wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT; |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 433 | wqi->fence_id = rq->global_seqno; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Dave Gordon | 10d2c3e | 2016-06-13 17:57:31 +0100 | [diff] [blame] | 436 | static int guc_ring_doorbell(struct i915_guc_client *gc) |
| 437 | { |
| 438 | struct guc_process_desc *desc; |
| 439 | union guc_doorbell_qw db_cmp, db_exc, db_ret; |
| 440 | union guc_doorbell_qw *db; |
| 441 | int attempt = 2, ret = -EAGAIN; |
| 442 | |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 443 | desc = gc->vaddr + gc->proc_desc_offset; |
Dave Gordon | 10d2c3e | 2016-06-13 17:57:31 +0100 | [diff] [blame] | 444 | |
| 445 | /* Update the tail so it is visible to GuC */ |
| 446 | desc->tail = gc->wq_tail; |
| 447 | |
| 448 | /* current cookie */ |
| 449 | db_cmp.db_status = GUC_DOORBELL_ENABLED; |
Chris Wilson | 357248b | 2016-11-29 12:10:21 +0000 | [diff] [blame] | 450 | db_cmp.cookie = gc->doorbell_cookie; |
Dave Gordon | 10d2c3e | 2016-06-13 17:57:31 +0100 | [diff] [blame] | 451 | |
| 452 | /* cookie to be updated */ |
| 453 | db_exc.db_status = GUC_DOORBELL_ENABLED; |
Chris Wilson | 357248b | 2016-11-29 12:10:21 +0000 | [diff] [blame] | 454 | db_exc.cookie = gc->doorbell_cookie + 1; |
Dave Gordon | 10d2c3e | 2016-06-13 17:57:31 +0100 | [diff] [blame] | 455 | if (db_exc.cookie == 0) |
| 456 | db_exc.cookie = 1; |
| 457 | |
| 458 | /* pointer of current doorbell cacheline */ |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 459 | db = gc->vaddr + gc->doorbell_offset; |
Dave Gordon | 10d2c3e | 2016-06-13 17:57:31 +0100 | [diff] [blame] | 460 | |
| 461 | while (attempt--) { |
| 462 | /* lets ring the doorbell */ |
| 463 | db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db, |
| 464 | db_cmp.value_qw, db_exc.value_qw); |
| 465 | |
| 466 | /* if the exchange was successfully executed */ |
| 467 | if (db_ret.value_qw == db_cmp.value_qw) { |
| 468 | /* db was successfully rung */ |
Chris Wilson | 357248b | 2016-11-29 12:10:21 +0000 | [diff] [blame] | 469 | gc->doorbell_cookie = db_exc.cookie; |
Dave Gordon | 10d2c3e | 2016-06-13 17:57:31 +0100 | [diff] [blame] | 470 | ret = 0; |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | /* XXX: doorbell was lost and need to acquire it again */ |
| 475 | if (db_ret.db_status == GUC_DOORBELL_DISABLED) |
| 476 | break; |
| 477 | |
Dave Gordon | 535b2f5 | 2016-08-18 18:17:23 +0100 | [diff] [blame] | 478 | DRM_WARN("Cookie mismatch. Expected %d, found %d\n", |
| 479 | db_cmp.cookie, db_ret.cookie); |
Dave Gordon | 10d2c3e | 2016-06-13 17:57:31 +0100 | [diff] [blame] | 480 | |
| 481 | /* update the cookie to newly read cookie from GuC */ |
| 482 | db_cmp.cookie = db_ret.cookie; |
| 483 | db_exc.cookie = db_ret.cookie + 1; |
| 484 | if (db_exc.cookie == 0) |
| 485 | db_exc.cookie = 1; |
| 486 | } |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 491 | /** |
Chris Wilson | 34ba5a8 | 2016-11-29 12:10:24 +0000 | [diff] [blame^] | 492 | * __i915_guc_submit() - Submit commands through GuC |
Alex Dai | feda33e | 2015-10-19 16:10:54 -0700 | [diff] [blame] | 493 | * @rq: request associated with the commands |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 494 | * |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 495 | * The caller must have already called i915_guc_wq_reserve() above with |
| 496 | * a result of 0 (success), guaranteeing that there is space in the work |
| 497 | * queue for the new request, so enqueuing the item cannot fail. |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 498 | * |
| 499 | * Bad Things Will Happen if the caller violates this protocol e.g. calls |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 500 | * submit() when _reserve() says there's no space, or calls _submit() |
| 501 | * a different number of times from (successful) calls to _reserve(). |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 502 | * |
| 503 | * The only error here arises if the doorbell hardware isn't functioning |
| 504 | * as expected, which really shouln't happen. |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 505 | */ |
Chris Wilson | 34ba5a8 | 2016-11-29 12:10:24 +0000 | [diff] [blame^] | 506 | static void __i915_guc_submit(struct drm_i915_gem_request *rq) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 507 | { |
Akash Goel | ed4596ea | 2016-10-25 22:05:23 +0530 | [diff] [blame] | 508 | struct drm_i915_private *dev_priv = rq->i915; |
Chris Wilson | d55ac5b | 2016-11-14 20:40:59 +0000 | [diff] [blame] | 509 | struct intel_engine_cs *engine = rq->engine; |
| 510 | unsigned int engine_id = engine->id; |
Dave Gordon | 7c2c270 | 2016-05-13 15:36:32 +0100 | [diff] [blame] | 511 | struct intel_guc *guc = &rq->i915->guc; |
| 512 | struct i915_guc_client *client = guc->execbuf_client; |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 513 | int b_ret; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 514 | |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 515 | spin_lock(&client->wq_lock); |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 516 | guc_wq_item_append(client, rq); |
Akash Goel | ed4596ea | 2016-10-25 22:05:23 +0530 | [diff] [blame] | 517 | |
| 518 | /* WA to flush out the pending GMADR writes to ring buffer. */ |
| 519 | if (i915_vma_is_map_and_fenceable(rq->ring->vma)) |
| 520 | POSTING_READ_FW(GUC_STATUS); |
| 521 | |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 522 | b_ret = guc_ring_doorbell(client); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 523 | |
Alex Dai | 397097b | 2016-01-23 11:58:14 -0800 | [diff] [blame] | 524 | client->submissions[engine_id] += 1; |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 525 | client->retcode = b_ret; |
| 526 | if (b_ret) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 527 | client->b_fail += 1; |
Dave Gordon | 0a31afb | 2016-05-13 15:36:34 +0100 | [diff] [blame] | 528 | |
Alex Dai | 397097b | 2016-01-23 11:58:14 -0800 | [diff] [blame] | 529 | guc->submissions[engine_id] += 1; |
Chris Wilson | 65e4760 | 2016-10-28 13:58:49 +0100 | [diff] [blame] | 530 | guc->last_seqno[engine_id] = rq->global_seqno; |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 531 | spin_unlock(&client->wq_lock); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Chris Wilson | 34ba5a8 | 2016-11-29 12:10:24 +0000 | [diff] [blame^] | 534 | static void i915_guc_submit(struct drm_i915_gem_request *rq) |
| 535 | { |
| 536 | struct intel_engine_cs *engine = rq->engine; |
| 537 | |
| 538 | /* We keep the previous context alive until we retire the following |
| 539 | * request. This ensures that any the context object is still pinned |
| 540 | * for any residual writes the HW makes into it on the context switch |
| 541 | * into the next object following the breadcrumb. Otherwise, we may |
| 542 | * retire the context too early. |
| 543 | */ |
| 544 | rq->previous_context = engine->last_context; |
| 545 | engine->last_context = rq->ctx; |
| 546 | |
| 547 | i915_gem_request_submit(rq); |
| 548 | __i915_guc_submit(rq); |
| 549 | } |
| 550 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 551 | /* |
| 552 | * Everything below here is concerned with setup & teardown, and is |
| 553 | * therefore not part of the somewhat time-critical batch-submission |
| 554 | * path of i915_guc_submit() above. |
| 555 | */ |
| 556 | |
| 557 | /** |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 558 | * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage |
| 559 | * @guc: the guc |
| 560 | * @size: size of area to allocate (both virtual space and memory) |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 561 | * |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 562 | * This is a wrapper to create an object for use with the GuC. In order to |
| 563 | * use it inside the GuC, an object needs to be pinned lifetime, so we allocate |
| 564 | * both some backing storage and a range inside the Global GTT. We must pin |
| 565 | * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that |
| 566 | * range is reserved inside GuC. |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 567 | * |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 568 | * Return: A i915_vma if successful, otherwise an ERR_PTR. |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 569 | */ |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 570 | static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size) |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 571 | { |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 572 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 573 | struct drm_i915_gem_object *obj; |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 574 | struct i915_vma *vma; |
| 575 | int ret; |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 576 | |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 577 | obj = i915_gem_object_create(&dev_priv->drm, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 578 | if (IS_ERR(obj)) |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 579 | return ERR_CAST(obj); |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 580 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 581 | vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL); |
| 582 | if (IS_ERR(vma)) |
| 583 | goto err; |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 584 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 585 | ret = i915_vma_pin(vma, 0, PAGE_SIZE, |
| 586 | PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP); |
| 587 | if (ret) { |
| 588 | vma = ERR_PTR(ret); |
| 589 | goto err; |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */ |
| 593 | I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE); |
| 594 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 595 | return vma; |
| 596 | |
| 597 | err: |
| 598 | i915_gem_object_put(obj); |
| 599 | return vma; |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Dave Gordon | 0daf556 | 2016-06-10 18:29:25 +0100 | [diff] [blame] | 602 | static void |
| 603 | guc_client_free(struct drm_i915_private *dev_priv, |
| 604 | struct i915_guc_client *client) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 605 | { |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 606 | struct intel_guc *guc = &dev_priv->guc; |
| 607 | |
| 608 | if (!client) |
| 609 | return; |
| 610 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 611 | /* |
| 612 | * XXX: wait for any outstanding submissions before freeing memory. |
| 613 | * Be sure to drop any locks |
| 614 | */ |
| 615 | |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 616 | if (client->vaddr) { |
Dave Gordon | 0d92a6a | 2016-04-19 16:08:34 +0100 | [diff] [blame] | 617 | /* |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 618 | * If we got as far as setting up a doorbell, make sure we |
| 619 | * shut it down before unmapping & deallocating the memory. |
Dave Gordon | 0d92a6a | 2016-04-19 16:08:34 +0100 | [diff] [blame] | 620 | */ |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 621 | guc_disable_doorbell(guc, client); |
Dave Gordon | 0d92a6a | 2016-04-19 16:08:34 +0100 | [diff] [blame] | 622 | |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 623 | i915_gem_object_unpin_map(client->vma->obj); |
Dave Gordon | 0d92a6a | 2016-04-19 16:08:34 +0100 | [diff] [blame] | 624 | } |
| 625 | |
Chris Wilson | 19880c4 | 2016-08-15 10:49:05 +0100 | [diff] [blame] | 626 | i915_vma_unpin_and_release(&client->vma); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 627 | |
| 628 | if (client->ctx_index != GUC_INVALID_CTX_ID) { |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 629 | guc_ctx_desc_fini(guc, client); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 630 | ida_simple_remove(&guc->ctx_ids, client->ctx_index); |
| 631 | } |
| 632 | |
| 633 | kfree(client); |
| 634 | } |
| 635 | |
Dave Gordon | 84b7f88 | 2016-08-09 15:19:20 +0100 | [diff] [blame] | 636 | /* Check that a doorbell register is in the expected state */ |
| 637 | static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id) |
| 638 | { |
| 639 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
| 640 | i915_reg_t drbreg = GEN8_DRBREGL(db_id); |
| 641 | uint32_t value = I915_READ(drbreg); |
| 642 | bool enabled = (value & GUC_DOORBELL_ENABLED) != 0; |
| 643 | bool expected = test_bit(db_id, guc->doorbell_bitmap); |
| 644 | |
| 645 | if (enabled == expected) |
| 646 | return true; |
| 647 | |
| 648 | DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n", |
| 649 | db_id, drbreg.reg, value, |
| 650 | expected ? "active" : "inactive"); |
| 651 | |
| 652 | return false; |
| 653 | } |
| 654 | |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 655 | /* |
Dave Gordon | 8888cd0 | 2016-08-09 15:19:19 +0100 | [diff] [blame] | 656 | * Borrow the first client to set up & tear down each unused doorbell |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 657 | * in turn, to ensure that all doorbell h/w is (re)initialised. |
| 658 | */ |
| 659 | static void guc_init_doorbell_hw(struct intel_guc *guc) |
| 660 | { |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 661 | struct i915_guc_client *client = guc->execbuf_client; |
Dave Gordon | 84b7f88 | 2016-08-09 15:19:20 +0100 | [diff] [blame] | 662 | uint16_t db_id; |
| 663 | int i, err; |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 664 | |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 665 | guc_disable_doorbell(guc, client); |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 666 | |
| 667 | for (i = 0; i < GUC_MAX_DOORBELLS; ++i) { |
Dave Gordon | 84b7f88 | 2016-08-09 15:19:20 +0100 | [diff] [blame] | 668 | /* Skip if doorbell is OK */ |
| 669 | if (guc_doorbell_check(guc, i)) |
Dave Gordon | 8888cd0 | 2016-08-09 15:19:19 +0100 | [diff] [blame] | 670 | continue; |
| 671 | |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 672 | err = guc_update_doorbell_id(guc, client, i); |
Dave Gordon | 84b7f88 | 2016-08-09 15:19:20 +0100 | [diff] [blame] | 673 | if (err) |
| 674 | DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n", |
| 675 | i, err); |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 678 | db_id = select_doorbell_register(guc, client->priority); |
| 679 | WARN_ON(db_id == GUC_INVALID_DOORBELL_ID); |
| 680 | |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 681 | err = guc_update_doorbell_id(guc, client, db_id); |
| 682 | if (err) |
Dave Gordon | 535b2f5 | 2016-08-18 18:17:23 +0100 | [diff] [blame] | 683 | DRM_WARN("Failed to restore doorbell to %d, err %d\n", |
| 684 | db_id, err); |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 685 | |
Dave Gordon | 84b7f88 | 2016-08-09 15:19:20 +0100 | [diff] [blame] | 686 | /* Read back & verify all doorbell registers */ |
| 687 | for (i = 0; i < GUC_MAX_DOORBELLS; ++i) |
| 688 | (void)guc_doorbell_check(guc, i); |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 691 | /** |
| 692 | * guc_client_alloc() - Allocate an i915_guc_client |
Dave Gordon | 0daf556 | 2016-06-10 18:29:25 +0100 | [diff] [blame] | 693 | * @dev_priv: driver private data structure |
Chris Wilson | ceae531 | 2016-08-17 13:42:42 +0100 | [diff] [blame] | 694 | * @engines: The set of engines to enable for this client |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 695 | * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW |
| 696 | * The kernel client to replace ExecList submission is created with |
| 697 | * NORMAL priority. Priority of a client for scheduler can be HIGH, |
| 698 | * while a preemption context can use CRITICAL. |
Alex Dai | feda33e | 2015-10-19 16:10:54 -0700 | [diff] [blame] | 699 | * @ctx: the context that owns the client (we use the default render |
| 700 | * context) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 701 | * |
Dave Gordon | 0d92a6a | 2016-04-19 16:08:34 +0100 | [diff] [blame] | 702 | * Return: An i915_guc_client object if success, else NULL. |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 703 | */ |
Dave Gordon | 0daf556 | 2016-06-10 18:29:25 +0100 | [diff] [blame] | 704 | static struct i915_guc_client * |
| 705 | guc_client_alloc(struct drm_i915_private *dev_priv, |
Dave Gordon | e02757d | 2016-08-09 15:19:21 +0100 | [diff] [blame] | 706 | uint32_t engines, |
Dave Gordon | 0daf556 | 2016-06-10 18:29:25 +0100 | [diff] [blame] | 707 | uint32_t priority, |
| 708 | struct i915_gem_context *ctx) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 709 | { |
| 710 | struct i915_guc_client *client; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 711 | struct intel_guc *guc = &dev_priv->guc; |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 712 | struct i915_vma *vma; |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 713 | void *vaddr; |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 714 | uint16_t db_id; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 715 | |
| 716 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
| 717 | if (!client) |
| 718 | return NULL; |
| 719 | |
Alex Dai | d167519 | 2015-08-12 15:43:43 +0100 | [diff] [blame] | 720 | client->owner = ctx; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 721 | client->guc = guc; |
Dave Gordon | e02757d | 2016-08-09 15:19:21 +0100 | [diff] [blame] | 722 | client->engines = engines; |
| 723 | client->priority = priority; |
| 724 | client->doorbell_id = GUC_INVALID_DOORBELL_ID; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 725 | |
| 726 | client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0, |
| 727 | GUC_MAX_GPU_CONTEXTS, GFP_KERNEL); |
| 728 | if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) { |
| 729 | client->ctx_index = GUC_INVALID_CTX_ID; |
| 730 | goto err; |
| 731 | } |
| 732 | |
| 733 | /* The first page is doorbell/proc_desc. Two followed pages are wq. */ |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 734 | vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE); |
| 735 | if (IS_ERR(vma)) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 736 | goto err; |
| 737 | |
Dave Gordon | 0d92a6a | 2016-04-19 16:08:34 +0100 | [diff] [blame] | 738 | /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */ |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 739 | client->vma = vma; |
Chris Wilson | 72aa0d8 | 2016-11-02 17:50:47 +0000 | [diff] [blame] | 740 | |
| 741 | vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB); |
| 742 | if (IS_ERR(vaddr)) |
| 743 | goto err; |
| 744 | |
| 745 | client->vaddr = vaddr; |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 746 | |
| 747 | spin_lock_init(&client->wq_lock); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 748 | client->wq_offset = GUC_DB_SIZE; |
| 749 | client->wq_size = GUC_WQ_SIZE; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 750 | |
Dave Gordon | f10d69a | 2016-06-13 17:57:33 +0100 | [diff] [blame] | 751 | db_id = select_doorbell_register(guc, client->priority); |
| 752 | if (db_id == GUC_INVALID_DOORBELL_ID) |
| 753 | /* XXX: evict a doorbell instead? */ |
| 754 | goto err; |
| 755 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 756 | client->doorbell_offset = select_doorbell_cacheline(guc); |
| 757 | |
| 758 | /* |
| 759 | * Since the doorbell only requires a single cacheline, we can save |
| 760 | * space by putting the application process descriptor in the same |
| 761 | * page. Use the half of the page that doesn't include the doorbell. |
| 762 | */ |
| 763 | if (client->doorbell_offset >= (GUC_DB_SIZE / 2)) |
| 764 | client->proc_desc_offset = 0; |
| 765 | else |
| 766 | client->proc_desc_offset = (GUC_DB_SIZE / 2); |
| 767 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 768 | guc_proc_desc_init(guc, client); |
| 769 | guc_ctx_desc_init(guc, client); |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 770 | |
| 771 | /* For runtime client allocation we need to enable the doorbell. Not |
| 772 | * required yet for the static execbuf_client as this special kernel |
| 773 | * client is enabled from i915_guc_submission_enable(). |
| 774 | * |
| 775 | * guc_update_doorbell_id(guc, client, db_id); |
| 776 | */ |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 777 | |
Dave Gordon | e02757d | 2016-08-09 15:19:21 +0100 | [diff] [blame] | 778 | DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n", |
| 779 | priority, client, client->engines, client->ctx_index); |
Dave Gordon | a667429 | 2016-06-13 17:57:32 +0100 | [diff] [blame] | 780 | DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n", |
| 781 | client->doorbell_id, client->doorbell_offset); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 782 | |
| 783 | return client; |
| 784 | |
| 785 | err: |
Dave Gordon | 0daf556 | 2016-06-10 18:29:25 +0100 | [diff] [blame] | 786 | guc_client_free(dev_priv, client); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 787 | return NULL; |
| 788 | } |
| 789 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 790 | /* |
| 791 | * Sub buffer switch callback. Called whenever relay has to switch to a new |
| 792 | * sub buffer, relay stays on the same sub buffer if 0 is returned. |
| 793 | */ |
| 794 | static int subbuf_start_callback(struct rchan_buf *buf, |
| 795 | void *subbuf, |
| 796 | void *prev_subbuf, |
| 797 | size_t prev_padding) |
| 798 | { |
| 799 | /* Use no-overwrite mode by default, where relay will stop accepting |
| 800 | * new data if there are no empty sub buffers left. |
| 801 | * There is no strict synchronization enforced by relay between Consumer |
| 802 | * and Producer. In overwrite mode, there is a possibility of getting |
| 803 | * inconsistent/garbled data, the producer could be writing on to the |
| 804 | * same sub buffer from which Consumer is reading. This can't be avoided |
| 805 | * unless Consumer is fast enough and can always run in tandem with |
| 806 | * Producer. |
| 807 | */ |
| 808 | if (relay_buf_full(buf)) |
| 809 | return 0; |
| 810 | |
| 811 | return 1; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * file_create() callback. Creates relay file in debugfs. |
| 816 | */ |
| 817 | static struct dentry *create_buf_file_callback(const char *filename, |
| 818 | struct dentry *parent, |
| 819 | umode_t mode, |
| 820 | struct rchan_buf *buf, |
| 821 | int *is_global) |
| 822 | { |
| 823 | struct dentry *buf_file; |
| 824 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 825 | /* This to enable the use of a single buffer for the relay channel and |
| 826 | * correspondingly have a single file exposed to User, through which |
| 827 | * it can collect the logs in order without any post-processing. |
Akash Goel | 1e6b8b0 | 2016-10-12 21:54:43 +0530 | [diff] [blame] | 828 | * Need to set 'is_global' even if parent is NULL for early logging. |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 829 | */ |
| 830 | *is_global = 1; |
| 831 | |
Akash Goel | 1e6b8b0 | 2016-10-12 21:54:43 +0530 | [diff] [blame] | 832 | if (!parent) |
| 833 | return NULL; |
| 834 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 835 | /* Not using the channel filename passed as an argument, since for each |
| 836 | * channel relay appends the corresponding CPU number to the filename |
| 837 | * passed in relay_open(). This should be fine as relay just needs a |
| 838 | * dentry of the file associated with the channel buffer and that file's |
| 839 | * name need not be same as the filename passed as an argument. |
| 840 | */ |
| 841 | buf_file = debugfs_create_file("guc_log", mode, |
| 842 | parent, buf, &relay_file_operations); |
| 843 | return buf_file; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * file_remove() default callback. Removes relay file in debugfs. |
| 848 | */ |
| 849 | static int remove_buf_file_callback(struct dentry *dentry) |
| 850 | { |
| 851 | debugfs_remove(dentry); |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | /* relay channel callbacks */ |
| 856 | static struct rchan_callbacks relay_callbacks = { |
| 857 | .subbuf_start = subbuf_start_callback, |
| 858 | .create_buf_file = create_buf_file_callback, |
| 859 | .remove_buf_file = remove_buf_file_callback, |
| 860 | }; |
| 861 | |
| 862 | static void guc_log_remove_relay_file(struct intel_guc *guc) |
| 863 | { |
| 864 | relay_close(guc->log.relay_chan); |
| 865 | } |
| 866 | |
Akash Goel | 1e6b8b0 | 2016-10-12 21:54:43 +0530 | [diff] [blame] | 867 | static int guc_log_create_relay_channel(struct intel_guc *guc) |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 868 | { |
| 869 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
| 870 | struct rchan *guc_log_relay_chan; |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 871 | size_t n_subbufs, subbuf_size; |
| 872 | |
Akash Goel | 1e6b8b0 | 2016-10-12 21:54:43 +0530 | [diff] [blame] | 873 | /* Keep the size of sub buffers same as shared log buffer */ |
| 874 | subbuf_size = guc->log.vma->obj->base.size; |
| 875 | |
| 876 | /* Store up to 8 snapshots, which is large enough to buffer sufficient |
| 877 | * boot time logs and provides enough leeway to User, in terms of |
| 878 | * latency, for consuming the logs from relay. Also doesn't take |
| 879 | * up too much memory. |
| 880 | */ |
| 881 | n_subbufs = 8; |
| 882 | |
| 883 | guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size, |
| 884 | n_subbufs, &relay_callbacks, dev_priv); |
| 885 | if (!guc_log_relay_chan) { |
| 886 | DRM_ERROR("Couldn't create relay chan for GuC logging\n"); |
| 887 | return -ENOMEM; |
| 888 | } |
| 889 | |
| 890 | GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size); |
| 891 | guc->log.relay_chan = guc_log_relay_chan; |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static int guc_log_create_relay_file(struct intel_guc *guc) |
| 896 | { |
| 897 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
| 898 | struct dentry *log_dir; |
| 899 | int ret; |
| 900 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 901 | /* For now create the log file in /sys/kernel/debug/dri/0 dir */ |
| 902 | log_dir = dev_priv->drm.primary->debugfs_root; |
| 903 | |
| 904 | /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is |
| 905 | * not mounted and so can't create the relay file. |
| 906 | * The relay API seems to fit well with debugfs only, for availing relay |
| 907 | * there are 3 requirements which can be met for debugfs file only in a |
| 908 | * straightforward/clean manner :- |
| 909 | * i) Need the associated dentry pointer of the file, while opening the |
| 910 | * relay channel. |
| 911 | * ii) Should be able to use 'relay_file_operations' fops for the file. |
| 912 | * iii) Set the 'i_private' field of file's inode to the pointer of |
| 913 | * relay channel buffer. |
| 914 | */ |
| 915 | if (!log_dir) { |
| 916 | DRM_ERROR("Debugfs dir not available yet for GuC log file\n"); |
| 917 | return -ENODEV; |
| 918 | } |
| 919 | |
Akash Goel | 1e6b8b0 | 2016-10-12 21:54:43 +0530 | [diff] [blame] | 920 | ret = relay_late_setup_files(guc->log.relay_chan, "guc_log", log_dir); |
| 921 | if (ret) { |
| 922 | DRM_ERROR("Couldn't associate relay chan with file %d\n", ret); |
| 923 | return ret; |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 924 | } |
| 925 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 929 | static void guc_move_to_next_buf(struct intel_guc *guc) |
| 930 | { |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 931 | /* Make sure the updates made in the sub buffer are visible when |
| 932 | * Consumer sees the following update to offset inside the sub buffer. |
| 933 | */ |
| 934 | smp_wmb(); |
| 935 | |
| 936 | /* All data has been written, so now move the offset of sub buffer. */ |
| 937 | relay_reserve(guc->log.relay_chan, guc->log.vma->obj->base.size); |
| 938 | |
| 939 | /* Switch to the next sub buffer */ |
| 940 | relay_flush(guc->log.relay_chan); |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static void *guc_get_write_buffer(struct intel_guc *guc) |
| 944 | { |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 945 | if (!guc->log.relay_chan) |
| 946 | return NULL; |
| 947 | |
| 948 | /* Just get the base address of a new sub buffer and copy data into it |
| 949 | * ourselves. NULL will be returned in no-overwrite mode, if all sub |
| 950 | * buffers are full. Could have used the relay_write() to indirectly |
| 951 | * copy the data, but that would have been bit convoluted, as we need to |
| 952 | * write to only certain locations inside a sub buffer which cannot be |
| 953 | * done without using relay_reserve() along with relay_write(). So its |
| 954 | * better to use relay_reserve() alone. |
| 955 | */ |
| 956 | return relay_reserve(guc->log.relay_chan, 0); |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 957 | } |
| 958 | |
Akash Goel | 5aa1ee4 | 2016-10-12 21:54:36 +0530 | [diff] [blame] | 959 | static bool |
| 960 | guc_check_log_buf_overflow(struct intel_guc *guc, |
| 961 | enum guc_log_buffer_type type, unsigned int full_cnt) |
| 962 | { |
| 963 | unsigned int prev_full_cnt = guc->log.prev_overflow_count[type]; |
| 964 | bool overflow = false; |
| 965 | |
| 966 | if (full_cnt != prev_full_cnt) { |
| 967 | overflow = true; |
| 968 | |
| 969 | guc->log.prev_overflow_count[type] = full_cnt; |
| 970 | guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt; |
| 971 | |
| 972 | if (full_cnt < prev_full_cnt) { |
| 973 | /* buffer_full_cnt is a 4 bit counter */ |
| 974 | guc->log.total_overflow_count[type] += 16; |
| 975 | } |
| 976 | DRM_ERROR_RATELIMITED("GuC log buffer overflow\n"); |
| 977 | } |
| 978 | |
| 979 | return overflow; |
| 980 | } |
| 981 | |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 982 | static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type) |
| 983 | { |
| 984 | switch (type) { |
| 985 | case GUC_ISR_LOG_BUFFER: |
| 986 | return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE; |
| 987 | case GUC_DPC_LOG_BUFFER: |
| 988 | return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE; |
| 989 | case GUC_CRASH_DUMP_LOG_BUFFER: |
| 990 | return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE; |
| 991 | default: |
| 992 | MISSING_CASE(type); |
| 993 | } |
| 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static void guc_read_update_log_buffer(struct intel_guc *guc) |
| 999 | { |
Akash Goel | 6941f3c | 2016-10-12 21:54:37 +0530 | [diff] [blame] | 1000 | unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt; |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1001 | struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state; |
| 1002 | struct guc_log_buffer_state log_buf_state_local; |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1003 | enum guc_log_buffer_type type; |
| 1004 | void *src_data, *dst_data; |
Akash Goel | 6941f3c | 2016-10-12 21:54:37 +0530 | [diff] [blame] | 1005 | bool new_overflow; |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1006 | |
| 1007 | if (WARN_ON(!guc->log.buf_addr)) |
| 1008 | return; |
| 1009 | |
| 1010 | /* Get the pointer to shared GuC log buffer */ |
| 1011 | log_buf_state = src_data = guc->log.buf_addr; |
| 1012 | |
| 1013 | /* Get the pointer to local buffer to store the logs */ |
| 1014 | log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc); |
| 1015 | |
| 1016 | /* Actual logs are present from the 2nd page */ |
| 1017 | src_data += PAGE_SIZE; |
| 1018 | dst_data += PAGE_SIZE; |
| 1019 | |
| 1020 | for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) { |
| 1021 | /* Make a copy of the state structure, inside GuC log buffer |
| 1022 | * (which is uncached mapped), on the stack to avoid reading |
| 1023 | * from it multiple times. |
| 1024 | */ |
| 1025 | memcpy(&log_buf_state_local, log_buf_state, |
| 1026 | sizeof(struct guc_log_buffer_state)); |
| 1027 | buffer_size = guc_get_log_buffer_size(type); |
Akash Goel | 6941f3c | 2016-10-12 21:54:37 +0530 | [diff] [blame] | 1028 | read_offset = log_buf_state_local.read_ptr; |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1029 | write_offset = log_buf_state_local.sampled_write_ptr; |
Akash Goel | 5aa1ee4 | 2016-10-12 21:54:36 +0530 | [diff] [blame] | 1030 | full_cnt = log_buf_state_local.buffer_full_cnt; |
| 1031 | |
| 1032 | /* Bookkeeping stuff */ |
| 1033 | guc->log.flush_count[type] += log_buf_state_local.flush_to_file; |
Akash Goel | 6941f3c | 2016-10-12 21:54:37 +0530 | [diff] [blame] | 1034 | new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt); |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1035 | |
| 1036 | /* Update the state of shared log buffer */ |
| 1037 | log_buf_state->read_ptr = write_offset; |
| 1038 | log_buf_state->flush_to_file = 0; |
| 1039 | log_buf_state++; |
| 1040 | |
| 1041 | if (unlikely(!log_buf_snapshot_state)) |
| 1042 | continue; |
| 1043 | |
| 1044 | /* First copy the state structure in snapshot buffer */ |
| 1045 | memcpy(log_buf_snapshot_state, &log_buf_state_local, |
| 1046 | sizeof(struct guc_log_buffer_state)); |
| 1047 | |
| 1048 | /* The write pointer could have been updated by GuC firmware, |
| 1049 | * after sending the flush interrupt to Host, for consistency |
| 1050 | * set write pointer value to same value of sampled_write_ptr |
| 1051 | * in the snapshot buffer. |
| 1052 | */ |
| 1053 | log_buf_snapshot_state->write_ptr = write_offset; |
| 1054 | log_buf_snapshot_state++; |
| 1055 | |
| 1056 | /* Now copy the actual logs. */ |
Akash Goel | 6941f3c | 2016-10-12 21:54:37 +0530 | [diff] [blame] | 1057 | if (unlikely(new_overflow)) { |
| 1058 | /* copy the whole buffer in case of overflow */ |
| 1059 | read_offset = 0; |
| 1060 | write_offset = buffer_size; |
| 1061 | } else if (unlikely((read_offset > buffer_size) || |
| 1062 | (write_offset > buffer_size))) { |
| 1063 | DRM_ERROR("invalid log buffer state\n"); |
| 1064 | /* copy whole buffer as offsets are unreliable */ |
| 1065 | read_offset = 0; |
| 1066 | write_offset = buffer_size; |
| 1067 | } |
| 1068 | |
| 1069 | /* Just copy the newly written data */ |
| 1070 | if (read_offset > write_offset) { |
Akash Goel | 7170659 | 2016-10-12 21:54:42 +0530 | [diff] [blame] | 1071 | i915_memcpy_from_wc(dst_data, src_data, write_offset); |
Akash Goel | 6941f3c | 2016-10-12 21:54:37 +0530 | [diff] [blame] | 1072 | bytes_to_copy = buffer_size - read_offset; |
| 1073 | } else { |
| 1074 | bytes_to_copy = write_offset - read_offset; |
| 1075 | } |
Akash Goel | 7170659 | 2016-10-12 21:54:42 +0530 | [diff] [blame] | 1076 | i915_memcpy_from_wc(dst_data + read_offset, |
| 1077 | src_data + read_offset, bytes_to_copy); |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1078 | |
| 1079 | src_data += buffer_size; |
| 1080 | dst_data += buffer_size; |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | if (log_buf_snapshot_state) |
| 1084 | guc_move_to_next_buf(guc); |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 1085 | else { |
| 1086 | /* Used rate limited to avoid deluge of messages, logs might be |
| 1087 | * getting consumed by User at a slow rate. |
| 1088 | */ |
| 1089 | DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n"); |
Akash Goel | 5aa1ee4 | 2016-10-12 21:54:36 +0530 | [diff] [blame] | 1090 | guc->log.capture_miss_count++; |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 1091 | } |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static void guc_capture_logs_work(struct work_struct *work) |
| 1095 | { |
| 1096 | struct drm_i915_private *dev_priv = |
| 1097 | container_of(work, struct drm_i915_private, guc.log.flush_work); |
| 1098 | |
| 1099 | i915_guc_capture_logs(dev_priv); |
| 1100 | } |
| 1101 | |
| 1102 | static void guc_log_cleanup(struct intel_guc *guc) |
| 1103 | { |
| 1104 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
| 1105 | |
| 1106 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 1107 | |
| 1108 | /* First disable the flush interrupt */ |
| 1109 | gen9_disable_guc_interrupts(dev_priv); |
| 1110 | |
| 1111 | if (guc->log.flush_wq) |
| 1112 | destroy_workqueue(guc->log.flush_wq); |
| 1113 | |
| 1114 | guc->log.flush_wq = NULL; |
| 1115 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 1116 | if (guc->log.relay_chan) |
| 1117 | guc_log_remove_relay_file(guc); |
| 1118 | |
| 1119 | guc->log.relay_chan = NULL; |
| 1120 | |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1121 | if (guc->log.buf_addr) |
| 1122 | i915_gem_object_unpin_map(guc->log.vma->obj); |
| 1123 | |
| 1124 | guc->log.buf_addr = NULL; |
| 1125 | } |
| 1126 | |
| 1127 | static int guc_log_create_extras(struct intel_guc *guc) |
| 1128 | { |
| 1129 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
| 1130 | void *vaddr; |
| 1131 | int ret; |
| 1132 | |
| 1133 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 1134 | |
| 1135 | /* Nothing to do */ |
| 1136 | if (i915.guc_log_level < 0) |
| 1137 | return 0; |
| 1138 | |
| 1139 | if (!guc->log.buf_addr) { |
Akash Goel | 7170659 | 2016-10-12 21:54:42 +0530 | [diff] [blame] | 1140 | /* Create a WC (Uncached for read) vmalloc mapping of log |
| 1141 | * buffer pages, so that we can directly get the data |
| 1142 | * (up-to-date) from memory. |
| 1143 | */ |
| 1144 | vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC); |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1145 | if (IS_ERR(vaddr)) { |
| 1146 | ret = PTR_ERR(vaddr); |
| 1147 | DRM_ERROR("Couldn't map log buffer pages %d\n", ret); |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | guc->log.buf_addr = vaddr; |
| 1152 | } |
| 1153 | |
Akash Goel | 1e6b8b0 | 2016-10-12 21:54:43 +0530 | [diff] [blame] | 1154 | if (!guc->log.relay_chan) { |
| 1155 | /* Create a relay channel, so that we have buffers for storing |
| 1156 | * the GuC firmware logs, the channel will be linked with a file |
| 1157 | * later on when debugfs is registered. |
| 1158 | */ |
| 1159 | ret = guc_log_create_relay_channel(guc); |
| 1160 | if (ret) |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1164 | if (!guc->log.flush_wq) { |
| 1165 | INIT_WORK(&guc->log.flush_work, guc_capture_logs_work); |
| 1166 | |
Akash Goel | 7ef54de | 2016-10-12 21:54:44 +0530 | [diff] [blame] | 1167 | /* |
| 1168 | * GuC log buffer flush work item has to do register access to |
| 1169 | * send the ack to GuC and this work item, if not synced before |
| 1170 | * suspend, can potentially get executed after the GFX device is |
| 1171 | * suspended. |
| 1172 | * By marking the WQ as freezable, we don't have to bother about |
| 1173 | * flushing of this work item from the suspend hooks, the pending |
| 1174 | * work item if any will be either executed before the suspend |
| 1175 | * or scheduled later on resume. This way the handling of work |
| 1176 | * item can be kept same between system suspend & rpm suspend. |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1177 | */ |
Akash Goel | 7ef54de | 2016-10-12 21:54:44 +0530 | [diff] [blame] | 1178 | guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log", |
| 1179 | WQ_HIGHPRI | WQ_FREEZABLE); |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1180 | if (guc->log.flush_wq == NULL) { |
| 1181 | DRM_ERROR("Couldn't allocate the wq for GuC logging\n"); |
| 1182 | return -ENOMEM; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 1189 | static void guc_log_create(struct intel_guc *guc) |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame] | 1190 | { |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1191 | struct i915_vma *vma; |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame] | 1192 | unsigned long offset; |
| 1193 | uint32_t size, flags; |
| 1194 | |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame] | 1195 | if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX) |
| 1196 | i915.guc_log_level = GUC_LOG_VERBOSITY_MAX; |
| 1197 | |
| 1198 | /* The first page is to save log buffer state. Allocate one |
| 1199 | * extra page for others in case for overlap */ |
| 1200 | size = (1 + GUC_LOG_DPC_PAGES + 1 + |
| 1201 | GUC_LOG_ISR_PAGES + 1 + |
| 1202 | GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT; |
| 1203 | |
Akash Goel | d6b40b4 | 2016-10-12 21:54:29 +0530 | [diff] [blame] | 1204 | vma = guc->log.vma; |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1205 | if (!vma) { |
Akash Goel | 7170659 | 2016-10-12 21:54:42 +0530 | [diff] [blame] | 1206 | /* We require SSE 4.1 for fast reads from the GuC log buffer and |
| 1207 | * it should be present on the chipsets supporting GuC based |
| 1208 | * submisssions. |
| 1209 | */ |
| 1210 | if (WARN_ON(!i915_memcpy_from_wc(NULL, NULL, 0))) { |
| 1211 | /* logging will not be enabled */ |
| 1212 | i915.guc_log_level = -1; |
| 1213 | return; |
| 1214 | } |
| 1215 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1216 | vma = guc_allocate_vma(guc, size); |
| 1217 | if (IS_ERR(vma)) { |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame] | 1218 | /* logging will be off */ |
| 1219 | i915.guc_log_level = -1; |
| 1220 | return; |
| 1221 | } |
| 1222 | |
Akash Goel | d6b40b4 | 2016-10-12 21:54:29 +0530 | [diff] [blame] | 1223 | guc->log.vma = vma; |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1224 | |
| 1225 | if (guc_log_create_extras(guc)) { |
| 1226 | guc_log_cleanup(guc); |
| 1227 | i915_vma_unpin_and_release(&guc->log.vma); |
| 1228 | i915.guc_log_level = -1; |
| 1229 | return; |
| 1230 | } |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /* each allocated unit is a page */ |
| 1234 | flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL | |
| 1235 | (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) | |
| 1236 | (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) | |
| 1237 | (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT); |
| 1238 | |
Chris Wilson | bde13eb | 2016-08-15 10:49:07 +0100 | [diff] [blame] | 1239 | offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */ |
Akash Goel | d6b40b4 | 2016-10-12 21:54:29 +0530 | [diff] [blame] | 1240 | guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags; |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame] | 1241 | } |
| 1242 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 1243 | static int guc_log_late_setup(struct intel_guc *guc) |
| 1244 | { |
| 1245 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
| 1246 | int ret; |
| 1247 | |
| 1248 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 1249 | |
| 1250 | if (i915.guc_log_level < 0) |
| 1251 | return -EINVAL; |
| 1252 | |
| 1253 | /* If log_level was set as -1 at boot time, then setup needed to |
| 1254 | * handle log buffer flush interrupts would not have been done yet, |
| 1255 | * so do that now. |
| 1256 | */ |
| 1257 | ret = guc_log_create_extras(guc); |
| 1258 | if (ret) |
| 1259 | goto err; |
| 1260 | |
| 1261 | ret = guc_log_create_relay_file(guc); |
| 1262 | if (ret) |
| 1263 | goto err; |
| 1264 | |
| 1265 | return 0; |
| 1266 | err: |
| 1267 | guc_log_cleanup(guc); |
| 1268 | /* logging will remain off */ |
| 1269 | i915.guc_log_level = -1; |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 1273 | static void guc_policies_init(struct guc_policies *policies) |
Alex Dai | 463704d | 2015-12-18 12:00:10 -0800 | [diff] [blame] | 1274 | { |
| 1275 | struct guc_policy *policy; |
| 1276 | u32 p, i; |
| 1277 | |
| 1278 | policies->dpc_promote_time = 500000; |
| 1279 | policies->max_num_work_items = POLICY_MAX_NUM_WI; |
| 1280 | |
| 1281 | for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) { |
Alex Dai | 397097b | 2016-01-23 11:58:14 -0800 | [diff] [blame] | 1282 | for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) { |
Alex Dai | 463704d | 2015-12-18 12:00:10 -0800 | [diff] [blame] | 1283 | policy = &policies->policy[p][i]; |
| 1284 | |
| 1285 | policy->execution_quantum = 1000000; |
| 1286 | policy->preemption_time = 500000; |
| 1287 | policy->fault_time = 250000; |
| 1288 | policy->policy_flags = 0; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | policies->is_valid = 1; |
| 1293 | } |
| 1294 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 1295 | static void guc_addon_create(struct intel_guc *guc) |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1296 | { |
| 1297 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1298 | struct i915_vma *vma; |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1299 | struct guc_ads *ads; |
Alex Dai | 463704d | 2015-12-18 12:00:10 -0800 | [diff] [blame] | 1300 | struct guc_policies *policies; |
Alex Dai | 5c148e0 | 2015-12-18 12:00:11 -0800 | [diff] [blame] | 1301 | struct guc_mmio_reg_state *reg_state; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 1302 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1303 | enum intel_engine_id id; |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1304 | struct page *page; |
Dave Gordon | b4ac5af | 2016-03-24 11:20:38 +0000 | [diff] [blame] | 1305 | u32 size; |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1306 | |
| 1307 | /* The ads obj includes the struct itself and buffers passed to GuC */ |
Alex Dai | 5c148e0 | 2015-12-18 12:00:11 -0800 | [diff] [blame] | 1308 | size = sizeof(struct guc_ads) + sizeof(struct guc_policies) + |
| 1309 | sizeof(struct guc_mmio_reg_state) + |
| 1310 | GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE; |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1311 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1312 | vma = guc->ads_vma; |
| 1313 | if (!vma) { |
| 1314 | vma = guc_allocate_vma(guc, PAGE_ALIGN(size)); |
| 1315 | if (IS_ERR(vma)) |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1316 | return; |
| 1317 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1318 | guc->ads_vma = vma; |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1319 | } |
| 1320 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1321 | page = i915_vma_first_page(vma); |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1322 | ads = kmap(page); |
| 1323 | |
| 1324 | /* |
| 1325 | * The GuC requires a "Golden Context" when it reinitialises |
| 1326 | * engines after a reset. Here we use the Render ring default |
| 1327 | * context, which must already exist and be pinned in the GGTT, |
| 1328 | * so its address won't change after we've told the GuC where |
| 1329 | * to find it. |
| 1330 | */ |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1331 | engine = dev_priv->engine[RCS]; |
Chris Wilson | 57e8853 | 2016-08-15 10:48:57 +0100 | [diff] [blame] | 1332 | ads->golden_context_lrca = engine->status_page.ggtt_offset; |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1333 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1334 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 1335 | ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine); |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1336 | |
Alex Dai | 463704d | 2015-12-18 12:00:10 -0800 | [diff] [blame] | 1337 | /* GuC scheduling policies */ |
| 1338 | policies = (void *)ads + sizeof(struct guc_ads); |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 1339 | guc_policies_init(policies); |
Alex Dai | 463704d | 2015-12-18 12:00:10 -0800 | [diff] [blame] | 1340 | |
Chris Wilson | bde13eb | 2016-08-15 10:49:07 +0100 | [diff] [blame] | 1341 | ads->scheduler_policies = |
| 1342 | i915_ggtt_offset(vma) + sizeof(struct guc_ads); |
Alex Dai | 463704d | 2015-12-18 12:00:10 -0800 | [diff] [blame] | 1343 | |
Alex Dai | 5c148e0 | 2015-12-18 12:00:11 -0800 | [diff] [blame] | 1344 | /* MMIO reg state */ |
| 1345 | reg_state = (void *)policies + sizeof(struct guc_policies); |
| 1346 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1347 | for_each_engine(engine, dev_priv, id) { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 1348 | reg_state->mmio_white_list[engine->guc_id].mmio_start = |
| 1349 | engine->mmio_base + GUC_MMIO_WHITE_LIST_START; |
Alex Dai | 5c148e0 | 2015-12-18 12:00:11 -0800 | [diff] [blame] | 1350 | |
| 1351 | /* Nothing to be saved or restored for now. */ |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 1352 | reg_state->mmio_white_list[engine->guc_id].count = 0; |
Alex Dai | 5c148e0 | 2015-12-18 12:00:11 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | ads->reg_state_addr = ads->scheduler_policies + |
| 1356 | sizeof(struct guc_policies); |
| 1357 | |
| 1358 | ads->reg_state_buffer = ads->reg_state_addr + |
| 1359 | sizeof(struct guc_mmio_reg_state); |
| 1360 | |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1361 | kunmap(page); |
| 1362 | } |
| 1363 | |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1364 | /* |
| 1365 | * Set up the memory resources to be shared with the GuC. At this point, |
| 1366 | * we require just one object that can be mapped through the GGTT. |
| 1367 | */ |
Dave Gordon | beffa51 | 2016-06-10 18:29:26 +0100 | [diff] [blame] | 1368 | int i915_guc_submission_init(struct drm_i915_private *dev_priv) |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1369 | { |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 1370 | const size_t ctxsize = sizeof(struct guc_context_desc); |
| 1371 | const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize; |
| 1372 | const size_t gemsize = round_up(poolsize, PAGE_SIZE); |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1373 | struct intel_guc *guc = &dev_priv->guc; |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1374 | struct i915_vma *vma; |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1375 | |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1376 | if (!HAS_GUC_SCHED(dev_priv)) |
| 1377 | return 0; |
| 1378 | |
Dave Gordon | 29fb72c | 2016-06-07 09:14:50 +0100 | [diff] [blame] | 1379 | /* Wipe bitmap & delete client in case of reinitialisation */ |
| 1380 | bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS); |
Dave Gordon | beffa51 | 2016-06-10 18:29:26 +0100 | [diff] [blame] | 1381 | i915_guc_submission_disable(dev_priv); |
Dave Gordon | 29fb72c | 2016-06-07 09:14:50 +0100 | [diff] [blame] | 1382 | |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1383 | if (!i915.enable_guc_submission) |
| 1384 | return 0; /* not enabled */ |
| 1385 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1386 | if (guc->ctx_pool_vma) |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1387 | return 0; /* already allocated */ |
| 1388 | |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 1389 | vma = guc_allocate_vma(guc, gemsize); |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1390 | if (IS_ERR(vma)) |
| 1391 | return PTR_ERR(vma); |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1392 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1393 | guc->ctx_pool_vma = vma; |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1394 | ida_init(&guc->ctx_ids); |
Dave Gordon | 7a9347f | 2016-09-12 21:19:37 +0100 | [diff] [blame] | 1395 | guc_log_create(guc); |
| 1396 | guc_addon_create(guc); |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1397 | |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1398 | guc->execbuf_client = guc_client_alloc(dev_priv, |
| 1399 | INTEL_INFO(dev_priv)->ring_mask, |
| 1400 | GUC_CTX_PRIORITY_KMD_NORMAL, |
| 1401 | dev_priv->kernel_context); |
| 1402 | if (!guc->execbuf_client) { |
| 1403 | DRM_ERROR("Failed to create GuC client for execbuf!\n"); |
| 1404 | goto err; |
| 1405 | } |
| 1406 | |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1407 | return 0; |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1408 | |
| 1409 | err: |
| 1410 | i915_guc_submission_fini(dev_priv); |
| 1411 | return -ENOMEM; |
| 1412 | } |
| 1413 | |
| 1414 | static void guc_reset_wq(struct i915_guc_client *gc) |
| 1415 | { |
| 1416 | struct guc_process_desc *desc = gc->vaddr + gc->proc_desc_offset; |
| 1417 | |
| 1418 | desc->head = 0; |
| 1419 | desc->tail = 0; |
| 1420 | |
| 1421 | gc->wq_tail = 0; |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
Dave Gordon | beffa51 | 2016-06-10 18:29:26 +0100 | [diff] [blame] | 1424 | int i915_guc_submission_enable(struct drm_i915_private *dev_priv) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1425 | { |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1426 | struct intel_guc *guc = &dev_priv->guc; |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1427 | struct i915_guc_client *client = guc->execbuf_client; |
Chris Wilson | ddd66c5 | 2016-08-02 22:50:31 +0100 | [diff] [blame] | 1428 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1429 | enum intel_engine_id id; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1430 | |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1431 | if (!client) |
| 1432 | return -ENODEV; |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1433 | |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 1434 | intel_guc_sample_forcewake(guc); |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1435 | |
| 1436 | guc_reset_wq(client); |
Dave Gordon | 4d75787 | 2016-06-13 17:57:34 +0100 | [diff] [blame] | 1437 | guc_init_doorbell_hw(guc); |
Alex Dai | f5d3c3e | 2015-08-18 14:34:47 -0700 | [diff] [blame] | 1438 | |
Chris Wilson | ddd66c5 | 2016-08-02 22:50:31 +0100 | [diff] [blame] | 1439 | /* Take over from manual control of ELSP (execlists) */ |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1440 | for_each_engine(engine, dev_priv, id) { |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1441 | struct drm_i915_gem_request *rq; |
| 1442 | |
Chris Wilson | ddd66c5 | 2016-08-02 22:50:31 +0100 | [diff] [blame] | 1443 | engine->submit_request = i915_guc_submit; |
Chris Wilson | 20311bd | 2016-11-14 20:41:03 +0000 | [diff] [blame] | 1444 | engine->schedule = NULL; |
Chris Wilson | ddd66c5 | 2016-08-02 22:50:31 +0100 | [diff] [blame] | 1445 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 1446 | /* Replay the current set of previously submitted requests */ |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1447 | list_for_each_entry(rq, &engine->timeline->requests, link) { |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 1448 | client->wq_rsvd += sizeof(struct guc_wq_item); |
Chris Wilson | 34ba5a8 | 2016-11-29 12:10:24 +0000 | [diff] [blame^] | 1449 | __i915_guc_submit(rq); |
Chris Wilson | dadd481 | 2016-09-09 14:11:57 +0100 | [diff] [blame] | 1450 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 1451 | } |
| 1452 | |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Dave Gordon | beffa51 | 2016-06-10 18:29:26 +0100 | [diff] [blame] | 1456 | void i915_guc_submission_disable(struct drm_i915_private *dev_priv) |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1457 | { |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1458 | struct intel_guc *guc = &dev_priv->guc; |
| 1459 | |
Chris Wilson | ddd66c5 | 2016-08-02 22:50:31 +0100 | [diff] [blame] | 1460 | if (!guc->execbuf_client) |
| 1461 | return; |
| 1462 | |
Chris Wilson | ddd66c5 | 2016-08-02 22:50:31 +0100 | [diff] [blame] | 1463 | /* Revert back to manual ELSP submission */ |
| 1464 | intel_execlists_enable_submission(dev_priv); |
Dave Gordon | 44a28b1 | 2015-08-12 15:43:41 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
Dave Gordon | beffa51 | 2016-06-10 18:29:26 +0100 | [diff] [blame] | 1467 | void i915_guc_submission_fini(struct drm_i915_private *dev_priv) |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1468 | { |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1469 | struct intel_guc *guc = &dev_priv->guc; |
Chris Wilson | 4d357af | 2016-11-29 12:10:23 +0000 | [diff] [blame] | 1470 | struct i915_guc_client *client; |
| 1471 | |
| 1472 | client = fetch_and_zero(&guc->execbuf_client); |
| 1473 | if (!client) |
| 1474 | return; |
| 1475 | |
| 1476 | guc_client_free(dev_priv, client); |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1477 | |
Chris Wilson | 19880c4 | 2016-08-15 10:49:05 +0100 | [diff] [blame] | 1478 | i915_vma_unpin_and_release(&guc->ads_vma); |
Akash Goel | d6b40b4 | 2016-10-12 21:54:29 +0530 | [diff] [blame] | 1479 | i915_vma_unpin_and_release(&guc->log.vma); |
Alex Dai | 68371a9 | 2015-12-18 12:00:09 -0800 | [diff] [blame] | 1480 | |
Chris Wilson | 8b797af | 2016-08-15 10:48:51 +0100 | [diff] [blame] | 1481 | if (guc->ctx_pool_vma) |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1482 | ida_destroy(&guc->ctx_ids); |
Chris Wilson | 19880c4 | 2016-08-15 10:49:05 +0100 | [diff] [blame] | 1483 | i915_vma_unpin_and_release(&guc->ctx_pool_vma); |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 1484 | } |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1485 | |
| 1486 | /** |
| 1487 | * intel_guc_suspend() - notify GuC entering suspend state |
| 1488 | * @dev: drm device |
| 1489 | */ |
| 1490 | int intel_guc_suspend(struct drm_device *dev) |
| 1491 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 1492 | struct drm_i915_private *dev_priv = to_i915(dev); |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1493 | struct intel_guc *guc = &dev_priv->guc; |
Chris Wilson | e2efd13 | 2016-05-24 14:53:34 +0100 | [diff] [blame] | 1494 | struct i915_gem_context *ctx; |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1495 | u32 data[3]; |
| 1496 | |
Dave Gordon | fce91f2 | 2016-05-20 11:42:42 +0100 | [diff] [blame] | 1497 | if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS) |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1498 | return 0; |
| 1499 | |
Sagar Arun Kamble | 26705e2 | 2016-10-12 21:54:31 +0530 | [diff] [blame] | 1500 | gen9_disable_guc_interrupts(dev_priv); |
| 1501 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 1502 | ctx = dev_priv->kernel_context; |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1503 | |
Arkadiusz Hiler | a80bc45 | 2016-11-25 18:59:34 +0100 | [diff] [blame] | 1504 | data[0] = INTEL_GUC_ACTION_ENTER_S_STATE; |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1505 | /* any value greater than GUC_POWER_D0 */ |
| 1506 | data[1] = GUC_POWER_D1; |
| 1507 | /* first page is shared data with GuC */ |
Chris Wilson | bde13eb | 2016-08-15 10:49:07 +0100 | [diff] [blame] | 1508 | data[2] = i915_ggtt_offset(ctx->engine[RCS].state); |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1509 | |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 1510 | return intel_guc_send(guc, data, ARRAY_SIZE(data)); |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | /** |
| 1515 | * intel_guc_resume() - notify GuC resuming from suspend state |
| 1516 | * @dev: drm device |
| 1517 | */ |
| 1518 | int intel_guc_resume(struct drm_device *dev) |
| 1519 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 1520 | struct drm_i915_private *dev_priv = to_i915(dev); |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1521 | struct intel_guc *guc = &dev_priv->guc; |
Chris Wilson | e2efd13 | 2016-05-24 14:53:34 +0100 | [diff] [blame] | 1522 | struct i915_gem_context *ctx; |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1523 | u32 data[3]; |
| 1524 | |
Dave Gordon | fce91f2 | 2016-05-20 11:42:42 +0100 | [diff] [blame] | 1525 | if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS) |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1526 | return 0; |
| 1527 | |
Sagar Arun Kamble | 26705e2 | 2016-10-12 21:54:31 +0530 | [diff] [blame] | 1528 | if (i915.guc_log_level >= 0) |
| 1529 | gen9_enable_guc_interrupts(dev_priv); |
| 1530 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 1531 | ctx = dev_priv->kernel_context; |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1532 | |
Arkadiusz Hiler | a80bc45 | 2016-11-25 18:59:34 +0100 | [diff] [blame] | 1533 | data[0] = INTEL_GUC_ACTION_EXIT_S_STATE; |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1534 | data[1] = GUC_POWER_D0; |
| 1535 | /* first page is shared data with GuC */ |
Chris Wilson | bde13eb | 2016-08-15 10:49:07 +0100 | [diff] [blame] | 1536 | data[2] = i915_ggtt_offset(ctx->engine[RCS].state); |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1537 | |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 1538 | return intel_guc_send(guc, data, ARRAY_SIZE(data)); |
Alex Dai | a1c4199 | 2015-09-30 09:46:37 -0700 | [diff] [blame] | 1539 | } |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1540 | |
| 1541 | void i915_guc_capture_logs(struct drm_i915_private *dev_priv) |
| 1542 | { |
| 1543 | guc_read_update_log_buffer(&dev_priv->guc); |
| 1544 | |
| 1545 | /* Generally device is expected to be active only at this |
| 1546 | * time, so get/put should be really quick. |
| 1547 | */ |
| 1548 | intel_runtime_pm_get(dev_priv); |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 1549 | intel_guc_log_flush_complete(&dev_priv->guc); |
Sagar Arun Kamble | 4100b2a | 2016-10-12 21:54:32 +0530 | [diff] [blame] | 1550 | intel_runtime_pm_put(dev_priv); |
| 1551 | } |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 1552 | |
Sagar Arun Kamble | 896a0cb | 2016-10-12 21:54:40 +0530 | [diff] [blame] | 1553 | void i915_guc_flush_logs(struct drm_i915_private *dev_priv) |
| 1554 | { |
| 1555 | if (!i915.enable_guc_submission || (i915.guc_log_level < 0)) |
| 1556 | return; |
| 1557 | |
| 1558 | /* First disable the interrupts, will be renabled afterwards */ |
| 1559 | gen9_disable_guc_interrupts(dev_priv); |
| 1560 | |
| 1561 | /* Before initiating the forceful flush, wait for any pending/ongoing |
| 1562 | * flush to complete otherwise forceful flush may not actually happen. |
| 1563 | */ |
| 1564 | flush_work(&dev_priv->guc.log.flush_work); |
| 1565 | |
| 1566 | /* Ask GuC to update the log buffer state */ |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 1567 | intel_guc_log_flush(&dev_priv->guc); |
Sagar Arun Kamble | 896a0cb | 2016-10-12 21:54:40 +0530 | [diff] [blame] | 1568 | |
| 1569 | /* GuC would have updated log buffer by now, so capture it */ |
| 1570 | i915_guc_capture_logs(dev_priv); |
| 1571 | } |
| 1572 | |
Akash Goel | f824083 | 2016-10-12 21:54:34 +0530 | [diff] [blame] | 1573 | void i915_guc_unregister(struct drm_i915_private *dev_priv) |
| 1574 | { |
| 1575 | if (!i915.enable_guc_submission) |
| 1576 | return; |
| 1577 | |
| 1578 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 1579 | guc_log_cleanup(&dev_priv->guc); |
| 1580 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1581 | } |
| 1582 | |
| 1583 | void i915_guc_register(struct drm_i915_private *dev_priv) |
| 1584 | { |
| 1585 | if (!i915.enable_guc_submission) |
| 1586 | return; |
| 1587 | |
| 1588 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 1589 | guc_log_late_setup(&dev_priv->guc); |
| 1590 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1591 | } |
Sagar Arun Kamble | 685534e | 2016-10-12 21:54:41 +0530 | [diff] [blame] | 1592 | |
| 1593 | int i915_guc_log_control(struct drm_i915_private *dev_priv, u64 control_val) |
| 1594 | { |
| 1595 | union guc_log_control log_param; |
| 1596 | int ret; |
| 1597 | |
| 1598 | log_param.value = control_val; |
| 1599 | |
| 1600 | if (log_param.verbosity < GUC_LOG_VERBOSITY_MIN || |
| 1601 | log_param.verbosity > GUC_LOG_VERBOSITY_MAX) |
| 1602 | return -EINVAL; |
| 1603 | |
| 1604 | /* This combination doesn't make sense & won't have any effect */ |
| 1605 | if (!log_param.logging_enabled && (i915.guc_log_level < 0)) |
| 1606 | return 0; |
| 1607 | |
Arkadiusz Hiler | 2d803c2 | 2016-11-25 18:59:35 +0100 | [diff] [blame] | 1608 | ret = intel_guc_log_control(&dev_priv->guc, log_param.value); |
Sagar Arun Kamble | 685534e | 2016-10-12 21:54:41 +0530 | [diff] [blame] | 1609 | if (ret < 0) { |
Arkadiusz Hiler | a80bc45 | 2016-11-25 18:59:34 +0100 | [diff] [blame] | 1610 | DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret); |
Sagar Arun Kamble | 685534e | 2016-10-12 21:54:41 +0530 | [diff] [blame] | 1611 | return ret; |
| 1612 | } |
| 1613 | |
| 1614 | i915.guc_log_level = log_param.verbosity; |
| 1615 | |
| 1616 | /* If log_level was set as -1 at boot time, then the relay channel file |
| 1617 | * wouldn't have been created by now and interrupts also would not have |
| 1618 | * been enabled. |
| 1619 | */ |
| 1620 | if (!dev_priv->guc.log.relay_chan) { |
| 1621 | ret = guc_log_late_setup(&dev_priv->guc); |
| 1622 | if (!ret) |
| 1623 | gen9_enable_guc_interrupts(dev_priv); |
| 1624 | } else if (!log_param.logging_enabled) { |
| 1625 | /* Once logging is disabled, GuC won't generate logs & send an |
| 1626 | * interrupt. But there could be some data in the log buffer |
| 1627 | * which is yet to be captured. So request GuC to update the log |
| 1628 | * buffer state and then collect the left over logs. |
| 1629 | */ |
| 1630 | i915_guc_flush_logs(dev_priv); |
| 1631 | |
| 1632 | /* As logging is disabled, update log level to reflect that */ |
| 1633 | i915.guc_log_level = -1; |
| 1634 | } else { |
| 1635 | /* In case interrupts were disabled, enable them now */ |
| 1636 | gen9_enable_guc_interrupts(dev_priv); |
| 1637 | } |
| 1638 | |
| 1639 | return ret; |
| 1640 | } |