i915_irq.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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_DEBUG("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_DEBUG("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. /**
  251. * i915_error_work_func - do process context error handling work
  252. * @work: work struct
  253. *
  254. * Fire an error uevent so userspace can see that a hang or error
  255. * was detected.
  256. */
  257. static void i915_error_work_func(struct work_struct *work)
  258. {
  259. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  260. error_work);
  261. struct drm_device *dev = dev_priv->dev;
  262. char *event_string = "ERROR=1";
  263. char *envp[] = { event_string, NULL };
  264. DRM_DEBUG("generating error event\n");
  265. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
  266. }
  267. /**
  268. * i915_capture_error_state - capture an error record for later analysis
  269. * @dev: drm device
  270. *
  271. * Should be called when an error is detected (either a hang or an error
  272. * interrupt) to capture error state from the time of the error. Fills
  273. * out a structure which becomes available in debugfs for user level tools
  274. * to pick up.
  275. */
  276. static void i915_capture_error_state(struct drm_device *dev)
  277. {
  278. struct drm_i915_private *dev_priv = dev->dev_private;
  279. struct drm_i915_error_state *error;
  280. unsigned long flags;
  281. spin_lock_irqsave(&dev_priv->error_lock, flags);
  282. if (dev_priv->first_error)
  283. goto out;
  284. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  285. if (!error) {
  286. DRM_DEBUG("out ot memory, not capturing error state\n");
  287. goto out;
  288. }
  289. error->eir = I915_READ(EIR);
  290. error->pgtbl_er = I915_READ(PGTBL_ER);
  291. error->pipeastat = I915_READ(PIPEASTAT);
  292. error->pipebstat = I915_READ(PIPEBSTAT);
  293. error->instpm = I915_READ(INSTPM);
  294. if (!IS_I965G(dev)) {
  295. error->ipeir = I915_READ(IPEIR);
  296. error->ipehr = I915_READ(IPEHR);
  297. error->instdone = I915_READ(INSTDONE);
  298. error->acthd = I915_READ(ACTHD);
  299. } else {
  300. error->ipeir = I915_READ(IPEIR_I965);
  301. error->ipehr = I915_READ(IPEHR_I965);
  302. error->instdone = I915_READ(INSTDONE_I965);
  303. error->instps = I915_READ(INSTPS);
  304. error->instdone1 = I915_READ(INSTDONE1);
  305. error->acthd = I915_READ(ACTHD_I965);
  306. }
  307. do_gettimeofday(&error->time);
  308. dev_priv->first_error = error;
  309. out:
  310. spin_unlock_irqrestore(&dev_priv->error_lock, flags);
  311. }
  312. /**
  313. * i915_handle_error - handle an error interrupt
  314. * @dev: drm device
  315. *
  316. * Do some basic checking of regsiter state at error interrupt time and
  317. * dump it to the syslog. Also call i915_capture_error_state() to make
  318. * sure we get a record and make it available in debugfs. Fire a uevent
  319. * so userspace knows something bad happened (should trigger collection
  320. * of a ring dump etc.).
  321. */
  322. static void i915_handle_error(struct drm_device *dev)
  323. {
  324. struct drm_i915_private *dev_priv = dev->dev_private;
  325. u32 eir = I915_READ(EIR);
  326. u32 pipea_stats = I915_READ(PIPEASTAT);
  327. u32 pipeb_stats = I915_READ(PIPEBSTAT);
  328. i915_capture_error_state(dev);
  329. printk(KERN_ERR "render error detected, EIR: 0x%08x\n",
  330. eir);
  331. if (IS_G4X(dev)) {
  332. if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
  333. u32 ipeir = I915_READ(IPEIR_I965);
  334. printk(KERN_ERR " IPEIR: 0x%08x\n",
  335. I915_READ(IPEIR_I965));
  336. printk(KERN_ERR " IPEHR: 0x%08x\n",
  337. I915_READ(IPEHR_I965));
  338. printk(KERN_ERR " INSTDONE: 0x%08x\n",
  339. I915_READ(INSTDONE_I965));
  340. printk(KERN_ERR " INSTPS: 0x%08x\n",
  341. I915_READ(INSTPS));
  342. printk(KERN_ERR " INSTDONE1: 0x%08x\n",
  343. I915_READ(INSTDONE1));
  344. printk(KERN_ERR " ACTHD: 0x%08x\n",
  345. I915_READ(ACTHD_I965));
  346. I915_WRITE(IPEIR_I965, ipeir);
  347. (void)I915_READ(IPEIR_I965);
  348. }
  349. if (eir & GM45_ERROR_PAGE_TABLE) {
  350. u32 pgtbl_err = I915_READ(PGTBL_ER);
  351. printk(KERN_ERR "page table error\n");
  352. printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
  353. pgtbl_err);
  354. I915_WRITE(PGTBL_ER, pgtbl_err);
  355. (void)I915_READ(PGTBL_ER);
  356. }
  357. }
  358. if (IS_I9XX(dev)) {
  359. if (eir & I915_ERROR_PAGE_TABLE) {
  360. u32 pgtbl_err = I915_READ(PGTBL_ER);
  361. printk(KERN_ERR "page table error\n");
  362. printk(KERN_ERR " PGTBL_ER: 0x%08x\n",
  363. pgtbl_err);
  364. I915_WRITE(PGTBL_ER, pgtbl_err);
  365. (void)I915_READ(PGTBL_ER);
  366. }
  367. }
  368. if (eir & I915_ERROR_MEMORY_REFRESH) {
  369. printk(KERN_ERR "memory refresh error\n");
  370. printk(KERN_ERR "PIPEASTAT: 0x%08x\n",
  371. pipea_stats);
  372. printk(KERN_ERR "PIPEBSTAT: 0x%08x\n",
  373. pipeb_stats);
  374. /* pipestat has already been acked */
  375. }
  376. if (eir & I915_ERROR_INSTRUCTION) {
  377. printk(KERN_ERR "instruction error\n");
  378. printk(KERN_ERR " INSTPM: 0x%08x\n",
  379. I915_READ(INSTPM));
  380. if (!IS_I965G(dev)) {
  381. u32 ipeir = I915_READ(IPEIR);
  382. printk(KERN_ERR " IPEIR: 0x%08x\n",
  383. I915_READ(IPEIR));
  384. printk(KERN_ERR " IPEHR: 0x%08x\n",
  385. I915_READ(IPEHR));
  386. printk(KERN_ERR " INSTDONE: 0x%08x\n",
  387. I915_READ(INSTDONE));
  388. printk(KERN_ERR " ACTHD: 0x%08x\n",
  389. I915_READ(ACTHD));
  390. I915_WRITE(IPEIR, ipeir);
  391. (void)I915_READ(IPEIR);
  392. } else {
  393. u32 ipeir = I915_READ(IPEIR_I965);
  394. printk(KERN_ERR " IPEIR: 0x%08x\n",
  395. I915_READ(IPEIR_I965));
  396. printk(KERN_ERR " IPEHR: 0x%08x\n",
  397. I915_READ(IPEHR_I965));
  398. printk(KERN_ERR " INSTDONE: 0x%08x\n",
  399. I915_READ(INSTDONE_I965));
  400. printk(KERN_ERR " INSTPS: 0x%08x\n",
  401. I915_READ(INSTPS));
  402. printk(KERN_ERR " INSTDONE1: 0x%08x\n",
  403. I915_READ(INSTDONE1));
  404. printk(KERN_ERR " ACTHD: 0x%08x\n",
  405. I915_READ(ACTHD_I965));
  406. I915_WRITE(IPEIR_I965, ipeir);
  407. (void)I915_READ(IPEIR_I965);
  408. }
  409. }
  410. I915_WRITE(EIR, eir);
  411. (void)I915_READ(EIR);
  412. eir = I915_READ(EIR);
  413. if (eir) {
  414. /*
  415. * some errors might have become stuck,
  416. * mask them.
  417. */
  418. DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
  419. I915_WRITE(EMR, I915_READ(EMR) | eir);
  420. I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  421. }
  422. queue_work(dev_priv->wq, &dev_priv->error_work);
  423. }
  424. irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
  425. {
  426. struct drm_device *dev = (struct drm_device *) arg;
  427. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  428. struct drm_i915_master_private *master_priv;
  429. u32 iir, new_iir;
  430. u32 pipea_stats, pipeb_stats;
  431. u32 vblank_status;
  432. u32 vblank_enable;
  433. int vblank = 0;
  434. unsigned long irqflags;
  435. int irq_received;
  436. int ret = IRQ_NONE;
  437. atomic_inc(&dev_priv->irq_received);
  438. if (IS_IGDNG(dev))
  439. return igdng_irq_handler(dev);
  440. iir = I915_READ(IIR);
  441. if (IS_I965G(dev)) {
  442. vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
  443. vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
  444. } else {
  445. vblank_status = I915_VBLANK_INTERRUPT_STATUS;
  446. vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
  447. }
  448. for (;;) {
  449. irq_received = iir != 0;
  450. /* Can't rely on pipestat interrupt bit in iir as it might
  451. * have been cleared after the pipestat interrupt was received.
  452. * It doesn't set the bit in iir again, but it still produces
  453. * interrupts (for non-MSI).
  454. */
  455. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  456. pipea_stats = I915_READ(PIPEASTAT);
  457. pipeb_stats = I915_READ(PIPEBSTAT);
  458. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  459. i915_handle_error(dev);
  460. /*
  461. * Clear the PIPE(A|B)STAT regs before the IIR
  462. */
  463. if (pipea_stats & 0x8000ffff) {
  464. if (pipea_stats & PIPE_FIFO_UNDERRUN_STATUS)
  465. DRM_DEBUG("pipe a underrun\n");
  466. I915_WRITE(PIPEASTAT, pipea_stats);
  467. irq_received = 1;
  468. }
  469. if (pipeb_stats & 0x8000ffff) {
  470. if (pipeb_stats & PIPE_FIFO_UNDERRUN_STATUS)
  471. DRM_DEBUG("pipe b underrun\n");
  472. I915_WRITE(PIPEBSTAT, pipeb_stats);
  473. irq_received = 1;
  474. }
  475. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  476. if (!irq_received)
  477. break;
  478. ret = IRQ_HANDLED;
  479. /* Consume port. Then clear IIR or we'll miss events */
  480. if ((I915_HAS_HOTPLUG(dev)) &&
  481. (iir & I915_DISPLAY_PORT_INTERRUPT)) {
  482. u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
  483. DRM_DEBUG("hotplug event received, stat 0x%08x\n",
  484. hotplug_status);
  485. if (hotplug_status & dev_priv->hotplug_supported_mask)
  486. queue_work(dev_priv->wq,
  487. &dev_priv->hotplug_work);
  488. I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
  489. I915_READ(PORT_HOTPLUG_STAT);
  490. /* EOS interrupts occurs */
  491. if (IS_IGD(dev) &&
  492. (hotplug_status & CRT_EOS_INT_STATUS)) {
  493. u32 temp;
  494. DRM_DEBUG("EOS interrupt occurs\n");
  495. /* status is already cleared */
  496. temp = I915_READ(ADPA);
  497. temp &= ~ADPA_DAC_ENABLE;
  498. I915_WRITE(ADPA, temp);
  499. temp = I915_READ(PORT_HOTPLUG_EN);
  500. temp &= ~CRT_EOS_INT_EN;
  501. I915_WRITE(PORT_HOTPLUG_EN, temp);
  502. temp = I915_READ(PORT_HOTPLUG_STAT);
  503. if (temp & CRT_EOS_INT_STATUS)
  504. I915_WRITE(PORT_HOTPLUG_STAT,
  505. CRT_EOS_INT_STATUS);
  506. }
  507. }
  508. I915_WRITE(IIR, iir);
  509. new_iir = I915_READ(IIR); /* Flush posted writes */
  510. if (dev->primary->master) {
  511. master_priv = dev->primary->master->driver_priv;
  512. if (master_priv->sarea_priv)
  513. master_priv->sarea_priv->last_dispatch =
  514. READ_BREADCRUMB(dev_priv);
  515. }
  516. if (iir & I915_USER_INTERRUPT) {
  517. dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
  518. DRM_WAKEUP(&dev_priv->irq_queue);
  519. }
  520. if (pipea_stats & vblank_status) {
  521. vblank++;
  522. drm_handle_vblank(dev, 0);
  523. }
  524. if (pipeb_stats & vblank_status) {
  525. vblank++;
  526. drm_handle_vblank(dev, 1);
  527. }
  528. if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
  529. (iir & I915_ASLE_INTERRUPT))
  530. opregion_asle_intr(dev);
  531. /* With MSI, interrupts are only generated when iir
  532. * transitions from zero to nonzero. If another bit got
  533. * set while we were handling the existing iir bits, then
  534. * we would never get another interrupt.
  535. *
  536. * This is fine on non-MSI as well, as if we hit this path
  537. * we avoid exiting the interrupt handler only to generate
  538. * another one.
  539. *
  540. * Note that for MSI this could cause a stray interrupt report
  541. * if an interrupt landed in the time between writing IIR and
  542. * the posting read. This should be rare enough to never
  543. * trigger the 99% of 100,000 interrupts test for disabling
  544. * stray interrupts.
  545. */
  546. iir = new_iir;
  547. }
  548. return ret;
  549. }
  550. static int i915_emit_irq(struct drm_device * dev)
  551. {
  552. drm_i915_private_t *dev_priv = dev->dev_private;
  553. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  554. RING_LOCALS;
  555. i915_kernel_lost_context(dev);
  556. DRM_DEBUG("\n");
  557. dev_priv->counter++;
  558. if (dev_priv->counter > 0x7FFFFFFFUL)
  559. dev_priv->counter = 1;
  560. if (master_priv->sarea_priv)
  561. master_priv->sarea_priv->last_enqueue = dev_priv->counter;
  562. BEGIN_LP_RING(4);
  563. OUT_RING(MI_STORE_DWORD_INDEX);
  564. OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  565. OUT_RING(dev_priv->counter);
  566. OUT_RING(MI_USER_INTERRUPT);
  567. ADVANCE_LP_RING();
  568. return dev_priv->counter;
  569. }
  570. void i915_user_irq_get(struct drm_device *dev)
  571. {
  572. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  573. unsigned long irqflags;
  574. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  575. if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
  576. if (IS_IGDNG(dev))
  577. igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
  578. else
  579. i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
  580. }
  581. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  582. }
  583. void i915_user_irq_put(struct drm_device *dev)
  584. {
  585. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  586. unsigned long irqflags;
  587. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  588. BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
  589. if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
  590. if (IS_IGDNG(dev))
  591. igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
  592. else
  593. i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
  594. }
  595. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  596. }
  597. static int i915_wait_irq(struct drm_device * dev, int irq_nr)
  598. {
  599. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  600. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  601. int ret = 0;
  602. DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
  603. READ_BREADCRUMB(dev_priv));
  604. if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
  605. if (master_priv->sarea_priv)
  606. master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
  607. return 0;
  608. }
  609. if (master_priv->sarea_priv)
  610. master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  611. i915_user_irq_get(dev);
  612. DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
  613. READ_BREADCRUMB(dev_priv) >= irq_nr);
  614. i915_user_irq_put(dev);
  615. if (ret == -EBUSY) {
  616. DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
  617. READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
  618. }
  619. return ret;
  620. }
  621. /* Needs the lock as it touches the ring.
  622. */
  623. int i915_irq_emit(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_irq_emit_t *emit = data;
  628. int result;
  629. if (!dev_priv || !dev_priv->ring.virtual_start) {
  630. DRM_ERROR("called with no initialization\n");
  631. return -EINVAL;
  632. }
  633. RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
  634. mutex_lock(&dev->struct_mutex);
  635. result = i915_emit_irq(dev);
  636. mutex_unlock(&dev->struct_mutex);
  637. if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
  638. DRM_ERROR("copy_to_user\n");
  639. return -EFAULT;
  640. }
  641. return 0;
  642. }
  643. /* Doesn't need the hardware lock.
  644. */
  645. int i915_irq_wait(struct drm_device *dev, void *data,
  646. struct drm_file *file_priv)
  647. {
  648. drm_i915_private_t *dev_priv = dev->dev_private;
  649. drm_i915_irq_wait_t *irqwait = data;
  650. if (!dev_priv) {
  651. DRM_ERROR("called with no initialization\n");
  652. return -EINVAL;
  653. }
  654. return i915_wait_irq(dev, irqwait->irq_seq);
  655. }
  656. /* Called from drm generic code, passed 'crtc' which
  657. * we use as a pipe index
  658. */
  659. int i915_enable_vblank(struct drm_device *dev, int pipe)
  660. {
  661. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  662. unsigned long irqflags;
  663. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  664. u32 pipeconf;
  665. pipeconf = I915_READ(pipeconf_reg);
  666. if (!(pipeconf & PIPEACONF_ENABLE))
  667. return -EINVAL;
  668. if (IS_IGDNG(dev))
  669. return 0;
  670. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  671. if (IS_I965G(dev))
  672. i915_enable_pipestat(dev_priv, pipe,
  673. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  674. else
  675. i915_enable_pipestat(dev_priv, pipe,
  676. PIPE_VBLANK_INTERRUPT_ENABLE);
  677. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  678. return 0;
  679. }
  680. /* Called from drm generic code, passed 'crtc' which
  681. * we use as a pipe index
  682. */
  683. void i915_disable_vblank(struct drm_device *dev, int pipe)
  684. {
  685. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  686. unsigned long irqflags;
  687. if (IS_IGDNG(dev))
  688. return;
  689. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  690. i915_disable_pipestat(dev_priv, pipe,
  691. PIPE_VBLANK_INTERRUPT_ENABLE |
  692. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  693. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  694. }
  695. void i915_enable_interrupt (struct drm_device *dev)
  696. {
  697. struct drm_i915_private *dev_priv = dev->dev_private;
  698. if (!IS_IGDNG(dev))
  699. opregion_enable_asle(dev);
  700. dev_priv->irq_enabled = 1;
  701. }
  702. /* Set the vblank monitor pipe
  703. */
  704. int i915_vblank_pipe_set(struct drm_device *dev, void *data,
  705. struct drm_file *file_priv)
  706. {
  707. drm_i915_private_t *dev_priv = dev->dev_private;
  708. if (!dev_priv) {
  709. DRM_ERROR("called with no initialization\n");
  710. return -EINVAL;
  711. }
  712. return 0;
  713. }
  714. int i915_vblank_pipe_get(struct drm_device *dev, void *data,
  715. struct drm_file *file_priv)
  716. {
  717. drm_i915_private_t *dev_priv = dev->dev_private;
  718. drm_i915_vblank_pipe_t *pipe = data;
  719. if (!dev_priv) {
  720. DRM_ERROR("called with no initialization\n");
  721. return -EINVAL;
  722. }
  723. pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  724. return 0;
  725. }
  726. /**
  727. * Schedule buffer swap at given vertical blank.
  728. */
  729. int i915_vblank_swap(struct drm_device *dev, void *data,
  730. struct drm_file *file_priv)
  731. {
  732. /* The delayed swap mechanism was fundamentally racy, and has been
  733. * removed. The model was that the client requested a delayed flip/swap
  734. * from the kernel, then waited for vblank before continuing to perform
  735. * rendering. The problem was that the kernel might wake the client
  736. * up before it dispatched the vblank swap (since the lock has to be
  737. * held while touching the ringbuffer), in which case the client would
  738. * clear and start the next frame before the swap occurred, and
  739. * flicker would occur in addition to likely missing the vblank.
  740. *
  741. * In the absence of this ioctl, userland falls back to a correct path
  742. * of waiting for a vblank, then dispatching the swap on its own.
  743. * Context switching to userland and back is plenty fast enough for
  744. * meeting the requirements of vblank swapping.
  745. */
  746. return -EINVAL;
  747. }
  748. /* drm_dma.h hooks
  749. */
  750. static void igdng_irq_preinstall(struct drm_device *dev)
  751. {
  752. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  753. I915_WRITE(HWSTAM, 0xeffe);
  754. /* XXX hotplug from PCH */
  755. I915_WRITE(DEIMR, 0xffffffff);
  756. I915_WRITE(DEIER, 0x0);
  757. (void) I915_READ(DEIER);
  758. /* and GT */
  759. I915_WRITE(GTIMR, 0xffffffff);
  760. I915_WRITE(GTIER, 0x0);
  761. (void) I915_READ(GTIER);
  762. }
  763. static int igdng_irq_postinstall(struct drm_device *dev)
  764. {
  765. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  766. /* enable kind of interrupts always enabled */
  767. u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
  768. u32 render_mask = GT_USER_INTERRUPT;
  769. dev_priv->irq_mask_reg = ~display_mask;
  770. dev_priv->de_irq_enable_reg = display_mask;
  771. /* should always can generate irq */
  772. I915_WRITE(DEIIR, I915_READ(DEIIR));
  773. I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
  774. I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
  775. (void) I915_READ(DEIER);
  776. /* user interrupt should be enabled, but masked initial */
  777. dev_priv->gt_irq_mask_reg = 0xffffffff;
  778. dev_priv->gt_irq_enable_reg = render_mask;
  779. I915_WRITE(GTIIR, I915_READ(GTIIR));
  780. I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
  781. I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
  782. (void) I915_READ(GTIER);
  783. return 0;
  784. }
  785. void i915_driver_irq_preinstall(struct drm_device * dev)
  786. {
  787. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  788. atomic_set(&dev_priv->irq_received, 0);
  789. INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
  790. INIT_WORK(&dev_priv->error_work, i915_error_work_func);
  791. if (IS_IGDNG(dev)) {
  792. igdng_irq_preinstall(dev);
  793. return;
  794. }
  795. if (I915_HAS_HOTPLUG(dev)) {
  796. I915_WRITE(PORT_HOTPLUG_EN, 0);
  797. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  798. }
  799. I915_WRITE(HWSTAM, 0xeffe);
  800. I915_WRITE(PIPEASTAT, 0);
  801. I915_WRITE(PIPEBSTAT, 0);
  802. I915_WRITE(IMR, 0xffffffff);
  803. I915_WRITE(IER, 0x0);
  804. (void) I915_READ(IER);
  805. }
  806. int i915_driver_irq_postinstall(struct drm_device *dev)
  807. {
  808. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  809. u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
  810. u32 error_mask;
  811. DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
  812. dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  813. if (IS_IGDNG(dev))
  814. return igdng_irq_postinstall(dev);
  815. /* Unmask the interrupts that we always want on. */
  816. dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
  817. dev_priv->pipestat[0] = 0;
  818. dev_priv->pipestat[1] = 0;
  819. if (I915_HAS_HOTPLUG(dev)) {
  820. u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
  821. /* Leave other bits alone */
  822. hotplug_en |= HOTPLUG_EN_MASK;
  823. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  824. dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
  825. TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
  826. SDVOB_HOTPLUG_INT_STATUS;
  827. if (IS_G4X(dev)) {
  828. dev_priv->hotplug_supported_mask |=
  829. HDMIB_HOTPLUG_INT_STATUS |
  830. HDMIC_HOTPLUG_INT_STATUS |
  831. HDMID_HOTPLUG_INT_STATUS;
  832. }
  833. /* Enable in IER... */
  834. enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
  835. /* and unmask in IMR */
  836. i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
  837. }
  838. /*
  839. * Enable some error detection, note the instruction error mask
  840. * bit is reserved, so we leave it masked.
  841. */
  842. if (IS_G4X(dev)) {
  843. error_mask = ~(GM45_ERROR_PAGE_TABLE |
  844. GM45_ERROR_MEM_PRIV |
  845. GM45_ERROR_CP_PRIV |
  846. I915_ERROR_MEMORY_REFRESH);
  847. } else {
  848. error_mask = ~(I915_ERROR_PAGE_TABLE |
  849. I915_ERROR_MEMORY_REFRESH);
  850. }
  851. I915_WRITE(EMR, error_mask);
  852. /* Disable pipe interrupt enables, clear pending pipe status */
  853. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  854. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  855. /* Clear pending interrupt status */
  856. I915_WRITE(IIR, I915_READ(IIR));
  857. I915_WRITE(IER, enable_mask);
  858. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  859. (void) I915_READ(IER);
  860. opregion_enable_asle(dev);
  861. return 0;
  862. }
  863. static void igdng_irq_uninstall(struct drm_device *dev)
  864. {
  865. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  866. I915_WRITE(HWSTAM, 0xffffffff);
  867. I915_WRITE(DEIMR, 0xffffffff);
  868. I915_WRITE(DEIER, 0x0);
  869. I915_WRITE(DEIIR, I915_READ(DEIIR));
  870. I915_WRITE(GTIMR, 0xffffffff);
  871. I915_WRITE(GTIER, 0x0);
  872. I915_WRITE(GTIIR, I915_READ(GTIIR));
  873. }
  874. void i915_driver_irq_uninstall(struct drm_device * dev)
  875. {
  876. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  877. if (!dev_priv)
  878. return;
  879. dev_priv->vblank_pipe = 0;
  880. if (IS_IGDNG(dev)) {
  881. igdng_irq_uninstall(dev);
  882. return;
  883. }
  884. if (I915_HAS_HOTPLUG(dev)) {
  885. I915_WRITE(PORT_HOTPLUG_EN, 0);
  886. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  887. }
  888. I915_WRITE(HWSTAM, 0xffffffff);
  889. I915_WRITE(PIPEASTAT, 0);
  890. I915_WRITE(PIPEBSTAT, 0);
  891. I915_WRITE(IMR, 0xffffffff);
  892. I915_WRITE(IER, 0x0);
  893. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  894. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  895. I915_WRITE(IIR, I915_READ(IIR));
  896. }