via_irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* via_irq.c
  2. *
  3. * Copyright 2004 BEAM Ltd.
  4. * Copyright 2002 Tungsten Graphics, Inc.
  5. * Copyright 2005 Thomas Hellstrom.
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the next
  16. * paragraph) shall be included in all copies or substantial portions of the
  17. * Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * BEAM LTD, TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * 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. * Terry Barnaby <terry1@beam.ltd.uk>
  30. * Keith Whitwell <keith@tungstengraphics.com>
  31. * Thomas Hellstrom <unichrome@shipmail.org>
  32. *
  33. * This code provides standard DRM access to the Via Unichrome / Pro Vertical blank
  34. * interrupt, as well as an infrastructure to handle other interrupts of the chip.
  35. * The refresh rate is also calculated for video playback sync purposes.
  36. */
  37. #include "drmP.h"
  38. #include "drm.h"
  39. #include "via_drm.h"
  40. #include "via_drv.h"
  41. #define VIA_REG_INTERRUPT 0x200
  42. /* VIA_REG_INTERRUPT */
  43. #define VIA_IRQ_GLOBAL (1 << 31)
  44. #define VIA_IRQ_VBLANK_ENABLE (1 << 19)
  45. #define VIA_IRQ_VBLANK_PENDING (1 << 3)
  46. #define VIA_IRQ_HQV0_ENABLE (1 << 11)
  47. #define VIA_IRQ_HQV1_ENABLE (1 << 25)
  48. #define VIA_IRQ_HQV0_PENDING (1 << 9)
  49. #define VIA_IRQ_HQV1_PENDING (1 << 10)
  50. #define VIA_IRQ_DMA0_DD_ENABLE (1 << 20)
  51. #define VIA_IRQ_DMA0_TD_ENABLE (1 << 21)
  52. #define VIA_IRQ_DMA1_DD_ENABLE (1 << 22)
  53. #define VIA_IRQ_DMA1_TD_ENABLE (1 << 23)
  54. #define VIA_IRQ_DMA0_DD_PENDING (1 << 4)
  55. #define VIA_IRQ_DMA0_TD_PENDING (1 << 5)
  56. #define VIA_IRQ_DMA1_DD_PENDING (1 << 6)
  57. #define VIA_IRQ_DMA1_TD_PENDING (1 << 7)
  58. /*
  59. * Device-specific IRQs go here. This type might need to be extended with
  60. * the register if there are multiple IRQ control registers.
  61. * Currently we activate the HQV interrupts of Unichrome Pro group A.
  62. */
  63. static maskarray_t via_pro_group_a_irqs[] = {
  64. {VIA_IRQ_HQV0_ENABLE, VIA_IRQ_HQV0_PENDING, 0x000003D0, 0x00008010,
  65. 0x00000000 },
  66. {VIA_IRQ_HQV1_ENABLE, VIA_IRQ_HQV1_PENDING, 0x000013D0, 0x00008010,
  67. 0x00000000 },
  68. {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
  69. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
  70. {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
  71. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
  72. };
  73. static int via_num_pro_group_a = ARRAY_SIZE(via_pro_group_a_irqs);
  74. static int via_irqmap_pro_group_a[] = {0, 1, -1, 2, -1, 3};
  75. static maskarray_t via_unichrome_irqs[] = {
  76. {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
  77. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
  78. {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
  79. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008}
  80. };
  81. static int via_num_unichrome = ARRAY_SIZE(via_unichrome_irqs);
  82. static int via_irqmap_unichrome[] = {-1, -1, -1, 0, -1, 1};
  83. static unsigned time_diff(struct timeval *now, struct timeval *then)
  84. {
  85. return (now->tv_usec >= then->tv_usec) ?
  86. now->tv_usec - then->tv_usec :
  87. 1000000 - (then->tv_usec - now->tv_usec);
  88. }
  89. u32 via_get_vblank_counter(struct drm_device *dev, int crtc)
  90. {
  91. drm_via_private_t *dev_priv = dev->dev_private;
  92. if (crtc != 0)
  93. return 0;
  94. return atomic_read(&dev_priv->vbl_received);
  95. }
  96. irqreturn_t via_driver_irq_handler(DRM_IRQ_ARGS)
  97. {
  98. struct drm_device *dev = (struct drm_device *) arg;
  99. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  100. u32 status;
  101. int handled = 0;
  102. struct timeval cur_vblank;
  103. drm_via_irq_t *cur_irq = dev_priv->via_irqs;
  104. int i;
  105. status = VIA_READ(VIA_REG_INTERRUPT);
  106. if (status & VIA_IRQ_VBLANK_PENDING) {
  107. atomic_inc(&dev_priv->vbl_received);
  108. if (!(atomic_read(&dev_priv->vbl_received) & 0x0F)) {
  109. do_gettimeofday(&cur_vblank);
  110. if (dev_priv->last_vblank_valid) {
  111. dev_priv->usec_per_vblank =
  112. time_diff(&cur_vblank,
  113. &dev_priv->last_vblank) >> 4;
  114. }
  115. dev_priv->last_vblank = cur_vblank;
  116. dev_priv->last_vblank_valid = 1;
  117. }
  118. if (!(atomic_read(&dev_priv->vbl_received) & 0xFF)) {
  119. DRM_DEBUG("US per vblank is: %u\n",
  120. dev_priv->usec_per_vblank);
  121. }
  122. drm_handle_vblank(dev, 0);
  123. handled = 1;
  124. }
  125. for (i = 0; i < dev_priv->num_irqs; ++i) {
  126. if (status & cur_irq->pending_mask) {
  127. atomic_inc(&cur_irq->irq_received);
  128. DRM_WAKEUP(&cur_irq->irq_queue);
  129. handled = 1;
  130. if (dev_priv->irq_map[drm_via_irq_dma0_td] == i)
  131. via_dmablit_handler(dev, 0, 1);
  132. else if (dev_priv->irq_map[drm_via_irq_dma1_td] == i)
  133. via_dmablit_handler(dev, 1, 1);
  134. }
  135. cur_irq++;
  136. }
  137. /* Acknowledge interrupts */
  138. VIA_WRITE(VIA_REG_INTERRUPT, status);
  139. if (handled)
  140. return IRQ_HANDLED;
  141. else
  142. return IRQ_NONE;
  143. }
  144. static __inline__ void viadrv_acknowledge_irqs(drm_via_private_t *dev_priv)
  145. {
  146. u32 status;
  147. if (dev_priv) {
  148. /* Acknowledge interrupts */
  149. status = VIA_READ(VIA_REG_INTERRUPT);
  150. VIA_WRITE(VIA_REG_INTERRUPT, status |
  151. dev_priv->irq_pending_mask);
  152. }
  153. }
  154. int via_enable_vblank(struct drm_device *dev, int crtc)
  155. {
  156. drm_via_private_t *dev_priv = dev->dev_private;
  157. u32 status;
  158. if (crtc != 0) {
  159. DRM_ERROR("%s: bad crtc %d\n", __func__, crtc);
  160. return -EINVAL;
  161. }
  162. status = VIA_READ(VIA_REG_INTERRUPT);
  163. VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_VBLANK_ENABLE);
  164. VIA_WRITE8(0x83d4, 0x11);
  165. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
  166. return 0;
  167. }
  168. void via_disable_vblank(struct drm_device *dev, int crtc)
  169. {
  170. drm_via_private_t *dev_priv = dev->dev_private;
  171. u32 status;
  172. status = VIA_READ(VIA_REG_INTERRUPT);
  173. VIA_WRITE(VIA_REG_INTERRUPT, status & ~VIA_IRQ_VBLANK_ENABLE);
  174. VIA_WRITE8(0x83d4, 0x11);
  175. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
  176. if (crtc != 0)
  177. DRM_ERROR("%s: bad crtc %d\n", __func__, crtc);
  178. }
  179. static int
  180. via_driver_irq_wait(struct drm_device *dev, unsigned int irq, int force_sequence,
  181. unsigned int *sequence)
  182. {
  183. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  184. unsigned int cur_irq_sequence;
  185. drm_via_irq_t *cur_irq;
  186. int ret = 0;
  187. maskarray_t *masks;
  188. int real_irq;
  189. DRM_DEBUG("\n");
  190. if (!dev_priv) {
  191. DRM_ERROR("called with no initialization\n");
  192. return -EINVAL;
  193. }
  194. if (irq >= drm_via_irq_num) {
  195. DRM_ERROR("Trying to wait on unknown irq %d\n", irq);
  196. return -EINVAL;
  197. }
  198. real_irq = dev_priv->irq_map[irq];
  199. if (real_irq < 0) {
  200. DRM_ERROR("Video IRQ %d not available on this hardware.\n",
  201. irq);
  202. return -EINVAL;
  203. }
  204. masks = dev_priv->irq_masks;
  205. cur_irq = dev_priv->via_irqs + real_irq;
  206. if (masks[real_irq][2] && !force_sequence) {
  207. DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
  208. ((VIA_READ(masks[irq][2]) & masks[irq][3]) ==
  209. masks[irq][4]));
  210. cur_irq_sequence = atomic_read(&cur_irq->irq_received);
  211. } else {
  212. DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
  213. (((cur_irq_sequence =
  214. atomic_read(&cur_irq->irq_received)) -
  215. *sequence) <= (1 << 23)));
  216. }
  217. *sequence = cur_irq_sequence;
  218. return ret;
  219. }
  220. /*
  221. * drm_dma.h hooks
  222. */
  223. void via_driver_irq_preinstall(struct drm_device *dev)
  224. {
  225. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  226. u32 status;
  227. drm_via_irq_t *cur_irq;
  228. int i;
  229. DRM_DEBUG("dev_priv: %p\n", dev_priv);
  230. if (dev_priv) {
  231. cur_irq = dev_priv->via_irqs;
  232. dev_priv->irq_enable_mask = VIA_IRQ_VBLANK_ENABLE;
  233. dev_priv->irq_pending_mask = VIA_IRQ_VBLANK_PENDING;
  234. if (dev_priv->chipset == VIA_PRO_GROUP_A ||
  235. dev_priv->chipset == VIA_DX9_0) {
  236. dev_priv->irq_masks = via_pro_group_a_irqs;
  237. dev_priv->num_irqs = via_num_pro_group_a;
  238. dev_priv->irq_map = via_irqmap_pro_group_a;
  239. } else {
  240. dev_priv->irq_masks = via_unichrome_irqs;
  241. dev_priv->num_irqs = via_num_unichrome;
  242. dev_priv->irq_map = via_irqmap_unichrome;
  243. }
  244. for (i = 0; i < dev_priv->num_irqs; ++i) {
  245. atomic_set(&cur_irq->irq_received, 0);
  246. cur_irq->enable_mask = dev_priv->irq_masks[i][0];
  247. cur_irq->pending_mask = dev_priv->irq_masks[i][1];
  248. DRM_INIT_WAITQUEUE(&cur_irq->irq_queue);
  249. dev_priv->irq_enable_mask |= cur_irq->enable_mask;
  250. dev_priv->irq_pending_mask |= cur_irq->pending_mask;
  251. cur_irq++;
  252. DRM_DEBUG("Initializing IRQ %d\n", i);
  253. }
  254. dev_priv->last_vblank_valid = 0;
  255. /* Clear VSync interrupt regs */
  256. status = VIA_READ(VIA_REG_INTERRUPT);
  257. VIA_WRITE(VIA_REG_INTERRUPT, status &
  258. ~(dev_priv->irq_enable_mask));
  259. /* Clear bits if they're already high */
  260. viadrv_acknowledge_irqs(dev_priv);
  261. }
  262. }
  263. int via_driver_irq_postinstall(struct drm_device *dev)
  264. {
  265. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  266. u32 status;
  267. DRM_DEBUG("via_driver_irq_postinstall\n");
  268. if (!dev_priv)
  269. return -EINVAL;
  270. status = VIA_READ(VIA_REG_INTERRUPT);
  271. VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_GLOBAL
  272. | dev_priv->irq_enable_mask);
  273. /* Some magic, oh for some data sheets ! */
  274. VIA_WRITE8(0x83d4, 0x11);
  275. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
  276. return 0;
  277. }
  278. void via_driver_irq_uninstall(struct drm_device *dev)
  279. {
  280. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  281. u32 status;
  282. DRM_DEBUG("\n");
  283. if (dev_priv) {
  284. /* Some more magic, oh for some data sheets ! */
  285. VIA_WRITE8(0x83d4, 0x11);
  286. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
  287. status = VIA_READ(VIA_REG_INTERRUPT);
  288. VIA_WRITE(VIA_REG_INTERRUPT, status &
  289. ~(VIA_IRQ_VBLANK_ENABLE | dev_priv->irq_enable_mask));
  290. }
  291. }
  292. int via_wait_irq(struct drm_device *dev, void *data, struct drm_file *file_priv)
  293. {
  294. drm_via_irqwait_t *irqwait = data;
  295. struct timeval now;
  296. int ret = 0;
  297. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  298. drm_via_irq_t *cur_irq = dev_priv->via_irqs;
  299. int force_sequence;
  300. if (irqwait->request.irq >= dev_priv->num_irqs) {
  301. DRM_ERROR("Trying to wait on unknown irq %d\n",
  302. irqwait->request.irq);
  303. return -EINVAL;
  304. }
  305. cur_irq += irqwait->request.irq;
  306. switch (irqwait->request.type & ~VIA_IRQ_FLAGS_MASK) {
  307. case VIA_IRQ_RELATIVE:
  308. irqwait->request.sequence +=
  309. atomic_read(&cur_irq->irq_received);
  310. irqwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  311. case VIA_IRQ_ABSOLUTE:
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. if (irqwait->request.type & VIA_IRQ_SIGNAL) {
  317. DRM_ERROR("Signals on Via IRQs not implemented yet.\n");
  318. return -EINVAL;
  319. }
  320. force_sequence = (irqwait->request.type & VIA_IRQ_FORCE_SEQUENCE);
  321. ret = via_driver_irq_wait(dev, irqwait->request.irq, force_sequence,
  322. &irqwait->request.sequence);
  323. do_gettimeofday(&now);
  324. irqwait->reply.tval_sec = now.tv_sec;
  325. irqwait->reply.tval_usec = now.tv_usec;
  326. return ret;
  327. }