pci_64.c 37 KB

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