pci-common.c 49 KB

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