intel_guc_submission.c 39 KB

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