io-workarounds.c 4.2 KB

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