intel_guc_submission.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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(rq->hw_context->lrc_desc);
  431. u32 ring_tail = intel_ring_set_tail(rq->ring, rq->tail) / sizeof(u64);
  432. spin_lock(&client->wq_lock);
  433. guc_wq_item_append(client, engine->guc_id, ctx_desc,
  434. ring_tail, rq->global_seqno);
  435. guc_ring_doorbell(client);
  436. client->submissions[engine->id] += 1;
  437. spin_unlock(&client->wq_lock);
  438. }
  439. /*
  440. * When we're doing submissions using regular execlists backend, writing to
  441. * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
  442. * pinned in mappable aperture portion of GGTT are visible to command streamer.
  443. * Writes done by GuC on our behalf are not guaranteeing such ordering,
  444. * therefore, to ensure the flush, we're issuing a POSTING READ.
  445. */
  446. static void flush_ggtt_writes(struct i915_vma *vma)
  447. {
  448. struct drm_i915_private *dev_priv = vma->vm->i915;
  449. if (i915_vma_is_map_and_fenceable(vma))
  450. POSTING_READ_FW(GUC_STATUS);
  451. }
  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. u32 ctx_desc = lower_32_bits(to_intel_context(client->owner,
  462. engine)->lrc_desc);
  463. u32 data[7];
  464. /*
  465. * The ring contains commands to write GUC_PREEMPT_FINISHED into HWSP.
  466. * See guc_fill_preempt_context().
  467. */
  468. spin_lock_irq(&client->wq_lock);
  469. guc_wq_item_append(client, engine->guc_id, ctx_desc,
  470. GUC_PREEMPT_BREADCRUMB_BYTES / sizeof(u64), 0);
  471. spin_unlock_irq(&client->wq_lock);
  472. /*
  473. * If GuC firmware performs an engine reset while that engine had
  474. * a preemption pending, it will set the terminated attribute bit
  475. * on our preemption stage descriptor. GuC firmware retains all
  476. * pending work items for a high-priority GuC client, unlike the
  477. * normal-priority GuC client where work items are dropped. It
  478. * wants to make sure the preempt-to-idle work doesn't run when
  479. * scheduling resumes, and uses this bit to inform its scheduler
  480. * and presumably us as well. Our job is to clear it for the next
  481. * preemption after reset, otherwise that and future preemptions
  482. * will never complete. We'll just clear it every time.
  483. */
  484. stage_desc->attribute &= ~GUC_STAGE_DESC_ATTR_TERMINATED;
  485. data[0] = INTEL_GUC_ACTION_REQUEST_PREEMPTION;
  486. data[1] = client->stage_id;
  487. data[2] = INTEL_GUC_PREEMPT_OPTION_DROP_WORK_Q |
  488. INTEL_GUC_PREEMPT_OPTION_DROP_SUBMIT_Q;
  489. data[3] = engine->guc_id;
  490. data[4] = guc->execbuf_client->priority;
  491. data[5] = guc->execbuf_client->stage_id;
  492. data[6] = intel_guc_ggtt_offset(guc, guc->shared_data);
  493. if (WARN_ON(intel_guc_send(guc, data, ARRAY_SIZE(data)))) {
  494. execlists_clear_active(&engine->execlists,
  495. EXECLISTS_ACTIVE_PREEMPT);
  496. tasklet_schedule(&engine->execlists.tasklet);
  497. }
  498. }
  499. /*
  500. * We're using user interrupt and HWSP value to mark that preemption has
  501. * finished and GPU is idle. Normally, we could unwind and continue similar to
  502. * execlists submission path. Unfortunately, with GuC we also need to wait for
  503. * it to finish its own postprocessing, before attempting to submit. Otherwise
  504. * GuC may silently ignore our submissions, and thus we risk losing request at
  505. * best, executing out-of-order and causing kernel panic at worst.
  506. */
  507. #define GUC_PREEMPT_POSTPROCESS_DELAY_MS 10
  508. static void wait_for_guc_preempt_report(struct intel_engine_cs *engine)
  509. {
  510. struct intel_guc *guc = &engine->i915->guc;
  511. struct guc_shared_ctx_data *data = guc->shared_data_vaddr;
  512. struct guc_ctx_report *report =
  513. &data->preempt_ctx_report[engine->guc_id];
  514. WARN_ON(wait_for_atomic(report->report_return_status ==
  515. INTEL_GUC_REPORT_STATUS_COMPLETE,
  516. GUC_PREEMPT_POSTPROCESS_DELAY_MS));
  517. /*
  518. * GuC is expecting that we're also going to clear the affected context
  519. * counter, let's also reset the return status to not depend on GuC
  520. * resetting it after recieving another preempt action
  521. */
  522. report->affected_count = 0;
  523. report->report_return_status = INTEL_GUC_REPORT_STATUS_UNKNOWN;
  524. }
  525. static void complete_preempt_context(struct intel_engine_cs *engine)
  526. {
  527. struct intel_engine_execlists *execlists = &engine->execlists;
  528. GEM_BUG_ON(!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT));
  529. execlists_cancel_port_requests(execlists);
  530. execlists_unwind_incomplete_requests(execlists);
  531. wait_for_guc_preempt_report(engine);
  532. intel_write_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX, 0);
  533. execlists_clear_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
  534. }
  535. /**
  536. * guc_submit() - Submit commands through GuC
  537. * @engine: engine associated with the commands
  538. *
  539. * The only error here arises if the doorbell hardware isn't functioning
  540. * as expected, which really shouln't happen.
  541. */
  542. static void guc_submit(struct intel_engine_cs *engine)
  543. {
  544. struct intel_guc *guc = &engine->i915->guc;
  545. struct intel_engine_execlists * const execlists = &engine->execlists;
  546. struct execlist_port *port = execlists->port;
  547. unsigned int n;
  548. for (n = 0; n < execlists_num_ports(execlists); n++) {
  549. struct i915_request *rq;
  550. unsigned int count;
  551. rq = port_unpack(&port[n], &count);
  552. if (rq && count == 0) {
  553. port_set(&port[n], port_pack(rq, ++count));
  554. flush_ggtt_writes(rq->ring->vma);
  555. guc_add_request(guc, rq);
  556. }
  557. }
  558. }
  559. static void port_assign(struct execlist_port *port, struct i915_request *rq)
  560. {
  561. GEM_BUG_ON(port_isset(port));
  562. port_set(port, i915_request_get(rq));
  563. }
  564. static inline int rq_prio(const struct i915_request *rq)
  565. {
  566. return rq->sched.attr.priority;
  567. }
  568. static inline int port_prio(const struct execlist_port *port)
  569. {
  570. return rq_prio(port_request(port));
  571. }
  572. static bool __guc_dequeue(struct intel_engine_cs *engine)
  573. {
  574. struct intel_engine_execlists * const execlists = &engine->execlists;
  575. struct execlist_port *port = execlists->port;
  576. struct i915_request *last = NULL;
  577. const struct execlist_port * const last_port =
  578. &execlists->port[execlists->port_mask];
  579. bool submit = false;
  580. struct rb_node *rb;
  581. lockdep_assert_held(&engine->timeline.lock);
  582. rb = execlists->first;
  583. GEM_BUG_ON(rb_first(&execlists->queue) != rb);
  584. if (port_isset(port)) {
  585. if (intel_engine_has_preemption(engine)) {
  586. struct guc_preempt_work *preempt_work =
  587. &engine->i915->guc.preempt_work[engine->id];
  588. int prio = execlists->queue_priority;
  589. if (__execlists_need_preempt(prio, port_prio(port))) {
  590. execlists_set_active(execlists,
  591. EXECLISTS_ACTIVE_PREEMPT);
  592. queue_work(engine->i915->guc.preempt_wq,
  593. &preempt_work->work);
  594. return false;
  595. }
  596. }
  597. port++;
  598. if (port_isset(port))
  599. return false;
  600. }
  601. GEM_BUG_ON(port_isset(port));
  602. while (rb) {
  603. struct i915_priolist *p = to_priolist(rb);
  604. struct i915_request *rq, *rn;
  605. list_for_each_entry_safe(rq, rn, &p->requests, sched.link) {
  606. if (last && rq->hw_context != last->hw_context) {
  607. if (port == last_port) {
  608. __list_del_many(&p->requests,
  609. &rq->sched.link);
  610. goto done;
  611. }
  612. if (submit)
  613. port_assign(port, last);
  614. port++;
  615. }
  616. INIT_LIST_HEAD(&rq->sched.link);
  617. __i915_request_submit(rq);
  618. trace_i915_request_in(rq, port_index(port, execlists));
  619. last = rq;
  620. submit = true;
  621. }
  622. rb = rb_next(rb);
  623. rb_erase(&p->node, &execlists->queue);
  624. INIT_LIST_HEAD(&p->requests);
  625. if (p->priority != I915_PRIORITY_NORMAL)
  626. kmem_cache_free(engine->i915->priorities, p);
  627. }
  628. done:
  629. execlists->queue_priority = rb ? to_priolist(rb)->priority : INT_MIN;
  630. execlists->first = rb;
  631. if (submit)
  632. port_assign(port, last);
  633. if (last)
  634. execlists_user_begin(execlists, execlists->port);
  635. /* We must always keep the beast fed if we have work piled up */
  636. GEM_BUG_ON(port_isset(execlists->port) &&
  637. !execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
  638. GEM_BUG_ON(execlists->first && !port_isset(execlists->port));
  639. return submit;
  640. }
  641. static void guc_dequeue(struct intel_engine_cs *engine)
  642. {
  643. unsigned long flags;
  644. bool submit;
  645. local_irq_save(flags);
  646. spin_lock(&engine->timeline.lock);
  647. submit = __guc_dequeue(engine);
  648. spin_unlock(&engine->timeline.lock);
  649. if (submit)
  650. guc_submit(engine);
  651. local_irq_restore(flags);
  652. }
  653. static void guc_submission_tasklet(unsigned long data)
  654. {
  655. struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
  656. struct intel_engine_execlists * const execlists = &engine->execlists;
  657. struct execlist_port *port = execlists->port;
  658. struct i915_request *rq;
  659. rq = port_request(port);
  660. while (rq && i915_request_completed(rq)) {
  661. trace_i915_request_out(rq);
  662. i915_request_put(rq);
  663. port = execlists_port_complete(execlists, port);
  664. if (port_isset(port)) {
  665. execlists_user_begin(execlists, port);
  666. rq = port_request(port);
  667. } else {
  668. execlists_user_end(execlists);
  669. rq = NULL;
  670. }
  671. }
  672. if (execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT) &&
  673. intel_read_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX) ==
  674. GUC_PREEMPT_FINISHED)
  675. complete_preempt_context(engine);
  676. if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
  677. guc_dequeue(engine);
  678. }
  679. static struct i915_request *
  680. guc_reset_prepare(struct intel_engine_cs *engine)
  681. {
  682. struct intel_engine_execlists * const execlists = &engine->execlists;
  683. GEM_TRACE("%s\n", engine->name);
  684. /*
  685. * Prevent request submission to the hardware until we have
  686. * completed the reset in i915_gem_reset_finish(). If a request
  687. * is completed by one engine, it may then queue a request
  688. * to a second via its execlists->tasklet *just* as we are
  689. * calling engine->init_hw() and also writing the ELSP.
  690. * Turning off the execlists->tasklet until the reset is over
  691. * prevents the race.
  692. */
  693. __tasklet_disable_sync_once(&execlists->tasklet);
  694. /*
  695. * We're using worker to queue preemption requests from the tasklet in
  696. * GuC submission mode.
  697. * Even though tasklet was disabled, we may still have a worker queued.
  698. * Let's make sure that all workers scheduled before disabling the
  699. * tasklet are completed before continuing with the reset.
  700. */
  701. if (engine->i915->guc.preempt_wq)
  702. flush_workqueue(engine->i915->guc.preempt_wq);
  703. return i915_gem_find_active_request(engine);
  704. }
  705. /*
  706. * Everything below here is concerned with setup & teardown, and is
  707. * therefore not part of the somewhat time-critical batch-submission
  708. * path of guc_submit() above.
  709. */
  710. /* Check that a doorbell register is in the expected state */
  711. static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
  712. {
  713. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  714. u32 drbregl;
  715. bool valid;
  716. GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
  717. drbregl = I915_READ(GEN8_DRBREGL(db_id));
  718. valid = drbregl & GEN8_DRB_VALID;
  719. if (test_bit(db_id, guc->doorbell_bitmap) == valid)
  720. return true;
  721. DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
  722. db_id, drbregl, yesno(valid));
  723. return false;
  724. }
  725. static bool guc_verify_doorbells(struct intel_guc *guc)
  726. {
  727. u16 db_id;
  728. for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
  729. if (!doorbell_ok(guc, db_id))
  730. return false;
  731. return true;
  732. }
  733. static int guc_clients_doorbell_init(struct intel_guc *guc)
  734. {
  735. int ret;
  736. ret = create_doorbell(guc->execbuf_client);
  737. if (ret)
  738. return ret;
  739. if (guc->preempt_client) {
  740. ret = create_doorbell(guc->preempt_client);
  741. if (ret) {
  742. destroy_doorbell(guc->execbuf_client);
  743. return ret;
  744. }
  745. }
  746. return 0;
  747. }
  748. static void guc_clients_doorbell_fini(struct intel_guc *guc)
  749. {
  750. /*
  751. * By the time we're here, GuC has already been reset.
  752. * Instead of trying (in vain) to communicate with it, let's just
  753. * cleanup the doorbell HW and our internal state.
  754. */
  755. if (guc->preempt_client) {
  756. __destroy_doorbell(guc->preempt_client);
  757. __update_doorbell_desc(guc->preempt_client,
  758. GUC_DOORBELL_INVALID);
  759. }
  760. __destroy_doorbell(guc->execbuf_client);
  761. __update_doorbell_desc(guc->execbuf_client, GUC_DOORBELL_INVALID);
  762. }
  763. /**
  764. * guc_client_alloc() - Allocate an intel_guc_client
  765. * @dev_priv: driver private data structure
  766. * @engines: The set of engines to enable for this client
  767. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  768. * The kernel client to replace ExecList submission is created with
  769. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  770. * while a preemption context can use CRITICAL.
  771. * @ctx: the context that owns the client (we use the default render
  772. * context)
  773. *
  774. * Return: An intel_guc_client object if success, else NULL.
  775. */
  776. static struct intel_guc_client *
  777. guc_client_alloc(struct drm_i915_private *dev_priv,
  778. u32 engines,
  779. u32 priority,
  780. struct i915_gem_context *ctx)
  781. {
  782. struct intel_guc_client *client;
  783. struct intel_guc *guc = &dev_priv->guc;
  784. struct i915_vma *vma;
  785. void *vaddr;
  786. int ret;
  787. client = kzalloc(sizeof(*client), GFP_KERNEL);
  788. if (!client)
  789. return ERR_PTR(-ENOMEM);
  790. client->guc = guc;
  791. client->owner = ctx;
  792. client->engines = engines;
  793. client->priority = priority;
  794. client->doorbell_id = GUC_DOORBELL_INVALID;
  795. spin_lock_init(&client->wq_lock);
  796. ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
  797. GFP_KERNEL);
  798. if (ret < 0)
  799. goto err_client;
  800. client->stage_id = ret;
  801. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  802. vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
  803. if (IS_ERR(vma)) {
  804. ret = PTR_ERR(vma);
  805. goto err_id;
  806. }
  807. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  808. client->vma = vma;
  809. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  810. if (IS_ERR(vaddr)) {
  811. ret = PTR_ERR(vaddr);
  812. goto err_vma;
  813. }
  814. client->vaddr = vaddr;
  815. client->doorbell_offset = __select_cacheline(guc);
  816. /*
  817. * Since the doorbell only requires a single cacheline, we can save
  818. * space by putting the application process descriptor in the same
  819. * page. Use the half of the page that doesn't include the doorbell.
  820. */
  821. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  822. client->proc_desc_offset = 0;
  823. else
  824. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  825. guc_proc_desc_init(guc, client);
  826. guc_stage_desc_init(guc, client);
  827. ret = reserve_doorbell(client);
  828. if (ret)
  829. goto err_vaddr;
  830. DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
  831. priority, client, client->engines, client->stage_id);
  832. DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
  833. client->doorbell_id, client->doorbell_offset);
  834. return client;
  835. err_vaddr:
  836. i915_gem_object_unpin_map(client->vma->obj);
  837. err_vma:
  838. i915_vma_unpin_and_release(&client->vma);
  839. err_id:
  840. ida_simple_remove(&guc->stage_ids, client->stage_id);
  841. err_client:
  842. kfree(client);
  843. return ERR_PTR(ret);
  844. }
  845. static void guc_client_free(struct intel_guc_client *client)
  846. {
  847. unreserve_doorbell(client);
  848. guc_stage_desc_fini(client->guc, client);
  849. i915_gem_object_unpin_map(client->vma->obj);
  850. i915_vma_unpin_and_release(&client->vma);
  851. ida_simple_remove(&client->guc->stage_ids, client->stage_id);
  852. kfree(client);
  853. }
  854. static inline bool ctx_save_restore_disabled(struct intel_context *ce)
  855. {
  856. u32 sr = ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1];
  857. #define SR_DISABLED \
  858. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT | \
  859. CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)
  860. return (sr & SR_DISABLED) == SR_DISABLED;
  861. #undef SR_DISABLED
  862. }
  863. static void guc_fill_preempt_context(struct intel_guc *guc)
  864. {
  865. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  866. struct intel_guc_client *client = guc->preempt_client;
  867. struct intel_engine_cs *engine;
  868. enum intel_engine_id id;
  869. for_each_engine(engine, dev_priv, id) {
  870. struct intel_context *ce =
  871. to_intel_context(client->owner, engine);
  872. u32 addr = intel_hws_preempt_done_address(engine);
  873. u32 *cs;
  874. GEM_BUG_ON(!ce->pin_count);
  875. /*
  876. * We rely on this context image *not* being saved after
  877. * preemption. This ensures that the RING_HEAD / RING_TAIL
  878. * remain pointing at initial values forever.
  879. */
  880. GEM_BUG_ON(!ctx_save_restore_disabled(ce));
  881. cs = ce->ring->vaddr;
  882. if (id == RCS) {
  883. cs = gen8_emit_ggtt_write_rcs(cs,
  884. GUC_PREEMPT_FINISHED,
  885. addr);
  886. } else {
  887. cs = gen8_emit_ggtt_write(cs,
  888. GUC_PREEMPT_FINISHED,
  889. addr);
  890. *cs++ = MI_NOOP;
  891. *cs++ = MI_NOOP;
  892. }
  893. *cs++ = MI_USER_INTERRUPT;
  894. *cs++ = MI_NOOP;
  895. GEM_BUG_ON((void *)cs - ce->ring->vaddr !=
  896. GUC_PREEMPT_BREADCRUMB_BYTES);
  897. flush_ggtt_writes(ce->ring->vma);
  898. }
  899. }
  900. static int guc_clients_create(struct intel_guc *guc)
  901. {
  902. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  903. struct intel_guc_client *client;
  904. GEM_BUG_ON(guc->execbuf_client);
  905. GEM_BUG_ON(guc->preempt_client);
  906. client = guc_client_alloc(dev_priv,
  907. INTEL_INFO(dev_priv)->ring_mask,
  908. GUC_CLIENT_PRIORITY_KMD_NORMAL,
  909. dev_priv->kernel_context);
  910. if (IS_ERR(client)) {
  911. DRM_ERROR("Failed to create GuC client for submission!\n");
  912. return PTR_ERR(client);
  913. }
  914. guc->execbuf_client = client;
  915. if (dev_priv->preempt_context) {
  916. client = guc_client_alloc(dev_priv,
  917. INTEL_INFO(dev_priv)->ring_mask,
  918. GUC_CLIENT_PRIORITY_KMD_HIGH,
  919. dev_priv->preempt_context);
  920. if (IS_ERR(client)) {
  921. DRM_ERROR("Failed to create GuC client for preemption!\n");
  922. guc_client_free(guc->execbuf_client);
  923. guc->execbuf_client = NULL;
  924. return PTR_ERR(client);
  925. }
  926. guc->preempt_client = client;
  927. guc_fill_preempt_context(guc);
  928. }
  929. return 0;
  930. }
  931. static void guc_clients_destroy(struct intel_guc *guc)
  932. {
  933. struct intel_guc_client *client;
  934. client = fetch_and_zero(&guc->preempt_client);
  935. if (client)
  936. guc_client_free(client);
  937. client = fetch_and_zero(&guc->execbuf_client);
  938. guc_client_free(client);
  939. }
  940. /*
  941. * Set up the memory resources to be shared with the GuC (via the GGTT)
  942. * at firmware loading time.
  943. */
  944. int intel_guc_submission_init(struct intel_guc *guc)
  945. {
  946. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  947. struct intel_engine_cs *engine;
  948. enum intel_engine_id id;
  949. int ret;
  950. if (guc->stage_desc_pool)
  951. return 0;
  952. ret = guc_stage_desc_pool_create(guc);
  953. if (ret)
  954. return ret;
  955. /*
  956. * Keep static analysers happy, let them know that we allocated the
  957. * vma after testing that it didn't exist earlier.
  958. */
  959. GEM_BUG_ON(!guc->stage_desc_pool);
  960. WARN_ON(!guc_verify_doorbells(guc));
  961. ret = guc_clients_create(guc);
  962. if (ret)
  963. goto err_pool;
  964. for_each_engine(engine, dev_priv, id) {
  965. guc->preempt_work[id].engine = engine;
  966. INIT_WORK(&guc->preempt_work[id].work, inject_preempt_context);
  967. }
  968. return 0;
  969. err_pool:
  970. guc_stage_desc_pool_destroy(guc);
  971. return ret;
  972. }
  973. void intel_guc_submission_fini(struct intel_guc *guc)
  974. {
  975. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  976. struct intel_engine_cs *engine;
  977. enum intel_engine_id id;
  978. for_each_engine(engine, dev_priv, id)
  979. cancel_work_sync(&guc->preempt_work[id].work);
  980. guc_clients_destroy(guc);
  981. WARN_ON(!guc_verify_doorbells(guc));
  982. guc_stage_desc_pool_destroy(guc);
  983. }
  984. static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
  985. {
  986. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  987. struct intel_engine_cs *engine;
  988. enum intel_engine_id id;
  989. int irqs;
  990. /* tell all command streamers to forward interrupts (but not vblank)
  991. * to GuC
  992. */
  993. irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
  994. for_each_engine(engine, dev_priv, id)
  995. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  996. /* route USER_INTERRUPT to Host, all others are sent to GuC. */
  997. irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  998. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  999. /* These three registers have the same bit definitions */
  1000. I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
  1001. I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
  1002. I915_WRITE(GUC_WD_VECS_IER, ~irqs);
  1003. /*
  1004. * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
  1005. * (unmasked) PM interrupts to the GuC. All other bits of this
  1006. * register *disable* generation of a specific interrupt.
  1007. *
  1008. * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
  1009. * writing to the PM interrupt mask register, i.e. interrupts
  1010. * that must not be disabled.
  1011. *
  1012. * If the GuC is handling these interrupts, then we must not let
  1013. * the PM code disable ANY interrupt that the GuC is expecting.
  1014. * So for each ENABLED (0) bit in this register, we must SET the
  1015. * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
  1016. * GuC needs ARAT expired interrupt unmasked hence it is set in
  1017. * pm_intrmsk_mbz.
  1018. *
  1019. * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
  1020. * result in the register bit being left SET!
  1021. */
  1022. rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
  1023. rps->pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  1024. }
  1025. static void guc_interrupts_release(struct drm_i915_private *dev_priv)
  1026. {
  1027. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  1028. struct intel_engine_cs *engine;
  1029. enum intel_engine_id id;
  1030. int irqs;
  1031. /*
  1032. * tell all command streamers NOT to forward interrupts or vblank
  1033. * to GuC.
  1034. */
  1035. irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
  1036. irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
  1037. for_each_engine(engine, dev_priv, id)
  1038. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  1039. /* route all GT interrupts to the host */
  1040. I915_WRITE(GUC_BCS_RCS_IER, 0);
  1041. I915_WRITE(GUC_VCS2_VCS1_IER, 0);
  1042. I915_WRITE(GUC_WD_VECS_IER, 0);
  1043. rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  1044. rps->pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
  1045. }
  1046. static void guc_submission_park(struct intel_engine_cs *engine)
  1047. {
  1048. intel_engine_unpin_breadcrumbs_irq(engine);
  1049. }
  1050. static void guc_submission_unpark(struct intel_engine_cs *engine)
  1051. {
  1052. intel_engine_pin_breadcrumbs_irq(engine);
  1053. }
  1054. int intel_guc_submission_enable(struct intel_guc *guc)
  1055. {
  1056. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1057. struct intel_engine_cs *engine;
  1058. enum intel_engine_id id;
  1059. int err;
  1060. /*
  1061. * We're using GuC work items for submitting work through GuC. Since
  1062. * we're coalescing multiple requests from a single context into a
  1063. * single work item prior to assigning it to execlist_port, we can
  1064. * never have more work items than the total number of ports (for all
  1065. * engines). The GuC firmware is controlling the HEAD of work queue,
  1066. * and it is guaranteed that it will remove the work item from the
  1067. * queue before our request is completed.
  1068. */
  1069. BUILD_BUG_ON(ARRAY_SIZE(engine->execlists.port) *
  1070. sizeof(struct guc_wq_item) *
  1071. I915_NUM_ENGINES > GUC_WQ_SIZE);
  1072. GEM_BUG_ON(!guc->execbuf_client);
  1073. guc_reset_wq(guc->execbuf_client);
  1074. if (guc->preempt_client)
  1075. guc_reset_wq(guc->preempt_client);
  1076. err = intel_guc_sample_forcewake(guc);
  1077. if (err)
  1078. return err;
  1079. err = guc_clients_doorbell_init(guc);
  1080. if (err)
  1081. return err;
  1082. /* Take over from manual control of ELSP (execlists) */
  1083. guc_interrupts_capture(dev_priv);
  1084. for_each_engine(engine, dev_priv, id) {
  1085. struct intel_engine_execlists * const execlists =
  1086. &engine->execlists;
  1087. execlists->tasklet.func = guc_submission_tasklet;
  1088. engine->reset.prepare = guc_reset_prepare;
  1089. engine->park = guc_submission_park;
  1090. engine->unpark = guc_submission_unpark;
  1091. engine->flags &= ~I915_ENGINE_SUPPORTS_STATS;
  1092. }
  1093. return 0;
  1094. }
  1095. void intel_guc_submission_disable(struct intel_guc *guc)
  1096. {
  1097. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1098. GEM_BUG_ON(dev_priv->gt.awake); /* GT should be parked first */
  1099. guc_interrupts_release(dev_priv);
  1100. guc_clients_doorbell_fini(guc);
  1101. /* Revert back to manual ELSP submission */
  1102. intel_engines_reset_default_submission(dev_priv);
  1103. }
  1104. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1105. #include "selftests/intel_guc.c"
  1106. #endif