18853Sandreas.hansson@arm.com/*
212532Sandreas.sandberg@arm.com * Copyright (c) 2012, 2018 ARM Limited
38853Sandreas.hansson@arm.com * All rights reserved
48853Sandreas.hansson@arm.com *
58853Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68853Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78853Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88853Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98853Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108853Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118853Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128853Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138853Sandreas.hansson@arm.com *
148853Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
158853Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
168853Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
178853Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
188853Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
198853Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
208853Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
218853Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
228853Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
238853Sandreas.hansson@arm.com * this software without specific prior written permission.
248853Sandreas.hansson@arm.com *
258853Sandreas.hansson@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
268853Sandreas.hansson@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
278853Sandreas.hansson@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
288853Sandreas.hansson@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
298853Sandreas.hansson@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
308853Sandreas.hansson@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
318853Sandreas.hansson@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
328853Sandreas.hansson@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
338853Sandreas.hansson@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
348853Sandreas.hansson@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
358853Sandreas.hansson@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
368853Sandreas.hansson@arm.com *
378853Sandreas.hansson@arm.com * Authors: Andreas Hansson
388853Sandreas.hansson@arm.com */
398853Sandreas.hansson@arm.com
4011793Sbrandon.potter@amd.com#include "mem/port_proxy.hh"
4111793Sbrandon.potter@amd.com
428853Sandreas.hansson@arm.com#include "base/chunk_generator.hh"
438853Sandreas.hansson@arm.com
448853Sandreas.hansson@arm.comvoid
4512532Sandreas.sandberg@arm.comPortProxy::readBlobPhys(Addr addr, Request::Flags flags,
4614009Sgabeblack@google.com                        void *p, int size) const
478853Sandreas.hansson@arm.com{
4810564Sandreas.hansson@arm.com    for (ChunkGenerator gen(addr, size, _cacheLineSize); !gen.done();
4910564Sandreas.hansson@arm.com         gen.next()) {
5012749Sgiacomo.travaglini@arm.com
5112749Sgiacomo.travaglini@arm.com        auto req = std::make_shared<Request>(
5212749Sgiacomo.travaglini@arm.com            gen.addr(), gen.size(), flags, Request::funcMasterId);
5312749Sgiacomo.travaglini@arm.com
5412749Sgiacomo.travaglini@arm.com        Packet pkt(req, MemCmd::ReadReq);
5514009Sgabeblack@google.com        pkt.dataStatic(static_cast<uint8_t *>(p));
5614196Sgabeblack@google.com        sendFunctional(&pkt);
5714009Sgabeblack@google.com        p = static_cast<uint8_t *>(p) + gen.size();
588853Sandreas.hansson@arm.com    }
598853Sandreas.hansson@arm.com}
608853Sandreas.hansson@arm.com
618853Sandreas.hansson@arm.comvoid
6212532Sandreas.sandberg@arm.comPortProxy::writeBlobPhys(Addr addr, Request::Flags flags,
6314009Sgabeblack@google.com                         const void *p, int size) const
6410564Sandreas.hansson@arm.com{
6510564Sandreas.hansson@arm.com    for (ChunkGenerator gen(addr, size, _cacheLineSize); !gen.done();
6610564Sandreas.hansson@arm.com         gen.next()) {
6712749Sgiacomo.travaglini@arm.com
6812749Sgiacomo.travaglini@arm.com        auto req = std::make_shared<Request>(
6912749Sgiacomo.travaglini@arm.com            gen.addr(), gen.size(), flags, Request::funcMasterId);
7012749Sgiacomo.travaglini@arm.com
7112749Sgiacomo.travaglini@arm.com        Packet pkt(req, MemCmd::WriteReq);
7214009Sgabeblack@google.com        pkt.dataStaticConst(static_cast<const uint8_t *>(p));
7314196Sgabeblack@google.com        sendFunctional(&pkt);
7414009Sgabeblack@google.com        p = static_cast<const uint8_t *>(p) + gen.size();
7510564Sandreas.hansson@arm.com    }
7610564Sandreas.hansson@arm.com}
7710564Sandreas.hansson@arm.com
7810564Sandreas.hansson@arm.comvoid
7912532Sandreas.sandberg@arm.comPortProxy::memsetBlobPhys(Addr addr, Request::Flags flags,
8012532Sandreas.sandberg@arm.com                          uint8_t v, int size) const
818853Sandreas.hansson@arm.com{
828853Sandreas.hansson@arm.com    // quick and dirty...
838853Sandreas.hansson@arm.com    uint8_t *buf = new uint8_t[size];
848853Sandreas.hansson@arm.com
858853Sandreas.hansson@arm.com    std::memset(buf, v, size);
8612532Sandreas.sandberg@arm.com    PortProxy::writeBlobPhys(addr, flags, buf, size);
878853Sandreas.hansson@arm.com
888853Sandreas.hansson@arm.com    delete [] buf;
898853Sandreas.hansson@arm.com}
9014008Sgabeblack@google.com
9114008Sgabeblack@google.combool
9214008Sgabeblack@google.comPortProxy::tryWriteString(Addr addr, const char *str) const
9314008Sgabeblack@google.com{
9414008Sgabeblack@google.com    do {
9514009Sgabeblack@google.com        if (!tryWriteBlob(addr++, str, 1))
9614008Sgabeblack@google.com            return false;
9714008Sgabeblack@google.com    } while (*str++);
9814008Sgabeblack@google.com    return true;
9914008Sgabeblack@google.com}
10014008Sgabeblack@google.com
10114008Sgabeblack@google.combool
10214008Sgabeblack@google.comPortProxy::tryReadString(std::string &str, Addr addr) const
10314008Sgabeblack@google.com{
10414008Sgabeblack@google.com    while (true) {
10514008Sgabeblack@google.com        uint8_t c;
10614008Sgabeblack@google.com        if (!tryReadBlob(addr++, &c, 1))
10714008Sgabeblack@google.com            return false;
10814008Sgabeblack@google.com        if (!c)
10914008Sgabeblack@google.com            return true;
11014008Sgabeblack@google.com        str += c;
11114008Sgabeblack@google.com    }
11214008Sgabeblack@google.com}
11314012Sgabeblack@google.com
11414012Sgabeblack@google.combool
11514012Sgabeblack@google.comPortProxy::tryReadString(char *str, Addr addr, size_t maxlen) const
11614012Sgabeblack@google.com{
11714012Sgabeblack@google.com    assert(maxlen);
11814012Sgabeblack@google.com    while (maxlen--) {
11914012Sgabeblack@google.com        if (!tryReadBlob(addr++, str, 1))
12014012Sgabeblack@google.com            return false;
12114012Sgabeblack@google.com        if (!*str++)
12214012Sgabeblack@google.com            return true;
12314012Sgabeblack@google.com    }
12414012Sgabeblack@google.com    // We ran out of room, so back up and add a terminator.
12514012Sgabeblack@google.com    *--str = '\0';
12614012Sgabeblack@google.com    return true;
12714012Sgabeblack@google.com}
128