io-workarounds.c 4.3 KB

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