Path: blob/master/drivers/infiniband/hw/ipath/ipath_wc_x86_64.c
15112 views
/*1* Copyright (c) 2006, 2007 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 "ipath_kernel.h"4344/**45* ipath_enable_wc - enable write combining for MMIO writes to the device46* @dd: infinipath device47*48* This routine is x86_64-specific; it twiddles the CPU's MTRRs to enable49* write combining.50*/51int ipath_enable_wc(struct ipath_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->ipath_piobcnt2k && dd->ipath_piobcnt4k) { /* 2 sizes */71unsigned long pio2kbase, pio4kbase;72pio2kbase = dd->ipath_piobufbase & 0xffffffffUL;73pio4kbase = (dd->ipath_piobufbase >> 32) & 0xffffffffUL;74if (pio2kbase < pio4kbase) { /* all, for now */75pioaddr = addr + pio2kbase;76piolen = pio4kbase - pio2kbase +77dd->ipath_piobcnt4k * dd->ipath_4kalign;78} else {79pioaddr = addr + pio4kbase;80piolen = pio2kbase - pio4kbase +81dd->ipath_piobcnt2k * dd->ipath_palign;82}83} else { /* single buffer size (2K, currently) */84pioaddr = addr + dd->ipath_piobufbase;85piolen = dd->ipath_piobcnt2k * dd->ipath_palign +86dd->ipath_piobcnt4k * dd->ipath_4kalign;87}8889for (bits = 0; !(piolen & (1ULL << bits)); bits++)90/* do nothing */ ;9192if (piolen != (1ULL << bits)) {93piolen >>= bits;94while (piolen >>= 1)95bits++;96piolen = 1ULL << (bits + 1);97}98if (pioaddr & (piolen - 1)) {99u64 atmp;100ipath_dbg("pioaddr %llx not on right boundary for size "101"%llx, fixing\n",102(unsigned long long) pioaddr,103(unsigned long long) piolen);104atmp = pioaddr & ~(piolen - 1);105if (atmp < addr || (atmp + piolen) > (addr + len)) {106ipath_dev_err(dd, "No way to align address/size "107"(%llx/%llx), no WC mtrr\n",108(unsigned long long) atmp,109(unsigned long long) piolen << 1);110ret = -ENODEV;111} else {112ipath_dbg("changing WC base from %llx to %llx, "113"len from %llx to %llx\n",114(unsigned long long) pioaddr,115(unsigned long long) atmp,116(unsigned long long) piolen,117(unsigned long long) piolen << 1);118pioaddr = atmp;119piolen <<= 1;120}121}122123if (!ret) {124int cookie;125ipath_cdbg(VERBOSE, "Setting mtrr for chip to WC "126"(addr %llx, len=0x%llx)\n",127(unsigned long long) pioaddr,128(unsigned long long) piolen);129cookie = mtrr_add(pioaddr, piolen, MTRR_TYPE_WRCOMB, 0);130if (cookie < 0) {131{132dev_info(&dd->pcidev->dev,133"mtrr_add() WC for PIO bufs "134"failed (%d)\n",135cookie);136ret = -EINVAL;137}138} else {139ipath_cdbg(VERBOSE, "Set mtrr for chip to WC, "140"cookie is %d\n", cookie);141dd->ipath_wc_cookie = cookie;142dd->ipath_wc_base = (unsigned long) pioaddr;143dd->ipath_wc_len = (unsigned long) piolen;144}145}146147return ret;148}149150/**151* ipath_disable_wc - disable write combining for MMIO writes to the device152* @dd: infinipath device153*/154void ipath_disable_wc(struct ipath_devdata *dd)155{156if (dd->ipath_wc_cookie) {157int r;158ipath_cdbg(VERBOSE, "undoing WCCOMB on pio buffers\n");159r = mtrr_del(dd->ipath_wc_cookie, dd->ipath_wc_base,160dd->ipath_wc_len);161if (r < 0)162dev_info(&dd->pcidev->dev,163"mtrr_del(%lx, %lx, %lx) failed: %d\n",164dd->ipath_wc_cookie, dd->ipath_wc_base,165dd->ipath_wc_len, r);166dd->ipath_wc_cookie = 0; /* even on failure */167}168}169170/**171* ipath_unordered_wc - indicate whether write combining is ordered172*173* Because our performance depends on our ability to do write combining mmio174* writes in the most efficient way, we need to know if we are on an Intel175* or AMD x86_64 processor. AMD x86_64 processors flush WC buffers out in176* the order completed, and so no special flushing is required to get177* correct ordering. Intel processors, however, will flush write buffers178* out in "random" orders, and so explicit ordering is needed at times.179*/180int ipath_unordered_wc(void)181{182return boot_cpu_data.x86_vendor != X86_VENDOR_AMD;183}184185186