Merge branch 'tracing-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[safe/jmp/linux-2.6] / kernel / irq / manage.c
index 61c679d..704e488 100644 (file)
@@ -46,9 +46,9 @@ void synchronize_irq(unsigned int irq)
                        cpu_relax();
 
                /* Ok, that indicated we're done: double-check carefully. */
-               spin_lock_irqsave(&desc->lock, flags);
+               raw_spin_lock_irqsave(&desc->lock, flags);
                status = desc->status;
-               spin_unlock_irqrestore(&desc->lock, flags);
+               raw_spin_unlock_irqrestore(&desc->lock, flags);
 
                /* Oops, that failed? */
        } while (status & IRQ_INPROGRESS);
@@ -114,7 +114,7 @@ int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
        if (!desc->chip->set_affinity)
                return -EINVAL;
 
-       spin_lock_irqsave(&desc->lock, flags);
+       raw_spin_lock_irqsave(&desc->lock, flags);
 
 #ifdef CONFIG_GENERIC_PENDING_IRQ
        if (desc->status & IRQ_MOVE_PCNTXT) {
@@ -134,7 +134,7 @@ int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
        }
 #endif
        desc->status |= IRQ_AFFINITY_SET;
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
        return 0;
 }
 
@@ -181,11 +181,11 @@ int irq_select_affinity_usr(unsigned int irq)
        unsigned long flags;
        int ret;
 
-       spin_lock_irqsave(&desc->lock, flags);
+       raw_spin_lock_irqsave(&desc->lock, flags);
        ret = setup_affinity(irq, desc);
        if (!ret)
                irq_set_thread_affinity(desc);
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
 
        return ret;
 }
@@ -230,9 +230,11 @@ void disable_irq_nosync(unsigned int irq)
        if (!desc)
                return;
 
-       spin_lock_irqsave(&desc->lock, flags);
+       chip_bus_lock(irq, desc);
+       raw_spin_lock_irqsave(&desc->lock, flags);
        __disable_irq(desc, irq, false);
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
+       chip_bus_sync_unlock(irq, desc);
 }
 EXPORT_SYMBOL(disable_irq_nosync);
 
@@ -294,7 +296,8 @@ void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
  *     matches the last disable, processing of interrupts on this
  *     IRQ line is re-enabled.
  *
- *     This function may be called from IRQ context.
+ *     This function may be called from IRQ context only when
+ *     desc->chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
  */
 void enable_irq(unsigned int irq)
 {
@@ -304,9 +307,11 @@ void enable_irq(unsigned int irq)
        if (!desc)
                return;
 
-       spin_lock_irqsave(&desc->lock, flags);
+       chip_bus_lock(irq, desc);
+       raw_spin_lock_irqsave(&desc->lock, flags);
        __enable_irq(desc, irq, false);
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
+       chip_bus_sync_unlock(irq, desc);
 }
 EXPORT_SYMBOL(enable_irq);
 
@@ -342,7 +347,7 @@ int set_irq_wake(unsigned int irq, unsigned int on)
        /* wakeup-capable irqs can be shared between drivers that
         * don't need to have the same sleep mode behaviors.
         */
-       spin_lock_irqsave(&desc->lock, flags);
+       raw_spin_lock_irqsave(&desc->lock, flags);
        if (on) {
                if (desc->wake_depth++ == 0) {
                        ret = set_irq_wake_real(irq, on);
@@ -363,7 +368,7 @@ int set_irq_wake(unsigned int irq, unsigned int on)
                }
        }
 
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
        return ret;
 }
 EXPORT_SYMBOL(set_irq_wake);
