blob: 7f8422a24875d8885894dd83268ced9af9ee09f2 [file] [log] [blame]
Ben Tyner032bf9e2020-05-06 21:27:54 -05001#include <hei_macros.hpp>
Zane Shelley7bf1f6d2019-10-18 16:03:51 -05002#include <isolator/hei_isolation_node.hpp>
3
4namespace libhei
5{
6
7//------------------------------------------------------------------------------
8
Zane Shelleyfe27b652019-10-28 11:33:07 -05009bool IsolationNode::analyze(const Chip& i_chip, AttentionType_t i_attnType,
10 IsolationData& io_isoData) const
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050011{
12 bool o_activeAttn = false; // Initially, assume no active attentions.
13
14 // Keep track of nodes that have been analyzed to avoid cyclic isolation.
15 pushIsolationStack();
16
17 // A rule for i_attnType must exist.
Zane Shelley83da2452019-10-25 15:45:34 -050018 auto rule_itr = iv_rules.find(i_attnType);
19 HEI_ASSERT(iv_rules.end() != rule_itr);
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050020
21 // Get the returned BitString for this rule.
Zane Shelleyfe27b652019-10-28 11:33:07 -050022 const BitString* bs = rule_itr->second->getBitString(i_chip);
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050023
24 // Ensure this BitString is not longer than the maximum bit field.
Zane Shelley11b89942019-11-07 11:07:28 -060025 HEI_ASSERT(bs->getBitLen() <= (1 << (sizeof(RegisterBit_t) * 8)));
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050026
27 // Find all active bits for this rule.
Zane Shelley83da2452019-10-25 15:45:34 -050028 for (RegisterBit_t bit = 0; bit < bs->getBitLen(); bit++)
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050029 {
30 // Continue to the next bit if not active.
Zane Shelley7f7a42d2019-10-28 13:28:31 -050031 if (!bs->isBitSet(bit))
32 continue;
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050033
34 // At least one active bit was found.
35 o_activeAttn = true;
36
37 // Determine if this attention originated from another register or if it
38 // is a leaf in the isolation tree.
Zane Shelley83da2452019-10-25 15:45:34 -050039 auto child_itr = iv_children.find(bit);
40 if (iv_children.end() != child_itr)
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050041 {
42 // This bit was driven from an attention from another register.
43 // Continue down the isolation tree to look for more attentions.
Zane Shelley7f7a42d2019-10-28 13:28:31 -050044 bool attnFound =
45 child_itr->second->analyze(i_chip, i_attnType, io_isoData);
Zane Shelley83da2452019-10-25 15:45:34 -050046 if (!attnFound)
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050047 {
48 // Something went wrong. There should have been an active
49 // attention. It's possible there is a bug in the Chip Data
50 // File. Or, it is also possible some other piece of code is
51 // clearing the attention before this code is able to analyze
52 // it. Another possibility is that the hardware it not behaving
53 // as expected. Since we really don't know what happened, we
54 // should not assert. Instead, add this bit's signature to
55 // io_isoData. If there are no other active attentions, the user
56 // application could use this signature to help determine, and
57 // circumvent, the isolation problem.
Zane Shelleyc4771992019-10-28 22:01:49 -050058 io_isoData.addSignature(Signature{i_chip, iv_hwReg.getId(),
59 iv_hwReg.getInstance(), bit,
60 i_attnType});
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050061 }
62 }
63 else
64 {
65 // We have reached a leaf in the isolation tree. Add this bit's
66 // signature to io_isoData.
Zane Shelleyc4771992019-10-28 22:01:49 -050067 io_isoData.addSignature(Signature{i_chip, iv_hwReg.getId(),
68 iv_hwReg.getInstance(), bit,
69 i_attnType});
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050070 }
71 }
72
73 // Analysis is complete on this node. So remove it from cv_isolationStack.
74 popIsolationStack();
75
76 return o_activeAttn;
77}
78
79//------------------------------------------------------------------------------
80
Zane Shelleyfe27b652019-10-28 11:33:07 -050081void IsolationNode::addRule(AttentionType_t i_attnType, const Register* i_rule)
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050082{
83 // A rule for this attention type should not already exist.
Zane Shelley83da2452019-10-25 15:45:34 -050084 HEI_ASSERT(iv_rules.end() == iv_rules.find(i_attnType));
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050085
86 // The rule should not be null.
Zane Shelley83da2452019-10-25 15:45:34 -050087 HEI_ASSERT(nullptr != i_rule);
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050088
89 // Add the new rule.
90 iv_rules[i_attnType] = i_rule;
91}
92
93//------------------------------------------------------------------------------
94
Zane Shelleyfe27b652019-10-28 11:33:07 -050095void IsolationNode::addChild(uint8_t i_bit, const IsolationNode* i_child)
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050096{
97 // An entry for this bit should not already exist.
Zane Shelley83da2452019-10-25 15:45:34 -050098 HEI_ASSERT(iv_children.end() == iv_children.find(i_bit));
Zane Shelley7bf1f6d2019-10-18 16:03:51 -050099
100 // The child register should not be null.
Zane Shelley83da2452019-10-25 15:45:34 -0500101 HEI_ASSERT(nullptr != i_child);
Zane Shelley7bf1f6d2019-10-18 16:03:51 -0500102
103 // Add the new rule.
104 iv_children[i_bit] = i_child;
105}
106
107//------------------------------------------------------------------------------
108
Zane Shelleyc4771992019-10-28 22:01:49 -0500109std::vector<const IsolationNode*> IsolationNode::cv_isolationStack{};
Zane Shelley7bf1f6d2019-10-18 16:03:51 -0500110
111//------------------------------------------------------------------------------
112
113void IsolationNode::pushIsolationStack() const
114{
115 // Ensure this node does not already exist in cv_isolationStack.
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500116 auto itr =
117 std::find(cv_isolationStack.begin(), cv_isolationStack.end(), this);
Zane Shelley83da2452019-10-25 15:45:34 -0500118 HEI_ASSERT(cv_isolationStack.end() == itr);
Zane Shelley7bf1f6d2019-10-18 16:03:51 -0500119
120 // Push to node to the stack.
Zane Shelley83da2452019-10-25 15:45:34 -0500121 cv_isolationStack.push_back(this);
Zane Shelley7bf1f6d2019-10-18 16:03:51 -0500122}
123
124//------------------------------------------------------------------------------
125
126} // end namespace libhei