pci-common.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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/bootmem.h>
  23. #include <linux/mm.h>
  24. #include <linux/list.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/irq.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/slab.h>
  29. #include <linux/of.h>
  30. #include <linux/of_address.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_pci.h>
  33. #include <linux/export.h>
  34. #include <asm/processor.h>
  35. #include <linux/io.h>
  36. #include <asm/pci-bridge.h>
  37. #include <asm/byteorder.h>
  38. static DEFINE_SPINLOCK(hose_spinlock);
  39. LIST_HEAD(hose_list);
  40. /* XXX kill that some day ... */
  41. static int global_phb_number; /* Global phb counter */
  42. /* ISA Memory physical address */
  43. resource_size_t isa_mem_base;
  44. unsigned long isa_io_base;
  45. static int pci_bus_count;
  46. struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
  47. {
  48. struct pci_controller *phb;
  49. phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
  50. if (!phb)
  51. return NULL;
  52. spin_lock(&hose_spinlock);
  53. phb->global_number = global_phb_number++;
  54. list_add_tail(&phb->list_node, &hose_list);
  55. spin_unlock(&hose_spinlock);
  56. phb->dn = dev;
  57. phb->is_dynamic = mem_init_done;
  58. return phb;
  59. }
  60. void pcibios_free_controller(struct pci_controller *phb)
  61. {
  62. spin_lock(&hose_spinlock);
  63. list_del(&phb->list_node);
  64. spin_unlock(&hose_spinlock);
  65. if (phb->is_dynamic)
  66. kfree(phb);
  67. }
  68. static resource_size_t pcibios_io_size(const struct pci_controller *hose)
  69. {
  70. return resource_size(&hose->io_resource);
  71. }
  72. int pcibios_vaddr_is_ioport(void __iomem *address)
  73. {
  74. int ret = 0;
  75. struct pci_controller *hose;
  76. resource_size_t size;
  77. spin_lock(&hose_spinlock);
  78. list_for_each_entry(hose, &hose_list, list_node) {
  79. size = pcibios_io_size(hose);
  80. if (address >= hose->io_base_virt &&
  81. address < (hose->io_base_virt + size)) {
  82. ret = 1;
  83. break;
  84. }
  85. }
  86. spin_unlock(&hose_spinlock);
  87. return ret;
  88. }
  89. unsigned long pci_address_to_pio(phys_addr_t address)
  90. {
  91. struct pci_controller *hose;
  92. resource_size_t size;
  93. unsigned long ret = ~0;
  94. spin_lock(&hose_spinlock);
  95. list_for_each_entry(hose, &hose_list, list_node) {
  96. size = pcibios_io_size(hose);
  97. if (address >= hose->io_base_phys &&
  98. address < (hose->io_base_phys + size)) {
  99. unsigned long base =
  100. (unsigned long)hose->io_base_virt - _IO_BASE;
  101. ret = base + (address - hose->io_base_phys);
  102. break;
  103. }
  104. }
  105. spin_unlock(&hose_spinlock);
  106. return ret;
  107. }
  108. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  109. /*
  110. * Return the domain number for this bus.
  111. */
  112. int pci_domain_nr(struct pci_bus *bus)
  113. {
  114. struct pci_controller *hose = pci_bus_to_host(bus);
  115. return hose->global_number;
  116. }
  117. EXPORT_SYMBOL(pci_domain_nr);
  118. /* This routine is meant to be used early during boot, when the
  119. * PCI bus numbers have not yet been assigned, and you need to
  120. * issue PCI config cycles to an OF device.
  121. * It could also be used to "fix" RTAS config cycles if you want
  122. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  123. * config cycles.
  124. */
  125. struct pci_controller *pci_find_hose_for_OF_device(struct device_node *node)
  126. {
  127. while (node) {
  128. struct pci_controller *hose, *tmp;
  129. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  130. if (hose->dn == node)
  131. return hose;
  132. node = node->parent;
  133. }
  134. return NULL;
  135. }
  136. void pcibios_set_master(struct pci_dev *dev)
  137. {
  138. /* No special bus mastering setup handling */
  139. }
  140. /*
  141. * Platform support for /proc/bus/pci/X/Y mmap()s,
  142. * modelled on the sparc64 implementation by Dave Miller.
  143. * -- paulus.
  144. */
  145. /*
  146. * Adjust vm_pgoff of VMA such that it is the physical page offset
  147. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  148. *
  149. * Basically, the user finds the base address for his device which he wishes
  150. * to mmap. They read the 32-bit value from the config space base register,
  151. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  152. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  153. *
  154. * Returns negative error code on failure, zero on success.
  155. */
  156. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  157. resource_size_t *offset,
  158. enum pci_mmap_state mmap_state)
  159. {
  160. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  161. unsigned long io_offset = 0;
  162. int i, res_bit;
  163. if (!hose)
  164. return NULL; /* should never happen */
  165. /* If memory, add on the PCI bridge address offset */
  166. if (mmap_state == pci_mmap_mem) {
  167. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  168. *offset += hose->pci_mem_offset;
  169. #endif
  170. res_bit = IORESOURCE_MEM;
  171. } else {
  172. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  173. *offset += io_offset;
  174. res_bit = IORESOURCE_IO;
  175. }
  176. /*
  177. * Check that the offset requested corresponds to one of the
  178. * resources of the device.
  179. */
  180. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  181. struct resource *rp = &dev->resource[i];
  182. int flags = rp->flags;
  183. /* treat ROM as memory (should be already) */
  184. if (i == PCI_ROM_RESOURCE)
  185. flags |= IORESOURCE_MEM;
  186. /* Active and same type? */
  187. if ((flags & res_bit) == 0)
  188. continue;
  189. /* In the range of this resource? */
  190. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  191. continue;
  192. /* found it! construct the final physical address */
  193. if (mmap_state == pci_mmap_io)
  194. *offset += hose->io_base_phys - io_offset;
  195. return rp;
  196. }
  197. return NULL;
  198. }
  199. /*
  200. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  201. * device mapping.
  202. */
  203. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  204. pgprot_t protection,
  205. enum pci_mmap_state mmap_state,
  206. int write_combine)
  207. {
  208. pgprot_t prot = protection;
  209. /* Write combine is always 0 on non-memory space mappings. On
  210. * memory space, if the user didn't pass 1, we check for a
  211. * "prefetchable" resource. This is a bit hackish, but we use
  212. * this to workaround the inability of /sysfs to provide a write
  213. * combine bit
  214. */
  215. if (mmap_state != pci_mmap_mem)
  216. write_combine = 0;
  217. else if (write_combine == 0) {
  218. if (rp->flags & IORESOURCE_PREFETCH)
  219. write_combine = 1;
  220. }
  221. return pgprot_noncached(prot);
  222. }
  223. /*
  224. * This one is used by /dev/mem and fbdev who have no clue about the
  225. * PCI device, it tries to find the PCI device first and calls the
  226. * above routine
  227. */
  228. pgprot_t pci_phys_mem_access_prot(struct file *file,
  229. unsigned long pfn,
  230. unsigned long size,
  231. pgprot_t prot)
  232. {
  233. struct pci_dev *pdev = NULL;
  234. struct resource *found = NULL;
  235. resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
  236. int i;
  237. if (page_is_ram(pfn))
  238. return prot;
  239. prot = pgprot_noncached(prot);
  240. for_each_pci_dev(pdev) {
  241. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  242. struct resource *rp = &pdev->resource[i];
  243. int flags = rp->flags;
  244. /* Active and same type? */
  245. if ((flags & IORESOURCE_MEM) == 0)
  246. continue;
  247. /* In the range of this resource? */
  248. if (offset < (rp->start & PAGE_MASK) ||
  249. offset > rp->end)
  250. continue;
  251. found = rp;
  252. break;
  253. }
  254. if (found)
  255. break;
  256. }
  257. if (found) {
  258. if (found->flags & IORESOURCE_PREFETCH)
  259. prot = pgprot_noncached_wc(prot);
  260. pci_dev_put(pdev);
  261. }
  262. pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
  263. (unsigned long long)offset, pgprot_val(prot));
  264. return prot;
  265. }
  266. /*
  267. * Perform the actual remap of the pages for a PCI device mapping, as
  268. * appropriate for this architecture. The region in the process to map
  269. * is described by vm_start and vm_end members of VMA, the base physical
  270. * address is found in vm_pgoff.
  271. * The pci device structure is provided so that architectures may make mapping
  272. * decisions on a per-device or per-bus basis.
  273. *
  274. * Returns a negative error code on failure, zero on success.
  275. */
  276. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  277. enum pci_mmap_state mmap_state, int write_combine)
  278. {
  279. resource_size_t offset =
  280. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  281. struct resource *rp;
  282. int ret;
  283. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  284. if (rp == NULL)
  285. return -EINVAL;
  286. vma->vm_pgoff = offset >> PAGE_SHIFT;
  287. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  288. vma->vm_page_prot,
  289. mmap_state, write_combine);
  290. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  291. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  292. return ret;
  293. }
  294. /* This provides legacy IO read access on a bus */
  295. int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
  296. {
  297. unsigned long offset;
  298. struct pci_controller *hose = pci_bus_to_host(bus);
  299. struct resource *rp = &hose->io_resource;
  300. void __iomem *addr;
  301. /* Check if port can be supported by that bus. We only check
  302. * the ranges of the PHB though, not the bus itself as the rules
  303. * for forwarding legacy cycles down bridges are not our problem
  304. * here. So if the host bridge supports it, we do it.
  305. */
  306. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  307. offset += port;
  308. if (!(rp->flags & IORESOURCE_IO))
  309. return -ENXIO;
  310. if (offset < rp->start || (offset + size) > rp->end)
  311. return -ENXIO;
  312. addr = hose->io_base_virt + port;
  313. switch (size) {
  314. case 1:
  315. *((u8 *)val) = in_8(addr);
  316. return 1;
  317. case 2:
  318. if (port & 1)
  319. return -EINVAL;
  320. *((u16 *)val) = in_le16(addr);
  321. return 2;
  322. case 4:
  323. if (port & 3)
  324. return -EINVAL;
  325. *((u32 *)val) = in_le32(addr);
  326. return 4;
  327. }
  328. return -EINVAL;
  329. }
  330. /* This provides legacy IO write access on a bus */
  331. int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
  332. {
  333. unsigned long offset;
  334. struct pci_controller *hose = pci_bus_to_host(bus);
  335. struct resource *rp = &hose->io_resource;
  336. void __iomem *addr;
  337. /* Check if port can be supported by that bus. We only check
  338. * the ranges of the PHB though, not the bus itself as the rules
  339. * for forwarding legacy cycles down bridges are not our problem
  340. * here. So if the host bridge supports it, we do it.
  341. */
  342. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  343. offset += port;
  344. if (!(rp->flags & IORESOURCE_IO))
  345. return -ENXIO;
  346. if (offset < rp->start || (offset + size) > rp->end)
  347. return -ENXIO;
  348. addr = hose->io_base_virt + port;
  349. /* WARNING: The generic code is idiotic. It gets passed a pointer
  350. * to what can be a 1, 2 or 4 byte quantity and always reads that
  351. * as a u32, which means that we have to correct the location of
  352. * the data read within those 32 bits for size 1 and 2
  353. */
  354. switch (size) {
  355. case 1:
  356. out_8(addr, val >> 24);
  357. return 1;
  358. case 2:
  359. if (port & 1)
  360. return -EINVAL;
  361. out_le16(addr, val >> 16);
  362. return 2;
  363. case 4:
  364. if (port & 3)
  365. return -EINVAL;
  366. out_le32(addr, val);
  367. return 4;
  368. }
  369. return -EINVAL;
  370. }
  371. /* This provides legacy IO or memory mmap access on a bus */
  372. int pci_mmap_legacy_page_range(struct pci_bus *bus,
  373. struct vm_area_struct *vma,
  374. enum pci_mmap_state mmap_state)
  375. {
  376. struct pci_controller *hose = pci_bus_to_host(bus);
  377. resource_size_t offset =
  378. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  379. resource_size_t size = vma->vm_end - vma->vm_start;
  380. struct resource *rp;
  381. pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
  382. pci_domain_nr(bus), bus->number,
  383. mmap_state == pci_mmap_mem ? "MEM" : "IO",
  384. (unsigned long long)offset,
  385. (unsigned long long)(offset + size - 1));
  386. if (mmap_state == pci_mmap_mem) {
  387. /* Hack alert !
  388. *
  389. * Because X is lame and can fail starting if it gets an error
  390. * trying to mmap legacy_mem (instead of just moving on without
  391. * legacy memory access) we fake it here by giving it anonymous
  392. * memory, effectively behaving just like /dev/zero
  393. */
  394. if ((offset + size) > hose->isa_mem_size) {
  395. #ifdef CONFIG_MMU
  396. pr_debug("Process %s (pid:%d) mapped non-existing PCI",
  397. current->comm, current->pid);
  398. pr_debug("legacy memory for 0%04x:%02x\n",
  399. pci_domain_nr(bus), bus->number);
  400. #endif
  401. if (vma->vm_flags & VM_SHARED)
  402. return shmem_zero_setup(vma);
  403. return 0;
  404. }
  405. offset += hose->isa_mem_phys;
  406. } else {
  407. unsigned long io_offset = (unsigned long)hose->io_base_virt -
  408. _IO_BASE;
  409. unsigned long roffset = offset + io_offset;
  410. rp = &hose->io_resource;
  411. if (!(rp->flags & IORESOURCE_IO))
  412. return -ENXIO;
  413. if (roffset < rp->start || (roffset + size) > rp->end)
  414. return -ENXIO;
  415. offset += hose->io_base_phys;
  416. }
  417. pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
  418. vma->vm_pgoff = offset >> PAGE_SHIFT;
  419. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  420. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  421. vma->vm_end - vma->vm_start,
  422. vma->vm_page_prot);
  423. }
  424. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  425. const struct resource *rsrc,
  426. resource_size_t *start, resource_size_t *end)
  427. {
  428. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  429. resource_size_t offset = 0;
  430. if (hose == NULL)
  431. return;
  432. if (rsrc->flags & IORESOURCE_IO)
  433. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  434. /* We pass a fully fixed up address to userland for MMIO instead of
  435. * a BAR value because X is lame and expects to be able to use that
  436. * to pass to /dev/mem !
  437. *
  438. * That means that we'll have potentially 64 bits values where some
  439. * userland apps only expect 32 (like X itself since it thinks only
  440. * Sparc has 64 bits MMIO) but if we don't do that, we break it on
  441. * 32 bits CHRPs :-(
  442. *
  443. * Hopefully, the sysfs insterface is immune to that gunk. Once X
  444. * has been fixed (and the fix spread enough), we can re-enable the
  445. * 2 lines below and pass down a BAR value to userland. In that case
  446. * we'll also have to re-enable the matching code in
  447. * __pci_mmap_make_offset().
  448. *
  449. * BenH.
  450. */
  451. #if 0
  452. else if (rsrc->flags & IORESOURCE_MEM)
  453. offset = hose->pci_mem_offset;
  454. #endif
  455. *start = rsrc->start - offset;
  456. *end = rsrc->end - offset;
  457. }
  458. /**
  459. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  460. * @hose: newly allocated pci_controller to be setup
  461. * @dev: device node of the host bridge
  462. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  463. *
  464. * This function will parse the "ranges" property of a PCI host bridge device
  465. * node and setup the resource mapping of a pci controller based on its
  466. * content.
  467. *
  468. * Life would be boring if it wasn't for a few issues that we have to deal
  469. * with here:
  470. *
  471. * - We can only cope with one IO space range and up to 3 Memory space
  472. * ranges. However, some machines (thanks Apple !) tend to split their
  473. * space into lots of small contiguous ranges. So we have to coalesce.
  474. *
  475. * - We can only cope with all memory ranges having the same offset
  476. * between CPU addresses and PCI addresses. Unfortunately, some bridges
  477. * are setup for a large 1:1 mapping along with a small "window" which
  478. * maps PCI address 0 to some arbitrary high address of the CPU space in
  479. * order to give access to the ISA memory hole.
  480. * The way out of here that I've chosen for now is to always set the
  481. * offset based on the first resource found, then override it if we
  482. * have a different offset and the previous was set by an ISA hole.
  483. *
  484. * - Some busses have IO space not starting at 0, which causes trouble with
  485. * the way we do our IO resource renumbering. The code somewhat deals with
  486. * it for 64 bits but I would expect problems on 32 bits.
  487. *
  488. * - Some 32 bits platforms such as 4xx can have physical space larger than
  489. * 32 bits so we need to use 64 bits values for the parsing
  490. */
  491. void pci_process_bridge_OF_ranges(struct pci_controller *hose,
  492. struct device_node *dev, int primary)
  493. {
  494. int memno = 0, isa_hole = -1;
  495. unsigned long long isa_mb = 0;
  496. struct resource *res;
  497. struct of_pci_range range;
  498. struct of_pci_range_parser parser;
  499. pr_info("PCI host bridge %s %s ranges:\n",
  500. dev->full_name, primary ? "(primary)" : "");
  501. /* Check for ranges property */
  502. if (of_pci_range_parser_init(&parser, dev))
  503. return;
  504. pr_debug("Parsing ranges property...\n");
  505. for_each_of_pci_range(&parser, &range) {
  506. /* Read next ranges element */
  507. pr_debug("pci_space: 0x%08x pci_addr:0x%016llx ",
  508. range.pci_space, range.pci_addr);
  509. pr_debug("cpu_addr:0x%016llx size:0x%016llx\n",
  510. range.cpu_addr, range.size);
  511. /* If we failed translation or got a zero-sized region
  512. * (some FW try to feed us with non sensical zero sized regions
  513. * such as power3 which look like some kind of attempt
  514. * at exposing the VGA memory hole)
  515. */
  516. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  517. continue;
  518. /* Act based on address space type */
  519. res = NULL;
  520. switch (range.flags & IORESOURCE_TYPE_BITS) {
  521. case IORESOURCE_IO:
  522. pr_info(" IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  523. range.cpu_addr, range.cpu_addr + range.size - 1,
  524. range.pci_addr);
  525. /* We support only one IO range */
  526. if (hose->pci_io_size) {
  527. pr_info(" \\--> Skipped (too many) !\n");
  528. continue;
  529. }
  530. /* On 32 bits, limit I/O space to 16MB */
  531. if (range.size > 0x01000000)
  532. range.size = 0x01000000;
  533. /* 32 bits needs to map IOs here */
  534. hose->io_base_virt = ioremap(range.cpu_addr,
  535. range.size);
  536. /* Expect trouble if pci_addr is not 0 */
  537. if (primary)
  538. isa_io_base =
  539. (unsigned long)hose->io_base_virt;
  540. /* pci_io_size and io_base_phys always represent IO
  541. * space starting at 0 so we factor in pci_addr
  542. */
  543. hose->pci_io_size = range.pci_addr + range.size;
  544. hose->io_base_phys = range.cpu_addr - range.pci_addr;
  545. /* Build resource */
  546. res = &hose->io_resource;
  547. range.cpu_addr = range.pci_addr;
  548. break;
  549. case IORESOURCE_MEM:
  550. pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  551. range.cpu_addr, range.cpu_addr + range.size - 1,
  552. range.pci_addr,
  553. (range.pci_space & 0x40000000) ?
  554. "Prefetch" : "");
  555. /* We support only 3 memory ranges */
  556. if (memno >= 3) {
  557. pr_info(" \\--> Skipped (too many) !\n");
  558. continue;
  559. }
  560. /* Handles ISA memory hole space here */
  561. if (range.pci_addr == 0) {
  562. isa_mb = range.cpu_addr;
  563. isa_hole = memno;
  564. if (primary || isa_mem_base == 0)
  565. isa_mem_base = range.cpu_addr;
  566. hose->isa_mem_phys = range.cpu_addr;
  567. hose->isa_mem_size = range.size;
  568. }
  569. /* We get the PCI/Mem offset from the first range or
  570. * the, current one if the offset came from an ISA
  571. * hole. If they don't match, bugger.
  572. */
  573. if (memno == 0 ||
  574. (isa_hole >= 0 && range.pci_addr != 0 &&
  575. hose->pci_mem_offset == isa_mb))
  576. hose->pci_mem_offset = range.cpu_addr -
  577. range.pci_addr;
  578. else if (range.pci_addr != 0 &&
  579. hose->pci_mem_offset != range.cpu_addr -
  580. range.pci_addr) {
  581. pr_info(" \\--> Skipped (offset mismatch) !\n");
  582. continue;
  583. }
  584. /* Build resource */
  585. res = &hose->mem_resources[memno++];
  586. break;
  587. }
  588. if (res != NULL)
  589. of_pci_range_to_resource(&range, dev, res);
  590. }
  591. /* If there's an ISA hole and the pci_mem_offset is -not- matching
  592. * the ISA hole offset, then we need to remove the ISA hole from
  593. * the resource list for that brige
  594. */
  595. if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
  596. unsigned int next = isa_hole + 1;
  597. pr_info(" Removing ISA hole at 0x%016llx\n", isa_mb);
  598. if (next < memno)
  599. memmove(&hose->mem_resources[isa_hole],
  600. &hose->mem_resources[next],
  601. sizeof(struct resource) * (memno - next));
  602. hose->mem_resources[--memno].flags = 0;
  603. }
  604. }
  605. /* Decide whether to display the domain number in /proc */
  606. int pci_proc_domain(struct pci_bus *bus)
  607. {
  608. return 0;
  609. }
  610. /* This header fixup will do the resource fixup for all devices as they are
  611. * probed, but not for bridge ranges
  612. */
  613. static void pcibios_fixup_resources(struct pci_dev *dev)
  614. {
  615. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  616. int i;
  617. if (!hose) {
  618. pr_err("No host bridge for PCI dev %s !\n",
  619. pci_name(dev));
  620. return;
  621. }
  622. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  623. struct resource *res = dev->resource + i;
  624. if (!res->flags)
  625. continue;
  626. if (res->start == 0) {
  627. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x]",
  628. pci_name(dev), i,
  629. (unsigned long long)res->start,
  630. (unsigned long long)res->end,
  631. (unsigned int)res->flags);
  632. pr_debug("is unassigned\n");
  633. res->end -= res->start;
  634. res->start = 0;
  635. res->flags |= IORESOURCE_UNSET;
  636. continue;
  637. }
  638. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x]\n",
  639. pci_name(dev), i,
  640. (unsigned long long)res->start,
  641. (unsigned long long)res->end,
  642. (unsigned int)res->flags);
  643. }
  644. }
  645. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  646. /* This function tries to figure out if a bridge resource has been initialized
  647. * by the firmware or not. It doesn't have to be absolutely bullet proof, but
  648. * things go more smoothly when it gets it right. It should covers cases such
  649. * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
  650. */
  651. static int pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
  652. struct resource *res)
  653. {
  654. struct pci_controller *hose = pci_bus_to_host(bus);
  655. struct pci_dev *dev = bus->self;
  656. resource_size_t offset;
  657. u16 command;
  658. int i;
  659. /* Job is a bit different between memory and IO */
  660. if (res->flags & IORESOURCE_MEM) {
  661. /* If the BAR is non-0 (res != pci_mem_offset) then it's
  662. * probably been initialized by somebody
  663. */
  664. if (res->start != hose->pci_mem_offset)
  665. return 0;
  666. /* The BAR is 0, let's check if memory decoding is enabled on
  667. * the bridge. If not, we consider it unassigned
  668. */
  669. pci_read_config_word(dev, PCI_COMMAND, &command);
  670. if ((command & PCI_COMMAND_MEMORY) == 0)
  671. return 1;
  672. /* Memory decoding is enabled and the BAR is 0. If any of
  673. * the bridge resources covers that starting address (0 then
  674. * it's good enough for us for memory
  675. */
  676. for (i = 0; i < 3; i++) {
  677. if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
  678. hose->mem_resources[i].start == hose->pci_mem_offset)
  679. return 0;
  680. }
  681. /* Well, it starts at 0 and we know it will collide so we may as
  682. * well consider it as unassigned. That covers the Apple case.
  683. */
  684. return 1;
  685. } else {
  686. /* If the BAR is non-0, then we consider it assigned */
  687. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  688. if (((res->start - offset) & 0xfffffffful) != 0)
  689. return 0;
  690. /* Here, we are a bit different than memory as typically IO
  691. * space starting at low addresses -is- valid. What we do
  692. * instead if that we consider as unassigned anything that
  693. * doesn't have IO enabled in the PCI command register,
  694. * and that's it.
  695. */
  696. pci_read_config_word(dev, PCI_COMMAND, &command);
  697. if (command & PCI_COMMAND_IO)
  698. return 0;
  699. /* It's starting at 0 and IO is disabled in the bridge, consider
  700. * it unassigned
  701. */
  702. return 1;
  703. }
  704. }
  705. /* Fixup resources of a PCI<->PCI bridge */
  706. static void pcibios_fixup_bridge(struct pci_bus *bus)
  707. {
  708. struct resource *res;
  709. int i;
  710. struct pci_dev *dev = bus->self;
  711. pci_bus_for_each_resource(bus, res, i) {
  712. if (!res)
  713. continue;
  714. if (!res->flags)
  715. continue;
  716. if (i >= 3 && bus->self->transparent)
  717. continue;
  718. pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
  719. pci_name(dev), i,
  720. (unsigned long long)res->start,
  721. (unsigned long long)res->end,
  722. (unsigned int)res->flags);
  723. /* Try to detect uninitialized P2P bridge resources,
  724. * and clear them out so they get re-assigned later
  725. */
  726. if (pcibios_uninitialized_bridge_resource(bus, res)) {
  727. res->flags = 0;
  728. pr_debug("PCI:%s (unassigned)\n",
  729. pci_name(dev));
  730. } else {
  731. pr_debug("PCI:%s %016llx-%016llx\n",
  732. pci_name(dev),
  733. (unsigned long long)res->start,
  734. (unsigned long long)res->end);
  735. }
  736. }
  737. }
  738. void pcibios_setup_bus_self(struct pci_bus *bus)
  739. {
  740. /* Fix up the bus resources for P2P bridges */
  741. if (bus->self != NULL)
  742. pcibios_fixup_bridge(bus);
  743. }
  744. void pcibios_setup_bus_devices(struct pci_bus *bus)
  745. {
  746. struct pci_dev *dev;
  747. pr_debug("PCI: Fixup bus devices %d (%s)\n",
  748. bus->number, bus->self ? pci_name(bus->self) : "PHB");
  749. list_for_each_entry(dev, &bus->devices, bus_list) {
  750. /* Setup OF node pointer in archdata */
  751. dev->dev.of_node = pci_device_to_OF_node(dev);
  752. /* Fixup NUMA node as it may not be setup yet by the generic
  753. * code and is needed by the DMA init
  754. */
  755. set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
  756. /* Read default IRQs and fixup if necessary */
  757. dev->irq = of_irq_parse_and_map_pci(dev, 0, 0);
  758. }
  759. }
  760. void pcibios_fixup_bus(struct pci_bus *bus)
  761. {
  762. /* When called from the generic PCI probe, read PCI<->PCI bridge
  763. * bases. This is -not- called when generating the PCI tree from
  764. * the OF device-tree.
  765. */
  766. if (bus->self != NULL)
  767. pci_read_bridge_bases(bus);
  768. /* Now fixup the bus bus */
  769. pcibios_setup_bus_self(bus);
  770. /* Now fixup devices on that bus */
  771. pcibios_setup_bus_devices(bus);
  772. }
  773. EXPORT_SYMBOL(pcibios_fixup_bus);
  774. static int skip_isa_ioresource_align(struct pci_dev *dev)
  775. {
  776. return 0;
  777. }
  778. /*
  779. * We need to avoid collisions with `mirrored' VGA ports
  780. * and other strange ISA hardware, so we always want the
  781. * addresses to be allocated in the 0x000-0x0ff region
  782. * modulo 0x400.
  783. *
  784. * Why? Because some silly external IO cards only decode
  785. * the low 10 bits of the IO address. The 0x00-0xff region
  786. * is reserved for motherboard devices that decode all 16
  787. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  788. * but we want to try to avoid allocating at 0x2900-0x2bff
  789. * which might have be mirrored at 0x0100-0x03ff..
  790. */
  791. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  792. resource_size_t size, resource_size_t align)
  793. {
  794. struct pci_dev *dev = data;
  795. resource_size_t start = res->start;
  796. if (res->flags & IORESOURCE_IO) {
  797. if (skip_isa_ioresource_align(dev))
  798. return start;
  799. if (start & 0x300)
  800. start = (start + 0x3ff) & ~0x3ff;
  801. }
  802. return start;
  803. }
  804. EXPORT_SYMBOL(pcibios_align_resource);
  805. /*
  806. * Reparent resource children of pr that conflict with res
  807. * under res, and make res replace those children.
  808. */
  809. static int __init reparent_resources(struct resource *parent,
  810. struct resource *res)
  811. {
  812. struct resource *p, **pp;
  813. struct resource **firstpp = NULL;
  814. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  815. if (p->end < res->start)
  816. continue;
  817. if (res->end < p->start)
  818. break;
  819. if (p->start < res->start || p->end > res->end)
  820. return -1; /* not completely contained */
  821. if (firstpp == NULL)
  822. firstpp = pp;
  823. }
  824. if (firstpp == NULL)
  825. return -1; /* didn't find any conflicting entries? */
  826. res->parent = parent;
  827. res->child = *firstpp;
  828. res->sibling = *pp;
  829. *firstpp = res;
  830. *pp = NULL;
  831. for (p = res->child; p != NULL; p = p->sibling) {
  832. p->parent = res;
  833. pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n",
  834. p->name,
  835. (unsigned long long)p->start,
  836. (unsigned long long)p->end, res->name);
  837. }
  838. return 0;
  839. }
  840. /*
  841. * Handle resources of PCI devices. If the world were perfect, we could
  842. * just allocate all the resource regions and do nothing more. It isn't.
  843. * On the other hand, we cannot just re-allocate all devices, as it would
  844. * require us to know lots of host bridge internals. So we attempt to
  845. * keep as much of the original configuration as possible, but tweak it
  846. * when it's found to be wrong.
  847. *
  848. * Known BIOS problems we have to work around:
  849. * - I/O or memory regions not configured
  850. * - regions configured, but not enabled in the command register
  851. * - bogus I/O addresses above 64K used
  852. * - expansion ROMs left enabled (this may sound harmless, but given
  853. * the fact the PCI specs explicitly allow address decoders to be
  854. * shared between expansion ROMs and other resource regions, it's
  855. * at least dangerous)
  856. *
  857. * Our solution:
  858. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  859. * This gives us fixed barriers on where we can allocate.
  860. * (2) Allocate resources for all enabled devices. If there is
  861. * a collision, just mark the resource as unallocated. Also
  862. * disable expansion ROMs during this step.
  863. * (3) Try to allocate resources for disabled devices. If the
  864. * resources were assigned correctly, everything goes well,
  865. * if they weren't, they won't disturb allocation of other
  866. * resources.
  867. * (4) Assign new addresses to resources which were either
  868. * not configured at all or misconfigured. If explicitly
  869. * requested by the user, configure expansion ROM address
  870. * as well.
  871. */
  872. static void pcibios_allocate_bus_resources(struct pci_bus *bus)
  873. {
  874. struct pci_bus *b;
  875. int i;
  876. struct resource *res, *pr;
  877. pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
  878. pci_domain_nr(bus), bus->number);
  879. pci_bus_for_each_resource(bus, res, i) {
  880. if (!res || !res->flags
  881. || res->start > res->end || res->parent)
  882. continue;
  883. if (bus->parent == NULL)
  884. pr = (res->flags & IORESOURCE_IO) ?
  885. &ioport_resource : &iomem_resource;
  886. else {
  887. /* Don't bother with non-root busses when
  888. * re-assigning all resources. We clear the
  889. * resource flags as if they were colliding
  890. * and as such ensure proper re-allocation
  891. * later.
  892. */
  893. pr = pci_find_parent_resource(bus->self, res);
  894. if (pr == res) {
  895. /* this happens when the generic PCI
  896. * code (wrongly) decides that this
  897. * bridge is transparent -- paulus
  898. */
  899. continue;
  900. }
  901. }
  902. pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx ",
  903. bus->self ? pci_name(bus->self) : "PHB",
  904. bus->number, i,
  905. (unsigned long long)res->start,
  906. (unsigned long long)res->end);
  907. pr_debug("[0x%x], parent %p (%s)\n",
  908. (unsigned int)res->flags,
  909. pr, (pr && pr->name) ? pr->name : "nil");
  910. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  911. if (request_resource(pr, res) == 0)
  912. continue;
  913. /*
  914. * Must be a conflict with an existing entry.
  915. * Move that entry (or entries) under the
  916. * bridge resource and try again.
  917. */
  918. if (reparent_resources(pr, res) == 0)
  919. continue;
  920. }
  921. pr_warn("PCI: Cannot allocate resource region ");
  922. pr_cont("%d of PCI bridge %d, will remap\n", i, bus->number);
  923. res->start = res->end = 0;
  924. res->flags = 0;
  925. }
  926. list_for_each_entry(b, &bus->children, node)
  927. pcibios_allocate_bus_resources(b);
  928. }
  929. static inline void alloc_resource(struct pci_dev *dev, int idx)
  930. {
  931. struct resource *pr, *r = &dev->resource[idx];
  932. pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
  933. pci_name(dev), idx,
  934. (unsigned long long)r->start,
  935. (unsigned long long)r->end,
  936. (unsigned int)r->flags);
  937. pr = pci_find_parent_resource(dev, r);
  938. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  939. request_resource(pr, r) < 0) {
  940. pr_warn("PCI: Cannot allocate resource region %d ", idx);
  941. pr_cont("of device %s, will remap\n", pci_name(dev));
  942. if (pr)
  943. pr_debug("PCI: parent is %p: %016llx-%016llx [%x]\n",
  944. pr,
  945. (unsigned long long)pr->start,
  946. (unsigned long long)pr->end,
  947. (unsigned int)pr->flags);
  948. /* We'll assign a new address later */
  949. r->flags |= IORESOURCE_UNSET;
  950. r->end -= r->start;
  951. r->start = 0;
  952. }
  953. }
  954. static void __init pcibios_allocate_resources(int pass)
  955. {
  956. struct pci_dev *dev = NULL;
  957. int idx, disabled;
  958. u16 command;
  959. struct resource *r;
  960. for_each_pci_dev(dev) {
  961. pci_read_config_word(dev, PCI_COMMAND, &command);
  962. for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
  963. r = &dev->resource[idx];
  964. if (r->parent) /* Already allocated */
  965. continue;
  966. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  967. continue; /* Not assigned at all */
  968. /* We only allocate ROMs on pass 1 just in case they
  969. * have been screwed up by firmware
  970. */
  971. if (idx == PCI_ROM_RESOURCE)
  972. disabled = 1;
  973. if (r->flags & IORESOURCE_IO)
  974. disabled = !(command & PCI_COMMAND_IO);
  975. else
  976. disabled = !(command & PCI_COMMAND_MEMORY);
  977. if (pass == disabled)
  978. alloc_resource(dev, idx);
  979. }
  980. if (pass)
  981. continue;
  982. r = &dev->resource[PCI_ROM_RESOURCE];
  983. if (r->flags) {
  984. /* Turn the ROM off, leave the resource region,
  985. * but keep it unregistered.
  986. */
  987. u32 reg;
  988. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  989. if (reg & PCI_ROM_ADDRESS_ENABLE) {
  990. pr_debug("PCI: Switching off ROM of %s\n",
  991. pci_name(dev));
  992. r->flags &= ~IORESOURCE_ROM_ENABLE;
  993. pci_write_config_dword(dev, dev->rom_base_reg,
  994. reg & ~PCI_ROM_ADDRESS_ENABLE);
  995. }
  996. }
  997. }
  998. }
  999. static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
  1000. {
  1001. struct pci_controller *hose = pci_bus_to_host(bus);
  1002. resource_size_t offset;
  1003. struct resource *res, *pres;
  1004. int i;
  1005. pr_debug("Reserving legacy ranges for domain %04x\n",
  1006. pci_domain_nr(bus));
  1007. /* Check for IO */
  1008. if (!(hose->io_resource.flags & IORESOURCE_IO))
  1009. goto no_io;
  1010. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  1011. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1012. BUG_ON(res == NULL);
  1013. res->name = "Legacy IO";
  1014. res->flags = IORESOURCE_IO;
  1015. res->start = offset;
  1016. res->end = (offset + 0xfff) & 0xfffffffful;
  1017. pr_debug("Candidate legacy IO: %pR\n", res);
  1018. if (request_resource(&hose->io_resource, res)) {
  1019. pr_debug("PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
  1020. pci_domain_nr(bus), bus->number, res);
  1021. kfree(res);
  1022. }
  1023. no_io:
  1024. /* Check for memory */
  1025. offset = hose->pci_mem_offset;
  1026. pr_debug("hose mem offset: %016llx\n", (unsigned long long)offset);
  1027. for (i = 0; i < 3; i++) {
  1028. pres = &hose->mem_resources[i];
  1029. if (!(pres->flags & IORESOURCE_MEM))
  1030. continue;
  1031. pr_debug("hose mem res: %pR\n", pres);
  1032. if ((pres->start - offset) <= 0xa0000 &&
  1033. (pres->end - offset) >= 0xbffff)
  1034. break;
  1035. }
  1036. if (i >= 3)
  1037. return;
  1038. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  1039. BUG_ON(res == NULL);
  1040. res->name = "Legacy VGA memory";
  1041. res->flags = IORESOURCE_MEM;
  1042. res->start = 0xa0000 + offset;
  1043. res->end = 0xbffff + offset;
  1044. pr_debug("Candidate VGA memory: %pR\n", res);
  1045. if (request_resource(pres, res)) {
  1046. pr_debug("PCI %04x:%02x Cannot reserve VGA memory %pR\n",
  1047. pci_domain_nr(bus), bus->number, res);
  1048. kfree(res);
  1049. }
  1050. }
  1051. void __init pcibios_resource_survey(void)
  1052. {
  1053. struct pci_bus *b;
  1054. /* Allocate and assign resources. If we re-assign everything, then
  1055. * we skip the allocate phase
  1056. */
  1057. list_for_each_entry(b, &pci_root_buses, node)
  1058. pcibios_allocate_bus_resources(b);
  1059. pcibios_allocate_resources(0);
  1060. pcibios_allocate_resources(1);
  1061. /* Before we start assigning unassigned resource, we try to reserve
  1062. * the low IO area and the VGA memory area if they intersect the
  1063. * bus available resources to avoid allocating things on top of them
  1064. */
  1065. list_for_each_entry(b, &pci_root_buses, node)
  1066. pcibios_reserve_legacy_regions(b);
  1067. /* Now proceed to assigning things that were left unassigned */
  1068. pr_debug("PCI: Assigning unassigned resources...\n");
  1069. pci_assign_unassigned_resources();
  1070. }
  1071. /* This is used by the PCI hotplug driver to allocate resource
  1072. * of newly plugged busses. We can try to consolidate with the
  1073. * rest of the code later, for now, keep it as-is as our main
  1074. * resource allocation function doesn't deal with sub-trees yet.
  1075. */
  1076. void pcibios_claim_one_bus(struct pci_bus *bus)
  1077. {
  1078. struct pci_dev *dev;
  1079. struct pci_bus *child_bus;
  1080. list_for_each_entry(dev, &bus->devices, bus_list) {
  1081. int i;
  1082. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  1083. struct resource *r = &dev->resource[i];
  1084. if (r->parent || !r->start || !r->flags)
  1085. continue;
  1086. pr_debug("PCI: Claiming %s: ", pci_name(dev));
  1087. pr_debug("Resource %d: %016llx..%016llx [%x]\n",
  1088. i, (unsigned long long)r->start,
  1089. (unsigned long long)r->end,
  1090. (unsigned int)r->flags);
  1091. pci_claim_resource(dev, i);
  1092. }
  1093. }
  1094. list_for_each_entry(child_bus, &bus->children, node)
  1095. pcibios_claim_one_bus(child_bus);
  1096. }
  1097. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  1098. /* pcibios_finish_adding_to_bus
  1099. *
  1100. * This is to be called by the hotplug code after devices have been
  1101. * added to a bus, this include calling it for a PHB that is just
  1102. * being added
  1103. */
  1104. void pcibios_finish_adding_to_bus(struct pci_bus *bus)
  1105. {
  1106. pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
  1107. pci_domain_nr(bus), bus->number);
  1108. /* Allocate bus and devices resources */
  1109. pcibios_allocate_bus_resources(bus);
  1110. pcibios_claim_one_bus(bus);
  1111. /* Add new devices to global lists. Register in proc, sysfs. */
  1112. pci_bus_add_devices(bus);
  1113. /* Fixup EEH */
  1114. /* eeh_add_device_tree_late(bus); */
  1115. }
  1116. EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
  1117. static void pcibios_setup_phb_resources(struct pci_controller *hose,
  1118. struct list_head *resources)
  1119. {
  1120. unsigned long io_offset;
  1121. struct resource *res;
  1122. int i;
  1123. /* Hookup PHB IO resource */
  1124. res = &hose->io_resource;
  1125. /* Fixup IO space offset */
  1126. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1127. res->start = (res->start + io_offset) & 0xffffffffu;
  1128. res->end = (res->end + io_offset) & 0xffffffffu;
  1129. if (!res->flags) {
  1130. pr_warn("PCI: I/O resource not set for host ");
  1131. pr_cont("bridge %s (domain %d)\n",
  1132. hose->dn->full_name, hose->global_number);
  1133. /* Workaround for lack of IO resource only on 32-bit */
  1134. res->start = (unsigned long)hose->io_base_virt - isa_io_base;
  1135. res->end = res->start + IO_SPACE_LIMIT;
  1136. res->flags = IORESOURCE_IO;
  1137. }
  1138. pci_add_resource_offset(resources, res,
  1139. (__force resource_size_t)(hose->io_base_virt - _IO_BASE));
  1140. pr_debug("PCI: PHB IO resource = %016llx-%016llx [%lx]\n",
  1141. (unsigned long long)res->start,
  1142. (unsigned long long)res->end,
  1143. (unsigned long)res->flags);
  1144. /* Hookup PHB Memory resources */
  1145. for (i = 0; i < 3; ++i) {
  1146. res = &hose->mem_resources[i];
  1147. if (!res->flags) {
  1148. if (i > 0)
  1149. continue;
  1150. pr_err("PCI: Memory resource 0 not set for ");
  1151. pr_cont("host bridge %s (domain %d)\n",
  1152. hose->dn->full_name, hose->global_number);
  1153. /* Workaround for lack of MEM resource only on 32-bit */
  1154. res->start = hose->pci_mem_offset;
  1155. res->end = (resource_size_t)-1LL;
  1156. res->flags = IORESOURCE_MEM;
  1157. }
  1158. pci_add_resource_offset(resources, res, hose->pci_mem_offset);
  1159. pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n",
  1160. i, (unsigned long long)res->start,
  1161. (unsigned long long)res->end,
  1162. (unsigned long)res->flags);
  1163. }
  1164. pr_debug("PCI: PHB MEM offset = %016llx\n",
  1165. (unsigned long long)hose->pci_mem_offset);
  1166. pr_debug("PCI: PHB IO offset = %08lx\n",
  1167. (unsigned long)hose->io_base_virt - _IO_BASE);
  1168. }
  1169. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  1170. {
  1171. struct pci_controller *hose = bus->sysdata;
  1172. return of_node_get(hose->dn);
  1173. }
  1174. static void pcibios_scan_phb(struct pci_controller *hose)
  1175. {
  1176. LIST_HEAD(resources);
  1177. struct pci_bus *bus;
  1178. struct device_node *node = hose->dn;
  1179. pr_debug("PCI: Scanning PHB %s\n", of_node_full_name(node));
  1180. pcibios_setup_phb_resources(hose, &resources);
  1181. bus = pci_scan_root_bus(hose->parent, hose->first_busno,
  1182. hose->ops, hose, &resources);
  1183. if (bus == NULL) {
  1184. pr_err("Failed to create bus for PCI domain %04x\n",
  1185. hose->global_number);
  1186. pci_free_resource_list(&resources);
  1187. return;
  1188. }
  1189. bus->busn_res.start = hose->first_busno;
  1190. hose->bus = bus;
  1191. hose->last_busno = bus->busn_res.end;
  1192. }
  1193. static int __init pcibios_init(void)
  1194. {
  1195. struct pci_controller *hose, *tmp;
  1196. int next_busno = 0;
  1197. pr_info("PCI: Probing PCI hardware\n");
  1198. /* Scan all of the recorded PCI controllers. */
  1199. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1200. hose->last_busno = 0xff;
  1201. pcibios_scan_phb(hose);
  1202. if (next_busno <= hose->last_busno)
  1203. next_busno = hose->last_busno + 1;
  1204. }
  1205. pci_bus_count = next_busno;
  1206. /* Call common code to handle resource allocation */
  1207. pcibios_resource_survey();
  1208. return 0;
  1209. }
  1210. subsys_initcall(pcibios_init);
  1211. static struct pci_controller *pci_bus_to_hose(int bus)
  1212. {
  1213. struct pci_controller *hose, *tmp;
  1214. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1215. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1216. return hose;
  1217. return NULL;
  1218. }
  1219. /* Provide information on locations of various I/O regions in physical
  1220. * memory. Do this on a per-card basis so that we choose the right
  1221. * root bridge.
  1222. * Note that the returned IO or memory base is a physical address
  1223. */
  1224. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1225. {
  1226. struct pci_controller *hose;
  1227. long result = -EOPNOTSUPP;
  1228. hose = pci_bus_to_hose(bus);
  1229. if (!hose)
  1230. return -ENODEV;
  1231. switch (which) {
  1232. case IOBASE_BRIDGE_NUMBER:
  1233. return (long)hose->first_busno;
  1234. case IOBASE_MEMORY:
  1235. return (long)hose->pci_mem_offset;
  1236. case IOBASE_IO:
  1237. return (long)hose->io_base_phys;
  1238. case IOBASE_ISA_IO:
  1239. return (long)isa_io_base;
  1240. case IOBASE_ISA_MEM:
  1241. return (long)isa_mem_base;
  1242. }
  1243. return result;
  1244. }
  1245. /*
  1246. * Null PCI config access functions, for the case when we can't
  1247. * find a hose.
  1248. */
  1249. #define NULL_PCI_OP(rw, size, type) \
  1250. static int \
  1251. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1252. { \
  1253. return PCIBIOS_DEVICE_NOT_FOUND; \
  1254. }
  1255. static int
  1256. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1257. int len, u32 *val)
  1258. {
  1259. return PCIBIOS_DEVICE_NOT_FOUND;
  1260. }
  1261. static int
  1262. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1263. int len, u32 val)
  1264. {
  1265. return PCIBIOS_DEVICE_NOT_FOUND;
  1266. }
  1267. static struct pci_ops null_pci_ops = {
  1268. .read = null_read_config,
  1269. .write = null_write_config,
  1270. };
  1271. /*
  1272. * These functions are used early on before PCI scanning is done
  1273. * and all of the pci_dev and pci_bus structures have been created.
  1274. */
  1275. static struct pci_bus *
  1276. fake_pci_bus(struct pci_controller *hose, int busnr)
  1277. {
  1278. static struct pci_bus bus;
  1279. if (!hose)
  1280. pr_err("Can't find hose for PCI bus %d!\n", busnr);
  1281. bus.number = busnr;
  1282. bus.sysdata = hose;
  1283. bus.ops = hose ? hose->ops : &null_pci_ops;
  1284. return &bus;
  1285. }
  1286. #define EARLY_PCI_OP(rw, size, type) \
  1287. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1288. int devfn, int offset, type value) \
  1289. { \
  1290. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1291. devfn, offset, value); \
  1292. }
  1293. EARLY_PCI_OP(read, byte, u8 *)
  1294. EARLY_PCI_OP(read, word, u16 *)
  1295. EARLY_PCI_OP(read, dword, u32 *)
  1296. EARLY_PCI_OP(write, byte, u8)
  1297. EARLY_PCI_OP(write, word, u16)
  1298. EARLY_PCI_OP(write, dword, u32)
  1299. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  1300. int cap)
  1301. {
  1302. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  1303. }