i915_irq.c 15 KB

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