intel_guc_submission.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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/circ_buf.h>
  25. #include <trace/events/dma_fence.h>
  26. #include "intel_guc_submission.h"
  27. #include "intel_lrc_reg.h"
  28. #include "i915_drv.h"
  29. #define GUC_PREEMPT_FINISHED 0x1
  30. #define GUC_PREEMPT_BREADCRUMB_DWORDS 0x8
  31. #define GUC_PREEMPT_BREADCRUMB_BYTES \
  32. (sizeof(u32) * GUC_PREEMPT_BREADCRUMB_DWORDS)
  33. /**
  34. * DOC: GuC-based command submission
  35. *
  36. * GuC client:
  37. * A intel_guc_client refers to a submission path through GuC. Currently, there
  38. * are two clients. One of them (the execbuf_client) is charged with all
  39. * submissions to the GuC, the other one (preempt_client) is responsible for
  40. * preempting the execbuf_client. This struct is the owner of a doorbell, a
  41. * process descriptor and a workqueue (all of them inside a single gem object
  42. * that contains all required pages for these elements).
  43. *
  44. * GuC stage descriptor:
  45. * During initialization, the driver allocates a static pool of 1024 such
  46. * descriptors, and shares them with the GuC.
  47. * Currently, there exists a 1:1 mapping between a intel_guc_client and a
  48. * guc_stage_desc (via the client's stage_id), so effectively only one
  49. * gets used. This stage descriptor lets the GuC know about the doorbell,
  50. * workqueue and process descriptor. Theoretically, it also lets the GuC
  51. * know about our HW contexts (context ID, etc...), but we actually
  52. * employ a kind of submission where the GuC uses the LRCA sent via the work
  53. * item instead (the single guc_stage_desc associated to execbuf client
  54. * contains information about the default kernel context only, but this is
  55. * essentially unused). This is called a "proxy" submission.
  56. *
  57. * The Scratch registers:
  58. * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
  59. * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
  60. * triggers an interrupt on the GuC via another register write (0xC4C8).
  61. * Firmware writes a success/fail code back to the action register after
  62. * processes the request. The kernel driver polls waiting for this update and
  63. * then proceeds.
  64. * See intel_guc_send()
  65. *
  66. * Doorbells:
  67. * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
  68. * mapped into process space.
  69. *
  70. * Work Items:
  71. * There are several types of work items that the host may place into a
  72. * workqueue, each with its own requirements and limitations. Currently only
  73. * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
  74. * represents in-order queue. The kernel driver packs ring tail pointer and an
  75. * ELSP context descriptor dword into Work Item.
  76. * See guc_add_request()
  77. *
  78. */
  79. static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  80. {
  81. return rb_entry(rb, struct i915_priolist, node);
  82. }
  83. static inline bool is_high_priority(struct intel_guc_client *client)
  84. {
  85. return (client->priority == GUC_CLIENT_PRIORITY_KMD_HIGH ||
  86. client->priority == GUC_CLIENT_PRIORITY_HIGH);
  87. }
  88. static int reserve_doorbell(struct intel_guc_client *client)
  89. {
  90. unsigned long offset;
  91. unsigned long end;
  92. u16 id;
  93. GEM_BUG_ON(client->doorbell_id != GUC_DOORBELL_INVALID);
  94. /*
  95. * The bitmap tracks which doorbell registers are currently in use.
  96. * It is split into two halves; the first half is used for normal
  97. * priority contexts, the second half for high-priority ones.
  98. */
  99. offset = 0;
  100. end = GUC_NUM_DOORBELLS / 2;
  101. if (is_high_priority(client)) {
  102. offset = end;
  103. end += offset;
  104. }
  105. id = find_next_zero_bit(client->guc->doorbell_bitmap, end, offset);
  106. if (id == end)
  107. return -ENOSPC;
  108. __set_bit(id, client->guc->doorbell_bitmap);
  109. client->doorbell_id = id;
  110. DRM_DEBUG_DRIVER("client %u (high prio=%s) reserved doorbell: %d\n",
  111. client->stage_id, yesno(is_high_priority(client)),
  112. id);
  113. return 0;
  114. }
  115. static bool has_doorbell(struct intel_guc_client *client)
  116. {
  117. if (client->doorbell_id == GUC_DOORBELL_INVALID)
  118. return false;
  119. return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
  120. }
  121. static void unreserve_doorbell(struct intel_guc_client *client)
  122. {
  123. GEM_BUG_ON(!has_doorbell(client));
  124. __clear_bit(client->doorbell_id, client->guc->doorbell_bitmap);
  125. client->doorbell_id = GUC_DOORBELL_INVALID;
  126. }
  127. /*
  128. * Tell the GuC to allocate or deallocate a specific doorbell
  129. */
  130. static int __guc_allocate_doorbell(struct intel_guc *guc, u32 stage_id)
  131. {
  132. u32 action[] = {
  133. INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
  134. stage_id
  135. };
  136. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  137. }
  138. static int __guc_deallocate_doorbell(struct intel_guc *guc, u32 stage_id)
  139. {
  140. u32 action[] = {
  141. INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
  142. stage_id
  143. };
  144. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  145. }
  146. static struct guc_stage_desc *__get_stage_desc(struct intel_guc_client *client)
  147. {
  148. struct guc_stage_desc *base = client->guc->stage_desc_pool_vaddr;
  149. return &base[client->stage_id];
  150. }
  151. /*
  152. * Initialise, update, or clear doorbell data shared with the GuC
  153. *
  154. * These functions modify shared data and so need access to the mapped
  155. * client object which contains the page being used for the doorbell
  156. */
  157. static void __update_doorbell_desc(struct intel_guc_client *client, u16 new_id)
  158. {
  159. struct guc_stage_desc *desc;
  160. /* Update the GuC's idea of the doorbell ID */
  161. desc = __get_stage_desc(client);
  162. desc->db_id = new_id;
  163. }
  164. static struct guc_doorbell_info *__get_doorbell(struct intel_guc_client *client)
  165. {
  166. return client->vaddr + client->doorbell_offset;
  167. }
  168. static void __create_doorbell(struct intel_guc_client *client)
  169. {
  170. struct guc_doorbell_info *doorbell;
  171. doorbell = __get_doorbell(client);
  172. doorbell->db_status = GUC_DOORBELL_ENABLED;
  173. doorbell->cookie = 0;
  174. }
  175. static void __destroy_doorbell(struct intel_guc_client *client)
  176. {
  177. struct drm_i915_private *dev_priv = guc_to_i915(client->guc);
  178. struct guc_doorbell_info *doorbell;
  179. u16 db_id = client->doorbell_id;
  180. doorbell = __get_doorbell(client);
  181. doorbell->db_status = GUC_DOORBELL_DISABLED;
  182. doorbell->cookie = 0;
  183. /* Doorbell release flow requires that we wait for GEN8_DRB_VALID bit
  184. * to go to zero after updating db_status before we call the GuC to
  185. * release the doorbell
  186. */
  187. if (wait_for_us(!(I915_READ(GEN8_DRBREGL(db_id)) & GEN8_DRB_VALID), 10))
  188. WARN_ONCE(true, "Doorbell never became invalid after disable\n");
  189. }
  190. static int create_doorbell(struct intel_guc_client *client)
  191. {
  192. int ret;
  193. if (WARN_ON(!has_doorbell(client)))
  194. return -ENODEV; /* internal setup error, should never happen */
  195. __update_doorbell_desc(client, client->doorbell_id);
  196. __create_doorbell(client);
  197. ret = __guc_allocate_doorbell(client->guc, client->stage_id);
  198. if (ret) {
  199. __destroy_doorbell(client);
  200. __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
  201. DRM_DEBUG_DRIVER("Couldn't create client %u doorbell: %d\n",
  202. client->stage_id, ret);
  203. return ret;
  204. }
  205. return 0;
  206. }
  207. static int destroy_doorbell(struct intel_guc_client *client)
  208. {
  209. int ret;
  210. GEM_BUG_ON(!has_doorbell(client));
  211. __destroy_doorbell(client);
  212. ret = __guc_deallocate_doorbell(client->guc, client->stage_id);
  213. if (ret)
  214. DRM_ERROR("Couldn't destroy client %u doorbell: %d\n",
  215. client->stage_id, ret);
  216. __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
  217. return ret;
  218. }
  219. static unsigned long __select_cacheline(struct intel_guc *guc)
  220. {
  221. unsigned long offset;
  222. /* Doorbell uses a single cache line within a page */
  223. offset = offset_in_page(guc->db_cacheline);
  224. /* Moving to next cache line to reduce contention */
  225. guc->db_cacheline += cache_line_size();
  226. DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
  227. offset, guc->db_cacheline, cache_line_size());
  228. return offset;
  229. }
  230. static inline struct guc_process_desc *
  231. __get_process_desc(struct intel_guc_client *client)
  232. {
  233. return client->vaddr + client->proc_desc_offset;
  234. }
  235. /*
  236. * Initialise the process descriptor shared with the GuC firmware.
  237. */
  238. static void guc_proc_desc_init(struct intel_guc *guc,
  239. struct intel_guc_client *client)
  240. {
  241. struct guc_process_desc *desc;
  242. desc = memset(__get_process_desc(client), 0, sizeof(*desc));
  243. /*
  244. * XXX: pDoorbell and WQVBaseAddress are pointers in process address
  245. * space for ring3 clients (set them as in mmap_ioctl) or kernel
  246. * space for kernel clients (map on demand instead? May make debug
  247. * easier to have it mapped).
  248. */
  249. desc->wq_base_addr = 0;
  250. desc->db_base_addr = 0;
  251. desc->stage_id = client->stage_id;
  252. desc->wq_size_bytes = GUC_WQ_SIZE;
  253. desc->wq_status = WQ_STATUS_ACTIVE;
  254. desc->priority = client->priority;
  255. }
  256. static int guc_stage_desc_pool_create(struct intel_guc *guc)
  257. {
  258. struct i915_vma *vma;
  259. void *vaddr;
  260. vma = intel_guc_allocate_vma(guc,
  261. PAGE_ALIGN(sizeof(struct guc_stage_desc) *
  262. GUC_MAX_STAGE_DESCRIPTORS));
  263. if (IS_ERR(vma))
  264. return PTR_ERR(vma);
  265. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  266. if (IS_ERR(vaddr)) {
  267. i915_vma_unpin_and_release(&vma, 0);
  268. return PTR_ERR(vaddr);
  269. }
  270. guc->stage_desc_pool = vma;
  271. guc->stage_desc_pool_vaddr = vaddr;
  272. ida_init(&guc->stage_ids);
  273. return 0;
  274. }
  275. static void guc_stage_desc_pool_destroy(struct intel_guc *guc)
  276. {
  277. ida_destroy(&guc->stage_ids);
  278. i915_vma_unpin_and_release(&guc->stage_desc_pool, I915_VMA_RELEASE_MAP);
  279. }
  280. /*
  281. * Initialise/clear the stage descriptor shared with the GuC firmware.
  282. *
  283. * This descriptor tells the GuC where (in GGTT space) to find the important
  284. * data structures relating to this client (doorbell, process descriptor,
  285. * write queue, etc).
  286. */
  287. static void guc_stage_desc_init(struct intel_guc *guc,
  288. struct intel_guc_client *client)
  289. {
  290. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  291. struct intel_engine_cs *engine;
  292. struct i915_gem_context *ctx = client->owner;
  293. struct guc_stage_desc *desc;
  294. unsigned int tmp;
  295. u32 gfx_addr;
  296. desc = __get_stage_desc(client);
  297. memset(desc, 0, sizeof(*desc));
  298. desc->attribute = GUC_STAGE_DESC_ATTR_ACTIVE |
  299. GUC_STAGE_DESC_ATTR_KERNEL;
  300. if (is_high_priority(client))
  301. desc->attribute |= GUC_STAGE_DESC_ATTR_PREEMPT;
  302. desc->stage_id = client->stage_id;
  303. desc->priority = client->priority;
  304. desc->db_id = client->doorbell_id;
  305. for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
  306. struct intel_context *ce = to_intel_context(ctx, engine);
  307. u32 guc_engine_id = engine->guc_id;
  308. struct guc_execlist_context *lrc = &desc->lrc[guc_engine_id];
  309. /* TODO: We have a design issue to be solved here. Only when we
  310. * receive the first batch, we know which engine is used by the
  311. * user. But here GuC expects the lrc and ring to be pinned. It
  312. * is not an issue for default context, which is the only one
  313. * for now who owns a GuC client. But for future owner of GuC
  314. * client, need to make sure lrc is pinned prior to enter here.
  315. */
  316. if (!ce->state)
  317. break; /* XXX: continue? */
  318. /*
  319. * XXX: When this is a GUC_STAGE_DESC_ATTR_KERNEL client (proxy
  320. * submission or, in other words, not using a direct submission
  321. * model) the KMD's LRCA is not used for any work submission.
  322. * Instead, the GuC uses the LRCA of the user mode context (see
  323. * guc_add_request below).
  324. */
  325. lrc->context_desc = lower_32_bits(ce->lrc_desc);
  326. /* The state page is after PPHWSP */
  327. lrc->ring_lrca = intel_guc_ggtt_offset(guc, ce->state) +
  328. LRC_STATE_PN * PAGE_SIZE;
  329. /* XXX: In direct submission, the GuC wants the HW context id
  330. * here. In proxy submission, it wants the stage id
  331. */
  332. lrc->context_id = (client->stage_id << GUC_ELC_CTXID_OFFSET) |
  333. (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
  334. lrc->ring_begin = intel_guc_ggtt_offset(guc, ce->ring->vma);
  335. lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
  336. lrc->ring_next_free_location = lrc->ring_begin;
  337. lrc->ring_current_tail_pointer_value = 0;
  338. desc->engines_used |= (1 << guc_engine_id);
  339. }
  340. DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
  341. client->engines, desc->engines_used);
  342. WARN_ON(desc->engines_used == 0);
  343. /*
  344. * The doorbell, process descriptor, and workqueue are all parts
  345. * of the client object, which the GuC will reference via the GGTT
  346. */
  347. gfx_addr = intel_guc_ggtt_offset(guc, client->vma);
  348. desc->db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
  349. client->doorbell_offset;
  350. desc->db_trigger_cpu = ptr_to_u64(__get_doorbell(client));
  351. desc->db_trigger_uk = gfx_addr + client->doorbell_offset;
  352. desc->process_desc = gfx_addr + client->proc_desc_offset;
  353. desc->wq_addr = gfx_addr + GUC_DB_SIZE;
  354. desc->wq_size = GUC_WQ_SIZE;
  355. desc->desc_private = ptr_to_u64(client);
  356. }
  357. static void guc_stage_desc_fini(struct intel_guc *guc,
  358. struct intel_guc_client *client)
  359. {
  360. struct guc_stage_desc *desc;
  361. desc = __get_stage_desc(client);
  362. memset(desc, 0, sizeof(*desc));
  363. }
  364. /* Construct a Work Item and append it to the GuC's Work Queue */
  365. static void guc_wq_item_append(struct intel_guc_client *client,
  366. u32 target_engine, u32 context_desc,
  367. u32 ring_tail, u32 fence_id)
  368. {
  369. /* wqi_len is in DWords, and does not include the one-word header */
  370. const size_t wqi_size = sizeof(struct guc_wq_item);
  371. const u32 wqi_len = wqi_size / sizeof(u32) - 1;
  372. struct guc_process_desc *desc = __get_process_desc(client);
  373. struct guc_wq_item *wqi;
  374. u32 wq_off;
  375. lockdep_assert_held(&client->wq_lock);
  376. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  377. * should not have the case where structure wqi is across page, neither
  378. * wrapped to the beginning. This simplifies the implementation below.
  379. *
  380. * XXX: if not the case, we need save data to a temp wqi and copy it to
  381. * workqueue buffer dw by dw.
  382. */
  383. BUILD_BUG_ON(wqi_size != 16);
  384. /* We expect the WQ to be active if we're appending items to it */
  385. GEM_BUG_ON(desc->wq_status != WQ_STATUS_ACTIVE);
  386. /* Free space is guaranteed. */
  387. wq_off = READ_ONCE(desc->tail);
  388. GEM_BUG_ON(CIRC_SPACE(wq_off, READ_ONCE(desc->head),
  389. GUC_WQ_SIZE) < wqi_size);
  390. GEM_BUG_ON(wq_off & (wqi_size - 1));
  391. /* WQ starts from the page after doorbell / process_desc */
  392. wqi = client->vaddr + wq_off + GUC_DB_SIZE;
  393. if (I915_SELFTEST_ONLY(client->use_nop_wqi)) {
  394. wqi->header = WQ_TYPE_NOOP | (wqi_len << WQ_LEN_SHIFT);
  395. } else {
  396. /* Now fill in the 4-word work queue item */
  397. wqi->header = WQ_TYPE_INORDER |
  398. (wqi_len << WQ_LEN_SHIFT) |
  399. (target_engine << WQ_TARGET_SHIFT) |
  400. WQ_NO_WCFLUSH_WAIT;
  401. wqi->context_desc = context_desc;
  402. wqi->submit_element_info = ring_tail << WQ_RING_TAIL_SHIFT;
  403. GEM_BUG_ON(ring_tail > WQ_RING_TAIL_MAX);
  404. wqi->fence_id = fence_id;
  405. }
  406. /* Make the update visible to GuC */
  407. WRITE_ONCE(desc->tail, (wq_off + wqi_size) & (GUC_WQ_SIZE - 1));
  408. }
  409. static void guc_reset_wq(struct intel_guc_client *client)
  410. {
  411. struct guc_process_desc *desc = __get_process_desc(client);
  412. desc->head = 0;
  413. desc->tail = 0;
  414. }
  415. static void guc_ring_doorbell(struct intel_guc_client *client)
  416. {
  417. struct guc_doorbell_info *db;
  418. u32 cookie;
  419. lockdep_assert_held(&client->wq_lock);
  420. /* pointer of current doorbell cacheline */
  421. db = __get_doorbell(client);
  422. /*
  423. * We're not expecting the doorbell cookie to change behind our back,
  424. * we also need to treat 0 as a reserved value.
  425. */
  426. cookie = READ_ONCE(db->cookie);
  427. WARN_ON_ONCE(xchg(&db->cookie, cookie + 1 ?: cookie + 2) != cookie);
  428. /* XXX: doorbell was lost and need to acquire it again */
  429. GEM_BUG_ON(db->db_status != GUC_DOORBELL_ENABLED);
  430. }
  431. static void guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  432. {
  433. struct intel_guc_client *client = guc->execbuf_client;
  434. struct intel_engine_cs *engine = rq->engine;
  435. u32 ctx_desc = lower_32_bits(rq->hw_context->lrc_desc);
  436. u32 ring_tail = intel_ring_set_tail(rq->ring, rq->tail) / sizeof(u64);
  437. spin_lock(&client->wq_lock);
  438. guc_wq_item_append(client, engine->guc_id, ctx_desc,
  439. ring_tail, rq->global_seqno);
  440. guc_ring_doorbell(client);
  441. client->submissions[engine->id] += 1;
  442. spin_unlock(&client->wq_lock);
  443. }
  444. /*
  445. * When we're doing submissions using regular execlists backend, writing to
  446. * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
  447. * pinned in mappable aperture portion of GGTT are visible to command streamer.
  448. * Writes done by GuC on our behalf are not guaranteeing such ordering,
  449. * therefore, to ensure the flush, we're issuing a POSTING READ.
  450. */
  451. static void flush_ggtt_writes(struct i915_vma *vma)
  452. {
  453. struct drm_i915_private *dev_priv = vma->vm->i915;
  454. if (i915_vma_is_map_and_fenceable(vma))
  455. POSTING_READ_FW(GUC_STATUS);
  456. }
  457. static void inject_preempt_context(struct work_struct *work)
  458. {
  459. struct guc_preempt_work *preempt_work =
  460. container_of(work, typeof(*preempt_work), work);
  461. struct intel_engine_cs *engine = preempt_work->engine;
  462. struct intel_guc *guc = container_of(preempt_work, typeof(*guc),
  463. preempt_work[engine->id]);
  464. struct intel_guc_client *client = guc->preempt_client;
  465. struct guc_stage_desc *stage_desc = __get_stage_desc(client);
  466. struct intel_context *ce = to_intel_context(client->owner, engine);
  467. u32 data[7];
  468. if (!ce->ring->emit) { /* recreate upon load/resume */
  469. u32 addr = intel_hws_preempt_done_address(engine);
  470. u32 *cs;
  471. cs = ce->ring->vaddr;
  472. if (engine->id == RCS) {
  473. cs = gen8_emit_ggtt_write_rcs(cs,
  474. GUC_PREEMPT_FINISHED,
  475. addr);
  476. } else {
  477. cs = gen8_emit_ggtt_write(cs,
  478. GUC_PREEMPT_FINISHED,
  479. addr);
  480. *cs++ = MI_NOOP;
  481. *cs++ = MI_NOOP;
  482. }
  483. *cs++ = MI_USER_INTERRUPT;
  484. *cs++ = MI_NOOP;
  485. ce->ring->emit = GUC_PREEMPT_BREADCRUMB_BYTES;
  486. GEM_BUG_ON((void *)cs - ce->ring->vaddr != ce->ring->emit);
  487. flush_ggtt_writes(ce->ring->vma);
  488. }
  489. spin_lock_irq(&client->wq_lock);
  490. guc_wq_item_append(client, engine->guc_id, lower_32_bits(ce->lrc_desc),
  491. GUC_PREEMPT_BREADCRUMB_BYTES / sizeof(u64), 0);
  492. spin_unlock_irq(&client->wq_lock);
  493. /*
  494. * If GuC firmware performs an engine reset while that engine had
  495. * a preemption pending, it will set the terminated attribute bit
  496. * on our preemption stage descriptor. GuC firmware retains all
  497. * pending work items for a high-priority GuC client, unlike the
  498. * normal-priority GuC client where work items are dropped. It
  499. * wants to make sure the preempt-to-idle work doesn't run when
  500. * scheduling resumes, and uses this bit to inform its scheduler
  501. * and presumably us as well. Our job is to clear it for the next
  502. * preemption after reset, otherwise that and future preemptions
  503. * will never complete. We'll just clear it every time.
  504. */
  505. stage_desc->attribute &= ~GUC_STAGE_DESC_ATTR_TERMINATED;
  506. data[0] = INTEL_GUC_ACTION_REQUEST_PREEMPTION;
  507. data[1] = client->stage_id;
  508. data[2] = INTEL_GUC_PREEMPT_OPTION_DROP_WORK_Q |
  509. INTEL_GUC_PREEMPT_OPTION_DROP_SUBMIT_Q;
  510. data[3] = engine->guc_id;
  511. data[4] = guc->execbuf_client->priority;
  512. data[5] = guc->execbuf_client->stage_id;
  513. data[6] = intel_guc_ggtt_offset(guc, guc->shared_data);
  514. if (WARN_ON(intel_guc_send(guc, data, ARRAY_SIZE(data)))) {
  515. execlists_clear_active(&engine->execlists,
  516. EXECLISTS_ACTIVE_PREEMPT);
  517. tasklet_schedule(&engine->execlists.tasklet);
  518. }
  519. }
  520. /*
  521. * We're using user interrupt and HWSP value to mark that preemption has
  522. * finished and GPU is idle. Normally, we could unwind and continue similar to
  523. * execlists submission path. Unfortunately, with GuC we also need to wait for
  524. * it to finish its own postprocessing, before attempting to submit. Otherwise
  525. * GuC may silently ignore our submissions, and thus we risk losing request at
  526. * best, executing out-of-order and causing kernel panic at worst.
  527. */
  528. #define GUC_PREEMPT_POSTPROCESS_DELAY_MS 10
  529. static void wait_for_guc_preempt_report(struct intel_engine_cs *engine)
  530. {
  531. struct intel_guc *guc = &engine->i915->guc;
  532. struct guc_shared_ctx_data *data = guc->shared_data_vaddr;
  533. struct guc_ctx_report *report =
  534. &data->preempt_ctx_report[engine->guc_id];
  535. WARN_ON(wait_for_atomic(report->report_return_status ==
  536. INTEL_GUC_REPORT_STATUS_COMPLETE,
  537. GUC_PREEMPT_POSTPROCESS_DELAY_MS));
  538. /*
  539. * GuC is expecting that we're also going to clear the affected context
  540. * counter, let's also reset the return status to not depend on GuC
  541. * resetting it after recieving another preempt action
  542. */
  543. report->affected_count = 0;
  544. report->report_return_status = INTEL_GUC_REPORT_STATUS_UNKNOWN;
  545. }
  546. static void complete_preempt_context(struct intel_engine_cs *engine)
  547. {
  548. struct intel_engine_execlists *execlists = &engine->execlists;
  549. GEM_BUG_ON(!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT));
  550. if (inject_preempt_hang(execlists))
  551. return;
  552. execlists_cancel_port_requests(execlists);
  553. execlists_unwind_incomplete_requests(execlists);
  554. wait_for_guc_preempt_report(engine);
  555. intel_write_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX, 0);
  556. }
  557. /**
  558. * guc_submit() - Submit commands through GuC
  559. * @engine: engine associated with the commands
  560. *
  561. * The only error here arises if the doorbell hardware isn't functioning
  562. * as expected, which really shouln't happen.
  563. */
  564. static void guc_submit(struct intel_engine_cs *engine)
  565. {
  566. struct intel_guc *guc = &engine->i915->guc;
  567. struct intel_engine_execlists * const execlists = &engine->execlists;
  568. struct execlist_port *port = execlists->port;
  569. unsigned int n;
  570. for (n = 0; n < execlists_num_ports(execlists); n++) {
  571. struct i915_request *rq;
  572. unsigned int count;
  573. rq = port_unpack(&port[n], &count);
  574. if (rq && count == 0) {
  575. port_set(&port[n], port_pack(rq, ++count));
  576. flush_ggtt_writes(rq->ring->vma);
  577. guc_add_request(guc, rq);
  578. }
  579. }
  580. }
  581. static void port_assign(struct execlist_port *port, struct i915_request *rq)
  582. {
  583. GEM_BUG_ON(port_isset(port));
  584. port_set(port, i915_request_get(rq));
  585. }
  586. static inline int rq_prio(const struct i915_request *rq)
  587. {
  588. return rq->sched.attr.priority;
  589. }
  590. static inline int port_prio(const struct execlist_port *port)
  591. {
  592. return rq_prio(port_request(port));
  593. }
  594. static bool __guc_dequeue(struct intel_engine_cs *engine)
  595. {
  596. struct intel_engine_execlists * const execlists = &engine->execlists;
  597. struct execlist_port *port = execlists->port;
  598. struct i915_request *last = NULL;
  599. const struct execlist_port * const last_port =
  600. &execlists->port[execlists->port_mask];
  601. bool submit = false;
  602. struct rb_node *rb;
  603. lockdep_assert_held(&engine->timeline.lock);
  604. if (port_isset(port)) {
  605. if (intel_engine_has_preemption(engine)) {
  606. struct guc_preempt_work *preempt_work =
  607. &engine->i915->guc.preempt_work[engine->id];
  608. int prio = execlists->queue_priority;
  609. if (__execlists_need_preempt(prio, port_prio(port))) {
  610. execlists_set_active(execlists,
  611. EXECLISTS_ACTIVE_PREEMPT);
  612. queue_work(engine->i915->guc.preempt_wq,
  613. &preempt_work->work);
  614. return false;
  615. }
  616. }
  617. port++;
  618. if (port_isset(port))
  619. return false;
  620. }
  621. GEM_BUG_ON(port_isset(port));
  622. while ((rb = rb_first_cached(&execlists->queue))) {
  623. struct i915_priolist *p = to_priolist(rb);
  624. struct i915_request *rq, *rn;
  625. list_for_each_entry_safe(rq, rn, &p->requests, sched.link) {
  626. if (last && rq->hw_context != last->hw_context) {
  627. if (port == last_port) {
  628. __list_del_many(&p->requests,
  629. &rq->sched.link);
  630. goto done;
  631. }
  632. if (submit)
  633. port_assign(port, last);
  634. port++;
  635. }
  636. INIT_LIST_HEAD(&rq->sched.link);
  637. __i915_request_submit(rq);
  638. trace_i915_request_in(rq, port_index(port, execlists));
  639. last = rq;
  640. submit = true;
  641. }
  642. rb_erase_cached(&p->node, &execlists->queue);
  643. INIT_LIST_HEAD(&p->requests);
  644. if (p->priority != I915_PRIORITY_NORMAL)
  645. kmem_cache_free(engine->i915->priorities, p);
  646. }
  647. done:
  648. execlists->queue_priority = rb ? to_priolist(rb)->priority : INT_MIN;
  649. if (submit)
  650. port_assign(port, last);
  651. if (last)
  652. execlists_user_begin(execlists, execlists->port);
  653. /* We must always keep the beast fed if we have work piled up */
  654. GEM_BUG_ON(port_isset(execlists->port) &&
  655. !execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
  656. GEM_BUG_ON(rb_first_cached(&execlists->queue) &&
  657. !port_isset(execlists->port));
  658. return submit;
  659. }
  660. static void guc_dequeue(struct intel_engine_cs *engine)
  661. {
  662. unsigned long flags;
  663. bool submit;
  664. local_irq_save(flags);
  665. spin_lock(&engine->timeline.lock);
  666. submit = __guc_dequeue(engine);
  667. spin_unlock(&engine->timeline.lock);
  668. if (submit)
  669. guc_submit(engine);
  670. local_irq_restore(flags);
  671. }
  672. static void guc_submission_tasklet(unsigned long data)
  673. {
  674. struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
  675. struct intel_engine_execlists * const execlists = &engine->execlists;
  676. struct execlist_port *port = execlists->port;
  677. struct i915_request *rq;
  678. rq = port_request(port);
  679. while (rq && i915_request_completed(rq)) {
  680. trace_i915_request_out(rq);
  681. i915_request_put(rq);
  682. port = execlists_port_complete(execlists, port);
  683. if (port_isset(port)) {
  684. execlists_user_begin(execlists, port);
  685. rq = port_request(port);
  686. } else {
  687. execlists_user_end(execlists);
  688. rq = NULL;
  689. }
  690. }
  691. if (execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT) &&
  692. intel_read_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX) ==
  693. GUC_PREEMPT_FINISHED)
  694. complete_preempt_context(engine);
  695. if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
  696. guc_dequeue(engine);
  697. }
  698. static struct i915_request *
  699. guc_reset_prepare(struct intel_engine_cs *engine)
  700. {
  701. struct intel_engine_execlists * const execlists = &engine->execlists;
  702. GEM_TRACE("%s\n", engine->name);
  703. /*
  704. * Prevent request submission to the hardware until we have
  705. * completed the reset in i915_gem_reset_finish(). If a request
  706. * is completed by one engine, it may then queue a request
  707. * to a second via its execlists->tasklet *just* as we are
  708. * calling engine->init_hw() and also writing the ELSP.
  709. * Turning off the execlists->tasklet until the reset is over
  710. * prevents the race.
  711. */
  712. __tasklet_disable_sync_once(&execlists->tasklet);
  713. /*
  714. * We're using worker to queue preemption requests from the tasklet in
  715. * GuC submission mode.
  716. * Even though tasklet was disabled, we may still have a worker queued.
  717. * Let's make sure that all workers scheduled before disabling the
  718. * tasklet are completed before continuing with the reset.
  719. */
  720. if (engine->i915->guc.preempt_wq)
  721. flush_workqueue(engine->i915->guc.preempt_wq);
  722. return i915_gem_find_active_request(engine);
  723. }
  724. /*
  725. * Everything below here is concerned with setup & teardown, and is
  726. * therefore not part of the somewhat time-critical batch-submission
  727. * path of guc_submit() above.
  728. */
  729. /* Check that a doorbell register is in the expected state */
  730. static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
  731. {
  732. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  733. u32 drbregl;
  734. bool valid;
  735. GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
  736. drbregl = I915_READ(GEN8_DRBREGL(db_id));
  737. valid = drbregl & GEN8_DRB_VALID;
  738. if (test_bit(db_id, guc->doorbell_bitmap) == valid)
  739. return true;
  740. DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
  741. db_id, drbregl, yesno(valid));
  742. return false;
  743. }
  744. static bool guc_verify_doorbells(struct intel_guc *guc)
  745. {
  746. u16 db_id;
  747. for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
  748. if (!doorbell_ok(guc, db_id))
  749. return false;
  750. return true;
  751. }
  752. static int guc_clients_doorbell_init(struct intel_guc *guc)
  753. {
  754. int ret;
  755. ret = create_doorbell(guc->execbuf_client);
  756. if (ret)
  757. return ret;
  758. if (guc->preempt_client) {
  759. ret = create_doorbell(guc->preempt_client);
  760. if (ret) {
  761. destroy_doorbell(guc->execbuf_client);
  762. return ret;
  763. }
  764. }
  765. return 0;
  766. }
  767. static void guc_clients_doorbell_fini(struct intel_guc *guc)
  768. {
  769. /*
  770. * By the time we're here, GuC has already been reset.
  771. * Instead of trying (in vain) to communicate with it, let's just
  772. * cleanup the doorbell HW and our internal state.
  773. */
  774. if (guc->preempt_client) {
  775. __destroy_doorbell(guc->preempt_client);
  776. __update_doorbell_desc(guc->preempt_client,
  777. GUC_DOORBELL_INVALID);
  778. }
  779. if (guc->execbuf_client) {
  780. __destroy_doorbell(guc->execbuf_client);
  781. __update_doorbell_desc(guc->execbuf_client,
  782. GUC_DOORBELL_INVALID);
  783. }
  784. }
  785. /**
  786. * guc_client_alloc() - Allocate an intel_guc_client
  787. * @dev_priv: driver private data structure
  788. * @engines: The set of engines to enable for this client
  789. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  790. * The kernel client to replace ExecList submission is created with
  791. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  792. * while a preemption context can use CRITICAL.
  793. * @ctx: the context that owns the client (we use the default render
  794. * context)
  795. *
  796. * Return: An intel_guc_client object if success, else NULL.
  797. */
  798. static struct intel_guc_client *
  799. guc_client_alloc(struct drm_i915_private *dev_priv,
  800. u32 engines,
  801. u32 priority,
  802. struct i915_gem_context *ctx)
  803. {
  804. struct intel_guc_client *client;
  805. struct intel_guc *guc = &dev_priv->guc;
  806. struct i915_vma *vma;
  807. void *vaddr;
  808. int ret;
  809. client = kzalloc(sizeof(*client), GFP_KERNEL);
  810. if (!client)
  811. return ERR_PTR(-ENOMEM);
  812. client->guc = guc;
  813. client->owner = ctx;
  814. client->engines = engines;
  815. client->priority = priority;
  816. client->doorbell_id = GUC_DOORBELL_INVALID;
  817. spin_lock_init(&client->wq_lock);
  818. ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
  819. GFP_KERNEL);
  820. if (ret < 0)
  821. goto err_client;
  822. client->stage_id = ret;
  823. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  824. vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
  825. if (IS_ERR(vma)) {
  826. ret = PTR_ERR(vma);
  827. goto err_id;
  828. }
  829. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  830. client->vma = vma;
  831. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  832. if (IS_ERR(vaddr)) {
  833. ret = PTR_ERR(vaddr);
  834. goto err_vma;
  835. }
  836. client->vaddr = vaddr;
  837. client->doorbell_offset = __select_cacheline(guc);
  838. /*
  839. * Since the doorbell only requires a single cacheline, we can save
  840. * space by putting the application process descriptor in the same
  841. * page. Use the half of the page that doesn't include the doorbell.
  842. */
  843. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  844. client->proc_desc_offset = 0;
  845. else
  846. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  847. guc_proc_desc_init(guc, client);
  848. guc_stage_desc_init(guc, client);
  849. ret = reserve_doorbell(client);
  850. if (ret)
  851. goto err_vaddr;
  852. DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
  853. priority, client, client->engines, client->stage_id);
  854. DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
  855. client->doorbell_id, client->doorbell_offset);
  856. return client;
  857. err_vaddr:
  858. i915_gem_object_unpin_map(client->vma->obj);
  859. err_vma:
  860. i915_vma_unpin_and_release(&client->vma, 0);
  861. err_id:
  862. ida_simple_remove(&guc->stage_ids, client->stage_id);
  863. err_client:
  864. kfree(client);
  865. return ERR_PTR(ret);
  866. }
  867. static void guc_client_free(struct intel_guc_client *client)
  868. {
  869. unreserve_doorbell(client);
  870. guc_stage_desc_fini(client->guc, client);
  871. i915_vma_unpin_and_release(&client->vma, I915_VMA_RELEASE_MAP);
  872. ida_simple_remove(&client->guc->stage_ids, client->stage_id);
  873. kfree(client);
  874. }
  875. static inline bool ctx_save_restore_disabled(struct intel_context *ce)
  876. {
  877. u32 sr = ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1];
  878. #define SR_DISABLED \
  879. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT | \
  880. CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)
  881. return (sr & SR_DISABLED) == SR_DISABLED;
  882. #undef SR_DISABLED
  883. }
  884. static int guc_clients_create(struct intel_guc *guc)
  885. {
  886. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  887. struct intel_guc_client *client;
  888. GEM_BUG_ON(guc->execbuf_client);
  889. GEM_BUG_ON(guc->preempt_client);
  890. client = guc_client_alloc(dev_priv,
  891. INTEL_INFO(dev_priv)->ring_mask,
  892. GUC_CLIENT_PRIORITY_KMD_NORMAL,
  893. dev_priv->kernel_context);
  894. if (IS_ERR(client)) {
  895. DRM_ERROR("Failed to create GuC client for submission!\n");
  896. return PTR_ERR(client);
  897. }
  898. guc->execbuf_client = client;
  899. if (dev_priv->preempt_context) {
  900. client = guc_client_alloc(dev_priv,
  901. INTEL_INFO(dev_priv)->ring_mask,
  902. GUC_CLIENT_PRIORITY_KMD_HIGH,
  903. dev_priv->preempt_context);
  904. if (IS_ERR(client)) {
  905. DRM_ERROR("Failed to create GuC client for preemption!\n");
  906. guc_client_free(guc->execbuf_client);
  907. guc->execbuf_client = NULL;
  908. return PTR_ERR(client);
  909. }
  910. guc->preempt_client = client;
  911. }
  912. return 0;
  913. }
  914. static void guc_clients_destroy(struct intel_guc *guc)
  915. {
  916. struct intel_guc_client *client;
  917. client = fetch_and_zero(&guc->preempt_client);
  918. if (client)
  919. guc_client_free(client);
  920. client = fetch_and_zero(&guc->execbuf_client);
  921. if (client)
  922. guc_client_free(client);
  923. }
  924. /*
  925. * Set up the memory resources to be shared with the GuC (via the GGTT)
  926. * at firmware loading time.
  927. */
  928. int intel_guc_submission_init(struct intel_guc *guc)
  929. {
  930. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  931. struct intel_engine_cs *engine;
  932. enum intel_engine_id id;
  933. int ret;
  934. if (guc->stage_desc_pool)
  935. return 0;
  936. ret = guc_stage_desc_pool_create(guc);
  937. if (ret)
  938. return ret;
  939. /*
  940. * Keep static analysers happy, let them know that we allocated the
  941. * vma after testing that it didn't exist earlier.
  942. */
  943. GEM_BUG_ON(!guc->stage_desc_pool);
  944. WARN_ON(!guc_verify_doorbells(guc));
  945. ret = guc_clients_create(guc);
  946. if (ret)
  947. goto err_pool;
  948. for_each_engine(engine, dev_priv, id) {
  949. guc->preempt_work[id].engine = engine;
  950. INIT_WORK(&guc->preempt_work[id].work, inject_preempt_context);
  951. }
  952. return 0;
  953. err_pool:
  954. guc_stage_desc_pool_destroy(guc);
  955. return ret;
  956. }
  957. void intel_guc_submission_fini(struct intel_guc *guc)
  958. {
  959. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  960. struct intel_engine_cs *engine;
  961. enum intel_engine_id id;
  962. for_each_engine(engine, dev_priv, id)
  963. cancel_work_sync(&guc->preempt_work[id].work);
  964. guc_clients_destroy(guc);
  965. WARN_ON(!guc_verify_doorbells(guc));
  966. if (guc->stage_desc_pool)
  967. guc_stage_desc_pool_destroy(guc);
  968. }
  969. static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
  970. {
  971. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  972. struct intel_engine_cs *engine;
  973. enum intel_engine_id id;
  974. int irqs;
  975. /* tell all command streamers to forward interrupts (but not vblank)
  976. * to GuC
  977. */
  978. irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
  979. for_each_engine(engine, dev_priv, id)
  980. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  981. /* route USER_INTERRUPT to Host, all others are sent to GuC. */
  982. irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  983. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  984. /* These three registers have the same bit definitions */
  985. I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
  986. I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
  987. I915_WRITE(GUC_WD_VECS_IER, ~irqs);
  988. /*
  989. * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
  990. * (unmasked) PM interrupts to the GuC. All other bits of this
  991. * register *disable* generation of a specific interrupt.
  992. *
  993. * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
  994. * writing to the PM interrupt mask register, i.e. interrupts
  995. * that must not be disabled.
  996. *
  997. * If the GuC is handling these interrupts, then we must not let
  998. * the PM code disable ANY interrupt that the GuC is expecting.
  999. * So for each ENABLED (0) bit in this register, we must SET the
  1000. * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
  1001. * GuC needs ARAT expired interrupt unmasked hence it is set in
  1002. * pm_intrmsk_mbz.
  1003. *
  1004. * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
  1005. * result in the register bit being left SET!
  1006. */
  1007. rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
  1008. rps->pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  1009. }
  1010. static void guc_interrupts_release(struct drm_i915_private *dev_priv)
  1011. {
  1012. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  1013. struct intel_engine_cs *engine;
  1014. enum intel_engine_id id;
  1015. int irqs;
  1016. /*
  1017. * tell all command streamers NOT to forward interrupts or vblank
  1018. * to GuC.
  1019. */
  1020. irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
  1021. irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
  1022. for_each_engine(engine, dev_priv, id)
  1023. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  1024. /* route all GT interrupts to the host */
  1025. I915_WRITE(GUC_BCS_RCS_IER, 0);
  1026. I915_WRITE(GUC_VCS2_VCS1_IER, 0);
  1027. I915_WRITE(GUC_WD_VECS_IER, 0);
  1028. rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  1029. rps->pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
  1030. }
  1031. static void guc_submission_park(struct intel_engine_cs *engine)
  1032. {
  1033. intel_engine_unpin_breadcrumbs_irq(engine);
  1034. }
  1035. static void guc_submission_unpark(struct intel_engine_cs *engine)
  1036. {
  1037. intel_engine_pin_breadcrumbs_irq(engine);
  1038. }
  1039. static void guc_set_default_submission(struct intel_engine_cs *engine)
  1040. {
  1041. /*
  1042. * We inherit a bunch of functions from execlists that we'd like
  1043. * to keep using:
  1044. *
  1045. * engine->submit_request = execlists_submit_request;
  1046. * engine->cancel_requests = execlists_cancel_requests;
  1047. * engine->schedule = execlists_schedule;
  1048. *
  1049. * But we need to override the actual submission backend in order
  1050. * to talk to the GuC.
  1051. */
  1052. intel_execlists_set_default_submission(engine);
  1053. engine->execlists.tasklet.func = guc_submission_tasklet;
  1054. engine->park = guc_submission_park;
  1055. engine->unpark = guc_submission_unpark;
  1056. engine->reset.prepare = guc_reset_prepare;
  1057. engine->flags &= ~I915_ENGINE_SUPPORTS_STATS;
  1058. }
  1059. int intel_guc_submission_enable(struct intel_guc *guc)
  1060. {
  1061. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1062. struct intel_engine_cs *engine;
  1063. enum intel_engine_id id;
  1064. int err;
  1065. /*
  1066. * We're using GuC work items for submitting work through GuC. Since
  1067. * we're coalescing multiple requests from a single context into a
  1068. * single work item prior to assigning it to execlist_port, we can
  1069. * never have more work items than the total number of ports (for all
  1070. * engines). The GuC firmware is controlling the HEAD of work queue,
  1071. * and it is guaranteed that it will remove the work item from the
  1072. * queue before our request is completed.
  1073. */
  1074. BUILD_BUG_ON(ARRAY_SIZE(engine->execlists.port) *
  1075. sizeof(struct guc_wq_item) *
  1076. I915_NUM_ENGINES > GUC_WQ_SIZE);
  1077. GEM_BUG_ON(!guc->execbuf_client);
  1078. guc_reset_wq(guc->execbuf_client);
  1079. if (guc->preempt_client)
  1080. guc_reset_wq(guc->preempt_client);
  1081. err = intel_guc_sample_forcewake(guc);
  1082. if (err)
  1083. return err;
  1084. err = guc_clients_doorbell_init(guc);
  1085. if (err)
  1086. return err;
  1087. /* Take over from manual control of ELSP (execlists) */
  1088. guc_interrupts_capture(dev_priv);
  1089. for_each_engine(engine, dev_priv, id) {
  1090. engine->set_default_submission = guc_set_default_submission;
  1091. engine->set_default_submission(engine);
  1092. }
  1093. return 0;
  1094. }
  1095. void intel_guc_submission_disable(struct intel_guc *guc)
  1096. {
  1097. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1098. GEM_BUG_ON(dev_priv->gt.awake); /* GT should be parked first */
  1099. guc_interrupts_release(dev_priv);
  1100. guc_clients_doorbell_fini(guc);
  1101. }
  1102. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1103. #include "selftests/intel_guc.c"
  1104. #endif