From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by dpdk.org (Postfix) with ESMTP id 18D618E5D for ; Wed, 20 Jan 2016 12:01:13 +0100 (CET) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP; 20 Jan 2016 03:01:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,320,1449561600"; d="scan'208";a="894537895" Received: from sie-lab-213-067.ir.intel.com (HELO silpixa00385445.ir.intel.com) ([10.237.213.67]) by orsmga002.jf.intel.com with ESMTP; 20 Jan 2016 03:01:12 -0800 From: Wojciech Andralojc To: dev@dpdk.org Date: Wed, 20 Jan 2016 10:56:39 +0000 Message-Id: <1453287399-65915-1-git-send-email-wojciechx.andralojc@intel.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1450354363-102022-1-git-send-email-wojciechx.andralojc@intel.com> References: <1450354363-102022-1-git-send-email-wojciechx.andralojc@intel.com> Subject: [dpdk-dev] [PATCH v2] Patch introducing API to read/write Intel Architecture Model Specific Registers (MSR)... X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jan 2016 11:01:14 -0000 Patch rework based on feedback, only x86 specific functions left under lib/librte_eal/common/include/arch/x86/. Signed-off-by: Wojciech Andralojc --- lib/librte_eal/common/include/arch/x86/rte_msr.h | 158 +++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 lib/librte_eal/common/include/arch/x86/rte_msr.h diff --git a/lib/librte_eal/common/include/arch/x86/rte_msr.h b/lib/librte_eal/common/include/arch/x86/rte_msr.h new file mode 100644 index 0000000..9d16633 --- /dev/null +++ b/lib/librte_eal/common/include/arch/x86/rte_msr.h @@ -0,0 +1,158 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2016 Intel Corporation. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _RTE_MSR_X86_64_H_ +#define _RTE_MSR_X86_64_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include //O_RDONLY +#include //pread + +#include +#include + +#define CPU_MSR_PATH "/dev/cpu/%u/msr" +#define CPU_MSR_PATH_MAX_LEN 32 + +/** + * This function should not be called directly. + * Function to open CPU's MSR file + */ +static int +__msr_open_file(const unsigned lcore, int flags) +{ + char fname[CPU_MSR_PATH_MAX_LEN] = {0}; + int fd = -1; + + snprintf(fname, sizeof(fname) - 1, CPU_MSR_PATH, lcore); + + fd = open(fname, flags); + + if (fd < 0) + RTE_LOG(ERR, PQOS, "Error opening file '%s'!\n", fname); + + return fd; +} + +/** + * Function to read CPU's MSR + * + * @param [in] lcore + * CPU logical core id + * + * @param [in] reg + * MSR reg to read + * + * @param [out] value + * Read value of MSR reg + * + * @return + * Operations status +*/ + +static inline int +rte_msr_read(const unsigned lcore, const uint32_t reg, uint64_t *value) +{ + int fd = -1; + int ret = -1; + + RTE_VERIFY(value != NULL); + if (value == NULL) + return -1; + + fd = __msr_open_file(lcore, O_RDONLY); + + if (fd >= 0) { + ssize_t read_ret = 0; + + read_ret = pread(fd, value, sizeof(value[0]), (off_t)reg); + + if (read_ret != sizeof(value[0])) { + RTE_LOG(ERR, PQOS, "RDMSR failed for reg[0x%x] on lcore %u\n", + (unsigned)reg, lcore); + } else + ret = 0; + + close(fd); + } + + return ret; +} + +/** + * Function to write CPU's MSR + * + * @param [in] lcore + * CPU logical core id + * + * @param [in] reg + * MSR reg to write + * + * @param [in] value + * Value to be written to MSR reg + * + * @return + * Operations status +*/ +static inline int +rte_msr_write(const unsigned lcore, const uint32_t reg, const uint64_t value) +{ + int fd = -1; + int ret = -1; + + fd = __msr_open_file(lcore, O_WRONLY); + + if (fd >= 0) { + ssize_t write_ret = 0; + + write_ret = pwrite(fd, &value, sizeof(value), (off_t)reg); + if (write_ret != sizeof(value)) { + RTE_LOG(ERR, PQOS, "WRMSR failed for reg[0x%x] <- value[0x%llx] on " + "lcore %u\n", (unsigned)reg, (unsigned long long)value, lcore); + } else + ret = 0; + + close(fd); + } + + return ret; +} + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_MSR_X86_64_H_ */ -- 1.9.3