intel_ringbuffer.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. /*
  2. * Copyright © 2008-2010 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. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Zou Nan hai <nanhai.zou@intel.com>
  26. * Xiang Hai hao<haihao.xiang@intel.com>
  27. *
  28. */
  29. #include "drmP.h"
  30. #include "drm.h"
  31. #include "i915_drv.h"
  32. #include "i915_drm.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. /*
  36. * 965+ support PIPE_CONTROL commands, which provide finer grained control
  37. * over cache flushing.
  38. */
  39. struct pipe_control {
  40. struct drm_i915_gem_object *obj;
  41. volatile u32 *cpu_page;
  42. u32 gtt_offset;
  43. };
  44. static inline int ring_space(struct intel_ring_buffer *ring)
  45. {
  46. int space = (ring->head & HEAD_ADDR) - (ring->tail + 8);
  47. if (space < 0)
  48. space += ring->size;
  49. return space;
  50. }
  51. static int
  52. render_ring_flush(struct intel_ring_buffer *ring,
  53. u32 invalidate_domains,
  54. u32 flush_domains)
  55. {
  56. struct drm_device *dev = ring->dev;
  57. u32 cmd;
  58. int ret;
  59. /*
  60. * read/write caches:
  61. *
  62. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  63. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  64. * also flushed at 2d versus 3d pipeline switches.
  65. *
  66. * read-only caches:
  67. *
  68. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  69. * MI_READ_FLUSH is set, and is always flushed on 965.
  70. *
  71. * I915_GEM_DOMAIN_COMMAND may not exist?
  72. *
  73. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  74. * invalidated when MI_EXE_FLUSH is set.
  75. *
  76. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  77. * invalidated with every MI_FLUSH.
  78. *
  79. * TLBs:
  80. *
  81. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  82. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  83. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  84. * are flushed at any MI_FLUSH.
  85. */
  86. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  87. if ((invalidate_domains|flush_domains) &
  88. I915_GEM_DOMAIN_RENDER)
  89. cmd &= ~MI_NO_WRITE_FLUSH;
  90. if (INTEL_INFO(dev)->gen < 4) {
  91. /*
  92. * On the 965, the sampler cache always gets flushed
  93. * and this bit is reserved.
  94. */
  95. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  96. cmd |= MI_READ_FLUSH;
  97. }
  98. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  99. cmd |= MI_EXE_FLUSH;
  100. if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
  101. (IS_G4X(dev) || IS_GEN5(dev)))
  102. cmd |= MI_INVALIDATE_ISP;
  103. ret = intel_ring_begin(ring, 2);
  104. if (ret)
  105. return ret;
  106. intel_ring_emit(ring, cmd);
  107. intel_ring_emit(ring, MI_NOOP);
  108. intel_ring_advance(ring);
  109. return 0;
  110. }
  111. /**
  112. * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
  113. * implementing two workarounds on gen6. From section 1.4.7.1
  114. * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
  115. *
  116. * [DevSNB-C+{W/A}] Before any depth stall flush (including those
  117. * produced by non-pipelined state commands), software needs to first
  118. * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
  119. * 0.
  120. *
  121. * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
  122. * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
  123. *
  124. * And the workaround for these two requires this workaround first:
  125. *
  126. * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
  127. * BEFORE the pipe-control with a post-sync op and no write-cache
  128. * flushes.
  129. *
  130. * And this last workaround is tricky because of the requirements on
  131. * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
  132. * volume 2 part 1:
  133. *
  134. * "1 of the following must also be set:
  135. * - Render Target Cache Flush Enable ([12] of DW1)
  136. * - Depth Cache Flush Enable ([0] of DW1)
  137. * - Stall at Pixel Scoreboard ([1] of DW1)
  138. * - Depth Stall ([13] of DW1)
  139. * - Post-Sync Operation ([13] of DW1)
  140. * - Notify Enable ([8] of DW1)"
  141. *
  142. * The cache flushes require the workaround flush that triggered this
  143. * one, so we can't use it. Depth stall would trigger the same.
  144. * Post-sync nonzero is what triggered this second workaround, so we
  145. * can't use that one either. Notify enable is IRQs, which aren't
  146. * really our business. That leaves only stall at scoreboard.
  147. */
  148. static int
  149. intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
  150. {
  151. struct pipe_control *pc = ring->private;
  152. u32 scratch_addr = pc->gtt_offset + 128;
  153. int ret;
  154. ret = intel_ring_begin(ring, 6);
  155. if (ret)
  156. return ret;
  157. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
  158. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
  159. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  160. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  161. intel_ring_emit(ring, 0); /* low dword */
  162. intel_ring_emit(ring, 0); /* high dword */
  163. intel_ring_emit(ring, MI_NOOP);
  164. intel_ring_advance(ring);
  165. ret = intel_ring_begin(ring, 6);
  166. if (ret)
  167. return ret;
  168. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
  169. intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
  170. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  171. intel_ring_emit(ring, 0);
  172. intel_ring_emit(ring, 0);
  173. intel_ring_emit(ring, MI_NOOP);
  174. intel_ring_advance(ring);
  175. return 0;
  176. }
  177. static int
  178. gen6_render_ring_flush(struct intel_ring_buffer *ring,
  179. u32 invalidate_domains, u32 flush_domains)
  180. {
  181. u32 flags = 0;
  182. struct pipe_control *pc = ring->private;
  183. u32 scratch_addr = pc->gtt_offset + 128;
  184. int ret;
  185. /* Force SNB workarounds for PIPE_CONTROL flushes */
  186. intel_emit_post_sync_nonzero_flush(ring);
  187. /* Just flush everything. Experiments have shown that reducing the
  188. * number of bits based on the write domains has little performance
  189. * impact.
  190. */
  191. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  192. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  193. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  194. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  195. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  196. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  197. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  198. ret = intel_ring_begin(ring, 6);
  199. if (ret)
  200. return ret;
  201. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
  202. intel_ring_emit(ring, flags);
  203. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
  204. intel_ring_emit(ring, 0); /* lower dword */
  205. intel_ring_emit(ring, 0); /* uppwer dword */
  206. intel_ring_emit(ring, MI_NOOP);
  207. intel_ring_advance(ring);
  208. return 0;
  209. }
  210. static void ring_write_tail(struct intel_ring_buffer *ring,
  211. u32 value)
  212. {
  213. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  214. I915_WRITE_TAIL(ring, value);
  215. }
  216. u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
  217. {
  218. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  219. u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
  220. RING_ACTHD(ring->mmio_base) : ACTHD;
  221. return I915_READ(acthd_reg);
  222. }
  223. static int init_ring_common(struct intel_ring_buffer *ring)
  224. {
  225. struct drm_device *dev = ring->dev;
  226. drm_i915_private_t *dev_priv = dev->dev_private;
  227. struct drm_i915_gem_object *obj = ring->obj;
  228. int ret = 0;
  229. u32 head;
  230. if (HAS_FORCE_WAKE(dev))
  231. gen6_gt_force_wake_get(dev_priv);
  232. /* Stop the ring if it's running. */
  233. I915_WRITE_CTL(ring, 0);
  234. I915_WRITE_HEAD(ring, 0);
  235. ring->write_tail(ring, 0);
  236. /* Initialize the ring. */
  237. I915_WRITE_START(ring, obj->gtt_offset);
  238. head = I915_READ_HEAD(ring) & HEAD_ADDR;
  239. /* G45 ring initialization fails to reset head to zero */
  240. if (head != 0) {
  241. DRM_DEBUG_KMS("%s head not reset to zero "
  242. "ctl %08x head %08x tail %08x start %08x\n",
  243. ring->name,
  244. I915_READ_CTL(ring),
  245. I915_READ_HEAD(ring),
  246. I915_READ_TAIL(ring),
  247. I915_READ_START(ring));
  248. I915_WRITE_HEAD(ring, 0);
  249. if (I915_READ_HEAD(ring) & HEAD_ADDR) {
  250. DRM_ERROR("failed to set %s head to zero "
  251. "ctl %08x head %08x tail %08x start %08x\n",
  252. ring->name,
  253. I915_READ_CTL(ring),
  254. I915_READ_HEAD(ring),
  255. I915_READ_TAIL(ring),
  256. I915_READ_START(ring));
  257. }
  258. }
  259. I915_WRITE_CTL(ring,
  260. ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
  261. | RING_VALID);
  262. /* If the head is still not zero, the ring is dead */
  263. if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
  264. I915_READ_START(ring) == obj->gtt_offset &&
  265. (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
  266. DRM_ERROR("%s initialization failed "
  267. "ctl %08x head %08x tail %08x start %08x\n",
  268. ring->name,
  269. I915_READ_CTL(ring),
  270. I915_READ_HEAD(ring),
  271. I915_READ_TAIL(ring),
  272. I915_READ_START(ring));
  273. ret = -EIO;
  274. goto out;
  275. }
  276. if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
  277. i915_kernel_lost_context(ring->dev);
  278. else {
  279. ring->head = I915_READ_HEAD(ring);
  280. ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
  281. ring->space = ring_space(ring);
  282. ring->last_retired_head = -1;
  283. }
  284. out:
  285. if (HAS_FORCE_WAKE(dev))
  286. gen6_gt_force_wake_put(dev_priv);
  287. return ret;
  288. }
  289. static int
  290. init_pipe_control(struct intel_ring_buffer *ring)
  291. {
  292. struct pipe_control *pc;
  293. struct drm_i915_gem_object *obj;
  294. int ret;
  295. if (ring->private)
  296. return 0;
  297. pc = kmalloc(sizeof(*pc), GFP_KERNEL);
  298. if (!pc)
  299. return -ENOMEM;
  300. obj = i915_gem_alloc_object(ring->dev, 4096);
  301. if (obj == NULL) {
  302. DRM_ERROR("Failed to allocate seqno page\n");
  303. ret = -ENOMEM;
  304. goto err;
  305. }
  306. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  307. ret = i915_gem_object_pin(obj, 4096, true);
  308. if (ret)
  309. goto err_unref;
  310. pc->gtt_offset = obj->gtt_offset;
  311. pc->cpu_page = kmap(obj->pages[0]);
  312. if (pc->cpu_page == NULL)
  313. goto err_unpin;
  314. pc->obj = obj;
  315. ring->private = pc;
  316. return 0;
  317. err_unpin:
  318. i915_gem_object_unpin(obj);
  319. err_unref:
  320. drm_gem_object_unreference(&obj->base);
  321. err:
  322. kfree(pc);
  323. return ret;
  324. }
  325. static void
  326. cleanup_pipe_control(struct intel_ring_buffer *ring)
  327. {
  328. struct pipe_control *pc = ring->private;
  329. struct drm_i915_gem_object *obj;
  330. if (!ring->private)
  331. return;
  332. obj = pc->obj;
  333. kunmap(obj->pages[0]);
  334. i915_gem_object_unpin(obj);
  335. drm_gem_object_unreference(&obj->base);
  336. kfree(pc);
  337. ring->private = NULL;
  338. }
  339. static int init_render_ring(struct intel_ring_buffer *ring)
  340. {
  341. struct drm_device *dev = ring->dev;
  342. struct drm_i915_private *dev_priv = dev->dev_private;
  343. int ret = init_ring_common(ring);
  344. if (INTEL_INFO(dev)->gen > 3) {
  345. int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
  346. I915_WRITE(MI_MODE, mode);
  347. if (IS_GEN7(dev))
  348. I915_WRITE(GFX_MODE_GEN7,
  349. GFX_MODE_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
  350. GFX_MODE_ENABLE(GFX_REPLAY_MODE));
  351. }
  352. if (INTEL_INFO(dev)->gen >= 5) {
  353. ret = init_pipe_control(ring);
  354. if (ret)
  355. return ret;
  356. }
  357. if (IS_GEN6(dev)) {
  358. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  359. * "If this bit is set, STCunit will have LRA as replacement
  360. * policy. [...] This bit must be reset. LRA replacement
  361. * policy is not supported."
  362. */
  363. I915_WRITE(CACHE_MODE_0,
  364. CM0_STC_EVICT_DISABLE_LRA_SNB << CM0_MASK_SHIFT);
  365. }
  366. if (INTEL_INFO(dev)->gen >= 6) {
  367. I915_WRITE(INSTPM,
  368. INSTPM_FORCE_ORDERING << 16 | INSTPM_FORCE_ORDERING);
  369. }
  370. return ret;
  371. }
  372. static void render_ring_cleanup(struct intel_ring_buffer *ring)
  373. {
  374. if (!ring->private)
  375. return;
  376. cleanup_pipe_control(ring);
  377. }
  378. static void
  379. update_mboxes(struct intel_ring_buffer *ring,
  380. u32 seqno,
  381. u32 mmio_offset)
  382. {
  383. intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
  384. MI_SEMAPHORE_GLOBAL_GTT |
  385. MI_SEMAPHORE_REGISTER |
  386. MI_SEMAPHORE_UPDATE);
  387. intel_ring_emit(ring, seqno);
  388. intel_ring_emit(ring, mmio_offset);
  389. }
  390. /**
  391. * gen6_add_request - Update the semaphore mailbox registers
  392. *
  393. * @ring - ring that is adding a request
  394. * @seqno - return seqno stuck into the ring
  395. *
  396. * Update the mailbox registers in the *other* rings with the current seqno.
  397. * This acts like a signal in the canonical semaphore.
  398. */
  399. static int
  400. gen6_add_request(struct intel_ring_buffer *ring,
  401. u32 *seqno)
  402. {
  403. u32 mbox1_reg;
  404. u32 mbox2_reg;
  405. int ret;
  406. ret = intel_ring_begin(ring, 10);
  407. if (ret)
  408. return ret;
  409. mbox1_reg = ring->signal_mbox[0];
  410. mbox2_reg = ring->signal_mbox[1];
  411. *seqno = i915_gem_next_request_seqno(ring);
  412. update_mboxes(ring, *seqno, mbox1_reg);
  413. update_mboxes(ring, *seqno, mbox2_reg);
  414. intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
  415. intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  416. intel_ring_emit(ring, *seqno);
  417. intel_ring_emit(ring, MI_USER_INTERRUPT);
  418. intel_ring_advance(ring);
  419. return 0;
  420. }
  421. /**
  422. * intel_ring_sync - sync the waiter to the signaller on seqno
  423. *
  424. * @waiter - ring that is waiting
  425. * @signaller - ring which has, or will signal
  426. * @seqno - seqno which the waiter will block on
  427. */
  428. static int
  429. intel_ring_sync(struct intel_ring_buffer *waiter,
  430. struct intel_ring_buffer *signaller,
  431. int ring,
  432. u32 seqno)
  433. {
  434. int ret;
  435. u32 dw1 = MI_SEMAPHORE_MBOX |
  436. MI_SEMAPHORE_COMPARE |
  437. MI_SEMAPHORE_REGISTER;
  438. ret = intel_ring_begin(waiter, 4);
  439. if (ret)
  440. return ret;
  441. intel_ring_emit(waiter, dw1 | signaller->semaphore_register[ring]);
  442. intel_ring_emit(waiter, seqno);
  443. intel_ring_emit(waiter, 0);
  444. intel_ring_emit(waiter, MI_NOOP);
  445. intel_ring_advance(waiter);
  446. return 0;
  447. }
  448. /* VCS->RCS (RVSYNC) or BCS->RCS (RBSYNC) */
  449. int
  450. render_ring_sync_to(struct intel_ring_buffer *waiter,
  451. struct intel_ring_buffer *signaller,
  452. u32 seqno)
  453. {
  454. WARN_ON(signaller->semaphore_register[RCS] == MI_SEMAPHORE_SYNC_INVALID);
  455. return intel_ring_sync(waiter,
  456. signaller,
  457. RCS,
  458. seqno);
  459. }
  460. /* RCS->VCS (VRSYNC) or BCS->VCS (VBSYNC) */
  461. int
  462. gen6_bsd_ring_sync_to(struct intel_ring_buffer *waiter,
  463. struct intel_ring_buffer *signaller,
  464. u32 seqno)
  465. {
  466. WARN_ON(signaller->semaphore_register[VCS] == MI_SEMAPHORE_SYNC_INVALID);
  467. return intel_ring_sync(waiter,
  468. signaller,
  469. VCS,
  470. seqno);
  471. }
  472. /* RCS->BCS (BRSYNC) or VCS->BCS (BVSYNC) */
  473. int
  474. gen6_blt_ring_sync_to(struct intel_ring_buffer *waiter,
  475. struct intel_ring_buffer *signaller,
  476. u32 seqno)
  477. {
  478. WARN_ON(signaller->semaphore_register[BCS] == MI_SEMAPHORE_SYNC_INVALID);
  479. return intel_ring_sync(waiter,
  480. signaller,
  481. BCS,
  482. seqno);
  483. }
  484. #define PIPE_CONTROL_FLUSH(ring__, addr__) \
  485. do { \
  486. intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
  487. PIPE_CONTROL_DEPTH_STALL); \
  488. intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
  489. intel_ring_emit(ring__, 0); \
  490. intel_ring_emit(ring__, 0); \
  491. } while (0)
  492. static int
  493. pc_render_add_request(struct intel_ring_buffer *ring,
  494. u32 *result)
  495. {
  496. u32 seqno = i915_gem_next_request_seqno(ring);
  497. struct pipe_control *pc = ring->private;
  498. u32 scratch_addr = pc->gtt_offset + 128;
  499. int ret;
  500. /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
  501. * incoherent with writes to memory, i.e. completely fubar,
  502. * so we need to use PIPE_NOTIFY instead.
  503. *
  504. * However, we also need to workaround the qword write
  505. * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
  506. * memory before requesting an interrupt.
  507. */
  508. ret = intel_ring_begin(ring, 32);
  509. if (ret)
  510. return ret;
  511. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
  512. PIPE_CONTROL_WRITE_FLUSH |
  513. PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
  514. intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
  515. intel_ring_emit(ring, seqno);
  516. intel_ring_emit(ring, 0);
  517. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  518. scratch_addr += 128; /* write to separate cachelines */
  519. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  520. scratch_addr += 128;
  521. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  522. scratch_addr += 128;
  523. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  524. scratch_addr += 128;
  525. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  526. scratch_addr += 128;
  527. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  528. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
  529. PIPE_CONTROL_WRITE_FLUSH |
  530. PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
  531. PIPE_CONTROL_NOTIFY);
  532. intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
  533. intel_ring_emit(ring, seqno);
  534. intel_ring_emit(ring, 0);
  535. intel_ring_advance(ring);
  536. *result = seqno;
  537. return 0;
  538. }
  539. static int
  540. render_ring_add_request(struct intel_ring_buffer *ring,
  541. u32 *result)
  542. {
  543. u32 seqno = i915_gem_next_request_seqno(ring);
  544. int ret;
  545. ret = intel_ring_begin(ring, 4);
  546. if (ret)
  547. return ret;
  548. intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
  549. intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  550. intel_ring_emit(ring, seqno);
  551. intel_ring_emit(ring, MI_USER_INTERRUPT);
  552. intel_ring_advance(ring);
  553. *result = seqno;
  554. return 0;
  555. }
  556. static u32
  557. gen6_ring_get_seqno(struct intel_ring_buffer *ring)
  558. {
  559. struct drm_device *dev = ring->dev;
  560. /* Workaround to force correct ordering between irq and seqno writes on
  561. * ivb (and maybe also on snb) by reading from a CS register (like
  562. * ACTHD) before reading the status page. */
  563. if (IS_GEN6(dev) || IS_GEN7(dev))
  564. intel_ring_get_active_head(ring);
  565. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  566. }
  567. static u32
  568. ring_get_seqno(struct intel_ring_buffer *ring)
  569. {
  570. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  571. }
  572. static u32
  573. pc_render_get_seqno(struct intel_ring_buffer *ring)
  574. {
  575. struct pipe_control *pc = ring->private;
  576. return pc->cpu_page[0];
  577. }
  578. static void
  579. ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
  580. {
  581. dev_priv->gt_irq_mask &= ~mask;
  582. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  583. POSTING_READ(GTIMR);
  584. }
  585. static void
  586. ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
  587. {
  588. dev_priv->gt_irq_mask |= mask;
  589. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  590. POSTING_READ(GTIMR);
  591. }
  592. static void
  593. i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
  594. {
  595. dev_priv->irq_mask &= ~mask;
  596. I915_WRITE(IMR, dev_priv->irq_mask);
  597. POSTING_READ(IMR);
  598. }
  599. static void
  600. i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
  601. {
  602. dev_priv->irq_mask |= mask;
  603. I915_WRITE(IMR, dev_priv->irq_mask);
  604. POSTING_READ(IMR);
  605. }
  606. static bool
  607. render_ring_get_irq(struct intel_ring_buffer *ring)
  608. {
  609. struct drm_device *dev = ring->dev;
  610. drm_i915_private_t *dev_priv = dev->dev_private;
  611. if (!dev->irq_enabled)
  612. return false;
  613. spin_lock(&ring->irq_lock);
  614. if (ring->irq_refcount++ == 0) {
  615. if (HAS_PCH_SPLIT(dev))
  616. ironlake_enable_irq(dev_priv,
  617. GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
  618. else
  619. i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
  620. }
  621. spin_unlock(&ring->irq_lock);
  622. return true;
  623. }
  624. static void
  625. render_ring_put_irq(struct intel_ring_buffer *ring)
  626. {
  627. struct drm_device *dev = ring->dev;
  628. drm_i915_private_t *dev_priv = dev->dev_private;
  629. spin_lock(&ring->irq_lock);
  630. if (--ring->irq_refcount == 0) {
  631. if (HAS_PCH_SPLIT(dev))
  632. ironlake_disable_irq(dev_priv,
  633. GT_USER_INTERRUPT |
  634. GT_PIPE_NOTIFY);
  635. else
  636. i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
  637. }
  638. spin_unlock(&ring->irq_lock);
  639. }
  640. void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
  641. {
  642. struct drm_device *dev = ring->dev;
  643. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  644. u32 mmio = 0;
  645. /* The ring status page addresses are no longer next to the rest of
  646. * the ring registers as of gen7.
  647. */
  648. if (IS_GEN7(dev)) {
  649. switch (ring->id) {
  650. case RCS:
  651. mmio = RENDER_HWS_PGA_GEN7;
  652. break;
  653. case BCS:
  654. mmio = BLT_HWS_PGA_GEN7;
  655. break;
  656. case VCS:
  657. mmio = BSD_HWS_PGA_GEN7;
  658. break;
  659. }
  660. } else if (IS_GEN6(ring->dev)) {
  661. mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
  662. } else {
  663. mmio = RING_HWS_PGA(ring->mmio_base);
  664. }
  665. I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
  666. POSTING_READ(mmio);
  667. /* Flush the TLB for this page */
  668. if (INTEL_INFO(dev)->gen >= 6) {
  669. u32 reg = RING_INSTPM(ring->mmio_base);
  670. I915_WRITE(reg,
  671. _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
  672. INSTPM_SYNC_FLUSH));
  673. if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
  674. 1000))
  675. DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
  676. ring->name);
  677. }
  678. }
  679. static int
  680. bsd_ring_flush(struct intel_ring_buffer *ring,
  681. u32 invalidate_domains,
  682. u32 flush_domains)
  683. {
  684. int ret;
  685. ret = intel_ring_begin(ring, 2);
  686. if (ret)
  687. return ret;
  688. intel_ring_emit(ring, MI_FLUSH);
  689. intel_ring_emit(ring, MI_NOOP);
  690. intel_ring_advance(ring);
  691. return 0;
  692. }
  693. static int
  694. ring_add_request(struct intel_ring_buffer *ring,
  695. u32 *result)
  696. {
  697. u32 seqno;
  698. int ret;
  699. ret = intel_ring_begin(ring, 4);
  700. if (ret)
  701. return ret;
  702. seqno = i915_gem_next_request_seqno(ring);
  703. intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
  704. intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  705. intel_ring_emit(ring, seqno);
  706. intel_ring_emit(ring, MI_USER_INTERRUPT);
  707. intel_ring_advance(ring);
  708. *result = seqno;
  709. return 0;
  710. }
  711. static bool
  712. gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
  713. {
  714. struct drm_device *dev = ring->dev;
  715. drm_i915_private_t *dev_priv = dev->dev_private;
  716. if (!dev->irq_enabled)
  717. return false;
  718. /* It looks like we need to prevent the gt from suspending while waiting
  719. * for an notifiy irq, otherwise irqs seem to get lost on at least the
  720. * blt/bsd rings on ivb. */
  721. gen6_gt_force_wake_get(dev_priv);
  722. spin_lock(&ring->irq_lock);
  723. if (ring->irq_refcount++ == 0) {
  724. ring->irq_mask &= ~rflag;
  725. I915_WRITE_IMR(ring, ring->irq_mask);
  726. ironlake_enable_irq(dev_priv, gflag);
  727. }
  728. spin_unlock(&ring->irq_lock);
  729. return true;
  730. }
  731. static void
  732. gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
  733. {
  734. struct drm_device *dev = ring->dev;
  735. drm_i915_private_t *dev_priv = dev->dev_private;
  736. spin_lock(&ring->irq_lock);
  737. if (--ring->irq_refcount == 0) {
  738. ring->irq_mask |= rflag;
  739. I915_WRITE_IMR(ring, ring->irq_mask);
  740. ironlake_disable_irq(dev_priv, gflag);
  741. }
  742. spin_unlock(&ring->irq_lock);
  743. gen6_gt_force_wake_put(dev_priv);
  744. }
  745. static bool
  746. bsd_ring_get_irq(struct intel_ring_buffer *ring)
  747. {
  748. struct drm_device *dev = ring->dev;
  749. drm_i915_private_t *dev_priv = dev->dev_private;
  750. if (!dev->irq_enabled)
  751. return false;
  752. spin_lock(&ring->irq_lock);
  753. if (ring->irq_refcount++ == 0) {
  754. if (IS_G4X(dev))
  755. i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
  756. else
  757. ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
  758. }
  759. spin_unlock(&ring->irq_lock);
  760. return true;
  761. }
  762. static void
  763. bsd_ring_put_irq(struct intel_ring_buffer *ring)
  764. {
  765. struct drm_device *dev = ring->dev;
  766. drm_i915_private_t *dev_priv = dev->dev_private;
  767. spin_lock(&ring->irq_lock);
  768. if (--ring->irq_refcount == 0) {
  769. if (IS_G4X(dev))
  770. i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
  771. else
  772. ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
  773. }
  774. spin_unlock(&ring->irq_lock);
  775. }
  776. static int
  777. ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
  778. {
  779. int ret;
  780. ret = intel_ring_begin(ring, 2);
  781. if (ret)
  782. return ret;
  783. intel_ring_emit(ring,
  784. MI_BATCH_BUFFER_START | (2 << 6) |
  785. MI_BATCH_NON_SECURE_I965);
  786. intel_ring_emit(ring, offset);
  787. intel_ring_advance(ring);
  788. return 0;
  789. }
  790. static int
  791. render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  792. u32 offset, u32 len)
  793. {
  794. struct drm_device *dev = ring->dev;
  795. int ret;
  796. if (IS_I830(dev) || IS_845G(dev)) {
  797. ret = intel_ring_begin(ring, 4);
  798. if (ret)
  799. return ret;
  800. intel_ring_emit(ring, MI_BATCH_BUFFER);
  801. intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
  802. intel_ring_emit(ring, offset + len - 8);
  803. intel_ring_emit(ring, 0);
  804. } else {
  805. ret = intel_ring_begin(ring, 2);
  806. if (ret)
  807. return ret;
  808. if (INTEL_INFO(dev)->gen >= 4) {
  809. intel_ring_emit(ring,
  810. MI_BATCH_BUFFER_START | (2 << 6) |
  811. MI_BATCH_NON_SECURE_I965);
  812. intel_ring_emit(ring, offset);
  813. } else {
  814. intel_ring_emit(ring,
  815. MI_BATCH_BUFFER_START | (2 << 6));
  816. intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
  817. }
  818. }
  819. intel_ring_advance(ring);
  820. return 0;
  821. }
  822. static void cleanup_status_page(struct intel_ring_buffer *ring)
  823. {
  824. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  825. struct drm_i915_gem_object *obj;
  826. obj = ring->status_page.obj;
  827. if (obj == NULL)
  828. return;
  829. kunmap(obj->pages[0]);
  830. i915_gem_object_unpin(obj);
  831. drm_gem_object_unreference(&obj->base);
  832. ring->status_page.obj = NULL;
  833. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  834. }
  835. static int init_status_page(struct intel_ring_buffer *ring)
  836. {
  837. struct drm_device *dev = ring->dev;
  838. drm_i915_private_t *dev_priv = dev->dev_private;
  839. struct drm_i915_gem_object *obj;
  840. int ret;
  841. obj = i915_gem_alloc_object(dev, 4096);
  842. if (obj == NULL) {
  843. DRM_ERROR("Failed to allocate status page\n");
  844. ret = -ENOMEM;
  845. goto err;
  846. }
  847. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  848. ret = i915_gem_object_pin(obj, 4096, true);
  849. if (ret != 0) {
  850. goto err_unref;
  851. }
  852. ring->status_page.gfx_addr = obj->gtt_offset;
  853. ring->status_page.page_addr = kmap(obj->pages[0]);
  854. if (ring->status_page.page_addr == NULL) {
  855. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  856. goto err_unpin;
  857. }
  858. ring->status_page.obj = obj;
  859. memset(ring->status_page.page_addr, 0, PAGE_SIZE);
  860. intel_ring_setup_status_page(ring);
  861. DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
  862. ring->name, ring->status_page.gfx_addr);
  863. return 0;
  864. err_unpin:
  865. i915_gem_object_unpin(obj);
  866. err_unref:
  867. drm_gem_object_unreference(&obj->base);
  868. err:
  869. return ret;
  870. }
  871. int intel_init_ring_buffer(struct drm_device *dev,
  872. struct intel_ring_buffer *ring)
  873. {
  874. struct drm_i915_gem_object *obj;
  875. int ret;
  876. ring->dev = dev;
  877. INIT_LIST_HEAD(&ring->active_list);
  878. INIT_LIST_HEAD(&ring->request_list);
  879. INIT_LIST_HEAD(&ring->gpu_write_list);
  880. init_waitqueue_head(&ring->irq_queue);
  881. spin_lock_init(&ring->irq_lock);
  882. ring->irq_mask = ~0;
  883. if (I915_NEED_GFX_HWS(dev)) {
  884. ret = init_status_page(ring);
  885. if (ret)
  886. return ret;
  887. }
  888. obj = i915_gem_alloc_object(dev, ring->size);
  889. if (obj == NULL) {
  890. DRM_ERROR("Failed to allocate ringbuffer\n");
  891. ret = -ENOMEM;
  892. goto err_hws;
  893. }
  894. ring->obj = obj;
  895. ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
  896. if (ret)
  897. goto err_unref;
  898. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  899. if (ret)
  900. goto err_unpin;
  901. ring->map.size = ring->size;
  902. ring->map.offset = dev->agp->base + obj->gtt_offset;
  903. ring->map.type = 0;
  904. ring->map.flags = 0;
  905. ring->map.mtrr = 0;
  906. drm_core_ioremap_wc(&ring->map, dev);
  907. if (ring->map.handle == NULL) {
  908. DRM_ERROR("Failed to map ringbuffer.\n");
  909. ret = -EINVAL;
  910. goto err_unpin;
  911. }
  912. ring->virtual_start = ring->map.handle;
  913. ret = ring->init(ring);
  914. if (ret)
  915. goto err_unmap;
  916. /* Workaround an erratum on the i830 which causes a hang if
  917. * the TAIL pointer points to within the last 2 cachelines
  918. * of the buffer.
  919. */
  920. ring->effective_size = ring->size;
  921. if (IS_I830(ring->dev) || IS_845G(ring->dev))
  922. ring->effective_size -= 128;
  923. return 0;
  924. err_unmap:
  925. drm_core_ioremapfree(&ring->map, dev);
  926. err_unpin:
  927. i915_gem_object_unpin(obj);
  928. err_unref:
  929. drm_gem_object_unreference(&obj->base);
  930. ring->obj = NULL;
  931. err_hws:
  932. cleanup_status_page(ring);
  933. return ret;
  934. }
  935. void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
  936. {
  937. struct drm_i915_private *dev_priv;
  938. int ret;
  939. if (ring->obj == NULL)
  940. return;
  941. /* Disable the ring buffer. The ring must be idle at this point */
  942. dev_priv = ring->dev->dev_private;
  943. ret = intel_wait_ring_idle(ring);
  944. if (ret)
  945. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  946. ring->name, ret);
  947. I915_WRITE_CTL(ring, 0);
  948. drm_core_ioremapfree(&ring->map, ring->dev);
  949. i915_gem_object_unpin(ring->obj);
  950. drm_gem_object_unreference(&ring->obj->base);
  951. ring->obj = NULL;
  952. if (ring->cleanup)
  953. ring->cleanup(ring);
  954. cleanup_status_page(ring);
  955. }
  956. static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
  957. {
  958. unsigned int *virt;
  959. int rem = ring->size - ring->tail;
  960. if (ring->space < rem) {
  961. int ret = intel_wait_ring_buffer(ring, rem);
  962. if (ret)
  963. return ret;
  964. }
  965. virt = (unsigned int *)(ring->virtual_start + ring->tail);
  966. rem /= 8;
  967. while (rem--) {
  968. *virt++ = MI_NOOP;
  969. *virt++ = MI_NOOP;
  970. }
  971. ring->tail = 0;
  972. ring->space = ring_space(ring);
  973. return 0;
  974. }
  975. static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
  976. {
  977. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  978. bool was_interruptible;
  979. int ret;
  980. /* XXX As we have not yet audited all the paths to check that
  981. * they are ready for ERESTARTSYS from intel_ring_begin, do not
  982. * allow us to be interruptible by a signal.
  983. */
  984. was_interruptible = dev_priv->mm.interruptible;
  985. dev_priv->mm.interruptible = false;
  986. ret = i915_wait_request(ring, seqno, true);
  987. dev_priv->mm.interruptible = was_interruptible;
  988. return ret;
  989. }
  990. static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
  991. {
  992. struct drm_i915_gem_request *request;
  993. u32 seqno = 0;
  994. int ret;
  995. i915_gem_retire_requests_ring(ring);
  996. if (ring->last_retired_head != -1) {
  997. ring->head = ring->last_retired_head;
  998. ring->last_retired_head = -1;
  999. ring->space = ring_space(ring);
  1000. if (ring->space >= n)
  1001. return 0;
  1002. }
  1003. list_for_each_entry(request, &ring->request_list, list) {
  1004. int space;
  1005. if (request->tail == -1)
  1006. continue;
  1007. space = request->tail - (ring->tail + 8);
  1008. if (space < 0)
  1009. space += ring->size;
  1010. if (space >= n) {
  1011. seqno = request->seqno;
  1012. break;
  1013. }
  1014. /* Consume this request in case we need more space than
  1015. * is available and so need to prevent a race between
  1016. * updating last_retired_head and direct reads of
  1017. * I915_RING_HEAD. It also provides a nice sanity check.
  1018. */
  1019. request->tail = -1;
  1020. }
  1021. if (seqno == 0)
  1022. return -ENOSPC;
  1023. ret = intel_ring_wait_seqno(ring, seqno);
  1024. if (ret)
  1025. return ret;
  1026. if (WARN_ON(ring->last_retired_head == -1))
  1027. return -ENOSPC;
  1028. ring->head = ring->last_retired_head;
  1029. ring->last_retired_head = -1;
  1030. ring->space = ring_space(ring);
  1031. if (WARN_ON(ring->space < n))
  1032. return -ENOSPC;
  1033. return 0;
  1034. }
  1035. int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
  1036. {
  1037. struct drm_device *dev = ring->dev;
  1038. struct drm_i915_private *dev_priv = dev->dev_private;
  1039. unsigned long end;
  1040. int ret;
  1041. ret = intel_ring_wait_request(ring, n);
  1042. if (ret != -ENOSPC)
  1043. return ret;
  1044. trace_i915_ring_wait_begin(ring);
  1045. if (drm_core_check_feature(dev, DRIVER_GEM))
  1046. /* With GEM the hangcheck timer should kick us out of the loop,
  1047. * leaving it early runs the risk of corrupting GEM state (due
  1048. * to running on almost untested codepaths). But on resume
  1049. * timers don't work yet, so prevent a complete hang in that
  1050. * case by choosing an insanely large timeout. */
  1051. end = jiffies + 60 * HZ;
  1052. else
  1053. end = jiffies + 3 * HZ;
  1054. do {
  1055. ring->head = I915_READ_HEAD(ring);
  1056. ring->space = ring_space(ring);
  1057. if (ring->space >= n) {
  1058. trace_i915_ring_wait_end(ring);
  1059. return 0;
  1060. }
  1061. if (dev->primary->master) {
  1062. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  1063. if (master_priv->sarea_priv)
  1064. master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  1065. }
  1066. msleep(1);
  1067. if (atomic_read(&dev_priv->mm.wedged))
  1068. return -EAGAIN;
  1069. } while (!time_after(jiffies, end));
  1070. trace_i915_ring_wait_end(ring);
  1071. return -EBUSY;
  1072. }
  1073. int intel_ring_begin(struct intel_ring_buffer *ring,
  1074. int num_dwords)
  1075. {
  1076. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1077. int n = 4*num_dwords;
  1078. int ret;
  1079. if (unlikely(atomic_read(&dev_priv->mm.wedged)))
  1080. return -EIO;
  1081. if (unlikely(ring->tail + n > ring->effective_size)) {
  1082. ret = intel_wrap_ring_buffer(ring);
  1083. if (unlikely(ret))
  1084. return ret;
  1085. }
  1086. if (unlikely(ring->space < n)) {
  1087. ret = intel_wait_ring_buffer(ring, n);
  1088. if (unlikely(ret))
  1089. return ret;
  1090. }
  1091. ring->space -= n;
  1092. return 0;
  1093. }
  1094. void intel_ring_advance(struct intel_ring_buffer *ring)
  1095. {
  1096. ring->tail &= ring->size - 1;
  1097. ring->write_tail(ring, ring->tail);
  1098. }
  1099. static const struct intel_ring_buffer render_ring = {
  1100. .name = "render ring",
  1101. .id = RCS,
  1102. .mmio_base = RENDER_RING_BASE,
  1103. .size = 32 * PAGE_SIZE,
  1104. .init = init_render_ring,
  1105. .write_tail = ring_write_tail,
  1106. .flush = render_ring_flush,
  1107. .add_request = render_ring_add_request,
  1108. .get_seqno = ring_get_seqno,
  1109. .irq_get = render_ring_get_irq,
  1110. .irq_put = render_ring_put_irq,
  1111. .dispatch_execbuffer = render_ring_dispatch_execbuffer,
  1112. .cleanup = render_ring_cleanup,
  1113. .sync_to = render_ring_sync_to,
  1114. .semaphore_register = {MI_SEMAPHORE_SYNC_INVALID,
  1115. MI_SEMAPHORE_SYNC_RV,
  1116. MI_SEMAPHORE_SYNC_RB},
  1117. .signal_mbox = {GEN6_VRSYNC, GEN6_BRSYNC},
  1118. };
  1119. /* ring buffer for bit-stream decoder */
  1120. static const struct intel_ring_buffer bsd_ring = {
  1121. .name = "bsd ring",
  1122. .id = VCS,
  1123. .mmio_base = BSD_RING_BASE,
  1124. .size = 32 * PAGE_SIZE,
  1125. .init = init_ring_common,
  1126. .write_tail = ring_write_tail,
  1127. .flush = bsd_ring_flush,
  1128. .add_request = ring_add_request,
  1129. .get_seqno = ring_get_seqno,
  1130. .irq_get = bsd_ring_get_irq,
  1131. .irq_put = bsd_ring_put_irq,
  1132. .dispatch_execbuffer = ring_dispatch_execbuffer,
  1133. };
  1134. static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
  1135. u32 value)
  1136. {
  1137. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1138. /* Every tail move must follow the sequence below */
  1139. I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1140. GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
  1141. GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
  1142. I915_WRITE(GEN6_BSD_RNCID, 0x0);
  1143. if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
  1144. GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
  1145. 50))
  1146. DRM_ERROR("timed out waiting for IDLE Indicator\n");
  1147. I915_WRITE_TAIL(ring, value);
  1148. I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1149. GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
  1150. GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
  1151. }
  1152. static int gen6_ring_flush(struct intel_ring_buffer *ring,
  1153. u32 invalidate, u32 flush)
  1154. {
  1155. uint32_t cmd;
  1156. int ret;
  1157. ret = intel_ring_begin(ring, 4);
  1158. if (ret)
  1159. return ret;
  1160. cmd = MI_FLUSH_DW;
  1161. if (invalidate & I915_GEM_GPU_DOMAINS)
  1162. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
  1163. intel_ring_emit(ring, cmd);
  1164. intel_ring_emit(ring, 0);
  1165. intel_ring_emit(ring, 0);
  1166. intel_ring_emit(ring, MI_NOOP);
  1167. intel_ring_advance(ring);
  1168. return 0;
  1169. }
  1170. static int
  1171. gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1172. u32 offset, u32 len)
  1173. {
  1174. int ret;
  1175. ret = intel_ring_begin(ring, 2);
  1176. if (ret)
  1177. return ret;
  1178. intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
  1179. /* bit0-7 is the length on GEN6+ */
  1180. intel_ring_emit(ring, offset);
  1181. intel_ring_advance(ring);
  1182. return 0;
  1183. }
  1184. static bool
  1185. gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
  1186. {
  1187. return gen6_ring_get_irq(ring,
  1188. GT_USER_INTERRUPT,
  1189. GEN6_RENDER_USER_INTERRUPT);
  1190. }
  1191. static void
  1192. gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
  1193. {
  1194. return gen6_ring_put_irq(ring,
  1195. GT_USER_INTERRUPT,
  1196. GEN6_RENDER_USER_INTERRUPT);
  1197. }
  1198. static bool
  1199. gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
  1200. {
  1201. return gen6_ring_get_irq(ring,
  1202. GT_GEN6_BSD_USER_INTERRUPT,
  1203. GEN6_BSD_USER_INTERRUPT);
  1204. }
  1205. static void
  1206. gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
  1207. {
  1208. return gen6_ring_put_irq(ring,
  1209. GT_GEN6_BSD_USER_INTERRUPT,
  1210. GEN6_BSD_USER_INTERRUPT);
  1211. }
  1212. /* ring buffer for Video Codec for Gen6+ */
  1213. static const struct intel_ring_buffer gen6_bsd_ring = {
  1214. .name = "gen6 bsd ring",
  1215. .id = VCS,
  1216. .mmio_base = GEN6_BSD_RING_BASE,
  1217. .size = 32 * PAGE_SIZE,
  1218. .init = init_ring_common,
  1219. .write_tail = gen6_bsd_ring_write_tail,
  1220. .flush = gen6_ring_flush,
  1221. .add_request = gen6_add_request,
  1222. .get_seqno = gen6_ring_get_seqno,
  1223. .irq_get = gen6_bsd_ring_get_irq,
  1224. .irq_put = gen6_bsd_ring_put_irq,
  1225. .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
  1226. .sync_to = gen6_bsd_ring_sync_to,
  1227. .semaphore_register = {MI_SEMAPHORE_SYNC_VR,
  1228. MI_SEMAPHORE_SYNC_INVALID,
  1229. MI_SEMAPHORE_SYNC_VB},
  1230. .signal_mbox = {GEN6_RVSYNC, GEN6_BVSYNC},
  1231. };
  1232. /* Blitter support (SandyBridge+) */
  1233. static bool
  1234. blt_ring_get_irq(struct intel_ring_buffer *ring)
  1235. {
  1236. return gen6_ring_get_irq(ring,
  1237. GT_BLT_USER_INTERRUPT,
  1238. GEN6_BLITTER_USER_INTERRUPT);
  1239. }
  1240. static void
  1241. blt_ring_put_irq(struct intel_ring_buffer *ring)
  1242. {
  1243. gen6_ring_put_irq(ring,
  1244. GT_BLT_USER_INTERRUPT,
  1245. GEN6_BLITTER_USER_INTERRUPT);
  1246. }
  1247. static int blt_ring_flush(struct intel_ring_buffer *ring,
  1248. u32 invalidate, u32 flush)
  1249. {
  1250. uint32_t cmd;
  1251. int ret;
  1252. ret = intel_ring_begin(ring, 4);
  1253. if (ret)
  1254. return ret;
  1255. cmd = MI_FLUSH_DW;
  1256. if (invalidate & I915_GEM_DOMAIN_RENDER)
  1257. cmd |= MI_INVALIDATE_TLB;
  1258. intel_ring_emit(ring, cmd);
  1259. intel_ring_emit(ring, 0);
  1260. intel_ring_emit(ring, 0);
  1261. intel_ring_emit(ring, MI_NOOP);
  1262. intel_ring_advance(ring);
  1263. return 0;
  1264. }
  1265. static const struct intel_ring_buffer gen6_blt_ring = {
  1266. .name = "blt ring",
  1267. .id = BCS,
  1268. .mmio_base = BLT_RING_BASE,
  1269. .size = 32 * PAGE_SIZE,
  1270. .init = init_ring_common,
  1271. .write_tail = ring_write_tail,
  1272. .flush = blt_ring_flush,
  1273. .add_request = gen6_add_request,
  1274. .get_seqno = gen6_ring_get_seqno,
  1275. .irq_get = blt_ring_get_irq,
  1276. .irq_put = blt_ring_put_irq,
  1277. .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
  1278. .sync_to = gen6_blt_ring_sync_to,
  1279. .semaphore_register = {MI_SEMAPHORE_SYNC_BR,
  1280. MI_SEMAPHORE_SYNC_BV,
  1281. MI_SEMAPHORE_SYNC_INVALID},
  1282. .signal_mbox = {GEN6_RBSYNC, GEN6_VBSYNC},
  1283. };
  1284. int intel_init_render_ring_buffer(struct drm_device *dev)
  1285. {
  1286. drm_i915_private_t *dev_priv = dev->dev_private;
  1287. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  1288. *ring = render_ring;
  1289. if (INTEL_INFO(dev)->gen >= 6) {
  1290. ring->add_request = gen6_add_request;
  1291. ring->flush = gen6_render_ring_flush;
  1292. ring->irq_get = gen6_render_ring_get_irq;
  1293. ring->irq_put = gen6_render_ring_put_irq;
  1294. ring->get_seqno = gen6_ring_get_seqno;
  1295. } else if (IS_GEN5(dev)) {
  1296. ring->add_request = pc_render_add_request;
  1297. ring->get_seqno = pc_render_get_seqno;
  1298. }
  1299. if (!I915_NEED_GFX_HWS(dev)) {
  1300. ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  1301. memset(ring->status_page.page_addr, 0, PAGE_SIZE);
  1302. }
  1303. return intel_init_ring_buffer(dev, ring);
  1304. }
  1305. int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
  1306. {
  1307. drm_i915_private_t *dev_priv = dev->dev_private;
  1308. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  1309. *ring = render_ring;
  1310. if (INTEL_INFO(dev)->gen >= 6) {
  1311. ring->add_request = gen6_add_request;
  1312. ring->irq_get = gen6_render_ring_get_irq;
  1313. ring->irq_put = gen6_render_ring_put_irq;
  1314. } else if (IS_GEN5(dev)) {
  1315. ring->add_request = pc_render_add_request;
  1316. ring->get_seqno = pc_render_get_seqno;
  1317. }
  1318. if (!I915_NEED_GFX_HWS(dev))
  1319. ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  1320. ring->dev = dev;
  1321. INIT_LIST_HEAD(&ring->active_list);
  1322. INIT_LIST_HEAD(&ring->request_list);
  1323. INIT_LIST_HEAD(&ring->gpu_write_list);
  1324. ring->size = size;
  1325. ring->effective_size = ring->size;
  1326. if (IS_I830(ring->dev))
  1327. ring->effective_size -= 128;
  1328. ring->map.offset = start;
  1329. ring->map.size = size;
  1330. ring->map.type = 0;
  1331. ring->map.flags = 0;
  1332. ring->map.mtrr = 0;
  1333. drm_core_ioremap_wc(&ring->map, dev);
  1334. if (ring->map.handle == NULL) {
  1335. DRM_ERROR("can not ioremap virtual address for"
  1336. " ring buffer\n");
  1337. return -ENOMEM;
  1338. }
  1339. ring->virtual_start = (void __force __iomem *)ring->map.handle;
  1340. return 0;
  1341. }
  1342. int intel_init_bsd_ring_buffer(struct drm_device *dev)
  1343. {
  1344. drm_i915_private_t *dev_priv = dev->dev_private;
  1345. struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
  1346. if (IS_GEN6(dev) || IS_GEN7(dev))
  1347. *ring = gen6_bsd_ring;
  1348. else
  1349. *ring = bsd_ring;
  1350. return intel_init_ring_buffer(dev, ring);
  1351. }
  1352. int intel_init_blt_ring_buffer(struct drm_device *dev)
  1353. {
  1354. drm_i915_private_t *dev_priv = dev->dev_private;
  1355. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  1356. *ring = gen6_blt_ring;
  1357. return intel_init_ring_buffer(dev, ring);
  1358. }