pci-common.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/string.h>
  21. #include <linux/init.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/mm.h>
  24. #include <linux/list.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/irq.h>
  27. #include <linux/vmalloc.h>
  28. #include <asm/processor.h>
  29. #include <asm/io.h>
  30. #include <asm/prom.h>
  31. #include <asm/pci-bridge.h>
  32. #include <asm/byteorder.h>
  33. static DEFINE_SPINLOCK(hose_spinlock);
  34. LIST_HEAD(hose_list);
  35. /* XXX kill that some day ... */
  36. static int global_phb_number; /* Global phb counter */
  37. /* ISA Memory physical address */
  38. resource_size_t isa_mem_base;
  39. /* Default PCI flags is 0 on ppc32, modified at boot on ppc64 */
  40. unsigned int pci_flags;
  41. static struct dma_map_ops *pci_dma_ops = &dma_direct_ops;
  42. void set_pci_dma_ops(struct dma_map_ops *dma_ops)
  43. {
  44. pci_dma_ops = dma_ops;
  45. }
  46. struct dma_map_ops *get_pci_dma_ops(void)
  47. {
  48. return pci_dma_ops;
  49. }
  50. EXPORT_SYMBOL(get_pci_dma_ops);
  51. int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  52. {
  53. return dma_set_mask(&dev->dev, mask);
  54. }
  55. int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  56. {
  57. int rc;
  58. rc = dma_set_mask(&dev->dev, mask);
  59. dev->dev.coherent_dma_mask = dev->dma_mask;
  60. return rc;
  61. }
  62. struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
  63. {
  64. struct pci_controller *phb;
  65. phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
  66. if (!phb)
  67. return NULL;
  68. spin_lock(&hose_spinlock);
  69. phb->global_number = global_phb_number++;
  70. list_add_tail(&phb->list_node, &hose_list);
  71. spin_unlock(&hose_spinlock);
  72. phb->dn = dev;
  73. phb->is_dynamic = mem_init_done;
  74. return phb;
  75. }
  76. void pcibios_free_controller(struct pci_controller *phb)
  77. {
  78. spin_lock(&hose_spinlock);
  79. list_del(&phb->list_node);
  80. spin_unlock(&hose_spinlock);
  81. if (phb->is_dynamic)
  82. kfree(phb);
  83. }
  84. static resource_size_t pcibios_io_size(const struct pci_controller *hose)
  85. {
  86. return hose->io_resource.end - hose->io_resource.start + 1;
  87. }
  88. int pcibios_vaddr_is_ioport(void __iomem *address)
  89. {
  90. int ret = 0;
  91. struct pci_controller *hose;
  92. resource_size_t size;
  93. spin_lock(&hose_spinlock);
  94. list_for_each_entry(hose, &hose_list, list_node) {
  95. size = pcibios_io_size(hose);
  96. if (address >= hose->io_base_virt &&
  97. address < (hose->io_base_virt + size)) {
  98. ret = 1;
  99. break;
  100. }
  101. }
  102. spin_unlock(&hose_spinlock);
  103. return ret;
  104. }
  105. unsigned long pci_address_to_pio(phys_addr_t address)
  106. {
  107. struct pci_controller *hose;
  108. resource_size_t size;
  109. unsigned long ret = ~0;
  110. spin_lock(&hose_spinlock);
  111. list_for_each_entry(hose, &hose_list, list_node) {
  112. size = pcibios_io_size(hose);
  113. if (address >= hose->io_base_phys &&
  114. address < (hose->io_base_phys + size)) {
  115. unsigned long base =
  116. (unsigned long)hose->io_base_virt - _IO_BASE;
  117. ret = base + (address - hose->io_base_phys);
  118. break;
  119. }
  120. }
  121. spin_unlock(&hose_spinlock);
  122. return ret;
  123. }
  124. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  125. /*
  126. * Return the domain number for this bus.
  127. */
  128. int pci_domain_nr(struct pci_bus *bus)
  129. {
  130. struct pci_controller *hose = pci_bus_to_host(bus);
  131. return hose->global_number;
  132. }
  133. EXPORT_SYMBOL(pci_domain_nr);
  134. /* This routine is meant to be used early during boot, when the
  135. * PCI bus numbers have not yet been assigned, and you need to
  136. * issue PCI config cycles to an OF device.
  137. * It could also be used to "fix" RTAS config cycles if you want
  138. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  139. * config cycles.
  140. */
  141. struct pci_controller *pci_find_hose_for_OF_device(struct device_node *node)
  142. {
  143. while (node) {
  144. struct pci_controller *hose, *tmp;
  145. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  146. if (hose->dn == node)
  147. return hose;
  148. node = node->parent;
  149. }
  150. return NULL;
  151. }
  152. static ssize_t pci_show_devspec(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. struct pci_dev *pdev;
  156. struct device_node *np;
  157. pdev = to_pci_dev(dev);
  158. np = pci_device_to_OF_node(pdev);
  159. if (np == NULL || np->full_name == NULL)
  160. return 0;
  161. return sprintf(buf, "%s", np->full_name);
  162. }
  163. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  164. /* Add sysfs properties */
  165. int pcibios_add_platform_entries(struct pci_dev *pdev)
  166. {
  167. return device_create_file(&pdev->dev, &dev_attr_devspec);
  168. }
  169. char __devinit *pcibios_setup(char *str)
  170. {
  171. return str;
  172. }
  173. /*
  174. * Reads the interrupt pin to determine if interrupt is use by card.
  175. * If the interrupt is used, then gets the interrupt line from the
  176. * openfirmware and sets it in the pci_dev and pci_config line.
  177. */
  178. int pci_read_irq_line(struct pci_dev *pci_dev)
  179. {
  180. struct of_irq oirq;
  181. unsigned int virq;
  182. /* The current device-tree that iSeries generates from the HV
  183. * PCI informations doesn't contain proper interrupt routing,
  184. * and all the fallback would do is print out crap, so we
  185. * don't attempt to resolve the interrupts here at all, some
  186. * iSeries specific fixup does it.
  187. *
  188. * In the long run, we will hopefully fix the generated device-tree
  189. * instead.
  190. */
  191. pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
  192. #ifdef DEBUG
  193. memset(&oirq, 0xff, sizeof(oirq));
  194. #endif
  195. /* Try to get a mapping from the device-tree */
  196. if (of_irq_map_pci(pci_dev, &oirq)) {
  197. u8 line, pin;
  198. /* If that fails, lets fallback to what is in the config
  199. * space and map that through the default controller. We
  200. * also set the type to level low since that's what PCI
  201. * interrupts are. If your platform does differently, then
  202. * either provide a proper interrupt tree or don't use this
  203. * function.
  204. */
  205. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  206. return -1;
  207. if (pin == 0)
  208. return -1;
  209. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  210. line == 0xff || line == 0) {
  211. return -1;
  212. }
  213. pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
  214. line, pin);
  215. virq = irq_create_mapping(NULL, line);
  216. if (virq != NO_IRQ)
  217. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  218. } else {
  219. pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  220. oirq.size, oirq.specifier[0], oirq.specifier[1],
  221. oirq.controller ? oirq.controller->full_name :
  222. "<default>");
  223. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  224. oirq.size);
  225. }
  226. if (virq == NO_IRQ) {
  227. pr_debug(" Failed to map !\n");
  228. return -1;
  229. }
  230. pr_debug(" Mapped to linux irq %d\n", virq);
  231. pci_dev->irq = virq;
  232. return 0;
  233. }
  234. EXPORT_SYMBOL(pci_read_irq_line);
  235. /*
  236. * Platform support for /proc/bus/pci/X/Y mmap()s,
  237. * modelled on the sparc64 implementation by Dave Miller.
  238. * -- paulus.
  239. */
  240. /*
  241. * Adjust vm_pgoff of VMA such that it is the physical page offset
  242. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  243. *
  244. * Basically, the user finds the base address for his device which he wishes
  245. * to mmap. They read the 32-bit value from the config space base register,
  246. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  247. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  248. *
  249. * Returns negative error code on failure, zero on success.
  250. */
  251. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  252. resource_size_t *offset,
  253. enum pci_mmap_state mmap_state)
  254. {
  255. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  256. unsigned long io_offset = 0;
  257. int i, res_bit;
  258. if (hose == 0)
  259. return NULL; /* should never happen */
  260. /* If memory, add on the PCI bridge address offset */
  261. if (mmap_state == pci_mmap_mem) {
  262. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  263. *offset += hose->pci_mem_offset;
  264. #endif
  265. res_bit = IORESOURCE_MEM;
  266. } else {
  267. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  268. *offset += io_offset;
  269. res_bit = IORESOURCE_IO;
  270. }
  271. /*
  272. * Check that the offset requested corresponds to one of the
  273. * resources of the device.
  274. */
  275. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  276. struct resource *rp = &dev->resource[i];
  277. int flags = rp->flags;
  278. /* treat ROM as memory (should be already) */
  279. if (i == PCI_ROM_RESOURCE)
  280. flags |= IORESOURCE_MEM;
  281. /* Active and same type? */
  282. if ((flags & res_bit) == 0)
  283. continue;
  284. /* In the range of this resource? */
  285. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  286. continue;
  287. /* found it! construct the final physical address */
  288. if (mmap_state == pci_mmap_io)
  289. *offset += hose->io_base_phys - io_offset;
  290. return rp;
  291. }
  292. return NULL;
  293. }
  294. /*
  295. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  296. * device mapping.
  297. */
  298. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  299. pgprot_t protection,
  300. enum pci_mmap_state mmap_state,
  301. int write_combine)
  302. {
  303. pgprot_t prot = protection;
  304. /* Write combine is always 0 on non-memory space mappings. On
  305. * memory space, if the user didn't pass 1, we check for a
  306. * "prefetchable" resource. This is a bit hackish, but we use
  307. * this to workaround the inability of /sysfs to provide a write
  308. * combine bit
  309. */
  310. if (mmap_state != pci_mmap_mem)
  311. write_combine = 0;
  312. else if (write_combine == 0) {
  313. if (rp->flags & IORESOURCE_PREFETCH)
  314. write_combine = 1;
  315. }
  316. return pgprot_noncached(prot);
  317. }
  318. /*
  319. * This one is used by /dev/mem and fbdev who have no clue about the
  320. * PCI device, it tries to find the PCI device first and calls the
  321. * above routine
  322. */
  323. pgprot_t pci_phys_mem_access_prot(struct file *file,
  324. unsigned long pfn,
  325. unsigned long size,
  326. pgprot_t prot)
  327. {
  328. struct pci_dev *pdev = NULL;
  329. struct resource *found = NULL;
  330. resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
  331. int i;
  332. if (page_is_ram(pfn))
  333. return prot;
  334. prot = pgprot_noncached(prot);
  335. for_each_pci_dev(pdev) {
  336. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  337. struct resource *rp = &pdev->resource[i];
  338. int flags = rp->flags;
  339. /* Active and same type? */
  340. if ((flags & IORESOURCE_MEM) == 0)
  341. continue;
  342. /* In the range of this resource? */
  343. if (offset < (rp->start & PAGE_MASK) ||
  344. offset > rp->end)
  345. continue;
  346. found = rp;
  347. break;
  348. }
  349. if (found)
  350. break;
  351. }
  352. if (found) {
  353. if (found->flags & IORESOURCE_PREFETCH)
  354. prot = pgprot_noncached_wc(prot);
  355. pci_dev_put(pdev);
  356. }
  357. pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
  358. (unsigned long long)offset, pgprot_val(prot));
  359. return prot;
  360. }
  361. /*
  362. * Perform the actual remap of the pages for a PCI device mapping, as
  363. * appropriate for this architecture. The region in the process to map
  364. * is described by vm_start and vm_end members of VMA, the base physical
  365. * address is found in vm_pgoff.
  366. * The pci device structure is provided so that architectures may make mapping
  367. * decisions on a per-device or per-bus basis.
  368. *
  369. * Returns a negative error code on failure, zero on success.
  370. */
  371. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  372. enum pci_mmap_state mmap_state, int write_combine)
  373. {
  374. resource_size_t offset =
  375. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  376. struct resource *rp;
  377. int ret;
  378. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  379. if (rp == NULL)
  380. return -EINVAL;
  381. vma->vm_pgoff = offset >> PAGE_SHIFT;
  382. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  383. vma->vm_page_prot,
  384. mmap_state, write_combine);
  385. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  386. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  387. return ret;
  388. }
  389. /* This provides legacy IO read access on a bus */
  390. int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
  391. {
  392. unsigned long offset;
  393. struct pci_controller *hose = pci_bus_to_host(bus);
  394. struct resource *rp = &hose->io_resource;
  395. void __iomem *addr;
  396. /* Check if port can be supported by that bus. We only check
  397. * the ranges of the PHB though, not the bus itself as the rules
  398. * for forwarding legacy cycles down bridges are not our problem
  399. * here. So if the host bridge supports it, we do it.
  400. */
  401. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  402. offset += port;
  403. if (!(rp->flags & IORESOURCE_IO))
  404. return -ENXIO;
  405. if (offset < rp->start || (offset + size) > rp->end)
  406. return -ENXIO;
  407. addr = hose->io_base_virt + port;
  408. switch (size) {
  409. case 1:
  410. *((u8 *)val) = in_8(addr);
  411. return 1;
  412. case 2:
  413. if (port & 1)
  414. return -EINVAL;
  415. *((u16 *)val) = in_le16(addr);
  416. return 2;
  417. case 4:
  418. if (port & 3)
  419. return -EINVAL;
  420. *((u32 *)val) = in_le32(addr);
  421. return 4;
  422. }
  423. return -EINVAL;
  424. }
  425. /* This provides legacy IO write access on a bus */
  426. int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
  427. {
  428. unsigned long offset;
  429. struct pci_controller *hose = pci_bus_to_host(bus);
  430. struct resource *rp = &hose->io_resource;
  431. void __iomem *addr;
  432. /* Check if port can be supported by that bus. We only check
  433. * the ranges of the PHB though, not the bus itself as the rules
  434. * for forwarding legacy cycles down bridges are not our problem
  435. * here. So if the host bridge supports it, we do it.
  436. */
  437. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  438. offset += port;
  439. if (!(rp->flags & IORESOURCE_IO))
  440. return -ENXIO;
  441. if (offset < rp->start || (offset + size) > rp->end)
  442. return -ENXIO;
  443. addr = hose->io_base_virt + port;
  444. /* WARNING: The generic code is idiotic. It gets passed a pointer
  445. * to what can be a 1, 2 or 4 byte quantity and always reads that
  446. * as a u32, which means that we have to correct the location of
  447. * the data read within those 32 bits for size 1 and 2
  448. */
  449. switch (size) {
  450. case 1:
  451. out_8(addr, val >> 24);
  452. return 1;
  453. case 2:
  454. if (port & 1)
  455. return -EINVAL;
  456. out_le16(addr, val >> 16);
  457. return 2;
  458. case 4:
  459. if (port & 3)
  460. return -EINVAL;
  461. out_le32(addr, val);
  462. return 4;
  463. }
  464. return -EINVAL;
  465. }
  466. /* This provides legacy IO or memory mmap access on a bus */
  467. int pci_mmap_legacy_page_range(struct pci_bus *bus,
  468. struct vm_area_struct *vma,
  469. enum pci_mmap_state mmap_state)
  470. {
  471. struct pci_controller *hose = pci_bus_to_host(bus);
  472. resource_size_t offset =
  473. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  474. resource_size_t size = vma->vm_end - vma->vm_start;
  475. struct resource *rp;
  476. pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
  477. pci_domain_nr(bus), bus->number,
  478. mmap_state == pci_mmap_mem ? "MEM" : "IO",
  479. (unsigned long long)offset,
  480. (unsigned long long)(offset + size - 1));
  481. if (mmap_state == pci_mmap_mem) {
  482. /* Hack alert !
  483. *
  484. * Because X is lame and can fail starting if it gets an error
  485. * trying to mmap legacy_mem (instead of just moving on without
  486. * legacy memory access) we fake it here by giving it anonymous
  487. * memory, effectively behaving just like /dev/zero
  488. */
  489. if ((offset + size) > hose->isa_mem_size) {
  490. #ifdef CONFIG_MMU
  491. printk(KERN_DEBUG
  492. "Process %s (pid:%d) mapped non-existing PCI"
  493. "legacy memory for 0%04x:%02x\n",
  494. current->comm, current->pid, pci_domain_nr(bus),
  495. bus->number);
  496. #endif
  497. if (vma->vm_flags & VM_SHARED)
  498. return shmem_zero_setup(vma);
  499. return 0;
  500. }
  501. offset += hose->isa_mem_phys;
  502. } else {
  503. unsigned long io_offset = (unsigned long)hose->io_base_virt - \
  504. _IO_BASE;
  505. unsigned long roffset = offset + io_offset;
  506. rp = &hose->io_resource;
  507. if (!(rp->flags & IORESOURCE_IO))
  508. return -ENXIO;
  509. if (roffset < rp->start || (roffset + size) > rp->end)
  510. return -ENXIO;
  511. offset += hose->io_base_phys;
  512. }
  513. pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
  514. vma->vm_pgoff = offset >> PAGE_SHIFT;
  515. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  516. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  517. vma->vm_end - vma->vm_start,
  518. vma->vm_page_prot);
  519. }
  520. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  521. const struct resource *rsrc,
  522. resource_size_t *start, resource_size_t *end)
  523. {
  524. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  525. resource_size_t offset = 0;
  526. if (hose == NULL)
  527. return;
  528. if (rsrc->flags & IORESOURCE_IO)
  529. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  530. /* We pass a fully fixed up address to userland for MMIO instead of
  531. * a BAR value because X is lame and expects to be able to use that
  532. * to pass to /dev/mem !
  533. *
  534. * That means that we'll have potentially 64 bits values where some
  535. * userland apps only expect 32 (like X itself since it thinks only
  536. * Sparc has 64 bits MMIO) but if we don't do that, we break it on
  537. * 32 bits CHRPs :-(
  538. *
  539. * Hopefully, the sysfs insterface is immune to that gunk. Once X
  540. * has been fixed (and the fix spread enough), we can re-enable the
  541. * 2 lines below and pass down a BAR value to userland. In that case
  542. * we'll also have to re-enable the matching code in
  543. * __pci_mmap_make_offset().
  544. *
  545. * BenH.
  546. */
  547. #if 0
  548. else if (rsrc->flags & IORESOURCE_MEM)
  549. offset = hose->pci_mem_offset;
  550. #endif
  551. *start = rsrc->start - offset;
  552. *end = rsrc->end - offset;
  553. }
  554. /**
  555. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  556. * @hose: newly allocated pci_controller to be setup
  557. * @dev: device node of the host bridge
  558. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  559. *
  560. * This function will parse the "ranges" property of a PCI host bridge device
  561. * node and setup the resource mapping of a pci controller based on its
  562. * content.
  563. *
  564. * Life would be boring if it wasn't for a few issues that we have to deal
  565. * with here:
  566. *
  567. * - We can only cope with one IO space range and up to 3 Memory space
  568. * ranges. However, some machines (thanks Apple !) tend to split their
  569. * space into lots of small contiguous ranges. So we have to coalesce.
  570. *
  571. * - We can only cope with all memory ranges having the same offset
  572. * between CPU addresses and PCI addresses. Unfortunately, some bridges
  573. * are setup for a large 1:1 mapping along with a small "window" which
  574. * maps PCI address 0 to some arbitrary high address of the CPU space in
  575. * order to give access to the ISA memory hole.
  576. * The way out of here that I've chosen for now is to always set the
  577. * offset based on the first resource found, then override it if we
  578. * have a different offset and the previous was set by an ISA hole.
  579. *
  580. * - Some busses have IO space not starting at 0, which causes trouble with
  581. * the way we do our IO resource renumbering. The code somewhat deals with
  582. * it for 64 bits but I would expect problems on 32 bits.
  583. *
  584. * - Some 32 bits platforms such as 4xx can have physical space larger than
  585. * 32 bits so we need to use 64 bits values for the parsing
  586. */
  587. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  588. struct device_node *dev,
  589. int primary)
  590. {
  591. const u32 *ranges;
  592. int rlen;
  593. int pna = of_n_addr_cells(dev);
  594. int np = pna + 5;
  595. int memno = 0, isa_hole = -1;
  596. u32 pci_space;
  597. unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
  598. unsigned long long isa_mb = 0;
  599. struct resource *res;
  600. printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
  601. dev->full_name, primary ? "(primary)" : "");
  602. /* Get ranges property */
  603. ranges = of_get_property(dev, "ranges", &rlen);
  604. if (ranges == NULL)
  605. return;
  606. /* Parse it */
  607. pr_debug("Parsing ranges property...\n");
  608. while ((rlen -= np * 4) >= 0) {
  609. /* Read next ranges element */
  610. pci_space = ranges[0];
  611. pci_addr = of_read_number(ranges + 1, 2);
  612. cpu_addr = of_translate_address(dev, ranges + 3);
  613. size = of_read_number(ranges + pna + 3, 2);
  614. pr_debug("pci_space: 0x%08x pci_addr:0x%016llx "
  615. "cpu_addr:0x%016llx size:0x%016llx\n",
  616. pci_space, pci_addr, cpu_addr, size);
  617. ranges += np;
  618. /* If we failed translation or got a zero-sized region
  619. * (some FW try to feed us with non sensical zero sized regions
  620. * such as power3 which look like some kind of attempt
  621. * at exposing the VGA memory hole)
  622. */
  623. if (cpu_addr == OF_BAD_ADDR || size == 0)
  624. continue;
  625. /* Now consume following elements while they are contiguous */
  626. for (; rlen >= np * sizeof(u32);
  627. ranges += np, rlen -= np * 4) {
  628. if (ranges[0] != pci_space)
  629. break;
  630. pci_next = of_read_number(ranges + 1, 2);
  631. cpu_next = of_translate_address(dev, ranges + 3);
  632. if (pci_next != pci_addr + size ||
  633. cpu_next != cpu_addr + size)
  634. break;
  635. size += of_read_number(ranges + pna + 3, 2);
  636. }
  637. /* Act based on address space type */
  638. res = NULL;
  639. switch ((pci_space >> 24) & 0x3) {
  640. case 1: /* PCI IO space */
  641. printk(KERN_INFO
  642. " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  643. cpu_addr, cpu_addr + size - 1, pci_addr);
  644. /* We support only one IO range */
  645. if (hose->pci_io_size) {
  646. printk(KERN_INFO
  647. " \\--> Skipped (too many) !\n");
  648. continue;
  649. }
  650. /* On 32 bits, limit I/O space to 16MB */
  651. if (size > 0x01000000)
  652. size = 0x01000000;
  653. /* 32 bits needs to map IOs here */
  654. hose->io_base_virt = ioremap(cpu_addr, size);
  655. /* Expect trouble if pci_addr is not 0 */
  656. if (primary)
  657. isa_io_base =
  658. (unsigned long)hose->io_base_virt;
  659. /* pci_io_size and io_base_phys always represent IO
  660. * space starting at 0 so we factor in pci_addr
  661. */
  662. hose->pci_io_size = pci_addr + size;
  663. hose->io_base_phys = cpu_addr - pci_addr;
  664. /* Build resource */
  665. res = &hose->io_resource;
  666. res->flags = IORESOURCE_IO;
  667. res->start = pci_addr;
  668. break;
  669. case 2: /* PCI Memory space */
  670. case 3: /* PCI 64 bits Memory space */
  671. printk(KERN_INFO
  672. " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  673. cpu_addr, cpu_addr + size - 1, pci_addr,
  674. (pci_space & 0x40000000) ? "Prefetch" : "");
  675. /* We support only 3 memory ranges */
  676. if (memno >= 3) {
  677. printk(KERN_INFO
  678. " \\--> Skipped (too many) !\n");
  679. continue;
  680. }
  681. /* Handles ISA memory hole space here */
  682. if (pci_addr == 0) {
  683. isa_mb = cpu_addr;
  684. isa_hole = memno;
  685. if (primary || isa_mem_base == 0)
  686. isa_mem_base = cpu_addr;
  687. hose->isa_mem_phys = cpu_addr;
  688. hose->isa_mem_size = size;
  689. }
  690. /* We get the PCI/Mem offset from the first range or
  691. * the, current one if the offset came from an ISA
  692. * hole. If they don't match, bugger.
  693. */
  694. if (memno == 0 ||
  695. (isa_hole >= 0 && pci_addr != 0 &&
  696. hose->pci_mem_offset == isa_mb))
  697. hose->pci_mem_offset = cpu_addr - pci_addr;
  698. else if (pci_addr != 0 &&
  699. hose->pci_mem_offset != cpu_addr - pci_addr) {
  700. printk(KERN_INFO
  701. " \\--> Skipped (offset mismatch) !\n");
  702. continue;
  703. }
  704. /* Build resource */
  705. res = &hose->mem_resources[memno++];
  706. res->flags = IORESOURCE_MEM;
  707. if (pci_space & 0x40000000)
  708. res->flags |= IORESOURCE_PREFETCH;
  709. res->start = cpu_addr;
  710. break;
  711. }
  712. if (res != NULL) {
  713. res->name = dev->full_name;
  714. res->end = res->start + size - 1;
  715. res->parent = NULL;
  716. res->sibling = NULL;
  717. res->child = NULL;
  718. }
  719. }
  720. /* If there's an ISA hole and the pci_mem_offset is -not- matching
  721. * the ISA hole offset, then we need to remove the ISA hole from
  722. * the resource list for that brige
  723. */
  724. if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
  725. unsigned int next = isa_hole + 1;
  726. printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb);
  727. if (next < memno)
  728. memmove(&hose->mem_resources[isa_hole],
  729. &hose->mem_resources[next],
  730. sizeof(struct resource) * (memno - next));
  731. hose->mem_resources[--memno].flags = 0;
  732. }
  733. }
  734. /* Decide whether to display the domain number in /proc */
  735. int pci_proc_domain(struct pci_bus *bus)
  736. {
  737. struct pci_controller *hose = pci_bus_to_host(bus);
  738. if (!(pci_flags & PCI_ENABLE_PROC_DOMAINS))
  739. return 0;
  740. if (pci_flags & PCI_COMPAT_DOMAIN_0)
  741. return hose->global_number != 0;
  742. return 1;
  743. }
  744. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  745. struct resource *res)
  746. {
  747. resource_size_t offset = 0, mask = (resource_size_t)-1;
  748. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  749. if (!hose)
  750. return;
  751. if (res->flags & IORESOURCE_IO) {
  752. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  753. mask = 0xffffffffu;
  754. } else if (res->flags & IORESOURCE_MEM)
  755. offset = hose->pci_mem_offset;
  756. region->start = (res->start - offset) & mask;
  757. region->end = (res->end - offset) & mask;
  758. }
  759. EXPORT_SYMBOL(pcibios_resource_to_bus);
  760. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  761. struct pci_bus_region *region)
  762. {
  763. resource_size_t offset = 0, mask = (resource_size_t)-1;
  764. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  765. if (!hose)
  766. return;
  767. if (res->flags & IORESOURCE_IO) {
  768. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  769. mask = 0xffffffffu;
  770. } else if (res->flags & IORESOURCE_MEM)
  771. offset = hose->pci_mem_offset;
  772. res->start = (region->start + offset) & mask;
  773. res->end = (region->end + offset) & mask;
  774. }
  775. EXPORT_SYMBOL(pcibios_bus_to_resource);
  776. /* Fixup a bus resource into a linux resource */
  777. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  778. {
  779. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  780. resource_size_t offset = 0, mask = (resource_size_t)-1;
  781. if (res->flags & IORESOURCE_IO) {
  782. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  783. mask = 0xffffffffu;
  784. } else if (res->flags & IORESOURCE_MEM)
  785. offset = hose->pci_mem_offset;
  786. res->start = (res->start + offset) & mask;
  787. res->end = (res->end + offset) & mask;
  788. }
  789. /* This header fixup will do the resource fixup for all devices as they are
  790. * probed, but not for bridge ranges
  791. */
  792. static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
  793. {
  794. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  795. int i;
  796. if (!hose) {
  797. printk(KERN_ERR "No host bridge for PCI dev %s !\n",
  798. pci_name(dev));
  799. return;
  800. }
  801. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  802. struct resource *res = dev->resource + i;
  803. if (!res->flags)
  804. continue;
  805. /* On platforms that have PCI_PROBE_ONLY set, we don't
  806. * consider 0 as an unassigned BAR value. It's technically
  807. * a valid value, but linux doesn't like it... so when we can
  808. * re-assign things, we do so, but if we can't, we keep it
  809. * around and hope for the best...
  810. */
  811. if (res->start == 0 && !(pci_flags & PCI_PROBE_ONLY)) {
  812. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x]" \
  813. "is unassigned\n",
  814. pci_name(dev), i,
  815. (unsigned long long)res->start,
  816. (unsigned long long)res->end,
  817. (unsigned int)res->flags);
  818. res->end -= res->start;
  819. res->start = 0;
  820. res->flags |= IORESOURCE_UNSET;
  821. continue;
  822. }
  823. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
  824. pci_name(dev), i,
  825. (unsigned long long)res->start,\
  826. (unsigned long long)res->end,
  827. (unsigned int)res->flags);
  828. fixup_resource(res, dev);
  829. pr_debug("PCI:%s %016llx-%016llx\n",
  830. pci_name(dev),
  831. (unsigned long long)res->start,
  832. (unsigned long long)res->end);
  833. }
  834. }
  835. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  836. /* This function tries to figure out if a bridge resource has been initialized
  837. * by the firmware or not. It doesn't have to be absolutely bullet proof, but
  838. * things go more smoothly when it gets it right. It should covers cases such
  839. * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
  840. */
  841. static int __devinit pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
  842. struct resource *res)
  843. {
  844. struct pci_controller *hose = pci_bus_to_host(bus);
  845. struct pci_dev *dev = bus->self;
  846. resource_size_t offset;
  847. u16 command;
  848. int i;
  849. /* We don't do anything if PCI_PROBE_ONLY is set */
  850. if (pci_flags & PCI_PROBE_ONLY)
  851. return 0;
  852. /* Job is a bit different between memory and IO */
  853. if (res->flags & IORESOURCE_MEM) {
  854. /* If the BAR is non-0 (res != pci_mem_offset) then it's
  855. * probably been initialized by somebody
  856. */
  857. if (res->start != hose->pci_mem_offset)
  858. return 0;
  859. /* The BAR is 0, let's check if memory decoding is enabled on
  860. * the bridge. If not, we consider it unassigned
  861. */
  862. pci_read_config_word(dev, PCI_COMMAND, &command);
  863. if ((command & PCI_COMMAND_MEMORY) == 0)
  864. return 1;
  865. /* Memory decoding is enabled and the BAR is 0. If any of
  866. * the bridge resources covers that starting address (0 then
  867. * it's good enough for us for memory
  868. */
  869. for (i = 0; i < 3; i++) {
  870. if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
  871. hose->mem_resources[i].start == hose->pci_mem_offset)
  872. return 0;
  873. }
  874. /* Well, it starts at 0 and we know it will collide so we may as
  875. * well consider it as unassigned. That covers the Apple case.
  876. */
  877. return 1;
  878. } else {
  879. /* If the BAR is non-0, then we consider it assigned */
  880. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  881. if (((res->start - offset) & 0xfffffffful) != 0)
  882. return 0;
  883. /* Here, we are a bit different than memory as typically IO
  884. * space starting at low addresses -is- valid. What we do
  885. * instead if that we consider as unassigned anything that
  886. * doesn't have IO enabled in the PCI command register,
  887. * and that's it.
  888. */
  889. pci_read_config_word(dev, PCI_COMMAND, &command);
  890. if (command & PCI_COMMAND_IO)
  891. return 0;
  892. /* It's starting at 0 and IO is disabled in the bridge, consider
  893. * it unassigned
  894. */
  895. return 1;
  896. }
  897. }
  898. /* Fixup resources of a PCI<->PCI bridge */
  899. static void __devinit pcibios_fixup_bridge(struct pci_bus *bus)
  900. {
  901. struct resource *res;
  902. int i;
  903. struct pci_dev *dev = bus->self;
  904. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  905. res = bus->resource[i];
  906. if (!res)
  907. continue;
  908. if (!res->flags)
  909. continue;
  910. if (i >= 3 && bus->self->transparent)
  911. continue;
  912. pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
  913. pci_name(dev), i,
  914. (unsigned long long)res->start,\
  915. (unsigned long long)res->end,
  916. (unsigned int)res->flags);
  917. /* Perform fixup */
  918. fixup_resource(res, dev);
  919. /* Try to detect uninitialized P2P bridge resources,
  920. * and clear them out so they get re-assigned later
  921. */
  922. if (pcibios_uninitialized_bridge_resource(bus, res)) {
  923. res->flags = 0;
  924. pr_debug("PCI:%s (unassigned)\n",
  925. pci_name(dev));
  926. } else {
  927. pr_debug("PCI:%s %016llx-%016llx\n",
  928. pci_name(dev),
  929. (unsigned long long)res->start,
  930. (unsigned long long)res->end);
  931. }
  932. }
  933. }
  934. void __devinit pcibios_setup_bus_self(struct pci_bus *bus)
  935. {
  936. /* Fix up the bus resources for P2P bridges */
  937. if (bus->self != NULL)
  938. pcibios_fixup_bridge(bus);
  939. }
  940. void __devinit pcibios_setup_bus_devices(struct pci_bus *bus)
  941. {
  942. struct pci_dev *dev;
  943. pr_debug("PCI: Fixup bus devices %d (%s)\n",
  944. bus->number, bus->self ? pci_name(bus->self) : "PHB");
  945. list_for_each_entry(dev, &bus->devices, bus_list) {
  946. struct dev_archdata *sd = &dev->dev.archdata;
  947. /* Setup OF node pointer in archdata */
  948. sd->of_node = pci_device_to_OF_node(dev);
  949. /* Fixup NUMA node as it may not be setup yet by the generic
  950. * code and is needed by the DMA init
  951. */
  952. set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
  953. /* Hook up default DMA ops */
  954. sd->dma_ops = pci_dma_ops;
  955. sd->dma_data = (void *)PCI_DRAM_OFFSET;
  956. /* Read default IRQs and fixup if necessary */
  957. pci_read_irq_line(dev);
  958. }
  959. }
  960. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  961. {
  962. /* When called from the generic PCI probe, read PCI<->PCI bridge
  963. * bases. This is -not- called when generating the PCI tree from
  964. * the OF device-tree.
  965. */
  966. if (bus->self != NULL)
  967. pci_read_bridge_bases(bus);
  968. /* Now fixup the bus bus */
  969. pcibios_setup_bus_self(bus);
  970. /* Now fixup devices on that bus */
  971. pcibios_setup_bus_devices(bus);
  972. }
  973. EXPORT_SYMBOL(pcibios_fixup_bus);
  974. static int skip_isa_ioresource_align(struct pci_dev *dev)
  975. {
  976. if ((pci_flags & PCI_CAN_SKIP_ISA_ALIGN) &&
  977. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  978. return 1;
  979. return 0;
  980. }
  981. /*
  982. * We need to avoid collisions with `mirrored' VGA ports
  983. * and other strange ISA hardware, so we always want the
  984. * addresses to be allocated in the 0x000-0x0ff region
  985. * modulo 0x400.
  986. *
  987. * Why? Because some silly external IO cards only decode
  988. * the low 10 bits of the IO address. The 0x00-0xff region
  989. * is reserved for motherboard devices that decode all 16
  990. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  991. * but we want to try to avoid allocating at 0x2900-0x2bff
  992. * which might have be mirrored at 0x0100-0x03ff..
  993. */
  994. void pcibios_align_resource(void *data, struct resource *res,
  995. resource_size_t size, resource_size_t align)
  996. {
  997. struct pci_dev *dev = data;
  998. if (res->flags & IORESOURCE_IO) {
  999. resource_size_t start = res->start;
  1000. if (skip_isa_ioresource_align(dev))
  1001. return;
  1002. if (start & 0x300) {
  1003. start = (start + 0x3ff) & ~0x3ff;
  1004. res->start = start;
  1005. }
  1006. }
  1007. }
  1008. EXPORT_SYMBOL(pcibios_align_resource);
  1009. /*
  1010. * Reparent resource children of pr that conflict with res
  1011. * under res, and make res replace those children.
  1012. */
  1013. static int __init reparent_resources(struct resource *parent,
  1014. struct resource *res)
  1015. {
  1016. struct resource *p, **pp;
  1017. struct resource **firstpp = NULL;
  1018. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  1019. if (p->end < res->start)
  1020. continue;
  1021. if (res->end < p->start)
  1022. break;
  1023. if (p->start < res->start || p->end > res->end)
  1024. return -1; /* not completely contained */
  1025. if (firstpp == NULL)
  1026. firstpp = pp;
  1027. }
  1028. if (firstpp == NULL)
  1029. return -1; /* didn't find any conflicting entries? */
  1030. res->parent = parent;
  1031. res->child = *firstpp;
  1032. res->sibling = *pp;
  1033. *firstpp = res;
  1034. *pp = NULL;
  1035. for (p = res->child; p != NULL; p = p->sibling) {
  1036. p->parent = res;
  1037. pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n",
  1038. p->name,
  1039. (unsigned long long)p->start,
  1040. (unsigned long long)p->end, res->name);
  1041. }
  1042. return 0;
  1043. }
  1044. /*
  1045. * Handle resources of PCI devices. If the world were perfect, we could
  1046. * just allocate all the resource regions and do nothing more. It isn't.
  1047. * On the other hand, we cannot just re-allocate all devices, as it would
  1048. * require us to know lots of host bridge internals. So we attempt to
  1049. * keep as much of the original configuration as possible, but tweak it
  1050. * when it's found to be wrong.
  1051. *
  1052. * Known BIOS problems we have to work around:
  1053. * - I/O or memory regions not configured
  1054. * - regions configured, but not enabled in the command register
  1055. * - bogus I/O addresses above 64K used
  1056. * - expansion ROMs left enabled (this may sound harmless, but given
  1057. * the fact the PCI specs explicitly allow address decoders to be
  1058. * shared between expansion ROMs and other resource regions, it's
  1059. * at least dangerous)
  1060. *
  1061. * Our solution:
  1062. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  1063. * This gives us fixed barriers on where we can allocate.
  1064. * (2) Allocate resources for all enabled devices. If there is
  1065. * a collision, just mark the resource as unallocated. Also
  1066. * disable expansion ROMs during this step.
  1067. * (3) Try to allocate resources for disabled devices. If the
  1068. * resources were assigned correctly, everything goes well,
  1069. * if they weren't, they won't disturb allocation of other
  1070. * resources.
  1071. * (4) Assign new addresses to resources which were either
  1072. * not configured at all or misconfigured. If explicitly
  1073. * requested by the user, configure expansion ROM address
  1074. * as well.
  1075. */
  1076. void pcibios_allocate_bus_resources(struct pci_bus *bus)
  1077. {
  1078. struct pci_bus *b;
  1079. int i;
  1080. struct resource *res, *pr;
  1081. pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
  1082. pci_domain_nr(bus), bus->number);
  1083. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  1084. res = bus->resource[i];
  1085. if (!res || !res->flags
  1086. || res->start > res->end || res->parent)
  1087. continue;
  1088. if (bus->parent == NULL)
  1089. pr = (res->flags & IORESOURCE_IO) ?
  1090. &ioport_resource : &iomem_resource;
  1091. else {
  1092. /* Don't bother with non-root busses when
  1093. * re-assigning all resources. We clear the
  1094. * resource flags as if they were colliding
  1095. * and as such ensure proper re-allocation
  1096. * later.
  1097. */
  1098. if (pci_flags & PCI_REASSIGN_ALL_RSRC)
  1099. goto clear_resource;
  1100. pr = pci_find_parent_resource(bus->self, res);
  1101. if (pr == res) {
  1102. /* this happens when the generic PCI
  1103. * code (wrongly) decides that this
  1104. * bridge is transparent -- paulus
  1105. */
  1106. continue;
  1107. }
  1108. }
  1109. pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
  1110. "[0x%x], parent %p (%s)\n",
  1111. bus->self ? pci_name(bus->self) : "PHB",
  1112. bus->number, i,
  1113. (unsigned long long)res->start,
  1114. (unsigned long long)res->end,
  1115. (unsigned int)res->flags,
  1116. pr, (pr && pr->name) ? pr->name : "nil");
  1117. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  1118. if (request_resource(pr, res) == 0)
  1119. continue;
  1120. /*
  1121. * Must be a conflict with an existing entry.
  1122. * Move that entry (or entries) under the
  1123. * bridge resource and try again.
  1124. */
  1125. if (reparent_resources(pr, res) == 0)
  1126. continue;
  1127. }
  1128. printk(KERN_WARNING "PCI: Cannot allocate resource region "
  1129. "%d of PCI bridge %d, will remap\n", i, bus->number);
  1130. clear_resource:
  1131. res->flags = 0;
  1132. }
  1133. list_for_each_entry(b, &bus->children, node)
  1134. pcibios_allocate_bus_resources(b);
  1135. }
  1136. static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
  1137. {
  1138. struct resource *pr, *r = &dev->resource[idx];
  1139. pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
  1140. pci_name(dev), idx,
  1141. (unsigned long long)r->start,
  1142. (unsigned long long)r->end,
  1143. (unsigned int)r->flags);
  1144. pr = pci_find_parent_resource(dev, r);
  1145. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  1146. request_resource(pr, r) < 0) {
  1147. printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
  1148. " of device %s, will remap\n", idx, pci_name(dev));
  1149. if (pr)
  1150. pr_debug("PCI: parent is %p: %016llx-%016llx [%x]\n",
  1151. pr,
  1152. (unsigned long long)pr->start,
  1153. (unsigned long long)pr->end,
  1154. (unsigned int)pr->flags);
  1155. /* We'll assign a new address later */
  1156. r->flags |= IORESOURCE_UNSET;
  1157. r->end -= r->start;
  1158. r->start = 0;
  1159. }
  1160. }
  1161. static void __init pcibios_allocate_resources(int pass)
  1162. {
  1163. struct pci_dev *dev = NULL;
  1164. int idx, disabled;
  1165. u16 command;
  1166. struct resource *r;
  1167. for_each_pci_dev(dev) {
  1168. pci_read_config_word(dev, PCI_COMMAND, &command);
  1169. for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
  1170. r = &dev->resource[idx];
  1171. if (r->parent) /* Already allocated */
  1172. continue;
  1173. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  1174. continue; /* Not assigned at all */
  1175. /* We only allocate ROMs on pass 1 just in case they
  1176. * have been screwed up by firmware
  1177. */
  1178. if (idx == PCI_ROM_RESOURCE)
  1179. disabled = 1;
  1180. if (r->flags & IORESOURCE_IO)
  1181. disabled = !(command & PCI_COMMAND_IO);
  1182. else
  1183. disabled = !(command & PCI_COMMAND_MEMORY);
  1184. if (pass == disabled)
  1185. alloc_resource(dev, idx);
  1186. }
  1187. if (pass)
  1188. continue;
  1189. r = &dev->resource[PCI_ROM_RESOURCE];
  1190. if (r->flags) {
  1191. /* Turn the ROM off, leave the resource region,
  1192. * but keep it unregistered.
  1193. */
  1194. u32 reg;
  1195. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  1196. if (reg & PCI_ROM_ADDRESS_ENABLE) {
  1197. pr_debug("PCI: Switching off ROM of %s\n",
  1198. pci_name(dev));
  1199. r->flags &= ~IORESOURCE_ROM_ENABLE;
  1200. pci_write_config_dword(dev, dev->rom_base_reg,
  1201. reg & ~PCI_ROM_ADDRESS_ENABLE);
  1202. }
  1203. }
  1204. }
  1205. }
  1206. static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
  1207. {
  1208. struct pci_controller *hose = pci_bus_to_host(bus);
  1209. resource_size_t offset;
  1210. struct resource *res, *pres;
  1211. int i;
  1212. pr_debug("Reserving legacy ranges for domain %04x\n",
  1213. pci_domain_nr(bus));
  1214. /* Check for IO */
  1215. if (!(hose->io_resource.flags & IORESOURCE_IO))
  1216. goto no_io;
  1217. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  1218. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1219. BUG_ON(res == NULL);
  1220. res->name = "Legacy IO";
  1221. res->flags = IORESOURCE_IO;
  1222. res->start = offset;
  1223. res->end = (offset + 0xfff) & 0xfffffffful;
  1224. pr_debug("Candidate legacy IO: %pR\n", res);
  1225. if (request_resource(&hose->io_resource, res)) {
  1226. printk(KERN_DEBUG
  1227. "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
  1228. pci_domain_nr(bus), bus->number, res);
  1229. kfree(res);
  1230. }
  1231. no_io:
  1232. /* Check for memory */
  1233. offset = hose->pci_mem_offset;
  1234. pr_debug("hose mem offset: %016llx\n", (unsigned long long)offset);
  1235. for (i = 0; i < 3; i++) {
  1236. pres = &hose->mem_resources[i];
  1237. if (!(pres->flags & IORESOURCE_MEM))
  1238. continue;
  1239. pr_debug("hose mem res: %pR\n", pres);
  1240. if ((pres->start - offset) <= 0xa0000 &&
  1241. (pres->end - offset) >= 0xbffff)
  1242. break;
  1243. }
  1244. if (i >= 3)
  1245. return;
  1246. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1247. BUG_ON(res == NULL);
  1248. res->name = "Legacy VGA memory";
  1249. res->flags = IORESOURCE_MEM;
  1250. res->start = 0xa0000 + offset;
  1251. res->end = 0xbffff + offset;
  1252. pr_debug("Candidate VGA memory: %pR\n", res);
  1253. if (request_resource(pres, res)) {
  1254. printk(KERN_DEBUG
  1255. "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
  1256. pci_domain_nr(bus), bus->number, res);
  1257. kfree(res);
  1258. }
  1259. }
  1260. void __init pcibios_resource_survey(void)
  1261. {
  1262. struct pci_bus *b;
  1263. /* Allocate and assign resources. If we re-assign everything, then
  1264. * we skip the allocate phase
  1265. */
  1266. list_for_each_entry(b, &pci_root_buses, node)
  1267. pcibios_allocate_bus_resources(b);
  1268. if (!(pci_flags & PCI_REASSIGN_ALL_RSRC)) {
  1269. pcibios_allocate_resources(0);
  1270. pcibios_allocate_resources(1);
  1271. }
  1272. /* Before we start assigning unassigned resource, we try to reserve
  1273. * the low IO area and the VGA memory area if they intersect the
  1274. * bus available resources to avoid allocating things on top of them
  1275. */
  1276. if (!(pci_flags & PCI_PROBE_ONLY)) {
  1277. list_for_each_entry(b, &pci_root_buses, node)
  1278. pcibios_reserve_legacy_regions(b);
  1279. }
  1280. /* Now, if the platform didn't decide to blindly trust the firmware,
  1281. * we proceed to assigning things that were left unassigned
  1282. */
  1283. if (!(pci_flags & PCI_PROBE_ONLY)) {
  1284. pr_debug("PCI: Assigning unassigned resources...\n");
  1285. pci_assign_unassigned_resources();
  1286. }
  1287. }
  1288. #ifdef CONFIG_HOTPLUG
  1289. /* This is used by the PCI hotplug driver to allocate resource
  1290. * of newly plugged busses. We can try to consolidate with the
  1291. * rest of the code later, for now, keep it as-is as our main
  1292. * resource allocation function doesn't deal with sub-trees yet.
  1293. */
  1294. void __devinit pcibios_claim_one_bus(struct pci_bus *bus)
  1295. {
  1296. struct pci_dev *dev;
  1297. struct pci_bus *child_bus;
  1298. list_for_each_entry(dev, &bus->devices, bus_list) {
  1299. int i;
  1300. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  1301. struct resource *r = &dev->resource[i];
  1302. if (r->parent || !r->start || !r->flags)
  1303. continue;
  1304. pr_debug("PCI: Claiming %s: "
  1305. "Resource %d: %016llx..%016llx [%x]\n",
  1306. pci_name(dev), i,
  1307. (unsigned long long)r->start,
  1308. (unsigned long long)r->end,
  1309. (unsigned int)r->flags);
  1310. pci_claim_resource(dev, i);
  1311. }
  1312. }
  1313. list_for_each_entry(child_bus, &bus->children, node)
  1314. pcibios_claim_one_bus(child_bus);
  1315. }
  1316. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  1317. /* pcibios_finish_adding_to_bus
  1318. *
  1319. * This is to be called by the hotplug code after devices have been
  1320. * added to a bus, this include calling it for a PHB that is just
  1321. * being added
  1322. */
  1323. void pcibios_finish_adding_to_bus(struct pci_bus *bus)
  1324. {
  1325. pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
  1326. pci_domain_nr(bus), bus->number);
  1327. /* Allocate bus and devices resources */
  1328. pcibios_allocate_bus_resources(bus);
  1329. pcibios_claim_one_bus(bus);
  1330. /* Add new devices to global lists. Register in proc, sysfs. */
  1331. pci_bus_add_devices(bus);
  1332. /* Fixup EEH */
  1333. eeh_add_device_tree_late(bus);
  1334. }
  1335. EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
  1336. #endif /* CONFIG_HOTPLUG */
  1337. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1338. {
  1339. return pci_enable_resources(dev, mask);
  1340. }
  1341. void __devinit pcibios_setup_phb_resources(struct pci_controller *hose)
  1342. {
  1343. struct pci_bus *bus = hose->bus;
  1344. struct resource *res;
  1345. int i;
  1346. /* Hookup PHB IO resource */
  1347. bus->resource[0] = res = &hose->io_resource;
  1348. if (!res->flags) {
  1349. printk(KERN_WARNING "PCI: I/O resource not set for host"
  1350. " bridge %s (domain %d)\n",
  1351. hose->dn->full_name, hose->global_number);
  1352. /* Workaround for lack of IO resource only on 32-bit */
  1353. res->start = (unsigned long)hose->io_base_virt - isa_io_base;
  1354. res->end = res->start + IO_SPACE_LIMIT;
  1355. res->flags = IORESOURCE_IO;
  1356. }
  1357. pr_debug("PCI: PHB IO resource = %016llx-%016llx [%lx]\n",
  1358. (unsigned long long)res->start,
  1359. (unsigned long long)res->end,
  1360. (unsigned long)res->flags);
  1361. /* Hookup PHB Memory resources */
  1362. for (i = 0; i < 3; ++i) {
  1363. res = &hose->mem_resources[i];
  1364. if (!res->flags) {
  1365. if (i > 0)
  1366. continue;
  1367. printk(KERN_ERR "PCI: Memory resource 0 not set for "
  1368. "host bridge %s (domain %d)\n",
  1369. hose->dn->full_name, hose->global_number);
  1370. /* Workaround for lack of MEM resource only on 32-bit */
  1371. res->start = hose->pci_mem_offset;
  1372. res->end = (resource_size_t)-1LL;
  1373. res->flags = IORESOURCE_MEM;
  1374. }
  1375. bus->resource[i+1] = res;
  1376. pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n",
  1377. i, (unsigned long long)res->start,
  1378. (unsigned long long)res->end,
  1379. (unsigned long)res->flags);
  1380. }
  1381. pr_debug("PCI: PHB MEM offset = %016llx\n",
  1382. (unsigned long long)hose->pci_mem_offset);
  1383. pr_debug("PCI: PHB IO offset = %08lx\n",
  1384. (unsigned long)hose->io_base_virt - _IO_BASE);
  1385. }
  1386. /*
  1387. * Null PCI config access functions, for the case when we can't
  1388. * find a hose.
  1389. */
  1390. #define NULL_PCI_OP(rw, size, type) \
  1391. static int \
  1392. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1393. { \
  1394. return PCIBIOS_DEVICE_NOT_FOUND; \
  1395. }
  1396. static int
  1397. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1398. int len, u32 *val)
  1399. {
  1400. return PCIBIOS_DEVICE_NOT_FOUND;
  1401. }
  1402. static int
  1403. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1404. int len, u32 val)
  1405. {
  1406. return PCIBIOS_DEVICE_NOT_FOUND;
  1407. }
  1408. static struct pci_ops null_pci_ops = {
  1409. .read = null_read_config,
  1410. .write = null_write_config,
  1411. };
  1412. /*
  1413. * These functions are used early on before PCI scanning is done
  1414. * and all of the pci_dev and pci_bus structures have been created.
  1415. */
  1416. static struct pci_bus *
  1417. fake_pci_bus(struct pci_controller *hose, int busnr)
  1418. {
  1419. static struct pci_bus bus;
  1420. if (!hose)
  1421. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1422. bus.number = busnr;
  1423. bus.sysdata = hose;
  1424. bus.ops = hose ? hose->ops : &null_pci_ops;
  1425. return &bus;
  1426. }
  1427. #define EARLY_PCI_OP(rw, size, type) \
  1428. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1429. int devfn, int offset, type value) \
  1430. { \
  1431. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1432. devfn, offset, value); \
  1433. }
  1434. EARLY_PCI_OP(read, byte, u8 *)
  1435. EARLY_PCI_OP(read, word, u16 *)
  1436. EARLY_PCI_OP(read, dword, u32 *)
  1437. EARLY_PCI_OP(write, byte, u8)
  1438. EARLY_PCI_OP(write, word, u16)
  1439. EARLY_PCI_OP(write, dword, u32)
  1440. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  1441. int cap)
  1442. {
  1443. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  1444. }