i915_guc_submission.c 36 KB

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