io.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * {read,write}{b,w,l,q} based on arch/arm64/include/asm/io.h
  4. * which was based on arch/arm/include/io.h
  5. *
  6. * Copyright (C) 1996-2000 Russell King
  7. * Copyright (C) 2012 ARM Ltd.
  8. * Copyright (C) 2014 Regents of the University of California
  9. */
  10. #ifndef _ASM_RISCV_IO_H
  11. #define _ASM_RISCV_IO_H
  12. #include <linux/types.h>
  13. #include <linux/pgtable.h>
  14. #include <asm/mmiowb.h>
  15. #include <asm/early_ioremap.h>
  16. /*
  17. * MMIO access functions are separated out to break dependency cycles
  18. * when using {read,write}* fns in low-level headers
  19. */
  20. #include <asm/mmio.h>
  21. /*
  22. * I/O port access constants.
  23. */
  24. #ifdef CONFIG_MMU
  25. #define IO_SPACE_LIMIT (PCI_IO_SIZE - 1)
  26. #define PCI_IOBASE ((void __iomem *)PCI_IO_START)
  27. #endif /* CONFIG_MMU */
  28. /*
  29. * Emulation routines for the port-mapped IO space used by some PCI drivers.
  30. * These are defined as being "fully synchronous", but also "not guaranteed to
  31. * be fully ordered with respect to other memory and I/O operations". We're
  32. * going to be on the safe side here and just make them:
  33. * - Fully ordered WRT each other, by bracketing them with two fences. The
  34. * outer set contains both I/O so inX is ordered with outX, while the inner just
  35. * needs the type of the access (I for inX and O for outX).
  36. * - Ordered in the same manner as readX/writeX WRT memory by subsuming their
  37. * fences.
  38. * - Ordered WRT timer reads, so udelay and friends don't get elided by the
  39. * implementation.
  40. * Note that there is no way to actually enforce that outX is a non-posted
  41. * operation on RISC-V, but hopefully the timer ordering constraint is
  42. * sufficient to ensure this works sanely on controllers that support I/O
  43. * writes.
  44. */
  45. #define __io_pbr() __asm__ __volatile__ ("fence io,i" : : : "memory");
  46. #define __io_par(v) __asm__ __volatile__ ("fence i,ior" : : : "memory");
  47. #define __io_pbw() __asm__ __volatile__ ("fence iow,o" : : : "memory");
  48. #define __io_paw() __asm__ __volatile__ ("fence o,io" : : : "memory");
  49. /*
  50. * Accesses from a single hart to a single I/O address must be ordered. This
  51. * allows us to use the raw read macros, but we still need to fence before and
  52. * after the block to ensure ordering WRT other macros. These are defined to
  53. * perform host-endian accesses so we use __raw instead of __cpu.
  54. */
  55. #define __io_reads_ins(port, ctype, len, bfence, afence) \
  56. static inline void __ ## port ## len(const volatile void __iomem *addr, \
  57. void *buffer, \
  58. unsigned int count) \
  59. { \
  60. bfence; \
  61. if (count) { \
  62. ctype *buf = buffer; \
  63. \
  64. do { \
  65. ctype x = __raw_read ## len(addr); \
  66. *buf++ = x; \
  67. } while (--count); \
  68. } \
  69. afence; \
  70. }
  71. #define __io_writes_outs(port, ctype, len, bfence, afence) \
  72. static inline void __ ## port ## len(volatile void __iomem *addr, \
  73. const void *buffer, \
  74. unsigned int count) \
  75. { \
  76. bfence; \
  77. if (count) { \
  78. const ctype *buf = buffer; \
  79. \
  80. do { \
  81. __raw_write ## len(*buf++, addr); \
  82. } while (--count); \
  83. } \
  84. afence; \
  85. }
  86. __io_reads_ins(reads, u8, b, __io_br(), __io_ar(addr))
  87. __io_reads_ins(reads, u16, w, __io_br(), __io_ar(addr))
  88. __io_reads_ins(reads, u32, l, __io_br(), __io_ar(addr))
  89. #define readsb(addr, buffer, count) __readsb(addr, buffer, count)
  90. #define readsw(addr, buffer, count) __readsw(addr, buffer, count)
  91. #define readsl(addr, buffer, count) __readsl(addr, buffer, count)
  92. __io_reads_ins(ins, u8, b, __io_pbr(), __io_par(addr))
  93. __io_reads_ins(ins, u16, w, __io_pbr(), __io_par(addr))
  94. __io_reads_ins(ins, u32, l, __io_pbr(), __io_par(addr))
  95. #define insb(addr, buffer, count) __insb(PCI_IOBASE + (addr), buffer, count)
  96. #define insw(addr, buffer, count) __insw(PCI_IOBASE + (addr), buffer, count)
  97. #define insl(addr, buffer, count) __insl(PCI_IOBASE + (addr), buffer, count)
  98. __io_writes_outs(writes, u8, b, __io_bw(), __io_aw())
  99. __io_writes_outs(writes, u16, w, __io_bw(), __io_aw())
  100. __io_writes_outs(writes, u32, l, __io_bw(), __io_aw())
  101. #define writesb(addr, buffer, count) __writesb(addr, buffer, count)
  102. #define writesw(addr, buffer, count) __writesw(addr, buffer, count)
  103. #define writesl(addr, buffer, count) __writesl(addr, buffer, count)
  104. __io_writes_outs(outs, u8, b, __io_pbw(), __io_paw())
  105. __io_writes_outs(outs, u16, w, __io_pbw(), __io_paw())
  106. __io_writes_outs(outs, u32, l, __io_pbw(), __io_paw())
  107. #define outsb(addr, buffer, count) __outsb(PCI_IOBASE + (addr), buffer, count)
  108. #define outsw(addr, buffer, count) __outsw(PCI_IOBASE + (addr), buffer, count)
  109. #define outsl(addr, buffer, count) __outsl(PCI_IOBASE + (addr), buffer, count)
  110. #ifdef CONFIG_64BIT
  111. __io_reads_ins(reads, u64, q, __io_br(), __io_ar(addr))
  112. #define readsq(addr, buffer, count) __readsq(addr, buffer, count)
  113. __io_reads_ins(ins, u64, q, __io_pbr(), __io_par(addr))
  114. #define insq(addr, buffer, count) __insq(PCI_IOBASE + (addr), buffer, count)
  115. __io_writes_outs(writes, u64, q, __io_bw(), __io_aw())
  116. #define writesq(addr, buffer, count) __writesq(addr, buffer, count)
  117. __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw())
  118. #define outsq(addr, buffer, count) __outsq(PCI_IOBASE + (addr), buffer, count)
  119. #endif
  120. #include <asm-generic/io.h>
  121. #ifdef CONFIG_MMU
  122. #define arch_memremap_wb(addr, size) \
  123. ((__force void *)ioremap_prot((addr), (size), _PAGE_KERNEL))
  124. #endif
  125. #endif /* _ASM_RISCV_IO_H */