console.cc revision 12237
12124SN/A/*
22124SN/A * Copyright (c) 2014 ARM Limited
35268Sksewell@umich.edu * All rights reserved
45268Sksewell@umich.edu *
55268Sksewell@umich.edu * The license below extends only to copyright in the software and shall
65268Sksewell@umich.edu * not be construed as granting a license to any other intellectual
75268Sksewell@umich.edu * property including but not limited to intellectual property relating
85268Sksewell@umich.edu * to a hardware implementation of the functionality of the software
95268Sksewell@umich.edu * licensed hereunder.  You may use the software subject to the license
105268Sksewell@umich.edu * terms below provided that you ensure that this notice is replicated
115268Sksewell@umich.edu * unmodified and in its entirety in all distributions of the software,
125268Sksewell@umich.edu * modified or unmodified, in source code or in binary form.
135268Sksewell@umich.edu *
145268Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
155268Sksewell@umich.edu * modification, are permitted provided that the following conditions are
165268Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
175268Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
185268Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
195268Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
205268Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
215268Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
225268Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
235268Sksewell@umich.edu * this software without specific prior written permission.
245268Sksewell@umich.edu *
255268Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
265268Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
275268Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
285268Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
295268Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
305268Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
312022SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
322649Ssaidi@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
332649Ssaidi@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
342706Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
352649Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362649Ssaidi@eecs.umich.edu *
372022SN/A * Authors: Andreas Sandberg
382124SN/A */
392124SN/A
402124SN/A#include "dev/virtio/console.hh"
412124SN/A
422124SN/A#include "debug/VIOConsole.hh"
432124SN/A#include "params/VirtIOConsole.hh"
442124SN/A#include "sim/system.hh"
455736Snate@binkert.org
462239SN/AVirtIOConsole::VirtIOConsole(Params *params)
472124SN/A    : VirtIODeviceBase(params, ID_CONSOLE, sizeof(Config), F_SIZE),
482124SN/A      qRecv(params->system->physProxy, params->qRecvSize, *this),
492124SN/A      qTrans(params->system->physProxy, params->qTransSize, *this),
502124SN/A      device(*params->device), callbackDataAvail(qRecv)
516207Sksewell@umich.edu{
522124SN/A    registerQueue(qRecv);
532742Sksewell@umich.edu    registerQueue(qTrans);
542022SN/A
552124SN/A    config.cols = 80;
562022SN/A    config.rows = 24;
572124SN/A
582124SN/A    device.regInterfaceCallback(&callbackDataAvail);
592124SN/A}
602124SN/A
612742Sksewell@umich.edu
622742Sksewell@umich.eduVirtIOConsole::~VirtIOConsole()
632742Sksewell@umich.edu{
642742Sksewell@umich.edu}
652742Sksewell@umich.eduvoid
662742Sksewell@umich.eduVirtIOConsole::readConfig(PacketPtr pkt, Addr cfgOffset)
672742Sksewell@umich.edu{
682742Sksewell@umich.edu    Config cfg_out;
696207Sksewell@umich.edu    cfg_out.rows = htov_legacy(config.rows);
706207Sksewell@umich.edu    cfg_out.cols = htov_legacy(config.cols);
712742Sksewell@umich.edu
722742Sksewell@umich.edu    readConfigBlob(pkt, cfgOffset, (uint8_t *)&cfg_out);
732742Sksewell@umich.edu}
742742Sksewell@umich.edu
752742Sksewell@umich.eduvoid
762742Sksewell@umich.eduVirtIOConsole::TermRecvQueue::trySend()
772022SN/A{
782022SN/A    DPRINTF(VIOConsole, "trySend\n");
792124SN/A
802022SN/A    // Send data as long as the terminal has outgoing data and we can
812124SN/A    // get free descriptors (i.e., there are buffers available to
822124SN/A    // send) from the guest.
832124SN/A    VirtDescriptor *d;
842742Sksewell@umich.edu    while (parent.device.dataAvailable() && (d = consumeDescriptor())) {
852239SN/A        DPRINTF(VIOConsole, "Got descriptor (len: %i)\n", d->size());
862124SN/A        size_t len(0);
872124SN/A        while (parent.device.dataAvailable() && len < d->size()) {
882742Sksewell@umich.edu            uint8_t in(parent.device.readData());
892742Sksewell@umich.edu            d->chainWrite(len, &in, sizeof(uint8_t));
902742Sksewell@umich.edu            ++len;
912742Sksewell@umich.edu        }
922742Sksewell@umich.edu
932742Sksewell@umich.edu        // Tell the guest that we are done with this descriptor.
942742Sksewell@umich.edu        produceDescriptor(d, len);
952742Sksewell@umich.edu        parent.kick();
964661Sksewell@umich.edu    }
974661Sksewell@umich.edu}
984661Sksewell@umich.edu
994661Sksewell@umich.eduvoid
1004661Sksewell@umich.eduVirtIOConsole::TermTransQueue::onNotifyDescriptor(VirtDescriptor *desc)
1014661Sksewell@umich.edu{
1024661Sksewell@umich.edu    DPRINTF(VIOConsole, "Got input data descriptor (len: %i)\n",
1035222Sksewell@umich.edu            desc->size());
1044661Sksewell@umich.edu
1054661Sksewell@umich.edu    // Copy the data from the guest and forward it to the
1065222Sksewell@umich.edu    // terminal.
1074661Sksewell@umich.edu    const size_t size(desc->chainSize());
1084661Sksewell@umich.edu    uint8_t data[size];
1095222Sksewell@umich.edu    desc->chainRead(0, data, size);
1104661Sksewell@umich.edu    for (int i = 0; i < desc->size(); ++i)
1114661Sksewell@umich.edu        parent.device.writeData(data[i]);
1125222Sksewell@umich.edu
1134661Sksewell@umich.edu    // Tell the guest that we are done with this descriptor.
1144661Sksewell@umich.edu    produceDescriptor(desc, 0);
1155222Sksewell@umich.edu    parent.kick();
1164661Sksewell@umich.edu}
1174661Sksewell@umich.edu
1184661Sksewell@umich.eduVirtIOConsole *
1194661Sksewell@umich.eduVirtIOConsoleParams::create()
1204661Sksewell@umich.edu{
1214661Sksewell@umich.edu    return new VirtIOConsole(this);
1224661Sksewell@umich.edu}
1234661Sksewell@umich.edu