i915_irq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
  2. */
  3. /*
  4. * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  22. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  23. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28. #include "drmP.h"
  29. #include "drm.h"
  30. #include "i915_drm.h"
  31. #include "i915_drv.h"
  32. #define MAX_NOPID ((u32)~0)
  33. /**
  34. * Interrupts that are always left unmasked.
  35. *
  36. * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
  37. * we leave them always unmasked in IMR and then control enabling them through
  38. * PIPESTAT alone.
  39. */
  40. #define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \
  41. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
  42. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
  43. /** Interrupts that we mask and unmask at runtime. */
  44. #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
  45. /** These are all of the interrupts used by the driver */
  46. #define I915_INTERRUPT_ENABLE_MASK (I915_INTERRUPT_ENABLE_FIX | \
  47. I915_INTERRUPT_ENABLE_VAR)
  48. void
  49. i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
  50. {
  51. if ((dev_priv->irq_mask_reg & mask) != 0) {
  52. dev_priv->irq_mask_reg &= ~mask;
  53. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  54. (void) I915_READ(IMR);
  55. }
  56. }
  57. static inline void
  58. i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
  59. {
  60. if ((dev_priv->irq_mask_reg & mask) != mask) {
  61. dev_priv->irq_mask_reg |= mask;
  62. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  63. (void) I915_READ(IMR);
  64. }
  65. }
  66. static inline u32
  67. i915_pipestat(int pipe)
  68. {
  69. if (pipe == 0)
  70. return PIPEASTAT;
  71. if (pipe == 1)
  72. return PIPEBSTAT;
  73. BUG_ON(1);
  74. }
  75. void
  76. i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
  77. {
  78. if ((dev_priv->pipestat[pipe] & mask) != mask) {
  79. u32 reg = i915_pipestat(pipe);
  80. dev_priv->pipestat[pipe] |= mask;
  81. /* Enable the interrupt, clear any pending status */
  82. I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
  83. (void) I915_READ(reg);
  84. }
  85. }
  86. void
  87. i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
  88. {
  89. if ((dev_priv->pipestat[pipe] & mask) != 0) {
  90. u32 reg = i915_pipestat(pipe);
  91. dev_priv->pipestat[pipe] &= ~mask;
  92. I915_WRITE(reg, dev_priv->pipestat[pipe]);
  93. (void) I915_READ(reg);
  94. }
  95. }
  96. /**
  97. * i915_pipe_enabled - check if a pipe is enabled
  98. * @dev: DRM device
  99. * @pipe: pipe to check
  100. *
  101. * Reading certain registers when the pipe is disabled can hang the chip.
  102. * Use this routine to make sure the PLL is running and the pipe is active
  103. * before reading such registers if unsure.
  104. */
  105. static int
  106. i915_pipe_enabled(struct drm_device *dev, int pipe)
  107. {
  108. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  109. unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
  110. if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
  111. return 1;
  112. return 0;
  113. }
  114. /* Called from drm generic code, passed a 'crtc', which
  115. * we use as a pipe index
  116. */
  117. u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
  118. {
  119. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  120. unsigned long high_frame;
  121. unsigned long low_frame;
  122. u32 high1, high2, low, count;
  123. high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
  124. low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
  125. if (!i915_pipe_enabled(dev, pipe)) {
  126. DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
  127. return 0;
  128. }
  129. /*
  130. * High & low register fields aren't synchronized, so make sure
  131. * we get a low value that's stable across two reads of the high
  132. * register.
  133. */
  134. do {
  135. high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
  136. PIPE_FRAME_HIGH_SHIFT);
  137. low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
  138. PIPE_FRAME_LOW_SHIFT);
  139. high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
  140. PIPE_FRAME_HIGH_SHIFT);
  141. } while (high1 != high2);
  142. count = (high1 << 8) | low;
  143. return count;
  144. }
  145. irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
  146. {
  147. struct drm_device *dev = (struct drm_device *) arg;
  148. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  149. u32 iir, new_iir;
  150. u32 pipea_stats, pipeb_stats;
  151. u32 vblank_status;
  152. u32 vblank_enable;
  153. int vblank = 0;
  154. unsigned long irqflags;
  155. int irq_received;
  156. int ret = IRQ_NONE;
  157. atomic_inc(&dev_priv->irq_received);
  158. iir = I915_READ(IIR);
  159. if (IS_I965G(dev)) {
  160. vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
  161. vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
  162. } else {
  163. vblank_status = I915_VBLANK_INTERRUPT_STATUS;
  164. vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
  165. }
  166. for (;;) {
  167. irq_received = iir != 0;
  168. /* Can't rely on pipestat interrupt bit in iir as it might
  169. * have been cleared after the pipestat interrupt was received.
  170. * It doesn't set the bit in iir again, but it still produces
  171. * interrupts (for non-MSI).
  172. */
  173. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  174. pipea_stats = I915_READ(PIPEASTAT);
  175. pipeb_stats = I915_READ(PIPEBSTAT);
  176. /*
  177. * Clear the PIPE(A|B)STAT regs before the IIR
  178. */
  179. if (pipea_stats & 0x8000ffff) {
  180. I915_WRITE(PIPEASTAT, pipea_stats);
  181. irq_received = 1;
  182. }
  183. if (pipeb_stats & 0x8000ffff) {
  184. I915_WRITE(PIPEBSTAT, pipeb_stats);
  185. irq_received = 1;
  186. }
  187. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  188. if (!irq_received)
  189. break;
  190. ret = IRQ_HANDLED;
  191. I915_WRITE(IIR, iir);
  192. new_iir = I915_READ(IIR); /* Flush posted writes */
  193. if (dev_priv->sarea_priv)
  194. dev_priv->sarea_priv->last_dispatch =
  195. READ_BREADCRUMB(dev_priv);
  196. if (iir & I915_USER_INTERRUPT) {
  197. dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
  198. DRM_WAKEUP(&dev_priv->irq_queue);
  199. }
  200. if (pipea_stats & vblank_status) {
  201. vblank++;
  202. drm_handle_vblank(dev, 0);
  203. }
  204. if (pipeb_stats & vblank_status) {
  205. vblank++;
  206. drm_handle_vblank(dev, 1);
  207. }
  208. if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
  209. (iir & I915_ASLE_INTERRUPT))
  210. opregion_asle_intr(dev);
  211. /* With MSI, interrupts are only generated when iir
  212. * transitions from zero to nonzero. If another bit got
  213. * set while we were handling the existing iir bits, then
  214. * we would never get another interrupt.
  215. *
  216. * This is fine on non-MSI as well, as if we hit this path
  217. * we avoid exiting the interrupt handler only to generate
  218. * another one.
  219. *
  220. * Note that for MSI this could cause a stray interrupt report
  221. * if an interrupt landed in the time between writing IIR and
  222. * the posting read. This should be rare enough to never
  223. * trigger the 99% of 100,000 interrupts test for disabling
  224. * stray interrupts.
  225. */
  226. iir = new_iir;
  227. }
  228. return ret;
  229. }
  230. static int i915_emit_irq(struct drm_device * dev)
  231. {
  232. drm_i915_private_t *dev_priv = dev->dev_private;
  233. RING_LOCALS;
  234. i915_kernel_lost_context(dev);
  235. DRM_DEBUG("\n");
  236. dev_priv->counter++;
  237. if (dev_priv->counter > 0x7FFFFFFFUL)
  238. dev_priv->counter = 1;
  239. if (dev_priv->sarea_priv)
  240. dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
  241. BEGIN_LP_RING(4);
  242. OUT_RING(MI_STORE_DWORD_INDEX);
  243. OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  244. OUT_RING(dev_priv->counter);
  245. OUT_RING(MI_USER_INTERRUPT);
  246. ADVANCE_LP_RING();
  247. return dev_priv->counter;
  248. }
  249. void i915_user_irq_get(struct drm_device *dev)
  250. {
  251. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  252. unsigned long irqflags;
  253. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  254. if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
  255. i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
  256. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  257. }
  258. void i915_user_irq_put(struct drm_device *dev)
  259. {
  260. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  261. unsigned long irqflags;
  262. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  263. BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
  264. if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
  265. i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
  266. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  267. }
  268. static int i915_wait_irq(struct drm_device * dev, int irq_nr)
  269. {
  270. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  271. int ret = 0;
  272. DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
  273. READ_BREADCRUMB(dev_priv));
  274. if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
  275. if (dev_priv->sarea_priv) {
  276. dev_priv->sarea_priv->last_dispatch =
  277. READ_BREADCRUMB(dev_priv);
  278. }
  279. return 0;
  280. }
  281. if (dev_priv->sarea_priv)
  282. dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  283. i915_user_irq_get(dev);
  284. DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
  285. READ_BREADCRUMB(dev_priv) >= irq_nr);
  286. i915_user_irq_put(dev);
  287. if (ret == -EBUSY) {
  288. DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
  289. READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
  290. }
  291. if (dev_priv->sarea_priv)
  292. dev_priv->sarea_priv->last_dispatch =
  293. READ_BREADCRUMB(dev_priv);
  294. return ret;
  295. }
  296. /* Needs the lock as it touches the ring.
  297. */
  298. int i915_irq_emit(struct drm_device *dev, void *data,
  299. struct drm_file *file_priv)
  300. {
  301. drm_i915_private_t *dev_priv = dev->dev_private;
  302. drm_i915_irq_emit_t *emit = data;
  303. int result;
  304. RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
  305. if (!dev_priv) {
  306. DRM_ERROR("called with no initialization\n");
  307. return -EINVAL;
  308. }
  309. mutex_lock(&dev->struct_mutex);
  310. result = i915_emit_irq(dev);
  311. mutex_unlock(&dev->struct_mutex);
  312. if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
  313. DRM_ERROR("copy_to_user\n");
  314. return -EFAULT;
  315. }
  316. return 0;
  317. }
  318. /* Doesn't need the hardware lock.
  319. */
  320. int i915_irq_wait(struct drm_device *dev, void *data,
  321. struct drm_file *file_priv)
  322. {
  323. drm_i915_private_t *dev_priv = dev->dev_private;
  324. drm_i915_irq_wait_t *irqwait = data;
  325. if (!dev_priv) {
  326. DRM_ERROR("called with no initialization\n");
  327. return -EINVAL;
  328. }
  329. return i915_wait_irq(dev, irqwait->irq_seq);
  330. }
  331. /* Called from drm generic code, passed 'crtc' which
  332. * we use as a pipe index
  333. */
  334. int i915_enable_vblank(struct drm_device *dev, int pipe)
  335. {
  336. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  337. unsigned long irqflags;
  338. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  339. if (IS_I965G(dev))
  340. i915_enable_pipestat(dev_priv, pipe,
  341. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  342. else
  343. i915_enable_pipestat(dev_priv, pipe,
  344. PIPE_VBLANK_INTERRUPT_ENABLE);
  345. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  346. return 0;
  347. }
  348. /* Called from drm generic code, passed 'crtc' which
  349. * we use as a pipe index
  350. */
  351. void i915_disable_vblank(struct drm_device *dev, int pipe)
  352. {
  353. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  354. unsigned long irqflags;
  355. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  356. i915_disable_pipestat(dev_priv, pipe,
  357. PIPE_VBLANK_INTERRUPT_ENABLE |
  358. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  359. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  360. }
  361. /* Set the vblank monitor pipe
  362. */
  363. int i915_vblank_pipe_set(struct drm_device *dev, void *data,
  364. struct drm_file *file_priv)
  365. {
  366. drm_i915_private_t *dev_priv = dev->dev_private;
  367. if (!dev_priv) {
  368. DRM_ERROR("called with no initialization\n");
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. int i915_vblank_pipe_get(struct drm_device *dev, void *data,
  374. struct drm_file *file_priv)
  375. {
  376. drm_i915_private_t *dev_priv = dev->dev_private;
  377. drm_i915_vblank_pipe_t *pipe = data;
  378. if (!dev_priv) {
  379. DRM_ERROR("called with no initialization\n");
  380. return -EINVAL;
  381. }
  382. pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  383. return 0;
  384. }
  385. /**
  386. * Schedule buffer swap at given vertical blank.
  387. */
  388. int i915_vblank_swap(struct drm_device *dev, void *data,
  389. struct drm_file *file_priv)
  390. {
  391. /* The delayed swap mechanism was fundamentally racy, and has been
  392. * removed. The model was that the client requested a delayed flip/swap
  393. * from the kernel, then waited for vblank before continuing to perform
  394. * rendering. The problem was that the kernel might wake the client
  395. * up before it dispatched the vblank swap (since the lock has to be
  396. * held while touching the ringbuffer), in which case the client would
  397. * clear and start the next frame before the swap occurred, and
  398. * flicker would occur in addition to likely missing the vblank.
  399. *
  400. * In the absence of this ioctl, userland falls back to a correct path
  401. * of waiting for a vblank, then dispatching the swap on its own.
  402. * Context switching to userland and back is plenty fast enough for
  403. * meeting the requirements of vblank swapping.
  404. */
  405. return -EINVAL;
  406. }
  407. /* drm_dma.h hooks
  408. */
  409. void i915_driver_irq_preinstall(struct drm_device * dev)
  410. {
  411. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  412. I915_WRITE(HWSTAM, 0xeffe);
  413. I915_WRITE(PIPEASTAT, 0);
  414. I915_WRITE(PIPEBSTAT, 0);
  415. I915_WRITE(IMR, 0xffffffff);
  416. I915_WRITE(IER, 0x0);
  417. (void) I915_READ(IER);
  418. }
  419. int i915_driver_irq_postinstall(struct drm_device *dev)
  420. {
  421. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  422. dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  423. dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
  424. /* Unmask the interrupts that we always want on. */
  425. dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
  426. dev_priv->pipestat[0] = 0;
  427. dev_priv->pipestat[1] = 0;
  428. /* Disable pipe interrupt enables, clear pending pipe status */
  429. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  430. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  431. /* Clear pending interrupt status */
  432. I915_WRITE(IIR, I915_READ(IIR));
  433. I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK);
  434. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  435. (void) I915_READ(IER);
  436. opregion_enable_asle(dev);
  437. DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
  438. return 0;
  439. }
  440. void i915_driver_irq_uninstall(struct drm_device * dev)
  441. {
  442. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  443. if (!dev_priv)
  444. return;
  445. dev_priv->vblank_pipe = 0;
  446. I915_WRITE(HWSTAM, 0xffffffff);
  447. I915_WRITE(PIPEASTAT, 0);
  448. I915_WRITE(PIPEBSTAT, 0);
  449. I915_WRITE(IMR, 0xffffffff);
  450. I915_WRITE(IER, 0x0);
  451. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  452. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  453. I915_WRITE(IIR, I915_READ(IIR));
  454. }