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