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