@@ -377,6 +382,7 @@ int can_request_irq(unsigned int irq, unsigned long irqflags)
 {
        struct irq_desc *desc = irq_to_desc(irq);
        struct irqaction *action;
+       unsigned long flags;
 
        if (!desc)
                return 0;
@@ -384,11 +390,14 @@ int can_request_irq(unsigned int irq, unsigned long irqflags)
        if (desc->status & IRQ_NOREQUEST)
                return 0;
 
+       raw_spin_lock_irqsave(&desc->lock, flags);
        action = desc->action;
        if (action)
                if (irqflags & action->flags & IRQF_SHARED)
                        action = NULL;
 
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
+
        return !action;
 }
 
@@ -436,6 +445,26 @@ int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
        return ret;
 }
 
+/*
+ * Default primary interrupt handler for threaded interrupts. Is
+ * assigned as primary handler when request_threaded_irq is called
+ * with handler == NULL. Useful for oneshot interrupts.
+ */
+static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
+{
+       return IRQ_WAKE_THREAD;
+}
+
+/*
+ * Primary handler for nested threaded interrupts. Should never be
+ * called.
+ */
+static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
+{
+       WARN(1, "Primary handler called for nested irq %d\n", irq);
+       return IRQ_NONE;
+}
+
 static int irq_wait_for_interrupt(struct irqaction *action)
 {
        while (!kthread_should_stop()) {
@@ -451,6 +480,41 @@ static int irq_wait_for_interrupt(struct irqaction *action)
        return -1;
 }
 
+/*
+ * Oneshot interrupts keep the irq line masked until the threaded
+ * handler finished. unmask if the interrupt has not been disabled and
+ * is marked MASKED.
+ */
+static void irq_finalize_oneshot(unsigned int irq, struct irq_desc *desc)
+{
+again:
+       chip_bus_lock(irq, desc);
+       raw_spin_lock_irq(&desc->lock);
+
+       /*
+        * Implausible though it may be we need to protect us against
+        * the following scenario:
+        *
+        * The thread is faster done than the hard interrupt handler
+        * on the other CPU. If we unmask the irq line then the
+        * interrupt can come in again and masks the line, leaves due
+        * to IRQ_INPROGRESS and the irq line is masked forever.
+        */
+       if (unlikely(desc->status & IRQ_INPROGRESS)) {
+               raw_spin_unlock_irq(&desc->lock);
+               chip_bus_sync_unlock(irq, desc);
+               cpu_relax();
+               goto again;
+       }
+
+       if (!(desc->status & IRQ_DISABLED) && (desc->status & IRQ_MASKED)) {
+               desc->status &= ~IRQ_MASKED;
+               desc->chip->unmask(irq);
+       }
+       raw_spin_unlock_irq(&desc->lock);
+       chip_bus_sync_unlock(irq, desc);
+}
+
 #ifdef CONFIG_SMP
 /*
  * Check whether we need to change the affinity of the interrupt thread.
@@ -472,9 +536,9 @@ irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
                return;
        }
 
-       spin_lock_irq(&desc->lock);
+       raw_spin_lock_irq(&desc->lock);
        cpumask_copy(mask, desc->affinity);
-       spin_unlock_irq(&desc->lock);
+       raw_spin_unlock_irq(&desc->lock);
 
        set_cpus_allowed_ptr(current, mask);
        free_cpumask_var(mask);
@@ -492,7 +556,7 @@ static int irq_thread(void *data)
        struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, };
        struct irqaction *action = data;
        struct irq_desc *desc = irq_to_desc(action->irq);
-       int wake;
+       int wake, oneshot = desc->status & IRQ_ONESHOT;
 
        sched_setscheduler(current, SCHED_FIFO, &param);
        current->irqaction = action;
@@ -503,7 +567,7 @@ static int irq_thread(void *data)
 
                atomic_inc(&desc->threads_active);
 
-               spin_lock_irq(&desc->lock);
+               raw_spin_lock_irq(&desc->lock);
                if (unlikely(desc->status & IRQ_DISABLED)) {
                        /*
                         * CHECKME: We might need a dedicated
@@ -513,11 +577,14 @@ static int irq_thread(void *data)
                         * retriggers the interrupt itself --- tglx
                         */
                        desc->status |= IRQ_PENDING;
-                       spin_unlock_irq(&desc->lock);
+                       raw_spin_unlock_irq(&desc->lock);
                } else {
-                       spin_unlock_irq(&desc->lock);
+                       raw_spin_unlock_irq(&desc->lock);
 
                        action->thread_fn(action->irq, action->dev_id);
+
+                       if (oneshot)
+                               irq_finalize_oneshot(action->irq, desc);
                }
 
                wake = atomic_dec_and_test(&desc->threads_active);
