pci_64.c 22 KB

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