pci-common.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. printk(KERN_INFO
  525. " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  526. cpu_addr, cpu_addr + size - 1, pci_addr,
  527. (pci_space & 0x40000000) ? "Prefetch" : "");
  528. /* We support only 3 memory ranges */
  529. if (memno >= 3) {
  530. printk(KERN_INFO
  531. " \\--> Skipped (too many) !\n");
  532. continue;
  533. }
  534. /* Handles ISA memory hole space here */
  535. if (pci_addr == 0) {
  536. isa_mb = cpu_addr;
  537. isa_hole = memno;
  538. if (primary || isa_mem_base == 0)
  539. isa_mem_base = cpu_addr;
  540. }
  541. /* We get the PCI/Mem offset from the first range or
  542. * the, current one if the offset came from an ISA
  543. * hole. If they don't match, bugger.
  544. */
  545. if (memno == 0 ||
  546. (isa_hole >= 0 && pci_addr != 0 &&
  547. hose->pci_mem_offset == isa_mb))
  548. hose->pci_mem_offset = cpu_addr - pci_addr;
  549. else if (pci_addr != 0 &&
  550. hose->pci_mem_offset != cpu_addr - pci_addr) {
  551. printk(KERN_INFO
  552. " \\--> Skipped (offset mismatch) !\n");
  553. continue;
  554. }
  555. /* Build resource */
  556. res = &hose->mem_resources[memno++];
  557. res->flags = IORESOURCE_MEM;
  558. if (pci_space & 0x40000000)
  559. res->flags |= IORESOURCE_PREFETCH;
  560. res->start = cpu_addr;
  561. break;
  562. }
  563. if (res != NULL) {
  564. res->name = dev->full_name;
  565. res->end = res->start + size - 1;
  566. res->parent = NULL;
  567. res->sibling = NULL;
  568. res->child = NULL;
  569. }
  570. }
  571. /* Out of paranoia, let's put the ISA hole last if any */
  572. if (isa_hole >= 0 && memno > 0 && isa_hole != (memno-1)) {
  573. struct resource tmp = hose->mem_resources[isa_hole];
  574. hose->mem_resources[isa_hole] = hose->mem_resources[memno-1];
  575. hose->mem_resources[memno-1] = tmp;
  576. }
  577. }
  578. /* Decide whether to display the domain number in /proc */
  579. int pci_proc_domain(struct pci_bus *bus)
  580. {
  581. struct pci_controller *hose = pci_bus_to_host(bus);
  582. #ifdef CONFIG_PPC64
  583. return hose->buid != 0;
  584. #else
  585. if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS))
  586. return 0;
  587. if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0)
  588. return hose->global_number != 0;
  589. return 1;
  590. #endif
  591. }
  592. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  593. struct resource *res)
  594. {
  595. resource_size_t offset = 0, mask = (resource_size_t)-1;
  596. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  597. if (!hose)
  598. return;
  599. if (res->flags & IORESOURCE_IO) {
  600. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  601. mask = 0xffffffffu;
  602. } else if (res->flags & IORESOURCE_MEM)
  603. offset = hose->pci_mem_offset;
  604. region->start = (res->start - offset) & mask;
  605. region->end = (res->end - offset) & mask;
  606. }
  607. EXPORT_SYMBOL(pcibios_resource_to_bus);
  608. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  609. struct pci_bus_region *region)
  610. {
  611. resource_size_t offset = 0, mask = (resource_size_t)-1;
  612. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  613. if (!hose)
  614. return;
  615. if (res->flags & IORESOURCE_IO) {
  616. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  617. mask = 0xffffffffu;
  618. } else if (res->flags & IORESOURCE_MEM)
  619. offset = hose->pci_mem_offset;
  620. res->start = (region->start + offset) & mask;
  621. res->end = (region->end + offset) & mask;
  622. }
  623. EXPORT_SYMBOL(pcibios_bus_to_resource);
  624. /* Fixup a bus resource into a linux resource */
  625. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  626. {
  627. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  628. resource_size_t offset = 0, mask = (resource_size_t)-1;
  629. if (res->flags & IORESOURCE_IO) {
  630. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  631. mask = 0xffffffffu;
  632. } else if (res->flags & IORESOURCE_MEM)
  633. offset = hose->pci_mem_offset;
  634. res->start = (res->start + offset) & mask;
  635. res->end = (res->end + offset) & mask;
  636. pr_debug("PCI:%s %016llx-%016llx\n",
  637. pci_name(dev),
  638. (unsigned long long)res->start,
  639. (unsigned long long)res->end);
  640. }
  641. /* This header fixup will do the resource fixup for all devices as they are
  642. * probed, but not for bridge ranges
  643. */
  644. static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
  645. {
  646. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  647. int i;
  648. if (!hose) {
  649. printk(KERN_ERR "No host bridge for PCI dev %s !\n",
  650. pci_name(dev));
  651. return;
  652. }
  653. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  654. struct resource *res = dev->resource + i;
  655. if (!res->flags)
  656. continue;
  657. if (res->end == 0xffffffff) {
  658. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
  659. pci_name(dev), i,
  660. (unsigned long long)res->start,
  661. (unsigned long long)res->end,
  662. (unsigned int)res->flags);
  663. res->end -= res->start;
  664. res->start = 0;
  665. res->flags |= IORESOURCE_UNSET;
  666. continue;
  667. }
  668. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
  669. pci_name(dev), i,
  670. (unsigned long long)res->start,\
  671. (unsigned long long)res->end,
  672. (unsigned int)res->flags);
  673. fixup_resource(res, dev);
  674. }
  675. /* Call machine specific resource fixup */
  676. if (ppc_md.pcibios_fixup_resources)
  677. ppc_md.pcibios_fixup_resources(dev);
  678. }
  679. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  680. static void __devinit __pcibios_fixup_bus(struct pci_bus *bus)
  681. {
  682. struct pci_controller *hose = pci_bus_to_host(bus);
  683. struct pci_dev *dev = bus->self;
  684. pr_debug("PCI: Fixup bus %d (%s)\n", bus->number, dev ? pci_name(dev) : "PHB");
  685. /* Fixup PCI<->PCI bridges. Host bridges are handled separately, for
  686. * now differently between 32 and 64 bits.
  687. */
  688. if (dev != NULL) {
  689. struct resource *res;
  690. int i;
  691. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  692. if ((res = bus->resource[i]) == NULL)
  693. continue;
  694. if (!res->flags)
  695. continue;
  696. if (i >= 3 && bus->self->transparent)
  697. continue;
  698. /* On PowerMac, Apple leaves bridge windows open over
  699. * an inaccessible region of memory space (0...fffff)
  700. * which is somewhat bogus, but that's what they think
  701. * means disabled...
  702. *
  703. * We clear those to force them to be reallocated later
  704. *
  705. * We detect such regions by the fact that the base is
  706. * equal to the pci_mem_offset of the host bridge and
  707. * their size is smaller than 1M.
  708. */
  709. if (res->flags & IORESOURCE_MEM &&
  710. res->start == hose->pci_mem_offset &&
  711. res->end < 0x100000) {
  712. printk(KERN_INFO
  713. "PCI: Closing bogus Apple Firmware"
  714. " region %d on bus 0x%02x\n",
  715. i, bus->number);
  716. res->flags = 0;
  717. continue;
  718. }
  719. pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
  720. pci_name(dev), i,
  721. (unsigned long long)res->start,\
  722. (unsigned long long)res->end,
  723. (unsigned int)res->flags);
  724. fixup_resource(res, dev);
  725. }
  726. }
  727. /* Additional setup that is different between 32 and 64 bits for now */
  728. pcibios_do_bus_setup(bus);
  729. /* Platform specific bus fixups */
  730. if (ppc_md.pcibios_fixup_bus)
  731. ppc_md.pcibios_fixup_bus(bus);
  732. /* Read default IRQs and fixup if necessary */
  733. list_for_each_entry(dev, &bus->devices, bus_list) {
  734. pci_read_irq_line(dev);
  735. if (ppc_md.pci_irq_fixup)
  736. ppc_md.pci_irq_fixup(dev);
  737. }
  738. }
  739. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  740. {
  741. /* When called from the generic PCI probe, read PCI<->PCI bridge
  742. * bases before proceeding
  743. */
  744. if (bus->self != NULL)
  745. pci_read_bridge_bases(bus);
  746. __pcibios_fixup_bus(bus);
  747. }
  748. EXPORT_SYMBOL(pcibios_fixup_bus);
  749. /* When building a bus from the OF tree rather than probing, we need a
  750. * slightly different version of the fixup which doesn't read the
  751. * bridge bases using config space accesses
  752. */
  753. void __devinit pcibios_fixup_of_probed_bus(struct pci_bus *bus)
  754. {
  755. __pcibios_fixup_bus(bus);
  756. }
  757. static int skip_isa_ioresource_align(struct pci_dev *dev)
  758. {
  759. if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
  760. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  761. return 1;
  762. return 0;
  763. }
  764. /*
  765. * We need to avoid collisions with `mirrored' VGA ports
  766. * and other strange ISA hardware, so we always want the
  767. * addresses to be allocated in the 0x000-0x0ff region
  768. * modulo 0x400.
  769. *
  770. * Why? Because some silly external IO cards only decode
  771. * the low 10 bits of the IO address. The 0x00-0xff region
  772. * is reserved for motherboard devices that decode all 16
  773. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  774. * but we want to try to avoid allocating at 0x2900-0x2bff
  775. * which might have be mirrored at 0x0100-0x03ff..
  776. */
  777. void pcibios_align_resource(void *data, struct resource *res,
  778. resource_size_t size, resource_size_t align)
  779. {
  780. struct pci_dev *dev = data;
  781. if (res->flags & IORESOURCE_IO) {
  782. resource_size_t start = res->start;
  783. if (skip_isa_ioresource_align(dev))
  784. return;
  785. if (start & 0x300) {
  786. start = (start + 0x3ff) & ~0x3ff;
  787. res->start = start;
  788. }
  789. }
  790. }
  791. EXPORT_SYMBOL(pcibios_align_resource);
  792. /*
  793. * Reparent resource children of pr that conflict with res
  794. * under res, and make res replace those children.
  795. */
  796. static int __init reparent_resources(struct resource *parent,
  797. struct resource *res)
  798. {
  799. struct resource *p, **pp;
  800. struct resource **firstpp = NULL;
  801. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  802. if (p->end < res->start)
  803. continue;
  804. if (res->end < p->start)
  805. break;
  806. if (p->start < res->start || p->end > res->end)
  807. return -1; /* not completely contained */
  808. if (firstpp == NULL)
  809. firstpp = pp;
  810. }
  811. if (firstpp == NULL)
  812. return -1; /* didn't find any conflicting entries? */
  813. res->parent = parent;
  814. res->child = *firstpp;
  815. res->sibling = *pp;
  816. *firstpp = res;
  817. *pp = NULL;
  818. for (p = res->child; p != NULL; p = p->sibling) {
  819. p->parent = res;
  820. DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
  821. p->name,
  822. (unsigned long long)p->start,
  823. (unsigned long long)p->end, res->name);
  824. }
  825. return 0;
  826. }
  827. /*
  828. * Handle resources of PCI devices. If the world were perfect, we could
  829. * just allocate all the resource regions and do nothing more. It isn't.
  830. * On the other hand, we cannot just re-allocate all devices, as it would
  831. * require us to know lots of host bridge internals. So we attempt to
  832. * keep as much of the original configuration as possible, but tweak it
  833. * when it's found to be wrong.
  834. *
  835. * Known BIOS problems we have to work around:
  836. * - I/O or memory regions not configured
  837. * - regions configured, but not enabled in the command register
  838. * - bogus I/O addresses above 64K used
  839. * - expansion ROMs left enabled (this may sound harmless, but given
  840. * the fact the PCI specs explicitly allow address decoders to be
  841. * shared between expansion ROMs and other resource regions, it's
  842. * at least dangerous)
  843. *
  844. * Our solution:
  845. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  846. * This gives us fixed barriers on where we can allocate.
  847. * (2) Allocate resources for all enabled devices. If there is
  848. * a collision, just mark the resource as unallocated. Also
  849. * disable expansion ROMs during this step.
  850. * (3) Try to allocate resources for disabled devices. If the
  851. * resources were assigned correctly, everything goes well,
  852. * if they weren't, they won't disturb allocation of other
  853. * resources.
  854. * (4) Assign new addresses to resources which were either
  855. * not configured at all or misconfigured. If explicitly
  856. * requested by the user, configure expansion ROM address
  857. * as well.
  858. */
  859. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  860. {
  861. struct pci_bus *bus;
  862. int i;
  863. struct resource *res, *pr;
  864. /* Depth-First Search on bus tree */
  865. list_for_each_entry(bus, bus_list, node) {
  866. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  867. if ((res = bus->resource[i]) == NULL || !res->flags
  868. || res->start > res->end)
  869. continue;
  870. if (bus->parent == NULL)
  871. pr = (res->flags & IORESOURCE_IO) ?
  872. &ioport_resource : &iomem_resource;
  873. else {
  874. /* Don't bother with non-root busses when
  875. * re-assigning all resources. We clear the
  876. * resource flags as if they were colliding
  877. * and as such ensure proper re-allocation
  878. * later.
  879. */
  880. if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
  881. goto clear_resource;
  882. pr = pci_find_parent_resource(bus->self, res);
  883. if (pr == res) {
  884. /* this happens when the generic PCI
  885. * code (wrongly) decides that this
  886. * bridge is transparent -- paulus
  887. */
  888. continue;
  889. }
  890. }
  891. DBG("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
  892. "[0x%x], parent %p (%s)\n",
  893. bus->self ? pci_name(bus->self) : "PHB",
  894. bus->number, i,
  895. (unsigned long long)res->start,
  896. (unsigned long long)res->end,
  897. (unsigned int)res->flags,
  898. pr, (pr && pr->name) ? pr->name : "nil");
  899. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  900. if (request_resource(pr, res) == 0)
  901. continue;
  902. /*
  903. * Must be a conflict with an existing entry.
  904. * Move that entry (or entries) under the
  905. * bridge resource and try again.
  906. */
  907. if (reparent_resources(pr, res) == 0)
  908. continue;
  909. }
  910. printk(KERN_WARNING
  911. "PCI: Cannot allocate resource region "
  912. "%d of PCI bridge %d, will remap\n",
  913. i, bus->number);
  914. clear_resource:
  915. res->flags = 0;
  916. }
  917. pcibios_allocate_bus_resources(&bus->children);
  918. }
  919. }
  920. static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
  921. {
  922. struct resource *pr, *r = &dev->resource[idx];
  923. DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
  924. pci_name(dev), idx,
  925. (unsigned long long)r->start,
  926. (unsigned long long)r->end,
  927. (unsigned int)r->flags);
  928. pr = pci_find_parent_resource(dev, r);
  929. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  930. request_resource(pr, r) < 0) {
  931. printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
  932. " of device %s, will remap\n", idx, pci_name(dev));
  933. if (pr)
  934. DBG("PCI: parent is %p: %016llx-%016llx [%x]\n", pr,
  935. (unsigned long long)pr->start,
  936. (unsigned long long)pr->end,
  937. (unsigned int)pr->flags);
  938. /* We'll assign a new address later */
  939. r->flags |= IORESOURCE_UNSET;
  940. r->end -= r->start;
  941. r->start = 0;
  942. }
  943. }
  944. static void __init pcibios_allocate_resources(int pass)
  945. {
  946. struct pci_dev *dev = NULL;
  947. int idx, disabled;
  948. u16 command;
  949. struct resource *r;
  950. for_each_pci_dev(dev) {
  951. pci_read_config_word(dev, PCI_COMMAND, &command);
  952. for (idx = 0; idx < 6; idx++) {
  953. r = &dev->resource[idx];
  954. if (r->parent) /* Already allocated */
  955. continue;
  956. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  957. continue; /* Not assigned at all */
  958. if (r->flags & IORESOURCE_IO)
  959. disabled = !(command & PCI_COMMAND_IO);
  960. else
  961. disabled = !(command & PCI_COMMAND_MEMORY);
  962. if (pass == disabled)
  963. alloc_resource(dev, idx);
  964. }
  965. if (pass)
  966. continue;
  967. r = &dev->resource[PCI_ROM_RESOURCE];
  968. if (r->flags & IORESOURCE_ROM_ENABLE) {
  969. /* Turn the ROM off, leave the resource region,
  970. * but keep it unregistered.
  971. */
  972. u32 reg;
  973. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  974. r->flags &= ~IORESOURCE_ROM_ENABLE;
  975. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  976. pci_write_config_dword(dev, dev->rom_base_reg,
  977. reg & ~PCI_ROM_ADDRESS_ENABLE);
  978. }
  979. }
  980. }
  981. void __init pcibios_resource_survey(void)
  982. {
  983. /* Allocate and assign resources. If we re-assign everything, then
  984. * we skip the allocate phase
  985. */
  986. pcibios_allocate_bus_resources(&pci_root_buses);
  987. if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
  988. pcibios_allocate_resources(0);
  989. pcibios_allocate_resources(1);
  990. }
  991. if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
  992. DBG("PCI: Assigning unassigned resouces...\n");
  993. pci_assign_unassigned_resources();
  994. }
  995. /* Call machine dependent fixup */
  996. if (ppc_md.pcibios_fixup)
  997. ppc_md.pcibios_fixup();
  998. }
  999. #ifdef CONFIG_HOTPLUG
  1000. /* This is used by the pSeries hotplug driver to allocate resource
  1001. * of newly plugged busses. We can try to consolidate with the
  1002. * rest of the code later, for now, keep it as-is
  1003. */
  1004. void __devinit pcibios_claim_one_bus(struct pci_bus *bus)
  1005. {
  1006. struct pci_dev *dev;
  1007. struct pci_bus *child_bus;
  1008. list_for_each_entry(dev, &bus->devices, bus_list) {
  1009. int i;
  1010. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  1011. struct resource *r = &dev->resource[i];
  1012. if (r->parent || !r->start || !r->flags)
  1013. continue;
  1014. pci_claim_resource(dev, i);
  1015. }
  1016. }
  1017. list_for_each_entry(child_bus, &bus->children, node)
  1018. pcibios_claim_one_bus(child_bus);
  1019. }
  1020. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  1021. #endif /* CONFIG_HOTPLUG */
  1022. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1023. {
  1024. u16 cmd, old_cmd;
  1025. int idx;
  1026. struct resource *r;
  1027. if (ppc_md.pcibios_enable_device_hook)
  1028. if (ppc_md.pcibios_enable_device_hook(dev))
  1029. return -EINVAL;
  1030. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1031. old_cmd = cmd;
  1032. for (idx = 0; idx < PCI_NUM_RESOURCES; idx++) {
  1033. /* Only set up the requested stuff */
  1034. if (!(mask & (1 << idx)))
  1035. continue;
  1036. r = &dev->resource[idx];
  1037. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  1038. continue;
  1039. if ((idx == PCI_ROM_RESOURCE) &&
  1040. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  1041. continue;
  1042. if (r->parent == NULL) {
  1043. printk(KERN_ERR "PCI: Device %s not available because"
  1044. " of resource collisions\n", pci_name(dev));
  1045. return -EINVAL;
  1046. }
  1047. if (r->flags & IORESOURCE_IO)
  1048. cmd |= PCI_COMMAND_IO;
  1049. if (r->flags & IORESOURCE_MEM)
  1050. cmd |= PCI_COMMAND_MEMORY;
  1051. }
  1052. if (cmd != old_cmd) {
  1053. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1054. pci_name(dev), old_cmd, cmd);
  1055. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1056. }
  1057. return 0;
  1058. }