pci_64.c 23 KB

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