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 <linux/slab.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. static DEFINE_SPINLOCK(hose_spinlock);
  35. LIST_HEAD(hose_list);
  36. /* XXX kill that some day ... */
  37. static int global_phb_number; /* Global phb counter */
  38. /* ISA Memory physical address */
  39. resource_size_t isa_mem_base;
  40. /* Default PCI flags is 0 on ppc32, modified at boot on ppc64 */
  41. unsigned int pci_flags;
  42. static struct dma_map_ops *pci_dma_ops = &dma_direct_ops;
  43. void set_pci_dma_ops(struct dma_map_ops *dma_ops)
  44. {
  45. pci_dma_ops = dma_ops;
  46. }
  47. struct dma_map_ops *get_pci_dma_ops(void)
  48. {
  49. return pci_dma_ops;
  50. }
  51. EXPORT_SYMBOL(get_pci_dma_ops);
  52. int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  53. {
  54. return dma_set_mask(&dev->dev, mask);
  55. }
  56. int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  57. {
  58. int rc;
  59. rc = dma_set_mask(&dev->dev, mask);
  60. dev->dev.coherent_dma_mask = dev->dma_mask;
  61. return rc;
  62. }
  63. struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
  64. {
  65. struct pci_controller *phb;
  66. phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
  67. if (!phb)
  68. return NULL;
  69. spin_lock(&hose_spinlock);
  70. phb->global_number = global_phb_number++;
  71. list_add_tail(&phb->list_node, &hose_list);
  72. spin_unlock(&hose_spinlock);
  73. phb->dn = dev;
  74. phb->is_dynamic = mem_init_done;
  75. return phb;
  76. }
  77. void pcibios_free_controller(struct pci_controller *phb)
  78. {
  79. spin_lock(&hose_spinlock);
  80. list_del(&phb->list_node);
  81. spin_unlock(&hose_spinlock);
  82. if (phb->is_dynamic)
  83. kfree(phb);
  84. }
  85. static resource_size_t pcibios_io_size(const struct pci_controller *hose)
  86. {
  87. return hose->io_resource.end - hose->io_resource.start + 1;
  88. }
  89. int pcibios_vaddr_is_ioport(void __iomem *address)
  90. {
  91. int ret = 0;
  92. struct pci_controller *hose;
  93. resource_size_t size;
  94. spin_lock(&hose_spinlock);
  95. list_for_each_entry(hose, &hose_list, list_node) {
  96. size = pcibios_io_size(hose);
  97. if (address >= hose->io_base_virt &&
  98. address < (hose->io_base_virt + size)) {
  99. ret = 1;
  100. break;
  101. }
  102. }
  103. spin_unlock(&hose_spinlock);
  104. return ret;
  105. }
  106. unsigned long pci_address_to_pio(phys_addr_t address)
  107. {
  108. struct pci_controller *hose;
  109. resource_size_t size;
  110. unsigned long ret = ~0;
  111. spin_lock(&hose_spinlock);
  112. list_for_each_entry(hose, &hose_list, list_node) {
  113. size = pcibios_io_size(hose);
  114. if (address >= hose->io_base_phys &&
  115. address < (hose->io_base_phys + size)) {
  116. unsigned long base =
  117. (unsigned long)hose->io_base_virt - _IO_BASE;
  118. ret = base + (address - hose->io_base_phys);
  119. break;
  120. }
  121. }
  122. spin_unlock(&hose_spinlock);
  123. return ret;
  124. }
  125. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  126. /*
  127. * Return the domain number for this bus.
  128. */
  129. int pci_domain_nr(struct pci_bus *bus)
  130. {
  131. struct pci_controller *hose = pci_bus_to_host(bus);
  132. return hose->global_number;
  133. }
  134. EXPORT_SYMBOL(pci_domain_nr);
  135. /* This routine is meant to be used early during boot, when the
  136. * PCI bus numbers have not yet been assigned, and you need to
  137. * issue PCI config cycles to an OF device.
  138. * It could also be used to "fix" RTAS config cycles if you want
  139. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  140. * config cycles.
  141. */
  142. struct pci_controller *pci_find_hose_for_OF_device(struct device_node *node)
  143. {
  144. while (node) {
  145. struct pci_controller *hose, *tmp;
  146. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  147. if (hose->dn == node)
  148. return hose;
  149. node = node->parent;
  150. }
  151. return NULL;
  152. }
  153. static ssize_t pci_show_devspec(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct pci_dev *pdev;
  157. struct device_node *np;
  158. pdev = to_pci_dev(dev);
  159. np = pci_device_to_OF_node(pdev);
  160. if (np == NULL || np->full_name == NULL)
  161. return 0;
  162. return sprintf(buf, "%s", np->full_name);
  163. }
  164. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  165. /* Add sysfs properties */
  166. int pcibios_add_platform_entries(struct pci_dev *pdev)
  167. {
  168. return device_create_file(&pdev->dev, &dev_attr_devspec);
  169. }
  170. char __devinit *pcibios_setup(char *str)
  171. {
  172. return str;
  173. }
  174. /*
  175. * Reads the interrupt pin to determine if interrupt is use by card.
  176. * If the interrupt is used, then gets the interrupt line from the
  177. * openfirmware and sets it in the pci_dev and pci_config line.
  178. */
  179. int pci_read_irq_line(struct pci_dev *pci_dev)
  180. {
  181. struct of_irq oirq;
  182. unsigned int virq;
  183. /* The current device-tree that iSeries generates from the HV
  184. * PCI informations doesn't contain proper interrupt routing,
  185. * and all the fallback would do is print out crap, so we
  186. * don't attempt to resolve the interrupts here at all, some
  187. * iSeries specific fixup does it.
  188. *
  189. * In the long run, we will hopefully fix the generated device-tree
  190. * instead.
  191. */
  192. pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
  193. #ifdef DEBUG
  194. memset(&oirq, 0xff, sizeof(oirq));
  195. #endif
  196. /* Try to get a mapping from the device-tree */
  197. if (of_irq_map_pci(pci_dev, &oirq)) {
  198. u8 line, pin;
  199. /* If that fails, lets fallback to what is in the config
  200. * space and map that through the default controller. We
  201. * also set the type to level low since that's what PCI
  202. * interrupts are. If your platform does differently, then
  203. * either provide a proper interrupt tree or don't use this
  204. * function.
  205. */
  206. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  207. return -1;
  208. if (pin == 0)
  209. return -1;
  210. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  211. line == 0xff || line == 0) {
  212. return -1;
  213. }
  214. pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
  215. line, pin);
  216. virq = irq_create_mapping(NULL, line);
  217. if (virq != NO_IRQ)
  218. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  219. } else {
  220. pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  221. oirq.size, oirq.specifier[0], oirq.specifier[1],
  222. oirq.controller ? oirq.controller->full_name :
  223. "<default>");
  224. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  225. oirq.size);
  226. }
  227. if (virq == NO_IRQ) {
  228. pr_debug(" Failed to map !\n");
  229. return -1;
  230. }
  231. pr_debug(" Mapped to linux irq %d\n", virq);
  232. pci_dev->irq = virq;
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(pci_read_irq_line);
  236. /*
  237. * Platform support for /proc/bus/pci/X/Y mmap()s,
  238. * modelled on the sparc64 implementation by Dave Miller.
  239. * -- paulus.
  240. */
  241. /*
  242. * Adjust vm_pgoff of VMA such that it is the physical page offset
  243. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  244. *
  245. * Basically, the user finds the base address for his device which he wishes
  246. * to mmap. They read the 32-bit value from the config space base register,
  247. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  248. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  249. *
  250. * Returns negative error code on failure, zero on success.
  251. */
  252. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  253. resource_size_t *offset,
  254. enum pci_mmap_state mmap_state)
  255. {
  256. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  257. unsigned long io_offset = 0;
  258. int i, res_bit;
  259. if (hose == 0)
  260. return NULL; /* should never happen */
  261. /* If memory, add on the PCI bridge address offset */
  262. if (mmap_state == pci_mmap_mem) {
  263. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  264. *offset += hose->pci_mem_offset;
  265. #endif
  266. res_bit = IORESOURCE_MEM;
  267. } else {
  268. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  269. *offset += io_offset;
  270. res_bit = IORESOURCE_IO;
  271. }
  272. /*
  273. * Check that the offset requested corresponds to one of the
  274. * resources of the device.
  275. */
  276. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  277. struct resource *rp = &dev->resource[i];
  278. int flags = rp->flags;
  279. /* treat ROM as memory (should be already) */
  280. if (i == PCI_ROM_RESOURCE)
  281. flags |= IORESOURCE_MEM;
  282. /* Active and same type? */
  283. if ((flags & res_bit) == 0)
  284. continue;
  285. /* In the range of this resource? */
  286. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  287. continue;
  288. /* found it! construct the final physical address */
  289. if (mmap_state == pci_mmap_io)
  290. *offset += hose->io_base_phys - io_offset;
  291. return rp;
  292. }
  293. return NULL;
  294. }
  295. /*
  296. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  297. * device mapping.
  298. */
  299. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  300. pgprot_t protection,
  301. enum pci_mmap_state mmap_state,
  302. int write_combine)
  303. {
  304. pgprot_t prot = protection;
  305. /* Write combine is always 0 on non-memory space mappings. On
  306. * memory space, if the user didn't pass 1, we check for a
  307. * "prefetchable" resource. This is a bit hackish, but we use
  308. * this to workaround the inability of /sysfs to provide a write
  309. * combine bit
  310. */
  311. if (mmap_state != pci_mmap_mem)
  312. write_combine = 0;
  313. else if (write_combine == 0) {
  314. if (rp->flags & IORESOURCE_PREFETCH)
  315. write_combine = 1;
  316. }
  317. return pgprot_noncached(prot);
  318. }
  319. /*
  320. * This one is used by /dev/mem and fbdev who have no clue about the
  321. * PCI device, it tries to find the PCI device first and calls the
  322. * above routine
  323. */
  324. pgprot_t pci_phys_mem_access_prot(struct file *file,
  325. unsigned long pfn,
  326. unsigned long size,
  327. pgprot_t prot)
  328. {
  329. struct pci_dev *pdev = NULL;
  330. struct resource *found = NULL;
  331. resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
  332. int i;
  333. if (page_is_ram(pfn))
  334. return prot;
  335. prot = pgprot_noncached(prot);
  336. for_each_pci_dev(pdev) {
  337. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  338. struct resource *rp = &pdev->resource[i];
  339. int flags = rp->flags;
  340. /* Active and same type? */
  341. if ((flags & IORESOURCE_MEM) == 0)
  342. continue;
  343. /* In the range of this resource? */
  344. if (offset < (rp->start & PAGE_MASK) ||
  345. offset > rp->end)
  346. continue;
  347. found = rp;
  348. break;
  349. }
  350. if (found)
  351. break;
  352. }
  353. if (found) {
  354. if (found->flags & IORESOURCE_PREFETCH)
  355. prot = pgprot_noncached_wc(prot);
  356. pci_dev_put(pdev);
  357. }
  358. pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
  359. (unsigned long long)offset, pgprot_val(prot));
  360. return prot;
  361. }
  362. /*
  363. * Perform the actual remap of the pages for a PCI device mapping, as
  364. * appropriate for this architecture. The region in the process to map
  365. * is described by vm_start and vm_end members of VMA, the base physical
  366. * address is found in vm_pgoff.
  367. * The pci device structure is provided so that architectures may make mapping
  368. * decisions on a per-device or per-bus basis.
  369. *
  370. * Returns a negative error code on failure, zero on success.
  371. */
  372. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  373. enum pci_mmap_state mmap_state, int write_combine)
  374. {
  375. resource_size_t offset =
  376. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  377. struct resource *rp;
  378. int ret;
  379. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  380. if (rp == NULL)
  381. return -EINVAL;
  382. vma->vm_pgoff = offset >> PAGE_SHIFT;
  383. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  384. vma->vm_page_prot,
  385. mmap_state, write_combine);
  386. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  387. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  388. return ret;
  389. }
  390. /* This provides legacy IO read access on a bus */
  391. int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
  392. {
  393. unsigned long offset;
  394. struct pci_controller *hose = pci_bus_to_host(bus);
  395. struct resource *rp = &hose->io_resource;
  396. void __iomem *addr;
  397. /* Check if port can be supported by that bus. We only check
  398. * the ranges of the PHB though, not the bus itself as the rules
  399. * for forwarding legacy cycles down bridges are not our problem
  400. * here. So if the host bridge supports it, we do it.
  401. */
  402. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  403. offset += port;
  404. if (!(rp->flags & IORESOURCE_IO))
  405. return -ENXIO;
  406. if (offset < rp->start || (offset + size) > rp->end)
  407. return -ENXIO;
  408. addr = hose->io_base_virt + port;
  409. switch (size) {
  410. case 1:
  411. *((u8 *)val) = in_8(addr);
  412. return 1;
  413. case 2:
  414. if (port & 1)
  415. return -EINVAL;
  416. *((u16 *)val) = in_le16(addr);
  417. return 2;
  418. case 4:
  419. if (port & 3)
  420. return -EINVAL;
  421. *((u32 *)val) = in_le32(addr);
  422. return 4;
  423. }
  424. return -EINVAL;
  425. }
  426. /* This provides legacy IO write access on a bus */
  427. int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
  428. {
  429. unsigned long offset;
  430. struct pci_controller *hose = pci_bus_to_host(bus);
  431. struct resource *rp = &hose->io_resource;
  432. void __iomem *addr;
  433. /* Check if port can be supported by that bus. We only check
  434. * the ranges of the PHB though, not the bus itself as the rules
  435. * for forwarding legacy cycles down bridges are not our problem
  436. * here. So if the host bridge supports it, we do it.
  437. */
  438. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  439. offset += port;
  440. if (!(rp->flags & IORESOURCE_IO))
  441. return -ENXIO;
  442. if (offset < rp->start || (offset + size) > rp->end)
  443. return -ENXIO;
  444. addr = hose->io_base_virt + port;
  445. /* WARNING: The generic code is idiotic. It gets passed a pointer
  446. * to what can be a 1, 2 or 4 byte quantity and always reads that
  447. * as a u32, which means that we have to correct the location of
  448. * the data read within those 32 bits for size 1 and 2
  449. */
  450. switch (size) {
  451. case 1:
  452. out_8(addr, val >> 24);
  453. return 1;
  454. case 2:
  455. if (port & 1)
  456. return -EINVAL;
  457. out_le16(addr, val >> 16);
  458. return 2;
  459. case 4:
  460. if (port & 3)
  461. return -EINVAL;
  462. out_le32(addr, val);
  463. return 4;
  464. }
  465. return -EINVAL;
  466. }
  467. /* This provides legacy IO or memory mmap access on a bus */
  468. int pci_mmap_legacy_page_range(struct pci_bus *bus,
  469. struct vm_area_struct *vma,
  470. enum pci_mmap_state mmap_state)
  471. {
  472. struct pci_controller *hose = pci_bus_to_host(bus);
  473. resource_size_t offset =
  474. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  475. resource_size_t size = vma->vm_end - vma->vm_start;
  476. struct resource *rp;
  477. pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
  478. pci_domain_nr(bus), bus->number,
  479. mmap_state == pci_mmap_mem ? "MEM" : "IO",
  480. (unsigned long long)offset,
  481. (unsigned long long)(offset + size - 1));
  482. if (mmap_state == pci_mmap_mem) {
  483. /* Hack alert !
  484. *
  485. * Because X is lame and can fail starting if it gets an error
  486. * trying to mmap legacy_mem (instead of just moving on without
  487. * legacy memory access) we fake it here by giving it anonymous
  488. * memory, effectively behaving just like /dev/zero
  489. */
  490. if ((offset + size) > hose->isa_mem_size) {
  491. #ifdef CONFIG_MMU
  492. printk(KERN_DEBUG
  493. "Process %s (pid:%d) mapped non-existing PCI"
  494. "legacy memory for 0%04x:%02x\n",
  495. current->comm, current->pid, pci_domain_nr(bus),
  496. bus->number);
  497. #endif
  498. if (vma->vm_flags & VM_SHARED)
  499. return shmem_zero_setup(vma);
  500. return 0;
  501. }
  502. offset += hose->isa_mem_phys;
  503. } else {
  504. unsigned long io_offset = (unsigned long)hose->io_base_virt - \
  505. _IO_BASE;
  506. unsigned long roffset = offset + io_offset;
  507. rp = &hose->io_resource;
  508. if (!(rp->flags & IORESOURCE_IO))
  509. return -ENXIO;
  510. if (roffset < rp->start || (roffset + size) > rp->end)
  511. return -ENXIO;
  512. offset += hose->io_base_phys;
  513. }
  514. pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
  515. vma->vm_pgoff = offset >> PAGE_SHIFT;
  516. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  517. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  518. vma->vm_end - vma->vm_start,
  519. vma->vm_page_prot);
  520. }
  521. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  522. const struct resource *rsrc,
  523. resource_size_t *start, resource_size_t *end)
  524. {
  525. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  526. resource_size_t offset = 0;
  527. if (hose == NULL)
  528. return;
  529. if (rsrc->flags & IORESOURCE_IO)
  530. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  531. /* We pass a fully fixed up address to userland for MMIO instead of
  532. * a BAR value because X is lame and expects to be able to use that
  533. * to pass to /dev/mem !
  534. *
  535. * That means that we'll have potentially 64 bits values where some
  536. * userland apps only expect 32 (like X itself since it thinks only
  537. * Sparc has 64 bits MMIO) but if we don't do that, we break it on
  538. * 32 bits CHRPs :-(
  539. *
  540. * Hopefully, the sysfs insterface is immune to that gunk. Once X
  541. * has been fixed (and the fix spread enough), we can re-enable the
  542. * 2 lines below and pass down a BAR value to userland. In that case
  543. * we'll also have to re-enable the matching code in
  544. * __pci_mmap_make_offset().
  545. *
  546. * BenH.
  547. */
  548. #if 0
  549. else if (rsrc->flags & IORESOURCE_MEM)
  550. offset = hose->pci_mem_offset;
  551. #endif
  552. *start = rsrc->start - offset;
  553. *end = rsrc->end - offset;
  554. }
  555. /**
  556. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  557. * @hose: newly allocated pci_controller to be setup
  558. * @dev: device node of the host bridge
  559. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  560. *
  561. * This function will parse the "ranges" property of a PCI host bridge device
  562. * node and setup the resource mapping of a pci controller based on its
  563. * content.
  564. *
  565. * Life would be boring if it wasn't for a few issues that we have to deal
  566. * with here:
  567. *
  568. * - We can only cope with one IO space range and up to 3 Memory space
  569. * ranges. However, some machines (thanks Apple !) tend to split their
  570. * space into lots of small contiguous ranges. So we have to coalesce.
  571. *
  572. * - We can only cope with all memory ranges having the same offset
  573. * between CPU addresses and PCI addresses. Unfortunately, some bridges
  574. * are setup for a large 1:1 mapping along with a small "window" which
  575. * maps PCI address 0 to some arbitrary high address of the CPU space in
  576. * order to give access to the ISA memory hole.
  577. * The way out of here that I've chosen for now is to always set the
  578. * offset based on the first resource found, then override it if we
  579. * have a different offset and the previous was set by an ISA hole.
  580. *
  581. * - Some busses have IO space not starting at 0, which causes trouble with
  582. * the way we do our IO resource renumbering. The code somewhat deals with
  583. * it for 64 bits but I would expect problems on 32 bits.
  584. *
  585. * - Some 32 bits platforms such as 4xx can have physical space larger than
  586. * 32 bits so we need to use 64 bits values for the parsing
  587. */
  588. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  589. struct device_node *dev,
  590. int primary)
  591. {
  592. const u32 *ranges;
  593. int rlen;
  594. int pna = of_n_addr_cells(dev);
  595. int np = pna + 5;
  596. int memno = 0, isa_hole = -1;
  597. u32 pci_space;
  598. unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
  599. unsigned long long isa_mb = 0;
  600. struct resource *res;
  601. printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
  602. dev->full_name, primary ? "(primary)" : "");
  603. /* Get ranges property */
  604. ranges = of_get_property(dev, "ranges", &rlen);
  605. if (ranges == NULL)
  606. return;
  607. /* Parse it */
  608. pr_debug("Parsing ranges property...\n");
  609. while ((rlen -= np * 4) >= 0) {
  610. /* Read next ranges element */
  611. pci_space = ranges[0];
  612. pci_addr = of_read_number(ranges + 1, 2);
  613. cpu_addr = of_translate_address(dev, ranges + 3);
  614. size = of_read_number(ranges + pna + 3, 2);
  615. pr_debug("pci_space: 0x%08x pci_addr:0x%016llx "
  616. "cpu_addr:0x%016llx size:0x%016llx\n",
  617. pci_space, pci_addr, cpu_addr, size);
  618. ranges += np;
  619. /* If we failed translation or got a zero-sized region
  620. * (some FW try to feed us with non sensical zero sized regions
  621. * such as power3 which look like some kind of attempt
  622. * at exposing the VGA memory hole)
  623. */
  624. if (cpu_addr == OF_BAD_ADDR || size == 0)
  625. continue;
  626. /* Now consume following elements while they are contiguous */
  627. for (; rlen >= np * sizeof(u32);
  628. ranges += np, rlen -= np * 4) {
  629. if (ranges[0] != pci_space)
  630. break;
  631. pci_next = of_read_number(ranges + 1, 2);
  632. cpu_next = of_translate_address(dev, ranges + 3);
  633. if (pci_next != pci_addr + size ||
  634. cpu_next != cpu_addr + size)
  635. break;
  636. size += of_read_number(ranges + pna + 3, 2);
  637. }
  638. /* Act based on address space type */
  639. res = NULL;
  640. switch ((pci_space >> 24) & 0x3) {
  641. case 1: /* PCI IO space */
  642. printk(KERN_INFO
  643. " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  644. cpu_addr, cpu_addr + size - 1, pci_addr);
  645. /* We support only one IO range */
  646. if (hose->pci_io_size) {
  647. printk(KERN_INFO
  648. " \\--> Skipped (too many) !\n");
  649. continue;
  650. }
  651. /* On 32 bits, limit I/O space to 16MB */
  652. if (size > 0x01000000)
  653. size = 0x01000000;
  654. /* 32 bits needs to map IOs here */
  655. hose->io_base_virt = ioremap(cpu_addr, size);
  656. /* Expect trouble if pci_addr is not 0 */
  657. if (primary)
  658. isa_io_base =
  659. (unsigned long)hose->io_base_virt;
  660. /* pci_io_size and io_base_phys always represent IO
  661. * space starting at 0 so we factor in pci_addr
  662. */
  663. hose->pci_io_size = pci_addr + size;
  664. hose->io_base_phys = cpu_addr - pci_addr;
  665. /* Build resource */
  666. res = &hose->io_resource;
  667. res->flags = IORESOURCE_IO;
  668. res->start = pci_addr;
  669. break;
  670. case 2: /* PCI Memory space */
  671. case 3: /* PCI 64 bits Memory space */
  672. printk(KERN_INFO
  673. " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  674. cpu_addr, cpu_addr + size - 1, pci_addr,
  675. (pci_space & 0x40000000) ? "Prefetch" : "");
  676. /* We support only 3 memory ranges */
  677. if (memno >= 3) {
  678. printk(KERN_INFO
  679. " \\--> Skipped (too many) !\n");
  680. continue;
  681. }
  682. /* Handles ISA memory hole space here */
  683. if (pci_addr == 0) {
  684. isa_mb = cpu_addr;
  685. isa_hole = memno;
  686. if (primary || isa_mem_base == 0)
  687. isa_mem_base = cpu_addr;
  688. hose->isa_mem_phys = cpu_addr;
  689. hose->isa_mem_size = size;
  690. }
  691. /* We get the PCI/Mem offset from the first range or
  692. * the, current one if the offset came from an ISA
  693. * hole. If they don't match, bugger.
  694. */
  695. if (memno == 0 ||
  696. (isa_hole >= 0 && pci_addr != 0 &&
  697. hose->pci_mem_offset == isa_mb))
  698. hose->pci_mem_offset = cpu_addr - pci_addr;
  699. else if (pci_addr != 0 &&
  700. hose->pci_mem_offset != cpu_addr - pci_addr) {
  701. printk(KERN_INFO
  702. " \\--> Skipped (offset mismatch) !\n");
  703. continue;
  704. }
  705. /* Build resource */
  706. res = &hose->mem_resources[memno++];
  707. res->flags = IORESOURCE_MEM;
  708. if (pci_space & 0x40000000)
  709. res->flags |= IORESOURCE_PREFETCH;
  710. res->start = cpu_addr;
  711. break;
  712. }
  713. if (res != NULL) {
  714. res->name = dev->full_name;
  715. res->end = res->start + size - 1;
  716. res->parent = NULL;
  717. res->sibling = NULL;
  718. res->child = NULL;
  719. }
  720. }
  721. /* If there's an ISA hole and the pci_mem_offset is -not- matching
  722. * the ISA hole offset, then we need to remove the ISA hole from
  723. * the resource list for that brige
  724. */
  725. if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
  726. unsigned int next = isa_hole + 1;
  727. printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb);
  728. if (next < memno)
  729. memmove(&hose->mem_resources[isa_hole],
  730. &hose->mem_resources[next],
  731. sizeof(struct resource) * (memno - next));
  732. hose->mem_resources[--memno].flags = 0;
  733. }
  734. }
  735. /* Decide whether to display the domain number in /proc */
  736. int pci_proc_domain(struct pci_bus *bus)
  737. {
  738. struct pci_controller *hose = pci_bus_to_host(bus);
  739. if (!(pci_flags & PCI_ENABLE_PROC_DOMAINS))
  740. return 0;
  741. if (pci_flags & PCI_COMPAT_DOMAIN_0)
  742. return hose->global_number != 0;
  743. return 1;
  744. }
  745. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  746. struct resource *res)
  747. {
  748. resource_size_t offset = 0, mask = (resource_size_t)-1;
  749. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  750. if (!hose)
  751. return;
  752. if (res->flags & IORESOURCE_IO) {
  753. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  754. mask = 0xffffffffu;
  755. } else if (res->flags & IORESOURCE_MEM)
  756. offset = hose->pci_mem_offset;
  757. region->start = (res->start - offset) & mask;
  758. region->end = (res->end - offset) & mask;
  759. }
  760. EXPORT_SYMBOL(pcibios_resource_to_bus);
  761. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  762. struct pci_bus_region *region)
  763. {
  764. resource_size_t offset = 0, mask = (resource_size_t)-1;
  765. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  766. if (!hose)
  767. return;
  768. if (res->flags & IORESOURCE_IO) {
  769. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  770. mask = 0xffffffffu;
  771. } else if (res->flags & IORESOURCE_MEM)
  772. offset = hose->pci_mem_offset;
  773. res->start = (region->start + offset) & mask;
  774. res->end = (region->end + offset) & mask;
  775. }
  776. EXPORT_SYMBOL(pcibios_bus_to_resource);
  777. /* Fixup a bus resource into a linux resource */
  778. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  779. {
  780. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  781. resource_size_t offset = 0, mask = (resource_size_t)-1;
  782. if (res->flags & IORESOURCE_IO) {
  783. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  784. mask = 0xffffffffu;
  785. } else if (res->flags & IORESOURCE_MEM)
  786. offset = hose->pci_mem_offset;
  787. res->start = (res->start + offset) & mask;
  788. res->end = (res->end + offset) & mask;
  789. }
  790. /* This header fixup will do the resource fixup for all devices as they are
  791. * probed, but not for bridge ranges
  792. */
  793. static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
  794. {
  795. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  796. int i;
  797. if (!hose) {
  798. printk(KERN_ERR "No host bridge for PCI dev %s !\n",
  799. pci_name(dev));
  800. return;
  801. }
  802. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  803. struct resource *res = dev->resource + i;
  804. if (!res->flags)
  805. continue;
  806. /* On platforms that have PCI_PROBE_ONLY set, we don't
  807. * consider 0 as an unassigned BAR value. It's technically
  808. * a valid value, but linux doesn't like it... so when we can
  809. * re-assign things, we do so, but if we can't, we keep it
  810. * around and hope for the best...
  811. */
  812. if (res->start == 0 && !(pci_flags & PCI_PROBE_ONLY)) {
  813. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x]" \
  814. "is unassigned\n",
  815. pci_name(dev), i,
  816. (unsigned long long)res->start,
  817. (unsigned long long)res->end,
  818. (unsigned int)res->flags);
  819. res->end -= res->start;
  820. res->start = 0;
  821. res->flags |= IORESOURCE_UNSET;
  822. continue;
  823. }
  824. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
  825. pci_name(dev), i,
  826. (unsigned long long)res->start,\
  827. (unsigned long long)res->end,
  828. (unsigned int)res->flags);
  829. fixup_resource(res, dev);
  830. pr_debug("PCI:%s %016llx-%016llx\n",
  831. pci_name(dev),
  832. (unsigned long long)res->start,
  833. (unsigned long long)res->end);
  834. }
  835. }
  836. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  837. /* This function tries to figure out if a bridge resource has been initialized
  838. * by the firmware or not. It doesn't have to be absolutely bullet proof, but
  839. * things go more smoothly when it gets it right. It should covers cases such
  840. * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
  841. */
  842. static int __devinit pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
  843. struct resource *res)
  844. {
  845. struct pci_controller *hose = pci_bus_to_host(bus);
  846. struct pci_dev *dev = bus->self;
  847. resource_size_t offset;
  848. u16 command;
  849. int i;
  850. /* We don't do anything if PCI_PROBE_ONLY is set */
  851. if (pci_flags & PCI_PROBE_ONLY)
  852. return 0;
  853. /* Job is a bit different between memory and IO */
  854. if (res->flags & IORESOURCE_MEM) {
  855. /* If the BAR is non-0 (res != pci_mem_offset) then it's
  856. * probably been initialized by somebody
  857. */
  858. if (res->start != hose->pci_mem_offset)
  859. return 0;
  860. /* The BAR is 0, let's check if memory decoding is enabled on
  861. * the bridge. If not, we consider it unassigned
  862. */
  863. pci_read_config_word(dev, PCI_COMMAND, &command);
  864. if ((command & PCI_COMMAND_MEMORY) == 0)
  865. return 1;
  866. /* Memory decoding is enabled and the BAR is 0. If any of
  867. * the bridge resources covers that starting address (0 then
  868. * it's good enough for us for memory
  869. */
  870. for (i = 0; i < 3; i++) {
  871. if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
  872. hose->mem_resources[i].start == hose->pci_mem_offset)
  873. return 0;
  874. }
  875. /* Well, it starts at 0 and we know it will collide so we may as
  876. * well consider it as unassigned. That covers the Apple case.
  877. */
  878. return 1;
  879. } else {
  880. /* If the BAR is non-0, then we consider it assigned */
  881. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  882. if (((res->start - offset) & 0xfffffffful) != 0)
  883. return 0;
  884. /* Here, we are a bit different than memory as typically IO
  885. * space starting at low addresses -is- valid. What we do
  886. * instead if that we consider as unassigned anything that
  887. * doesn't have IO enabled in the PCI command register,
  888. * and that's it.
  889. */
  890. pci_read_config_word(dev, PCI_COMMAND, &command);
  891. if (command & PCI_COMMAND_IO)
  892. return 0;
  893. /* It's starting at 0 and IO is disabled in the bridge, consider
  894. * it unassigned
  895. */
  896. return 1;
  897. }
  898. }
  899. /* Fixup resources of a PCI<->PCI bridge */
  900. static void __devinit pcibios_fixup_bridge(struct pci_bus *bus)
  901. {
  902. struct resource *res;
  903. int i;
  904. struct pci_dev *dev = bus->self;
  905. pci_bus_for_each_resource(bus, res, i) {
  906. res = bus->resource[i];
  907. if (!res)
  908. continue;
  909. if (!res->flags)
  910. continue;
  911. if (i >= 3 && bus->self->transparent)
  912. continue;
  913. pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
  914. pci_name(dev), i,
  915. (unsigned long long)res->start,\
  916. (unsigned long long)res->end,
  917. (unsigned int)res->flags);
  918. /* Perform fixup */
  919. fixup_resource(res, dev);
  920. /* Try to detect uninitialized P2P bridge resources,
  921. * and clear them out so they get re-assigned later
  922. */
  923. if (pcibios_uninitialized_bridge_resource(bus, res)) {
  924. res->flags = 0;
  925. pr_debug("PCI:%s (unassigned)\n",
  926. pci_name(dev));
  927. } else {
  928. pr_debug("PCI:%s %016llx-%016llx\n",
  929. pci_name(dev),
  930. (unsigned long long)res->start,
  931. (unsigned long long)res->end);
  932. }
  933. }
  934. }
  935. void __devinit pcibios_setup_bus_self(struct pci_bus *bus)
  936. {
  937. /* Fix up the bus resources for P2P bridges */
  938. if (bus->self != NULL)
  939. pcibios_fixup_bridge(bus);
  940. }
  941. void __devinit pcibios_setup_bus_devices(struct pci_bus *bus)
  942. {
  943. struct pci_dev *dev;
  944. pr_debug("PCI: Fixup bus devices %d (%s)\n",
  945. bus->number, bus->self ? pci_name(bus->self) : "PHB");
  946. list_for_each_entry(dev, &bus->devices, bus_list) {
  947. struct dev_archdata *sd = &dev->dev.archdata;
  948. /* Setup OF node pointer in archdata */
  949. sd->of_node = pci_device_to_OF_node(dev);
  950. /* Fixup NUMA node as it may not be setup yet by the generic
  951. * code and is needed by the DMA init
  952. */
  953. set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
  954. /* Hook up default DMA ops */
  955. sd->dma_ops = pci_dma_ops;
  956. sd->dma_data = (void *)PCI_DRAM_OFFSET;
  957. /* Read default IRQs and fixup if necessary */
  958. pci_read_irq_line(dev);
  959. }
  960. }
  961. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  962. {
  963. /* When called from the generic PCI probe, read PCI<->PCI bridge
  964. * bases. This is -not- called when generating the PCI tree from
  965. * the OF device-tree.
  966. */
  967. if (bus->self != NULL)
  968. pci_read_bridge_bases(bus);
  969. /* Now fixup the bus bus */
  970. pcibios_setup_bus_self(bus);
  971. /* Now fixup devices on that bus */
  972. pcibios_setup_bus_devices(bus);
  973. }
  974. EXPORT_SYMBOL(pcibios_fixup_bus);
  975. static int skip_isa_ioresource_align(struct pci_dev *dev)
  976. {
  977. if ((pci_flags & PCI_CAN_SKIP_ISA_ALIGN) &&
  978. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  979. return 1;
  980. return 0;
  981. }
  982. /*
  983. * We need to avoid collisions with `mirrored' VGA ports
  984. * and other strange ISA hardware, so we always want the
  985. * addresses to be allocated in the 0x000-0x0ff region
  986. * modulo 0x400.
  987. *
  988. * Why? Because some silly external IO cards only decode
  989. * the low 10 bits of the IO address. The 0x00-0xff region
  990. * is reserved for motherboard devices that decode all 16
  991. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  992. * but we want to try to avoid allocating at 0x2900-0x2bff
  993. * which might have be mirrored at 0x0100-0x03ff..
  994. */
  995. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  996. resource_size_t size, resource_size_t align)
  997. {
  998. struct pci_dev *dev = data;
  999. resource_size_t start = res->start;
  1000. if (res->flags & IORESOURCE_IO) {
  1001. if (skip_isa_ioresource_align(dev))
  1002. return start;
  1003. if (start & 0x300)
  1004. start = (start + 0x3ff) & ~0x3ff;
  1005. }
  1006. return start;
  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. pci_bus_for_each_resource(bus, res, 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. }