i915_guc_submission.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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, offset, end);
  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 = rq->tail;
  406. assert_ring_tail_valid(rq->ring, rq->tail);
  407. tail >>= 3;
  408. GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
  409. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  410. * should not have the case where structure wqi is across page, neither
  411. * wrapped to the beginning. This simplifies the implementation below.
  412. *
  413. * XXX: if not the case, we need save data to a temp wqi and copy it to
  414. * workqueue buffer dw by dw.
  415. */
  416. BUILD_BUG_ON(wqi_size != 16);
  417. GEM_BUG_ON(client->wq_rsvd < wqi_size);
  418. /* postincrement WQ tail for next time */
  419. wq_off = client->wq_tail;
  420. GEM_BUG_ON(wq_off & (wqi_size - 1));
  421. client->wq_tail += wqi_size;
  422. client->wq_tail &= client->wq_size - 1;
  423. client->wq_rsvd -= wqi_size;
  424. /* WQ starts from the page after doorbell / process_desc */
  425. wqi = client->vaddr + wq_off + GUC_DB_SIZE;
  426. /* Now fill in the 4-word work queue item */
  427. wqi->header = WQ_TYPE_INORDER |
  428. (wqi_len << WQ_LEN_SHIFT) |
  429. (engine->guc_id << WQ_TARGET_SHIFT) |
  430. WQ_NO_WCFLUSH_WAIT;
  431. /* The GuC wants only the low-order word of the context descriptor */
  432. wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
  433. wqi->submit_element_info = tail << WQ_RING_TAIL_SHIFT;
  434. wqi->fence_id = rq->global_seqno;
  435. }
  436. static void guc_reset_wq(struct i915_guc_client *client)
  437. {
  438. struct guc_process_desc *desc = __get_process_desc(client);
  439. desc->head = 0;
  440. desc->tail = 0;
  441. client->wq_tail = 0;
  442. }
  443. static int guc_ring_doorbell(struct i915_guc_client *client)
  444. {
  445. struct guc_process_desc *desc = __get_process_desc(client);
  446. union guc_doorbell_qw db_cmp, db_exc, db_ret;
  447. union guc_doorbell_qw *db;
  448. int attempt = 2, ret = -EAGAIN;
  449. /* Update the tail so it is visible to GuC */
  450. desc->tail = client->wq_tail;
  451. /* current cookie */
  452. db_cmp.db_status = GUC_DOORBELL_ENABLED;
  453. db_cmp.cookie = client->doorbell_cookie;
  454. /* cookie to be updated */
  455. db_exc.db_status = GUC_DOORBELL_ENABLED;
  456. db_exc.cookie = client->doorbell_cookie + 1;
  457. if (db_exc.cookie == 0)
  458. db_exc.cookie = 1;
  459. /* pointer of current doorbell cacheline */
  460. db = (union guc_doorbell_qw *)__get_doorbell(client);
  461. while (attempt--) {
  462. /* lets ring the doorbell */
  463. db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
  464. db_cmp.value_qw, db_exc.value_qw);
  465. /* if the exchange was successfully executed */
  466. if (db_ret.value_qw == db_cmp.value_qw) {
  467. /* db was successfully rung */
  468. client->doorbell_cookie = db_exc.cookie;
  469. ret = 0;
  470. break;
  471. }
  472. /* XXX: doorbell was lost and need to acquire it again */
  473. if (db_ret.db_status == GUC_DOORBELL_DISABLED)
  474. break;
  475. DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
  476. db_cmp.cookie, db_ret.cookie);
  477. /* update the cookie to newly read cookie from GuC */
  478. db_cmp.cookie = db_ret.cookie;
  479. db_exc.cookie = db_ret.cookie + 1;
  480. if (db_exc.cookie == 0)
  481. db_exc.cookie = 1;
  482. }
  483. return ret;
  484. }
  485. /**
  486. * __i915_guc_submit() - Submit commands through GuC
  487. * @rq: request associated with the commands
  488. *
  489. * The caller must have already called i915_guc_wq_reserve() above with
  490. * a result of 0 (success), guaranteeing that there is space in the work
  491. * queue for the new request, so enqueuing the item cannot fail.
  492. *
  493. * Bad Things Will Happen if the caller violates this protocol e.g. calls
  494. * submit() when _reserve() says there's no space, or calls _submit()
  495. * a different number of times from (successful) calls to _reserve().
  496. *
  497. * The only error here arises if the doorbell hardware isn't functioning
  498. * as expected, which really shouln't happen.
  499. */
  500. static void __i915_guc_submit(struct drm_i915_gem_request *rq)
  501. {
  502. struct drm_i915_private *dev_priv = rq->i915;
  503. struct intel_engine_cs *engine = rq->engine;
  504. unsigned int engine_id = engine->id;
  505. struct intel_guc *guc = &rq->i915->guc;
  506. struct i915_guc_client *client = guc->execbuf_client;
  507. unsigned long flags;
  508. int b_ret;
  509. /* WA to flush out the pending GMADR writes to ring buffer. */
  510. if (i915_vma_is_map_and_fenceable(rq->ring->vma))
  511. POSTING_READ_FW(GUC_STATUS);
  512. spin_lock_irqsave(&client->wq_lock, flags);
  513. guc_wq_item_append(client, rq);
  514. b_ret = guc_ring_doorbell(client);
  515. client->submissions[engine_id] += 1;
  516. client->retcode = b_ret;
  517. if (b_ret)
  518. client->b_fail += 1;
  519. guc->submissions[engine_id] += 1;
  520. guc->last_seqno[engine_id] = rq->global_seqno;
  521. spin_unlock_irqrestore(&client->wq_lock, flags);
  522. }
  523. static void i915_guc_submit(struct drm_i915_gem_request *rq)
  524. {
  525. __i915_gem_request_submit(rq);
  526. __i915_guc_submit(rq);
  527. }
  528. static void nested_enable_signaling(struct drm_i915_gem_request *rq)
  529. {
  530. /* If we use dma_fence_enable_sw_signaling() directly, lockdep
  531. * detects an ordering issue between the fence lockclass and the
  532. * global_timeline. This circular dependency can only occur via 2
  533. * different fences (but same fence lockclass), so we use the nesting
  534. * annotation here to prevent the warn, equivalent to the nesting
  535. * inside i915_gem_request_submit() for when we also enable the
  536. * signaler.
  537. */
  538. if (test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  539. &rq->fence.flags))
  540. return;
  541. GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags));
  542. trace_dma_fence_enable_signal(&rq->fence);
  543. spin_lock_nested(&rq->lock, SINGLE_DEPTH_NESTING);
  544. intel_engine_enable_signaling(rq);
  545. spin_unlock(&rq->lock);
  546. }
  547. static bool i915_guc_dequeue(struct intel_engine_cs *engine)
  548. {
  549. struct execlist_port *port = engine->execlist_port;
  550. struct drm_i915_gem_request *last = port[0].request;
  551. struct rb_node *rb;
  552. bool submit = false;
  553. spin_lock_irq(&engine->timeline->lock);
  554. rb = engine->execlist_first;
  555. while (rb) {
  556. struct drm_i915_gem_request *rq =
  557. rb_entry(rb, typeof(*rq), priotree.node);
  558. if (last && rq->ctx != last->ctx) {
  559. if (port != engine->execlist_port)
  560. break;
  561. i915_gem_request_assign(&port->request, last);
  562. nested_enable_signaling(last);
  563. port++;
  564. }
  565. rb = rb_next(rb);
  566. rb_erase(&rq->priotree.node, &engine->execlist_queue);
  567. RB_CLEAR_NODE(&rq->priotree.node);
  568. rq->priotree.priority = INT_MAX;
  569. i915_guc_submit(rq);
  570. trace_i915_gem_request_in(rq, port - engine->execlist_port);
  571. last = rq;
  572. submit = true;
  573. }
  574. if (submit) {
  575. i915_gem_request_assign(&port->request, last);
  576. nested_enable_signaling(last);
  577. engine->execlist_first = rb;
  578. }
  579. spin_unlock_irq(&engine->timeline->lock);
  580. return submit;
  581. }
  582. static void i915_guc_irq_handler(unsigned long data)
  583. {
  584. struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
  585. struct execlist_port *port = engine->execlist_port;
  586. struct drm_i915_gem_request *rq;
  587. bool submit;
  588. do {
  589. rq = port[0].request;
  590. while (rq && i915_gem_request_completed(rq)) {
  591. trace_i915_gem_request_out(rq);
  592. i915_gem_request_put(rq);
  593. port[0].request = port[1].request;
  594. port[1].request = NULL;
  595. rq = port[0].request;
  596. }
  597. submit = false;
  598. if (!port[1].request)
  599. submit = i915_guc_dequeue(engine);
  600. } while (submit);
  601. }
  602. /*
  603. * Everything below here is concerned with setup & teardown, and is
  604. * therefore not part of the somewhat time-critical batch-submission
  605. * path of i915_guc_submit() above.
  606. */
  607. /**
  608. * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
  609. * @guc: the guc
  610. * @size: size of area to allocate (both virtual space and memory)
  611. *
  612. * This is a wrapper to create an object for use with the GuC. In order to
  613. * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
  614. * both some backing storage and a range inside the Global GTT. We must pin
  615. * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
  616. * range is reserved inside GuC.
  617. *
  618. * Return: A i915_vma if successful, otherwise an ERR_PTR.
  619. */
  620. struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
  621. {
  622. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  623. struct drm_i915_gem_object *obj;
  624. struct i915_vma *vma;
  625. int ret;
  626. obj = i915_gem_object_create(dev_priv, size);
  627. if (IS_ERR(obj))
  628. return ERR_CAST(obj);
  629. vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
  630. if (IS_ERR(vma))
  631. goto err;
  632. ret = i915_vma_pin(vma, 0, PAGE_SIZE,
  633. PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  634. if (ret) {
  635. vma = ERR_PTR(ret);
  636. goto err;
  637. }
  638. return vma;
  639. err:
  640. i915_gem_object_put(obj);
  641. return vma;
  642. }
  643. /* Check that a doorbell register is in the expected state */
  644. static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
  645. {
  646. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  647. u32 drbregl;
  648. bool valid;
  649. GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
  650. drbregl = I915_READ(GEN8_DRBREGL(db_id));
  651. valid = drbregl & GEN8_DRB_VALID;
  652. if (test_bit(db_id, guc->doorbell_bitmap) == valid)
  653. return true;
  654. DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
  655. db_id, drbregl, yesno(valid));
  656. return false;
  657. }
  658. /*
  659. * If the GuC thinks that the doorbell is unassigned (e.g. because we reset and
  660. * reloaded the GuC FW) we can use this function to tell the GuC to reassign the
  661. * doorbell to the rightful owner.
  662. */
  663. static int __reset_doorbell(struct i915_guc_client* client, u16 db_id)
  664. {
  665. int err;
  666. __update_doorbell_desc(client, db_id);
  667. err = __create_doorbell(client);
  668. if (!err)
  669. err = __destroy_doorbell(client);
  670. return err;
  671. }
  672. /*
  673. * Set up & tear down each unused doorbell in turn, to ensure that all doorbell
  674. * HW is (re)initialised. For that end, we might have to borrow the first
  675. * client. Also, tell GuC about all the doorbells in use by all clients.
  676. * We do this because the KMD, the GuC and the doorbell HW can easily go out of
  677. * sync (e.g. we can reset the GuC, but not the doorbel HW).
  678. */
  679. static int guc_init_doorbell_hw(struct intel_guc *guc)
  680. {
  681. struct i915_guc_client *client = guc->execbuf_client;
  682. bool recreate_first_client = false;
  683. u16 db_id;
  684. int ret;
  685. /* For unused doorbells, make sure they are disabled */
  686. for_each_clear_bit(db_id, guc->doorbell_bitmap, GUC_NUM_DOORBELLS) {
  687. if (doorbell_ok(guc, db_id))
  688. continue;
  689. if (has_doorbell(client)) {
  690. /* Borrow execbuf_client (we will recreate it later) */
  691. destroy_doorbell(client);
  692. recreate_first_client = true;
  693. }
  694. ret = __reset_doorbell(client, db_id);
  695. WARN(ret, "Doorbell %u reset failed, err %d\n", db_id, ret);
  696. }
  697. if (recreate_first_client) {
  698. ret = __reserve_doorbell(client);
  699. if (unlikely(ret)) {
  700. DRM_ERROR("Couldn't re-reserve first client db: %d\n", ret);
  701. return ret;
  702. }
  703. __update_doorbell_desc(client, client->doorbell_id);
  704. }
  705. /* Now for every client (and not only execbuf_client) make sure their
  706. * doorbells are known by the GuC */
  707. //for (client = client_list; client != NULL; client = client->next)
  708. {
  709. ret = __create_doorbell(client);
  710. if (ret) {
  711. DRM_ERROR("Couldn't recreate client %u doorbell: %d\n",
  712. client->stage_id, ret);
  713. return ret;
  714. }
  715. }
  716. /* Read back & verify all (used & unused) doorbell registers */
  717. for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
  718. WARN_ON(!doorbell_ok(guc, db_id));
  719. return 0;
  720. }
  721. /**
  722. * guc_client_alloc() - Allocate an i915_guc_client
  723. * @dev_priv: driver private data structure
  724. * @engines: The set of engines to enable for this client
  725. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  726. * The kernel client to replace ExecList submission is created with
  727. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  728. * while a preemption context can use CRITICAL.
  729. * @ctx: the context that owns the client (we use the default render
  730. * context)
  731. *
  732. * Return: An i915_guc_client object if success, else NULL.
  733. */
  734. static struct i915_guc_client *
  735. guc_client_alloc(struct drm_i915_private *dev_priv,
  736. uint32_t engines,
  737. uint32_t priority,
  738. struct i915_gem_context *ctx)
  739. {
  740. struct i915_guc_client *client;
  741. struct intel_guc *guc = &dev_priv->guc;
  742. struct i915_vma *vma;
  743. void *vaddr;
  744. int ret;
  745. client = kzalloc(sizeof(*client), GFP_KERNEL);
  746. if (!client)
  747. return ERR_PTR(-ENOMEM);
  748. client->guc = guc;
  749. client->owner = ctx;
  750. client->engines = engines;
  751. client->priority = priority;
  752. client->doorbell_id = GUC_DOORBELL_INVALID;
  753. client->wq_offset = GUC_DB_SIZE;
  754. client->wq_size = GUC_WQ_SIZE;
  755. spin_lock_init(&client->wq_lock);
  756. ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
  757. GFP_KERNEL);
  758. if (ret < 0)
  759. goto err_client;
  760. client->stage_id = ret;
  761. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  762. vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
  763. if (IS_ERR(vma)) {
  764. ret = PTR_ERR(vma);
  765. goto err_id;
  766. }
  767. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  768. client->vma = vma;
  769. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  770. if (IS_ERR(vaddr)) {
  771. ret = PTR_ERR(vaddr);
  772. goto err_vma;
  773. }
  774. client->vaddr = vaddr;
  775. client->doorbell_offset = __select_cacheline(guc);
  776. /*
  777. * Since the doorbell only requires a single cacheline, we can save
  778. * space by putting the application process descriptor in the same
  779. * page. Use the half of the page that doesn't include the doorbell.
  780. */
  781. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  782. client->proc_desc_offset = 0;
  783. else
  784. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  785. guc_proc_desc_init(guc, client);
  786. guc_stage_desc_init(guc, client);
  787. ret = create_doorbell(client);
  788. if (ret)
  789. goto err_vaddr;
  790. DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
  791. priority, client, client->engines, client->stage_id);
  792. DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
  793. client->doorbell_id, client->doorbell_offset);
  794. return client;
  795. err_vaddr:
  796. i915_gem_object_unpin_map(client->vma->obj);
  797. err_vma:
  798. i915_vma_unpin_and_release(&client->vma);
  799. err_id:
  800. ida_simple_remove(&guc->stage_ids, client->stage_id);
  801. err_client:
  802. kfree(client);
  803. return ERR_PTR(ret);
  804. }
  805. static void guc_client_free(struct i915_guc_client *client)
  806. {
  807. /*
  808. * XXX: wait for any outstanding submissions before freeing memory.
  809. * Be sure to drop any locks
  810. */
  811. /* FIXME: in many cases, by the time we get here the GuC has been
  812. * reset, so we cannot destroy the doorbell properly. Ignore the
  813. * error message for now */
  814. destroy_doorbell(client);
  815. guc_stage_desc_fini(client->guc, client);
  816. i915_gem_object_unpin_map(client->vma->obj);
  817. i915_vma_unpin_and_release(&client->vma);
  818. ida_simple_remove(&client->guc->stage_ids, client->stage_id);
  819. kfree(client);
  820. }
  821. static void guc_policies_init(struct guc_policies *policies)
  822. {
  823. struct guc_policy *policy;
  824. u32 p, i;
  825. policies->dpc_promote_time = 500000;
  826. policies->max_num_work_items = POLICY_MAX_NUM_WI;
  827. for (p = 0; p < GUC_CLIENT_PRIORITY_NUM; p++) {
  828. for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
  829. policy = &policies->policy[p][i];
  830. policy->execution_quantum = 1000000;
  831. policy->preemption_time = 500000;
  832. policy->fault_time = 250000;
  833. policy->policy_flags = 0;
  834. }
  835. }
  836. policies->is_valid = 1;
  837. }
  838. static int guc_ads_create(struct intel_guc *guc)
  839. {
  840. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  841. struct i915_vma *vma;
  842. struct page *page;
  843. /* The ads obj includes the struct itself and buffers passed to GuC */
  844. struct {
  845. struct guc_ads ads;
  846. struct guc_policies policies;
  847. struct guc_mmio_reg_state reg_state;
  848. u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
  849. } __packed *blob;
  850. struct intel_engine_cs *engine;
  851. enum intel_engine_id id;
  852. u32 base;
  853. GEM_BUG_ON(guc->ads_vma);
  854. vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
  855. if (IS_ERR(vma))
  856. return PTR_ERR(vma);
  857. guc->ads_vma = vma;
  858. page = i915_vma_first_page(vma);
  859. blob = kmap(page);
  860. /* GuC scheduling policies */
  861. guc_policies_init(&blob->policies);
  862. /* MMIO reg state */
  863. for_each_engine(engine, dev_priv, id) {
  864. blob->reg_state.white_list[engine->guc_id].mmio_start =
  865. engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
  866. /* Nothing to be saved or restored for now. */
  867. blob->reg_state.white_list[engine->guc_id].count = 0;
  868. }
  869. /*
  870. * The GuC requires a "Golden Context" when it reinitialises
  871. * engines after a reset. Here we use the Render ring default
  872. * context, which must already exist and be pinned in the GGTT,
  873. * so its address won't change after we've told the GuC where
  874. * to find it.
  875. */
  876. blob->ads.golden_context_lrca =
  877. dev_priv->engine[RCS]->status_page.ggtt_offset;
  878. for_each_engine(engine, dev_priv, id)
  879. blob->ads.eng_state_size[engine->guc_id] =
  880. intel_lr_context_size(engine);
  881. base = guc_ggtt_offset(vma);
  882. blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
  883. blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
  884. blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
  885. kunmap(page);
  886. return 0;
  887. }
  888. static void guc_ads_destroy(struct intel_guc *guc)
  889. {
  890. i915_vma_unpin_and_release(&guc->ads_vma);
  891. }
  892. /*
  893. * Set up the memory resources to be shared with the GuC (via the GGTT)
  894. * at firmware loading time.
  895. */
  896. int i915_guc_submission_init(struct drm_i915_private *dev_priv)
  897. {
  898. struct intel_guc *guc = &dev_priv->guc;
  899. struct i915_vma *vma;
  900. void *vaddr;
  901. int ret;
  902. if (guc->stage_desc_pool)
  903. return 0;
  904. vma = intel_guc_allocate_vma(guc,
  905. PAGE_ALIGN(sizeof(struct guc_stage_desc) *
  906. GUC_MAX_STAGE_DESCRIPTORS));
  907. if (IS_ERR(vma))
  908. return PTR_ERR(vma);
  909. guc->stage_desc_pool = vma;
  910. vaddr = i915_gem_object_pin_map(guc->stage_desc_pool->obj, I915_MAP_WB);
  911. if (IS_ERR(vaddr)) {
  912. ret = PTR_ERR(vaddr);
  913. goto err_vma;
  914. }
  915. guc->stage_desc_pool_vaddr = vaddr;
  916. ret = intel_guc_log_create(guc);
  917. if (ret < 0)
  918. goto err_vaddr;
  919. ret = guc_ads_create(guc);
  920. if (ret < 0)
  921. goto err_log;
  922. ida_init(&guc->stage_ids);
  923. return 0;
  924. err_log:
  925. intel_guc_log_destroy(guc);
  926. err_vaddr:
  927. i915_gem_object_unpin_map(guc->stage_desc_pool->obj);
  928. err_vma:
  929. i915_vma_unpin_and_release(&guc->stage_desc_pool);
  930. return ret;
  931. }
  932. void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
  933. {
  934. struct intel_guc *guc = &dev_priv->guc;
  935. ida_destroy(&guc->stage_ids);
  936. guc_ads_destroy(guc);
  937. intel_guc_log_destroy(guc);
  938. i915_gem_object_unpin_map(guc->stage_desc_pool->obj);
  939. i915_vma_unpin_and_release(&guc->stage_desc_pool);
  940. }
  941. static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
  942. {
  943. struct intel_engine_cs *engine;
  944. enum intel_engine_id id;
  945. int irqs;
  946. /* tell all command streamers to forward interrupts (but not vblank) to GuC */
  947. irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
  948. for_each_engine(engine, dev_priv, id)
  949. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  950. /* route USER_INTERRUPT to Host, all others are sent to GuC. */
  951. irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  952. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  953. /* These three registers have the same bit definitions */
  954. I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
  955. I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
  956. I915_WRITE(GUC_WD_VECS_IER, ~irqs);
  957. /*
  958. * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
  959. * (unmasked) PM interrupts to the GuC. All other bits of this
  960. * register *disable* generation of a specific interrupt.
  961. *
  962. * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
  963. * writing to the PM interrupt mask register, i.e. interrupts
  964. * that must not be disabled.
  965. *
  966. * If the GuC is handling these interrupts, then we must not let
  967. * the PM code disable ANY interrupt that the GuC is expecting.
  968. * So for each ENABLED (0) bit in this register, we must SET the
  969. * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
  970. * GuC needs ARAT expired interrupt unmasked hence it is set in
  971. * pm_intrmsk_mbz.
  972. *
  973. * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
  974. * result in the register bit being left SET!
  975. */
  976. dev_priv->rps.pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
  977. dev_priv->rps.pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  978. }
  979. static void guc_interrupts_release(struct drm_i915_private *dev_priv)
  980. {
  981. struct intel_engine_cs *engine;
  982. enum intel_engine_id id;
  983. int irqs;
  984. /*
  985. * tell all command streamers NOT to forward interrupts or vblank
  986. * to GuC.
  987. */
  988. irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
  989. irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
  990. for_each_engine(engine, dev_priv, id)
  991. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  992. /* route all GT interrupts to the host */
  993. I915_WRITE(GUC_BCS_RCS_IER, 0);
  994. I915_WRITE(GUC_VCS2_VCS1_IER, 0);
  995. I915_WRITE(GUC_WD_VECS_IER, 0);
  996. dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  997. dev_priv->rps.pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
  998. }
  999. int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
  1000. {
  1001. struct intel_guc *guc = &dev_priv->guc;
  1002. struct i915_guc_client *client = guc->execbuf_client;
  1003. struct intel_engine_cs *engine;
  1004. enum intel_engine_id id;
  1005. int err;
  1006. if (!client) {
  1007. client = guc_client_alloc(dev_priv,
  1008. INTEL_INFO(dev_priv)->ring_mask,
  1009. GUC_CLIENT_PRIORITY_KMD_NORMAL,
  1010. dev_priv->kernel_context);
  1011. if (IS_ERR(client)) {
  1012. DRM_ERROR("Failed to create GuC client for execbuf!\n");
  1013. return PTR_ERR(client);
  1014. }
  1015. guc->execbuf_client = client;
  1016. }
  1017. err = intel_guc_sample_forcewake(guc);
  1018. if (err)
  1019. goto err_execbuf_client;
  1020. guc_reset_wq(client);
  1021. err = guc_init_doorbell_hw(guc);
  1022. if (err)
  1023. goto err_execbuf_client;
  1024. /* Take over from manual control of ELSP (execlists) */
  1025. guc_interrupts_capture(dev_priv);
  1026. for_each_engine(engine, dev_priv, id) {
  1027. const int wqi_size = sizeof(struct guc_wq_item);
  1028. struct drm_i915_gem_request *rq;
  1029. /* The tasklet was initialised by execlists, and may be in
  1030. * a state of flux (across a reset) and so we just want to
  1031. * take over the callback without changing any other state
  1032. * in the tasklet.
  1033. */
  1034. engine->irq_tasklet.func = i915_guc_irq_handler;
  1035. clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  1036. /* Replay the current set of previously submitted requests */
  1037. spin_lock_irq(&engine->timeline->lock);
  1038. list_for_each_entry(rq, &engine->timeline->requests, link) {
  1039. guc_client_update_wq_rsvd(client, wqi_size);
  1040. __i915_guc_submit(rq);
  1041. }
  1042. spin_unlock_irq(&engine->timeline->lock);
  1043. }
  1044. return 0;
  1045. err_execbuf_client:
  1046. guc_client_free(guc->execbuf_client);
  1047. guc->execbuf_client = NULL;
  1048. return err;
  1049. }
  1050. void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
  1051. {
  1052. struct intel_guc *guc = &dev_priv->guc;
  1053. guc_interrupts_release(dev_priv);
  1054. /* Revert back to manual ELSP submission */
  1055. intel_engines_reset_default_submission(dev_priv);
  1056. guc_client_free(guc->execbuf_client);
  1057. guc->execbuf_client = NULL;
  1058. }
  1059. /**
  1060. * intel_guc_suspend() - notify GuC entering suspend state
  1061. * @dev_priv: i915 device private
  1062. */
  1063. int intel_guc_suspend(struct drm_i915_private *dev_priv)
  1064. {
  1065. struct intel_guc *guc = &dev_priv->guc;
  1066. struct i915_gem_context *ctx;
  1067. u32 data[3];
  1068. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  1069. return 0;
  1070. gen9_disable_guc_interrupts(dev_priv);
  1071. ctx = dev_priv->kernel_context;
  1072. data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
  1073. /* any value greater than GUC_POWER_D0 */
  1074. data[1] = GUC_POWER_D1;
  1075. /* first page is shared data with GuC */
  1076. data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
  1077. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  1078. }
  1079. /**
  1080. * intel_guc_resume() - notify GuC resuming from suspend state
  1081. * @dev_priv: i915 device private
  1082. */
  1083. int intel_guc_resume(struct drm_i915_private *dev_priv)
  1084. {
  1085. struct intel_guc *guc = &dev_priv->guc;
  1086. struct i915_gem_context *ctx;
  1087. u32 data[3];
  1088. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  1089. return 0;
  1090. if (i915.guc_log_level >= 0)
  1091. gen9_enable_guc_interrupts(dev_priv);
  1092. ctx = dev_priv->kernel_context;
  1093. data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
  1094. data[1] = GUC_POWER_D0;
  1095. /* first page is shared data with GuC */
  1096. data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
  1097. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  1098. }