pci_64.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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 <asm/processor.h>
  24. #include <asm/io.h>
  25. #include <asm/prom.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/machdep.h>
  29. #include <asm/ppc-pci.h>
  30. #ifdef DEBUG
  31. #include <asm/udbg.h>
  32. #define DBG(fmt...) printk(fmt)
  33. #else
  34. #define DBG(fmt...)
  35. #endif
  36. unsigned long pci_probe_only = 1;
  37. int pci_assign_all_buses = 0;
  38. #ifdef CONFIG_PPC_MULTIPLATFORM
  39. static void fixup_resource(struct resource *res, struct pci_dev *dev);
  40. static void do_bus_setup(struct pci_bus *bus);
  41. static void phbs_remap_io(void);
  42. #endif
  43. /* pci_io_base -- the base address from which io bars are offsets.
  44. * This is the lowest I/O base address (so bar values are always positive),
  45. * and it *must* be the start of ISA space if an ISA bus exists because
  46. * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
  47. * page is mapped and isa_io_limit prevents access to it.
  48. */
  49. unsigned long isa_io_base; /* NULL if no ISA bus */
  50. EXPORT_SYMBOL(isa_io_base);
  51. unsigned long pci_io_base;
  52. EXPORT_SYMBOL(pci_io_base);
  53. void iSeries_pcibios_init(void);
  54. LIST_HEAD(hose_list);
  55. struct dma_mapping_ops pci_dma_ops;
  56. EXPORT_SYMBOL(pci_dma_ops);
  57. int global_phb_number; /* Global phb counter */
  58. /* Cached ISA bridge dev. */
  59. struct pci_dev *ppc64_isabridge_dev = NULL;
  60. EXPORT_SYMBOL_GPL(ppc64_isabridge_dev);
  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 - pci_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 - pci_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. pci_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. static DEFINE_SPINLOCK(hose_spinlock);
  143. /*
  144. * pci_controller(phb) initialized common variables.
  145. */
  146. static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
  147. {
  148. memset(hose, 0, sizeof(struct pci_controller));
  149. spin_lock(&hose_spinlock);
  150. hose->global_number = global_phb_number++;
  151. list_add_tail(&hose->list_node, &hose_list);
  152. spin_unlock(&hose_spinlock);
  153. }
  154. struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
  155. {
  156. struct pci_controller *phb;
  157. if (mem_init_done)
  158. phb = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  159. else
  160. phb = alloc_bootmem(sizeof (struct pci_controller));
  161. if (phb == NULL)
  162. return NULL;
  163. pci_setup_pci_controller(phb);
  164. phb->arch_data = dev;
  165. phb->is_dynamic = mem_init_done;
  166. if (dev)
  167. PHB_SET_NODE(phb, of_node_to_nid(dev));
  168. return phb;
  169. }
  170. void pcibios_free_controller(struct pci_controller *phb)
  171. {
  172. if (phb->is_dynamic)
  173. kfree(phb);
  174. }
  175. #ifndef CONFIG_PPC_ISERIES
  176. void __devinit pcibios_claim_one_bus(struct pci_bus *b)
  177. {
  178. struct pci_dev *dev;
  179. struct pci_bus *child_bus;
  180. list_for_each_entry(dev, &b->devices, bus_list) {
  181. int i;
  182. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  183. struct resource *r = &dev->resource[i];
  184. if (r->parent || !r->start || !r->flags)
  185. continue;
  186. pci_claim_resource(dev, i);
  187. }
  188. }
  189. list_for_each_entry(child_bus, &b->children, node)
  190. pcibios_claim_one_bus(child_bus);
  191. }
  192. #ifdef CONFIG_HOTPLUG
  193. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  194. #endif
  195. static void __init pcibios_claim_of_setup(void)
  196. {
  197. struct pci_bus *b;
  198. list_for_each_entry(b, &pci_root_buses, node)
  199. pcibios_claim_one_bus(b);
  200. }
  201. #endif
  202. #ifdef CONFIG_PPC_MULTIPLATFORM
  203. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  204. {
  205. const u32 *prop;
  206. int len;
  207. prop = get_property(np, name, &len);
  208. if (prop && len >= 4)
  209. return *prop;
  210. return def;
  211. }
  212. static unsigned int pci_parse_of_flags(u32 addr0)
  213. {
  214. unsigned int flags = 0;
  215. if (addr0 & 0x02000000) {
  216. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  217. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  218. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  219. if (addr0 & 0x40000000)
  220. flags |= IORESOURCE_PREFETCH
  221. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  222. } else if (addr0 & 0x01000000)
  223. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  224. return flags;
  225. }
  226. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  227. static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
  228. {
  229. u64 base, size;
  230. unsigned int flags;
  231. struct resource *res;
  232. const u32 *addrs;
  233. u32 i;
  234. int proplen;
  235. addrs = get_property(node, "assigned-addresses", &proplen);
  236. if (!addrs)
  237. return;
  238. DBG(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  239. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  240. flags = pci_parse_of_flags(addrs[0]);
  241. if (!flags)
  242. continue;
  243. base = GET_64BIT(addrs, 1);
  244. size = GET_64BIT(addrs, 3);
  245. if (!size)
  246. continue;
  247. i = addrs[0] & 0xff;
  248. DBG(" base: %llx, size: %llx, i: %x\n",
  249. (unsigned long long)base, (unsigned long long)size, i);
  250. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  251. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  252. } else if (i == dev->rom_base_reg) {
  253. res = &dev->resource[PCI_ROM_RESOURCE];
  254. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  255. } else {
  256. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  257. continue;
  258. }
  259. res->start = base;
  260. res->end = base + size - 1;
  261. res->flags = flags;
  262. res->name = pci_name(dev);
  263. fixup_resource(res, dev);
  264. }
  265. }
  266. struct pci_dev *of_create_pci_dev(struct device_node *node,
  267. struct pci_bus *bus, int devfn)
  268. {
  269. struct pci_dev *dev;
  270. const char *type;
  271. dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  272. if (!dev)
  273. return NULL;
  274. type = get_property(node, "device_type", NULL);
  275. if (type == NULL)
  276. type = "";
  277. DBG(" create device, devfn: %x, type: %s\n", devfn, type);
  278. memset(dev, 0, sizeof(struct pci_dev));
  279. dev->bus = bus;
  280. dev->sysdata = node;
  281. dev->dev.parent = bus->bridge;
  282. dev->dev.bus = &pci_bus_type;
  283. dev->devfn = devfn;
  284. dev->multifunction = 0; /* maybe a lie? */
  285. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  286. dev->device = get_int_prop(node, "device-id", 0xffff);
  287. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  288. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  289. dev->cfg_size = pci_cfg_space_size(dev);
  290. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  291. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  292. dev->class = get_int_prop(node, "class-code", 0);
  293. DBG(" class: 0x%x\n", dev->class);
  294. dev->current_state = 4; /* unknown power state */
  295. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  296. /* a PCI-PCI bridge */
  297. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  298. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  299. } else if (!strcmp(type, "cardbus")) {
  300. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  301. } else {
  302. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  303. dev->rom_base_reg = PCI_ROM_ADDRESS;
  304. /* Maybe do a default OF mapping here */
  305. dev->irq = NO_IRQ;
  306. }
  307. pci_parse_of_addrs(node, dev);
  308. DBG(" adding to system ...\n");
  309. pci_device_add(dev, bus);
  310. /* XXX pci_scan_msi_device(dev); */
  311. return dev;
  312. }
  313. EXPORT_SYMBOL(of_create_pci_dev);
  314. void __devinit of_scan_bus(struct device_node *node,
  315. struct pci_bus *bus)
  316. {
  317. struct device_node *child = NULL;
  318. const u32 *reg;
  319. int reglen, devfn;
  320. struct pci_dev *dev;
  321. DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number);
  322. while ((child = of_get_next_child(node, child)) != NULL) {
  323. DBG(" * %s\n", child->full_name);
  324. reg = get_property(child, "reg", &reglen);
  325. if (reg == NULL || reglen < 20)
  326. continue;
  327. devfn = (reg[0] >> 8) & 0xff;
  328. /* create a new pci_dev for this device */
  329. dev = of_create_pci_dev(child, bus, devfn);
  330. if (!dev)
  331. continue;
  332. DBG("dev header type: %x\n", dev->hdr_type);
  333. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  334. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  335. of_scan_pci_bridge(child, dev);
  336. }
  337. do_bus_setup(bus);
  338. }
  339. EXPORT_SYMBOL(of_scan_bus);
  340. void __devinit of_scan_pci_bridge(struct device_node *node,
  341. struct pci_dev *dev)
  342. {
  343. struct pci_bus *bus;
  344. const u32 *busrange, *ranges;
  345. int len, i, mode;
  346. struct resource *res;
  347. unsigned int flags;
  348. u64 size;
  349. DBG("of_scan_pci_bridge(%s)\n", node->full_name);
  350. /* parse bus-range property */
  351. busrange = get_property(node, "bus-range", &len);
  352. if (busrange == NULL || len != 8) {
  353. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  354. node->full_name);
  355. return;
  356. }
  357. ranges = get_property(node, "ranges", &len);
  358. if (ranges == NULL) {
  359. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  360. node->full_name);
  361. return;
  362. }
  363. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  364. if (!bus) {
  365. printk(KERN_ERR "Failed to create pci bus for %s\n",
  366. node->full_name);
  367. return;
  368. }
  369. bus->primary = dev->bus->number;
  370. bus->subordinate = busrange[1];
  371. bus->bridge_ctl = 0;
  372. bus->sysdata = node;
  373. /* parse ranges property */
  374. /* PCI #address-cells == 3 and #size-cells == 2 always */
  375. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  376. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  377. res->flags = 0;
  378. bus->resource[i] = res;
  379. ++res;
  380. }
  381. i = 1;
  382. for (; len >= 32; len -= 32, ranges += 8) {
  383. flags = pci_parse_of_flags(ranges[0]);
  384. size = GET_64BIT(ranges, 6);
  385. if (flags == 0 || size == 0)
  386. continue;
  387. if (flags & IORESOURCE_IO) {
  388. res = bus->resource[0];
  389. if (res->flags) {
  390. printk(KERN_ERR "PCI: ignoring extra I/O range"
  391. " for bridge %s\n", node->full_name);
  392. continue;
  393. }
  394. } else {
  395. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  396. printk(KERN_ERR "PCI: too many memory ranges"
  397. " for bridge %s\n", node->full_name);
  398. continue;
  399. }
  400. res = bus->resource[i];
  401. ++i;
  402. }
  403. res->start = GET_64BIT(ranges, 1);
  404. res->end = res->start + size - 1;
  405. res->flags = flags;
  406. fixup_resource(res, dev);
  407. }
  408. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  409. bus->number);
  410. DBG(" bus name: %s\n", bus->name);
  411. mode = PCI_PROBE_NORMAL;
  412. if (ppc_md.pci_probe_mode)
  413. mode = ppc_md.pci_probe_mode(bus);
  414. DBG(" probe mode: %d\n", mode);
  415. if (mode == PCI_PROBE_DEVTREE)
  416. of_scan_bus(node, bus);
  417. else if (mode == PCI_PROBE_NORMAL)
  418. pci_scan_child_bus(bus);
  419. }
  420. EXPORT_SYMBOL(of_scan_pci_bridge);
  421. #endif /* CONFIG_PPC_MULTIPLATFORM */
  422. void __devinit scan_phb(struct pci_controller *hose)
  423. {
  424. struct pci_bus *bus;
  425. struct device_node *node = hose->arch_data;
  426. int i, mode;
  427. struct resource *res;
  428. DBG("Scanning PHB %s\n", node ? node->full_name : "<NO NAME>");
  429. bus = pci_create_bus(NULL, hose->first_busno, hose->ops, node);
  430. if (bus == NULL) {
  431. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  432. hose->global_number);
  433. return;
  434. }
  435. bus->secondary = hose->first_busno;
  436. hose->bus = bus;
  437. bus->resource[0] = res = &hose->io_resource;
  438. if (res->flags && request_resource(&ioport_resource, res))
  439. printk(KERN_ERR "Failed to request PCI IO region "
  440. "on PCI domain %04x\n", hose->global_number);
  441. for (i = 0; i < 3; ++i) {
  442. res = &hose->mem_resources[i];
  443. bus->resource[i+1] = res;
  444. if (res->flags && request_resource(&iomem_resource, res))
  445. printk(KERN_ERR "Failed to request PCI memory region "
  446. "on PCI domain %04x\n", hose->global_number);
  447. }
  448. mode = PCI_PROBE_NORMAL;
  449. #ifdef CONFIG_PPC_MULTIPLATFORM
  450. if (node && ppc_md.pci_probe_mode)
  451. mode = ppc_md.pci_probe_mode(bus);
  452. DBG(" probe mode: %d\n", mode);
  453. if (mode == PCI_PROBE_DEVTREE) {
  454. bus->subordinate = hose->last_busno;
  455. of_scan_bus(node, bus);
  456. }
  457. #endif /* CONFIG_PPC_MULTIPLATFORM */
  458. if (mode == PCI_PROBE_NORMAL)
  459. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  460. }
  461. static int __init pcibios_init(void)
  462. {
  463. struct pci_controller *hose, *tmp;
  464. /* For now, override phys_mem_access_prot. If we need it,
  465. * later, we may move that initialization to each ppc_md
  466. */
  467. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  468. #ifdef CONFIG_PPC_ISERIES
  469. iSeries_pcibios_init();
  470. #endif
  471. printk(KERN_DEBUG "PCI: Probing PCI hardware\n");
  472. /* Scan all of the recorded PCI controllers. */
  473. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  474. scan_phb(hose);
  475. pci_bus_add_devices(hose->bus);
  476. }
  477. #ifndef CONFIG_PPC_ISERIES
  478. if (pci_probe_only)
  479. pcibios_claim_of_setup();
  480. else
  481. /* FIXME: `else' will be removed when
  482. pci_assign_unassigned_resources() is able to work
  483. correctly with [partially] allocated PCI tree. */
  484. pci_assign_unassigned_resources();
  485. #endif /* !CONFIG_PPC_ISERIES */
  486. /* Call machine dependent final fixup */
  487. if (ppc_md.pcibios_fixup)
  488. ppc_md.pcibios_fixup();
  489. /* Cache the location of the ISA bridge (if we have one) */
  490. ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  491. if (ppc64_isabridge_dev != NULL)
  492. printk(KERN_DEBUG "ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
  493. #ifdef CONFIG_PPC_MULTIPLATFORM
  494. /* map in PCI I/O space */
  495. phbs_remap_io();
  496. #endif
  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. /*
  530. * Return the domain number for this bus.
  531. */
  532. int pci_domain_nr(struct pci_bus *bus)
  533. {
  534. #ifdef CONFIG_PPC_ISERIES
  535. return 0;
  536. #else
  537. struct pci_controller *hose = pci_bus_to_host(bus);
  538. return hose->global_number;
  539. #endif
  540. }
  541. EXPORT_SYMBOL(pci_domain_nr);
  542. /* Decide whether to display the domain number in /proc */
  543. int pci_proc_domain(struct pci_bus *bus)
  544. {
  545. #ifdef CONFIG_PPC_ISERIES
  546. return 0;
  547. #else
  548. struct pci_controller *hose = pci_bus_to_host(bus);
  549. return hose->buid;
  550. #endif
  551. }
  552. /*
  553. * Platform support for /proc/bus/pci/X/Y mmap()s,
  554. * modelled on the sparc64 implementation by Dave Miller.
  555. * -- paulus.
  556. */
  557. /*
  558. * Adjust vm_pgoff of VMA such that it is the physical page offset
  559. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  560. *
  561. * Basically, the user finds the base address for his device which he wishes
  562. * to mmap. They read the 32-bit value from the config space base register,
  563. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  564. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  565. *
  566. * Returns negative error code on failure, zero on success.
  567. */
  568. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  569. unsigned long *offset,
  570. enum pci_mmap_state mmap_state)
  571. {
  572. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  573. unsigned long io_offset = 0;
  574. int i, res_bit;
  575. if (hose == 0)
  576. return NULL; /* should never happen */
  577. /* If memory, add on the PCI bridge address offset */
  578. if (mmap_state == pci_mmap_mem) {
  579. *offset += hose->pci_mem_offset;
  580. res_bit = IORESOURCE_MEM;
  581. } else {
  582. io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  583. *offset += io_offset;
  584. res_bit = IORESOURCE_IO;
  585. }
  586. /*
  587. * Check that the offset requested corresponds to one of the
  588. * resources of the device.
  589. */
  590. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  591. struct resource *rp = &dev->resource[i];
  592. int flags = rp->flags;
  593. /* treat ROM as memory (should be already) */
  594. if (i == PCI_ROM_RESOURCE)
  595. flags |= IORESOURCE_MEM;
  596. /* Active and same type? */
  597. if ((flags & res_bit) == 0)
  598. continue;
  599. /* In the range of this resource? */
  600. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  601. continue;
  602. /* found it! construct the final physical address */
  603. if (mmap_state == pci_mmap_io)
  604. *offset += hose->io_base_phys - io_offset;
  605. return rp;
  606. }
  607. return NULL;
  608. }
  609. /*
  610. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  611. * device mapping.
  612. */
  613. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  614. pgprot_t protection,
  615. enum pci_mmap_state mmap_state,
  616. int write_combine)
  617. {
  618. unsigned long prot = pgprot_val(protection);
  619. /* Write combine is always 0 on non-memory space mappings. On
  620. * memory space, if the user didn't pass 1, we check for a
  621. * "prefetchable" resource. This is a bit hackish, but we use
  622. * this to workaround the inability of /sysfs to provide a write
  623. * combine bit
  624. */
  625. if (mmap_state != pci_mmap_mem)
  626. write_combine = 0;
  627. else if (write_combine == 0) {
  628. if (rp->flags & IORESOURCE_PREFETCH)
  629. write_combine = 1;
  630. }
  631. /* XXX would be nice to have a way to ask for write-through */
  632. prot |= _PAGE_NO_CACHE;
  633. if (write_combine)
  634. prot &= ~_PAGE_GUARDED;
  635. else
  636. prot |= _PAGE_GUARDED;
  637. printk(KERN_DEBUG "PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  638. prot);
  639. return __pgprot(prot);
  640. }
  641. /*
  642. * This one is used by /dev/mem and fbdev who have no clue about the
  643. * PCI device, it tries to find the PCI device first and calls the
  644. * above routine
  645. */
  646. pgprot_t pci_phys_mem_access_prot(struct file *file,
  647. unsigned long pfn,
  648. unsigned long size,
  649. pgprot_t protection)
  650. {
  651. struct pci_dev *pdev = NULL;
  652. struct resource *found = NULL;
  653. unsigned long prot = pgprot_val(protection);
  654. unsigned long offset = pfn << PAGE_SHIFT;
  655. int i;
  656. if (page_is_ram(pfn))
  657. return __pgprot(prot);
  658. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  659. for_each_pci_dev(pdev) {
  660. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  661. struct resource *rp = &pdev->resource[i];
  662. int flags = rp->flags;
  663. /* Active and same type? */
  664. if ((flags & IORESOURCE_MEM) == 0)
  665. continue;
  666. /* In the range of this resource? */
  667. if (offset < (rp->start & PAGE_MASK) ||
  668. offset > rp->end)
  669. continue;
  670. found = rp;
  671. break;
  672. }
  673. if (found)
  674. break;
  675. }
  676. if (found) {
  677. if (found->flags & IORESOURCE_PREFETCH)
  678. prot &= ~_PAGE_GUARDED;
  679. pci_dev_put(pdev);
  680. }
  681. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  682. return __pgprot(prot);
  683. }
  684. /*
  685. * Perform the actual remap of the pages for a PCI device mapping, as
  686. * appropriate for this architecture. The region in the process to map
  687. * is described by vm_start and vm_end members of VMA, the base physical
  688. * address is found in vm_pgoff.
  689. * The pci device structure is provided so that architectures may make mapping
  690. * decisions on a per-device or per-bus basis.
  691. *
  692. * Returns a negative error code on failure, zero on success.
  693. */
  694. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  695. enum pci_mmap_state mmap_state, int write_combine)
  696. {
  697. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  698. struct resource *rp;
  699. int ret;
  700. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  701. if (rp == NULL)
  702. return -EINVAL;
  703. vma->vm_pgoff = offset >> PAGE_SHIFT;
  704. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  705. vma->vm_page_prot,
  706. mmap_state, write_combine);
  707. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  708. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  709. return ret;
  710. }
  711. static ssize_t pci_show_devspec(struct device *dev,
  712. struct device_attribute *attr, char *buf)
  713. {
  714. struct pci_dev *pdev;
  715. struct device_node *np;
  716. pdev = to_pci_dev (dev);
  717. np = pci_device_to_OF_node(pdev);
  718. if (np == NULL || np->full_name == NULL)
  719. return 0;
  720. return sprintf(buf, "%s", np->full_name);
  721. }
  722. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  723. void pcibios_add_platform_entries(struct pci_dev *pdev)
  724. {
  725. device_create_file(&pdev->dev, &dev_attr_devspec);
  726. }
  727. #ifdef CONFIG_PPC_MULTIPLATFORM
  728. #define ISA_SPACE_MASK 0x1
  729. #define ISA_SPACE_IO 0x1
  730. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  731. unsigned long phb_io_base_phys,
  732. void __iomem * phb_io_base_virt)
  733. {
  734. /* Remove these asap */
  735. struct pci_address {
  736. u32 a_hi;
  737. u32 a_mid;
  738. u32 a_lo;
  739. };
  740. struct isa_address {
  741. u32 a_hi;
  742. u32 a_lo;
  743. };
  744. struct isa_range {
  745. struct isa_address isa_addr;
  746. struct pci_address pci_addr;
  747. unsigned int size;
  748. };
  749. const struct isa_range *range;
  750. unsigned long pci_addr;
  751. unsigned int isa_addr;
  752. unsigned int size;
  753. int rlen = 0;
  754. range = get_property(isa_node, "ranges", &rlen);
  755. if (range == NULL || (rlen < sizeof(struct isa_range))) {
  756. printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
  757. "mapping 64k\n");
  758. __ioremap_explicit(phb_io_base_phys,
  759. (unsigned long)phb_io_base_virt,
  760. 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
  761. return;
  762. }
  763. /* From "ISA Binding to 1275"
  764. * The ranges property is laid out as an array of elements,
  765. * each of which comprises:
  766. * cells 0 - 1: an ISA address
  767. * cells 2 - 4: a PCI address
  768. * (size depending on dev->n_addr_cells)
  769. * cell 5: the size of the range
  770. */
  771. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
  772. isa_addr = range->isa_addr.a_lo;
  773. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  774. range->pci_addr.a_lo;
  775. /* Assume these are both zero */
  776. if ((pci_addr != 0) || (isa_addr != 0)) {
  777. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  778. __FUNCTION__);
  779. return;
  780. }
  781. size = PAGE_ALIGN(range->size);
  782. __ioremap_explicit(phb_io_base_phys,
  783. (unsigned long) phb_io_base_virt,
  784. size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  785. }
  786. }
  787. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  788. struct device_node *dev, int prim)
  789. {
  790. const unsigned int *ranges;
  791. unsigned int pci_space;
  792. unsigned long size;
  793. int rlen = 0;
  794. int memno = 0;
  795. struct resource *res;
  796. int np, na = prom_n_addr_cells(dev);
  797. unsigned long pci_addr, cpu_phys_addr;
  798. np = na + 5;
  799. /* From "PCI Binding to 1275"
  800. * The ranges property is laid out as an array of elements,
  801. * each of which comprises:
  802. * cells 0 - 2: a PCI address
  803. * cells 3 or 3+4: a CPU physical address
  804. * (size depending on dev->n_addr_cells)
  805. * cells 4+5 or 5+6: the size of the range
  806. */
  807. ranges = get_property(dev, "ranges", &rlen);
  808. if (ranges == NULL)
  809. return;
  810. hose->io_base_phys = 0;
  811. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  812. res = NULL;
  813. pci_space = ranges[0];
  814. pci_addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  815. cpu_phys_addr = ranges[3];
  816. if (na >= 2)
  817. cpu_phys_addr = (cpu_phys_addr << 32) | ranges[4];
  818. size = ((unsigned long)ranges[na+3] << 32) | ranges[na+4];
  819. ranges += np;
  820. if (size == 0)
  821. continue;
  822. /* Now consume following elements while they are contiguous */
  823. while (rlen >= np * sizeof(unsigned int)) {
  824. unsigned long addr, phys;
  825. if (ranges[0] != pci_space)
  826. break;
  827. addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  828. phys = ranges[3];
  829. if (na >= 2)
  830. phys = (phys << 32) | ranges[4];
  831. if (addr != pci_addr + size ||
  832. phys != cpu_phys_addr + size)
  833. break;
  834. size += ((unsigned long)ranges[na+3] << 32)
  835. | ranges[na+4];
  836. ranges += np;
  837. rlen -= np * sizeof(unsigned int);
  838. }
  839. switch ((pci_space >> 24) & 0x3) {
  840. case 1: /* I/O space */
  841. hose->io_base_phys = cpu_phys_addr;
  842. hose->pci_io_size = size;
  843. res = &hose->io_resource;
  844. res->flags = IORESOURCE_IO;
  845. res->start = pci_addr;
  846. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  847. res->start, res->start + size - 1);
  848. break;
  849. case 2: /* memory space */
  850. memno = 0;
  851. while (memno < 3 && hose->mem_resources[memno].flags)
  852. ++memno;
  853. if (memno == 0)
  854. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  855. if (memno < 3) {
  856. res = &hose->mem_resources[memno];
  857. res->flags = IORESOURCE_MEM;
  858. res->start = cpu_phys_addr;
  859. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  860. res->start, res->start + size - 1);
  861. }
  862. break;
  863. }
  864. if (res != NULL) {
  865. res->name = dev->full_name;
  866. res->end = res->start + size - 1;
  867. res->parent = NULL;
  868. res->sibling = NULL;
  869. res->child = NULL;
  870. }
  871. }
  872. }
  873. void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
  874. {
  875. unsigned long size = hose->pci_io_size;
  876. unsigned long io_virt_offset;
  877. struct resource *res;
  878. struct device_node *isa_dn;
  879. hose->io_base_virt = reserve_phb_iospace(size);
  880. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  881. hose->global_number, hose->io_base_phys,
  882. (unsigned long) hose->io_base_virt);
  883. if (primary) {
  884. pci_io_base = (unsigned long)hose->io_base_virt;
  885. isa_dn = of_find_node_by_type(NULL, "isa");
  886. if (isa_dn) {
  887. isa_io_base = pci_io_base;
  888. pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
  889. hose->io_base_virt);
  890. of_node_put(isa_dn);
  891. }
  892. }
  893. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  894. res = &hose->io_resource;
  895. res->start += io_virt_offset;
  896. res->end += io_virt_offset;
  897. }
  898. void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
  899. int primary)
  900. {
  901. unsigned long size = hose->pci_io_size;
  902. unsigned long io_virt_offset;
  903. struct resource *res;
  904. hose->io_base_virt = __ioremap(hose->io_base_phys, size,
  905. _PAGE_NO_CACHE | _PAGE_GUARDED);
  906. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  907. hose->global_number, hose->io_base_phys,
  908. (unsigned long) hose->io_base_virt);
  909. if (primary)
  910. pci_io_base = (unsigned long)hose->io_base_virt;
  911. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  912. res = &hose->io_resource;
  913. res->start += io_virt_offset;
  914. res->end += io_virt_offset;
  915. }
  916. static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
  917. unsigned long *start_virt, unsigned long *size)
  918. {
  919. struct pci_controller *hose = pci_bus_to_host(bus);
  920. struct pci_bus_region region;
  921. struct resource *res;
  922. if (bus->self) {
  923. res = bus->resource[0];
  924. pcibios_resource_to_bus(bus->self, &region, res);
  925. *start_phys = hose->io_base_phys + region.start;
  926. *start_virt = (unsigned long) hose->io_base_virt +
  927. region.start;
  928. if (region.end > region.start)
  929. *size = region.end - region.start + 1;
  930. else {
  931. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  932. __FUNCTION__, region.start, region.end);
  933. return 1;
  934. }
  935. } else {
  936. /* Root Bus */
  937. res = &hose->io_resource;
  938. *start_phys = hose->io_base_phys;
  939. *start_virt = (unsigned long) hose->io_base_virt;
  940. if (res->end > res->start)
  941. *size = res->end - res->start + 1;
  942. else {
  943. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  944. __FUNCTION__, res->start, res->end);
  945. return 1;
  946. }
  947. }
  948. return 0;
  949. }
  950. int unmap_bus_range(struct pci_bus *bus)
  951. {
  952. unsigned long start_phys;
  953. unsigned long start_virt;
  954. unsigned long size;
  955. if (!bus) {
  956. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  957. return 1;
  958. }
  959. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  960. return 1;
  961. if (iounmap_explicit((void __iomem *) start_virt, size))
  962. return 1;
  963. return 0;
  964. }
  965. EXPORT_SYMBOL(unmap_bus_range);
  966. int remap_bus_range(struct pci_bus *bus)
  967. {
  968. unsigned long start_phys;
  969. unsigned long start_virt;
  970. unsigned long size;
  971. if (!bus) {
  972. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  973. return 1;
  974. }
  975. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  976. return 1;
  977. if (start_phys == 0)
  978. return 1;
  979. printk(KERN_DEBUG "mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
  980. if (__ioremap_explicit(start_phys, start_virt, size,
  981. _PAGE_NO_CACHE | _PAGE_GUARDED))
  982. return 1;
  983. return 0;
  984. }
  985. EXPORT_SYMBOL(remap_bus_range);
  986. static void phbs_remap_io(void)
  987. {
  988. struct pci_controller *hose, *tmp;
  989. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  990. remap_bus_range(hose->bus);
  991. }
  992. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  993. {
  994. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  995. unsigned long offset;
  996. if (res->flags & IORESOURCE_IO) {
  997. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  998. res->start += offset;
  999. res->end += offset;
  1000. } else if (res->flags & IORESOURCE_MEM) {
  1001. res->start += hose->pci_mem_offset;
  1002. res->end += hose->pci_mem_offset;
  1003. }
  1004. }
  1005. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  1006. struct pci_bus *bus)
  1007. {
  1008. /* Update device resources. */
  1009. int i;
  1010. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1011. if (dev->resource[i].flags)
  1012. fixup_resource(&dev->resource[i], dev);
  1013. }
  1014. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  1015. static void __devinit do_bus_setup(struct pci_bus *bus)
  1016. {
  1017. struct pci_dev *dev;
  1018. ppc_md.iommu_bus_setup(bus);
  1019. list_for_each_entry(dev, &bus->devices, bus_list)
  1020. ppc_md.iommu_dev_setup(dev);
  1021. if (ppc_md.irq_bus_setup)
  1022. ppc_md.irq_bus_setup(bus);
  1023. }
  1024. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  1025. {
  1026. struct pci_dev *dev = bus->self;
  1027. if (dev && pci_probe_only &&
  1028. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1029. /* This is a subordinate bridge */
  1030. pci_read_bridge_bases(bus);
  1031. pcibios_fixup_device_resources(dev, bus);
  1032. }
  1033. do_bus_setup(bus);
  1034. if (!pci_probe_only)
  1035. return;
  1036. list_for_each_entry(dev, &bus->devices, bus_list)
  1037. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1038. pcibios_fixup_device_resources(dev, bus);
  1039. }
  1040. EXPORT_SYMBOL(pcibios_fixup_bus);
  1041. /*
  1042. * Reads the interrupt pin to determine if interrupt is use by card.
  1043. * If the interrupt is used, then gets the interrupt line from the
  1044. * openfirmware and sets it in the pci_dev and pci_config line.
  1045. */
  1046. int pci_read_irq_line(struct pci_dev *pci_dev)
  1047. {
  1048. struct of_irq oirq;
  1049. unsigned int virq;
  1050. DBG("Try to map irq for %s...\n", pci_name(pci_dev));
  1051. #ifdef DEBUG
  1052. memset(&oirq, 0xff, sizeof(oirq));
  1053. #endif
  1054. /* Try to get a mapping from the device-tree */
  1055. if (of_irq_map_pci(pci_dev, &oirq)) {
  1056. u8 line, pin;
  1057. /* If that fails, lets fallback to what is in the config
  1058. * space and map that through the default controller. We
  1059. * also set the type to level low since that's what PCI
  1060. * interrupts are. If your platform does differently, then
  1061. * either provide a proper interrupt tree or don't use this
  1062. * function.
  1063. */
  1064. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  1065. return -1;
  1066. if (pin == 0)
  1067. return -1;
  1068. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  1069. line == 0xff) {
  1070. return -1;
  1071. }
  1072. DBG(" -> no map ! Using irq line %d from PCI config\n", line);
  1073. virq = irq_create_mapping(NULL, line);
  1074. if (virq != NO_IRQ)
  1075. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  1076. } else {
  1077. DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  1078. oirq.size, oirq.specifier[0], oirq.specifier[1],
  1079. oirq.controller->full_name);
  1080. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  1081. oirq.size);
  1082. }
  1083. if(virq == NO_IRQ) {
  1084. DBG(" -> failed to map !\n");
  1085. return -1;
  1086. }
  1087. DBG(" -> mapped to linux irq %d\n", virq);
  1088. pci_dev->irq = virq;
  1089. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, virq);
  1090. return 0;
  1091. }
  1092. EXPORT_SYMBOL(pci_read_irq_line);
  1093. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1094. const struct resource *rsrc,
  1095. u64 *start, u64 *end)
  1096. {
  1097. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1098. unsigned long offset = 0;
  1099. if (hose == NULL)
  1100. return;
  1101. if (rsrc->flags & IORESOURCE_IO)
  1102. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  1103. hose->io_base_phys;
  1104. *start = rsrc->start + offset;
  1105. *end = rsrc->end + offset;
  1106. }
  1107. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  1108. {
  1109. if (!have_of)
  1110. return NULL;
  1111. while(node) {
  1112. struct pci_controller *hose, *tmp;
  1113. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1114. if (hose->arch_data == node)
  1115. return hose;
  1116. node = node->parent;
  1117. }
  1118. return NULL;
  1119. }
  1120. #endif /* CONFIG_PPC_MULTIPLATFORM */
  1121. unsigned long pci_address_to_pio(phys_addr_t address)
  1122. {
  1123. struct pci_controller *hose, *tmp;
  1124. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1125. if (address >= hose->io_base_phys &&
  1126. address < (hose->io_base_phys + hose->pci_io_size)) {
  1127. unsigned long base =
  1128. (unsigned long)hose->io_base_virt - pci_io_base;
  1129. return base + (address - hose->io_base_phys);
  1130. }
  1131. }
  1132. return (unsigned int)-1;
  1133. }
  1134. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  1135. #define IOBASE_BRIDGE_NUMBER 0
  1136. #define IOBASE_MEMORY 1
  1137. #define IOBASE_IO 2
  1138. #define IOBASE_ISA_IO 3
  1139. #define IOBASE_ISA_MEM 4
  1140. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  1141. unsigned long in_devfn)
  1142. {
  1143. struct pci_controller* hose;
  1144. struct list_head *ln;
  1145. struct pci_bus *bus = NULL;
  1146. struct device_node *hose_node;
  1147. /* Argh ! Please forgive me for that hack, but that's the
  1148. * simplest way to get existing XFree to not lockup on some
  1149. * G5 machines... So when something asks for bus 0 io base
  1150. * (bus 0 is HT root), we return the AGP one instead.
  1151. */
  1152. if (machine_is_compatible("MacRISC4"))
  1153. if (in_bus == 0)
  1154. in_bus = 0xf0;
  1155. /* That syscall isn't quite compatible with PCI domains, but it's
  1156. * used on pre-domains setup. We return the first match
  1157. */
  1158. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  1159. bus = pci_bus_b(ln);
  1160. if (in_bus >= bus->number && in_bus < (bus->number + bus->subordinate))
  1161. break;
  1162. bus = NULL;
  1163. }
  1164. if (bus == NULL || bus->sysdata == NULL)
  1165. return -ENODEV;
  1166. hose_node = (struct device_node *)bus->sysdata;
  1167. hose = PCI_DN(hose_node)->phb;
  1168. switch (which) {
  1169. case IOBASE_BRIDGE_NUMBER:
  1170. return (long)hose->first_busno;
  1171. case IOBASE_MEMORY:
  1172. return (long)hose->pci_mem_offset;
  1173. case IOBASE_IO:
  1174. return (long)hose->io_base_phys;
  1175. case IOBASE_ISA_IO:
  1176. return (long)isa_io_base;
  1177. case IOBASE_ISA_MEM:
  1178. return -EINVAL;
  1179. }
  1180. return -EOPNOTSUPP;
  1181. }
  1182. #ifdef CONFIG_NUMA
  1183. int pcibus_to_node(struct pci_bus *bus)
  1184. {
  1185. struct pci_controller *phb = pci_bus_to_host(bus);
  1186. return phb->node;
  1187. }
  1188. EXPORT_SYMBOL(pcibus_to_node);
  1189. #endif