pci_64.c 16 KB

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