blob: ca4f469c441fba278ba3838da79cebe0e326abc7 [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;
38 long seconds = 0;
39 long nanoseconds = 0;
40
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;
122 EPECIStatus ret;
123 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;
127 uint8_t u8Seg = 0;
128 uint8_t u8Bar = 0;
129 uint8_t u8AddrType = 0;
130 uint8_t u8PciBus = 0;
131 uint8_t u8PciDev = 0;
132 uint8_t u8PciFunc = 0;
133 uint16_t u16PciReg = 0;
134 uint64_t u64Offset = 0;
135 uint32_t u32PciWriteVal = 0;
136 uint64_t u64MmioWriteVal = 0;
137 uint8_t u8PkgIndex = 0;
138 uint16_t u16PkgParam = 0;
139 uint32_t u32PkgValue = 0;
140 uint8_t u8MsrThread = 0;
141 uint16_t u16MsrAddr = 0;
142 uint64_t u64MsrVal = 0;
143 short temperature;
144 uint64_t dib;
145 int index = 0;
146 uint8_t cc = 0;
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530147 bool measureTime = false;
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700148 bool verbose = false;
Jason M. Billsff44e542021-04-05 08:11:52 -0700149 bool looped = false;
150 uint32_t loops = 1;
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530151 uint32_t loopCount = 1;
Nirav Shahfd5dfd52022-03-09 12:29:41 -0800152 uint32_t ccCounts[CC_COUNT] = {0};
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530153 struct timespec begin;
154 double timeSpent = 0.0;
155 double totalTimeSpent = 0.0;
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700156
157 //
158 // Parse arguments.
159 //
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530160 while (-1 != (c = getopt(argc, argv, "hvtl:a:i:s:d:")))
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700161 {
162 switch (c)
163 {
164 case 'h':
165 Usage(argv[0]);
166 return 0;
167 break;
168
169 case 'v':
170 verbose = true;
171 break;
172
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530173 case 't':
174 measureTime = true;
175 break;
176
Jason M. Billsff44e542021-04-05 08:11:52 -0700177 case 'l':
178 looped = true;
179 errno = 0;
180 if (optarg != NULL)
181 loops = (uint32_t)strtoul(optarg, NULL, 0);
182 if (!loops || errno)
183 {
184 printf("ERROR: Invalid loop count\n");
185 if (errno)
186 perror("");
187 goto ErrorExit;
188 }
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530189 loopCount =
190 loops; // Preserve a copy for average time measurement
Jason M. Billsff44e542021-04-05 08:11:52 -0700191 break;
192
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700193 case 'a':
194 if (optarg != NULL)
195 address = (uint8_t)strtoul(optarg, NULL, 0);
196 if (address < MIN_CLIENT_ADDR || address > MAX_CLIENT_ADDR)
197 {
198 printf("ERROR: Invalid address \"0x%x\"\n", address);
199 goto ErrorExit;
200 }
201
202 break;
203
Jason M. Bills7b112802022-03-01 13:28:56 -0800204 case 'i':
205 if (optarg != NULL)
206 domainId = (uint8_t)strtoul(optarg, NULL, 0);
207 if (domainId >= 128) // Domain ID is only 7 bits
208 {
209 printf("ERROR: Invalid domain ID \"%d\"\n", domainId);
210 goto ErrorExit;
211 }
212 break;
213
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700214 case 's':
215 if (optarg != NULL)
216 u8Size = (uint8_t)strtoul(optarg, NULL, 0);
217 if (u8Size != 1 && u8Size != 2 && u8Size != 4 && u8Size != 8 &&
218 u8Size != 16)
219 {
220 printf("ERROR: Invalid size \"%d\"\n", u8Size);
221 goto ErrorExit;
222 }
223 break;
224
Anna Platash03d7dae2021-02-05 13:52:05 +0100225 case 'd':
226 peci_SetDevName(optarg);
227 break;
228
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700229 default:
230 printf("ERROR: Unrecognized option \"-%c\"\n", optopt);
231 goto ErrorExit;
232 break;
233 }
234 }
235
236 // Get the command from the first parameter
237 cmd = argv[optind++];
238 if (cmd == NULL)
239 {
240 Usage(argv[0]);
241 return 0;
242 }
243
244 // Allow any case
245 while (cmd[i])
246 {
247 cmd[i] = (char)tolower((int)cmd[i]);
248 i++;
249 }
250
251 //
252 // Execute the command
253 //
254 if (verbose)
255 {
256 printf("PECI target[0x%x]: ", address);
257 }
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530258
259 if (measureTime)
260 {
261 if (verbose && (loopCount > 1))
262 {
263 printf("Warning: Request-response time measurement with verbose "
264 "mode can affect the time between consecutive commands in "
265 "looped mode!\n");
266 }
267 }
268
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700269 if (strcmp(cmd, "ping") == 0)
270 {
271 if (verbose)
272 {
273 printf("Pinging ... ");
274 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700275 while (loops--)
276 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530277 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -0700278 ret = peci_Ping(address);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530279 timeSpent = getTimeDifference(begin);
280 if (verbose && measureTime)
281 {
282 printf("\nTime taken in iteration %d = %lf s\n",
283 (loopCount - loops), timeSpent);
284 }
285 totalTimeSpent += timeSpent;
286
Jason M. Billsff44e542021-04-05 08:11:52 -0700287 if (verbose || loops == 0)
288 {
289 if (0 != ret)
290 {
291 printf("Failed\n");
292 }
293 else
294 {
295 printf("Succeeded\n");
296 }
297 }
298 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700299 }
300 else if (strcmp(cmd, "getdib") == 0)
301 {
302 if (verbose)
303 {
304 printf("GetDIB\n");
305 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700306 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700307 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530308 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -0700309 ret = peci_GetDIB(address, &dib);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530310 timeSpent = getTimeDifference(begin);
311 if (verbose && measureTime)
312 {
313 printf("\nTime taken in iteration %d = %lf s\n",
314 (loopCount - loops), timeSpent);
315 }
316 totalTimeSpent += timeSpent;
317
Jason M. Billsff44e542021-04-05 08:11:52 -0700318 if (verbose || loops == 0)
319 {
320 if (0 != ret)
321 {
322 printf("ERROR %d: Retrieving DIB failed\n", ret);
323 }
324 else
325 {
326 printf(" 0x%" PRIx64 "\n", dib);
327 }
328 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700329 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700330 }
331
332 else if (strcmp(cmd, "gettemp") == 0)
333 {
334 if (verbose)
335 {
336 printf("GetTemp\n");
337 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700338 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700339 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530340 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -0700341 ret = peci_GetTemp(address, &temperature);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530342 timeSpent = getTimeDifference(begin);
343 if (verbose && measureTime)
344 {
345 printf("\nTime taken in iteration %d = %lf s\n",
346 (loopCount - loops), timeSpent);
347 }
348 totalTimeSpent += timeSpent;
349
Jason M. Billsff44e542021-04-05 08:11:52 -0700350 if (verbose || loops == 0)
351 {
352 if (0 != ret)
353 {
354 printf("ERROR %d: Retrieving temperature failed\n", ret);
355 }
356 else
357 {
358 printf(" %04xh (%c%d.%02dC)\n",
359 (int)(unsigned int)(unsigned short)temperature,
360 (0 > temperature) ? '-' : '+',
361 (int)((unsigned int)ABS(temperature) / 64),
362 (int)(((unsigned int)ABS(temperature) % 64) * 100) /
363 64);
364 }
365 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700366 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700367 }
368
369 else if (strcmp(cmd, "rdpkgconfig") == 0)
370 {
371 index = argc;
372 switch (argc - optind)
373 {
374 case 2:
375 u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
376 u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
377 break;
378 default:
379 printf("ERROR: Unsupported arguments for Pkg Read\n");
380 goto ErrorExit;
381 break;
382 }
383 if (verbose)
384 {
385 printf("Pkg Read of Index %02x Param %04x\n", u8PkgIndex,
386 u16PkgParam);
387 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700388 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700389 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530390 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800391 ret =
392 peci_RdPkgConfig_dom(address, domainId, u8PkgIndex, u16PkgParam,
393 u8Size, (uint8_t*)&u32PkgValue, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530394 timeSpent = getTimeDifference(begin);
395 if (verbose && measureTime)
396 {
397 printf("\nTime taken in iteration %d = %lf s\n",
398 (loopCount - loops), timeSpent);
399 }
400 totalTimeSpent += timeSpent;
401
Jason M. Billsff44e542021-04-05 08:11:52 -0700402 ccCounts[cc]++;
403
404 if (verbose || loops == 0)
405 {
406 if (0 != ret)
407 {
408 printf("ERROR %d: command failed\n", ret);
409 printf(" cc:0x%02x\n", cc);
410 }
411 else
412 {
413 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
414 u32PkgValue);
415 }
416 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700417 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700418 if (looped)
419 {
420 printLoopSummary(ccCounts);
421 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700422 }
423 else if (strcmp(cmd, "wrpkgconfig") == 0)
424 {
425 index = argc;
426 switch (argc - optind)
427 {
428 case 3:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700429 u32PkgValue = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700430 u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
431 u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
432 break;
433 default:
434 printf("ERROR: Unsupported arguments for Pkg Write\n");
435 goto ErrorExit;
436 break;
437 }
438 if (verbose)
439 {
440 printf("Pkg Write of Index %02x Param %04x: 0x%0*x\n", u8PkgIndex,
441 u16PkgParam, u8Size * 2, u32PkgValue);
442 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700443 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700444 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530445 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800446 ret = peci_WrPkgConfig_dom(address, domainId, u8PkgIndex,
447 u16PkgParam, u32PkgValue, u8Size, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530448 timeSpent = getTimeDifference(begin);
449 if (verbose && measureTime)
450 {
451 printf("\nTime taken in iteration %d = %lf s\n",
452 (loopCount - loops), timeSpent);
453 }
454 totalTimeSpent += timeSpent;
455
Jason M. Billsff44e542021-04-05 08:11:52 -0700456 ccCounts[cc]++;
457
458 if (verbose || loops == 0)
459 {
460 if (0 != ret)
461 {
462 printf("ERROR %d: command failed\n", ret);
463 }
464 printf(" cc:0x%02x\n", cc);
465 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700466 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700467 if (looped)
468 {
469 printLoopSummary(ccCounts);
470 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700471 }
472 else if (strcmp(cmd, "rdiamsr") == 0)
473 {
474 index = argc;
475 switch (argc - optind)
476 {
477 case 2:
478 u16MsrAddr = (uint16_t)strtoul(argv[--index], NULL, 0);
479 u8MsrThread = (uint8_t)strtoul(argv[--index], NULL, 0);
480 break;
481 default:
482 printf("ERROR: Unsupported arguments for MSR Read\n");
483 goto ErrorExit;
484 break;
485 }
486 if (verbose)
487 {
488 printf("MSR Read of Thread %02x MSR %04x\n", u8MsrThread,
489 u16MsrAddr);
490 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700491 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700492 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530493 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800494 ret = peci_RdIAMSR_dom(address, domainId, u8MsrThread, u16MsrAddr,
495 &u64MsrVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530496 timeSpent = getTimeDifference(begin);
497 if (verbose && measureTime)
498 {
499 printf("\nTime taken in iteration %d = %lf s\n",
500 (loopCount - loops), timeSpent);
501 }
502 totalTimeSpent += timeSpent;
503
Jason M. Billsff44e542021-04-05 08:11:52 -0700504 ccCounts[cc]++;
505
506 if (verbose || loops == 0)
507 {
508 if (0 != ret)
509 {
510 printf("ERROR %d: command failed\n", ret);
511 printf(" cc:0x%02x\n", cc);
512 }
513 else
514 {
515 printf(" cc:0x%02x 0x%0*llx\n", cc, u8Size * 2,
516 (unsigned long long)u64MsrVal);
517 }
518 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700519 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700520 if (looped)
521 {
522 printLoopSummary(ccCounts);
523 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700524 }
525 else if (strcmp(cmd, "rdpciconfig") == 0)
526 {
527 index = argc;
528 switch (argc - optind)
529 {
530 case 4:
531 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
532 /* FALLTHROUGH */
533 case 3:
534 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
535 /* FALLTHROUGH */
536 case 2:
537 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
538 /* FALLTHROUGH */
539 case 1:
540 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
541 break;
542 default:
543 printf("ERROR: Unsupported arguments for PCI Read\n");
544 goto ErrorExit;
545 break;
546 }
547 if (verbose)
548 {
549 printf("PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus, u8PciDev,
550 u8PciFunc, u16PciReg);
551 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700552 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700553 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530554 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800555 ret = peci_RdPCIConfig_dom(address, domainId, u8PciBus, u8PciDev,
556 u8PciFunc, u16PciReg,
557 (uint8_t*)&u32PciReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530558 timeSpent = getTimeDifference(begin);
559 if (verbose && measureTime)
560 {
561 printf("\nTime taken in iteration %d = %lf s\n",
562 (loopCount - loops), timeSpent);
563 }
564 totalTimeSpent += timeSpent;
565
Jason M. Billsff44e542021-04-05 08:11:52 -0700566 ccCounts[cc]++;
567
568 if (verbose || loops == 0)
569 {
570 if (0 != ret)
571 {
572 printf("ERROR %d: command failed\n", ret);
573 printf(" cc:0x%02x\n", cc);
574 }
575 else
576 {
577 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
578 u32PciReadVal);
579 }
580 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700581 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700582 if (looped)
583 {
584 printLoopSummary(ccCounts);
585 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700586 }
587 else if (strcmp(cmd, "rdpciconfiglocal") == 0)
588 {
589 index = argc;
590 switch (argc - optind)
591 {
592 case 4:
593 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
594 /* FALLTHROUGH */
595 case 3:
596 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
597 /* FALLTHROUGH */
598 case 2:
599 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
600 /* FALLTHROUGH */
601 case 1:
602 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
603 break;
604 default:
605 printf("ERROR: Unsupported arguments for Local PCI Read\n");
606 goto ErrorExit;
607 break;
608 }
609 if (verbose)
610 {
611 printf("Local PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus,
612 u8PciDev, u8PciFunc, u16PciReg);
613 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700614 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700615 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530616 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800617 ret = peci_RdPCIConfigLocal_dom(
618 address, domainId, u8PciBus, u8PciDev, u8PciFunc, u16PciReg,
619 u8Size, (uint8_t*)&u32PciReadVal, &cc);
Jason M. Billsff44e542021-04-05 08:11:52 -0700620 ccCounts[cc]++;
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530621 timeSpent = getTimeDifference(begin);
622 if (verbose && measureTime)
623 {
624 printf("\nTime taken in iteration %d = %lf s\n",
625 (loopCount - loops), timeSpent);
626 }
627 totalTimeSpent += timeSpent;
Jason M. Billsff44e542021-04-05 08:11:52 -0700628
629 if (verbose || loops == 0)
630 {
631 if (0 != ret)
632 {
633 printf("ERROR %d: command failed\n", ret);
634 printf(" cc:0x%02x\n", cc);
635 }
636 else
637 {
638 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
639 u32PciReadVal);
640 }
641 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700642 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700643 if (looped)
644 {
645 printLoopSummary(ccCounts);
646 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700647 }
648 else if (strcmp(cmd, "wrpciconfiglocal") == 0)
649 {
650 index = argc;
Jason M. Bills62cbc712020-05-07 14:07:49 -0700651 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700652 switch (argc - optind)
653 {
654 case 5:
655 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
656 /* FALLTHROUGH */
657 case 4:
658 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
659 /* FALLTHROUGH */
660 case 3:
661 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
662 /* FALLTHROUGH */
663 case 2:
664 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
665 break;
666 default:
667 printf("ERROR: Unsupported arguments for Local PCI Write\n");
668 goto ErrorExit;
669 break;
670 }
671 if (verbose)
672 {
673 printf("Local PCI Write of %02x:%02x:%02x Reg %02x: 0x%0*x\n",
674 u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
675 u32PciWriteVal);
676 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700677 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700678 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530679 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800680 ret = peci_WrPCIConfigLocal_dom(address, domainId, u8PciBus,
681 u8PciDev, u8PciFunc, u16PciReg,
682 u8Size, u32PciWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530683 timeSpent = getTimeDifference(begin);
684 if (verbose && measureTime)
685 {
686 printf("\nTime taken in iteration %d = %lf s\n",
687 (loopCount - loops), timeSpent);
688 }
689 totalTimeSpent += timeSpent;
690
Jason M. Billsff44e542021-04-05 08:11:52 -0700691 ccCounts[cc]++;
692
693 if (verbose || loops == 0)
694 {
695 if (0 != ret)
696 {
697 printf("ERROR %d: command failed\n", ret);
698 }
699 printf(" cc:0x%02x\n", cc);
700 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700701 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700702 if (looped)
703 {
704 printLoopSummary(ccCounts);
705 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700706 }
707 else if (strcmp(cmd, "rdendpointconfigpcilocal") == 0)
708 {
709 index = argc;
710 switch (argc - optind)
711 {
712 case 5:
713 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
714 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
715 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
716 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
717 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
718 break;
719
720 default:
721 printf("ERROR: Unsupported arguments for Endpoint Local PCI "
722 "Read\n");
723 goto ErrorExit;
724 }
725 if (verbose)
726 {
727 printf(
728 "Endpoint Local PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
729 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
730 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700731 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700732 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530733 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800734 ret = peci_RdEndPointConfigPciLocal_dom(
735 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
736 u16PciReg, u8Size, (uint8_t*)&u32PciReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530737 timeSpent = getTimeDifference(begin);
738 if (verbose && measureTime)
739 {
740 printf("\nTime taken in iteration %d = %lf s\n",
741 (loopCount - loops), timeSpent);
742 }
743 totalTimeSpent += timeSpent;
744
Jason M. Billsff44e542021-04-05 08:11:52 -0700745 ccCounts[cc]++;
746
747 if (verbose || loops == 0)
748 {
749 if (0 != ret)
750 {
751 printf("ERROR %d: command failed\n", ret);
752 printf(" cc:0x%02x\n", cc);
753 }
754 else
755 {
756 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
757 u32PciReadVal);
758 }
759 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700760 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700761 if (looped)
762 {
763 printLoopSummary(ccCounts);
764 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700765 }
766 else if (strcmp(cmd, "wrendpointconfigpcilocal") == 0)
767 {
768 index = argc;
769 switch (argc - optind)
770 {
771 case 6:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700772 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700773 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
774 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
775 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
776 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
777 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
778 break;
779
780 default:
781 printf("ERROR: Unsupported arguments for Endpoint Local PCI "
782 "Write\n");
783 goto ErrorExit;
784 }
785 if (verbose)
786 {
787 printf("Endpoint Local PCI Write of Seg:%02x %02x:%02x:%02x Reg "
788 "%02x: 0x%0*x\n",
789 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
790 u32PciWriteVal);
791 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700792 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700793 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530794 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800795 ret = peci_WrEndPointPCIConfigLocal_dom(
796 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
797 u16PciReg, u8Size, u32PciWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530798 timeSpent = getTimeDifference(begin);
799 if (verbose && measureTime)
800 {
801 printf("\nTime taken in iteration %d = %lf s\n",
802 (loopCount - loops), timeSpent);
803 }
804 totalTimeSpent += timeSpent;
805
Jason M. Billsff44e542021-04-05 08:11:52 -0700806 ccCounts[cc]++;
807
808 if (verbose || loops == 0)
809 {
810 if (0 != ret)
811 {
812 printf("ERROR %d: command failed\n", ret);
813 }
814 printf(" cc:0x%02x\n", cc);
815 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700816 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700817 if (looped)
818 {
819 printLoopSummary(ccCounts);
820 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700821 }
822 else if (strcmp(cmd, "rdendpointconfigpci") == 0)
823 {
824 index = argc;
825 switch (argc - optind)
826 {
827 case 5:
828 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
829 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
830 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
831 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
832 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
833 break;
834
835 default:
836 printf("ERROR: Unsupported arguments for Endpoint PCI Read\n");
837 goto ErrorExit;
838 }
839 if (verbose)
840 {
841 printf("Endpoint PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
842 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
843 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700844 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700845 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530846 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800847 ret = peci_RdEndPointConfigPci_dom(
848 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
849 u16PciReg, u8Size, (uint8_t*)&u32PciReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530850 timeSpent = getTimeDifference(begin);
851 if (verbose && measureTime)
852 {
853 printf("\nTime taken in iteration %d = %lf s\n",
854 (loopCount - loops), timeSpent);
855 }
856 totalTimeSpent += timeSpent;
857
Jason M. Billsff44e542021-04-05 08:11:52 -0700858 ccCounts[cc]++;
859
860 if (verbose || loops == 0)
861 {
862 if (0 != ret)
863 {
864 printf("ERROR %d: command failed\n", ret);
865 printf(" cc:0x%02x\n", cc);
866 }
867 else
868 {
869 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
870 u32PciReadVal);
871 }
872 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700873 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700874 if (looped)
875 {
876 printLoopSummary(ccCounts);
877 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700878 }
879 else if (strcmp(cmd, "wrendpointconfigpci") == 0)
880 {
881 index = argc;
882 switch (argc - optind)
883 {
884 case 6:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700885 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700886 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
887 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
888 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
889 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
890 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
891 break;
892
893 default:
894 printf("ERROR: Unsupported arguments for Endpoint PCI Write\n");
895 goto ErrorExit;
896 }
897 if (verbose)
898 {
899 printf("Endpoint PCI Write of Seg:%02x %02x:%02x:%02x Reg %02x: "
900 "0x%0*x\n",
901 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
902 u32PciWriteVal);
903 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700904 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700905 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530906 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800907 ret = peci_WrEndPointPCIConfig_dom(
908 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
909 u16PciReg, u8Size, u32PciWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530910 timeSpent = getTimeDifference(begin);
911 if (verbose && measureTime)
912 {
913 printf("\nTime taken in iteration %d = %lf s\n",
914 (loopCount - loops), timeSpent);
915 }
916 totalTimeSpent += timeSpent;
917
Jason M. Billsff44e542021-04-05 08:11:52 -0700918 ccCounts[cc]++;
919
920 if (verbose || loops == 0)
921 {
922 if (0 != ret)
923 {
924 printf("ERROR %d: command failed\n", ret);
925 }
926 printf(" cc:0x%02x\n", cc);
927 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700928 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700929 if (looped)
930 {
931 printLoopSummary(ccCounts);
932 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700933 }
934 else if (strcmp(cmd, "rdendpointconfigmmio") == 0)
935 {
936 index = argc;
937 switch (argc - optind)
938 {
939 case 7:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700940 u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700941 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
942 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
943 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
944 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
945 u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
946 u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
947 break;
948
949 default:
950 printf("ERROR: Unsupported arguments for Endpoint MMIO Read\n");
951 goto ErrorExit;
952 }
953 if (verbose)
954 {
955 printf("Endpoint MMIO Read of Seg:%02x %02x:%02x:%02x AType:%02x "
956 "Bar:%02x Offset:0x%" PRIx64 "\n",
957 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
958 u64Offset);
959 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700960 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700961 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530962 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -0800963 ret = peci_RdEndPointConfigMmio_dom(
964 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
Jason M. Billsff44e542021-04-05 08:11:52 -0700965 u8AddrType, u64Offset, u8Size, (uint8_t*)&u32PciReadVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +0530966 timeSpent = getTimeDifference(begin);
967 if (verbose && measureTime)
968 {
969 printf("\nTime taken in iteration %d = %lf s\n",
970 (loopCount - loops), timeSpent);
971 }
972 totalTimeSpent += timeSpent;
973
Jason M. Billsff44e542021-04-05 08:11:52 -0700974 ccCounts[cc]++;
975
976 if (verbose || loops == 0)
977 {
978 if (0 != ret)
979 {
980 printf("ERROR %d: command failed\n", ret);
981 printf(" cc:0x%02x\n", cc);
982 }
983 else
984 {
985 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
986 u32PciReadVal);
987 }
988 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700989 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700990 if (looped)
991 {
992 printLoopSummary(ccCounts);
993 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700994 }
995 else if (strcmp(cmd, "wrendpointconfigmmio") == 0)
996 {
997 index = argc;
998 switch (argc - optind)
999 {
1000 case 8:
Jason M. Bills62cbc712020-05-07 14:07:49 -07001001 u64MmioWriteVal = (uint64_t)strtoull(argv[--index], NULL, 0);
1002 u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001003 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
1004 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
1005 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
1006 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
1007 u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
1008 u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
1009 break;
1010
1011 default:
1012 printf(
1013 "ERROR: Unsupported arguments for Endpoint MMIO Write\n");
1014 goto ErrorExit;
1015 }
1016 if (verbose)
1017 {
1018 printf("Endpoint MMIO Write of Seg:%02x %02x:%02x:%02x AType:%02x "
1019 "Bar:%02x Offset:0x%" PRIx64 ": 0x%0*" PRIx64 "\n",
1020 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
1021 u64Offset, u8Size * 2, u64MmioWriteVal);
1022 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001023 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001024 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301025 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Bills7b112802022-03-01 13:28:56 -08001026 ret = peci_WrEndPointConfigMmio_dom(
1027 address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
Jason M. Billsff44e542021-04-05 08:11:52 -07001028 u8AddrType, u64Offset, u8Size, u64MmioWriteVal, &cc);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301029 timeSpent = getTimeDifference(begin);
1030 if (verbose && measureTime)
1031 {
1032 printf("\nTime taken in iteration %d = %lf s\n",
1033 (loopCount - loops), timeSpent);
1034 }
1035 totalTimeSpent += timeSpent;
1036
Jason M. Billsff44e542021-04-05 08:11:52 -07001037 ccCounts[cc]++;
1038
1039 if (verbose || loops == 0)
1040 {
1041 if (0 != ret)
1042 {
1043 printf("ERROR %d: command failed\n", ret);
1044 }
1045 printf(" cc:0x%02x\n", cc);
1046 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001047 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001048 if (looped)
1049 {
1050 printLoopSummary(ccCounts);
1051 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001052 }
1053 else if (strcmp(cmd, "raw") == 0)
1054 {
1055 if ((argc - optind) < 3)
1056 {
1057 printf("ERROR: Unsupported arguments for raw command\n");
1058 goto ErrorExit;
1059 }
1060
1061 // Address is provided in the first byte of the PECI command
1062 uint8_t rawAddr = (uint8_t)strtoul(argv[optind++], NULL, 0);
1063 // Write length is provided in the second byte of the PECI command
1064 uint8_t writeLength = (uint8_t)strtoul(argv[optind++], NULL, 0);
1065 // Read length is provided in the third byte of the PECI command
1066 uint8_t readLength = (uint8_t)strtoul(argv[optind++], NULL, 0);
1067
Jason M. Billsc965e722020-07-06 15:49:14 -07001068 // remaining parameters should fit within write length
1069 if ((argc - optind) > writeLength)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001070 {
1071 printf("ERROR: Incorrect write length for raw command\n");
1072 goto ErrorExit;
1073 }
1074 uint8_t* rawCmd = (uint8_t*)calloc(writeLength, sizeof(uint8_t));
1075 if (rawCmd == NULL)
1076 {
1077 // calloc failed, abort the sequence
1078 printf("Raw command memory allocation failed\n");
1079 return 1;
1080 }
Jason M. Billsc965e722020-07-06 15:49:14 -07001081 for (i = 0; i < (argc - optind); i++)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001082 {
1083 rawCmd[i] = (uint8_t)strtoul(argv[i + optind], NULL, 0);
1084 }
1085 if (verbose)
1086 {
1087 printf("Raw command: %02x %02x %02x ", rawAddr, writeLength,
1088 readLength);
1089 for (i = 0; i < writeLength; i++)
1090 {
1091 printf("0x%02x ", rawCmd[i]);
1092 }
1093 printf("\n");
1094 }
1095
1096 uint8_t* rawResp = (uint8_t*)calloc(readLength, sizeof(uint8_t));
1097 if (rawResp == NULL)
1098 {
1099 // calloc failed, abort the sequence
1100 printf("Raw command memory allocation failed\n");
1101 free(rawCmd);
1102 return 1;
1103 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001104 while (loops--)
Jason M. Bills0e21dde2020-07-06 14:51:53 -07001105 {
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301106 clock_gettime(CLOCK_REALTIME, &begin);
Jason M. Billsff44e542021-04-05 08:11:52 -07001107 ret = peci_raw(rawAddr, readLength, rawCmd, writeLength, rawResp,
1108 readLength);
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301109 timeSpent = getTimeDifference(begin);
1110 if (verbose && measureTime)
1111 {
1112 printf("\nTime taken in iteration %d = %lf s\n",
1113 (loopCount - loops), timeSpent);
1114 }
1115 totalTimeSpent += timeSpent;
1116
Jason M. Billsff44e542021-04-05 08:11:52 -07001117 if (verbose)
1118 {
1119 printf(" ");
1120 for (i = 0; i < readLength; i++)
1121 {
1122 printf("0x%02x ", rawResp[i]);
1123 }
1124 printf("\n");
1125 }
1126 ccCounts[rawResp[0]]++;
1127 }
1128 if (!verbose)
1129 {
1130 if (0 != ret)
1131 {
1132 printf("ERROR %d: command failed\n", ret);
1133 }
1134 printf(" ");
Jason M. Bills0e21dde2020-07-06 14:51:53 -07001135 for (i = 0; i < readLength; i++)
1136 {
1137 printf("0x%02x ", rawResp[i]);
1138 }
1139 printf("\n");
1140 }
Jason M. Billsff44e542021-04-05 08:11:52 -07001141
1142 if (looped)
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001143 {
Jason M. Billsff44e542021-04-05 08:11:52 -07001144 printLoopSummary(ccCounts);
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001145 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001146
1147 free(rawCmd);
1148 free(rawResp);
1149 }
1150 else
1151 {
1152 printf("ERROR: Unrecognized command\n");
1153 goto ErrorExit;
1154 }
1155
Sumanth Bhat1ae54d22023-03-01 23:03:03 +05301156 if (measureTime)
1157 {
1158 printf("Total time taken = %lf seconds\n", totalTimeSpent);
1159 if (loopCount > 1)
1160 {
1161 printf("Average time taken per command = %lf seconds\n",
1162 totalTimeSpent / loopCount);
1163 }
1164 }
1165
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001166 return 0;
1167
1168ErrorExit:
1169 Usage(argv[0]);
1170 return 1;
1171}