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();
  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. struct drm_i915_master_private *master_priv;
  150. u32 iir, new_iir;
  151. u32 pipea_stats, pipeb_stats;
  152. u32 vblank_status;
  153. u32 vblank_enable;
  154. int vblank = 0;
  155. unsigned long irqflags;
  156. int irq_received;
  157. int ret = IRQ_NONE;
  158. atomic_inc(&dev_priv->irq_received);
  159. iir = I915_READ(IIR);
  160. if (IS_I965G(dev)) {
  161. vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
  162. vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
  163. } else {
  164. vblank_status = I915_VBLANK_INTERRUPT_STATUS;
  165. vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
  166. }
  167. for (;;) {
  168. irq_received = iir != 0;
  169. /* Can't rely on pipestat interrupt bit in iir as it might
  170. * have been cleared after the pipestat interrupt was received.
  171. * It doesn't set the bit in iir again, but it still produces
  172. * interrupts (for non-MSI).
  173. */
  174. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  175. pipea_stats = I915_READ(PIPEASTAT);
  176. pipeb_stats = I915_READ(PIPEBSTAT);
  177. /*
  178. * Clear the PIPE(A|B)STAT regs before the IIR
  179. */
  180. if (pipea_stats & 0x8000ffff) {
  181. I915_WRITE(PIPEASTAT, pipea_stats);
  182. irq_received = 1;
  183. }
  184. if (pipeb_stats & 0x8000ffff) {
  185. I915_WRITE(PIPEBSTAT, pipeb_stats);
  186. irq_received = 1;
  187. }
  188. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  189. if (!irq_received)
  190. break;
  191. ret = IRQ_HANDLED;
  192. I915_WRITE(IIR, iir);
  193. new_iir = I915_READ(IIR); /* Flush posted writes */
  194. if (dev->primary->master) {
  195. master_priv = dev->primary->master->driver_priv;
  196. if (master_priv->sarea_priv)
  197. master_priv->sarea_priv->last_dispatch =
  198. READ_BREADCRUMB(dev_priv);
  199. }
  200. if (iir & I915_USER_INTERRUPT) {
  201. dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
  202. DRM_WAKEUP(&dev_priv->irq_queue);
  203. }
  204. if (pipea_stats & vblank_status) {
  205. vblank++;
  206. drm_handle_vblank(dev, 0);
  207. }
  208. if (pipeb_stats & vblank_status) {
  209. vblank++;
  210. drm_handle_vblank(dev, 1);
  211. }
  212. if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
  213. (iir & I915_ASLE_INTERRUPT))
  214. opregion_asle_intr(dev);
  215. /* With MSI, interrupts are only generated when iir
  216. * transitions from zero to nonzero. If another bit got
  217. * set while we were handling the existing iir bits, then
  218. * we would never get another interrupt.
  219. *
  220. * This is fine on non-MSI as well, as if we hit this path
  221. * we avoid exiting the interrupt handler only to generate
  222. * another one.
  223. *
  224. * Note that for MSI this could cause a stray interrupt report
  225. * if an interrupt landed in the time between writing IIR and
  226. * the posting read. This should be rare enough to never
  227. * trigger the 99% of 100,000 interrupts test for disabling
  228. * stray interrupts.
  229. */
  230. iir = new_iir;
  231. }
  232. return ret;
  233. }
  234. static int i915_emit_irq(struct drm_device * dev)
  235. {
  236. drm_i915_private_t *dev_priv = dev->dev_private;
  237. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  238. RING_LOCALS;
  239. i915_kernel_lost_context(dev);
  240. DRM_DEBUG("\n");
  241. dev_priv->counter++;
  242. if (dev_priv->counter > 0x7FFFFFFFUL)
  243. dev_priv->counter = 1;
  244. if (master_priv->sarea_priv)
  245. master_priv->sarea_priv->last_enqueue = dev_priv->counter;
  246. BEGIN_LP_RING(4);
  247. OUT_RING(MI_STORE_DWORD_INDEX);
  248. OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  249. OUT_RING(dev_priv->counter);
  250. OUT_RING(MI_USER_INTERRUPT);
  251. ADVANCE_LP_RING();
  252. return dev_priv->counter;
  253. }
  254. void i915_user_irq_get(struct drm_device *dev)
  255. {
  256. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  257. unsigned long irqflags;
  258. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  259. if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
  260. i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
  261. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  262. }
  263. void i915_user_irq_put(struct drm_device *dev)
  264. {
  265. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  266. unsigned long irqflags;
  267. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  268. BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
  269. if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
  270. i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
  271. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  272. }
  273. static int i915_wait_irq(struct drm_device * dev, int irq_nr)
  274. {
  275. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  276. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  277. int ret = 0;
  278. DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
  279. READ_BREADCRUMB(dev_priv));
  280. if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
  281. if (master_priv->sarea_priv)
  282. master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
  283. return 0;
  284. }
  285. if (master_priv->sarea_priv)
  286. master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  287. i915_user_irq_get(dev);
  288. DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
  289. READ_BREADCRUMB(dev_priv) >= irq_nr);
  290. i915_user_irq_put(dev);
  291. if (ret == -EBUSY) {
  292. DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
  293. READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
  294. }
  295. return ret;
  296. }
  297. /* Needs the lock as it touches the ring.
  298. */
  299. int i915_irq_emit(struct drm_device *dev, void *data,
  300. struct drm_file *file_priv)
  301. {
  302. drm_i915_private_t *dev_priv = dev->dev_private;
  303. drm_i915_irq_emit_t *emit = data;
  304. int result;
  305. RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
  306. if (!dev_priv) {
  307. DRM_ERROR("called with no initialization\n");
  308. return -EINVAL;
  309. }
  310. mutex_lock(&dev->struct_mutex);
  311. result = i915_emit_irq(dev);
  312. mutex_unlock(&dev->struct_mutex);
  313. if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
  314. DRM_ERROR("copy_to_user\n");
  315. return -EFAULT;
  316. }
  317. return 0;
  318. }
  319. /* Doesn't need the hardware lock.
  320. */
  321. int i915_irq_wait(struct drm_device *dev, void *data,
  322. struct drm_file *file_priv)
  323. {
  324. drm_i915_private_t *dev_priv = dev->dev_private;
  325. drm_i915_irq_wait_t *irqwait = data;
  326. if (!dev_priv) {
  327. DRM_ERROR("called with no initialization\n");
  328. return -EINVAL;
  329. }
  330. return i915_wait_irq(dev, irqwait->irq_seq);
  331. }
  332. /* Called from drm generic code, passed 'crtc' which
  333. * we use as a pipe index
  334. */
  335. int i915_enable_vblank(struct drm_device *dev, int pipe)
  336. {
  337. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  338. unsigned long irqflags;
  339. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  340. if (IS_I965G(dev))
  341. i915_enable_pipestat(dev_priv, pipe,
  342. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  343. else
  344. i915_enable_pipestat(dev_priv, pipe,
  345. PIPE_VBLANK_INTERRUPT_ENABLE);
  346. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  347. return 0;
  348. }
  349. /* Called from drm generic code, passed 'crtc' which
  350. * we use as a pipe index
  351. */
  352. void i915_disable_vblank(struct drm_device *dev, int pipe)
  353. {
  354. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  355. unsigned long irqflags;
  356. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  357. i915_disable_pipestat(dev_priv, pipe,
  358. PIPE_VBLANK_INTERRUPT_ENABLE |
  359. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  360. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  361. }
  362. /* Set the vblank monitor pipe
  363. */
  364. int i915_vblank_pipe_set(struct drm_device *dev, void *data,
  365. struct drm_file *file_priv)
  366. {
  367. drm_i915_private_t *dev_priv = dev->dev_private;
  368. if (!dev_priv) {
  369. DRM_ERROR("called with no initialization\n");
  370. return -EINVAL;
  371. }
  372. return 0;
  373. }
  374. int i915_vblank_pipe_get(struct drm_device *dev, void *data,
  375. struct drm_file *file_priv)
  376. {
  377. drm_i915_private_t *dev_priv = dev->dev_private;
  378. drm_i915_vblank_pipe_t *pipe = data;
  379. if (!dev_priv) {
  380. DRM_ERROR("called with no initialization\n");
  381. return -EINVAL;
  382. }
  383. pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  384. return 0;
  385. }
  386. /**
  387. * Schedule buffer swap at given vertical blank.
  388. */
  389. int i915_vblank_swap(struct drm_device *dev, void *data,
  390. struct drm_file *file_priv)
  391. {
  392. /* The delayed swap mechanism was fundamentally racy, and has been
  393. * removed. The model was that the client requested a delayed flip/swap
  394. * from the kernel, then waited for vblank before continuing to perform
  395. * rendering. The problem was that the kernel might wake the client
  396. * up before it dispatched the vblank swap (since the lock has to be
  397. * held while touching the ringbuffer), in which case the client would
  398. * clear and start the next frame before the swap occurred, and
  399. * flicker would occur in addition to likely missing the vblank.
  400. *
  401. * In the absence of this ioctl, userland falls back to a correct path
  402. * of waiting for a vblank, then dispatching the swap on its own.
  403. * Context switching to userland and back is plenty fast enough for
  404. * meeting the requirements of vblank swapping.
  405. */
  406. return -EINVAL;
  407. }
  408. /* drm_dma.h hooks
  409. */
  410. void i915_driver_irq_preinstall(struct drm_device * dev)
  411. {
  412. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  413. I915_WRITE(HWSTAM, 0xeffe);
  414. I915_WRITE(PIPEASTAT, 0);
  415. I915_WRITE(PIPEBSTAT, 0);
  416. I915_WRITE(IMR, 0xffffffff);
  417. I915_WRITE(IER, 0x0);
  418. (void) I915_READ(IER);
  419. }
  420. int i915_driver_irq_postinstall(struct drm_device *dev)
  421. {
  422. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  423. dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  424. dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
  425. /* Unmask the interrupts that we always want on. */
  426. dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
  427. dev_priv->pipestat[0] = 0;
  428. dev_priv->pipestat[1] = 0;
  429. /* Disable pipe interrupt enables, clear pending pipe status */
  430. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  431. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  432. /* Clear pending interrupt status */
  433. I915_WRITE(IIR, I915_READ(IIR));
  434. I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK);
  435. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  436. (void) I915_READ(IER);
  437. opregion_enable_asle(dev);
  438. DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
  439. return 0;
  440. }
  441. void i915_driver_irq_uninstall(struct drm_device * dev)
  442. {
  443. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  444. if (!dev_priv)
  445. return;
  446. dev_priv->vblank_pipe = 0;
  447. I915_WRITE(HWSTAM, 0xffffffff);
  448. I915_WRITE(PIPEASTAT, 0);
  449. I915_WRITE(PIPEBSTAT, 0);
  450. I915_WRITE(IMR, 0xffffffff);
  451. I915_WRITE(IER, 0x0);
  452. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  453. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  454. I915_WRITE(IIR, I915_READ(IIR));
  455. }