pci_64.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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. /* XXX kill that some day ... */
  53. int global_phb_number; /* Global phb counter */
  54. void set_pci_dma_ops(struct dma_mapping_ops *dma_ops)
  55. {
  56. pci_dma_ops = dma_ops;
  57. }
  58. struct dma_mapping_ops *get_pci_dma_ops(void)
  59. {
  60. return pci_dma_ops;
  61. }
  62. EXPORT_SYMBOL(get_pci_dma_ops);
  63. static void fixup_broken_pcnet32(struct pci_dev* dev)
  64. {
  65. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  66. dev->vendor = PCI_VENDOR_ID_AMD;
  67. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  68. }
  69. }
  70. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  71. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  72. struct resource *res)
  73. {
  74. unsigned long offset = 0;
  75. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  76. if (!hose)
  77. return;
  78. if (res->flags & IORESOURCE_IO)
  79. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  80. if (res->flags & IORESOURCE_MEM)
  81. offset = hose->pci_mem_offset;
  82. region->start = res->start - offset;
  83. region->end = res->end - offset;
  84. }
  85. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  86. struct pci_bus_region *region)
  87. {
  88. unsigned long offset = 0;
  89. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  90. if (!hose)
  91. return;
  92. if (res->flags & IORESOURCE_IO)
  93. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  94. if (res->flags & IORESOURCE_MEM)
  95. offset = hose->pci_mem_offset;
  96. res->start = region->start + offset;
  97. res->end = region->end + offset;
  98. }
  99. #ifdef CONFIG_HOTPLUG
  100. EXPORT_SYMBOL(pcibios_resource_to_bus);
  101. EXPORT_SYMBOL(pcibios_bus_to_resource);
  102. #endif
  103. /*
  104. * We need to avoid collisions with `mirrored' VGA ports
  105. * and other strange ISA hardware, so we always want the
  106. * addresses to be allocated in the 0x000-0x0ff region
  107. * modulo 0x400.
  108. *
  109. * Why? Because some silly external IO cards only decode
  110. * the low 10 bits of the IO address. The 0x00-0xff region
  111. * is reserved for motherboard devices that decode all 16
  112. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  113. * but we want to try to avoid allocating at 0x2900-0x2bff
  114. * which might have be mirrored at 0x0100-0x03ff..
  115. */
  116. void pcibios_align_resource(void *data, struct resource *res,
  117. resource_size_t size, resource_size_t align)
  118. {
  119. struct pci_dev *dev = data;
  120. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  121. resource_size_t start = res->start;
  122. unsigned long alignto;
  123. if (res->flags & IORESOURCE_IO) {
  124. unsigned long offset = (unsigned long)hose->io_base_virt -
  125. _IO_BASE;
  126. /* Make sure we start at our min on all hoses */
  127. if (start - offset < PCIBIOS_MIN_IO)
  128. start = PCIBIOS_MIN_IO + offset;
  129. /*
  130. * Put everything into 0x00-0xff region modulo 0x400
  131. */
  132. if (start & 0x300)
  133. start = (start + 0x3ff) & ~0x3ff;
  134. } else if (res->flags & IORESOURCE_MEM) {
  135. /* Make sure we start at our min on all hoses */
  136. if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
  137. start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
  138. /* Align to multiple of size of minimum base. */
  139. alignto = max(0x1000UL, align);
  140. start = ALIGN(start, alignto);
  141. }
  142. res->start = start;
  143. }
  144. static DEFINE_SPINLOCK(hose_spinlock);
  145. /*
  146. * pci_controller(phb) initialized common variables.
  147. */
  148. static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
  149. {
  150. memset(hose, 0, sizeof(struct pci_controller));
  151. spin_lock(&hose_spinlock);
  152. hose->global_number = global_phb_number++;
  153. list_add_tail(&hose->list_node, &hose_list);
  154. spin_unlock(&hose_spinlock);
  155. }
  156. struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
  157. {
  158. struct pci_controller *phb;
  159. if (mem_init_done)
  160. phb = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  161. else
  162. phb = alloc_bootmem(sizeof (struct pci_controller));
  163. if (phb == NULL)
  164. return NULL;
  165. pci_setup_pci_controller(phb);
  166. phb->arch_data = dev;
  167. phb->is_dynamic = mem_init_done;
  168. if (dev) {
  169. int nid = of_node_to_nid(dev);
  170. if (nid < 0 || !node_online(nid))
  171. nid = -1;
  172. PHB_SET_NODE(phb, nid);
  173. }
  174. return phb;
  175. }
  176. void pcibios_free_controller(struct pci_controller *phb)
  177. {
  178. spin_lock(&hose_spinlock);
  179. list_del(&phb->list_node);
  180. spin_unlock(&hose_spinlock);
  181. if (phb->is_dynamic)
  182. kfree(phb);
  183. }
  184. void __devinit pcibios_claim_one_bus(struct pci_bus *b)
  185. {
  186. struct pci_dev *dev;
  187. struct pci_bus *child_bus;
  188. list_for_each_entry(dev, &b->devices, bus_list) {
  189. int i;
  190. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  191. struct resource *r = &dev->resource[i];
  192. if (r->parent || !r->start || !r->flags)
  193. continue;
  194. pci_claim_resource(dev, i);
  195. }
  196. }
  197. list_for_each_entry(child_bus, &b->children, node)
  198. pcibios_claim_one_bus(child_bus);
  199. }
  200. #ifdef CONFIG_HOTPLUG
  201. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  202. #endif
  203. static void __init pcibios_claim_of_setup(void)
  204. {
  205. struct pci_bus *b;
  206. if (firmware_has_feature(FW_FEATURE_ISERIES))
  207. return;
  208. list_for_each_entry(b, &pci_root_buses, node)
  209. pcibios_claim_one_bus(b);
  210. }
  211. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  212. {
  213. const u32 *prop;
  214. int len;
  215. prop = of_get_property(np, name, &len);
  216. if (prop && len >= 4)
  217. return *prop;
  218. return def;
  219. }
  220. static unsigned int pci_parse_of_flags(u32 addr0)
  221. {
  222. unsigned int flags = 0;
  223. if (addr0 & 0x02000000) {
  224. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  225. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  226. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  227. if (addr0 & 0x40000000)
  228. flags |= IORESOURCE_PREFETCH
  229. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  230. } else if (addr0 & 0x01000000)
  231. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  232. return flags;
  233. }
  234. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  235. static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
  236. {
  237. u64 base, size;
  238. unsigned int flags;
  239. struct resource *res;
  240. const u32 *addrs;
  241. u32 i;
  242. int proplen;
  243. addrs = of_get_property(node, "assigned-addresses", &proplen);
  244. if (!addrs)
  245. return;
  246. DBG(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  247. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  248. flags = pci_parse_of_flags(addrs[0]);
  249. if (!flags)
  250. continue;
  251. base = GET_64BIT(addrs, 1);
  252. size = GET_64BIT(addrs, 3);
  253. if (!size)
  254. continue;
  255. i = addrs[0] & 0xff;
  256. DBG(" base: %llx, size: %llx, i: %x\n",
  257. (unsigned long long)base, (unsigned long long)size, i);
  258. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  259. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  260. } else if (i == dev->rom_base_reg) {
  261. res = &dev->resource[PCI_ROM_RESOURCE];
  262. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  263. } else {
  264. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  265. continue;
  266. }
  267. res->start = base;
  268. res->end = base + size - 1;
  269. res->flags = flags;
  270. res->name = pci_name(dev);
  271. fixup_resource(res, dev);
  272. }
  273. }
  274. struct pci_dev *of_create_pci_dev(struct device_node *node,
  275. struct pci_bus *bus, int devfn)
  276. {
  277. struct pci_dev *dev;
  278. const char *type;
  279. dev = alloc_pci_dev();
  280. if (!dev)
  281. return NULL;
  282. type = of_get_property(node, "device_type", NULL);
  283. if (type == NULL)
  284. type = "";
  285. DBG(" create device, devfn: %x, type: %s\n", devfn, type);
  286. dev->bus = bus;
  287. dev->sysdata = node;
  288. dev->dev.parent = bus->bridge;
  289. dev->dev.bus = &pci_bus_type;
  290. dev->devfn = devfn;
  291. dev->multifunction = 0; /* maybe a lie? */
  292. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  293. dev->device = get_int_prop(node, "device-id", 0xffff);
  294. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  295. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  296. dev->cfg_size = pci_cfg_space_size(dev);
  297. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  298. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  299. dev->class = get_int_prop(node, "class-code", 0);
  300. DBG(" class: 0x%x\n", dev->class);
  301. dev->current_state = 4; /* unknown power state */
  302. dev->error_state = pci_channel_io_normal;
  303. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  304. /* a PCI-PCI bridge */
  305. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  306. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  307. } else if (!strcmp(type, "cardbus")) {
  308. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  309. } else {
  310. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  311. dev->rom_base_reg = PCI_ROM_ADDRESS;
  312. /* Maybe do a default OF mapping here */
  313. dev->irq = NO_IRQ;
  314. }
  315. pci_parse_of_addrs(node, dev);
  316. DBG(" adding to system ...\n");
  317. pci_device_add(dev, bus);
  318. return dev;
  319. }
  320. EXPORT_SYMBOL(of_create_pci_dev);
  321. void __devinit of_scan_bus(struct device_node *node,
  322. struct pci_bus *bus)
  323. {
  324. struct device_node *child = NULL;
  325. const u32 *reg;
  326. int reglen, devfn;
  327. struct pci_dev *dev;
  328. DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number);
  329. while ((child = of_get_next_child(node, child)) != NULL) {
  330. DBG(" * %s\n", child->full_name);
  331. reg = of_get_property(child, "reg", &reglen);
  332. if (reg == NULL || reglen < 20)
  333. continue;
  334. devfn = (reg[0] >> 8) & 0xff;
  335. /* create a new pci_dev for this device */
  336. dev = of_create_pci_dev(child, bus, devfn);
  337. if (!dev)
  338. continue;
  339. DBG("dev header type: %x\n", dev->hdr_type);
  340. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  341. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  342. of_scan_pci_bridge(child, dev);
  343. }
  344. do_bus_setup(bus);
  345. }
  346. EXPORT_SYMBOL(of_scan_bus);
  347. void __devinit of_scan_pci_bridge(struct device_node *node,
  348. struct pci_dev *dev)
  349. {
  350. struct pci_bus *bus;
  351. const u32 *busrange, *ranges;
  352. int len, i, mode;
  353. struct resource *res;
  354. unsigned int flags;
  355. u64 size;
  356. DBG("of_scan_pci_bridge(%s)\n", node->full_name);
  357. /* parse bus-range property */
  358. busrange = of_get_property(node, "bus-range", &len);
  359. if (busrange == NULL || len != 8) {
  360. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  361. node->full_name);
  362. return;
  363. }
  364. ranges = of_get_property(node, "ranges", &len);
  365. if (ranges == NULL) {
  366. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  367. node->full_name);
  368. return;
  369. }
  370. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  371. if (!bus) {
  372. printk(KERN_ERR "Failed to create pci bus for %s\n",
  373. node->full_name);
  374. return;
  375. }
  376. bus->primary = dev->bus->number;
  377. bus->subordinate = busrange[1];
  378. bus->bridge_ctl = 0;
  379. bus->sysdata = node;
  380. /* parse ranges property */
  381. /* PCI #address-cells == 3 and #size-cells == 2 always */
  382. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  383. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  384. res->flags = 0;
  385. bus->resource[i] = res;
  386. ++res;
  387. }
  388. i = 1;
  389. for (; len >= 32; len -= 32, ranges += 8) {
  390. flags = pci_parse_of_flags(ranges[0]);
  391. size = GET_64BIT(ranges, 6);
  392. if (flags == 0 || size == 0)
  393. continue;
  394. if (flags & IORESOURCE_IO) {
  395. res = bus->resource[0];
  396. if (res->flags) {
  397. printk(KERN_ERR "PCI: ignoring extra I/O range"
  398. " for bridge %s\n", node->full_name);
  399. continue;
  400. }
  401. } else {
  402. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  403. printk(KERN_ERR "PCI: too many memory ranges"
  404. " for bridge %s\n", node->full_name);
  405. continue;
  406. }
  407. res = bus->resource[i];
  408. ++i;
  409. }
  410. res->start = GET_64BIT(ranges, 1);
  411. res->end = res->start + size - 1;
  412. res->flags = flags;
  413. fixup_resource(res, dev);
  414. }
  415. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  416. bus->number);
  417. DBG(" bus name: %s\n", bus->name);
  418. mode = PCI_PROBE_NORMAL;
  419. if (ppc_md.pci_probe_mode)
  420. mode = ppc_md.pci_probe_mode(bus);
  421. DBG(" probe mode: %d\n", mode);
  422. if (mode == PCI_PROBE_DEVTREE)
  423. of_scan_bus(node, bus);
  424. else if (mode == PCI_PROBE_NORMAL)
  425. pci_scan_child_bus(bus);
  426. }
  427. EXPORT_SYMBOL(of_scan_pci_bridge);
  428. void __devinit scan_phb(struct pci_controller *hose)
  429. {
  430. struct pci_bus *bus;
  431. struct device_node *node = hose->arch_data;
  432. int i, mode;
  433. struct resource *res;
  434. DBG("Scanning PHB %s\n", node ? node->full_name : "<NO NAME>");
  435. bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node);
  436. if (bus == NULL) {
  437. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  438. hose->global_number);
  439. return;
  440. }
  441. bus->secondary = hose->first_busno;
  442. hose->bus = bus;
  443. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  444. pcibios_map_io_space(bus);
  445. bus->resource[0] = res = &hose->io_resource;
  446. if (res->flags && request_resource(&ioport_resource, res)) {
  447. printk(KERN_ERR "Failed to request PCI IO region "
  448. "on PCI domain %04x\n", hose->global_number);
  449. DBG("res->start = 0x%016lx, res->end = 0x%016lx\n",
  450. res->start, res->end);
  451. }
  452. for (i = 0; i < 3; ++i) {
  453. res = &hose->mem_resources[i];
  454. bus->resource[i+1] = res;
  455. if (res->flags && request_resource(&iomem_resource, res))
  456. printk(KERN_ERR "Failed to request PCI memory region "
  457. "on PCI domain %04x\n", hose->global_number);
  458. }
  459. mode = PCI_PROBE_NORMAL;
  460. if (node && ppc_md.pci_probe_mode)
  461. mode = ppc_md.pci_probe_mode(bus);
  462. DBG(" probe mode: %d\n", mode);
  463. if (mode == PCI_PROBE_DEVTREE) {
  464. bus->subordinate = hose->last_busno;
  465. of_scan_bus(node, bus);
  466. }
  467. if (mode == PCI_PROBE_NORMAL)
  468. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  469. }
  470. static int __init pcibios_init(void)
  471. {
  472. struct pci_controller *hose, *tmp;
  473. /* For now, override phys_mem_access_prot. If we need it,
  474. * later, we may move that initialization to each ppc_md
  475. */
  476. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  477. if (firmware_has_feature(FW_FEATURE_ISERIES))
  478. iSeries_pcibios_init();
  479. printk(KERN_DEBUG "PCI: Probing PCI hardware\n");
  480. /* Scan all of the recorded PCI controllers. */
  481. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  482. scan_phb(hose);
  483. pci_bus_add_devices(hose->bus);
  484. }
  485. if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
  486. if (pci_probe_only)
  487. pcibios_claim_of_setup();
  488. else
  489. /* FIXME: `else' will be removed when
  490. pci_assign_unassigned_resources() is able to work
  491. correctly with [partially] allocated PCI tree. */
  492. pci_assign_unassigned_resources();
  493. }
  494. /* Call machine dependent final fixup */
  495. if (ppc_md.pcibios_fixup)
  496. ppc_md.pcibios_fixup();
  497. printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
  498. return 0;
  499. }
  500. subsys_initcall(pcibios_init);
  501. char __init *pcibios_setup(char *str)
  502. {
  503. return str;
  504. }
  505. int pcibios_enable_device(struct pci_dev *dev, int mask)
  506. {
  507. u16 cmd, oldcmd;
  508. int i;
  509. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  510. oldcmd = cmd;
  511. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  512. struct resource *res = &dev->resource[i];
  513. /* Only set up the requested stuff */
  514. if (!(mask & (1<<i)))
  515. continue;
  516. if (res->flags & IORESOURCE_IO)
  517. cmd |= PCI_COMMAND_IO;
  518. if (res->flags & IORESOURCE_MEM)
  519. cmd |= PCI_COMMAND_MEMORY;
  520. }
  521. if (cmd != oldcmd) {
  522. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  523. pci_name(dev), cmd);
  524. /* Enable the appropriate bits in the PCI command register. */
  525. pci_write_config_word(dev, PCI_COMMAND, cmd);
  526. }
  527. return 0;
  528. }
  529. /* Decide whether to display the domain number in /proc */
  530. int pci_proc_domain(struct pci_bus *bus)
  531. {
  532. if (firmware_has_feature(FW_FEATURE_ISERIES))
  533. return 0;
  534. else {
  535. struct pci_controller *hose = pci_bus_to_host(bus);
  536. return hose->buid;
  537. }
  538. }
  539. /*
  540. * Platform support for /proc/bus/pci/X/Y mmap()s,
  541. * modelled on the sparc64 implementation by Dave Miller.
  542. * -- paulus.
  543. */
  544. /*
  545. * Adjust vm_pgoff of VMA such that it is the physical page offset
  546. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  547. *
  548. * Basically, the user finds the base address for his device which he wishes
  549. * to mmap. They read the 32-bit value from the config space base register,
  550. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  551. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  552. *
  553. * Returns negative error code on failure, zero on success.
  554. */
  555. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  556. resource_size_t *offset,
  557. enum pci_mmap_state mmap_state)
  558. {
  559. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  560. unsigned long io_offset = 0;
  561. int i, res_bit;
  562. if (hose == 0)
  563. return NULL; /* should never happen */
  564. /* If memory, add on the PCI bridge address offset */
  565. if (mmap_state == pci_mmap_mem) {
  566. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  567. *offset += hose->pci_mem_offset;
  568. #endif
  569. res_bit = IORESOURCE_MEM;
  570. } else {
  571. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  572. *offset += io_offset;
  573. res_bit = IORESOURCE_IO;
  574. }
  575. /*
  576. * Check that the offset requested corresponds to one of the
  577. * resources of the device.
  578. */
  579. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  580. struct resource *rp = &dev->resource[i];
  581. int flags = rp->flags;
  582. /* treat ROM as memory (should be already) */
  583. if (i == PCI_ROM_RESOURCE)
  584. flags |= IORESOURCE_MEM;
  585. /* Active and same type? */
  586. if ((flags & res_bit) == 0)
  587. continue;
  588. /* In the range of this resource? */
  589. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  590. continue;
  591. /* found it! construct the final physical address */
  592. if (mmap_state == pci_mmap_io)
  593. *offset += hose->io_base_phys - io_offset;
  594. return rp;
  595. }
  596. return NULL;
  597. }
  598. /*
  599. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  600. * device mapping.
  601. */
  602. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  603. pgprot_t protection,
  604. enum pci_mmap_state mmap_state,
  605. int write_combine)
  606. {
  607. unsigned long prot = pgprot_val(protection);
  608. /* Write combine is always 0 on non-memory space mappings. On
  609. * memory space, if the user didn't pass 1, we check for a
  610. * "prefetchable" resource. This is a bit hackish, but we use
  611. * this to workaround the inability of /sysfs to provide a write
  612. * combine bit
  613. */
  614. if (mmap_state != pci_mmap_mem)
  615. write_combine = 0;
  616. else if (write_combine == 0) {
  617. if (rp->flags & IORESOURCE_PREFETCH)
  618. write_combine = 1;
  619. }
  620. /* XXX would be nice to have a way to ask for write-through */
  621. prot |= _PAGE_NO_CACHE;
  622. if (write_combine)
  623. prot &= ~_PAGE_GUARDED;
  624. else
  625. prot |= _PAGE_GUARDED;
  626. return __pgprot(prot);
  627. }
  628. /*
  629. * This one is used by /dev/mem and fbdev who have no clue about the
  630. * PCI device, it tries to find the PCI device first and calls the
  631. * above routine
  632. */
  633. pgprot_t pci_phys_mem_access_prot(struct file *file,
  634. unsigned long pfn,
  635. unsigned long size,
  636. pgprot_t protection)
  637. {
  638. struct pci_dev *pdev = NULL;
  639. struct resource *found = NULL;
  640. unsigned long prot = pgprot_val(protection);
  641. unsigned long offset = pfn << PAGE_SHIFT;
  642. int i;
  643. if (page_is_ram(pfn))
  644. return __pgprot(prot);
  645. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  646. for_each_pci_dev(pdev) {
  647. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  648. struct resource *rp = &pdev->resource[i];
  649. int flags = rp->flags;
  650. /* Active and same type? */
  651. if ((flags & IORESOURCE_MEM) == 0)
  652. continue;
  653. /* In the range of this resource? */
  654. if (offset < (rp->start & PAGE_MASK) ||
  655. offset > rp->end)
  656. continue;
  657. found = rp;
  658. break;
  659. }
  660. if (found)
  661. break;
  662. }
  663. if (found) {
  664. if (found->flags & IORESOURCE_PREFETCH)
  665. prot &= ~_PAGE_GUARDED;
  666. pci_dev_put(pdev);
  667. }
  668. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  669. return __pgprot(prot);
  670. }
  671. /*
  672. * Perform the actual remap of the pages for a PCI device mapping, as
  673. * appropriate for this architecture. The region in the process to map
  674. * is described by vm_start and vm_end members of VMA, the base physical
  675. * address is found in vm_pgoff.
  676. * The pci device structure is provided so that architectures may make mapping
  677. * decisions on a per-device or per-bus basis.
  678. *
  679. * Returns a negative error code on failure, zero on success.
  680. */
  681. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  682. enum pci_mmap_state mmap_state, int write_combine)
  683. {
  684. resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
  685. struct resource *rp;
  686. int ret;
  687. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  688. if (rp == NULL)
  689. return -EINVAL;
  690. vma->vm_pgoff = offset >> PAGE_SHIFT;
  691. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  692. vma->vm_page_prot,
  693. mmap_state, write_combine);
  694. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  695. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  696. return ret;
  697. }
  698. static ssize_t pci_show_devspec(struct device *dev,
  699. struct device_attribute *attr, char *buf)
  700. {
  701. struct pci_dev *pdev;
  702. struct device_node *np;
  703. pdev = to_pci_dev (dev);
  704. np = pci_device_to_OF_node(pdev);
  705. if (np == NULL || np->full_name == NULL)
  706. return 0;
  707. return sprintf(buf, "%s", np->full_name);
  708. }
  709. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  710. void pcibios_add_platform_entries(struct pci_dev *pdev)
  711. {
  712. device_create_file(&pdev->dev, &dev_attr_devspec);
  713. }
  714. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  715. struct device_node *dev, int prim)
  716. {
  717. const unsigned int *ranges;
  718. unsigned int pci_space;
  719. unsigned long size;
  720. int rlen = 0;
  721. int memno = 0;
  722. struct resource *res;
  723. int np, na = of_n_addr_cells(dev);
  724. unsigned long pci_addr, cpu_phys_addr;
  725. np = na + 5;
  726. /* From "PCI Binding to 1275"
  727. * The ranges property is laid out as an array of elements,
  728. * each of which comprises:
  729. * cells 0 - 2: a PCI address
  730. * cells 3 or 3+4: a CPU physical address
  731. * (size depending on dev->n_addr_cells)
  732. * cells 4+5 or 5+6: the size of the range
  733. */
  734. ranges = of_get_property(dev, "ranges", &rlen);
  735. if (ranges == NULL)
  736. return;
  737. hose->io_base_phys = 0;
  738. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  739. res = NULL;
  740. pci_space = ranges[0];
  741. pci_addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  742. cpu_phys_addr = of_translate_address(dev, &ranges[3]);
  743. size = ((unsigned long)ranges[na+3] << 32) | ranges[na+4];
  744. ranges += np;
  745. if (size == 0)
  746. continue;
  747. /* Now consume following elements while they are contiguous */
  748. while (rlen >= np * sizeof(unsigned int)) {
  749. unsigned long addr, phys;
  750. if (ranges[0] != pci_space)
  751. break;
  752. addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  753. phys = ranges[3];
  754. if (na >= 2)
  755. phys = (phys << 32) | ranges[4];
  756. if (addr != pci_addr + size ||
  757. phys != cpu_phys_addr + size)
  758. break;
  759. size += ((unsigned long)ranges[na+3] << 32)
  760. | ranges[na+4];
  761. ranges += np;
  762. rlen -= np * sizeof(unsigned int);
  763. }
  764. switch ((pci_space >> 24) & 0x3) {
  765. case 1: /* I/O space */
  766. hose->io_base_phys = cpu_phys_addr - pci_addr;
  767. /* handle from 0 to top of I/O window */
  768. hose->pci_io_size = pci_addr + size;
  769. res = &hose->io_resource;
  770. res->flags = IORESOURCE_IO;
  771. res->start = pci_addr;
  772. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  773. res->start, res->start + size - 1);
  774. break;
  775. case 2: /* memory space */
  776. memno = 0;
  777. while (memno < 3 && hose->mem_resources[memno].flags)
  778. ++memno;
  779. if (memno == 0)
  780. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  781. if (memno < 3) {
  782. res = &hose->mem_resources[memno];
  783. res->flags = IORESOURCE_MEM;
  784. res->start = cpu_phys_addr;
  785. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  786. res->start, res->start + size - 1);
  787. }
  788. break;
  789. }
  790. if (res != NULL) {
  791. res->name = dev->full_name;
  792. res->end = res->start + size - 1;
  793. res->parent = NULL;
  794. res->sibling = NULL;
  795. res->child = NULL;
  796. }
  797. }
  798. }
  799. #ifdef CONFIG_HOTPLUG
  800. int pcibios_unmap_io_space(struct pci_bus *bus)
  801. {
  802. struct pci_controller *hose;
  803. WARN_ON(bus == NULL);
  804. /* If this is not a PHB, we only flush the hash table over
  805. * the area mapped by this bridge. We don't play with the PTE
  806. * mappings since we might have to deal with sub-page alignemnts
  807. * so flushing the hash table is the only sane way to make sure
  808. * that no hash entries are covering that removed bridge area
  809. * while still allowing other busses overlapping those pages
  810. */
  811. if (bus->self) {
  812. struct resource *res = bus->resource[0];
  813. DBG("IO unmapping for PCI-PCI bridge %s\n",
  814. pci_name(bus->self));
  815. __flush_hash_table_range(&init_mm, res->start + _IO_BASE,
  816. res->end - res->start + 1);
  817. return 0;
  818. }
  819. /* Get the host bridge */
  820. hose = pci_bus_to_host(bus);
  821. /* Check if we have IOs allocated */
  822. if (hose->io_base_alloc == 0)
  823. return 0;
  824. DBG("IO unmapping for PHB %s\n",
  825. ((struct device_node *)hose->arch_data)->full_name);
  826. DBG(" alloc=0x%p\n", hose->io_base_alloc);
  827. /* This is a PHB, we fully unmap the IO area */
  828. vunmap(hose->io_base_alloc);
  829. return 0;
  830. }
  831. EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
  832. #endif /* CONFIG_HOTPLUG */
  833. int __devinit pcibios_map_io_space(struct pci_bus *bus)
  834. {
  835. struct vm_struct *area;
  836. unsigned long phys_page;
  837. unsigned long size_page;
  838. unsigned long io_virt_offset;
  839. struct pci_controller *hose;
  840. WARN_ON(bus == NULL);
  841. /* If this not a PHB, nothing to do, page tables still exist and
  842. * thus HPTEs will be faulted in when needed
  843. */
  844. if (bus->self) {
  845. DBG("IO mapping for PCI-PCI bridge %s\n",
  846. pci_name(bus->self));
  847. DBG(" virt=0x%016lx...0x%016lx\n",
  848. bus->resource[0]->start + _IO_BASE,
  849. bus->resource[0]->end + _IO_BASE);
  850. return 0;
  851. }
  852. /* Get the host bridge */
  853. hose = pci_bus_to_host(bus);
  854. phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
  855. size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE);
  856. /* Make sure IO area address is clear */
  857. hose->io_base_alloc = NULL;
  858. /* If there's no IO to map on that bus, get away too */
  859. if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
  860. return 0;
  861. /* Let's allocate some IO space for that guy. We don't pass
  862. * VM_IOREMAP because we don't care about alignment tricks that
  863. * the core does in that case. Maybe we should due to stupid card
  864. * with incomplete address decoding but I'd rather not deal with
  865. * those outside of the reserved 64K legacy region.
  866. */
  867. area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
  868. if (area == NULL)
  869. return -ENOMEM;
  870. hose->io_base_alloc = area->addr;
  871. hose->io_base_virt = (void __iomem *)(area->addr +
  872. hose->io_base_phys - phys_page);
  873. DBG("IO mapping for PHB %s\n",
  874. ((struct device_node *)hose->arch_data)->full_name);
  875. DBG(" phys=0x%016lx, virt=0x%p (alloc=0x%p)\n",
  876. hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
  877. DBG(" size=0x%016lx (alloc=0x%016lx)\n",
  878. hose->pci_io_size, size_page);
  879. /* Establish the mapping */
  880. if (__ioremap_at(phys_page, area->addr, size_page,
  881. _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)
  882. return -ENOMEM;
  883. /* Fixup hose IO resource */
  884. io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  885. hose->io_resource.start += io_virt_offset;
  886. hose->io_resource.end += io_virt_offset;
  887. DBG(" hose->io_resource=0x%016lx...0x%016lx\n",
  888. hose->io_resource.start, hose->io_resource.end);
  889. return 0;
  890. }
  891. EXPORT_SYMBOL_GPL(pcibios_map_io_space);
  892. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  893. {
  894. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  895. unsigned long offset;
  896. if (res->flags & IORESOURCE_IO) {
  897. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  898. res->start += offset;
  899. res->end += offset;
  900. } else if (res->flags & IORESOURCE_MEM) {
  901. res->start += hose->pci_mem_offset;
  902. res->end += hose->pci_mem_offset;
  903. }
  904. }
  905. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  906. struct pci_bus *bus)
  907. {
  908. /* Update device resources. */
  909. int i;
  910. DBG("%s: Fixup resources:\n", pci_name(dev));
  911. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  912. struct resource *res = &dev->resource[i];
  913. if (!res->flags)
  914. continue;
  915. DBG(" 0x%02x < %08lx:0x%016lx...0x%016lx\n",
  916. i, res->flags, res->start, res->end);
  917. fixup_resource(res, dev);
  918. DBG(" > %08lx:0x%016lx...0x%016lx\n",
  919. res->flags, res->start, res->end);
  920. }
  921. }
  922. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  923. void __devinit pcibios_setup_new_device(struct pci_dev *dev)
  924. {
  925. struct dev_archdata *sd = &dev->dev.archdata;
  926. sd->of_node = pci_device_to_OF_node(dev);
  927. DBG("PCI device %s OF node: %s\n", pci_name(dev),
  928. sd->of_node ? sd->of_node->full_name : "<none>");
  929. sd->dma_ops = pci_dma_ops;
  930. #ifdef CONFIG_NUMA
  931. sd->numa_node = pcibus_to_node(dev->bus);
  932. #else
  933. sd->numa_node = -1;
  934. #endif
  935. if (ppc_md.pci_dma_dev_setup)
  936. ppc_md.pci_dma_dev_setup(dev);
  937. }
  938. EXPORT_SYMBOL(pcibios_setup_new_device);
  939. static void __devinit do_bus_setup(struct pci_bus *bus)
  940. {
  941. struct pci_dev *dev;
  942. if (ppc_md.pci_dma_bus_setup)
  943. ppc_md.pci_dma_bus_setup(bus);
  944. list_for_each_entry(dev, &bus->devices, bus_list)
  945. pcibios_setup_new_device(dev);
  946. /* Read default IRQs and fixup if necessary */
  947. list_for_each_entry(dev, &bus->devices, bus_list) {
  948. pci_read_irq_line(dev);
  949. if (ppc_md.pci_irq_fixup)
  950. ppc_md.pci_irq_fixup(dev);
  951. }
  952. }
  953. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  954. {
  955. struct pci_dev *dev = bus->self;
  956. struct device_node *np;
  957. np = pci_bus_to_OF_node(bus);
  958. DBG("pcibios_fixup_bus(%s)\n", np ? np->full_name : "<???>");
  959. if (dev && pci_probe_only &&
  960. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  961. /* This is a subordinate bridge */
  962. pci_read_bridge_bases(bus);
  963. pcibios_fixup_device_resources(dev, bus);
  964. }
  965. do_bus_setup(bus);
  966. if (!pci_probe_only)
  967. return;
  968. list_for_each_entry(dev, &bus->devices, bus_list)
  969. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  970. pcibios_fixup_device_resources(dev, bus);
  971. }
  972. EXPORT_SYMBOL(pcibios_fixup_bus);
  973. /*
  974. * Reads the interrupt pin to determine if interrupt is use by card.
  975. * If the interrupt is used, then gets the interrupt line from the
  976. * openfirmware and sets it in the pci_dev and pci_config line.
  977. */
  978. int pci_read_irq_line(struct pci_dev *pci_dev)
  979. {
  980. struct of_irq oirq;
  981. unsigned int virq;
  982. DBG("Try to map irq for %s...\n", pci_name(pci_dev));
  983. #ifdef DEBUG
  984. memset(&oirq, 0xff, sizeof(oirq));
  985. #endif
  986. /* Try to get a mapping from the device-tree */
  987. if (of_irq_map_pci(pci_dev, &oirq)) {
  988. u8 line, pin;
  989. /* If that fails, lets fallback to what is in the config
  990. * space and map that through the default controller. We
  991. * also set the type to level low since that's what PCI
  992. * interrupts are. If your platform does differently, then
  993. * either provide a proper interrupt tree or don't use this
  994. * function.
  995. */
  996. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  997. return -1;
  998. if (pin == 0)
  999. return -1;
  1000. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  1001. line == 0xff) {
  1002. return -1;
  1003. }
  1004. DBG(" -> no map ! Using irq line %d from PCI config\n", line);
  1005. virq = irq_create_mapping(NULL, line);
  1006. if (virq != NO_IRQ)
  1007. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  1008. } else {
  1009. DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  1010. oirq.size, oirq.specifier[0], oirq.specifier[1],
  1011. oirq.controller->full_name);
  1012. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  1013. oirq.size);
  1014. }
  1015. if(virq == NO_IRQ) {
  1016. DBG(" -> failed to map !\n");
  1017. return -1;
  1018. }
  1019. DBG(" -> mapped to linux irq %d\n", virq);
  1020. pci_dev->irq = virq;
  1021. return 0;
  1022. }
  1023. EXPORT_SYMBOL(pci_read_irq_line);
  1024. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1025. const struct resource *rsrc,
  1026. resource_size_t *start, resource_size_t *end)
  1027. {
  1028. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1029. resource_size_t offset = 0;
  1030. if (hose == NULL)
  1031. return;
  1032. if (rsrc->flags & IORESOURCE_IO)
  1033. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  1034. /* We pass a fully fixed up address to userland for MMIO instead of
  1035. * a BAR value because X is lame and expects to be able to use that
  1036. * to pass to /dev/mem !
  1037. *
  1038. * That means that we'll have potentially 64 bits values where some
  1039. * userland apps only expect 32 (like X itself since it thinks only
  1040. * Sparc has 64 bits MMIO) but if we don't do that, we break it on
  1041. * 32 bits CHRPs :-(
  1042. *
  1043. * Hopefully, the sysfs insterface is immune to that gunk. Once X
  1044. * has been fixed (and the fix spread enough), we can re-enable the
  1045. * 2 lines below and pass down a BAR value to userland. In that case
  1046. * we'll also have to re-enable the matching code in
  1047. * __pci_mmap_make_offset().
  1048. *
  1049. * BenH.
  1050. */
  1051. #if 0
  1052. else if (rsrc->flags & IORESOURCE_MEM)
  1053. offset = hose->pci_mem_offset;
  1054. #endif
  1055. *start = rsrc->start - offset;
  1056. *end = rsrc->end - offset;
  1057. }
  1058. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  1059. {
  1060. if (!have_of)
  1061. return NULL;
  1062. while(node) {
  1063. struct pci_controller *hose, *tmp;
  1064. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1065. if (hose->arch_data == node)
  1066. return hose;
  1067. node = node->parent;
  1068. }
  1069. return NULL;
  1070. }
  1071. unsigned long pci_address_to_pio(phys_addr_t address)
  1072. {
  1073. struct pci_controller *hose, *tmp;
  1074. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1075. if (address >= hose->io_base_phys &&
  1076. address < (hose->io_base_phys + hose->pci_io_size)) {
  1077. unsigned long base =
  1078. (unsigned long)hose->io_base_virt - _IO_BASE;
  1079. return base + (address - hose->io_base_phys);
  1080. }
  1081. }
  1082. return (unsigned int)-1;
  1083. }
  1084. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  1085. #define IOBASE_BRIDGE_NUMBER 0
  1086. #define IOBASE_MEMORY 1
  1087. #define IOBASE_IO 2
  1088. #define IOBASE_ISA_IO 3
  1089. #define IOBASE_ISA_MEM 4
  1090. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  1091. unsigned long in_devfn)
  1092. {
  1093. struct pci_controller* hose;
  1094. struct list_head *ln;
  1095. struct pci_bus *bus = NULL;
  1096. struct device_node *hose_node;
  1097. /* Argh ! Please forgive me for that hack, but that's the
  1098. * simplest way to get existing XFree to not lockup on some
  1099. * G5 machines... So when something asks for bus 0 io base
  1100. * (bus 0 is HT root), we return the AGP one instead.
  1101. */
  1102. if (machine_is_compatible("MacRISC4"))
  1103. if (in_bus == 0)
  1104. in_bus = 0xf0;
  1105. /* That syscall isn't quite compatible with PCI domains, but it's
  1106. * used on pre-domains setup. We return the first match
  1107. */
  1108. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  1109. bus = pci_bus_b(ln);
  1110. if (in_bus >= bus->number && in_bus <= bus->subordinate)
  1111. break;
  1112. bus = NULL;
  1113. }
  1114. if (bus == NULL || bus->sysdata == NULL)
  1115. return -ENODEV;
  1116. hose_node = (struct device_node *)bus->sysdata;
  1117. hose = PCI_DN(hose_node)->phb;
  1118. switch (which) {
  1119. case IOBASE_BRIDGE_NUMBER:
  1120. return (long)hose->first_busno;
  1121. case IOBASE_MEMORY:
  1122. return (long)hose->pci_mem_offset;
  1123. case IOBASE_IO:
  1124. return (long)hose->io_base_phys;
  1125. case IOBASE_ISA_IO:
  1126. return (long)isa_io_base;
  1127. case IOBASE_ISA_MEM:
  1128. return -EINVAL;
  1129. }
  1130. return -EOPNOTSUPP;
  1131. }
  1132. #ifdef CONFIG_NUMA
  1133. int pcibus_to_node(struct pci_bus *bus)
  1134. {
  1135. struct pci_controller *phb = pci_bus_to_host(bus);
  1136. return phb->node;
  1137. }
  1138. EXPORT_SYMBOL(pcibus_to_node);
  1139. #endif