summaryrefslogtreecommitdiffstats
path: root/src/usr/fapi2/plat_mmio_access.C
blob: 60526761b0823a300aae3c9ebf1b2045f7130191 (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
206
207
208
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/fapi2/plat_mmio_access.C $                            */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2018                             */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file plat_mmio_access.C
 *
 * @brief Implements FAPI mmio functions at the platform layer.
 */

#include <stdint.h>
#include <errl/errlentry.H>
#include <devicefw/userif.H>
#include <return_code.H>
#include <target.H>
#include <target_types.H>
#include <plat_utils.H>
#include <attribute_service.H>
#include <hwpf_fapi2_reasoncodes.H>
#include <fapi2/plat_mmio_access.H>


namespace fapi2
{
//------------------------------------------------------------------------------
// HW Communication Functions to be implemented at the platform layer.
//------------------------------------------------------------------------------

/// @brief Platform-level implementation of getMMIO()
///        Reads data via MMIO from the target
ReturnCode platGetMMIO( const Target<TARGET_TYPE_ALL>& i_target,
                        const uint64_t i_mmioAddr,
                        const size_t i_transSize,
                        std::vector<uint8_t>& o_data )
{
    ReturnCode l_rc;
    errlHndl_t l_err = nullptr;

    FAPI_DBG(ENTER_MRK "platGetMMIO");

    // Note: Trace is placed here in plat code because PPE doesn't support
    //       trace in common fapi2_mmio_access.H
    bool l_traceit = platIsScanTraceEnabled();

    // Grab the name of the target
    TARGETING::ATTR_FAPI_NAME_type l_targName = {0};
    fapi2::toString(i_target, l_targName, sizeof(l_targName));

    size_t l_get_size = o_data.size();

    // create a temporary buffer for read data
    uint8_t * l_data_read = new uint8_t[ l_get_size ];

    do
    {
        // Extract the component pointer
        TARGETING::Target * l_target = nullptr;
        l_err = fapi2::platAttrSvc::getTargetingTarget(i_target, l_target);
        if ( l_err )
        {
            FAPI_ERR( "platGetMMIO: Error from getTargetingTarget on %s",
                      l_targName );
            break; //return with error
        }

        // call MMIO driver
        l_err = DeviceFW::deviceRead(l_target,
                               l_data_read,
                               l_get_size,
                               DEVICE_MMIO_ADDRESS(i_mmioAddr, i_transSize));

        if (l_traceit)
        {
            // Only trace the first 8 bytes of data read
            // (don't want to overflow trace buffer)
            uint64_t l_traceDataRead = 0;
            if (l_get_size >= sizeof(l_traceDataRead))
            {
              memcpy(&l_traceDataRead, l_data_read, sizeof(l_traceDataRead));
            }
            else if (l_get_size > 0)
            {
              memcpy(&l_traceDataRead, l_data_read, l_get_size);
            }
            FAPI_SCAN("TRACE : getMMIO  :  %s %d - %d %.16llX",
                      l_targName,
                      o_data.size(),
                      l_get_size,
                      l_traceDataRead);
        }

    } while(0);

    if (l_err)
    {
        l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_err));
    }
    else
    {
        // read was successful so copy data into o_data
        o_data.clear();
        o_data.insert( o_data.end(),
                       &l_data_read[0],
                       &l_data_read[l_get_size] );
    }
    delete [] l_data_read;

    FAPI_DBG(EXIT_MRK "platGetMMIO");
    return l_rc;
}


/// @brief Platform-level implementation of putMMIO()
///        Writes data via MMIO to the target
ReturnCode platPutMMIO( const Target<TARGET_TYPE_ALL>& i_target,
                        const uint64_t i_mmioAddr,
                        const size_t i_transSize,
                        const std::vector<uint8_t>& i_data )
{
    ReturnCode l_rc;
    errlHndl_t l_err = nullptr;
    uint8_t * l_writeDataPtr;

    FAPI_DBG(ENTER_MRK "platPutMMIO");

    // Note: Trace is placed here in plat code because PPE doesn't support
    //       trace in common fapi2_mmio_access.H
    bool l_traceit = platIsScanTraceEnabled();

    // Grab the name of the target
    TARGETING::ATTR_FAPI_NAME_type l_targName = {0};
    fapi2::toString(i_target, l_targName, sizeof(l_targName));

    do {
        // Extract the component pointer
        TARGETING::Target * l_target = nullptr;
        l_err = fapi2::platAttrSvc::getTargetingTarget(i_target, l_target);
        if (l_err)
        {
            FAPI_ERR( "platPutMMIO: Error from getTargetingTarget on %s",
                      l_targName );
            break; //return with error
        }

        //copy data from const vector to data ptr
        l_writeDataPtr = new uint8_t[ i_data.size() ];
        std::copy(i_data.begin(), i_data.end(), l_writeDataPtr);
        size_t l_dataSize = i_data.size();

        // call MMIO driver
        l_err = DeviceFW::deviceWrite(l_target,
                                l_writeDataPtr,
                                l_dataSize,
                                DEVICE_MMIO_ADDRESS(i_mmioAddr, i_transSize));
        if (l_traceit)
        {
            // trace the first 8 bytes of written data
            // (avoid trace buffer overflow)
            uint64_t traceWriteData = 0;
            if (l_dataSize > sizeof(traceWriteData))
            {
                // copy what will fit into traceWriteData variable
                memcpy(&traceWriteData, l_writeDataPtr, sizeof(traceWriteData));
            }
            else
            {
                memcpy(&traceWriteData, l_writeDataPtr, l_dataSize);
            }
            FAPI_SCAN( "TRACE : putMMIO    :  %s : %d %.16llX",
                       l_targName,
                       l_dataSize,
                       traceWriteData );
        }

        delete [] l_writeDataPtr;

    } while (0);

    if (l_err)
    {
        l_rc.setPlatDataPtr(reinterpret_cast<void *> (l_err));
    }

    FAPI_DBG(EXIT_MRK "platPutMMIO");
    return l_rc;
}

} // End namespace
OpenPOWER on IntegriCloud