pci-common.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. #undef DEBUG
  19. #include <linux/kernel.h>
  20. #include <linux/pci.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/mm.h>
  25. #include <linux/list.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/irq.h>
  28. #include <linux/vmalloc.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. #include <asm/machdep.h>
  35. #include <asm/ppc-pci.h>
  36. #include <asm/firmware.h>
  37. #ifdef DEBUG
  38. #include <asm/udbg.h>
  39. #define DBG(fmt...) printk(fmt)
  40. #else
  41. #define DBG(fmt...)
  42. #endif
  43. static DEFINE_SPINLOCK(hose_spinlock);
  44. /* XXX kill that some day ... */
  45. static int global_phb_number; /* Global phb counter */
  46. /* ISA Memory physical address */
  47. resource_size_t isa_mem_base;
  48. /* Default PCI flags is 0 */
  49. unsigned int ppc_pci_flags;
  50. struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
  51. {
  52. struct pci_controller *phb;
  53. phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
  54. if (phb == NULL)
  55. return NULL;
  56. spin_lock(&hose_spinlock);
  57. phb->global_number = global_phb_number++;
  58. list_add_tail(&phb->list_node, &hose_list);
  59. spin_unlock(&hose_spinlock);
  60. phb->dn = dev;
  61. phb->is_dynamic = mem_init_done;
  62. #ifdef CONFIG_PPC64
  63. if (dev) {
  64. int nid = of_node_to_nid(dev);
  65. if (nid < 0 || !node_online(nid))
  66. nid = -1;
  67. PHB_SET_NODE(phb, nid);
  68. }
  69. #endif
  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. int pcibios_vaddr_is_ioport(void __iomem *address)
  81. {
  82. int ret = 0;
  83. struct pci_controller *hose;
  84. unsigned long size;
  85. spin_lock(&hose_spinlock);
  86. list_for_each_entry(hose, &hose_list, list_node) {
  87. #ifdef CONFIG_PPC64
  88. size = hose->pci_io_size;
  89. #else
  90. size = hose->io_resource.end - hose->io_resource.start + 1;
  91. #endif
  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. /*
  102. * Return the domain number for this bus.
  103. */
  104. int pci_domain_nr(struct pci_bus *bus)
  105. {
  106. struct pci_controller *hose = pci_bus_to_host(bus);
  107. return hose->global_number;
  108. }
  109. EXPORT_SYMBOL(pci_domain_nr);
  110. #ifdef CONFIG_PPC_OF
  111. /* This routine is meant to be used early during boot, when the
  112. * PCI bus numbers have not yet been assigned, and you need to
  113. * issue PCI config cycles to an OF device.
  114. * It could also be used to "fix" RTAS config cycles if you want
  115. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  116. * config cycles.
  117. */
  118. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  119. {
  120. if (!have_of)
  121. return NULL;
  122. while(node) {
  123. struct pci_controller *hose, *tmp;
  124. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  125. if (hose->dn == node)
  126. return hose;
  127. node = node->parent;
  128. }
  129. return NULL;
  130. }
  131. static ssize_t pci_show_devspec(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. struct pci_dev *pdev;
  135. struct device_node *np;
  136. pdev = to_pci_dev (dev);
  137. np = pci_device_to_OF_node(pdev);
  138. if (np == NULL || np->full_name == NULL)
  139. return 0;
  140. return sprintf(buf, "%s", np->full_name);
  141. }
  142. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  143. #endif /* CONFIG_PPC_OF */
  144. /* Add sysfs properties */
  145. int pcibios_add_platform_entries(struct pci_dev *pdev)
  146. {
  147. #ifdef CONFIG_PPC_OF
  148. return device_create_file(&pdev->dev, &dev_attr_devspec);
  149. #else
  150. return 0;
  151. #endif /* CONFIG_PPC_OF */
  152. }
  153. char __devinit *pcibios_setup(char *str)
  154. {
  155. return str;
  156. }
  157. /*
  158. * Reads the interrupt pin to determine if interrupt is use by card.
  159. * If the interrupt is used, then gets the interrupt line from the
  160. * openfirmware and sets it in the pci_dev and pci_config line.
  161. */
  162. int pci_read_irq_line(struct pci_dev *pci_dev)
  163. {
  164. struct of_irq oirq;
  165. unsigned int virq;
  166. /* The current device-tree that iSeries generates from the HV
  167. * PCI informations doesn't contain proper interrupt routing,
  168. * and all the fallback would do is print out crap, so we
  169. * don't attempt to resolve the interrupts here at all, some
  170. * iSeries specific fixup does it.
  171. *
  172. * In the long run, we will hopefully fix the generated device-tree
  173. * instead.
  174. */
  175. #ifdef CONFIG_PPC_ISERIES
  176. if (firmware_has_feature(FW_FEATURE_ISERIES))
  177. return -1;
  178. #endif
  179. DBG("Try to map irq for %s...\n", pci_name(pci_dev));
  180. #ifdef DEBUG
  181. memset(&oirq, 0xff, sizeof(oirq));
  182. #endif
  183. /* Try to get a mapping from the device-tree */
  184. if (of_irq_map_pci(pci_dev, &oirq)) {
  185. u8 line, pin;
  186. /* If that fails, lets fallback to what is in the config
  187. * space and map that through the default controller. We
  188. * also set the type to level low since that's what PCI
  189. * interrupts are. If your platform does differently, then
  190. * either provide a proper interrupt tree or don't use this
  191. * function.
  192. */
  193. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  194. return -1;
  195. if (pin == 0)
  196. return -1;
  197. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  198. line == 0xff) {
  199. return -1;
  200. }
  201. DBG(" -> no map ! Using irq line %d from PCI config\n", line);
  202. virq = irq_create_mapping(NULL, line);
  203. if (virq != NO_IRQ)
  204. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  205. } else {
  206. DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  207. oirq.size, oirq.specifier[0], oirq.specifier[1],
  208. oirq.controller->full_name);
  209. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  210. oirq.size);
  211. }
  212. if(virq == NO_IRQ) {
  213. DBG(" -> failed to map !\n");
  214. return -1;
  215. }
  216. DBG(" -> mapped to linux irq %d\n", virq);
  217. pci_dev->irq = virq;
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(pci_read_irq_line);
  221. /*
  222. * Platform support for /proc/bus/pci/X/Y mmap()s,
  223. * modelled on the sparc64 implementation by Dave Miller.
  224. * -- paulus.
  225. */
  226. /*
  227. * Adjust vm_pgoff of VMA such that it is the physical page offset
  228. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  229. *
  230. * Basically, the user finds the base address for his device which he wishes
  231. * to mmap. They read the 32-bit value from the config space base register,
  232. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  233. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  234. *
  235. * Returns negative error code on failure, zero on success.
  236. */
  237. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  238. resource_size_t *offset,
  239. enum pci_mmap_state mmap_state)
  240. {
  241. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  242. unsigned long io_offset = 0;
  243. int i, res_bit;
  244. if (hose == 0)
  245. return NULL; /* should never happen */
  246. /* If memory, add on the PCI bridge address offset */
  247. if (mmap_state == pci_mmap_mem) {
  248. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  249. *offset += hose->pci_mem_offset;
  250. #endif
  251. res_bit = IORESOURCE_MEM;
  252. } else {
  253. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  254. *offset += io_offset;
  255. res_bit = IORESOURCE_IO;
  256. }
  257. /*
  258. * Check that the offset requested corresponds to one of the
  259. * resources of the device.
  260. */
  261. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  262. struct resource *rp = &dev->resource[i];
  263. int flags = rp->flags;
  264. /* treat ROM as memory (should be already) */
  265. if (i == PCI_ROM_RESOURCE)
  266. flags |= IORESOURCE_MEM;
  267. /* Active and same type? */
  268. if ((flags & res_bit) == 0)
  269. continue;
  270. /* In the range of this resource? */
  271. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  272. continue;
  273. /* found it! construct the final physical address */
  274. if (mmap_state == pci_mmap_io)
  275. *offset += hose->io_base_phys - io_offset;
  276. return rp;
  277. }
  278. return NULL;
  279. }
  280. /*
  281. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  282. * device mapping.
  283. */
  284. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  285. pgprot_t protection,
  286. enum pci_mmap_state mmap_state,
  287. int write_combine)
  288. {
  289. unsigned long prot = pgprot_val(protection);
  290. /* Write combine is always 0 on non-memory space mappings. On
  291. * memory space, if the user didn't pass 1, we check for a
  292. * "prefetchable" resource. This is a bit hackish, but we use
  293. * this to workaround the inability of /sysfs to provide a write
  294. * combine bit
  295. */
  296. if (mmap_state != pci_mmap_mem)
  297. write_combine = 0;
  298. else if (write_combine == 0) {
  299. if (rp->flags & IORESOURCE_PREFETCH)
  300. write_combine = 1;
  301. }
  302. /* XXX would be nice to have a way to ask for write-through */
  303. prot |= _PAGE_NO_CACHE;
  304. if (write_combine)
  305. prot &= ~_PAGE_GUARDED;
  306. else
  307. prot |= _PAGE_GUARDED;
  308. return __pgprot(prot);
  309. }
  310. /*
  311. * This one is used by /dev/mem and fbdev who have no clue about the
  312. * PCI device, it tries to find the PCI device first and calls the
  313. * above routine
  314. */
  315. pgprot_t pci_phys_mem_access_prot(struct file *file,
  316. unsigned long pfn,
  317. unsigned long size,
  318. pgprot_t protection)
  319. {
  320. struct pci_dev *pdev = NULL;
  321. struct resource *found = NULL;
  322. unsigned long prot = pgprot_val(protection);
  323. unsigned long offset = pfn << PAGE_SHIFT;
  324. int i;
  325. if (page_is_ram(pfn))
  326. return __pgprot(prot);
  327. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  328. for_each_pci_dev(pdev) {
  329. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  330. struct resource *rp = &pdev->resource[i];
  331. int flags = rp->flags;
  332. /* Active and same type? */
  333. if ((flags & IORESOURCE_MEM) == 0)
  334. continue;
  335. /* In the range of this resource? */
  336. if (offset < (rp->start & PAGE_MASK) ||
  337. offset > rp->end)
  338. continue;
  339. found = rp;
  340. break;
  341. }
  342. if (found)
  343. break;
  344. }
  345. if (found) {
  346. if (found->flags & IORESOURCE_PREFETCH)
  347. prot &= ~_PAGE_GUARDED;
  348. pci_dev_put(pdev);
  349. }
  350. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  351. return __pgprot(prot);
  352. }
  353. /*
  354. * Perform the actual remap of the pages for a PCI device mapping, as
  355. * appropriate for this architecture. The region in the process to map
  356. * is described by vm_start and vm_end members of VMA, the base physical
  357. * address is found in vm_pgoff.
  358. * The pci device structure is provided so that architectures may make mapping
  359. * decisions on a per-device or per-bus basis.
  360. *
  361. * Returns a negative error code on failure, zero on success.
  362. */
  363. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  364. enum pci_mmap_state mmap_state, int write_combine)
  365. {
  366. resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
  367. struct resource *rp;
  368. int ret;
  369. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  370. if (rp == NULL)
  371. return -EINVAL;
  372. vma->vm_pgoff = offset >> PAGE_SHIFT;
  373. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  374. vma->vm_page_prot,
  375. mmap_state, write_combine);
  376. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  377. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  378. return ret;
  379. }
  380. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  381. const struct resource *rsrc,
  382. resource_size_t *start, resource_size_t *end)
  383. {
  384. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  385. resource_size_t offset = 0;
  386. if (hose == NULL)
  387. return;
  388. if (rsrc->flags & IORESOURCE_IO)
  389. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  390. /* We pass a fully fixed up address to userland for MMIO instead of
  391. * a BAR value because X is lame and expects to be able to use that
  392. * to pass to /dev/mem !
  393. *
  394. * That means that we'll have potentially 64 bits values where some
  395. * userland apps only expect 32 (like X itself since it thinks only
  396. * Sparc has 64 bits MMIO) but if we don't do that, we break it on
  397. * 32 bits CHRPs :-(
  398. *
  399. * Hopefully, the sysfs insterface is immune to that gunk. Once X
  400. * has been fixed (and the fix spread enough), we can re-enable the
  401. * 2 lines below and pass down a BAR value to userland. In that case
  402. * we'll also have to re-enable the matching code in
  403. * __pci_mmap_make_offset().
  404. *
  405. * BenH.
  406. */
  407. #if 0
  408. else if (rsrc->flags & IORESOURCE_MEM)
  409. offset = hose->pci_mem_offset;
  410. #endif
  411. *start = rsrc->start - offset;
  412. *end = rsrc->end - offset;
  413. }
  414. /**
  415. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  416. * @hose: newly allocated pci_controller to be setup
  417. * @dev: device node of the host bridge
  418. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  419. *
  420. * This function will parse the "ranges" property of a PCI host bridge device
  421. * node and setup the resource mapping of a pci controller based on its
  422. * content.
  423. *
  424. * Life would be boring if it wasn't for a few issues that we have to deal
  425. * with here:
  426. *
  427. * - We can only cope with one IO space range and up to 3 Memory space
  428. * ranges. However, some machines (thanks Apple !) tend to split their
  429. * space into lots of small contiguous ranges. So we have to coalesce.
  430. *
  431. * - We can only cope with all memory ranges having the same offset
  432. * between CPU addresses and PCI addresses. Unfortunately, some bridges
  433. * are setup for a large 1:1 mapping along with a small "window" which
  434. * maps PCI address 0 to some arbitrary high address of the CPU space in
  435. * order to give access to the ISA memory hole.
  436. * The way out of here that I've chosen for now is to always set the
  437. * offset based on the first resource found, then override it if we
  438. * have a different offset and the previous was set by an ISA hole.
  439. *
  440. * - Some busses have IO space not starting at 0, which causes trouble with
  441. * the way we do our IO resource renumbering. The code somewhat deals with
  442. * it for 64 bits but I would expect problems on 32 bits.
  443. *
  444. * - Some 32 bits platforms such as 4xx can have physical space larger than
  445. * 32 bits so we need to use 64 bits values for the parsing
  446. */
  447. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  448. struct device_node *dev,
  449. int primary)
  450. {
  451. const u32 *ranges;
  452. int rlen;
  453. int pna = of_n_addr_cells(dev);
  454. int np = pna + 5;
  455. int memno = 0, isa_hole = -1;
  456. u32 pci_space;
  457. unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
  458. unsigned long long isa_mb = 0;
  459. struct resource *res;
  460. printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
  461. dev->full_name, primary ? "(primary)" : "");
  462. /* Get ranges property */
  463. ranges = of_get_property(dev, "ranges", &rlen);
  464. if (ranges == NULL)
  465. return;
  466. /* Parse it */
  467. while ((rlen -= np * 4) >= 0) {
  468. /* Read next ranges element */
  469. pci_space = ranges[0];
  470. pci_addr = of_read_number(ranges + 1, 2);
  471. cpu_addr = of_translate_address(dev, ranges + 3);
  472. size = of_read_number(ranges + pna + 3, 2);
  473. ranges += np;
  474. if (cpu_addr == OF_BAD_ADDR || size == 0)
  475. continue;
  476. /* Now consume following elements while they are contiguous */
  477. for (; rlen >= np * sizeof(u32);
  478. ranges += np, rlen -= np * 4) {
  479. if (ranges[0] != pci_space)
  480. break;
  481. pci_next = of_read_number(ranges + 1, 2);
  482. cpu_next = of_translate_address(dev, ranges + 3);
  483. if (pci_next != pci_addr + size ||
  484. cpu_next != cpu_addr + size)
  485. break;
  486. size += of_read_number(ranges + pna + 3, 2);
  487. }
  488. /* Act based on address space type */
  489. res = NULL;
  490. switch ((pci_space >> 24) & 0x3) {
  491. case 1: /* PCI IO space */
  492. printk(KERN_INFO
  493. " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  494. cpu_addr, cpu_addr + size - 1, pci_addr);
  495. /* We support only one IO range */
  496. if (hose->pci_io_size) {
  497. printk(KERN_INFO
  498. " \\--> Skipped (too many) !\n");
  499. continue;
  500. }
  501. #ifdef CONFIG_PPC32
  502. /* On 32 bits, limit I/O space to 16MB */
  503. if (size > 0x01000000)
  504. size = 0x01000000;
  505. /* 32 bits needs to map IOs here */
  506. hose->io_base_virt = ioremap(cpu_addr, size);
  507. /* Expect trouble if pci_addr is not 0 */
  508. if (primary)
  509. isa_io_base =
  510. (unsigned long)hose->io_base_virt;
  511. #endif /* CONFIG_PPC32 */
  512. /* pci_io_size and io_base_phys always represent IO
  513. * space starting at 0 so we factor in pci_addr
  514. */
  515. hose->pci_io_size = pci_addr + size;
  516. hose->io_base_phys = cpu_addr - pci_addr;
  517. /* Build resource */
  518. res = &hose->io_resource;
  519. res->flags = IORESOURCE_IO;
  520. res->start = pci_addr;
  521. break;
  522. case 2: /* PCI Memory space */
  523. printk(KERN_INFO
  524. " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  525. cpu_addr, cpu_addr + size - 1, pci_addr,
  526. (pci_space & 0x40000000) ? "Prefetch" : "");
  527. /* We support only 3 memory ranges */
  528. if (memno >= 3) {
  529. printk(KERN_INFO
  530. " \\--> Skipped (too many) !\n");
  531. continue;
  532. }
  533. /* Handles ISA memory hole space here */
  534. if (pci_addr == 0) {
  535. isa_mb = cpu_addr;
  536. isa_hole = memno;
  537. if (primary || isa_mem_base == 0)
  538. isa_mem_base = cpu_addr;
  539. }
  540. /* We get the PCI/Mem offset from the first range or
  541. * the, current one if the offset came from an ISA
  542. * hole. If they don't match, bugger.
  543. */
  544. if (memno == 0 ||
  545. (isa_hole >= 0 && pci_addr != 0 &&
  546. hose->pci_mem_offset == isa_mb))
  547. hose->pci_mem_offset = cpu_addr - pci_addr;
  548. else if (pci_addr != 0 &&
  549. hose->pci_mem_offset != cpu_addr - pci_addr) {
  550. printk(KERN_INFO
  551. " \\--> Skipped (offset mismatch) !\n");
  552. continue;
  553. }
  554. /* Build resource */
  555. res = &hose->mem_resources[memno++];
  556. res->flags = IORESOURCE_MEM;
  557. if (pci_space & 0x40000000)
  558. res->flags |= IORESOURCE_PREFETCH;
  559. res->start = cpu_addr;
  560. break;
  561. }
  562. if (res != NULL) {
  563. res->name = dev->full_name;
  564. res->end = res->start + size - 1;
  565. res->parent = NULL;
  566. res->sibling = NULL;
  567. res->child = NULL;
  568. }
  569. }
  570. /* Out of paranoia, let's put the ISA hole last if any */
  571. if (isa_hole >= 0 && memno > 0 && isa_hole != (memno-1)) {
  572. struct resource tmp = hose->mem_resources[isa_hole];
  573. hose->mem_resources[isa_hole] = hose->mem_resources[memno-1];
  574. hose->mem_resources[memno-1] = tmp;
  575. }
  576. }
  577. /* Decide whether to display the domain number in /proc */
  578. int pci_proc_domain(struct pci_bus *bus)
  579. {
  580. struct pci_controller *hose = pci_bus_to_host(bus);
  581. #ifdef CONFIG_PPC64
  582. return hose->buid != 0;
  583. #else
  584. if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS))
  585. return 0;
  586. if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0)
  587. return hose->global_number != 0;
  588. return 1;
  589. #endif
  590. }
  591. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  592. struct resource *res)
  593. {
  594. resource_size_t offset = 0, mask = (resource_size_t)-1;
  595. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  596. if (!hose)
  597. return;
  598. if (res->flags & IORESOURCE_IO) {
  599. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  600. mask = 0xffffffffu;
  601. } else if (res->flags & IORESOURCE_MEM)
  602. offset = hose->pci_mem_offset;
  603. region->start = (res->start - offset) & mask;
  604. region->end = (res->end - offset) & mask;
  605. }
  606. EXPORT_SYMBOL(pcibios_resource_to_bus);
  607. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  608. struct pci_bus_region *region)
  609. {
  610. resource_size_t offset = 0, mask = (resource_size_t)-1;
  611. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  612. if (!hose)
  613. return;
  614. if (res->flags & IORESOURCE_IO) {
  615. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  616. mask = 0xffffffffu;
  617. } else if (res->flags & IORESOURCE_MEM)
  618. offset = hose->pci_mem_offset;
  619. res->start = (region->start + offset) & mask;
  620. res->end = (region->end + offset) & mask;
  621. }
  622. EXPORT_SYMBOL(pcibios_bus_to_resource);
  623. /* Fixup a bus resource into a linux resource */
  624. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  625. {
  626. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  627. resource_size_t offset = 0, mask = (resource_size_t)-1;
  628. if (res->flags & IORESOURCE_IO) {
  629. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  630. mask = 0xffffffffu;
  631. } else if (res->flags & IORESOURCE_MEM)
  632. offset = hose->pci_mem_offset;
  633. res->start = (res->start + offset) & mask;
  634. res->end = (res->end + offset) & mask;
  635. pr_debug("PCI:%s %016llx-%016llx\n",
  636. pci_name(dev),
  637. (unsigned long long)res->start,
  638. (unsigned long long)res->end);
  639. }
  640. /* This header fixup will do the resource fixup for all devices as they are
  641. * probed, but not for bridge ranges
  642. */
  643. static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
  644. {
  645. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  646. int i;
  647. if (!hose) {
  648. printk(KERN_ERR "No host bridge for PCI dev %s !\n",
  649. pci_name(dev));
  650. return;
  651. }
  652. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  653. struct resource *res = dev->resource + i;
  654. if (!res->flags)
  655. continue;
  656. if (res->end == 0xffffffff) {
  657. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
  658. pci_name(dev), i,
  659. (unsigned long long)res->start,
  660. (unsigned long long)res->end,
  661. (unsigned int)res->flags);
  662. res->end -= res->start;
  663. res->start = 0;
  664. res->flags |= IORESOURCE_UNSET;
  665. continue;
  666. }
  667. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
  668. pci_name(dev), i,
  669. (unsigned long long)res->start,\
  670. (unsigned long long)res->end,
  671. (unsigned int)res->flags);
  672. fixup_resource(res, dev);
  673. }
  674. /* Call machine specific resource fixup */
  675. if (ppc_md.pcibios_fixup_resources)
  676. ppc_md.pcibios_fixup_resources(dev);
  677. }
  678. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  679. static void __devinit __pcibios_fixup_bus(struct pci_bus *bus)
  680. {
  681. struct pci_dev *dev = bus->self;
  682. pr_debug("PCI: Fixup bus %d (%s)\n", bus->number, dev ? pci_name(dev) : "PHB");
  683. /* Fixup PCI<->PCI bridges. Host bridges are handled separately, for
  684. * now differently between 32 and 64 bits.
  685. */
  686. if (dev != NULL) {
  687. struct resource *res;
  688. int i;
  689. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  690. if ((res = bus->resource[i]) == NULL)
  691. continue;
  692. if (!res->flags || bus->self->transparent)
  693. continue;
  694. pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
  695. pci_name(dev), i,
  696. (unsigned long long)res->start,\
  697. (unsigned long long)res->end,
  698. (unsigned int)res->flags);
  699. fixup_resource(res, dev);
  700. }
  701. }
  702. /* Additional setup that is different between 32 and 64 bits for now */
  703. pcibios_do_bus_setup(bus);
  704. /* Platform specific bus fixups */
  705. if (ppc_md.pcibios_fixup_bus)
  706. ppc_md.pcibios_fixup_bus(bus);
  707. /* Read default IRQs and fixup if necessary */
  708. list_for_each_entry(dev, &bus->devices, bus_list) {
  709. pci_read_irq_line(dev);
  710. if (ppc_md.pci_irq_fixup)
  711. ppc_md.pci_irq_fixup(dev);
  712. }
  713. }
  714. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  715. {
  716. /* When called from the generic PCI probe, read PCI<->PCI bridge
  717. * bases before proceeding
  718. */
  719. if (bus->self != NULL)
  720. pci_read_bridge_bases(bus);
  721. __pcibios_fixup_bus(bus);
  722. }
  723. EXPORT_SYMBOL(pcibios_fixup_bus);
  724. /* When building a bus from the OF tree rather than probing, we need a
  725. * slightly different version of the fixup which doesn't read the
  726. * bridge bases using config space accesses
  727. */
  728. void __devinit pcibios_fixup_of_probed_bus(struct pci_bus *bus)
  729. {
  730. __pcibios_fixup_bus(bus);
  731. }
  732. static int skip_isa_ioresource_align(struct pci_dev *dev)
  733. {
  734. if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
  735. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  736. return 1;
  737. return 0;
  738. }
  739. /*
  740. * We need to avoid collisions with `mirrored' VGA ports
  741. * and other strange ISA hardware, so we always want the
  742. * addresses to be allocated in the 0x000-0x0ff region
  743. * modulo 0x400.
  744. *
  745. * Why? Because some silly external IO cards only decode
  746. * the low 10 bits of the IO address. The 0x00-0xff region
  747. * is reserved for motherboard devices that decode all 16
  748. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  749. * but we want to try to avoid allocating at 0x2900-0x2bff
  750. * which might have be mirrored at 0x0100-0x03ff..
  751. */
  752. void pcibios_align_resource(void *data, struct resource *res,
  753. resource_size_t size, resource_size_t align)
  754. {
  755. struct pci_dev *dev = data;
  756. if (res->flags & IORESOURCE_IO) {
  757. resource_size_t start = res->start;
  758. if (skip_isa_ioresource_align(dev))
  759. return;
  760. if (start & 0x300) {
  761. start = (start + 0x3ff) & ~0x3ff;
  762. res->start = start;
  763. }
  764. }
  765. }
  766. EXPORT_SYMBOL(pcibios_align_resource);
  767. /*
  768. * Reparent resource children of pr that conflict with res
  769. * under res, and make res replace those children.
  770. */
  771. static int __init reparent_resources(struct resource *parent,
  772. struct resource *res)
  773. {
  774. struct resource *p, **pp;
  775. struct resource **firstpp = NULL;
  776. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  777. if (p->end < res->start)
  778. continue;
  779. if (res->end < p->start)
  780. break;
  781. if (p->start < res->start || p->end > res->end)
  782. return -1; /* not completely contained */
  783. if (firstpp == NULL)
  784. firstpp = pp;
  785. }
  786. if (firstpp == NULL)
  787. return -1; /* didn't find any conflicting entries? */
  788. res->parent = parent;
  789. res->child = *firstpp;
  790. res->sibling = *pp;
  791. *firstpp = res;
  792. *pp = NULL;
  793. for (p = res->child; p != NULL; p = p->sibling) {
  794. p->parent = res;
  795. DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
  796. p->name,
  797. (unsigned long long)p->start,
  798. (unsigned long long)p->end, res->name);
  799. }
  800. return 0;
  801. }
  802. /*
  803. * Handle resources of PCI devices. If the world were perfect, we could
  804. * just allocate all the resource regions and do nothing more. It isn't.
  805. * On the other hand, we cannot just re-allocate all devices, as it would
  806. * require us to know lots of host bridge internals. So we attempt to
  807. * keep as much of the original configuration as possible, but tweak it
  808. * when it's found to be wrong.
  809. *
  810. * Known BIOS problems we have to work around:
  811. * - I/O or memory regions not configured
  812. * - regions configured, but not enabled in the command register
  813. * - bogus I/O addresses above 64K used
  814. * - expansion ROMs left enabled (this may sound harmless, but given
  815. * the fact the PCI specs explicitly allow address decoders to be
  816. * shared between expansion ROMs and other resource regions, it's
  817. * at least dangerous)
  818. *
  819. * Our solution:
  820. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  821. * This gives us fixed barriers on where we can allocate.
  822. * (2) Allocate resources for all enabled devices. If there is
  823. * a collision, just mark the resource as unallocated. Also
  824. * disable expansion ROMs during this step.
  825. * (3) Try to allocate resources for disabled devices. If the
  826. * resources were assigned correctly, everything goes well,
  827. * if they weren't, they won't disturb allocation of other
  828. * resources.
  829. * (4) Assign new addresses to resources which were either
  830. * not configured at all or misconfigured. If explicitly
  831. * requested by the user, configure expansion ROM address
  832. * as well.
  833. */
  834. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  835. {
  836. struct pci_bus *bus;
  837. int i;
  838. struct resource *res, *pr;
  839. /* Depth-First Search on bus tree */
  840. list_for_each_entry(bus, bus_list, node) {
  841. for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
  842. if ((res = bus->resource[i]) == NULL || !res->flags
  843. || res->start > res->end)
  844. continue;
  845. if (bus->parent == NULL)
  846. pr = (res->flags & IORESOURCE_IO) ?
  847. &ioport_resource : &iomem_resource;
  848. else {
  849. /* Don't bother with non-root busses when
  850. * re-assigning all resources. We clear the
  851. * resource flags as if they were colliding
  852. * and as such ensure proper re-allocation
  853. * later.
  854. */
  855. if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
  856. goto clear_resource;
  857. pr = pci_find_parent_resource(bus->self, res);
  858. if (pr == res) {
  859. /* this happens when the generic PCI
  860. * code (wrongly) decides that this
  861. * bridge is transparent -- paulus
  862. */
  863. continue;
  864. }
  865. }
  866. DBG("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
  867. "[0x%x], parent %p (%s)\n",
  868. bus->self ? pci_name(bus->self) : "PHB",
  869. bus->number, i,
  870. (unsigned long long)res->start,
  871. (unsigned long long)res->end,
  872. (unsigned int)res->flags,
  873. pr, (pr && pr->name) ? pr->name : "nil");
  874. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  875. if (request_resource(pr, res) == 0)
  876. continue;
  877. /*
  878. * Must be a conflict with an existing entry.
  879. * Move that entry (or entries) under the
  880. * bridge resource and try again.
  881. */
  882. if (reparent_resources(pr, res) == 0)
  883. continue;
  884. }
  885. printk(KERN_WARNING
  886. "PCI: Cannot allocate resource region "
  887. "%d of PCI bridge %d, will remap\n",
  888. i, bus->number);
  889. clear_resource:
  890. res->flags = 0;
  891. }
  892. pcibios_allocate_bus_resources(&bus->children);
  893. }
  894. }
  895. static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
  896. {
  897. struct resource *pr, *r = &dev->resource[idx];
  898. DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
  899. pci_name(dev), idx,
  900. (unsigned long long)r->start,
  901. (unsigned long long)r->end,
  902. (unsigned int)r->flags);
  903. pr = pci_find_parent_resource(dev, r);
  904. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  905. request_resource(pr, r) < 0) {
  906. printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
  907. " of device %s, will remap\n", idx, pci_name(dev));
  908. if (pr)
  909. DBG("PCI: parent is %p: %016llx-%016llx [%x]\n", pr,
  910. (unsigned long long)pr->start,
  911. (unsigned long long)pr->end,
  912. (unsigned int)pr->flags);
  913. /* We'll assign a new address later */
  914. r->flags |= IORESOURCE_UNSET;
  915. r->end -= r->start;
  916. r->start = 0;
  917. }
  918. }
  919. static void __init pcibios_allocate_resources(int pass)
  920. {
  921. struct pci_dev *dev = NULL;
  922. int idx, disabled;
  923. u16 command;
  924. struct resource *r;
  925. for_each_pci_dev(dev) {
  926. pci_read_config_word(dev, PCI_COMMAND, &command);
  927. for (idx = 0; idx < 6; idx++) {
  928. r = &dev->resource[idx];
  929. if (r->parent) /* Already allocated */
  930. continue;
  931. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  932. continue; /* Not assigned at all */
  933. if (r->flags & IORESOURCE_IO)
  934. disabled = !(command & PCI_COMMAND_IO);
  935. else
  936. disabled = !(command & PCI_COMMAND_MEMORY);
  937. if (pass == disabled)
  938. alloc_resource(dev, idx);
  939. }
  940. if (pass)
  941. continue;
  942. r = &dev->resource[PCI_ROM_RESOURCE];
  943. if (r->flags & IORESOURCE_ROM_ENABLE) {
  944. /* Turn the ROM off, leave the resource region,
  945. * but keep it unregistered.
  946. */
  947. u32 reg;
  948. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  949. r->flags &= ~IORESOURCE_ROM_ENABLE;
  950. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  951. pci_write_config_dword(dev, dev->rom_base_reg,
  952. reg & ~PCI_ROM_ADDRESS_ENABLE);
  953. }
  954. }
  955. }
  956. void __init pcibios_resource_survey(void)
  957. {
  958. /* Allocate and assign resources. If we re-assign everything, then
  959. * we skip the allocate phase
  960. */
  961. pcibios_allocate_bus_resources(&pci_root_buses);
  962. if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
  963. pcibios_allocate_resources(0);
  964. pcibios_allocate_resources(1);
  965. }
  966. if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
  967. DBG("PCI: Assigning unassigned resouces...\n");
  968. pci_assign_unassigned_resources();
  969. }
  970. /* Call machine dependent fixup */
  971. if (ppc_md.pcibios_fixup)
  972. ppc_md.pcibios_fixup();
  973. }
  974. #ifdef CONFIG_HOTPLUG
  975. /* This is used by the pSeries hotplug driver to allocate resource
  976. * of newly plugged busses. We can try to consolidate with the
  977. * rest of the code later, for now, keep it as-is
  978. */
  979. void __devinit pcibios_claim_one_bus(struct pci_bus *bus)
  980. {
  981. struct pci_dev *dev;
  982. struct pci_bus *child_bus;
  983. list_for_each_entry(dev, &bus->devices, bus_list) {
  984. int i;
  985. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  986. struct resource *r = &dev->resource[i];
  987. if (r->parent || !r->start || !r->flags)
  988. continue;
  989. pci_claim_resource(dev, i);
  990. }
  991. }
  992. list_for_each_entry(child_bus, &bus->children, node)
  993. pcibios_claim_one_bus(child_bus);
  994. }
  995. EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
  996. #endif /* CONFIG_HOTPLUG */
  997. int pcibios_enable_device(struct pci_dev *dev, int mask)
  998. {
  999. u16 cmd, old_cmd;
  1000. int idx;
  1001. struct resource *r;
  1002. if (ppc_md.pcibios_enable_device_hook)
  1003. if (ppc_md.pcibios_enable_device_hook(dev))
  1004. return -EINVAL;
  1005. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1006. old_cmd = cmd;
  1007. for (idx = 0; idx < PCI_NUM_RESOURCES; idx++) {
  1008. /* Only set up the requested stuff */
  1009. if (!(mask & (1 << idx)))
  1010. continue;
  1011. r = &dev->resource[idx];
  1012. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  1013. continue;
  1014. if ((idx == PCI_ROM_RESOURCE) &&
  1015. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  1016. continue;
  1017. if (r->parent == NULL) {
  1018. printk(KERN_ERR "PCI: Device %s not available because"
  1019. " of resource collisions\n", pci_name(dev));
  1020. return -EINVAL;
  1021. }
  1022. if (r->flags & IORESOURCE_IO)
  1023. cmd |= PCI_COMMAND_IO;
  1024. if (r->flags & IORESOURCE_MEM)
  1025. cmd |= PCI_COMMAND_MEMORY;
  1026. }
  1027. if (cmd != old_cmd) {
  1028. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1029. pci_name(dev), old_cmd, cmd);
  1030. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1031. }
  1032. return 0;
  1033. }