pci_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Port for PPC64 David Engebretsen, IBM Corp.
  3. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  4. *
  5. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  6. * Rework, based on alpha PCI code.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #undef DEBUG
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/string.h>
  17. #include <linux/init.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/mm.h>
  20. #include <linux/list.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/irq.h>
  23. #include <linux/vmalloc.h>
  24. #include <asm/processor.h>
  25. #include <asm/io.h>
  26. #include <asm/prom.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/machdep.h>
  30. #include <asm/ppc-pci.h>
  31. unsigned long pci_probe_only = 1;
  32. /* pci_io_base -- the base address from which io bars are offsets.
  33. * This is the lowest I/O base address (so bar values are always positive),
  34. * and it *must* be the start of ISA space if an ISA bus exists because
  35. * ISA drivers use hard coded offsets. If no ISA bus exists nothing
  36. * is mapped on the first 64K of IO space
  37. */
  38. unsigned long pci_io_base = ISA_IO_BASE;
  39. EXPORT_SYMBOL(pci_io_base);
  40. static void fixup_broken_pcnet32(struct pci_dev* dev)
  41. {
  42. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  43. dev->vendor = PCI_VENDOR_ID_AMD;
  44. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  45. }
  46. }
  47. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  48. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  49. {
  50. const u32 *prop;
  51. int len;
  52. prop = of_get_property(np, name, &len);
  53. if (prop && len >= 4)
  54. return *prop;
  55. return def;
  56. }
  57. static unsigned int pci_parse_of_flags(u32 addr0)
  58. {
  59. unsigned int flags = 0;
  60. if (addr0 & 0x02000000) {
  61. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  62. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  63. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  64. if (addr0 & 0x40000000)
  65. flags |= IORESOURCE_PREFETCH
  66. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  67. } else if (addr0 & 0x01000000)
  68. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  69. return flags;
  70. }
  71. static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
  72. {
  73. u64 base, size;
  74. unsigned int flags;
  75. struct resource *res;
  76. const u32 *addrs;
  77. u32 i;
  78. int proplen;
  79. addrs = of_get_property(node, "assigned-addresses", &proplen);
  80. if (!addrs)
  81. return;
  82. pr_debug(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  83. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  84. flags = pci_parse_of_flags(addrs[0]);
  85. if (!flags)
  86. continue;
  87. base = of_read_number(&addrs[1], 2);
  88. size = of_read_number(&addrs[3], 2);
  89. if (!size)
  90. continue;
  91. i = addrs[0] & 0xff;
  92. pr_debug(" base: %llx, size: %llx, i: %x\n",
  93. (unsigned long long)base,
  94. (unsigned long long)size, i);
  95. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  96. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  97. } else if (i == dev->rom_base_reg) {
  98. res = &dev->resource[PCI_ROM_RESOURCE];
  99. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  100. } else {
  101. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  102. continue;
  103. }
  104. res->start = base;
  105. res->end = base + size - 1;
  106. res->flags = flags;
  107. res->name = pci_name(dev);
  108. }
  109. }
  110. struct pci_dev *of_create_pci_dev(struct device_node *node,
  111. struct pci_bus *bus, int devfn)
  112. {
  113. struct pci_dev *dev;
  114. const char *type;
  115. dev = alloc_pci_dev();
  116. if (!dev)
  117. return NULL;
  118. type = of_get_property(node, "device_type", NULL);
  119. if (type == NULL)
  120. type = "";
  121. pr_debug(" create device, devfn: %x, type: %s\n", devfn, type);
  122. dev->bus = bus;
  123. dev->sysdata = node;
  124. dev->dev.parent = bus->bridge;
  125. dev->dev.bus = &pci_bus_type;
  126. dev->devfn = devfn;
  127. dev->multifunction = 0; /* maybe a lie? */
  128. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  129. dev->device = get_int_prop(node, "device-id", 0xffff);
  130. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  131. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  132. dev->cfg_size = pci_cfg_space_size(dev);
  133. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  134. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  135. dev->class = get_int_prop(node, "class-code", 0);
  136. dev->revision = get_int_prop(node, "revision-id", 0);
  137. pr_debug(" class: 0x%x\n", dev->class);
  138. pr_debug(" revision: 0x%x\n", dev->revision);
  139. dev->current_state = 4; /* unknown power state */
  140. dev->error_state = pci_channel_io_normal;
  141. dev->dma_mask = 0xffffffff;
  142. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  143. /* a PCI-PCI bridge */
  144. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  145. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  146. } else if (!strcmp(type, "cardbus")) {
  147. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  148. } else {
  149. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  150. dev->rom_base_reg = PCI_ROM_ADDRESS;
  151. /* Maybe do a default OF mapping here */
  152. dev->irq = NO_IRQ;
  153. }
  154. pci_parse_of_addrs(node, dev);
  155. pr_debug(" adding to system ...\n");
  156. pci_device_add(dev, bus);
  157. return dev;
  158. }
  159. EXPORT_SYMBOL(of_create_pci_dev);
  160. static void __devinit __of_scan_bus(struct device_node *node,
  161. struct pci_bus *bus, int rescan_existing)
  162. {
  163. struct device_node *child;
  164. const u32 *reg;
  165. int reglen, devfn;
  166. struct pci_dev *dev;
  167. pr_debug("of_scan_bus(%s) bus no %d... \n",
  168. node->full_name, bus->number);
  169. /* Scan direct children */
  170. for_each_child_of_node(node, child) {
  171. pr_debug(" * %s\n", child->full_name);
  172. reg = of_get_property(child, "reg", &reglen);
  173. if (reg == NULL || reglen < 20)
  174. continue;
  175. devfn = (reg[0] >> 8) & 0xff;
  176. /* create a new pci_dev for this device */
  177. dev = of_create_pci_dev(child, bus, devfn);
  178. if (!dev)
  179. continue;
  180. pr_debug(" dev header type: %x\n", dev->hdr_type);
  181. }
  182. /* Apply all fixups necessary. We don't fixup the bus "self"
  183. * for an existing bridge that is being rescanned
  184. */
  185. if (!rescan_existing)
  186. pcibios_setup_bus_self(bus);
  187. pcibios_setup_bus_devices(bus);
  188. /* Now scan child busses */
  189. list_for_each_entry(dev, &bus->devices, bus_list) {
  190. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  191. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  192. struct device_node *child = pci_device_to_OF_node(dev);
  193. if (dev)
  194. of_scan_pci_bridge(child, dev);
  195. }
  196. }
  197. }
  198. void __devinit of_scan_bus(struct device_node *node,
  199. struct pci_bus *bus)
  200. {
  201. __of_scan_bus(node, bus, 0);
  202. }
  203. EXPORT_SYMBOL_GPL(of_scan_bus);
  204. void __devinit of_rescan_bus(struct device_node *node,
  205. struct pci_bus *bus)
  206. {
  207. __of_scan_bus(node, bus, 1);
  208. }
  209. EXPORT_SYMBOL_GPL(of_rescan_bus);
  210. void __devinit of_scan_pci_bridge(struct device_node *node,
  211. struct pci_dev *dev)
  212. {
  213. struct pci_bus *bus;
  214. const u32 *busrange, *ranges;
  215. int len, i, mode;
  216. struct resource *res;
  217. unsigned int flags;
  218. u64 size;
  219. pr_debug("of_scan_pci_bridge(%s)\n", node->full_name);
  220. /* parse bus-range property */
  221. busrange = of_get_property(node, "bus-range", &len);
  222. if (busrange == NULL || len != 8) {
  223. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  224. node->full_name);
  225. return;
  226. }
  227. ranges = of_get_property(node, "ranges", &len);
  228. if (ranges == NULL) {
  229. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  230. node->full_name);
  231. return;
  232. }
  233. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  234. if (!bus) {
  235. printk(KERN_ERR "Failed to create pci bus for %s\n",
  236. node->full_name);
  237. return;
  238. }
  239. bus->primary = dev->bus->number;
  240. bus->subordinate = busrange[1];
  241. bus->bridge_ctl = 0;
  242. bus->sysdata = node;
  243. /* parse ranges property */
  244. /* PCI #address-cells == 3 and #size-cells == 2 always */
  245. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  246. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  247. res->flags = 0;
  248. bus->resource[i] = res;
  249. ++res;
  250. }
  251. i = 1;
  252. for (; len >= 32; len -= 32, ranges += 8) {
  253. flags = pci_parse_of_flags(ranges[0]);
  254. size = of_read_number(&ranges[6], 2);
  255. if (flags == 0 || size == 0)
  256. continue;
  257. if (flags & IORESOURCE_IO) {
  258. res = bus->resource[0];
  259. if (res->flags) {
  260. printk(KERN_ERR "PCI: ignoring extra I/O range"
  261. " for bridge %s\n", node->full_name);
  262. continue;
  263. }
  264. } else {
  265. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  266. printk(KERN_ERR "PCI: too many memory ranges"
  267. " for bridge %s\n", node->full_name);
  268. continue;
  269. }
  270. res = bus->resource[i];
  271. ++i;
  272. }
  273. res->start = of_read_number(&ranges[1], 2);
  274. res->end = res->start + size - 1;
  275. res->flags = flags;
  276. }
  277. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  278. bus->number);
  279. pr_debug(" bus name: %s\n", bus->name);
  280. mode = PCI_PROBE_NORMAL;
  281. if (ppc_md.pci_probe_mode)
  282. mode = ppc_md.pci_probe_mode(bus);
  283. pr_debug(" probe mode: %d\n", mode);
  284. if (mode == PCI_PROBE_DEVTREE)
  285. of_scan_bus(node, bus);
  286. else if (mode == PCI_PROBE_NORMAL)
  287. pci_scan_child_bus(bus);
  288. }
  289. EXPORT_SYMBOL(of_scan_pci_bridge);
  290. void __devinit scan_phb(struct pci_controller *hose)
  291. {
  292. struct pci_bus *bus;
  293. struct device_node *node = hose->dn;
  294. int mode;
  295. pr_debug("PCI: Scanning PHB %s\n",
  296. node ? node->full_name : "<NO NAME>");
  297. /* Create an empty bus for the toplevel */
  298. bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node);
  299. if (bus == NULL) {
  300. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  301. hose->global_number);
  302. return;
  303. }
  304. bus->secondary = hose->first_busno;
  305. hose->bus = bus;
  306. /* Get some IO space for the new PHB */
  307. pcibios_map_io_space(bus);
  308. /* Wire up PHB bus resources */
  309. pcibios_setup_phb_resources(hose);
  310. /* Get probe mode and perform scan */
  311. mode = PCI_PROBE_NORMAL;
  312. if (node && ppc_md.pci_probe_mode)
  313. mode = ppc_md.pci_probe_mode(bus);
  314. pr_debug(" probe mode: %d\n", mode);
  315. if (mode == PCI_PROBE_DEVTREE) {
  316. bus->subordinate = hose->last_busno;
  317. of_scan_bus(node, bus);
  318. }
  319. if (mode == PCI_PROBE_NORMAL)
  320. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  321. }
  322. static int __init pcibios_init(void)
  323. {
  324. struct pci_controller *hose, *tmp;
  325. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  326. /* For now, override phys_mem_access_prot. If we need it,g
  327. * later, we may move that initialization to each ppc_md
  328. */
  329. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  330. if (pci_probe_only)
  331. ppc_pci_flags |= PPC_PCI_PROBE_ONLY;
  332. /* On ppc64, we always enable PCI domains and we keep domain 0
  333. * backward compatible in /proc for video cards
  334. */
  335. ppc_pci_flags |= PPC_PCI_ENABLE_PROC_DOMAINS | PPC_PCI_COMPAT_DOMAIN_0;
  336. /* Scan all of the recorded PCI controllers. */
  337. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  338. scan_phb(hose);
  339. pci_bus_add_devices(hose->bus);
  340. }
  341. /* Call common code to handle resource allocation */
  342. pcibios_resource_survey();
  343. printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
  344. return 0;
  345. }
  346. subsys_initcall(pcibios_init);
  347. #ifdef CONFIG_HOTPLUG
  348. int pcibios_unmap_io_space(struct pci_bus *bus)
  349. {
  350. struct pci_controller *hose;
  351. WARN_ON(bus == NULL);
  352. /* If this is not a PHB, we only flush the hash table over
  353. * the area mapped by this bridge. We don't play with the PTE
  354. * mappings since we might have to deal with sub-page alignemnts
  355. * so flushing the hash table is the only sane way to make sure
  356. * that no hash entries are covering that removed bridge area
  357. * while still allowing other busses overlapping those pages
  358. */
  359. if (bus->self) {
  360. struct resource *res = bus->resource[0];
  361. pr_debug("IO unmapping for PCI-PCI bridge %s\n",
  362. pci_name(bus->self));
  363. __flush_hash_table_range(&init_mm, res->start + _IO_BASE,
  364. res->end + _IO_BASE + 1);
  365. return 0;
  366. }
  367. /* Get the host bridge */
  368. hose = pci_bus_to_host(bus);
  369. /* Check if we have IOs allocated */
  370. if (hose->io_base_alloc == 0)
  371. return 0;
  372. pr_debug("IO unmapping for PHB %s\n", hose->dn->full_name);
  373. pr_debug(" alloc=0x%p\n", hose->io_base_alloc);
  374. /* This is a PHB, we fully unmap the IO area */
  375. vunmap(hose->io_base_alloc);
  376. return 0;
  377. }
  378. EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
  379. #endif /* CONFIG_HOTPLUG */
  380. int __devinit pcibios_map_io_space(struct pci_bus *bus)
  381. {
  382. struct vm_struct *area;
  383. unsigned long phys_page;
  384. unsigned long size_page;
  385. unsigned long io_virt_offset;
  386. struct pci_controller *hose;
  387. WARN_ON(bus == NULL);
  388. /* If this not a PHB, nothing to do, page tables still exist and
  389. * thus HPTEs will be faulted in when needed
  390. */
  391. if (bus->self) {
  392. pr_debug("IO mapping for PCI-PCI bridge %s\n",
  393. pci_name(bus->self));
  394. pr_debug(" virt=0x%016llx...0x%016llx\n",
  395. bus->resource[0]->start + _IO_BASE,
  396. bus->resource[0]->end + _IO_BASE);
  397. return 0;
  398. }
  399. /* Get the host bridge */
  400. hose = pci_bus_to_host(bus);
  401. phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
  402. size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE);
  403. /* Make sure IO area address is clear */
  404. hose->io_base_alloc = NULL;
  405. /* If there's no IO to map on that bus, get away too */
  406. if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
  407. return 0;
  408. /* Let's allocate some IO space for that guy. We don't pass
  409. * VM_IOREMAP because we don't care about alignment tricks that
  410. * the core does in that case. Maybe we should due to stupid card
  411. * with incomplete address decoding but I'd rather not deal with
  412. * those outside of the reserved 64K legacy region.
  413. */
  414. area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
  415. if (area == NULL)
  416. return -ENOMEM;
  417. hose->io_base_alloc = area->addr;
  418. hose->io_base_virt = (void __iomem *)(area->addr +
  419. hose->io_base_phys - phys_page);
  420. pr_debug("IO mapping for PHB %s\n", hose->dn->full_name);
  421. pr_debug(" phys=0x%016llx, virt=0x%p (alloc=0x%p)\n",
  422. hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
  423. pr_debug(" size=0x%016lx (alloc=0x%016lx)\n",
  424. hose->pci_io_size, size_page);
  425. /* Establish the mapping */
  426. if (__ioremap_at(phys_page, area->addr, size_page,
  427. _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)
  428. return -ENOMEM;
  429. /* Fixup hose IO resource */
  430. io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  431. hose->io_resource.start += io_virt_offset;
  432. hose->io_resource.end += io_virt_offset;
  433. pr_debug(" hose->io_resource=0x%016llx...0x%016llx\n",
  434. hose->io_resource.start, hose->io_resource.end);
  435. return 0;
  436. }
  437. EXPORT_SYMBOL_GPL(pcibios_map_io_space);
  438. #define IOBASE_BRIDGE_NUMBER 0
  439. #define IOBASE_MEMORY 1
  440. #define IOBASE_IO 2
  441. #define IOBASE_ISA_IO 3
  442. #define IOBASE_ISA_MEM 4
  443. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  444. unsigned long in_devfn)
  445. {
  446. struct pci_controller* hose;
  447. struct list_head *ln;
  448. struct pci_bus *bus = NULL;
  449. struct device_node *hose_node;
  450. /* Argh ! Please forgive me for that hack, but that's the
  451. * simplest way to get existing XFree to not lockup on some
  452. * G5 machines... So when something asks for bus 0 io base
  453. * (bus 0 is HT root), we return the AGP one instead.
  454. */
  455. if (in_bus == 0 && machine_is_compatible("MacRISC4")) {
  456. struct device_node *agp;
  457. agp = of_find_compatible_node(NULL, NULL, "u3-agp");
  458. if (agp)
  459. in_bus = 0xf0;
  460. of_node_put(agp);
  461. }
  462. /* That syscall isn't quite compatible with PCI domains, but it's
  463. * used on pre-domains setup. We return the first match
  464. */
  465. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  466. bus = pci_bus_b(ln);
  467. if (in_bus >= bus->number && in_bus <= bus->subordinate)
  468. break;
  469. bus = NULL;
  470. }
  471. if (bus == NULL || bus->sysdata == NULL)
  472. return -ENODEV;
  473. hose_node = (struct device_node *)bus->sysdata;
  474. hose = PCI_DN(hose_node)->phb;
  475. switch (which) {
  476. case IOBASE_BRIDGE_NUMBER:
  477. return (long)hose->first_busno;
  478. case IOBASE_MEMORY:
  479. return (long)hose->pci_mem_offset;
  480. case IOBASE_IO:
  481. return (long)hose->io_base_phys;
  482. case IOBASE_ISA_IO:
  483. return (long)isa_io_base;
  484. case IOBASE_ISA_MEM:
  485. return -EINVAL;
  486. }
  487. return -EOPNOTSUPP;
  488. }
  489. #ifdef CONFIG_NUMA
  490. int pcibus_to_node(struct pci_bus *bus)
  491. {
  492. struct pci_controller *phb = pci_bus_to_host(bus);
  493. return phb->node;
  494. }
  495. EXPORT_SYMBOL(pcibus_to_node);
  496. #endif