via_irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <drm/drmP.h>
  38. #include <drm/via_drm.h>
  39. #include "via_drv.h"
  40. #define VIA_REG_INTERRUPT 0x200
  41. /* VIA_REG_INTERRUPT */
  42. #define VIA_IRQ_GLOBAL (1 << 31)
  43. #define VIA_IRQ_VBLANK_ENABLE (1 << 19)
  44. #define VIA_IRQ_VBLANK_PENDING (1 << 3)
  45. #define VIA_IRQ_HQV0_ENABLE (1 << 11)
  46. #define VIA_IRQ_HQV1_ENABLE (1 << 25)
  47. #define VIA_IRQ_HQV0_PENDING (1 << 9)
  48. #define VIA_IRQ_HQV1_PENDING (1 << 10)
  49. #define VIA_IRQ_DMA0_DD_ENABLE (1 << 20)
  50. #define VIA_IRQ_DMA0_TD_ENABLE (1 << 21)
  51. #define VIA_IRQ_DMA1_DD_ENABLE (1 << 22)
  52. #define VIA_IRQ_DMA1_TD_ENABLE (1 << 23)
  53. #define VIA_IRQ_DMA0_DD_PENDING (1 << 4)
  54. #define VIA_IRQ_DMA0_TD_PENDING (1 << 5)
  55. #define VIA_IRQ_DMA1_DD_PENDING (1 << 6)
  56. #define VIA_IRQ_DMA1_TD_PENDING (1 << 7)
  57. /*
  58. * Device-specific IRQs go here. This type might need to be extended with
  59. * the register if there are multiple IRQ control registers.
  60. * Currently we activate the HQV interrupts of Unichrome Pro group A.
  61. */
  62. static maskarray_t via_pro_group_a_irqs[] = {
  63. {VIA_IRQ_HQV0_ENABLE, VIA_IRQ_HQV0_PENDING, 0x000003D0, 0x00008010,
  64. 0x00000000 },
  65. {VIA_IRQ_HQV1_ENABLE, VIA_IRQ_HQV1_PENDING, 0x000013D0, 0x00008010,
  66. 0x00000000 },
  67. {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
  68. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
  69. {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
  70. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
  71. };
  72. static int via_num_pro_group_a = ARRAY_SIZE(via_pro_group_a_irqs);
  73. static int via_irqmap_pro_group_a[] = {0, 1, -1, 2, -1, 3};
  74. static maskarray_t via_unichrome_irqs[] = {
  75. {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
  76. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
  77. {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
  78. VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008}
  79. };
  80. static int via_num_unichrome = ARRAY_SIZE(via_unichrome_irqs);
  81. static int via_irqmap_unichrome[] = {-1, -1, -1, 0, -1, 1};
  82. static unsigned time_diff(struct timeval *now, struct timeval *then)
  83. {
  84. return (now->tv_usec >= then->tv_usec) ?
  85. now->tv_usec - then->tv_usec :
  86. 1000000 - (then->tv_usec - now->tv_usec);
  87. }
  88. u32 via_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  89. {
  90. drm_via_private_t *dev_priv = dev->dev_private;
  91. if (pipe != 0)
  92. return 0;
  93. return atomic_read(&dev_priv->vbl_received);
  94. }
  95. irqreturn_t via_driver_irq_handler(int irq, void *arg)
  96. {
  97. struct drm_device *dev = (struct drm_device *) arg;
  98. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  99. u32 status;
  100. int handled = 0;
  101. struct timeval cur_vblank;
  102. drm_via_irq_t *cur_irq = dev_priv->via_irqs;
  103. int i;
  104. status = VIA_READ(VIA_REG_INTERRUPT);
  105. if (status & VIA_IRQ_VBLANK_PENDING) {
  106. atomic_inc(&dev_priv->vbl_received);
  107. if (!(atomic_read(&dev_priv->vbl_received) & 0x0F)) {
  108. do_gettimeofday(&cur_vblank);
  109. if (dev_priv->last_vblank_valid) {
  110. dev_priv->usec_per_vblank =
  111. time_diff(&cur_vblank,
  112. &dev_priv->last_vblank) >> 4;
  113. }
  114. dev_priv->last_vblank = cur_vblank;
  115. dev_priv->last_vblank_valid = 1;
  116. }
  117. if (!(atomic_read(&dev_priv->vbl_received) & 0xFF)) {
  118. DRM_DEBUG("US per vblank is: %u\n",
  119. dev_priv->usec_per_vblank);
  120. }
  121. drm_handle_vblank(dev, 0);
  122. handled = 1;
  123. }
  124. for (i = 0; i < dev_priv->num_irqs; ++i) {
  125. if (status & cur_irq->pending_mask) {
  126. atomic_inc(&cur_irq->irq_received);
  127. wake_up(&cur_irq->irq_queue);
  128. handled = 1;
  129. if (dev_priv->irq_map[drm_via_irq_dma0_td] == i)
  130. via_dmablit_handler(dev, 0, 1);
  131. else if (dev_priv->irq_map[drm_via_irq_dma1_td] == i)
  132. via_dmablit_handler(dev, 1, 1);
  133. }
  134. cur_irq++;
  135. }
  136. /* Acknowledge interrupts */
  137. VIA_WRITE(VIA_REG_INTERRUPT, status);
  138. if (handled)
  139. return IRQ_HANDLED;
  140. else
  141. return IRQ_NONE;
  142. }
  143. static __inline__ void viadrv_acknowledge_irqs(drm_via_private_t *dev_priv)
  144. {
  145. u32 status;
  146. if (dev_priv) {
  147. /* Acknowledge interrupts */
  148. status = VIA_READ(VIA_REG_INTERRUPT);
  149. VIA_WRITE(VIA_REG_INTERRUPT, status |
  150. dev_priv->irq_pending_mask);
  151. }
  152. }
  153. int via_enable_vblank(struct drm_device *dev, unsigned int pipe)
  154. {
  155. drm_via_private_t *dev_priv = dev->dev_private;
  156. u32 status;
  157. if (pipe != 0) {
  158. DRM_ERROR("%s: bad crtc %u\n", __func__, pipe);
  159. return -EINVAL;
  160. }
  161. status = VIA_READ(VIA_REG_INTERRUPT);
  162. VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_VBLANK_ENABLE);
  163. VIA_WRITE8(0x83d4, 0x11);
  164. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
  165. return 0;
  166. }
  167. void via_disable_vblank(struct drm_device *dev, unsigned int pipe)
  168. {
  169. drm_via_private_t *dev_priv = dev->dev_private;
  170. u32 status;
  171. status = VIA_READ(VIA_REG_INTERRUPT);
  172. VIA_WRITE(VIA_REG_INTERRUPT, status & ~VIA_IRQ_VBLANK_ENABLE);
  173. VIA_WRITE8(0x83d4, 0x11);
  174. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
  175. if (pipe != 0)
  176. DRM_ERROR("%s: bad crtc %u\n", __func__, pipe);
  177. }
  178. static int
  179. via_driver_irq_wait(struct drm_device *dev, unsigned int irq, int force_sequence,
  180. unsigned int *sequence)
  181. {
  182. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  183. unsigned int cur_irq_sequence;
  184. drm_via_irq_t *cur_irq;
  185. int ret = 0;
  186. maskarray_t *masks;
  187. int real_irq;
  188. DRM_DEBUG("\n");
  189. if (!dev_priv) {
  190. DRM_ERROR("called with no initialization\n");
  191. return -EINVAL;
  192. }
  193. if (irq >= drm_via_irq_num) {
  194. DRM_ERROR("Trying to wait on unknown irq %d\n", irq);
  195. return -EINVAL;
  196. }
  197. real_irq = dev_priv->irq_map[irq];
  198. if (real_irq < 0) {
  199. DRM_ERROR("Video IRQ %d not available on this hardware.\n",
  200. irq);
  201. return -EINVAL;
  202. }
  203. masks = dev_priv->irq_masks;
  204. cur_irq = dev_priv->via_irqs + real_irq;
  205. if (masks[real_irq][2] && !force_sequence) {
  206. DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * HZ,
  207. ((VIA_READ(masks[irq][2]) & masks[irq][3]) ==
  208. masks[irq][4]));
  209. cur_irq_sequence = atomic_read(&cur_irq->irq_received);
  210. } else {
  211. DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * HZ,
  212. (((cur_irq_sequence =
  213. atomic_read(&cur_irq->irq_received)) -
  214. *sequence) <= (1 << 23)));
  215. }
  216. *sequence = cur_irq_sequence;
  217. return ret;
  218. }
  219. /*
  220. * drm_dma.h hooks
  221. */
  222. void via_driver_irq_preinstall(struct drm_device *dev)
  223. {
  224. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  225. u32 status;
  226. drm_via_irq_t *cur_irq;
  227. int i;
  228. DRM_DEBUG("dev_priv: %p\n", dev_priv);
  229. if (dev_priv) {
  230. cur_irq = dev_priv->via_irqs;
  231. dev_priv->irq_enable_mask = VIA_IRQ_VBLANK_ENABLE;
  232. dev_priv->irq_pending_mask = VIA_IRQ_VBLANK_PENDING;
  233. if (dev_priv->chipset == VIA_PRO_GROUP_A ||
  234. dev_priv->chipset == VIA_DX9_0) {
  235. dev_priv->irq_masks = via_pro_group_a_irqs;
  236. dev_priv->num_irqs = via_num_pro_group_a;
  237. dev_priv->irq_map = via_irqmap_pro_group_a;
  238. } else {
  239. dev_priv->irq_masks = via_unichrome_irqs;
  240. dev_priv->num_irqs = via_num_unichrome;
  241. dev_priv->irq_map = via_irqmap_unichrome;
  242. }
  243. for (i = 0; i < dev_priv->num_irqs; ++i) {
  244. atomic_set(&cur_irq->irq_received, 0);
  245. cur_irq->enable_mask = dev_priv->irq_masks[i][0];
  246. cur_irq->pending_mask = dev_priv->irq_masks[i][1];
  247. init_waitqueue_head(&cur_irq->irq_queue);
  248. dev_priv->irq_enable_mask |= cur_irq->enable_mask;
  249. dev_priv->irq_pending_mask |= cur_irq->pending_mask;
  250. cur_irq++;
  251. DRM_DEBUG("Initializing IRQ %d\n", i);
  252. }
  253. dev_priv->last_vblank_valid = 0;
  254. /* Clear VSync interrupt regs */
  255. status = VIA_READ(VIA_REG_INTERRUPT);
  256. VIA_WRITE(VIA_REG_INTERRUPT, status &
  257. ~(dev_priv->irq_enable_mask));
  258. /* Clear bits if they're already high */
  259. viadrv_acknowledge_irqs(dev_priv);
  260. }
  261. }
  262. int via_driver_irq_postinstall(struct drm_device *dev)
  263. {
  264. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  265. u32 status;
  266. DRM_DEBUG("via_driver_irq_postinstall\n");
  267. if (!dev_priv)
  268. return -EINVAL;
  269. status = VIA_READ(VIA_REG_INTERRUPT);
  270. VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_GLOBAL
  271. | dev_priv->irq_enable_mask);
  272. /* Some magic, oh for some data sheets ! */
  273. VIA_WRITE8(0x83d4, 0x11);
  274. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
  275. return 0;
  276. }
  277. void via_driver_irq_uninstall(struct drm_device *dev)
  278. {
  279. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  280. u32 status;
  281. DRM_DEBUG("\n");
  282. if (dev_priv) {
  283. /* Some more magic, oh for some data sheets ! */
  284. VIA_WRITE8(0x83d4, 0x11);
  285. VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
  286. status = VIA_READ(VIA_REG_INTERRUPT);
  287. VIA_WRITE(VIA_REG_INTERRUPT, status &
  288. ~(VIA_IRQ_VBLANK_ENABLE | dev_priv->irq_enable_mask));
  289. }
  290. }
  291. int via_wait_irq(struct drm_device *dev, void *data, struct drm_file *file_priv)
  292. {
  293. drm_via_irqwait_t *irqwait = data;
  294. struct timeval now;
  295. int ret = 0;
  296. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  297. drm_via_irq_t *cur_irq = dev_priv->via_irqs;
  298. int force_sequence;
  299. if (irqwait->request.irq >= dev_priv->num_irqs) {
  300. DRM_ERROR("Trying to wait on unknown irq %d\n",
  301. irqwait->request.irq);
  302. return -EINVAL;
  303. }
  304. cur_irq += irqwait->request.irq;
  305. switch (irqwait->request.type & ~VIA_IRQ_FLAGS_MASK) {
  306. case VIA_IRQ_RELATIVE:
  307. irqwait->request.sequence +=
  308. atomic_read(&cur_irq->irq_received);
  309. irqwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  310. case VIA_IRQ_ABSOLUTE:
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. if (irqwait->request.type & VIA_IRQ_SIGNAL) {
  316. DRM_ERROR("Signals on Via IRQs not implemented yet.\n");
  317. return -EINVAL;
  318. }
  319. force_sequence = (irqwait->request.type & VIA_IRQ_FORCE_SEQUENCE);
  320. ret = via_driver_irq_wait(dev, irqwait->request.irq, force_sequence,
  321. &irqwait->request.sequence);
  322. do_gettimeofday(&now);
  323. irqwait->reply.tval_sec = now.tv_sec;
  324. irqwait->reply.tval_usec = now.tv_usec;
  325. return ret;
  326. }