pci-common.c 46 KB

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