pci-common.c 45 KB

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