blob: 673d5630e2b4c48f03c9e9a130783c1b8fbd7185 [file] [log] [blame]
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#include <ctype.h>
Jason M. Billsff44e542021-04-05 08:11:52 -070017#include <errno.h>
Jason M. Bills7ef5a552020-04-06 14:58:44 -070018#include <inttypes.h>
Jason M. Billsff44e542021-04-05 08:11:52 -070019#include <limits.h>
Jason M. Bills7ef5a552020-04-06 14:58:44 -070020#include <peci.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Sumanth Bhat1ae54d22023-03-01 23:03:03 +053024#include <time.h>
Jason M. Bills7ef5a552020-04-06 14:58:44 -070025#include <unistd.h>
26#ifndef ABS
27#define ABS(_v_) (((_v_) > 0) ? (_v_) : -(_v_))
28#endif
29
Jason M. Billsff44e542021-04-05 08:11:52 -070030#define CC_COUNT 256 // CC is a byte so only has 256 possible values
31
Jason M. Bills7ef5a552020-04-06 14:58:44 -070032extern EPECIStatus peci_GetDIB(uint8_t target, uint64_t* dib);
33
Sumanth Bhat1ae54d22023-03-01 23:03:03 +053034double getTimeDifference(const struct timespec begin)
35{
36 double timeDiff = 0.0;
37 struct timespec end;
Patrick Williams5c2fb322023-08-04 10:46:03 -050038 time_t seconds = 0;
39 time_t nanoseconds = 0;
Sumanth Bhat1ae54d22023-03-01 23:03:03 +053040
41 clock_gettime(CLOCK_REALTIME, &end);
42 seconds = end.tv_sec - begin.tv_sec;
43 nanoseconds = end.tv_nsec - begin.tv_nsec;
44 timeDiff = (double)seconds + (double)nanoseconds * 1e-9;
45
46 return timeDiff;
47}
48
Jason M. Bills7ef5a552020-04-06 14:58:44 -070049void Usage(char* progname)
50{
51 printf("Usage:\n");
Sumanth Bhat1ae54d22023-03-01 23:03:03 +053052 printf("%s [-h] [-v] [-t] [-a <addr>] [-i <domain id>] [-s <size>] [-l "
53 "<count>] "
Jason M. Bills7b112802022-03-01 13:28:56 -080054 "<command> [parameters]\n",
Jason M. Billsff44e542021-04-05 08:11:52 -070055 progname);
Jason M. Bills7ef5a552020-04-06 14:58:44 -070056 printf("Options:\n");
Jason M. Billsff44e542021-04-05 08:11:52 -070057 printf("\t%-12s%s\n", "-h", "Display this help information");
58 printf("\t%-12s%s\n", "-v",
Jason M. Bills7ef5a552020-04-06 14:58:44 -070059 "Display additional information about the command");
Sumanth Bhat1ae54d22023-03-01 23:03:03 +053060 printf("\t%-12s%s\n", "-t", "Measure request-to-response time");
Jason M. Billsff44e542021-04-05 08:11:52 -070061 printf("\t%-12s%s %lu\n", "-l <count>",
62 "Loop the command the given number of times. <count> is in the "
63 "range 1 to",
64 ULONG_MAX);
65 printf("\t%-12s%s\n", "-a <addr>",
Jason M. Bills7ef5a552020-04-06 14:58:44 -070066 "Address of the target. Accepted values are 48-55 (0x30-0x37). "
67 "Default is 48 (0x30)");
Jason M. Bills7b112802022-03-01 13:28:56 -080068 printf("\t%-12s%s\n", "-i <domain id>",
69 "Domain ID of the target. Accepted values are 0-127. Default is 0");
Jason M. Billsff44e542021-04-05 08:11:52 -070070 printf("\t%-12s%s\n", "-s <size>",
Jason M. Bills7ef5a552020-04-06 14:58:44 -070071 "Size of data to read or write in bytes. Accepted values are 1, 2, "
72 "4, 8, and 16. Default is 4");
Anna Platash03d7dae2021-02-05 13:52:05 +010073 printf("\t%-12s%s\n", "-d",
74 "Set PECI device name, for example \"-d /dev/peci-0\"");
Jason M. Bills7ef5a552020-04-06 14:58:44 -070075 printf("Commands:\n");
76 printf("\t%-28s%s\n", "Ping", "Ping the target");
77 printf("\t%-28s%s\n", "GetTemp", "Get the temperature");
78 printf("\t%-28s%s\n", "GetDIB", "Get the DIB");
79 printf("\t%-28s%s\n", "RdPkgConfig",
80 "Read Package Config <Index Parameter>");
81 printf("\t%-28s%s\n", "WrPkgConfig",
82 "Write Package Config <Index Parameter Data>");
83 printf("\t%-28s%s\n", "RdIAMSR", "MSR Read <Thread Address>");
84 printf("\t%-28s%s\n", "RdPCIConfig", "PCI Read <Bus Dev Func [Reg]>");
85 printf("\t%-28s%s\n", "RdPCIConfigLocal",
86 "Local PCI Read <Bus Dev Func [Reg]>");
87 printf("\t%-28s%s\n", "WrPCIConfigLocal",
88 "Local PCI Write <Bus Dev Func Reg Data>");
89 printf("\t%-28s%s\n", "RdEndpointConfigPCILocal",
90 "Endpoint Local PCI Config Read <Seg Bus Dev Func Reg>");
91 printf("\t%-28s%s\n", "WrEndpointConfigPCILocal",
92 "Endpoint Local PCI Config Write <Seg Bus Dev Func Reg Data>");
93 printf("\t%-28s%s\n", "RdEndpointConfigPCI",
94 "Endpoint PCI Config Read <Seg Bus Dev Func Reg>");
95 printf("\t%-28s%s\n", "WrEndpointConfigPCI",
96 "Endpoint PCI Config Write <Seg Bus Dev Func Reg Data>");
97 printf("\t%-28s%s\n", "RdEndpointConfigMMIO",
98 "Endpoint MMIO Read <AType Bar Seg Bus Dev Func Reg>");
99 printf("\t%-28s%s\n", "WrEndpointConfigMMIO",
100 "Endpoint MMIO Write <AType Bar Seg Bus Dev Func Reg Data>");
101 printf("\t%-28s%s\n", "raw", "Raw PECI command in bytes");
102 printf("\n");
103}
104
Jason M. Billsff44e542021-04-05 08:11:52 -0700105static void printLoopSummary(uint32_t* ccCounts)
106{
107 printf("Completion code counts:\n");
108 for (uint32_t i = 0; i < CC_COUNT; i++)
109 {
110 if (ccCounts[i])
111 {
112 printf(" 0x%02x: %d\n", i, ccCounts[i]);
113 }
114 }
115}
116
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700117int main(int argc, char* argv[])
118{
119 int c;
120 int i = 0;
121 char* cmd = NULL;
Patrick Williamsb65de2d2023-05-26 15:14:02 -0500122 EPECIStatus ret = PECI_CC_SUCCESS;
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700123 uint8_t address = 0x30; // use default address of 48d
Jason M. Bills7b112802022-03-01 13:28:56 -0800124 uint8_t domainId = 0; // use default domain ID of 0
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700125 uint8_t u8Size = 4; // default to a DWORD
126 uint32_t u32PciReadVal = 0;
Matt Simmeringc4c3fee2023-06-12 17:06:57 -0700127 uint64_t u64MmioReadVal = 0;
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700128 uint8_t u8Seg = 0;
129 uint8_t u8Bar = 0;
130 uint8_t u8AddrType = 0;
131 uint8_t u8PciBus = 0;
132 uint8_t u8PciDev = 0;
133 uint8_t u8PciFunc = 0;
134 uint16_t u16PciReg = 0;
135 uint64_t u64Offset = 0;
136 uint32_t u32PciWriteVal = 0;
137 uint64_t u64MmioWriteVal = 0;
138 uint8_t u8PkgIndex = 0;
139 uint16_t u16PkgParam = 0;
140 uint32_t u32PkgValue = 0;
141 uint8_t u8MsrThread = 0;
142 uint16_t u16MsrAddr = 0;
143 uint64_t u64MsrVal = 0;
144 short temperature;
145 uint64_t dib;
146 int index = 0;
147 uint8_t cc = 0;
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530148 bool measureTime = false;
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700149 bool verbose = false;
Jason M. Billsff44e542021-04-05 08:11:52 -0700150 bool looped = false;
151 uint32_t loops = 1;
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530152 uint32_t loopCount = 1;
Nirav Shahfd5dfd52022-03-09 12:29:41 -0800153 uint32_t ccCounts[CC_COUNT] = {0};
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530154 struct timespec begin;
155 double timeSpent = 0.0;
156 double totalTimeSpent = 0.0;
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700157
158 //
159 // Parse arguments.
160 //
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530161 while (-1 != (c = getopt(argc, argv, "hvtl:a:i:s:d:")))
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700162 {
163 switch (c)
164 {
165 case 'h':
166 Usage(argv[0]);
167 return 0;
168 break;
169
170 case 'v':
171 verbose = true;
172 break;
173
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530174 case 't':
175 measureTime = true;
176 break;
177
Jason M. Billsff44e542021-04-05 08:11:52 -0700178 case 'l':
179 looped = true;
180 errno = 0;
181 if (optarg != NULL)
182 loops = (uint32_t)strtoul(optarg, NULL, 0);
183 if (!loops || errno)
184 {
185 printf("ERROR: Invalid loop count\n");
186 if (errno)
187 perror("");
188 goto ErrorExit;
189 }
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530190 loopCount =
191 loops; // Preserve a copy for average time measurement
Jason M. Billsff44e542021-04-05 08:11:52 -0700192 break;
193
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700194 case 'a':
195 if (optarg != NULL)
196 address = (uint8_t)strtoul(optarg, NULL, 0);
197 if (address < MIN_CLIENT_ADDR || address > MAX_CLIENT_ADDR)
198 {
199 printf("ERROR: Invalid address \"0x%x\"\n", address);
200 goto ErrorExit;
201 }
202
203 break;
204
Jason M. Bills7b112802022-03-01 13:28:56 -0800205 case 'i':
206 if (optarg != NULL)
207 domainId = (uint8_t)strtoul(optarg, NULL, 0);
208 if (domainId >= 128) // Domain ID is only 7 bits
209 {
210 printf("ERROR: Invalid domain ID \"%d\"\n", domainId);
211 goto ErrorExit;
212 }
213 break;
214
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700215 case 's':
216 if (optarg != NULL)
217 u8Size = (uint8_t)strtoul(optarg, NULL, 0);
218 if (u8Size != 1 && u8Size != 2 && u8Size != 4 && u8Size != 8 &&
219 u8Size != 16)
220 {
221 printf("ERROR: Invalid size \"%d\"\n", u8Size);
222 goto ErrorExit;
223 }
224 break;
225
Anna Platash03d7dae2021-02-05 13:52:05 +0100226 case 'd':
227 peci_SetDevName(optarg);
228 break;
229
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700230 default:
231 printf("ERROR: Unrecognized option \"-%c\"\n", optopt);
232 goto ErrorExit;
233 break;
234 }
235 }
236
237 // Get the command from the first parameter
238 cmd = argv[optind++];
239 if (cmd == NULL)
240 {
241 Usage(argv[0]);
242 return 0;
243 }
244
245 // Allow any case
246 while (cmd[i])
247 {
248 cmd[i] = (char)tolower((int)cmd[i]);
249 i++;
250 }
251
252 //
253 // Execute the command
254 //
255 if (verbose)
256 {
257 printf("PECI target[0x%x]: ", address);
258 }
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530259
260 if (measureTime)
261 {
262 if (verbose && (loopCount > 1))
263 {
264 printf("Warning: Request-response time measurement with verbose "
265 "mode can affect the time between consecutive commands in "
266 "looped mode!\n");
267 }
268 }
269
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700270 if (strcmp(cmd, "ping") == 0)
271 {
272 if (verbose)
273 {
274 printf("Pinging ... ");
275 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700276 while (loops--)
277 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530278 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -0700279 ret = peci_Ping(address);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530280 timeSpent = getTimeDifference(begin);
281 if (verbose && measureTime)
282 {
283 printf("\nTime taken in iteration %d = %lf s\n",
284 (loopCount - loops), timeSpent);
285 }
286 totalTimeSpent += timeSpent;
287
Jason M. Billsff44e542021-04-05 08:11:52 -0700288 if (verbose || loops == 0)
289 {
290 if (0 != ret)
291 {
292 printf("Failed\n");
293 }
294 else
295 {
296 printf("Succeeded\n");
297 }
298 }
299 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700300 }
301 else if (strcmp(cmd, "getdib") == 0)
302 {
303 if (verbose)
304 {
305 printf("GetDIB\n");
306 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700307 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700308 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530309 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -0700310 ret = peci_GetDIB(address, &dib);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530311 timeSpent = getTimeDifference(begin);
312 if (verbose && measureTime)
313 {
314 printf("\nTime taken in iteration %d = %lf s\n",
315 (loopCount - loops), timeSpent);
316 }
317 totalTimeSpent += timeSpent;
318
Jason M. Billsff44e542021-04-05 08:11:52 -0700319 if (verbose || loops == 0)
320 {
321 if (0 != ret)
322 {
323 printf("ERROR %d: Retrieving DIB failed\n", ret);
324 }
325 else
326 {
327 printf(" 0x%" PRIx64 "\n", dib);
328 }
329 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700330 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700331 }
332
333 else if (strcmp(cmd, "gettemp") == 0)
334 {
335 if (verbose)
336 {
337 printf("GetTemp\n");
338 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700339 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700340 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530341 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -0700342 ret = peci_GetTemp(address, &temperature);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530343 timeSpent = getTimeDifference(begin);
344 if (verbose && measureTime)
345 {
346 printf("\nTime taken in iteration %d = %lf s\n",
347 (loopCount - loops), timeSpent);
348 }
349 totalTimeSpent += timeSpent;
350
Jason M. Billsff44e542021-04-05 08:11:52 -0700351 if (verbose || loops == 0)
352 {
353 if (0 != ret)
354 {
355 printf("ERROR %d: Retrieving temperature failed\n", ret);
356 }
357 else
358 {
359 printf(" %04xh (%c%d.%02dC)\n",
360 (int)(unsigned int)(unsigned short)temperature,
361 (0 > temperature) ? '-' : '+',
362 (int)((unsigned int)ABS(temperature) / 64),
363 (int)(((unsigned int)ABS(temperature) % 64) * 100) /
364 64);
365 }
366 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700367 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700368 }
369
370 else if (strcmp(cmd, "rdpkgconfig") == 0)
371 {
372 index = argc;
373 switch (argc - optind)
374 {
375 case 2:
376 u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
377 u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
378 break;
379 default:
380 printf("ERROR: Unsupported arguments for Pkg Read\n");
381 goto ErrorExit;
382 break;
383 }
384 if (verbose)
385 {
386 printf("Pkg Read of Index %02x Param %04x\n", u8PkgIndex,
387 u16PkgParam);
388 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700389 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700390 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700391 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530392 clock_gettime(CLOCK_REALTIME, &begin);
Patrick Williams7169faa2023-05-10 07:51:24 -0500393 ret = peci_RdPkgConfig_dom(address, domainId, u8PkgIndex,
394 u16PkgParam, u8Size,
395 (uint8_t*)&u32PkgValue, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530396 timeSpent = getTimeDifference(begin);
397 if (verbose && measureTime)
398 {
399 printf("\nTime taken in iteration %d = %lf s\n",
400 (loopCount - loops), timeSpent);
401 }
402 totalTimeSpent += timeSpent;
403
Jason M. Billsff44e542021-04-05 08:11:52 -0700404 ccCounts[cc]++;
405
406 if (verbose || loops == 0)
407 {
408 if (0 != ret)
409 {
410 printf("ERROR %d: command failed\n", ret);
411 printf(" cc:0x%02x\n", cc);
412 }
413 else
414 {
415 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
416 u32PkgValue);
417 }
418 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700419 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700420 if (looped)
421 {
422 printLoopSummary(ccCounts);
423 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700424 }
425 else if (strcmp(cmd, "wrpkgconfig") == 0)
426 {
427 index = argc;
428 switch (argc - optind)
429 {
430 case 3:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700431 u32PkgValue = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700432 u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
433 u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
434 break;
435 default:
436 printf("ERROR: Unsupported arguments for Pkg Write\n");
437 goto ErrorExit;
438 break;
439 }
440 if (verbose)
441 {
442 printf("Pkg Write of Index %02x Param %04x: 0x%0*x\n", u8PkgIndex,
443 u16PkgParam, u8Size * 2, u32PkgValue);
444 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700445 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700446 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700447 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530448 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800449 ret = peci_WrPkgConfig_dom(address, domainId, u8PkgIndex,
450 u16PkgParam, u32PkgValue, u8Size, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530451 timeSpent = getTimeDifference(begin);
452 if (verbose && measureTime)
453 {
454 printf("\nTime taken in iteration %d = %lf s\n",
455 (loopCount - loops), timeSpent);
456 }
457 totalTimeSpent += timeSpent;
458
Jason M. Billsff44e542021-04-05 08:11:52 -0700459 ccCounts[cc]++;
460
461 if (verbose || loops == 0)
462 {
463 if (0 != ret)
464 {
465 printf("ERROR %d: command failed\n", ret);
466 }
467 printf(" cc:0x%02x\n", cc);
468 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700469 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700470 if (looped)
471 {
472 printLoopSummary(ccCounts);
473 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700474 }
475 else if (strcmp(cmd, "rdiamsr") == 0)
476 {
477 index = argc;
478 switch (argc - optind)
479 {
480 case 2:
481 u16MsrAddr = (uint16_t)strtoul(argv[--index], NULL, 0);
482 u8MsrThread = (uint8_t)strtoul(argv[--index], NULL, 0);
483 break;
484 default:
485 printf("ERROR: Unsupported arguments for MSR Read\n");
486 goto ErrorExit;
487 break;
488 }
489 if (verbose)
490 {
491 printf("MSR Read of Thread %02x MSR %04x\n", u8MsrThread,
492 u16MsrAddr);
493 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700494 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700495 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700496 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530497 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800498 ret = peci_RdIAMSR_dom(address, domainId, u8MsrThread, u16MsrAddr,
499 &u64MsrVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530500 timeSpent = getTimeDifference(begin);
501 if (verbose && measureTime)
502 {
503 printf("\nTime taken in iteration %d = %lf s\n",
504 (loopCount - loops), timeSpent);
505 }
506 totalTimeSpent += timeSpent;
507
Jason M. Billsff44e542021-04-05 08:11:52 -0700508 ccCounts[cc]++;
509
510 if (verbose || loops == 0)
511 {
512 if (0 != ret)
513 {
514 printf("ERROR %d: command failed\n", ret);
515 printf(" cc:0x%02x\n", cc);
516 }
517 else
518 {
519 printf(" cc:0x%02x 0x%0*llx\n", cc, u8Size * 2,
520 (unsigned long long)u64MsrVal);
521 }
522 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700523 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700524 if (looped)
525 {
526 printLoopSummary(ccCounts);
527 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700528 }
529 else if (strcmp(cmd, "rdpciconfig") == 0)
530 {
531 index = argc;
532 switch (argc - optind)
533 {
534 case 4:
535 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
536 /* FALLTHROUGH */
537 case 3:
538 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
539 /* FALLTHROUGH */
540 case 2:
541 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
542 /* FALLTHROUGH */
543 case 1:
544 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
545 break;
546 default:
547 printf("ERROR: Unsupported arguments for PCI Read\n");
548 goto ErrorExit;
549 break;
550 }
551 if (verbose)
552 {
553 printf("PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus, u8PciDev,
554 u8PciFunc, u16PciReg);
555 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700556 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700557 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700558 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530559 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800560 ret = peci_RdPCIConfig_dom(address, domainId, u8PciBus, u8PciDev,
561 u8PciFunc, u16PciReg,
562 (uint8_t*)&u32PciReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530563 timeSpent = getTimeDifference(begin);
564 if (verbose && measureTime)
565 {
566 printf("\nTime taken in iteration %d = %lf s\n",
567 (loopCount - loops), timeSpent);
568 }
569 totalTimeSpent += timeSpent;
570
Jason M. Billsff44e542021-04-05 08:11:52 -0700571 ccCounts[cc]++;
572
573 if (verbose || loops == 0)
574 {
575 if (0 != ret)
576 {
577 printf("ERROR %d: command failed\n", ret);
578 printf(" cc:0x%02x\n", cc);
579 }
580 else
581 {
582 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
583 u32PciReadVal);
584 }
585 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700586 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700587 if (looped)
588 {
589 printLoopSummary(ccCounts);
590 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700591 }
592 else if (strcmp(cmd, "rdpciconfiglocal") == 0)
593 {
594 index = argc;
595 switch (argc - optind)
596 {
597 case 4:
598 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
599 /* FALLTHROUGH */
600 case 3:
601 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
602 /* FALLTHROUGH */
603 case 2:
604 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
605 /* FALLTHROUGH */
606 case 1:
607 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
608 break;
609 default:
610 printf("ERROR: Unsupported arguments for Local PCI Read\n");
611 goto ErrorExit;
612 break;
613 }
614 if (verbose)
615 {
616 printf("Local PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus,
617 u8PciDev, u8PciFunc, u16PciReg);
618 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700619 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700620 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700621 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530622 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800623 ret = peci_RdPCIConfigLocal_dom(
624 address, domainId, u8PciBus, u8PciDev, u8PciFunc, u16PciReg,
625 u8Size, (uint8_t*)&u32PciReadVal, &cc);
Jason M. Billsff44e542021-04-05 08:11:52 -0700626 ccCounts[cc]++;
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530627 timeSpent = getTimeDifference(begin);
628 if (verbose && measureTime)
629 {
630 printf("\nTime taken in iteration %d = %lf s\n",
631 (loopCount - loops), timeSpent);
632 }
633 totalTimeSpent += timeSpent;
Jason M. Billsff44e542021-04-05 08:11:52 -0700634
635 if (verbose || loops == 0)
636 {
637 if (0 != ret)
638 {
639 printf("ERROR %d: command failed\n", ret);
640 printf(" cc:0x%02x\n", cc);
641 }
642 else
643 {
644 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
645 u32PciReadVal);
646 }
647 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700648 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700649 if (looped)
650 {
651 printLoopSummary(ccCounts);
652 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700653 }
654 else if (strcmp(cmd, "wrpciconfiglocal") == 0)
655 {
656 index = argc;
Jason M. Bills62cbc712020-05-07 14:07:49 -0700657 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700658 switch (argc - optind)
659 {
660 case 5:
661 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
662 /* FALLTHROUGH */
663 case 4:
664 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
665 /* FALLTHROUGH */
666 case 3:
667 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
668 /* FALLTHROUGH */
669 case 2:
670 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
671 break;
672 default:
673 printf("ERROR: Unsupported arguments for Local PCI Write\n");
674 goto ErrorExit;
675 break;
676 }
677 if (verbose)
678 {
679 printf("Local PCI Write of %02x:%02x:%02x Reg %02x: 0x%0*x\n",
680 u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
681 u32PciWriteVal);
682 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700683 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700684 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700685 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530686 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800687 ret = peci_WrPCIConfigLocal_dom(address, domainId, u8PciBus,
688 u8PciDev, u8PciFunc, u16PciReg,
689 u8Size, u32PciWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530690 timeSpent = getTimeDifference(begin);
691 if (verbose && measureTime)
692 {
693 printf("\nTime taken in iteration %d = %lf s\n",
694 (loopCount - loops), timeSpent);
695 }
696 totalTimeSpent += timeSpent;
697
Jason M. Billsff44e542021-04-05 08:11:52 -0700698 ccCounts[cc]++;
699
700 if (verbose || loops == 0)
701 {
702 if (0 != ret)
703 {
704 printf("ERROR %d: command failed\n", ret);
705 }
706 printf(" cc:0x%02x\n", cc);
707 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700708 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700709 if (looped)
710 {
711 printLoopSummary(ccCounts);
712 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700713 }
714 else if (strcmp(cmd, "rdendpointconfigpcilocal") == 0)
715 {
716 index = argc;
717 switch (argc - optind)
718 {
719 case 5:
720 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
721 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
722 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
723 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
724 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
725 break;
726
727 default:
728 printf("ERROR: Unsupported arguments for Endpoint Local PCI "
729 "Read\n");
730 goto ErrorExit;
731 }
732 if (verbose)
733 {
734 printf(
735 "Endpoint Local PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
736 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
737 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700738 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700739 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700740 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530741 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800742 ret = peci_RdEndPointConfigPciLocal_dom(
743 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
744 u16PciReg, u8Size, (uint8_t*)&u32PciReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530745 timeSpent = getTimeDifference(begin);
746 if (verbose && measureTime)
747 {
748 printf("\nTime taken in iteration %d = %lf s\n",
749 (loopCount - loops), timeSpent);
750 }
751 totalTimeSpent += timeSpent;
752
Jason M. Billsff44e542021-04-05 08:11:52 -0700753 ccCounts[cc]++;
754
755 if (verbose || loops == 0)
756 {
757 if (0 != ret)
758 {
759 printf("ERROR %d: command failed\n", ret);
760 printf(" cc:0x%02x\n", cc);
761 }
762 else
763 {
764 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
765 u32PciReadVal);
766 }
767 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700768 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700769 if (looped)
770 {
771 printLoopSummary(ccCounts);
772 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700773 }
774 else if (strcmp(cmd, "wrendpointconfigpcilocal") == 0)
775 {
776 index = argc;
777 switch (argc - optind)
778 {
779 case 6:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700780 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700781 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
782 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
783 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
784 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
785 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
786 break;
787
788 default:
789 printf("ERROR: Unsupported arguments for Endpoint Local PCI "
790 "Write\n");
791 goto ErrorExit;
792 }
793 if (verbose)
794 {
795 printf("Endpoint Local PCI Write of Seg:%02x %02x:%02x:%02x Reg "
796 "%02x: 0x%0*x\n",
797 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
798 u32PciWriteVal);
799 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700800 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700801 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700802 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530803 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800804 ret = peci_WrEndPointPCIConfigLocal_dom(
805 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
806 u16PciReg, u8Size, u32PciWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530807 timeSpent = getTimeDifference(begin);
808 if (verbose && measureTime)
809 {
810 printf("\nTime taken in iteration %d = %lf s\n",
811 (loopCount - loops), timeSpent);
812 }
813 totalTimeSpent += timeSpent;
814
Jason M. Billsff44e542021-04-05 08:11:52 -0700815 ccCounts[cc]++;
816
817 if (verbose || loops == 0)
818 {
819 if (0 != ret)
820 {
821 printf("ERROR %d: command failed\n", ret);
822 }
823 printf(" cc:0x%02x\n", cc);
824 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700825 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700826 if (looped)
827 {
828 printLoopSummary(ccCounts);
829 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700830 }
831 else if (strcmp(cmd, "rdendpointconfigpci") == 0)
832 {
833 index = argc;
834 switch (argc - optind)
835 {
836 case 5:
837 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
838 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
839 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
840 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
841 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
842 break;
843
844 default:
845 printf("ERROR: Unsupported arguments for Endpoint PCI Read\n");
846 goto ErrorExit;
847 }
848 if (verbose)
849 {
850 printf("Endpoint PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
851 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
852 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700853 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700854 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700855 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530856 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800857 ret = peci_RdEndPointConfigPci_dom(
858 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
859 u16PciReg, u8Size, (uint8_t*)&u32PciReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530860 timeSpent = getTimeDifference(begin);
861 if (verbose && measureTime)
862 {
863 printf("\nTime taken in iteration %d = %lf s\n",
864 (loopCount - loops), timeSpent);
865 }
866 totalTimeSpent += timeSpent;
867
Jason M. Billsff44e542021-04-05 08:11:52 -0700868 ccCounts[cc]++;
869
870 if (verbose || loops == 0)
871 {
872 if (0 != ret)
873 {
874 printf("ERROR %d: command failed\n", ret);
875 printf(" cc:0x%02x\n", cc);
876 }
877 else
878 {
879 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
880 u32PciReadVal);
881 }
882 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700883 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700884 if (looped)
885 {
886 printLoopSummary(ccCounts);
887 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700888 }
889 else if (strcmp(cmd, "wrendpointconfigpci") == 0)
890 {
891 index = argc;
892 switch (argc - optind)
893 {
894 case 6:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700895 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700896 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
897 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
898 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
899 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
900 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
901 break;
902
903 default:
904 printf("ERROR: Unsupported arguments for Endpoint PCI Write\n");
905 goto ErrorExit;
906 }
907 if (verbose)
908 {
909 printf("Endpoint PCI Write of Seg:%02x %02x:%02x:%02x Reg %02x: "
910 "0x%0*x\n",
911 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
912 u32PciWriteVal);
913 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700914 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700915 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700916 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530917 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800918 ret = peci_WrEndPointPCIConfig_dom(
919 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
920 u16PciReg, u8Size, u32PciWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530921 timeSpent = getTimeDifference(begin);
922 if (verbose && measureTime)
923 {
924 printf("\nTime taken in iteration %d = %lf s\n",
925 (loopCount - loops), timeSpent);
926 }
927 totalTimeSpent += timeSpent;
928
Jason M. Billsff44e542021-04-05 08:11:52 -0700929 ccCounts[cc]++;
930
931 if (verbose || loops == 0)
932 {
933 if (0 != ret)
934 {
935 printf("ERROR %d: command failed\n", ret);
936 }
937 printf(" cc:0x%02x\n", cc);
938 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700939 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700940 if (looped)
941 {
942 printLoopSummary(ccCounts);
943 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700944 }
945 else if (strcmp(cmd, "rdendpointconfigmmio") == 0)
946 {
947 index = argc;
948 switch (argc - optind)
949 {
950 case 7:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700951 u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700952 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
953 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
954 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
955 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
956 u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
957 u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
958 break;
959
960 default:
961 printf("ERROR: Unsupported arguments for Endpoint MMIO Read\n");
962 goto ErrorExit;
963 }
964 if (verbose)
965 {
966 printf("Endpoint MMIO Read of Seg:%02x %02x:%02x:%02x AType:%02x "
967 "Bar:%02x Offset:0x%" PRIx64 "\n",
968 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
969 u64Offset);
970 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700971 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700972 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -0700973 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530974 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800975 ret = peci_RdEndPointConfigMmio_dom(
976 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
Matt Simmeringc4c3fee2023-06-12 17:06:57 -0700977 u8AddrType, u64Offset, u8Size, (uint8_t*)&u64MmioReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530978 timeSpent = getTimeDifference(begin);
979 if (verbose && measureTime)
980 {
981 printf("\nTime taken in iteration %d = %lf s\n",
982 (loopCount - loops), timeSpent);
983 }
984 totalTimeSpent += timeSpent;
985
Jason M. Billsff44e542021-04-05 08:11:52 -0700986 ccCounts[cc]++;
987
988 if (verbose || loops == 0)
989 {
990 if (0 != ret)
991 {
992 printf("ERROR %d: command failed\n", ret);
993 printf(" cc:0x%02x\n", cc);
994 }
995 else
996 {
Matt Simmeringc4c3fee2023-06-12 17:06:57 -0700997 printf(" cc:0x%02x 0x%0*" PRIx64 "\n", cc, u8Size * 2,
998 u64MmioReadVal);
Jason M. Billsff44e542021-04-05 08:11:52 -0700999 }
1000 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001001 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001002 if (looped)
1003 {
1004 printLoopSummary(ccCounts);
1005 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001006 }
1007 else if (strcmp(cmd, "wrendpointconfigmmio") == 0)
1008 {
1009 index = argc;
1010 switch (argc - optind)
1011 {
1012 case 8:
Jason M. Bills62cbc712020-05-07 14:07:49 -07001013 u64MmioWriteVal = (uint64_t)strtoull(argv[--index], NULL, 0);
1014 u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001015 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
1016 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
1017 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
1018 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
1019 u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
1020 u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
1021 break;
1022
1023 default:
1024 printf(
1025 "ERROR: Unsupported arguments for Endpoint MMIO Write\n");
1026 goto ErrorExit;
1027 }
1028 if (verbose)
1029 {
1030 printf("Endpoint MMIO Write of Seg:%02x %02x:%02x:%02x AType:%02x "
1031 "Bar:%02x Offset:0x%" PRIx64 ": 0x%0*" PRIx64 "\n",
1032 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
1033 u64Offset, u8Size * 2, u64MmioWriteVal);
1034 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001035 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001036 {
Jason M. Bills5f1d7f72023-05-12 13:58:35 -07001037 cc = 0; // reset the cc for each loop
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301038 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -08001039 ret = peci_WrEndPointConfigMmio_dom(
1040 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
Jason M. Billsff44e542021-04-05 08:11:52 -07001041 u8AddrType, u64Offset, u8Size, u64MmioWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301042 timeSpent = getTimeDifference(begin);
1043 if (verbose && measureTime)
1044 {
1045 printf("\nTime taken in iteration %d = %lf s\n",
1046 (loopCount - loops), timeSpent);
1047 }
1048 totalTimeSpent += timeSpent;
1049
Jason M. Billsff44e542021-04-05 08:11:52 -07001050 ccCounts[cc]++;
1051
1052 if (verbose || loops == 0)
1053 {
1054 if (0 != ret)
1055 {
1056 printf("ERROR %d: command failed\n", ret);
1057 }
1058 printf(" cc:0x%02x\n", cc);
1059 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001060 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001061 if (looped)
1062 {
1063 printLoopSummary(ccCounts);
1064 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001065 }
1066 else if (strcmp(cmd, "raw") == 0)
1067 {
1068 if ((argc - optind) < 3)
1069 {
1070 printf("ERROR: Unsupported arguments for raw command\n");
1071 goto ErrorExit;
1072 }
1073
1074 // Address is provided in the first byte of the PECI command
1075 uint8_t rawAddr = (uint8_t)strtoul(argv[optind++], NULL, 0);
1076 // Write length is provided in the second byte of the PECI command
1077 uint8_t writeLength = (uint8_t)strtoul(argv[optind++], NULL, 0);
1078 // Read length is provided in the third byte of the PECI command
1079 uint8_t readLength = (uint8_t)strtoul(argv[optind++], NULL, 0);
1080
Jason M. Billsc965e722020-07-06 15:49:14 -07001081 // remaining parameters should fit within write length
1082 if ((argc - optind) > writeLength)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001083 {
1084 printf("ERROR: Incorrect write length for raw command\n");
1085 goto ErrorExit;
1086 }
1087 uint8_t* rawCmd = (uint8_t*)calloc(writeLength, sizeof(uint8_t));
1088 if (rawCmd == NULL)
1089 {
1090 // calloc failed, abort the sequence
1091 printf("Raw command memory allocation failed\n");
1092 return 1;
1093 }
Jason M. Billsc965e722020-07-06 15:49:14 -07001094 for (i = 0; i < (argc - optind); i++)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001095 {
1096 rawCmd[i] = (uint8_t)strtoul(argv[i + optind], NULL, 0);
1097 }
1098 if (verbose)
1099 {
1100 printf("Raw command: %02x %02x %02x ", rawAddr, writeLength,
1101 readLength);
1102 for (i = 0; i < writeLength; i++)
1103 {
1104 printf("0x%02x ", rawCmd[i]);
1105 }
1106 printf("\n");
1107 }
1108
1109 uint8_t* rawResp = (uint8_t*)calloc(readLength, sizeof(uint8_t));
1110 if (rawResp == NULL)
1111 {
1112 // calloc failed, abort the sequence
1113 printf("Raw command memory allocation failed\n");
1114 free(rawCmd);
1115 return 1;
1116 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001117 while (loops--)
Jason M. Bills0e21dde2020-07-06 14:51:53 -07001118 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301119 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -07001120 ret = peci_raw(rawAddr, readLength, rawCmd, writeLength, rawResp,
1121 readLength);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301122 timeSpent = getTimeDifference(begin);
1123 if (verbose && measureTime)
1124 {
1125 printf("\nTime taken in iteration %d = %lf s\n",
1126 (loopCount - loops), timeSpent);
1127 }
1128 totalTimeSpent += timeSpent;
1129
Jason M. Billsff44e542021-04-05 08:11:52 -07001130 if (verbose)
1131 {
1132 printf(" ");
1133 for (i = 0; i < readLength; i++)
1134 {
1135 printf("0x%02x ", rawResp[i]);
1136 }
1137 printf("\n");
1138 }
1139 ccCounts[rawResp[0]]++;
1140 }
1141 if (!verbose)
1142 {
Patrick Williamsb65de2d2023-05-26 15:14:02 -05001143 if (PECI_CC_SUCCESS != ret)
Jason M. Billsff44e542021-04-05 08:11:52 -07001144 {
1145 printf("ERROR %d: command failed\n", ret);
1146 }
1147 printf(" ");
Jason M. Bills0e21dde2020-07-06 14:51:53 -07001148 for (i = 0; i < readLength; i++)
1149 {
1150 printf("0x%02x ", rawResp[i]);
1151 }
1152 printf("\n");
1153 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001154
1155 if (looped)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001156 {
Jason M. Billsff44e542021-04-05 08:11:52 -07001157 printLoopSummary(ccCounts);
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001158 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001159
1160 free(rawCmd);
1161 free(rawResp);
1162 }
1163 else
1164 {
1165 printf("ERROR: Unrecognized command\n");
1166 goto ErrorExit;
1167 }
1168
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301169 if (measureTime)
1170 {
1171 printf("Total time taken = %lf seconds\n", totalTimeSpent);
1172 if (loopCount > 1)
1173 {
1174 printf("Average time taken per command = %lf seconds\n",
1175 totalTimeSpent / loopCount);
1176 }
1177 }
1178
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001179 return 0;
1180
1181ErrorExit:
1182 Usage(argv[0]);
1183 return 1;
1184}