xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision 2d5f5659649924b86d527a2af2552bab741d1d6f)
15516b540SKumar Gala /*
25516b540SKumar Gala  * Contains common pci routines for ALL ppc platform
3cf1d8a8aSKumar Gala  * (based on pci_32.c and pci_64.c)
4cf1d8a8aSKumar Gala  *
5cf1d8a8aSKumar Gala  * Port for PPC64 David Engebretsen, IBM Corp.
6cf1d8a8aSKumar Gala  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
7cf1d8a8aSKumar Gala  *
8cf1d8a8aSKumar Gala  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
9cf1d8a8aSKumar Gala  *   Rework, based on alpha PCI code.
10cf1d8a8aSKumar Gala  *
11cf1d8a8aSKumar Gala  * Common pmac/prep/chrp pci routines. -- Cort
125516b540SKumar Gala  *
135516b540SKumar Gala  * This program is free software; you can redistribute it and/or
145516b540SKumar Gala  * modify it under the terms of the GNU General Public License
155516b540SKumar Gala  * as published by the Free Software Foundation; either version
165516b540SKumar Gala  * 2 of the License, or (at your option) any later version.
175516b540SKumar Gala  */
185516b540SKumar Gala 
195516b540SKumar Gala #undef DEBUG
205516b540SKumar Gala 
215516b540SKumar Gala #include <linux/kernel.h>
225516b540SKumar Gala #include <linux/pci.h>
235516b540SKumar Gala #include <linux/string.h>
245516b540SKumar Gala #include <linux/init.h>
255516b540SKumar Gala #include <linux/bootmem.h>
265516b540SKumar Gala #include <linux/mm.h>
275516b540SKumar Gala #include <linux/list.h>
285516b540SKumar Gala #include <linux/syscalls.h>
295516b540SKumar Gala #include <linux/irq.h>
305516b540SKumar Gala #include <linux/vmalloc.h>
315516b540SKumar Gala 
325516b540SKumar Gala #include <asm/processor.h>
335516b540SKumar Gala #include <asm/io.h>
345516b540SKumar Gala #include <asm/prom.h>
355516b540SKumar Gala #include <asm/pci-bridge.h>
365516b540SKumar Gala #include <asm/byteorder.h>
375516b540SKumar Gala #include <asm/machdep.h>
385516b540SKumar Gala #include <asm/ppc-pci.h>
395516b540SKumar Gala #include <asm/firmware.h>
405516b540SKumar Gala 
415516b540SKumar Gala #ifdef DEBUG
425516b540SKumar Gala #include <asm/udbg.h>
435516b540SKumar Gala #define DBG(fmt...) printk(fmt)
445516b540SKumar Gala #else
455516b540SKumar Gala #define DBG(fmt...)
465516b540SKumar Gala #endif
475516b540SKumar Gala 
48a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
49a4c9e328SKumar Gala 
50a4c9e328SKumar Gala /* XXX kill that some day ... */
51a4c9e328SKumar Gala int global_phb_number;		/* Global phb counter */
52a4c9e328SKumar Gala 
53a4c9e328SKumar Gala extern struct list_head hose_list;
54a4c9e328SKumar Gala 
55a4c9e328SKumar Gala /*
56a4c9e328SKumar Gala  * pci_controller(phb) initialized common variables.
57a4c9e328SKumar Gala  */
58a4c9e328SKumar Gala static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
59a4c9e328SKumar Gala {
60a4c9e328SKumar Gala 	memset(hose, 0, sizeof(struct pci_controller));
61a4c9e328SKumar Gala 
62a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
63a4c9e328SKumar Gala 	hose->global_number = global_phb_number++;
64a4c9e328SKumar Gala 	list_add_tail(&hose->list_node, &hose_list);
65a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
66a4c9e328SKumar Gala }
67a4c9e328SKumar Gala 
68*2d5f5659SLinas Vepstas struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
69a4c9e328SKumar Gala {
70a4c9e328SKumar Gala 	struct pci_controller *phb;
71a4c9e328SKumar Gala 
72*2d5f5659SLinas Vepstas 	phb = alloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
73a4c9e328SKumar Gala 	if (phb == NULL)
74a4c9e328SKumar Gala 		return NULL;
75a4c9e328SKumar Gala 	pci_setup_pci_controller(phb);
76a4c9e328SKumar Gala 	phb->arch_data = dev;
77a4c9e328SKumar Gala 	phb->is_dynamic = mem_init_done;
78a4c9e328SKumar Gala #ifdef CONFIG_PPC64
79a4c9e328SKumar Gala 	if (dev) {
80a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
81a4c9e328SKumar Gala 
82a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
83a4c9e328SKumar Gala 			nid = -1;
84a4c9e328SKumar Gala 
85a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
86a4c9e328SKumar Gala 	}
87a4c9e328SKumar Gala #endif
88a4c9e328SKumar Gala 	return phb;
89a4c9e328SKumar Gala }
90a4c9e328SKumar Gala 
91a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
92a4c9e328SKumar Gala {
93a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
94a4c9e328SKumar Gala 	list_del(&phb->list_node);
95a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
96a4c9e328SKumar Gala 
97a4c9e328SKumar Gala 	if (phb->is_dynamic)
98a4c9e328SKumar Gala 		kfree(phb);
99a4c9e328SKumar Gala }
100a4c9e328SKumar Gala 
1016dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
1026dfbde20SBenjamin Herrenschmidt {
1036dfbde20SBenjamin Herrenschmidt 	int ret = 0;
1046dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
1056dfbde20SBenjamin Herrenschmidt 	unsigned long size;
1066dfbde20SBenjamin Herrenschmidt 
1076dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
1086dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
1096dfbde20SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
1106dfbde20SBenjamin Herrenschmidt 		size = hose->pci_io_size;
1116dfbde20SBenjamin Herrenschmidt #else
1126dfbde20SBenjamin Herrenschmidt 		size = hose->io_resource.end - hose->io_resource.start + 1;
1136dfbde20SBenjamin Herrenschmidt #endif
1146dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
1156dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
1166dfbde20SBenjamin Herrenschmidt 			ret = 1;
1176dfbde20SBenjamin Herrenschmidt 			break;
1186dfbde20SBenjamin Herrenschmidt 		}
1196dfbde20SBenjamin Herrenschmidt 	}
1206dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
1216dfbde20SBenjamin Herrenschmidt 	return ret;
1226dfbde20SBenjamin Herrenschmidt }
1236dfbde20SBenjamin Herrenschmidt 
1245516b540SKumar Gala /*
1255516b540SKumar Gala  * Return the domain number for this bus.
1265516b540SKumar Gala  */
1275516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
1285516b540SKumar Gala {
1295516b540SKumar Gala 	if (firmware_has_feature(FW_FEATURE_ISERIES))
1305516b540SKumar Gala 		return 0;
1315516b540SKumar Gala 	else {
1325516b540SKumar Gala 		struct pci_controller *hose = pci_bus_to_host(bus);
1335516b540SKumar Gala 
1345516b540SKumar Gala 		return hose->global_number;
1355516b540SKumar Gala 	}
1365516b540SKumar Gala }
1375516b540SKumar Gala 
1385516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
13958083dadSKumar Gala 
14058083dadSKumar Gala #ifdef CONFIG_PPC_OF
141a4c9e328SKumar Gala 
142a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
143a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
144a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
145a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
146a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
147a4c9e328SKumar Gala  * config cycles.
148a4c9e328SKumar Gala  */
149a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
150a4c9e328SKumar Gala {
151a4c9e328SKumar Gala 	if (!have_of)
152a4c9e328SKumar Gala 		return NULL;
153a4c9e328SKumar Gala 	while(node) {
154a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
155a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
156a4c9e328SKumar Gala 			if (hose->arch_data == node)
157a4c9e328SKumar Gala 				return hose;
158a4c9e328SKumar Gala 		node = node->parent;
159a4c9e328SKumar Gala 	}
160a4c9e328SKumar Gala 	return NULL;
161a4c9e328SKumar Gala }
162a4c9e328SKumar Gala 
16358083dadSKumar Gala static ssize_t pci_show_devspec(struct device *dev,
16458083dadSKumar Gala 		struct device_attribute *attr, char *buf)
16558083dadSKumar Gala {
16658083dadSKumar Gala 	struct pci_dev *pdev;
16758083dadSKumar Gala 	struct device_node *np;
16858083dadSKumar Gala 
16958083dadSKumar Gala 	pdev = to_pci_dev (dev);
17058083dadSKumar Gala 	np = pci_device_to_OF_node(pdev);
17158083dadSKumar Gala 	if (np == NULL || np->full_name == NULL)
17258083dadSKumar Gala 		return 0;
17358083dadSKumar Gala 	return sprintf(buf, "%s", np->full_name);
17458083dadSKumar Gala }
17558083dadSKumar Gala static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
17658083dadSKumar Gala #endif /* CONFIG_PPC_OF */
17758083dadSKumar Gala 
17858083dadSKumar Gala /* Add sysfs properties */
1794f3731daSTony Breeds int pcibios_add_platform_entries(struct pci_dev *pdev)
18058083dadSKumar Gala {
18158083dadSKumar Gala #ifdef CONFIG_PPC_OF
1824f3731daSTony Breeds 	return device_create_file(&pdev->dev, &dev_attr_devspec);
1834f3731daSTony Breeds #else
1844f3731daSTony Breeds 	return 0;
18558083dadSKumar Gala #endif /* CONFIG_PPC_OF */
1864f3731daSTony Breeds 
18758083dadSKumar Gala }
18858083dadSKumar Gala 
189a2b7390aSStephen Rothwell char __devinit *pcibios_setup(char *str)
19058083dadSKumar Gala {
19158083dadSKumar Gala 	return str;
19258083dadSKumar Gala }
19358083dadSKumar Gala 
19458083dadSKumar Gala /*
19558083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
19658083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
19758083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
19858083dadSKumar Gala  */
19958083dadSKumar Gala int pci_read_irq_line(struct pci_dev *pci_dev)
20058083dadSKumar Gala {
20158083dadSKumar Gala 	struct of_irq oirq;
20258083dadSKumar Gala 	unsigned int virq;
20358083dadSKumar Gala 
20458083dadSKumar Gala 	DBG("Try to map irq for %s...\n", pci_name(pci_dev));
20558083dadSKumar Gala 
20658083dadSKumar Gala #ifdef DEBUG
20758083dadSKumar Gala 	memset(&oirq, 0xff, sizeof(oirq));
20858083dadSKumar Gala #endif
20958083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
21058083dadSKumar Gala 	if (of_irq_map_pci(pci_dev, &oirq)) {
21158083dadSKumar Gala 		u8 line, pin;
21258083dadSKumar Gala 
21358083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
21458083dadSKumar Gala 		 * space and map that through the default controller. We
21558083dadSKumar Gala 		 * also set the type to level low since that's what PCI
21658083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
21758083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
21858083dadSKumar Gala 		 * function.
21958083dadSKumar Gala 		 */
22058083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
22158083dadSKumar Gala 			return -1;
22258083dadSKumar Gala 		if (pin == 0)
22358083dadSKumar Gala 			return -1;
22458083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
22558083dadSKumar Gala 		    line == 0xff) {
22658083dadSKumar Gala 			return -1;
22758083dadSKumar Gala 		}
22858083dadSKumar Gala 		DBG(" -> no map ! Using irq line %d from PCI config\n", line);
22958083dadSKumar Gala 
23058083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
23158083dadSKumar Gala 		if (virq != NO_IRQ)
23258083dadSKumar Gala 			set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
23358083dadSKumar Gala 	} else {
23458083dadSKumar Gala 		DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
23558083dadSKumar Gala 		    oirq.size, oirq.specifier[0], oirq.specifier[1],
23658083dadSKumar Gala 		    oirq.controller->full_name);
23758083dadSKumar Gala 
23858083dadSKumar Gala 		virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
23958083dadSKumar Gala 					     oirq.size);
24058083dadSKumar Gala 	}
24158083dadSKumar Gala 	if(virq == NO_IRQ) {
24258083dadSKumar Gala 		DBG(" -> failed to map !\n");
24358083dadSKumar Gala 		return -1;
24458083dadSKumar Gala 	}
24558083dadSKumar Gala 
24658083dadSKumar Gala 	DBG(" -> mapped to linux irq %d\n", virq);
24758083dadSKumar Gala 
24858083dadSKumar Gala 	pci_dev->irq = virq;
24958083dadSKumar Gala 
25058083dadSKumar Gala 	return 0;
25158083dadSKumar Gala }
25258083dadSKumar Gala EXPORT_SYMBOL(pci_read_irq_line);
25358083dadSKumar Gala 
25458083dadSKumar Gala /*
25558083dadSKumar Gala  * Platform support for /proc/bus/pci/X/Y mmap()s,
25658083dadSKumar Gala  * modelled on the sparc64 implementation by Dave Miller.
25758083dadSKumar Gala  *  -- paulus.
25858083dadSKumar Gala  */
25958083dadSKumar Gala 
26058083dadSKumar Gala /*
26158083dadSKumar Gala  * Adjust vm_pgoff of VMA such that it is the physical page offset
26258083dadSKumar Gala  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
26358083dadSKumar Gala  *
26458083dadSKumar Gala  * Basically, the user finds the base address for his device which he wishes
26558083dadSKumar Gala  * to mmap.  They read the 32-bit value from the config space base register,
26658083dadSKumar Gala  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
26758083dadSKumar Gala  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
26858083dadSKumar Gala  *
26958083dadSKumar Gala  * Returns negative error code on failure, zero on success.
27058083dadSKumar Gala  */
27158083dadSKumar Gala static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
27258083dadSKumar Gala 					       resource_size_t *offset,
27358083dadSKumar Gala 					       enum pci_mmap_state mmap_state)
27458083dadSKumar Gala {
27558083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
27658083dadSKumar Gala 	unsigned long io_offset = 0;
27758083dadSKumar Gala 	int i, res_bit;
27858083dadSKumar Gala 
27958083dadSKumar Gala 	if (hose == 0)
28058083dadSKumar Gala 		return NULL;		/* should never happen */
28158083dadSKumar Gala 
28258083dadSKumar Gala 	/* If memory, add on the PCI bridge address offset */
28358083dadSKumar Gala 	if (mmap_state == pci_mmap_mem) {
28458083dadSKumar Gala #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
28558083dadSKumar Gala 		*offset += hose->pci_mem_offset;
28658083dadSKumar Gala #endif
28758083dadSKumar Gala 		res_bit = IORESOURCE_MEM;
28858083dadSKumar Gala 	} else {
28958083dadSKumar Gala 		io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
29058083dadSKumar Gala 		*offset += io_offset;
29158083dadSKumar Gala 		res_bit = IORESOURCE_IO;
29258083dadSKumar Gala 	}
29358083dadSKumar Gala 
29458083dadSKumar Gala 	/*
29558083dadSKumar Gala 	 * Check that the offset requested corresponds to one of the
29658083dadSKumar Gala 	 * resources of the device.
29758083dadSKumar Gala 	 */
29858083dadSKumar Gala 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
29958083dadSKumar Gala 		struct resource *rp = &dev->resource[i];
30058083dadSKumar Gala 		int flags = rp->flags;
30158083dadSKumar Gala 
30258083dadSKumar Gala 		/* treat ROM as memory (should be already) */
30358083dadSKumar Gala 		if (i == PCI_ROM_RESOURCE)
30458083dadSKumar Gala 			flags |= IORESOURCE_MEM;
30558083dadSKumar Gala 
30658083dadSKumar Gala 		/* Active and same type? */
30758083dadSKumar Gala 		if ((flags & res_bit) == 0)
30858083dadSKumar Gala 			continue;
30958083dadSKumar Gala 
31058083dadSKumar Gala 		/* In the range of this resource? */
31158083dadSKumar Gala 		if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
31258083dadSKumar Gala 			continue;
31358083dadSKumar Gala 
31458083dadSKumar Gala 		/* found it! construct the final physical address */
31558083dadSKumar Gala 		if (mmap_state == pci_mmap_io)
31658083dadSKumar Gala 			*offset += hose->io_base_phys - io_offset;
31758083dadSKumar Gala 		return rp;
31858083dadSKumar Gala 	}
31958083dadSKumar Gala 
32058083dadSKumar Gala 	return NULL;
32158083dadSKumar Gala }
32258083dadSKumar Gala 
32358083dadSKumar Gala /*
32458083dadSKumar Gala  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
32558083dadSKumar Gala  * device mapping.
32658083dadSKumar Gala  */
32758083dadSKumar Gala static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
32858083dadSKumar Gala 				      pgprot_t protection,
32958083dadSKumar Gala 				      enum pci_mmap_state mmap_state,
33058083dadSKumar Gala 				      int write_combine)
33158083dadSKumar Gala {
33258083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
33358083dadSKumar Gala 
33458083dadSKumar Gala 	/* Write combine is always 0 on non-memory space mappings. On
33558083dadSKumar Gala 	 * memory space, if the user didn't pass 1, we check for a
33658083dadSKumar Gala 	 * "prefetchable" resource. This is a bit hackish, but we use
33758083dadSKumar Gala 	 * this to workaround the inability of /sysfs to provide a write
33858083dadSKumar Gala 	 * combine bit
33958083dadSKumar Gala 	 */
34058083dadSKumar Gala 	if (mmap_state != pci_mmap_mem)
34158083dadSKumar Gala 		write_combine = 0;
34258083dadSKumar Gala 	else if (write_combine == 0) {
34358083dadSKumar Gala 		if (rp->flags & IORESOURCE_PREFETCH)
34458083dadSKumar Gala 			write_combine = 1;
34558083dadSKumar Gala 	}
34658083dadSKumar Gala 
34758083dadSKumar Gala 	/* XXX would be nice to have a way to ask for write-through */
34858083dadSKumar Gala 	prot |= _PAGE_NO_CACHE;
34958083dadSKumar Gala 	if (write_combine)
35058083dadSKumar Gala 		prot &= ~_PAGE_GUARDED;
35158083dadSKumar Gala 	else
35258083dadSKumar Gala 		prot |= _PAGE_GUARDED;
35358083dadSKumar Gala 
35458083dadSKumar Gala 	return __pgprot(prot);
35558083dadSKumar Gala }
35658083dadSKumar Gala 
35758083dadSKumar Gala /*
35858083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
35958083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
36058083dadSKumar Gala  * above routine
36158083dadSKumar Gala  */
36258083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
36358083dadSKumar Gala 				  unsigned long pfn,
36458083dadSKumar Gala 				  unsigned long size,
36558083dadSKumar Gala 				  pgprot_t protection)
36658083dadSKumar Gala {
36758083dadSKumar Gala 	struct pci_dev *pdev = NULL;
36858083dadSKumar Gala 	struct resource *found = NULL;
36958083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
37058083dadSKumar Gala 	unsigned long offset = pfn << PAGE_SHIFT;
37158083dadSKumar Gala 	int i;
37258083dadSKumar Gala 
37358083dadSKumar Gala 	if (page_is_ram(pfn))
37458083dadSKumar Gala 		return __pgprot(prot);
37558083dadSKumar Gala 
37658083dadSKumar Gala 	prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
37758083dadSKumar Gala 
37858083dadSKumar Gala 	for_each_pci_dev(pdev) {
37958083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
38058083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
38158083dadSKumar Gala 			int flags = rp->flags;
38258083dadSKumar Gala 
38358083dadSKumar Gala 			/* Active and same type? */
38458083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
38558083dadSKumar Gala 				continue;
38658083dadSKumar Gala 			/* In the range of this resource? */
38758083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
38858083dadSKumar Gala 			    offset > rp->end)
38958083dadSKumar Gala 				continue;
39058083dadSKumar Gala 			found = rp;
39158083dadSKumar Gala 			break;
39258083dadSKumar Gala 		}
39358083dadSKumar Gala 		if (found)
39458083dadSKumar Gala 			break;
39558083dadSKumar Gala 	}
39658083dadSKumar Gala 	if (found) {
39758083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
39858083dadSKumar Gala 			prot &= ~_PAGE_GUARDED;
39958083dadSKumar Gala 		pci_dev_put(pdev);
40058083dadSKumar Gala 	}
40158083dadSKumar Gala 
40258083dadSKumar Gala 	DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
40358083dadSKumar Gala 
40458083dadSKumar Gala 	return __pgprot(prot);
40558083dadSKumar Gala }
40658083dadSKumar Gala 
40758083dadSKumar Gala 
40858083dadSKumar Gala /*
40958083dadSKumar Gala  * Perform the actual remap of the pages for a PCI device mapping, as
41058083dadSKumar Gala  * appropriate for this architecture.  The region in the process to map
41158083dadSKumar Gala  * is described by vm_start and vm_end members of VMA, the base physical
41258083dadSKumar Gala  * address is found in vm_pgoff.
41358083dadSKumar Gala  * The pci device structure is provided so that architectures may make mapping
41458083dadSKumar Gala  * decisions on a per-device or per-bus basis.
41558083dadSKumar Gala  *
41658083dadSKumar Gala  * Returns a negative error code on failure, zero on success.
41758083dadSKumar Gala  */
41858083dadSKumar Gala int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
41958083dadSKumar Gala 			enum pci_mmap_state mmap_state, int write_combine)
42058083dadSKumar Gala {
42158083dadSKumar Gala 	resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
42258083dadSKumar Gala 	struct resource *rp;
42358083dadSKumar Gala 	int ret;
42458083dadSKumar Gala 
42558083dadSKumar Gala 	rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
42658083dadSKumar Gala 	if (rp == NULL)
42758083dadSKumar Gala 		return -EINVAL;
42858083dadSKumar Gala 
42958083dadSKumar Gala 	vma->vm_pgoff = offset >> PAGE_SHIFT;
43058083dadSKumar Gala 	vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
43158083dadSKumar Gala 						  vma->vm_page_prot,
43258083dadSKumar Gala 						  mmap_state, write_combine);
43358083dadSKumar Gala 
43458083dadSKumar Gala 	ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
43558083dadSKumar Gala 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
43658083dadSKumar Gala 
43758083dadSKumar Gala 	return ret;
43858083dadSKumar Gala }
43958083dadSKumar Gala 
44058083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
44158083dadSKumar Gala 			  const struct resource *rsrc,
44258083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
44358083dadSKumar Gala {
44458083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
44558083dadSKumar Gala 	resource_size_t offset = 0;
44658083dadSKumar Gala 
44758083dadSKumar Gala 	if (hose == NULL)
44858083dadSKumar Gala 		return;
44958083dadSKumar Gala 
45058083dadSKumar Gala 	if (rsrc->flags & IORESOURCE_IO)
45158083dadSKumar Gala 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
45258083dadSKumar Gala 
45358083dadSKumar Gala 	/* We pass a fully fixed up address to userland for MMIO instead of
45458083dadSKumar Gala 	 * a BAR value because X is lame and expects to be able to use that
45558083dadSKumar Gala 	 * to pass to /dev/mem !
45658083dadSKumar Gala 	 *
45758083dadSKumar Gala 	 * That means that we'll have potentially 64 bits values where some
45858083dadSKumar Gala 	 * userland apps only expect 32 (like X itself since it thinks only
45958083dadSKumar Gala 	 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
46058083dadSKumar Gala 	 * 32 bits CHRPs :-(
46158083dadSKumar Gala 	 *
46258083dadSKumar Gala 	 * Hopefully, the sysfs insterface is immune to that gunk. Once X
46358083dadSKumar Gala 	 * has been fixed (and the fix spread enough), we can re-enable the
46458083dadSKumar Gala 	 * 2 lines below and pass down a BAR value to userland. In that case
46558083dadSKumar Gala 	 * we'll also have to re-enable the matching code in
46658083dadSKumar Gala 	 * __pci_mmap_make_offset().
46758083dadSKumar Gala 	 *
46858083dadSKumar Gala 	 * BenH.
46958083dadSKumar Gala 	 */
47058083dadSKumar Gala #if 0
47158083dadSKumar Gala 	else if (rsrc->flags & IORESOURCE_MEM)
47258083dadSKumar Gala 		offset = hose->pci_mem_offset;
47358083dadSKumar Gala #endif
47458083dadSKumar Gala 
47558083dadSKumar Gala 	*start = rsrc->start - offset;
47658083dadSKumar Gala 	*end = rsrc->end - offset;
47758083dadSKumar Gala }
478