i915_irq.c 16 KB

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