blob: 218778617ee4dfba7e9f0655b88d4dcf40abef35 [file] [log] [blame]
David S. Millera2fb23a2007-02-28 23:35:04 -08001/* pci.c: UltraSparc PCI controller support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
4 * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
David S. Millera2fb23a2007-02-28 23:35:04 -08006 *
7 * OF tree based PCI bus probing taken from the PowerPC port
8 * with minor modifications, see there for credits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/string.h>
14#include <linux/sched.h>
15#include <linux/capability.h>
16#include <linux/errno.h>
David S. Millerc57c2ff2007-05-08 00:43:56 -070017#include <linux/pci.h>
David S. Miller35a17eb2007-02-10 17:41:02 -080018#include <linux/msi.h>
19#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
David S. Miller356d1642008-08-30 00:36:11 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/pgtable.h>
26#include <asm/irq.h>
David S. Millere87dc352006-06-21 18:18:47 -070027#include <asm/prom.h>
David S. Miller01f94c42007-03-04 12:53:19 -080028#include <asm/apb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Miller1e8a8cc2007-02-28 23:38:38 -080030#include "pci_impl.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/* List of all PCI controllers found in the system. */
David S. Miller34768bc2007-05-07 23:06:27 -070033struct pci_pbm_info *pci_pbm_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David S. Miller6c108f12007-05-07 23:49:01 -070035/* Each PBM found gets a unique index. */
36int pci_num_pbms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038volatile int pci_poke_in_progress;
39volatile int pci_poke_cpu = -1;
40volatile int pci_poke_faulted;
41
42static DEFINE_SPINLOCK(pci_poke_lock);
43
44void pci_config_read8(u8 *addr, u8 *ret)
45{
46 unsigned long flags;
47 u8 byte;
48
49 spin_lock_irqsave(&pci_poke_lock, flags);
50 pci_poke_cpu = smp_processor_id();
51 pci_poke_in_progress = 1;
52 pci_poke_faulted = 0;
53 __asm__ __volatile__("membar #Sync\n\t"
54 "lduba [%1] %2, %0\n\t"
55 "membar #Sync"
56 : "=r" (byte)
57 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
58 : "memory");
59 pci_poke_in_progress = 0;
60 pci_poke_cpu = -1;
61 if (!pci_poke_faulted)
62 *ret = byte;
63 spin_unlock_irqrestore(&pci_poke_lock, flags);
64}
65
66void pci_config_read16(u16 *addr, u16 *ret)
67{
68 unsigned long flags;
69 u16 word;
70
71 spin_lock_irqsave(&pci_poke_lock, flags);
72 pci_poke_cpu = smp_processor_id();
73 pci_poke_in_progress = 1;
74 pci_poke_faulted = 0;
75 __asm__ __volatile__("membar #Sync\n\t"
76 "lduha [%1] %2, %0\n\t"
77 "membar #Sync"
78 : "=r" (word)
79 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
80 : "memory");
81 pci_poke_in_progress = 0;
82 pci_poke_cpu = -1;
83 if (!pci_poke_faulted)
84 *ret = word;
85 spin_unlock_irqrestore(&pci_poke_lock, flags);
86}
87
88void pci_config_read32(u32 *addr, u32 *ret)
89{
90 unsigned long flags;
91 u32 dword;
92
93 spin_lock_irqsave(&pci_poke_lock, flags);
94 pci_poke_cpu = smp_processor_id();
95 pci_poke_in_progress = 1;
96 pci_poke_faulted = 0;
97 __asm__ __volatile__("membar #Sync\n\t"
98 "lduwa [%1] %2, %0\n\t"
99 "membar #Sync"
100 : "=r" (dword)
101 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
102 : "memory");
103 pci_poke_in_progress = 0;
104 pci_poke_cpu = -1;
105 if (!pci_poke_faulted)
106 *ret = dword;
107 spin_unlock_irqrestore(&pci_poke_lock, flags);
108}
109
110void pci_config_write8(u8 *addr, u8 val)
111{
112 unsigned long flags;
113
114 spin_lock_irqsave(&pci_poke_lock, flags);
115 pci_poke_cpu = smp_processor_id();
116 pci_poke_in_progress = 1;
117 pci_poke_faulted = 0;
118 __asm__ __volatile__("membar #Sync\n\t"
119 "stba %0, [%1] %2\n\t"
120 "membar #Sync"
121 : /* no outputs */
122 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
123 : "memory");
124 pci_poke_in_progress = 0;
125 pci_poke_cpu = -1;
126 spin_unlock_irqrestore(&pci_poke_lock, flags);
127}
128
129void pci_config_write16(u16 *addr, u16 val)
130{
131 unsigned long flags;
132
133 spin_lock_irqsave(&pci_poke_lock, flags);
134 pci_poke_cpu = smp_processor_id();
135 pci_poke_in_progress = 1;
136 pci_poke_faulted = 0;
137 __asm__ __volatile__("membar #Sync\n\t"
138 "stha %0, [%1] %2\n\t"
139 "membar #Sync"
140 : /* no outputs */
141 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
142 : "memory");
143 pci_poke_in_progress = 0;
144 pci_poke_cpu = -1;
145 spin_unlock_irqrestore(&pci_poke_lock, flags);
146}
147
148void pci_config_write32(u32 *addr, u32 val)
149{
150 unsigned long flags;
151
152 spin_lock_irqsave(&pci_poke_lock, flags);
153 pci_poke_cpu = smp_processor_id();
154 pci_poke_in_progress = 1;
155 pci_poke_faulted = 0;
156 __asm__ __volatile__("membar #Sync\n\t"
157 "stwa %0, [%1] %2\n\t"
158 "membar #Sync"
159 : /* no outputs */
160 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
161 : "memory");
162 pci_poke_in_progress = 0;
163 pci_poke_cpu = -1;
164 spin_unlock_irqrestore(&pci_poke_lock, flags);
165}
166
167/* Probe for all PCI controllers in the system. */
David S. Millere87dc352006-06-21 18:18:47 -0700168extern void sabre_init(struct device_node *, const char *);
169extern void psycho_init(struct device_node *, const char *);
170extern void schizo_init(struct device_node *, const char *);
171extern void schizo_plus_init(struct device_node *, const char *);
172extern void tomatillo_init(struct device_node *, const char *);
173extern void sun4v_pci_init(struct device_node *, const char *);
David S. Miller861fe902007-05-02 17:31:36 -0700174extern void fire_pci_init(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176static struct {
177 char *model_name;
David S. Millere87dc352006-06-21 18:18:47 -0700178 void (*init)(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179} pci_controller_table[] __initdata = {
180 { "SUNW,sabre", sabre_init },
181 { "pci108e,a000", sabre_init },
182 { "pci108e,a001", sabre_init },
183 { "SUNW,psycho", psycho_init },
184 { "pci108e,8000", psycho_init },
185 { "SUNW,schizo", schizo_init },
186 { "pci108e,8001", schizo_init },
187 { "SUNW,schizo+", schizo_plus_init },
188 { "pci108e,8002", schizo_plus_init },
189 { "SUNW,tomatillo", tomatillo_init },
190 { "pci108e,a801", tomatillo_init },
David S. Miller8f6a93a2006-02-09 21:32:07 -0800191 { "SUNW,sun4v-pci", sun4v_pci_init },
David S. Miller861fe902007-05-02 17:31:36 -0700192 { "pciex108e,80f0", fire_pci_init },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
Alejandro Martinez Ruiz29f139c2007-10-22 17:24:19 -0700194#define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
David S. Millere87dc352006-06-21 18:18:47 -0700196static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int i;
199
200 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
201 if (!strncmp(model_name,
202 pci_controller_table[i].model_name,
203 namelen)) {
David S. Millere87dc352006-06-21 18:18:47 -0700204 pci_controller_table[i].init(dp, model_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 1;
206 }
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 return 0;
210}
211
David S. Millere87dc352006-06-21 18:18:47 -0700212static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David S. Millere87dc352006-06-21 18:18:47 -0700214 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int count = 0;
216
David S. Millere87dc352006-06-21 18:18:47 -0700217 for_each_node_by_name(dp, "pci") {
218 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int len;
220
David S. Millere87dc352006-06-21 18:18:47 -0700221 prop = of_find_property(dp, "model", &len);
222 if (!prop)
223 prop = of_find_property(dp, "compatible", &len);
224
225 if (prop) {
226 const char *model = prop->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int item_len = 0;
228
229 /* Our value may be a multi-valued string in the
230 * case of some compatible properties. For sanity,
David S. Millere87dc352006-06-21 18:18:47 -0700231 * only try the first one.
232 */
233 while (model[item_len] && len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 len--;
235 item_len++;
236 }
237
David S. Millere87dc352006-06-21 18:18:47 -0700238 if (handler(model, item_len, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 count++;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 return count;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/* Find each controller in the system, attach and initialize
247 * software state structure for each and link into the
David S. Miller34768bc2007-05-07 23:06:27 -0700248 * pci_pbm_root. Setup the controller enough such
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * that bus scanning can be done.
250 */
251static void __init pci_controller_probe(void)
252{
253 printk("PCI: Probing for controllers.\n");
254
255 pci_controller_scan(pci_controller_init);
256}
257
David S. Miller5840fc62007-05-22 01:24:14 -0700258static int ofpci_verbose;
259
260static int __init ofpci_debug(char *str)
261{
262 int val = 0;
263
264 get_option(&str, &val);
265 if (val)
266 ofpci_verbose = 1;
267 return 1;
268}
269
270__setup("ofpci_debug=", ofpci_debug);
271
David S. Millera2fb23a2007-02-28 23:35:04 -0800272static unsigned long pci_parse_of_flags(u32 addr0)
273{
274 unsigned long flags = 0;
275
276 if (addr0 & 0x02000000) {
277 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
278 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
279 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
280 if (addr0 & 0x40000000)
281 flags |= IORESOURCE_PREFETCH
282 | PCI_BASE_ADDRESS_MEM_PREFETCH;
283 } else if (addr0 & 0x01000000)
284 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
285 return flags;
286}
287
288/* The of_device layer has translated all of the assigned-address properties
289 * into physical address resources, we only have to figure out the register
290 * mapping.
291 */
292static void pci_parse_of_addrs(struct of_device *op,
293 struct device_node *node,
294 struct pci_dev *dev)
295{
296 struct resource *op_res;
297 const u32 *addrs;
298 int proplen;
299
300 addrs = of_get_property(node, "assigned-addresses", &proplen);
301 if (!addrs)
302 return;
David S. Miller5840fc62007-05-22 01:24:14 -0700303 if (ofpci_verbose)
304 printk(" parse addresses (%d bytes) @ %p\n",
305 proplen, addrs);
David S. Millera2fb23a2007-02-28 23:35:04 -0800306 op_res = &op->resource[0];
307 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
308 struct resource *res;
309 unsigned long flags;
310 int i;
311
312 flags = pci_parse_of_flags(addrs[0]);
313 if (!flags)
314 continue;
315 i = addrs[0] & 0xff;
David S. Miller5840fc62007-05-22 01:24:14 -0700316 if (ofpci_verbose)
317 printk(" start: %lx, end: %lx, i: %x\n",
318 op_res->start, op_res->end, i);
David S. Millera2fb23a2007-02-28 23:35:04 -0800319
320 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
321 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
322 } else if (i == dev->rom_base_reg) {
323 res = &dev->resource[PCI_ROM_RESOURCE];
324 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
325 } else {
326 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
327 continue;
328 }
329 res->start = op_res->start;
330 res->end = op_res->end;
331 res->flags = flags;
332 res->name = pci_name(dev);
333 }
334}
335
336struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
337 struct device_node *node,
David S. Millerc26d3c02008-05-01 01:12:40 -0700338 struct pci_bus *bus, int devfn)
David S. Millera2fb23a2007-02-28 23:35:04 -0800339{
340 struct dev_archdata *sd;
David S. Millerae05f872008-08-29 22:42:34 -0700341 struct of_device *op;
David S. Millera2fb23a2007-02-28 23:35:04 -0800342 struct pci_dev *dev;
343 const char *type;
David S. Miller01f94c42007-03-04 12:53:19 -0800344 u32 class;
David S. Millera2fb23a2007-02-28 23:35:04 -0800345
David S. Miller26e63852007-05-10 02:16:27 -0700346 dev = alloc_pci_dev();
David S. Millera2fb23a2007-02-28 23:35:04 -0800347 if (!dev)
348 return NULL;
349
350 sd = &dev->dev.archdata;
351 sd->iommu = pbm->iommu;
352 sd->stc = &pbm->stc;
353 sd->host_controller = pbm;
354 sd->prom_node = node;
David S. Millerae05f872008-08-29 22:42:34 -0700355 sd->op = op = of_find_device_by_node(node);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700356 sd->numa_node = pbm->numa_node;
David S. Millera2fb23a2007-02-28 23:35:04 -0800357
David S. Millerae05f872008-08-29 22:42:34 -0700358 sd = &op->dev.archdata;
David S. Millerad7ad572007-07-27 22:39:14 -0700359 sd->iommu = pbm->iommu;
360 sd->stc = &pbm->stc;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700361 sd->numa_node = pbm->numa_node;
David S. Millerad7ad572007-07-27 22:39:14 -0700362
David S. Millerae05f872008-08-29 22:42:34 -0700363 if (!strcmp(node->name, "ebus"))
364 of_propagate_archdata(op);
365
David S. Millera2fb23a2007-02-28 23:35:04 -0800366 type = of_get_property(node, "device_type", NULL);
367 if (type == NULL)
368 type = "";
369
David S. Miller5840fc62007-05-22 01:24:14 -0700370 if (ofpci_verbose)
371 printk(" create device, devfn: %x, type: %s\n",
372 devfn, type);
David S. Millera2fb23a2007-02-28 23:35:04 -0800373
374 dev->bus = bus;
375 dev->sysdata = node;
376 dev->dev.parent = bus->bridge;
377 dev->dev.bus = &pci_bus_type;
378 dev->devfn = devfn;
379 dev->multifunction = 0; /* maybe a lie? */
380
David S. Millerc26d3c02008-05-01 01:12:40 -0700381 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
382 dev->device = of_getintprop_default(node, "device-id", 0xffff);
383 dev->subsystem_vendor =
384 of_getintprop_default(node, "subsystem-vendor-id", 0);
385 dev->subsystem_device =
386 of_getintprop_default(node, "subsystem-id", 0);
David S. Millera2fb23a2007-02-28 23:35:04 -0800387
David S. Millerc26d3c02008-05-01 01:12:40 -0700388 dev->cfg_size = pci_cfg_space_size(dev);
David S. Miller01f94c42007-03-04 12:53:19 -0800389
David S. Millerc26d3c02008-05-01 01:12:40 -0700390 /* We can't actually use the firmware value, we have
391 * to read what is in the register right now. One
392 * reason is that in the case of IDE interfaces the
393 * firmware can sample the value before the the IDE
394 * interface is programmed into native mode.
395 */
396 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
397 dev->class = class >> 8;
398 dev->revision = class & 0xff;
David S. Miller28f57e72007-03-12 19:40:26 -0700399
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200400 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
David S. Millerc26d3c02008-05-01 01:12:40 -0700401 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
402
David S. Miller5840fc62007-05-22 01:24:14 -0700403 if (ofpci_verbose)
404 printk(" class: 0x%x device name: %s\n",
405 dev->class, pci_name(dev));
David S. Millera2fb23a2007-02-28 23:35:04 -0800406
David S. Miller861fe902007-05-02 17:31:36 -0700407 /* I have seen IDE devices which will not respond to
408 * the bmdma simplex check reads if bus mastering is
409 * disabled.
410 */
411 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
412 pci_set_master(dev);
413
David S. Millera2fb23a2007-02-28 23:35:04 -0800414 dev->current_state = 4; /* unknown power state */
415 dev->error_state = pci_channel_io_normal;
416
David S. Millerc26d3c02008-05-01 01:12:40 -0700417 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
418 /* a PCI-PCI bridge */
David S. Millera2fb23a2007-02-28 23:35:04 -0800419 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
420 dev->rom_base_reg = PCI_ROM_ADDRESS1;
David S. Millerc26d3c02008-05-01 01:12:40 -0700421 } else if (!strcmp(type, "cardbus")) {
422 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
David S. Millera2fb23a2007-02-28 23:35:04 -0800423 } else {
David S. Millerc26d3c02008-05-01 01:12:40 -0700424 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
425 dev->rom_base_reg = PCI_ROM_ADDRESS;
David S. Millera2fb23a2007-02-28 23:35:04 -0800426
David S. Millerc26d3c02008-05-01 01:12:40 -0700427 dev->irq = sd->op->irqs[0];
428 if (dev->irq == 0xffffffff)
429 dev->irq = PCI_IRQ_NONE;
David S. Millera2fb23a2007-02-28 23:35:04 -0800430 }
David S. Millerc26d3c02008-05-01 01:12:40 -0700431
David S. Millera2fb23a2007-02-28 23:35:04 -0800432 pci_parse_of_addrs(sd->op, node, dev);
433
David S. Miller5840fc62007-05-22 01:24:14 -0700434 if (ofpci_verbose)
435 printk(" adding to system ...\n");
David S. Millera2fb23a2007-02-28 23:35:04 -0800436
437 pci_device_add(dev, bus);
438
439 return dev;
440}
441
David S. Millera6009dd2007-05-07 00:01:38 -0700442static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
David S. Miller01f94c42007-03-04 12:53:19 -0800443{
444 u32 idx, first, last;
445
446 first = 8;
447 last = 0;
448 for (idx = 0; idx < 8; idx++) {
449 if ((map & (1 << idx)) != 0) {
450 if (first > idx)
451 first = idx;
452 if (last < idx)
453 last = idx;
454 }
455 }
456
457 *first_p = first;
458 *last_p = last;
459}
460
David S. Millerf16537b2007-05-11 14:29:43 -0700461static void pci_resource_adjust(struct resource *res,
462 struct resource *root)
David S. Miller0bae5f82007-03-08 22:42:19 -0800463{
464 res->start += root->start;
465 res->end += root->start;
466}
467
David S. Miller8c2786c2007-06-07 21:59:44 -0700468/* For PCI bus devices which lack a 'ranges' property we interrogate
469 * the config space values to set the resources, just like the generic
470 * Linux PCI probing code does.
471 */
472static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
473 struct pci_bus *bus,
474 struct pci_pbm_info *pbm)
475{
476 struct resource *res;
477 u8 io_base_lo, io_limit_lo;
478 u16 mem_base_lo, mem_limit_lo;
479 unsigned long base, limit;
480
481 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
482 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
483 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
484 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
485
486 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
487 u16 io_base_hi, io_limit_hi;
488
489 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
490 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
491 base |= (io_base_hi << 16);
492 limit |= (io_limit_hi << 16);
493 }
494
495 res = bus->resource[0];
496 if (base <= limit) {
497 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
498 if (!res->start)
499 res->start = base;
500 if (!res->end)
501 res->end = limit + 0xfff;
502 pci_resource_adjust(res, &pbm->io_space);
503 }
504
505 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
506 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
507 base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
508 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
509
510 res = bus->resource[1];
511 if (base <= limit) {
512 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
513 IORESOURCE_MEM);
514 res->start = base;
515 res->end = limit + 0xfffff;
516 pci_resource_adjust(res, &pbm->mem_space);
517 }
518
519 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
520 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
521 base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
522 limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
523
524 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
525 u32 mem_base_hi, mem_limit_hi;
526
527 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
528 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
529
530 /*
531 * Some bridges set the base > limit by default, and some
532 * (broken) BIOSes do not initialize them. If we find
533 * this, just assume they are not being used.
534 */
535 if (mem_base_hi <= mem_limit_hi) {
536 base |= ((long) mem_base_hi) << 32;
537 limit |= ((long) mem_limit_hi) << 32;
538 }
539 }
540
541 res = bus->resource[2];
542 if (base <= limit) {
543 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
544 IORESOURCE_MEM | IORESOURCE_PREFETCH);
545 res->start = base;
546 res->end = limit + 0xfffff;
547 pci_resource_adjust(res, &pbm->mem_space);
548 }
549}
550
David S. Miller01f94c42007-03-04 12:53:19 -0800551/* Cook up fake bus resources for SUNW,simba PCI bridges which lack
552 * a proper 'ranges' property.
553 */
David S. Millera6009dd2007-05-07 00:01:38 -0700554static void __devinit apb_fake_ranges(struct pci_dev *dev,
555 struct pci_bus *bus,
556 struct pci_pbm_info *pbm)
David S. Miller01f94c42007-03-04 12:53:19 -0800557{
558 struct resource *res;
559 u32 first, last;
560 u8 map;
561
562 pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
563 apb_calc_first_last(map, &first, &last);
564 res = bus->resource[0];
565 res->start = (first << 21);
566 res->end = (last << 21) + ((1 << 21) - 1);
567 res->flags = IORESOURCE_IO;
David S. Miller0bae5f82007-03-08 22:42:19 -0800568 pci_resource_adjust(res, &pbm->io_space);
David S. Miller01f94c42007-03-04 12:53:19 -0800569
570 pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
571 apb_calc_first_last(map, &first, &last);
572 res = bus->resource[1];
573 res->start = (first << 21);
574 res->end = (last << 21) + ((1 << 21) - 1);
575 res->flags = IORESOURCE_MEM;
David S. Miller0bae5f82007-03-08 22:42:19 -0800576 pci_resource_adjust(res, &pbm->mem_space);
David S. Miller01f94c42007-03-04 12:53:19 -0800577}
578
David S. Millera6009dd2007-05-07 00:01:38 -0700579static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
580 struct device_node *node,
581 struct pci_bus *bus);
David S. Millera2fb23a2007-02-28 23:35:04 -0800582
583#define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
584
David S. Millera6009dd2007-05-07 00:01:38 -0700585static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
586 struct device_node *node,
587 struct pci_dev *dev)
David S. Millera2fb23a2007-02-28 23:35:04 -0800588{
589 struct pci_bus *bus;
590 const u32 *busrange, *ranges;
David S. Miller01f94c42007-03-04 12:53:19 -0800591 int len, i, simba;
David S. Millera2fb23a2007-02-28 23:35:04 -0800592 struct resource *res;
593 unsigned int flags;
594 u64 size;
595
David S. Miller5840fc62007-05-22 01:24:14 -0700596 if (ofpci_verbose)
597 printk("of_scan_pci_bridge(%s)\n", node->full_name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800598
599 /* parse bus-range property */
600 busrange = of_get_property(node, "bus-range", &len);
601 if (busrange == NULL || len != 8) {
602 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
603 node->full_name);
604 return;
605 }
606 ranges = of_get_property(node, "ranges", &len);
David S. Miller01f94c42007-03-04 12:53:19 -0800607 simba = 0;
David S. Millera2fb23a2007-02-28 23:35:04 -0800608 if (ranges == NULL) {
David S. Millera165b422007-03-29 01:50:16 -0700609 const char *model = of_get_property(node, "model", NULL);
David S. Miller8c2786c2007-06-07 21:59:44 -0700610 if (model && !strcmp(model, "SUNW,simba"))
David S. Miller01f94c42007-03-04 12:53:19 -0800611 simba = 1;
David S. Millera2fb23a2007-02-28 23:35:04 -0800612 }
613
614 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
615 if (!bus) {
616 printk(KERN_ERR "Failed to create pci bus for %s\n",
617 node->full_name);
618 return;
619 }
620
621 bus->primary = dev->bus->number;
622 bus->subordinate = busrange[1];
623 bus->bridge_ctl = 0;
624
David S. Miller01f94c42007-03-04 12:53:19 -0800625 /* parse ranges property, or cook one up by hand for Simba */
David S. Millera2fb23a2007-02-28 23:35:04 -0800626 /* PCI #address-cells == 3 and #size-cells == 2 always */
627 res = &dev->resource[PCI_BRIDGE_RESOURCES];
628 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
629 res->flags = 0;
630 bus->resource[i] = res;
631 ++res;
632 }
David S. Miller01f94c42007-03-04 12:53:19 -0800633 if (simba) {
634 apb_fake_ranges(dev, bus, pbm);
David S. Miller8c2786c2007-06-07 21:59:44 -0700635 goto after_ranges;
636 } else if (ranges == NULL) {
637 pci_cfg_fake_ranges(dev, bus, pbm);
638 goto after_ranges;
David S. Miller01f94c42007-03-04 12:53:19 -0800639 }
David S. Millera2fb23a2007-02-28 23:35:04 -0800640 i = 1;
641 for (; len >= 32; len -= 32, ranges += 8) {
642 struct resource *root;
643
644 flags = pci_parse_of_flags(ranges[0]);
645 size = GET_64BIT(ranges, 6);
646 if (flags == 0 || size == 0)
647 continue;
648 if (flags & IORESOURCE_IO) {
649 res = bus->resource[0];
650 if (res->flags) {
651 printk(KERN_ERR "PCI: ignoring extra I/O range"
652 " for bridge %s\n", node->full_name);
653 continue;
654 }
655 root = &pbm->io_space;
656 } else {
657 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
658 printk(KERN_ERR "PCI: too many memory ranges"
659 " for bridge %s\n", node->full_name);
660 continue;
661 }
662 res = bus->resource[i];
663 ++i;
664 root = &pbm->mem_space;
665 }
666
667 res->start = GET_64BIT(ranges, 1);
668 res->end = res->start + size - 1;
669 res->flags = flags;
670
671 /* Another way to implement this would be to add an of_device
672 * layer routine that can calculate a resource for a given
673 * range property value in a PCI device.
674 */
David S. Miller0bae5f82007-03-08 22:42:19 -0800675 pci_resource_adjust(res, root);
David S. Millera2fb23a2007-02-28 23:35:04 -0800676 }
David S. Miller8c2786c2007-06-07 21:59:44 -0700677after_ranges:
David S. Millera2fb23a2007-02-28 23:35:04 -0800678 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
679 bus->number);
David S. Miller5840fc62007-05-22 01:24:14 -0700680 if (ofpci_verbose)
681 printk(" bus name: %s\n", bus->name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800682
683 pci_of_scan_bus(pbm, node, bus);
684}
685
David S. Millera6009dd2007-05-07 00:01:38 -0700686static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
687 struct device_node *node,
688 struct pci_bus *bus)
David S. Millera2fb23a2007-02-28 23:35:04 -0800689{
690 struct device_node *child;
691 const u32 *reg;
David S. Miller2cc73452007-09-12 10:15:59 +0200692 int reglen, devfn, prev_devfn;
David S. Millera2fb23a2007-02-28 23:35:04 -0800693 struct pci_dev *dev;
694
David S. Miller5840fc62007-05-22 01:24:14 -0700695 if (ofpci_verbose)
696 printk("PCI: scan_bus[%s] bus no %d\n",
697 node->full_name, bus->number);
David S. Millera2fb23a2007-02-28 23:35:04 -0800698
699 child = NULL;
David S. Miller2cc73452007-09-12 10:15:59 +0200700 prev_devfn = -1;
David S. Millera2fb23a2007-02-28 23:35:04 -0800701 while ((child = of_get_next_child(node, child)) != NULL) {
David S. Miller5840fc62007-05-22 01:24:14 -0700702 if (ofpci_verbose)
703 printk(" * %s\n", child->full_name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800704 reg = of_get_property(child, "reg", &reglen);
705 if (reg == NULL || reglen < 20)
706 continue;
David S. Miller2cc73452007-09-12 10:15:59 +0200707
David S. Millera2fb23a2007-02-28 23:35:04 -0800708 devfn = (reg[0] >> 8) & 0xff;
709
David S. Miller2cc73452007-09-12 10:15:59 +0200710 /* This is a workaround for some device trees
711 * which list PCI devices twice. On the V100
712 * for example, device number 3 is listed twice.
713 * Once as "pm" and once again as "lomp".
714 */
715 if (devfn == prev_devfn)
716 continue;
717 prev_devfn = devfn;
718
David S. Millera2fb23a2007-02-28 23:35:04 -0800719 /* create a new pci_dev for this device */
David S. Millerc26d3c02008-05-01 01:12:40 -0700720 dev = of_create_pci_dev(pbm, child, bus, devfn);
David S. Millera2fb23a2007-02-28 23:35:04 -0800721 if (!dev)
722 continue;
David S. Miller5840fc62007-05-22 01:24:14 -0700723 if (ofpci_verbose)
724 printk("PCI: dev header type: %x\n",
725 dev->hdr_type);
David S. Millera2fb23a2007-02-28 23:35:04 -0800726
727 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
728 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
729 of_scan_pci_bridge(pbm, child, dev);
730 }
731}
732
733static ssize_t
734show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
735{
736 struct pci_dev *pdev;
737 struct device_node *dp;
738
739 pdev = to_pci_dev(dev);
740 dp = pdev->dev.archdata.prom_node;
741
742 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
743}
744
745static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
746
747static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
748{
749 struct pci_dev *dev;
David S. Millera378fd02007-03-01 11:46:13 -0800750 struct pci_bus *child_bus;
David S. Millera2fb23a2007-02-28 23:35:04 -0800751 int err;
752
753 list_for_each_entry(dev, &bus->devices, bus_list) {
754 /* we don't really care if we can create this file or
755 * not, but we need to assign the result of the call
756 * or the world will fall under alien invasion and
757 * everybody will be frozen on a spaceship ready to be
758 * eaten on alpha centauri by some green and jelly
759 * humanoid.
760 */
761 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
762 }
David S. Millera378fd02007-03-01 11:46:13 -0800763 list_for_each_entry(child_bus, &bus->children, node)
764 pci_bus_register_of_sysfs(child_bus);
David S. Millera2fb23a2007-02-28 23:35:04 -0800765}
766
David S. Millera6009dd2007-05-07 00:01:38 -0700767struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
David S. Millera2fb23a2007-02-28 23:35:04 -0800768{
David S. Millera2fb23a2007-02-28 23:35:04 -0800769 struct device_node *node = pbm->prom_node;
770 struct pci_bus *bus;
771
772 printk("PCI: Scanning PBM %s\n", node->full_name);
773
774 /* XXX parent device? XXX */
David S. Millerf1cd8de2007-05-07 23:24:05 -0700775 bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
David S. Millera2fb23a2007-02-28 23:35:04 -0800776 if (!bus) {
777 printk(KERN_ERR "Failed to create bus for %s\n",
778 node->full_name);
779 return NULL;
780 }
781 bus->secondary = pbm->pci_first_busno;
782 bus->subordinate = pbm->pci_last_busno;
783
784 bus->resource[0] = &pbm->io_space;
785 bus->resource[1] = &pbm->mem_space;
786
787 pci_of_scan_bus(pbm, node, bus);
788 pci_bus_add_devices(bus);
789 pci_bus_register_of_sysfs(bus);
790
791 return bus;
792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794static void __init pci_scan_each_controller_bus(void)
795{
David S. Miller34768bc2007-05-07 23:06:27 -0700796 struct pci_pbm_info *pbm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
David S. Miller34768bc2007-05-07 23:06:27 -0700798 for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
799 pbm->scan_bus(pbm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802extern void power_init(void);
803
804static int __init pcibios_init(void)
805{
806 pci_controller_probe();
David S. Miller34768bc2007-05-07 23:06:27 -0700807 if (pci_pbm_root == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
809
810 pci_scan_each_controller_bus();
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 power_init();
813
814 return 0;
815}
816
817subsys_initcall(pcibios_init);
818
Robert Reiff6b45da2007-04-12 13:47:37 -0700819void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct pci_pbm_info *pbm = pbus->sysdata;
822
823 /* Generic PCI bus probing sets these to point at
824 * &io{port,mem}_resouce which is wrong for us.
825 */
826 pbus->resource[0] = &pbm->io_space;
827 pbus->resource[1] = &pbm->mem_space;
828}
829
David S. Miller085ae412005-08-08 13:19:08 -0700830struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 struct pci_pbm_info *pbm = pdev->bus->sysdata;
David S. Miller085ae412005-08-08 13:19:08 -0700833 struct resource *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
David S. Miller085ae412005-08-08 13:19:08 -0700835 if (r->flags & IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 root = &pbm->io_space;
David S. Miller085ae412005-08-08 13:19:08 -0700837 if (r->flags & IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 root = &pbm->mem_space;
839
David S. Miller085ae412005-08-08 13:19:08 -0700840 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843void pcibios_update_irq(struct pci_dev *pdev, int irq)
844{
845}
846
847void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700848 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850}
851
David S. Millera2fb23a2007-02-28 23:35:04 -0800852int pcibios_enable_device(struct pci_dev *dev, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
David S. Millera2fb23a2007-02-28 23:35:04 -0800854 u16 cmd, oldcmd;
855 int i;
856
857 pci_read_config_word(dev, PCI_COMMAND, &cmd);
858 oldcmd = cmd;
859
860 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
861 struct resource *res = &dev->resource[i];
862
863 /* Only set up the requested stuff */
864 if (!(mask & (1<<i)))
865 continue;
866
867 if (res->flags & IORESOURCE_IO)
868 cmd |= PCI_COMMAND_IO;
869 if (res->flags & IORESOURCE_MEM)
870 cmd |= PCI_COMMAND_MEMORY;
871 }
872
873 if (cmd != oldcmd) {
874 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
875 pci_name(dev), cmd);
876 /* Enable the appropriate bits in the PCI command register. */
877 pci_write_config_word(dev, PCI_COMMAND, cmd);
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
880}
881
882void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
883 struct resource *res)
884{
885 struct pci_pbm_info *pbm = pdev->bus->sysdata;
886 struct resource zero_res, *root;
887
888 zero_res.start = 0;
889 zero_res.end = 0;
890 zero_res.flags = res->flags;
891
892 if (res->flags & IORESOURCE_IO)
893 root = &pbm->io_space;
894 else
895 root = &pbm->mem_space;
896
David S. Miller0bae5f82007-03-08 22:42:19 -0800897 pci_resource_adjust(&zero_res, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 region->start = res->start - zero_res.start;
900 region->end = res->end - zero_res.start;
901}
David S. Miller5fdfd422006-04-17 13:34:44 -0700902EXPORT_SYMBOL(pcibios_resource_to_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
905 struct pci_bus_region *region)
906{
907 struct pci_pbm_info *pbm = pdev->bus->sysdata;
908 struct resource *root;
909
910 res->start = region->start;
911 res->end = region->end;
912
913 if (res->flags & IORESOURCE_IO)
914 root = &pbm->io_space;
915 else
916 root = &pbm->mem_space;
917
David S. Miller0bae5f82007-03-08 22:42:19 -0800918 pci_resource_adjust(res, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
Keith Owens41290c12005-08-24 16:06:25 +1000920EXPORT_SYMBOL(pcibios_bus_to_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Robert Reiff6b45da2007-04-12 13:47:37 -0700922char * __devinit pcibios_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return str;
925}
926
927/* Platform support for /proc/bus/pci/X/Y mmap()s. */
928
929/* If the user uses a host-bridge as the PCI device, he may use
930 * this to perform a raw mmap() of the I/O or MEM space behind
931 * that controller.
932 *
933 * This can be useful for execution of x86 PCI bios initialization code
934 * on a PCI card, like the xfree86 int10 stuff does.
935 */
936static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
937 enum pci_mmap_state mmap_state)
938{
David S. Millera2fb23a2007-02-28 23:35:04 -0800939 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 unsigned long space_size, user_offset, user_size;
941
David S. Miller3875c5c2007-03-08 22:52:11 -0800942 if (mmap_state == pci_mmap_io) {
943 space_size = (pbm->io_space.end -
944 pbm->io_space.start) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 } else {
David S. Miller3875c5c2007-03-08 22:52:11 -0800946 space_size = (pbm->mem_space.end -
947 pbm->mem_space.start) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 /* Make sure the request is in range. */
951 user_offset = vma->vm_pgoff << PAGE_SHIFT;
952 user_size = vma->vm_end - vma->vm_start;
953
954 if (user_offset >= space_size ||
955 (user_offset + user_size) > space_size)
956 return -EINVAL;
957
David S. Miller3875c5c2007-03-08 22:52:11 -0800958 if (mmap_state == pci_mmap_io) {
959 vma->vm_pgoff = (pbm->io_space.start +
960 user_offset) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 } else {
David S. Miller3875c5c2007-03-08 22:52:11 -0800962 vma->vm_pgoff = (pbm->mem_space.start +
963 user_offset) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
966 return 0;
967}
968
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700969/* Adjust vm_pgoff of VMA such that it is the physical page offset
970 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 *
972 * Basically, the user finds the base address for his device which he wishes
973 * to mmap. They read the 32-bit value from the config space base register,
974 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
975 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
976 *
977 * Returns negative error code on failure, zero on success.
978 */
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700979static int __pci_mmap_make_offset(struct pci_dev *pdev,
980 struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 enum pci_mmap_state mmap_state)
982{
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700983 unsigned long user_paddr, user_size;
984 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700986 /* First compute the physical address in vma->vm_pgoff,
987 * making sure the user offset is within range in the
988 * appropriate PCI space.
989 */
990 err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
991 if (err)
992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700994 /* If this is a mapping on a host bridge, any address
995 * is OK.
996 */
997 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
998 return err;
999
1000 /* Otherwise make sure it's in the range for one of the
1001 * device's resources.
1002 */
1003 user_paddr = vma->vm_pgoff << PAGE_SHIFT;
1004 user_size = vma->vm_end - vma->vm_start;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001007 struct resource *rp = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 /* Active? */
1010 if (!rp->flags)
1011 continue;
1012
1013 /* Same type? */
1014 if (i == PCI_ROM_RESOURCE) {
1015 if (mmap_state != pci_mmap_mem)
1016 continue;
1017 } else {
1018 if ((mmap_state == pci_mmap_io &&
1019 (rp->flags & IORESOURCE_IO) == 0) ||
1020 (mmap_state == pci_mmap_mem &&
1021 (rp->flags & IORESOURCE_MEM) == 0))
1022 continue;
1023 }
1024
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001025 if ((rp->start <= user_paddr) &&
1026 (user_paddr + user_size) <= (rp->end + 1UL))
1027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001030 if (i > PCI_ROM_RESOURCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -EINVAL;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
1034}
1035
1036/* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1037 * mapping.
1038 */
1039static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1040 enum pci_mmap_state mmap_state)
1041{
1042 vma->vm_flags |= (VM_IO | VM_RESERVED);
1043}
1044
1045/* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1046 * device mapping.
1047 */
1048static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1049 enum pci_mmap_state mmap_state)
1050{
David S. Millera7a6cac2005-09-01 21:51:26 -07001051 /* Our io_remap_pfn_range takes care of this, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054/* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1055 * for this architecture. The region in the process to map is described by vm_start
1056 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1057 * The pci device structure is provided so that architectures may make mapping
1058 * decisions on a per-device or per-bus basis.
1059 *
1060 * Returns a negative error code on failure, zero on success.
1061 */
1062int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1063 enum pci_mmap_state mmap_state,
1064 int write_combine)
1065{
1066 int ret;
1067
1068 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1069 if (ret < 0)
1070 return ret;
1071
1072 __pci_mmap_set_flags(dev, vma, mmap_state);
1073 __pci_mmap_set_pgprot(dev, vma, mmap_state);
1074
David S. Miller14778d92006-03-21 02:29:39 -08001075 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ret = io_remap_pfn_range(vma, vma->vm_start,
1077 vma->vm_pgoff,
1078 vma->vm_end - vma->vm_start,
1079 vma->vm_page_prot);
1080 if (ret)
1081 return ret;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
1084}
1085
David S. Millerc1b1a5f2008-03-19 04:52:48 -07001086#ifdef CONFIG_NUMA
1087int pcibus_to_node(struct pci_bus *pbus)
1088{
1089 struct pci_pbm_info *pbm = pbus->sysdata;
1090
1091 return pbm->numa_node;
1092}
1093EXPORT_SYMBOL(pcibus_to_node);
1094#endif
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/* Return the domain nuber for this pci bus */
1097
1098int pci_domain_nr(struct pci_bus *pbus)
1099{
1100 struct pci_pbm_info *pbm = pbus->sysdata;
1101 int ret;
1102
1103 if (pbm == NULL || pbm->parent == NULL) {
1104 ret = -ENXIO;
1105 } else {
David S. Miller6c108f12007-05-07 23:49:01 -07001106 ret = pbm->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
1109 return ret;
1110}
1111EXPORT_SYMBOL(pci_domain_nr);
1112
David S. Miller35a17eb2007-02-10 17:41:02 -08001113#ifdef CONFIG_PCI_MSI
1114int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1115{
David S. Millera2fb23a2007-02-28 23:35:04 -08001116 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Millere9870c42007-05-07 23:28:50 -07001117 int virt_irq;
David S. Miller35a17eb2007-02-10 17:41:02 -08001118
David S. Millere9870c42007-05-07 23:28:50 -07001119 if (!pbm->setup_msi_irq)
David S. Miller35a17eb2007-02-10 17:41:02 -08001120 return -EINVAL;
1121
David S. Millere9870c42007-05-07 23:28:50 -07001122 return pbm->setup_msi_irq(&virt_irq, pdev, desc);
David S. Miller35a17eb2007-02-10 17:41:02 -08001123}
1124
1125void arch_teardown_msi_irq(unsigned int virt_irq)
1126{
David S. Millerabfd3362007-02-26 09:40:34 -08001127 struct msi_desc *entry = get_irq_msi(virt_irq);
David S. Miller35a17eb2007-02-10 17:41:02 -08001128 struct pci_dev *pdev = entry->dev;
David S. Millera2fb23a2007-02-28 23:35:04 -08001129 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Miller35a17eb2007-02-10 17:41:02 -08001130
David S. Millere9870c42007-05-07 23:28:50 -07001131 if (!pbm->teardown_msi_irq)
David S. Miller35a17eb2007-02-10 17:41:02 -08001132 return;
1133
David S. Millere9870c42007-05-07 23:28:50 -07001134 return pbm->teardown_msi_irq(virt_irq, pdev);
David S. Miller35a17eb2007-02-10 17:41:02 -08001135}
1136#endif /* !(CONFIG_PCI_MSI) */
1137
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001138struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1139{
David S. Millera2fb23a2007-02-28 23:35:04 -08001140 return pdev->dev.archdata.prom_node;
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001141}
1142EXPORT_SYMBOL(pci_device_to_OF_node);
1143
David S. Millerad7ad572007-07-27 22:39:14 -07001144static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
1145{
1146 struct pci_dev *ali_isa_bridge;
1147 u8 val;
1148
1149 /* ALI sound chips generate 31-bits of DMA, a special register
1150 * determines what bit 31 is emitted as.
1151 */
1152 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
1153 PCI_DEVICE_ID_AL_M1533,
1154 NULL);
1155
1156 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
1157 if (set_bit)
1158 val |= 0x01;
1159 else
1160 val &= ~0x01;
1161 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
1162 pci_dev_put(ali_isa_bridge);
1163}
1164
1165int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
1166{
1167 u64 dma_addr_mask;
1168
1169 if (pdev == NULL) {
1170 dma_addr_mask = 0xffffffff;
1171 } else {
1172 struct iommu *iommu = pdev->dev.archdata.iommu;
1173
1174 dma_addr_mask = iommu->dma_addr_mask;
1175
1176 if (pdev->vendor == PCI_VENDOR_ID_AL &&
1177 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
1178 device_mask == 0x7fffffff) {
1179 ali_sound_dma_hack(pdev,
1180 (dma_addr_mask & 0x80000000) != 0);
1181 return 1;
1182 }
1183 }
1184
1185 if (device_mask >= (1UL << 32UL))
1186 return 0;
1187
1188 return (device_mask & dma_addr_mask) == dma_addr_mask;
1189}
1190
David S. Millerbcea1db2007-12-25 02:20:33 -08001191void pci_resource_to_user(const struct pci_dev *pdev, int bar,
1192 const struct resource *rp, resource_size_t *start,
1193 resource_size_t *end)
1194{
1195 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1196 unsigned long offset;
1197
1198 if (rp->flags & IORESOURCE_IO)
1199 offset = pbm->io_space.start;
1200 else
1201 offset = pbm->mem_space.start;
1202
1203 *start = rp->start - offset;
1204 *end = rp->end - offset;
1205}