qib_wc_x86_64.c 4.8 KB

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