Path: blob/master/drivers/infiniband/hw/qib/qib_wc_x86_64.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.2* Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233/*34* This file is conditionally built on x86_64 only. Otherwise weak symbol35* versions of the functions exported from here are used.36*/3738#include <linux/pci.h>39#include <asm/mtrr.h>40#include <asm/processor.h>4142#include "qib.h"4344/**45* qib_enable_wc - enable write combining for MMIO writes to the device46* @dd: qlogic_ib device47*48* This routine is x86_64-specific; it twiddles the CPU's MTRRs to enable49* write combining.50*/51int qib_enable_wc(struct qib_devdata *dd)52{53int ret = 0;54u64 pioaddr, piolen;55unsigned bits;56const unsigned long addr = pci_resource_start(dd->pcidev, 0);57const size_t len = pci_resource_len(dd->pcidev, 0);5859/*60* Set the PIO buffers to be WCCOMB, so we get HT bursts to the61* chip. Linux (possibly the hardware) requires it to be on a power62* of 2 address matching the length (which has to be a power of 2).63* For rev1, that means the base address, for rev2, it will be just64* the PIO buffers themselves.65* For chips with two sets of buffers, the calculations are66* somewhat more complicated; we need to sum, and the piobufbase67* register has both offsets, 2K in low 32 bits, 4K in high 32 bits.68* The buffers are still packed, so a single range covers both.69*/70if (dd->piobcnt2k && dd->piobcnt4k) {71/* 2 sizes for chip */72unsigned long pio2kbase, pio4kbase;73pio2kbase = dd->piobufbase & 0xffffffffUL;74pio4kbase = (dd->piobufbase >> 32) & 0xffffffffUL;75if (pio2kbase < pio4kbase) {76/* all current chips */77pioaddr = addr + pio2kbase;78piolen = pio4kbase - pio2kbase +79dd->piobcnt4k * dd->align4k;80} else {81pioaddr = addr + pio4kbase;82piolen = pio2kbase - pio4kbase +83dd->piobcnt2k * dd->palign;84}85} else { /* single buffer size (2K, currently) */86pioaddr = addr + dd->piobufbase;87piolen = dd->piobcnt2k * dd->palign +88dd->piobcnt4k * dd->align4k;89}9091for (bits = 0; !(piolen & (1ULL << bits)); bits++)92/* do nothing */ ;9394if (piolen != (1ULL << bits)) {95piolen >>= bits;96while (piolen >>= 1)97bits++;98piolen = 1ULL << (bits + 1);99}100if (pioaddr & (piolen - 1)) {101u64 atmp;102atmp = pioaddr & ~(piolen - 1);103if (atmp < addr || (atmp + piolen) > (addr + len)) {104qib_dev_err(dd, "No way to align address/size "105"(%llx/%llx), no WC mtrr\n",106(unsigned long long) atmp,107(unsigned long long) piolen << 1);108ret = -ENODEV;109} else {110pioaddr = atmp;111piolen <<= 1;112}113}114115if (!ret) {116int cookie;117118cookie = mtrr_add(pioaddr, piolen, MTRR_TYPE_WRCOMB, 0);119if (cookie < 0) {120{121qib_devinfo(dd->pcidev,122"mtrr_add() WC for PIO bufs "123"failed (%d)\n",124cookie);125ret = -EINVAL;126}127} else {128dd->wc_cookie = cookie;129dd->wc_base = (unsigned long) pioaddr;130dd->wc_len = (unsigned long) piolen;131}132}133134return ret;135}136137/**138* qib_disable_wc - disable write combining for MMIO writes to the device139* @dd: qlogic_ib device140*/141void qib_disable_wc(struct qib_devdata *dd)142{143if (dd->wc_cookie) {144int r;145146r = mtrr_del(dd->wc_cookie, dd->wc_base,147dd->wc_len);148if (r < 0)149qib_devinfo(dd->pcidev,150"mtrr_del(%lx, %lx, %lx) failed: %d\n",151dd->wc_cookie, dd->wc_base,152dd->wc_len, r);153dd->wc_cookie = 0; /* even on failure */154}155}156157/**158* qib_unordered_wc - indicate whether write combining is ordered159*160* Because our performance depends on our ability to do write combining mmio161* writes in the most efficient way, we need to know if we are on an Intel162* or AMD x86_64 processor. AMD x86_64 processors flush WC buffers out in163* the order completed, and so no special flushing is required to get164* correct ordering. Intel processors, however, will flush write buffers165* out in "random" orders, and so explicit ordering is needed at times.166*/167int qib_unordered_wc(void)168{169return boot_cpu_data.x86_vendor != X86_VENDOR_AMD;170}171172173