io-workarounds.c 4.8 KB
Newer Older
1
/*
2 3
 * Support PCI IO workaround
 *
4 5
 *  Copyright (C) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>
 *		       IBM, Corp.
6
 *  (C) Copyright 2007-2008 TOSHIBA CORPORATION
7 8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#undef DEBUG

#include <linux/kernel.h>
15
#include <linux/sched.h>	/* for init_mm */
16

17 18
#include <asm/io.h>
#include <asm/machdep.h>
19
#include <asm/pgtable.h>
20
#include <asm/ppc-pci.h>
21
#include <asm/io-workarounds.h>
22

23
#define IOWA_MAX_BUS	8
24

25 26
static struct iowa_bus iowa_busses[IOWA_MAX_BUS];
static unsigned int iowa_bus_count;
27

28 29 30 31 32
static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr)
{
	int i, j;
	struct resource *res;
	unsigned long vstart, vend;
33

34 35 36
	for (i = 0; i < iowa_bus_count; i++) {
		struct iowa_bus *bus = &iowa_busses[i];
		struct pci_controller *phb = bus->phb;
37

38 39 40 41 42 43 44 45 46 47 48 49 50
		if (vaddr) {
			vstart = (unsigned long)phb->io_base_virt;
			vend = vstart + phb->pci_io_size - 1;
			if ((vaddr >= vstart) && (vaddr <= vend))
				return bus;
		}

		if (paddr)
			for (j = 0; j < 3; j++) {
				res = &phb->mem_resources[j];
				if (paddr >= res->start && paddr <= res->end)
					return bus;
			}
51
	}
52

53 54 55
	return NULL;
}

56
#ifdef CONFIG_PPC_INDIRECT_MMIO
57
struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
58
{
59
	unsigned hugepage_shift;
60
	struct iowa_bus *bus;
61 62 63 64
	int token;

	token = PCI_GET_ADDR_TOKEN(addr);

65 66
	if (token && token <= iowa_bus_count)
		bus = &iowa_busses[token - 1];
67 68 69 70 71
	else {
		unsigned long vaddr, paddr;
		pte_t *ptep;

		vaddr = (unsigned long)PCI_FIX_ADDR(addr);
72 73
		if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
			return NULL;
74 75 76 77 78
		/*
		 * We won't find huge pages here (iomem). Also can't hit
		 * a page table free due to init_mm
		 */
		ptep = __find_linux_pte_or_hugepte(init_mm.pgd, vaddr,
79
						   NULL, &hugepage_shift);
80 81
		if (ptep == NULL)
			paddr = 0;
82 83
		else {
			WARN_ON(hugepage_shift);
84
			paddr = pte_pfn(*ptep) << PAGE_SHIFT;
85
		}
86
		bus = iowa_pci_find(vaddr, paddr);
87 88

		if (bus == NULL)
89
			return NULL;
90 91
	}

92
	return bus;
93
}
94 95 96 97 98 99
#else /* CONFIG_PPC_INDIRECT_MMIO */
struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
{
	return NULL;
}
#endif /* !CONFIG_PPC_INDIRECT_MMIO */
100

101
#ifdef CONFIG_PPC_INDIRECT_PIO
102
struct iowa_bus *iowa_pio_find_bus(unsigned long port)
103
{
104 105
	unsigned long vaddr = (unsigned long)pci_io_base + port;
	return iowa_pci_find(vaddr, 0);
106
}
107 108 109 110 111 112
#else
struct iowa_bus *iowa_pio_find_bus(unsigned long port)
{
	return NULL;
}
#endif
113

114 115 116 117 118 119 120 121
#define DEF_PCI_AC_RET(name, ret, at, al, space, aa)		\
static ret iowa_##name at					\
{								\
	struct iowa_bus *bus;					\
	bus = iowa_##space##_find_bus(aa);			\
	if (bus && bus->ops && bus->ops->name)			\
		return bus->ops->name al;			\
	return __do_##name al;					\
122 123
}

124 125 126 127 128 129 130 131 132 133
#define DEF_PCI_AC_NORET(name, at, al, space, aa)		\
static void iowa_##name at					\
{								\
	struct iowa_bus *bus;					\
	bus = iowa_##space##_find_bus(aa);			\
	if (bus && bus->ops && bus->ops->name) {		\
		bus->ops->name al;				\
		return;						\
	}							\
	__do_##name al;						\
134 135
}

136
#include <asm/io-defs.h>
137

138 139
#undef DEF_PCI_AC_RET
#undef DEF_PCI_AC_NORET
140

141
static const struct ppc_pci_io iowa_pci_io = {
142

143 144
#define DEF_PCI_AC_RET(name, ret, at, al, space, aa)	.name = iowa_##name,
#define DEF_PCI_AC_NORET(name, at, al, space, aa)	.name = iowa_##name,
145

146
#include <asm/io-defs.h>
147

148 149
#undef DEF_PCI_AC_RET
#undef DEF_PCI_AC_NORET
150

151
};
152

153
#ifdef CONFIG_PPC_INDIRECT_MMIO
154
static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
155
				  unsigned long flags, void *caller)
156
{
157
	struct iowa_bus *bus;
158
	void __iomem *res = __ioremap_caller(addr, size, flags, caller);
159 160
	int busno;

161
	bus = iowa_pci_find(0, (unsigned long)addr);
162
	if (bus != NULL) {
163
		busno = bus - iowa_busses;
164 165 166 167
		PCI_SET_ADDR_TOKEN(res, busno + 1);
	}
	return res;
}
168 169 170
#else /* CONFIG_PPC_INDIRECT_MMIO */
#define iowa_ioremap NULL
#endif /* !CONFIG_PPC_INDIRECT_MMIO */
171

172
/* Enable IO workaround */
173
static void io_workaround_init(void)
174 175 176 177 178 179 180 181 182 183 184
{
	static int io_workaround_inited;

	if (io_workaround_inited)
		return;
	ppc_pci_io = iowa_pci_io;
	ppc_md.ioremap = iowa_ioremap;
	io_workaround_inited = 1;
}

/* Register new bus to support workaround */
185 186
void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops,
		       int (*initfunc)(struct iowa_bus *, void *), void *data)
187
{
188
	struct iowa_bus *bus;
189
	struct device_node *np = phb->dn;
190

191 192
	io_workaround_init();

193 194 195
	if (iowa_bus_count >= IOWA_MAX_BUS) {
		pr_err("IOWA:Too many pci bridges, "
		       "workarounds disabled for %s\n", np->full_name);
196 197 198
		return;
	}

199 200 201
	bus = &iowa_busses[iowa_bus_count];
	bus->phb = phb;
	bus->ops = ops;
202
	bus->private = data;
203

204 205 206
	if (initfunc)
		if ((*initfunc)(bus, data))
			return;
207

208
	iowa_bus_count++;
209

210
	pr_debug("IOWA:[%d]Add bus, %s.\n", iowa_bus_count-1, np->full_name);
211 212
}