pci_64.c 26 KB

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