i915_irq.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 <linux/sysrq.h>
  29. #include "drmP.h"
  30. #include "drm.h"
  31. #include "i915_drm.h"
  32. #include "i915_drv.h"
  33. #include "intel_drv.h"
  34. #define MAX_NOPID ((u32)~0)
  35. /**
  36. * Interrupts that are always left unmasked.
  37. *
  38. * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
  39. * we leave them always unmasked in IMR and then control enabling them through
  40. * PIPESTAT alone.
  41. */
  42. #define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \
  43. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
  44. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT | \
  45. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  46. /** Interrupts that we mask and unmask at runtime. */
  47. #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
  48. #define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
  49. PIPE_VBLANK_INTERRUPT_STATUS)
  50. #define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
  51. PIPE_VBLANK_INTERRUPT_ENABLE)
  52. #define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
  53. DRM_I915_VBLANK_PIPE_B)
  54. void
  55. igdng_enable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
  56. {
  57. if ((dev_priv->gt_irq_mask_reg & mask) != 0) {
  58. dev_priv->gt_irq_mask_reg &= ~mask;
  59. I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
  60. (void) I915_READ(GTIMR);
  61. }
  62. }
  63. static inline void
  64. igdng_disable_graphics_irq(drm_i915_private_t *dev_priv, u32 mask)
  65. {
  66. if ((dev_priv->gt_irq_mask_reg & mask) != mask) {
  67. dev_priv->gt_irq_mask_reg |= mask;
  68. I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
  69. (void) I915_READ(GTIMR);
  70. }
  71. }
  72. /* For display hotplug interrupt */
  73. void
  74. igdng_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
  75. {
  76. if ((dev_priv->irq_mask_reg & mask) != 0) {
  77. dev_priv->irq_mask_reg &= ~mask;
  78. I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
  79. (void) I915_READ(DEIMR);
  80. }
  81. }
  82. static inline void
  83. igdng_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
  84. {
  85. if ((dev_priv->irq_mask_reg & mask) != mask) {
  86. dev_priv->irq_mask_reg |= mask;
  87. I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
  88. (void) I915_READ(DEIMR);
  89. }
  90. }
  91. void
  92. i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
  93. {
  94. if ((dev_priv->irq_mask_reg & mask) != 0) {
  95. dev_priv->irq_mask_reg &= ~mask;
  96. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  97. (void) I915_READ(IMR);
  98. }
  99. }
  100. static inline void
  101. i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
  102. {
  103. if ((dev_priv->irq_mask_reg & mask) != mask) {
  104. dev_priv->irq_mask_reg |= mask;
  105. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  106. (void) I915_READ(IMR);
  107. }
  108. }
  109. static inline u32
  110. i915_pipestat(int pipe)
  111. {
  112. if (pipe == 0)
  113. return PIPEASTAT;
  114. if (pipe == 1)
  115. return PIPEBSTAT;
  116. BUG();
  117. }
  118. void
  119. i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
  120. {
  121. if ((dev_priv->pipestat[pipe] & mask) != mask) {
  122. u32 reg = i915_pipestat(pipe);
  123. dev_priv->pipestat[pipe] |= mask;
  124. /* Enable the interrupt, clear any pending status */
  125. I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
  126. (void) I915_READ(reg);
  127. }
  128. }
  129. void
  130. i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
  131. {
  132. if ((dev_priv->pipestat[pipe] & mask) != 0) {
  133. u32 reg = i915_pipestat(pipe);
  134. dev_priv->pipestat[pipe] &= ~mask;
  135. I915_WRITE(reg, dev_priv->pipestat[pipe]);
  136. (void) I915_READ(reg);
  137. }
  138. }
  139. /**
  140. * i915_pipe_enabled - check if a pipe is enabled
  141. * @dev: DRM device
  142. * @pipe: pipe to check
  143. *
  144. * Reading certain registers when the pipe is disabled can hang the chip.
  145. * Use this routine to make sure the PLL is running and the pipe is active
  146. * before reading such registers if unsure.
  147. */
  148. static int
  149. i915_pipe_enabled(struct drm_device *dev, int pipe)
  150. {
  151. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  152. unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
  153. if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
  154. return 1;
  155. return 0;
  156. }
  157. /* Called from drm generic code, passed a 'crtc', which
  158. * we use as a pipe index
  159. */
  160. u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
  161. {
  162. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  163. unsigned long high_frame;
  164. unsigned long low_frame;
  165. u32 high1, high2, low, count;
  166. high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
  167. low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
  168. if (!i915_pipe_enabled(dev, pipe)) {
  169. DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
  170. return 0;
  171. }
  172. /*
  173. * High & low register fields aren't synchronized, so make sure
  174. * we get a low value that's stable across two reads of the high
  175. * register.
  176. */
  177. do {
  178. high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
  179. PIPE_FRAME_HIGH_SHIFT);
  180. low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
  181. PIPE_FRAME_LOW_SHIFT);
  182. high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
  183. PIPE_FRAME_HIGH_SHIFT);
  184. } while (high1 != high2);
  185. count = (high1 << 8) | low;
  186. return count;
  187. }
  188. u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
  189. {
  190. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  191. int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
  192. if (!i915_pipe_enabled(dev, pipe)) {
  193. DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
  194. return 0;
  195. }
  196. return I915_READ(reg);
  197. }
  198. /*
  199. * Handle hotplug events outside the interrupt handler proper.
  200. */
  201. static void i915_hotplug_work_func(struct work_struct *work)
  202. {
  203. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  204. hotplug_work);
  205. struct drm_device *dev = dev_priv->dev;
  206. struct drm_mode_config *mode_config = &dev->mode_config;
  207. struct drm_connector *connector;
  208. if (mode_config->num_connector) {
  209. list_for_each_entry(connector, &mode_config->connector_list, head) {
  210. struct intel_output *intel_output = to_intel_output(connector);
  211. if (intel_output->hot_plug)
  212. (*intel_output->hot_plug) (intel_output);
  213. }
  214. }
  215. /* Just fire off a uevent and let userspace tell us what to do */
  216. drm_sysfs_hotplug_event(dev);
  217. }
  218. irqreturn_t igdng_irq_handler(struct drm_device *dev)
  219. {
  220. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  221. int ret = IRQ_NONE;
  222. u32 de_iir, gt_iir;
  223. u32 new_de_iir, new_gt_iir;
  224. struct drm_i915_master_private *master_priv;
  225. de_iir = I915_READ(DEIIR);
  226. gt_iir = I915_READ(GTIIR);
  227. for (;;) {
  228. if (de_iir == 0 && gt_iir == 0)
  229. break;
  230. ret = IRQ_HANDLED;
  231. I915_WRITE(DEIIR, de_iir);
  232. new_de_iir = I915_READ(DEIIR);
  233. I915_WRITE(GTIIR, gt_iir);
  234. new_gt_iir = I915_READ(GTIIR);
  235. if (dev->primary->master) {
  236. master_priv = dev->primary->master->driver_priv;
  237. if (master_priv->sarea_priv)
  238. master_priv->sarea_priv->last_dispatch =
  239. READ_BREADCRUMB(dev_priv);
  240. }
  241. if (gt_iir & GT_USER_INTERRUPT) {
  242. dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
  243. DRM_WAKEUP(&dev_priv->irq_queue);
  244. }
  245. de_iir = new_de_iir;
  246. gt_iir = new_gt_iir;
  247. }
  248. return ret;
  249. }
  250. static void i915_capture_error_state(struct drm_device *dev)
  251. {
  252. struct drm_i915_private *dev_priv = dev->dev_private;
  253. struct drm_i915_error_state *error;
  254. unsigned long flags;
  255. spin_lock_irqsave(&dev_priv->error_lock, flags);
  256. if (dev_priv->first_error)
  257. goto out;
  258. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  259. if (!error) {
  260. DRM_DEBUG("out ot memory, not capturing error state\n");
  261. goto out;
  262. }
  263. error->eir = I915_READ(EIR);
  264. error->pgtbl_er = I915_READ(PGTBL_ER);
  265. error->pipeastat = I915_READ(PIPEASTAT);
  266. error->pipebstat = I915_READ(PIPEBSTAT);
  267. error->instpm = I915_READ(INSTPM);
  268. if (!IS_I965G(dev)) {
  269. error->ipeir = I915_READ(IPEIR);
  270. error->ipehr = I915_READ(IPEHR);
  271. error->instdone = I915_READ(INSTDONE);
  272. error->acthd = I915_READ(ACTHD);
  273. } else {
  274. error->ipeir = I915_READ(IPEIR_I965);
  275. error->ipehr = I915_READ(IPEHR_I965);
  276. error->instdone = I915_READ(INSTDONE_I965);
  277. error->instps = I915_READ(INSTPS);
  278. error->instdone1 = I915_READ(INSTDONE1);
  279. error->acthd = I915_READ(ACTHD_I965);
  280. }
  281. dev_priv->first_error = error;
  282. out:
  283. spin_unlock_irqrestore(&dev_priv->error_lock, flags);
  284. }
  285. irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
  286. {
  287. struct drm_device *dev = (struct drm_device *) arg;
  288. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  289. struct drm_i915_master_private *master_priv;
  290. u32 iir, new_iir;
  291. u32 pipea_stats, pipeb_stats;
  292. u32 vblank_status;
  293. u32 vblank_enable;
  294. int vblank = 0;
  295. unsigned long irqflags;
  296. int irq_received;
  297. int ret = IRQ_NONE;
  298. atomic_inc(&dev_priv->irq_received);
  299. if (IS_IGDNG(dev))
  300. return igdng_irq_handler(dev);
  301. iir = I915_READ(IIR);
  302. if (IS_I965G(dev)) {
  303. vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
  304. vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
  305. } else {
  306. vblank_status = I915_VBLANK_INTERRUPT_STATUS;
  307. vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
  308. }
  309. for (;;) {
  310. irq_received = iir != 0;
  311. /* Can't rely on pipestat interrupt bit in iir as it might
  312. * have been cleared after the pipestat interrupt was received.
  313. * It doesn't set the bit in iir again, but it still produces
  314. * interrupts (for non-MSI).
  315. */
  316. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  317. pipea_stats = I915_READ(PIPEASTAT);
  318. pipeb_stats = I915_READ(PIPEBSTAT);
  319. /*
  320. * Clear the PIPE(A|B)STAT regs before the IIR
  321. */
  322. if (pipea_stats & 0x8000ffff) {
  323. if (pipea_stats & PIPE_FIFO_UNDERRUN_STATUS)
  324. DRM_DEBUG("pipe a underrun\n");
  325. I915_WRITE(PIPEASTAT, pipea_stats);
  326. irq_received = 1;
  327. }
  328. if (pipeb_stats & 0x8000ffff) {
  329. if (pipeb_stats & PIPE_FIFO_UNDERRUN_STATUS)
  330. DRM_DEBUG("pipe b underrun\n");
  331. I915_WRITE(PIPEBSTAT, pipeb_stats);
  332. irq_received = 1;
  333. }
  334. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  335. if (!irq_received)
  336. break;
  337. ret = IRQ_HANDLED;
  338. /* Consume port. Then clear IIR or we'll miss events */
  339. if ((I915_HAS_HOTPLUG(dev)) &&
  340. (iir & I915_DISPLAY_PORT_INTERRUPT)) {
  341. u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
  342. DRM_DEBUG("hotplug event received, stat 0x%08x\n",
  343. hotplug_status);
  344. if (hotplug_status & dev_priv->hotplug_supported_mask)
  345. schedule_work(&dev_priv->hotplug_work);
  346. I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
  347. I915_READ(PORT_HOTPLUG_STAT);
  348. }
  349. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT) {
  350. u32 eir = I915_READ(EIR);
  351. i915_capture_error_state(dev);
  352. printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
  353. eir);
  354. if (eir & I915_ERROR_PAGE_TABLE) {
  355. u32 pgtbl_err = I915_READ(PGTBL_ER);
  356. printk(KERN_ERR "page table error\n");
  357. printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
  358. pgtbl_err);
  359. I915_WRITE(PGTBL_ER, pgtbl_err);
  360. (void)I915_READ(PGTBL_ER);
  361. }
  362. if (eir & I915_ERROR_MEMORY_REFRESH) {
  363. printk(KERN_ERR "memory refresh error\n");
  364. printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
  365. pipea_stats);
  366. printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
  367. pipeb_stats);
  368. /* pipestat has already been acked */
  369. }
  370. if (eir & I915_ERROR_INSTRUCTION) {
  371. printk(KERN_ERR "instruction error\n");
  372. printk(KERN_ERR " INSTPM: 0x%08x\n",
  373. I915_READ(INSTPM));
  374. if (!IS_I965G(dev)) {
  375. u32 ipeir = I915_READ(IPEIR);
  376. printk(KERN_ERR " IPEIR: 0x%08x\n",
  377. I915_READ(IPEIR));
  378. printk(KERN_ERR " IPEHR: 0x%08x\n",
  379. I915_READ(IPEHR));
  380. printk(KERN_ERR " INSTDONE: 0x%08x\n",
  381. I915_READ(INSTDONE));
  382. printk(KERN_ERR " ACTHD: 0x%08x\n",
  383. I915_READ(ACTHD));
  384. I915_WRITE(IPEIR, ipeir);
  385. (void)I915_READ(IPEIR);
  386. } else {
  387. u32 ipeir = I915_READ(IPEIR_I965);
  388. printk(KERN_ERR " IPEIR: 0x%08x\n",
  389. I915_READ(IPEIR_I965));
  390. printk(KERN_ERR " IPEHR: 0x%08x\n",
  391. I915_READ(IPEHR_I965));
  392. printk(KERN_ERR " INSTDONE: 0x%08x\n",
  393. I915_READ(INSTDONE_I965));
  394. printk(KERN_ERR " INSTPS: 0x%08x\n",
  395. I915_READ(INSTPS));
  396. printk(KERN_ERR " INSTDONE1: 0x%08x\n",
  397. I915_READ(INSTDONE1));
  398. printk(KERN_ERR " ACTHD: 0x%08x\n",
  399. I915_READ(ACTHD_I965));
  400. I915_WRITE(IPEIR_I965, ipeir);
  401. (void)I915_READ(IPEIR_I965);
  402. }
  403. }
  404. I915_WRITE(EIR, eir);
  405. (void)I915_READ(EIR);
  406. eir = I915_READ(EIR);
  407. if (eir) {
  408. /*
  409. * some errors might have become stuck,
  410. * mask them.
  411. */
  412. DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
  413. I915_WRITE(EMR, I915_READ(EMR) | eir);
  414. I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  415. }
  416. }
  417. I915_WRITE(IIR, iir);
  418. new_iir = I915_READ(IIR); /* Flush posted writes */
  419. if (dev->primary->master) {
  420. master_priv = dev->primary->master->driver_priv;
  421. if (master_priv->sarea_priv)
  422. master_priv->sarea_priv->last_dispatch =
  423. READ_BREADCRUMB(dev_priv);
  424. }
  425. if (iir & I915_USER_INTERRUPT) {
  426. dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
  427. DRM_WAKEUP(&dev_priv->irq_queue);
  428. }
  429. if (pipea_stats & vblank_status) {
  430. vblank++;
  431. drm_handle_vblank(dev, 0);
  432. }
  433. if (pipeb_stats & vblank_status) {
  434. vblank++;
  435. drm_handle_vblank(dev, 1);
  436. }
  437. if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
  438. (iir & I915_ASLE_INTERRUPT))
  439. opregion_asle_intr(dev);
  440. /* With MSI, interrupts are only generated when iir
  441. * transitions from zero to nonzero. If another bit got
  442. * set while we were handling the existing iir bits, then
  443. * we would never get another interrupt.
  444. *
  445. * This is fine on non-MSI as well, as if we hit this path
  446. * we avoid exiting the interrupt handler only to generate
  447. * another one.
  448. *
  449. * Note that for MSI this could cause a stray interrupt report
  450. * if an interrupt landed in the time between writing IIR and
  451. * the posting read. This should be rare enough to never
  452. * trigger the 99% of 100,000 interrupts test for disabling
  453. * stray interrupts.
  454. */
  455. iir = new_iir;
  456. }
  457. return ret;
  458. }
  459. static int i915_emit_irq(struct drm_device * dev)
  460. {
  461. drm_i915_private_t *dev_priv = dev->dev_private;
  462. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  463. RING_LOCALS;
  464. i915_kernel_lost_context(dev);
  465. DRM_DEBUG("\n");
  466. dev_priv->counter++;
  467. if (dev_priv->counter > 0x7FFFFFFFUL)
  468. dev_priv->counter = 1;
  469. if (master_priv->sarea_priv)
  470. master_priv->sarea_priv->last_enqueue = dev_priv->counter;
  471. BEGIN_LP_RING(4);
  472. OUT_RING(MI_STORE_DWORD_INDEX);
  473. OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  474. OUT_RING(dev_priv->counter);
  475. OUT_RING(MI_USER_INTERRUPT);
  476. ADVANCE_LP_RING();
  477. return dev_priv->counter;
  478. }
  479. void i915_user_irq_get(struct drm_device *dev)
  480. {
  481. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  482. unsigned long irqflags;
  483. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  484. if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
  485. if (IS_IGDNG(dev))
  486. igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
  487. else
  488. i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
  489. }
  490. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  491. }
  492. void i915_user_irq_put(struct drm_device *dev)
  493. {
  494. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  495. unsigned long irqflags;
  496. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  497. BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
  498. if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
  499. if (IS_IGDNG(dev))
  500. igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
  501. else
  502. i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
  503. }
  504. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  505. }
  506. static int i915_wait_irq(struct drm_device * dev, int irq_nr)
  507. {
  508. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  509. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  510. int ret = 0;
  511. DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
  512. READ_BREADCRUMB(dev_priv));
  513. if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
  514. if (master_priv->sarea_priv)
  515. master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
  516. return 0;
  517. }
  518. if (master_priv->sarea_priv)
  519. master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  520. i915_user_irq_get(dev);
  521. DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
  522. READ_BREADCRUMB(dev_priv) >= irq_nr);
  523. i915_user_irq_put(dev);
  524. if (ret == -EBUSY) {
  525. DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
  526. READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
  527. }
  528. return ret;
  529. }
  530. /* Needs the lock as it touches the ring.
  531. */
  532. int i915_irq_emit(struct drm_device *dev, void *data,
  533. struct drm_file *file_priv)
  534. {
  535. drm_i915_private_t *dev_priv = dev->dev_private;
  536. drm_i915_irq_emit_t *emit = data;
  537. int result;
  538. if (!dev_priv || !dev_priv->ring.virtual_start) {
  539. DRM_ERROR("called with no initialization\n");
  540. return -EINVAL;
  541. }
  542. RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
  543. mutex_lock(&dev->struct_mutex);
  544. result = i915_emit_irq(dev);
  545. mutex_unlock(&dev->struct_mutex);
  546. if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
  547. DRM_ERROR("copy_to_user\n");
  548. return -EFAULT;
  549. }
  550. return 0;
  551. }
  552. /* Doesn't need the hardware lock.
  553. */
  554. int i915_irq_wait(struct drm_device *dev, void *data,
  555. struct drm_file *file_priv)
  556. {
  557. drm_i915_private_t *dev_priv = dev->dev_private;
  558. drm_i915_irq_wait_t *irqwait = data;
  559. if (!dev_priv) {
  560. DRM_ERROR("called with no initialization\n");
  561. return -EINVAL;
  562. }
  563. return i915_wait_irq(dev, irqwait->irq_seq);
  564. }
  565. /* Called from drm generic code, passed 'crtc' which
  566. * we use as a pipe index
  567. */
  568. int i915_enable_vblank(struct drm_device *dev, int pipe)
  569. {
  570. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  571. unsigned long irqflags;
  572. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  573. u32 pipeconf;
  574. pipeconf = I915_READ(pipeconf_reg);
  575. if (!(pipeconf & PIPEACONF_ENABLE))
  576. return -EINVAL;
  577. if (IS_IGDNG(dev))
  578. return 0;
  579. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  580. if (IS_I965G(dev))
  581. i915_enable_pipestat(dev_priv, pipe,
  582. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  583. else
  584. i915_enable_pipestat(dev_priv, pipe,
  585. PIPE_VBLANK_INTERRUPT_ENABLE);
  586. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  587. return 0;
  588. }
  589. /* Called from drm generic code, passed 'crtc' which
  590. * we use as a pipe index
  591. */
  592. void i915_disable_vblank(struct drm_device *dev, int pipe)
  593. {
  594. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  595. unsigned long irqflags;
  596. if (IS_IGDNG(dev))
  597. return;
  598. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  599. i915_disable_pipestat(dev_priv, pipe,
  600. PIPE_VBLANK_INTERRUPT_ENABLE |
  601. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  602. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  603. }
  604. void i915_enable_interrupt (struct drm_device *dev)
  605. {
  606. struct drm_i915_private *dev_priv = dev->dev_private;
  607. if (!IS_IGDNG(dev))
  608. opregion_enable_asle(dev);
  609. dev_priv->irq_enabled = 1;
  610. }
  611. /* Set the vblank monitor pipe
  612. */
  613. int i915_vblank_pipe_set(struct drm_device *dev, void *data,
  614. struct drm_file *file_priv)
  615. {
  616. drm_i915_private_t *dev_priv = dev->dev_private;
  617. if (!dev_priv) {
  618. DRM_ERROR("called with no initialization\n");
  619. return -EINVAL;
  620. }
  621. return 0;
  622. }
  623. int i915_vblank_pipe_get(struct drm_device *dev, void *data,
  624. struct drm_file *file_priv)
  625. {
  626. drm_i915_private_t *dev_priv = dev->dev_private;
  627. drm_i915_vblank_pipe_t *pipe = data;
  628. if (!dev_priv) {
  629. DRM_ERROR("called with no initialization\n");
  630. return -EINVAL;
  631. }
  632. pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  633. return 0;
  634. }
  635. /**
  636. * Schedule buffer swap at given vertical blank.
  637. */
  638. int i915_vblank_swap(struct drm_device *dev, void *data,
  639. struct drm_file *file_priv)
  640. {
  641. /* The delayed swap mechanism was fundamentally racy, and has been
  642. * removed. The model was that the client requested a delayed flip/swap
  643. * from the kernel, then waited for vblank before continuing to perform
  644. * rendering. The problem was that the kernel might wake the client
  645. * up before it dispatched the vblank swap (since the lock has to be
  646. * held while touching the ringbuffer), in which case the client would
  647. * clear and start the next frame before the swap occurred, and
  648. * flicker would occur in addition to likely missing the vblank.
  649. *
  650. * In the absence of this ioctl, userland falls back to a correct path
  651. * of waiting for a vblank, then dispatching the swap on its own.
  652. * Context switching to userland and back is plenty fast enough for
  653. * meeting the requirements of vblank swapping.
  654. */
  655. return -EINVAL;
  656. }
  657. /* drm_dma.h hooks
  658. */
  659. static void igdng_irq_preinstall(struct drm_device *dev)
  660. {
  661. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  662. I915_WRITE(HWSTAM, 0xeffe);
  663. /* XXX hotplug from PCH */
  664. I915_WRITE(DEIMR, 0xffffffff);
  665. I915_WRITE(DEIER, 0x0);
  666. (void) I915_READ(DEIER);
  667. /* and GT */
  668. I915_WRITE(GTIMR, 0xffffffff);
  669. I915_WRITE(GTIER, 0x0);
  670. (void) I915_READ(GTIER);
  671. }
  672. static int igdng_irq_postinstall(struct drm_device *dev)
  673. {
  674. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  675. /* enable kind of interrupts always enabled */
  676. u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
  677. u32 render_mask = GT_USER_INTERRUPT;
  678. dev_priv->irq_mask_reg = ~display_mask;
  679. dev_priv->de_irq_enable_reg = display_mask;
  680. /* should always can generate irq */
  681. I915_WRITE(DEIIR, I915_READ(DEIIR));
  682. I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
  683. I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
  684. (void) I915_READ(DEIER);
  685. /* user interrupt should be enabled, but masked initial */
  686. dev_priv->gt_irq_mask_reg = 0xffffffff;
  687. dev_priv->gt_irq_enable_reg = render_mask;
  688. I915_WRITE(GTIIR, I915_READ(GTIIR));
  689. I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
  690. I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
  691. (void) I915_READ(GTIER);
  692. return 0;
  693. }
  694. void i915_driver_irq_preinstall(struct drm_device * dev)
  695. {
  696. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  697. atomic_set(&dev_priv->irq_received, 0);
  698. INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
  699. if (IS_IGDNG(dev)) {
  700. igdng_irq_preinstall(dev);
  701. return;
  702. }
  703. if (I915_HAS_HOTPLUG(dev)) {
  704. I915_WRITE(PORT_HOTPLUG_EN, 0);
  705. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  706. }
  707. I915_WRITE(HWSTAM, 0xeffe);
  708. I915_WRITE(PIPEASTAT, 0);
  709. I915_WRITE(PIPEBSTAT, 0);
  710. I915_WRITE(IMR, 0xffffffff);
  711. I915_WRITE(IER, 0x0);
  712. (void) I915_READ(IER);
  713. }
  714. int i915_driver_irq_postinstall(struct drm_device *dev)
  715. {
  716. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  717. u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
  718. u32 error_mask;
  719. DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
  720. dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  721. if (IS_IGDNG(dev))
  722. return igdng_irq_postinstall(dev);
  723. /* Unmask the interrupts that we always want on. */
  724. dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
  725. dev_priv->pipestat[0] = 0;
  726. dev_priv->pipestat[1] = 0;
  727. if (I915_HAS_HOTPLUG(dev)) {
  728. u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
  729. /* Leave other bits alone */
  730. hotplug_en |= HOTPLUG_EN_MASK;
  731. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  732. dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
  733. TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
  734. SDVOB_HOTPLUG_INT_STATUS;
  735. if (IS_G4X(dev)) {
  736. dev_priv->hotplug_supported_mask |=
  737. HDMIB_HOTPLUG_INT_STATUS |
  738. HDMIC_HOTPLUG_INT_STATUS |
  739. HDMID_HOTPLUG_INT_STATUS;
  740. }
  741. /* Enable in IER... */
  742. enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
  743. /* and unmask in IMR */
  744. i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
  745. }
  746. /*
  747. * Enable some error detection, note the instruction error mask
  748. * bit is reserved, so we leave it masked.
  749. */
  750. if (IS_G4X(dev)) {
  751. error_mask = ~(GM45_ERROR_PAGE_TABLE |
  752. GM45_ERROR_MEM_PRIV |
  753. GM45_ERROR_CP_PRIV |
  754. I915_ERROR_MEMORY_REFRESH);
  755. } else {
  756. error_mask = ~(I915_ERROR_PAGE_TABLE |
  757. I915_ERROR_MEMORY_REFRESH);
  758. }
  759. I915_WRITE(EMR, error_mask);
  760. /* Disable pipe interrupt enables, clear pending pipe status */
  761. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  762. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  763. /* Clear pending interrupt status */
  764. I915_WRITE(IIR, I915_READ(IIR));
  765. I915_WRITE(IER, enable_mask);
  766. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  767. (void) I915_READ(IER);
  768. opregion_enable_asle(dev);
  769. return 0;
  770. }
  771. static void igdng_irq_uninstall(struct drm_device *dev)
  772. {
  773. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  774. I915_WRITE(HWSTAM, 0xffffffff);
  775. I915_WRITE(DEIMR, 0xffffffff);
  776. I915_WRITE(DEIER, 0x0);
  777. I915_WRITE(DEIIR, I915_READ(DEIIR));
  778. I915_WRITE(GTIMR, 0xffffffff);
  779. I915_WRITE(GTIER, 0x0);
  780. I915_WRITE(GTIIR, I915_READ(GTIIR));
  781. }
  782. void i915_driver_irq_uninstall(struct drm_device * dev)
  783. {
  784. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  785. if (!dev_priv)
  786. return;
  787. dev_priv->vblank_pipe = 0;
  788. if (IS_IGDNG(dev)) {
  789. igdng_irq_uninstall(dev);
  790. return;
  791. }
  792. if (I915_HAS_HOTPLUG(dev)) {
  793. I915_WRITE(PORT_HOTPLUG_EN, 0);
  794. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  795. }
  796. I915_WRITE(HWSTAM, 0xffffffff);
  797. I915_WRITE(PIPEASTAT, 0);
  798. I915_WRITE(PIPEBSTAT, 0);
  799. I915_WRITE(IMR, 0xffffffff);
  800. I915_WRITE(IER, 0x0);
  801. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  802. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  803. I915_WRITE(IIR, I915_READ(IIR));
  804. }