io-workarounds.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  3. * IBM, Corp.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #undef DEBUG
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/pci.h>
  13. #include <asm/io.h>
  14. #include <asm/machdep.h>
  15. #include <asm/pci-bridge.h>
  16. #include <asm/ppc-pci.h>
  17. #define SPIDER_PCI_REG_BASE 0xd000
  18. #define SPIDER_PCI_VCI_CNTL_STAT 0x0110
  19. #define SPIDER_PCI_DUMMY_READ 0x0810
  20. #define SPIDER_PCI_DUMMY_READ_BASE 0x0814
  21. /* Undefine that to re-enable bogus prefetch
  22. *
  23. * Without that workaround, the chip will do bogus prefetch past
  24. * page boundary from system memory. This setting will disable that,
  25. * though the documentation is unclear as to the consequences of doing
  26. * so, either purely performances, or possible misbehaviour... It's not
  27. * clear wether the chip can handle unaligned accesses at all without
  28. * prefetching enabled.
  29. *
  30. * For now, things appear to be behaving properly with that prefetching
  31. * disabled and IDE, possibly because IDE isn't doing any unaligned
  32. * access.
  33. */
  34. #define SPIDER_DISABLE_PREFETCH
  35. #define MAX_SPIDERS 3
  36. static struct spider_pci_bus {
  37. void __iomem *regs;
  38. unsigned long mmio_start;
  39. unsigned long mmio_end;
  40. unsigned long pio_vstart;
  41. unsigned long pio_vend;
  42. } spider_pci_busses[MAX_SPIDERS];
  43. static int spider_pci_count;
  44. static struct spider_pci_bus *spider_pci_find(unsigned long vaddr,
  45. unsigned long paddr)
  46. {
  47. int i;
  48. for (i = 0; i < spider_pci_count; i++) {
  49. struct spider_pci_bus *bus = &spider_pci_busses[i];
  50. if (paddr && paddr >= bus->mmio_start && paddr < bus->mmio_end)
  51. return bus;
  52. if (vaddr && vaddr >= bus->pio_vstart && vaddr < bus->pio_vend)
  53. return bus;
  54. }
  55. return NULL;
  56. }
  57. static void spider_io_flush(const volatile void __iomem *addr)
  58. {
  59. struct spider_pci_bus *bus;
  60. int token;
  61. /* Get platform token (set by ioremap) from address */
  62. token = PCI_GET_ADDR_TOKEN(addr);
  63. /* Fast path if we have a non-0 token, it indicates which bus we
  64. * are on.
  65. *
  66. * If the token is 0, that means either that the ioremap was done
  67. * before we initialized this layer, or it's a PIO operation. We
  68. * fallback to a low path in this case. Hopefully, internal devices
  69. * which are ioremap'ed early should use in_XX/out_XX functions
  70. * instead of the PCI ones and thus not suffer from the slowdown.
  71. *
  72. * Also note that currently, the workaround will not work for areas
  73. * that are not mapped with PTEs (bolted in the hash table). This
  74. * is the case for ioremaps done very early at boot (before
  75. * mem_init_done) and includes the mapping of the ISA IO space.
  76. *
  77. * Fortunately, none of the affected devices is expected to do DMA
  78. * and thus there should be no problem in practice.
  79. *
  80. * In order to improve performances, we only do the PTE search for
  81. * addresses falling in the PHB IO space area. That means it will
  82. * not work for hotplug'ed PHBs but those don't exist with Spider.
  83. */
  84. if (token && token <= spider_pci_count)
  85. bus = &spider_pci_busses[token - 1];
  86. else {
  87. unsigned long vaddr, paddr;
  88. pte_t *ptep;
  89. /* Fixup physical address */
  90. vaddr = (unsigned long)PCI_FIX_ADDR(addr);
  91. /* Check if it's in allowed range for PIO */
  92. if (vaddr < PHB_IO_BASE || vaddr > PHB_IO_END)
  93. return;
  94. /* Try to find a PTE. If not, clear the paddr, we'll do
  95. * a vaddr only lookup (PIO only)
  96. */
  97. ptep = find_linux_pte(init_mm.pgd, vaddr);
  98. if (ptep == NULL)
  99. paddr = 0;
  100. else
  101. paddr = pte_pfn(*ptep) << PAGE_SHIFT;
  102. bus = spider_pci_find(vaddr, paddr);
  103. if (bus == NULL)
  104. return;
  105. }
  106. /* Now do the workaround
  107. */
  108. (void)in_be32(bus->regs + SPIDER_PCI_DUMMY_READ);
  109. }
  110. static u8 spider_readb(const volatile void __iomem *addr)
  111. {
  112. u8 val = __do_readb(addr);
  113. spider_io_flush(addr);
  114. return val;
  115. }
  116. static u16 spider_readw(const volatile void __iomem *addr)
  117. {
  118. u16 val = __do_readw(addr);
  119. spider_io_flush(addr);
  120. return val;
  121. }
  122. static u32 spider_readl(const volatile void __iomem *addr)
  123. {
  124. u32 val = __do_readl(addr);
  125. spider_io_flush(addr);
  126. return val;
  127. }
  128. static u64 spider_readq(const volatile void __iomem *addr)
  129. {
  130. u64 val = __do_readq(addr);
  131. spider_io_flush(addr);
  132. return val;
  133. }
  134. static u16 spider_readw_be(const volatile void __iomem *addr)
  135. {
  136. u16 val = __do_readw_be(addr);
  137. spider_io_flush(addr);
  138. return val;
  139. }
  140. static u32 spider_readl_be(const volatile void __iomem *addr)
  141. {
  142. u32 val = __do_readl_be(addr);
  143. spider_io_flush(addr);
  144. return val;
  145. }
  146. static u64 spider_readq_be(const volatile void __iomem *addr)
  147. {
  148. u64 val = __do_readq_be(addr);
  149. spider_io_flush(addr);
  150. return val;
  151. }
  152. static void spider_readsb(const volatile void __iomem *addr, void *buf,
  153. unsigned long count)
  154. {
  155. __do_readsb(addr, buf, count);
  156. spider_io_flush(addr);
  157. }
  158. static void spider_readsw(const volatile void __iomem *addr, void *buf,
  159. unsigned long count)
  160. {
  161. __do_readsw(addr, buf, count);
  162. spider_io_flush(addr);
  163. }
  164. static void spider_readsl(const volatile void __iomem *addr, void *buf,
  165. unsigned long count)
  166. {
  167. __do_readsl(addr, buf, count);
  168. spider_io_flush(addr);
  169. }
  170. static void spider_memcpy_fromio(void *dest, const volatile void __iomem *src,
  171. unsigned long n)
  172. {
  173. __do_memcpy_fromio(dest, src, n);
  174. spider_io_flush(src);
  175. }
  176. static void __iomem * spider_ioremap(unsigned long addr, unsigned long size,
  177. unsigned long flags)
  178. {
  179. struct spider_pci_bus *bus;
  180. void __iomem *res = __ioremap(addr, size, flags);
  181. int busno;
  182. pr_debug("spider_ioremap(0x%lx, 0x%lx, 0x%lx) -> 0x%p\n",
  183. addr, size, flags, res);
  184. bus = spider_pci_find(0, addr);
  185. if (bus != NULL) {
  186. busno = bus - spider_pci_busses;
  187. pr_debug(" found bus %d, setting token\n", busno);
  188. PCI_SET_ADDR_TOKEN(res, busno + 1);
  189. }
  190. pr_debug(" result=0x%p\n", res);
  191. return res;
  192. }
  193. static void __init spider_pci_setup_chip(struct spider_pci_bus *bus)
  194. {
  195. #ifdef SPIDER_DISABLE_PREFETCH
  196. u32 val = in_be32(bus->regs + SPIDER_PCI_VCI_CNTL_STAT);
  197. pr_debug(" PVCI_Control_Status was 0x%08x\n", val);
  198. out_be32(bus->regs + SPIDER_PCI_VCI_CNTL_STAT, val | 0x8);
  199. #endif
  200. /* Configure the dummy address for the workaround */
  201. out_be32(bus->regs + SPIDER_PCI_DUMMY_READ_BASE, 0x80000000);
  202. }
  203. static void __init spider_pci_add_one(struct pci_controller *phb)
  204. {
  205. struct spider_pci_bus *bus = &spider_pci_busses[spider_pci_count];
  206. struct device_node *np = phb->arch_data;
  207. struct resource rsrc;
  208. void __iomem *regs;
  209. if (spider_pci_count >= MAX_SPIDERS) {
  210. printk(KERN_ERR "Too many spider bridges, workarounds"
  211. " disabled for %s\n", np->full_name);
  212. return;
  213. }
  214. /* Get the registers for the beast */
  215. if (of_address_to_resource(np, 0, &rsrc)) {
  216. printk(KERN_ERR "Failed to get registers for spider %s"
  217. " workarounds disabled\n", np->full_name);
  218. return;
  219. }
  220. /* Mask out some useless bits in there to get to the base of the
  221. * spider chip
  222. */
  223. rsrc.start &= ~0xfffffffful;
  224. /* Map them */
  225. regs = ioremap(rsrc.start + SPIDER_PCI_REG_BASE, 0x1000);
  226. if (regs == NULL) {
  227. printk(KERN_ERR "Failed to map registers for spider %s"
  228. " workarounds disabled\n", np->full_name);
  229. return;
  230. }
  231. spider_pci_count++;
  232. /* We assume spiders only have one MMIO resource */
  233. bus->mmio_start = phb->mem_resources[0].start;
  234. bus->mmio_end = phb->mem_resources[0].end + 1;
  235. bus->pio_vstart = (unsigned long)phb->io_base_virt;
  236. bus->pio_vend = bus->pio_vstart + phb->pci_io_size;
  237. bus->regs = regs;
  238. printk(KERN_INFO "PCI: Spider MMIO workaround for %s\n",np->full_name);
  239. pr_debug(" mmio (P) = 0x%016lx..0x%016lx\n",
  240. bus->mmio_start, bus->mmio_end);
  241. pr_debug(" pio (V) = 0x%016lx..0x%016lx\n",
  242. bus->pio_vstart, bus->pio_vend);
  243. pr_debug(" regs (P) = 0x%016lx (V) = 0x%p\n",
  244. rsrc.start + SPIDER_PCI_REG_BASE, bus->regs);
  245. spider_pci_setup_chip(bus);
  246. }
  247. static struct ppc_pci_io __initdata spider_pci_io = {
  248. .readb = spider_readb,
  249. .readw = spider_readw,
  250. .readl = spider_readl,
  251. .readq = spider_readq,
  252. .readw_be = spider_readw_be,
  253. .readl_be = spider_readl_be,
  254. .readq_be = spider_readq_be,
  255. .readsb = spider_readsb,
  256. .readsw = spider_readsw,
  257. .readsl = spider_readsl,
  258. .memcpy_fromio = spider_memcpy_fromio,
  259. };
  260. static int __init spider_pci_workaround_init(void)
  261. {
  262. struct pci_controller *phb;
  263. if (!machine_is(cell))
  264. return 0;
  265. /* Find spider bridges. We assume they have been all probed
  266. * in setup_arch(). If that was to change, we would need to
  267. * update this code to cope with dynamically added busses
  268. */
  269. list_for_each_entry(phb, &hose_list, list_node) {
  270. struct device_node *np = phb->arch_data;
  271. const char *model = of_get_property(np, "model", NULL);
  272. /* If no model property or name isn't exactly "pci", skip */
  273. if (model == NULL || strcmp(np->name, "pci"))
  274. continue;
  275. /* If model is not "Spider", skip */
  276. if (strcmp(model, "Spider"))
  277. continue;
  278. spider_pci_add_one(phb);
  279. }
  280. /* No Spider PCI found, exit */
  281. if (spider_pci_count == 0)
  282. return 0;
  283. /* Setup IO callbacks. We only setup MMIO reads. PIO reads will
  284. * fallback to MMIO reads (though without a token, thus slower)
  285. */
  286. ppc_pci_io = spider_pci_io;
  287. /* Setup ioremap callback */
  288. ppc_md.ioremap = spider_ioremap;
  289. return 0;
  290. }
  291. arch_initcall(spider_pci_workaround_init);