i915_guc_submission.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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/firmware.h>
  25. #include <linux/circ_buf.h>
  26. #include "i915_drv.h"
  27. #include "intel_guc.h"
  28. /**
  29. * DOC: GuC-based command submission
  30. *
  31. * i915_guc_client:
  32. * We use the term client to avoid confusion with contexts. A i915_guc_client is
  33. * equivalent to GuC object guc_context_desc. This context descriptor is
  34. * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
  35. * and workqueue for it. Also the process descriptor (guc_process_desc), which
  36. * is mapped to client space. So the client can write Work Item then ring the
  37. * doorbell.
  38. *
  39. * To simplify the implementation, we allocate one gem object that contains all
  40. * pages for doorbell, process descriptor and workqueue.
  41. *
  42. * The Scratch registers:
  43. * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
  44. * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
  45. * triggers an interrupt on the GuC via another register write (0xC4C8).
  46. * Firmware writes a success/fail code back to the action register after
  47. * processes the request. The kernel driver polls waiting for this update and
  48. * then proceeds.
  49. * See host2guc_action()
  50. *
  51. * Doorbells:
  52. * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
  53. * mapped into process space.
  54. *
  55. * Work Items:
  56. * There are several types of work items that the host may place into a
  57. * workqueue, each with its own requirements and limitations. Currently only
  58. * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
  59. * represents in-order queue. The kernel driver packs ring tail pointer and an
  60. * ELSP context descriptor dword into Work Item.
  61. * See guc_add_workqueue_item()
  62. *
  63. */
  64. /*
  65. * Read GuC command/status register (SOFT_SCRATCH_0)
  66. * Return true if it contains a response rather than a command
  67. */
  68. static inline bool host2guc_action_response(struct drm_i915_private *dev_priv,
  69. u32 *status)
  70. {
  71. u32 val = I915_READ(SOFT_SCRATCH(0));
  72. *status = val;
  73. return GUC2HOST_IS_RESPONSE(val);
  74. }
  75. static int host2guc_action(struct intel_guc *guc, u32 *data, u32 len)
  76. {
  77. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  78. u32 status;
  79. int i;
  80. int ret;
  81. if (WARN_ON(len < 1 || len > 15))
  82. return -EINVAL;
  83. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  84. dev_priv->guc.action_count += 1;
  85. dev_priv->guc.action_cmd = data[0];
  86. for (i = 0; i < len; i++)
  87. I915_WRITE(SOFT_SCRATCH(i), data[i]);
  88. POSTING_READ(SOFT_SCRATCH(i - 1));
  89. I915_WRITE(HOST2GUC_INTERRUPT, HOST2GUC_TRIGGER);
  90. /* No HOST2GUC command should take longer than 10ms */
  91. ret = wait_for_atomic(host2guc_action_response(dev_priv, &status), 10);
  92. if (status != GUC2HOST_STATUS_SUCCESS) {
  93. /*
  94. * Either the GuC explicitly returned an error (which
  95. * we convert to -EIO here) or no response at all was
  96. * received within the timeout limit (-ETIMEDOUT)
  97. */
  98. if (ret != -ETIMEDOUT)
  99. ret = -EIO;
  100. DRM_ERROR("GUC: host2guc action 0x%X failed. ret=%d "
  101. "status=0x%08X response=0x%08X\n",
  102. data[0], ret, status,
  103. I915_READ(SOFT_SCRATCH(15)));
  104. dev_priv->guc.action_fail += 1;
  105. dev_priv->guc.action_err = ret;
  106. }
  107. dev_priv->guc.action_status = status;
  108. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  109. return ret;
  110. }
  111. /*
  112. * Tell the GuC to allocate or deallocate a specific doorbell
  113. */
  114. static int host2guc_allocate_doorbell(struct intel_guc *guc,
  115. struct i915_guc_client *client)
  116. {
  117. u32 data[2];
  118. data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL;
  119. data[1] = client->ctx_index;
  120. return host2guc_action(guc, data, 2);
  121. }
  122. static int host2guc_release_doorbell(struct intel_guc *guc,
  123. struct i915_guc_client *client)
  124. {
  125. u32 data[2];
  126. data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL;
  127. data[1] = client->ctx_index;
  128. return host2guc_action(guc, data, 2);
  129. }
  130. static int host2guc_sample_forcewake(struct intel_guc *guc,
  131. struct i915_guc_client *client)
  132. {
  133. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  134. struct drm_device *dev = dev_priv->dev;
  135. u32 data[2];
  136. data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
  137. /* WaRsDisableCoarsePowerGating:skl,bxt */
  138. if (!intel_enable_rc6(dev) ||
  139. NEEDS_WaRsDisableCoarsePowerGating(dev))
  140. data[1] = 0;
  141. else
  142. /* bit 0 and 1 are for Render and Media domain separately */
  143. data[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
  144. return host2guc_action(guc, data, ARRAY_SIZE(data));
  145. }
  146. /*
  147. * Initialise, update, or clear doorbell data shared with the GuC
  148. *
  149. * These functions modify shared data and so need access to the mapped
  150. * client object which contains the page being used for the doorbell
  151. */
  152. static void guc_init_doorbell(struct intel_guc *guc,
  153. struct i915_guc_client *client)
  154. {
  155. struct guc_doorbell_info *doorbell;
  156. void *base;
  157. base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
  158. doorbell = base + client->doorbell_offset;
  159. doorbell->db_status = 1;
  160. doorbell->cookie = 0;
  161. kunmap_atomic(base);
  162. }
  163. static int guc_ring_doorbell(struct i915_guc_client *gc)
  164. {
  165. struct guc_process_desc *desc;
  166. union guc_doorbell_qw db_cmp, db_exc, db_ret;
  167. union guc_doorbell_qw *db;
  168. void *base;
  169. int attempt = 2, ret = -EAGAIN;
  170. base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, 0));
  171. desc = base + gc->proc_desc_offset;
  172. /* Update the tail so it is visible to GuC */
  173. desc->tail = gc->wq_tail;
  174. /* current cookie */
  175. db_cmp.db_status = GUC_DOORBELL_ENABLED;
  176. db_cmp.cookie = gc->cookie;
  177. /* cookie to be updated */
  178. db_exc.db_status = GUC_DOORBELL_ENABLED;
  179. db_exc.cookie = gc->cookie + 1;
  180. if (db_exc.cookie == 0)
  181. db_exc.cookie = 1;
  182. /* pointer of current doorbell cacheline */
  183. db = base + gc->doorbell_offset;
  184. while (attempt--) {
  185. /* lets ring the doorbell */
  186. db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
  187. db_cmp.value_qw, db_exc.value_qw);
  188. /* if the exchange was successfully executed */
  189. if (db_ret.value_qw == db_cmp.value_qw) {
  190. /* db was successfully rung */
  191. gc->cookie = db_exc.cookie;
  192. ret = 0;
  193. break;
  194. }
  195. /* XXX: doorbell was lost and need to acquire it again */
  196. if (db_ret.db_status == GUC_DOORBELL_DISABLED)
  197. break;
  198. DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n",
  199. db_cmp.cookie, db_ret.cookie);
  200. /* update the cookie to newly read cookie from GuC */
  201. db_cmp.cookie = db_ret.cookie;
  202. db_exc.cookie = db_ret.cookie + 1;
  203. if (db_exc.cookie == 0)
  204. db_exc.cookie = 1;
  205. }
  206. /* Finally, update the cached copy of the GuC's WQ head */
  207. gc->wq_head = desc->head;
  208. kunmap_atomic(base);
  209. return ret;
  210. }
  211. static void guc_disable_doorbell(struct intel_guc *guc,
  212. struct i915_guc_client *client)
  213. {
  214. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  215. struct guc_doorbell_info *doorbell;
  216. void *base;
  217. i915_reg_t drbreg = GEN8_DRBREGL(client->doorbell_id);
  218. int value;
  219. base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
  220. doorbell = base + client->doorbell_offset;
  221. doorbell->db_status = 0;
  222. kunmap_atomic(base);
  223. I915_WRITE(drbreg, I915_READ(drbreg) & ~GEN8_DRB_VALID);
  224. value = I915_READ(drbreg);
  225. WARN_ON((value & GEN8_DRB_VALID) != 0);
  226. I915_WRITE(GEN8_DRBREGU(client->doorbell_id), 0);
  227. I915_WRITE(drbreg, 0);
  228. /* XXX: wait for any interrupts */
  229. /* XXX: wait for workqueue to drain */
  230. }
  231. /*
  232. * Select, assign and relase doorbell cachelines
  233. *
  234. * These functions track which doorbell cachelines are in use.
  235. * The data they manipulate is protected by the host2guc lock.
  236. */
  237. static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
  238. {
  239. const uint32_t cacheline_size = cache_line_size();
  240. uint32_t offset;
  241. /* Doorbell uses a single cache line within a page */
  242. offset = offset_in_page(guc->db_cacheline);
  243. /* Moving to next cache line to reduce contention */
  244. guc->db_cacheline += cacheline_size;
  245. DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
  246. offset, guc->db_cacheline, cacheline_size);
  247. return offset;
  248. }
  249. static uint16_t assign_doorbell(struct intel_guc *guc, uint32_t priority)
  250. {
  251. /*
  252. * The bitmap is split into two halves; the first half is used for
  253. * normal priority contexts, the second half for high-priority ones.
  254. * Note that logically higher priorities are numerically less than
  255. * normal ones, so the test below means "is it high-priority?"
  256. */
  257. const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
  258. const uint16_t half = GUC_MAX_DOORBELLS / 2;
  259. const uint16_t start = hi_pri ? half : 0;
  260. const uint16_t end = start + half;
  261. uint16_t id;
  262. id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
  263. if (id == end)
  264. id = GUC_INVALID_DOORBELL_ID;
  265. else
  266. bitmap_set(guc->doorbell_bitmap, id, 1);
  267. DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
  268. hi_pri ? "high" : "normal", id);
  269. return id;
  270. }
  271. static void release_doorbell(struct intel_guc *guc, uint16_t id)
  272. {
  273. bitmap_clear(guc->doorbell_bitmap, id, 1);
  274. }
  275. /*
  276. * Initialise the process descriptor shared with the GuC firmware.
  277. */
  278. static void guc_init_proc_desc(struct intel_guc *guc,
  279. struct i915_guc_client *client)
  280. {
  281. struct guc_process_desc *desc;
  282. void *base;
  283. base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
  284. desc = base + client->proc_desc_offset;
  285. memset(desc, 0, sizeof(*desc));
  286. /*
  287. * XXX: pDoorbell and WQVBaseAddress are pointers in process address
  288. * space for ring3 clients (set them as in mmap_ioctl) or kernel
  289. * space for kernel clients (map on demand instead? May make debug
  290. * easier to have it mapped).
  291. */
  292. desc->wq_base_addr = 0;
  293. desc->db_base_addr = 0;
  294. desc->context_id = client->ctx_index;
  295. desc->wq_size_bytes = client->wq_size;
  296. desc->wq_status = WQ_STATUS_ACTIVE;
  297. desc->priority = client->priority;
  298. kunmap_atomic(base);
  299. }
  300. /*
  301. * Initialise/clear the context descriptor shared with the GuC firmware.
  302. *
  303. * This descriptor tells the GuC where (in GGTT space) to find the important
  304. * data structures relating to this client (doorbell, process descriptor,
  305. * write queue, etc).
  306. */
  307. static void guc_init_ctx_desc(struct intel_guc *guc,
  308. struct i915_guc_client *client)
  309. {
  310. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  311. struct intel_engine_cs *ring;
  312. struct intel_context *ctx = client->owner;
  313. struct guc_context_desc desc;
  314. struct sg_table *sg;
  315. int i;
  316. memset(&desc, 0, sizeof(desc));
  317. desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
  318. desc.context_id = client->ctx_index;
  319. desc.priority = client->priority;
  320. desc.db_id = client->doorbell_id;
  321. for_each_ring(ring, dev_priv, i) {
  322. struct guc_execlist_context *lrc = &desc.lrc[ring->guc_id];
  323. struct drm_i915_gem_object *obj;
  324. uint64_t ctx_desc;
  325. /* TODO: We have a design issue to be solved here. Only when we
  326. * receive the first batch, we know which engine is used by the
  327. * user. But here GuC expects the lrc and ring to be pinned. It
  328. * is not an issue for default context, which is the only one
  329. * for now who owns a GuC client. But for future owner of GuC
  330. * client, need to make sure lrc is pinned prior to enter here.
  331. */
  332. obj = ctx->engine[i].state;
  333. if (!obj)
  334. break; /* XXX: continue? */
  335. ctx_desc = intel_lr_context_descriptor(ctx, ring);
  336. lrc->context_desc = (u32)ctx_desc;
  337. /* The state page is after PPHWSP */
  338. lrc->ring_lcra = i915_gem_obj_ggtt_offset(obj) +
  339. LRC_STATE_PN * PAGE_SIZE;
  340. lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
  341. (ring->guc_id << GUC_ELC_ENGINE_OFFSET);
  342. obj = ctx->engine[i].ringbuf->obj;
  343. lrc->ring_begin = i915_gem_obj_ggtt_offset(obj);
  344. lrc->ring_end = lrc->ring_begin + obj->base.size - 1;
  345. lrc->ring_next_free_location = lrc->ring_begin;
  346. lrc->ring_current_tail_pointer_value = 0;
  347. desc.engines_used |= (1 << ring->guc_id);
  348. }
  349. WARN_ON(desc.engines_used == 0);
  350. /*
  351. * The CPU address is only needed at certain points, so kmap_atomic on
  352. * demand instead of storing it in the ctx descriptor.
  353. * XXX: May make debug easier to have it mapped
  354. */
  355. desc.db_trigger_cpu = 0;
  356. desc.db_trigger_uk = client->doorbell_offset +
  357. i915_gem_obj_ggtt_offset(client->client_obj);
  358. desc.db_trigger_phy = client->doorbell_offset +
  359. sg_dma_address(client->client_obj->pages->sgl);
  360. desc.process_desc = client->proc_desc_offset +
  361. i915_gem_obj_ggtt_offset(client->client_obj);
  362. desc.wq_addr = client->wq_offset +
  363. i915_gem_obj_ggtt_offset(client->client_obj);
  364. desc.wq_size = client->wq_size;
  365. /*
  366. * XXX: Take LRCs from an existing intel_context if this is not an
  367. * IsKMDCreatedContext client
  368. */
  369. desc.desc_private = (uintptr_t)client;
  370. /* Pool context is pinned already */
  371. sg = guc->ctx_pool_obj->pages;
  372. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  373. sizeof(desc) * client->ctx_index);
  374. }
  375. static void guc_fini_ctx_desc(struct intel_guc *guc,
  376. struct i915_guc_client *client)
  377. {
  378. struct guc_context_desc desc;
  379. struct sg_table *sg;
  380. memset(&desc, 0, sizeof(desc));
  381. sg = guc->ctx_pool_obj->pages;
  382. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  383. sizeof(desc) * client->ctx_index);
  384. }
  385. int i915_guc_wq_check_space(struct i915_guc_client *gc)
  386. {
  387. struct guc_process_desc *desc;
  388. void *base;
  389. u32 size = sizeof(struct guc_wq_item);
  390. int ret = -ETIMEDOUT, timeout_counter = 200;
  391. if (!gc)
  392. return 0;
  393. /* Quickly return if wq space is available since last time we cache the
  394. * head position. */
  395. if (CIRC_SPACE(gc->wq_tail, gc->wq_head, gc->wq_size) >= size)
  396. return 0;
  397. base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, 0));
  398. desc = base + gc->proc_desc_offset;
  399. while (timeout_counter-- > 0) {
  400. gc->wq_head = desc->head;
  401. if (CIRC_SPACE(gc->wq_tail, gc->wq_head, gc->wq_size) >= size) {
  402. ret = 0;
  403. break;
  404. }
  405. if (timeout_counter)
  406. usleep_range(1000, 2000);
  407. };
  408. kunmap_atomic(base);
  409. return ret;
  410. }
  411. static int guc_add_workqueue_item(struct i915_guc_client *gc,
  412. struct drm_i915_gem_request *rq)
  413. {
  414. struct guc_wq_item *wqi;
  415. void *base;
  416. u32 tail, wq_len, wq_off, space;
  417. space = CIRC_SPACE(gc->wq_tail, gc->wq_head, gc->wq_size);
  418. if (WARN_ON(space < sizeof(struct guc_wq_item)))
  419. return -ENOSPC; /* shouldn't happen */
  420. /* postincrement WQ tail for next time */
  421. wq_off = gc->wq_tail;
  422. gc->wq_tail += sizeof(struct guc_wq_item);
  423. gc->wq_tail &= gc->wq_size - 1;
  424. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  425. * should not have the case where structure wqi is across page, neither
  426. * wrapped to the beginning. This simplifies the implementation below.
  427. *
  428. * XXX: if not the case, we need save data to a temp wqi and copy it to
  429. * workqueue buffer dw by dw.
  430. */
  431. WARN_ON(sizeof(struct guc_wq_item) != 16);
  432. WARN_ON(wq_off & 3);
  433. /* wq starts from the page after doorbell / process_desc */
  434. base = kmap_atomic(i915_gem_object_get_page(gc->client_obj,
  435. (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT));
  436. wq_off &= PAGE_SIZE - 1;
  437. wqi = (struct guc_wq_item *)((char *)base + wq_off);
  438. /* len does not include the header */
  439. wq_len = sizeof(struct guc_wq_item) / sizeof(u32) - 1;
  440. wqi->header = WQ_TYPE_INORDER |
  441. (wq_len << WQ_LEN_SHIFT) |
  442. (rq->ring->guc_id << WQ_TARGET_SHIFT) |
  443. WQ_NO_WCFLUSH_WAIT;
  444. /* The GuC wants only the low-order word of the context descriptor */
  445. wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, rq->ring);
  446. /* The GuC firmware wants the tail index in QWords, not bytes */
  447. tail = rq->ringbuf->tail >> 3;
  448. wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
  449. wqi->fence_id = 0; /*XXX: what fence to be here */
  450. kunmap_atomic(base);
  451. return 0;
  452. }
  453. /**
  454. * i915_guc_submit() - Submit commands through GuC
  455. * @client: the guc client where commands will go through
  456. * @rq: request associated with the commands
  457. *
  458. * Return: 0 if succeed
  459. */
  460. int i915_guc_submit(struct i915_guc_client *client,
  461. struct drm_i915_gem_request *rq)
  462. {
  463. struct intel_guc *guc = client->guc;
  464. unsigned int engine_id = rq->ring->guc_id;
  465. int q_ret, b_ret;
  466. q_ret = guc_add_workqueue_item(client, rq);
  467. if (q_ret == 0)
  468. b_ret = guc_ring_doorbell(client);
  469. client->submissions[engine_id] += 1;
  470. if (q_ret) {
  471. client->q_fail += 1;
  472. client->retcode = q_ret;
  473. } else if (b_ret) {
  474. client->b_fail += 1;
  475. client->retcode = q_ret = b_ret;
  476. } else {
  477. client->retcode = 0;
  478. }
  479. guc->submissions[engine_id] += 1;
  480. guc->last_seqno[engine_id] = rq->seqno;
  481. return q_ret;
  482. }
  483. /*
  484. * Everything below here is concerned with setup & teardown, and is
  485. * therefore not part of the somewhat time-critical batch-submission
  486. * path of i915_guc_submit() above.
  487. */
  488. /**
  489. * gem_allocate_guc_obj() - Allocate gem object for GuC usage
  490. * @dev: drm device
  491. * @size: size of object
  492. *
  493. * This is a wrapper to create a gem obj. In order to use it inside GuC, the
  494. * object needs to be pinned lifetime. Also we must pin it to gtt space other
  495. * than [0, GUC_WOPCM_TOP) because this range is reserved inside GuC.
  496. *
  497. * Return: A drm_i915_gem_object if successful, otherwise NULL.
  498. */
  499. static struct drm_i915_gem_object *gem_allocate_guc_obj(struct drm_device *dev,
  500. u32 size)
  501. {
  502. struct drm_i915_private *dev_priv = dev->dev_private;
  503. struct drm_i915_gem_object *obj;
  504. obj = i915_gem_alloc_object(dev, size);
  505. if (!obj)
  506. return NULL;
  507. if (i915_gem_object_get_pages(obj)) {
  508. drm_gem_object_unreference(&obj->base);
  509. return NULL;
  510. }
  511. if (i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
  512. PIN_OFFSET_BIAS | GUC_WOPCM_TOP)) {
  513. drm_gem_object_unreference(&obj->base);
  514. return NULL;
  515. }
  516. /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
  517. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  518. return obj;
  519. }
  520. /**
  521. * gem_release_guc_obj() - Release gem object allocated for GuC usage
  522. * @obj: gem obj to be released
  523. */
  524. static void gem_release_guc_obj(struct drm_i915_gem_object *obj)
  525. {
  526. if (!obj)
  527. return;
  528. if (i915_gem_obj_is_pinned(obj))
  529. i915_gem_object_ggtt_unpin(obj);
  530. drm_gem_object_unreference(&obj->base);
  531. }
  532. static void guc_client_free(struct drm_device *dev,
  533. struct i915_guc_client *client)
  534. {
  535. struct drm_i915_private *dev_priv = dev->dev_private;
  536. struct intel_guc *guc = &dev_priv->guc;
  537. if (!client)
  538. return;
  539. if (client->doorbell_id != GUC_INVALID_DOORBELL_ID) {
  540. /*
  541. * First disable the doorbell, then tell the GuC we've
  542. * finished with it, finally deallocate it in our bitmap
  543. */
  544. guc_disable_doorbell(guc, client);
  545. host2guc_release_doorbell(guc, client);
  546. release_doorbell(guc, client->doorbell_id);
  547. }
  548. /*
  549. * XXX: wait for any outstanding submissions before freeing memory.
  550. * Be sure to drop any locks
  551. */
  552. gem_release_guc_obj(client->client_obj);
  553. if (client->ctx_index != GUC_INVALID_CTX_ID) {
  554. guc_fini_ctx_desc(guc, client);
  555. ida_simple_remove(&guc->ctx_ids, client->ctx_index);
  556. }
  557. kfree(client);
  558. }
  559. /**
  560. * guc_client_alloc() - Allocate an i915_guc_client
  561. * @dev: drm device
  562. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  563. * The kernel client to replace ExecList submission is created with
  564. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  565. * while a preemption context can use CRITICAL.
  566. * @ctx: the context that owns the client (we use the default render
  567. * context)
  568. *
  569. * Return: An i915_guc_client object if success.
  570. */
  571. static struct i915_guc_client *guc_client_alloc(struct drm_device *dev,
  572. uint32_t priority,
  573. struct intel_context *ctx)
  574. {
  575. struct i915_guc_client *client;
  576. struct drm_i915_private *dev_priv = dev->dev_private;
  577. struct intel_guc *guc = &dev_priv->guc;
  578. struct drm_i915_gem_object *obj;
  579. client = kzalloc(sizeof(*client), GFP_KERNEL);
  580. if (!client)
  581. return NULL;
  582. client->doorbell_id = GUC_INVALID_DOORBELL_ID;
  583. client->priority = priority;
  584. client->owner = ctx;
  585. client->guc = guc;
  586. client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
  587. GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
  588. if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
  589. client->ctx_index = GUC_INVALID_CTX_ID;
  590. goto err;
  591. }
  592. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  593. obj = gem_allocate_guc_obj(dev, GUC_DB_SIZE + GUC_WQ_SIZE);
  594. if (!obj)
  595. goto err;
  596. client->client_obj = obj;
  597. client->wq_offset = GUC_DB_SIZE;
  598. client->wq_size = GUC_WQ_SIZE;
  599. client->doorbell_offset = select_doorbell_cacheline(guc);
  600. /*
  601. * Since the doorbell only requires a single cacheline, we can save
  602. * space by putting the application process descriptor in the same
  603. * page. Use the half of the page that doesn't include the doorbell.
  604. */
  605. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  606. client->proc_desc_offset = 0;
  607. else
  608. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  609. client->doorbell_id = assign_doorbell(guc, client->priority);
  610. if (client->doorbell_id == GUC_INVALID_DOORBELL_ID)
  611. /* XXX: evict a doorbell instead */
  612. goto err;
  613. guc_init_proc_desc(guc, client);
  614. guc_init_ctx_desc(guc, client);
  615. guc_init_doorbell(guc, client);
  616. /* XXX: Any cache flushes needed? General domain mgmt calls? */
  617. if (host2guc_allocate_doorbell(guc, client))
  618. goto err;
  619. DRM_DEBUG_DRIVER("new priority %u client %p: ctx_index %u db_id %u\n",
  620. priority, client, client->ctx_index, client->doorbell_id);
  621. return client;
  622. err:
  623. DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
  624. guc_client_free(dev, client);
  625. return NULL;
  626. }
  627. static void guc_create_log(struct intel_guc *guc)
  628. {
  629. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  630. struct drm_i915_gem_object *obj;
  631. unsigned long offset;
  632. uint32_t size, flags;
  633. if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
  634. return;
  635. if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
  636. i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
  637. /* The first page is to save log buffer state. Allocate one
  638. * extra page for others in case for overlap */
  639. size = (1 + GUC_LOG_DPC_PAGES + 1 +
  640. GUC_LOG_ISR_PAGES + 1 +
  641. GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
  642. obj = guc->log_obj;
  643. if (!obj) {
  644. obj = gem_allocate_guc_obj(dev_priv->dev, size);
  645. if (!obj) {
  646. /* logging will be off */
  647. i915.guc_log_level = -1;
  648. return;
  649. }
  650. guc->log_obj = obj;
  651. }
  652. /* each allocated unit is a page */
  653. flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
  654. (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
  655. (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
  656. (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
  657. offset = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; /* in pages */
  658. guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
  659. }
  660. static void init_guc_policies(struct guc_policies *policies)
  661. {
  662. struct guc_policy *policy;
  663. u32 p, i;
  664. policies->dpc_promote_time = 500000;
  665. policies->max_num_work_items = POLICY_MAX_NUM_WI;
  666. for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
  667. for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
  668. policy = &policies->policy[p][i];
  669. policy->execution_quantum = 1000000;
  670. policy->preemption_time = 500000;
  671. policy->fault_time = 250000;
  672. policy->policy_flags = 0;
  673. }
  674. }
  675. policies->is_valid = 1;
  676. }
  677. static void guc_create_ads(struct intel_guc *guc)
  678. {
  679. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  680. struct drm_i915_gem_object *obj;
  681. struct guc_ads *ads;
  682. struct guc_policies *policies;
  683. struct guc_mmio_reg_state *reg_state;
  684. struct intel_engine_cs *ring;
  685. struct page *page;
  686. u32 size, i;
  687. /* The ads obj includes the struct itself and buffers passed to GuC */
  688. size = sizeof(struct guc_ads) + sizeof(struct guc_policies) +
  689. sizeof(struct guc_mmio_reg_state) +
  690. GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE;
  691. obj = guc->ads_obj;
  692. if (!obj) {
  693. obj = gem_allocate_guc_obj(dev_priv->dev, PAGE_ALIGN(size));
  694. if (!obj)
  695. return;
  696. guc->ads_obj = obj;
  697. }
  698. page = i915_gem_object_get_page(obj, 0);
  699. ads = kmap(page);
  700. /*
  701. * The GuC requires a "Golden Context" when it reinitialises
  702. * engines after a reset. Here we use the Render ring default
  703. * context, which must already exist and be pinned in the GGTT,
  704. * so its address won't change after we've told the GuC where
  705. * to find it.
  706. */
  707. ring = &dev_priv->ring[RCS];
  708. ads->golden_context_lrca = ring->status_page.gfx_addr;
  709. for_each_ring(ring, dev_priv, i)
  710. ads->eng_state_size[ring->guc_id] = intel_lr_context_size(ring);
  711. /* GuC scheduling policies */
  712. policies = (void *)ads + sizeof(struct guc_ads);
  713. init_guc_policies(policies);
  714. ads->scheduler_policies = i915_gem_obj_ggtt_offset(obj) +
  715. sizeof(struct guc_ads);
  716. /* MMIO reg state */
  717. reg_state = (void *)policies + sizeof(struct guc_policies);
  718. for_each_ring(ring, dev_priv, i) {
  719. reg_state->mmio_white_list[ring->guc_id].mmio_start =
  720. ring->mmio_base + GUC_MMIO_WHITE_LIST_START;
  721. /* Nothing to be saved or restored for now. */
  722. reg_state->mmio_white_list[ring->guc_id].count = 0;
  723. }
  724. ads->reg_state_addr = ads->scheduler_policies +
  725. sizeof(struct guc_policies);
  726. ads->reg_state_buffer = ads->reg_state_addr +
  727. sizeof(struct guc_mmio_reg_state);
  728. kunmap(page);
  729. }
  730. /*
  731. * Set up the memory resources to be shared with the GuC. At this point,
  732. * we require just one object that can be mapped through the GGTT.
  733. */
  734. int i915_guc_submission_init(struct drm_device *dev)
  735. {
  736. struct drm_i915_private *dev_priv = dev->dev_private;
  737. const size_t ctxsize = sizeof(struct guc_context_desc);
  738. const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
  739. const size_t gemsize = round_up(poolsize, PAGE_SIZE);
  740. struct intel_guc *guc = &dev_priv->guc;
  741. if (!i915.enable_guc_submission)
  742. return 0; /* not enabled */
  743. if (guc->ctx_pool_obj)
  744. return 0; /* already allocated */
  745. guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv->dev, gemsize);
  746. if (!guc->ctx_pool_obj)
  747. return -ENOMEM;
  748. ida_init(&guc->ctx_ids);
  749. guc_create_log(guc);
  750. guc_create_ads(guc);
  751. return 0;
  752. }
  753. int i915_guc_submission_enable(struct drm_device *dev)
  754. {
  755. struct drm_i915_private *dev_priv = dev->dev_private;
  756. struct intel_guc *guc = &dev_priv->guc;
  757. struct intel_context *ctx = dev_priv->kernel_context;
  758. struct i915_guc_client *client;
  759. /* client for execbuf submission */
  760. client = guc_client_alloc(dev, GUC_CTX_PRIORITY_KMD_NORMAL, ctx);
  761. if (!client) {
  762. DRM_ERROR("Failed to create execbuf guc_client\n");
  763. return -ENOMEM;
  764. }
  765. guc->execbuf_client = client;
  766. host2guc_sample_forcewake(guc, client);
  767. return 0;
  768. }
  769. void i915_guc_submission_disable(struct drm_device *dev)
  770. {
  771. struct drm_i915_private *dev_priv = dev->dev_private;
  772. struct intel_guc *guc = &dev_priv->guc;
  773. guc_client_free(dev, guc->execbuf_client);
  774. guc->execbuf_client = NULL;
  775. }
  776. void i915_guc_submission_fini(struct drm_device *dev)
  777. {
  778. struct drm_i915_private *dev_priv = dev->dev_private;
  779. struct intel_guc *guc = &dev_priv->guc;
  780. gem_release_guc_obj(dev_priv->guc.ads_obj);
  781. guc->ads_obj = NULL;
  782. gem_release_guc_obj(dev_priv->guc.log_obj);
  783. guc->log_obj = NULL;
  784. if (guc->ctx_pool_obj)
  785. ida_destroy(&guc->ctx_ids);
  786. gem_release_guc_obj(guc->ctx_pool_obj);
  787. guc->ctx_pool_obj = NULL;
  788. }
  789. /**
  790. * intel_guc_suspend() - notify GuC entering suspend state
  791. * @dev: drm device
  792. */
  793. int intel_guc_suspend(struct drm_device *dev)
  794. {
  795. struct drm_i915_private *dev_priv = dev->dev_private;
  796. struct intel_guc *guc = &dev_priv->guc;
  797. struct intel_context *ctx;
  798. u32 data[3];
  799. if (!i915.enable_guc_submission)
  800. return 0;
  801. ctx = dev_priv->kernel_context;
  802. data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
  803. /* any value greater than GUC_POWER_D0 */
  804. data[1] = GUC_POWER_D1;
  805. /* first page is shared data with GuC */
  806. data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
  807. return host2guc_action(guc, data, ARRAY_SIZE(data));
  808. }
  809. /**
  810. * intel_guc_resume() - notify GuC resuming from suspend state
  811. * @dev: drm device
  812. */
  813. int intel_guc_resume(struct drm_device *dev)
  814. {
  815. struct drm_i915_private *dev_priv = dev->dev_private;
  816. struct intel_guc *guc = &dev_priv->guc;
  817. struct intel_context *ctx;
  818. u32 data[3];
  819. if (!i915.enable_guc_submission)
  820. return 0;
  821. ctx = dev_priv->kernel_context;
  822. data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
  823. data[1] = GUC_POWER_D0;
  824. /* first page is shared data with GuC */
  825. data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
  826. return host2guc_action(guc, data, ARRAY_SIZE(data));
  827. }