1 /**
2  * @file These are the implementations of the user interfaces declared
3  *       in hei_user_interface.hpp
4  */
5 
6 #include <assert.h>
7 #include <inttypes.h>
8 #include <libpdbg.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 
12 #include <hei_user_interface.hpp>
13 #include <util/pdbg.hpp>
14 #include <util/trace.hpp>
15 
16 namespace libhei
17 {
18 
19 const char* __regType(RegisterType_t i_regType)
20 {
21     const char* str = "";
22     switch (i_regType)
23     {
24         case REG_TYPE_SCOM:
25             str = "SCOM";
26             break;
27         case REG_TYPE_ID_SCOM:
28             str = "ID_SCOM";
29             break;
30         default:
31             trace::err("Unsupported register type: i_regType=0x%02x",
32                        i_regType);
33             assert(0);
34     }
35     return str;
36 }
37 
38 //------------------------------------------------------------------------------
39 
40 bool __readProc(pdbg_target* i_procTrgt, RegisterType_t i_regType,
41                 uint64_t i_address, uint64_t& o_value)
42 {
43     bool accessFailure = false;
44 
45     // The processor PIB target is required for SCOM access.
46     char path[16];
47     sprintf(path, "/proc%d/pib", pdbg_target_index(i_procTrgt));
48     pdbg_target* scomTrgt = pdbg_target_from_path(nullptr, path);
49     assert(nullptr != scomTrgt);
50 
51     switch (i_regType)
52     {
53         case REG_TYPE_SCOM:
54         case REG_TYPE_ID_SCOM:
55             // Read the 64-bit SCOM register.
56             accessFailure = (0 != pib_read(scomTrgt, i_address, &o_value));
57             break;
58 
59         default:
60             trace::err("Unsupported register type: trgt=%s regType=0x%02x "
61                        "addr=0x%0" PRIx64,
62                        util::pdbg::getPath(i_procTrgt), i_regType, i_address);
63             assert(0); // an unsupported register type
64     }
65 
66     return accessFailure;
67 }
68 
69 //------------------------------------------------------------------------------
70 
71 bool __readOcmb(pdbg_target* i_obmcTrgt, RegisterType_t i_regType,
72                 uint64_t i_address, uint64_t& o_value)
73 {
74     bool accessFailure = false;
75 
76     /* TODO: ocmb_getscom() currently does not exist upstream.
77     // The OCMB target is used for SCOM access.
78     pdbg_target* scomTrgt = i_obmcTrgt;
79 
80     switch (i_regType)
81     {
82         case REG_TYPE_SCOM:
83         case REG_TYPE_ID_SCOM:
84             // Read the 64-bit SCOM register.
85             accessFailure = (0 != ocmb_getscom(scomTrgt, i_address, &o_value));
86             break;
87 
88         default:
89             trace::err("Unsupported register type: trgt=%s regType=0x%02x "
90                        "addr=0x%0" PRIx64,
91                        util::pdbg::getPath(i_obmcTrgt), i_regType, i_address);
92             assert(0);
93     }
94     */
95 
96     return accessFailure;
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 bool registerRead(const Chip& i_chip, RegisterType_t i_regType,
102                   uint64_t i_address, uint64_t& o_value)
103 {
104     bool accessFailure = false;
105 
106     auto trgt = util::pdbg::getTrgt(i_chip);
107 
108     uint8_t trgtType = util::pdbg::getTrgtType(trgt);
109 
110     switch (trgtType)
111     {
112         case 0x05: // PROC
113             accessFailure = __readProc(trgt, i_regType, i_address, o_value);
114             break;
115 
116         case 0x4b: // OCMB_CHIP
117             accessFailure = __readOcmb(trgt, i_regType, i_address, o_value);
118             break;
119 
120         default:
121             trace::err("Unsupported target type: trgt=%s trgtType=0x%02x",
122                        util::pdbg::getPath(trgt), trgtType);
123             assert(0);
124     }
125 
126     if (accessFailure)
127     {
128         trace::err("%s failure: trgt=%s addr=0x%0" PRIx64, __regType(i_regType),
129                    util::pdbg::getPath(trgt), i_address);
130         o_value = 0; // just in case
131     }
132 
133     return accessFailure;
134 }
135 
136 //------------------------------------------------------------------------------
137 
138 // prints a single line to stdout
139 void hei_inf(char* format, ...)
140 {
141     va_list args;
142     va_start(args, format);
143     trace::inf(format, args);
144     va_end(args);
145 }
146 
147 //------------------------------------------------------------------------------
148 
149 // prints a single line to stderr
150 void hei_err(char* format, ...)
151 {
152     va_list args;
153     va_start(args, format);
154     trace::err(format, args);
155     va_end(args);
156 }
157 
158 } // namespace libhei
159