ipath_wc_x86_64.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * This file is conditionally built on x86_64 only. Otherwise weak symbol
  35. * versions of the functions exported from here are used.
  36. */
  37. #include <linux/pci.h>
  38. #include <asm/mtrr.h>
  39. #include <asm/processor.h>
  40. #include "ipath_kernel.h"
  41. /**
  42. * ipath_enable_wc - enable write combining for MMIO writes to the device
  43. * @dd: infinipath device
  44. *
  45. * This routine is x86_64-specific; it twiddles the CPU's MTRRs to enable
  46. * write combining.
  47. */
  48. int ipath_enable_wc(struct ipath_devdata *dd)
  49. {
  50. int ret = 0;
  51. u64 pioaddr, piolen;
  52. unsigned bits;
  53. const unsigned long addr = pci_resource_start(dd->pcidev, 0);
  54. const size_t len = pci_resource_len(dd->pcidev, 0);
  55. /*
  56. * Set the PIO buffers to be WCCOMB, so we get HT bursts to the
  57. * chip. Linux (possibly the hardware) requires it to be on a power
  58. * of 2 address matching the length (which has to be a power of 2).
  59. * For rev1, that means the base address, for rev2, it will be just
  60. * the PIO buffers themselves.
  61. * For chips with two sets of buffers, the calculations are
  62. * somewhat more complicated; we need to sum, and the piobufbase
  63. * register has both offsets, 2K in low 32 bits, 4K in high 32 bits.
  64. * The buffers are still packed, so a single range covers both.
  65. */
  66. if (dd->ipath_piobcnt2k && dd->ipath_piobcnt4k) { /* 2 sizes */
  67. unsigned long pio2kbase, pio4kbase;
  68. pio2kbase = dd->ipath_piobufbase & 0xffffffffUL;
  69. pio4kbase = (dd->ipath_piobufbase >> 32) & 0xffffffffUL;
  70. if (pio2kbase < pio4kbase) { /* all, for now */
  71. pioaddr = addr + pio2kbase;
  72. piolen = pio4kbase - pio2kbase +
  73. dd->ipath_piobcnt4k * dd->ipath_4kalign;
  74. } else {
  75. pioaddr = addr + pio4kbase;
  76. piolen = pio2kbase - pio4kbase +
  77. dd->ipath_piobcnt2k * dd->ipath_palign;
  78. }
  79. } else { /* single buffer size (2K, currently) */
  80. pioaddr = addr + dd->ipath_piobufbase;
  81. piolen = dd->ipath_piobcnt2k * dd->ipath_palign +
  82. dd->ipath_piobcnt4k * dd->ipath_4kalign;
  83. }
  84. for (bits = 0; !(piolen & (1ULL << bits)); bits++)
  85. /* do nothing */ ;
  86. if (piolen != (1ULL << bits)) {
  87. piolen >>= bits;
  88. while (piolen >>= 1)
  89. bits++;
  90. piolen = 1ULL << (bits + 1);
  91. }
  92. if (pioaddr & (piolen - 1)) {
  93. u64 atmp;
  94. ipath_dbg("pioaddr %llx not on right boundary for size "
  95. "%llx, fixing\n",
  96. (unsigned long long) pioaddr,
  97. (unsigned long long) piolen);
  98. atmp = pioaddr & ~(piolen - 1);
  99. if (atmp < addr || (atmp + piolen) > (addr + len)) {
  100. ipath_dev_err(dd, "No way to align address/size "
  101. "(%llx/%llx), no WC mtrr\n",
  102. (unsigned long long) atmp,
  103. (unsigned long long) piolen << 1);
  104. ret = -ENODEV;
  105. } else {
  106. ipath_dbg("changing WC base from %llx to %llx, "
  107. "len from %llx to %llx\n",
  108. (unsigned long long) pioaddr,
  109. (unsigned long long) atmp,
  110. (unsigned long long) piolen,
  111. (unsigned long long) piolen << 1);
  112. pioaddr = atmp;
  113. piolen <<= 1;
  114. }
  115. }
  116. if (!ret) {
  117. int cookie;
  118. ipath_cdbg(VERBOSE, "Setting mtrr for chip to WC "
  119. "(addr %llx, len=0x%llx)\n",
  120. (unsigned long long) pioaddr,
  121. (unsigned long long) piolen);
  122. cookie = mtrr_add(pioaddr, piolen, MTRR_TYPE_WRCOMB, 0);
  123. if (cookie < 0) {
  124. {
  125. dev_info(&dd->pcidev->dev,
  126. "mtrr_add() WC for PIO bufs "
  127. "failed (%d)\n",
  128. cookie);
  129. ret = -EINVAL;
  130. }
  131. } else {
  132. ipath_cdbg(VERBOSE, "Set mtrr for chip to WC, "
  133. "cookie is %d\n", cookie);
  134. dd->ipath_wc_cookie = cookie;
  135. dd->ipath_wc_base = (unsigned long) pioaddr;
  136. dd->ipath_wc_len = (unsigned long) piolen;
  137. }
  138. }
  139. return ret;
  140. }
  141. /**
  142. * ipath_disable_wc - disable write combining for MMIO writes to the device
  143. * @dd: infinipath device
  144. */
  145. void ipath_disable_wc(struct ipath_devdata *dd)
  146. {
  147. if (dd->ipath_wc_cookie) {
  148. int r;
  149. ipath_cdbg(VERBOSE, "undoing WCCOMB on pio buffers\n");
  150. r = mtrr_del(dd->ipath_wc_cookie, dd->ipath_wc_base,
  151. dd->ipath_wc_len);
  152. if (r < 0)
  153. dev_info(&dd->pcidev->dev,
  154. "mtrr_del(%lx, %lx, %lx) failed: %d\n",
  155. dd->ipath_wc_cookie, dd->ipath_wc_base,
  156. dd->ipath_wc_len, r);
  157. dd->ipath_wc_cookie = 0; /* even on failure */
  158. }
  159. }
  160. /**
  161. * ipath_unordered_wc - indicate whether write combining is ordered
  162. *
  163. * Because our performance depends on our ability to do write combining mmio
  164. * writes in the most efficient way, we need to know if we are on an Intel
  165. * or AMD x86_64 processor. AMD x86_64 processors flush WC buffers out in
  166. * the order completed, and so no special flushing is required to get
  167. * correct ordering. Intel processors, however, will flush write buffers
  168. * out in "random" orders, and so explicit ordering is needed at times.
  169. */
  170. int ipath_unordered_wc(void)
  171. {
  172. return boot_cpu_data.x86_vendor != X86_VENDOR_AMD;
  173. }