octeon_mem_ops.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. **********************************************************************/
  19. #include <linux/netdevice.h>
  20. #include "liquidio_common.h"
  21. #include "octeon_droq.h"
  22. #include "octeon_iq.h"
  23. #include "response_manager.h"
  24. #include "octeon_device.h"
  25. #define MEMOPS_IDX MAX_BAR1_MAP_INDEX
  26. #ifdef __BIG_ENDIAN_BITFIELD
  27. static inline void
  28. octeon_toggle_bar1_swapmode(struct octeon_device *oct, u32 idx)
  29. {
  30. u32 mask;
  31. mask = oct->fn_list.bar1_idx_read(oct, idx);
  32. mask = (mask & 0x2) ? (mask & ~2) : (mask | 2);
  33. oct->fn_list.bar1_idx_write(oct, idx, mask);
  34. }
  35. #else
  36. #define octeon_toggle_bar1_swapmode(oct, idx)
  37. #endif
  38. static void
  39. octeon_pci_fastwrite(struct octeon_device *oct, u8 __iomem *mapped_addr,
  40. u8 *hostbuf, u32 len)
  41. {
  42. while ((len) && ((unsigned long)mapped_addr) & 7) {
  43. writeb(*(hostbuf++), mapped_addr++);
  44. len--;
  45. }
  46. octeon_toggle_bar1_swapmode(oct, MEMOPS_IDX);
  47. while (len >= 8) {
  48. writeq(*((u64 *)hostbuf), mapped_addr);
  49. mapped_addr += 8;
  50. hostbuf += 8;
  51. len -= 8;
  52. }
  53. octeon_toggle_bar1_swapmode(oct, MEMOPS_IDX);
  54. while (len--)
  55. writeb(*(hostbuf++), mapped_addr++);
  56. }
  57. static void
  58. octeon_pci_fastread(struct octeon_device *oct, u8 __iomem *mapped_addr,
  59. u8 *hostbuf, u32 len)
  60. {
  61. while ((len) && ((unsigned long)mapped_addr) & 7) {
  62. *(hostbuf++) = readb(mapped_addr++);
  63. len--;
  64. }
  65. octeon_toggle_bar1_swapmode(oct, MEMOPS_IDX);
  66. while (len >= 8) {
  67. *((u64 *)hostbuf) = readq(mapped_addr);
  68. mapped_addr += 8;
  69. hostbuf += 8;
  70. len -= 8;
  71. }
  72. octeon_toggle_bar1_swapmode(oct, MEMOPS_IDX);
  73. while (len--)
  74. *(hostbuf++) = readb(mapped_addr++);
  75. }
  76. /* Core mem read/write with temporary bar1 settings. */
  77. /* op = 1 to read, op = 0 to write. */
  78. static void
  79. __octeon_pci_rw_core_mem(struct octeon_device *oct, u64 addr,
  80. u8 *hostbuf, u32 len, u32 op)
  81. {
  82. u32 copy_len = 0, index_reg_val = 0;
  83. unsigned long flags;
  84. u8 __iomem *mapped_addr;
  85. spin_lock_irqsave(&oct->mem_access_lock, flags);
  86. /* Save the original index reg value. */
  87. index_reg_val = oct->fn_list.bar1_idx_read(oct, MEMOPS_IDX);
  88. do {
  89. oct->fn_list.bar1_idx_setup(oct, addr, MEMOPS_IDX, 1);
  90. mapped_addr = oct->mmio[1].hw_addr
  91. + (MEMOPS_IDX << 22) + (addr & 0x3fffff);
  92. /* If operation crosses a 4MB boundary, split the transfer
  93. * at the 4MB
  94. * boundary.
  95. */
  96. if (((addr + len - 1) & ~(0x3fffff)) != (addr & ~(0x3fffff))) {
  97. copy_len = (u32)(((addr & ~(0x3fffff)) +
  98. (MEMOPS_IDX << 22)) - addr);
  99. } else {
  100. copy_len = len;
  101. }
  102. if (op) { /* read from core */
  103. octeon_pci_fastread(oct, mapped_addr, hostbuf,
  104. copy_len);
  105. } else {
  106. octeon_pci_fastwrite(oct, mapped_addr, hostbuf,
  107. copy_len);
  108. }
  109. len -= copy_len;
  110. addr += copy_len;
  111. hostbuf += copy_len;
  112. } while (len);
  113. oct->fn_list.bar1_idx_write(oct, MEMOPS_IDX, index_reg_val);
  114. spin_unlock_irqrestore(&oct->mem_access_lock, flags);
  115. }
  116. void
  117. octeon_pci_read_core_mem(struct octeon_device *oct,
  118. u64 coreaddr,
  119. u8 *buf,
  120. u32 len)
  121. {
  122. __octeon_pci_rw_core_mem(oct, coreaddr, buf, len, 1);
  123. }
  124. void
  125. octeon_pci_write_core_mem(struct octeon_device *oct,
  126. u64 coreaddr,
  127. u8 *buf,
  128. u32 len)
  129. {
  130. __octeon_pci_rw_core_mem(oct, coreaddr, buf, len, 0);
  131. }
  132. u64 octeon_read_device_mem64(struct octeon_device *oct, u64 coreaddr)
  133. {
  134. __be64 ret;
  135. __octeon_pci_rw_core_mem(oct, coreaddr, (u8 *)&ret, 8, 1);
  136. return be64_to_cpu(ret);
  137. }
  138. u32 octeon_read_device_mem32(struct octeon_device *oct, u64 coreaddr)
  139. {
  140. __be32 ret;
  141. __octeon_pci_rw_core_mem(oct, coreaddr, (u8 *)&ret, 4, 1);
  142. return be32_to_cpu(ret);
  143. }
  144. void octeon_write_device_mem32(struct octeon_device *oct, u64 coreaddr,
  145. u32 val)
  146. {
  147. __be32 t = cpu_to_be32(val);
  148. __octeon_pci_rw_core_mem(oct, coreaddr, (u8 *)&t, 4, 0);
  149. }