i915_irq.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. }
  491. I915_WRITE(IIR, iir);
  492. new_iir = I915_READ(IIR); /* Flush posted writes */
  493. if (dev->primary->master) {
  494. master_priv = dev->primary->master->driver_priv;
  495. if (master_priv->sarea_priv)
  496. master_priv->sarea_priv->last_dispatch =
  497. READ_BREADCRUMB(dev_priv);
  498. }
  499. if (iir & I915_USER_INTERRUPT) {
  500. dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
  501. DRM_WAKEUP(&dev_priv->irq_queue);
  502. }
  503. if (pipea_stats & vblank_status) {
  504. vblank++;
  505. drm_handle_vblank(dev, 0);
  506. }
  507. if (pipeb_stats & vblank_status) {
  508. vblank++;
  509. drm_handle_vblank(dev, 1);
  510. }
  511. if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
  512. (iir & I915_ASLE_INTERRUPT))
  513. opregion_asle_intr(dev);
  514. /* With MSI, interrupts are only generated when iir
  515. * transitions from zero to nonzero. If another bit got
  516. * set while we were handling the existing iir bits, then
  517. * we would never get another interrupt.
  518. *
  519. * This is fine on non-MSI as well, as if we hit this path
  520. * we avoid exiting the interrupt handler only to generate
  521. * another one.
  522. *
  523. * Note that for MSI this could cause a stray interrupt report
  524. * if an interrupt landed in the time between writing IIR and
  525. * the posting read. This should be rare enough to never
  526. * trigger the 99% of 100,000 interrupts test for disabling
  527. * stray interrupts.
  528. */
  529. iir = new_iir;
  530. }
  531. return ret;
  532. }
  533. static int i915_emit_irq(struct drm_device * dev)
  534. {
  535. drm_i915_private_t *dev_priv = dev->dev_private;
  536. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  537. RING_LOCALS;
  538. i915_kernel_lost_context(dev);
  539. DRM_DEBUG("\n");
  540. dev_priv->counter++;
  541. if (dev_priv->counter > 0x7FFFFFFFUL)
  542. dev_priv->counter = 1;
  543. if (master_priv->sarea_priv)
  544. master_priv->sarea_priv->last_enqueue = dev_priv->counter;
  545. BEGIN_LP_RING(4);
  546. OUT_RING(MI_STORE_DWORD_INDEX);
  547. OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  548. OUT_RING(dev_priv->counter);
  549. OUT_RING(MI_USER_INTERRUPT);
  550. ADVANCE_LP_RING();
  551. return dev_priv->counter;
  552. }
  553. void i915_user_irq_get(struct drm_device *dev)
  554. {
  555. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  556. unsigned long irqflags;
  557. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  558. if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) {
  559. if (IS_IGDNG(dev))
  560. igdng_enable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
  561. else
  562. i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
  563. }
  564. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  565. }
  566. void i915_user_irq_put(struct drm_device *dev)
  567. {
  568. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  569. unsigned long irqflags;
  570. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  571. BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
  572. if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) {
  573. if (IS_IGDNG(dev))
  574. igdng_disable_graphics_irq(dev_priv, GT_USER_INTERRUPT);
  575. else
  576. i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
  577. }
  578. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  579. }
  580. static int i915_wait_irq(struct drm_device * dev, int irq_nr)
  581. {
  582. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  583. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  584. int ret = 0;
  585. DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
  586. READ_BREADCRUMB(dev_priv));
  587. if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
  588. if (master_priv->sarea_priv)
  589. master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
  590. return 0;
  591. }
  592. if (master_priv->sarea_priv)
  593. master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  594. i915_user_irq_get(dev);
  595. DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
  596. READ_BREADCRUMB(dev_priv) >= irq_nr);
  597. i915_user_irq_put(dev);
  598. if (ret == -EBUSY) {
  599. DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
  600. READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
  601. }
  602. return ret;
  603. }
  604. /* Needs the lock as it touches the ring.
  605. */
  606. int i915_irq_emit(struct drm_device *dev, void *data,
  607. struct drm_file *file_priv)
  608. {
  609. drm_i915_private_t *dev_priv = dev->dev_private;
  610. drm_i915_irq_emit_t *emit = data;
  611. int result;
  612. if (!dev_priv || !dev_priv->ring.virtual_start) {
  613. DRM_ERROR("called with no initialization\n");
  614. return -EINVAL;
  615. }
  616. RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
  617. mutex_lock(&dev->struct_mutex);
  618. result = i915_emit_irq(dev);
  619. mutex_unlock(&dev->struct_mutex);
  620. if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
  621. DRM_ERROR("copy_to_user\n");
  622. return -EFAULT;
  623. }
  624. return 0;
  625. }
  626. /* Doesn't need the hardware lock.
  627. */
  628. int i915_irq_wait(struct drm_device *dev, void *data,
  629. struct drm_file *file_priv)
  630. {
  631. drm_i915_private_t *dev_priv = dev->dev_private;
  632. drm_i915_irq_wait_t *irqwait = data;
  633. if (!dev_priv) {
  634. DRM_ERROR("called with no initialization\n");
  635. return -EINVAL;
  636. }
  637. return i915_wait_irq(dev, irqwait->irq_seq);
  638. }
  639. /* Called from drm generic code, passed 'crtc' which
  640. * we use as a pipe index
  641. */
  642. int i915_enable_vblank(struct drm_device *dev, int pipe)
  643. {
  644. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  645. unsigned long irqflags;
  646. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  647. u32 pipeconf;
  648. pipeconf = I915_READ(pipeconf_reg);
  649. if (!(pipeconf & PIPEACONF_ENABLE))
  650. return -EINVAL;
  651. if (IS_IGDNG(dev))
  652. return 0;
  653. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  654. if (IS_I965G(dev))
  655. i915_enable_pipestat(dev_priv, pipe,
  656. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  657. else
  658. i915_enable_pipestat(dev_priv, pipe,
  659. PIPE_VBLANK_INTERRUPT_ENABLE);
  660. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  661. return 0;
  662. }
  663. /* Called from drm generic code, passed 'crtc' which
  664. * we use as a pipe index
  665. */
  666. void i915_disable_vblank(struct drm_device *dev, int pipe)
  667. {
  668. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  669. unsigned long irqflags;
  670. if (IS_IGDNG(dev))
  671. return;
  672. spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
  673. i915_disable_pipestat(dev_priv, pipe,
  674. PIPE_VBLANK_INTERRUPT_ENABLE |
  675. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  676. spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
  677. }
  678. void i915_enable_interrupt (struct drm_device *dev)
  679. {
  680. struct drm_i915_private *dev_priv = dev->dev_private;
  681. if (!IS_IGDNG(dev))
  682. opregion_enable_asle(dev);
  683. dev_priv->irq_enabled = 1;
  684. }
  685. /* Set the vblank monitor pipe
  686. */
  687. int i915_vblank_pipe_set(struct drm_device *dev, void *data,
  688. struct drm_file *file_priv)
  689. {
  690. drm_i915_private_t *dev_priv = dev->dev_private;
  691. if (!dev_priv) {
  692. DRM_ERROR("called with no initialization\n");
  693. return -EINVAL;
  694. }
  695. return 0;
  696. }
  697. int i915_vblank_pipe_get(struct drm_device *dev, void *data,
  698. struct drm_file *file_priv)
  699. {
  700. drm_i915_private_t *dev_priv = dev->dev_private;
  701. drm_i915_vblank_pipe_t *pipe = data;
  702. if (!dev_priv) {
  703. DRM_ERROR("called with no initialization\n");
  704. return -EINVAL;
  705. }
  706. pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  707. return 0;
  708. }
  709. /**
  710. * Schedule buffer swap at given vertical blank.
  711. */
  712. int i915_vblank_swap(struct drm_device *dev, void *data,
  713. struct drm_file *file_priv)
  714. {
  715. /* The delayed swap mechanism was fundamentally racy, and has been
  716. * removed. The model was that the client requested a delayed flip/swap
  717. * from the kernel, then waited for vblank before continuing to perform
  718. * rendering. The problem was that the kernel might wake the client
  719. * up before it dispatched the vblank swap (since the lock has to be
  720. * held while touching the ringbuffer), in which case the client would
  721. * clear and start the next frame before the swap occurred, and
  722. * flicker would occur in addition to likely missing the vblank.
  723. *
  724. * In the absence of this ioctl, userland falls back to a correct path
  725. * of waiting for a vblank, then dispatching the swap on its own.
  726. * Context switching to userland and back is plenty fast enough for
  727. * meeting the requirements of vblank swapping.
  728. */
  729. return -EINVAL;
  730. }
  731. /* drm_dma.h hooks
  732. */
  733. static void igdng_irq_preinstall(struct drm_device *dev)
  734. {
  735. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  736. I915_WRITE(HWSTAM, 0xeffe);
  737. /* XXX hotplug from PCH */
  738. I915_WRITE(DEIMR, 0xffffffff);
  739. I915_WRITE(DEIER, 0x0);
  740. (void) I915_READ(DEIER);
  741. /* and GT */
  742. I915_WRITE(GTIMR, 0xffffffff);
  743. I915_WRITE(GTIER, 0x0);
  744. (void) I915_READ(GTIER);
  745. }
  746. static int igdng_irq_postinstall(struct drm_device *dev)
  747. {
  748. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  749. /* enable kind of interrupts always enabled */
  750. u32 display_mask = DE_MASTER_IRQ_CONTROL /*| DE_PCH_EVENT */;
  751. u32 render_mask = GT_USER_INTERRUPT;
  752. dev_priv->irq_mask_reg = ~display_mask;
  753. dev_priv->de_irq_enable_reg = display_mask;
  754. /* should always can generate irq */
  755. I915_WRITE(DEIIR, I915_READ(DEIIR));
  756. I915_WRITE(DEIMR, dev_priv->irq_mask_reg);
  757. I915_WRITE(DEIER, dev_priv->de_irq_enable_reg);
  758. (void) I915_READ(DEIER);
  759. /* user interrupt should be enabled, but masked initial */
  760. dev_priv->gt_irq_mask_reg = 0xffffffff;
  761. dev_priv->gt_irq_enable_reg = render_mask;
  762. I915_WRITE(GTIIR, I915_READ(GTIIR));
  763. I915_WRITE(GTIMR, dev_priv->gt_irq_mask_reg);
  764. I915_WRITE(GTIER, dev_priv->gt_irq_enable_reg);
  765. (void) I915_READ(GTIER);
  766. return 0;
  767. }
  768. void i915_driver_irq_preinstall(struct drm_device * dev)
  769. {
  770. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  771. atomic_set(&dev_priv->irq_received, 0);
  772. INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
  773. INIT_WORK(&dev_priv->error_work, i915_error_work_func);
  774. if (IS_IGDNG(dev)) {
  775. igdng_irq_preinstall(dev);
  776. return;
  777. }
  778. if (I915_HAS_HOTPLUG(dev)) {
  779. I915_WRITE(PORT_HOTPLUG_EN, 0);
  780. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  781. }
  782. I915_WRITE(HWSTAM, 0xeffe);
  783. I915_WRITE(PIPEASTAT, 0);
  784. I915_WRITE(PIPEBSTAT, 0);
  785. I915_WRITE(IMR, 0xffffffff);
  786. I915_WRITE(IER, 0x0);
  787. (void) I915_READ(IER);
  788. }
  789. int i915_driver_irq_postinstall(struct drm_device *dev)
  790. {
  791. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  792. u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
  793. u32 error_mask;
  794. DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
  795. dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
  796. if (IS_IGDNG(dev))
  797. return igdng_irq_postinstall(dev);
  798. /* Unmask the interrupts that we always want on. */
  799. dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
  800. dev_priv->pipestat[0] = 0;
  801. dev_priv->pipestat[1] = 0;
  802. if (I915_HAS_HOTPLUG(dev)) {
  803. u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
  804. /* Leave other bits alone */
  805. hotplug_en |= HOTPLUG_EN_MASK;
  806. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  807. dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
  808. TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
  809. SDVOB_HOTPLUG_INT_STATUS;
  810. if (IS_G4X(dev)) {
  811. dev_priv->hotplug_supported_mask |=
  812. HDMIB_HOTPLUG_INT_STATUS |
  813. HDMIC_HOTPLUG_INT_STATUS |
  814. HDMID_HOTPLUG_INT_STATUS;
  815. }
  816. /* Enable in IER... */
  817. enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
  818. /* and unmask in IMR */
  819. i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
  820. }
  821. /*
  822. * Enable some error detection, note the instruction error mask
  823. * bit is reserved, so we leave it masked.
  824. */
  825. if (IS_G4X(dev)) {
  826. error_mask = ~(GM45_ERROR_PAGE_TABLE |
  827. GM45_ERROR_MEM_PRIV |
  828. GM45_ERROR_CP_PRIV |
  829. I915_ERROR_MEMORY_REFRESH);
  830. } else {
  831. error_mask = ~(I915_ERROR_PAGE_TABLE |
  832. I915_ERROR_MEMORY_REFRESH);
  833. }
  834. I915_WRITE(EMR, error_mask);
  835. /* Disable pipe interrupt enables, clear pending pipe status */
  836. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  837. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  838. /* Clear pending interrupt status */
  839. I915_WRITE(IIR, I915_READ(IIR));
  840. I915_WRITE(IER, enable_mask);
  841. I915_WRITE(IMR, dev_priv->irq_mask_reg);
  842. (void) I915_READ(IER);
  843. opregion_enable_asle(dev);
  844. return 0;
  845. }
  846. static void igdng_irq_uninstall(struct drm_device *dev)
  847. {
  848. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  849. I915_WRITE(HWSTAM, 0xffffffff);
  850. I915_WRITE(DEIMR, 0xffffffff);
  851. I915_WRITE(DEIER, 0x0);
  852. I915_WRITE(DEIIR, I915_READ(DEIIR));
  853. I915_WRITE(GTIMR, 0xffffffff);
  854. I915_WRITE(GTIER, 0x0);
  855. I915_WRITE(GTIIR, I915_READ(GTIIR));
  856. }
  857. void i915_driver_irq_uninstall(struct drm_device * dev)
  858. {
  859. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  860. if (!dev_priv)
  861. return;
  862. dev_priv->vblank_pipe = 0;
  863. if (IS_IGDNG(dev)) {
  864. igdng_irq_uninstall(dev);
  865. return;
  866. }
  867. if (I915_HAS_HOTPLUG(dev)) {
  868. I915_WRITE(PORT_HOTPLUG_EN, 0);
  869. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  870. }
  871. I915_WRITE(HWSTAM, 0xffffffff);
  872. I915_WRITE(PIPEASTAT, 0);
  873. I915_WRITE(PIPEBSTAT, 0);
  874. I915_WRITE(IMR, 0xffffffff);
  875. I915_WRITE(IER, 0x0);
  876. I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
  877. I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
  878. I915_WRITE(IIR, I915_READ(IIR));
  879. }