pci-common.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Contains common pci routines for ALL ppc platform
  4. * (based on pci_32.c and pci_64.c)
  5. *
  6. * Port for PPC64 David Engebretsen, IBM Corp.
  7. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  8. *
  9. * Copyright (C) 2003 Anton Blanchard <[email protected]>, IBM
  10. * Rework, based on alpha PCI code.
  11. *
  12. * Common pmac/prep/chrp pci routines. -- Cort
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/string.h>
  17. #include <linux/init.h>
  18. #include <linux/memblock.h>
  19. #include <linux/mm.h>
  20. #include <linux/shmem_fs.h>
  21. #include <linux/list.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/irq.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/slab.h>
  26. #include <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_pci.h>
  30. #include <linux/export.h>
  31. #include <asm/processor.h>
  32. #include <linux/io.h>
  33. #include <asm/pci-bridge.h>
  34. #include <asm/byteorder.h>
  35. static DEFINE_SPINLOCK(hose_spinlock);
  36. LIST_HEAD(hose_list);
  37. /* XXX kill that some day ... */
  38. static int global_phb_number; /* Global phb counter */
  39. /* ISA Memory physical address */
  40. resource_size_t isa_mem_base;
  41. unsigned long isa_io_base;
  42. EXPORT_SYMBOL(isa_io_base);
  43. static int pci_bus_count;
  44. struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
  45. {
  46. struct pci_controller *phb;
  47. phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
  48. if (!phb)
  49. return NULL;
  50. spin_lock(&hose_spinlock);
  51. phb->global_number = global_phb_number++;
  52. list_add_tail(&phb->list_node, &hose_list);
  53. spin_unlock(&hose_spinlock);
  54. phb->dn = dev;
  55. phb->is_dynamic = mem_init_done;
  56. return phb;
  57. }
  58. void pcibios_free_controller(struct pci_controller *phb)
  59. {
  60. spin_lock(&hose_spinlock);
  61. list_del(&phb->list_node);
  62. spin_unlock(&hose_spinlock);
  63. if (phb->is_dynamic)
  64. kfree(phb);
  65. }
  66. static resource_size_t pcibios_io_size(const struct pci_controller *hose)
  67. {
  68. return resource_size(&hose->io_resource);
  69. }
  70. int pcibios_vaddr_is_ioport(void __iomem *address)
  71. {
  72. int ret = 0;
  73. struct pci_controller *hose;
  74. resource_size_t size;
  75. spin_lock(&hose_spinlock);
  76. list_for_each_entry(hose, &hose_list, list_node) {
  77. size = pcibios_io_size(hose);
  78. if (address >= hose->io_base_virt &&
  79. address < (hose->io_base_virt + size)) {
  80. ret = 1;
  81. break;
  82. }
  83. }
  84. spin_unlock(&hose_spinlock);
  85. return ret;
  86. }
  87. unsigned long pci_address_to_pio(phys_addr_t address)
  88. {
  89. struct pci_controller *hose;
  90. resource_size_t size;
  91. unsigned long ret = ~0;
  92. spin_lock(&hose_spinlock);
  93. list_for_each_entry(hose, &hose_list, list_node) {
  94. size = pcibios_io_size(hose);
  95. if (address >= hose->io_base_phys &&
  96. address < (hose->io_base_phys + size)) {
  97. unsigned long base =
  98. (unsigned long)hose->io_base_virt - _IO_BASE;
  99. ret = base + (address - hose->io_base_phys);
  100. break;
  101. }
  102. }
  103. spin_unlock(&hose_spinlock);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  107. /* This routine is meant to be used early during boot, when the
  108. * PCI bus numbers have not yet been assigned, and you need to
  109. * issue PCI config cycles to an OF device.
  110. * It could also be used to "fix" RTAS config cycles if you want
  111. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  112. * config cycles.
  113. */
  114. struct pci_controller *pci_find_hose_for_OF_device(struct device_node *node)
  115. {
  116. while (node) {
  117. struct pci_controller *hose, *tmp;
  118. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  119. if (hose->dn == node)
  120. return hose;
  121. node = node->parent;
  122. }
  123. return NULL;
  124. }
  125. void pcibios_set_master(struct pci_dev *dev)
  126. {
  127. /* No special bus mastering setup handling */
  128. }
  129. /*
  130. * Platform support for /proc/bus/pci/X/Y mmap()s.
  131. */
  132. int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
  133. {
  134. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  135. resource_size_t ioaddr = pci_resource_start(pdev, bar);
  136. if (!hose)
  137. return -EINVAL; /* should never happen */
  138. /* Convert to an offset within this PCI controller */
  139. ioaddr -= (unsigned long)hose->io_base_virt - _IO_BASE;
  140. vma->vm_pgoff += (ioaddr + hose->io_base_phys) >> PAGE_SHIFT;
  141. return 0;
  142. }
  143. /* This provides legacy IO read access on a bus */
  144. int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
  145. {
  146. unsigned long offset;
  147. struct pci_controller *hose = pci_bus_to_host(bus);
  148. struct resource *rp = &hose->io_resource;
  149. void __iomem *addr;
  150. /* Check if port can be supported by that bus. We only check
  151. * the ranges of the PHB though, not the bus itself as the rules
  152. * for forwarding legacy cycles down bridges are not our problem
  153. * here. So if the host bridge supports it, we do it.
  154. */
  155. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  156. offset += port;
  157. if (!(rp->flags & IORESOURCE_IO))
  158. return -ENXIO;
  159. if (offset < rp->start || (offset + size) > rp->end)
  160. return -ENXIO;
  161. addr = hose->io_base_virt + port;
  162. switch (size) {
  163. case 1:
  164. *((u8 *)val) = in_8(addr);
  165. return 1;
  166. case 2:
  167. if (port & 1)
  168. return -EINVAL;
  169. *((u16 *)val) = in_le16(addr);
  170. return 2;
  171. case 4:
  172. if (port & 3)
  173. return -EINVAL;
  174. *((u32 *)val) = in_le32(addr);
  175. return 4;
  176. }
  177. return -EINVAL;
  178. }
  179. /* This provides legacy IO write access on a bus */
  180. int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
  181. {
  182. unsigned long offset;
  183. struct pci_controller *hose = pci_bus_to_host(bus);
  184. struct resource *rp = &hose->io_resource;
  185. void __iomem *addr;
  186. /* Check if port can be supported by that bus. We only check
  187. * the ranges of the PHB though, not the bus itself as the rules
  188. * for forwarding legacy cycles down bridges are not our problem
  189. * here. So if the host bridge supports it, we do it.
  190. */
  191. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  192. offset += port;
  193. if (!(rp->flags & IORESOURCE_IO))
  194. return -ENXIO;
  195. if (offset < rp->start || (offset + size) > rp->end)
  196. return -ENXIO;
  197. addr = hose->io_base_virt + port;
  198. /* WARNING: The generic code is idiotic. It gets passed a pointer
  199. * to what can be a 1, 2 or 4 byte quantity and always reads that
  200. * as a u32, which means that we have to correct the location of
  201. * the data read within those 32 bits for size 1 and 2
  202. */
  203. switch (size) {
  204. case 1:
  205. out_8(addr, val >> 24);
  206. return 1;
  207. case 2:
  208. if (port & 1)
  209. return -EINVAL;
  210. out_le16(addr, val >> 16);
  211. return 2;
  212. case 4:
  213. if (port & 3)
  214. return -EINVAL;
  215. out_le32(addr, val);
  216. return 4;
  217. }
  218. return -EINVAL;
  219. }
  220. /* This provides legacy IO or memory mmap access on a bus */
  221. int pci_mmap_legacy_page_range(struct pci_bus *bus,
  222. struct vm_area_struct *vma,
  223. enum pci_mmap_state mmap_state)
  224. {
  225. struct pci_controller *hose = pci_bus_to_host(bus);
  226. resource_size_t offset =
  227. ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
  228. resource_size_t size = vma->vm_end - vma->vm_start;
  229. struct resource *rp;
  230. pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
  231. pci_domain_nr(bus), bus->number,
  232. mmap_state == pci_mmap_mem ? "MEM" : "IO",
  233. (unsigned long long)offset,
  234. (unsigned long long)(offset + size - 1));
  235. if (mmap_state == pci_mmap_mem) {
  236. /* Hack alert !
  237. *
  238. * Because X is lame and can fail starting if it gets an error
  239. * trying to mmap legacy_mem (instead of just moving on without
  240. * legacy memory access) we fake it here by giving it anonymous
  241. * memory, effectively behaving just like /dev/zero
  242. */
  243. if ((offset + size) > hose->isa_mem_size) {
  244. pr_debug("Process %s (pid:%d) mapped non-existing PCI",
  245. current->comm, current->pid);
  246. pr_debug("legacy memory for 0%04x:%02x\n",
  247. pci_domain_nr(bus), bus->number);
  248. if (vma->vm_flags & VM_SHARED)
  249. return shmem_zero_setup(vma);
  250. return 0;
  251. }
  252. offset += hose->isa_mem_phys;
  253. } else {
  254. unsigned long io_offset = (unsigned long)hose->io_base_virt -
  255. _IO_BASE;
  256. unsigned long roffset = offset + io_offset;
  257. rp = &hose->io_resource;
  258. if (!(rp->flags & IORESOURCE_IO))
  259. return -ENXIO;
  260. if (roffset < rp->start || (roffset + size) > rp->end)
  261. return -ENXIO;
  262. offset += hose->io_base_phys;
  263. }
  264. pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
  265. vma->vm_pgoff = offset >> PAGE_SHIFT;
  266. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  267. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  268. vma->vm_end - vma->vm_start,
  269. vma->vm_page_prot);
  270. }
  271. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  272. const struct resource *rsrc,
  273. resource_size_t *start, resource_size_t *end)
  274. {
  275. struct pci_bus_region region;
  276. if (rsrc->flags & IORESOURCE_IO) {
  277. pcibios_resource_to_bus(dev->bus, &region,
  278. (struct resource *) rsrc);
  279. *start = region.start;
  280. *end = region.end;
  281. return;
  282. }
  283. /* We pass a CPU physical address to userland for MMIO instead of a
  284. * BAR value because X is lame and expects to be able to use that
  285. * to pass to /dev/mem!
  286. *
  287. * That means we may have 64-bit values where some apps only expect
  288. * 32 (like X itself since it thinks only Sparc has 64-bit MMIO).
  289. */
  290. *start = rsrc->start;
  291. *end = rsrc->end;
  292. }
  293. /**
  294. * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
  295. * @hose: newly allocated pci_controller to be setup
  296. * @dev: device node of the host bridge
  297. * @primary: set if primary bus (32 bits only, soon to be deprecated)
  298. *
  299. * This function will parse the "ranges" property of a PCI host bridge device
  300. * node and setup the resource mapping of a pci controller based on its
  301. * content.
  302. *
  303. * Life would be boring if it wasn't for a few issues that we have to deal
  304. * with here:
  305. *
  306. * - We can only cope with one IO space range and up to 3 Memory space
  307. * ranges. However, some machines (thanks Apple !) tend to split their
  308. * space into lots of small contiguous ranges. So we have to coalesce.
  309. *
  310. * - We can only cope with all memory ranges having the same offset
  311. * between CPU addresses and PCI addresses. Unfortunately, some bridges
  312. * are setup for a large 1:1 mapping along with a small "window" which
  313. * maps PCI address 0 to some arbitrary high address of the CPU space in
  314. * order to give access to the ISA memory hole.
  315. * The way out of here that I've chosen for now is to always set the
  316. * offset based on the first resource found, then override it if we
  317. * have a different offset and the previous was set by an ISA hole.
  318. *
  319. * - Some busses have IO space not starting at 0, which causes trouble with
  320. * the way we do our IO resource renumbering. The code somewhat deals with
  321. * it for 64 bits but I would expect problems on 32 bits.
  322. *
  323. * - Some 32 bits platforms such as 4xx can have physical space larger than
  324. * 32 bits so we need to use 64 bits values for the parsing
  325. */
  326. void pci_process_bridge_OF_ranges(struct pci_controller *hose,
  327. struct device_node *dev, int primary)
  328. {
  329. int memno = 0, isa_hole = -1;
  330. unsigned long long isa_mb = 0;
  331. struct resource *res;
  332. struct of_pci_range range;
  333. struct of_pci_range_parser parser;
  334. pr_info("PCI host bridge %pOF %s ranges:\n",
  335. dev, primary ? "(primary)" : "");
  336. /* Check for ranges property */
  337. if (of_pci_range_parser_init(&parser, dev))
  338. return;
  339. pr_debug("Parsing ranges property...\n");
  340. for_each_of_pci_range(&parser, &range) {
  341. /* Read next ranges element */
  342. /* If we failed translation or got a zero-sized region
  343. * (some FW try to feed us with non sensical zero sized regions
  344. * such as power3 which look like some kind of attempt
  345. * at exposing the VGA memory hole)
  346. */
  347. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  348. continue;
  349. /* Act based on address space type */
  350. res = NULL;
  351. switch (range.flags & IORESOURCE_TYPE_BITS) {
  352. case IORESOURCE_IO:
  353. pr_info(" IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  354. range.cpu_addr, range.cpu_addr + range.size - 1,
  355. range.pci_addr);
  356. /* We support only one IO range */
  357. if (hose->pci_io_size) {
  358. pr_info(" \\--> Skipped (too many) !\n");
  359. continue;
  360. }
  361. /* On 32 bits, limit I/O space to 16MB */
  362. if (range.size > 0x01000000)
  363. range.size = 0x01000000;
  364. /* 32 bits needs to map IOs here */
  365. hose->io_base_virt = ioremap(range.cpu_addr,
  366. range.size);
  367. /* Expect trouble if pci_addr is not 0 */
  368. if (primary)
  369. isa_io_base =
  370. (unsigned long)hose->io_base_virt;
  371. /* pci_io_size and io_base_phys always represent IO
  372. * space starting at 0 so we factor in pci_addr
  373. */
  374. hose->pci_io_size = range.pci_addr + range.size;
  375. hose->io_base_phys = range.cpu_addr - range.pci_addr;
  376. /* Build resource */
  377. res = &hose->io_resource;
  378. range.cpu_addr = range.pci_addr;
  379. break;
  380. case IORESOURCE_MEM:
  381. pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
  382. range.cpu_addr, range.cpu_addr + range.size - 1,
  383. range.pci_addr,
  384. (range.flags & IORESOURCE_PREFETCH) ?
  385. "Prefetch" : "");
  386. /* We support only 3 memory ranges */
  387. if (memno >= 3) {
  388. pr_info(" \\--> Skipped (too many) !\n");
  389. continue;
  390. }
  391. /* Handles ISA memory hole space here */
  392. if (range.pci_addr == 0) {
  393. isa_mb = range.cpu_addr;
  394. isa_hole = memno;
  395. if (primary || isa_mem_base == 0)
  396. isa_mem_base = range.cpu_addr;
  397. hose->isa_mem_phys = range.cpu_addr;
  398. hose->isa_mem_size = range.size;
  399. }
  400. /* We get the PCI/Mem offset from the first range or
  401. * the, current one if the offset came from an ISA
  402. * hole. If they don't match, bugger.
  403. */
  404. if (memno == 0 ||
  405. (isa_hole >= 0 && range.pci_addr != 0 &&
  406. hose->pci_mem_offset == isa_mb))
  407. hose->pci_mem_offset = range.cpu_addr -
  408. range.pci_addr;
  409. else if (range.pci_addr != 0 &&
  410. hose->pci_mem_offset != range.cpu_addr -
  411. range.pci_addr) {
  412. pr_info(" \\--> Skipped (offset mismatch) !\n");
  413. continue;
  414. }
  415. /* Build resource */
  416. res = &hose->mem_resources[memno++];
  417. break;
  418. }
  419. if (res != NULL) {
  420. res->name = dev->full_name;
  421. res->flags = range.flags;
  422. res->start = range.cpu_addr;
  423. res->end = range.cpu_addr + range.size - 1;
  424. res->parent = res->child = res->sibling = NULL;
  425. }
  426. }
  427. /* If there's an ISA hole and the pci_mem_offset is -not- matching
  428. * the ISA hole offset, then we need to remove the ISA hole from
  429. * the resource list for that brige
  430. */
  431. if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
  432. unsigned int next = isa_hole + 1;
  433. pr_info(" Removing ISA hole at 0x%016llx\n", isa_mb);
  434. if (next < memno)
  435. memmove(&hose->mem_resources[isa_hole],
  436. &hose->mem_resources[next],
  437. sizeof(struct resource) * (memno - next));
  438. hose->mem_resources[--memno].flags = 0;
  439. }
  440. }
  441. /* Display the domain number in /proc */
  442. int pci_proc_domain(struct pci_bus *bus)
  443. {
  444. return pci_domain_nr(bus);
  445. }
  446. /* This header fixup will do the resource fixup for all devices as they are
  447. * probed, but not for bridge ranges
  448. */
  449. static void pcibios_fixup_resources(struct pci_dev *dev)
  450. {
  451. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  452. int i;
  453. if (!hose) {
  454. pr_err("No host bridge for PCI dev %s !\n",
  455. pci_name(dev));
  456. return;
  457. }
  458. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  459. struct resource *res = dev->resource + i;
  460. if (!res->flags)
  461. continue;
  462. if (res->start == 0) {
  463. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x]",
  464. pci_name(dev), i,
  465. (unsigned long long)res->start,
  466. (unsigned long long)res->end,
  467. (unsigned int)res->flags);
  468. pr_debug("is unassigned\n");
  469. res->end -= res->start;
  470. res->start = 0;
  471. res->flags |= IORESOURCE_UNSET;
  472. continue;
  473. }
  474. pr_debug("PCI:%s Resource %d %016llx-%016llx [%x]\n",
  475. pci_name(dev), i,
  476. (unsigned long long)res->start,
  477. (unsigned long long)res->end,
  478. (unsigned int)res->flags);
  479. }
  480. }
  481. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  482. int pcibios_device_add(struct pci_dev *dev)
  483. {
  484. dev->irq = of_irq_parse_and_map_pci(dev, 0, 0);
  485. return 0;
  486. }
  487. /*
  488. * Reparent resource children of pr that conflict with res
  489. * under res, and make res replace those children.
  490. */
  491. static int __init reparent_resources(struct resource *parent,
  492. struct resource *res)
  493. {
  494. struct resource *p, **pp;
  495. struct resource **firstpp = NULL;
  496. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  497. if (p->end < res->start)
  498. continue;
  499. if (res->end < p->start)
  500. break;
  501. if (p->start < res->start || p->end > res->end)
  502. return -1; /* not completely contained */
  503. if (firstpp == NULL)
  504. firstpp = pp;
  505. }
  506. if (firstpp == NULL)
  507. return -1; /* didn't find any conflicting entries? */
  508. res->parent = parent;
  509. res->child = *firstpp;
  510. res->sibling = *pp;
  511. *firstpp = res;
  512. *pp = NULL;
  513. for (p = res->child; p != NULL; p = p->sibling) {
  514. p->parent = res;
  515. pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n",
  516. p->name,
  517. (unsigned long long)p->start,
  518. (unsigned long long)p->end, res->name);
  519. }
  520. return 0;
  521. }
  522. /*
  523. * Handle resources of PCI devices. If the world were perfect, we could
  524. * just allocate all the resource regions and do nothing more. It isn't.
  525. * On the other hand, we cannot just re-allocate all devices, as it would
  526. * require us to know lots of host bridge internals. So we attempt to
  527. * keep as much of the original configuration as possible, but tweak it
  528. * when it's found to be wrong.
  529. *
  530. * Known BIOS problems we have to work around:
  531. * - I/O or memory regions not configured
  532. * - regions configured, but not enabled in the command register
  533. * - bogus I/O addresses above 64K used
  534. * - expansion ROMs left enabled (this may sound harmless, but given
  535. * the fact the PCI specs explicitly allow address decoders to be
  536. * shared between expansion ROMs and other resource regions, it's
  537. * at least dangerous)
  538. *
  539. * Our solution:
  540. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  541. * This gives us fixed barriers on where we can allocate.
  542. * (2) Allocate resources for all enabled devices. If there is
  543. * a collision, just mark the resource as unallocated. Also
  544. * disable expansion ROMs during this step.
  545. * (3) Try to allocate resources for disabled devices. If the
  546. * resources were assigned correctly, everything goes well,
  547. * if they weren't, they won't disturb allocation of other
  548. * resources.
  549. * (4) Assign new addresses to resources which were either
  550. * not configured at all or misconfigured. If explicitly
  551. * requested by the user, configure expansion ROM address
  552. * as well.
  553. */
  554. static void pcibios_allocate_bus_resources(struct pci_bus *bus)
  555. {
  556. struct pci_bus *b;
  557. int i;
  558. struct resource *res, *pr;
  559. pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
  560. pci_domain_nr(bus), bus->number);
  561. pci_bus_for_each_resource(bus, res, i) {
  562. if (!res || !res->flags
  563. || res->start > res->end || res->parent)
  564. continue;
  565. if (bus->parent == NULL)
  566. pr = (res->flags & IORESOURCE_IO) ?
  567. &ioport_resource : &iomem_resource;
  568. else {
  569. /* Don't bother with non-root busses when
  570. * re-assigning all resources. We clear the
  571. * resource flags as if they were colliding
  572. * and as such ensure proper re-allocation
  573. * later.
  574. */
  575. pr = pci_find_parent_resource(bus->self, res);
  576. if (pr == res) {
  577. /* this happens when the generic PCI
  578. * code (wrongly) decides that this
  579. * bridge is transparent -- paulus
  580. */
  581. continue;
  582. }
  583. }
  584. pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx ",
  585. bus->self ? pci_name(bus->self) : "PHB",
  586. bus->number, i,
  587. (unsigned long long)res->start,
  588. (unsigned long long)res->end);
  589. pr_debug("[0x%x], parent %p (%s)\n",
  590. (unsigned int)res->flags,
  591. pr, (pr && pr->name) ? pr->name : "nil");
  592. if (pr && !(pr->flags & IORESOURCE_UNSET)) {
  593. struct pci_dev *dev = bus->self;
  594. if (request_resource(pr, res) == 0)
  595. continue;
  596. /*
  597. * Must be a conflict with an existing entry.
  598. * Move that entry (or entries) under the
  599. * bridge resource and try again.
  600. */
  601. if (reparent_resources(pr, res) == 0)
  602. continue;
  603. if (dev && i < PCI_BRIDGE_RESOURCE_NUM &&
  604. pci_claim_bridge_resource(dev,
  605. i + PCI_BRIDGE_RESOURCES) == 0)
  606. continue;
  607. }
  608. pr_warn("PCI: Cannot allocate resource region ");
  609. pr_cont("%d of PCI bridge %d, will remap\n", i, bus->number);
  610. res->start = res->end = 0;
  611. res->flags = 0;
  612. }
  613. list_for_each_entry(b, &bus->children, node)
  614. pcibios_allocate_bus_resources(b);
  615. }
  616. static inline void alloc_resource(struct pci_dev *dev, int idx)
  617. {
  618. struct resource *pr, *r = &dev->resource[idx];
  619. pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
  620. pci_name(dev), idx,
  621. (unsigned long long)r->start,
  622. (unsigned long long)r->end,
  623. (unsigned int)r->flags);
  624. pr = pci_find_parent_resource(dev, r);
  625. if (!pr || (pr->flags & IORESOURCE_UNSET) ||
  626. request_resource(pr, r) < 0) {
  627. pr_warn("PCI: Cannot allocate resource region %d ", idx);
  628. pr_cont("of device %s, will remap\n", pci_name(dev));
  629. if (pr)
  630. pr_debug("PCI: parent is %p: %016llx-%016llx [%x]\n",
  631. pr,
  632. (unsigned long long)pr->start,
  633. (unsigned long long)pr->end,
  634. (unsigned int)pr->flags);
  635. /* We'll assign a new address later */
  636. r->flags |= IORESOURCE_UNSET;
  637. r->end -= r->start;
  638. r->start = 0;
  639. }
  640. }
  641. static void __init pcibios_allocate_resources(int pass)
  642. {
  643. struct pci_dev *dev = NULL;
  644. int idx, disabled;
  645. u16 command;
  646. struct resource *r;
  647. for_each_pci_dev(dev) {
  648. pci_read_config_word(dev, PCI_COMMAND, &command);
  649. for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
  650. r = &dev->resource[idx];
  651. if (r->parent) /* Already allocated */
  652. continue;
  653. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  654. continue; /* Not assigned at all */
  655. /* We only allocate ROMs on pass 1 just in case they
  656. * have been screwed up by firmware
  657. */
  658. if (idx == PCI_ROM_RESOURCE)
  659. disabled = 1;
  660. if (r->flags & IORESOURCE_IO)
  661. disabled = !(command & PCI_COMMAND_IO);
  662. else
  663. disabled = !(command & PCI_COMMAND_MEMORY);
  664. if (pass == disabled)
  665. alloc_resource(dev, idx);
  666. }
  667. if (pass)
  668. continue;
  669. r = &dev->resource[PCI_ROM_RESOURCE];
  670. if (r->flags) {
  671. /* Turn the ROM off, leave the resource region,
  672. * but keep it unregistered.
  673. */
  674. u32 reg;
  675. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  676. if (reg & PCI_ROM_ADDRESS_ENABLE) {
  677. pr_debug("PCI: Switching off ROM of %s\n",
  678. pci_name(dev));
  679. r->flags &= ~IORESOURCE_ROM_ENABLE;
  680. pci_write_config_dword(dev, dev->rom_base_reg,
  681. reg & ~PCI_ROM_ADDRESS_ENABLE);
  682. }
  683. }
  684. }
  685. }
  686. static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
  687. {
  688. struct pci_controller *hose = pci_bus_to_host(bus);
  689. resource_size_t offset;
  690. struct resource *res, *pres;
  691. int i;
  692. pr_debug("Reserving legacy ranges for domain %04x\n",
  693. pci_domain_nr(bus));
  694. /* Check for IO */
  695. if (!(hose->io_resource.flags & IORESOURCE_IO))
  696. goto no_io;
  697. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  698. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  699. BUG_ON(res == NULL);
  700. res->name = "Legacy IO";
  701. res->flags = IORESOURCE_IO;
  702. res->start = offset;
  703. res->end = (offset + 0xfff) & 0xfffffffful;
  704. pr_debug("Candidate legacy IO: %pR\n", res);
  705. if (request_resource(&hose->io_resource, res)) {
  706. pr_debug("PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
  707. pci_domain_nr(bus), bus->number, res);
  708. kfree(res);
  709. }
  710. no_io:
  711. /* Check for memory */
  712. offset = hose->pci_mem_offset;
  713. pr_debug("hose mem offset: %016llx\n", (unsigned long long)offset);
  714. for (i = 0; i < 3; i++) {
  715. pres = &hose->mem_resources[i];
  716. if (!(pres->flags & IORESOURCE_MEM))
  717. continue;
  718. pr_debug("hose mem res: %pR\n", pres);
  719. if ((pres->start - offset) <= 0xa0000 &&
  720. (pres->end - offset) >= 0xbffff)
  721. break;
  722. }
  723. if (i >= 3)
  724. return;
  725. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  726. BUG_ON(res == NULL);
  727. res->name = "Legacy VGA memory";
  728. res->flags = IORESOURCE_MEM;
  729. res->start = 0xa0000 + offset;
  730. res->end = 0xbffff + offset;
  731. pr_debug("Candidate VGA memory: %pR\n", res);
  732. if (request_resource(pres, res)) {
  733. pr_debug("PCI %04x:%02x Cannot reserve VGA memory %pR\n",
  734. pci_domain_nr(bus), bus->number, res);
  735. kfree(res);
  736. }
  737. }
  738. void __init pcibios_resource_survey(void)
  739. {
  740. struct pci_bus *b;
  741. /* Allocate and assign resources. If we re-assign everything, then
  742. * we skip the allocate phase
  743. */
  744. list_for_each_entry(b, &pci_root_buses, node)
  745. pcibios_allocate_bus_resources(b);
  746. pcibios_allocate_resources(0);
  747. pcibios_allocate_resources(1);
  748. /* Before we start assigning unassigned resource, we try to reserve
  749. * the low IO area and the VGA memory area if they intersect the
  750. * bus available resources to avoid allocating things on top of them
  751. */
  752. list_for_each_entry(b, &pci_root_buses, node)
  753. pcibios_reserve_legacy_regions(b);
  754. /* Now proceed to assigning things that were left unassigned */
  755. pr_debug("PCI: Assigning unassigned resources...\n");
  756. pci_assign_unassigned_resources();
  757. }
  758. static void pcibios_setup_phb_resources(struct pci_controller *hose,
  759. struct list_head *resources)
  760. {
  761. unsigned long io_offset;
  762. struct resource *res;
  763. int i;
  764. /* Hookup PHB IO resource */
  765. res = &hose->io_resource;
  766. /* Fixup IO space offset */
  767. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  768. res->start = (res->start + io_offset) & 0xffffffffu;
  769. res->end = (res->end + io_offset) & 0xffffffffu;
  770. if (!res->flags) {
  771. pr_warn("PCI: I/O resource not set for host ");
  772. pr_cont("bridge %pOF (domain %d)\n",
  773. hose->dn, hose->global_number);
  774. /* Workaround for lack of IO resource only on 32-bit */
  775. res->start = (unsigned long)hose->io_base_virt - isa_io_base;
  776. res->end = res->start + IO_SPACE_LIMIT;
  777. res->flags = IORESOURCE_IO;
  778. }
  779. pci_add_resource_offset(resources, res,
  780. (__force resource_size_t)(hose->io_base_virt - _IO_BASE));
  781. pr_debug("PCI: PHB IO resource = %016llx-%016llx [%lx]\n",
  782. (unsigned long long)res->start,
  783. (unsigned long long)res->end,
  784. (unsigned long)res->flags);
  785. /* Hookup PHB Memory resources */
  786. for (i = 0; i < 3; ++i) {
  787. res = &hose->mem_resources[i];
  788. if (!res->flags) {
  789. if (i > 0)
  790. continue;
  791. pr_err("PCI: Memory resource 0 not set for ");
  792. pr_cont("host bridge %pOF (domain %d)\n",
  793. hose->dn, hose->global_number);
  794. /* Workaround for lack of MEM resource only on 32-bit */
  795. res->start = hose->pci_mem_offset;
  796. res->end = (resource_size_t)-1LL;
  797. res->flags = IORESOURCE_MEM;
  798. }
  799. pci_add_resource_offset(resources, res, hose->pci_mem_offset);
  800. pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n",
  801. i, (unsigned long long)res->start,
  802. (unsigned long long)res->end,
  803. (unsigned long)res->flags);
  804. }
  805. pr_debug("PCI: PHB MEM offset = %016llx\n",
  806. (unsigned long long)hose->pci_mem_offset);
  807. pr_debug("PCI: PHB IO offset = %08lx\n",
  808. (unsigned long)hose->io_base_virt - _IO_BASE);
  809. }
  810. static void pcibios_scan_phb(struct pci_controller *hose)
  811. {
  812. LIST_HEAD(resources);
  813. struct pci_bus *bus;
  814. struct device_node *node = hose->dn;
  815. pr_debug("PCI: Scanning PHB %pOF\n", node);
  816. pcibios_setup_phb_resources(hose, &resources);
  817. bus = pci_scan_root_bus(hose->parent, hose->first_busno,
  818. hose->ops, hose, &resources);
  819. if (bus == NULL) {
  820. pr_err("Failed to create bus for PCI domain %04x\n",
  821. hose->global_number);
  822. pci_free_resource_list(&resources);
  823. return;
  824. }
  825. bus->busn_res.start = hose->first_busno;
  826. hose->bus = bus;
  827. hose->last_busno = bus->busn_res.end;
  828. }
  829. static int __init pcibios_init(void)
  830. {
  831. struct pci_controller *hose, *tmp;
  832. int next_busno = 0;
  833. pr_info("PCI: Probing PCI hardware\n");
  834. /* Scan all of the recorded PCI controllers. */
  835. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  836. hose->last_busno = 0xff;
  837. pcibios_scan_phb(hose);
  838. if (next_busno <= hose->last_busno)
  839. next_busno = hose->last_busno + 1;
  840. }
  841. pci_bus_count = next_busno;
  842. /* Call common code to handle resource allocation */
  843. pcibios_resource_survey();
  844. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  845. if (hose->bus)
  846. pci_bus_add_devices(hose->bus);
  847. }
  848. return 0;
  849. }
  850. subsys_initcall(pcibios_init);
  851. static struct pci_controller *pci_bus_to_hose(int bus)
  852. {
  853. struct pci_controller *hose, *tmp;
  854. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  855. if (bus >= hose->first_busno && bus <= hose->last_busno)
  856. return hose;
  857. return NULL;
  858. }
  859. /* Provide information on locations of various I/O regions in physical
  860. * memory. Do this on a per-card basis so that we choose the right
  861. * root bridge.
  862. * Note that the returned IO or memory base is a physical address
  863. */
  864. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  865. {
  866. struct pci_controller *hose;
  867. long result = -EOPNOTSUPP;
  868. hose = pci_bus_to_hose(bus);
  869. if (!hose)
  870. return -ENODEV;
  871. switch (which) {
  872. case IOBASE_BRIDGE_NUMBER:
  873. return (long)hose->first_busno;
  874. case IOBASE_MEMORY:
  875. return (long)hose->pci_mem_offset;
  876. case IOBASE_IO:
  877. return (long)hose->io_base_phys;
  878. case IOBASE_ISA_IO:
  879. return (long)isa_io_base;
  880. case IOBASE_ISA_MEM:
  881. return (long)isa_mem_base;
  882. }
  883. return result;
  884. }
  885. /*
  886. * Null PCI config access functions, for the case when we can't
  887. * find a hose.
  888. */
  889. #define NULL_PCI_OP(rw, size, type) \
  890. static int \
  891. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  892. { \
  893. return PCIBIOS_DEVICE_NOT_FOUND; \
  894. }
  895. static int
  896. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  897. int len, u32 *val)
  898. {
  899. return PCIBIOS_DEVICE_NOT_FOUND;
  900. }
  901. static int
  902. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  903. int len, u32 val)
  904. {
  905. return PCIBIOS_DEVICE_NOT_FOUND;
  906. }
  907. static struct pci_ops null_pci_ops = {
  908. .read = null_read_config,
  909. .write = null_write_config,
  910. };
  911. /*
  912. * These functions are used early on before PCI scanning is done
  913. * and all of the pci_dev and pci_bus structures have been created.
  914. */
  915. static struct pci_bus *
  916. fake_pci_bus(struct pci_controller *hose, int busnr)
  917. {
  918. static struct pci_bus bus;
  919. if (!hose)
  920. pr_err("Can't find hose for PCI bus %d!\n", busnr);
  921. bus.number = busnr;
  922. bus.sysdata = hose;
  923. bus.ops = hose ? hose->ops : &null_pci_ops;
  924. return &bus;
  925. }
  926. #define EARLY_PCI_OP(rw, size, type) \
  927. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  928. int devfn, int offset, type value) \
  929. { \
  930. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  931. devfn, offset, value); \
  932. }
  933. EARLY_PCI_OP(read, byte, u8 *)
  934. EARLY_PCI_OP(read, word, u16 *)
  935. EARLY_PCI_OP(read, dword, u32 *)
  936. EARLY_PCI_OP(write, byte, u8)
  937. EARLY_PCI_OP(write, word, u16)
  938. EARLY_PCI_OP(write, dword, u32)
  939. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  940. int cap)
  941. {
  942. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  943. }