b1846b891ea556e143d9fae15f8a527cc8120c9c
[sfrench/cifs-2.6.git] / arch / ia64 / pci / pci.c
1 /*
2  * pci.c - Low-Level PCI Access in IA-64
3  *
4  * Derived from bios32.c of i386 tree.
5  *
6  * (c) Copyright 2002, 2005 Hewlett-Packard Development Company, L.P.
7  *      David Mosberger-Tang <davidm@hpl.hp.com>
8  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
9  * Copyright (C) 2004 Silicon Graphics, Inc.
10  *
11  * Note: Above list of copyright holders is incomplete...
12  */
13
14 #include <linux/acpi.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/pci.h>
18 #include <linux/pci-acpi.h>
19 #include <linux/init.h>
20 #include <linux/ioport.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/bootmem.h>
24 #include <linux/export.h>
25
26 #include <asm/machvec.h>
27 #include <asm/page.h>
28 #include <asm/io.h>
29 #include <asm/sal.h>
30 #include <asm/smp.h>
31 #include <asm/irq.h>
32 #include <asm/hw_irq.h>
33
34 /*
35  * Low-level SAL-based PCI configuration access functions. Note that SAL
36  * calls are already serialized (via sal_lock), so we don't need another
37  * synchronization mechanism here.
38  */
39
40 #define PCI_SAL_ADDRESS(seg, bus, devfn, reg)           \
41         (((u64) seg << 24) | (bus << 16) | (devfn << 8) | (reg))
42
43 /* SAL 3.2 adds support for extended config space. */
44
45 #define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg)       \
46         (((u64) seg << 28) | (bus << 20) | (devfn << 12) | (reg))
47
48 int raw_pci_read(unsigned int seg, unsigned int bus, unsigned int devfn,
49               int reg, int len, u32 *value)
50 {
51         u64 addr, data = 0;
52         int mode, result;
53
54         if (!value || (seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
55                 return -EINVAL;
56
57         if ((seg | reg) <= 255) {
58                 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
59                 mode = 0;
60         } else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
61                 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
62                 mode = 1;
63         } else {
64                 return -EINVAL;
65         }
66
67         result = ia64_sal_pci_config_read(addr, mode, len, &data);
68         if (result != 0)
69                 return -EINVAL;
70
71         *value = (u32) data;
72         return 0;
73 }
74
75 int raw_pci_write(unsigned int seg, unsigned int bus, unsigned int devfn,
76                int reg, int len, u32 value)
77 {
78         u64 addr;
79         int mode, result;
80
81         if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
82                 return -EINVAL;
83
84         if ((seg | reg) <= 255) {
85                 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
86                 mode = 0;
87         } else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
88                 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
89                 mode = 1;
90         } else {
91                 return -EINVAL;
92         }
93         result = ia64_sal_pci_config_write(addr, mode, len, value);
94         if (result != 0)
95                 return -EINVAL;
96         return 0;
97 }
98
99 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
100                                                         int size, u32 *value)
101 {
102         return raw_pci_read(pci_domain_nr(bus), bus->number,
103                                  devfn, where, size, value);
104 }
105
106 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
107                                                         int size, u32 value)
108 {
109         return raw_pci_write(pci_domain_nr(bus), bus->number,
110                                   devfn, where, size, value);
111 }
112
113 struct pci_ops pci_root_ops = {
114         .read = pci_read,
115         .write = pci_write,
116 };
117
118 struct pci_root_info {
119         struct pci_controller controller;
120         struct acpi_device *bridge;
121         struct list_head resources;
122         struct list_head io_resources;
123         char name[16];
124 };
125
126 static unsigned int
127 new_space (u64 phys_base, int sparse)
128 {
129         u64 mmio_base;
130         int i;
131
132         if (phys_base == 0)
133                 return 0;       /* legacy I/O port space */
134
135         mmio_base = (u64) ioremap(phys_base, 0);
136         for (i = 0; i < num_io_spaces; i++)
137                 if (io_space[i].mmio_base == mmio_base &&
138                     io_space[i].sparse == sparse)
139                         return i;
140
141         if (num_io_spaces == MAX_IO_SPACES) {
142                 pr_err("PCI: Too many IO port spaces "
143                         "(MAX_IO_SPACES=%lu)\n", MAX_IO_SPACES);
144                 return ~0;
145         }
146
147         i = num_io_spaces++;
148         io_space[i].mmio_base = mmio_base;
149         io_space[i].sparse = sparse;
150
151         return i;
152 }
153
154 static int add_io_space(struct device *dev, struct pci_root_info *info,
155                         struct resource_entry *entry)
156 {
157         struct resource_entry *iospace;
158         struct resource *resource, *res = entry->res;
159         char *name;
160         unsigned long base, min, max, base_port;
161         unsigned int sparse = 0, space_nr, len;
162
163         len = strlen(info->name) + 32;
164         iospace = resource_list_create_entry(NULL, len);
165         if (!iospace) {
166                 dev_err(dev, "PCI: No memory for %s I/O port space\n",
167                         info->name);
168                 return -ENOMEM;
169         }
170
171         if (res->flags & IORESOURCE_IO_SPARSE)
172                 sparse = 1;
173         space_nr = new_space(entry->offset, sparse);
174         if (space_nr == ~0)
175                 goto free_resource;
176
177         name = (char *)(iospace + 1);
178         min = res->start - entry->offset;
179         max = res->end - entry->offset;
180         base = __pa(io_space[space_nr].mmio_base);
181         base_port = IO_SPACE_BASE(space_nr);
182         snprintf(name, len, "%s I/O Ports %08lx-%08lx", info->name,
183                  base_port + min, base_port + max);
184
185         /*
186          * The SDM guarantees the legacy 0-64K space is sparse, but if the
187          * mapping is done by the processor (not the bridge), ACPI may not
188          * mark it as sparse.
189          */
190         if (space_nr == 0)
191                 sparse = 1;
192
193         resource = iospace->res;
194         resource->name  = name;
195         resource->flags = IORESOURCE_MEM;
196         resource->start = base + (sparse ? IO_SPACE_SPARSE_ENCODING(min) : min);
197         resource->end   = base + (sparse ? IO_SPACE_SPARSE_ENCODING(max) : max);
198         if (insert_resource(&iomem_resource, resource)) {
199                 dev_err(dev,
200                         "can't allocate host bridge io space resource  %pR\n",
201                         resource);
202                 goto free_resource;
203         }
204
205         entry->offset = base_port;
206         res->start = min + base_port;
207         res->end = max + base_port;
208         resource_list_add_tail(iospace, &info->io_resources);
209
210         return 0;
211
212 free_resource:
213         resource_list_free_entry(iospace);
214         return -ENOSPC;
215 }
216
217 /*
218  * An IO port or MMIO resource assigned to a PCI host bridge may be
219  * consumed by the host bridge itself or available to its child
220  * bus/devices. The ACPI specification defines a bit (Producer/Consumer)
221  * to tell whether the resource is consumed by the host bridge itself,
222  * but firmware hasn't used that bit consistently, so we can't rely on it.
223  *
224  * On x86 and IA64 platforms, all IO port and MMIO resources are assumed
225  * to be available to child bus/devices except one special case:
226  *     IO port [0xCF8-0xCFF] is consumed by the host bridge itself
227  *     to access PCI configuration space.
228  *
229  * So explicitly filter out PCI CFG IO ports[0xCF8-0xCFF].
230  */
231 static bool resource_is_pcicfg_ioport(struct resource *res)
232 {
233         return (res->flags & IORESOURCE_IO) &&
234                 res->start == 0xCF8 && res->end == 0xCFF;
235 }
236
237 static int
238 probe_pci_root_info(struct pci_root_info *info, struct acpi_device *device,
239                     int busnum, int domain)
240 {
241         int ret;
242         struct list_head *list = &info->resources;
243         struct resource_entry *entry, *tmp;
244
245         ret = acpi_dev_get_resources(device, list,
246                                      acpi_dev_filter_resource_type_cb,
247                                      (void *)(IORESOURCE_IO | IORESOURCE_MEM));
248         if (ret < 0)
249                 dev_warn(&device->dev,
250                          "failed to parse _CRS method, error code %d\n", ret);
251         else if (ret == 0)
252                 dev_dbg(&device->dev,
253                         "no IO and memory resources present in _CRS\n");
254         else
255                 resource_list_for_each_entry_safe(entry, tmp, list) {
256                         if ((entry->res->flags & IORESOURCE_DISABLED) ||
257                             resource_is_pcicfg_ioport(entry->res))
258                                 resource_list_destroy_entry(entry);
259                         else
260                                 entry->res->name = info->name;
261                 }
262
263         return ret;
264 }
265
266 static void validate_resources(struct device *dev, struct list_head *resources,
267                                unsigned long type)
268 {
269         LIST_HEAD(list);
270         struct resource *res1, *res2, *root = NULL;
271         struct resource_entry *tmp, *entry, *entry2;
272
273         BUG_ON((type & (IORESOURCE_MEM | IORESOURCE_IO)) == 0);
274         root = (type & IORESOURCE_MEM) ? &iomem_resource : &ioport_resource;
275
276         list_splice_init(resources, &list);
277         resource_list_for_each_entry_safe(entry, tmp, &list) {
278                 bool free = false;
279                 resource_size_t end;
280
281                 res1 = entry->res;
282                 if (!(res1->flags & type))
283                         goto next;
284
285                 /* Exclude non-addressable range or non-addressable portion */
286                 end = min(res1->end, root->end);
287                 if (end <= res1->start) {
288                         dev_info(dev, "host bridge window %pR (ignored, not CPU addressable)\n",
289                                  res1);
290                         free = true;
291                         goto next;
292                 } else if (res1->end != end) {
293                         dev_info(dev, "host bridge window %pR ([%#llx-%#llx] ignored, not CPU addressable)\n",
294                                  res1, (unsigned long long)end + 1,
295                                  (unsigned long long)res1->end);
296                         res1->end = end;
297                 }
298
299                 resource_list_for_each_entry(entry2, resources) {
300                         res2 = entry2->res;
301                         if (!(res2->flags & type))
302                                 continue;
303
304                         /*
305                          * I don't like throwing away windows because then
306                          * our resources no longer match the ACPI _CRS, but
307                          * the kernel resource tree doesn't allow overlaps.
308                          */
309                         if (resource_overlaps(res1, res2)) {
310                                 res2->start = min(res1->start, res2->start);
311                                 res2->end = max(res1->end, res2->end);
312                                 dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
313                                          res2, res1);
314                                 free = true;
315                                 goto next;
316                         }
317                 }
318
319 next:
320                 resource_list_del(entry);
321                 if (free)
322                         resource_list_free_entry(entry);
323                 else
324                         resource_list_add_tail(entry, resources);
325         }
326 }
327
328 static void add_resources(struct pci_root_info *info, struct device *dev)
329 {
330         struct resource_entry *entry, *tmp;
331         struct resource *res, *conflict, *root = NULL;
332         struct list_head *list = &info->resources;
333
334         validate_resources(dev, list, IORESOURCE_MEM);
335         validate_resources(dev, list, IORESOURCE_IO);
336
337         resource_list_for_each_entry_safe(entry, tmp, list) {
338                 res = entry->res;
339                 if (res->flags & IORESOURCE_MEM) {
340                         root = &iomem_resource;
341                         /*
342                          * HP's firmware has a hack to work around a Windows
343                          * bug. Ignore these tiny memory ranges.
344                          */
345                         if (resource_size(res) <= 16) {
346                                 resource_list_destroy_entry(entry);
347                                 continue;
348                         }
349                 } else if (res->flags & IORESOURCE_IO) {
350                         root = &ioport_resource;
351                         if (add_io_space(&info->bridge->dev, info, entry)) {
352                                 resource_list_destroy_entry(entry);
353                                 continue;
354                         }
355                 } else {
356                         BUG_ON(res);
357                 }
358
359                 conflict = insert_resource_conflict(root, res);
360                 if (conflict) {
361                         dev_info(dev,
362                                  "ignoring host bridge window %pR (conflicts with %s %pR)\n",
363                                  res, conflict->name, conflict);
364                         resource_list_destroy_entry(entry);
365                 }
366         }
367 }
368
369 static void __release_pci_root_info(struct pci_root_info *info)
370 {
371         struct resource *res;
372         struct resource_entry *entry, *tentry;
373
374         resource_list_for_each_entry_safe(entry, tentry, &info->io_resources) {
375                 release_resource(entry->res);
376                 resource_list_destroy_entry(entry);
377         }
378
379         resource_list_for_each_entry_safe(entry, tentry, &info->resources) {
380                 res = entry->res;
381                 if (res->parent &&
382                     (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
383                         release_resource(res);
384                 resource_list_destroy_entry(entry);
385         }
386
387         kfree(info);
388 }
389
390 static void release_pci_root_info(struct pci_host_bridge *bridge)
391 {
392         struct pci_root_info *info = bridge->release_data;
393
394         __release_pci_root_info(info);
395 }
396
397 struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
398 {
399         struct acpi_device *device = root->device;
400         int domain = root->segment;
401         int bus = root->secondary.start;
402         struct pci_root_info *info;
403         struct pci_bus *pbus;
404         int ret;
405
406         info = kzalloc(sizeof(*info), GFP_KERNEL);
407         if (!info) {
408                 dev_err(&device->dev,
409                         "pci_bus %04x:%02x: ignored (out of memory)\n",
410                         domain, bus);
411                 return NULL;
412         }
413
414         info->controller.segment = domain;
415         info->controller.companion = device;
416         info->controller.node = acpi_get_node(device->handle);
417         info->bridge = device;
418         INIT_LIST_HEAD(&info->resources);
419         INIT_LIST_HEAD(&info->io_resources);
420         snprintf(info->name, sizeof(info->name),
421                  "PCI Bus %04x:%02x", domain, bus);
422
423         ret = probe_pci_root_info(info, device, bus, domain);
424         if (ret <= 0) {
425                 kfree(info);
426                 return NULL;
427         }
428         add_resources(info, &info->bridge->dev);
429         pci_add_resource(&info->resources, &root->secondary);
430
431         /*
432          * See arch/x86/pci/acpi.c.
433          * The desired pci bus might already be scanned in a quirk. We
434          * should handle the case here, but it appears that IA64 hasn't
435          * such quirk. So we just ignore the case now.
436          */
437         pbus = pci_create_root_bus(NULL, bus, &pci_root_ops,
438                                    &info->controller, &info->resources);
439         if (!pbus) {
440                 __release_pci_root_info(info);
441                 return NULL;
442         }
443
444         pci_set_host_bridge_release(to_pci_host_bridge(pbus->bridge),
445                         release_pci_root_info, info);
446         pci_scan_child_bus(pbus);
447         return pbus;
448 }
449
450 int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
451 {
452         /*
453          * We pass NULL as parent to pci_create_root_bus(), so if it is not NULL
454          * here, pci_create_root_bus() has been called by someone else and
455          * sysdata is likely to be different from what we expect.  Let it go in
456          * that case.
457          */
458         if (!bridge->dev.parent) {
459                 struct pci_controller *controller = bridge->bus->sysdata;
460                 ACPI_COMPANION_SET(&bridge->dev, controller->companion);
461         }
462         return 0;
463 }
464
465 void pcibios_fixup_device_resources(struct pci_dev *dev)
466 {
467         int idx;
468
469         if (!dev->bus)
470                 return;
471
472         for (idx = 0; idx < PCI_BRIDGE_RESOURCES; idx++) {
473                 struct resource *r = &dev->resource[idx];
474
475                 if (!r->flags || r->parent || !r->start)
476                         continue;
477
478                 pci_claim_resource(dev, idx);
479         }
480 }
481 EXPORT_SYMBOL_GPL(pcibios_fixup_device_resources);
482
483 static void pcibios_fixup_bridge_resources(struct pci_dev *dev)
484 {
485         int idx;
486
487         if (!dev->bus)
488                 return;
489
490         for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
491                 struct resource *r = &dev->resource[idx];
492
493                 if (!r->flags || r->parent || !r->start)
494                         continue;
495
496                 pci_claim_bridge_resource(dev, idx);
497         }
498 }
499
500 /*
501  *  Called after each bus is probed, but before its children are examined.
502  */
503 void pcibios_fixup_bus(struct pci_bus *b)
504 {
505         struct pci_dev *dev;
506
507         if (b->self) {
508                 pci_read_bridge_bases(b);
509                 pcibios_fixup_bridge_resources(b->self);
510         }
511         list_for_each_entry(dev, &b->devices, bus_list)
512                 pcibios_fixup_device_resources(dev);
513         platform_pci_fixup_bus(b);
514 }
515
516 void pcibios_add_bus(struct pci_bus *bus)
517 {
518         acpi_pci_add_bus(bus);
519 }
520
521 void pcibios_remove_bus(struct pci_bus *bus)
522 {
523         acpi_pci_remove_bus(bus);
524 }
525
526 void pcibios_set_master (struct pci_dev *dev)
527 {
528         /* No special bus mastering setup handling */
529 }
530
531 int
532 pcibios_enable_device (struct pci_dev *dev, int mask)
533 {
534         int ret;
535
536         ret = pci_enable_resources(dev, mask);
537         if (ret < 0)
538                 return ret;
539
540         if (!dev->msi_enabled)
541                 return acpi_pci_irq_enable(dev);
542         return 0;
543 }
544
545 void
546 pcibios_disable_device (struct pci_dev *dev)
547 {
548         BUG_ON(atomic_read(&dev->enable_cnt));
549         if (!dev->msi_enabled)
550                 acpi_pci_irq_disable(dev);
551 }
552
553 resource_size_t
554 pcibios_align_resource (void *data, const struct resource *res,
555                         resource_size_t size, resource_size_t align)
556 {
557         return res->start;
558 }
559
560 int
561 pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
562                      enum pci_mmap_state mmap_state, int write_combine)
563 {
564         unsigned long size = vma->vm_end - vma->vm_start;
565         pgprot_t prot;
566
567         /*
568          * I/O space cannot be accessed via normal processor loads and
569          * stores on this platform.
570          */
571         if (mmap_state == pci_mmap_io)
572                 /*
573                  * XXX we could relax this for I/O spaces for which ACPI
574                  * indicates that the space is 1-to-1 mapped.  But at the
575                  * moment, we don't support multiple PCI address spaces and
576                  * the legacy I/O space is not 1-to-1 mapped, so this is moot.
577                  */
578                 return -EINVAL;
579
580         if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
581                 return -EINVAL;
582
583         prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
584                                     vma->vm_page_prot);
585
586         /*
587          * If the user requested WC, the kernel uses UC or WC for this region,
588          * and the chipset supports WC, we can use WC. Otherwise, we have to
589          * use the same attribute the kernel uses.
590          */
591         if (write_combine &&
592             ((pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_UC ||
593              (pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_WC) &&
594             efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
595                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
596         else
597                 vma->vm_page_prot = prot;
598
599         if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
600                              vma->vm_end - vma->vm_start, vma->vm_page_prot))
601                 return -EAGAIN;
602
603         return 0;
604 }
605
606 /**
607  * ia64_pci_get_legacy_mem - generic legacy mem routine
608  * @bus: bus to get legacy memory base address for
609  *
610  * Find the base of legacy memory for @bus.  This is typically the first
611  * megabyte of bus address space for @bus or is simply 0 on platforms whose
612  * chipsets support legacy I/O and memory routing.  Returns the base address
613  * or an error pointer if an error occurred.
614  *
615  * This is the ia64 generic version of this routine.  Other platforms
616  * are free to override it with a machine vector.
617  */
618 char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
619 {
620         return (char *)__IA64_UNCACHED_OFFSET;
621 }
622
623 /**
624  * pci_mmap_legacy_page_range - map legacy memory space to userland
625  * @bus: bus whose legacy space we're mapping
626  * @vma: vma passed in by mmap
627  *
628  * Map legacy memory space for this device back to userspace using a machine
629  * vector to get the base address.
630  */
631 int
632 pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
633                            enum pci_mmap_state mmap_state)
634 {
635         unsigned long size = vma->vm_end - vma->vm_start;
636         pgprot_t prot;
637         char *addr;
638
639         /* We only support mmap'ing of legacy memory space */
640         if (mmap_state != pci_mmap_mem)
641                 return -ENOSYS;
642
643         /*
644          * Avoid attribute aliasing.  See Documentation/ia64/aliasing.txt
645          * for more details.
646          */
647         if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
648                 return -EINVAL;
649         prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
650                                     vma->vm_page_prot);
651
652         addr = pci_get_legacy_mem(bus);
653         if (IS_ERR(addr))
654                 return PTR_ERR(addr);
655
656         vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
657         vma->vm_page_prot = prot;
658
659         if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
660                             size, vma->vm_page_prot))
661                 return -EAGAIN;
662
663         return 0;
664 }
665
666 /**
667  * ia64_pci_legacy_read - read from legacy I/O space
668  * @bus: bus to read
669  * @port: legacy port value
670  * @val: caller allocated storage for returned value
671  * @size: number of bytes to read
672  *
673  * Simply reads @size bytes from @port and puts the result in @val.
674  *
675  * Again, this (and the write routine) are generic versions that can be
676  * overridden by the platform.  This is necessary on platforms that don't
677  * support legacy I/O routing or that hard fail on legacy I/O timeouts.
678  */
679 int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
680 {
681         int ret = size;
682
683         switch (size) {
684         case 1:
685                 *val = inb(port);
686                 break;
687         case 2:
688                 *val = inw(port);
689                 break;
690         case 4:
691                 *val = inl(port);
692                 break;
693         default:
694                 ret = -EINVAL;
695                 break;
696         }
697
698         return ret;
699 }
700
701 /**
702  * ia64_pci_legacy_write - perform a legacy I/O write
703  * @bus: bus pointer
704  * @port: port to write
705  * @val: value to write
706  * @size: number of bytes to write from @val
707  *
708  * Simply writes @size bytes of @val to @port.
709  */
710 int ia64_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
711 {
712         int ret = size;
713
714         switch (size) {
715         case 1:
716                 outb(val, port);
717                 break;
718         case 2:
719                 outw(val, port);
720                 break;
721         case 4:
722                 outl(val, port);
723                 break;
724         default:
725                 ret = -EINVAL;
726                 break;
727         }
728
729         return ret;
730 }
731
732 /**
733  * set_pci_cacheline_size - determine cacheline size for PCI devices
734  *
735  * We want to use the line-size of the outer-most cache.  We assume
736  * that this line-size is the same for all CPUs.
737  *
738  * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
739  */
740 static void __init set_pci_dfl_cacheline_size(void)
741 {
742         unsigned long levels, unique_caches;
743         long status;
744         pal_cache_config_info_t cci;
745
746         status = ia64_pal_cache_summary(&levels, &unique_caches);
747         if (status != 0) {
748                 pr_err("%s: ia64_pal_cache_summary() failed "
749                         "(status=%ld)\n", __func__, status);
750                 return;
751         }
752
753         status = ia64_pal_cache_config_info(levels - 1,
754                                 /* cache_type (data_or_unified)= */ 2, &cci);
755         if (status != 0) {
756                 pr_err("%s: ia64_pal_cache_config_info() failed "
757                         "(status=%ld)\n", __func__, status);
758                 return;
759         }
760         pci_dfl_cache_line_size = (1 << cci.pcci_line_size) / 4;
761 }
762
763 u64 ia64_dma_get_required_mask(struct device *dev)
764 {
765         u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
766         u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
767         u64 mask;
768
769         if (!high_totalram) {
770                 /* convert to mask just covering totalram */
771                 low_totalram = (1 << (fls(low_totalram) - 1));
772                 low_totalram += low_totalram - 1;
773                 mask = low_totalram;
774         } else {
775                 high_totalram = (1 << (fls(high_totalram) - 1));
776                 high_totalram += high_totalram - 1;
777                 mask = (((u64)high_totalram) << 32) + 0xffffffff;
778         }
779         return mask;
780 }
781 EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);
782
783 u64 dma_get_required_mask(struct device *dev)
784 {
785         return platform_dma_get_required_mask(dev);
786 }
787 EXPORT_SYMBOL_GPL(dma_get_required_mask);
788
789 static int __init pcibios_init(void)
790 {
791         set_pci_dfl_cacheline_size();
792         return 0;
793 }
794
795 subsys_initcall(pcibios_init);