i915_guc_submission.c 31 KB

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