pci_64.c 36 KB

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