pci_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. #ifdef DEBUG
  32. #include <asm/udbg.h>
  33. #define DBG(fmt...) printk(fmt)
  34. #else
  35. #define DBG(fmt...)
  36. #endif
  37. unsigned long pci_probe_only = 1;
  38. /* pci_io_base -- the base address from which io bars are offsets.
  39. * This is the lowest I/O base address (so bar values are always positive),
  40. * and it *must* be the start of ISA space if an ISA bus exists because
  41. * ISA drivers use hard coded offsets. If no ISA bus exists nothing
  42. * is mapped on the first 64K of IO space
  43. */
  44. unsigned long pci_io_base = ISA_IO_BASE;
  45. EXPORT_SYMBOL(pci_io_base);
  46. LIST_HEAD(hose_list);
  47. static struct dma_mapping_ops *pci_dma_ops;
  48. void set_pci_dma_ops(struct dma_mapping_ops *dma_ops)
  49. {
  50. pci_dma_ops = dma_ops;
  51. }
  52. struct dma_mapping_ops *get_pci_dma_ops(void)
  53. {
  54. return pci_dma_ops;
  55. }
  56. EXPORT_SYMBOL(get_pci_dma_ops);
  57. int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  58. {
  59. return dma_set_mask(&dev->dev, mask);
  60. }
  61. int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  62. {
  63. int rc;
  64. rc = dma_set_mask(&dev->dev, mask);
  65. dev->dev.coherent_dma_mask = dev->dma_mask;
  66. return rc;
  67. }
  68. static void fixup_broken_pcnet32(struct pci_dev* dev)
  69. {
  70. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  71. dev->vendor = PCI_VENDOR_ID_AMD;
  72. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  73. }
  74. }
  75. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  76. /*
  77. * We need to avoid collisions with `mirrored' VGA ports
  78. * and other strange ISA hardware, so we always want the
  79. * addresses to be allocated in the 0x000-0x0ff region
  80. * modulo 0x400.
  81. *
  82. * Why? Because some silly external IO cards only decode
  83. * the low 10 bits of the IO address. The 0x00-0xff region
  84. * is reserved for motherboard devices that decode all 16
  85. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  86. * but we want to try to avoid allocating at 0x2900-0x2bff
  87. * which might have be mirrored at 0x0100-0x03ff..
  88. */
  89. void pcibios_align_resource(void *data, struct resource *res,
  90. resource_size_t size, resource_size_t align)
  91. {
  92. struct pci_dev *dev = data;
  93. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  94. resource_size_t start = res->start;
  95. unsigned long alignto;
  96. if (res->flags & IORESOURCE_IO) {
  97. unsigned long offset = (unsigned long)hose->io_base_virt -
  98. _IO_BASE;
  99. /* Make sure we start at our min on all hoses */
  100. if (start - offset < PCIBIOS_MIN_IO)
  101. start = PCIBIOS_MIN_IO + offset;
  102. /*
  103. * Put everything into 0x00-0xff region modulo 0x400
  104. */
  105. if (start & 0x300)
  106. start = (start + 0x3ff) & ~0x3ff;
  107. } else if (res->flags & IORESOURCE_MEM) {
  108. /* Make sure we start at our min on all hoses */
  109. if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
  110. start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
  111. /* Align to multiple of size of minimum base. */
  112. alignto = max(0x1000UL, align);
  113. start = ALIGN(start, alignto);
  114. }
  115. res->start = start;
  116. }
  117. void __devinit pcibios_claim_one_bus(struct pci_bus *b)
  118. {
  119. struct pci_dev *dev;
  120. struct pci_bus *child_bus;
  121. list_for_each_entry(dev, &b->devices, bus_list) {
  122. int i;
  123. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  124. struct resource *r = &dev->resource[i];
  125. if (r->parent || !r->start || !r->flags)
  126. continue;
  127. pci_claim_resource(dev, i);
  128. }
  129. }
  130. list_for_each_entry(child_bus, &b->children, node)
  131. pcibios_claim_one_bus(child_bus);
  132. }
  133. #ifdef CONFIG_HOTPLUG
  134. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  135. #endif
  136. static void __init pcibios_claim_of_setup(void)
  137. {
  138. struct pci_bus *b;
  139. list_for_each_entry(b, &pci_root_buses, node)
  140. pcibios_claim_one_bus(b);
  141. }
  142. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  143. {
  144. const u32 *prop;
  145. int len;
  146. prop = of_get_property(np, name, &len);
  147. if (prop && len >= 4)
  148. return *prop;
  149. return def;
  150. }
  151. static unsigned int pci_parse_of_flags(u32 addr0)
  152. {
  153. unsigned int flags = 0;
  154. if (addr0 & 0x02000000) {
  155. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  156. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  157. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  158. if (addr0 & 0x40000000)
  159. flags |= IORESOURCE_PREFETCH
  160. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  161. } else if (addr0 & 0x01000000)
  162. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  163. return flags;
  164. }
  165. static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
  166. {
  167. u64 base, size;
  168. unsigned int flags;
  169. struct resource *res;
  170. const u32 *addrs;
  171. u32 i;
  172. int proplen;
  173. addrs = of_get_property(node, "assigned-addresses", &proplen);
  174. if (!addrs)
  175. return;
  176. DBG(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  177. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  178. flags = pci_parse_of_flags(addrs[0]);
  179. if (!flags)
  180. continue;
  181. base = of_read_number(&addrs[1], 2);
  182. size = of_read_number(&addrs[3], 2);
  183. if (!size)
  184. continue;
  185. i = addrs[0] & 0xff;
  186. DBG(" base: %llx, size: %llx, i: %x\n",
  187. (unsigned long long)base, (unsigned long long)size, i);
  188. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  189. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  190. } else if (i == dev->rom_base_reg) {
  191. res = &dev->resource[PCI_ROM_RESOURCE];
  192. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  193. } else {
  194. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  195. continue;
  196. }
  197. res->start = base;
  198. res->end = base + size - 1;
  199. res->flags = flags;
  200. res->name = pci_name(dev);
  201. }
  202. }
  203. struct pci_dev *of_create_pci_dev(struct device_node *node,
  204. struct pci_bus *bus, int devfn)
  205. {
  206. struct pci_dev *dev;
  207. const char *type;
  208. dev = alloc_pci_dev();
  209. if (!dev)
  210. return NULL;
  211. type = of_get_property(node, "device_type", NULL);
  212. if (type == NULL)
  213. type = "";
  214. DBG(" create device, devfn: %x, type: %s\n", devfn, type);
  215. dev->bus = bus;
  216. dev->sysdata = node;
  217. dev->dev.parent = bus->bridge;
  218. dev->dev.bus = &pci_bus_type;
  219. dev->devfn = devfn;
  220. dev->multifunction = 0; /* maybe a lie? */
  221. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  222. dev->device = get_int_prop(node, "device-id", 0xffff);
  223. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  224. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  225. dev->cfg_size = pci_cfg_space_size(dev);
  226. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  227. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  228. dev->class = get_int_prop(node, "class-code", 0);
  229. dev->revision = get_int_prop(node, "revision-id", 0);
  230. DBG(" class: 0x%x\n", dev->class);
  231. DBG(" revision: 0x%x\n", dev->revision);
  232. dev->current_state = 4; /* unknown power state */
  233. dev->error_state = pci_channel_io_normal;
  234. dev->dma_mask = 0xffffffff;
  235. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  236. /* a PCI-PCI bridge */
  237. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  238. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  239. } else if (!strcmp(type, "cardbus")) {
  240. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  241. } else {
  242. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  243. dev->rom_base_reg = PCI_ROM_ADDRESS;
  244. /* Maybe do a default OF mapping here */
  245. dev->irq = NO_IRQ;
  246. }
  247. pci_parse_of_addrs(node, dev);
  248. DBG(" adding to system ...\n");
  249. pci_device_add(dev, bus);
  250. return dev;
  251. }
  252. EXPORT_SYMBOL(of_create_pci_dev);
  253. void __devinit of_scan_bus(struct device_node *node,
  254. struct pci_bus *bus)
  255. {
  256. struct device_node *child = NULL;
  257. const u32 *reg;
  258. int reglen, devfn;
  259. struct pci_dev *dev;
  260. DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number);
  261. /* Scan direct children */
  262. while ((child = of_get_next_child(node, child)) != NULL) {
  263. DBG(" * %s\n", child->full_name);
  264. reg = of_get_property(child, "reg", &reglen);
  265. if (reg == NULL || reglen < 20)
  266. continue;
  267. devfn = (reg[0] >> 8) & 0xff;
  268. /* create a new pci_dev for this device */
  269. dev = of_create_pci_dev(child, bus, devfn);
  270. if (!dev)
  271. continue;
  272. DBG(" dev header type: %x\n", dev->hdr_type);
  273. }
  274. /* Ally all fixups */
  275. pcibios_fixup_of_probed_bus(bus);
  276. /* Now scan child busses */
  277. list_for_each_entry(dev, &bus->devices, bus_list) {
  278. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  279. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  280. struct device_node *child = pci_device_to_OF_node(dev);
  281. if (dev)
  282. of_scan_pci_bridge(child, dev);
  283. }
  284. }
  285. }
  286. EXPORT_SYMBOL(of_scan_bus);
  287. void __devinit of_scan_pci_bridge(struct device_node *node,
  288. struct pci_dev *dev)
  289. {
  290. struct pci_bus *bus;
  291. const u32 *busrange, *ranges;
  292. int len, i, mode;
  293. struct resource *res;
  294. unsigned int flags;
  295. u64 size;
  296. DBG("of_scan_pci_bridge(%s)\n", node->full_name);
  297. /* parse bus-range property */
  298. busrange = of_get_property(node, "bus-range", &len);
  299. if (busrange == NULL || len != 8) {
  300. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  301. node->full_name);
  302. return;
  303. }
  304. ranges = of_get_property(node, "ranges", &len);
  305. if (ranges == NULL) {
  306. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  307. node->full_name);
  308. return;
  309. }
  310. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  311. if (!bus) {
  312. printk(KERN_ERR "Failed to create pci bus for %s\n",
  313. node->full_name);
  314. return;
  315. }
  316. bus->primary = dev->bus->number;
  317. bus->subordinate = busrange[1];
  318. bus->bridge_ctl = 0;
  319. bus->sysdata = node;
  320. /* parse ranges property */
  321. /* PCI #address-cells == 3 and #size-cells == 2 always */
  322. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  323. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  324. res->flags = 0;
  325. bus->resource[i] = res;
  326. ++res;
  327. }
  328. i = 1;
  329. for (; len >= 32; len -= 32, ranges += 8) {
  330. flags = pci_parse_of_flags(ranges[0]);
  331. size = of_read_number(&ranges[6], 2);
  332. if (flags == 0 || size == 0)
  333. continue;
  334. if (flags & IORESOURCE_IO) {
  335. res = bus->resource[0];
  336. if (res->flags) {
  337. printk(KERN_ERR "PCI: ignoring extra I/O range"
  338. " for bridge %s\n", node->full_name);
  339. continue;
  340. }
  341. } else {
  342. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  343. printk(KERN_ERR "PCI: too many memory ranges"
  344. " for bridge %s\n", node->full_name);
  345. continue;
  346. }
  347. res = bus->resource[i];
  348. ++i;
  349. }
  350. res->start = of_read_number(&ranges[1], 2);
  351. res->end = res->start + size - 1;
  352. res->flags = flags;
  353. }
  354. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  355. bus->number);
  356. DBG(" bus name: %s\n", bus->name);
  357. mode = PCI_PROBE_NORMAL;
  358. if (ppc_md.pci_probe_mode)
  359. mode = ppc_md.pci_probe_mode(bus);
  360. DBG(" probe mode: %d\n", mode);
  361. if (mode == PCI_PROBE_DEVTREE)
  362. of_scan_bus(node, bus);
  363. else if (mode == PCI_PROBE_NORMAL)
  364. pci_scan_child_bus(bus);
  365. }
  366. EXPORT_SYMBOL(of_scan_pci_bridge);
  367. void __devinit scan_phb(struct pci_controller *hose)
  368. {
  369. struct pci_bus *bus;
  370. struct device_node *node = hose->dn;
  371. int i, mode;
  372. struct resource *res;
  373. DBG("Scanning PHB %s\n", node ? node->full_name : "<NO NAME>");
  374. bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node);
  375. if (bus == NULL) {
  376. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  377. hose->global_number);
  378. return;
  379. }
  380. bus->secondary = hose->first_busno;
  381. hose->bus = bus;
  382. pcibios_map_io_space(bus);
  383. bus->resource[0] = res = &hose->io_resource;
  384. if (res->flags && request_resource(&ioport_resource, res)) {
  385. printk(KERN_ERR "Failed to request PCI IO region "
  386. "on PCI domain %04x\n", hose->global_number);
  387. DBG("res->start = 0x%016lx, res->end = 0x%016lx\n",
  388. res->start, res->end);
  389. }
  390. for (i = 0; i < 3; ++i) {
  391. res = &hose->mem_resources[i];
  392. bus->resource[i+1] = res;
  393. if (res->flags && request_resource(&iomem_resource, res))
  394. printk(KERN_ERR "Failed to request PCI memory region "
  395. "on PCI domain %04x\n", hose->global_number);
  396. }
  397. mode = PCI_PROBE_NORMAL;
  398. if (node && ppc_md.pci_probe_mode)
  399. mode = ppc_md.pci_probe_mode(bus);
  400. DBG(" probe mode: %d\n", mode);
  401. if (mode == PCI_PROBE_DEVTREE) {
  402. bus->subordinate = hose->last_busno;
  403. of_scan_bus(node, bus);
  404. }
  405. if (mode == PCI_PROBE_NORMAL)
  406. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  407. }
  408. static int __init pcibios_init(void)
  409. {
  410. struct pci_controller *hose, *tmp;
  411. /* For now, override phys_mem_access_prot. If we need it,
  412. * later, we may move that initialization to each ppc_md
  413. */
  414. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  415. printk(KERN_DEBUG "PCI: Probing PCI hardware\n");
  416. /* Scan all of the recorded PCI controllers. */
  417. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  418. scan_phb(hose);
  419. pci_bus_add_devices(hose->bus);
  420. }
  421. if (pci_probe_only)
  422. pcibios_claim_of_setup();
  423. else
  424. /* FIXME: `else' will be removed when
  425. pci_assign_unassigned_resources() is able to work
  426. correctly with [partially] allocated PCI tree. */
  427. pci_assign_unassigned_resources();
  428. /* Call machine dependent final fixup */
  429. if (ppc_md.pcibios_fixup)
  430. ppc_md.pcibios_fixup();
  431. printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
  432. return 0;
  433. }
  434. subsys_initcall(pcibios_init);
  435. int pcibios_enable_device(struct pci_dev *dev, int mask)
  436. {
  437. u16 cmd, oldcmd;
  438. int i;
  439. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  440. oldcmd = cmd;
  441. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  442. struct resource *res = &dev->resource[i];
  443. /* Only set up the requested stuff */
  444. if (!(mask & (1<<i)))
  445. continue;
  446. if (res->flags & IORESOURCE_IO)
  447. cmd |= PCI_COMMAND_IO;
  448. if (res->flags & IORESOURCE_MEM)
  449. cmd |= PCI_COMMAND_MEMORY;
  450. }
  451. if (cmd != oldcmd) {
  452. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  453. pci_name(dev), cmd);
  454. /* Enable the appropriate bits in the PCI command register. */
  455. pci_write_config_word(dev, PCI_COMMAND, cmd);
  456. }
  457. return 0;
  458. }
  459. #ifdef CONFIG_HOTPLUG
  460. int pcibios_unmap_io_space(struct pci_bus *bus)
  461. {
  462. struct pci_controller *hose;
  463. WARN_ON(bus == NULL);
  464. /* If this is not a PHB, we only flush the hash table over
  465. * the area mapped by this bridge. We don't play with the PTE
  466. * mappings since we might have to deal with sub-page alignemnts
  467. * so flushing the hash table is the only sane way to make sure
  468. * that no hash entries are covering that removed bridge area
  469. * while still allowing other busses overlapping those pages
  470. */
  471. if (bus->self) {
  472. struct resource *res = bus->resource[0];
  473. DBG("IO unmapping for PCI-PCI bridge %s\n",
  474. pci_name(bus->self));
  475. __flush_hash_table_range(&init_mm, res->start + _IO_BASE,
  476. res->end - res->start + 1);
  477. return 0;
  478. }
  479. /* Get the host bridge */
  480. hose = pci_bus_to_host(bus);
  481. /* Check if we have IOs allocated */
  482. if (hose->io_base_alloc == 0)
  483. return 0;
  484. DBG("IO unmapping for PHB %s\n", hose->dn->full_name);
  485. DBG(" alloc=0x%p\n", hose->io_base_alloc);
  486. /* This is a PHB, we fully unmap the IO area */
  487. vunmap(hose->io_base_alloc);
  488. return 0;
  489. }
  490. EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
  491. #endif /* CONFIG_HOTPLUG */
  492. int __devinit pcibios_map_io_space(struct pci_bus *bus)
  493. {
  494. struct vm_struct *area;
  495. unsigned long phys_page;
  496. unsigned long size_page;
  497. unsigned long io_virt_offset;
  498. struct pci_controller *hose;
  499. WARN_ON(bus == NULL);
  500. /* If this not a PHB, nothing to do, page tables still exist and
  501. * thus HPTEs will be faulted in when needed
  502. */
  503. if (bus->self) {
  504. DBG("IO mapping for PCI-PCI bridge %s\n",
  505. pci_name(bus->self));
  506. DBG(" virt=0x%016lx...0x%016lx\n",
  507. bus->resource[0]->start + _IO_BASE,
  508. bus->resource[0]->end + _IO_BASE);
  509. return 0;
  510. }
  511. /* Get the host bridge */
  512. hose = pci_bus_to_host(bus);
  513. phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
  514. size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE);
  515. /* Make sure IO area address is clear */
  516. hose->io_base_alloc = NULL;
  517. /* If there's no IO to map on that bus, get away too */
  518. if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
  519. return 0;
  520. /* Let's allocate some IO space for that guy. We don't pass
  521. * VM_IOREMAP because we don't care about alignment tricks that
  522. * the core does in that case. Maybe we should due to stupid card
  523. * with incomplete address decoding but I'd rather not deal with
  524. * those outside of the reserved 64K legacy region.
  525. */
  526. area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
  527. if (area == NULL)
  528. return -ENOMEM;
  529. hose->io_base_alloc = area->addr;
  530. hose->io_base_virt = (void __iomem *)(area->addr +
  531. hose->io_base_phys - phys_page);
  532. DBG("IO mapping for PHB %s\n", hose->dn->full_name);
  533. DBG(" phys=0x%016lx, virt=0x%p (alloc=0x%p)\n",
  534. hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
  535. DBG(" size=0x%016lx (alloc=0x%016lx)\n",
  536. hose->pci_io_size, size_page);
  537. /* Establish the mapping */
  538. if (__ioremap_at(phys_page, area->addr, size_page,
  539. _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)
  540. return -ENOMEM;
  541. /* Fixup hose IO resource */
  542. io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  543. hose->io_resource.start += io_virt_offset;
  544. hose->io_resource.end += io_virt_offset;
  545. DBG(" hose->io_resource=0x%016lx...0x%016lx\n",
  546. hose->io_resource.start, hose->io_resource.end);
  547. return 0;
  548. }
  549. EXPORT_SYMBOL_GPL(pcibios_map_io_space);
  550. void __devinit pcibios_setup_new_device(struct pci_dev *dev)
  551. {
  552. struct dev_archdata *sd = &dev->dev.archdata;
  553. sd->of_node = pci_device_to_OF_node(dev);
  554. DBG("PCI: device %s OF node: %s\n", pci_name(dev),
  555. sd->of_node ? sd->of_node->full_name : "<none>");
  556. sd->dma_ops = pci_dma_ops;
  557. #ifdef CONFIG_NUMA
  558. sd->numa_node = pcibus_to_node(dev->bus);
  559. #else
  560. sd->numa_node = -1;
  561. #endif
  562. if (ppc_md.pci_dma_dev_setup)
  563. ppc_md.pci_dma_dev_setup(dev);
  564. }
  565. EXPORT_SYMBOL(pcibios_setup_new_device);
  566. void __devinit pcibios_do_bus_setup(struct pci_bus *bus)
  567. {
  568. struct pci_dev *dev;
  569. if (ppc_md.pci_dma_bus_setup)
  570. ppc_md.pci_dma_bus_setup(bus);
  571. list_for_each_entry(dev, &bus->devices, bus_list)
  572. pcibios_setup_new_device(dev);
  573. }
  574. unsigned long pci_address_to_pio(phys_addr_t address)
  575. {
  576. struct pci_controller *hose, *tmp;
  577. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  578. if (address >= hose->io_base_phys &&
  579. address < (hose->io_base_phys + hose->pci_io_size)) {
  580. unsigned long base =
  581. (unsigned long)hose->io_base_virt - _IO_BASE;
  582. return base + (address - hose->io_base_phys);
  583. }
  584. }
  585. return (unsigned int)-1;
  586. }
  587. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  588. #define IOBASE_BRIDGE_NUMBER 0
  589. #define IOBASE_MEMORY 1
  590. #define IOBASE_IO 2
  591. #define IOBASE_ISA_IO 3
  592. #define IOBASE_ISA_MEM 4
  593. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  594. unsigned long in_devfn)
  595. {
  596. struct pci_controller* hose;
  597. struct list_head *ln;
  598. struct pci_bus *bus = NULL;
  599. struct device_node *hose_node;
  600. /* Argh ! Please forgive me for that hack, but that's the
  601. * simplest way to get existing XFree to not lockup on some
  602. * G5 machines... So when something asks for bus 0 io base
  603. * (bus 0 is HT root), we return the AGP one instead.
  604. */
  605. if (machine_is_compatible("MacRISC4"))
  606. if (in_bus == 0)
  607. in_bus = 0xf0;
  608. /* That syscall isn't quite compatible with PCI domains, but it's
  609. * used on pre-domains setup. We return the first match
  610. */
  611. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  612. bus = pci_bus_b(ln);
  613. if (in_bus >= bus->number && in_bus <= bus->subordinate)
  614. break;
  615. bus = NULL;
  616. }
  617. if (bus == NULL || bus->sysdata == NULL)
  618. return -ENODEV;
  619. hose_node = (struct device_node *)bus->sysdata;
  620. hose = PCI_DN(hose_node)->phb;
  621. switch (which) {
  622. case IOBASE_BRIDGE_NUMBER:
  623. return (long)hose->first_busno;
  624. case IOBASE_MEMORY:
  625. return (long)hose->pci_mem_offset;
  626. case IOBASE_IO:
  627. return (long)hose->io_base_phys;
  628. case IOBASE_ISA_IO:
  629. return (long)isa_io_base;
  630. case IOBASE_ISA_MEM:
  631. return -EINVAL;
  632. }
  633. return -EOPNOTSUPP;
  634. }
  635. #ifdef CONFIG_NUMA
  636. int pcibus_to_node(struct pci_bus *bus)
  637. {
  638. struct pci_controller *phb = pci_bus_to_host(bus);
  639. return phb->node;
  640. }
  641. EXPORT_SYMBOL(pcibus_to_node);
  642. #endif