intel_guc_submission.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/circ_buf.h>
  25. #include <trace/events/dma_fence.h>
  26. #include "intel_guc_submission.h"
  27. #include "i915_drv.h"
  28. /**
  29. * DOC: GuC-based command submission
  30. *
  31. * GuC client:
  32. * A intel_guc_client refers to a submission path through GuC. Currently, there
  33. * are two clients. One of them (the execbuf_client) is charged with all
  34. * submissions to the GuC, the other one (preempt_client) is responsible for
  35. * preempting the execbuf_client. This struct is the owner of a doorbell, a
  36. * process descriptor and a workqueue (all of them inside a single gem object
  37. * that contains all required pages for these elements).
  38. *
  39. * GuC stage descriptor:
  40. * During initialization, the driver allocates a static pool of 1024 such
  41. * descriptors, and shares them with the GuC.
  42. * Currently, there exists a 1:1 mapping between a intel_guc_client and a
  43. * guc_stage_desc (via the client's stage_id), so effectively only one
  44. * gets used. This stage descriptor lets the GuC know about the doorbell,
  45. * workqueue and process descriptor. Theoretically, it also lets the GuC
  46. * know about our HW contexts (context ID, etc...), but we actually
  47. * employ a kind of submission where the GuC uses the LRCA sent via the work
  48. * item instead (the single guc_stage_desc associated to execbuf client
  49. * contains information about the default kernel context only, but this is
  50. * essentially unused). This is called a "proxy" submission.
  51. *
  52. * The Scratch registers:
  53. * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
  54. * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
  55. * triggers an interrupt on the GuC via another register write (0xC4C8).
  56. * Firmware writes a success/fail code back to the action register after
  57. * processes the request. The kernel driver polls waiting for this update and
  58. * then proceeds.
  59. * See intel_guc_send()
  60. *
  61. * Doorbells:
  62. * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
  63. * mapped into process space.
  64. *
  65. * Work Items:
  66. * There are several types of work items that the host may place into a
  67. * workqueue, each with its own requirements and limitations. Currently only
  68. * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
  69. * represents in-order queue. The kernel driver packs ring tail pointer and an
  70. * ELSP context descriptor dword into Work Item.
  71. * See guc_add_request()
  72. *
  73. * ADS:
  74. * The Additional Data Struct (ADS) has pointers for different buffers used by
  75. * the GuC. One single gem object contains the ADS struct itself (guc_ads), the
  76. * scheduling policies (guc_policies), a structure describing a collection of
  77. * register sets (guc_mmio_reg_state) and some extra pages for the GuC to save
  78. * its internal state for sleep.
  79. *
  80. */
  81. static inline bool is_high_priority(struct intel_guc_client *client)
  82. {
  83. return (client->priority == GUC_CLIENT_PRIORITY_KMD_HIGH ||
  84. client->priority == GUC_CLIENT_PRIORITY_HIGH);
  85. }
  86. static int __reserve_doorbell(struct intel_guc_client *client)
  87. {
  88. unsigned long offset;
  89. unsigned long end;
  90. u16 id;
  91. GEM_BUG_ON(client->doorbell_id != GUC_DOORBELL_INVALID);
  92. /*
  93. * The bitmap tracks which doorbell registers are currently in use.
  94. * It is split into two halves; the first half is used for normal
  95. * priority contexts, the second half for high-priority ones.
  96. */
  97. offset = 0;
  98. end = GUC_NUM_DOORBELLS / 2;
  99. if (is_high_priority(client)) {
  100. offset = end;
  101. end += offset;
  102. }
  103. id = find_next_zero_bit(client->guc->doorbell_bitmap, end, offset);
  104. if (id == end)
  105. return -ENOSPC;
  106. __set_bit(id, client->guc->doorbell_bitmap);
  107. client->doorbell_id = id;
  108. DRM_DEBUG_DRIVER("client %u (high prio=%s) reserved doorbell: %d\n",
  109. client->stage_id, yesno(is_high_priority(client)),
  110. id);
  111. return 0;
  112. }
  113. static void __unreserve_doorbell(struct intel_guc_client *client)
  114. {
  115. GEM_BUG_ON(client->doorbell_id == GUC_DOORBELL_INVALID);
  116. __clear_bit(client->doorbell_id, client->guc->doorbell_bitmap);
  117. client->doorbell_id = GUC_DOORBELL_INVALID;
  118. }
  119. /*
  120. * Tell the GuC to allocate or deallocate a specific doorbell
  121. */
  122. static int __guc_allocate_doorbell(struct intel_guc *guc, u32 stage_id)
  123. {
  124. u32 action[] = {
  125. INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
  126. stage_id
  127. };
  128. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  129. }
  130. static int __guc_deallocate_doorbell(struct intel_guc *guc, u32 stage_id)
  131. {
  132. u32 action[] = {
  133. INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
  134. stage_id
  135. };
  136. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  137. }
  138. static struct guc_stage_desc *__get_stage_desc(struct intel_guc_client *client)
  139. {
  140. struct guc_stage_desc *base = client->guc->stage_desc_pool_vaddr;
  141. return &base[client->stage_id];
  142. }
  143. /*
  144. * Initialise, update, or clear doorbell data shared with the GuC
  145. *
  146. * These functions modify shared data and so need access to the mapped
  147. * client object which contains the page being used for the doorbell
  148. */
  149. static void __update_doorbell_desc(struct intel_guc_client *client, u16 new_id)
  150. {
  151. struct guc_stage_desc *desc;
  152. /* Update the GuC's idea of the doorbell ID */
  153. desc = __get_stage_desc(client);
  154. desc->db_id = new_id;
  155. }
  156. static struct guc_doorbell_info *__get_doorbell(struct intel_guc_client *client)
  157. {
  158. return client->vaddr + client->doorbell_offset;
  159. }
  160. static bool has_doorbell(struct intel_guc_client *client)
  161. {
  162. if (client->doorbell_id == GUC_DOORBELL_INVALID)
  163. return false;
  164. return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
  165. }
  166. static int __create_doorbell(struct intel_guc_client *client)
  167. {
  168. struct guc_doorbell_info *doorbell;
  169. int err;
  170. doorbell = __get_doorbell(client);
  171. doorbell->db_status = GUC_DOORBELL_ENABLED;
  172. doorbell->cookie = 0;
  173. err = __guc_allocate_doorbell(client->guc, client->stage_id);
  174. if (err) {
  175. doorbell->db_status = GUC_DOORBELL_DISABLED;
  176. DRM_ERROR("Couldn't create client %u doorbell: %d\n",
  177. client->stage_id, err);
  178. }
  179. return err;
  180. }
  181. static int __destroy_doorbell(struct intel_guc_client *client)
  182. {
  183. struct drm_i915_private *dev_priv = guc_to_i915(client->guc);
  184. struct guc_doorbell_info *doorbell;
  185. u16 db_id = client->doorbell_id;
  186. GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
  187. doorbell = __get_doorbell(client);
  188. doorbell->db_status = GUC_DOORBELL_DISABLED;
  189. doorbell->cookie = 0;
  190. /* Doorbell release flow requires that we wait for GEN8_DRB_VALID bit
  191. * to go to zero after updating db_status before we call the GuC to
  192. * release the doorbell
  193. */
  194. if (wait_for_us(!(I915_READ(GEN8_DRBREGL(db_id)) & GEN8_DRB_VALID), 10))
  195. WARN_ONCE(true, "Doorbell never became invalid after disable\n");
  196. return __guc_deallocate_doorbell(client->guc, client->stage_id);
  197. }
  198. static int create_doorbell(struct intel_guc_client *client)
  199. {
  200. int ret;
  201. ret = __reserve_doorbell(client);
  202. if (ret)
  203. return ret;
  204. __update_doorbell_desc(client, client->doorbell_id);
  205. ret = __create_doorbell(client);
  206. if (ret)
  207. goto err;
  208. return 0;
  209. err:
  210. __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
  211. __unreserve_doorbell(client);
  212. return ret;
  213. }
  214. static int destroy_doorbell(struct intel_guc_client *client)
  215. {
  216. int err;
  217. GEM_BUG_ON(!has_doorbell(client));
  218. /* XXX: wait for any interrupts */
  219. /* XXX: wait for workqueue to drain */
  220. err = __destroy_doorbell(client);
  221. if (err)
  222. return err;
  223. __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
  224. __unreserve_doorbell(client);
  225. return 0;
  226. }
  227. static unsigned long __select_cacheline(struct intel_guc *guc)
  228. {
  229. unsigned long offset;
  230. /* Doorbell uses a single cache line within a page */
  231. offset = offset_in_page(guc->db_cacheline);
  232. /* Moving to next cache line to reduce contention */
  233. guc->db_cacheline += cache_line_size();
  234. DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
  235. offset, guc->db_cacheline, cache_line_size());
  236. return offset;
  237. }
  238. static inline struct guc_process_desc *
  239. __get_process_desc(struct intel_guc_client *client)
  240. {
  241. return client->vaddr + client->proc_desc_offset;
  242. }
  243. /*
  244. * Initialise the process descriptor shared with the GuC firmware.
  245. */
  246. static void guc_proc_desc_init(struct intel_guc *guc,
  247. struct intel_guc_client *client)
  248. {
  249. struct guc_process_desc *desc;
  250. desc = memset(__get_process_desc(client), 0, sizeof(*desc));
  251. /*
  252. * XXX: pDoorbell and WQVBaseAddress are pointers in process address
  253. * space for ring3 clients (set them as in mmap_ioctl) or kernel
  254. * space for kernel clients (map on demand instead? May make debug
  255. * easier to have it mapped).
  256. */
  257. desc->wq_base_addr = 0;
  258. desc->db_base_addr = 0;
  259. desc->stage_id = client->stage_id;
  260. desc->wq_size_bytes = GUC_WQ_SIZE;
  261. desc->wq_status = WQ_STATUS_ACTIVE;
  262. desc->priority = client->priority;
  263. }
  264. static int guc_stage_desc_pool_create(struct intel_guc *guc)
  265. {
  266. struct i915_vma *vma;
  267. void *vaddr;
  268. vma = intel_guc_allocate_vma(guc,
  269. PAGE_ALIGN(sizeof(struct guc_stage_desc) *
  270. GUC_MAX_STAGE_DESCRIPTORS));
  271. if (IS_ERR(vma))
  272. return PTR_ERR(vma);
  273. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  274. if (IS_ERR(vaddr)) {
  275. i915_vma_unpin_and_release(&vma);
  276. return PTR_ERR(vaddr);
  277. }
  278. guc->stage_desc_pool = vma;
  279. guc->stage_desc_pool_vaddr = vaddr;
  280. ida_init(&guc->stage_ids);
  281. return 0;
  282. }
  283. static void guc_stage_desc_pool_destroy(struct intel_guc *guc)
  284. {
  285. ida_destroy(&guc->stage_ids);
  286. i915_gem_object_unpin_map(guc->stage_desc_pool->obj);
  287. i915_vma_unpin_and_release(&guc->stage_desc_pool);
  288. }
  289. /*
  290. * Initialise/clear the stage descriptor shared with the GuC firmware.
  291. *
  292. * This descriptor tells the GuC where (in GGTT space) to find the important
  293. * data structures relating to this client (doorbell, process descriptor,
  294. * write queue, etc).
  295. */
  296. static void guc_stage_desc_init(struct intel_guc *guc,
  297. struct intel_guc_client *client)
  298. {
  299. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  300. struct intel_engine_cs *engine;
  301. struct i915_gem_context *ctx = client->owner;
  302. struct guc_stage_desc *desc;
  303. unsigned int tmp;
  304. u32 gfx_addr;
  305. desc = __get_stage_desc(client);
  306. memset(desc, 0, sizeof(*desc));
  307. desc->attribute = GUC_STAGE_DESC_ATTR_ACTIVE |
  308. GUC_STAGE_DESC_ATTR_KERNEL;
  309. if (is_high_priority(client))
  310. desc->attribute |= GUC_STAGE_DESC_ATTR_PREEMPT;
  311. desc->stage_id = client->stage_id;
  312. desc->priority = client->priority;
  313. desc->db_id = client->doorbell_id;
  314. for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
  315. struct intel_context *ce = &ctx->engine[engine->id];
  316. u32 guc_engine_id = engine->guc_id;
  317. struct guc_execlist_context *lrc = &desc->lrc[guc_engine_id];
  318. /* TODO: We have a design issue to be solved here. Only when we
  319. * receive the first batch, we know which engine is used by the
  320. * user. But here GuC expects the lrc and ring to be pinned. It
  321. * is not an issue for default context, which is the only one
  322. * for now who owns a GuC client. But for future owner of GuC
  323. * client, need to make sure lrc is pinned prior to enter here.
  324. */
  325. if (!ce->state)
  326. break; /* XXX: continue? */
  327. /*
  328. * XXX: When this is a GUC_STAGE_DESC_ATTR_KERNEL client (proxy
  329. * submission or, in other words, not using a direct submission
  330. * model) the KMD's LRCA is not used for any work submission.
  331. * Instead, the GuC uses the LRCA of the user mode context (see
  332. * guc_add_request below).
  333. */
  334. lrc->context_desc = lower_32_bits(ce->lrc_desc);
  335. /* The state page is after PPHWSP */
  336. lrc->ring_lrca =
  337. guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
  338. /* XXX: In direct submission, the GuC wants the HW context id
  339. * here. In proxy submission, it wants the stage id
  340. */
  341. lrc->context_id = (client->stage_id << GUC_ELC_CTXID_OFFSET) |
  342. (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
  343. lrc->ring_begin = guc_ggtt_offset(ce->ring->vma);
  344. lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
  345. lrc->ring_next_free_location = lrc->ring_begin;
  346. lrc->ring_current_tail_pointer_value = 0;
  347. desc->engines_used |= (1 << guc_engine_id);
  348. }
  349. DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
  350. client->engines, desc->engines_used);
  351. WARN_ON(desc->engines_used == 0);
  352. /*
  353. * The doorbell, process descriptor, and workqueue are all parts
  354. * of the client object, which the GuC will reference via the GGTT
  355. */
  356. gfx_addr = guc_ggtt_offset(client->vma);
  357. desc->db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
  358. client->doorbell_offset;
  359. desc->db_trigger_cpu = ptr_to_u64(__get_doorbell(client));
  360. desc->db_trigger_uk = gfx_addr + client->doorbell_offset;
  361. desc->process_desc = gfx_addr + client->proc_desc_offset;
  362. desc->wq_addr = gfx_addr + GUC_DB_SIZE;
  363. desc->wq_size = GUC_WQ_SIZE;
  364. desc->desc_private = ptr_to_u64(client);
  365. }
  366. static void guc_stage_desc_fini(struct intel_guc *guc,
  367. struct intel_guc_client *client)
  368. {
  369. struct guc_stage_desc *desc;
  370. desc = __get_stage_desc(client);
  371. memset(desc, 0, sizeof(*desc));
  372. }
  373. static int guc_shared_data_create(struct intel_guc *guc)
  374. {
  375. struct i915_vma *vma;
  376. void *vaddr;
  377. vma = intel_guc_allocate_vma(guc, PAGE_SIZE);
  378. if (IS_ERR(vma))
  379. return PTR_ERR(vma);
  380. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  381. if (IS_ERR(vaddr)) {
  382. i915_vma_unpin_and_release(&vma);
  383. return PTR_ERR(vaddr);
  384. }
  385. guc->shared_data = vma;
  386. guc->shared_data_vaddr = vaddr;
  387. return 0;
  388. }
  389. static void guc_shared_data_destroy(struct intel_guc *guc)
  390. {
  391. i915_gem_object_unpin_map(guc->shared_data->obj);
  392. i915_vma_unpin_and_release(&guc->shared_data);
  393. }
  394. /* Construct a Work Item and append it to the GuC's Work Queue */
  395. static void guc_wq_item_append(struct intel_guc_client *client,
  396. u32 target_engine, u32 context_desc,
  397. u32 ring_tail, u32 fence_id)
  398. {
  399. /* wqi_len is in DWords, and does not include the one-word header */
  400. const size_t wqi_size = sizeof(struct guc_wq_item);
  401. const u32 wqi_len = wqi_size / sizeof(u32) - 1;
  402. struct guc_process_desc *desc = __get_process_desc(client);
  403. struct guc_wq_item *wqi;
  404. u32 wq_off;
  405. lockdep_assert_held(&client->wq_lock);
  406. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  407. * should not have the case where structure wqi is across page, neither
  408. * wrapped to the beginning. This simplifies the implementation below.
  409. *
  410. * XXX: if not the case, we need save data to a temp wqi and copy it to
  411. * workqueue buffer dw by dw.
  412. */
  413. BUILD_BUG_ON(wqi_size != 16);
  414. /* Free space is guaranteed. */
  415. wq_off = READ_ONCE(desc->tail);
  416. GEM_BUG_ON(CIRC_SPACE(wq_off, READ_ONCE(desc->head),
  417. GUC_WQ_SIZE) < wqi_size);
  418. GEM_BUG_ON(wq_off & (wqi_size - 1));
  419. /* WQ starts from the page after doorbell / process_desc */
  420. wqi = client->vaddr + wq_off + GUC_DB_SIZE;
  421. /* Now fill in the 4-word work queue item */
  422. wqi->header = WQ_TYPE_INORDER |
  423. (wqi_len << WQ_LEN_SHIFT) |
  424. (target_engine << WQ_TARGET_SHIFT) |
  425. WQ_NO_WCFLUSH_WAIT;
  426. wqi->context_desc = context_desc;
  427. wqi->submit_element_info = ring_tail << WQ_RING_TAIL_SHIFT;
  428. GEM_BUG_ON(ring_tail > WQ_RING_TAIL_MAX);
  429. wqi->fence_id = fence_id;
  430. /* Make the update visible to GuC */
  431. WRITE_ONCE(desc->tail, (wq_off + wqi_size) & (GUC_WQ_SIZE - 1));
  432. }
  433. static void guc_reset_wq(struct intel_guc_client *client)
  434. {
  435. struct guc_process_desc *desc = __get_process_desc(client);
  436. desc->head = 0;
  437. desc->tail = 0;
  438. }
  439. static void guc_ring_doorbell(struct intel_guc_client *client)
  440. {
  441. struct guc_doorbell_info *db;
  442. u32 cookie;
  443. lockdep_assert_held(&client->wq_lock);
  444. /* pointer of current doorbell cacheline */
  445. db = __get_doorbell(client);
  446. /*
  447. * We're not expecting the doorbell cookie to change behind our back,
  448. * we also need to treat 0 as a reserved value.
  449. */
  450. cookie = READ_ONCE(db->cookie);
  451. WARN_ON_ONCE(xchg(&db->cookie, cookie + 1 ?: cookie + 2) != cookie);
  452. /* XXX: doorbell was lost and need to acquire it again */
  453. GEM_BUG_ON(db->db_status != GUC_DOORBELL_ENABLED);
  454. }
  455. static void guc_add_request(struct intel_guc *guc,
  456. struct drm_i915_gem_request *rq)
  457. {
  458. struct intel_guc_client *client = guc->execbuf_client;
  459. struct intel_engine_cs *engine = rq->engine;
  460. u32 ctx_desc = lower_32_bits(intel_lr_context_descriptor(rq->ctx,
  461. engine));
  462. u32 ring_tail = intel_ring_set_tail(rq->ring, rq->tail) / sizeof(u64);
  463. spin_lock(&client->wq_lock);
  464. guc_wq_item_append(client, engine->guc_id, ctx_desc,
  465. ring_tail, rq->global_seqno);
  466. guc_ring_doorbell(client);
  467. client->submissions[engine->id] += 1;
  468. spin_unlock(&client->wq_lock);
  469. }
  470. /*
  471. * When we're doing submissions using regular execlists backend, writing to
  472. * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
  473. * pinned in mappable aperture portion of GGTT are visible to command streamer.
  474. * Writes done by GuC on our behalf are not guaranteeing such ordering,
  475. * therefore, to ensure the flush, we're issuing a POSTING READ.
  476. */
  477. static void flush_ggtt_writes(struct i915_vma *vma)
  478. {
  479. struct drm_i915_private *dev_priv = to_i915(vma->obj->base.dev);
  480. if (i915_vma_is_map_and_fenceable(vma))
  481. POSTING_READ_FW(GUC_STATUS);
  482. }
  483. #define GUC_PREEMPT_FINISHED 0x1
  484. #define GUC_PREEMPT_BREADCRUMB_DWORDS 0x8
  485. static void inject_preempt_context(struct work_struct *work)
  486. {
  487. struct guc_preempt_work *preempt_work =
  488. container_of(work, typeof(*preempt_work), work);
  489. struct intel_engine_cs *engine = preempt_work->engine;
  490. struct intel_guc *guc = container_of(preempt_work, typeof(*guc),
  491. preempt_work[engine->id]);
  492. struct intel_guc_client *client = guc->preempt_client;
  493. struct guc_stage_desc *stage_desc = __get_stage_desc(client);
  494. struct intel_ring *ring = client->owner->engine[engine->id].ring;
  495. u32 ctx_desc = lower_32_bits(intel_lr_context_descriptor(client->owner,
  496. engine));
  497. u32 *cs = ring->vaddr + ring->tail;
  498. u32 data[7];
  499. if (engine->id == RCS) {
  500. cs = gen8_emit_ggtt_write_rcs(cs, GUC_PREEMPT_FINISHED,
  501. intel_hws_preempt_done_address(engine));
  502. } else {
  503. cs = gen8_emit_ggtt_write(cs, GUC_PREEMPT_FINISHED,
  504. intel_hws_preempt_done_address(engine));
  505. *cs++ = MI_NOOP;
  506. *cs++ = MI_NOOP;
  507. }
  508. *cs++ = MI_USER_INTERRUPT;
  509. *cs++ = MI_NOOP;
  510. GEM_BUG_ON(!IS_ALIGNED(ring->size,
  511. GUC_PREEMPT_BREADCRUMB_DWORDS * sizeof(u32)));
  512. GEM_BUG_ON((void *)cs - (ring->vaddr + ring->tail) !=
  513. GUC_PREEMPT_BREADCRUMB_DWORDS * sizeof(u32));
  514. ring->tail += GUC_PREEMPT_BREADCRUMB_DWORDS * sizeof(u32);
  515. ring->tail &= (ring->size - 1);
  516. flush_ggtt_writes(ring->vma);
  517. spin_lock_irq(&client->wq_lock);
  518. guc_wq_item_append(client, engine->guc_id, ctx_desc,
  519. ring->tail / sizeof(u64), 0);
  520. spin_unlock_irq(&client->wq_lock);
  521. /*
  522. * If GuC firmware performs an engine reset while that engine had
  523. * a preemption pending, it will set the terminated attribute bit
  524. * on our preemption stage descriptor. GuC firmware retains all
  525. * pending work items for a high-priority GuC client, unlike the
  526. * normal-priority GuC client where work items are dropped. It
  527. * wants to make sure the preempt-to-idle work doesn't run when
  528. * scheduling resumes, and uses this bit to inform its scheduler
  529. * and presumably us as well. Our job is to clear it for the next
  530. * preemption after reset, otherwise that and future preemptions
  531. * will never complete. We'll just clear it every time.
  532. */
  533. stage_desc->attribute &= ~GUC_STAGE_DESC_ATTR_TERMINATED;
  534. data[0] = INTEL_GUC_ACTION_REQUEST_PREEMPTION;
  535. data[1] = client->stage_id;
  536. data[2] = INTEL_GUC_PREEMPT_OPTION_DROP_WORK_Q |
  537. INTEL_GUC_PREEMPT_OPTION_DROP_SUBMIT_Q;
  538. data[3] = engine->guc_id;
  539. data[4] = guc->execbuf_client->priority;
  540. data[5] = guc->execbuf_client->stage_id;
  541. data[6] = guc_ggtt_offset(guc->shared_data);
  542. if (WARN_ON(intel_guc_send(guc, data, ARRAY_SIZE(data)))) {
  543. execlists_clear_active(&engine->execlists,
  544. EXECLISTS_ACTIVE_PREEMPT);
  545. tasklet_schedule(&engine->execlists.tasklet);
  546. }
  547. }
  548. /*
  549. * We're using user interrupt and HWSP value to mark that preemption has
  550. * finished and GPU is idle. Normally, we could unwind and continue similar to
  551. * execlists submission path. Unfortunately, with GuC we also need to wait for
  552. * it to finish its own postprocessing, before attempting to submit. Otherwise
  553. * GuC may silently ignore our submissions, and thus we risk losing request at
  554. * best, executing out-of-order and causing kernel panic at worst.
  555. */
  556. #define GUC_PREEMPT_POSTPROCESS_DELAY_MS 10
  557. static void wait_for_guc_preempt_report(struct intel_engine_cs *engine)
  558. {
  559. struct intel_guc *guc = &engine->i915->guc;
  560. struct guc_shared_ctx_data *data = guc->shared_data_vaddr;
  561. struct guc_ctx_report *report =
  562. &data->preempt_ctx_report[engine->guc_id];
  563. WARN_ON(wait_for_atomic(report->report_return_status ==
  564. INTEL_GUC_REPORT_STATUS_COMPLETE,
  565. GUC_PREEMPT_POSTPROCESS_DELAY_MS));
  566. /*
  567. * GuC is expecting that we're also going to clear the affected context
  568. * counter, let's also reset the return status to not depend on GuC
  569. * resetting it after recieving another preempt action
  570. */
  571. report->affected_count = 0;
  572. report->report_return_status = INTEL_GUC_REPORT_STATUS_UNKNOWN;
  573. }
  574. /**
  575. * guc_submit() - Submit commands through GuC
  576. * @engine: engine associated with the commands
  577. *
  578. * The only error here arises if the doorbell hardware isn't functioning
  579. * as expected, which really shouln't happen.
  580. */
  581. static void guc_submit(struct intel_engine_cs *engine)
  582. {
  583. struct intel_guc *guc = &engine->i915->guc;
  584. struct intel_engine_execlists * const execlists = &engine->execlists;
  585. struct execlist_port *port = execlists->port;
  586. unsigned int n;
  587. for (n = 0; n < execlists_num_ports(execlists); n++) {
  588. struct drm_i915_gem_request *rq;
  589. unsigned int count;
  590. rq = port_unpack(&port[n], &count);
  591. if (rq && count == 0) {
  592. port_set(&port[n], port_pack(rq, ++count));
  593. flush_ggtt_writes(rq->ring->vma);
  594. guc_add_request(guc, rq);
  595. }
  596. }
  597. }
  598. static void port_assign(struct execlist_port *port,
  599. struct drm_i915_gem_request *rq)
  600. {
  601. GEM_BUG_ON(rq == port_request(port));
  602. if (port_isset(port))
  603. i915_gem_request_put(port_request(port));
  604. port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
  605. }
  606. static void guc_dequeue(struct intel_engine_cs *engine)
  607. {
  608. struct intel_engine_execlists * const execlists = &engine->execlists;
  609. struct execlist_port *port = execlists->port;
  610. struct drm_i915_gem_request *last = NULL;
  611. const struct execlist_port * const last_port =
  612. &execlists->port[execlists->port_mask];
  613. bool submit = false;
  614. struct rb_node *rb;
  615. spin_lock_irq(&engine->timeline->lock);
  616. rb = execlists->first;
  617. GEM_BUG_ON(rb_first(&execlists->queue) != rb);
  618. if (!rb)
  619. goto unlock;
  620. if (HAS_LOGICAL_RING_PREEMPTION(engine->i915) && port_isset(port)) {
  621. struct guc_preempt_work *preempt_work =
  622. &engine->i915->guc.preempt_work[engine->id];
  623. if (rb_entry(rb, struct i915_priolist, node)->priority >
  624. max(port_request(port)->priotree.priority, 0)) {
  625. execlists_set_active(execlists,
  626. EXECLISTS_ACTIVE_PREEMPT);
  627. queue_work(engine->i915->guc.preempt_wq,
  628. &preempt_work->work);
  629. goto unlock;
  630. } else if (port_isset(last_port)) {
  631. goto unlock;
  632. }
  633. port++;
  634. }
  635. do {
  636. struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
  637. struct drm_i915_gem_request *rq, *rn;
  638. list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
  639. if (last && rq->ctx != last->ctx) {
  640. if (port == last_port) {
  641. __list_del_many(&p->requests,
  642. &rq->priotree.link);
  643. goto done;
  644. }
  645. if (submit)
  646. port_assign(port, last);
  647. port++;
  648. }
  649. INIT_LIST_HEAD(&rq->priotree.link);
  650. __i915_gem_request_submit(rq);
  651. trace_i915_gem_request_in(rq,
  652. port_index(port, execlists));
  653. last = rq;
  654. submit = true;
  655. }
  656. rb = rb_next(rb);
  657. rb_erase(&p->node, &execlists->queue);
  658. INIT_LIST_HEAD(&p->requests);
  659. if (p->priority != I915_PRIORITY_NORMAL)
  660. kmem_cache_free(engine->i915->priorities, p);
  661. } while (rb);
  662. done:
  663. execlists->first = rb;
  664. if (submit) {
  665. port_assign(port, last);
  666. execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
  667. guc_submit(engine);
  668. }
  669. unlock:
  670. spin_unlock_irq(&engine->timeline->lock);
  671. }
  672. static void guc_submission_tasklet(unsigned long data)
  673. {
  674. struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
  675. struct intel_engine_execlists * const execlists = &engine->execlists;
  676. struct execlist_port *port = execlists->port;
  677. struct drm_i915_gem_request *rq;
  678. rq = port_request(&port[0]);
  679. while (rq && i915_gem_request_completed(rq)) {
  680. trace_i915_gem_request_out(rq);
  681. i915_gem_request_put(rq);
  682. execlists_port_complete(execlists, port);
  683. rq = port_request(&port[0]);
  684. }
  685. if (!rq)
  686. execlists_clear_active(execlists, EXECLISTS_ACTIVE_USER);
  687. if (execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT) &&
  688. intel_read_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX) ==
  689. GUC_PREEMPT_FINISHED) {
  690. execlists_cancel_port_requests(&engine->execlists);
  691. execlists_unwind_incomplete_requests(execlists);
  692. wait_for_guc_preempt_report(engine);
  693. execlists_clear_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
  694. intel_write_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX, 0);
  695. }
  696. if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
  697. guc_dequeue(engine);
  698. }
  699. /*
  700. * Everything below here is concerned with setup & teardown, and is
  701. * therefore not part of the somewhat time-critical batch-submission
  702. * path of guc_submit() above.
  703. */
  704. /* Check that a doorbell register is in the expected state */
  705. static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
  706. {
  707. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  708. u32 drbregl;
  709. bool valid;
  710. GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
  711. drbregl = I915_READ(GEN8_DRBREGL(db_id));
  712. valid = drbregl & GEN8_DRB_VALID;
  713. if (test_bit(db_id, guc->doorbell_bitmap) == valid)
  714. return true;
  715. DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
  716. db_id, drbregl, yesno(valid));
  717. return false;
  718. }
  719. /*
  720. * If the GuC thinks that the doorbell is unassigned (e.g. because we reset and
  721. * reloaded the GuC FW) we can use this function to tell the GuC to reassign the
  722. * doorbell to the rightful owner.
  723. */
  724. static int __reset_doorbell(struct intel_guc_client *client, u16 db_id)
  725. {
  726. int err;
  727. __update_doorbell_desc(client, db_id);
  728. err = __create_doorbell(client);
  729. if (!err)
  730. err = __destroy_doorbell(client);
  731. return err;
  732. }
  733. /*
  734. * Set up & tear down each unused doorbell in turn, to ensure that all doorbell
  735. * HW is (re)initialised. For that end, we might have to borrow the first
  736. * client. Also, tell GuC about all the doorbells in use by all clients.
  737. * We do this because the KMD, the GuC and the doorbell HW can easily go out of
  738. * sync (e.g. we can reset the GuC, but not the doorbel HW).
  739. */
  740. static int guc_init_doorbell_hw(struct intel_guc *guc)
  741. {
  742. struct intel_guc_client *client = guc->execbuf_client;
  743. bool recreate_first_client = false;
  744. u16 db_id;
  745. int ret;
  746. /* For unused doorbells, make sure they are disabled */
  747. for_each_clear_bit(db_id, guc->doorbell_bitmap, GUC_NUM_DOORBELLS) {
  748. if (doorbell_ok(guc, db_id))
  749. continue;
  750. if (has_doorbell(client)) {
  751. /* Borrow execbuf_client (we will recreate it later) */
  752. destroy_doorbell(client);
  753. recreate_first_client = true;
  754. }
  755. ret = __reset_doorbell(client, db_id);
  756. WARN(ret, "Doorbell %u reset failed, err %d\n", db_id, ret);
  757. }
  758. if (recreate_first_client) {
  759. ret = __reserve_doorbell(client);
  760. if (unlikely(ret)) {
  761. DRM_ERROR("Couldn't re-reserve first client db: %d\n",
  762. ret);
  763. return ret;
  764. }
  765. __update_doorbell_desc(client, client->doorbell_id);
  766. }
  767. /* Now for every client (and not only execbuf_client) make sure their
  768. * doorbells are known by the GuC
  769. */
  770. ret = __create_doorbell(guc->execbuf_client);
  771. if (ret)
  772. return ret;
  773. ret = __create_doorbell(guc->preempt_client);
  774. if (ret) {
  775. __destroy_doorbell(guc->execbuf_client);
  776. return ret;
  777. }
  778. /* Read back & verify all (used & unused) doorbell registers */
  779. for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
  780. WARN_ON(!doorbell_ok(guc, db_id));
  781. return 0;
  782. }
  783. /**
  784. * guc_client_alloc() - Allocate an intel_guc_client
  785. * @dev_priv: driver private data structure
  786. * @engines: The set of engines to enable for this client
  787. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  788. * The kernel client to replace ExecList submission is created with
  789. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  790. * while a preemption context can use CRITICAL.
  791. * @ctx: the context that owns the client (we use the default render
  792. * context)
  793. *
  794. * Return: An intel_guc_client object if success, else NULL.
  795. */
  796. static struct intel_guc_client *
  797. guc_client_alloc(struct drm_i915_private *dev_priv,
  798. u32 engines,
  799. u32 priority,
  800. struct i915_gem_context *ctx)
  801. {
  802. struct intel_guc_client *client;
  803. struct intel_guc *guc = &dev_priv->guc;
  804. struct i915_vma *vma;
  805. void *vaddr;
  806. int ret;
  807. client = kzalloc(sizeof(*client), GFP_KERNEL);
  808. if (!client)
  809. return ERR_PTR(-ENOMEM);
  810. client->guc = guc;
  811. client->owner = ctx;
  812. client->engines = engines;
  813. client->priority = priority;
  814. client->doorbell_id = GUC_DOORBELL_INVALID;
  815. spin_lock_init(&client->wq_lock);
  816. ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
  817. GFP_KERNEL);
  818. if (ret < 0)
  819. goto err_client;
  820. client->stage_id = ret;
  821. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  822. vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
  823. if (IS_ERR(vma)) {
  824. ret = PTR_ERR(vma);
  825. goto err_id;
  826. }
  827. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  828. client->vma = vma;
  829. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  830. if (IS_ERR(vaddr)) {
  831. ret = PTR_ERR(vaddr);
  832. goto err_vma;
  833. }
  834. client->vaddr = vaddr;
  835. client->doorbell_offset = __select_cacheline(guc);
  836. /*
  837. * Since the doorbell only requires a single cacheline, we can save
  838. * space by putting the application process descriptor in the same
  839. * page. Use the half of the page that doesn't include the doorbell.
  840. */
  841. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  842. client->proc_desc_offset = 0;
  843. else
  844. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  845. guc_proc_desc_init(guc, client);
  846. guc_stage_desc_init(guc, client);
  847. ret = create_doorbell(client);
  848. if (ret)
  849. goto err_vaddr;
  850. DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
  851. priority, client, client->engines, client->stage_id);
  852. DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
  853. client->doorbell_id, client->doorbell_offset);
  854. return client;
  855. err_vaddr:
  856. i915_gem_object_unpin_map(client->vma->obj);
  857. err_vma:
  858. i915_vma_unpin_and_release(&client->vma);
  859. err_id:
  860. ida_simple_remove(&guc->stage_ids, client->stage_id);
  861. err_client:
  862. kfree(client);
  863. return ERR_PTR(ret);
  864. }
  865. static void guc_client_free(struct intel_guc_client *client)
  866. {
  867. /*
  868. * XXX: wait for any outstanding submissions before freeing memory.
  869. * Be sure to drop any locks
  870. */
  871. /* FIXME: in many cases, by the time we get here the GuC has been
  872. * reset, so we cannot destroy the doorbell properly. Ignore the
  873. * error message for now
  874. */
  875. destroy_doorbell(client);
  876. guc_stage_desc_fini(client->guc, client);
  877. i915_gem_object_unpin_map(client->vma->obj);
  878. i915_vma_unpin_and_release(&client->vma);
  879. ida_simple_remove(&client->guc->stage_ids, client->stage_id);
  880. kfree(client);
  881. }
  882. static int guc_clients_create(struct intel_guc *guc)
  883. {
  884. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  885. struct intel_guc_client *client;
  886. GEM_BUG_ON(guc->execbuf_client);
  887. GEM_BUG_ON(guc->preempt_client);
  888. client = guc_client_alloc(dev_priv,
  889. INTEL_INFO(dev_priv)->ring_mask,
  890. GUC_CLIENT_PRIORITY_KMD_NORMAL,
  891. dev_priv->kernel_context);
  892. if (IS_ERR(client)) {
  893. DRM_ERROR("Failed to create GuC client for submission!\n");
  894. return PTR_ERR(client);
  895. }
  896. guc->execbuf_client = client;
  897. client = guc_client_alloc(dev_priv,
  898. INTEL_INFO(dev_priv)->ring_mask,
  899. GUC_CLIENT_PRIORITY_KMD_HIGH,
  900. dev_priv->preempt_context);
  901. if (IS_ERR(client)) {
  902. DRM_ERROR("Failed to create GuC client for preemption!\n");
  903. guc_client_free(guc->execbuf_client);
  904. guc->execbuf_client = NULL;
  905. return PTR_ERR(client);
  906. }
  907. guc->preempt_client = client;
  908. return 0;
  909. }
  910. static void guc_clients_destroy(struct intel_guc *guc)
  911. {
  912. struct intel_guc_client *client;
  913. client = fetch_and_zero(&guc->execbuf_client);
  914. guc_client_free(client);
  915. client = fetch_and_zero(&guc->preempt_client);
  916. guc_client_free(client);
  917. }
  918. static void guc_policy_init(struct guc_policy *policy)
  919. {
  920. policy->execution_quantum = POLICY_DEFAULT_EXECUTION_QUANTUM_US;
  921. policy->preemption_time = POLICY_DEFAULT_PREEMPTION_TIME_US;
  922. policy->fault_time = POLICY_DEFAULT_FAULT_TIME_US;
  923. policy->policy_flags = 0;
  924. }
  925. static void guc_policies_init(struct guc_policies *policies)
  926. {
  927. struct guc_policy *policy;
  928. u32 p, i;
  929. policies->dpc_promote_time = POLICY_DEFAULT_DPC_PROMOTE_TIME_US;
  930. policies->max_num_work_items = POLICY_MAX_NUM_WI;
  931. for (p = 0; p < GUC_CLIENT_PRIORITY_NUM; p++) {
  932. for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
  933. policy = &policies->policy[p][i];
  934. guc_policy_init(policy);
  935. }
  936. }
  937. policies->is_valid = 1;
  938. }
  939. /*
  940. * The first 80 dwords of the register state context, containing the
  941. * execlists and ppgtt registers.
  942. */
  943. #define LR_HW_CONTEXT_SIZE (80 * sizeof(u32))
  944. static int guc_ads_create(struct intel_guc *guc)
  945. {
  946. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  947. struct i915_vma *vma;
  948. struct page *page;
  949. /* The ads obj includes the struct itself and buffers passed to GuC */
  950. struct {
  951. struct guc_ads ads;
  952. struct guc_policies policies;
  953. struct guc_mmio_reg_state reg_state;
  954. u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
  955. } __packed *blob;
  956. struct intel_engine_cs *engine;
  957. enum intel_engine_id id;
  958. const u32 skipped_offset = LRC_HEADER_PAGES * PAGE_SIZE;
  959. const u32 skipped_size = LRC_PPHWSP_SZ * PAGE_SIZE + LR_HW_CONTEXT_SIZE;
  960. u32 base;
  961. GEM_BUG_ON(guc->ads_vma);
  962. vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
  963. if (IS_ERR(vma))
  964. return PTR_ERR(vma);
  965. guc->ads_vma = vma;
  966. page = i915_vma_first_page(vma);
  967. blob = kmap(page);
  968. /* GuC scheduling policies */
  969. guc_policies_init(&blob->policies);
  970. /* MMIO reg state */
  971. for_each_engine(engine, dev_priv, id) {
  972. blob->reg_state.white_list[engine->guc_id].mmio_start =
  973. engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
  974. /* Nothing to be saved or restored for now. */
  975. blob->reg_state.white_list[engine->guc_id].count = 0;
  976. }
  977. /*
  978. * The GuC requires a "Golden Context" when it reinitialises
  979. * engines after a reset. Here we use the Render ring default
  980. * context, which must already exist and be pinned in the GGTT,
  981. * so its address won't change after we've told the GuC where
  982. * to find it. Note that we have to skip our header (1 page),
  983. * because our GuC shared data is there.
  984. */
  985. blob->ads.golden_context_lrca =
  986. guc_ggtt_offset(dev_priv->kernel_context->engine[RCS].state) +
  987. skipped_offset;
  988. /*
  989. * The GuC expects us to exclude the portion of the context image that
  990. * it skips from the size it is to read. It starts reading from after
  991. * the execlist context (so skipping the first page [PPHWSP] and 80
  992. * dwords). Weird guc is weird.
  993. */
  994. for_each_engine(engine, dev_priv, id)
  995. blob->ads.eng_state_size[engine->guc_id] =
  996. engine->context_size - skipped_size;
  997. base = guc_ggtt_offset(vma);
  998. blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
  999. blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
  1000. blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
  1001. kunmap(page);
  1002. return 0;
  1003. }
  1004. static void guc_ads_destroy(struct intel_guc *guc)
  1005. {
  1006. i915_vma_unpin_and_release(&guc->ads_vma);
  1007. }
  1008. static int guc_preempt_work_create(struct intel_guc *guc)
  1009. {
  1010. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1011. struct intel_engine_cs *engine;
  1012. enum intel_engine_id id;
  1013. /*
  1014. * Even though both sending GuC action, and adding a new workitem to
  1015. * GuC workqueue are serialized (each with its own locking), since
  1016. * we're using mutliple engines, it's possible that we're going to
  1017. * issue a preempt request with two (or more - each for different
  1018. * engine) workitems in GuC queue. In this situation, GuC may submit
  1019. * all of them, which will make us very confused.
  1020. * Our preemption contexts may even already be complete - before we
  1021. * even had the chance to sent the preempt action to GuC!. Rather
  1022. * than introducing yet another lock, we can just use ordered workqueue
  1023. * to make sure we're always sending a single preemption request with a
  1024. * single workitem.
  1025. */
  1026. guc->preempt_wq = alloc_ordered_workqueue("i915-guc_preempt",
  1027. WQ_HIGHPRI);
  1028. if (!guc->preempt_wq)
  1029. return -ENOMEM;
  1030. for_each_engine(engine, dev_priv, id) {
  1031. guc->preempt_work[id].engine = engine;
  1032. INIT_WORK(&guc->preempt_work[id].work, inject_preempt_context);
  1033. }
  1034. return 0;
  1035. }
  1036. static void guc_preempt_work_destroy(struct intel_guc *guc)
  1037. {
  1038. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1039. struct intel_engine_cs *engine;
  1040. enum intel_engine_id id;
  1041. for_each_engine(engine, dev_priv, id)
  1042. cancel_work_sync(&guc->preempt_work[id].work);
  1043. destroy_workqueue(guc->preempt_wq);
  1044. guc->preempt_wq = NULL;
  1045. }
  1046. /*
  1047. * Set up the memory resources to be shared with the GuC (via the GGTT)
  1048. * at firmware loading time.
  1049. */
  1050. int intel_guc_submission_init(struct intel_guc *guc)
  1051. {
  1052. int ret;
  1053. if (guc->stage_desc_pool)
  1054. return 0;
  1055. ret = guc_stage_desc_pool_create(guc);
  1056. if (ret)
  1057. return ret;
  1058. /*
  1059. * Keep static analysers happy, let them know that we allocated the
  1060. * vma after testing that it didn't exist earlier.
  1061. */
  1062. GEM_BUG_ON(!guc->stage_desc_pool);
  1063. ret = guc_shared_data_create(guc);
  1064. if (ret)
  1065. goto err_stage_desc_pool;
  1066. GEM_BUG_ON(!guc->shared_data);
  1067. ret = intel_guc_log_create(guc);
  1068. if (ret < 0)
  1069. goto err_shared_data;
  1070. ret = guc_preempt_work_create(guc);
  1071. if (ret)
  1072. goto err_log;
  1073. GEM_BUG_ON(!guc->preempt_wq);
  1074. ret = guc_ads_create(guc);
  1075. if (ret < 0)
  1076. goto err_wq;
  1077. GEM_BUG_ON(!guc->ads_vma);
  1078. return 0;
  1079. err_wq:
  1080. guc_preempt_work_destroy(guc);
  1081. err_log:
  1082. intel_guc_log_destroy(guc);
  1083. err_shared_data:
  1084. guc_shared_data_destroy(guc);
  1085. err_stage_desc_pool:
  1086. guc_stage_desc_pool_destroy(guc);
  1087. return ret;
  1088. }
  1089. void intel_guc_submission_fini(struct intel_guc *guc)
  1090. {
  1091. guc_ads_destroy(guc);
  1092. guc_preempt_work_destroy(guc);
  1093. intel_guc_log_destroy(guc);
  1094. guc_shared_data_destroy(guc);
  1095. guc_stage_desc_pool_destroy(guc);
  1096. }
  1097. static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
  1098. {
  1099. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  1100. struct intel_engine_cs *engine;
  1101. enum intel_engine_id id;
  1102. int irqs;
  1103. /* tell all command streamers to forward interrupts (but not vblank)
  1104. * to GuC
  1105. */
  1106. irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
  1107. for_each_engine(engine, dev_priv, id)
  1108. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  1109. /* route USER_INTERRUPT to Host, all others are sent to GuC. */
  1110. irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  1111. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1112. /* These three registers have the same bit definitions */
  1113. I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
  1114. I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
  1115. I915_WRITE(GUC_WD_VECS_IER, ~irqs);
  1116. /*
  1117. * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
  1118. * (unmasked) PM interrupts to the GuC. All other bits of this
  1119. * register *disable* generation of a specific interrupt.
  1120. *
  1121. * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
  1122. * writing to the PM interrupt mask register, i.e. interrupts
  1123. * that must not be disabled.
  1124. *
  1125. * If the GuC is handling these interrupts, then we must not let
  1126. * the PM code disable ANY interrupt that the GuC is expecting.
  1127. * So for each ENABLED (0) bit in this register, we must SET the
  1128. * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
  1129. * GuC needs ARAT expired interrupt unmasked hence it is set in
  1130. * pm_intrmsk_mbz.
  1131. *
  1132. * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
  1133. * result in the register bit being left SET!
  1134. */
  1135. rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
  1136. rps->pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  1137. }
  1138. static void guc_interrupts_release(struct drm_i915_private *dev_priv)
  1139. {
  1140. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  1141. struct intel_engine_cs *engine;
  1142. enum intel_engine_id id;
  1143. int irqs;
  1144. /*
  1145. * tell all command streamers NOT to forward interrupts or vblank
  1146. * to GuC.
  1147. */
  1148. irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
  1149. irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
  1150. for_each_engine(engine, dev_priv, id)
  1151. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  1152. /* route all GT interrupts to the host */
  1153. I915_WRITE(GUC_BCS_RCS_IER, 0);
  1154. I915_WRITE(GUC_VCS2_VCS1_IER, 0);
  1155. I915_WRITE(GUC_WD_VECS_IER, 0);
  1156. rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
  1157. rps->pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
  1158. }
  1159. static void guc_submission_park(struct intel_engine_cs *engine)
  1160. {
  1161. intel_engine_unpin_breadcrumbs_irq(engine);
  1162. }
  1163. static void guc_submission_unpark(struct intel_engine_cs *engine)
  1164. {
  1165. intel_engine_pin_breadcrumbs_irq(engine);
  1166. }
  1167. int intel_guc_submission_enable(struct intel_guc *guc)
  1168. {
  1169. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1170. struct intel_engine_cs *engine;
  1171. enum intel_engine_id id;
  1172. int err;
  1173. /*
  1174. * We're using GuC work items for submitting work through GuC. Since
  1175. * we're coalescing multiple requests from a single context into a
  1176. * single work item prior to assigning it to execlist_port, we can
  1177. * never have more work items than the total number of ports (for all
  1178. * engines). The GuC firmware is controlling the HEAD of work queue,
  1179. * and it is guaranteed that it will remove the work item from the
  1180. * queue before our request is completed.
  1181. */
  1182. BUILD_BUG_ON(ARRAY_SIZE(engine->execlists.port) *
  1183. sizeof(struct guc_wq_item) *
  1184. I915_NUM_ENGINES > GUC_WQ_SIZE);
  1185. /*
  1186. * We're being called on both module initialization and on reset,
  1187. * until this flow is changed, we're using regular client presence to
  1188. * determine which case are we in, and whether we should allocate new
  1189. * clients or just reset their workqueues.
  1190. */
  1191. if (!guc->execbuf_client) {
  1192. err = guc_clients_create(guc);
  1193. if (err)
  1194. return err;
  1195. } else {
  1196. guc_reset_wq(guc->execbuf_client);
  1197. guc_reset_wq(guc->preempt_client);
  1198. }
  1199. err = intel_guc_sample_forcewake(guc);
  1200. if (err)
  1201. goto err_free_clients;
  1202. err = guc_init_doorbell_hw(guc);
  1203. if (err)
  1204. goto err_free_clients;
  1205. /* Take over from manual control of ELSP (execlists) */
  1206. guc_interrupts_capture(dev_priv);
  1207. for_each_engine(engine, dev_priv, id) {
  1208. struct intel_engine_execlists * const execlists =
  1209. &engine->execlists;
  1210. execlists->tasklet.func = guc_submission_tasklet;
  1211. engine->park = guc_submission_park;
  1212. engine->unpark = guc_submission_unpark;
  1213. }
  1214. return 0;
  1215. err_free_clients:
  1216. guc_clients_destroy(guc);
  1217. return err;
  1218. }
  1219. void intel_guc_submission_disable(struct intel_guc *guc)
  1220. {
  1221. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1222. GEM_BUG_ON(dev_priv->gt.awake); /* GT should be parked first */
  1223. guc_interrupts_release(dev_priv);
  1224. /* Revert back to manual ELSP submission */
  1225. intel_engines_reset_default_submission(dev_priv);
  1226. guc_clients_destroy(guc);
  1227. }
  1228. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1229. #include "selftests/intel_guc.c"
  1230. #endif