intel_guc_submission.c 37 KB

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