i915_irq.c 28 KB

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