Merge commit 'v2.6.29-rc4' into core/percpu
[safe/jmp/linux-2.6] / kernel / irq / numa_migrate.c
1 /*
2  * NUMA irq-desc migration code
3  *
4  * Migrate IRQ data structures (irq_desc, chip_data, etc.) over to
5  * the new "home node" of the IRQ.
6  */
7
8 #include <linux/irq.h>
9 #include <linux/module.h>
10 #include <linux/random.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel_stat.h>
13
14 #include "internals.h"
15
16 static void init_copy_kstat_irqs(struct irq_desc *old_desc,
17                                  struct irq_desc *desc,
18                                  int cpu, int nr)
19 {
20         unsigned long bytes;
21
22         init_kstat_irqs(desc, cpu, nr);
23
24         if (desc->kstat_irqs != old_desc->kstat_irqs) {
25                 /* Compute how many bytes we need per irq and allocate them */
26                 bytes = nr * sizeof(unsigned int);
27
28                 memcpy(desc->kstat_irqs, old_desc->kstat_irqs, bytes);
29         }
30 }
31
32 static void free_kstat_irqs(struct irq_desc *old_desc, struct irq_desc *desc)
33 {
34         if (old_desc->kstat_irqs == desc->kstat_irqs)
35                 return;
36
37         kfree(old_desc->kstat_irqs);
38         old_desc->kstat_irqs = NULL;
39 }
40
41 static bool init_copy_one_irq_desc(int irq, struct irq_desc *old_desc,
42                  struct irq_desc *desc, int cpu)
43 {
44         memcpy(desc, old_desc, sizeof(struct irq_desc));
45         if (!init_alloc_desc_masks(desc, cpu, false)) {
46                 printk(KERN_ERR "irq %d: can not get new irq_desc cpumask "
47                                 "for migration.\n", irq);
48                 return false;
49         }
50         spin_lock_init(&desc->lock);
51         desc->cpu = cpu;
52         lockdep_set_class(&desc->lock, &irq_desc_lock_class);
53         init_copy_kstat_irqs(old_desc, desc, cpu, nr_cpu_ids);
54         init_copy_desc_masks(old_desc, desc);
55         arch_init_copy_chip_data(old_desc, desc, cpu);
56         return true;
57 }
58
59 static void free_one_irq_desc(struct irq_desc *old_desc, struct irq_desc *desc)
60 {
61         free_kstat_irqs(old_desc, desc);
62         arch_free_chip_data(old_desc, desc);
63 }
64
65 static struct irq_desc *__real_move_irq_desc(struct irq_desc *old_desc,
66                                                 int cpu)
67 {
68         struct irq_desc *desc;
69         unsigned int irq;
70         unsigned long flags;
71         int node;
72
73         irq = old_desc->irq;
74
75         spin_lock_irqsave(&sparse_irq_lock, flags);
76
77         /* We have to check it to avoid races with another CPU */
78         desc = irq_desc_ptrs[irq];
79
80         if (desc && old_desc != desc)
81                 goto out_unlock;
82
83         node = cpu_to_node(cpu);
84         desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
85         if (!desc) {
86                 printk(KERN_ERR "irq %d: can not get new irq_desc "
87                                 "for migration.\n", irq);
88                 /* still use old one */
89                 desc = old_desc;
90                 goto out_unlock;
91         }
92         if (!init_copy_one_irq_desc(irq, old_desc, desc, cpu)) {
93                 /* still use old one */
94                 kfree(desc);
95                 desc = old_desc;
96                 goto out_unlock;
97         }
98
99         irq_desc_ptrs[irq] = desc;
100         spin_unlock_irqrestore(&sparse_irq_lock, flags);
101
102         /* free the old one */
103         free_one_irq_desc(old_desc, desc);
104         spin_unlock(&old_desc->lock);
105         kfree(old_desc);
106         spin_lock(&desc->lock);
107
108         return desc;
109
110 out_unlock:
111         spin_unlock_irqrestore(&sparse_irq_lock, flags);
112
113         return desc;
114 }
115
116 struct irq_desc *move_irq_desc(struct irq_desc *desc, int cpu)
117 {
118         int old_cpu;
119         int node, old_node;
120
121         /* those all static, do move them */
122         if (desc->irq < NR_IRQS_LEGACY)
123                 return desc;
124
125         old_cpu = desc->cpu;
126         if (old_cpu != cpu) {
127                 node = cpu_to_node(cpu);
128                 old_node = cpu_to_node(old_cpu);
129                 if (old_node != node)
130                         desc = __real_move_irq_desc(desc, cpu);
131                 else
132                         desc->cpu = cpu;
133         }
134
135         return desc;
136 }
137