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