pci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 <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/udbg.h>
  30. #include "pci.h"
  31. #ifdef DEBUG
  32. #define DBG(fmt...) udbg_printf(fmt)
  33. #else
  34. #define DBG(fmt...)
  35. #endif
  36. unsigned long pci_probe_only = 1;
  37. unsigned long pci_assign_all_buses = 0;
  38. /*
  39. * legal IO pages under MAX_ISA_PORT. This is to ensure we don't touch
  40. * devices we don't have access to.
  41. */
  42. unsigned long io_page_mask;
  43. EXPORT_SYMBOL(io_page_mask);
  44. unsigned int pcibios_assign_all_busses(void)
  45. {
  46. return pci_assign_all_buses;
  47. }
  48. /* pci_io_base -- the base address from which io bars are offsets.
  49. * This is the lowest I/O base address (so bar values are always positive),
  50. * and it *must* be the start of ISA space if an ISA bus exists because
  51. * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
  52. * page is mapped and isa_io_limit prevents access to it.
  53. */
  54. unsigned long isa_io_base; /* NULL if no ISA bus */
  55. EXPORT_SYMBOL(isa_io_base);
  56. unsigned long pci_io_base;
  57. EXPORT_SYMBOL(pci_io_base);
  58. void iSeries_pcibios_init(void);
  59. LIST_HEAD(hose_list);
  60. struct dma_mapping_ops pci_dma_ops;
  61. EXPORT_SYMBOL(pci_dma_ops);
  62. int global_phb_number; /* Global phb counter */
  63. /* Cached ISA bridge dev. */
  64. struct pci_dev *ppc64_isabridge_dev = NULL;
  65. static void fixup_broken_pcnet32(struct pci_dev* dev)
  66. {
  67. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  68. dev->vendor = PCI_VENDOR_ID_AMD;
  69. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  70. }
  71. }
  72. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  73. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  74. struct resource *res)
  75. {
  76. unsigned long offset = 0;
  77. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  78. if (!hose)
  79. return;
  80. if (res->flags & IORESOURCE_IO)
  81. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  82. if (res->flags & IORESOURCE_MEM)
  83. offset = hose->pci_mem_offset;
  84. region->start = res->start - offset;
  85. region->end = res->end - offset;
  86. }
  87. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  88. struct pci_bus_region *region)
  89. {
  90. unsigned long offset = 0;
  91. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  92. if (!hose)
  93. return;
  94. if (res->flags & IORESOURCE_IO)
  95. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  96. if (res->flags & IORESOURCE_MEM)
  97. offset = hose->pci_mem_offset;
  98. res->start = region->start + offset;
  99. res->end = region->end + offset;
  100. }
  101. #ifdef CONFIG_HOTPLUG
  102. EXPORT_SYMBOL(pcibios_resource_to_bus);
  103. EXPORT_SYMBOL(pcibios_bus_to_resource);
  104. #endif
  105. /*
  106. * We need to avoid collisions with `mirrored' VGA ports
  107. * and other strange ISA hardware, so we always want the
  108. * addresses to be allocated in the 0x000-0x0ff region
  109. * modulo 0x400.
  110. *
  111. * Why? Because some silly external IO cards only decode
  112. * the low 10 bits of the IO address. The 0x00-0xff region
  113. * is reserved for motherboard devices that decode all 16
  114. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  115. * but we want to try to avoid allocating at 0x2900-0x2bff
  116. * which might have be mirrored at 0x0100-0x03ff..
  117. */
  118. void pcibios_align_resource(void *data, struct resource *res,
  119. unsigned long size, unsigned long align)
  120. {
  121. struct pci_dev *dev = data;
  122. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  123. unsigned long start = res->start;
  124. unsigned long alignto;
  125. if (res->flags & IORESOURCE_IO) {
  126. unsigned long offset = (unsigned long)hose->io_base_virt -
  127. pci_io_base;
  128. /* Make sure we start at our min on all hoses */
  129. if (start - offset < PCIBIOS_MIN_IO)
  130. start = PCIBIOS_MIN_IO + offset;
  131. /*
  132. * Put everything into 0x00-0xff region modulo 0x400
  133. */
  134. if (start & 0x300)
  135. start = (start + 0x3ff) & ~0x3ff;
  136. } else if (res->flags & IORESOURCE_MEM) {
  137. /* Make sure we start at our min on all hoses */
  138. if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
  139. start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
  140. /* Align to multiple of size of minimum base. */
  141. alignto = max(0x1000UL, align);
  142. start = ALIGN(start, alignto);
  143. }
  144. res->start = start;
  145. }
  146. static DEFINE_SPINLOCK(hose_spinlock);
  147. /*
  148. * pci_controller(phb) initialized common variables.
  149. */
  150. void __devinit pci_setup_pci_controller(struct pci_controller *hose)
  151. {
  152. memset(hose, 0, sizeof(struct pci_controller));
  153. spin_lock(&hose_spinlock);
  154. hose->global_number = global_phb_number++;
  155. list_add_tail(&hose->list_node, &hose_list);
  156. spin_unlock(&hose_spinlock);
  157. }
  158. static void __init pcibios_claim_one_bus(struct pci_bus *b)
  159. {
  160. struct pci_dev *dev;
  161. struct pci_bus *child_bus;
  162. list_for_each_entry(dev, &b->devices, bus_list) {
  163. int i;
  164. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  165. struct resource *r = &dev->resource[i];
  166. if (r->parent || !r->start || !r->flags)
  167. continue;
  168. pci_claim_resource(dev, i);
  169. }
  170. }
  171. list_for_each_entry(child_bus, &b->children, node)
  172. pcibios_claim_one_bus(child_bus);
  173. }
  174. #ifndef CONFIG_PPC_ISERIES
  175. static void __init pcibios_claim_of_setup(void)
  176. {
  177. struct pci_bus *b;
  178. list_for_each_entry(b, &pci_root_buses, node)
  179. pcibios_claim_one_bus(b);
  180. }
  181. #endif
  182. static int __init pcibios_init(void)
  183. {
  184. struct pci_controller *hose, *tmp;
  185. struct pci_bus *bus;
  186. /* For now, override phys_mem_access_prot. If we need it,
  187. * later, we may move that initialization to each ppc_md
  188. */
  189. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  190. #ifdef CONFIG_PPC_ISERIES
  191. iSeries_pcibios_init();
  192. #endif
  193. printk("PCI: Probing PCI hardware\n");
  194. /* Scan all of the recorded PCI controllers. */
  195. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  196. hose->last_busno = 0xff;
  197. bus = pci_scan_bus(hose->first_busno, hose->ops,
  198. hose->arch_data);
  199. hose->bus = bus;
  200. hose->last_busno = bus->subordinate;
  201. }
  202. #ifndef CONFIG_PPC_ISERIES
  203. if (pci_probe_only)
  204. pcibios_claim_of_setup();
  205. else
  206. /* FIXME: `else' will be removed when
  207. pci_assign_unassigned_resources() is able to work
  208. correctly with [partially] allocated PCI tree. */
  209. pci_assign_unassigned_resources();
  210. #endif /* !CONFIG_PPC_ISERIES */
  211. /* Call machine dependent final fixup */
  212. if (ppc_md.pcibios_fixup)
  213. ppc_md.pcibios_fixup();
  214. /* Cache the location of the ISA bridge (if we have one) */
  215. ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  216. if (ppc64_isabridge_dev != NULL)
  217. printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
  218. printk("PCI: Probing PCI hardware done\n");
  219. return 0;
  220. }
  221. subsys_initcall(pcibios_init);
  222. char __init *pcibios_setup(char *str)
  223. {
  224. return str;
  225. }
  226. int pcibios_enable_device(struct pci_dev *dev, int mask)
  227. {
  228. u16 cmd, oldcmd;
  229. int i;
  230. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  231. oldcmd = cmd;
  232. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  233. struct resource *res = &dev->resource[i];
  234. /* Only set up the requested stuff */
  235. if (!(mask & (1<<i)))
  236. continue;
  237. if (res->flags & IORESOURCE_IO)
  238. cmd |= PCI_COMMAND_IO;
  239. if (res->flags & IORESOURCE_MEM)
  240. cmd |= PCI_COMMAND_MEMORY;
  241. }
  242. if (cmd != oldcmd) {
  243. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  244. pci_name(dev), cmd);
  245. /* Enable the appropriate bits in the PCI command register. */
  246. pci_write_config_word(dev, PCI_COMMAND, cmd);
  247. }
  248. return 0;
  249. }
  250. /*
  251. * Return the domain number for this bus.
  252. */
  253. int pci_domain_nr(struct pci_bus *bus)
  254. {
  255. #ifdef CONFIG_PPC_ISERIES
  256. return 0;
  257. #else
  258. struct pci_controller *hose = pci_bus_to_host(bus);
  259. return hose->global_number;
  260. #endif
  261. }
  262. EXPORT_SYMBOL(pci_domain_nr);
  263. /* Decide whether to display the domain number in /proc */
  264. int pci_proc_domain(struct pci_bus *bus)
  265. {
  266. #ifdef CONFIG_PPC_ISERIES
  267. return 0;
  268. #else
  269. struct pci_controller *hose = pci_bus_to_host(bus);
  270. return hose->buid;
  271. #endif
  272. }
  273. /*
  274. * Platform support for /proc/bus/pci/X/Y mmap()s,
  275. * modelled on the sparc64 implementation by Dave Miller.
  276. * -- paulus.
  277. */
  278. /*
  279. * Adjust vm_pgoff of VMA such that it is the physical page offset
  280. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  281. *
  282. * Basically, the user finds the base address for his device which he wishes
  283. * to mmap. They read the 32-bit value from the config space base register,
  284. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  285. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  286. *
  287. * Returns negative error code on failure, zero on success.
  288. */
  289. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  290. unsigned long *offset,
  291. enum pci_mmap_state mmap_state)
  292. {
  293. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  294. unsigned long io_offset = 0;
  295. int i, res_bit;
  296. if (hose == 0)
  297. return NULL; /* should never happen */
  298. /* If memory, add on the PCI bridge address offset */
  299. if (mmap_state == pci_mmap_mem) {
  300. *offset += hose->pci_mem_offset;
  301. res_bit = IORESOURCE_MEM;
  302. } else {
  303. io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  304. *offset += io_offset;
  305. res_bit = IORESOURCE_IO;
  306. }
  307. /*
  308. * Check that the offset requested corresponds to one of the
  309. * resources of the device.
  310. */
  311. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  312. struct resource *rp = &dev->resource[i];
  313. int flags = rp->flags;
  314. /* treat ROM as memory (should be already) */
  315. if (i == PCI_ROM_RESOURCE)
  316. flags |= IORESOURCE_MEM;
  317. /* Active and same type? */
  318. if ((flags & res_bit) == 0)
  319. continue;
  320. /* In the range of this resource? */
  321. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  322. continue;
  323. /* found it! construct the final physical address */
  324. if (mmap_state == pci_mmap_io)
  325. *offset += hose->io_base_phys - io_offset;
  326. return rp;
  327. }
  328. return NULL;
  329. }
  330. /*
  331. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  332. * device mapping.
  333. */
  334. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  335. pgprot_t protection,
  336. enum pci_mmap_state mmap_state,
  337. int write_combine)
  338. {
  339. unsigned long prot = pgprot_val(protection);
  340. /* Write combine is always 0 on non-memory space mappings. On
  341. * memory space, if the user didn't pass 1, we check for a
  342. * "prefetchable" resource. This is a bit hackish, but we use
  343. * this to workaround the inability of /sysfs to provide a write
  344. * combine bit
  345. */
  346. if (mmap_state != pci_mmap_mem)
  347. write_combine = 0;
  348. else if (write_combine == 0) {
  349. if (rp->flags & IORESOURCE_PREFETCH)
  350. write_combine = 1;
  351. }
  352. /* XXX would be nice to have a way to ask for write-through */
  353. prot |= _PAGE_NO_CACHE;
  354. if (write_combine)
  355. prot &= ~_PAGE_GUARDED;
  356. else
  357. prot |= _PAGE_GUARDED;
  358. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  359. prot);
  360. return __pgprot(prot);
  361. }
  362. /*
  363. * This one is used by /dev/mem and fbdev who have no clue about the
  364. * PCI device, it tries to find the PCI device first and calls the
  365. * above routine
  366. */
  367. pgprot_t pci_phys_mem_access_prot(struct file *file,
  368. unsigned long offset,
  369. unsigned long size,
  370. pgprot_t protection)
  371. {
  372. struct pci_dev *pdev = NULL;
  373. struct resource *found = NULL;
  374. unsigned long prot = pgprot_val(protection);
  375. int i;
  376. if (page_is_ram(offset >> PAGE_SHIFT))
  377. return __pgprot(prot);
  378. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  379. for_each_pci_dev(pdev) {
  380. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  381. struct resource *rp = &pdev->resource[i];
  382. int flags = rp->flags;
  383. /* Active and same type? */
  384. if ((flags & IORESOURCE_MEM) == 0)
  385. continue;
  386. /* In the range of this resource? */
  387. if (offset < (rp->start & PAGE_MASK) ||
  388. offset > rp->end)
  389. continue;
  390. found = rp;
  391. break;
  392. }
  393. if (found)
  394. break;
  395. }
  396. if (found) {
  397. if (found->flags & IORESOURCE_PREFETCH)
  398. prot &= ~_PAGE_GUARDED;
  399. pci_dev_put(pdev);
  400. }
  401. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  402. return __pgprot(prot);
  403. }
  404. /*
  405. * Perform the actual remap of the pages for a PCI device mapping, as
  406. * appropriate for this architecture. The region in the process to map
  407. * is described by vm_start and vm_end members of VMA, the base physical
  408. * address is found in vm_pgoff.
  409. * The pci device structure is provided so that architectures may make mapping
  410. * decisions on a per-device or per-bus basis.
  411. *
  412. * Returns a negative error code on failure, zero on success.
  413. */
  414. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  415. enum pci_mmap_state mmap_state,
  416. int write_combine)
  417. {
  418. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  419. struct resource *rp;
  420. int ret;
  421. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  422. if (rp == NULL)
  423. return -EINVAL;
  424. vma->vm_pgoff = offset >> PAGE_SHIFT;
  425. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  426. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  427. vma->vm_page_prot,
  428. mmap_state, write_combine);
  429. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  430. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  431. return ret;
  432. }
  433. #ifdef CONFIG_PPC_MULTIPLATFORM
  434. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  435. {
  436. struct pci_dev *pdev;
  437. struct device_node *np;
  438. pdev = to_pci_dev (dev);
  439. np = pci_device_to_OF_node(pdev);
  440. if (np == NULL || np->full_name == NULL)
  441. return 0;
  442. return sprintf(buf, "%s", np->full_name);
  443. }
  444. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  445. #endif /* CONFIG_PPC_MULTIPLATFORM */
  446. void pcibios_add_platform_entries(struct pci_dev *pdev)
  447. {
  448. #ifdef CONFIG_PPC_MULTIPLATFORM
  449. device_create_file(&pdev->dev, &dev_attr_devspec);
  450. #endif /* CONFIG_PPC_MULTIPLATFORM */
  451. }
  452. #ifdef CONFIG_PPC_MULTIPLATFORM
  453. #define ISA_SPACE_MASK 0x1
  454. #define ISA_SPACE_IO 0x1
  455. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  456. unsigned long phb_io_base_phys,
  457. void __iomem * phb_io_base_virt)
  458. {
  459. struct isa_range *range;
  460. unsigned long pci_addr;
  461. unsigned int isa_addr;
  462. unsigned int size;
  463. int rlen = 0;
  464. range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
  465. if (range == NULL || (rlen < sizeof(struct isa_range))) {
  466. printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
  467. "mapping 64k\n");
  468. __ioremap_explicit(phb_io_base_phys,
  469. (unsigned long)phb_io_base_virt,
  470. 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
  471. return;
  472. }
  473. /* From "ISA Binding to 1275"
  474. * The ranges property is laid out as an array of elements,
  475. * each of which comprises:
  476. * cells 0 - 1: an ISA address
  477. * cells 2 - 4: a PCI address
  478. * (size depending on dev->n_addr_cells)
  479. * cell 5: the size of the range
  480. */
  481. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
  482. isa_addr = range->isa_addr.a_lo;
  483. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  484. range->pci_addr.a_lo;
  485. /* Assume these are both zero */
  486. if ((pci_addr != 0) || (isa_addr != 0)) {
  487. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  488. __FUNCTION__);
  489. return;
  490. }
  491. size = PAGE_ALIGN(range->size);
  492. __ioremap_explicit(phb_io_base_phys,
  493. (unsigned long) phb_io_base_virt,
  494. size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  495. }
  496. }
  497. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  498. struct device_node *dev)
  499. {
  500. unsigned int *ranges;
  501. unsigned long size;
  502. int rlen = 0;
  503. int memno = 0;
  504. struct resource *res;
  505. int np, na = prom_n_addr_cells(dev);
  506. unsigned long pci_addr, cpu_phys_addr;
  507. np = na + 5;
  508. /* From "PCI Binding to 1275"
  509. * The ranges property is laid out as an array of elements,
  510. * each of which comprises:
  511. * cells 0 - 2: a PCI address
  512. * cells 3 or 3+4: a CPU physical address
  513. * (size depending on dev->n_addr_cells)
  514. * cells 4+5 or 5+6: the size of the range
  515. */
  516. rlen = 0;
  517. hose->io_base_phys = 0;
  518. ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  519. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  520. res = NULL;
  521. pci_addr = (unsigned long)ranges[1] << 32 | ranges[2];
  522. cpu_phys_addr = ranges[3];
  523. if (na == 2)
  524. cpu_phys_addr = cpu_phys_addr << 32 | ranges[4];
  525. size = (unsigned long)ranges[na+3] << 32 | ranges[na+4];
  526. if (size == 0)
  527. continue;
  528. switch ((ranges[0] >> 24) & 0x3) {
  529. case 1: /* I/O space */
  530. hose->io_base_phys = cpu_phys_addr;
  531. hose->pci_io_size = size;
  532. res = &hose->io_resource;
  533. res->flags = IORESOURCE_IO;
  534. res->start = pci_addr;
  535. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  536. res->start, res->start + size - 1);
  537. break;
  538. case 2: /* memory space */
  539. memno = 0;
  540. while (memno < 3 && hose->mem_resources[memno].flags)
  541. ++memno;
  542. if (memno == 0)
  543. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  544. if (memno < 3) {
  545. res = &hose->mem_resources[memno];
  546. res->flags = IORESOURCE_MEM;
  547. res->start = cpu_phys_addr;
  548. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  549. res->start, res->start + size - 1);
  550. }
  551. break;
  552. }
  553. if (res != NULL) {
  554. res->name = dev->full_name;
  555. res->end = res->start + size - 1;
  556. res->parent = NULL;
  557. res->sibling = NULL;
  558. res->child = NULL;
  559. }
  560. ranges += np;
  561. }
  562. }
  563. void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
  564. {
  565. unsigned long size = hose->pci_io_size;
  566. unsigned long io_virt_offset;
  567. struct resource *res;
  568. struct device_node *isa_dn;
  569. hose->io_base_virt = reserve_phb_iospace(size);
  570. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  571. hose->global_number, hose->io_base_phys,
  572. (unsigned long) hose->io_base_virt);
  573. if (primary) {
  574. pci_io_base = (unsigned long)hose->io_base_virt;
  575. isa_dn = of_find_node_by_type(NULL, "isa");
  576. if (isa_dn) {
  577. isa_io_base = pci_io_base;
  578. pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
  579. hose->io_base_virt);
  580. of_node_put(isa_dn);
  581. /* Allow all IO */
  582. io_page_mask = -1;
  583. }
  584. }
  585. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  586. res = &hose->io_resource;
  587. res->start += io_virt_offset;
  588. res->end += io_virt_offset;
  589. }
  590. void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
  591. int primary)
  592. {
  593. unsigned long size = hose->pci_io_size;
  594. unsigned long io_virt_offset;
  595. struct resource *res;
  596. hose->io_base_virt = __ioremap(hose->io_base_phys, size,
  597. _PAGE_NO_CACHE | _PAGE_GUARDED);
  598. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  599. hose->global_number, hose->io_base_phys,
  600. (unsigned long) hose->io_base_virt);
  601. if (primary)
  602. pci_io_base = (unsigned long)hose->io_base_virt;
  603. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  604. res = &hose->io_resource;
  605. res->start += io_virt_offset;
  606. res->end += io_virt_offset;
  607. }
  608. static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
  609. unsigned long *start_virt, unsigned long *size)
  610. {
  611. struct pci_controller *hose = pci_bus_to_host(bus);
  612. struct pci_bus_region region;
  613. struct resource *res;
  614. if (bus->self) {
  615. res = bus->resource[0];
  616. pcibios_resource_to_bus(bus->self, &region, res);
  617. *start_phys = hose->io_base_phys + region.start;
  618. *start_virt = (unsigned long) hose->io_base_virt +
  619. region.start;
  620. if (region.end > region.start)
  621. *size = region.end - region.start + 1;
  622. else {
  623. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  624. __FUNCTION__, region.start, region.end);
  625. return 1;
  626. }
  627. } else {
  628. /* Root Bus */
  629. res = &hose->io_resource;
  630. *start_phys = hose->io_base_phys;
  631. *start_virt = (unsigned long) hose->io_base_virt;
  632. if (res->end > res->start)
  633. *size = res->end - res->start + 1;
  634. else {
  635. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  636. __FUNCTION__, res->start, res->end);
  637. return 1;
  638. }
  639. }
  640. return 0;
  641. }
  642. int unmap_bus_range(struct pci_bus *bus)
  643. {
  644. unsigned long start_phys;
  645. unsigned long start_virt;
  646. unsigned long size;
  647. if (!bus) {
  648. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  649. return 1;
  650. }
  651. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  652. return 1;
  653. if (iounmap_explicit((void __iomem *) start_virt, size))
  654. return 1;
  655. return 0;
  656. }
  657. EXPORT_SYMBOL(unmap_bus_range);
  658. int remap_bus_range(struct pci_bus *bus)
  659. {
  660. unsigned long start_phys;
  661. unsigned long start_virt;
  662. unsigned long size;
  663. if (!bus) {
  664. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  665. return 1;
  666. }
  667. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  668. return 1;
  669. printk("mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
  670. if (__ioremap_explicit(start_phys, start_virt, size,
  671. _PAGE_NO_CACHE | _PAGE_GUARDED))
  672. return 1;
  673. return 0;
  674. }
  675. EXPORT_SYMBOL(remap_bus_range);
  676. void phbs_remap_io(void)
  677. {
  678. struct pci_controller *hose, *tmp;
  679. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  680. remap_bus_range(hose->bus);
  681. }
  682. /*
  683. * ppc64 can have multifunction devices that do not respond to function 0.
  684. * In this case we must scan all functions.
  685. */
  686. int pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
  687. {
  688. struct device_node *busdn, *dn;
  689. if (bus->self)
  690. busdn = pci_device_to_OF_node(bus->self);
  691. else
  692. busdn = bus->sysdata; /* must be a phb */
  693. if (busdn == NULL)
  694. return 0;
  695. /*
  696. * Check to see if there is any of the 8 functions are in the
  697. * device tree. If they are then we need to scan all the
  698. * functions of this slot.
  699. */
  700. for (dn = busdn->child; dn; dn = dn->sibling)
  701. if ((dn->devfn >> 3) == (devfn >> 3))
  702. return 1;
  703. return 0;
  704. }
  705. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  706. struct pci_bus *bus)
  707. {
  708. /* Update device resources. */
  709. struct pci_controller *hose = pci_bus_to_host(bus);
  710. int i;
  711. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  712. if (dev->resource[i].flags & IORESOURCE_IO) {
  713. unsigned long offset = (unsigned long)hose->io_base_virt
  714. - pci_io_base;
  715. unsigned long start, end, mask;
  716. start = dev->resource[i].start += offset;
  717. end = dev->resource[i].end += offset;
  718. /* Need to allow IO access to pages that are in the
  719. ISA range */
  720. if (start < MAX_ISA_PORT) {
  721. if (end > MAX_ISA_PORT)
  722. end = MAX_ISA_PORT;
  723. start >>= PAGE_SHIFT;
  724. end >>= PAGE_SHIFT;
  725. /* get the range of pages for the map */
  726. mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
  727. io_page_mask |= mask;
  728. }
  729. }
  730. else if (dev->resource[i].flags & IORESOURCE_MEM) {
  731. dev->resource[i].start += hose->pci_mem_offset;
  732. dev->resource[i].end += hose->pci_mem_offset;
  733. }
  734. }
  735. }
  736. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  737. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  738. {
  739. struct pci_controller *hose = pci_bus_to_host(bus);
  740. struct pci_dev *dev = bus->self;
  741. struct resource *res;
  742. int i;
  743. if (!dev) {
  744. /* Root bus. */
  745. hose->bus = bus;
  746. bus->resource[0] = res = &hose->io_resource;
  747. if (res->flags && request_resource(&ioport_resource, res))
  748. printk(KERN_ERR "Failed to request IO on "
  749. "PCI domain %d\n", pci_domain_nr(bus));
  750. for (i = 0; i < 3; ++i) {
  751. res = &hose->mem_resources[i];
  752. bus->resource[i+1] = res;
  753. if (res->flags && request_resource(&iomem_resource, res))
  754. printk(KERN_ERR "Failed to request MEM on "
  755. "PCI domain %d\n",
  756. pci_domain_nr(bus));
  757. }
  758. } else if (pci_probe_only &&
  759. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  760. /* This is a subordinate bridge */
  761. pci_read_bridge_bases(bus);
  762. pcibios_fixup_device_resources(dev, bus);
  763. }
  764. ppc_md.iommu_bus_setup(bus);
  765. list_for_each_entry(dev, &bus->devices, bus_list)
  766. ppc_md.iommu_dev_setup(dev);
  767. if (ppc_md.irq_bus_setup)
  768. ppc_md.irq_bus_setup(bus);
  769. if (!pci_probe_only)
  770. return;
  771. list_for_each_entry(dev, &bus->devices, bus_list) {
  772. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  773. pcibios_fixup_device_resources(dev, bus);
  774. }
  775. }
  776. EXPORT_SYMBOL(pcibios_fixup_bus);
  777. /*
  778. * Reads the interrupt pin to determine if interrupt is use by card.
  779. * If the interrupt is used, then gets the interrupt line from the
  780. * openfirmware and sets it in the pci_dev and pci_config line.
  781. */
  782. int pci_read_irq_line(struct pci_dev *pci_dev)
  783. {
  784. u8 intpin;
  785. struct device_node *node;
  786. pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
  787. if (intpin == 0)
  788. return 0;
  789. node = pci_device_to_OF_node(pci_dev);
  790. if (node == NULL)
  791. return -1;
  792. if (node->n_intrs == 0)
  793. return -1;
  794. pci_dev->irq = node->intrs[0].line;
  795. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  796. return 0;
  797. }
  798. EXPORT_SYMBOL(pci_read_irq_line);
  799. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  800. const struct resource *rsrc,
  801. u64 *start, u64 *end)
  802. {
  803. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  804. unsigned long offset = 0;
  805. if (hose == NULL)
  806. return;
  807. if (rsrc->flags & IORESOURCE_IO)
  808. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  809. hose->io_base_phys;
  810. *start = rsrc->start + offset;
  811. *end = rsrc->end + offset;
  812. }
  813. #endif /* CONFIG_PPC_MULTIPLATFORM */