@@ -565,7 +632,7 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
        struct irqaction *old, **old_ptr;
        const char *old_name = NULL;
        unsigned long flags;
-       int shared = 0;
+       int nested, shared = 0;
        int ret;
 
        if (!desc)
@@ -590,10 +657,32 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
                rand_initialize_irq(irq);
        }
 
+       /* Oneshot interrupts are not allowed with shared */
+       if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED))
+               return -EINVAL;
+
+       /*
+        * Check whether the interrupt nests into another interrupt
+        * thread.
+        */
+       nested = desc->status & IRQ_NESTED_THREAD;
+       if (nested) {
+               if (!new->thread_fn)
+                       return -EINVAL;
+               /*
+                * Replace the primary handler which was provided from
+                * the driver for non nested interrupt handling by the
+                * dummy function which warns when called.
+                */
+               new->handler = irq_nested_primary_handler;
+       }
+
        /*
-        * Threaded handler ?
+        * Create a handler thread when a thread function is supplied
+        * and the interrupt does not nest into another interrupt
+        * thread.
         */
-       if (new->thread_fn) {
+       if (new->thread_fn && !nested) {
                struct task_struct *t;
 
                t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
@@ -607,13 +696,12 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
                 */
                get_task_struct(t);
                new->thread = t;
-               wake_up_process(t);
        }
 
        /*
         * The following block of code has to be executed atomically
         */
-       spin_lock_irqsave(&desc->lock, flags);
+       raw_spin_lock_irqsave(&desc->lock, flags);
        old_ptr = &desc->action;
        old = *old_ptr;
        if (old) {
@@ -663,9 +751,22 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
                        desc->status |= IRQ_PER_CPU;
 #endif
 
-               desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
+               desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | IRQ_ONESHOT |
                                  IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);
 
+               if (new->flags & IRQF_ONESHOT)
+                       desc->status |= IRQ_ONESHOT;
+
+               /*
+                * Force MSI interrupts to run with interrupts
+                * disabled. The multi vector cards can cause stack
+                * overflows due to nested interrupts when enough of
+                * them are directed to a core and fire at the same
+                * time.
+                */
+               if (desc->msi_desc)
+                       new->flags |= IRQF_DISABLED;
+
                if (!(desc->status & IRQ_NOAUTOEN)) {
                        desc->depth = 0;
                        desc->status &= ~IRQ_DISABLED;
@@ -690,6 +791,7 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
                                (int)(new->flags & IRQF_TRIGGER_MASK));
        }
 
+       new->irq = irq;
        *old_ptr = new;
 
        /* Reset broken irq detection when installing new handler */
@@ -705,9 +807,15 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
                __enable_irq(desc, irq, false);
        }
 
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
+
+       /*
+        * Strictly no need to wake it up, but hung_task complains
+        * when no hard interrupt wakes the thread up.
+        */
+       if (new->thread)
+               wake_up_process(new->thread);
 
-       new->irq = irq;
        register_irq_proc(irq, desc);
        new->dir = NULL;
        register_handler_proc(irq, new);
