intel_guc_submission.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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);
  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_gem_object_unpin_map(guc->stage_desc_pool->obj);
  279. i915_vma_unpin_and_release(&guc->stage_desc_pool);
  280. }
  281. /*
  282. * Initialise/clear the stage descriptor shared with the GuC firmware.
  283. *
  284. * This descriptor tells the GuC where (in GGTT space) to find the important
  285. * data structures relating to this client (doorbell, process descriptor,
  286. * write queue, etc).
  287. */
  288. static void guc_stage_desc_init(struct intel_guc *guc,
  289. struct intel_guc_client *client)
  290. {
  291. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  292. struct intel_engine_cs *engine;
  293. struct i915_gem_context *ctx = client->owner;
  294. struct guc_stage_desc *desc;
  295. unsigned int tmp;
  296. u32 gfx_addr;
  297. desc = __get_stage_desc(client);
  298. memset(desc, 0, sizeof(*desc));
  299. desc->attribute = GUC_STAGE_DESC_ATTR_ACTIVE |
  300. GUC_STAGE_DESC_ATTR_KERNEL;
  301. if (is_high_priority(client))
  302. desc->attribute |= GUC_STAGE_DESC_ATTR_PREEMPT;
  303. desc->stage_id = client->stage_id;
  304. desc->priority = client->priority;
  305. desc->db_id = client->doorbell_id;
  306. for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
  307. struct intel_context *ce = to_intel_context(ctx, engine);
  308. u32 guc_engine_id = engine->guc_id;
  309. struct guc_execlist_context *lrc = &desc->lrc[guc_engine_id];
  310. /* TODO: We have a design issue to be solved here. Only when we
  311. * receive the first batch, we know which engine is used by the
  312. * user. But here GuC expects the lrc and ring to be pinned. It
  313. * is not an issue for default context, which is the only one
  314. * for now who owns a GuC client. But for future owner of GuC
  315. * client, need to make sure lrc is pinned prior to enter here.
  316. */
  317. if (!ce->state)
  318. break; /* XXX: continue? */
  319. /*
  320. * XXX: When this is a GUC_STAGE_DESC_ATTR_KERNEL client (proxy
  321. * submission or, in other words, not using a direct submission
  322. * model) the KMD's LRCA is not used for any work submission.
  323. * Instead, the GuC uses the LRCA of the user mode context (see
  324. * guc_add_request below).
  325. */
  326. lrc->context_desc = lower_32_bits(ce->lrc_desc);
  327. /* The state page is after PPHWSP */
  328. lrc->ring_lrca = intel_guc_ggtt_offset(guc, ce->state) +
  329. LRC_STATE_PN * PAGE_SIZE;
  330. /* XXX: In direct submission, the GuC wants the HW context id
  331. * here. In proxy submission, it wants the stage id
  332. */
  333. lrc->context_id = (client->stage_id << GUC_ELC_CTXID_OFFSET) |
  334. (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
  335. lrc->ring_begin = intel_guc_ggtt_offset(guc, ce->ring->vma);
  336. lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
  337. lrc->ring_next_free_location = lrc->ring_begin;
  338. lrc->ring_current_tail_pointer_value = 0;
  339. desc->engines_used |= (1 << guc_engine_id);
  340. }
  341. DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
  342. client->engines, desc->engines_used);
  343. WARN_ON(desc->engines_used == 0);
  344. /*
  345. * The doorbell, process descriptor, and workqueue are all parts
  346. * of the client object, which the GuC will reference via the GGTT
  347. */
  348. gfx_addr = intel_guc_ggtt_offset(guc, client->vma);
  349. desc->db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
  350. client->doorbell_offset;
  351. desc->db_trigger_cpu = ptr_to_u64(__get_doorbell(client));
  352. desc->db_trigger_uk = gfx_addr + client->doorbell_offset;
  353. desc->process_desc = gfx_addr + client->proc_desc_offset;
  354. desc->wq_addr = gfx_addr + GUC_DB_SIZE;
  355. desc->wq_size = GUC_WQ_SIZE;
  356. desc->desc_private = ptr_to_u64(client);
  357. }
  358. static void guc_stage_desc_fini(struct intel_guc *guc,
  359. struct intel_guc_client *client)
  360. {
  361. struct guc_stage_desc *desc;
  362. desc = __get_stage_desc(client);
  363. memset(desc, 0, sizeof(*desc));
  364. }
  365. /* Construct a Work Item and append it to the GuC's Work Queue */
  366. static void guc_wq_item_append(struct intel_guc_client *client,
  367. u32 target_engine, u32 context_desc,
  368. u32 ring_tail, u32 fence_id)
  369. {
  370. /* wqi_len is in DWords, and does not include the one-word header */
  371. const size_t wqi_size = sizeof(struct guc_wq_item);
  372. const u32 wqi_len = wqi_size / sizeof(u32) - 1;
  373. struct guc_process_desc *desc = __get_process_desc(client);
  374. struct guc_wq_item *wqi;
  375. u32 wq_off;
  376. lockdep_assert_held(&client->wq_lock);
  377. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  378. * should not have the case where structure wqi is across page, neither
  379. * wrapped to the beginning. This simplifies the implementation below.
  380. *
  381. * XXX: if not the case, we need save data to a temp wqi and copy it to
  382. * workqueue buffer dw by dw.
  383. */
  384. BUILD_BUG_ON(wqi_size != 16);
  385. /* Free space is guaranteed. */
  386. wq_off = READ_ONCE(desc->tail);
  387. GEM_BUG_ON(CIRC_SPACE(wq_off, READ_ONCE(desc->head),
  388. GUC_WQ_SIZE) < wqi_size);
  389. GEM_BUG_ON(wq_off & (wqi_size - 1));
  390. /* WQ starts from the page after doorbell / process_desc */
  391. wqi = client->vaddr + wq_off + GUC_DB_SIZE;
  392. /* Now fill in the 4-word work queue item */
  393. wqi->header = WQ_TYPE_INORDER |
  394. (wqi_len << WQ_LEN_SHIFT) |
  395. (target_engine << WQ_TARGET_SHIFT) |
  396. WQ_NO_WCFLUSH_WAIT;
  397. wqi->context_desc = context_desc;
  398. wqi->submit_element_info = ring_tail << WQ_RING_TAIL_SHIFT;
  399. GEM_BUG_ON(ring_tail > WQ_RING_TAIL_MAX);
  400. wqi->fence_id = fence_id;
  401. /* Make the update visible to GuC */
  402. WRITE_ONCE(desc->tail, (wq_off + wqi_size) & (GUC_WQ_SIZE - 1));
  403. }
  404. static void guc_reset_wq(struct intel_guc_client *client)
  405. {
  406. struct guc_process_desc *desc = __get_process_desc(client);
  407. desc->head = 0;
  408. desc->tail = 0;
  409. }
  410. static void guc_ring_doorbell(struct intel_guc_client *client)
  411. {
  412. struct guc_doorbell_info *db;
  413. u32 cookie;
  414. lockdep_assert_held(&client->wq_lock);
  415. /* pointer of current doorbell cacheline */
  416. db = __get_doorbell(client);
  417. /*
  418. * We're not expecting the doorbell cookie to change behind our back,
  419. * we also need to treat 0 as a reserved value.
  420. */
  421. cookie = READ_ONCE(db->cookie);
  422. WARN_ON_ONCE(xchg(&db->cookie, cookie + 1 ?: cookie + 2) != cookie);
  423. /* XXX: doorbell was lost and need to acquire it again */
  424. GEM_BUG_ON(db->db_status != GUC_DOORBELL_ENABLED);
  425. }
  426. static void guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  427. {
  428. struct intel_guc_client *client = guc->execbuf_client;
  429. struct intel_engine_cs *engine = rq->engine;
  430. u32 ctx_desc = lower_32_bits(intel_lr_context_descriptor(rq->ctx,
  431. engine));
  432. u32 ring_tail = intel_ring_set_tail(rq->ring, rq->tail) / sizeof(u64);
  433. spin_lock(&client->wq_lock);
  434. guc_wq_item_append(client, engine->guc_id, ctx_desc,
  435. ring_tail, rq->global_seqno);
  436. guc_ring_doorbell(client);
  437. client->submissions[engine->id] += 1;
  438. spin_unlock(&client->wq_lock);
  439. }
  440. /*
  441. * When we're doing submissions using regular execlists backend, writing to
  442. * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
  443. * pinned in mappable aperture portion of GGTT are visible to command streamer.
  444. * Writes done by GuC on our behalf are not guaranteeing such ordering,
  445. * therefore, to ensure the flush, we're issuing a POSTING READ.
  446. */
  447. static void flush_ggtt_writes(struct i915_vma *vma)
  448. {
  449. struct drm_i915_private *dev_priv = to_i915(vma->obj->base.dev);
  450. if (i915_vma_is_map_and_fenceable(vma))
  451. POSTING_READ_FW(GUC_STATUS);
  452. }
  453. static void inject_preempt_context(struct work_struct *work)
  454. {
  455. struct guc_preempt_work *preempt_work =
  456. container_of(work, typeof(*preempt_work), work);
  457. struct intel_engine_cs *engine = preempt_work->engine;
  458. struct intel_guc *guc = container_of(preempt_work, typeof(*guc),
  459. preempt_work[engine->id]);
  460. struct intel_guc_client *client = guc->preempt_client;
  461. struct guc_stage_desc *stage_desc = __get_stage_desc(client);
  462. u32 ctx_desc = lower_32_bits(intel_lr_context_descriptor(client->owner,
  463. engine));
  464. u32 data[7];
  465. /*
  466. * The ring contains commands to write GUC_PREEMPT_FINISHED into HWSP.
  467. * See guc_fill_preempt_context().
  468. */
  469. spin_lock_irq(&client->wq_lock);
  470. guc_wq_item_append(client, engine->guc_id, ctx_desc,
  471. GUC_PREEMPT_BREADCRUMB_BYTES / sizeof(u64), 0);
  472. spin_unlock_irq(&client->wq_lock);
  473. /*
  474. * If GuC firmware performs an engine reset while that engine had
  475. * a preemption pending, it will set the terminated attribute bit
  476. * on our preemption stage descriptor. GuC firmware retains all
  477. * pending work items for a high-priority GuC client, unlike the
  478. * normal-priority GuC client where work items are dropped. It
  479. * wants to make sure the preempt-to-idle work doesn't run when
  480. * scheduling resumes, and uses this bit to inform its scheduler
  481. * and presumably us as well. Our job is to clear it for the next
  482. * preemption after reset, otherwise that and future preemptions
  483. * will never complete. We'll just clear it every time.
  484. */
  485. stage_desc->attribute &= ~GUC_STAGE_DESC_ATTR_TERMINATED;
  486. data[0] = INTEL_GUC_ACTION_REQUEST_PREEMPTION;
  487. data[1] = client->stage_id;
  488. data[2] = INTEL_GUC_PREEMPT_OPTION_DROP_WORK_Q |
  489. INTEL_GUC_PREEMPT_OPTION_DROP_SUBMIT_Q;
  490. data[3] = engine->guc_id;
  491. data[4] = guc->execbuf_client->priority;
  492. data[5] = guc->execbuf_client->stage_id;
  493. data[6] = intel_guc_ggtt_offset(guc, guc->shared_data);
  494. if (WARN_ON(intel_guc_send(guc, data, ARRAY_SIZE(data)))) {
  495. execlists_clear_active(&engine->execlists,
  496. EXECLISTS_ACTIVE_PREEMPT);
  497. tasklet_schedule(&engine->execlists.tasklet);
  498. }
  499. }
  500. /*
  501. * We're using user interrupt and HWSP value to mark that preemption has
  502. * finished and GPU is idle. Normally, we could unwind and continue similar to
  503. * execlists submission path. Unfortunately, with GuC we also need to wait for
  504. * it to finish its own postprocessing, before attempting to submit. Otherwise
  505. * GuC may silently ignore our submissions, and thus we risk losing request at
  506. * best, executing out-of-order and causing kernel panic at worst.
  507. */
  508. #define GUC_PREEMPT_POSTPROCESS_DELAY_MS 10
  509. static void wait_for_guc_preempt_report(struct intel_engine_cs *engine)
  510. {
  511. struct intel_guc *guc = &engine->i915->guc;
  512. struct guc_shared_ctx_data *data = guc->shared_data_vaddr;
  513. struct guc_ctx_report *report =
  514. &data->preempt_ctx_report[engine->guc_id];
  515. WARN_ON(wait_for_atomic(report->report_return_status ==
  516. INTEL_GUC_REPORT_STATUS_COMPLETE,
  517. GUC_PREEMPT_POSTPROCESS_DELAY_MS));
  518. /*
  519. * GuC is expecting that we're also going to clear the affected context
  520. * counter, let's also reset the return status to not depend on GuC
  521. * resetting it after recieving another preempt action
  522. */
  523. report->affected_count = 0;
  524. report->report_return_status = INTEL_GUC_REPORT_STATUS_UNKNOWN;
  525. }
  526. /**
  527. * guc_submit() - Submit commands through GuC
  528. * @engine: engine associated with the commands
  529. *
  530. * The only error here arises if the doorbell hardware isn't functioning
  531. * as expected, which really shouln't happen.
  532. */
  533. static void guc_submit(struct intel_engine_cs *engine)
  534. {
  535. struct intel_guc *guc = &engine->i915->guc;
  536. struct intel_engine_execlists * const execlists = &engine->execlists;
  537. struct execlist_port *port = execlists->port;
  538. unsigned int n;
  539. for (n = 0; n < execlists_num_ports(execlists); n++) {
  540. struct i915_request *rq;
  541. unsigned int count;
  542. rq = port_unpack(&port[n], &count);
  543. if (rq && count == 0) {
  544. port_set(&port[n], port_pack(rq, ++count));
  545. flush_ggtt_writes(rq->ring->vma);
  546. guc_add_request(guc, rq);
  547. }
  548. }
  549. }
  550. static void port_assign(struct execlist_port *port, struct i915_request *rq)
  551. {
  552. GEM_BUG_ON(port_isset(port));
  553. port_set(port, i915_request_get(rq));
  554. }
  555. static inline int rq_prio(const struct i915_request *rq)
  556. {
  557. return rq->sched.attr.priority;
  558. }
  559. static inline int port_prio(const struct execlist_port *port)
  560. {
  561. return rq_prio(port_request(port));
  562. }
  563. static bool __guc_dequeue(struct intel_engine_cs *engine)
  564. {
  565. struct intel_engine_execlists * const execlists = &engine->execlists;
  566. struct execlist_port *port = execlists->port;
  567. struct i915_request *last = NULL;
  568. const struct execlist_port * const last_port =
  569. &execlists->port[execlists->port_mask];
  570. bool submit = false;
  571. struct rb_node *rb;
  572. lockdep_assert_held(&engine->timeline.lock);
  573. rb = execlists->first;
  574. GEM_BUG_ON(rb_first(&execlists->queue) != rb);
  575. if (port_isset(port)) {
  576. if (intel_engine_has_preemption(engine)) {
  577. struct guc_preempt_work *preempt_work =
  578. &engine->i915->guc.preempt_work[engine->id];
  579. int prio = execlists->queue_priority;
  580. if (__execlists_need_preempt(prio, port_prio(port))) {
  581. execlists_set_active(execlists,
  582. EXECLISTS_ACTIVE_PREEMPT);
  583. queue_work(engine->i915->guc.preempt_wq,
  584. &preempt_work->work);
  585. return false;
  586. }
  587. }
  588. port++;
  589. if (port_isset(port))
  590. return false;
  591. }
  592. GEM_BUG_ON(port_isset(port));
  593. while (rb) {
  594. struct i915_priolist *p = to_priolist(rb);
  595. struct i915_request *rq, *rn;
  596. list_for_each_entry_safe(rq, rn, &p->requests, sched.link) {
  597. if (last && rq->ctx != last->ctx) {
  598. if (port == last_port) {
  599. __list_del_many(&p->requests,
  600. &rq->sched.link);
  601. goto done;
  602. }
  603. if (submit)
  604. port_assign(port, last);
  605. port++;
  606. }
  607. INIT_LIST_HEAD(&rq->sched.link);
  608. __i915_request_submit(rq);
  609. trace_i915_request_in(rq, port_index(port, execlists));
  610. last = rq;
  611. submit = true;
  612. }
  613. rb = rb_next(rb);
  614. rb_erase(&p->node, &execlists->queue);
  615. INIT_LIST_HEAD(&p->requests);
  616. if (p->priority != I915_PRIORITY_NORMAL)
  617. kmem_cache_free(engine->i915->priorities, p);
  618. }
  619. done:
  620. execlists->queue_priority = rb ? to_priolist(rb)->priority : INT_MIN;
  621. execlists->first = rb;
  622. if (submit)
  623. port_assign(port, last);
  624. if (last)
  625. execlists_user_begin(execlists, execlists->port);
  626. /* We must always keep the beast fed if we have work piled up */
  627. GEM_BUG_ON(port_isset(execlists->port) &&
  628. !execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
  629. GEM_BUG_ON(execlists->first && !port_isset(execlists->port));
  630. return submit;
  631. }
  632. static void guc_dequeue(struct intel_engine_cs *engine)
  633. {
  634. unsigned long flags;
  635. bool submit;
  636. local_irq_save(flags);
  637. spin_lock(&engine->timeline.lock);
  638. submit = __guc_dequeue(engine);
  639. spin_unlock(&engine->timeline.lock);
  640. if (submit)
  641. guc_submit(engine);
  642. local_irq_restore(flags);
  643. }
  644. static void guc_submission_tasklet(unsigned long data)
  645. {
  646. struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
  647. struct intel_engine_execlists * const execlists = &engine->execlists;
  648. struct execlist_port *port = execlists->port;
  649. struct i915_request *rq;
  650. rq = port_request(port);
  651. while (rq && i915_request_completed(rq)) {
  652. trace_i915_request_out(rq);
  653. i915_request_put(rq);
  654. port = execlists_port_complete(execlists, port);
  655. if (port_isset(port)) {
  656. execlists_user_begin(execlists, port);
  657. rq = port_request(port);
  658. } else {
  659. execlists_user_end(execlists);
  660. rq = NULL;
  661. }
  662. }
  663. if (execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT) &&
  664. intel_read_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX) ==
  665. GUC_PREEMPT_FINISHED) {
  666. execlists_cancel_port_requests(&engine->execlists);
  667. execlists_unwind_incomplete_requests(execlists);
  668. wait_for_guc_preempt_report(engine);
  669. execlists_clear_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
  670. intel_write_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX, 0);
  671. }
  672. if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
  673. guc_dequeue(engine);
  674. }
  675. /*
  676. * Everything below here is concerned with setup & teardown, and is
  677. * therefore not part of the somewhat time-critical batch-submission
  678. * path of guc_submit() above.
  679. */
  680. /* Check that a doorbell register is in the expected state */
  681. static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
  682. {
  683. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  684. u32 drbregl;
  685. bool valid;
  686. GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
  687. drbregl = I915_READ(GEN8_DRBREGL(db_id));
  688. valid = drbregl & GEN8_DRB_VALID;
  689. if (test_bit(db_id, guc->doorbell_bitmap) == valid)
  690. return true;
  691. DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
  692. db_id, drbregl, yesno(valid));
  693. return false;
  694. }
  695. static bool guc_verify_doorbells(struct intel_guc *guc)
  696. {
  697. u16 db_id;
  698. for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
  699. if (!doorbell_ok(guc, db_id))
  700. return false;
  701. return true;
  702. }
  703. static int guc_clients_doorbell_init(struct intel_guc *guc)
  704. {
  705. int ret;
  706. ret = create_doorbell(guc->execbuf_client);
  707. if (ret)
  708. return ret;
  709. if (guc->preempt_client) {
  710. ret = create_doorbell(guc->preempt_client);
  711. if (ret) {
  712. destroy_doorbell(guc->execbuf_client);
  713. return ret;
  714. }
  715. }
  716. return 0;
  717. }
  718. static void guc_clients_doorbell_fini(struct intel_guc *guc)
  719. {
  720. /*
  721. * By the time we're here, GuC has already been reset.
  722. * Instead of trying (in vain) to communicate with it, let's just
  723. * cleanup the doorbell HW and our internal state.
  724. */
  725. if (guc->preempt_client) {
  726. __destroy_doorbell(guc->preempt_client);
  727. __update_doorbell_desc(guc->preempt_client,
  728. GUC_DOORBELL_INVALID);
  729. }
  730. __destroy_doorbell(guc->execbuf_client);
  731. __update_doorbell_desc(guc->execbuf_client, GUC_DOORBELL_INVALID);
  732. }
  733. /**
  734. * guc_client_alloc() - Allocate an intel_guc_client
  735. * @dev_priv: driver private data structure
  736. * @engines: The set of engines to enable for this client
  737. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  738. * The kernel client to replace ExecList submission is created with
  739. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  740. * while a preemption context can use CRITICAL.
  741. * @ctx: the context that owns the client (we use the default render
  742. * context)
  743. *
  744. * Return: An intel_guc_client object if success, else NULL.
  745. */
  746. static struct intel_guc_client *
  747. guc_client_alloc(struct drm_i915_private *dev_priv,
  748. u32 engines,
  749. u32 priority,
  750. struct i915_gem_context *ctx)
  751. {
  752. struct intel_guc_client *client;
  753. struct intel_guc *guc = &dev_priv->guc;
  754. struct i915_vma *vma;
  755. void *vaddr;
  756. int ret;
  757. client = kzalloc(sizeof(*client), GFP_KERNEL);
  758. if (!client)
  759. return ERR_PTR(-ENOMEM);
  760. client->guc = guc;
  761. client->owner = ctx;
  762. client->engines = engines;
  763. client->priority = priority;
  764. client->doorbell_id = GUC_DOORBELL_INVALID;
  765. spin_lock_init(&client->wq_lock);
  766. ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
  767. GFP_KERNEL);
  768. if (ret < 0)
  769. goto err_client;
  770. client->stage_id = ret;
  771. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  772. vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
  773. if (IS_ERR(vma)) {
  774. ret = PTR_ERR(vma);
  775. goto err_id;
  776. }
  777. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  778. client->vma = vma;
  779. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  780. if (IS_ERR(vaddr)) {
  781. ret = PTR_ERR(vaddr);
  782. goto err_vma;
  783. }
  784. client->vaddr = vaddr;
  785. client->doorbell_offset = __select_cacheline(guc);
  786. /*
  787. * Since the doorbell only requires a single cacheline, we can save
  788. * space by putting the application process descriptor in the same
  789. * page. Use the half of the page that doesn't include the doorbell.
  790. */
  791. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  792. client->proc_desc_offset = 0;
  793. else
  794. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  795. guc_proc_desc_init(guc, client);
  796. guc_stage_desc_init(guc, client);
  797. ret = reserve_doorbell(client);
  798. if (ret)
  799. goto err_vaddr;
  800. DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
  801. priority, client, client->engines, client->stage_id);
  802. DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
  803. client->doorbell_id, client->doorbell_offset);
  804. return client;
  805. err_vaddr:
  806. i915_gem_object_unpin_map(client->vma->obj);
  807. err_vma:
  808. i915_vma_unpin_and_release(&client->vma);
  809. err_id:
  810. ida_simple_remove(&guc->stage_ids, client->stage_id);
  811. err_client:
  812. kfree(client);
  813. return ERR_PTR(ret);
  814. }
  815. static void guc_client_free(struct intel_guc_client *client)
  816. {
  817. unreserve_doorbell(client);
  818. guc_stage_desc_fini(client->guc, client);
  819. i915_gem_object_unpin_map(client->vma->obj);
  820. i915_vma_unpin_and_release(&client->vma);
  821. ida_simple_remove(&client->guc->stage_ids, client->stage_id);
  822. kfree(client);
  823. }
  824. static inline bool ctx_save_restore_disabled(struct intel_context *ce)
  825. {
  826. u32 sr = ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1];
  827. #define SR_DISABLED \
  828. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT | \
  829. CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)
  830. return (sr & SR_DISABLED) == SR_DISABLED;
  831. #undef SR_DISABLED
  832. }
  833. static void guc_fill_preempt_context(struct intel_guc *guc)
  834. {
  835. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  836. struct intel_guc_client *client = guc->preempt_client;
  837. struct intel_engine_cs *engine;
  838. enum intel_engine_id id;
  839. for_each_engine(engine, dev_priv, id) {
  840. struct intel_context *ce =
  841. to_intel_context(client->owner, engine);
  842. u32 addr = intel_hws_preempt_done_address(engine);
  843. u32 *cs;
  844. GEM_BUG_ON(!ce->pin_count);
  845. /*
  846. * We rely on this context image *not* being saved after
  847. * preemption. This ensures that the RING_HEAD / RING_TAIL
  848. * remain pointing at initial values forever.
  849. */
  850. GEM_BUG_ON(!ctx_save_restore_disabled(ce));
  851. cs = ce->ring->vaddr;
  852. if (id == RCS) {
  853. cs = gen8_emit_ggtt_write_rcs(cs,
  854. GUC_PREEMPT_FINISHED,
  855. addr);
  856. } else {
  857. cs = gen8_emit_ggtt_write(cs,
  858. GUC_PREEMPT_FINISHED,
  859. addr);
  860. *cs++ = MI_NOOP;
  861. *cs++ = MI_NOOP;
  862. }
  863. *cs++ = MI_USER_INTERRUPT;
  864. *cs++ = MI_NOOP;
  865. GEM_BUG_ON((void *)cs - ce->ring->vaddr !=
  866. GUC_PREEMPT_BREADCRUMB_BYTES);
  867. flush_ggtt_writes(ce->ring->vma);
  868. }
  869. }
  870. static int guc_clients_create(struct intel_guc *guc)
  871. {
  872. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  873. struct intel_guc_client *client;
  874. GEM_BUG_ON(guc->execbuf_client);
  875. GEM_BUG_ON(guc->preempt_client);
  876. client = guc_client_alloc(dev_priv,
  877. INTEL_INFO(dev_priv)->ring_mask,
  878. GUC_CLIENT_PRIORITY_KMD_NORMAL,
  879. dev_priv->kernel_context);
  880. if (IS_ERR(client)) {
  881. DRM_ERROR("Failed to create GuC client for submission!\n");
  882. return PTR_ERR(client);
  883. }
  884. guc->execbuf_client = client;
  885. if (dev_priv->preempt_context) {
  886. client = guc_client_alloc(dev_priv,
  887. INTEL_INFO(dev_priv)->ring_mask,
  888. GUC_CLIENT_PRIORITY_KMD_HIGH,
  889. dev_priv->preempt_context);
  890. if (IS_ERR(client)) {
  891. DRM_ERROR("Failed to create GuC client for preemption!\n");
  892. guc_client_free(guc->execbuf_client);
  893. guc->execbuf_client = NULL;
  894. return PTR_ERR(client);
  895. }
  896. guc->preempt_client = client;
  897. guc_fill_preempt_context(guc);
  898. }
  899. return 0;
  900. }
  901. static void guc_clients_destroy(struct intel_guc *guc)
  902. {
  903. struct intel_guc_client *client;
  904. client = fetch_and_zero(&guc->preempt_client);
  905. if (client)
  906. guc_client_free(client);
  907. client = fetch_and_zero(&guc->execbuf_client);
  908. guc_client_free(client);
  909. }
  910. /*
  911. * Set up the memory resources to be shared with the GuC (via the GGTT)
  912. * at firmware loading time.
  913. */
  914. int intel_guc_submission_init(struct intel_guc *guc)
  915. {
  916. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  917. struct intel_engine_cs *engine;
  918. enum intel_engine_id id;
  919. int ret;
  920. if (guc->stage_desc_pool)
  921. return 0;
  922. ret = guc_stage_desc_pool_create(guc);
  923. if (ret)
  924. return ret;
  925. /*
  926. * Keep static analysers happy, let them know that we allocated the
  927. * vma after testing that it didn't exist earlier.
  928. */
  929. GEM_BUG_ON(!guc->stage_desc_pool);
  930. WARN_ON(!guc_verify_doorbells(guc));
  931. ret = guc_clients_create(guc);
  932. if (ret)
  933. return ret;
  934. for_each_engine(engine, dev_priv, id) {
  935. guc->preempt_work[id].engine = engine;
  936. INIT_WORK(&guc->preempt_work[id].work, inject_preempt_context);
  937. }
  938. return 0;
  939. }
  940. void intel_guc_submission_fini(struct intel_guc *guc)
  941. {
  942. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  943. struct intel_engine_cs *engine;
  944. enum intel_engine_id id;
  945. for_each_engine(engine, dev_priv, id)
  946. cancel_work_sync(&guc->preempt_work[id].work);
  947. guc_clients_destroy(guc);
  948. WARN_ON(!guc_verify_doorbells(guc));
  949. guc_stage_desc_pool_destroy(guc);
  950. }
  951. static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
  952. {
  953. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  954. struct intel_engine_cs *engine;
  955. enum intel_engine_id id;
  956. int irqs;
  957. /* tell all command streamers to forward interrupts (but not vblank)
  958. * to GuC
  959. */
  960. irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
  961. for_each_engine(engine, dev_priv, id)
  962. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  963. /* route USER_INTERRUPT to Host, all others are sent to GuC. */
  964. irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  965. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  966. /* These three registers have the same bit definitions */
  967. I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
  968. I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
  969. I915_WRITE(GUC_WD_VECS_IER, ~irqs);
  970. /*
  971. * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
  972. * (unmasked) PM interrupts to the GuC. All other bits of this
  973. * register *disable* generation of a specific interrupt.
  974. *
  975. * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
  976. * writing to the PM interrupt mask register, i.e. interrupts
  977. * that must not be disabled.
  978. *
  979. * If the GuC is handling these interrupts, then we must not let
  980. * the PM code disable ANY interrupt that the GuC is expecting.
  981. * So for each ENABLED (0) bit in this register, we must SET the
  982. * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
  983. * GuC needs ARAT expired interrupt unmasked hence it is set in
  984. * pm_intrmsk_mbz.
  985. *
  986. * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
  987. * result in the register bit being left SET!
  988. */
  989. rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
  990. rps->pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  991. }
  992. static void guc_interrupts_release(struct drm_i915_private *dev_priv)
  993. {
  994. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  995. struct intel_engine_cs *engine;
  996. enum intel_engine_id id;
  997. int irqs;
  998. /*
  999. * tell all command streamers NOT to forward interrupts or vblank
  1000. * to GuC.
  1001. */
  1002. irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
  1003. irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
  1004. for_each_engine(engine, dev_priv, id)
  1005. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  1006. /* route all GT interrupts to the host */
  1007. I915_WRITE(GUC_BCS_RCS_IER, 0);
  1008. I915_WRITE(GUC_VCS2_VCS1_IER, 0);
  1009. I915_WRITE(GUC_WD_VECS_IER, 0);
  1010. rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  1011. rps->pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
  1012. }
  1013. static void guc_submission_park(struct intel_engine_cs *engine)
  1014. {
  1015. intel_engine_unpin_breadcrumbs_irq(engine);
  1016. }
  1017. static void guc_submission_unpark(struct intel_engine_cs *engine)
  1018. {
  1019. intel_engine_pin_breadcrumbs_irq(engine);
  1020. }
  1021. int intel_guc_submission_enable(struct intel_guc *guc)
  1022. {
  1023. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1024. struct intel_engine_cs *engine;
  1025. enum intel_engine_id id;
  1026. int err;
  1027. /*
  1028. * We're using GuC work items for submitting work through GuC. Since
  1029. * we're coalescing multiple requests from a single context into a
  1030. * single work item prior to assigning it to execlist_port, we can
  1031. * never have more work items than the total number of ports (for all
  1032. * engines). The GuC firmware is controlling the HEAD of work queue,
  1033. * and it is guaranteed that it will remove the work item from the
  1034. * queue before our request is completed.
  1035. */
  1036. BUILD_BUG_ON(ARRAY_SIZE(engine->execlists.port) *
  1037. sizeof(struct guc_wq_item) *
  1038. I915_NUM_ENGINES > GUC_WQ_SIZE);
  1039. GEM_BUG_ON(!guc->execbuf_client);
  1040. guc_reset_wq(guc->execbuf_client);
  1041. if (guc->preempt_client)
  1042. guc_reset_wq(guc->preempt_client);
  1043. err = intel_guc_sample_forcewake(guc);
  1044. if (err)
  1045. return err;
  1046. err = guc_clients_doorbell_init(guc);
  1047. if (err)
  1048. return err;
  1049. /* Take over from manual control of ELSP (execlists) */
  1050. guc_interrupts_capture(dev_priv);
  1051. for_each_engine(engine, dev_priv, id) {
  1052. struct intel_engine_execlists * const execlists =
  1053. &engine->execlists;
  1054. execlists->tasklet.func = guc_submission_tasklet;
  1055. engine->park = guc_submission_park;
  1056. engine->unpark = guc_submission_unpark;
  1057. engine->flags &= ~I915_ENGINE_SUPPORTS_STATS;
  1058. }
  1059. return 0;
  1060. }
  1061. void intel_guc_submission_disable(struct intel_guc *guc)
  1062. {
  1063. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1064. GEM_BUG_ON(dev_priv->gt.awake); /* GT should be parked first */
  1065. guc_interrupts_release(dev_priv);
  1066. guc_clients_doorbell_fini(guc);
  1067. /* Revert back to manual ELSP submission */
  1068. intel_engines_reset_default_submission(dev_priv);
  1069. }
  1070. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1071. #include "selftests/intel_guc.c"
  1072. #endif