pci-common.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * Contains common pci routines for ALL ppc platform
  3. * (based on pci_32.c and pci_64.c)
  4. *
  5. * Port for PPC64 David Engebretsen, IBM Corp.
  6. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  7. *
  8. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  9. * Rework, based on alpha PCI code.
  10. *
  11. * Common pmac/prep/chrp pci routines. -- Cort
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #undef DEBUG
  19. #include <linux/kernel.h>
  20. #include <linux/pci.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/mm.h>
  25. #include <linux/list.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/irq.h>
  28. #include <linux/vmalloc.h>
  29. #include <asm/processor.h>
  30. #include <asm/io.h>
  31. #include <asm/prom.h>
  32. #include <asm/pci-bridge.h>
  33. #include <asm/byteorder.h>
  34. #include <asm/machdep.h>
  35. #include <asm/ppc-pci.h>
  36. #include <asm/firmware.h>
  37. #ifdef DEBUG
  38. #include <asm/udbg.h>
  39. #define DBG(fmt...) printk(fmt)
  40. #else
  41. #define DBG(fmt...)
  42. #endif
  43. static DEFINE_SPINLOCK(hose_spinlock);
  44. /* XXX kill that some day ... */
  45. static int global_phb_number; /* Global phb counter */
  46. /* ISA Memory physical address */
  47. resource_size_t isa_mem_base;
  48. /* Default PCI flags is 0 */
  49. unsigned int ppc_pci_flags;
  50. struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
  51. {
  52. struct pci_controller *phb;
  53. phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
  54. if (phb == NULL)
  55. return NULL;
  56. spin_lock(&hose_spinlock);
  57. phb->global_number = global_phb_number++;
  58. list_add_tail(&phb->list_node, &hose_list);
  59. spin_unlock(&hose_spinlock);
  60. phb->dn = dev;
  61. phb->is_dynamic = mem_init_done;
  62. #ifdef CONFIG_PPC64
  63. if (dev) {
  64. int nid = of_node_to_nid(dev);
  65. if (nid < 0 || !node_online(nid))
  66. nid = -1;
  67. PHB_SET_NODE(phb, nid);
  68. }
  69. #endif
  70. return phb;
  71. }
  72. void pcibios_free_controller(struct pci_controller *phb)
  73. {
  74. spin_lock(&hose_spinlock);
  75. list_del(&phb->list_node);
  76. spin_unlock(&hose_spinlock);
  77. if (phb->is_dynamic)
  78. kfree(phb);
  79. }
  80. int pcibios_vaddr_is_ioport(void __iomem *address)
  81. {
  82. int ret = 0;
  83. struct pci_controller *hose;
  84. unsigned long size;
  85. spin_lock(&hose_spinlock);
  86. list_for_each_entry(hose, &hose_list, list_node) {
  87. #ifdef CONFIG_PPC64
  88. size = hose->pci_io_size;
  89. #else
  90. size = hose->io_resource.end - hose->io_resource.start + 1;
  91. #endif
  92. if (address >= hose->io_base_virt &&
  93. address < (hose->io_base_virt + size)) {
  94. ret = 1;
  95. break;
  96. }
  97. }
  98. spin_unlock(&hose_spinlock);
  99. return ret;
  100. }
  101. /*
  102. * Return the domain number for this bus.
  103. */
  104. int pci_domain_nr(struct pci_bus *bus)
  105. {
  106. struct pci_controller *hose = pci_bus_to_host(bus);
  107. return hose->global_number;
  108. }
  109. EXPORT_SYMBOL(pci_domain_nr);
  110. #ifdef CONFIG_PPC_OF
  111. /* This routine is meant to be used early during boot, when the
  112. * PCI bus numbers have not yet been assigned, and you need to
  113. * issue PCI config cycles to an OF device.
  114. * It could also be used to "fix" RTAS config cycles if you want
  115. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  116. * config cycles.
  117. */
  118. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  119. {
  120. if (!have_of)
  121. return NULL;
  122. while(node) {
  123. struct pci_controller *hose, *tmp;
  124. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  125. if (hose->dn == node)
  126. return hose;
  127. node = node->parent;
  128. }
  129. return NULL;
  130. }
  131. static ssize_t pci_show_devspec(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. struct pci_dev *pdev;
  135. struct device_node *np;
  136. pdev = to_pci_dev (dev);
  137. np = pci_device_to_OF_node(pdev);
  138. if (np == NULL || np->full_name == NULL)
  139. return 0;
  140. return sprintf(buf, "%s", np->full_name);
  141. }
  142. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  143. #endif /* CONFIG_PPC_OF */
  144. /* Add sysfs properties */
  145. int pcibios_add_platform_entries(struct pci_dev *pdev)
  146. {
  147. #ifdef CONFIG_PPC_OF
  148. return device_create_file(&pdev->dev, &dev_attr_devspec);
  149. #else
  150. return 0;
  151. #endif /* CONFIG_PPC_OF */
  152. }
  153. char __devinit *pcibios_setup(char *str)
  154. {
  155. return str;
  156. }
  157. /*
  158. * Reads the interrupt pin to determine if interrupt is use by card.
  159. * If the interrupt is used, then gets the interrupt line from the
  160. * openfirmware and sets it in the pci_dev and pci_config line.
  161. */
  162. int pci_read_irq_line(struct pci_dev *pci_dev)
  163. {
  164. struct of_irq oirq;
  165. unsigned int virq;
  166. /* The current device-tree that iSeries generates from the HV
  167. * PCI informations doesn't contain proper interrupt routing,
  168. * and all the fallback would do is print out crap, so we
  169. * don't attempt to resolve the interrupts here at all, some
  170. * iSeries specific fixup does it.
  171. *
  172. * In the long run, we will hopefully fix the generated device-tree
  173. * instead.
  174. */
  175. #ifdef CONFIG_PPC_ISERIES
  176. if (firmware_has_feature(FW_FEATURE_ISERIES))
  177. return -1;
  178. #endif
  179. DBG("Try to map irq for %s...\n", pci_name(pci_dev));
  180. #ifdef DEBUG
  181. memset(&oirq, 0xff, sizeof(oirq));
  182. #endif
  183. /* Try to get a mapping from the device-tree */
  184. if (of_irq_map_pci(pci_dev, &oirq)) {
  185. u8 line, pin;
  186. /* If that fails, lets fallback to what is in the config
  187. * space and map that through the default controller. We
  188. * also set the type to level low since that's what PCI
  189. * interrupts are. If your platform does differently, then
  190. * either provide a proper interrupt tree or don't use this
  191. * function.
  192. */
  193. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  194. return -1;
  195. if (pin == 0)
  196. return -1;
  197. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  198. line == 0xff || line == 0) {
  199. return -1;
  200. }
  201. DBG(" -> no map ! Using line %d (pin %d) from PCI config\n",
  202. line, pin);
  203. virq = irq_create_mapping(NULL, line);
  204. if (virq != NO_IRQ)
  205. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  206. } else {
  207. DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  208. oirq.size, oirq.specifier[0], oirq.specifier[1],
  209. oirq.controller->full_name);
  210. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  211. oirq.size);
  212. }
  213. if(virq == NO_IRQ) {
  214. DBG(" -> failed to map !\n");
  215. return -1;
  216. }
  217. DBG(" -> mapped to linux irq %d\n", virq);
  218. pci_dev->irq = virq;
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(pci_read_irq_line);
  222. /*
  223. * Platform support for /proc/bus/pci/X/Y mmap()s,
  224. * modelled on the sparc64 implementation by Dave Miller.
  225. * -- paulus.
  226. */
  227. /*
  228. * Adjust vm_pgoff of VMA such that it is the physical page offset
  229. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  230. *
  231. * Basically, the user finds the base address for his device which he wishes
  232. * to mmap. They read the 32-bit value from the config space base register,
  233. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  234. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  235. *
  236. * Returns negative error code on failure, zero on success.
  237. */
  238. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  239. resource_size_t *offset,
  240. enum pci_mmap_state mmap_state)
  241. {
  242. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  243. unsigned long io_offset = 0;
  244. int i, res_bit;
  245. if (hose == 0)
  246. return NULL; /* should never happen */
  247. /* If memory, add on the PCI bridge address offset */
  248. if (mmap_state == pci_mmap_mem) {
  249. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  250. *offset += hose->pci_mem_offset;
  251. #endif
  252. res_bit = IORESOURCE_MEM;
  253. } else {
  254. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  255. *offset += io_offset;
  256. res_bit = IORESOURCE_IO;
  257. }
  258. /*
  259. * Check that the offset requested corresponds to one of the
  260. * resources of the device.
  261. */
  262. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  263. struct resource *rp = &dev->resource[i];
  264. int flags = rp->flags;
  265. /* treat ROM as memory (should be already) */
  266. if (i == PCI_ROM_RESOURCE)
  267. flags |= IORESOURCE_MEM;
  268. /* Active and same type? */
  269. if ((flags & res_bit) == 0)
  270. continue;
  271. /* In the range of this resource? */
  272. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  273. continue;
  274. /* found it! construct the final physical address */
  275. if (mmap_state == pci_mmap_io)
  276. *offset += hose->io_base_phys - io_offset;
  277. return rp;
  278. }
  279. return NULL;
  280. }
  281. /*
  282. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  283. * device mapping.
  284. */
  285. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  286. pgprot_t protection,
  287. enum pci_mmap_state mmap_state,
  288. int write_combine)
  289. {
  290. unsigned long prot = pgprot_val(protection);
  291. /* Write combine is always 0 on non-memory space mappings. On
  292. * memory space, if the user didn't pass 1, we check for a
  293. * "prefetchable" resource. This is a bit hackish, but we use
  294. * this to workaround the inability of /sysfs to provide a write
  295. * combine bit
  296. */
  297. if (mmap_state != pci_mmap_mem)
  298. write_combine = 0;
  299. else if (write_combine == 0) {
  300. if (rp->flags & IORESOURCE_PREFETCH)
  301. write_combine = 1;
  302. }
  303. /* XXX would be nice to have a way to ask for write-through */
  304. prot |= _PAGE_NO_CACHE;
  305. if (write_combine)
  306. prot &= ~_PAGE_GUARDED;
  307. else
  308. prot |= _PAGE_GUARDED;
  309. return __pgprot(prot);
  310. }
  311. /*
  312. * This one is used by /dev/mem and fbdev who have no clue about the
  313. * PCI device, it tries to find the PCI device first and calls the
  314. * above routine
  315. */
  316. pgprot_t pci_phys_mem_access_prot(struct file *file,
  317. unsigned long pfn,
  318. unsigned long size,
  319. pgprot_t protection)
  320. {
  321. struct pci_dev *pdev = NULL;
  322. struct resource *found = NULL;
  323. unsigned long prot = pgprot_val(protection);
  324. unsigned long offset = pfn << PAGE_SHIFT;
  325. int i;
  326. if (page_is_ram(pfn))
  327. return __pgprot(prot);
  328. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  329. for_each_pci_dev(pdev) {
  330. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  331. struct resource *rp = &pdev->resource[i];
  332. int flags = rp->flags;
  333. /* Active and same type? */
  334. if ((flags & IORESOURCE_MEM) == 0)
  335. continue;
  336. /* In the range of this resource? */
  337. if (offset < (rp->start & PAGE_MASK) ||
  338. offset > rp->end)
  339. continue;
  340. found = rp;
  341. break;
  342. }
  343. if (found)
  344. break;
  345. }
  346. if (found) {
  347. if (found->flags & IORESOURCE_PREFETCH)
  348. prot &= ~_PAGE_GUARDED;
  349. pci_dev_put(pdev);
  350. }
  351. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  352. return __pgprot(prot);
  353. }
  354. /*
  355. * Perform the actual remap of the pages for a PCI device mapping, as
  356. * appropriate for this architecture. The region in the process to map
  357. * is described by vm_start and vm_end members of VMA, the base physical
  358. * address is found in vm_pgoff.
  359. * The pci device structure is provided so that architectures may make mapping
  360. * decisions on a per-device or per-bus basis.
  361. *
  362. * Returns a negative error code on failure, zero on success.
  363. */
  364. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  365. enum pci_mmap_state mmap_state, int write_combine)
  366. {
  367. resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
  368. struct resource *rp;
  369. int ret;
  370. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  371. if (rp == NULL)
  372. return -EINVAL;
  373. vma->vm_pgoff = offset >> PAGE_SHIFT;
  374. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  375. vma->vm_page_prot,
  376. mmap_state, write_combine);
  377. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  378. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  379. return ret;
  380. }
  381. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  382. const struct resource *rsrc,
  383. resource_size_t *start, resource_size_t *end)
  384. {
  385. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  386. resource_size_t offset = 0;
  387. if (hose == NULL)
  388. return;
  389. if (rsrc->flags & IORESOURCE_IO)
  390. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  391. /* We pass a fully fixed up address to userland for MMIO instead of
  392. * a BAR value because X is lame and expects to be able to use that
  393. * to pass to /dev/mem !
  394. *
  395. * That means that we'll have potentially 64 bits values where some
  396. * userland apps only expect 32 (like X itself since it thinks only
  397. * Sparc has 64 bits MMIO) but if we don't do that, we break it on
  398. * 32 bits CHRPs :-(
  399. *
  400. * Hopefully, the sysfs insterface is immune to that gunk. Once X
  401. * has been fixed (and the fix spread enough), we can re-enable the
  402. * 2 lines below and pass down a BAR value to userland. In that case
  403. * we'll also have to re-enable the matching code in
  404. * __pci_mmap_make_offset().
  405. *
  406. * BenH.
  407. */
  408. #if 0
  409. else if (rsrc->flags & IORESOURCE_MEM)
  410. offset = hose->pci_mem_offset;
  411. #endif
  412. *start = rsrc->start - offset;
  413. *end = rsrc->end - offset;
  414. }
  415. /**
  416. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  417. * @hose: newly allocated pci_controller to be setup
  418. * @dev: device node of the host bridge
  419. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  420. *
  421. * This function will parse the "ranges" property of a PCI host bridge device
  422. * node and setup the resource mapping of a pci controller based on its
  423. * content.
  424. *
  425. * Life would be boring if it wasn't for a few issues that we have to deal
  426. * with here:
  427. *
  428. * - We can only cope with one IO space range and up to 3 Memory space
  429. * ranges. However, some machines (thanks Apple !) tend to split their
  430. * space into lots of small contiguous ranges. So we have to coalesce.
  431. *
  432. * - We can only cope with all memory ranges having the same offset
  433. * between CPU addresses and PCI addresses. Unfortunately, some bridges
  434. * are setup for a large 1:1 mapping along with a small "window" which
  435. * maps PCI address 0 to some arbitrary high address of the CPU space in
  436. * order to give access to the ISA memory hole.
  437. * The way out of here that I've chosen for now is to always set the
  438. * offset based on the first resource found, then override it if we
  439. * have a different offset and the previous was set by an ISA hole.
  440. *
  441. * - Some busses have IO space not starting at 0, which causes trouble with
  442. * the way we do our IO resource renumbering. The code somewhat deals with
  443. * it for 64 bits but I would expect problems on 32 bits.
  444. *
  445. * - Some 32 bits platforms such as 4xx can have physical space larger than
  446. * 32 bits so we need to use 64 bits values for the parsing
  447. */
  448. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  449. struct device_node *dev,
  450. int primary)
  451. {
  452. const u32 *ranges;
  453. int rlen;
  454. int pna = of_n_addr_cells(dev);
  455. int np = pna + 5;
  456. int memno = 0, isa_hole = -1;
  457. u32 pci_space;
  458. unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
  459. unsigned long long isa_mb = 0;
  460. struct resource *res;
  461. printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
  462. dev->full_name, primary ? "(primary)" : "");
  463. /* Get ranges property */
  464. ranges = of_get_property(dev, "ranges", &rlen);
  465. if (ranges == NULL)
  466. return;
  467. /* Parse it */
  468. while ((rlen -= np * 4) >= 0) {
  469. /* Read next ranges element */
  470. pci_space = ranges[0];
  471. pci_addr = of_read_number(ranges + 1, 2);
  472. cpu_addr = of_translate_address(dev, ranges + 3);
  473. size = of_read_number(ranges + pna + 3, 2);
  474. ranges += np;
  475. if (cpu_addr == OF_BAD_ADDR || size == 0)
  476. continue;
  477. /* Now consume following elements while they are contiguous */
  478. for (; rlen >= np * sizeof(u32);
  479. ranges += np, rlen -= np * 4) {
  480. if (ranges[0] != pci_space)
  481. break;
  482. pci_next = of_read_number(ranges + 1, 2);
  483. cpu_next = of_translate_address(dev, ranges + 3);
  484. if (pci_next != pci_addr + size ||
  485. cpu_next != cpu_addr + size)
  486. break;
  487. size += of_read_number(ranges + pna + 3, 2);
  488. }
  489. /* Act based on address space type */
  490. res = NULL;
  491. switch ((pci_space >> 24) & 0x3) {
  492. case 1: /* PCI IO space */
  493. printk(KERN_INFO
  494. " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  495. cpu_addr, cpu_addr + size - 1, pci_addr);
  496. /* We support only one IO range */
  497. if (hose->pci_io_size) {
  498. printk(KERN_INFO
  499. " \\--> Skipped (too many) !\n");
  500. continue;
  501. }
  502. #ifdef CONFIG_PPC32
  503. /* On 32 bits, limit I/O space to 16MB */
  504. if (size > 0x01000000)
  505. size = 0x01000000;
  506. /* 32 bits needs to map IOs here */
  507. hose->io_base_virt = ioremap(cpu_addr, size);
  508. /* Expect trouble if pci_addr is not 0 */
  509. if (primary)
  510. isa_io_base =
  511. (unsigned long)hose->io_base_virt;
  512. #endif /* CONFIG_PPC32 */
  513. /* pci_io_size and io_base_phys always represent IO
  514. * space starting at 0 so we factor in pci_addr
  515. */
  516. hose->pci_io_size = pci_addr + size;
  517. hose->io_base_phys = cpu_addr - pci_addr;
  518. /* Build resource */
  519. res = &hose->io_resource;
  520. res->flags = IORESOURCE_IO;
  521. res->start = pci_addr;
  522. break;
  523. case 2: /* PCI Memory space */
  524. case 3: /* PCI 64 bits Memory space */
  525. printk(KERN_INFO
  526. " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  527. cpu_addr, cpu_addr + size - 1, pci_addr,
  528. (pci_space & 0x40000000) ? "Prefetch" : "");
  529. /* We support only 3 memory ranges */
  530. if (memno >= 3) {
  531. printk(KERN_INFO
  532. " \\--> Skipped (too many) !\n");
  533. continue;
  534. }
  535. /* Handles ISA memory hole space here */
  536. if (pci_addr == 0) {
  537. isa_mb = cpu_addr;
  538. isa_hole = memno;
  539. if (primary || isa_mem_base == 0)
  540. isa_mem_base = cpu_addr;
  541. }
  542. /* We get the PCI/Mem offset from the first range or
  543. * the, current one if the offset came from an ISA
  544. * hole. If they don't match, bugger.
  545. */
  546. if (memno == 0 ||
  547. (isa_hole >= 0 && pci_addr != 0 &&
  548. hose->pci_mem_offset == isa_mb))
  549. hose->pci_mem_offset = cpu_addr - pci_addr;
  550. else if (pci_addr != 0 &&
  551. hose->pci_mem_offset != cpu_addr - pci_addr) {
  552. printk(KERN_INFO
  553. " \\--> Skipped (offset mismatch) !\n");
  554. continue;
  555. }
  556. /* Build resource */
  557. res = &hose->mem_resources[memno++];
  558. res->flags = IORESOURCE_MEM;
  559. if (pci_space & 0x40000000)
  560. res->flags |= IORESOURCE_PREFETCH;
  561. res->start = cpu_addr;
  562. break;
  563. }
  564. if (res != NULL) {
  565. res->name = dev->full_name;
  566. res->end = res->start + size - 1;
  567. res->parent = NULL;
  568. res->sibling = NULL;
  569. res->child = NULL;
  570. }
  571. }
  572. /* If there's an ISA hole and the pci_mem_offset is -not- matching
  573. * the ISA hole offset, then we need to remove the ISA hole from
  574. * the resource list for that brige
  575. */
  576. if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
  577. unsigned int next = isa_hole + 1;
  578. printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb);
  579. if (next < memno)
  580. memmove(&hose->mem_resources[isa_hole],
  581. &hose->mem_resources[next],
  582. sizeof(struct resource) * (memno - next));
  583. hose->mem_resources[--memno].flags = 0;
  584. }
  585. }
  586. /* Decide whether to display the domain number in /proc */
  587. int pci_proc_domain(struct pci_bus *bus)
  588. {
  589. struct pci_controller *hose = pci_bus_to_host(bus);
  590. #ifdef CONFIG_PPC64
  591. return hose->buid != 0;
  592. #else
  593. if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS))
  594. return 0;
  595. if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0)
  596. return hose->global_number != 0;
  597. return 1;
  598. #endif
  599. }
  600. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  601. struct resource *res)
  602. {
  603. resource_size_t offset = 0, mask = (resource_size_t)-1;
  604. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  605. if (!hose)
  606. return;
  607. if (res->flags & IORESOURCE_IO) {
  608. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  609. mask = 0xffffffffu;
  610. } else if (res->flags & IORESOURCE_MEM)
  611. offset = hose->pci_mem_offset;
  612. region->start = (res->start - offset) & mask;
  613. region->end = (res->end - offset) & mask;
  614. }
  615. EXPORT_SYMBOL(pcibios_resource_to_bus);
  616. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  617. struct pci_bus_region *region)
  618. {
  619. resource_size_t offset = 0, mask = (resource_size_t)-1;
  620. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  621. if (!hose)
  622. return;
  623. if (res->flags & IORESOURCE_IO) {
  624. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  625. mask = 0xffffffffu;
  626. } else if (res->flags & IORESOURCE_MEM)
  627. offset = hose->pci_mem_offset;
  628. res->start = (region->start + offset) & mask;
  629. res->end = (region->end + offset) & mask;
  630. }
  631. EXPORT_SYMBOL(pcibios_bus_to_resource);
  632. /* Fixup a bus resource into a linux resource */
  633. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  634. {
  635. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  636. resource_size_t offset = 0, mask = (resource_size_t)-1;
  637. if (res->flags & IORESOURCE_IO) {
  638. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  639. mask = 0xffffffffu;
  640. } else if (res->flags & IORESOURCE_MEM)
  641. offset = hose->pci_mem_offset;
  642. res->start = (res->start + offset) & mask;
  643. res->end = (res->end + offset) & mask;
  644. pr_debug("PCI:%s %016llx-%016llx\n",
  645. pci_name(dev),
  646. (unsigned long long)res->start,
  647. (unsigned long long)res->end);
  648. }
  649. /* This header fixup will do the resource fixup for all devices as they are
  650. * probed, but not for bridge ranges
  651. */
  652. static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
  653. {
  654. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  655. int i;
  656. if (!hose) {
  657. printk(KERN_ERR "No host bridge for PCI dev %s !\n",
  658. pci_name(dev));
  659. return;
  660. }
  661. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  662. struct resource *res = dev->resource + i;
  663. if (!res->flags)
  664. continue;
  665. /* On platforms that have PPC_PCI_PROBE_ONLY set, we don't
  666. * consider 0 as an unassigned BAR value. It's technically
  667. * a valid value, but linux doesn't like it... so when we can
  668. * re-assign things, we do so, but if we can't, we keep it
  669. * around and hope for the best...
  670. */
  671. if (res->start == 0 && !(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
  672. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
  673. pci_name(dev), i,
  674. (unsigned long long)res->start,
  675. (unsigned long long)res->end,
  676. (unsigned int)res->flags);
  677. res->end -= res->start;
  678. res->start = 0;
  679. res->flags |= IORESOURCE_UNSET;
  680. continue;
  681. }
  682. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
  683. pci_name(dev), i,
  684. (unsigned long long)res->start,\
  685. (unsigned long long)res->end,
  686. (unsigned int)res->flags);
  687. fixup_resource(res, dev);
  688. }
  689. /* Call machine specific resource fixup */
  690. if (ppc_md.pcibios_fixup_resources)
  691. ppc_md.pcibios_fixup_resources(dev);
  692. }
  693. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  694. static void __devinit __pcibios_fixup_bus(struct pci_bus *bus)
  695. {
  696. struct pci_controller *hose = pci_bus_to_host(bus);
  697. struct pci_dev *dev = bus->self;
  698. pr_debug("PCI: Fixup bus %d (%s)\n", bus->number, dev ? pci_name(dev) : "PHB");
  699. /* Fixup PCI<->PCI bridges. Host bridges are handled separately, for
  700. * now differently between 32 and 64 bits.
  701. */
  702. if (dev != NULL) {
  703. struct resource *res;
  704. int i;
  705. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  706. if ((res = bus->resource[i]) == NULL)
  707. continue;
  708. if (!res->flags)
  709. continue;
  710. if (i >= 3 && bus->self->transparent)
  711. continue;
  712. /* On PowerMac, Apple leaves bridge windows open over
  713. * an inaccessible region of memory space (0...fffff)
  714. * which is somewhat bogus, but that's what they think
  715. * means disabled...
  716. *
  717. * We clear those to force them to be reallocated later
  718. *
  719. * We detect such regions by the fact that the base is
  720. * equal to the pci_mem_offset of the host bridge and
  721. * their size is smaller than 1M.
  722. */
  723. if (res->flags & IORESOURCE_MEM &&
  724. res->start == hose->pci_mem_offset &&
  725. res->end < 0x100000) {
  726. printk(KERN_INFO
  727. "PCI: Closing bogus Apple Firmware"
  728. " region %d on bus 0x%02x\n",
  729. i, bus->number);
  730. res->flags = 0;
  731. continue;
  732. }
  733. pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
  734. pci_name(dev), i,
  735. (unsigned long long)res->start,\
  736. (unsigned long long)res->end,
  737. (unsigned int)res->flags);
  738. fixup_resource(res, dev);
  739. }
  740. }
  741. /* Additional setup that is different between 32 and 64 bits for now */
  742. pcibios_do_bus_setup(bus);
  743. /* Platform specific bus fixups */
  744. if (ppc_md.pcibios_fixup_bus)
  745. ppc_md.pcibios_fixup_bus(bus);
  746. /* Read default IRQs and fixup if necessary */
  747. list_for_each_entry(dev, &bus->devices, bus_list) {
  748. pci_read_irq_line(dev);
  749. if (ppc_md.pci_irq_fixup)
  750. ppc_md.pci_irq_fixup(dev);
  751. }
  752. }
  753. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  754. {
  755. /* When called from the generic PCI probe, read PCI<->PCI bridge
  756. * bases before proceeding
  757. */
  758. if (bus->self != NULL)
  759. pci_read_bridge_bases(bus);
  760. __pcibios_fixup_bus(bus);
  761. }
  762. EXPORT_SYMBOL(pcibios_fixup_bus);
  763. /* When building a bus from the OF tree rather than probing, we need a
  764. * slightly different version of the fixup which doesn't read the
  765. * bridge bases using config space accesses
  766. */
  767. void __devinit pcibios_fixup_of_probed_bus(struct pci_bus *bus)
  768. {
  769. __pcibios_fixup_bus(bus);
  770. }
  771. static int skip_isa_ioresource_align(struct pci_dev *dev)
  772. {
  773. if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
  774. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  775. return 1;
  776. return 0;
  777. }
  778. /*
  779. * We need to avoid collisions with `mirrored' VGA ports
  780. * and other strange ISA hardware, so we always want the
  781. * addresses to be allocated in the 0x000-0x0ff region
  782. * modulo 0x400.
  783. *
  784. * Why? Because some silly external IO cards only decode
  785. * the low 10 bits of the IO address. The 0x00-0xff region
  786. * is reserved for motherboard devices that decode all 16
  787. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  788. * but we want to try to avoid allocating at 0x2900-0x2bff
  789. * which might have be mirrored at 0x0100-0x03ff..
  790. */
  791. void pcibios_align_resource(void *data, struct resource *res,
  792. resource_size_t size, resource_size_t align)
  793. {
  794. struct pci_dev *dev = data;
  795. if (res->flags & IORESOURCE_IO) {
  796. resource_size_t start = res->start;
  797. if (skip_isa_ioresource_align(dev))
  798. return;
  799. if (start & 0x300) {
  800. start = (start + 0x3ff) & ~0x3ff;
  801. res->start = start;
  802. }
  803. }
  804. }
  805. EXPORT_SYMBOL(pcibios_align_resource);
  806. /*
  807. * Reparent resource children of pr that conflict with res
  808. * under res, and make res replace those children.
  809. */
  810. static int __init reparent_resources(struct resource *parent,
  811. struct resource *res)
  812. {
  813. struct resource *p, **pp;
  814. struct resource **firstpp = NULL;
  815. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  816. if (p->end < res->start)
  817. continue;
  818. if (res->end < p->start)
  819. break;
  820. if (p->start < res->start || p->end > res->end)
  821. return -1; /* not completely contained */
  822. if (firstpp == NULL)
  823. firstpp = pp;
  824. }
  825. if (firstpp == NULL)
  826. return -1; /* didn't find any conflicting entries? */
  827. res->parent = parent;
  828. res->child = *firstpp;
  829. res->sibling = *pp;
  830. *firstpp = res;
  831. *pp = NULL;
  832. for (p = res->child; p != NULL; p = p->sibling) {
  833. p->parent = res;
  834. DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
  835. p->name,
  836. (unsigned long long)p->start,
  837. (unsigned long long)p->end, res->name);
  838. }
  839. return 0;
  840. }
  841. /*
  842. * Handle resources of PCI devices. If the world were perfect, we could
  843. * just allocate all the resource regions and do nothing more. It isn't.
  844. * On the other hand, we cannot just re-allocate all devices, as it would
  845. * require us to know lots of host bridge internals. So we attempt to
  846. * keep as much of the original configuration as possible, but tweak it
  847. * when it's found to be wrong.
  848. *
  849. * Known BIOS problems we have to work around:
  850. * - I/O or memory regions not configured
  851. * - regions configured, but not enabled in the command register
  852. * - bogus I/O addresses above 64K used
  853. * - expansion ROMs left enabled (this may sound harmless, but given
  854. * the fact the PCI specs explicitly allow address decoders to be
  855. * shared between expansion ROMs and other resource regions, it's
  856. * at least dangerous)
  857. *
  858. * Our solution:
  859. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  860. * This gives us fixed barriers on where we can allocate.
  861. * (2) Allocate resources for all enabled devices. If there is
  862. * a collision, just mark the resource as unallocated. Also
  863. * disable expansion ROMs during this step.
  864. * (3) Try to allocate resources for disabled devices. If the
  865. * resources were assigned correctly, everything goes well,
  866. * if they weren't, they won't disturb allocation of other
  867. * resources.
  868. * (4) Assign new addresses to resources which were either
  869. * not configured at all or misconfigured. If explicitly
  870. * requested by the user, configure expansion ROM address
  871. * as well.
  872. */
  873. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  874. {
  875. struct pci_bus *bus;
  876. int i;
  877. struct resource *res, *pr;
  878. /* Depth-First Search on bus tree */
  879. list_for_each_entry(bus, bus_list, node) {
  880. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  881. if ((res = bus->resource[i]) == NULL || !res->flags
  882. || res->start > res->end)
  883. continue;
  884. if (bus->parent == NULL)
  885. pr = (res->flags & IORESOURCE_IO) ?
  886. &ioport_resource : &iomem_resource;
  887. else {
  888. /* Don't bother with non-root busses when
  889. * re-assigning all resources. We clear the
  890. * resource flags as if they were colliding
  891. * and as such ensure proper re-allocation
  892. * later.
  893. */
  894. if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
  895. goto clear_resource;
  896. pr = pci_find_parent_resource(bus->self, res);
  897. if (pr == res) {
  898. /* this happens when the generic PCI
  899. * code (wrongly) decides that this
  900. * bridge is transparent -- paulus
  901. */
  902. continue;
  903. }
  904. }
  905. DBG("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
  906. "[0x%x], parent %p (%s)\n",
  907. bus->self ? pci_name(bus->self) : "PHB",
  908. bus->number, i,
  909. (unsigned long long)res->start,
  910. (unsigned long long)res->end,
  911. (unsigned int)res->flags,
  912. pr, (pr && pr->name) ? pr->name : "nil");
  913. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  914. if (request_resource(pr, res) == 0)
  915. continue;
  916. /*
  917. * Must be a conflict with an existing entry.
  918. * Move that entry (or entries) under the
  919. * bridge resource and try again.
  920. */
  921. if (reparent_resources(pr, res) == 0)
  922. continue;
  923. }
  924. printk(KERN_WARNING
  925. "PCI: Cannot allocate resource region "
  926. "%d of PCI bridge %d, will remap\n",
  927. i, bus->number);
  928. clear_resource:
  929. res->flags = 0;
  930. }
  931. pcibios_allocate_bus_resources(&bus->children);
  932. }
  933. }
  934. static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
  935. {
  936. struct resource *pr, *r = &dev->resource[idx];
  937. DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
  938. pci_name(dev), idx,
  939. (unsigned long long)r->start,
  940. (unsigned long long)r->end,
  941. (unsigned int)r->flags);
  942. pr = pci_find_parent_resource(dev, r);
  943. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  944. request_resource(pr, r) < 0) {
  945. printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
  946. " of device %s, will remap\n", idx, pci_name(dev));
  947. if (pr)
  948. DBG("PCI: parent is %p: %016llx-%016llx [%x]\n", pr,
  949. (unsigned long long)pr->start,
  950. (unsigned long long)pr->end,
  951. (unsigned int)pr->flags);
  952. /* We'll assign a new address later */
  953. r->flags |= IORESOURCE_UNSET;
  954. r->end -= r->start;
  955. r->start = 0;
  956. }
  957. }
  958. static void __init pcibios_allocate_resources(int pass)
  959. {
  960. struct pci_dev *dev = NULL;
  961. int idx, disabled;
  962. u16 command;
  963. struct resource *r;
  964. for_each_pci_dev(dev) {
  965. pci_read_config_word(dev, PCI_COMMAND, &command);
  966. for (idx = 0; idx < 6; idx++) {
  967. r = &dev->resource[idx];
  968. if (r->parent) /* Already allocated */
  969. continue;
  970. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  971. continue; /* Not assigned at all */
  972. if (r->flags & IORESOURCE_IO)
  973. disabled = !(command & PCI_COMMAND_IO);
  974. else
  975. disabled = !(command & PCI_COMMAND_MEMORY);
  976. if (pass == disabled)
  977. alloc_resource(dev, idx);
  978. }
  979. if (pass)
  980. continue;
  981. r = &dev->resource[PCI_ROM_RESOURCE];
  982. if (r->flags & IORESOURCE_ROM_ENABLE) {
  983. /* Turn the ROM off, leave the resource region,
  984. * but keep it unregistered.
  985. */
  986. u32 reg;
  987. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  988. r->flags &= ~IORESOURCE_ROM_ENABLE;
  989. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  990. pci_write_config_dword(dev, dev->rom_base_reg,
  991. reg & ~PCI_ROM_ADDRESS_ENABLE);
  992. }
  993. }
  994. }
  995. void __init pcibios_resource_survey(void)
  996. {
  997. /* Allocate and assign resources. If we re-assign everything, then
  998. * we skip the allocate phase
  999. */
  1000. pcibios_allocate_bus_resources(&pci_root_buses);
  1001. if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
  1002. pcibios_allocate_resources(0);
  1003. pcibios_allocate_resources(1);
  1004. }
  1005. if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
  1006. DBG("PCI: Assigning unassigned resouces...\n");
  1007. pci_assign_unassigned_resources();
  1008. }
  1009. /* Call machine dependent fixup */
  1010. if (ppc_md.pcibios_fixup)
  1011. ppc_md.pcibios_fixup();
  1012. }
  1013. #ifdef CONFIG_HOTPLUG
  1014. /* This is used by the pSeries hotplug driver to allocate resource
  1015. * of newly plugged busses. We can try to consolidate with the
  1016. * rest of the code later, for now, keep it as-is
  1017. */
  1018. void __devinit pcibios_claim_one_bus(struct pci_bus *bus)
  1019. {
  1020. struct pci_dev *dev;
  1021. struct pci_bus *child_bus;
  1022. list_for_each_entry(dev, &bus->devices, bus_list) {
  1023. int i;
  1024. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  1025. struct resource *r = &dev->resource[i];
  1026. if (r->parent || !r->start || !r->flags)
  1027. continue;
  1028. pci_claim_resource(dev, i);
  1029. }
  1030. }
  1031. list_for_each_entry(child_bus, &bus->children, node)
  1032. pcibios_claim_one_bus(child_bus);
  1033. }
  1034. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  1035. #endif /* CONFIG_HOTPLUG */
  1036. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1037. {
  1038. if (ppc_md.pcibios_enable_device_hook)
  1039. if (ppc_md.pcibios_enable_device_hook(dev))
  1040. return -EINVAL;
  1041. return pci_enable_resources(dev, mask);
  1042. }