110389SAndreas.Sandberg@ARM.com/*
210389SAndreas.Sandberg@ARM.com * Copyright (c) 2014 ARM Limited
310389SAndreas.Sandberg@ARM.com * All rights reserved
410389SAndreas.Sandberg@ARM.com *
510389SAndreas.Sandberg@ARM.com * The license below extends only to copyright in the software and shall
610389SAndreas.Sandberg@ARM.com * not be construed as granting a license to any other intellectual
710389SAndreas.Sandberg@ARM.com * property including but not limited to intellectual property relating
810389SAndreas.Sandberg@ARM.com * to a hardware implementation of the functionality of the software
910389SAndreas.Sandberg@ARM.com * licensed hereunder.  You may use the software subject to the license
1010389SAndreas.Sandberg@ARM.com * terms below provided that you ensure that this notice is replicated
1110389SAndreas.Sandberg@ARM.com * unmodified and in its entirety in all distributions of the software,
1210389SAndreas.Sandberg@ARM.com * modified or unmodified, in source code or in binary form.
1310389SAndreas.Sandberg@ARM.com *
1410389SAndreas.Sandberg@ARM.com * Redistribution and use in source and binary forms, with or without
1510389SAndreas.Sandberg@ARM.com * modification, are permitted provided that the following conditions are
1610389SAndreas.Sandberg@ARM.com * met: redistributions of source code must retain the above copyright
1710389SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer;
1810389SAndreas.Sandberg@ARM.com * redistributions in binary form must reproduce the above copyright
1910389SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer in the
2010389SAndreas.Sandberg@ARM.com * documentation and/or other materials provided with the distribution;
2110389SAndreas.Sandberg@ARM.com * neither the name of the copyright holders nor the names of its
2210389SAndreas.Sandberg@ARM.com * contributors may be used to endorse or promote products derived from
2310389SAndreas.Sandberg@ARM.com * this software without specific prior written permission.
2410389SAndreas.Sandberg@ARM.com *
2510389SAndreas.Sandberg@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610389SAndreas.Sandberg@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710389SAndreas.Sandberg@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810389SAndreas.Sandberg@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910389SAndreas.Sandberg@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010389SAndreas.Sandberg@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110389SAndreas.Sandberg@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210389SAndreas.Sandberg@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310389SAndreas.Sandberg@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410389SAndreas.Sandberg@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510389SAndreas.Sandberg@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610389SAndreas.Sandberg@ARM.com *
3710389SAndreas.Sandberg@ARM.com * Authors: Andreas Sandberg
3810389SAndreas.Sandberg@ARM.com */
3910389SAndreas.Sandberg@ARM.com
4011793Sbrandon.potter@amd.com#include "dev/virtio/console.hh"
4111793Sbrandon.potter@amd.com
4210389SAndreas.Sandberg@ARM.com#include "debug/VIOConsole.hh"
4310389SAndreas.Sandberg@ARM.com#include "params/VirtIOConsole.hh"
4410389SAndreas.Sandberg@ARM.com#include "sim/system.hh"
4510389SAndreas.Sandberg@ARM.com
4610389SAndreas.Sandberg@ARM.comVirtIOConsole::VirtIOConsole(Params *params)
4710389SAndreas.Sandberg@ARM.com    : VirtIODeviceBase(params, ID_CONSOLE, sizeof(Config), F_SIZE),
4810389SAndreas.Sandberg@ARM.com      qRecv(params->system->physProxy, params->qRecvSize, *this),
4910389SAndreas.Sandberg@ARM.com      qTrans(params->system->physProxy, params->qTransSize, *this),
5012237Sandreas.sandberg@arm.com      device(*params->device), callbackDataAvail(qRecv)
5110389SAndreas.Sandberg@ARM.com{
5210389SAndreas.Sandberg@ARM.com    registerQueue(qRecv);
5310389SAndreas.Sandberg@ARM.com    registerQueue(qTrans);
5410389SAndreas.Sandberg@ARM.com
5510389SAndreas.Sandberg@ARM.com    config.cols = 80;
5610389SAndreas.Sandberg@ARM.com    config.rows = 24;
5710389SAndreas.Sandberg@ARM.com
5812237Sandreas.sandberg@arm.com    device.regInterfaceCallback(&callbackDataAvail);
5910389SAndreas.Sandberg@ARM.com}
6010389SAndreas.Sandberg@ARM.com
6110389SAndreas.Sandberg@ARM.com
6210389SAndreas.Sandberg@ARM.comVirtIOConsole::~VirtIOConsole()
6310389SAndreas.Sandberg@ARM.com{
6410389SAndreas.Sandberg@ARM.com}
6510389SAndreas.Sandberg@ARM.comvoid
6610389SAndreas.Sandberg@ARM.comVirtIOConsole::readConfig(PacketPtr pkt, Addr cfgOffset)
6710389SAndreas.Sandberg@ARM.com{
6810389SAndreas.Sandberg@ARM.com    Config cfg_out;
6910389SAndreas.Sandberg@ARM.com    cfg_out.rows = htov_legacy(config.rows);
7010389SAndreas.Sandberg@ARM.com    cfg_out.cols = htov_legacy(config.cols);
7110389SAndreas.Sandberg@ARM.com
7210389SAndreas.Sandberg@ARM.com    readConfigBlob(pkt, cfgOffset, (uint8_t *)&cfg_out);
7310389SAndreas.Sandberg@ARM.com}
7410389SAndreas.Sandberg@ARM.com
7510389SAndreas.Sandberg@ARM.comvoid
7610389SAndreas.Sandberg@ARM.comVirtIOConsole::TermRecvQueue::trySend()
7710389SAndreas.Sandberg@ARM.com{
7810389SAndreas.Sandberg@ARM.com    DPRINTF(VIOConsole, "trySend\n");
7910389SAndreas.Sandberg@ARM.com
8010389SAndreas.Sandberg@ARM.com    // Send data as long as the terminal has outgoing data and we can
8110389SAndreas.Sandberg@ARM.com    // get free descriptors (i.e., there are buffers available to
8210389SAndreas.Sandberg@ARM.com    // send) from the guest.
8310389SAndreas.Sandberg@ARM.com    VirtDescriptor *d;
8412237Sandreas.sandberg@arm.com    while (parent.device.dataAvailable() && (d = consumeDescriptor())) {
8510389SAndreas.Sandberg@ARM.com        DPRINTF(VIOConsole, "Got descriptor (len: %i)\n", d->size());
8610389SAndreas.Sandberg@ARM.com        size_t len(0);
8712237Sandreas.sandberg@arm.com        while (parent.device.dataAvailable() && len < d->size()) {
8812237Sandreas.sandberg@arm.com            uint8_t in(parent.device.readData());
8910389SAndreas.Sandberg@ARM.com            d->chainWrite(len, &in, sizeof(uint8_t));
9010389SAndreas.Sandberg@ARM.com            ++len;
9110389SAndreas.Sandberg@ARM.com        }
9210389SAndreas.Sandberg@ARM.com
9310389SAndreas.Sandberg@ARM.com        // Tell the guest that we are done with this descriptor.
9410389SAndreas.Sandberg@ARM.com        produceDescriptor(d, len);
9510389SAndreas.Sandberg@ARM.com        parent.kick();
9610389SAndreas.Sandberg@ARM.com    }
9710389SAndreas.Sandberg@ARM.com}
9810389SAndreas.Sandberg@ARM.com
9910389SAndreas.Sandberg@ARM.comvoid
10010389SAndreas.Sandberg@ARM.comVirtIOConsole::TermTransQueue::onNotifyDescriptor(VirtDescriptor *desc)
10110389SAndreas.Sandberg@ARM.com{
10210389SAndreas.Sandberg@ARM.com    DPRINTF(VIOConsole, "Got input data descriptor (len: %i)\n",
10310389SAndreas.Sandberg@ARM.com            desc->size());
10410389SAndreas.Sandberg@ARM.com
10510389SAndreas.Sandberg@ARM.com    // Copy the data from the guest and forward it to the
10610389SAndreas.Sandberg@ARM.com    // terminal.
10710389SAndreas.Sandberg@ARM.com    const size_t size(desc->chainSize());
10810389SAndreas.Sandberg@ARM.com    uint8_t data[size];
10910389SAndreas.Sandberg@ARM.com    desc->chainRead(0, data, size);
11010389SAndreas.Sandberg@ARM.com    for (int i = 0; i < desc->size(); ++i)
11112237Sandreas.sandberg@arm.com        parent.device.writeData(data[i]);
11210389SAndreas.Sandberg@ARM.com
11310389SAndreas.Sandberg@ARM.com    // Tell the guest that we are done with this descriptor.
11410389SAndreas.Sandberg@ARM.com    produceDescriptor(desc, 0);
11510389SAndreas.Sandberg@ARM.com    parent.kick();
11610389SAndreas.Sandberg@ARM.com}
11710389SAndreas.Sandberg@ARM.com
11810389SAndreas.Sandberg@ARM.comVirtIOConsole *
11910389SAndreas.Sandberg@ARM.comVirtIOConsoleParams::create()
12010389SAndreas.Sandberg@ARM.com{
12110389SAndreas.Sandberg@ARM.com    return new VirtIOConsole(this);
12210389SAndreas.Sandberg@ARM.com}
123