Merge branch 'audit.b32' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit...
[pandora-kernel.git] / drivers / pci / htirq.c
1 /*
2  * File:        htirq.c
3  * Purpose:     Hypertransport Interrupt Capability
4  *
5  * Copyright (C) 2006 Linux Networx
6  * Copyright (C) Eric Biederman <ebiederman@lnxi.com>
7  */
8
9 #include <linux/irq.h>
10 #include <linux/pci.h>
11 #include <linux/spinlock.h>
12 #include <linux/slab.h>
13 #include <linux/gfp.h>
14 #include <linux/htirq.h>
15
16 /* Global ht irq lock.
17  *
18  * This is needed to serialize access to the data port in hypertransport
19  * irq capability.
20  *
21  * With multiple simultaneous hypertransport irq devices it might pay
22  * to make this more fine grained.  But start with simple, stupid, and correct.
23  */
24 static DEFINE_SPINLOCK(ht_irq_lock);
25
26 struct ht_irq_cfg {
27         struct pci_dev *dev;
28         unsigned pos;
29         unsigned idx;
30 };
31
32 void write_ht_irq_low(unsigned int irq, u32 data)
33 {
34         struct ht_irq_cfg *cfg = get_irq_data(irq);
35         unsigned long flags;
36         spin_lock_irqsave(&ht_irq_lock, flags);
37         pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx);
38         pci_write_config_dword(cfg->dev, cfg->pos + 4, data);
39         spin_unlock_irqrestore(&ht_irq_lock, flags);
40 }
41
42 void write_ht_irq_high(unsigned int irq, u32 data)
43 {
44         struct ht_irq_cfg *cfg = get_irq_data(irq);
45         unsigned long flags;
46         spin_lock_irqsave(&ht_irq_lock, flags);
47         pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx + 1);
48         pci_write_config_dword(cfg->dev, cfg->pos + 4, data);
49         spin_unlock_irqrestore(&ht_irq_lock, flags);
50 }
51
52 u32 read_ht_irq_low(unsigned int irq)
53 {
54         struct ht_irq_cfg *cfg = get_irq_data(irq);
55         unsigned long flags;
56         u32 data;
57         spin_lock_irqsave(&ht_irq_lock, flags);
58         pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx);
59         pci_read_config_dword(cfg->dev, cfg->pos + 4, &data);
60         spin_unlock_irqrestore(&ht_irq_lock, flags);
61         return data;
62 }
63
64 u32 read_ht_irq_high(unsigned int irq)
65 {
66         struct ht_irq_cfg *cfg = get_irq_data(irq);
67         unsigned long flags;
68         u32 data;
69         spin_lock_irqsave(&ht_irq_lock, flags);
70         pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx + 1);
71         pci_read_config_dword(cfg->dev, cfg->pos + 4, &data);
72         spin_unlock_irqrestore(&ht_irq_lock, flags);
73         return data;
74 }
75
76 void mask_ht_irq(unsigned int irq)
77 {
78         struct ht_irq_cfg *cfg;
79         unsigned long flags;
80         u32 data;
81
82         cfg = get_irq_data(irq);
83
84         spin_lock_irqsave(&ht_irq_lock, flags);
85         pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx);
86         pci_read_config_dword(cfg->dev, cfg->pos + 4, &data);
87         data |= 1;
88         pci_write_config_dword(cfg->dev, cfg->pos + 4, data);
89         spin_unlock_irqrestore(&ht_irq_lock, flags);
90 }
91
92 void unmask_ht_irq(unsigned int irq)
93 {
94         struct ht_irq_cfg *cfg;
95         unsigned long flags;
96         u32 data;
97
98         cfg = get_irq_data(irq);
99
100         spin_lock_irqsave(&ht_irq_lock, flags);
101         pci_write_config_byte(cfg->dev, cfg->pos + 2, cfg->idx);
102         pci_read_config_dword(cfg->dev, cfg->pos + 4, &data);
103         data &= ~1;
104         pci_write_config_dword(cfg->dev, cfg->pos + 4, data);
105         spin_unlock_irqrestore(&ht_irq_lock, flags);
106 }
107
108 /**
109  * ht_create_irq - create an irq and attach it to a device.
110  * @dev: The hypertransport device to find the irq capability on.
111  * @idx: Which of the possible irqs to attach to.
112  *
113  * ht_create_irq is needs to be called for all hypertransport devices
114  * that generate irqs.
115  *
116  * The irq number of the new irq or a negative error value is returned.
117  */
118 int ht_create_irq(struct pci_dev *dev, int idx)
119 {
120         struct ht_irq_cfg *cfg;
121         unsigned long flags;
122         u32 data;
123         int max_irq;
124         int pos;
125         int irq;
126
127         pos = pci_find_capability(dev, PCI_CAP_ID_HT);
128         while (pos) {
129                 u8 subtype;
130                 pci_read_config_byte(dev, pos + 3, &subtype);
131                 if (subtype == HT_CAPTYPE_IRQ)
132                         break;
133                 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_HT);
134         }
135         if (!pos)
136                 return -EINVAL;
137
138         /* Verify the idx I want to use is in range */
139         spin_lock_irqsave(&ht_irq_lock, flags);
140         pci_write_config_byte(dev, pos + 2, 1);
141         pci_read_config_dword(dev, pos + 4, &data);
142         spin_unlock_irqrestore(&ht_irq_lock, flags);
143
144         max_irq = (data >> 16) & 0xff;
145         if ( idx > max_irq)
146                 return -EINVAL;
147
148         cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
149         if (!cfg)
150                 return -ENOMEM;
151
152         cfg->dev = dev;
153         cfg->pos = pos;
154         cfg->idx = 0x10 + (idx * 2);
155
156         irq = create_irq();
157         if (irq < 0) {
158                 kfree(cfg);
159                 return -EBUSY;
160         }
161         set_irq_data(irq, cfg);
162
163         if (arch_setup_ht_irq(irq, dev) < 0) {
164                 ht_destroy_irq(irq);
165                 return -EBUSY;
166         }
167
168         return irq;
169 }
170
171 /**
172  * ht_destroy_irq - destroy an irq created with ht_create_irq
173  *
174  * This reverses ht_create_irq removing the specified irq from
175  * existence.  The irq should be free before this happens.
176  */
177 void ht_destroy_irq(unsigned int irq)
178 {
179         struct ht_irq_cfg *cfg;
180
181         cfg = get_irq_data(irq);
182         set_irq_chip(irq, NULL);
183         set_irq_data(irq, NULL);
184         destroy_irq(irq);
185
186         kfree(cfg);
187 }
188
189 EXPORT_SYMBOL(ht_create_irq);
190 EXPORT_SYMBOL(ht_destroy_irq);