radeon_irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* radeon_irq.c -- IRQ handling for radeon -*- linux-c -*- */
  2. /*
  3. * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
  4. *
  5. * The Weather Channel (TM) funded Tungsten Graphics to develop the
  6. * initial release of the Radeon 8500 driver under the XFree86 license.
  7. * This notice must be preserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. *
  28. * Authors:
  29. * Keith Whitwell <keith@tungstengraphics.com>
  30. * Michel D�zer <michel@daenzer.net>
  31. */
  32. #include "drmP.h"
  33. #include "drm.h"
  34. #include "radeon_drm.h"
  35. #include "radeon_drv.h"
  36. void radeon_irq_set_state(struct drm_device *dev, u32 mask, int state)
  37. {
  38. drm_radeon_private_t *dev_priv = dev->dev_private;
  39. if (state)
  40. dev_priv->irq_enable_reg |= mask;
  41. else
  42. dev_priv->irq_enable_reg &= ~mask;
  43. if (dev->irq_enabled)
  44. RADEON_WRITE(RADEON_GEN_INT_CNTL, dev_priv->irq_enable_reg);
  45. }
  46. static void r500_vbl_irq_set_state(struct drm_device *dev, u32 mask, int state)
  47. {
  48. drm_radeon_private_t *dev_priv = dev->dev_private;
  49. if (state)
  50. dev_priv->r500_disp_irq_reg |= mask;
  51. else
  52. dev_priv->r500_disp_irq_reg &= ~mask;
  53. if (dev->irq_enabled)
  54. RADEON_WRITE(R500_DxMODE_INT_MASK, dev_priv->r500_disp_irq_reg);
  55. }
  56. int radeon_enable_vblank(struct drm_device *dev, int crtc)
  57. {
  58. drm_radeon_private_t *dev_priv = dev->dev_private;
  59. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600) {
  60. switch (crtc) {
  61. case 0:
  62. r500_vbl_irq_set_state(dev, R500_D1MODE_INT_MASK, 1);
  63. break;
  64. case 1:
  65. r500_vbl_irq_set_state(dev, R500_D2MODE_INT_MASK, 1);
  66. break;
  67. default:
  68. DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
  69. crtc);
  70. return -EINVAL;
  71. }
  72. } else {
  73. switch (crtc) {
  74. case 0:
  75. radeon_irq_set_state(dev, RADEON_CRTC_VBLANK_MASK, 1);
  76. break;
  77. case 1:
  78. radeon_irq_set_state(dev, RADEON_CRTC2_VBLANK_MASK, 1);
  79. break;
  80. default:
  81. DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
  82. crtc);
  83. return -EINVAL;
  84. }
  85. }
  86. return 0;
  87. }
  88. void radeon_disable_vblank(struct drm_device *dev, int crtc)
  89. {
  90. drm_radeon_private_t *dev_priv = dev->dev_private;
  91. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600) {
  92. switch (crtc) {
  93. case 0:
  94. r500_vbl_irq_set_state(dev, R500_D1MODE_INT_MASK, 0);
  95. break;
  96. case 1:
  97. r500_vbl_irq_set_state(dev, R500_D2MODE_INT_MASK, 0);
  98. break;
  99. default:
  100. DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
  101. crtc);
  102. break;
  103. }
  104. } else {
  105. switch (crtc) {
  106. case 0:
  107. radeon_irq_set_state(dev, RADEON_CRTC_VBLANK_MASK, 0);
  108. break;
  109. case 1:
  110. radeon_irq_set_state(dev, RADEON_CRTC2_VBLANK_MASK, 0);
  111. break;
  112. default:
  113. DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
  114. crtc);
  115. break;
  116. }
  117. }
  118. }
  119. static inline u32 radeon_acknowledge_irqs(drm_radeon_private_t *dev_priv, u32 *r500_disp_int)
  120. {
  121. u32 irqs = RADEON_READ(RADEON_GEN_INT_STATUS);
  122. u32 irq_mask = RADEON_SW_INT_TEST;
  123. *r500_disp_int = 0;
  124. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600) {
  125. /* vbl interrupts in a different place */
  126. if (irqs & R500_DISPLAY_INT_STATUS) {
  127. /* if a display interrupt */
  128. u32 disp_irq;
  129. disp_irq = RADEON_READ(R500_DISP_INTERRUPT_STATUS);
  130. *r500_disp_int = disp_irq;
  131. if (disp_irq & R500_D1_VBLANK_INTERRUPT)
  132. RADEON_WRITE(R500_D1MODE_VBLANK_STATUS, R500_VBLANK_ACK);
  133. if (disp_irq & R500_D2_VBLANK_INTERRUPT)
  134. RADEON_WRITE(R500_D2MODE_VBLANK_STATUS, R500_VBLANK_ACK);
  135. }
  136. irq_mask |= R500_DISPLAY_INT_STATUS;
  137. } else
  138. irq_mask |= RADEON_CRTC_VBLANK_STAT | RADEON_CRTC2_VBLANK_STAT;
  139. irqs &= irq_mask;
  140. if (irqs)
  141. RADEON_WRITE(RADEON_GEN_INT_STATUS, irqs);
  142. return irqs;
  143. }
  144. /* Interrupts - Used for device synchronization and flushing in the
  145. * following circumstances:
  146. *
  147. * - Exclusive FB access with hw idle:
  148. * - Wait for GUI Idle (?) interrupt, then do normal flush.
  149. *
  150. * - Frame throttling, NV_fence:
  151. * - Drop marker irq's into command stream ahead of time.
  152. * - Wait on irq's with lock *not held*
  153. * - Check each for termination condition
  154. *
  155. * - Internally in cp_getbuffer, etc:
  156. * - as above, but wait with lock held???
  157. *
  158. * NOTE: These functions are misleadingly named -- the irq's aren't
  159. * tied to dma at all, this is just a hangover from dri prehistory.
  160. */
  161. irqreturn_t radeon_driver_irq_handler(DRM_IRQ_ARGS)
  162. {
  163. struct drm_device *dev = (struct drm_device *) arg;
  164. drm_radeon_private_t *dev_priv =
  165. (drm_radeon_private_t *) dev->dev_private;
  166. u32 stat;
  167. u32 r500_disp_int;
  168. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  169. return IRQ_NONE;
  170. /* Only consider the bits we're interested in - others could be used
  171. * outside the DRM
  172. */
  173. stat = radeon_acknowledge_irqs(dev_priv, &r500_disp_int);
  174. if (!stat)
  175. return IRQ_NONE;
  176. stat &= dev_priv->irq_enable_reg;
  177. /* SW interrupt */
  178. if (stat & RADEON_SW_INT_TEST)
  179. DRM_WAKEUP(&dev_priv->swi_queue);
  180. /* VBLANK interrupt */
  181. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600) {
  182. if (r500_disp_int & R500_D1_VBLANK_INTERRUPT)
  183. drm_handle_vblank(dev, 0);
  184. if (r500_disp_int & R500_D2_VBLANK_INTERRUPT)
  185. drm_handle_vblank(dev, 1);
  186. } else {
  187. if (stat & RADEON_CRTC_VBLANK_STAT)
  188. drm_handle_vblank(dev, 0);
  189. if (stat & RADEON_CRTC2_VBLANK_STAT)
  190. drm_handle_vblank(dev, 1);
  191. }
  192. return IRQ_HANDLED;
  193. }
  194. static int radeon_emit_irq(struct drm_device * dev)
  195. {
  196. drm_radeon_private_t *dev_priv = dev->dev_private;
  197. unsigned int ret;
  198. RING_LOCALS;
  199. atomic_inc(&dev_priv->swi_emitted);
  200. ret = atomic_read(&dev_priv->swi_emitted);
  201. BEGIN_RING(4);
  202. OUT_RING_REG(RADEON_LAST_SWI_REG, ret);
  203. OUT_RING_REG(RADEON_GEN_INT_STATUS, RADEON_SW_INT_FIRE);
  204. ADVANCE_RING();
  205. COMMIT_RING();
  206. return ret;
  207. }
  208. static int radeon_wait_irq(struct drm_device * dev, int swi_nr)
  209. {
  210. drm_radeon_private_t *dev_priv =
  211. (drm_radeon_private_t *) dev->dev_private;
  212. int ret = 0;
  213. if (RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr)
  214. return 0;
  215. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  216. DRM_WAIT_ON(ret, dev_priv->swi_queue, 3 * DRM_HZ,
  217. RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr);
  218. return ret;
  219. }
  220. u32 radeon_get_vblank_counter(struct drm_device *dev, int crtc)
  221. {
  222. drm_radeon_private_t *dev_priv = dev->dev_private;
  223. if (!dev_priv) {
  224. DRM_ERROR("called with no initialization\n");
  225. return -EINVAL;
  226. }
  227. if (crtc < 0 || crtc > 1) {
  228. DRM_ERROR("Invalid crtc %d\n", crtc);
  229. return -EINVAL;
  230. }
  231. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600) {
  232. if (crtc == 0)
  233. return RADEON_READ(R500_D1CRTC_FRAME_COUNT);
  234. else
  235. return RADEON_READ(R500_D2CRTC_FRAME_COUNT);
  236. } else {
  237. if (crtc == 0)
  238. return RADEON_READ(RADEON_CRTC_CRNT_FRAME);
  239. else
  240. return RADEON_READ(RADEON_CRTC2_CRNT_FRAME);
  241. }
  242. }
  243. /* Needs the lock as it touches the ring.
  244. */
  245. int radeon_irq_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
  246. {
  247. drm_radeon_private_t *dev_priv = dev->dev_private;
  248. drm_radeon_irq_emit_t *emit = data;
  249. int result;
  250. if (!dev_priv) {
  251. DRM_ERROR("called with no initialization\n");
  252. return -EINVAL;
  253. }
  254. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  255. return -EINVAL;
  256. LOCK_TEST_WITH_RETURN(dev, file_priv);
  257. result = radeon_emit_irq(dev);
  258. if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
  259. DRM_ERROR("copy_to_user\n");
  260. return -EFAULT;
  261. }
  262. return 0;
  263. }
  264. /* Doesn't need the hardware lock.
  265. */
  266. int radeon_irq_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
  267. {
  268. drm_radeon_private_t *dev_priv = dev->dev_private;
  269. drm_radeon_irq_wait_t *irqwait = data;
  270. if (!dev_priv) {
  271. DRM_ERROR("called with no initialization\n");
  272. return -EINVAL;
  273. }
  274. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  275. return -EINVAL;
  276. return radeon_wait_irq(dev, irqwait->irq_seq);
  277. }
  278. /* drm_dma.h hooks
  279. */
  280. void radeon_driver_irq_preinstall(struct drm_device * dev)
  281. {
  282. drm_radeon_private_t *dev_priv =
  283. (drm_radeon_private_t *) dev->dev_private;
  284. u32 dummy;
  285. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  286. return;
  287. /* Disable *all* interrupts */
  288. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600)
  289. RADEON_WRITE(R500_DxMODE_INT_MASK, 0);
  290. RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
  291. /* Clear bits if they're already high */
  292. radeon_acknowledge_irqs(dev_priv, &dummy);
  293. }
  294. int radeon_driver_irq_postinstall(struct drm_device *dev)
  295. {
  296. drm_radeon_private_t *dev_priv =
  297. (drm_radeon_private_t *) dev->dev_private;
  298. atomic_set(&dev_priv->swi_emitted, 0);
  299. DRM_INIT_WAITQUEUE(&dev_priv->swi_queue);
  300. dev->max_vblank_count = 0x001fffff;
  301. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  302. return 0;
  303. radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
  304. return 0;
  305. }
  306. void radeon_driver_irq_uninstall(struct drm_device * dev)
  307. {
  308. drm_radeon_private_t *dev_priv =
  309. (drm_radeon_private_t *) dev->dev_private;
  310. if (!dev_priv)
  311. return;
  312. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  313. return;
  314. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600)
  315. RADEON_WRITE(R500_DxMODE_INT_MASK, 0);
  316. /* Disable *all* interrupts */
  317. RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
  318. }
  319. int radeon_vblank_crtc_get(struct drm_device *dev)
  320. {
  321. drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
  322. return dev_priv->vblank_crtc;
  323. }
  324. int radeon_vblank_crtc_set(struct drm_device *dev, int64_t value)
  325. {
  326. drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
  327. if (value & ~(DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
  328. DRM_ERROR("called with invalid crtc 0x%x\n", (unsigned int)value);
  329. return -EINVAL;
  330. }
  331. dev_priv->vblank_crtc = (unsigned int)value;
  332. return 0;
  333. }