pci-common.c 46 KB

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