1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * arch/xtensa/kernel/pci.c
4 *
5 * PCI bios-type initialisation for PCI machines
6 *
7 * Copyright (C) 2001-2005 Tensilica Inc.
8 *
9 * Based largely on work from Cort (ppc/kernel/pci.c)
10 * IO functions copied from sparc.
11 *
12 * Chris Zankel <chris@zankel.net>
13 */
14
15#include <linux/kernel.h>
16#include <linux/pci.h>
17#include <linux/delay.h>
18#include <linux/string.h>
19#include <linux/init.h>
20#include <linux/sched.h>
21#include <linux/errno.h>
22#include <linux/memblock.h>
23
24#include <asm/pci-bridge.h>
25#include <asm/platform.h>
26
27/*
28 * We need to avoid collisions with `mirrored' VGA ports
29 * and other strange ISA hardware, so we always want the
30 * addresses to be allocated in the 0x000-0x0ff region
31 * modulo 0x400.
32 *
33 * Why? Because some silly external IO cards only decode
34 * the low 10 bits of the IO address. The 0x00-0xff region
35 * is reserved for motherboard devices that decode all 16
36 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
37 * but we want to try to avoid allocating at 0x2900-0x2bff
38 * which might have be mirrored at 0x0100-0x03ff..
39 */
40resource_size_t
41pcibios_align_resource(void *data, const struct resource *res,
42		       resource_size_t size, resource_size_t align)
43{
44	struct pci_dev *dev = data;
45	resource_size_t start = res->start;
46
47	if (res->flags & IORESOURCE_IO) {
48		if (size > 0x100) {
49			pr_err("PCI: I/O Region %s/%d too large (%u bytes)\n",
50					pci_name(dev), dev->resource - res,
51					size);
52		}
53
54		if (start & 0x300)
55			start = (start + 0x3ff) & ~0x3ff;
56	}
57
58	return start;
59}
60
61void pcibios_fixup_bus(struct pci_bus *bus)
62{
63	if (bus->parent) {
64		/* This is a subordinate bridge */
65		pci_read_bridge_bases(bus);
66	}
67}
68
69/*
70 * Platform support for /proc/bus/pci/X/Y mmap()s.
71 *  -- paulus.
72 */
73
74int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
75{
76	struct pci_controller *pci_ctrl = (struct pci_controller*) pdev->sysdata;
77	resource_size_t ioaddr = pci_resource_start(pdev, bar);
78
79	if (!pci_ctrl)
80		return -EINVAL;		/* should never happen */
81
82	/* Convert to an offset within this PCI controller */
83	ioaddr -= (unsigned long)pci_ctrl->io_space.base;
84
85	vma->vm_pgoff += (ioaddr + pci_ctrl->io_space.start) >> PAGE_SHIFT;
86	return 0;
87}
88