pci-common.c 46 KB

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