blob: 91ec2bb497e97d6622be7f38c048360402f4caf9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i830_dma.c -- DMA support for the I830 -*- linux-c -*-
2 *
3 * Copyright 2002 Tungsten Graphics, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Dave Airlieb5e89ed2005-09-25 14:28:13 +100012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors: Keith Whitwell <keith@tungstengraphics.com>
26 *
27 */
28
29#include "drmP.h"
30#include "drm.h"
31#include "i830_drm.h"
32#include "i830_drv.h"
33#include <linux/interrupt.h> /* For task queue support */
34#include <linux/delay.h>
35
Dave Airlieb5e89ed2005-09-25 14:28:13 +100036irqreturn_t i830_driver_irq_handler(DRM_IRQ_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Dave Airlieeddca552007-07-11 16:09:54 +100038 struct drm_device *dev = (struct drm_device *) arg;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100039 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
40 u16 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Airlieb5e89ed2005-09-25 14:28:13 +100042 temp = I830_READ16(I830REG_INT_IDENTITY_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 DRM_DEBUG("%x\n", temp);
44
Dave Airlieb5e89ed2005-09-25 14:28:13 +100045 if (!(temp & 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return IRQ_NONE;
47
Dave Airlieb5e89ed2005-09-25 14:28:13 +100048 I830_WRITE16(I830REG_INT_IDENTITY_R, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 atomic_inc(&dev_priv->irq_received);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051 wake_up_interruptible(&dev_priv->irq_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 return IRQ_HANDLED;
54}
55
Dave Airlieeddca552007-07-11 16:09:54 +100056static int i830_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 drm_i830_private_t *dev_priv = dev->dev_private;
59 RING_LOCALS;
60
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070061 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 atomic_inc(&dev_priv->irq_emitted);
64
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 BEGIN_LP_RING(2);
66 OUT_RING(0);
67 OUT_RING(GFX_OP_USER_INTERRUPT);
68 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 return atomic_read(&dev_priv->irq_emitted);
71}
72
Dave Airlieeddca552007-07-11 16:09:54 +100073static int i830_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 DECLARE_WAITQUEUE(entry, current);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 unsigned long end = jiffies + HZ * 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int ret = 0;
79
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070080 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 if (atomic_read(&dev_priv->irq_received) >= irq_nr)
83 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;
86
87 add_wait_queue(&dev_priv->irq_queue, &entry);
88
89 for (;;) {
90 __set_current_state(TASK_INTERRUPTIBLE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091 if (atomic_read(&dev_priv->irq_received) >= irq_nr)
92 break;
93 if ((signed)(end - jiffies) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 DRM_ERROR("timeout iir %x imr %x ier %x hwstam %x\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 I830_READ16(I830REG_INT_IDENTITY_R),
96 I830_READ16(I830REG_INT_MASK_R),
97 I830_READ16(I830REG_INT_ENABLE_R),
98 I830_READ16(I830REG_HWSTAM));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000100 ret = -EBUSY; /* Lockup? Missed irq? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 break;
102 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 schedule_timeout(HZ * 3);
104 if (signal_pending(current)) {
105 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 break;
107 }
108 }
109
110 __set_current_state(TASK_RUNNING);
111 remove_wait_queue(&dev_priv->irq_queue, &entry);
112 return ret;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* Needs the lock as it touches the ring.
116 */
Eric Anholtc153f452007-09-03 12:06:45 +1000117int i830_irq_emit(struct drm_device *dev, void *data,
118 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000121 drm_i830_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int result;
123
Eric Anholt6c340ea2007-08-25 20:23:09 +1000124 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000126 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700127 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EINVAL;
129 }
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 result = i830_emit_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Eric Anholtc153f452007-09-03 12:06:45 +1000133 if (copy_to_user(emit->irq_seq, &result, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -EFAULT;
136 }
137
138 return 0;
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/* Doesn't need the hardware lock.
142 */
Eric Anholtc153f452007-09-03 12:06:45 +1000143int i830_irq_wait(struct drm_device *dev, void *data,
144 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 drm_i830_private_t *dev_priv = dev->dev_private;
Dave Airliebc5f4522007-11-05 12:50:58 +1000147 drm_i830_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700150 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -EINVAL;
152 }
153
Eric Anholtc153f452007-09-03 12:06:45 +1000154 return i830_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* drm_dma.h hooks
158*/
Dave Airlieeddca552007-07-11 16:09:54 +1000159void i830_driver_irq_preinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000160{
161 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 I830_WRITE16(I830REG_HWSTAM, 0xffff);
164 I830_WRITE16(I830REG_INT_MASK_R, 0x0);
165 I830_WRITE16(I830REG_INT_ENABLE_R, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 atomic_set(&dev_priv->irq_received, 0);
167 atomic_set(&dev_priv->irq_emitted, 0);
168 init_waitqueue_head(&dev_priv->irq_queue);
169}
170
Dave Airlieeddca552007-07-11 16:09:54 +1000171void i830_driver_irq_postinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172{
173 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000175 I830_WRITE16(I830REG_INT_ENABLE_R, 0x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Dave Airlieeddca552007-07-11 16:09:54 +1000178void i830_driver_irq_uninstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179{
180 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!dev_priv)
182 return;
183
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 I830_WRITE16(I830REG_INT_MASK_R, 0xffff);
185 I830_WRITE16(I830REG_INT_ENABLE_R, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}