@@ -726,7 +834,7 @@ mismatch:
        ret = -EBUSY;
 
 out_thread:
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
        if (new->thread) {
                struct task_struct *t = new->thread;
 
@@ -761,7 +869,6 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
 {
        struct irq_desc *desc = irq_to_desc(irq);
        struct irqaction *action, **action_ptr;
-       struct task_struct *irqthread;
        unsigned long flags;
 
        WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
@@ -769,7 +876,7 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
        if (!desc)
                return NULL;
 
-       spin_lock_irqsave(&desc->lock, flags);
+       raw_spin_lock_irqsave(&desc->lock, flags);
 
        /*
         * There can be multiple actions per IRQ descriptor, find the right
@@ -781,7 +888,7 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
 
                if (!action) {
                        WARN(1, "Trying to free already-free IRQ %d\n", irq);
-                       spin_unlock_irqrestore(&desc->lock, flags);
+                       raw_spin_unlock_irqrestore(&desc->lock, flags);
 
                        return NULL;
                }
@@ -809,22 +916,13 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
                        desc->chip->disable(irq);
        }
 
-       irqthread = action->thread;
-       action->thread = NULL;
-
-       spin_unlock_irqrestore(&desc->lock, flags);
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
 
        unregister_handler_proc(irq, action);
 
        /* Make sure it's not being used on another CPU: */
        synchronize_irq(irq);
 
-       if (irqthread) {
-               if (!test_bit(IRQTF_DIED, &action->thread_flags))
-                       kthread_stop(irqthread);
-               put_task_struct(irqthread);
-       }
-
 #ifdef CONFIG_DEBUG_SHIRQ
        /*
         * It's a shared IRQ -- the driver ought to be prepared for an IRQ
@@ -840,6 +938,13 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
                local_irq_restore(flags);
        }
 #endif
+
+       if (action->thread) {
+               if (!test_bit(IRQTF_DIED, &action->thread_flags))
+                       kthread_stop(action->thread);
+               put_task_struct(action->thread);
+       }
+
        return action;
 }
 
@@ -872,7 +977,14 @@ EXPORT_SYMBOL_GPL(remove_irq);
  */
 void free_irq(unsigned int irq, void *dev_id)
 {
+       struct irq_desc *desc = irq_to_desc(irq);
+
+       if (!desc)
+               return;
+
+       chip_bus_lock(irq, desc);
        kfree(__free_irq(irq, dev_id));
+       chip_bus_sync_unlock(irq, desc);
 }
 EXPORT_SYMBOL(free_irq);
 
@@ -881,6 +993,8 @@ EXPORT_SYMBOL(free_irq);
  *     @irq: Interrupt line to allocate
  *     @handler: Function to be called when the IRQ occurs.
  *               Primary handler for threaded interrupts
+ *               If NULL and thread_fn != NULL the default
+ *               primary handler is installed
  *     @thread_fn: Function called from the irq handler thread
  *                 If NULL, no irq thread is created
  *     @irqflags: Interrupt type flags
@@ -960,8 +1074,12 @@ int request_threaded_irq(unsigned int irq, irq_handler_t handler,
 
        if (desc->status & IRQ_NOREQUEST)
                return -EINVAL;
-       if (!handler)
-               return -EINVAL;
+
+       if (!handler) {
+               if (!thread_fn)
+                       return -EINVAL;
+               handler = irq_default_primary_handler;
+       }
 
        action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
        if (!action)
@@ -973,12 +1091,15 @@ int request_threaded_irq(unsigned int irq, irq_handler_t handler,
        action->name = devname;
        action->dev_id = dev_id;
 
+       chip_bus_lock(irq, desc);
        retval = __setup_irq(irq, desc, action);
+       chip_bus_sync_unlock(irq, desc);
+
        if (retval)
                kfree(action);
 
 #ifdef CONFIG_DEBUG_SHIRQ
-       if (irqflags & IRQF_SHARED) {
+       if (!retval && (irqflags & IRQF_SHARED)) {
                /*
                 * It's a shared IRQ -- the driver ought to be prepared for it
                 * to happen immediately, so let's make sure....