i915_guc_submission.c 27 KB

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