]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/intel-ipsec-mb/LibPerfApp/msr.c
1 /**********************************************************************
2 Copyright(c) 2018 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
31 * @brief Provides access to MSR read & write operations
39 #include <sys/types.h>
46 static int *m_msr_fd
= NULL
; /**< MSR driver file descriptors table */
47 static unsigned m_maxcores
= 0; /**< max number of cores (size of the
51 machine_init(const unsigned max_core_id
)
57 return MACHINE_RETVAL_PARAM
;
59 m_maxcores
= max_core_id
+ 1;
62 * Allocate table to hold MSR driver file descriptors
63 * Each file descriptor is for a different core.
64 * Core id is an index to the table.
66 m_msr_fd
= (int *)malloc(m_maxcores
* sizeof(m_msr_fd
[0]));
67 if (m_msr_fd
== NULL
) {
69 return MACHINE_RETVAL_ERROR
;
72 for (i
= 0; i
< m_maxcores
; i
++)
75 return MACHINE_RETVAL_OK
;
84 ASSERT(m_msr_fd
!= NULL
);
86 return MACHINE_RETVAL_ERROR
;
89 * Close open file descriptors and free up table memory.
91 for (i
= 0; i
< m_maxcores
; i
++)
92 if (m_msr_fd
[i
] != -1) {
101 return MACHINE_RETVAL_OK
;
106 * @brief Returns MSR driver file descriptor for given core id
108 * File descriptor could be previously open and comes from
109 * m_msr_fd table or is open (& cached) during the call.
111 * @param lcore logical core id
113 * @return MSR driver file descriptor corresponding \a lcore
116 msr_file_open(const unsigned lcore
)
118 ASSERT(lcore
< m_maxcores
);
119 ASSERT(m_msr_fd
!= NULL
);
121 int fd
= m_msr_fd
[lcore
];
126 memset(fname
, 0, sizeof(fname
));
127 snprintf(fname
, sizeof(fname
)-1,
128 "/dev/cpu/%u/msr", lcore
);
129 fd
= open(fname
, O_RDWR
);
131 fprintf(stderr
, "Error opening file '%s'!\n", fname
);
133 m_msr_fd
[lcore
] = fd
;
141 msr_read(const unsigned lcore
,
145 int ret
= MACHINE_RETVAL_OK
;
148 ssize_t read_ret
= 0;
150 ASSERT(value
!= NULL
);
152 return MACHINE_RETVAL_PARAM
;
154 ASSERT(lcore
< m_maxcores
);
155 if (lcore
>= m_maxcores
)
156 return MACHINE_RETVAL_PARAM
;
158 ASSERT(m_msr_fd
!= NULL
);
159 if (m_msr_fd
== NULL
)
160 return MACHINE_RETVAL_ERROR
;
162 fd
= msr_file_open(lcore
);
164 return MACHINE_RETVAL_ERROR
;
166 read_ret
= pread(fd
, value
, sizeof(value
[0]), (off_t
)reg
);
168 if (read_ret
!= sizeof(value
[0])) {
169 fprintf(stderr
, "RDMSR failed for reg[0x%x] on lcore %u\n",
170 (unsigned)reg
, lcore
);
171 ret
= MACHINE_RETVAL_ERROR
;
178 msr_write(const unsigned lcore
,
180 const uint64_t value
)
182 int ret
= MACHINE_RETVAL_OK
;
185 ssize_t write_ret
= 0;
187 ASSERT(lcore
< m_maxcores
);
188 if (lcore
>= m_maxcores
)
189 return MACHINE_RETVAL_PARAM
;
191 ASSERT(m_msr_fd
!= NULL
);
192 if (m_msr_fd
== NULL
)
193 return MACHINE_RETVAL_ERROR
;
195 fd
= msr_file_open(lcore
);
197 return MACHINE_RETVAL_ERROR
;
199 write_ret
= pwrite(fd
, &value
, sizeof(value
), (off_t
)reg
);
201 if (write_ret
!= sizeof(value
)) {
202 fprintf(stderr
, "WRMSR failed for reg[0x%x] "
203 "<- value[0x%llx] on lcore %u\n",
204 (unsigned)reg
, (unsigned long long)value
, lcore
);
205 ret
= MACHINE_RETVAL_ERROR
;