pci_64.c 23 KB

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