summaryrefslogtreecommitdiffstats
path: root/src/usr/devicefw/associator.C
blob: 95c8ef7a0f4a06c25223bbc24133a6367d4cafab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <algorithm>
#include "associator.H"

#include <trace/interface.H>

namespace DeviceFW
{
    trace_desc_t* g_traceBuffer = NULL;
    TRAC_INIT(&g_traceBuffer, "DevFW", 4096);

    Associator::Associator() : iv_mutex(mutex_create())
    {
        TRACFCOMP(g_traceBuffer, ENTER_MRK "Associator::Associator");
        
        // Allocate first level of map (access types).
        iv_routeMap = iv_associations.allocate(LAST_DRIVER_ACCESS_TYPE);
    }

    Associator::~Associator()
    {
        mutex_destroy(iv_mutex);

        TRACFCOMP(g_traceBuffer, EXIT_MRK "Associator::~Associator");        
    }

    void Associator::registerRoute(int64_t i_opType,
                                   int64_t i_accType,
                                   int64_t i_targetType,
                                   deviceOp_t i_regRoute)
    {
        TRACFCOMP(g_traceBuffer, "Device route registered for (%d, %d, %d)",
                  i_opType, i_accType, i_targetType);

        // The ranges of the parameters should all be verified by the
        // compiler due to the template specializations in driverif.H.  
        // No assert-checks will be done here.

        mutex_lock(iv_mutex);
        
        size_t ops = 0;
        AssociationData targets = AssociationData();
        
        // Look up second level of map (op-type) or allocate fresh block.
        ops = iv_associations[iv_routeMap][i_accType].offset;
        if (0 == ops)
        {
                // space for LAST_OP_TYPE plus WILDCARD(-1).
            ops = iv_associations.allocate(LAST_OP_TYPE + 1) + 1;
            iv_associations[iv_routeMap][i_accType].offset = ops;
        }
        
        // Look up third level of map (access-type) or allocate fresh block.
        targets = iv_associations[ops][i_opType];
        if (0 == targets.offset)
        {
            // To conserve space only allocate 1 block for WILDCARD.
            if (WILDCARD == i_targetType)
            {
                targets.offset = iv_associations.allocate(1);
                targets.flag = true; // True flag indicates WILDCARD.
            }
            else
            {
                // Allocate full number of spaces.
                targets.offset = iv_associations.allocate(LAST_TARGET_TYPE);
            }
            iv_associations[ops][i_opType] = targets;
        }
            // Ensure the right block size was allocated previously for this
            // target-type (wildcard vs non-wildcard).
        if(((targets.flag) && (i_targetType != WILDCARD)) ||
           ((!targets.flag) && (i_targetType == WILDCARD)))
        {
            // TODO: Create error log for invalid target type?
            bool l_invalidTargetType = false;
            assert(l_invalidTargetType);
        }

        // Index offset to proper target type.  This is now lowest level of map.
        targets.offset += (i_targetType == WILDCARD ? 0 : i_targetType);


        // Search function vector for entry.
        opVector_t::iterator opLocation = std::find(iv_operations.begin(),
                                                    iv_operations.end(),
                                                    i_regRoute);
        // Insert function into vector if not found.
        if (iv_operations.end() == opLocation) 
        {
            iv_operations.push_back(i_regRoute);
            opLocation = iv_operations.end() - 1;
        }

        // TODO: Implement std::distance algorithm and change to:
        //      std::distance(iv_operations.begin(), opLocation);
        size_t opLoc = opLocation - iv_operations.begin();

        // Set function offset into map.  True flag indicates valid.
        (*iv_associations[targets.offset]) = AssociationData(true, opLoc);

        mutex_unlock(iv_mutex);
    }

    ErrorHandle_t Associator::performOp(OperationType i_opType,
                                        TargetHandle_t i_target,
                                        void* io_buffer, size_t& io_buflen,
                                        int64_t i_accessType, va_list i_addr) 
        const
    {
        TRACDCOMP(g_traceBuffer, "Device op requested for (%d, %d, %d)",
                  i_opType, i_accessType, /*TODO: i_target->type*/PROCESSOR);

        // The ranges of the parameters should all be verified by the
        // compiler due to the template specializations in driverif.H.  
        // No assert-checks will be done here.
        
        ErrorHandle_t l_errl = NULL;
        
        mutex_lock(iv_mutex);

            // Function pointer found for this route request.
        deviceOp_t l_devRoute = NULL;
            // Pointer to root of the map.
        const AssociationData* routeMap = iv_associations[iv_routeMap];

        do
        {
            // Follow first level (access type), verify.
            if (0 == routeMap[i_accessType].offset)
            {
                break;
            }

            const AssociationData* ops = 
                iv_associations[routeMap[i_accessType].offset];

            // Check op type = WILDCARD registrations.
            if (0 != ops[WILDCARD].offset)
            {
                // Check access type = WILDCARD registrations.
                if (ops[WILDCARD].flag)
                {
                    l_devRoute = 
                        iv_operations[
                            iv_associations[ops[WILDCARD].offset]->offset];
                    break;
                }
                
                // Check access type = i_target->type registrations.
                const AssociationData* targets =
                    iv_associations[ops[WILDCARD].offset];
                if (targets[/*TODO: i_target->type*/PROCESSOR].flag)
                {
                    l_devRoute =
                        iv_operations[
                            targets[/*TODO: i_target->type*/PROCESSOR].offset];
                    break;
                }
            }
            
            // Check op type = i_opType registrations.
            if (0 != ops[i_opType].offset)
            {
                // Check access type = WILDCARD registrations.
                if(ops[i_opType].flag)
                {
                    l_devRoute =
                        iv_operations[
                            iv_associations[ops[i_opType].offset]->offset];
                    break;
                }
                
                // Check access type = i_target->type registrations.
                const AssociationData* targets =
                    iv_associations[ops[i_opType].offset];
                if (targets[/*TODO: i_target->type*/PROCESSOR].flag)
                {
                    l_devRoute =
                        iv_operations[
                            targets[/*TODO: i_target->type*/PROCESSOR].offset];
                    break;
                }
            }
        } while(0);

        mutex_unlock(iv_mutex);        
        
        // Call function if one was found, create error otherwise.
        if (NULL == l_devRoute)
        {
            // TODO: create error log.
            assert(NULL != l_devRoute);
        }
        else
        {
            l_errl = (*l_devRoute)(i_opType, i_target, 
                                   io_buffer, io_buflen,
                                   i_accessType, i_addr);
        }

        return l_errl;
    }
}


OpenPOWER on IntegriCloud