octeon_mem_ops.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 BAR1_INDEX_DYNAMIC_MAP
  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. u64 static_mapping_base;
  86. static_mapping_base = oct->console_nb_info.dram_region_base;
  87. if (static_mapping_base &&
  88. static_mapping_base == (addr & ~(OCTEON_BAR1_ENTRY_SIZE - 1ULL))) {
  89. int bar1_index = oct->console_nb_info.bar1_index;
  90. mapped_addr = oct->mmio[1].hw_addr
  91. + (bar1_index << ilog2(OCTEON_BAR1_ENTRY_SIZE))
  92. + (addr & (OCTEON_BAR1_ENTRY_SIZE - 1ULL));
  93. if (op)
  94. octeon_pci_fastread(oct, mapped_addr, hostbuf, len);
  95. else
  96. octeon_pci_fastwrite(oct, mapped_addr, hostbuf, len);
  97. return;
  98. }
  99. spin_lock_irqsave(&oct->mem_access_lock, flags);
  100. /* Save the original index reg value. */
  101. index_reg_val = oct->fn_list.bar1_idx_read(oct, MEMOPS_IDX);
  102. do {
  103. oct->fn_list.bar1_idx_setup(oct, addr, MEMOPS_IDX, 1);
  104. mapped_addr = oct->mmio[1].hw_addr
  105. + (MEMOPS_IDX << 22) + (addr & 0x3fffff);
  106. /* If operation crosses a 4MB boundary, split the transfer
  107. * at the 4MB
  108. * boundary.
  109. */
  110. if (((addr + len - 1) & ~(0x3fffff)) != (addr & ~(0x3fffff))) {
  111. copy_len = (u32)(((addr & ~(0x3fffff)) +
  112. (MEMOPS_IDX << 22)) - addr);
  113. } else {
  114. copy_len = len;
  115. }
  116. if (op) { /* read from core */
  117. octeon_pci_fastread(oct, mapped_addr, hostbuf,
  118. copy_len);
  119. } else {
  120. octeon_pci_fastwrite(oct, mapped_addr, hostbuf,
  121. copy_len);
  122. }
  123. len -= copy_len;
  124. addr += copy_len;
  125. hostbuf += copy_len;
  126. } while (len);
  127. oct->fn_list.bar1_idx_write(oct, MEMOPS_IDX, index_reg_val);
  128. spin_unlock_irqrestore(&oct->mem_access_lock, flags);
  129. }
  130. void
  131. octeon_pci_read_core_mem(struct octeon_device *oct,
  132. u64 coreaddr,
  133. u8 *buf,
  134. u32 len)
  135. {
  136. __octeon_pci_rw_core_mem(oct, coreaddr, buf, len, 1);
  137. }
  138. void
  139. octeon_pci_write_core_mem(struct octeon_device *oct,
  140. u64 coreaddr,
  141. const u8 *buf,
  142. u32 len)
  143. {
  144. __octeon_pci_rw_core_mem(oct, coreaddr, (u8 *)buf, len, 0);
  145. }
  146. u64 octeon_read_device_mem64(struct octeon_device *oct, u64 coreaddr)
  147. {
  148. __be64 ret;
  149. __octeon_pci_rw_core_mem(oct, coreaddr, (u8 *)&ret, 8, 1);
  150. return be64_to_cpu(ret);
  151. }
  152. u32 octeon_read_device_mem32(struct octeon_device *oct, u64 coreaddr)
  153. {
  154. __be32 ret;
  155. __octeon_pci_rw_core_mem(oct, coreaddr, (u8 *)&ret, 4, 1);
  156. return be32_to_cpu(ret);
  157. }
  158. void octeon_write_device_mem32(struct octeon_device *oct, u64 coreaddr,
  159. u32 val)
  160. {
  161. __be32 t = cpu_to_be32(val);
  162. __octeon_pci_rw_core_mem(oct, coreaddr, (u8 *)&t, 4, 0);
  163. }