Merge branch 'upstream' of git://lost.foo-projects.org/~ahkok/git/netdev-2.6 into...
[pandora-kernel.git] / arch / i386 / pci / common.c
1 /*
2  *      Low-Level PCI Support for PC
3  *
4  *      (c) 1999--2000 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <linux/sched.h>
8 #include <linux/pci.h>
9 #include <linux/ioport.h>
10 #include <linux/init.h>
11 #include <linux/dmi.h>
12
13 #include <asm/acpi.h>
14 #include <asm/segment.h>
15 #include <asm/io.h>
16 #include <asm/smp.h>
17
18 #include "pci.h"
19
20 #ifdef CONFIG_PCI_BIOS
21 extern  void pcibios_sort(void);
22 #endif
23
24 unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |
25                                 PCI_PROBE_MMCONF;
26
27 int pci_routeirq;
28 int pcibios_last_bus = -1;
29 unsigned long pirq_table_addr;
30 struct pci_bus *pci_root_bus;
31 struct pci_raw_ops *raw_pci_ops;
32
33 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
34 {
35         return raw_pci_ops->read(0, bus->number, devfn, where, size, value);
36 }
37
38 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
39 {
40         return raw_pci_ops->write(0, bus->number, devfn, where, size, value);
41 }
42
43 struct pci_ops pci_root_ops = {
44         .read = pci_read,
45         .write = pci_write,
46 };
47
48 /*
49  * legacy, numa, and acpi all want to call pcibios_scan_root
50  * from their initcalls. This flag prevents that.
51  */
52 int pcibios_scanned;
53
54 /*
55  * This interrupt-safe spinlock protects all accesses to PCI
56  * configuration space.
57  */
58 DEFINE_SPINLOCK(pci_config_lock);
59
60 /*
61  * Several buggy motherboards address only 16 devices and mirror
62  * them to next 16 IDs. We try to detect this `feature' on all
63  * primary buses (those containing host bridges as they are
64  * expected to be unique) and remove the ghost devices.
65  */
66
67 static void __devinit pcibios_fixup_ghosts(struct pci_bus *b)
68 {
69         struct list_head *ln, *mn;
70         struct pci_dev *d, *e;
71         int mirror = PCI_DEVFN(16,0);
72         int seen_host_bridge = 0;
73         int i;
74
75         DBG("PCI: Scanning for ghost devices on bus %d\n", b->number);
76         list_for_each(ln, &b->devices) {
77                 d = pci_dev_b(ln);
78                 if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
79                         seen_host_bridge++;
80                 for (mn=ln->next; mn != &b->devices; mn=mn->next) {
81                         e = pci_dev_b(mn);
82                         if (e->devfn != d->devfn + mirror ||
83                             e->vendor != d->vendor ||
84                             e->device != d->device ||
85                             e->class != d->class)
86                                 continue;
87                         for(i=0; i<PCI_NUM_RESOURCES; i++)
88                                 if (e->resource[i].start != d->resource[i].start ||
89                                     e->resource[i].end != d->resource[i].end ||
90                                     e->resource[i].flags != d->resource[i].flags)
91                                         continue;
92                         break;
93                 }
94                 if (mn == &b->devices)
95                         return;
96         }
97         if (!seen_host_bridge)
98                 return;
99         printk(KERN_WARNING "PCI: Ignoring ghost devices on bus %02x\n", b->number);
100
101         ln = &b->devices;
102         while (ln->next != &b->devices) {
103                 d = pci_dev_b(ln->next);
104                 if (d->devfn >= mirror) {
105                         list_del(&d->global_list);
106                         list_del(&d->bus_list);
107                         kfree(d);
108                 } else
109                         ln = ln->next;
110         }
111 }
112
113 /*
114  *  Called after each bus is probed, but before its children
115  *  are examined.
116  */
117
118 void __devinit  pcibios_fixup_bus(struct pci_bus *b)
119 {
120         pcibios_fixup_ghosts(b);
121         pci_read_bridge_bases(b);
122 }
123
124 /*
125  * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
126  */
127 #ifdef __i386__
128 static int __devinit assign_all_busses(struct dmi_system_id *d)
129 {
130         pci_probe |= PCI_ASSIGN_ALL_BUSSES;
131         printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
132                         " (pci=assign-busses)\n", d->ident);
133         return 0;
134 }
135 #endif
136
137 /*
138  * Laptops which need pci=assign-busses to see Cardbus cards
139  */
140 static struct dmi_system_id __devinitdata pciprobe_dmi_table[] = {
141 #ifdef __i386__
142         {
143                 .callback = assign_all_busses,
144                 .ident = "Samsung X20 Laptop",
145                 .matches = {
146                         DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
147                         DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
148                 },
149         },
150 #endif          /* __i386__ */
151         {}
152 };
153
154 struct pci_bus * __devinit pcibios_scan_root(int busnum)
155 {
156         struct pci_bus *bus = NULL;
157
158         dmi_check_system(pciprobe_dmi_table);
159
160         while ((bus = pci_find_next_bus(bus)) != NULL) {
161                 if (bus->number == busnum) {
162                         /* Already scanned */
163                         return bus;
164                 }
165         }
166
167         printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
168
169         return pci_scan_bus_parented(NULL, busnum, &pci_root_ops, NULL);
170 }
171
172 extern u8 pci_cache_line_size;
173
174 static int __init pcibios_init(void)
175 {
176         struct cpuinfo_x86 *c = &boot_cpu_data;
177
178         if (!raw_pci_ops) {
179                 printk(KERN_WARNING "PCI: System does not support PCI\n");
180                 return 0;
181         }
182
183         /*
184          * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
185          * and P4. It's also good for 386/486s (which actually have 16)
186          * as quite a few PCI devices do not support smaller values.
187          */
188         pci_cache_line_size = 32 >> 2;
189         if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
190                 pci_cache_line_size = 64 >> 2;  /* K7 & K8 */
191         else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
192                 pci_cache_line_size = 128 >> 2; /* P4 */
193
194         pcibios_resource_survey();
195
196 #ifdef CONFIG_PCI_BIOS
197         if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
198                 pcibios_sort();
199 #endif
200         return 0;
201 }
202
203 subsys_initcall(pcibios_init);
204
205 char * __devinit  pcibios_setup(char *str)
206 {
207         if (!strcmp(str, "off")) {
208                 pci_probe = 0;
209                 return NULL;
210         }
211 #ifdef CONFIG_PCI_BIOS
212         else if (!strcmp(str, "bios")) {
213                 pci_probe = PCI_PROBE_BIOS;
214                 return NULL;
215         } else if (!strcmp(str, "nobios")) {
216                 pci_probe &= ~PCI_PROBE_BIOS;
217                 return NULL;
218         } else if (!strcmp(str, "nosort")) {
219                 pci_probe |= PCI_NO_SORT;
220                 return NULL;
221         } else if (!strcmp(str, "biosirq")) {
222                 pci_probe |= PCI_BIOS_IRQ_SCAN;
223                 return NULL;
224         } else if (!strncmp(str, "pirqaddr=", 9)) {
225                 pirq_table_addr = simple_strtoul(str+9, NULL, 0);
226                 return NULL;
227         }
228 #endif
229 #ifdef CONFIG_PCI_DIRECT
230         else if (!strcmp(str, "conf1")) {
231                 pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
232                 return NULL;
233         }
234         else if (!strcmp(str, "conf2")) {
235                 pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
236                 return NULL;
237         }
238 #endif
239 #ifdef CONFIG_PCI_MMCONFIG
240         else if (!strcmp(str, "nommconf")) {
241                 pci_probe &= ~PCI_PROBE_MMCONF;
242                 return NULL;
243         }
244 #endif
245         else if (!strcmp(str, "noacpi")) {
246                 acpi_noirq_set();
247                 return NULL;
248         }
249 #ifndef CONFIG_X86_VISWS
250         else if (!strcmp(str, "usepirqmask")) {
251                 pci_probe |= PCI_USE_PIRQ_MASK;
252                 return NULL;
253         } else if (!strncmp(str, "irqmask=", 8)) {
254                 pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
255                 return NULL;
256         } else if (!strncmp(str, "lastbus=", 8)) {
257                 pcibios_last_bus = simple_strtol(str+8, NULL, 0);
258                 return NULL;
259         }
260 #endif
261         else if (!strcmp(str, "rom")) {
262                 pci_probe |= PCI_ASSIGN_ROMS;
263                 return NULL;
264         } else if (!strcmp(str, "assign-busses")) {
265                 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
266                 return NULL;
267         } else if (!strcmp(str, "routeirq")) {
268                 pci_routeirq = 1;
269                 return NULL;
270         }
271         return str;
272 }
273
274 unsigned int pcibios_assign_all_busses(void)
275 {
276         return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
277 }
278
279 int pcibios_enable_device(struct pci_dev *dev, int mask)
280 {
281         int err;
282
283         if ((err = pcibios_enable_resources(dev, mask)) < 0)
284                 return err;
285
286         return pcibios_enable_irq(dev);
287 }
288
289 void pcibios_disable_device (struct pci_dev *dev)
290 {
291         pcibios_disable_resources(dev);
292         if (pcibios_disable_irq)
293                 pcibios_disable_irq(dev);
294 }