1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/config.h>
14
15#include <linux/acpi.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/pci.h>
19#include <linux/init.h>
20#include <linux/ioport.h>
21#include <linux/slab.h>
22#include <linux/smp_lock.h>
23#include <linux/spinlock.h>
24
25#include <asm/machvec.h>
26#include <asm/page.h>
27#include <asm/segment.h>
28#include <asm/system.h>
29#include <asm/io.h>
30
31#include <asm/sal.h>
32
33
34#ifdef CONFIG_SMP
35# include <asm/smp.h>
36#endif
37#include <asm/irq.h>
38#include <asm/hw_irq.h>
39
40
41#undef DEBUG
42#define DEBUG
43
44#ifdef DEBUG
45#define DBG(x...) printk(x)
46#else
47#define DBG(x...)
48#endif
49
50static int pci_routeirq;
51
52
53
54
55
56
57
58#define PCI_SAL_ADDRESS(seg, bus, devfn, reg) \
59 ((u64)(seg << 24) | (u64)(bus << 16) | \
60 (u64)(devfn << 8) | (u64)(reg))
61
62
63
64#define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg) \
65 ((u64)(seg << 28) | (u64)(bus << 20) | \
66 (u64)(devfn << 12) | (u64)(reg))
67
68static int
69pci_sal_read (int seg, int bus, int devfn, int reg, int len, u32 *value)
70{
71 u64 addr, mode, data = 0;
72 int result = 0;
73
74 if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
75 return -EINVAL;
76
77 if ((seg | reg) <= 255) {
78 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
79 mode = 0;
80 } else {
81 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
82 mode = 1;
83 }
84 result = ia64_sal_pci_config_read(addr, mode, len, &data);
85
86 *value = (u32) data;
87
88 return result;
89}
90
91static int
92pci_sal_write (int seg, int bus, int devfn, int reg, int len, u32 value)
93{
94 u64 addr, mode;
95
96 if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
97 return -EINVAL;
98
99 if ((seg | reg) <= 255) {
100 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
101 mode = 0;
102 } else {
103 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
104 mode = 1;
105 }
106 return ia64_sal_pci_config_write(addr, mode, len, value);
107}
108
109static struct pci_raw_ops pci_sal_ops = {
110 .read = pci_sal_read,
111 .write = pci_sal_write
112};
113
114struct pci_raw_ops *raw_pci_ops = &pci_sal_ops;
115
116static int
117pci_read (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
118{
119 return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
120 devfn, where, size, value);
121}
122
123static int
124pci_write (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
125{
126 return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
127 devfn, where, size, value);
128}
129
130struct pci_ops pci_root_ops = {
131 .read = pci_read,
132 .write = pci_write,
133};
134
135#ifdef CONFIG_NUMA
136extern acpi_status acpi_map_iosapic(acpi_handle, u32, void *, void **);
137static void acpi_map_iosapics(void)
138{
139 acpi_get_devices(NULL, acpi_map_iosapic, NULL, NULL);
140}
141#else
142static void acpi_map_iosapics(void)
143{
144 return;
145}
146#endif
147
148static int __init
149pci_acpi_init (void)
150{
151 struct pci_dev *dev = NULL;
152
153 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
154
155 acpi_map_iosapics();
156
157 if (pci_routeirq) {
158
159
160
161
162
163 printk(KERN_INFO "** Routing PCI interrupts for all devices because \"pci=routeirq\"\n");
164 printk(KERN_INFO "** was specified. If this was required to make a driver work,\n");
165 printk(KERN_INFO "** please email the output of \"lspci\" to bjorn.helgaas@hp.com\n");
166 printk(KERN_INFO "** so I can fix the driver.\n");
167 for_each_pci_dev(dev)
168 acpi_pci_irq_enable(dev);
169 } else {
170 printk(KERN_INFO "** PCI interrupts are no longer routed automatically. If this\n");
171 printk(KERN_INFO "** causes a device to stop working, it is probably because the\n");
172 printk(KERN_INFO "** driver failed to call pci_enable_device(). As a temporary\n");
173 printk(KERN_INFO "** workaround, the \"pci=routeirq\" argument restores the old\n");
174 printk(KERN_INFO "** behavior. If this argument makes the device work again,\n");
175 printk(KERN_INFO "** please email the output of \"lspci\" to bjorn.helgaas@hp.com\n");
176 printk(KERN_INFO "** so I can fix the driver.\n");
177 }
178
179 return 0;
180}
181
182subsys_initcall(pci_acpi_init);
183
184
185
186static struct pci_controller * __devinit
187alloc_pci_controller (int seg)
188{
189 struct pci_controller *controller;
190
191 controller = kmalloc(sizeof(*controller), GFP_KERNEL);
192 if (!controller)
193 return NULL;
194
195 memset(controller, 0, sizeof(*controller));
196 controller->segment = seg;
197 return controller;
198}
199
200static int __devinit
201alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end,
202 unsigned long flags)
203{
204 struct resource *res;
205
206 res = kmalloc(sizeof(*res), GFP_KERNEL);
207 if (!res)
208 return -ENOMEM;
209
210 memset(res, 0, sizeof(*res));
211 res->name = name;
212 res->start = start;
213 res->end = end;
214 res->flags = flags;
215
216 if (insert_resource(root, res)) {
217 kfree(res);
218 return -EBUSY;
219 }
220
221 return 0;
222}
223
224static u64 __devinit
225add_io_space (struct acpi_resource_address64 *addr)
226{
227 u64 offset;
228 int sparse = 0;
229 int i;
230
231 if (addr->address_translation_offset == 0)
232 return IO_SPACE_BASE(0);
233
234 if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
235 sparse = 1;
236
237 offset = (u64) ioremap(addr->address_translation_offset, 0);
238 for (i = 0; i < num_io_spaces; i++)
239 if (io_space[i].mmio_base == offset &&
240 io_space[i].sparse == sparse)
241 return IO_SPACE_BASE(i);
242
243 if (num_io_spaces == MAX_IO_SPACES) {
244 printk("Too many IO port spaces\n");
245 return ~0;
246 }
247
248 i = num_io_spaces++;
249 io_space[i].mmio_base = offset;
250 io_space[i].sparse = sparse;
251
252 return IO_SPACE_BASE(i);
253}
254
255static acpi_status __devinit
256count_window (struct acpi_resource *resource, void *data)
257{
258 unsigned int *windows = (unsigned int *) data;
259 struct acpi_resource_address64 addr;
260 acpi_status status;
261
262 status = acpi_resource_to_address64(resource, &addr);
263 if (ACPI_SUCCESS(status))
264 if (addr.resource_type == ACPI_MEMORY_RANGE ||
265 addr.resource_type == ACPI_IO_RANGE)
266 (*windows)++;
267
268 return AE_OK;
269}
270
271struct pci_root_info {
272 struct pci_controller *controller;
273 char *name;
274};
275
276static acpi_status __devinit
277add_window (struct acpi_resource *res, void *data)
278{
279 struct pci_root_info *info = (struct pci_root_info *) data;
280 struct pci_window *window;
281 struct acpi_resource_address64 addr;
282 acpi_status status;
283 unsigned long flags, offset = 0;
284 struct resource *root;
285
286 status = acpi_resource_to_address64(res, &addr);
287 if (ACPI_SUCCESS(status)) {
288 if (!addr.address_length)
289 return AE_OK;
290
291 if (addr.resource_type == ACPI_MEMORY_RANGE) {
292 flags = IORESOURCE_MEM;
293 root = &iomem_resource;
294 offset = addr.address_translation_offset;
295 } else if (addr.resource_type == ACPI_IO_RANGE) {
296 flags = IORESOURCE_IO;
297 root = &ioport_resource;
298 offset = add_io_space(&addr);
299 if (offset == ~0)
300 return AE_OK;
301 } else
302 return AE_OK;
303
304 window = &info->controller->window[info->controller->windows++];
305 window->resource.flags = flags;
306 window->resource.start = addr.min_address_range;
307 window->resource.end = addr.max_address_range;
308 window->offset = offset;
309
310 if (alloc_resource(info->name, root, addr.min_address_range + offset,
311 addr.max_address_range + offset, flags))
312 printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
313 addr.min_address_range + offset, addr.max_address_range + offset,
314 root->name, info->name);
315 }
316
317 return AE_OK;
318}
319
320struct pci_bus * __devinit
321pci_acpi_scan_root (struct acpi_device *device, int domain, int bus)
322{
323 struct pci_root_info info;
324 struct pci_controller *controller;
325 unsigned int windows = 0;
326 char *name;
327
328 controller = alloc_pci_controller(domain);
329 if (!controller)
330 goto out1;
331
332 controller->acpi_handle = device->handle;
333
334 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window, &windows);
335 controller->window = kmalloc(sizeof(*controller->window) * windows, GFP_KERNEL);
336 if (!controller->window)
337 goto out2;
338
339 name = kmalloc(16, GFP_KERNEL);
340 if (!name)
341 goto out3;
342
343 sprintf(name, "PCI Bus %04x:%02x", domain, bus);
344 info.controller = controller;
345 info.name = name;
346 acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window, &info);
347
348 return pci_scan_bus(bus, &pci_root_ops, controller);
349
350out3:
351 kfree(controller->window);
352out2:
353 kfree(controller);
354out1:
355 return NULL;
356}
357
358void pcibios_resource_to_bus(struct pci_dev *dev,
359 struct pci_bus_region *region, struct resource *res)
360{
361 struct pci_controller *controller = PCI_CONTROLLER(dev);
362 unsigned long offset = 0;
363 int i;
364
365 for (i = 0; i < controller->windows; i++) {
366 struct pci_window *window = &controller->window[i];
367 if (!(window->resource.flags & res->flags))
368 continue;
369 if (window->resource.start > res->start - window->offset)
370 continue;
371 if (window->resource.end < res->end - window->offset)
372 continue;
373 offset = window->offset;
374 break;
375 }
376
377 region->start = res->start - offset;
378 region->end = res->end - offset;
379}
380EXPORT_SYMBOL(pcibios_resource_to_bus);
381
382void pcibios_bus_to_resource(struct pci_dev *dev,
383 struct resource *res, struct pci_bus_region *region)
384{
385 struct pci_controller *controller = PCI_CONTROLLER(dev);
386 unsigned long offset = 0;
387 int i;
388
389 for (i = 0; i < controller->windows; i++) {
390 struct pci_window *window = &controller->window[i];
391 if (!(window->resource.flags & res->flags))
392 continue;
393 if (window->resource.start > region->start)
394 continue;
395 if (window->resource.end < region->end)
396 continue;
397 offset = window->offset;
398 break;
399 }
400
401 res->start = region->start + offset;
402 res->end = region->end + offset;
403}
404
405static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
406{
407 struct pci_bus_region region;
408 int i;
409 int limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ? \
410 PCI_BRIDGE_RESOURCES : PCI_NUM_RESOURCES;
411
412 for (i = 0; i < limit; i++) {
413 if (!dev->resource[i].flags)
414 continue;
415 region.start = dev->resource[i].start;
416 region.end = dev->resource[i].end;
417 pcibios_bus_to_resource(dev, &dev->resource[i], ®ion);
418 pci_claim_resource(dev, i);
419 }
420}
421
422
423
424
425void __devinit
426pcibios_fixup_bus (struct pci_bus *b)
427{
428 struct pci_dev *dev;
429
430 list_for_each_entry(dev, &b->devices, bus_list)
431 pcibios_fixup_device_resources(dev);
432
433 return;
434}
435
436void __devinit
437pcibios_update_irq (struct pci_dev *dev, int irq)
438{
439 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
440
441
442}
443
444static inline int
445pcibios_enable_resources (struct pci_dev *dev, int mask)
446{
447 u16 cmd, old_cmd;
448 int idx;
449 struct resource *r;
450
451 if (!dev)
452 return -EINVAL;
453
454 pci_read_config_word(dev, PCI_COMMAND, &cmd);
455 old_cmd = cmd;
456 for (idx=0; idx<6; idx++) {
457
458 if (!(mask & (1 << idx)))
459 continue;
460
461 r = &dev->resource[idx];
462 if (!r->start && r->end) {
463 printk(KERN_ERR
464 "PCI: Device %s not available because of resource collisions\n",
465 pci_name(dev));
466 return -EINVAL;
467 }
468 if (r->flags & IORESOURCE_IO)
469 cmd |= PCI_COMMAND_IO;
470 if (r->flags & IORESOURCE_MEM)
471 cmd |= PCI_COMMAND_MEMORY;
472 }
473 if (dev->resource[PCI_ROM_RESOURCE].start)
474 cmd |= PCI_COMMAND_MEMORY;
475 if (cmd != old_cmd) {
476 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
477 pci_write_config_word(dev, PCI_COMMAND, cmd);
478 }
479 return 0;
480}
481
482int
483pcibios_enable_device (struct pci_dev *dev, int mask)
484{
485 int ret;
486
487 ret = pcibios_enable_resources(dev, mask);
488 if (ret < 0)
489 return ret;
490
491 return acpi_pci_irq_enable(dev);
492}
493
494#ifdef CONFIG_ACPI_DEALLOCATE_IRQ
495void
496pcibios_disable_device (struct pci_dev *dev)
497{
498 acpi_pci_irq_disable(dev);
499}
500#endif
501
502void
503pcibios_align_resource (void *data, struct resource *res,
504 unsigned long size, unsigned long align)
505{
506}
507
508
509
510
511char * __init
512pcibios_setup (char *str)
513{
514 if (!strcmp(str, "routeirq"))
515 pci_routeirq = 1;
516 return NULL;
517}
518
519int
520pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
521 enum pci_mmap_state mmap_state, int write_combine)
522{
523
524
525
526
527 if (mmap_state == pci_mmap_io)
528
529
530
531
532
533
534 return -EINVAL;
535
536
537
538
539
540 vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
541
542 if (write_combine && efi_range_is_wc(vma->vm_start,
543 vma->vm_end - vma->vm_start))
544 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
545 else
546 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
547
548 if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
549 vma->vm_end - vma->vm_start, vma->vm_page_prot))
550 return -EAGAIN;
551
552 return 0;
553}
554
555
556
557
558
559
560
561
562
563
564
565
566
567char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
568{
569 return (char *)__IA64_UNCACHED_OFFSET;
570}
571
572
573
574
575
576
577
578
579
580int
581pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma)
582{
583 char *addr;
584
585 addr = pci_get_legacy_mem(bus);
586 if (IS_ERR(addr))
587 return PTR_ERR(addr);
588
589 vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
590 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
591 vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
592
593 if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
594 vma->vm_end - vma->vm_start, vma->vm_page_prot))
595 return -EAGAIN;
596
597 return 0;
598}
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
614{
615 int ret = size;
616
617 switch (size) {
618 case 1:
619 *val = inb(port);
620 break;
621 case 2:
622 *val = inw(port);
623 break;
624 case 4:
625 *val = inl(port);
626 break;
627 default:
628 ret = -EINVAL;
629 break;
630 }
631
632 return ret;
633}
634
635
636
637
638
639
640
641
642
643
644int ia64_pci_legacy_write(struct pci_dev *bus, u16 port, u32 val, u8 size)
645{
646 int ret = 0;
647
648 switch (size) {
649 case 1:
650 outb(val, port);
651 break;
652 case 2:
653 outw(val, port);
654 break;
655 case 4:
656 outl(val, port);
657 break;
658 default:
659 ret = -EINVAL;
660 break;
661 }
662
663 return ret;
664}
665
666
667
668
669
670
671
672
673
674
675
676
677static unsigned long
678pci_cacheline_size (void)
679{
680 u64 levels, unique_caches;
681 s64 status;
682 pal_cache_config_info_t cci;
683 static u8 cacheline_size;
684
685 if (cacheline_size)
686 return cacheline_size;
687
688 status = ia64_pal_cache_summary(&levels, &unique_caches);
689 if (status != 0) {
690 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
691 __FUNCTION__, status);
692 return SMP_CACHE_BYTES;
693 }
694
695 status = ia64_pal_cache_config_info(levels - 1, 2,
696 &cci);
697 if (status != 0) {
698 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
699 __FUNCTION__, status);
700 return SMP_CACHE_BYTES;
701 }
702 cacheline_size = 1 << cci.pcci_line_size;
703 return cacheline_size;
704}
705
706
707
708
709
710
711
712
713
714int
715pcibios_prep_mwi (struct pci_dev *dev)
716{
717 unsigned long desired_linesize, current_linesize;
718 int rc = 0;
719 u8 pci_linesize;
720
721 desired_linesize = pci_cacheline_size();
722
723 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
724 current_linesize = 4 * pci_linesize;
725 if (desired_linesize != current_linesize) {
726 printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
727 pci_name(dev), current_linesize);
728 if (current_linesize > desired_linesize) {
729 printk(" expected %lu bytes instead\n", desired_linesize);
730 rc = -EINVAL;
731 } else {
732 printk(" correcting to %lu\n", desired_linesize);
733 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
734 }
735 }
736 return rc;
737}
738
739int pci_vector_resources(int last, int nr_released)
740{
741 int count = nr_released;
742
743 count += (IA64_LAST_DEVICE_VECTOR - last);
744
745 return count;
746}
747