io-workarounds.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. ptep = find_linux_pte_or_hugepte(init_mm.pgd, vaddr,
  62. &hugepage_shift);
  63. if (ptep == NULL)
  64. paddr = 0;
  65. else {
  66. /*
  67. * we don't have hugepages backing iomem
  68. */
  69. WARN_ON(hugepage_shift);
  70. paddr = pte_pfn(*ptep) << PAGE_SHIFT;
  71. }
  72. bus = iowa_pci_find(vaddr, paddr);
  73. if (bus == NULL)
  74. return NULL;
  75. }
  76. return bus;
  77. }
  78. #else /* CONFIG_PPC_INDIRECT_MMIO */
  79. struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
  80. {
  81. return NULL;
  82. }
  83. #endif /* !CONFIG_PPC_INDIRECT_MMIO */
  84. #ifdef CONFIG_PPC_INDIRECT_PIO
  85. struct iowa_bus *iowa_pio_find_bus(unsigned long port)
  86. {
  87. unsigned long vaddr = (unsigned long)pci_io_base + port;
  88. return iowa_pci_find(vaddr, 0);
  89. }
  90. #else
  91. struct iowa_bus *iowa_pio_find_bus(unsigned long port)
  92. {
  93. return NULL;
  94. }
  95. #endif
  96. #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \
  97. static ret iowa_##name at \
  98. { \
  99. struct iowa_bus *bus; \
  100. bus = iowa_##space##_find_bus(aa); \
  101. if (bus && bus->ops && bus->ops->name) \
  102. return bus->ops->name al; \
  103. return __do_##name al; \
  104. }
  105. #define DEF_PCI_AC_NORET(name, at, al, space, aa) \
  106. static void iowa_##name at \
  107. { \
  108. struct iowa_bus *bus; \
  109. bus = iowa_##space##_find_bus(aa); \
  110. if (bus && bus->ops && bus->ops->name) { \
  111. bus->ops->name al; \
  112. return; \
  113. } \
  114. __do_##name al; \
  115. }
  116. #include <asm/io-defs.h>
  117. #undef DEF_PCI_AC_RET
  118. #undef DEF_PCI_AC_NORET
  119. static const struct ppc_pci_io iowa_pci_io = {
  120. #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name,
  121. #define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name,
  122. #include <asm/io-defs.h>
  123. #undef DEF_PCI_AC_RET
  124. #undef DEF_PCI_AC_NORET
  125. };
  126. #ifdef CONFIG_PPC_INDIRECT_MMIO
  127. static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
  128. unsigned long flags, void *caller)
  129. {
  130. struct iowa_bus *bus;
  131. void __iomem *res = __ioremap_caller(addr, size, flags, caller);
  132. int busno;
  133. bus = iowa_pci_find(0, (unsigned long)addr);
  134. if (bus != NULL) {
  135. busno = bus - iowa_busses;
  136. PCI_SET_ADDR_TOKEN(res, busno + 1);
  137. }
  138. return res;
  139. }
  140. #else /* CONFIG_PPC_INDIRECT_MMIO */
  141. #define iowa_ioremap NULL
  142. #endif /* !CONFIG_PPC_INDIRECT_MMIO */
  143. /* Enable IO workaround */
  144. static void io_workaround_init(void)
  145. {
  146. static int io_workaround_inited;
  147. if (io_workaround_inited)
  148. return;
  149. ppc_pci_io = iowa_pci_io;
  150. ppc_md.ioremap = iowa_ioremap;
  151. io_workaround_inited = 1;
  152. }
  153. /* Register new bus to support workaround */
  154. void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops,
  155. int (*initfunc)(struct iowa_bus *, void *), void *data)
  156. {
  157. struct iowa_bus *bus;
  158. struct device_node *np = phb->dn;
  159. io_workaround_init();
  160. if (iowa_bus_count >= IOWA_MAX_BUS) {
  161. pr_err("IOWA:Too many pci bridges, "
  162. "workarounds disabled for %s\n", np->full_name);
  163. return;
  164. }
  165. bus = &iowa_busses[iowa_bus_count];
  166. bus->phb = phb;
  167. bus->ops = ops;
  168. bus->private = data;
  169. if (initfunc)
  170. if ((*initfunc)(bus, data))
  171. return;
  172. iowa_bus_count++;
  173. pr_debug("IOWA:[%d]Add bus, %s.\n", iowa_bus_count-1, np->full_name);
  174. }