Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Low-Level PCI Access for i386 machines |
| 3 | * |
| 4 | * Copyright 1993, 1994 Drew Eckhardt |
| 5 | * Visionary Computing |
| 6 | * (Unix and Linux consulting and custom programming) |
| 7 | * Drew@Colorado.EDU |
| 8 | * +1 (303) 786-7975 |
| 9 | * |
| 10 | * Drew's work was sponsored by: |
| 11 | * iX Multiuser Multitasking Magazine |
| 12 | * Hannover, Germany |
| 13 | * hm@ix.de |
| 14 | * |
| 15 | * Copyright 1997--2000 Martin Mares <mj@ucw.cz> |
| 16 | * |
| 17 | * For more information, please consult the following manuals (look at |
| 18 | * http://www.pcisig.com/ for how to get them): |
| 19 | * |
| 20 | * PCI BIOS Specification |
| 21 | * PCI Local Bus Specification |
| 22 | * PCI to PCI Bridge Specification |
| 23 | * PCI System Design Guide |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/pci.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/ioport.h> |
| 32 | #include <linux/errno.h> |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 33 | #include <linux/bootmem.h> |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame^] | 34 | #include <linux/acpi.h> |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 35 | |
| 36 | #include <asm/pat.h> |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame^] | 37 | #include <asm/hpet.h> |
| 38 | #include <asm/io_apic.h> |
| 39 | #include <asm/e820.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include "pci.h" |
| 42 | |
Gary Hade | 036fff4 | 2007-10-03 15:56:14 -0700 | [diff] [blame] | 43 | static int |
| 44 | skip_isa_ioresource_align(struct pci_dev *dev) { |
| 45 | |
| 46 | if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) && |
Gary Hade | 1194925 | 2007-10-08 16:24:16 -0700 | [diff] [blame] | 47 | !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA)) |
Gary Hade | 036fff4 | 2007-10-03 15:56:14 -0700 | [diff] [blame] | 48 | return 1; |
| 49 | return 0; |
| 50 | } |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * We need to avoid collisions with `mirrored' VGA ports |
| 54 | * and other strange ISA hardware, so we always want the |
| 55 | * addresses to be allocated in the 0x000-0x0ff region |
| 56 | * modulo 0x400. |
| 57 | * |
| 58 | * Why? Because some silly external IO cards only decode |
| 59 | * the low 10 bits of the IO address. The 0x00-0xff region |
| 60 | * is reserved for motherboard devices that decode all 16 |
| 61 | * bits, so it's ok to allocate at, say, 0x2800-0x28ff, |
| 62 | * but we want to try to avoid allocating at 0x2900-0x2bff |
| 63 | * which might have be mirrored at 0x0100-0x03ff.. |
| 64 | */ |
| 65 | void |
| 66 | pcibios_align_resource(void *data, struct resource *res, |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 67 | resource_size_t size, resource_size_t align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Gary Hade | 036fff4 | 2007-10-03 15:56:14 -0700 | [diff] [blame] | 69 | struct pci_dev *dev = data; |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (res->flags & IORESOURCE_IO) { |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 72 | resource_size_t start = res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Gary Hade | 036fff4 | 2007-10-03 15:56:14 -0700 | [diff] [blame] | 74 | if (skip_isa_ioresource_align(dev)) |
| 75 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (start & 0x300) { |
| 77 | start = (start + 0x3ff) & ~0x3ff; |
| 78 | res->start = start; |
| 79 | } |
| 80 | } |
| 81 | } |
Dave Airlie | 6c00a61 | 2007-10-29 18:06:10 +1000 | [diff] [blame] | 82 | EXPORT_SYMBOL(pcibios_align_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame^] | 84 | static int check_res_with_valid(struct pci_dev *dev, struct resource *res) |
| 85 | { |
| 86 | unsigned long base; |
| 87 | unsigned long size; |
| 88 | int i; |
| 89 | |
| 90 | base = res->start; |
| 91 | size = (res->start == 0 && res->end == res->start) ? 0 : |
| 92 | (res->end - res->start + 1); |
| 93 | |
| 94 | if (!base || !size) |
| 95 | return 0; |
| 96 | |
| 97 | #ifdef CONFIG_HPET_TIMER |
| 98 | /* for hpet */ |
| 99 | if (base == hpet_address && (res->flags & IORESOURCE_MEM)) { |
| 100 | dev_info(&dev->dev, "BAR has HPET at %08lx-%08lx\n", |
| 101 | base, base + size - 1); |
| 102 | return 1; |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | #ifdef CONFIG_X86_IO_APIC |
| 107 | for (i = 0; i < nr_ioapics; i++) { |
| 108 | unsigned long ioapic_phys = mp_ioapics[i].mp_apicaddr; |
| 109 | |
| 110 | if (base == ioapic_phys && (res->flags & IORESOURCE_MEM)) { |
| 111 | dev_info(&dev->dev, "BAR has ioapic at %08lx-%08lx\n", |
| 112 | base, base + size - 1); |
| 113 | return 1; |
| 114 | } |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | #ifdef CONFIG_PCI_MMCONFIG |
| 119 | for (i = 0; i < pci_mmcfg_config_num; i++) { |
| 120 | unsigned long addr; |
| 121 | |
| 122 | addr = pci_mmcfg_config[i].address; |
| 123 | if (base == addr && (res->flags & IORESOURCE_MEM)) { |
| 124 | dev_info(&dev->dev, "BAR has MMCONFIG at %08lx-%08lx\n", |
| 125 | base, base + size - 1); |
| 126 | return 1; |
| 127 | } |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int check_platform(struct pci_dev *dev, struct resource *res) |
| 135 | { |
| 136 | struct resource *root = NULL; |
| 137 | |
| 138 | /* |
| 139 | * forcibly insert it into the |
| 140 | * resource tree |
| 141 | */ |
| 142 | if (res->flags & IORESOURCE_MEM) |
| 143 | root = &iomem_resource; |
| 144 | else if (res->flags & IORESOURCE_IO) |
| 145 | root = &ioport_resource; |
| 146 | |
| 147 | if (root && check_res_with_valid(dev, res)) { |
| 148 | insert_resource(root, res); |
| 149 | |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* |
| 156 | * Handle resources of PCI devices. If the world were perfect, we could |
| 157 | * just allocate all the resource regions and do nothing more. It isn't. |
| 158 | * On the other hand, we cannot just re-allocate all devices, as it would |
| 159 | * require us to know lots of host bridge internals. So we attempt to |
| 160 | * keep as much of the original configuration as possible, but tweak it |
| 161 | * when it's found to be wrong. |
| 162 | * |
| 163 | * Known BIOS problems we have to work around: |
| 164 | * - I/O or memory regions not configured |
| 165 | * - regions configured, but not enabled in the command register |
| 166 | * - bogus I/O addresses above 64K used |
| 167 | * - expansion ROMs left enabled (this may sound harmless, but given |
| 168 | * the fact the PCI specs explicitly allow address decoders to be |
| 169 | * shared between expansion ROMs and other resource regions, it's |
| 170 | * at least dangerous) |
| 171 | * |
| 172 | * Our solution: |
| 173 | * (1) Allocate resources for all buses behind PCI-to-PCI bridges. |
| 174 | * This gives us fixed barriers on where we can allocate. |
| 175 | * (2) Allocate resources for all enabled devices. If there is |
| 176 | * a collision, just mark the resource as unallocated. Also |
| 177 | * disable expansion ROMs during this step. |
| 178 | * (3) Try to allocate resources for disabled devices. If the |
| 179 | * resources were assigned correctly, everything goes well, |
| 180 | * if they weren't, they won't disturb allocation of other |
| 181 | * resources. |
| 182 | * (4) Assign new addresses to resources which were either |
| 183 | * not configured at all or misconfigured. If explicitly |
| 184 | * requested by the user, configure expansion ROM address |
| 185 | * as well. |
| 186 | */ |
| 187 | |
| 188 | static void __init pcibios_allocate_bus_resources(struct list_head *bus_list) |
| 189 | { |
| 190 | struct pci_bus *bus; |
| 191 | struct pci_dev *dev; |
| 192 | int idx; |
| 193 | struct resource *r, *pr; |
| 194 | |
| 195 | /* Depth-First Search on bus tree */ |
| 196 | list_for_each_entry(bus, bus_list, node) { |
| 197 | if ((dev = bus->self)) { |
Randy Dunlap | 7edab2f | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 198 | for (idx = PCI_BRIDGE_RESOURCES; |
| 199 | idx < PCI_NUM_RESOURCES; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | r = &dev->resource[idx]; |
Ivan Kokshaysky | 299de03 | 2005-06-15 18:59:27 +0400 | [diff] [blame] | 201 | if (!r->flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | continue; |
| 203 | pr = pci_find_parent_resource(dev, r); |
Randy Dunlap | 7edab2f | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 204 | if (!r->start || !pr || |
| 205 | request_resource(pr, r) < 0) { |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame^] | 206 | if (check_platform(dev, r)) |
| 207 | continue; |
Linus Torvalds | 011fec7 | 2008-09-02 10:38:28 -0700 | [diff] [blame] | 208 | dev_err(&dev->dev, "BAR %d: can't allocate resource\n", idx); |
Randy Dunlap | 7edab2f | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 209 | /* |
| 210 | * Something is wrong with the region. |
| 211 | * Invalidate the resource to prevent |
| 212 | * child resource allocations in this |
| 213 | * range. |
| 214 | */ |
Ivan Kokshaysky | 299de03 | 2005-06-15 18:59:27 +0400 | [diff] [blame] | 215 | r->flags = 0; |
| 216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | pcibios_allocate_bus_resources(&bus->children); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static void __init pcibios_allocate_resources(int pass) |
| 224 | { |
| 225 | struct pci_dev *dev = NULL; |
| 226 | int idx, disabled; |
| 227 | u16 command; |
| 228 | struct resource *r, *pr; |
| 229 | |
| 230 | for_each_pci_dev(dev) { |
| 231 | pci_read_config_word(dev, PCI_COMMAND, &command); |
Randy Dunlap | 7edab2f | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 232 | for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | r = &dev->resource[idx]; |
| 234 | if (r->parent) /* Already allocated */ |
| 235 | continue; |
| 236 | if (!r->start) /* Address not assigned at all */ |
| 237 | continue; |
| 238 | if (r->flags & IORESOURCE_IO) |
| 239 | disabled = !(command & PCI_COMMAND_IO); |
| 240 | else |
| 241 | disabled = !(command & PCI_COMMAND_MEMORY); |
| 242 | if (pass == disabled) { |
Linus Torvalds | 011fec7 | 2008-09-02 10:38:28 -0700 | [diff] [blame] | 243 | dev_dbg(&dev->dev, "resource %#08llx-%#08llx (f=%lx, d=%d, p=%d)\n", |
Bjorn Helgaas | 12c0b20 | 2008-07-23 17:00:13 -0600 | [diff] [blame] | 244 | (unsigned long long) r->start, |
| 245 | (unsigned long long) r->end, |
| 246 | r->flags, disabled, pass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | pr = pci_find_parent_resource(dev, r); |
| 248 | if (!pr || request_resource(pr, r) < 0) { |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame^] | 249 | if (check_platform(dev, r)) |
| 250 | continue; |
Linus Torvalds | 011fec7 | 2008-09-02 10:38:28 -0700 | [diff] [blame] | 251 | dev_err(&dev->dev, "BAR %d: can't allocate resource\n", idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /* We'll assign a new address later */ |
| 253 | r->end -= r->start; |
| 254 | r->start = 0; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | if (!pass) { |
| 259 | r = &dev->resource[PCI_ROM_RESOURCE]; |
| 260 | if (r->flags & IORESOURCE_ROM_ENABLE) { |
Randy Dunlap | 7edab2f | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 261 | /* Turn the ROM off, leave the resource region, |
| 262 | * but keep it unregistered. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | u32 reg; |
Bjorn Helgaas | 12c0b20 | 2008-07-23 17:00:13 -0600 | [diff] [blame] | 264 | dev_dbg(&dev->dev, "disabling ROM\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | r->flags &= ~IORESOURCE_ROM_ENABLE; |
Randy Dunlap | 7edab2f | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 266 | pci_read_config_dword(dev, |
| 267 | dev->rom_base_reg, ®); |
| 268 | pci_write_config_dword(dev, dev->rom_base_reg, |
| 269 | reg & ~PCI_ROM_ADDRESS_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static int __init pcibios_assign_resources(void) |
| 276 | { |
| 277 | struct pci_dev *dev = NULL; |
Ivan Kokshaysky | 81d4af1 | 2005-08-30 18:48:52 +0400 | [diff] [blame] | 278 | struct resource *r, *pr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Ivan Kokshaysky | 81d4af1 | 2005-08-30 18:48:52 +0400 | [diff] [blame] | 280 | if (!(pci_probe & PCI_ASSIGN_ROMS)) { |
Randy Dunlap | 7edab2f | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 281 | /* |
| 282 | * Try to use BIOS settings for ROMs, otherwise let |
| 283 | * pci_assign_unassigned_resources() allocate the new |
| 284 | * addresses. |
| 285 | */ |
Ivan Kokshaysky | 81d4af1 | 2005-08-30 18:48:52 +0400 | [diff] [blame] | 286 | for_each_pci_dev(dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | r = &dev->resource[PCI_ROM_RESOURCE]; |
Ivan Kokshaysky | 81d4af1 | 2005-08-30 18:48:52 +0400 | [diff] [blame] | 288 | if (!r->flags || !r->start) |
| 289 | continue; |
| 290 | pr = pci_find_parent_resource(dev, r); |
| 291 | if (!pr || request_resource(pr, r) < 0) { |
| 292 | r->end -= r->start; |
| 293 | r->start = 0; |
| 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
Ivan Kokshaysky | 81d4af1 | 2005-08-30 18:48:52 +0400 | [diff] [blame] | 297 | |
| 298 | pci_assign_unassigned_resources(); |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | void __init pcibios_resource_survey(void) |
| 304 | { |
| 305 | DBG("PCI: Allocating resources\n"); |
| 306 | pcibios_allocate_bus_resources(&pci_root_buses); |
| 307 | pcibios_allocate_resources(0); |
| 308 | pcibios_allocate_resources(1); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * called in fs_initcall (one below subsys_initcall), |
| 313 | * give a chance for motherboard reserve resources |
| 314 | */ |
| 315 | fs_initcall(pcibios_assign_resources); |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* |
| 318 | * If we set up a device for bus mastering, we need to check the latency |
| 319 | * timer as certain crappy BIOSes forget to set it properly. |
| 320 | */ |
| 321 | unsigned int pcibios_max_latency = 255; |
| 322 | |
| 323 | void pcibios_set_master(struct pci_dev *dev) |
| 324 | { |
| 325 | u8 lat; |
| 326 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); |
| 327 | if (lat < 16) |
| 328 | lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; |
| 329 | else if (lat > pcibios_max_latency) |
| 330 | lat = pcibios_max_latency; |
| 331 | else |
| 332 | return; |
Bjorn Helgaas | 12c0b20 | 2008-07-23 17:00:13 -0600 | [diff] [blame] | 333 | dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); |
| 335 | } |
| 336 | |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 337 | static void pci_unmap_page_range(struct vm_area_struct *vma) |
| 338 | { |
| 339 | u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT; |
| 340 | free_memtype(addr, addr + vma->vm_end - vma->vm_start); |
| 341 | } |
| 342 | |
| 343 | static void pci_track_mmap_page_range(struct vm_area_struct *vma) |
| 344 | { |
| 345 | u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT; |
| 346 | unsigned long flags = pgprot_val(vma->vm_page_prot) |
| 347 | & _PAGE_CACHE_MASK; |
| 348 | |
| 349 | reserve_memtype(addr, addr + vma->vm_end - vma->vm_start, flags, NULL); |
| 350 | } |
| 351 | |
| 352 | static struct vm_operations_struct pci_mmap_ops = { |
| 353 | .open = pci_track_mmap_page_range, |
| 354 | .close = pci_unmap_page_range, |
Rik van Riel | 7ae8ed5 | 2008-07-23 21:27:07 -0700 | [diff] [blame] | 355 | .access = generic_access_phys, |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 356 | }; |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, |
| 359 | enum pci_mmap_state mmap_state, int write_combine) |
| 360 | { |
| 361 | unsigned long prot; |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 362 | u64 addr = vma->vm_pgoff << PAGE_SHIFT; |
| 363 | unsigned long len = vma->vm_end - vma->vm_start; |
| 364 | unsigned long flags; |
| 365 | unsigned long new_flags; |
Venki Pallipadi | dee7cbb | 2008-03-24 14:39:55 -0700 | [diff] [blame] | 366 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | /* I/O space cannot be accessed via normal processor loads and |
| 369 | * stores on this platform. |
| 370 | */ |
| 371 | if (mmap_state == pci_mmap_io) |
| 372 | return -EINVAL; |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | prot = pgprot_val(vma->vm_page_prot); |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 375 | if (pat_enabled && write_combine) |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 376 | prot |= _PAGE_CACHE_WC; |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 377 | else if (pat_enabled || boot_cpu_data.x86 > 3) |
Suresh Siddha | de33c44 | 2008-04-25 17:07:22 -0700 | [diff] [blame] | 378 | /* |
| 379 | * ioremap() and ioremap_nocache() defaults to UC MINUS for now. |
| 380 | * To avoid attribute conflicts, request UC MINUS here |
| 381 | * aswell. |
| 382 | */ |
| 383 | prot |= _PAGE_CACHE_UC_MINUS; |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | vma->vm_page_prot = __pgprot(prot); |
| 386 | |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 387 | flags = pgprot_val(vma->vm_page_prot) & _PAGE_CACHE_MASK; |
Venki Pallipadi | dee7cbb | 2008-03-24 14:39:55 -0700 | [diff] [blame] | 388 | retval = reserve_memtype(addr, addr + len, flags, &new_flags); |
| 389 | if (retval) |
| 390 | return retval; |
| 391 | |
| 392 | if (flags != new_flags) { |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 393 | /* |
| 394 | * Do not fallback to certain memory types with certain |
| 395 | * requested type: |
| 396 | * - request is uncached, return cannot be write-back |
| 397 | * - request is uncached, return cannot be write-combine |
| 398 | * - request is write-combine, return cannot be write-back |
| 399 | */ |
Suresh Siddha | de33c44 | 2008-04-25 17:07:22 -0700 | [diff] [blame] | 400 | if ((flags == _PAGE_CACHE_UC_MINUS && |
| 401 | (new_flags == _PAGE_CACHE_WB)) || |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 402 | (flags == _PAGE_CACHE_WC && |
| 403 | new_flags == _PAGE_CACHE_WB)) { |
| 404 | free_memtype(addr, addr+len); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | flags = new_flags; |
| 408 | } |
| 409 | |
Yinghai Lu | 965194c | 2008-07-12 14:31:28 -0700 | [diff] [blame] | 410 | if (((vma->vm_pgoff < max_low_pfn_mapped) || |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 411 | (vma->vm_pgoff >= (1UL<<(32 - PAGE_SHIFT)) && |
Yinghai Lu | 965194c | 2008-07-12 14:31:28 -0700 | [diff] [blame] | 412 | vma->vm_pgoff < max_pfn_mapped)) && |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 413 | ioremap_change_attr((unsigned long)__va(addr), len, flags)) { |
| 414 | free_memtype(addr, addr + len); |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | |
Michael S. Tsirkin | 346d388 | 2005-07-31 11:51:45 +0300 | [diff] [blame] | 418 | if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
| 419 | vma->vm_end - vma->vm_start, |
| 420 | vma->vm_page_prot)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return -EAGAIN; |
| 422 | |
venkatesh.pallipadi@intel.com | 03d72aa | 2008-03-18 17:00:19 -0700 | [diff] [blame] | 423 | vma->vm_ops = &pci_mmap_ops; |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return 0; |
| 426 | } |