pci-common.c 46 KB

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