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 <anton@au.ibm.com>, 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 = kzalloc(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 video 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(unsigned long pfn,
  438. unsigned long size,
  439. pgprot_t prot)
  440. {
  441. struct pci_dev *pdev = NULL;
  442. struct resource *found = NULL;
  443. resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
  444. int i;
  445. if (page_is_ram(pfn))
  446. return prot;
  447. prot = pgprot_noncached(prot);
  448. for_each_pci_dev(pdev) {
  449. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  450. struct resource *rp = &pdev->resource[i];
  451. int flags = rp->flags;
  452. /* Active and same type? */
  453. if ((flags & IORESOURCE_MEM) == 0)
  454. continue;
  455. /* In the range of this resource? */
  456. if (offset < (rp->start & PAGE_MASK) ||
  457. offset > rp->end)
  458. continue;
  459. found = rp;
  460. break;
  461. }
  462. if (found)
  463. break;
  464. }
  465. if (found) {
  466. if (found->flags & IORESOURCE_PREFETCH)
  467. prot = pgprot_noncached_wc(prot);
  468. pci_dev_put(pdev);
  469. }
  470. pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
  471. (unsigned long long)offset, pgprot_val(prot));
  472. return prot;
  473. }
  474. /* This provides legacy IO read access on a bus */
  475. int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
  476. {
  477. unsigned long offset;
  478. struct pci_controller *hose = pci_bus_to_host(bus);
  479. struct resource *rp = &hose->io_resource;
  480. void __iomem *addr;
  481. /* Check if port can be supported by that bus. We only check
  482. * the ranges of the PHB though, not the bus itself as the rules
  483. * for forwarding legacy cycles down bridges are not our problem
  484. * here. So if the host bridge supports it, we do it.
  485. */
  486. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  487. offset += port;
  488. if (!(rp->flags & IORESOURCE_IO))
  489. return -ENXIO;
  490. if (offset < rp->start || (offset + size) > rp->end)
  491. return -ENXIO;
  492. addr = hose->io_base_virt + port;
  493. switch(size) {
  494. case 1:
  495. *((u8 *)val) = in_8(addr);
  496. return 1;
  497. case 2:
  498. if (port & 1)
  499. return -EINVAL;
  500. *((u16 *)val) = in_le16(addr);
  501. return 2;
  502. case 4:
  503. if (port & 3)
  504. return -EINVAL;
  505. *((u32 *)val) = in_le32(addr);
  506. return 4;
  507. }
  508. return -EINVAL;
  509. }
  510. /* This provides legacy IO write access on a bus */
  511. int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
  512. {
  513. unsigned long offset;
  514. struct pci_controller *hose = pci_bus_to_host(bus);
  515. struct resource *rp = &hose->io_resource;
  516. void __iomem *addr;
  517. /* Check if port can be supported by that bus. We only check
  518. * the ranges of the PHB though, not the bus itself as the rules
  519. * for forwarding legacy cycles down bridges are not our problem
  520. * here. So if the host bridge supports it, we do it.
  521. */
  522. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  523. offset += port;
  524. if (!(rp->flags & IORESOURCE_IO))
  525. return -ENXIO;
  526. if (offset < rp->start || (offset + size) > rp->end)
  527. return -ENXIO;
  528. addr = hose->io_base_virt + port;
  529. /* WARNING: The generic code is idiotic. It gets passed a pointer
  530. * to what can be a 1, 2 or 4 byte quantity and always reads that
  531. * as a u32, which means that we have to correct the location of
  532. * the data read within those 32 bits for size 1 and 2
  533. */
  534. switch(size) {
  535. case 1:
  536. out_8(addr, val >> 24);
  537. return 1;
  538. case 2:
  539. if (port & 1)
  540. return -EINVAL;
  541. out_le16(addr, val >> 16);
  542. return 2;
  543. case 4:
  544. if (port & 3)
  545. return -EINVAL;
  546. out_le32(addr, val);
  547. return 4;
  548. }
  549. return -EINVAL;
  550. }
  551. /* This provides legacy IO or memory mmap access on a bus */
  552. int pci_mmap_legacy_page_range(struct pci_bus *bus,
  553. struct vm_area_struct *vma,
  554. enum pci_mmap_state mmap_state)
  555. {
  556. struct pci_controller *hose = pci_bus_to_host(bus);
  557. resource_size_t offset =
  558. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  559. resource_size_t size = vma->vm_end - vma->vm_start;
  560. struct resource *rp;
  561. pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
  562. pci_domain_nr(bus), bus->number,
  563. mmap_state == pci_mmap_mem ? "MEM" : "IO",
  564. (unsigned long long)offset,
  565. (unsigned long long)(offset + size - 1));
  566. if (mmap_state == pci_mmap_mem) {
  567. /* Hack alert !
  568. *
  569. * Because X is lame and can fail starting if it gets an error trying
  570. * to mmap legacy_mem (instead of just moving on without legacy memory
  571. * access) we fake it here by giving it anonymous memory, effectively
  572. * behaving just like /dev/zero
  573. */
  574. if ((offset + size) > hose->isa_mem_size) {
  575. printk(KERN_DEBUG
  576. "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
  577. current->comm, current->pid, pci_domain_nr(bus), bus->number);
  578. if (vma->vm_flags & VM_SHARED)
  579. return shmem_zero_setup(vma);
  580. return 0;
  581. }
  582. offset += hose->isa_mem_phys;
  583. } else {
  584. unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  585. unsigned long roffset = offset + io_offset;
  586. rp = &hose->io_resource;
  587. if (!(rp->flags & IORESOURCE_IO))
  588. return -ENXIO;
  589. if (roffset < rp->start || (roffset + size) > rp->end)
  590. return -ENXIO;
  591. offset += hose->io_base_phys;
  592. }
  593. pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
  594. vma->vm_pgoff = offset >> PAGE_SHIFT;
  595. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  596. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  597. vma->vm_end - vma->vm_start,
  598. vma->vm_page_prot);
  599. }
  600. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  601. const struct resource *rsrc,
  602. resource_size_t *start, resource_size_t *end)
  603. {
  604. struct pci_bus_region region;
  605. if (rsrc->flags & IORESOURCE_IO) {
  606. pcibios_resource_to_bus(dev->bus, &region,
  607. (struct resource *) rsrc);
  608. *start = region.start;
  609. *end = region.end;
  610. return;
  611. }
  612. /* We pass a CPU physical address to userland for MMIO instead of a
  613. * BAR value because X is lame and expects to be able to use that
  614. * to pass to /dev/mem!
  615. *
  616. * That means we may have 64-bit values where some apps only expect
  617. * 32 (like X itself since it thinks only Sparc has 64-bit MMIO).
  618. */
  619. *start = rsrc->start;
  620. *end = rsrc->end;
  621. }
  622. /**
  623. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  624. * @hose: newly allocated pci_controller to be setup
  625. * @dev: device node of the host bridge
  626. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  627. *
  628. * This function will parse the "ranges" property of a PCI host bridge device
  629. * node and setup the resource mapping of a pci controller based on its
  630. * content.
  631. *
  632. * Life would be boring if it wasn't for a few issues that we have to deal
  633. * with here:
  634. *
  635. * - We can only cope with one IO space range and up to 3 Memory space
  636. * ranges. However, some machines (thanks Apple !) tend to split their
  637. * space into lots of small contiguous ranges. So we have to coalesce.
  638. *
  639. * - Some busses have IO space not starting at 0, which causes trouble with
  640. * the way we do our IO resource renumbering. The code somewhat deals with
  641. * it for 64 bits but I would expect problems on 32 bits.
  642. *
  643. * - Some 32 bits platforms such as 4xx can have physical space larger than
  644. * 32 bits so we need to use 64 bits values for the parsing
  645. */
  646. void pci_process_bridge_OF_ranges(struct pci_controller *hose,
  647. struct device_node *dev, int primary)
  648. {
  649. int memno = 0;
  650. struct resource *res;
  651. struct of_pci_range range;
  652. struct of_pci_range_parser parser;
  653. printk(KERN_INFO "PCI host bridge %pOF %s ranges:\n",
  654. dev, primary ? "(primary)" : "");
  655. /* Check for ranges property */
  656. if (of_pci_range_parser_init(&parser, dev))
  657. return;
  658. /* Parse it */
  659. for_each_of_pci_range(&parser, &range) {
  660. /* If we failed translation or got a zero-sized region
  661. * (some FW try to feed us with non sensical zero sized regions
  662. * such as power3 which look like some kind of attempt at exposing
  663. * the VGA memory hole)
  664. */
  665. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  666. continue;
  667. /* Act based on address space type */
  668. res = NULL;
  669. switch (range.flags & IORESOURCE_TYPE_BITS) {
  670. case IORESOURCE_IO:
  671. printk(KERN_INFO
  672. " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  673. range.cpu_addr, range.cpu_addr + range.size - 1,
  674. range.pci_addr);
  675. /* We support only one IO range */
  676. if (hose->pci_io_size) {
  677. printk(KERN_INFO
  678. " \\--> Skipped (too many) !\n");
  679. continue;
  680. }
  681. #ifdef CONFIG_PPC32
  682. /* On 32 bits, limit I/O space to 16MB */
  683. if (range.size > 0x01000000)
  684. range.size = 0x01000000;
  685. /* 32 bits needs to map IOs here */
  686. hose->io_base_virt = ioremap(range.cpu_addr,
  687. range.size);
  688. /* Expect trouble if pci_addr is not 0 */
  689. if (primary)
  690. isa_io_base =
  691. (unsigned long)hose->io_base_virt;
  692. #endif /* CONFIG_PPC32 */
  693. /* pci_io_size and io_base_phys always represent IO
  694. * space starting at 0 so we factor in pci_addr
  695. */
  696. hose->pci_io_size = range.pci_addr + range.size;
  697. hose->io_base_phys = range.cpu_addr - range.pci_addr;
  698. /* Build resource */
  699. res = &hose->io_resource;
  700. range.cpu_addr = range.pci_addr;
  701. break;
  702. case IORESOURCE_MEM:
  703. printk(KERN_INFO
  704. " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  705. range.cpu_addr, range.cpu_addr + range.size - 1,
  706. range.pci_addr,
  707. (range.flags & IORESOURCE_PREFETCH) ?
  708. "Prefetch" : "");
  709. /* We support only 3 memory ranges */
  710. if (memno >= 3) {
  711. printk(KERN_INFO
  712. " \\--> Skipped (too many) !\n");
  713. continue;
  714. }
  715. /* Handles ISA memory hole space here */
  716. if (range.pci_addr == 0) {
  717. if (primary || isa_mem_base == 0)
  718. isa_mem_base = range.cpu_addr;
  719. hose->isa_mem_phys = range.cpu_addr;
  720. hose->isa_mem_size = range.size;
  721. }
  722. /* Build resource */
  723. hose->mem_offset[memno] = range.cpu_addr -
  724. range.pci_addr;
  725. res = &hose->mem_resources[memno++];
  726. break;
  727. }
  728. if (res != NULL) {
  729. res->name = dev->full_name;
  730. res->flags = range.flags;
  731. res->start = range.cpu_addr;
  732. res->end = range.cpu_addr + range.size - 1;
  733. res->parent = res->child = res->sibling = NULL;
  734. }
  735. }
  736. }
  737. /* Decide whether to display the domain number in /proc */
  738. int pci_proc_domain(struct pci_bus *bus)
  739. {
  740. struct pci_controller *hose = pci_bus_to_host(bus);
  741. if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
  742. return 0;
  743. if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
  744. return hose->global_number != 0;
  745. return 1;
  746. }
  747. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  748. {
  749. if (ppc_md.pcibios_root_bridge_prepare)
  750. return ppc_md.pcibios_root_bridge_prepare(bridge);
  751. return 0;
  752. }
  753. /* This header fixup will do the resource fixup for all devices as they are
  754. * probed, but not for bridge ranges
  755. */
  756. static void pcibios_fixup_resources(struct pci_dev *dev)
  757. {
  758. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  759. struct resource *res;
  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. pci_dev_for_each_resource(dev, res, i) {
  769. struct pci_bus_region reg;
  770. if (!res->flags)
  771. continue;
  772. /* If we're going to re-assign everything, we mark all resources
  773. * as unset (and 0-base them). In addition, we mark BARs starting
  774. * at 0 as unset as well, except if PCI_PROBE_ONLY is also set
  775. * since in that case, we don't want to re-assign anything
  776. */
  777. pcibios_resource_to_bus(dev->bus, &reg, res);
  778. if (pci_has_flag(PCI_REASSIGN_ALL_RSRC) ||
  779. (reg.start == 0 && !pci_has_flag(PCI_PROBE_ONLY))) {
  780. /* Only print message if not re-assigning */
  781. if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC))
  782. pr_debug("PCI:%s Resource %d %pR is unassigned\n",
  783. pci_name(dev), i, res);
  784. res->end -= res->start;
  785. res->start = 0;
  786. res->flags |= IORESOURCE_UNSET;
  787. continue;
  788. }
  789. pr_debug("PCI:%s Resource %d %pR\n", pci_name(dev), i, res);
  790. }
  791. /* Call machine specific resource fixup */
  792. if (ppc_md.pcibios_fixup_resources)
  793. ppc_md.pcibios_fixup_resources(dev);
  794. }
  795. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  796. /* This function tries to figure out if a bridge resource has been initialized
  797. * by the firmware or not. It doesn't have to be absolutely bullet proof, but
  798. * things go more smoothly when it gets it right. It should covers cases such
  799. * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
  800. */
  801. static int pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
  802. struct resource *res)
  803. {
  804. struct pci_controller *hose = pci_bus_to_host(bus);
  805. struct pci_dev *dev = bus->self;
  806. resource_size_t offset;
  807. struct pci_bus_region region;
  808. u16 command;
  809. int i;
  810. /* We don't do anything if PCI_PROBE_ONLY is set */
  811. if (pci_has_flag(PCI_PROBE_ONLY))
  812. return 0;
  813. /* Job is a bit different between memory and IO */
  814. if (res->flags & IORESOURCE_MEM) {
  815. pcibios_resource_to_bus(dev->bus, &region, res);
  816. /* If the BAR is non-0 then it's probably been initialized */
  817. if (region.start != 0)
  818. return 0;
  819. /* The BAR is 0, let's check if memory decoding is enabled on
  820. * the bridge. If not, we consider it unassigned
  821. */
  822. pci_read_config_word(dev, PCI_COMMAND, &command);
  823. if ((command & PCI_COMMAND_MEMORY) == 0)
  824. return 1;
  825. /* Memory decoding is enabled and the BAR is 0. If any of the bridge
  826. * resources covers that starting address (0 then it's good enough for
  827. * us for memory space)
  828. */
  829. for (i = 0; i < 3; i++) {
  830. if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
  831. hose->mem_resources[i].start == hose->mem_offset[i])
  832. return 0;
  833. }
  834. /* Well, it starts at 0 and we know it will collide so we may as
  835. * well consider it as unassigned. That covers the Apple case.
  836. */
  837. return 1;
  838. } else {
  839. /* If the BAR is non-0, then we consider it assigned */
  840. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  841. if (((res->start - offset) & 0xfffffffful) != 0)
  842. return 0;
  843. /* Here, we are a bit different than memory as typically IO space
  844. * starting at low addresses -is- valid. What we do instead if that
  845. * we consider as unassigned anything that doesn't have IO enabled
  846. * in the PCI command register, and that's it.
  847. */
  848. pci_read_config_word(dev, PCI_COMMAND, &command);
  849. if (command & PCI_COMMAND_IO)
  850. return 0;
  851. /* It's starting at 0 and IO is disabled in the bridge, consider
  852. * it unassigned
  853. */
  854. return 1;
  855. }
  856. }
  857. /* Fixup resources of a PCI<->PCI bridge */
  858. static void pcibios_fixup_bridge(struct pci_bus *bus)
  859. {
  860. struct resource *res;
  861. int i;
  862. struct pci_dev *dev = bus->self;
  863. pci_bus_for_each_resource(bus, res, i) {
  864. if (!res || !res->flags)
  865. continue;
  866. if (i >= 3 && bus->self->transparent)
  867. continue;
  868. /* If we're going to reassign everything, we can
  869. * shrink the P2P resource to have size as being
  870. * of 0 in order to save space.
  871. */
  872. if (pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
  873. res->flags |= IORESOURCE_UNSET;
  874. res->start = 0;
  875. res->end = -1;
  876. continue;
  877. }
  878. pr_debug("PCI:%s Bus rsrc %d %pR\n", pci_name(dev), i, res);
  879. /* Try to detect uninitialized P2P bridge resources,
  880. * and clear them out so they get re-assigned later
  881. */
  882. if (pcibios_uninitialized_bridge_resource(bus, res)) {
  883. res->flags = 0;
  884. pr_debug("PCI:%s (unassigned)\n", pci_name(dev));
  885. }
  886. }
  887. }
  888. void pcibios_setup_bus_self(struct pci_bus *bus)
  889. {
  890. struct pci_controller *phb;
  891. /* Fix up the bus resources for P2P bridges */
  892. if (bus->self != NULL)
  893. pcibios_fixup_bridge(bus);
  894. /* Platform specific bus fixups. This is currently only used
  895. * by fsl_pci and I'm hoping to get rid of it at some point
  896. */
  897. if (ppc_md.pcibios_fixup_bus)
  898. ppc_md.pcibios_fixup_bus(bus);
  899. /* Setup bus DMA mappings */
  900. phb = pci_bus_to_host(bus);
  901. if (phb->controller_ops.dma_bus_setup)
  902. phb->controller_ops.dma_bus_setup(bus);
  903. }
  904. void pcibios_bus_add_device(struct pci_dev *dev)
  905. {
  906. struct pci_controller *phb;
  907. /* Fixup NUMA node as it may not be setup yet by the generic
  908. * code and is needed by the DMA init
  909. */
  910. set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
  911. /* Hook up default DMA ops */
  912. set_dma_ops(&dev->dev, pci_dma_ops);
  913. dev->dev.archdata.dma_offset = PCI_DRAM_OFFSET;
  914. /* Additional platform DMA/iommu setup */
  915. phb = pci_bus_to_host(dev->bus);
  916. if (phb->controller_ops.dma_dev_setup)
  917. phb->controller_ops.dma_dev_setup(dev);
  918. /* Read default IRQs and fixup if necessary */
  919. pci_read_irq_line(dev);
  920. if (ppc_md.pci_irq_fixup)
  921. ppc_md.pci_irq_fixup(dev);
  922. if (ppc_md.pcibios_bus_add_device)
  923. ppc_md.pcibios_bus_add_device(dev);
  924. }
  925. int pcibios_device_add(struct pci_dev *dev)
  926. {
  927. struct irq_domain *d;
  928. #ifdef CONFIG_PCI_IOV
  929. if (ppc_md.pcibios_fixup_sriov)
  930. ppc_md.pcibios_fixup_sriov(dev);
  931. #endif /* CONFIG_PCI_IOV */
  932. d = dev_get_msi_domain(&dev->bus->dev);
  933. if (d)
  934. dev_set_msi_domain(&dev->dev, d);
  935. return 0;
  936. }
  937. void pcibios_set_master(struct pci_dev *dev)
  938. {
  939. /* No special bus mastering setup handling */
  940. }
  941. void pcibios_fixup_bus(struct pci_bus *bus)
  942. {
  943. /* When called from the generic PCI probe, read PCI<->PCI bridge
  944. * bases. This is -not- called when generating the PCI tree from
  945. * the OF device-tree.
  946. */
  947. pci_read_bridge_bases(bus);
  948. /* Now fixup the bus */
  949. pcibios_setup_bus_self(bus);
  950. }
  951. EXPORT_SYMBOL(pcibios_fixup_bus);
  952. static int skip_isa_ioresource_align(struct pci_dev *dev)
  953. {
  954. if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
  955. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  956. return 1;
  957. return 0;
  958. }
  959. /*
  960. * We need to avoid collisions with `mirrored' VGA ports
  961. * and other strange ISA hardware, so we always want the
  962. * addresses to be allocated in the 0x000-0x0ff region
  963. * modulo 0x400.
  964. *
  965. * Why? Because some silly external IO cards only decode
  966. * the low 10 bits of the IO address. The 0x00-0xff region
  967. * is reserved for motherboard devices that decode all 16
  968. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  969. * but we want to try to avoid allocating at 0x2900-0x2bff
  970. * which might have be mirrored at 0x0100-0x03ff..
  971. */
  972. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  973. resource_size_t size, resource_size_t align)
  974. {
  975. struct pci_dev *dev = data;
  976. resource_size_t start = res->start;
  977. if (res->flags & IORESOURCE_IO) {
  978. if (skip_isa_ioresource_align(dev))
  979. return start;
  980. if (start & 0x300)
  981. start = (start + 0x3ff) & ~0x3ff;
  982. }
  983. return start;
  984. }
  985. EXPORT_SYMBOL(pcibios_align_resource);
  986. /*
  987. * Reparent resource children of pr that conflict with res
  988. * under res, and make res replace those children.
  989. */
  990. static int reparent_resources(struct resource *parent,
  991. struct resource *res)
  992. {
  993. struct resource *p, **pp;
  994. struct resource **firstpp = NULL;
  995. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  996. if (p->end < res->start)
  997. continue;
  998. if (res->end < p->start)
  999. break;
  1000. if (p->start < res->start || p->end > res->end)
  1001. return -1; /* not completely contained */
  1002. if (firstpp == NULL)
  1003. firstpp = pp;
  1004. }
  1005. if (firstpp == NULL)
  1006. return -1; /* didn't find any conflicting entries? */
  1007. res->parent = parent;
  1008. res->child = *firstpp;
  1009. res->sibling = *pp;
  1010. *firstpp = res;
  1011. *pp = NULL;
  1012. for (p = res->child; p != NULL; p = p->sibling) {
  1013. p->parent = res;
  1014. pr_debug("PCI: Reparented %s %pR under %s\n",
  1015. p->name, p, res->name);
  1016. }
  1017. return 0;
  1018. }
  1019. /*
  1020. * Handle resources of PCI devices. If the world were perfect, we could
  1021. * just allocate all the resource regions and do nothing more. It isn't.
  1022. * On the other hand, we cannot just re-allocate all devices, as it would
  1023. * require us to know lots of host bridge internals. So we attempt to
  1024. * keep as much of the original configuration as possible, but tweak it
  1025. * when it's found to be wrong.
  1026. *
  1027. * Known BIOS problems we have to work around:
  1028. * - I/O or memory regions not configured
  1029. * - regions configured, but not enabled in the command register
  1030. * - bogus I/O addresses above 64K used
  1031. * - expansion ROMs left enabled (this may sound harmless, but given
  1032. * the fact the PCI specs explicitly allow address decoders to be
  1033. * shared between expansion ROMs and other resource regions, it's
  1034. * at least dangerous)
  1035. *
  1036. * Our solution:
  1037. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  1038. * This gives us fixed barriers on where we can allocate.
  1039. * (2) Allocate resources for all enabled devices. If there is
  1040. * a collision, just mark the resource as unallocated. Also
  1041. * disable expansion ROMs during this step.
  1042. * (3) Try to allocate resources for disabled devices. If the
  1043. * resources were assigned correctly, everything goes well,
  1044. * if they weren't, they won't disturb allocation of other
  1045. * resources.
  1046. * (4) Assign new addresses to resources which were either
  1047. * not configured at all or misconfigured. If explicitly
  1048. * requested by the user, configure expansion ROM address
  1049. * as well.
  1050. */
  1051. static void pcibios_allocate_bus_resources(struct pci_bus *bus)
  1052. {
  1053. struct pci_bus *b;
  1054. int i;
  1055. struct resource *res, *pr;
  1056. pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
  1057. pci_domain_nr(bus), bus->number);
  1058. pci_bus_for_each_resource(bus, res, i) {
  1059. if (!res || !res->flags || res->start > res->end || res->parent)
  1060. continue;
  1061. /* If the resource was left unset at this point, we clear it */
  1062. if (res->flags & IORESOURCE_UNSET)
  1063. goto clear_resource;
  1064. if (bus->parent == NULL)
  1065. pr = (res->flags & IORESOURCE_IO) ?
  1066. &ioport_resource : &iomem_resource;
  1067. else {
  1068. pr = pci_find_parent_resource(bus->self, res);
  1069. if (pr == res) {
  1070. /* this happens when the generic PCI
  1071. * code (wrongly) decides that this
  1072. * bridge is transparent -- paulus
  1073. */
  1074. continue;
  1075. }
  1076. }
  1077. pr_debug("PCI: %s (bus %d) bridge rsrc %d: %pR, parent %p (%s)\n",
  1078. bus->self ? pci_name(bus->self) : "PHB", bus->number,
  1079. i, res, pr, (pr && pr->name) ? pr->name : "nil");
  1080. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  1081. struct pci_dev *dev = bus->self;
  1082. if (request_resource(pr, res) == 0)
  1083. continue;
  1084. /*
  1085. * Must be a conflict with an existing entry.
  1086. * Move that entry (or entries) under the
  1087. * bridge resource and try again.
  1088. */
  1089. if (reparent_resources(pr, res) == 0)
  1090. continue;
  1091. if (dev && i < PCI_BRIDGE_RESOURCE_NUM &&
  1092. pci_claim_bridge_resource(dev,
  1093. i + PCI_BRIDGE_RESOURCES) == 0)
  1094. continue;
  1095. }
  1096. pr_warn("PCI: Cannot allocate resource region %d of PCI bridge %d, will remap\n",
  1097. i, bus->number);
  1098. clear_resource:
  1099. /* The resource might be figured out when doing
  1100. * reassignment based on the resources required
  1101. * by the downstream PCI devices. Here we set
  1102. * the size of the resource to be 0 in order to
  1103. * save more space.
  1104. */
  1105. res->start = 0;
  1106. res->end = -1;
  1107. res->flags = 0;
  1108. }
  1109. list_for_each_entry(b, &bus->children, node)
  1110. pcibios_allocate_bus_resources(b);
  1111. }
  1112. static inline void alloc_resource(struct pci_dev *dev, int idx)
  1113. {
  1114. struct resource *pr, *r = &dev->resource[idx];
  1115. pr_debug("PCI: Allocating %s: Resource %d: %pR\n",
  1116. pci_name(dev), idx, r);
  1117. pr = pci_find_parent_resource(dev, r);
  1118. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  1119. request_resource(pr, r) < 0) {
  1120. printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
  1121. " of device %s, will remap\n", idx, pci_name(dev));
  1122. if (pr)
  1123. pr_debug("PCI: parent is %p: %pR\n", pr, pr);
  1124. /* We'll assign a new address later */
  1125. r->flags |= IORESOURCE_UNSET;
  1126. r->end -= r->start;
  1127. r->start = 0;
  1128. }
  1129. }
  1130. static void __init pcibios_allocate_resources(int pass)
  1131. {
  1132. struct pci_dev *dev = NULL;
  1133. int idx, disabled;
  1134. u16 command;
  1135. struct resource *r;
  1136. for_each_pci_dev(dev) {
  1137. pci_read_config_word(dev, PCI_COMMAND, &command);
  1138. for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
  1139. r = &dev->resource[idx];
  1140. if (r->parent) /* Already allocated */
  1141. continue;
  1142. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  1143. continue; /* Not assigned at all */
  1144. /* We only allocate ROMs on pass 1 just in case they
  1145. * have been screwed up by firmware
  1146. */
  1147. if (idx == PCI_ROM_RESOURCE )
  1148. disabled = 1;
  1149. if (r->flags & IORESOURCE_IO)
  1150. disabled = !(command & PCI_COMMAND_IO);
  1151. else
  1152. disabled = !(command & PCI_COMMAND_MEMORY);
  1153. if (pass == disabled)
  1154. alloc_resource(dev, idx);
  1155. }
  1156. if (pass)
  1157. continue;
  1158. r = &dev->resource[PCI_ROM_RESOURCE];
  1159. if (r->flags) {
  1160. /* Turn the ROM off, leave the resource region,
  1161. * but keep it unregistered.
  1162. */
  1163. u32 reg;
  1164. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  1165. if (reg & PCI_ROM_ADDRESS_ENABLE) {
  1166. pr_debug("PCI: Switching off ROM of %s\n",
  1167. pci_name(dev));
  1168. r->flags &= ~IORESOURCE_ROM_ENABLE;
  1169. pci_write_config_dword(dev, dev->rom_base_reg,
  1170. reg & ~PCI_ROM_ADDRESS_ENABLE);
  1171. }
  1172. }
  1173. }
  1174. }
  1175. static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
  1176. {
  1177. struct pci_controller *hose = pci_bus_to_host(bus);
  1178. resource_size_t offset;
  1179. struct resource *res, *pres;
  1180. int i;
  1181. pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
  1182. /* Check for IO */
  1183. if (!(hose->io_resource.flags & IORESOURCE_IO))
  1184. goto no_io;
  1185. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  1186. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1187. BUG_ON(res == NULL);
  1188. res->name = "Legacy IO";
  1189. res->flags = IORESOURCE_IO;
  1190. res->start = offset;
  1191. res->end = (offset + 0xfff) & 0xfffffffful;
  1192. pr_debug("Candidate legacy IO: %pR\n", res);
  1193. if (request_resource(&hose->io_resource, res)) {
  1194. printk(KERN_DEBUG
  1195. "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
  1196. pci_domain_nr(bus), bus->number, res);
  1197. kfree(res);
  1198. }
  1199. no_io:
  1200. /* Check for memory */
  1201. for (i = 0; i < 3; i++) {
  1202. pres = &hose->mem_resources[i];
  1203. offset = hose->mem_offset[i];
  1204. if (!(pres->flags & IORESOURCE_MEM))
  1205. continue;
  1206. pr_debug("hose mem res: %pR\n", pres);
  1207. if ((pres->start - offset) <= 0xa0000 &&
  1208. (pres->end - offset) >= 0xbffff)
  1209. break;
  1210. }
  1211. if (i >= 3)
  1212. return;
  1213. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1214. BUG_ON(res == NULL);
  1215. res->name = "Legacy VGA memory";
  1216. res->flags = IORESOURCE_MEM;
  1217. res->start = 0xa0000 + offset;
  1218. res->end = 0xbffff + offset;
  1219. pr_debug("Candidate VGA memory: %pR\n", res);
  1220. if (request_resource(pres, res)) {
  1221. printk(KERN_DEBUG
  1222. "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
  1223. pci_domain_nr(bus), bus->number, res);
  1224. kfree(res);
  1225. }
  1226. }
  1227. void __init pcibios_resource_survey(void)
  1228. {
  1229. struct pci_bus *b;
  1230. /* Allocate and assign resources */
  1231. list_for_each_entry(b, &pci_root_buses, node)
  1232. pcibios_allocate_bus_resources(b);
  1233. if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
  1234. pcibios_allocate_resources(0);
  1235. pcibios_allocate_resources(1);
  1236. }
  1237. /* Before we start assigning unassigned resource, we try to reserve
  1238. * the low IO area and the VGA memory area if they intersect the
  1239. * bus available resources to avoid allocating things on top of them
  1240. */
  1241. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  1242. list_for_each_entry(b, &pci_root_buses, node)
  1243. pcibios_reserve_legacy_regions(b);
  1244. }
  1245. /* Now, if the platform didn't decide to blindly trust the firmware,
  1246. * we proceed to assigning things that were left unassigned
  1247. */
  1248. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  1249. pr_debug("PCI: Assigning unassigned resources...\n");
  1250. pci_assign_unassigned_resources();
  1251. }
  1252. }
  1253. /* This is used by the PCI hotplug driver to allocate resource
  1254. * of newly plugged busses. We can try to consolidate with the
  1255. * rest of the code later, for now, keep it as-is as our main
  1256. * resource allocation function doesn't deal with sub-trees yet.
  1257. */
  1258. void pcibios_claim_one_bus(struct pci_bus *bus)
  1259. {
  1260. struct pci_dev *dev;
  1261. struct pci_bus *child_bus;
  1262. list_for_each_entry(dev, &bus->devices, bus_list) {
  1263. struct resource *r;
  1264. int i;
  1265. pci_dev_for_each_resource(dev, r, i) {
  1266. if (r->parent || !r->start || !r->flags)
  1267. continue;
  1268. pr_debug("PCI: Claiming %s: Resource %d: %pR\n",
  1269. pci_name(dev), i, r);
  1270. if (pci_claim_resource(dev, i) == 0)
  1271. continue;
  1272. pci_claim_bridge_resource(dev, i);
  1273. }
  1274. }
  1275. list_for_each_entry(child_bus, &bus->children, node)
  1276. pcibios_claim_one_bus(child_bus);
  1277. }
  1278. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  1279. /* pcibios_finish_adding_to_bus
  1280. *
  1281. * This is to be called by the hotplug code after devices have been
  1282. * added to a bus, this include calling it for a PHB that is just
  1283. * being added
  1284. */
  1285. void pcibios_finish_adding_to_bus(struct pci_bus *bus)
  1286. {
  1287. pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
  1288. pci_domain_nr(bus), bus->number);
  1289. /* Allocate bus and devices resources */
  1290. pcibios_allocate_bus_resources(bus);
  1291. pcibios_claim_one_bus(bus);
  1292. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  1293. if (bus->self)
  1294. pci_assign_unassigned_bridge_resources(bus->self);
  1295. else
  1296. pci_assign_unassigned_bus_resources(bus);
  1297. }
  1298. /* Add new devices to global lists. Register in proc, sysfs. */
  1299. pci_bus_add_devices(bus);
  1300. }
  1301. EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
  1302. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1303. {
  1304. struct pci_controller *phb = pci_bus_to_host(dev->bus);
  1305. if (phb->controller_ops.enable_device_hook)
  1306. if (!phb->controller_ops.enable_device_hook(dev))
  1307. return -EINVAL;
  1308. return pci_enable_resources(dev, mask);
  1309. }
  1310. void pcibios_disable_device(struct pci_dev *dev)
  1311. {
  1312. struct pci_controller *phb = pci_bus_to_host(dev->bus);
  1313. if (phb->controller_ops.disable_device)
  1314. phb->controller_ops.disable_device(dev);
  1315. }
  1316. resource_size_t pcibios_io_space_offset(struct pci_controller *hose)
  1317. {
  1318. return (unsigned long) hose->io_base_virt - _IO_BASE;
  1319. }
  1320. static void pcibios_setup_phb_resources(struct pci_controller *hose,
  1321. struct list_head *resources)
  1322. {
  1323. struct resource *res;
  1324. resource_size_t offset;
  1325. int i;
  1326. /* Hookup PHB IO resource */
  1327. res = &hose->io_resource;
  1328. if (!res->flags) {
  1329. pr_debug("PCI: I/O resource not set for host"
  1330. " bridge %pOF (domain %d)\n",
  1331. hose->dn, hose->global_number);
  1332. } else {
  1333. offset = pcibios_io_space_offset(hose);
  1334. pr_debug("PCI: PHB IO resource = %pR off 0x%08llx\n",
  1335. res, (unsigned long long)offset);
  1336. pci_add_resource_offset(resources, res, offset);
  1337. }
  1338. /* Hookup PHB Memory resources */
  1339. for (i = 0; i < 3; ++i) {
  1340. res = &hose->mem_resources[i];
  1341. if (!res->flags)
  1342. continue;
  1343. offset = hose->mem_offset[i];
  1344. pr_debug("PCI: PHB MEM resource %d = %pR off 0x%08llx\n", i,
  1345. res, (unsigned long long)offset);
  1346. pci_add_resource_offset(resources, res, offset);
  1347. }
  1348. }
  1349. /*
  1350. * Null PCI config access functions, for the case when we can't
  1351. * find a hose.
  1352. */
  1353. #define NULL_PCI_OP(rw, size, type) \
  1354. static int \
  1355. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1356. { \
  1357. return PCIBIOS_DEVICE_NOT_FOUND; \
  1358. }
  1359. static int
  1360. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1361. int len, u32 *val)
  1362. {
  1363. return PCIBIOS_DEVICE_NOT_FOUND;
  1364. }
  1365. static int
  1366. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1367. int len, u32 val)
  1368. {
  1369. return PCIBIOS_DEVICE_NOT_FOUND;
  1370. }
  1371. static struct pci_ops null_pci_ops =
  1372. {
  1373. .read = null_read_config,
  1374. .write = null_write_config,
  1375. };
  1376. /*
  1377. * These functions are used early on before PCI scanning is done
  1378. * and all of the pci_dev and pci_bus structures have been created.
  1379. */
  1380. static struct pci_bus *
  1381. fake_pci_bus(struct pci_controller *hose, int busnr)
  1382. {
  1383. static struct pci_bus bus;
  1384. if (hose == NULL) {
  1385. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1386. }
  1387. bus.number = busnr;
  1388. bus.sysdata = hose;
  1389. bus.ops = hose? hose->ops: &null_pci_ops;
  1390. return &bus;
  1391. }
  1392. #define EARLY_PCI_OP(rw, size, type) \
  1393. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1394. int devfn, int offset, type value) \
  1395. { \
  1396. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1397. devfn, offset, value); \
  1398. }
  1399. EARLY_PCI_OP(read, byte, u8 *)
  1400. EARLY_PCI_OP(read, word, u16 *)
  1401. EARLY_PCI_OP(read, dword, u32 *)
  1402. EARLY_PCI_OP(write, byte, u8)
  1403. EARLY_PCI_OP(write, word, u16)
  1404. EARLY_PCI_OP(write, dword, u32)
  1405. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  1406. int cap)
  1407. {
  1408. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  1409. }
  1410. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  1411. {
  1412. struct pci_controller *hose = bus->sysdata;
  1413. return of_node_get(hose->dn);
  1414. }
  1415. /**
  1416. * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
  1417. * @hose: Pointer to the PCI host controller instance structure
  1418. */
  1419. void pcibios_scan_phb(struct pci_controller *hose)
  1420. {
  1421. LIST_HEAD(resources);
  1422. struct pci_bus *bus;
  1423. struct device_node *node = hose->dn;
  1424. int mode;
  1425. pr_debug("PCI: Scanning PHB %pOF\n", node);
  1426. /* Get some IO space for the new PHB */
  1427. pcibios_setup_phb_io_space(hose);
  1428. /* Wire up PHB bus resources */
  1429. pcibios_setup_phb_resources(hose, &resources);
  1430. hose->busn.start = hose->first_busno;
  1431. hose->busn.end = hose->last_busno;
  1432. hose->busn.flags = IORESOURCE_BUS;
  1433. pci_add_resource(&resources, &hose->busn);
  1434. /* Create an empty bus for the toplevel */
  1435. bus = pci_create_root_bus(hose->parent, hose->first_busno,
  1436. hose->ops, hose, &resources);
  1437. if (bus == NULL) {
  1438. pr_err("Failed to create bus for PCI domain %04x\n",
  1439. hose->global_number);
  1440. pci_free_resource_list(&resources);
  1441. return;
  1442. }
  1443. hose->bus = bus;
  1444. /* Get probe mode and perform scan */
  1445. mode = PCI_PROBE_NORMAL;
  1446. if (node && hose->controller_ops.probe_mode)
  1447. mode = hose->controller_ops.probe_mode(bus);
  1448. pr_debug(" probe mode: %d\n", mode);
  1449. if (mode == PCI_PROBE_DEVTREE)
  1450. of_scan_bus(node, bus);
  1451. if (mode == PCI_PROBE_NORMAL) {
  1452. pci_bus_update_busn_res_end(bus, 255);
  1453. hose->last_busno = pci_scan_child_bus(bus);
  1454. pci_bus_update_busn_res_end(bus, hose->last_busno);
  1455. }
  1456. /* Platform gets a chance to do some global fixups before
  1457. * we proceed to resource allocation
  1458. */
  1459. if (ppc_md.pcibios_fixup_phb)
  1460. ppc_md.pcibios_fixup_phb(hose);
  1461. /* Configure PCI Express settings */
  1462. if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
  1463. struct pci_bus *child;
  1464. list_for_each_entry(child, &bus->children, node)
  1465. pcie_bus_configure_settings(child);
  1466. }
  1467. }
  1468. EXPORT_SYMBOL_GPL(pcibios_scan_phb);
  1469. static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
  1470. {
  1471. int class = dev->class >> 8;
  1472. /* When configured as agent, programming interface = 1 */
  1473. int prog_if = dev->class & 0xf;
  1474. struct resource *r;
  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. pci_dev_for_each_resource(dev, r) {
  1481. r->start = 0;
  1482. r->end = 0;
  1483. r->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);