i915_guc_submission.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. doorbell = client->client_base + client->doorbell_offset;
  157. doorbell->db_status = GUC_DOORBELL_ENABLED;
  158. doorbell->cookie = 0;
  159. }
  160. static int guc_ring_doorbell(struct i915_guc_client *gc)
  161. {
  162. struct guc_process_desc *desc;
  163. union guc_doorbell_qw db_cmp, db_exc, db_ret;
  164. union guc_doorbell_qw *db;
  165. int attempt = 2, ret = -EAGAIN;
  166. desc = gc->client_base + gc->proc_desc_offset;
  167. /* Update the tail so it is visible to GuC */
  168. desc->tail = gc->wq_tail;
  169. /* current cookie */
  170. db_cmp.db_status = GUC_DOORBELL_ENABLED;
  171. db_cmp.cookie = gc->cookie;
  172. /* cookie to be updated */
  173. db_exc.db_status = GUC_DOORBELL_ENABLED;
  174. db_exc.cookie = gc->cookie + 1;
  175. if (db_exc.cookie == 0)
  176. db_exc.cookie = 1;
  177. /* pointer of current doorbell cacheline */
  178. db = gc->client_base + gc->doorbell_offset;
  179. while (attempt--) {
  180. /* lets ring the doorbell */
  181. db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
  182. db_cmp.value_qw, db_exc.value_qw);
  183. /* if the exchange was successfully executed */
  184. if (db_ret.value_qw == db_cmp.value_qw) {
  185. /* db was successfully rung */
  186. gc->cookie = db_exc.cookie;
  187. ret = 0;
  188. break;
  189. }
  190. /* XXX: doorbell was lost and need to acquire it again */
  191. if (db_ret.db_status == GUC_DOORBELL_DISABLED)
  192. break;
  193. DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n",
  194. db_cmp.cookie, db_ret.cookie);
  195. /* update the cookie to newly read cookie from GuC */
  196. db_cmp.cookie = db_ret.cookie;
  197. db_exc.cookie = db_ret.cookie + 1;
  198. if (db_exc.cookie == 0)
  199. db_exc.cookie = 1;
  200. }
  201. return ret;
  202. }
  203. static void guc_disable_doorbell(struct intel_guc *guc,
  204. struct i915_guc_client *client)
  205. {
  206. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  207. struct guc_doorbell_info *doorbell;
  208. i915_reg_t drbreg = GEN8_DRBREGL(client->doorbell_id);
  209. int value;
  210. doorbell = client->client_base + client->doorbell_offset;
  211. doorbell->db_status = GUC_DOORBELL_DISABLED;
  212. I915_WRITE(drbreg, I915_READ(drbreg) & ~GEN8_DRB_VALID);
  213. value = I915_READ(drbreg);
  214. WARN_ON((value & GEN8_DRB_VALID) != 0);
  215. I915_WRITE(GEN8_DRBREGU(client->doorbell_id), 0);
  216. I915_WRITE(drbreg, 0);
  217. /* XXX: wait for any interrupts */
  218. /* XXX: wait for workqueue to drain */
  219. }
  220. /*
  221. * Select, assign and relase doorbell cachelines
  222. *
  223. * These functions track which doorbell cachelines are in use.
  224. * The data they manipulate is protected by the host2guc lock.
  225. */
  226. static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
  227. {
  228. const uint32_t cacheline_size = cache_line_size();
  229. uint32_t offset;
  230. /* Doorbell uses a single cache line within a page */
  231. offset = offset_in_page(guc->db_cacheline);
  232. /* Moving to next cache line to reduce contention */
  233. guc->db_cacheline += cacheline_size;
  234. DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
  235. offset, guc->db_cacheline, cacheline_size);
  236. return offset;
  237. }
  238. static uint16_t assign_doorbell(struct intel_guc *guc, uint32_t priority)
  239. {
  240. /*
  241. * The bitmap is split into two halves; the first half is used for
  242. * normal priority contexts, the second half for high-priority ones.
  243. * Note that logically higher priorities are numerically less than
  244. * normal ones, so the test below means "is it high-priority?"
  245. */
  246. const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
  247. const uint16_t half = GUC_MAX_DOORBELLS / 2;
  248. const uint16_t start = hi_pri ? half : 0;
  249. const uint16_t end = start + half;
  250. uint16_t id;
  251. id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
  252. if (id == end)
  253. id = GUC_INVALID_DOORBELL_ID;
  254. else
  255. bitmap_set(guc->doorbell_bitmap, id, 1);
  256. DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
  257. hi_pri ? "high" : "normal", id);
  258. return id;
  259. }
  260. static void release_doorbell(struct intel_guc *guc, uint16_t id)
  261. {
  262. bitmap_clear(guc->doorbell_bitmap, id, 1);
  263. }
  264. /*
  265. * Initialise the process descriptor shared with the GuC firmware.
  266. */
  267. static void guc_init_proc_desc(struct intel_guc *guc,
  268. struct i915_guc_client *client)
  269. {
  270. struct guc_process_desc *desc;
  271. desc = client->client_base + client->proc_desc_offset;
  272. memset(desc, 0, sizeof(*desc));
  273. /*
  274. * XXX: pDoorbell and WQVBaseAddress are pointers in process address
  275. * space for ring3 clients (set them as in mmap_ioctl) or kernel
  276. * space for kernel clients (map on demand instead? May make debug
  277. * easier to have it mapped).
  278. */
  279. desc->wq_base_addr = 0;
  280. desc->db_base_addr = 0;
  281. desc->context_id = client->ctx_index;
  282. desc->wq_size_bytes = client->wq_size;
  283. desc->wq_status = WQ_STATUS_ACTIVE;
  284. desc->priority = client->priority;
  285. }
  286. /*
  287. * Initialise/clear the context descriptor shared with the GuC firmware.
  288. *
  289. * This descriptor tells the GuC where (in GGTT space) to find the important
  290. * data structures relating to this client (doorbell, process descriptor,
  291. * write queue, etc).
  292. */
  293. static void guc_init_ctx_desc(struct intel_guc *guc,
  294. struct i915_guc_client *client)
  295. {
  296. struct drm_i915_gem_object *client_obj = client->client_obj;
  297. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  298. struct intel_engine_cs *engine;
  299. struct intel_context *ctx = client->owner;
  300. struct guc_context_desc desc;
  301. struct sg_table *sg;
  302. enum intel_engine_id id;
  303. u32 gfx_addr;
  304. memset(&desc, 0, sizeof(desc));
  305. desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
  306. desc.context_id = client->ctx_index;
  307. desc.priority = client->priority;
  308. desc.db_id = client->doorbell_id;
  309. for_each_engine_id(engine, dev_priv, id) {
  310. struct guc_execlist_context *lrc = &desc.lrc[engine->guc_id];
  311. struct drm_i915_gem_object *obj;
  312. uint64_t ctx_desc;
  313. /* TODO: We have a design issue to be solved here. Only when we
  314. * receive the first batch, we know which engine is used by the
  315. * user. But here GuC expects the lrc and ring to be pinned. It
  316. * is not an issue for default context, which is the only one
  317. * for now who owns a GuC client. But for future owner of GuC
  318. * client, need to make sure lrc is pinned prior to enter here.
  319. */
  320. obj = ctx->engine[id].state;
  321. if (!obj)
  322. break; /* XXX: continue? */
  323. ctx_desc = intel_lr_context_descriptor(ctx, engine);
  324. lrc->context_desc = (u32)ctx_desc;
  325. /* The state page is after PPHWSP */
  326. gfx_addr = i915_gem_obj_ggtt_offset(obj);
  327. lrc->ring_lcra = gfx_addr + LRC_STATE_PN * PAGE_SIZE;
  328. lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
  329. (engine->guc_id << GUC_ELC_ENGINE_OFFSET);
  330. obj = ctx->engine[id].ringbuf->obj;
  331. gfx_addr = i915_gem_obj_ggtt_offset(obj);
  332. lrc->ring_begin = gfx_addr;
  333. lrc->ring_end = gfx_addr + obj->base.size - 1;
  334. lrc->ring_next_free_location = gfx_addr;
  335. lrc->ring_current_tail_pointer_value = 0;
  336. desc.engines_used |= (1 << engine->guc_id);
  337. }
  338. WARN_ON(desc.engines_used == 0);
  339. /*
  340. * The doorbell, process descriptor, and workqueue are all parts
  341. * of the client object, which the GuC will reference via the GGTT
  342. */
  343. gfx_addr = i915_gem_obj_ggtt_offset(client_obj);
  344. desc.db_trigger_phy = sg_dma_address(client_obj->pages->sgl) +
  345. client->doorbell_offset;
  346. desc.db_trigger_cpu = (uintptr_t)client->client_base +
  347. client->doorbell_offset;
  348. desc.db_trigger_uk = gfx_addr + client->doorbell_offset;
  349. desc.process_desc = gfx_addr + client->proc_desc_offset;
  350. desc.wq_addr = gfx_addr + client->wq_offset;
  351. desc.wq_size = client->wq_size;
  352. /*
  353. * XXX: Take LRCs from an existing intel_context if this is not an
  354. * IsKMDCreatedContext client
  355. */
  356. desc.desc_private = (uintptr_t)client;
  357. /* Pool context is pinned already */
  358. sg = guc->ctx_pool_obj->pages;
  359. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  360. sizeof(desc) * client->ctx_index);
  361. }
  362. static void guc_fini_ctx_desc(struct intel_guc *guc,
  363. struct i915_guc_client *client)
  364. {
  365. struct guc_context_desc desc;
  366. struct sg_table *sg;
  367. memset(&desc, 0, sizeof(desc));
  368. sg = guc->ctx_pool_obj->pages;
  369. sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  370. sizeof(desc) * client->ctx_index);
  371. }
  372. int i915_guc_wq_check_space(struct i915_guc_client *gc)
  373. {
  374. struct guc_process_desc *desc;
  375. u32 size = sizeof(struct guc_wq_item);
  376. int ret = -ETIMEDOUT, timeout_counter = 200;
  377. if (!gc)
  378. return 0;
  379. desc = gc->client_base + gc->proc_desc_offset;
  380. while (timeout_counter-- > 0) {
  381. if (CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size) >= size) {
  382. ret = 0;
  383. break;
  384. }
  385. if (timeout_counter)
  386. usleep_range(1000, 2000);
  387. };
  388. return ret;
  389. }
  390. static int guc_add_workqueue_item(struct i915_guc_client *gc,
  391. struct drm_i915_gem_request *rq)
  392. {
  393. struct guc_process_desc *desc;
  394. struct guc_wq_item *wqi;
  395. void *base;
  396. u32 tail, wq_len, wq_off, space;
  397. desc = gc->client_base + gc->proc_desc_offset;
  398. space = CIRC_SPACE(gc->wq_tail, desc->head, gc->wq_size);
  399. if (WARN_ON(space < sizeof(struct guc_wq_item)))
  400. return -ENOSPC; /* shouldn't happen */
  401. /* postincrement WQ tail for next time */
  402. wq_off = gc->wq_tail;
  403. gc->wq_tail += sizeof(struct guc_wq_item);
  404. gc->wq_tail &= gc->wq_size - 1;
  405. /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  406. * should not have the case where structure wqi is across page, neither
  407. * wrapped to the beginning. This simplifies the implementation below.
  408. *
  409. * XXX: if not the case, we need save data to a temp wqi and copy it to
  410. * workqueue buffer dw by dw.
  411. */
  412. WARN_ON(sizeof(struct guc_wq_item) != 16);
  413. WARN_ON(wq_off & 3);
  414. /* wq starts from the page after doorbell / process_desc */
  415. base = kmap_atomic(i915_gem_object_get_page(gc->client_obj,
  416. (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT));
  417. wq_off &= PAGE_SIZE - 1;
  418. wqi = (struct guc_wq_item *)((char *)base + wq_off);
  419. /* len does not include the header */
  420. wq_len = sizeof(struct guc_wq_item) / sizeof(u32) - 1;
  421. wqi->header = WQ_TYPE_INORDER |
  422. (wq_len << WQ_LEN_SHIFT) |
  423. (rq->engine->guc_id << WQ_TARGET_SHIFT) |
  424. WQ_NO_WCFLUSH_WAIT;
  425. /* The GuC wants only the low-order word of the context descriptor */
  426. wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx,
  427. rq->engine);
  428. /* The GuC firmware wants the tail index in QWords, not bytes */
  429. tail = rq->ringbuf->tail >> 3;
  430. wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
  431. wqi->fence_id = 0; /*XXX: what fence to be here */
  432. kunmap_atomic(base);
  433. return 0;
  434. }
  435. /**
  436. * i915_guc_submit() - Submit commands through GuC
  437. * @client: the guc client where commands will go through
  438. * @rq: request associated with the commands
  439. *
  440. * Return: 0 if succeed
  441. */
  442. int i915_guc_submit(struct i915_guc_client *client,
  443. struct drm_i915_gem_request *rq)
  444. {
  445. struct intel_guc *guc = client->guc;
  446. unsigned int engine_id = rq->engine->guc_id;
  447. int q_ret, b_ret;
  448. q_ret = guc_add_workqueue_item(client, rq);
  449. if (q_ret == 0)
  450. b_ret = guc_ring_doorbell(client);
  451. client->submissions[engine_id] += 1;
  452. if (q_ret) {
  453. client->q_fail += 1;
  454. client->retcode = q_ret;
  455. } else if (b_ret) {
  456. client->b_fail += 1;
  457. client->retcode = q_ret = b_ret;
  458. } else {
  459. client->retcode = 0;
  460. }
  461. guc->submissions[engine_id] += 1;
  462. guc->last_seqno[engine_id] = rq->seqno;
  463. return q_ret;
  464. }
  465. /*
  466. * Everything below here is concerned with setup & teardown, and is
  467. * therefore not part of the somewhat time-critical batch-submission
  468. * path of i915_guc_submit() above.
  469. */
  470. /**
  471. * gem_allocate_guc_obj() - Allocate gem object for GuC usage
  472. * @dev: drm device
  473. * @size: size of object
  474. *
  475. * This is a wrapper to create a gem obj. In order to use it inside GuC, the
  476. * object needs to be pinned lifetime. Also we must pin it to gtt space other
  477. * than [0, GUC_WOPCM_TOP) because this range is reserved inside GuC.
  478. *
  479. * Return: A drm_i915_gem_object if successful, otherwise NULL.
  480. */
  481. static struct drm_i915_gem_object *gem_allocate_guc_obj(struct drm_device *dev,
  482. u32 size)
  483. {
  484. struct drm_i915_private *dev_priv = dev->dev_private;
  485. struct drm_i915_gem_object *obj;
  486. obj = i915_gem_alloc_object(dev, size);
  487. if (!obj)
  488. return NULL;
  489. if (i915_gem_object_get_pages(obj)) {
  490. drm_gem_object_unreference(&obj->base);
  491. return NULL;
  492. }
  493. if (i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
  494. PIN_OFFSET_BIAS | GUC_WOPCM_TOP)) {
  495. drm_gem_object_unreference(&obj->base);
  496. return NULL;
  497. }
  498. /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
  499. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  500. return obj;
  501. }
  502. /**
  503. * gem_release_guc_obj() - Release gem object allocated for GuC usage
  504. * @obj: gem obj to be released
  505. */
  506. static void gem_release_guc_obj(struct drm_i915_gem_object *obj)
  507. {
  508. if (!obj)
  509. return;
  510. if (i915_gem_obj_is_pinned(obj))
  511. i915_gem_object_ggtt_unpin(obj);
  512. drm_gem_object_unreference(&obj->base);
  513. }
  514. static void guc_client_free(struct drm_device *dev,
  515. struct i915_guc_client *client)
  516. {
  517. struct drm_i915_private *dev_priv = dev->dev_private;
  518. struct intel_guc *guc = &dev_priv->guc;
  519. if (!client)
  520. return;
  521. /*
  522. * XXX: wait for any outstanding submissions before freeing memory.
  523. * Be sure to drop any locks
  524. */
  525. if (client->client_base) {
  526. /*
  527. * If we got as far as setting up a doorbell, make sure
  528. * we shut it down before unmapping & deallocating the
  529. * memory. So first disable the doorbell, then tell the
  530. * GuC that we've finished with it, finally deallocate
  531. * it in our bitmap
  532. */
  533. if (client->doorbell_id != GUC_INVALID_DOORBELL_ID) {
  534. guc_disable_doorbell(guc, client);
  535. host2guc_release_doorbell(guc, client);
  536. release_doorbell(guc, client->doorbell_id);
  537. }
  538. kunmap(kmap_to_page(client->client_base));
  539. }
  540. gem_release_guc_obj(client->client_obj);
  541. if (client->ctx_index != GUC_INVALID_CTX_ID) {
  542. guc_fini_ctx_desc(guc, client);
  543. ida_simple_remove(&guc->ctx_ids, client->ctx_index);
  544. }
  545. kfree(client);
  546. }
  547. /**
  548. * guc_client_alloc() - Allocate an i915_guc_client
  549. * @dev: drm device
  550. * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  551. * The kernel client to replace ExecList submission is created with
  552. * NORMAL priority. Priority of a client for scheduler can be HIGH,
  553. * while a preemption context can use CRITICAL.
  554. * @ctx: the context that owns the client (we use the default render
  555. * context)
  556. *
  557. * Return: An i915_guc_client object if success, else NULL.
  558. */
  559. static struct i915_guc_client *guc_client_alloc(struct drm_device *dev,
  560. uint32_t priority,
  561. struct intel_context *ctx)
  562. {
  563. struct i915_guc_client *client;
  564. struct drm_i915_private *dev_priv = dev->dev_private;
  565. struct intel_guc *guc = &dev_priv->guc;
  566. struct drm_i915_gem_object *obj;
  567. client = kzalloc(sizeof(*client), GFP_KERNEL);
  568. if (!client)
  569. return NULL;
  570. client->doorbell_id = GUC_INVALID_DOORBELL_ID;
  571. client->priority = priority;
  572. client->owner = ctx;
  573. client->guc = guc;
  574. client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
  575. GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
  576. if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
  577. client->ctx_index = GUC_INVALID_CTX_ID;
  578. goto err;
  579. }
  580. /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  581. obj = gem_allocate_guc_obj(dev, GUC_DB_SIZE + GUC_WQ_SIZE);
  582. if (!obj)
  583. goto err;
  584. /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
  585. client->client_obj = obj;
  586. client->client_base = kmap(i915_gem_object_get_page(obj, 0));
  587. client->wq_offset = GUC_DB_SIZE;
  588. client->wq_size = GUC_WQ_SIZE;
  589. client->doorbell_offset = select_doorbell_cacheline(guc);
  590. /*
  591. * Since the doorbell only requires a single cacheline, we can save
  592. * space by putting the application process descriptor in the same
  593. * page. Use the half of the page that doesn't include the doorbell.
  594. */
  595. if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  596. client->proc_desc_offset = 0;
  597. else
  598. client->proc_desc_offset = (GUC_DB_SIZE / 2);
  599. client->doorbell_id = assign_doorbell(guc, client->priority);
  600. if (client->doorbell_id == GUC_INVALID_DOORBELL_ID)
  601. /* XXX: evict a doorbell instead */
  602. goto err;
  603. guc_init_proc_desc(guc, client);
  604. guc_init_ctx_desc(guc, client);
  605. guc_init_doorbell(guc, client);
  606. /* XXX: Any cache flushes needed? General domain mgmt calls? */
  607. if (host2guc_allocate_doorbell(guc, client))
  608. goto err;
  609. DRM_DEBUG_DRIVER("new priority %u client %p: ctx_index %u db_id %u\n",
  610. priority, client, client->ctx_index, client->doorbell_id);
  611. return client;
  612. err:
  613. DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
  614. guc_client_free(dev, client);
  615. return NULL;
  616. }
  617. static void guc_create_log(struct intel_guc *guc)
  618. {
  619. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  620. struct drm_i915_gem_object *obj;
  621. unsigned long offset;
  622. uint32_t size, flags;
  623. if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
  624. return;
  625. if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
  626. i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
  627. /* The first page is to save log buffer state. Allocate one
  628. * extra page for others in case for overlap */
  629. size = (1 + GUC_LOG_DPC_PAGES + 1 +
  630. GUC_LOG_ISR_PAGES + 1 +
  631. GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
  632. obj = guc->log_obj;
  633. if (!obj) {
  634. obj = gem_allocate_guc_obj(dev_priv->dev, size);
  635. if (!obj) {
  636. /* logging will be off */
  637. i915.guc_log_level = -1;
  638. return;
  639. }
  640. guc->log_obj = obj;
  641. }
  642. /* each allocated unit is a page */
  643. flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
  644. (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
  645. (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
  646. (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
  647. offset = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; /* in pages */
  648. guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
  649. }
  650. static void init_guc_policies(struct guc_policies *policies)
  651. {
  652. struct guc_policy *policy;
  653. u32 p, i;
  654. policies->dpc_promote_time = 500000;
  655. policies->max_num_work_items = POLICY_MAX_NUM_WI;
  656. for (p = 0; p < GUC_CTX_PRIORITY_NUM; p++) {
  657. for (i = GUC_RENDER_ENGINE; i < GUC_MAX_ENGINES_NUM; i++) {
  658. policy = &policies->policy[p][i];
  659. policy->execution_quantum = 1000000;
  660. policy->preemption_time = 500000;
  661. policy->fault_time = 250000;
  662. policy->policy_flags = 0;
  663. }
  664. }
  665. policies->is_valid = 1;
  666. }
  667. static void guc_create_ads(struct intel_guc *guc)
  668. {
  669. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  670. struct drm_i915_gem_object *obj;
  671. struct guc_ads *ads;
  672. struct guc_policies *policies;
  673. struct guc_mmio_reg_state *reg_state;
  674. struct intel_engine_cs *engine;
  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. obj = guc->ads_obj;
  682. if (!obj) {
  683. obj = gem_allocate_guc_obj(dev_priv->dev, PAGE_ALIGN(size));
  684. if (!obj)
  685. return;
  686. guc->ads_obj = obj;
  687. }
  688. page = i915_gem_object_get_page(obj, 0);
  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.gfx_addr;
  699. for_each_engine(engine, dev_priv)
  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. init_guc_policies(policies);
  704. ads->scheduler_policies = i915_gem_obj_ggtt_offset(obj) +
  705. sizeof(struct guc_ads);
  706. /* MMIO reg state */
  707. reg_state = (void *)policies + sizeof(struct guc_policies);
  708. for_each_engine(engine, dev_priv) {
  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_device *dev)
  725. {
  726. struct drm_i915_private *dev_priv = dev->dev_private;
  727. const size_t ctxsize = sizeof(struct guc_context_desc);
  728. const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
  729. const size_t gemsize = round_up(poolsize, PAGE_SIZE);
  730. struct intel_guc *guc = &dev_priv->guc;
  731. if (!i915.enable_guc_submission)
  732. return 0; /* not enabled */
  733. if (guc->ctx_pool_obj)
  734. return 0; /* already allocated */
  735. guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv->dev, gemsize);
  736. if (!guc->ctx_pool_obj)
  737. return -ENOMEM;
  738. ida_init(&guc->ctx_ids);
  739. guc_create_log(guc);
  740. guc_create_ads(guc);
  741. return 0;
  742. }
  743. int i915_guc_submission_enable(struct drm_device *dev)
  744. {
  745. struct drm_i915_private *dev_priv = dev->dev_private;
  746. struct intel_guc *guc = &dev_priv->guc;
  747. struct intel_context *ctx = dev_priv->kernel_context;
  748. struct i915_guc_client *client;
  749. /* client for execbuf submission */
  750. client = guc_client_alloc(dev, GUC_CTX_PRIORITY_KMD_NORMAL, ctx);
  751. if (!client) {
  752. DRM_ERROR("Failed to create execbuf guc_client\n");
  753. return -ENOMEM;
  754. }
  755. guc->execbuf_client = client;
  756. host2guc_sample_forcewake(guc, client);
  757. return 0;
  758. }
  759. void i915_guc_submission_disable(struct drm_device *dev)
  760. {
  761. struct drm_i915_private *dev_priv = dev->dev_private;
  762. struct intel_guc *guc = &dev_priv->guc;
  763. guc_client_free(dev, guc->execbuf_client);
  764. guc->execbuf_client = NULL;
  765. }
  766. void i915_guc_submission_fini(struct drm_device *dev)
  767. {
  768. struct drm_i915_private *dev_priv = dev->dev_private;
  769. struct intel_guc *guc = &dev_priv->guc;
  770. gem_release_guc_obj(dev_priv->guc.ads_obj);
  771. guc->ads_obj = NULL;
  772. gem_release_guc_obj(dev_priv->guc.log_obj);
  773. guc->log_obj = NULL;
  774. if (guc->ctx_pool_obj)
  775. ida_destroy(&guc->ctx_ids);
  776. gem_release_guc_obj(guc->ctx_pool_obj);
  777. guc->ctx_pool_obj = NULL;
  778. }
  779. /**
  780. * intel_guc_suspend() - notify GuC entering suspend state
  781. * @dev: drm device
  782. */
  783. int intel_guc_suspend(struct drm_device *dev)
  784. {
  785. struct drm_i915_private *dev_priv = dev->dev_private;
  786. struct intel_guc *guc = &dev_priv->guc;
  787. struct intel_context *ctx;
  788. u32 data[3];
  789. if (!i915.enable_guc_submission)
  790. return 0;
  791. ctx = dev_priv->kernel_context;
  792. data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
  793. /* any value greater than GUC_POWER_D0 */
  794. data[1] = GUC_POWER_D1;
  795. /* first page is shared data with GuC */
  796. data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
  797. return host2guc_action(guc, data, ARRAY_SIZE(data));
  798. }
  799. /**
  800. * intel_guc_resume() - notify GuC resuming from suspend state
  801. * @dev: drm device
  802. */
  803. int intel_guc_resume(struct drm_device *dev)
  804. {
  805. struct drm_i915_private *dev_priv = dev->dev_private;
  806. struct intel_guc *guc = &dev_priv->guc;
  807. struct intel_context *ctx;
  808. u32 data[3];
  809. if (!i915.enable_guc_submission)
  810. return 0;
  811. ctx = dev_priv->kernel_context;
  812. data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
  813. data[1] = GUC_POWER_D0;
  814. /* first page is shared data with GuC */
  815. data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
  816. return host2guc_action(guc, data, ARRAY_SIZE(data));
  817. }