io-workarounds.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <asm/io.h>
  15. #include <asm/machdep.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/ppc-pci.h>
  18. #include "io-workarounds.h"
  19. #define IOWA_MAX_BUS 8
  20. static struct iowa_bus iowa_busses[IOWA_MAX_BUS];
  21. static unsigned int iowa_bus_count;
  22. static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr)
  23. {
  24. int i, j;
  25. struct resource *res;
  26. unsigned long vstart, vend;
  27. for (i = 0; i < iowa_bus_count; i++) {
  28. struct iowa_bus *bus = &iowa_busses[i];
  29. struct pci_controller *phb = bus->phb;
  30. if (vaddr) {
  31. vstart = (unsigned long)phb->io_base_virt;
  32. vend = vstart + phb->pci_io_size - 1;
  33. if ((vaddr >= vstart) && (vaddr <= vend))
  34. return bus;
  35. }
  36. if (paddr)
  37. for (j = 0; j < 3; j++) {
  38. res = &phb->mem_resources[j];
  39. if (paddr >= res->start && paddr <= res->end)
  40. return bus;
  41. }
  42. }
  43. return NULL;
  44. }
  45. struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
  46. {
  47. struct iowa_bus *bus;
  48. int token;
  49. token = PCI_GET_ADDR_TOKEN(addr);
  50. if (token && token <= iowa_bus_count)
  51. bus = &iowa_busses[token - 1];
  52. else {
  53. unsigned long vaddr, paddr;
  54. pte_t *ptep;
  55. vaddr = (unsigned long)PCI_FIX_ADDR(addr);
  56. if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
  57. return NULL;
  58. ptep = find_linux_pte(init_mm.pgd, vaddr);
  59. if (ptep == NULL)
  60. paddr = 0;
  61. else
  62. paddr = pte_pfn(*ptep) << PAGE_SHIFT;
  63. bus = iowa_pci_find(vaddr, paddr);
  64. if (bus == NULL)
  65. return NULL;
  66. }
  67. return bus;
  68. }
  69. struct iowa_bus *iowa_pio_find_bus(unsigned long port)
  70. {
  71. unsigned long vaddr = (unsigned long)pci_io_base + port;
  72. return iowa_pci_find(vaddr, 0);
  73. }
  74. #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \
  75. static ret iowa_##name at \
  76. { \
  77. struct iowa_bus *bus; \
  78. bus = iowa_##space##_find_bus(aa); \
  79. if (bus && bus->ops && bus->ops->name) \
  80. return bus->ops->name al; \
  81. return __do_##name al; \
  82. }
  83. #define DEF_PCI_AC_NORET(name, at, al, space, aa) \
  84. static void iowa_##name at \
  85. { \
  86. struct iowa_bus *bus; \
  87. bus = iowa_##space##_find_bus(aa); \
  88. if (bus && bus->ops && bus->ops->name) { \
  89. bus->ops->name al; \
  90. return; \
  91. } \
  92. __do_##name al; \
  93. }
  94. #include <asm/io-defs.h>
  95. #undef DEF_PCI_AC_RET
  96. #undef DEF_PCI_AC_NORET
  97. static const struct ppc_pci_io __devinitconst iowa_pci_io = {
  98. #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name,
  99. #define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name,
  100. #include <asm/io-defs.h>
  101. #undef DEF_PCI_AC_RET
  102. #undef DEF_PCI_AC_NORET
  103. };
  104. static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
  105. unsigned long flags, void *caller)
  106. {
  107. struct iowa_bus *bus;
  108. void __iomem *res = __ioremap_caller(addr, size, flags, caller);
  109. int busno;
  110. bus = iowa_pci_find(0, (unsigned long)addr);
  111. if (bus != NULL) {
  112. busno = bus - iowa_busses;
  113. PCI_SET_ADDR_TOKEN(res, busno + 1);
  114. }
  115. return res;
  116. }
  117. /* Regist new bus to support workaround */
  118. void __devinit iowa_register_bus(struct pci_controller *phb,
  119. struct ppc_pci_io *ops,
  120. int (*initfunc)(struct iowa_bus *, void *), void *data)
  121. {
  122. struct iowa_bus *bus;
  123. struct device_node *np = phb->dn;
  124. if (iowa_bus_count >= IOWA_MAX_BUS) {
  125. pr_err("IOWA:Too many pci bridges, "
  126. "workarounds disabled for %s\n", np->full_name);
  127. return;
  128. }
  129. bus = &iowa_busses[iowa_bus_count];
  130. bus->phb = phb;
  131. bus->ops = ops;
  132. if (initfunc)
  133. if ((*initfunc)(bus, data))
  134. return;
  135. iowa_bus_count++;
  136. pr_debug("IOWA:[%d]Add bus, %s.\n", iowa_bus_count-1, np->full_name);
  137. }
  138. /* enable IO workaround */
  139. void __devinit io_workaround_init(void)
  140. {
  141. static int io_workaround_inited;
  142. if (io_workaround_inited)
  143. return;
  144. ppc_pci_io = iowa_pci_io;
  145. ppc_md.ioremap = iowa_ioremap;
  146. io_workaround_inited = 1;
  147. }