Driver core: plug device probe memory leak
[linux-2.6/linux-loongson.git] / drivers / pci / htirq.c
blob0e27f2404a83d81acd5abbae17d99483d59055a3
1 /*
2 * File: htirq.c
3 * Purpose: Hypertransport Interrupt Capability
5 * Copyright (C) 2006 Linux Networx
6 * Copyright (C) Eric Biederman <ebiederman@lnxi.com>
7 */
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>
16 /* Global ht irq lock.
18 * This is needed to serialize access to the data port in hypertransport
19 * irq capability.
21 * With multiple simultaneous hypertransport irq devices it might pay
22 * to make this more fine grained. But start with simple, stupid, and correct.
24 static DEFINE_SPINLOCK(ht_irq_lock);
26 struct ht_irq_cfg {
27 struct pci_dev *dev;
28 unsigned pos;
29 unsigned idx;
32 void write_ht_irq_low(unsigned int irq, u32 data)
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);
42 void write_ht_irq_high(unsigned int irq, u32 data)
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);
52 u32 read_ht_irq_low(unsigned int irq)
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;
64 u32 read_ht_irq_high(unsigned int irq)
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;
76 void mask_ht_irq(unsigned int irq)
78 struct ht_irq_cfg *cfg;
79 unsigned long flags;
80 u32 data;
82 cfg = get_irq_data(irq);
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);
92 void unmask_ht_irq(unsigned int irq)
94 struct ht_irq_cfg *cfg;
95 unsigned long flags;
96 u32 data;
98 cfg = get_irq_data(irq);
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);
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.
113 * ht_create_irq is needs to be called for all hypertransport devices
114 * that generate irqs.
116 * The irq number of the new irq or a negative error value is returned.
118 int ht_create_irq(struct pci_dev *dev, int idx)
120 struct ht_irq_cfg *cfg;
121 unsigned long flags;
122 u32 data;
123 int max_irq;
124 int pos;
125 int irq;
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);
135 if (!pos)
136 return -EINVAL;
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);
144 max_irq = (data >> 16) & 0xff;
145 if ( idx > max_irq)
146 return -EINVAL;
148 cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
149 if (!cfg)
150 return -ENOMEM;
152 cfg->dev = dev;
153 cfg->pos = pos;
154 cfg->idx = 0x10 + (idx * 2);
156 irq = create_irq();
157 if (irq < 0) {
158 kfree(cfg);
159 return -EBUSY;
161 set_irq_data(irq, cfg);
163 if (arch_setup_ht_irq(irq, dev) < 0) {
164 ht_destroy_irq(irq);
165 return -EBUSY;
168 return irq;
172 * ht_destroy_irq - destroy an irq created with ht_create_irq
174 * This reverses ht_create_irq removing the specified irq from
175 * existence. The irq should be free before this happens.
177 void ht_destroy_irq(unsigned int irq)
179 struct ht_irq_cfg *cfg;
181 cfg = get_irq_data(irq);
182 set_irq_chip(irq, NULL);
183 set_irq_data(irq, NULL);
184 destroy_irq(irq);
186 kfree(cfg);
189 EXPORT_SYMBOL(ht_create_irq);
190 EXPORT_SYMBOL(ht_destroy_irq);