io-workarounds.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support PCI IO workaround
  4. *
  5. * Copyright (C) 2006 Benjamin Herrenschmidt <[email protected]>
  6. * IBM, Corp.
  7. * (C) Copyright 2007-2008 TOSHIBA CORPORATION
  8. */
  9. #undef DEBUG
  10. #include <linux/kernel.h>
  11. #include <linux/sched/mm.h> /* for init_mm */
  12. #include <linux/pgtable.h>
  13. #include <asm/io.h>
  14. #include <asm/machdep.h>
  15. #include <asm/ppc-pci.h>
  16. #include <asm/io-workarounds.h>
  17. #include <asm/pte-walk.h>
  18. #define IOWA_MAX_BUS 8
  19. static struct iowa_bus iowa_busses[IOWA_MAX_BUS];
  20. static unsigned int iowa_bus_count;
  21. static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr)
  22. {
  23. int i, j;
  24. struct resource *res;
  25. unsigned long vstart, vend;
  26. for (i = 0; i < iowa_bus_count; i++) {
  27. struct iowa_bus *bus = &iowa_busses[i];
  28. struct pci_controller *phb = bus->phb;
  29. if (vaddr) {
  30. vstart = (unsigned long)phb->io_base_virt;
  31. vend = vstart + phb->pci_io_size - 1;
  32. if ((vaddr >= vstart) && (vaddr <= vend))
  33. return bus;
  34. }
  35. if (paddr)
  36. for (j = 0; j < 3; j++) {
  37. res = &phb->mem_resources[j];
  38. if (paddr >= res->start && paddr <= res->end)
  39. return bus;
  40. }
  41. }
  42. return NULL;
  43. }
  44. #ifdef CONFIG_PPC_INDIRECT_MMIO
  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. vaddr = (unsigned long)PCI_FIX_ADDR(addr);
  55. if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
  56. return NULL;
  57. paddr = ppc_find_vmap_phys(vaddr);
  58. bus = iowa_pci_find(vaddr, paddr);
  59. if (bus == NULL)
  60. return NULL;
  61. }
  62. return bus;
  63. }
  64. #else /* CONFIG_PPC_INDIRECT_MMIO */
  65. struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
  66. {
  67. return NULL;
  68. }
  69. #endif /* !CONFIG_PPC_INDIRECT_MMIO */
  70. #ifdef CONFIG_PPC_INDIRECT_PIO
  71. struct iowa_bus *iowa_pio_find_bus(unsigned long port)
  72. {
  73. unsigned long vaddr = (unsigned long)pci_io_base + port;
  74. return iowa_pci_find(vaddr, 0);
  75. }
  76. #else
  77. struct iowa_bus *iowa_pio_find_bus(unsigned long port)
  78. {
  79. return NULL;
  80. }
  81. #endif
  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. #ifdef CONFIG_PPC_INDIRECT_MMIO
  113. void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
  114. pgprot_t prot, void *caller)
  115. {
  116. struct iowa_bus *bus;
  117. void __iomem *res = __ioremap_caller(addr, size, prot, caller);
  118. int busno;
  119. bus = iowa_pci_find(0, (unsigned long)addr);
  120. if (bus != NULL) {
  121. busno = bus - iowa_busses;
  122. PCI_SET_ADDR_TOKEN(res, busno + 1);
  123. }
  124. return res;
  125. }
  126. #endif /* !CONFIG_PPC_INDIRECT_MMIO */
  127. bool io_workaround_inited;
  128. /* Enable IO workaround */
  129. static void io_workaround_init(void)
  130. {
  131. if (io_workaround_inited)
  132. return;
  133. ppc_pci_io = iowa_pci_io;
  134. io_workaround_inited = true;
  135. }
  136. /* Register new bus to support workaround */
  137. void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops,
  138. int (*initfunc)(struct iowa_bus *, void *), void *data)
  139. {
  140. struct iowa_bus *bus;
  141. struct device_node *np = phb->dn;
  142. io_workaround_init();
  143. if (iowa_bus_count >= IOWA_MAX_BUS) {
  144. pr_err("IOWA:Too many pci bridges, "
  145. "workarounds disabled for %pOF\n", np);
  146. return;
  147. }
  148. bus = &iowa_busses[iowa_bus_count];
  149. bus->phb = phb;
  150. bus->ops = ops;
  151. bus->private = data;
  152. if (initfunc)
  153. if ((*initfunc)(bus, data))
  154. return;
  155. iowa_bus_count++;
  156. pr_debug("IOWA:[%d]Add bus, %pOF.\n", iowa_bus_count-1, np);
  157. }