pci-common.c 40 KB

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