pci-common.c 33 KB

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