pci-common.c 33 KB

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