timekeeping: Fix timezone update
[safe/jmp/linux-2.6] / kernel / irq / manage.c
index d222515..3164ba7 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,10 +134,26 @@ 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;
 }
 
+int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
+{
+       struct irq_desc *desc = irq_to_desc(irq);
+       unsigned long flags;
+
+       if (!desc)
+               return -EINVAL;
+
+       raw_spin_lock_irqsave(&desc->lock, flags);
+       desc->affinity_hint = m;
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
+
 #ifndef CONFIG_AUTO_IRQ_AFFINITY
 /*
  * Generic version of the affinity autoselector.
@@ -181,11 +197,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 +246,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 +312,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 +323,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 +363,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 +384,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 +398,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 +406,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 +461,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 +496,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 +552,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 +572,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 +583,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 +593,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 +648,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 +673,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 +712,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 +767,12 @@ __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;
+
                if (!(desc->status & IRQ_NOAUTOEN)) {
                        desc->depth = 0;
                        desc->status &= ~IRQ_DISABLED;
@@ -690,6 +797,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 +813,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 +840,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;
 
@@ -768,7 +882,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
@@ -780,7 +894,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;
                }
@@ -808,7 +922,13 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
                        desc->chip->disable(irq);
        }
 
-       spin_unlock_irqrestore(&desc->lock, flags);
+#ifdef CONFIG_SMP
+       /* make sure affinity_hint is cleaned up */
+       if (WARN_ON_ONCE(desc->affinity_hint))
+               desc->affinity_hint = NULL;
+#endif
+
+       raw_spin_unlock_irqrestore(&desc->lock, flags);
 
        unregister_handler_proc(irq, action);
 
@@ -869,7 +989,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);
 
@@ -878,6 +1005,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
@@ -910,7 +1039,6 @@ EXPORT_SYMBOL(free_irq);
  *     Flags:
  *
  *     IRQF_SHARED             Interrupt is shared
- *     IRQF_DISABLED   Disable local interrupts while processing
  *     IRQF_SAMPLE_RANDOM      The interrupt can be used for entropy
  *     IRQF_TRIGGER_*          Specify active edge(s) or level
  *
@@ -924,25 +1052,6 @@ int request_threaded_irq(unsigned int irq, irq_handler_t handler,
        int retval;
 
        /*
-        * handle_IRQ_event() always ignores IRQF_DISABLED except for
-        * the _first_ irqaction (sigh).  That can cause oopsing, but
-        * the behavior is classified as "will not fix" so we need to
-        * start nudging drivers away from using that idiom.
-        */
-       if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) ==
-                                       (IRQF_SHARED|IRQF_DISABLED)) {
-               pr_warning(
-                 "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n",
-                       irq, devname);
-       }
-
-#ifdef CONFIG_LOCKDEP
-       /*
-        * Lockdep wants atomic interrupt handlers:
-        */
-       irqflags |= IRQF_DISABLED;
-#endif
-       /*
         * Sanity-check: shared interrupts must pass in a real dev-ID,
         * otherwise we'll have trouble later trying to figure out
         * which interrupt is which (messes up the interrupt freeing
@@ -957,8 +1066,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)
@@ -970,12 +1083,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....
@@ -996,3 +1112,40 @@ int request_threaded_irq(unsigned int irq, irq_handler_t handler,
        return retval;
 }
 EXPORT_SYMBOL(request_threaded_irq);
+
+/**
+ *     request_any_context_irq - allocate an interrupt line
+ *     @irq: Interrupt line to allocate
+ *     @handler: Function to be called when the IRQ occurs.
+ *               Threaded handler for threaded interrupts.
+ *     @flags: Interrupt type flags
+ *     @name: An ascii name for the claiming device
+ *     @dev_id: A cookie passed back to the handler function
+ *
+ *     This call allocates interrupt resources and enables the
+ *     interrupt line and IRQ handling. It selects either a
+ *     hardirq or threaded handling method depending on the
+ *     context.
+ *
+ *     On failure, it returns a negative value. On success,
+ *     it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
+ */
+int request_any_context_irq(unsigned int irq, irq_handler_t handler,
+                           unsigned long flags, const char *name, void *dev_id)
+{
+       struct irq_desc *desc = irq_to_desc(irq);
+       int ret;
+
+       if (!desc)
+               return -EINVAL;
+
+       if (desc->status & IRQ_NESTED_THREAD) {
+               ret = request_threaded_irq(irq, NULL, handler,
+                                          flags, name, dev_id);
+               return !ret ? IRQC_IS_NESTED : ret;
+       }
+
+       ret = request_irq(irq, handler, flags, name, dev_id);
+       return !ret ? IRQC_IS_HARDIRQ : ret;
+}
+EXPORT_SYMBOL_GPL(request_any_context_irq);