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 
__regType(RegisterType_t i_regType)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 
__readProc(pdbg_target * i_procTrgt,RegisterType_t i_regType,uint64_t i_address,uint64_t & o_value)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     pdbg_target* scomTrgt = util::pdbg::getPibTrgt(i_procTrgt);
47 
48     switch (i_regType)
49     {
50         case REG_TYPE_SCOM:
51         case REG_TYPE_ID_SCOM:
52             // Read the 64-bit SCOM register.
53             accessFailure = (0 != pib_read(scomTrgt, i_address, &o_value));
54             break;
55 
56         default:
57             trace::err("Unsupported register type: trgt=%s regType=0x%02x "
58                        "addr=0x%0" PRIx64,
59                        util::pdbg::getPath(i_procTrgt), i_regType, i_address);
60             assert(0); // an unsupported register type
61     }
62 
63     return accessFailure;
64 }
65 
66 //------------------------------------------------------------------------------
67 
__readOcmb(pdbg_target * i_obmcTrgt,RegisterType_t i_regType,uint64_t i_address,uint64_t & o_value)68 bool __readOcmb(pdbg_target* i_obmcTrgt, RegisterType_t i_regType,
69                 uint64_t i_address, uint64_t& o_value)
70 {
71     bool accessFailure = false;
72 
73     // The OCMB target is used for SCOM access.
74     pdbg_target* scomTrgt = i_obmcTrgt;
75 
76     switch (i_regType)
77     {
78         case REG_TYPE_SCOM:
79         case REG_TYPE_ID_SCOM:
80             // Read the 64-bit SCOM register.
81             accessFailure = (0 != ocmb_getscom(scomTrgt, i_address, &o_value));
82             break;
83 
84         default:
85             trace::err("Unsupported register type: trgt=%s regType=0x%02x "
86                        "addr=0x%0" PRIx64,
87                        util::pdbg::getPath(i_obmcTrgt), i_regType, i_address);
88             assert(0);
89     }
90 
91     return accessFailure;
92 }
93 
94 //------------------------------------------------------------------------------
95 
registerRead(const Chip & i_chip,RegisterType_t i_regType,uint64_t i_address,uint64_t & o_value)96 bool registerRead(const Chip& i_chip, RegisterType_t i_regType,
97                   uint64_t i_address, uint64_t& o_value)
98 {
99     bool accessFailure = false;
100 
101     auto trgt = util::pdbg::getTrgt(i_chip);
102 
103     uint8_t trgtType = util::pdbg::getTrgtType(trgt);
104 
105     switch (trgtType)
106     {
107         case 0x05: // PROC
108             accessFailure = __readProc(trgt, i_regType, i_address, o_value);
109             break;
110 
111         case 0x4b: // OCMB_CHIP
112             accessFailure = __readOcmb(trgt, i_regType, i_address, o_value);
113             break;
114 
115         default:
116             trace::err("Unsupported target type: trgt=%s trgtType=0x%02x",
117                        util::pdbg::getPath(trgt), trgtType);
118             assert(0);
119     }
120 
121     if (accessFailure)
122     {
123         trace::err("%s failure: trgt=%s addr=0x%0" PRIx64, __regType(i_regType),
124                    util::pdbg::getPath(trgt), i_address);
125         o_value = 0; // just in case
126     }
127 
128     return accessFailure;
129 }
130 
131 //------------------------------------------------------------------------------
132 
133 // prints a single line to stdout
hei_inf(char * format,...)134 void hei_inf(char* format, ...)
135 {
136     va_list args;
137     va_start(args, format);
138     trace::inf(format, args);
139     va_end(args);
140 }
141 
142 //------------------------------------------------------------------------------
143 
144 // prints a single line to stderr
hei_err(char * format,...)145 void hei_err(char* format, ...)
146 {
147     va_list args;
148     va_start(args, format);
149     trace::err(format, args);
150     va_end(args);
151 }
152 
153 } // namespace libhei
154