i915_guc_submission.c 39 KB

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