blob: 49f912766519c493ebf51db93e4c2ced79a81db8 [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>
21
22#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/pgtable.h>
24#include <asm/irq.h>
25#include <asm/ebus.h>
26#include <asm/isa.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#ifndef CONFIG_PCI
33/* A "nop" PCI implementation. */
34asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
35 unsigned long off, unsigned long len,
36 unsigned char *buf)
37{
38 return 0;
39}
40asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
41 unsigned long off, unsigned long len,
42 unsigned char *buf)
43{
44 return 0;
45}
46#else
47
48/* List of all PCI controllers found in the system. */
David S. Miller34768bc2007-05-07 23:06:27 -070049struct pci_pbm_info *pci_pbm_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
David S. Miller6c108f12007-05-07 23:49:01 -070051/* Each PBM found gets a unique index. */
52int pci_num_pbms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054volatile int pci_poke_in_progress;
55volatile int pci_poke_cpu = -1;
56volatile int pci_poke_faulted;
57
58static DEFINE_SPINLOCK(pci_poke_lock);
59
60void pci_config_read8(u8 *addr, u8 *ret)
61{
62 unsigned long flags;
63 u8 byte;
64
65 spin_lock_irqsave(&pci_poke_lock, flags);
66 pci_poke_cpu = smp_processor_id();
67 pci_poke_in_progress = 1;
68 pci_poke_faulted = 0;
69 __asm__ __volatile__("membar #Sync\n\t"
70 "lduba [%1] %2, %0\n\t"
71 "membar #Sync"
72 : "=r" (byte)
73 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
74 : "memory");
75 pci_poke_in_progress = 0;
76 pci_poke_cpu = -1;
77 if (!pci_poke_faulted)
78 *ret = byte;
79 spin_unlock_irqrestore(&pci_poke_lock, flags);
80}
81
82void pci_config_read16(u16 *addr, u16 *ret)
83{
84 unsigned long flags;
85 u16 word;
86
87 spin_lock_irqsave(&pci_poke_lock, flags);
88 pci_poke_cpu = smp_processor_id();
89 pci_poke_in_progress = 1;
90 pci_poke_faulted = 0;
91 __asm__ __volatile__("membar #Sync\n\t"
92 "lduha [%1] %2, %0\n\t"
93 "membar #Sync"
94 : "=r" (word)
95 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
96 : "memory");
97 pci_poke_in_progress = 0;
98 pci_poke_cpu = -1;
99 if (!pci_poke_faulted)
100 *ret = word;
101 spin_unlock_irqrestore(&pci_poke_lock, flags);
102}
103
104void pci_config_read32(u32 *addr, u32 *ret)
105{
106 unsigned long flags;
107 u32 dword;
108
109 spin_lock_irqsave(&pci_poke_lock, flags);
110 pci_poke_cpu = smp_processor_id();
111 pci_poke_in_progress = 1;
112 pci_poke_faulted = 0;
113 __asm__ __volatile__("membar #Sync\n\t"
114 "lduwa [%1] %2, %0\n\t"
115 "membar #Sync"
116 : "=r" (dword)
117 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
118 : "memory");
119 pci_poke_in_progress = 0;
120 pci_poke_cpu = -1;
121 if (!pci_poke_faulted)
122 *ret = dword;
123 spin_unlock_irqrestore(&pci_poke_lock, flags);
124}
125
126void pci_config_write8(u8 *addr, u8 val)
127{
128 unsigned long flags;
129
130 spin_lock_irqsave(&pci_poke_lock, flags);
131 pci_poke_cpu = smp_processor_id();
132 pci_poke_in_progress = 1;
133 pci_poke_faulted = 0;
134 __asm__ __volatile__("membar #Sync\n\t"
135 "stba %0, [%1] %2\n\t"
136 "membar #Sync"
137 : /* no outputs */
138 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
139 : "memory");
140 pci_poke_in_progress = 0;
141 pci_poke_cpu = -1;
142 spin_unlock_irqrestore(&pci_poke_lock, flags);
143}
144
145void pci_config_write16(u16 *addr, u16 val)
146{
147 unsigned long flags;
148
149 spin_lock_irqsave(&pci_poke_lock, flags);
150 pci_poke_cpu = smp_processor_id();
151 pci_poke_in_progress = 1;
152 pci_poke_faulted = 0;
153 __asm__ __volatile__("membar #Sync\n\t"
154 "stha %0, [%1] %2\n\t"
155 "membar #Sync"
156 : /* no outputs */
157 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
158 : "memory");
159 pci_poke_in_progress = 0;
160 pci_poke_cpu = -1;
161 spin_unlock_irqrestore(&pci_poke_lock, flags);
162}
163
164void pci_config_write32(u32 *addr, u32 val)
165{
166 unsigned long flags;
167
168 spin_lock_irqsave(&pci_poke_lock, flags);
169 pci_poke_cpu = smp_processor_id();
170 pci_poke_in_progress = 1;
171 pci_poke_faulted = 0;
172 __asm__ __volatile__("membar #Sync\n\t"
173 "stwa %0, [%1] %2\n\t"
174 "membar #Sync"
175 : /* no outputs */
176 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
177 : "memory");
178 pci_poke_in_progress = 0;
179 pci_poke_cpu = -1;
180 spin_unlock_irqrestore(&pci_poke_lock, flags);
181}
182
183/* Probe for all PCI controllers in the system. */
David S. Millere87dc352006-06-21 18:18:47 -0700184extern void sabre_init(struct device_node *, const char *);
185extern void psycho_init(struct device_node *, const char *);
186extern void schizo_init(struct device_node *, const char *);
187extern void schizo_plus_init(struct device_node *, const char *);
188extern void tomatillo_init(struct device_node *, const char *);
189extern void sun4v_pci_init(struct device_node *, const char *);
David S. Miller861fe902007-05-02 17:31:36 -0700190extern void fire_pci_init(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192static struct {
193 char *model_name;
David S. Millere87dc352006-06-21 18:18:47 -0700194 void (*init)(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195} pci_controller_table[] __initdata = {
196 { "SUNW,sabre", sabre_init },
197 { "pci108e,a000", sabre_init },
198 { "pci108e,a001", sabre_init },
199 { "SUNW,psycho", psycho_init },
200 { "pci108e,8000", psycho_init },
201 { "SUNW,schizo", schizo_init },
202 { "pci108e,8001", schizo_init },
203 { "SUNW,schizo+", schizo_plus_init },
204 { "pci108e,8002", schizo_plus_init },
205 { "SUNW,tomatillo", tomatillo_init },
206 { "pci108e,a801", tomatillo_init },
David S. Miller8f6a93a2006-02-09 21:32:07 -0800207 { "SUNW,sun4v-pci", sun4v_pci_init },
David S. Miller861fe902007-05-02 17:31:36 -0700208 { "pciex108e,80f0", fire_pci_init },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
Alejandro Martinez Ruiz29f139c2007-10-22 17:24:19 -0700210#define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
David S. Millere87dc352006-06-21 18:18:47 -0700212static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int i;
215
216 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
217 if (!strncmp(model_name,
218 pci_controller_table[i].model_name,
219 namelen)) {
David S. Millere87dc352006-06-21 18:18:47 -0700220 pci_controller_table[i].init(dp, model_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 1;
222 }
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 return 0;
226}
227
David S. Millere87dc352006-06-21 18:18:47 -0700228static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
David S. Millere87dc352006-06-21 18:18:47 -0700230 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int count = 0;
232
David S. Millere87dc352006-06-21 18:18:47 -0700233 for_each_node_by_name(dp, "pci") {
234 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int len;
236
David S. Millere87dc352006-06-21 18:18:47 -0700237 prop = of_find_property(dp, "model", &len);
238 if (!prop)
239 prop = of_find_property(dp, "compatible", &len);
240
241 if (prop) {
242 const char *model = prop->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int item_len = 0;
244
245 /* Our value may be a multi-valued string in the
246 * case of some compatible properties. For sanity,
David S. Millere87dc352006-06-21 18:18:47 -0700247 * only try the first one.
248 */
249 while (model[item_len] && len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 len--;
251 item_len++;
252 }
253
David S. Millere87dc352006-06-21 18:18:47 -0700254 if (handler(model, item_len, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 count++;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
259 return count;
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* Find each controller in the system, attach and initialize
263 * software state structure for each and link into the
David S. Miller34768bc2007-05-07 23:06:27 -0700264 * pci_pbm_root. Setup the controller enough such
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * that bus scanning can be done.
266 */
267static void __init pci_controller_probe(void)
268{
269 printk("PCI: Probing for controllers.\n");
270
271 pci_controller_scan(pci_controller_init);
272}
273
David S. Miller5840fc62007-05-22 01:24:14 -0700274static int ofpci_verbose;
275
276static int __init ofpci_debug(char *str)
277{
278 int val = 0;
279
280 get_option(&str, &val);
281 if (val)
282 ofpci_verbose = 1;
283 return 1;
284}
285
286__setup("ofpci_debug=", ofpci_debug);
287
David S. Millera2fb23a2007-02-28 23:35:04 -0800288static unsigned long pci_parse_of_flags(u32 addr0)
289{
290 unsigned long flags = 0;
291
292 if (addr0 & 0x02000000) {
293 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
294 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
295 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
296 if (addr0 & 0x40000000)
297 flags |= IORESOURCE_PREFETCH
298 | PCI_BASE_ADDRESS_MEM_PREFETCH;
299 } else if (addr0 & 0x01000000)
300 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
301 return flags;
302}
303
304/* The of_device layer has translated all of the assigned-address properties
305 * into physical address resources, we only have to figure out the register
306 * mapping.
307 */
308static void pci_parse_of_addrs(struct of_device *op,
309 struct device_node *node,
310 struct pci_dev *dev)
311{
312 struct resource *op_res;
313 const u32 *addrs;
314 int proplen;
315
316 addrs = of_get_property(node, "assigned-addresses", &proplen);
317 if (!addrs)
318 return;
David S. Miller5840fc62007-05-22 01:24:14 -0700319 if (ofpci_verbose)
320 printk(" parse addresses (%d bytes) @ %p\n",
321 proplen, addrs);
David S. Millera2fb23a2007-02-28 23:35:04 -0800322 op_res = &op->resource[0];
323 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
324 struct resource *res;
325 unsigned long flags;
326 int i;
327
328 flags = pci_parse_of_flags(addrs[0]);
329 if (!flags)
330 continue;
331 i = addrs[0] & 0xff;
David S. Miller5840fc62007-05-22 01:24:14 -0700332 if (ofpci_verbose)
333 printk(" start: %lx, end: %lx, i: %x\n",
334 op_res->start, op_res->end, i);
David S. Millera2fb23a2007-02-28 23:35:04 -0800335
336 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
337 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
338 } else if (i == dev->rom_base_reg) {
339 res = &dev->resource[PCI_ROM_RESOURCE];
340 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
341 } else {
342 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
343 continue;
344 }
345 res->start = op_res->start;
346 res->end = op_res->end;
347 res->flags = flags;
348 res->name = pci_name(dev);
349 }
350}
351
352struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
353 struct device_node *node,
David S. Miller97b3cf02007-03-11 16:42:53 -0700354 struct pci_bus *bus, int devfn,
355 int host_controller)
David S. Millera2fb23a2007-02-28 23:35:04 -0800356{
357 struct dev_archdata *sd;
358 struct pci_dev *dev;
359 const char *type;
David S. Miller01f94c42007-03-04 12:53:19 -0800360 u32 class;
David S. Millera2fb23a2007-02-28 23:35:04 -0800361
David S. Miller26e63852007-05-10 02:16:27 -0700362 dev = alloc_pci_dev();
David S. Millera2fb23a2007-02-28 23:35:04 -0800363 if (!dev)
364 return NULL;
365
366 sd = &dev->dev.archdata;
367 sd->iommu = pbm->iommu;
368 sd->stc = &pbm->stc;
369 sd->host_controller = pbm;
370 sd->prom_node = node;
371 sd->op = of_find_device_by_node(node);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700372 sd->numa_node = pbm->numa_node;
David S. Millera2fb23a2007-02-28 23:35:04 -0800373
David S. Millerad7ad572007-07-27 22:39:14 -0700374 sd = &sd->op->dev.archdata;
375 sd->iommu = pbm->iommu;
376 sd->stc = &pbm->stc;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700377 sd->numa_node = pbm->numa_node;
David S. Millerad7ad572007-07-27 22:39:14 -0700378
David S. Millera2fb23a2007-02-28 23:35:04 -0800379 type = of_get_property(node, "device_type", NULL);
380 if (type == NULL)
381 type = "";
382
David S. Miller5840fc62007-05-22 01:24:14 -0700383 if (ofpci_verbose)
384 printk(" create device, devfn: %x, type: %s\n",
385 devfn, type);
David S. Millera2fb23a2007-02-28 23:35:04 -0800386
387 dev->bus = bus;
388 dev->sysdata = node;
389 dev->dev.parent = bus->bridge;
390 dev->dev.bus = &pci_bus_type;
391 dev->devfn = devfn;
392 dev->multifunction = 0; /* maybe a lie? */
393
David S. Miller97b3cf02007-03-11 16:42:53 -0700394 if (host_controller) {
David S. Millera2d6ea02007-07-25 23:30:16 -0700395 if (tlb_type != hypervisor) {
396 pci_read_config_word(dev, PCI_VENDOR_ID,
397 &dev->vendor);
398 pci_read_config_word(dev, PCI_DEVICE_ID,
399 &dev->device);
400 } else {
401 dev->vendor = PCI_VENDOR_ID_SUN;
402 dev->device = 0x80f0;
403 }
David S. Miller97b3cf02007-03-11 16:42:53 -0700404 dev->cfg_size = 256;
David S. Miller28f57e72007-03-12 19:40:26 -0700405 dev->class = PCI_CLASS_BRIDGE_HOST << 8;
406 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
407 0x00, PCI_SLOT(devfn), PCI_FUNC(devfn));
David S. Miller97b3cf02007-03-11 16:42:53 -0700408 } else {
409 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
410 dev->device = of_getintprop_default(node, "device-id", 0xffff);
411 dev->subsystem_vendor =
412 of_getintprop_default(node, "subsystem-vendor-id", 0);
413 dev->subsystem_device =
414 of_getintprop_default(node, "subsystem-id", 0);
David S. Millera2fb23a2007-02-28 23:35:04 -0800415
David S. Miller97b3cf02007-03-11 16:42:53 -0700416 dev->cfg_size = pci_cfg_space_size(dev);
David S. Miller01f94c42007-03-04 12:53:19 -0800417
David S. Miller97b3cf02007-03-11 16:42:53 -0700418 /* We can't actually use the firmware value, we have
419 * to read what is in the register right now. One
420 * reason is that in the case of IDE interfaces the
421 * firmware can sample the value before the the IDE
422 * interface is programmed into native mode.
423 */
424 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
425 dev->class = class >> 8;
Auke Kokb8a3a522007-06-08 15:46:30 -0700426 dev->revision = class & 0xff;
David S. Miller28f57e72007-03-12 19:40:26 -0700427
428 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
429 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
David S. Miller97b3cf02007-03-11 16:42:53 -0700430 }
David S. Miller5840fc62007-05-22 01:24:14 -0700431 if (ofpci_verbose)
432 printk(" class: 0x%x device name: %s\n",
433 dev->class, pci_name(dev));
David S. Millera2fb23a2007-02-28 23:35:04 -0800434
David S. Miller861fe902007-05-02 17:31:36 -0700435 /* I have seen IDE devices which will not respond to
436 * the bmdma simplex check reads if bus mastering is
437 * disabled.
438 */
439 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
440 pci_set_master(dev);
441
David S. Millera2fb23a2007-02-28 23:35:04 -0800442 dev->current_state = 4; /* unknown power state */
443 dev->error_state = pci_channel_io_normal;
444
David S. Miller97b3cf02007-03-11 16:42:53 -0700445 if (host_controller) {
David S. Millera2fb23a2007-02-28 23:35:04 -0800446 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
447 dev->rom_base_reg = PCI_ROM_ADDRESS1;
David S. Miller97b3cf02007-03-11 16:42:53 -0700448 dev->irq = PCI_IRQ_NONE;
David S. Millera2fb23a2007-02-28 23:35:04 -0800449 } else {
David S. Miller97b3cf02007-03-11 16:42:53 -0700450 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
451 /* a PCI-PCI bridge */
452 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
453 dev->rom_base_reg = PCI_ROM_ADDRESS1;
454 } else if (!strcmp(type, "cardbus")) {
455 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
456 } else {
457 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
458 dev->rom_base_reg = PCI_ROM_ADDRESS;
David S. Millera2fb23a2007-02-28 23:35:04 -0800459
David S. Miller97b3cf02007-03-11 16:42:53 -0700460 dev->irq = sd->op->irqs[0];
461 if (dev->irq == 0xffffffff)
462 dev->irq = PCI_IRQ_NONE;
463 }
David S. Millera2fb23a2007-02-28 23:35:04 -0800464 }
David S. Millera2fb23a2007-02-28 23:35:04 -0800465 pci_parse_of_addrs(sd->op, node, dev);
466
David S. Miller5840fc62007-05-22 01:24:14 -0700467 if (ofpci_verbose)
468 printk(" adding to system ...\n");
David S. Millera2fb23a2007-02-28 23:35:04 -0800469
470 pci_device_add(dev, bus);
471
472 return dev;
473}
474
David S. Millera6009dd2007-05-07 00:01:38 -0700475static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
David S. Miller01f94c42007-03-04 12:53:19 -0800476{
477 u32 idx, first, last;
478
479 first = 8;
480 last = 0;
481 for (idx = 0; idx < 8; idx++) {
482 if ((map & (1 << idx)) != 0) {
483 if (first > idx)
484 first = idx;
485 if (last < idx)
486 last = idx;
487 }
488 }
489
490 *first_p = first;
491 *last_p = last;
492}
493
David S. Millerf16537b2007-05-11 14:29:43 -0700494static void pci_resource_adjust(struct resource *res,
495 struct resource *root)
David S. Miller0bae5f82007-03-08 22:42:19 -0800496{
497 res->start += root->start;
498 res->end += root->start;
499}
500
David S. Miller8c2786c2007-06-07 21:59:44 -0700501/* For PCI bus devices which lack a 'ranges' property we interrogate
502 * the config space values to set the resources, just like the generic
503 * Linux PCI probing code does.
504 */
505static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
506 struct pci_bus *bus,
507 struct pci_pbm_info *pbm)
508{
509 struct resource *res;
510 u8 io_base_lo, io_limit_lo;
511 u16 mem_base_lo, mem_limit_lo;
512 unsigned long base, limit;
513
514 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
515 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
516 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
517 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
518
519 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
520 u16 io_base_hi, io_limit_hi;
521
522 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
523 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
524 base |= (io_base_hi << 16);
525 limit |= (io_limit_hi << 16);
526 }
527
528 res = bus->resource[0];
529 if (base <= limit) {
530 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
531 if (!res->start)
532 res->start = base;
533 if (!res->end)
534 res->end = limit + 0xfff;
535 pci_resource_adjust(res, &pbm->io_space);
536 }
537
538 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
539 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
540 base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
541 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
542
543 res = bus->resource[1];
544 if (base <= limit) {
545 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
546 IORESOURCE_MEM);
547 res->start = base;
548 res->end = limit + 0xfffff;
549 pci_resource_adjust(res, &pbm->mem_space);
550 }
551
552 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
553 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
554 base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
555 limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
556
557 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
558 u32 mem_base_hi, mem_limit_hi;
559
560 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
561 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
562
563 /*
564 * Some bridges set the base > limit by default, and some
565 * (broken) BIOSes do not initialize them. If we find
566 * this, just assume they are not being used.
567 */
568 if (mem_base_hi <= mem_limit_hi) {
569 base |= ((long) mem_base_hi) << 32;
570 limit |= ((long) mem_limit_hi) << 32;
571 }
572 }
573
574 res = bus->resource[2];
575 if (base <= limit) {
576 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
577 IORESOURCE_MEM | IORESOURCE_PREFETCH);
578 res->start = base;
579 res->end = limit + 0xfffff;
580 pci_resource_adjust(res, &pbm->mem_space);
581 }
582}
583
David S. Miller01f94c42007-03-04 12:53:19 -0800584/* Cook up fake bus resources for SUNW,simba PCI bridges which lack
585 * a proper 'ranges' property.
586 */
David S. Millera6009dd2007-05-07 00:01:38 -0700587static void __devinit apb_fake_ranges(struct pci_dev *dev,
588 struct pci_bus *bus,
589 struct pci_pbm_info *pbm)
David S. Miller01f94c42007-03-04 12:53:19 -0800590{
591 struct resource *res;
592 u32 first, last;
593 u8 map;
594
595 pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
596 apb_calc_first_last(map, &first, &last);
597 res = bus->resource[0];
598 res->start = (first << 21);
599 res->end = (last << 21) + ((1 << 21) - 1);
600 res->flags = IORESOURCE_IO;
David S. Miller0bae5f82007-03-08 22:42:19 -0800601 pci_resource_adjust(res, &pbm->io_space);
David S. Miller01f94c42007-03-04 12:53:19 -0800602
603 pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
604 apb_calc_first_last(map, &first, &last);
605 res = bus->resource[1];
606 res->start = (first << 21);
607 res->end = (last << 21) + ((1 << 21) - 1);
608 res->flags = IORESOURCE_MEM;
David S. Miller0bae5f82007-03-08 22:42:19 -0800609 pci_resource_adjust(res, &pbm->mem_space);
David S. Miller01f94c42007-03-04 12:53:19 -0800610}
611
David S. Millera6009dd2007-05-07 00:01:38 -0700612static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
613 struct device_node *node,
614 struct pci_bus *bus);
David S. Millera2fb23a2007-02-28 23:35:04 -0800615
616#define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
617
David S. Millera6009dd2007-05-07 00:01:38 -0700618static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
619 struct device_node *node,
620 struct pci_dev *dev)
David S. Millera2fb23a2007-02-28 23:35:04 -0800621{
622 struct pci_bus *bus;
623 const u32 *busrange, *ranges;
David S. Miller01f94c42007-03-04 12:53:19 -0800624 int len, i, simba;
David S. Millera2fb23a2007-02-28 23:35:04 -0800625 struct resource *res;
626 unsigned int flags;
627 u64 size;
628
David S. Miller5840fc62007-05-22 01:24:14 -0700629 if (ofpci_verbose)
630 printk("of_scan_pci_bridge(%s)\n", node->full_name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800631
632 /* parse bus-range property */
633 busrange = of_get_property(node, "bus-range", &len);
634 if (busrange == NULL || len != 8) {
635 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
636 node->full_name);
637 return;
638 }
639 ranges = of_get_property(node, "ranges", &len);
David S. Miller01f94c42007-03-04 12:53:19 -0800640 simba = 0;
David S. Millera2fb23a2007-02-28 23:35:04 -0800641 if (ranges == NULL) {
David S. Millera165b422007-03-29 01:50:16 -0700642 const char *model = of_get_property(node, "model", NULL);
David S. Miller8c2786c2007-06-07 21:59:44 -0700643 if (model && !strcmp(model, "SUNW,simba"))
David S. Miller01f94c42007-03-04 12:53:19 -0800644 simba = 1;
David S. Millera2fb23a2007-02-28 23:35:04 -0800645 }
646
647 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
648 if (!bus) {
649 printk(KERN_ERR "Failed to create pci bus for %s\n",
650 node->full_name);
651 return;
652 }
653
654 bus->primary = dev->bus->number;
655 bus->subordinate = busrange[1];
656 bus->bridge_ctl = 0;
657
David S. Miller01f94c42007-03-04 12:53:19 -0800658 /* parse ranges property, or cook one up by hand for Simba */
David S. Millera2fb23a2007-02-28 23:35:04 -0800659 /* PCI #address-cells == 3 and #size-cells == 2 always */
660 res = &dev->resource[PCI_BRIDGE_RESOURCES];
661 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
662 res->flags = 0;
663 bus->resource[i] = res;
664 ++res;
665 }
David S. Miller01f94c42007-03-04 12:53:19 -0800666 if (simba) {
667 apb_fake_ranges(dev, bus, pbm);
David S. Miller8c2786c2007-06-07 21:59:44 -0700668 goto after_ranges;
669 } else if (ranges == NULL) {
670 pci_cfg_fake_ranges(dev, bus, pbm);
671 goto after_ranges;
David S. Miller01f94c42007-03-04 12:53:19 -0800672 }
David S. Millera2fb23a2007-02-28 23:35:04 -0800673 i = 1;
674 for (; len >= 32; len -= 32, ranges += 8) {
675 struct resource *root;
676
677 flags = pci_parse_of_flags(ranges[0]);
678 size = GET_64BIT(ranges, 6);
679 if (flags == 0 || size == 0)
680 continue;
681 if (flags & IORESOURCE_IO) {
682 res = bus->resource[0];
683 if (res->flags) {
684 printk(KERN_ERR "PCI: ignoring extra I/O range"
685 " for bridge %s\n", node->full_name);
686 continue;
687 }
688 root = &pbm->io_space;
689 } else {
690 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
691 printk(KERN_ERR "PCI: too many memory ranges"
692 " for bridge %s\n", node->full_name);
693 continue;
694 }
695 res = bus->resource[i];
696 ++i;
697 root = &pbm->mem_space;
698 }
699
700 res->start = GET_64BIT(ranges, 1);
701 res->end = res->start + size - 1;
702 res->flags = flags;
703
704 /* Another way to implement this would be to add an of_device
705 * layer routine that can calculate a resource for a given
706 * range property value in a PCI device.
707 */
David S. Miller0bae5f82007-03-08 22:42:19 -0800708 pci_resource_adjust(res, root);
David S. Millera2fb23a2007-02-28 23:35:04 -0800709 }
David S. Miller8c2786c2007-06-07 21:59:44 -0700710after_ranges:
David S. Millera2fb23a2007-02-28 23:35:04 -0800711 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
712 bus->number);
David S. Miller5840fc62007-05-22 01:24:14 -0700713 if (ofpci_verbose)
714 printk(" bus name: %s\n", bus->name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800715
716 pci_of_scan_bus(pbm, node, bus);
717}
718
David S. Millera6009dd2007-05-07 00:01:38 -0700719static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
720 struct device_node *node,
721 struct pci_bus *bus)
David S. Millera2fb23a2007-02-28 23:35:04 -0800722{
723 struct device_node *child;
724 const u32 *reg;
David S. Miller2cc73452007-09-12 10:15:59 +0200725 int reglen, devfn, prev_devfn;
David S. Millera2fb23a2007-02-28 23:35:04 -0800726 struct pci_dev *dev;
727
David S. Miller5840fc62007-05-22 01:24:14 -0700728 if (ofpci_verbose)
729 printk("PCI: scan_bus[%s] bus no %d\n",
730 node->full_name, bus->number);
David S. Millera2fb23a2007-02-28 23:35:04 -0800731
732 child = NULL;
David S. Miller2cc73452007-09-12 10:15:59 +0200733 prev_devfn = -1;
David S. Millera2fb23a2007-02-28 23:35:04 -0800734 while ((child = of_get_next_child(node, child)) != NULL) {
David S. Miller5840fc62007-05-22 01:24:14 -0700735 if (ofpci_verbose)
736 printk(" * %s\n", child->full_name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800737 reg = of_get_property(child, "reg", &reglen);
738 if (reg == NULL || reglen < 20)
739 continue;
David S. Miller2cc73452007-09-12 10:15:59 +0200740
David S. Millera2fb23a2007-02-28 23:35:04 -0800741 devfn = (reg[0] >> 8) & 0xff;
742
David S. Miller2cc73452007-09-12 10:15:59 +0200743 /* This is a workaround for some device trees
744 * which list PCI devices twice. On the V100
745 * for example, device number 3 is listed twice.
746 * Once as "pm" and once again as "lomp".
747 */
748 if (devfn == prev_devfn)
749 continue;
750 prev_devfn = devfn;
751
David S. Millera2fb23a2007-02-28 23:35:04 -0800752 /* create a new pci_dev for this device */
David S. Miller97b3cf02007-03-11 16:42:53 -0700753 dev = of_create_pci_dev(pbm, child, bus, devfn, 0);
David S. Millera2fb23a2007-02-28 23:35:04 -0800754 if (!dev)
755 continue;
David S. Miller5840fc62007-05-22 01:24:14 -0700756 if (ofpci_verbose)
757 printk("PCI: dev header type: %x\n",
758 dev->hdr_type);
David S. Millera2fb23a2007-02-28 23:35:04 -0800759
760 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
761 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
762 of_scan_pci_bridge(pbm, child, dev);
763 }
764}
765
766static ssize_t
767show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
768{
769 struct pci_dev *pdev;
770 struct device_node *dp;
771
772 pdev = to_pci_dev(dev);
773 dp = pdev->dev.archdata.prom_node;
774
775 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
776}
777
778static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
779
780static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
781{
782 struct pci_dev *dev;
David S. Millera378fd02007-03-01 11:46:13 -0800783 struct pci_bus *child_bus;
David S. Millera2fb23a2007-02-28 23:35:04 -0800784 int err;
785
786 list_for_each_entry(dev, &bus->devices, bus_list) {
787 /* we don't really care if we can create this file or
788 * not, but we need to assign the result of the call
789 * or the world will fall under alien invasion and
790 * everybody will be frozen on a spaceship ready to be
791 * eaten on alpha centauri by some green and jelly
792 * humanoid.
793 */
794 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
795 }
David S. Millera378fd02007-03-01 11:46:13 -0800796 list_for_each_entry(child_bus, &bus->children, node)
797 pci_bus_register_of_sysfs(child_bus);
David S. Millera2fb23a2007-02-28 23:35:04 -0800798}
799
David S. Miller97b3cf02007-03-11 16:42:53 -0700800int pci_host_bridge_read_pci_cfg(struct pci_bus *bus_dev,
801 unsigned int devfn,
802 int where, int size,
803 u32 *value)
804{
805 static u8 fake_pci_config[] = {
806 0x8e, 0x10, /* Vendor: 0x108e (Sun) */
David S. Millera2d6ea02007-07-25 23:30:16 -0700807 0xf0, 0x80, /* Device: 0x80f0 (Fire) */
David S. Miller97b3cf02007-03-11 16:42:53 -0700808 0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
809 0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
810 0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
811 0x00, /* Cacheline: 0x00 */
812 0x40, /* Latency: 0x40 */
813 0x00, /* Header-Type: 0x00 normal */
814 };
815
816 *value = 0;
817 if (where >= 0 && where < sizeof(fake_pci_config) &&
818 (where + size) >= 0 &&
819 (where + size) < sizeof(fake_pci_config) &&
820 size <= sizeof(u32)) {
821 while (size--) {
822 *value <<= 8;
823 *value |= fake_pci_config[where + size];
824 }
825 }
826
827 return PCIBIOS_SUCCESSFUL;
828}
829
830int pci_host_bridge_write_pci_cfg(struct pci_bus *bus_dev,
831 unsigned int devfn,
832 int where, int size,
833 u32 value)
834{
835 return PCIBIOS_SUCCESSFUL;
836}
837
David S. Millera6009dd2007-05-07 00:01:38 -0700838struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
David S. Millera2fb23a2007-02-28 23:35:04 -0800839{
David S. Millera2fb23a2007-02-28 23:35:04 -0800840 struct device_node *node = pbm->prom_node;
David S. Miller97b3cf02007-03-11 16:42:53 -0700841 struct pci_dev *host_pdev;
David S. Millera2fb23a2007-02-28 23:35:04 -0800842 struct pci_bus *bus;
843
844 printk("PCI: Scanning PBM %s\n", node->full_name);
845
846 /* XXX parent device? XXX */
David S. Millerf1cd8de2007-05-07 23:24:05 -0700847 bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
David S. Millera2fb23a2007-02-28 23:35:04 -0800848 if (!bus) {
849 printk(KERN_ERR "Failed to create bus for %s\n",
850 node->full_name);
851 return NULL;
852 }
853 bus->secondary = pbm->pci_first_busno;
854 bus->subordinate = pbm->pci_last_busno;
855
856 bus->resource[0] = &pbm->io_space;
857 bus->resource[1] = &pbm->mem_space;
858
David S. Miller97b3cf02007-03-11 16:42:53 -0700859 /* Create the dummy host bridge and link it in. */
860 host_pdev = of_create_pci_dev(pbm, node, bus, 0x00, 1);
861 bus->self = host_pdev;
862
David S. Millera2fb23a2007-02-28 23:35:04 -0800863 pci_of_scan_bus(pbm, node, bus);
864 pci_bus_add_devices(bus);
865 pci_bus_register_of_sysfs(bus);
866
867 return bus;
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static void __init pci_scan_each_controller_bus(void)
871{
David S. Miller34768bc2007-05-07 23:06:27 -0700872 struct pci_pbm_info *pbm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
David S. Miller34768bc2007-05-07 23:06:27 -0700874 for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
875 pbm->scan_bus(pbm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878extern void power_init(void);
879
880static int __init pcibios_init(void)
881{
882 pci_controller_probe();
David S. Miller34768bc2007-05-07 23:06:27 -0700883 if (pci_pbm_root == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 0;
885
886 pci_scan_each_controller_bus();
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 isa_init();
889 ebus_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 power_init();
891
892 return 0;
893}
894
895subsys_initcall(pcibios_init);
896
Robert Reiff6b45da2007-04-12 13:47:37 -0700897void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct pci_pbm_info *pbm = pbus->sysdata;
900
901 /* Generic PCI bus probing sets these to point at
902 * &io{port,mem}_resouce which is wrong for us.
903 */
904 pbus->resource[0] = &pbm->io_space;
905 pbus->resource[1] = &pbm->mem_space;
906}
907
David S. Miller085ae412005-08-08 13:19:08 -0700908struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct pci_pbm_info *pbm = pdev->bus->sysdata;
David S. Miller085ae412005-08-08 13:19:08 -0700911 struct resource *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
David S. Miller085ae412005-08-08 13:19:08 -0700913 if (r->flags & IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 root = &pbm->io_space;
David S. Miller085ae412005-08-08 13:19:08 -0700915 if (r->flags & IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 root = &pbm->mem_space;
917
David S. Miller085ae412005-08-08 13:19:08 -0700918 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921void pcibios_update_irq(struct pci_dev *pdev, int irq)
922{
923}
924
925void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700926 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928}
929
David S. Millera2fb23a2007-02-28 23:35:04 -0800930int pcibios_enable_device(struct pci_dev *dev, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
David S. Millera2fb23a2007-02-28 23:35:04 -0800932 u16 cmd, oldcmd;
933 int i;
934
935 pci_read_config_word(dev, PCI_COMMAND, &cmd);
936 oldcmd = cmd;
937
938 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
939 struct resource *res = &dev->resource[i];
940
941 /* Only set up the requested stuff */
942 if (!(mask & (1<<i)))
943 continue;
944
945 if (res->flags & IORESOURCE_IO)
946 cmd |= PCI_COMMAND_IO;
947 if (res->flags & IORESOURCE_MEM)
948 cmd |= PCI_COMMAND_MEMORY;
949 }
950
951 if (cmd != oldcmd) {
952 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
953 pci_name(dev), cmd);
954 /* Enable the appropriate bits in the PCI command register. */
955 pci_write_config_word(dev, PCI_COMMAND, cmd);
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return 0;
958}
959
960void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
961 struct resource *res)
962{
963 struct pci_pbm_info *pbm = pdev->bus->sysdata;
964 struct resource zero_res, *root;
965
966 zero_res.start = 0;
967 zero_res.end = 0;
968 zero_res.flags = res->flags;
969
970 if (res->flags & IORESOURCE_IO)
971 root = &pbm->io_space;
972 else
973 root = &pbm->mem_space;
974
David S. Miller0bae5f82007-03-08 22:42:19 -0800975 pci_resource_adjust(&zero_res, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 region->start = res->start - zero_res.start;
978 region->end = res->end - zero_res.start;
979}
David S. Miller5fdfd422006-04-17 13:34:44 -0700980EXPORT_SYMBOL(pcibios_resource_to_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
983 struct pci_bus_region *region)
984{
985 struct pci_pbm_info *pbm = pdev->bus->sysdata;
986 struct resource *root;
987
988 res->start = region->start;
989 res->end = region->end;
990
991 if (res->flags & IORESOURCE_IO)
992 root = &pbm->io_space;
993 else
994 root = &pbm->mem_space;
995
David S. Miller0bae5f82007-03-08 22:42:19 -0800996 pci_resource_adjust(res, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Keith Owens41290c12005-08-24 16:06:25 +1000998EXPORT_SYMBOL(pcibios_bus_to_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Robert Reiff6b45da2007-04-12 13:47:37 -07001000char * __devinit pcibios_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return str;
1003}
1004
1005/* Platform support for /proc/bus/pci/X/Y mmap()s. */
1006
1007/* If the user uses a host-bridge as the PCI device, he may use
1008 * this to perform a raw mmap() of the I/O or MEM space behind
1009 * that controller.
1010 *
1011 * This can be useful for execution of x86 PCI bios initialization code
1012 * on a PCI card, like the xfree86 int10 stuff does.
1013 */
1014static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
1015 enum pci_mmap_state mmap_state)
1016{
David S. Millera2fb23a2007-02-28 23:35:04 -08001017 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 unsigned long space_size, user_offset, user_size;
1019
David S. Miller3875c5c2007-03-08 22:52:11 -08001020 if (mmap_state == pci_mmap_io) {
1021 space_size = (pbm->io_space.end -
1022 pbm->io_space.start) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else {
David S. Miller3875c5c2007-03-08 22:52:11 -08001024 space_size = (pbm->mem_space.end -
1025 pbm->mem_space.start) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
1028 /* Make sure the request is in range. */
1029 user_offset = vma->vm_pgoff << PAGE_SHIFT;
1030 user_size = vma->vm_end - vma->vm_start;
1031
1032 if (user_offset >= space_size ||
1033 (user_offset + user_size) > space_size)
1034 return -EINVAL;
1035
David S. Miller3875c5c2007-03-08 22:52:11 -08001036 if (mmap_state == pci_mmap_io) {
1037 vma->vm_pgoff = (pbm->io_space.start +
1038 user_offset) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 } else {
David S. Miller3875c5c2007-03-08 22:52:11 -08001040 vma->vm_pgoff = (pbm->mem_space.start +
1041 user_offset) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044 return 0;
1045}
1046
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001047/* Adjust vm_pgoff of VMA such that it is the physical page offset
1048 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *
1050 * Basically, the user finds the base address for his device which he wishes
1051 * to mmap. They read the 32-bit value from the config space base register,
1052 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
1053 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
1054 *
1055 * Returns negative error code on failure, zero on success.
1056 */
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001057static int __pci_mmap_make_offset(struct pci_dev *pdev,
1058 struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 enum pci_mmap_state mmap_state)
1060{
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001061 unsigned long user_paddr, user_size;
1062 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001064 /* First compute the physical address in vma->vm_pgoff,
1065 * making sure the user offset is within range in the
1066 * appropriate PCI space.
1067 */
1068 err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
1069 if (err)
1070 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001072 /* If this is a mapping on a host bridge, any address
1073 * is OK.
1074 */
1075 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1076 return err;
1077
1078 /* Otherwise make sure it's in the range for one of the
1079 * device's resources.
1080 */
1081 user_paddr = vma->vm_pgoff << PAGE_SHIFT;
1082 user_size = vma->vm_end - vma->vm_start;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001085 struct resource *rp = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /* Active? */
1088 if (!rp->flags)
1089 continue;
1090
1091 /* Same type? */
1092 if (i == PCI_ROM_RESOURCE) {
1093 if (mmap_state != pci_mmap_mem)
1094 continue;
1095 } else {
1096 if ((mmap_state == pci_mmap_io &&
1097 (rp->flags & IORESOURCE_IO) == 0) ||
1098 (mmap_state == pci_mmap_mem &&
1099 (rp->flags & IORESOURCE_MEM) == 0))
1100 continue;
1101 }
1102
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001103 if ((rp->start <= user_paddr) &&
1104 (user_paddr + user_size) <= (rp->end + 1UL))
1105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001108 if (i > PCI_ROM_RESOURCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -EINVAL;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return 0;
1112}
1113
1114/* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1115 * mapping.
1116 */
1117static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1118 enum pci_mmap_state mmap_state)
1119{
1120 vma->vm_flags |= (VM_IO | VM_RESERVED);
1121}
1122
1123/* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1124 * device mapping.
1125 */
1126static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1127 enum pci_mmap_state mmap_state)
1128{
David S. Millera7a6cac2005-09-01 21:51:26 -07001129 /* Our io_remap_pfn_range takes care of this, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1133 * for this architecture. The region in the process to map is described by vm_start
1134 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1135 * The pci device structure is provided so that architectures may make mapping
1136 * decisions on a per-device or per-bus basis.
1137 *
1138 * Returns a negative error code on failure, zero on success.
1139 */
1140int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1141 enum pci_mmap_state mmap_state,
1142 int write_combine)
1143{
1144 int ret;
1145
1146 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1147 if (ret < 0)
1148 return ret;
1149
1150 __pci_mmap_set_flags(dev, vma, mmap_state);
1151 __pci_mmap_set_pgprot(dev, vma, mmap_state);
1152
David S. Miller14778d92006-03-21 02:29:39 -08001153 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ret = io_remap_pfn_range(vma, vma->vm_start,
1155 vma->vm_pgoff,
1156 vma->vm_end - vma->vm_start,
1157 vma->vm_page_prot);
1158 if (ret)
1159 return ret;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162}
1163
David S. Millerc1b1a5f2008-03-19 04:52:48 -07001164#ifdef CONFIG_NUMA
1165int pcibus_to_node(struct pci_bus *pbus)
1166{
1167 struct pci_pbm_info *pbm = pbus->sysdata;
1168
1169 return pbm->numa_node;
1170}
1171EXPORT_SYMBOL(pcibus_to_node);
1172#endif
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174/* Return the domain nuber for this pci bus */
1175
1176int pci_domain_nr(struct pci_bus *pbus)
1177{
1178 struct pci_pbm_info *pbm = pbus->sysdata;
1179 int ret;
1180
1181 if (pbm == NULL || pbm->parent == NULL) {
1182 ret = -ENXIO;
1183 } else {
David S. Miller6c108f12007-05-07 23:49:01 -07001184 ret = pbm->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
1187 return ret;
1188}
1189EXPORT_SYMBOL(pci_domain_nr);
1190
David S. Miller35a17eb2007-02-10 17:41:02 -08001191#ifdef CONFIG_PCI_MSI
1192int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1193{
David S. Millera2fb23a2007-02-28 23:35:04 -08001194 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Millere9870c42007-05-07 23:28:50 -07001195 int virt_irq;
David S. Miller35a17eb2007-02-10 17:41:02 -08001196
David S. Millere9870c42007-05-07 23:28:50 -07001197 if (!pbm->setup_msi_irq)
David S. Miller35a17eb2007-02-10 17:41:02 -08001198 return -EINVAL;
1199
David S. Millere9870c42007-05-07 23:28:50 -07001200 return pbm->setup_msi_irq(&virt_irq, pdev, desc);
David S. Miller35a17eb2007-02-10 17:41:02 -08001201}
1202
1203void arch_teardown_msi_irq(unsigned int virt_irq)
1204{
David S. Millerabfd3362007-02-26 09:40:34 -08001205 struct msi_desc *entry = get_irq_msi(virt_irq);
David S. Miller35a17eb2007-02-10 17:41:02 -08001206 struct pci_dev *pdev = entry->dev;
David S. Millera2fb23a2007-02-28 23:35:04 -08001207 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Miller35a17eb2007-02-10 17:41:02 -08001208
David S. Millere9870c42007-05-07 23:28:50 -07001209 if (!pbm->teardown_msi_irq)
David S. Miller35a17eb2007-02-10 17:41:02 -08001210 return;
1211
David S. Millere9870c42007-05-07 23:28:50 -07001212 return pbm->teardown_msi_irq(virt_irq, pdev);
David S. Miller35a17eb2007-02-10 17:41:02 -08001213}
1214#endif /* !(CONFIG_PCI_MSI) */
1215
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001216struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1217{
David S. Millera2fb23a2007-02-28 23:35:04 -08001218 return pdev->dev.archdata.prom_node;
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001219}
1220EXPORT_SYMBOL(pci_device_to_OF_node);
1221
David S. Millerad7ad572007-07-27 22:39:14 -07001222static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
1223{
1224 struct pci_dev *ali_isa_bridge;
1225 u8 val;
1226
1227 /* ALI sound chips generate 31-bits of DMA, a special register
1228 * determines what bit 31 is emitted as.
1229 */
1230 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
1231 PCI_DEVICE_ID_AL_M1533,
1232 NULL);
1233
1234 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
1235 if (set_bit)
1236 val |= 0x01;
1237 else
1238 val &= ~0x01;
1239 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
1240 pci_dev_put(ali_isa_bridge);
1241}
1242
1243int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
1244{
1245 u64 dma_addr_mask;
1246
1247 if (pdev == NULL) {
1248 dma_addr_mask = 0xffffffff;
1249 } else {
1250 struct iommu *iommu = pdev->dev.archdata.iommu;
1251
1252 dma_addr_mask = iommu->dma_addr_mask;
1253
1254 if (pdev->vendor == PCI_VENDOR_ID_AL &&
1255 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
1256 device_mask == 0x7fffffff) {
1257 ali_sound_dma_hack(pdev,
1258 (dma_addr_mask & 0x80000000) != 0);
1259 return 1;
1260 }
1261 }
1262
1263 if (device_mask >= (1UL << 32UL))
1264 return 0;
1265
1266 return (device_mask & dma_addr_mask) == dma_addr_mask;
1267}
1268
David S. Millerbcea1db2007-12-25 02:20:33 -08001269void pci_resource_to_user(const struct pci_dev *pdev, int bar,
1270 const struct resource *rp, resource_size_t *start,
1271 resource_size_t *end)
1272{
1273 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1274 unsigned long offset;
1275
1276 if (rp->flags & IORESOURCE_IO)
1277 offset = pbm->io_space.start;
1278 else
1279 offset = pbm->mem_space.start;
1280
1281 *start = rp->start - offset;
1282 *end = rp->end - offset;
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285#endif /* !(CONFIG_PCI) */