pci-common.c 48 KB

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