i915_guc_submission.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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 <linux/debugfs.h>
  26. #include <linux/relay.h>
  27. #include "i915_drv.h"
  28. #include "intel_uc.h"
  29. /**
  30. * DOC: GuC-based command submission
  31. *
  32. * i915_guc_client:
  33. * We use the term client to avoid confusion with contexts. A i915_guc_client is
  34. * equivalent to GuC object guc_context_desc. This context descriptor is
  35. * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
  36. * and workqueue for it. Also the process descriptor (guc_process_desc), which
  37. * is mapped to client space. So the client can write Work Item then ring the
  38. * doorbell.
  39. *
  40. * To simplify the implementation, we allocate one gem object that contains all
  41. * pages for doorbell, process descriptor and workqueue.
  42. *
  43. * The Scratch registers:
  44. * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
  45. * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
  46. * triggers an interrupt on the GuC via another register write (0xC4C8).
  47. * Firmware writes a success/fail code back to the action register after
  48. * processes the request. The kernel driver polls waiting for this update and
  49. * then proceeds.
  50. * See intel_guc_send()
  51. *
  52. * Doorbells:
  53. * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
  54. * mapped into process space.
  55. *
  56. * Work Items:
  57. * There are several types of work items that the host may place into a
  58. * workqueue, each with its own requirements and limitations. Currently only
  59. * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
  60. * represents in-order queue. The kernel driver packs ring tail pointer and an
  61. * ELSP context descriptor dword into Work Item.
  62. * See guc_wq_item_append()
  63. *
  64. */
  65. /*
  66. * Tell the GuC to allocate or deallocate a specific doorbell
  67. */
  68. static int guc_allocate_doorbell(struct intel_guc *guc,
  69. struct i915_guc_client *client)
  70. {
  71. u32 action[] = {
  72. INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
  73. client->ctx_index
  74. };
  75. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  76. }
  77. static int guc_release_doorbell(struct intel_guc *guc,
  78. struct i915_guc_client *client)
  79. {
  80. u32 action[] = {
  81. INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
  82. client->ctx_index
  83. };
  84. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  85. }
  86. /*
  87. * Initialise, update, or clear doorbell data shared with the GuC
  88. *
  89. * These functions modify shared data and so need access to the mapped
  90. * client object which contains the page being used for the doorbell
  91. */
  92. static int guc_update_doorbell_id(struct intel_guc *guc,
  93. struct i915_guc_client *client,
  94. u16 new_id)
  95. {
  96. struct sg_table *sg = guc->ctx_pool_vma->pages;
  97. void *doorbell_bitmap = guc->doorbell_bitmap;
  98. struct guc_doorbell_info *doorbell;
  99. struct guc_context_desc desc;
  100. size_t len;
  101. doorbell = client->vaddr + client->doorbell_offset;
  102. if (client->doorbell_id != GUC_INVALID_DOORBELL_ID &&
  103. test_bit(client->doorbell_id, doorbell_bitmap)) {
  104. /* Deactivate the old doorbell */
  105. doorbell->db_status = GUC_DOORBELL_DISABLED;
  106. (void)guc_release_doorbell(guc, client);
  107. __clear_bit(client->doorbell_id, doorbell_bitmap);
  108. }
  109. /* Update the GuC's idea of the doorbell ID */
  110. len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  111. sizeof(desc) * client->ctx_index);
  112. if (len != sizeof(desc))
  113. return -EFAULT;
  114. desc.db_id = new_id;
  115. len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  116. sizeof(desc) * client->ctx_index);
  117. if (len != sizeof(desc))
  118. return -EFAULT;
  119. client->doorbell_id = new_id;
  120. if (new_id == GUC_INVALID_DOORBELL_ID)
  121. return 0;
  122. /* Activate the new doorbell */
  123. __set_bit(new_id, doorbell_bitmap);
  124. doorbell->db_status = GUC_DOORBELL_ENABLED;
  125. doorbell->cookie = client->doorbell_cookie;
  126. return guc_allocate_doorbell(guc, client);
  127. }
  128. static void guc_disable_doorbell(struct intel_guc *guc,
  129. struct i915_guc_client *client)
  130. {
  131. (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
  132. /* XXX: wait for any interrupts */
  133. /* XXX: wait for workqueue to drain */
  134. }
  135. static uint16_t
  136. select_doorbell_register(struct intel_guc *guc, uint32_t priority)
  137. {
  138. /*
  139. * The bitmap tracks which doorbell registers are currently in use.
  140. * It is split into two halves; the first half is used for normal
  141. * priority contexts, the second half for high-priority ones.
  142. * Note that logically higher priorities are numerically less than
  143. * normal ones, so the test below means "is it high-priority?"
  144. */
  145. const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
  146. const uint16_t half = GUC_MAX_DOORBELLS / 2;
  147. const uint16_t start = hi_pri ? half : 0;
  148. const uint16_t end = start + half;
  149. uint16_t id;
  150. id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
  151. if (id == end)
  152. id = GUC_INVALID_DOORBELL_ID;
  153. DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
  154. hi_pri ? "high" : "normal", id);
  155. return id;
  156. }
  157. /*
  158. * Select, assign and relase doorbell cachelines
  159. *
  160. * These functions track which doorbell cachelines are in use.
  161. * The data they manipulate is protected by the intel_guc_send lock.
  162. */
  163. static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
  164. {
  165. const uint32_t cacheline_size = cache_line_size();
  166. uint32_t offset;
  167. /* Doorbell uses a single cache line within a page */
  168. offset = offset_in_page(guc->db_cacheline);
  169. /* Moving to next cache line to reduce contention */
  170. guc->db_cacheline += cacheline_size;
  171. DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
  172. offset, guc->db_cacheline, cacheline_size);
  173. return offset;
  174. }
  175. /*
  176. * Initialise the process descriptor shared with the GuC firmware.
  177. */
  178. static void guc_proc_desc_init(struct intel_guc *guc,
  179. struct i915_guc_client *client)
  180. {
  181. struct guc_process_desc *desc;
  182. desc = client->vaddr + client->proc_desc_offset;
  183. memset(desc, 0, sizeof(*desc));
  184. /*
  185. * XXX: pDoorbell and WQVBaseAddress are pointers in process address
  186. * space for ring3 clients (set them as in mmap_ioctl) or kernel
  187. * space for kernel clients (map on demand instead? May make debug
  188. * easier to have it mapped).
  189. */
  190. desc->wq_base_addr = 0;
  191. desc->db_base_addr = 0;
  192. desc->context_id = client->ctx_index;
  193. desc->wq_size_bytes = client->wq_size;
  194. desc->wq_status = WQ_STATUS_ACTIVE;
  195. desc->priority = client->priority;
  196. }
  197. /*
  198. * Initialise/clear the context descriptor shared with the GuC firmware.
  199. *
  200. * This descriptor tells the GuC where (in GGTT space) to find the important
  201. * data structures relating to this client (doorbell, process descriptor,
  202. * write queue, etc).
  203. */
  204. static void guc_ctx_desc_init(struct intel_guc *guc,
  205. struct i915_guc_client *client)
  206. {
  207. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  208. struct intel_engine_cs *engine;
  209. struct i915_gem_context *ctx = client->owner;
  210. struct guc_context_desc desc;
  211. struct sg_table *sg;
  212. unsigned int tmp;
  213. u32 gfx_addr;
  214. memset(&desc, 0, sizeof(desc));
  215. desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
  216. desc.context_id = client->ctx_index;
  217. desc.priority = client->priority;
  218. desc.db_id = client->doorbell_id;
  219. for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
  220. struct intel_context *ce = &ctx->engine[engine->id];
  221. uint32_t guc_engine_id = engine->guc_id;
  222. struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
  223. /* TODO: We have a design issue to be solved here. Only when we
  224. * receive the first batch, we know which engine is used by the
  225. * user. But here GuC expects the lrc and ring to be pinned. It
  226. * is not an issue for default context, which is the only one
  227. * for now who owns a GuC client. But for future owner of GuC
  228. * client, need to make sure lrc is pinned prior to enter here.
  229. */
  230. if (!ce->state)
  231. break; /* XXX: continue? */
  232. lrc->context_desc = lower_32_bits(ce->lrc_desc);
  233. /* The state page is after PPHWSP */
  234. lrc->ring_lcra =
  235. i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
  236. lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
  237. (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
  238. lrc->ring_begin = i915_ggtt_offset(ce->ring->vma);
  239. lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
  240. lrc->ring_next_free_location = lrc->ring_begin;
  241. lrc->ring_current_tail_pointer_value = 0;
  242. desc.engines_used |= (1 << guc_engine_id);
  243. }
  244. DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
  245. client->engines, desc.engines_used);
  246. WARN_ON(desc.engines_used == 0);
  247. /*
  248. * The doorbell, process descriptor, and workqueue are all parts
  249. * of the client object, which the GuC will reference via the GGTT
  250. */
  251. gfx_addr = i915_ggtt_offset(client->vma);
  252. desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
  253. client->doorbell_offset;
  254. desc.db_trigger_cpu =
  255. (uintptr_t)client->vaddr + client->doorbell_offset;
  256. desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
  257. desc.process_desc = gfx_addr + client->proc_desc_offset;
  258. desc.wq_addr = gfx_addr + client->wq_offset;
  259. desc.wq_size = client->wq_size;
  260. /*
  261. * XXX: Take LRCs from an existing context if this is not an
  262. * IsKMDCreatedContext client
  263. */
  264. desc.desc_private = (uintptr_t)client;
  265. /* Pool context is pinned already */
  266. sg = guc->ctx_pool_vma->pages;
  267. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  268. sizeof(desc) * client->ctx_index);
  269. }
  270. static void guc_ctx_desc_fini(struct intel_guc *guc,
  271. struct i915_guc_client *client)
  272. {
  273. struct guc_context_desc desc;
  274. struct sg_table *sg;
  275. memset(&desc, 0, sizeof(desc));
  276. sg = guc->ctx_pool_vma->pages;
  277. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  278. sizeof(desc) * client->ctx_index);
  279. }
  280. /**
  281. * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
  282. * @request: request associated with the commands
  283. *
  284. * Return: 0 if space is available
  285. * -EAGAIN if space is not currently available
  286. *
  287. * This function must be called (and must return 0) before a request
  288. * is submitted to the GuC via i915_guc_submit() below. Once a result
  289. * of 0 has been returned, it must be balanced by a corresponding
  290. * call to submit().
  291. *
  292. * Reservation allows the caller to determine in advance that space
  293. * will be available for the next submission before committing resources
  294. * to it, and helps avoid late failures with complicated recovery paths.
  295. */
  296. int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
  297. {
  298. const size_t wqi_size = sizeof(struct guc_wq_item);
  299. struct i915_guc_client *gc = request->i915->guc.execbuf_client;
  300. struct guc_process_desc *desc = gc->vaddr + gc->proc_desc_offset;
  301. u32 freespace;
  302. int ret;
  303. spin_lock(&gc->wq_lock);
  304. freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
  305. freespace -= gc->wq_rsvd;
  306. if (likely(freespace >= wqi_size)) {
  307. gc->wq_rsvd += wqi_size;
  308. ret = 0;
  309. } else {
  310. gc->no_wq_space++;
  311. ret = -EAGAIN;
  312. }
  313. spin_unlock(&gc->wq_lock);
  314. return ret;
  315. }
  316. void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
  317. {
  318. const size_t wqi_size = sizeof(struct guc_wq_item);
  319. struct i915_guc_client *gc = request->i915->guc.execbuf_client;
  320. GEM_BUG_ON(READ_ONCE(gc->wq_rsvd) < wqi_size);
  321. spin_lock(&gc->wq_lock);
  322. gc->wq_rsvd -= wqi_size;
  323. spin_unlock(&gc->wq_lock);
  324. }
  325. /* Construct a Work Item and append it to the GuC's Work Queue */
  326. static void guc_wq_item_append(struct i915_guc_client *gc,
  327. struct drm_i915_gem_request *rq)
  328. {
  329. /* wqi_len is in DWords, and does not include the one-word header */
  330. const size_t wqi_size = sizeof(struct guc_wq_item);
  331. const u32 wqi_len = wqi_size/sizeof(u32) - 1;
  332. struct intel_engine_cs *engine = rq->engine;
  333. struct guc_process_desc *desc;
  334. struct guc_wq_item *wqi;
  335. u32 freespace, tail, wq_off;
  336. desc = gc->vaddr + gc->proc_desc_offset;
  337. /* Free space is guaranteed, see i915_guc_wq_reserve() above */
  338. freespace = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
  339. GEM_BUG_ON(freespace < wqi_size);
  340. /* The GuC firmware wants the tail index in QWords, not bytes */
  341. tail = rq->tail;
  342. GEM_BUG_ON(tail & 7);
  343. tail >>= 3;
  344. GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
  345. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  346. * should not have the case where structure wqi is across page, neither
  347. * wrapped to the beginning. This simplifies the implementation below.
  348. *
  349. * XXX: if not the case, we need save data to a temp wqi and copy it to
  350. * workqueue buffer dw by dw.
  351. */
  352. BUILD_BUG_ON(wqi_size != 16);
  353. GEM_BUG_ON(gc->wq_rsvd < wqi_size);
  354. /* postincrement WQ tail for next time */
  355. wq_off = gc->wq_tail;
  356. GEM_BUG_ON(wq_off & (wqi_size - 1));
  357. gc->wq_tail += wqi_size;
  358. gc->wq_tail &= gc->wq_size - 1;
  359. gc->wq_rsvd -= wqi_size;
  360. /* WQ starts from the page after doorbell / process_desc */
  361. wqi = gc->vaddr + wq_off + GUC_DB_SIZE;
  362. /* Now fill in the 4-word work queue item */
  363. wqi->header = WQ_TYPE_INORDER |
  364. (wqi_len << WQ_LEN_SHIFT) |
  365. (engine->guc_id << WQ_TARGET_SHIFT) |
  366. WQ_NO_WCFLUSH_WAIT;
  367. /* The GuC wants only the low-order word of the context descriptor */
  368. wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
  369. wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
  370. wqi->fence_id = rq->global_seqno;
  371. }
  372. static int guc_ring_doorbell(struct i915_guc_client *gc)
  373. {
  374. struct guc_process_desc *desc;
  375. union guc_doorbell_qw db_cmp, db_exc, db_ret;
  376. union guc_doorbell_qw *db;
  377. int attempt = 2, ret = -EAGAIN;
  378. desc = gc->vaddr + gc->proc_desc_offset;
  379. /* Update the tail so it is visible to GuC */
  380. desc->tail = gc->wq_tail;
  381. /* current cookie */
  382. db_cmp.db_status = GUC_DOORBELL_ENABLED;
  383. db_cmp.cookie = gc->doorbell_cookie;
  384. /* cookie to be updated */
  385. db_exc.db_status = GUC_DOORBELL_ENABLED;
  386. db_exc.cookie = gc->doorbell_cookie + 1;
  387. if (db_exc.cookie == 0)
  388. db_exc.cookie = 1;
  389. /* pointer of current doorbell cacheline */
  390. db = gc->vaddr + gc->doorbell_offset;
  391. while (attempt--) {
  392. /* lets ring the doorbell */
  393. db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
  394. db_cmp.value_qw, db_exc.value_qw);
  395. /* if the exchange was successfully executed */
  396. if (db_ret.value_qw == db_cmp.value_qw) {
  397. /* db was successfully rung */
  398. gc->doorbell_cookie = db_exc.cookie;
  399. ret = 0;
  400. break;
  401. }
  402. /* XXX: doorbell was lost and need to acquire it again */
  403. if (db_ret.db_status == GUC_DOORBELL_DISABLED)
  404. break;
  405. DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
  406. db_cmp.cookie, db_ret.cookie);
  407. /* update the cookie to newly read cookie from GuC */
  408. db_cmp.cookie = db_ret.cookie;
  409. db_exc.cookie = db_ret.cookie + 1;
  410. if (db_exc.cookie == 0)
  411. db_exc.cookie = 1;
  412. }
  413. return ret;
  414. }
  415. /**
  416. * __i915_guc_submit() - Submit commands through GuC
  417. * @rq: request associated with the commands
  418. *
  419. * The caller must have already called i915_guc_wq_reserve() above with
  420. * a result of 0 (success), guaranteeing that there is space in the work
  421. * queue for the new request, so enqueuing the item cannot fail.
  422. *
  423. * Bad Things Will Happen if the caller violates this protocol e.g. calls
  424. * submit() when _reserve() says there's no space, or calls _submit()
  425. * a different number of times from (successful) calls to _reserve().
  426. *
  427. * The only error here arises if the doorbell hardware isn't functioning
  428. * as expected, which really shouln't happen.
  429. */
  430. static void __i915_guc_submit(struct drm_i915_gem_request *rq)
  431. {
  432. struct drm_i915_private *dev_priv = rq->i915;
  433. struct intel_engine_cs *engine = rq->engine;
  434. unsigned int engine_id = engine->id;
  435. struct intel_guc *guc = &rq->i915->guc;
  436. struct i915_guc_client *client = guc->execbuf_client;
  437. int b_ret;
  438. spin_lock(&client->wq_lock);
  439. guc_wq_item_append(client, rq);
  440. /* WA to flush out the pending GMADR writes to ring buffer. */
  441. if (i915_vma_is_map_and_fenceable(rq->ring->vma))
  442. POSTING_READ_FW(GUC_STATUS);
  443. b_ret = guc_ring_doorbell(client);
  444. client->submissions[engine_id] += 1;
  445. client->retcode = b_ret;
  446. if (b_ret)
  447. client->b_fail += 1;
  448. guc->submissions[engine_id] += 1;
  449. guc->last_seqno[engine_id] = rq->global_seqno;
  450. spin_unlock(&client->wq_lock);
  451. }
  452. static void i915_guc_submit(struct drm_i915_gem_request *rq)
  453. {
  454. struct intel_engine_cs *engine = rq->engine;
  455. /* We keep the previous context alive until we retire the following
  456. * request. This ensures that any the context object is still pinned
  457. * for any residual writes the HW makes into it on the context switch
  458. * into the next object following the breadcrumb. Otherwise, we may
  459. * retire the context too early.
  460. */
  461. rq->previous_context = engine->last_context;
  462. engine->last_context = rq->ctx;
  463. i915_gem_request_submit(rq);
  464. __i915_guc_submit(rq);
  465. }
  466. /*
  467. * Everything below here is concerned with setup & teardown, and is
  468. * therefore not part of the somewhat time-critical batch-submission
  469. * path of i915_guc_submit() above.
  470. */
  471. /**
  472. * guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
  473. * @guc: the guc
  474. * @size: size of area to allocate (both virtual space and memory)
  475. *
  476. * This is a wrapper to create an object for use with the GuC. In order to
  477. * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
  478. * both some backing storage and a range inside the Global GTT. We must pin
  479. * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
  480. * range is reserved inside GuC.
  481. *
  482. * Return: A i915_vma if successful, otherwise an ERR_PTR.
  483. */
  484. static struct i915_vma *guc_allocate_vma(struct intel_guc *guc, u32 size)
  485. {
  486. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  487. struct drm_i915_gem_object *obj;
  488. struct i915_vma *vma;
  489. int ret;
  490. obj = i915_gem_object_create(dev_priv, size);
  491. if (IS_ERR(obj))
  492. return ERR_CAST(obj);
  493. vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
  494. if (IS_ERR(vma))
  495. goto err;
  496. ret = i915_vma_pin(vma, 0, PAGE_SIZE,
  497. PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  498. if (ret) {
  499. vma = ERR_PTR(ret);
  500. goto err;
  501. }
  502. /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
  503. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  504. return vma;
  505. err:
  506. i915_gem_object_put(obj);
  507. return vma;
  508. }
  509. static void
  510. guc_client_free(struct drm_i915_private *dev_priv,
  511. struct i915_guc_client *client)
  512. {
  513. struct intel_guc *guc = &dev_priv->guc;
  514. if (!client)
  515. return;
  516. /*
  517. * XXX: wait for any outstanding submissions before freeing memory.
  518. * Be sure to drop any locks
  519. */
  520. if (client->vaddr) {
  521. /*
  522. * If we got as far as setting up a doorbell, make sure we
  523. * shut it down before unmapping & deallocating the memory.
  524. */
  525. guc_disable_doorbell(guc, client);
  526. i915_gem_object_unpin_map(client->vma->obj);
  527. }
  528. i915_vma_unpin_and_release(&client->vma);
  529. if (client->ctx_index != GUC_INVALID_CTX_ID) {
  530. guc_ctx_desc_fini(guc, client);
  531. ida_simple_remove(&guc->ctx_ids, client->ctx_index);
  532. }
  533. kfree(client);
  534. }
  535. /* Check that a doorbell register is in the expected state */
  536. static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
  537. {
  538. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  539. i915_reg_t drbreg = GEN8_DRBREGL(db_id);
  540. uint32_t value = I915_READ(drbreg);
  541. bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
  542. bool expected = test_bit(db_id, guc->doorbell_bitmap);
  543. if (enabled == expected)
  544. return true;
  545. DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
  546. db_id, drbreg.reg, value,
  547. expected ? "active" : "inactive");
  548. return false;
  549. }
  550. /*
  551. * Borrow the first client to set up & tear down each unused doorbell
  552. * in turn, to ensure that all doorbell h/w is (re)initialised.
  553. */
  554. static void guc_init_doorbell_hw(struct intel_guc *guc)
  555. {
  556. struct i915_guc_client *client = guc->execbuf_client;
  557. uint16_t db_id;
  558. int i, err;
  559. guc_disable_doorbell(guc, client);
  560. for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
  561. /* Skip if doorbell is OK */
  562. if (guc_doorbell_check(guc, i))
  563. continue;
  564. err = guc_update_doorbell_id(guc, client, i);
  565. if (err)
  566. DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
  567. i, err);
  568. }
  569. db_id = select_doorbell_register(guc, client->priority);
  570. WARN_ON(db_id == GUC_INVALID_DOORBELL_ID);
  571. err = guc_update_doorbell_id(guc, client, db_id);
  572. if (err)
  573. DRM_WARN("Failed to restore doorbell to %d, err %d\n",
  574. db_id, err);
  575. /* Read back & verify all doorbell registers */
  576. for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
  577. (void)guc_doorbell_check(guc, i);
  578. }
  579. /**
  580. * guc_client_alloc() - Allocate an i915_guc_client
  581. * @dev_priv: driver private data structure
  582. * @engines: The set of engines to enable for this client
  583. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  584. * The kernel client to replace ExecList submission is created with
  585. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  586. * while a preemption context can use CRITICAL.
  587. * @ctx: the context that owns the client (we use the default render
  588. * context)
  589. *
  590. * Return: An i915_guc_client object if success, else NULL.
  591. */
  592. static struct i915_guc_client *
  593. guc_client_alloc(struct drm_i915_private *dev_priv,
  594. uint32_t engines,
  595. uint32_t priority,
  596. struct i915_gem_context *ctx)
  597. {
  598. struct i915_guc_client *client;
  599. struct intel_guc *guc = &dev_priv->guc;
  600. struct i915_vma *vma;
  601. void *vaddr;
  602. uint16_t db_id;
  603. client = kzalloc(sizeof(*client), GFP_KERNEL);
  604. if (!client)
  605. return NULL;
  606. client->owner = ctx;
  607. client->guc = guc;
  608. client->engines = engines;
  609. client->priority = priority;
  610. client->doorbell_id = GUC_INVALID_DOORBELL_ID;
  611. client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
  612. GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
  613. if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
  614. client->ctx_index = GUC_INVALID_CTX_ID;
  615. goto err;
  616. }
  617. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  618. vma = guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
  619. if (IS_ERR(vma))
  620. goto err;
  621. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  622. client->vma = vma;
  623. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  624. if (IS_ERR(vaddr))
  625. goto err;
  626. client->vaddr = vaddr;
  627. spin_lock_init(&client->wq_lock);
  628. client->wq_offset = GUC_DB_SIZE;
  629. client->wq_size = GUC_WQ_SIZE;
  630. db_id = select_doorbell_register(guc, client->priority);
  631. if (db_id == GUC_INVALID_DOORBELL_ID)
  632. /* XXX: evict a doorbell instead? */
  633. goto err;
  634. client->doorbell_offset = select_doorbell_cacheline(guc);
  635. /*
  636. * Since the doorbell only requires a single cacheline, we can save
  637. * space by putting the application process descriptor in the same
  638. * page. Use the half of the page that doesn't include the doorbell.
  639. */
  640. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  641. client->proc_desc_offset = 0;
  642. else
  643. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  644. guc_proc_desc_init(guc, client);
  645. guc_ctx_desc_init(guc, client);
  646. /* For runtime client allocation we need to enable the doorbell. Not
  647. * required yet for the static execbuf_client as this special kernel
  648. * client is enabled from i915_guc_submission_enable().
  649. *
  650. * guc_update_doorbell_id(guc, client, db_id);
  651. */
  652. DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
  653. priority, client, client->engines, client->ctx_index);
  654. DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
  655. client->doorbell_id, client->doorbell_offset);
  656. return client;
  657. err:
  658. guc_client_free(dev_priv, client);
  659. return NULL;
  660. }
  661. /*
  662. * Sub buffer switch callback. Called whenever relay has to switch to a new
  663. * sub buffer, relay stays on the same sub buffer if 0 is returned.
  664. */
  665. static int subbuf_start_callback(struct rchan_buf *buf,
  666. void *subbuf,
  667. void *prev_subbuf,
  668. size_t prev_padding)
  669. {
  670. /* Use no-overwrite mode by default, where relay will stop accepting
  671. * new data if there are no empty sub buffers left.
  672. * There is no strict synchronization enforced by relay between Consumer
  673. * and Producer. In overwrite mode, there is a possibility of getting
  674. * inconsistent/garbled data, the producer could be writing on to the
  675. * same sub buffer from which Consumer is reading. This can't be avoided
  676. * unless Consumer is fast enough and can always run in tandem with
  677. * Producer.
  678. */
  679. if (relay_buf_full(buf))
  680. return 0;
  681. return 1;
  682. }
  683. /*
  684. * file_create() callback. Creates relay file in debugfs.
  685. */
  686. static struct dentry *create_buf_file_callback(const char *filename,
  687. struct dentry *parent,
  688. umode_t mode,
  689. struct rchan_buf *buf,
  690. int *is_global)
  691. {
  692. struct dentry *buf_file;
  693. /* This to enable the use of a single buffer for the relay channel and
  694. * correspondingly have a single file exposed to User, through which
  695. * it can collect the logs in order without any post-processing.
  696. * Need to set 'is_global' even if parent is NULL for early logging.
  697. */
  698. *is_global = 1;
  699. if (!parent)
  700. return NULL;
  701. /* Not using the channel filename passed as an argument, since for each
  702. * channel relay appends the corresponding CPU number to the filename
  703. * passed in relay_open(). This should be fine as relay just needs a
  704. * dentry of the file associated with the channel buffer and that file's
  705. * name need not be same as the filename passed as an argument.
  706. */
  707. buf_file = debugfs_create_file("guc_log", mode,
  708. parent, buf, &relay_file_operations);
  709. return buf_file;
  710. }
  711. /*
  712. * file_remove() default callback. Removes relay file in debugfs.
  713. */
  714. static int remove_buf_file_callback(struct dentry *dentry)
  715. {
  716. debugfs_remove(dentry);
  717. return 0;
  718. }
  719. /* relay channel callbacks */
  720. static struct rchan_callbacks relay_callbacks = {
  721. .subbuf_start = subbuf_start_callback,
  722. .create_buf_file = create_buf_file_callback,
  723. .remove_buf_file = remove_buf_file_callback,
  724. };
  725. static void guc_log_remove_relay_file(struct intel_guc *guc)
  726. {
  727. relay_close(guc->log.relay_chan);
  728. }
  729. static int guc_log_create_relay_channel(struct intel_guc *guc)
  730. {
  731. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  732. struct rchan *guc_log_relay_chan;
  733. size_t n_subbufs, subbuf_size;
  734. /* Keep the size of sub buffers same as shared log buffer */
  735. subbuf_size = guc->log.vma->obj->base.size;
  736. /* Store up to 8 snapshots, which is large enough to buffer sufficient
  737. * boot time logs and provides enough leeway to User, in terms of
  738. * latency, for consuming the logs from relay. Also doesn't take
  739. * up too much memory.
  740. */
  741. n_subbufs = 8;
  742. guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
  743. n_subbufs, &relay_callbacks, dev_priv);
  744. if (!guc_log_relay_chan) {
  745. DRM_ERROR("Couldn't create relay chan for GuC logging\n");
  746. return -ENOMEM;
  747. }
  748. GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
  749. guc->log.relay_chan = guc_log_relay_chan;
  750. return 0;
  751. }
  752. static int guc_log_create_relay_file(struct intel_guc *guc)
  753. {
  754. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  755. struct dentry *log_dir;
  756. int ret;
  757. /* For now create the log file in /sys/kernel/debug/dri/0 dir */
  758. log_dir = dev_priv->drm.primary->debugfs_root;
  759. /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
  760. * not mounted and so can't create the relay file.
  761. * The relay API seems to fit well with debugfs only, for availing relay
  762. * there are 3 requirements which can be met for debugfs file only in a
  763. * straightforward/clean manner :-
  764. * i) Need the associated dentry pointer of the file, while opening the
  765. * relay channel.
  766. * ii) Should be able to use 'relay_file_operations' fops for the file.
  767. * iii) Set the 'i_private' field of file's inode to the pointer of
  768. * relay channel buffer.
  769. */
  770. if (!log_dir) {
  771. DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
  772. return -ENODEV;
  773. }
  774. ret = relay_late_setup_files(guc->log.relay_chan, "guc_log", log_dir);
  775. if (ret) {
  776. DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
  777. return ret;
  778. }
  779. return 0;
  780. }
  781. static void guc_move_to_next_buf(struct intel_guc *guc)
  782. {
  783. /* Make sure the updates made in the sub buffer are visible when
  784. * Consumer sees the following update to offset inside the sub buffer.
  785. */
  786. smp_wmb();
  787. /* All data has been written, so now move the offset of sub buffer. */
  788. relay_reserve(guc->log.relay_chan, guc->log.vma->obj->base.size);
  789. /* Switch to the next sub buffer */
  790. relay_flush(guc->log.relay_chan);
  791. }
  792. static void *guc_get_write_buffer(struct intel_guc *guc)
  793. {
  794. if (!guc->log.relay_chan)
  795. return NULL;
  796. /* Just get the base address of a new sub buffer and copy data into it
  797. * ourselves. NULL will be returned in no-overwrite mode, if all sub
  798. * buffers are full. Could have used the relay_write() to indirectly
  799. * copy the data, but that would have been bit convoluted, as we need to
  800. * write to only certain locations inside a sub buffer which cannot be
  801. * done without using relay_reserve() along with relay_write(). So its
  802. * better to use relay_reserve() alone.
  803. */
  804. return relay_reserve(guc->log.relay_chan, 0);
  805. }
  806. static bool
  807. guc_check_log_buf_overflow(struct intel_guc *guc,
  808. enum guc_log_buffer_type type, unsigned int full_cnt)
  809. {
  810. unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
  811. bool overflow = false;
  812. if (full_cnt != prev_full_cnt) {
  813. overflow = true;
  814. guc->log.prev_overflow_count[type] = full_cnt;
  815. guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
  816. if (full_cnt < prev_full_cnt) {
  817. /* buffer_full_cnt is a 4 bit counter */
  818. guc->log.total_overflow_count[type] += 16;
  819. }
  820. DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
  821. }
  822. return overflow;
  823. }
  824. static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
  825. {
  826. switch (type) {
  827. case GUC_ISR_LOG_BUFFER:
  828. return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
  829. case GUC_DPC_LOG_BUFFER:
  830. return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
  831. case GUC_CRASH_DUMP_LOG_BUFFER:
  832. return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
  833. default:
  834. MISSING_CASE(type);
  835. }
  836. return 0;
  837. }
  838. static void guc_read_update_log_buffer(struct intel_guc *guc)
  839. {
  840. unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
  841. struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
  842. struct guc_log_buffer_state log_buf_state_local;
  843. enum guc_log_buffer_type type;
  844. void *src_data, *dst_data;
  845. bool new_overflow;
  846. if (WARN_ON(!guc->log.buf_addr))
  847. return;
  848. /* Get the pointer to shared GuC log buffer */
  849. log_buf_state = src_data = guc->log.buf_addr;
  850. /* Get the pointer to local buffer to store the logs */
  851. log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
  852. /* Actual logs are present from the 2nd page */
  853. src_data += PAGE_SIZE;
  854. dst_data += PAGE_SIZE;
  855. for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
  856. /* Make a copy of the state structure, inside GuC log buffer
  857. * (which is uncached mapped), on the stack to avoid reading
  858. * from it multiple times.
  859. */
  860. memcpy(&log_buf_state_local, log_buf_state,
  861. sizeof(struct guc_log_buffer_state));
  862. buffer_size = guc_get_log_buffer_size(type);
  863. read_offset = log_buf_state_local.read_ptr;
  864. write_offset = log_buf_state_local.sampled_write_ptr;
  865. full_cnt = log_buf_state_local.buffer_full_cnt;
  866. /* Bookkeeping stuff */
  867. guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
  868. new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
  869. /* Update the state of shared log buffer */
  870. log_buf_state->read_ptr = write_offset;
  871. log_buf_state->flush_to_file = 0;
  872. log_buf_state++;
  873. if (unlikely(!log_buf_snapshot_state))
  874. continue;
  875. /* First copy the state structure in snapshot buffer */
  876. memcpy(log_buf_snapshot_state, &log_buf_state_local,
  877. sizeof(struct guc_log_buffer_state));
  878. /* The write pointer could have been updated by GuC firmware,
  879. * after sending the flush interrupt to Host, for consistency
  880. * set write pointer value to same value of sampled_write_ptr
  881. * in the snapshot buffer.
  882. */
  883. log_buf_snapshot_state->write_ptr = write_offset;
  884. log_buf_snapshot_state++;
  885. /* Now copy the actual logs. */
  886. if (unlikely(new_overflow)) {
  887. /* copy the whole buffer in case of overflow */
  888. read_offset = 0;
  889. write_offset = buffer_size;
  890. } else if (unlikely((read_offset > buffer_size) ||
  891. (write_offset > buffer_size))) {
  892. DRM_ERROR("invalid log buffer state\n");
  893. /* copy whole buffer as offsets are unreliable */
  894. read_offset = 0;
  895. write_offset = buffer_size;
  896. }
  897. /* Just copy the newly written data */
  898. if (read_offset > write_offset) {
  899. i915_memcpy_from_wc(dst_data, src_data, write_offset);
  900. bytes_to_copy = buffer_size - read_offset;
  901. } else {
  902. bytes_to_copy = write_offset - read_offset;
  903. }
  904. i915_memcpy_from_wc(dst_data + read_offset,
  905. src_data + read_offset, bytes_to_copy);
  906. src_data += buffer_size;
  907. dst_data += buffer_size;
  908. }
  909. if (log_buf_snapshot_state)
  910. guc_move_to_next_buf(guc);
  911. else {
  912. /* Used rate limited to avoid deluge of messages, logs might be
  913. * getting consumed by User at a slow rate.
  914. */
  915. DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
  916. guc->log.capture_miss_count++;
  917. }
  918. }
  919. static void guc_capture_logs_work(struct work_struct *work)
  920. {
  921. struct drm_i915_private *dev_priv =
  922. container_of(work, struct drm_i915_private, guc.log.flush_work);
  923. i915_guc_capture_logs(dev_priv);
  924. }
  925. static void guc_log_cleanup(struct intel_guc *guc)
  926. {
  927. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  928. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  929. /* First disable the flush interrupt */
  930. gen9_disable_guc_interrupts(dev_priv);
  931. if (guc->log.flush_wq)
  932. destroy_workqueue(guc->log.flush_wq);
  933. guc->log.flush_wq = NULL;
  934. if (guc->log.relay_chan)
  935. guc_log_remove_relay_file(guc);
  936. guc->log.relay_chan = NULL;
  937. if (guc->log.buf_addr)
  938. i915_gem_object_unpin_map(guc->log.vma->obj);
  939. guc->log.buf_addr = NULL;
  940. }
  941. static int guc_log_create_extras(struct intel_guc *guc)
  942. {
  943. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  944. void *vaddr;
  945. int ret;
  946. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  947. /* Nothing to do */
  948. if (i915.guc_log_level < 0)
  949. return 0;
  950. if (!guc->log.buf_addr) {
  951. /* Create a WC (Uncached for read) vmalloc mapping of log
  952. * buffer pages, so that we can directly get the data
  953. * (up-to-date) from memory.
  954. */
  955. vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
  956. if (IS_ERR(vaddr)) {
  957. ret = PTR_ERR(vaddr);
  958. DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
  959. return ret;
  960. }
  961. guc->log.buf_addr = vaddr;
  962. }
  963. if (!guc->log.relay_chan) {
  964. /* Create a relay channel, so that we have buffers for storing
  965. * the GuC firmware logs, the channel will be linked with a file
  966. * later on when debugfs is registered.
  967. */
  968. ret = guc_log_create_relay_channel(guc);
  969. if (ret)
  970. return ret;
  971. }
  972. if (!guc->log.flush_wq) {
  973. INIT_WORK(&guc->log.flush_work, guc_capture_logs_work);
  974. /*
  975. * GuC log buffer flush work item has to do register access to
  976. * send the ack to GuC and this work item, if not synced before
  977. * suspend, can potentially get executed after the GFX device is
  978. * suspended.
  979. * By marking the WQ as freezable, we don't have to bother about
  980. * flushing of this work item from the suspend hooks, the pending
  981. * work item if any will be either executed before the suspend
  982. * or scheduled later on resume. This way the handling of work
  983. * item can be kept same between system suspend & rpm suspend.
  984. */
  985. guc->log.flush_wq = alloc_ordered_workqueue("i915-guc_log",
  986. WQ_HIGHPRI | WQ_FREEZABLE);
  987. if (guc->log.flush_wq == NULL) {
  988. DRM_ERROR("Couldn't allocate the wq for GuC logging\n");
  989. return -ENOMEM;
  990. }
  991. }
  992. return 0;
  993. }
  994. static void guc_log_create(struct intel_guc *guc)
  995. {
  996. struct i915_vma *vma;
  997. unsigned long offset;
  998. uint32_t size, flags;
  999. if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
  1000. i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
  1001. /* The first page is to save log buffer state. Allocate one
  1002. * extra page for others in case for overlap */
  1003. size = (1 + GUC_LOG_DPC_PAGES + 1 +
  1004. GUC_LOG_ISR_PAGES + 1 +
  1005. GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
  1006. vma = guc->log.vma;
  1007. if (!vma) {
  1008. /* We require SSE 4.1 for fast reads from the GuC log buffer and
  1009. * it should be present on the chipsets supporting GuC based
  1010. * submisssions.
  1011. */
  1012. if (WARN_ON(!i915_memcpy_from_wc(NULL, NULL, 0))) {
  1013. /* logging will not be enabled */
  1014. i915.guc_log_level = -1;
  1015. return;
  1016. }
  1017. vma = guc_allocate_vma(guc, size);
  1018. if (IS_ERR(vma)) {
  1019. /* logging will be off */
  1020. i915.guc_log_level = -1;
  1021. return;
  1022. }
  1023. guc->log.vma = vma;
  1024. if (guc_log_create_extras(guc)) {
  1025. guc_log_cleanup(guc);
  1026. i915_vma_unpin_and_release(&guc->log.vma);
  1027. i915.guc_log_level = -1;
  1028. return;
  1029. }
  1030. }
  1031. /* each allocated unit is a page */
  1032. flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
  1033. (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
  1034. (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
  1035. (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
  1036. offset = i915_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
  1037. guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
  1038. }
  1039. static int guc_log_late_setup(struct intel_guc *guc)
  1040. {
  1041. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1042. int ret;
  1043. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  1044. if (i915.guc_log_level < 0)
  1045. return -EINVAL;
  1046. /* If log_level was set as -1 at boot time, then setup needed to
  1047. * handle log buffer flush interrupts would not have been done yet,
  1048. * so do that now.
  1049. */
  1050. ret = guc_log_create_extras(guc);
  1051. if (ret)
  1052. goto err;
  1053. ret = guc_log_create_relay_file(guc);
  1054. if (ret)
  1055. goto err;
  1056. return 0;
  1057. err:
  1058. guc_log_cleanup(guc);
  1059. /* logging will remain off */
  1060. i915.guc_log_level = -1;
  1061. return ret;
  1062. }
  1063. static void guc_policies_init(struct guc_policies *policies)
  1064. {
  1065. struct guc_policy *policy;
  1066. u32 p, i;
  1067. policies->dpc_promote_time = 500000;
  1068. policies->max_num_work_items = POLICY_MAX_NUM_WI;
  1069. for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
  1070. for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
  1071. policy = &policies->policy[p][i];
  1072. policy->execution_quantum = 1000000;
  1073. policy->preemption_time = 500000;
  1074. policy->fault_time = 250000;
  1075. policy->policy_flags = 0;
  1076. }
  1077. }
  1078. policies->is_valid = 1;
  1079. }
  1080. static void guc_addon_create(struct intel_guc *guc)
  1081. {
  1082. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  1083. struct i915_vma *vma;
  1084. struct guc_ads *ads;
  1085. struct guc_policies *policies;
  1086. struct guc_mmio_reg_state *reg_state;
  1087. struct intel_engine_cs *engine;
  1088. enum intel_engine_id id;
  1089. struct page *page;
  1090. u32 size;
  1091. /* The ads obj includes the struct itself and buffers passed to GuC */
  1092. size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
  1093. sizeof(struct guc_mmio_reg_state) +
  1094. GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
  1095. vma = guc->ads_vma;
  1096. if (!vma) {
  1097. vma = guc_allocate_vma(guc, PAGE_ALIGN(size));
  1098. if (IS_ERR(vma))
  1099. return;
  1100. guc->ads_vma = vma;
  1101. }
  1102. page = i915_vma_first_page(vma);
  1103. ads = kmap(page);
  1104. /*
  1105. * The GuC requires a "Golden Context" when it reinitialises
  1106. * engines after a reset. Here we use the Render ring default
  1107. * context, which must already exist and be pinned in the GGTT,
  1108. * so its address won't change after we've told the GuC where
  1109. * to find it.
  1110. */
  1111. engine = dev_priv->engine[RCS];
  1112. ads->golden_context_lrca = engine->status_page.ggtt_offset;
  1113. for_each_engine(engine, dev_priv, id)
  1114. ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
  1115. /* GuC scheduling policies */
  1116. policies = (void *)ads + sizeof(struct guc_ads);
  1117. guc_policies_init(policies);
  1118. ads->scheduler_policies =
  1119. i915_ggtt_offset(vma) + sizeof(struct guc_ads);
  1120. /* MMIO reg state */
  1121. reg_state = (void *)policies + sizeof(struct guc_policies);
  1122. for_each_engine(engine, dev_priv, id) {
  1123. reg_state->mmio_white_list[engine->guc_id].mmio_start =
  1124. engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
  1125. /* Nothing to be saved or restored for now. */
  1126. reg_state->mmio_white_list[engine->guc_id].count = 0;
  1127. }
  1128. ads->reg_state_addr = ads->scheduler_policies +
  1129. sizeof(struct guc_policies);
  1130. ads->reg_state_buffer = ads->reg_state_addr +
  1131. sizeof(struct guc_mmio_reg_state);
  1132. kunmap(page);
  1133. }
  1134. /*
  1135. * Set up the memory resources to be shared with the GuC. At this point,
  1136. * we require just one object that can be mapped through the GGTT.
  1137. */
  1138. int i915_guc_submission_init(struct drm_i915_private *dev_priv)
  1139. {
  1140. const size_t ctxsize = sizeof(struct guc_context_desc);
  1141. const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
  1142. const size_t gemsize = round_up(poolsize, PAGE_SIZE);
  1143. struct intel_guc *guc = &dev_priv->guc;
  1144. struct i915_vma *vma;
  1145. if (!HAS_GUC_SCHED(dev_priv))
  1146. return 0;
  1147. /* Wipe bitmap & delete client in case of reinitialisation */
  1148. bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
  1149. i915_guc_submission_disable(dev_priv);
  1150. if (!i915.enable_guc_submission)
  1151. return 0; /* not enabled */
  1152. if (guc->ctx_pool_vma)
  1153. return 0; /* already allocated */
  1154. vma = guc_allocate_vma(guc, gemsize);
  1155. if (IS_ERR(vma))
  1156. return PTR_ERR(vma);
  1157. guc->ctx_pool_vma = vma;
  1158. ida_init(&guc->ctx_ids);
  1159. guc_log_create(guc);
  1160. guc_addon_create(guc);
  1161. guc->execbuf_client = guc_client_alloc(dev_priv,
  1162. INTEL_INFO(dev_priv)->ring_mask,
  1163. GUC_CTX_PRIORITY_KMD_NORMAL,
  1164. dev_priv->kernel_context);
  1165. if (!guc->execbuf_client) {
  1166. DRM_ERROR("Failed to create GuC client for execbuf!\n");
  1167. goto err;
  1168. }
  1169. return 0;
  1170. err:
  1171. i915_guc_submission_fini(dev_priv);
  1172. return -ENOMEM;
  1173. }
  1174. static void guc_reset_wq(struct i915_guc_client *gc)
  1175. {
  1176. struct guc_process_desc *desc = gc->vaddr + gc->proc_desc_offset;
  1177. desc->head = 0;
  1178. desc->tail = 0;
  1179. gc->wq_tail = 0;
  1180. }
  1181. int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
  1182. {
  1183. struct intel_guc *guc = &dev_priv->guc;
  1184. struct i915_guc_client *client = guc->execbuf_client;
  1185. struct intel_engine_cs *engine;
  1186. enum intel_engine_id id;
  1187. if (!client)
  1188. return -ENODEV;
  1189. intel_guc_sample_forcewake(guc);
  1190. guc_reset_wq(client);
  1191. guc_init_doorbell_hw(guc);
  1192. /* Take over from manual control of ELSP (execlists) */
  1193. for_each_engine(engine, dev_priv, id) {
  1194. struct drm_i915_gem_request *rq;
  1195. engine->submit_request = i915_guc_submit;
  1196. engine->schedule = NULL;
  1197. /* Replay the current set of previously submitted requests */
  1198. list_for_each_entry(rq, &engine->timeline->requests, link) {
  1199. client->wq_rsvd += sizeof(struct guc_wq_item);
  1200. __i915_guc_submit(rq);
  1201. }
  1202. }
  1203. return 0;
  1204. }
  1205. void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
  1206. {
  1207. struct intel_guc *guc = &dev_priv->guc;
  1208. if (!guc->execbuf_client)
  1209. return;
  1210. /* Revert back to manual ELSP submission */
  1211. intel_execlists_enable_submission(dev_priv);
  1212. }
  1213. void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
  1214. {
  1215. struct intel_guc *guc = &dev_priv->guc;
  1216. struct i915_guc_client *client;
  1217. client = fetch_and_zero(&guc->execbuf_client);
  1218. if (!client)
  1219. return;
  1220. guc_client_free(dev_priv, client);
  1221. i915_vma_unpin_and_release(&guc->ads_vma);
  1222. i915_vma_unpin_and_release(&guc->log.vma);
  1223. if (guc->ctx_pool_vma)
  1224. ida_destroy(&guc->ctx_ids);
  1225. i915_vma_unpin_and_release(&guc->ctx_pool_vma);
  1226. }
  1227. /**
  1228. * intel_guc_suspend() - notify GuC entering suspend state
  1229. * @dev_priv: i915 device private
  1230. */
  1231. int intel_guc_suspend(struct drm_i915_private *dev_priv)
  1232. {
  1233. struct intel_guc *guc = &dev_priv->guc;
  1234. struct i915_gem_context *ctx;
  1235. u32 data[3];
  1236. if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
  1237. return 0;
  1238. gen9_disable_guc_interrupts(dev_priv);
  1239. ctx = dev_priv->kernel_context;
  1240. data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
  1241. /* any value greater than GUC_POWER_D0 */
  1242. data[1] = GUC_POWER_D1;
  1243. /* first page is shared data with GuC */
  1244. data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
  1245. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  1246. }
  1247. /**
  1248. * intel_guc_resume() - notify GuC resuming from suspend state
  1249. * @dev_priv: i915 device private
  1250. */
  1251. int intel_guc_resume(struct drm_i915_private *dev_priv)
  1252. {
  1253. struct intel_guc *guc = &dev_priv->guc;
  1254. struct i915_gem_context *ctx;
  1255. u32 data[3];
  1256. if (guc->guc_fw.guc_fw_load_status != GUC_FIRMWARE_SUCCESS)
  1257. return 0;
  1258. if (i915.guc_log_level >= 0)
  1259. gen9_enable_guc_interrupts(dev_priv);
  1260. ctx = dev_priv->kernel_context;
  1261. data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
  1262. data[1] = GUC_POWER_D0;
  1263. /* first page is shared data with GuC */
  1264. data[2] = i915_ggtt_offset(ctx->engine[RCS].state);
  1265. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  1266. }
  1267. void i915_guc_capture_logs(struct drm_i915_private *dev_priv)
  1268. {
  1269. guc_read_update_log_buffer(&dev_priv->guc);
  1270. /* Generally device is expected to be active only at this
  1271. * time, so get/put should be really quick.
  1272. */
  1273. intel_runtime_pm_get(dev_priv);
  1274. intel_guc_log_flush_complete(&dev_priv->guc);
  1275. intel_runtime_pm_put(dev_priv);
  1276. }
  1277. void i915_guc_flush_logs(struct drm_i915_private *dev_priv)
  1278. {
  1279. if (!i915.enable_guc_submission || (i915.guc_log_level < 0))
  1280. return;
  1281. /* First disable the interrupts, will be renabled afterwards */
  1282. gen9_disable_guc_interrupts(dev_priv);
  1283. /* Before initiating the forceful flush, wait for any pending/ongoing
  1284. * flush to complete otherwise forceful flush may not actually happen.
  1285. */
  1286. flush_work(&dev_priv->guc.log.flush_work);
  1287. /* Ask GuC to update the log buffer state */
  1288. intel_guc_log_flush(&dev_priv->guc);
  1289. /* GuC would have updated log buffer by now, so capture it */
  1290. i915_guc_capture_logs(dev_priv);
  1291. }
  1292. void i915_guc_unregister(struct drm_i915_private *dev_priv)
  1293. {
  1294. if (!i915.enable_guc_submission)
  1295. return;
  1296. mutex_lock(&dev_priv->drm.struct_mutex);
  1297. guc_log_cleanup(&dev_priv->guc);
  1298. mutex_unlock(&dev_priv->drm.struct_mutex);
  1299. }
  1300. void i915_guc_register(struct drm_i915_private *dev_priv)
  1301. {
  1302. if (!i915.enable_guc_submission)
  1303. return;
  1304. mutex_lock(&dev_priv->drm.struct_mutex);
  1305. guc_log_late_setup(&dev_priv->guc);
  1306. mutex_unlock(&dev_priv->drm.struct_mutex);
  1307. }
  1308. int i915_guc_log_control(struct drm_i915_private *dev_priv, u64 control_val)
  1309. {
  1310. union guc_log_control log_param;
  1311. int ret;
  1312. log_param.value = control_val;
  1313. if (log_param.verbosity < GUC_LOG_VERBOSITY_MIN ||
  1314. log_param.verbosity > GUC_LOG_VERBOSITY_MAX)
  1315. return -EINVAL;
  1316. /* This combination doesn't make sense & won't have any effect */
  1317. if (!log_param.logging_enabled && (i915.guc_log_level < 0))
  1318. return 0;
  1319. ret = intel_guc_log_control(&dev_priv->guc, log_param.value);
  1320. if (ret < 0) {
  1321. DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
  1322. return ret;
  1323. }
  1324. i915.guc_log_level = log_param.verbosity;
  1325. /* If log_level was set as -1 at boot time, then the relay channel file
  1326. * wouldn't have been created by now and interrupts also would not have
  1327. * been enabled.
  1328. */
  1329. if (!dev_priv->guc.log.relay_chan) {
  1330. ret = guc_log_late_setup(&dev_priv->guc);
  1331. if (!ret)
  1332. gen9_enable_guc_interrupts(dev_priv);
  1333. } else if (!log_param.logging_enabled) {
  1334. /* Once logging is disabled, GuC won't generate logs & send an
  1335. * interrupt. But there could be some data in the log buffer
  1336. * which is yet to be captured. So request GuC to update the log
  1337. * buffer state and then collect the left over logs.
  1338. */
  1339. i915_guc_flush_logs(dev_priv);
  1340. /* As logging is disabled, update log level to reflect that */
  1341. i915.guc_log_level = -1;
  1342. } else {
  1343. /* In case interrupts were disabled, enable them now */
  1344. gen9_enable_guc_interrupts(dev_priv);
  1345. }
  1346. return ret;
  1347. }