pci-common.c 45 KB

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