io-workarounds.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Support PCI IO workaround
  3. *
  4. * Copyright (C) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  5. * IBM, Corp.
  6. * (C) Copyright 2007-2008 TOSHIBA CORPORATION
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #undef DEBUG
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h> /* for init_mm */
  15. #include <asm/io.h>
  16. #include <asm/machdep.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/ppc-pci.h>
  19. #include <asm/io-workarounds.h>
  20. #define IOWA_MAX_BUS 8
  21. static struct iowa_bus iowa_busses[IOWA_MAX_BUS];
  22. static unsigned int iowa_bus_count;
  23. static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr)
  24. {
  25. int i, j;
  26. struct resource *res;
  27. unsigned long vstart, vend;
  28. for (i = 0; i < iowa_bus_count; i++) {
  29. struct iowa_bus *bus = &iowa_busses[i];
  30. struct pci_controller *phb = bus->phb;
  31. if (vaddr) {
  32. vstart = (unsigned long)phb->io_base_virt;
  33. vend = vstart + phb->pci_io_size - 1;
  34. if ((vaddr >= vstart) && (vaddr <= vend))
  35. return bus;
  36. }
  37. if (paddr)
  38. for (j = 0; j < 3; j++) {
  39. res = &phb->mem_resources[j];
  40. if (paddr >= res->start && paddr <= res->end)
  41. return bus;
  42. }
  43. }
  44. return NULL;
  45. }
  46. #ifdef CONFIG_PPC_INDIRECT_MMIO
  47. struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
  48. {
  49. unsigned hugepage_shift;
  50. struct iowa_bus *bus;
  51. int token;
  52. token = PCI_GET_ADDR_TOKEN(addr);
  53. if (token && token <= iowa_bus_count)
  54. bus = &iowa_busses[token - 1];
  55. else {
  56. unsigned long vaddr, paddr;
  57. pte_t *ptep;
  58. vaddr = (unsigned long)PCI_FIX_ADDR(addr);
  59. if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
  60. return NULL;
  61. /*
  62. * We won't find huge pages here (iomem). Also can't hit
  63. * a page table free due to init_mm
  64. */
  65. ptep = __find_linux_pte_or_hugepte(init_mm.pgd, vaddr,
  66. &hugepage_shift);
  67. if (ptep == NULL)
  68. paddr = 0;
  69. else {
  70. WARN_ON(hugepage_shift);
  71. paddr = pte_pfn(*ptep) << PAGE_SHIFT;
  72. }
  73. bus = iowa_pci_find(vaddr, paddr);
  74. if (bus == NULL)
  75. return NULL;
  76. }
  77. return bus;
  78. }
  79. #else /* CONFIG_PPC_INDIRECT_MMIO */
  80. struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
  81. {
  82. return NULL;
  83. }
  84. #endif /* !CONFIG_PPC_INDIRECT_MMIO */
  85. #ifdef CONFIG_PPC_INDIRECT_PIO
  86. struct iowa_bus *iowa_pio_find_bus(unsigned long port)
  87. {
  88. unsigned long vaddr = (unsigned long)pci_io_base + port;
  89. return iowa_pci_find(vaddr, 0);
  90. }
  91. #else
  92. struct iowa_bus *iowa_pio_find_bus(unsigned long port)
  93. {
  94. return NULL;
  95. }
  96. #endif
  97. #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \
  98. static ret iowa_##name at \
  99. { \
  100. struct iowa_bus *bus; \
  101. bus = iowa_##space##_find_bus(aa); \
  102. if (bus && bus->ops && bus->ops->name) \
  103. return bus->ops->name al; \
  104. return __do_##name al; \
  105. }
  106. #define DEF_PCI_AC_NORET(name, at, al, space, aa) \
  107. static void iowa_##name at \
  108. { \
  109. struct iowa_bus *bus; \
  110. bus = iowa_##space##_find_bus(aa); \
  111. if (bus && bus->ops && bus->ops->name) { \
  112. bus->ops->name al; \
  113. return; \
  114. } \
  115. __do_##name al; \
  116. }
  117. #include <asm/io-defs.h>
  118. #undef DEF_PCI_AC_RET
  119. #undef DEF_PCI_AC_NORET
  120. static const struct ppc_pci_io iowa_pci_io = {
  121. #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name,
  122. #define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name,
  123. #include <asm/io-defs.h>
  124. #undef DEF_PCI_AC_RET
  125. #undef DEF_PCI_AC_NORET
  126. };
  127. #ifdef CONFIG_PPC_INDIRECT_MMIO
  128. static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
  129. unsigned long flags, void *caller)
  130. {
  131. struct iowa_bus *bus;
  132. void __iomem *res = __ioremap_caller(addr, size, flags, caller);
  133. int busno;
  134. bus = iowa_pci_find(0, (unsigned long)addr);
  135. if (bus != NULL) {
  136. busno = bus - iowa_busses;
  137. PCI_SET_ADDR_TOKEN(res, busno + 1);
  138. }
  139. return res;
  140. }
  141. #else /* CONFIG_PPC_INDIRECT_MMIO */
  142. #define iowa_ioremap NULL
  143. #endif /* !CONFIG_PPC_INDIRECT_MMIO */
  144. /* Enable IO workaround */
  145. static void io_workaround_init(void)
  146. {
  147. static int io_workaround_inited;
  148. if (io_workaround_inited)
  149. return;
  150. ppc_pci_io = iowa_pci_io;
  151. ppc_md.ioremap = iowa_ioremap;
  152. io_workaround_inited = 1;
  153. }
  154. /* Register new bus to support workaround */
  155. void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops,
  156. int (*initfunc)(struct iowa_bus *, void *), void *data)
  157. {
  158. struct iowa_bus *bus;
  159. struct device_node *np = phb->dn;
  160. io_workaround_init();
  161. if (iowa_bus_count >= IOWA_MAX_BUS) {
  162. pr_err("IOWA:Too many pci bridges, "
  163. "workarounds disabled for %s\n", np->full_name);
  164. return;
  165. }
  166. bus = &iowa_busses[iowa_bus_count];
  167. bus->phb = phb;
  168. bus->ops = ops;
  169. bus->private = data;
  170. if (initfunc)
  171. if ((*initfunc)(bus, data))
  172. return;
  173. iowa_bus_count++;
  174. pr_debug("IOWA:[%d]Add bus, %s.\n", iowa_bus_count-1, np->full_name);
  175. }