pci.c 25 KB

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