i915_guc_submission.c 29 KB

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