pci-common.c 46 KB

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