| 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> | 
|  | 33 |  | 
|  | 34 | #include "pci.h" | 
|  | 35 |  | 
|  | 36 | /* | 
|  | 37 | * We need to avoid collisions with `mirrored' VGA ports | 
|  | 38 | * and other strange ISA hardware, so we always want the | 
|  | 39 | * addresses to be allocated in the 0x000-0x0ff region | 
|  | 40 | * modulo 0x400. | 
|  | 41 | * | 
|  | 42 | * Why? Because some silly external IO cards only decode | 
|  | 43 | * the low 10 bits of the IO address. The 0x00-0xff region | 
|  | 44 | * is reserved for motherboard devices that decode all 16 | 
|  | 45 | * bits, so it's ok to allocate at, say, 0x2800-0x28ff, | 
|  | 46 | * but we want to try to avoid allocating at 0x2900-0x2bff | 
|  | 47 | * which might have be mirrored at 0x0100-0x03ff.. | 
|  | 48 | */ | 
|  | 49 | void | 
|  | 50 | pcibios_align_resource(void *data, struct resource *res, | 
|  | 51 | unsigned long size, unsigned long align) | 
|  | 52 | { | 
|  | 53 | if (res->flags & IORESOURCE_IO) { | 
|  | 54 | unsigned long start = res->start; | 
|  | 55 |  | 
|  | 56 | if (start & 0x300) { | 
|  | 57 | start = (start + 0x3ff) & ~0x3ff; | 
|  | 58 | res->start = start; | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 |  | 
|  | 64 | /* | 
|  | 65 | *  Handle resources of PCI devices.  If the world were perfect, we could | 
|  | 66 | *  just allocate all the resource regions and do nothing more.  It isn't. | 
|  | 67 | *  On the other hand, we cannot just re-allocate all devices, as it would | 
|  | 68 | *  require us to know lots of host bridge internals.  So we attempt to | 
|  | 69 | *  keep as much of the original configuration as possible, but tweak it | 
|  | 70 | *  when it's found to be wrong. | 
|  | 71 | * | 
|  | 72 | *  Known BIOS problems we have to work around: | 
|  | 73 | *	- I/O or memory regions not configured | 
|  | 74 | *	- regions configured, but not enabled in the command register | 
|  | 75 | *	- bogus I/O addresses above 64K used | 
|  | 76 | *	- expansion ROMs left enabled (this may sound harmless, but given | 
|  | 77 | *	  the fact the PCI specs explicitly allow address decoders to be | 
|  | 78 | *	  shared between expansion ROMs and other resource regions, it's | 
|  | 79 | *	  at least dangerous) | 
|  | 80 | * | 
|  | 81 | *  Our solution: | 
|  | 82 | *	(1) Allocate resources for all buses behind PCI-to-PCI bridges. | 
|  | 83 | *	    This gives us fixed barriers on where we can allocate. | 
|  | 84 | *	(2) Allocate resources for all enabled devices.  If there is | 
|  | 85 | *	    a collision, just mark the resource as unallocated. Also | 
|  | 86 | *	    disable expansion ROMs during this step. | 
|  | 87 | *	(3) Try to allocate resources for disabled devices.  If the | 
|  | 88 | *	    resources were assigned correctly, everything goes well, | 
|  | 89 | *	    if they weren't, they won't disturb allocation of other | 
|  | 90 | *	    resources. | 
|  | 91 | *	(4) Assign new addresses to resources which were either | 
|  | 92 | *	    not configured at all or misconfigured.  If explicitly | 
|  | 93 | *	    requested by the user, configure expansion ROM address | 
|  | 94 | *	    as well. | 
|  | 95 | */ | 
|  | 96 |  | 
|  | 97 | static void __init pcibios_allocate_bus_resources(struct list_head *bus_list) | 
|  | 98 | { | 
|  | 99 | struct pci_bus *bus; | 
|  | 100 | struct pci_dev *dev; | 
|  | 101 | int idx; | 
|  | 102 | struct resource *r, *pr; | 
|  | 103 |  | 
|  | 104 | /* Depth-First Search on bus tree */ | 
|  | 105 | list_for_each_entry(bus, bus_list, node) { | 
|  | 106 | if ((dev = bus->self)) { | 
|  | 107 | for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) { | 
|  | 108 | r = &dev->resource[idx]; | 
|  | 109 | if (!r->start) | 
|  | 110 | continue; | 
|  | 111 | pr = pci_find_parent_resource(dev, r); | 
|  | 112 | if (!pr || request_resource(pr, r) < 0) | 
|  | 113 | printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %s\n", idx, pci_name(dev)); | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 | pcibios_allocate_bus_resources(&bus->children); | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | static void __init pcibios_allocate_resources(int pass) | 
|  | 121 | { | 
|  | 122 | struct pci_dev *dev = NULL; | 
|  | 123 | int idx, disabled; | 
|  | 124 | u16 command; | 
|  | 125 | struct resource *r, *pr; | 
|  | 126 |  | 
|  | 127 | for_each_pci_dev(dev) { | 
|  | 128 | pci_read_config_word(dev, PCI_COMMAND, &command); | 
|  | 129 | for(idx = 0; idx < 6; idx++) { | 
|  | 130 | r = &dev->resource[idx]; | 
|  | 131 | if (r->parent)		/* Already allocated */ | 
|  | 132 | continue; | 
|  | 133 | if (!r->start)		/* Address not assigned at all */ | 
|  | 134 | continue; | 
|  | 135 | if (r->flags & IORESOURCE_IO) | 
|  | 136 | disabled = !(command & PCI_COMMAND_IO); | 
|  | 137 | else | 
|  | 138 | disabled = !(command & PCI_COMMAND_MEMORY); | 
|  | 139 | if (pass == disabled) { | 
|  | 140 | DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n", | 
|  | 141 | r->start, r->end, r->flags, disabled, pass); | 
|  | 142 | pr = pci_find_parent_resource(dev, r); | 
|  | 143 | if (!pr || request_resource(pr, r) < 0) { | 
|  | 144 | printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %s\n", idx, pci_name(dev)); | 
|  | 145 | /* We'll assign a new address later */ | 
|  | 146 | r->end -= r->start; | 
|  | 147 | r->start = 0; | 
|  | 148 | } | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 | if (!pass) { | 
|  | 152 | r = &dev->resource[PCI_ROM_RESOURCE]; | 
|  | 153 | if (r->flags & IORESOURCE_ROM_ENABLE) { | 
|  | 154 | /* Turn the ROM off, leave the resource region, but keep it unregistered. */ | 
|  | 155 | u32 reg; | 
|  | 156 | DBG("PCI: Switching off ROM of %s\n", pci_name(dev)); | 
|  | 157 | r->flags &= ~IORESOURCE_ROM_ENABLE; | 
|  | 158 | pci_read_config_dword(dev, dev->rom_base_reg, ®); | 
|  | 159 | pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE); | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | static int __init pcibios_assign_resources(void) | 
|  | 166 | { | 
|  | 167 | struct pci_dev *dev = NULL; | 
|  | 168 | int idx; | 
|  | 169 | struct resource *r; | 
|  | 170 |  | 
|  | 171 | for_each_pci_dev(dev) { | 
|  | 172 | int class = dev->class >> 8; | 
|  | 173 |  | 
|  | 174 | /* Don't touch classless devices and host bridges */ | 
|  | 175 | if (!class || class == PCI_CLASS_BRIDGE_HOST) | 
|  | 176 | continue; | 
|  | 177 |  | 
|  | 178 | for(idx=0; idx<6; idx++) { | 
|  | 179 | r = &dev->resource[idx]; | 
|  | 180 |  | 
|  | 181 | /* | 
|  | 182 | *  Don't touch IDE controllers and I/O ports of video cards! | 
|  | 183 | */ | 
|  | 184 | if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) || | 
|  | 185 | (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO))) | 
|  | 186 | continue; | 
|  | 187 |  | 
|  | 188 | /* | 
|  | 189 | *  We shall assign a new address to this resource, either because | 
|  | 190 | *  the BIOS forgot to do so or because we have decided the old | 
|  | 191 | *  address was unusable for some reason. | 
|  | 192 | */ | 
|  | 193 | if (!r->start && r->end) | 
|  | 194 | pci_assign_resource(dev, idx); | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | if (pci_probe & PCI_ASSIGN_ROMS) { | 
|  | 198 | r = &dev->resource[PCI_ROM_RESOURCE]; | 
|  | 199 | r->end -= r->start; | 
|  | 200 | r->start = 0; | 
|  | 201 | if (r->end) | 
|  | 202 | pci_assign_resource(dev, PCI_ROM_RESOURCE); | 
|  | 203 | } | 
|  | 204 | } | 
|  | 205 | return 0; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | void __init pcibios_resource_survey(void) | 
|  | 209 | { | 
|  | 210 | DBG("PCI: Allocating resources\n"); | 
|  | 211 | pcibios_allocate_bus_resources(&pci_root_buses); | 
|  | 212 | pcibios_allocate_resources(0); | 
|  | 213 | pcibios_allocate_resources(1); | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | /** | 
|  | 217 | * called in fs_initcall (one below subsys_initcall), | 
|  | 218 | * give a chance for motherboard reserve resources | 
|  | 219 | */ | 
|  | 220 | fs_initcall(pcibios_assign_resources); | 
|  | 221 |  | 
|  | 222 | int pcibios_enable_resources(struct pci_dev *dev, int mask) | 
|  | 223 | { | 
|  | 224 | u16 cmd, old_cmd; | 
|  | 225 | int idx; | 
|  | 226 | struct resource *r; | 
|  | 227 |  | 
|  | 228 | pci_read_config_word(dev, PCI_COMMAND, &cmd); | 
|  | 229 | old_cmd = cmd; | 
|  | 230 | for(idx=0; idx<6; idx++) { | 
|  | 231 | /* Only set up the requested stuff */ | 
|  | 232 | if (!(mask & (1<<idx))) | 
|  | 233 | continue; | 
|  | 234 |  | 
|  | 235 | r = &dev->resource[idx]; | 
|  | 236 | if (!r->start && r->end) { | 
|  | 237 | printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev)); | 
|  | 238 | return -EINVAL; | 
|  | 239 | } | 
|  | 240 | if (r->flags & IORESOURCE_IO) | 
|  | 241 | cmd |= PCI_COMMAND_IO; | 
|  | 242 | if (r->flags & IORESOURCE_MEM) | 
|  | 243 | cmd |= PCI_COMMAND_MEMORY; | 
|  | 244 | } | 
|  | 245 | if (dev->resource[PCI_ROM_RESOURCE].start) | 
|  | 246 | cmd |= PCI_COMMAND_MEMORY; | 
|  | 247 | if (cmd != old_cmd) { | 
|  | 248 | printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd); | 
|  | 249 | pci_write_config_word(dev, PCI_COMMAND, cmd); | 
|  | 250 | } | 
|  | 251 | return 0; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | /* | 
|  | 255 | *  If we set up a device for bus mastering, we need to check the latency | 
|  | 256 | *  timer as certain crappy BIOSes forget to set it properly. | 
|  | 257 | */ | 
|  | 258 | unsigned int pcibios_max_latency = 255; | 
|  | 259 |  | 
|  | 260 | void pcibios_set_master(struct pci_dev *dev) | 
|  | 261 | { | 
|  | 262 | u8 lat; | 
|  | 263 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); | 
|  | 264 | if (lat < 16) | 
|  | 265 | lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; | 
|  | 266 | else if (lat > pcibios_max_latency) | 
|  | 267 | lat = pcibios_max_latency; | 
|  | 268 | else | 
|  | 269 | return; | 
|  | 270 | printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat); | 
|  | 271 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, | 
|  | 275 | enum pci_mmap_state mmap_state, int write_combine) | 
|  | 276 | { | 
|  | 277 | unsigned long prot; | 
|  | 278 |  | 
|  | 279 | /* I/O space cannot be accessed via normal processor loads and | 
|  | 280 | * stores on this platform. | 
|  | 281 | */ | 
|  | 282 | if (mmap_state == pci_mmap_io) | 
|  | 283 | return -EINVAL; | 
|  | 284 |  | 
|  | 285 | /* Leave vm_pgoff as-is, the PCI space address is the physical | 
|  | 286 | * address on this platform. | 
|  | 287 | */ | 
|  | 288 | vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO); | 
|  | 289 |  | 
|  | 290 | prot = pgprot_val(vma->vm_page_prot); | 
|  | 291 | if (boot_cpu_data.x86 > 3) | 
|  | 292 | prot |= _PAGE_PCD | _PAGE_PWT; | 
|  | 293 | vma->vm_page_prot = __pgprot(prot); | 
|  | 294 |  | 
|  | 295 | /* Write-combine setting is ignored, it is changed via the mtrr | 
|  | 296 | * interfaces on this platform. | 
|  | 297 | */ | 
|  | 298 | if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, | 
|  | 299 | vma->vm_end - vma->vm_start, | 
|  | 300 | vma->vm_page_prot)) | 
|  | 301 | return -EAGAIN; | 
|  | 302 |  | 
|  | 303 | return 0; | 
|  | 304 | } |