xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision a4c9e328279d55622d56507629d6b8942e8cc9c9)
15516b540SKumar Gala /*
25516b540SKumar Gala  * Contains common pci routines for ALL ppc platform
35516b540SKumar Gala  *
45516b540SKumar Gala  * This program is free software; you can redistribute it and/or
55516b540SKumar Gala  * modify it under the terms of the GNU General Public License
65516b540SKumar Gala  * as published by the Free Software Foundation; either version
75516b540SKumar Gala  * 2 of the License, or (at your option) any later version.
85516b540SKumar Gala  */
95516b540SKumar Gala 
105516b540SKumar Gala #undef DEBUG
115516b540SKumar Gala 
125516b540SKumar Gala #include <linux/kernel.h>
135516b540SKumar Gala #include <linux/pci.h>
145516b540SKumar Gala #include <linux/string.h>
155516b540SKumar Gala #include <linux/init.h>
165516b540SKumar Gala #include <linux/bootmem.h>
175516b540SKumar Gala #include <linux/mm.h>
185516b540SKumar Gala #include <linux/list.h>
195516b540SKumar Gala #include <linux/syscalls.h>
205516b540SKumar Gala #include <linux/irq.h>
215516b540SKumar Gala #include <linux/vmalloc.h>
225516b540SKumar Gala 
235516b540SKumar Gala #include <asm/processor.h>
245516b540SKumar Gala #include <asm/io.h>
255516b540SKumar Gala #include <asm/prom.h>
265516b540SKumar Gala #include <asm/pci-bridge.h>
275516b540SKumar Gala #include <asm/byteorder.h>
285516b540SKumar Gala #include <asm/machdep.h>
295516b540SKumar Gala #include <asm/ppc-pci.h>
305516b540SKumar Gala #include <asm/firmware.h>
315516b540SKumar Gala 
325516b540SKumar Gala #ifdef DEBUG
335516b540SKumar Gala #include <asm/udbg.h>
345516b540SKumar Gala #define DBG(fmt...) printk(fmt)
355516b540SKumar Gala #else
365516b540SKumar Gala #define DBG(fmt...)
375516b540SKumar Gala #endif
385516b540SKumar Gala 
39*a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
40*a4c9e328SKumar Gala 
41*a4c9e328SKumar Gala /* XXX kill that some day ... */
42*a4c9e328SKumar Gala int global_phb_number;		/* Global phb counter */
43*a4c9e328SKumar Gala 
44*a4c9e328SKumar Gala extern struct list_head hose_list;
45*a4c9e328SKumar Gala 
46*a4c9e328SKumar Gala /*
47*a4c9e328SKumar Gala  * pci_controller(phb) initialized common variables.
48*a4c9e328SKumar Gala  */
49*a4c9e328SKumar Gala static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
50*a4c9e328SKumar Gala {
51*a4c9e328SKumar Gala 	memset(hose, 0, sizeof(struct pci_controller));
52*a4c9e328SKumar Gala 
53*a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
54*a4c9e328SKumar Gala 	hose->global_number = global_phb_number++;
55*a4c9e328SKumar Gala 	list_add_tail(&hose->list_node, &hose_list);
56*a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
57*a4c9e328SKumar Gala }
58*a4c9e328SKumar Gala 
59*a4c9e328SKumar Gala struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
60*a4c9e328SKumar Gala {
61*a4c9e328SKumar Gala 	struct pci_controller *phb;
62*a4c9e328SKumar Gala 
63*a4c9e328SKumar Gala 	if (mem_init_done)
64*a4c9e328SKumar Gala 		phb = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
65*a4c9e328SKumar Gala 	else
66*a4c9e328SKumar Gala 		phb = alloc_bootmem(sizeof (struct pci_controller));
67*a4c9e328SKumar Gala 	if (phb == NULL)
68*a4c9e328SKumar Gala 		return NULL;
69*a4c9e328SKumar Gala 	pci_setup_pci_controller(phb);
70*a4c9e328SKumar Gala 	phb->arch_data = dev;
71*a4c9e328SKumar Gala 	phb->is_dynamic = mem_init_done;
72*a4c9e328SKumar Gala #ifdef CONFIG_PPC64
73*a4c9e328SKumar Gala 	if (dev) {
74*a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
75*a4c9e328SKumar Gala 
76*a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
77*a4c9e328SKumar Gala 			nid = -1;
78*a4c9e328SKumar Gala 
79*a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
80*a4c9e328SKumar Gala 	}
81*a4c9e328SKumar Gala #endif
82*a4c9e328SKumar Gala 	return phb;
83*a4c9e328SKumar Gala }
84*a4c9e328SKumar Gala 
85*a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
86*a4c9e328SKumar Gala {
87*a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
88*a4c9e328SKumar Gala 	list_del(&phb->list_node);
89*a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
90*a4c9e328SKumar Gala 
91*a4c9e328SKumar Gala 	if (phb->is_dynamic)
92*a4c9e328SKumar Gala 		kfree(phb);
93*a4c9e328SKumar Gala }
94*a4c9e328SKumar Gala 
955516b540SKumar Gala /*
965516b540SKumar Gala  * Return the domain number for this bus.
975516b540SKumar Gala  */
985516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
995516b540SKumar Gala {
1005516b540SKumar Gala 	if (firmware_has_feature(FW_FEATURE_ISERIES))
1015516b540SKumar Gala 		return 0;
1025516b540SKumar Gala 	else {
1035516b540SKumar Gala 		struct pci_controller *hose = pci_bus_to_host(bus);
1045516b540SKumar Gala 
1055516b540SKumar Gala 		return hose->global_number;
1065516b540SKumar Gala 	}
1075516b540SKumar Gala }
1085516b540SKumar Gala 
1095516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
11058083dadSKumar Gala 
11158083dadSKumar Gala #ifdef CONFIG_PPC_OF
112*a4c9e328SKumar Gala 
113*a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
114*a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
115*a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
116*a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
117*a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
118*a4c9e328SKumar Gala  * config cycles.
119*a4c9e328SKumar Gala  */
120*a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
121*a4c9e328SKumar Gala {
122*a4c9e328SKumar Gala 	if (!have_of)
123*a4c9e328SKumar Gala 		return NULL;
124*a4c9e328SKumar Gala 	while(node) {
125*a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
126*a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
127*a4c9e328SKumar Gala 			if (hose->arch_data == node)
128*a4c9e328SKumar Gala 				return hose;
129*a4c9e328SKumar Gala 		node = node->parent;
130*a4c9e328SKumar Gala 	}
131*a4c9e328SKumar Gala 	return NULL;
132*a4c9e328SKumar Gala }
133*a4c9e328SKumar Gala 
13458083dadSKumar Gala static ssize_t pci_show_devspec(struct device *dev,
13558083dadSKumar Gala 		struct device_attribute *attr, char *buf)
13658083dadSKumar Gala {
13758083dadSKumar Gala 	struct pci_dev *pdev;
13858083dadSKumar Gala 	struct device_node *np;
13958083dadSKumar Gala 
14058083dadSKumar Gala 	pdev = to_pci_dev (dev);
14158083dadSKumar Gala 	np = pci_device_to_OF_node(pdev);
14258083dadSKumar Gala 	if (np == NULL || np->full_name == NULL)
14358083dadSKumar Gala 		return 0;
14458083dadSKumar Gala 	return sprintf(buf, "%s", np->full_name);
14558083dadSKumar Gala }
14658083dadSKumar Gala static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
14758083dadSKumar Gala #endif /* CONFIG_PPC_OF */
14858083dadSKumar Gala 
14958083dadSKumar Gala /* Add sysfs properties */
15058083dadSKumar Gala void pcibios_add_platform_entries(struct pci_dev *pdev)
15158083dadSKumar Gala {
15258083dadSKumar Gala #ifdef CONFIG_PPC_OF
15358083dadSKumar Gala 	device_create_file(&pdev->dev, &dev_attr_devspec);
15458083dadSKumar Gala #endif /* CONFIG_PPC_OF */
15558083dadSKumar Gala }
15658083dadSKumar Gala 
15758083dadSKumar Gala char __init *pcibios_setup(char *str)
15858083dadSKumar Gala {
15958083dadSKumar Gala 	return str;
16058083dadSKumar Gala }
16158083dadSKumar Gala 
16258083dadSKumar Gala /*
16358083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
16458083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
16558083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
16658083dadSKumar Gala  */
16758083dadSKumar Gala int pci_read_irq_line(struct pci_dev *pci_dev)
16858083dadSKumar Gala {
16958083dadSKumar Gala 	struct of_irq oirq;
17058083dadSKumar Gala 	unsigned int virq;
17158083dadSKumar Gala 
17258083dadSKumar Gala 	DBG("Try to map irq for %s...\n", pci_name(pci_dev));
17358083dadSKumar Gala 
17458083dadSKumar Gala #ifdef DEBUG
17558083dadSKumar Gala 	memset(&oirq, 0xff, sizeof(oirq));
17658083dadSKumar Gala #endif
17758083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
17858083dadSKumar Gala 	if (of_irq_map_pci(pci_dev, &oirq)) {
17958083dadSKumar Gala 		u8 line, pin;
18058083dadSKumar Gala 
18158083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
18258083dadSKumar Gala 		 * space and map that through the default controller. We
18358083dadSKumar Gala 		 * also set the type to level low since that's what PCI
18458083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
18558083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
18658083dadSKumar Gala 		 * function.
18758083dadSKumar Gala 		 */
18858083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
18958083dadSKumar Gala 			return -1;
19058083dadSKumar Gala 		if (pin == 0)
19158083dadSKumar Gala 			return -1;
19258083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
19358083dadSKumar Gala 		    line == 0xff) {
19458083dadSKumar Gala 			return -1;
19558083dadSKumar Gala 		}
19658083dadSKumar Gala 		DBG(" -> no map ! Using irq line %d from PCI config\n", line);
19758083dadSKumar Gala 
19858083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
19958083dadSKumar Gala 		if (virq != NO_IRQ)
20058083dadSKumar Gala 			set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
20158083dadSKumar Gala 	} else {
20258083dadSKumar Gala 		DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
20358083dadSKumar Gala 		    oirq.size, oirq.specifier[0], oirq.specifier[1],
20458083dadSKumar Gala 		    oirq.controller->full_name);
20558083dadSKumar Gala 
20658083dadSKumar Gala 		virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
20758083dadSKumar Gala 					     oirq.size);
20858083dadSKumar Gala 	}
20958083dadSKumar Gala 	if(virq == NO_IRQ) {
21058083dadSKumar Gala 		DBG(" -> failed to map !\n");
21158083dadSKumar Gala 		return -1;
21258083dadSKumar Gala 	}
21358083dadSKumar Gala 
21458083dadSKumar Gala 	DBG(" -> mapped to linux irq %d\n", virq);
21558083dadSKumar Gala 
21658083dadSKumar Gala 	pci_dev->irq = virq;
21758083dadSKumar Gala 
21858083dadSKumar Gala 	return 0;
21958083dadSKumar Gala }
22058083dadSKumar Gala EXPORT_SYMBOL(pci_read_irq_line);
22158083dadSKumar Gala 
22258083dadSKumar Gala /*
22358083dadSKumar Gala  * Platform support for /proc/bus/pci/X/Y mmap()s,
22458083dadSKumar Gala  * modelled on the sparc64 implementation by Dave Miller.
22558083dadSKumar Gala  *  -- paulus.
22658083dadSKumar Gala  */
22758083dadSKumar Gala 
22858083dadSKumar Gala /*
22958083dadSKumar Gala  * Adjust vm_pgoff of VMA such that it is the physical page offset
23058083dadSKumar Gala  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
23158083dadSKumar Gala  *
23258083dadSKumar Gala  * Basically, the user finds the base address for his device which he wishes
23358083dadSKumar Gala  * to mmap.  They read the 32-bit value from the config space base register,
23458083dadSKumar Gala  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
23558083dadSKumar Gala  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
23658083dadSKumar Gala  *
23758083dadSKumar Gala  * Returns negative error code on failure, zero on success.
23858083dadSKumar Gala  */
23958083dadSKumar Gala static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
24058083dadSKumar Gala 					       resource_size_t *offset,
24158083dadSKumar Gala 					       enum pci_mmap_state mmap_state)
24258083dadSKumar Gala {
24358083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
24458083dadSKumar Gala 	unsigned long io_offset = 0;
24558083dadSKumar Gala 	int i, res_bit;
24658083dadSKumar Gala 
24758083dadSKumar Gala 	if (hose == 0)
24858083dadSKumar Gala 		return NULL;		/* should never happen */
24958083dadSKumar Gala 
25058083dadSKumar Gala 	/* If memory, add on the PCI bridge address offset */
25158083dadSKumar Gala 	if (mmap_state == pci_mmap_mem) {
25258083dadSKumar Gala #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
25358083dadSKumar Gala 		*offset += hose->pci_mem_offset;
25458083dadSKumar Gala #endif
25558083dadSKumar Gala 		res_bit = IORESOURCE_MEM;
25658083dadSKumar Gala 	} else {
25758083dadSKumar Gala 		io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
25858083dadSKumar Gala 		*offset += io_offset;
25958083dadSKumar Gala 		res_bit = IORESOURCE_IO;
26058083dadSKumar Gala 	}
26158083dadSKumar Gala 
26258083dadSKumar Gala 	/*
26358083dadSKumar Gala 	 * Check that the offset requested corresponds to one of the
26458083dadSKumar Gala 	 * resources of the device.
26558083dadSKumar Gala 	 */
26658083dadSKumar Gala 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
26758083dadSKumar Gala 		struct resource *rp = &dev->resource[i];
26858083dadSKumar Gala 		int flags = rp->flags;
26958083dadSKumar Gala 
27058083dadSKumar Gala 		/* treat ROM as memory (should be already) */
27158083dadSKumar Gala 		if (i == PCI_ROM_RESOURCE)
27258083dadSKumar Gala 			flags |= IORESOURCE_MEM;
27358083dadSKumar Gala 
27458083dadSKumar Gala 		/* Active and same type? */
27558083dadSKumar Gala 		if ((flags & res_bit) == 0)
27658083dadSKumar Gala 			continue;
27758083dadSKumar Gala 
27858083dadSKumar Gala 		/* In the range of this resource? */
27958083dadSKumar Gala 		if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
28058083dadSKumar Gala 			continue;
28158083dadSKumar Gala 
28258083dadSKumar Gala 		/* found it! construct the final physical address */
28358083dadSKumar Gala 		if (mmap_state == pci_mmap_io)
28458083dadSKumar Gala 			*offset += hose->io_base_phys - io_offset;
28558083dadSKumar Gala 		return rp;
28658083dadSKumar Gala 	}
28758083dadSKumar Gala 
28858083dadSKumar Gala 	return NULL;
28958083dadSKumar Gala }
29058083dadSKumar Gala 
29158083dadSKumar Gala /*
29258083dadSKumar Gala  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
29358083dadSKumar Gala  * device mapping.
29458083dadSKumar Gala  */
29558083dadSKumar Gala static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
29658083dadSKumar Gala 				      pgprot_t protection,
29758083dadSKumar Gala 				      enum pci_mmap_state mmap_state,
29858083dadSKumar Gala 				      int write_combine)
29958083dadSKumar Gala {
30058083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
30158083dadSKumar Gala 
30258083dadSKumar Gala 	/* Write combine is always 0 on non-memory space mappings. On
30358083dadSKumar Gala 	 * memory space, if the user didn't pass 1, we check for a
30458083dadSKumar Gala 	 * "prefetchable" resource. This is a bit hackish, but we use
30558083dadSKumar Gala 	 * this to workaround the inability of /sysfs to provide a write
30658083dadSKumar Gala 	 * combine bit
30758083dadSKumar Gala 	 */
30858083dadSKumar Gala 	if (mmap_state != pci_mmap_mem)
30958083dadSKumar Gala 		write_combine = 0;
31058083dadSKumar Gala 	else if (write_combine == 0) {
31158083dadSKumar Gala 		if (rp->flags & IORESOURCE_PREFETCH)
31258083dadSKumar Gala 			write_combine = 1;
31358083dadSKumar Gala 	}
31458083dadSKumar Gala 
31558083dadSKumar Gala 	/* XXX would be nice to have a way to ask for write-through */
31658083dadSKumar Gala 	prot |= _PAGE_NO_CACHE;
31758083dadSKumar Gala 	if (write_combine)
31858083dadSKumar Gala 		prot &= ~_PAGE_GUARDED;
31958083dadSKumar Gala 	else
32058083dadSKumar Gala 		prot |= _PAGE_GUARDED;
32158083dadSKumar Gala 
32258083dadSKumar Gala 	return __pgprot(prot);
32358083dadSKumar Gala }
32458083dadSKumar Gala 
32558083dadSKumar Gala /*
32658083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
32758083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
32858083dadSKumar Gala  * above routine
32958083dadSKumar Gala  */
33058083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
33158083dadSKumar Gala 				  unsigned long pfn,
33258083dadSKumar Gala 				  unsigned long size,
33358083dadSKumar Gala 				  pgprot_t protection)
33458083dadSKumar Gala {
33558083dadSKumar Gala 	struct pci_dev *pdev = NULL;
33658083dadSKumar Gala 	struct resource *found = NULL;
33758083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
33858083dadSKumar Gala 	unsigned long offset = pfn << PAGE_SHIFT;
33958083dadSKumar Gala 	int i;
34058083dadSKumar Gala 
34158083dadSKumar Gala 	if (page_is_ram(pfn))
34258083dadSKumar Gala 		return __pgprot(prot);
34358083dadSKumar Gala 
34458083dadSKumar Gala 	prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
34558083dadSKumar Gala 
34658083dadSKumar Gala 	for_each_pci_dev(pdev) {
34758083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
34858083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
34958083dadSKumar Gala 			int flags = rp->flags;
35058083dadSKumar Gala 
35158083dadSKumar Gala 			/* Active and same type? */
35258083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
35358083dadSKumar Gala 				continue;
35458083dadSKumar Gala 			/* In the range of this resource? */
35558083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
35658083dadSKumar Gala 			    offset > rp->end)
35758083dadSKumar Gala 				continue;
35858083dadSKumar Gala 			found = rp;
35958083dadSKumar Gala 			break;
36058083dadSKumar Gala 		}
36158083dadSKumar Gala 		if (found)
36258083dadSKumar Gala 			break;
36358083dadSKumar Gala 	}
36458083dadSKumar Gala 	if (found) {
36558083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
36658083dadSKumar Gala 			prot &= ~_PAGE_GUARDED;
36758083dadSKumar Gala 		pci_dev_put(pdev);
36858083dadSKumar Gala 	}
36958083dadSKumar Gala 
37058083dadSKumar Gala 	DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
37158083dadSKumar Gala 
37258083dadSKumar Gala 	return __pgprot(prot);
37358083dadSKumar Gala }
37458083dadSKumar Gala 
37558083dadSKumar Gala 
37658083dadSKumar Gala /*
37758083dadSKumar Gala  * Perform the actual remap of the pages for a PCI device mapping, as
37858083dadSKumar Gala  * appropriate for this architecture.  The region in the process to map
37958083dadSKumar Gala  * is described by vm_start and vm_end members of VMA, the base physical
38058083dadSKumar Gala  * address is found in vm_pgoff.
38158083dadSKumar Gala  * The pci device structure is provided so that architectures may make mapping
38258083dadSKumar Gala  * decisions on a per-device or per-bus basis.
38358083dadSKumar Gala  *
38458083dadSKumar Gala  * Returns a negative error code on failure, zero on success.
38558083dadSKumar Gala  */
38658083dadSKumar Gala int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
38758083dadSKumar Gala 			enum pci_mmap_state mmap_state, int write_combine)
38858083dadSKumar Gala {
38958083dadSKumar Gala 	resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
39058083dadSKumar Gala 	struct resource *rp;
39158083dadSKumar Gala 	int ret;
39258083dadSKumar Gala 
39358083dadSKumar Gala 	rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
39458083dadSKumar Gala 	if (rp == NULL)
39558083dadSKumar Gala 		return -EINVAL;
39658083dadSKumar Gala 
39758083dadSKumar Gala 	vma->vm_pgoff = offset >> PAGE_SHIFT;
39858083dadSKumar Gala 	vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
39958083dadSKumar Gala 						  vma->vm_page_prot,
40058083dadSKumar Gala 						  mmap_state, write_combine);
40158083dadSKumar Gala 
40258083dadSKumar Gala 	ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
40358083dadSKumar Gala 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
40458083dadSKumar Gala 
40558083dadSKumar Gala 	return ret;
40658083dadSKumar Gala }
40758083dadSKumar Gala 
40858083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
40958083dadSKumar Gala 			  const struct resource *rsrc,
41058083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
41158083dadSKumar Gala {
41258083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
41358083dadSKumar Gala 	resource_size_t offset = 0;
41458083dadSKumar Gala 
41558083dadSKumar Gala 	if (hose == NULL)
41658083dadSKumar Gala 		return;
41758083dadSKumar Gala 
41858083dadSKumar Gala 	if (rsrc->flags & IORESOURCE_IO)
41958083dadSKumar Gala 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
42058083dadSKumar Gala 
42158083dadSKumar Gala 	/* We pass a fully fixed up address to userland for MMIO instead of
42258083dadSKumar Gala 	 * a BAR value because X is lame and expects to be able to use that
42358083dadSKumar Gala 	 * to pass to /dev/mem !
42458083dadSKumar Gala 	 *
42558083dadSKumar Gala 	 * That means that we'll have potentially 64 bits values where some
42658083dadSKumar Gala 	 * userland apps only expect 32 (like X itself since it thinks only
42758083dadSKumar Gala 	 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
42858083dadSKumar Gala 	 * 32 bits CHRPs :-(
42958083dadSKumar Gala 	 *
43058083dadSKumar Gala 	 * Hopefully, the sysfs insterface is immune to that gunk. Once X
43158083dadSKumar Gala 	 * has been fixed (and the fix spread enough), we can re-enable the
43258083dadSKumar Gala 	 * 2 lines below and pass down a BAR value to userland. In that case
43358083dadSKumar Gala 	 * we'll also have to re-enable the matching code in
43458083dadSKumar Gala 	 * __pci_mmap_make_offset().
43558083dadSKumar Gala 	 *
43658083dadSKumar Gala 	 * BenH.
43758083dadSKumar Gala 	 */
43858083dadSKumar Gala #if 0
43958083dadSKumar Gala 	else if (rsrc->flags & IORESOURCE_MEM)
44058083dadSKumar Gala 		offset = hose->pci_mem_offset;
44158083dadSKumar Gala #endif
44258083dadSKumar Gala 
44358083dadSKumar Gala 	*start = rsrc->start - offset;
44458083dadSKumar Gala 	*end = rsrc->end - offset;
44558083dadSKumar Gala }
446