i915_guc_submission.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/circ_buf.h>
  25. #include "i915_drv.h"
  26. #include "intel_uc.h"
  27. /**
  28. * DOC: GuC-based command submission
  29. *
  30. * i915_guc_client:
  31. * We use the term client to avoid confusion with contexts. A i915_guc_client is
  32. * equivalent to GuC object guc_context_desc. This context descriptor is
  33. * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
  34. * and workqueue for it. Also the process descriptor (guc_process_desc), which
  35. * is mapped to client space. So the client can write Work Item then ring the
  36. * doorbell.
  37. *
  38. * To simplify the implementation, we allocate one gem object that contains all
  39. * pages for doorbell, process descriptor and workqueue.
  40. *
  41. * The Scratch registers:
  42. * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
  43. * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
  44. * triggers an interrupt on the GuC via another register write (0xC4C8).
  45. * Firmware writes a success/fail code back to the action register after
  46. * processes the request. The kernel driver polls waiting for this update and
  47. * then proceeds.
  48. * See intel_guc_send()
  49. *
  50. * Doorbells:
  51. * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
  52. * mapped into process space.
  53. *
  54. * Work Items:
  55. * There are several types of work items that the host may place into a
  56. * workqueue, each with its own requirements and limitations. Currently only
  57. * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
  58. * represents in-order queue. The kernel driver packs ring tail pointer and an
  59. * ELSP context descriptor dword into Work Item.
  60. * See guc_wq_item_append()
  61. *
  62. */
  63. /*
  64. * Tell the GuC to allocate or deallocate a specific doorbell
  65. */
  66. static int guc_allocate_doorbell(struct intel_guc *guc,
  67. struct i915_guc_client *client)
  68. {
  69. u32 action[] = {
  70. INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
  71. client->ctx_index
  72. };
  73. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  74. }
  75. static int guc_release_doorbell(struct intel_guc *guc,
  76. struct i915_guc_client *client)
  77. {
  78. u32 action[] = {
  79. INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
  80. client->ctx_index
  81. };
  82. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  83. }
  84. /*
  85. * Initialise, update, or clear doorbell data shared with the GuC
  86. *
  87. * These functions modify shared data and so need access to the mapped
  88. * client object which contains the page being used for the doorbell
  89. */
  90. static int guc_update_doorbell_id(struct intel_guc *guc,
  91. struct i915_guc_client *client,
  92. u16 new_id)
  93. {
  94. struct sg_table *sg = guc->ctx_pool_vma->pages;
  95. void *doorbell_bitmap = guc->doorbell_bitmap;
  96. struct guc_doorbell_info *doorbell;
  97. struct guc_context_desc desc;
  98. size_t len;
  99. doorbell = client->vaddr + client->doorbell_offset;
  100. if (client->doorbell_id != GUC_INVALID_DOORBELL_ID &&
  101. test_bit(client->doorbell_id, doorbell_bitmap)) {
  102. /* Deactivate the old doorbell */
  103. doorbell->db_status = GUC_DOORBELL_DISABLED;
  104. (void)guc_release_doorbell(guc, client);
  105. __clear_bit(client->doorbell_id, doorbell_bitmap);
  106. }
  107. /* Update the GuC's idea of the doorbell ID */
  108. len = sg_pcopy_to_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  109. sizeof(desc) * client->ctx_index);
  110. if (len != sizeof(desc))
  111. return -EFAULT;
  112. desc.db_id = new_id;
  113. len = sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  114. sizeof(desc) * client->ctx_index);
  115. if (len != sizeof(desc))
  116. return -EFAULT;
  117. client->doorbell_id = new_id;
  118. if (new_id == GUC_INVALID_DOORBELL_ID)
  119. return 0;
  120. /* Activate the new doorbell */
  121. __set_bit(new_id, doorbell_bitmap);
  122. doorbell->db_status = GUC_DOORBELL_ENABLED;
  123. doorbell->cookie = client->doorbell_cookie;
  124. return guc_allocate_doorbell(guc, client);
  125. }
  126. static void guc_disable_doorbell(struct intel_guc *guc,
  127. struct i915_guc_client *client)
  128. {
  129. (void)guc_update_doorbell_id(guc, client, GUC_INVALID_DOORBELL_ID);
  130. /* XXX: wait for any interrupts */
  131. /* XXX: wait for workqueue to drain */
  132. }
  133. static uint16_t
  134. select_doorbell_register(struct intel_guc *guc, uint32_t priority)
  135. {
  136. /*
  137. * The bitmap tracks which doorbell registers are currently in use.
  138. * It is split into two halves; the first half is used for normal
  139. * priority contexts, the second half for high-priority ones.
  140. * Note that logically higher priorities are numerically less than
  141. * normal ones, so the test below means "is it high-priority?"
  142. */
  143. const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
  144. const uint16_t half = GUC_MAX_DOORBELLS / 2;
  145. const uint16_t start = hi_pri ? half : 0;
  146. const uint16_t end = start + half;
  147. uint16_t id;
  148. id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
  149. if (id == end)
  150. id = GUC_INVALID_DOORBELL_ID;
  151. DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
  152. hi_pri ? "high" : "normal", id);
  153. return id;
  154. }
  155. /*
  156. * Select, assign and relase doorbell cachelines
  157. *
  158. * These functions track which doorbell cachelines are in use.
  159. * The data they manipulate is protected by the intel_guc_send lock.
  160. */
  161. static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
  162. {
  163. const uint32_t cacheline_size = cache_line_size();
  164. uint32_t offset;
  165. /* Doorbell uses a single cache line within a page */
  166. offset = offset_in_page(guc->db_cacheline);
  167. /* Moving to next cache line to reduce contention */
  168. guc->db_cacheline += cacheline_size;
  169. DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
  170. offset, guc->db_cacheline, cacheline_size);
  171. return offset;
  172. }
  173. /*
  174. * Initialise the process descriptor shared with the GuC firmware.
  175. */
  176. static void guc_proc_desc_init(struct intel_guc *guc,
  177. struct i915_guc_client *client)
  178. {
  179. struct guc_process_desc *desc;
  180. desc = client->vaddr + client->proc_desc_offset;
  181. memset(desc, 0, sizeof(*desc));
  182. /*
  183. * XXX: pDoorbell and WQVBaseAddress are pointers in process address
  184. * space for ring3 clients (set them as in mmap_ioctl) or kernel
  185. * space for kernel clients (map on demand instead? May make debug
  186. * easier to have it mapped).
  187. */
  188. desc->wq_base_addr = 0;
  189. desc->db_base_addr = 0;
  190. desc->context_id = client->ctx_index;
  191. desc->wq_size_bytes = client->wq_size;
  192. desc->wq_status = WQ_STATUS_ACTIVE;
  193. desc->priority = client->priority;
  194. }
  195. /*
  196. * Initialise/clear the context descriptor shared with the GuC firmware.
  197. *
  198. * This descriptor tells the GuC where (in GGTT space) to find the important
  199. * data structures relating to this client (doorbell, process descriptor,
  200. * write queue, etc).
  201. */
  202. static void guc_ctx_desc_init(struct intel_guc *guc,
  203. struct i915_guc_client *client)
  204. {
  205. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  206. struct intel_engine_cs *engine;
  207. struct i915_gem_context *ctx = client->owner;
  208. struct guc_context_desc desc;
  209. struct sg_table *sg;
  210. unsigned int tmp;
  211. u32 gfx_addr;
  212. memset(&desc, 0, sizeof(desc));
  213. desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
  214. desc.context_id = client->ctx_index;
  215. desc.priority = client->priority;
  216. desc.db_id = client->doorbell_id;
  217. for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
  218. struct intel_context *ce = &ctx->engine[engine->id];
  219. uint32_t guc_engine_id = engine->guc_id;
  220. struct guc_execlist_context *lrc = &desc.lrc[guc_engine_id];
  221. /* TODO: We have a design issue to be solved here. Only when we
  222. * receive the first batch, we know which engine is used by the
  223. * user. But here GuC expects the lrc and ring to be pinned. It
  224. * is not an issue for default context, which is the only one
  225. * for now who owns a GuC client. But for future owner of GuC
  226. * client, need to make sure lrc is pinned prior to enter here.
  227. */
  228. if (!ce->state)
  229. break; /* XXX: continue? */
  230. lrc->context_desc = lower_32_bits(ce->lrc_desc);
  231. /* The state page is after PPHWSP */
  232. lrc->ring_lcra =
  233. guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
  234. lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
  235. (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
  236. lrc->ring_begin = guc_ggtt_offset(ce->ring->vma);
  237. lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
  238. lrc->ring_next_free_location = lrc->ring_begin;
  239. lrc->ring_current_tail_pointer_value = 0;
  240. desc.engines_used |= (1 << guc_engine_id);
  241. }
  242. DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
  243. client->engines, desc.engines_used);
  244. WARN_ON(desc.engines_used == 0);
  245. /*
  246. * The doorbell, process descriptor, and workqueue are all parts
  247. * of the client object, which the GuC will reference via the GGTT
  248. */
  249. gfx_addr = guc_ggtt_offset(client->vma);
  250. desc.db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
  251. client->doorbell_offset;
  252. desc.db_trigger_cpu =
  253. (uintptr_t)client->vaddr + client->doorbell_offset;
  254. desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
  255. desc.process_desc = gfx_addr + client->proc_desc_offset;
  256. desc.wq_addr = gfx_addr + client->wq_offset;
  257. desc.wq_size = client->wq_size;
  258. /*
  259. * XXX: Take LRCs from an existing context if this is not an
  260. * IsKMDCreatedContext client
  261. */
  262. desc.desc_private = (uintptr_t)client;
  263. /* Pool context is pinned already */
  264. sg = guc->ctx_pool_vma->pages;
  265. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  266. sizeof(desc) * client->ctx_index);
  267. }
  268. static void guc_ctx_desc_fini(struct intel_guc *guc,
  269. struct i915_guc_client *client)
  270. {
  271. struct guc_context_desc desc;
  272. struct sg_table *sg;
  273. memset(&desc, 0, sizeof(desc));
  274. sg = guc->ctx_pool_vma->pages;
  275. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  276. sizeof(desc) * client->ctx_index);
  277. }
  278. /**
  279. * i915_guc_wq_reserve() - reserve space in the GuC's workqueue
  280. * @request: request associated with the commands
  281. *
  282. * Return: 0 if space is available
  283. * -EAGAIN if space is not currently available
  284. *
  285. * This function must be called (and must return 0) before a request
  286. * is submitted to the GuC via i915_guc_submit() below. Once a result
  287. * of 0 has been returned, it must be balanced by a corresponding
  288. * call to submit().
  289. *
  290. * Reservation allows the caller to determine in advance that space
  291. * will be available for the next submission before committing resources
  292. * to it, and helps avoid late failures with complicated recovery paths.
  293. */
  294. int i915_guc_wq_reserve(struct drm_i915_gem_request *request)
  295. {
  296. const size_t wqi_size = sizeof(struct guc_wq_item);
  297. struct i915_guc_client *client = request->i915->guc.execbuf_client;
  298. struct guc_process_desc *desc = client->vaddr +
  299. client->proc_desc_offset;
  300. u32 freespace;
  301. int ret;
  302. spin_lock(&client->wq_lock);
  303. freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
  304. freespace -= client->wq_rsvd;
  305. if (likely(freespace >= wqi_size)) {
  306. client->wq_rsvd += wqi_size;
  307. ret = 0;
  308. } else {
  309. client->no_wq_space++;
  310. ret = -EAGAIN;
  311. }
  312. spin_unlock(&client->wq_lock);
  313. return ret;
  314. }
  315. void i915_guc_wq_unreserve(struct drm_i915_gem_request *request)
  316. {
  317. const size_t wqi_size = sizeof(struct guc_wq_item);
  318. struct i915_guc_client *client = request->i915->guc.execbuf_client;
  319. GEM_BUG_ON(READ_ONCE(client->wq_rsvd) < wqi_size);
  320. spin_lock(&client->wq_lock);
  321. client->wq_rsvd -= wqi_size;
  322. spin_unlock(&client->wq_lock);
  323. }
  324. /* Construct a Work Item and append it to the GuC's Work Queue */
  325. static void guc_wq_item_append(struct i915_guc_client *client,
  326. struct drm_i915_gem_request *rq)
  327. {
  328. /* wqi_len is in DWords, and does not include the one-word header */
  329. const size_t wqi_size = sizeof(struct guc_wq_item);
  330. const u32 wqi_len = wqi_size/sizeof(u32) - 1;
  331. struct intel_engine_cs *engine = rq->engine;
  332. struct guc_process_desc *desc;
  333. struct guc_wq_item *wqi;
  334. u32 freespace, tail, wq_off;
  335. desc = client->vaddr + client->proc_desc_offset;
  336. /* Free space is guaranteed, see i915_guc_wq_reserve() above */
  337. freespace = CIRC_SPACE(client->wq_tail, desc->head, client->wq_size);
  338. GEM_BUG_ON(freespace < wqi_size);
  339. /* The GuC firmware wants the tail index in QWords, not bytes */
  340. tail = rq->tail;
  341. GEM_BUG_ON(tail & 7);
  342. tail >>= 3;
  343. GEM_BUG_ON(tail > WQ_RING_TAIL_MAX);
  344. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  345. * should not have the case where structure wqi is across page, neither
  346. * wrapped to the beginning. This simplifies the implementation below.
  347. *
  348. * XXX: if not the case, we need save data to a temp wqi and copy it to
  349. * workqueue buffer dw by dw.
  350. */
  351. BUILD_BUG_ON(wqi_size != 16);
  352. GEM_BUG_ON(client->wq_rsvd < wqi_size);
  353. /* postincrement WQ tail for next time */
  354. wq_off = client->wq_tail;
  355. GEM_BUG_ON(wq_off & (wqi_size - 1));
  356. client->wq_tail += wqi_size;
  357. client->wq_tail &= client->wq_size - 1;
  358. client->wq_rsvd -= wqi_size;
  359. /* WQ starts from the page after doorbell / process_desc */
  360. wqi = client->vaddr + wq_off + GUC_DB_SIZE;
  361. /* Now fill in the 4-word work queue item */
  362. wqi->header = WQ_TYPE_INORDER |
  363. (wqi_len << WQ_LEN_SHIFT) |
  364. (engine->guc_id << WQ_TARGET_SHIFT) |
  365. WQ_NO_WCFLUSH_WAIT;
  366. /* The GuC wants only the low-order word of the context descriptor */
  367. wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, engine);
  368. wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
  369. wqi->fence_id = rq->global_seqno;
  370. }
  371. static int guc_ring_doorbell(struct i915_guc_client *client)
  372. {
  373. struct guc_process_desc *desc;
  374. union guc_doorbell_qw db_cmp, db_exc, db_ret;
  375. union guc_doorbell_qw *db;
  376. int attempt = 2, ret = -EAGAIN;
  377. desc = client->vaddr + client->proc_desc_offset;
  378. /* Update the tail so it is visible to GuC */
  379. desc->tail = client->wq_tail;
  380. /* current cookie */
  381. db_cmp.db_status = GUC_DOORBELL_ENABLED;
  382. db_cmp.cookie = client->doorbell_cookie;
  383. /* cookie to be updated */
  384. db_exc.db_status = GUC_DOORBELL_ENABLED;
  385. db_exc.cookie = client->doorbell_cookie + 1;
  386. if (db_exc.cookie == 0)
  387. db_exc.cookie = 1;
  388. /* pointer of current doorbell cacheline */
  389. db = client->vaddr + client->doorbell_offset;
  390. while (attempt--) {
  391. /* lets ring the doorbell */
  392. db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
  393. db_cmp.value_qw, db_exc.value_qw);
  394. /* if the exchange was successfully executed */
  395. if (db_ret.value_qw == db_cmp.value_qw) {
  396. /* db was successfully rung */
  397. client->doorbell_cookie = db_exc.cookie;
  398. ret = 0;
  399. break;
  400. }
  401. /* XXX: doorbell was lost and need to acquire it again */
  402. if (db_ret.db_status == GUC_DOORBELL_DISABLED)
  403. break;
  404. DRM_WARN("Cookie mismatch. Expected %d, found %d\n",
  405. db_cmp.cookie, db_ret.cookie);
  406. /* update the cookie to newly read cookie from GuC */
  407. db_cmp.cookie = db_ret.cookie;
  408. db_exc.cookie = db_ret.cookie + 1;
  409. if (db_exc.cookie == 0)
  410. db_exc.cookie = 1;
  411. }
  412. return ret;
  413. }
  414. /**
  415. * __i915_guc_submit() - Submit commands through GuC
  416. * @rq: request associated with the commands
  417. *
  418. * The caller must have already called i915_guc_wq_reserve() above with
  419. * a result of 0 (success), guaranteeing that there is space in the work
  420. * queue for the new request, so enqueuing the item cannot fail.
  421. *
  422. * Bad Things Will Happen if the caller violates this protocol e.g. calls
  423. * submit() when _reserve() says there's no space, or calls _submit()
  424. * a different number of times from (successful) calls to _reserve().
  425. *
  426. * The only error here arises if the doorbell hardware isn't functioning
  427. * as expected, which really shouln't happen.
  428. */
  429. static void __i915_guc_submit(struct drm_i915_gem_request *rq)
  430. {
  431. struct drm_i915_private *dev_priv = rq->i915;
  432. struct intel_engine_cs *engine = rq->engine;
  433. unsigned int engine_id = engine->id;
  434. struct intel_guc *guc = &rq->i915->guc;
  435. struct i915_guc_client *client = guc->execbuf_client;
  436. int b_ret;
  437. spin_lock(&client->wq_lock);
  438. guc_wq_item_append(client, rq);
  439. /* WA to flush out the pending GMADR writes to ring buffer. */
  440. if (i915_vma_is_map_and_fenceable(rq->ring->vma))
  441. POSTING_READ_FW(GUC_STATUS);
  442. b_ret = guc_ring_doorbell(client);
  443. client->submissions[engine_id] += 1;
  444. client->retcode = b_ret;
  445. if (b_ret)
  446. client->b_fail += 1;
  447. guc->submissions[engine_id] += 1;
  448. guc->last_seqno[engine_id] = rq->global_seqno;
  449. spin_unlock(&client->wq_lock);
  450. }
  451. static void i915_guc_submit(struct drm_i915_gem_request *rq)
  452. {
  453. i915_gem_request_submit(rq);
  454. __i915_guc_submit(rq);
  455. }
  456. /*
  457. * Everything below here is concerned with setup & teardown, and is
  458. * therefore not part of the somewhat time-critical batch-submission
  459. * path of i915_guc_submit() above.
  460. */
  461. /**
  462. * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
  463. * @guc: the guc
  464. * @size: size of area to allocate (both virtual space and memory)
  465. *
  466. * This is a wrapper to create an object for use with the GuC. In order to
  467. * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
  468. * both some backing storage and a range inside the Global GTT. We must pin
  469. * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
  470. * range is reserved inside GuC.
  471. *
  472. * Return: A i915_vma if successful, otherwise an ERR_PTR.
  473. */
  474. struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
  475. {
  476. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  477. struct drm_i915_gem_object *obj;
  478. struct i915_vma *vma;
  479. int ret;
  480. obj = i915_gem_object_create(dev_priv, size);
  481. if (IS_ERR(obj))
  482. return ERR_CAST(obj);
  483. vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
  484. if (IS_ERR(vma))
  485. goto err;
  486. ret = i915_vma_pin(vma, 0, PAGE_SIZE,
  487. PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  488. if (ret) {
  489. vma = ERR_PTR(ret);
  490. goto err;
  491. }
  492. return vma;
  493. err:
  494. i915_gem_object_put(obj);
  495. return vma;
  496. }
  497. static void
  498. guc_client_free(struct drm_i915_private *dev_priv,
  499. struct i915_guc_client *client)
  500. {
  501. struct intel_guc *guc = &dev_priv->guc;
  502. if (!client)
  503. return;
  504. /*
  505. * XXX: wait for any outstanding submissions before freeing memory.
  506. * Be sure to drop any locks
  507. */
  508. if (client->vaddr) {
  509. /*
  510. * If we got as far as setting up a doorbell, make sure we
  511. * shut it down before unmapping & deallocating the memory.
  512. */
  513. guc_disable_doorbell(guc, client);
  514. i915_gem_object_unpin_map(client->vma->obj);
  515. }
  516. i915_vma_unpin_and_release(&client->vma);
  517. if (client->ctx_index != GUC_INVALID_CTX_ID) {
  518. guc_ctx_desc_fini(guc, client);
  519. ida_simple_remove(&guc->ctx_ids, client->ctx_index);
  520. }
  521. kfree(client);
  522. }
  523. /* Check that a doorbell register is in the expected state */
  524. static bool guc_doorbell_check(struct intel_guc *guc, uint16_t db_id)
  525. {
  526. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  527. i915_reg_t drbreg = GEN8_DRBREGL(db_id);
  528. uint32_t value = I915_READ(drbreg);
  529. bool enabled = (value & GUC_DOORBELL_ENABLED) != 0;
  530. bool expected = test_bit(db_id, guc->doorbell_bitmap);
  531. if (enabled == expected)
  532. return true;
  533. DRM_DEBUG_DRIVER("Doorbell %d (reg 0x%x) 0x%x, should be %s\n",
  534. db_id, drbreg.reg, value,
  535. expected ? "active" : "inactive");
  536. return false;
  537. }
  538. /*
  539. * Borrow the first client to set up & tear down each unused doorbell
  540. * in turn, to ensure that all doorbell h/w is (re)initialised.
  541. */
  542. static void guc_init_doorbell_hw(struct intel_guc *guc)
  543. {
  544. struct i915_guc_client *client = guc->execbuf_client;
  545. uint16_t db_id;
  546. int i, err;
  547. guc_disable_doorbell(guc, client);
  548. for (i = 0; i < GUC_MAX_DOORBELLS; ++i) {
  549. /* Skip if doorbell is OK */
  550. if (guc_doorbell_check(guc, i))
  551. continue;
  552. err = guc_update_doorbell_id(guc, client, i);
  553. if (err)
  554. DRM_DEBUG_DRIVER("Doorbell %d update failed, err %d\n",
  555. i, err);
  556. }
  557. db_id = select_doorbell_register(guc, client->priority);
  558. WARN_ON(db_id == GUC_INVALID_DOORBELL_ID);
  559. err = guc_update_doorbell_id(guc, client, db_id);
  560. if (err)
  561. DRM_WARN("Failed to restore doorbell to %d, err %d\n",
  562. db_id, err);
  563. /* Read back & verify all doorbell registers */
  564. for (i = 0; i < GUC_MAX_DOORBELLS; ++i)
  565. (void)guc_doorbell_check(guc, i);
  566. }
  567. /**
  568. * guc_client_alloc() - Allocate an i915_guc_client
  569. * @dev_priv: driver private data structure
  570. * @engines: The set of engines to enable for this client
  571. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  572. * The kernel client to replace ExecList submission is created with
  573. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  574. * while a preemption context can use CRITICAL.
  575. * @ctx: the context that owns the client (we use the default render
  576. * context)
  577. *
  578. * Return: An i915_guc_client object if success, else NULL.
  579. */
  580. static struct i915_guc_client *
  581. guc_client_alloc(struct drm_i915_private *dev_priv,
  582. uint32_t engines,
  583. uint32_t priority,
  584. struct i915_gem_context *ctx)
  585. {
  586. struct i915_guc_client *client;
  587. struct intel_guc *guc = &dev_priv->guc;
  588. struct i915_vma *vma;
  589. void *vaddr;
  590. uint16_t db_id;
  591. client = kzalloc(sizeof(*client), GFP_KERNEL);
  592. if (!client)
  593. return NULL;
  594. client->owner = ctx;
  595. client->guc = guc;
  596. client->engines = engines;
  597. client->priority = priority;
  598. client->doorbell_id = GUC_INVALID_DOORBELL_ID;
  599. client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
  600. GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
  601. if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
  602. client->ctx_index = GUC_INVALID_CTX_ID;
  603. goto err;
  604. }
  605. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  606. vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
  607. if (IS_ERR(vma))
  608. goto err;
  609. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  610. client->vma = vma;
  611. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  612. if (IS_ERR(vaddr))
  613. goto err;
  614. client->vaddr = vaddr;
  615. spin_lock_init(&client->wq_lock);
  616. client->wq_offset = GUC_DB_SIZE;
  617. client->wq_size = GUC_WQ_SIZE;
  618. db_id = select_doorbell_register(guc, client->priority);
  619. if (db_id == GUC_INVALID_DOORBELL_ID)
  620. /* XXX: evict a doorbell instead? */
  621. goto err;
  622. client->doorbell_offset = select_doorbell_cacheline(guc);
  623. /*
  624. * Since the doorbell only requires a single cacheline, we can save
  625. * space by putting the application process descriptor in the same
  626. * page. Use the half of the page that doesn't include the doorbell.
  627. */
  628. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  629. client->proc_desc_offset = 0;
  630. else
  631. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  632. guc_proc_desc_init(guc, client);
  633. guc_ctx_desc_init(guc, client);
  634. /* For runtime client allocation we need to enable the doorbell. Not
  635. * required yet for the static execbuf_client as this special kernel
  636. * client is enabled from i915_guc_submission_enable().
  637. *
  638. * guc_update_doorbell_id(guc, client, db_id);
  639. */
  640. DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: ctx_index %u\n",
  641. priority, client, client->engines, client->ctx_index);
  642. DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%x\n",
  643. client->doorbell_id, client->doorbell_offset);
  644. return client;
  645. err:
  646. guc_client_free(dev_priv, client);
  647. return NULL;
  648. }
  649. static void guc_policies_init(struct guc_policies *policies)
  650. {
  651. struct guc_policy *policy;
  652. u32 p, i;
  653. policies->dpc_promote_time = 500000;
  654. policies->max_num_work_items = POLICY_MAX_NUM_WI;
  655. for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
  656. for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
  657. policy = &policies->policy[p][i];
  658. policy->execution_quantum = 1000000;
  659. policy->preemption_time = 500000;
  660. policy->fault_time = 250000;
  661. policy->policy_flags = 0;
  662. }
  663. }
  664. policies->is_valid = 1;
  665. }
  666. static void guc_addon_create(struct intel_guc *guc)
  667. {
  668. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  669. struct i915_vma *vma;
  670. struct guc_ads *ads;
  671. struct guc_policies *policies;
  672. struct guc_mmio_reg_state *reg_state;
  673. struct intel_engine_cs *engine;
  674. enum intel_engine_id id;
  675. struct page *page;
  676. u32 size;
  677. /* The ads obj includes the struct itself and buffers passed to GuC */
  678. size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
  679. sizeof(struct guc_mmio_reg_state) +
  680. GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
  681. vma = guc->ads_vma;
  682. if (!vma) {
  683. vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(size));
  684. if (IS_ERR(vma))
  685. return;
  686. guc->ads_vma = vma;
  687. }
  688. page = i915_vma_first_page(vma);
  689. ads = kmap(page);
  690. /*
  691. * The GuC requires a "Golden Context" when it reinitialises
  692. * engines after a reset. Here we use the Render ring default
  693. * context, which must already exist and be pinned in the GGTT,
  694. * so its address won't change after we've told the GuC where
  695. * to find it.
  696. */
  697. engine = dev_priv->engine[RCS];
  698. ads->golden_context_lrca = engine->status_page.ggtt_offset;
  699. for_each_engine(engine, dev_priv, id)
  700. ads->eng_state_size[engine->guc_id] = intel_lr_context_size(engine);
  701. /* GuC scheduling policies */
  702. policies = (void *)ads + sizeof(struct guc_ads);
  703. guc_policies_init(policies);
  704. ads->scheduler_policies =
  705. guc_ggtt_offset(vma) + sizeof(struct guc_ads);
  706. /* MMIO reg state */
  707. reg_state = (void *)policies + sizeof(struct guc_policies);
  708. for_each_engine(engine, dev_priv, id) {
  709. reg_state->mmio_white_list[engine->guc_id].mmio_start =
  710. engine->mmio_base + GUC_MMIO_WHITE_LIST_START;
  711. /* Nothing to be saved or restored for now. */
  712. reg_state->mmio_white_list[engine->guc_id].count = 0;
  713. }
  714. ads->reg_state_addr = ads->scheduler_policies +
  715. sizeof(struct guc_policies);
  716. ads->reg_state_buffer = ads->reg_state_addr +
  717. sizeof(struct guc_mmio_reg_state);
  718. kunmap(page);
  719. }
  720. /*
  721. * Set up the memory resources to be shared with the GuC. At this point,
  722. * we require just one object that can be mapped through the GGTT.
  723. */
  724. int i915_guc_submission_init(struct drm_i915_private *dev_priv)
  725. {
  726. const size_t ctxsize = sizeof(struct guc_context_desc);
  727. const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
  728. const size_t gemsize = round_up(poolsize, PAGE_SIZE);
  729. struct intel_guc *guc = &dev_priv->guc;
  730. struct i915_vma *vma;
  731. if (!HAS_GUC_SCHED(dev_priv))
  732. return 0;
  733. /* Wipe bitmap & delete client in case of reinitialisation */
  734. bitmap_clear(guc->doorbell_bitmap, 0, GUC_MAX_DOORBELLS);
  735. i915_guc_submission_disable(dev_priv);
  736. if (!i915.enable_guc_submission)
  737. return 0; /* not enabled */
  738. if (guc->ctx_pool_vma)
  739. return 0; /* already allocated */
  740. vma = intel_guc_allocate_vma(guc, gemsize);
  741. if (IS_ERR(vma))
  742. return PTR_ERR(vma);
  743. guc->ctx_pool_vma = vma;
  744. ida_init(&guc->ctx_ids);
  745. intel_guc_log_create(guc);
  746. guc_addon_create(guc);
  747. guc->execbuf_client = guc_client_alloc(dev_priv,
  748. INTEL_INFO(dev_priv)->ring_mask,
  749. GUC_CTX_PRIORITY_KMD_NORMAL,
  750. dev_priv->kernel_context);
  751. if (!guc->execbuf_client) {
  752. DRM_ERROR("Failed to create GuC client for execbuf!\n");
  753. goto err;
  754. }
  755. return 0;
  756. err:
  757. i915_guc_submission_fini(dev_priv);
  758. return -ENOMEM;
  759. }
  760. static void guc_reset_wq(struct i915_guc_client *client)
  761. {
  762. struct guc_process_desc *desc = client->vaddr +
  763. client->proc_desc_offset;
  764. desc->head = 0;
  765. desc->tail = 0;
  766. client->wq_tail = 0;
  767. }
  768. int i915_guc_submission_enable(struct drm_i915_private *dev_priv)
  769. {
  770. struct intel_guc *guc = &dev_priv->guc;
  771. struct i915_guc_client *client = guc->execbuf_client;
  772. struct intel_engine_cs *engine;
  773. enum intel_engine_id id;
  774. if (!client)
  775. return -ENODEV;
  776. intel_guc_sample_forcewake(guc);
  777. guc_reset_wq(client);
  778. guc_init_doorbell_hw(guc);
  779. /* Take over from manual control of ELSP (execlists) */
  780. for_each_engine(engine, dev_priv, id) {
  781. struct drm_i915_gem_request *rq;
  782. engine->submit_request = i915_guc_submit;
  783. engine->schedule = NULL;
  784. /* Replay the current set of previously submitted requests */
  785. list_for_each_entry(rq, &engine->timeline->requests, link) {
  786. client->wq_rsvd += sizeof(struct guc_wq_item);
  787. __i915_guc_submit(rq);
  788. }
  789. }
  790. return 0;
  791. }
  792. void i915_guc_submission_disable(struct drm_i915_private *dev_priv)
  793. {
  794. struct intel_guc *guc = &dev_priv->guc;
  795. if (!guc->execbuf_client)
  796. return;
  797. /* Revert back to manual ELSP submission */
  798. intel_execlists_enable_submission(dev_priv);
  799. }
  800. void i915_guc_submission_fini(struct drm_i915_private *dev_priv)
  801. {
  802. struct intel_guc *guc = &dev_priv->guc;
  803. struct i915_guc_client *client;
  804. client = fetch_and_zero(&guc->execbuf_client);
  805. if (!client)
  806. return;
  807. guc_client_free(dev_priv, client);
  808. i915_vma_unpin_and_release(&guc->ads_vma);
  809. i915_vma_unpin_and_release(&guc->log.vma);
  810. if (guc->ctx_pool_vma)
  811. ida_destroy(&guc->ctx_ids);
  812. i915_vma_unpin_and_release(&guc->ctx_pool_vma);
  813. }
  814. /**
  815. * intel_guc_suspend() - notify GuC entering suspend state
  816. * @dev_priv: i915 device private
  817. */
  818. int intel_guc_suspend(struct drm_i915_private *dev_priv)
  819. {
  820. struct intel_guc *guc = &dev_priv->guc;
  821. struct i915_gem_context *ctx;
  822. u32 data[3];
  823. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  824. return 0;
  825. gen9_disable_guc_interrupts(dev_priv);
  826. ctx = dev_priv->kernel_context;
  827. data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
  828. /* any value greater than GUC_POWER_D0 */
  829. data[1] = GUC_POWER_D1;
  830. /* first page is shared data with GuC */
  831. data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
  832. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  833. }
  834. /**
  835. * intel_guc_resume() - notify GuC resuming from suspend state
  836. * @dev_priv: i915 device private
  837. */
  838. int intel_guc_resume(struct drm_i915_private *dev_priv)
  839. {
  840. struct intel_guc *guc = &dev_priv->guc;
  841. struct i915_gem_context *ctx;
  842. u32 data[3];
  843. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  844. return 0;
  845. if (i915.guc_log_level >= 0)
  846. gen9_enable_guc_interrupts(dev_priv);
  847. ctx = dev_priv->kernel_context;
  848. data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
  849. data[1] = GUC_POWER_D0;
  850. /* first page is shared data with GuC */
  851. data[2] = guc_ggtt_offset(ctx->engine[RCS].state);
  852. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  853. }