qemu/hw/core/remote-port-stream.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2013 Xilinx Inc
   3 * Copyright (c) 2013 Peter Crosthwaite <peter.crosthwaite@xilinx.com>
   4 * Copyright (c) 2013 Edgar E. Iglesias <edgar.iglesias@xilinx.com>
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24
  25#include "qemu/osdep.h"
  26#include "hw/sysbus.h"
  27#include "qemu/log.h"
  28
  29#include "qemu/bitops.h"
  30#include "qapi/qmp/qerror.h"
  31#include "hw/register.h"
  32#include "hw/stream.h"
  33#include "qapi/error.h"
  34#include "hw/remote-port-device.h"
  35
  36#ifndef REMOTE_PORT_STREAM_ERR_DEBUG
  37#define REMOTE_PORT_STREAM_ERR_DEBUG 0
  38#endif
  39
  40#define TYPE_REMOTE_PORT_STREAM "remote-port-stream"
  41
  42#define REMOTE_PORT_STREAM(obj) \
  43     OBJECT_CHECK(RemotePortStream, (obj), TYPE_REMOTE_PORT_STREAM)
  44
  45typedef struct RemotePortStream RemotePortStream;
  46
  47struct RemotePortStream {
  48    DeviceState parent_obj;
  49
  50    RemotePort *rp;
  51    uint32_t rp_dev;
  52
  53    StreamSlave *tx_dev;
  54
  55    StreamCanPushNotifyFn notify;
  56    void *notify_opaque;
  57
  58    uint8_t *buf;
  59    struct rp_pkt pkt;
  60
  61    bool rsp_pending;
  62    uint32_t current_id;
  63};
  64
  65static void rp_stream_notify(void *opaque)
  66{
  67    RemotePortStream *s = REMOTE_PORT_STREAM(opaque);
  68
  69    if (s->buf && stream_can_push(s->tx_dev, rp_stream_notify, s)) {
  70        RemotePortDynPkt rsp;
  71        size_t pktlen = sizeof(struct rp_pkt_busaccess);
  72        size_t enclen;
  73        int64_t delay = 0; /* FIXME - Implement */
  74
  75        size_t ret = stream_push(s->tx_dev, s->buf, 4, 0);
  76        assert(ret == 4);
  77        s->buf = NULL;
  78
  79        memset(&rsp, 0, sizeof(rsp));
  80        rp_dpkt_alloc(&rsp, pktlen);
  81
  82        enclen = rp_encode_write_resp(s->pkt.hdr.id, s->rp_dev,
  83                                      &rsp.pkt->busaccess,
  84                                      s->pkt.busaccess.timestamp + delay,
  85                                      0, 0,
  86                                      s->pkt.busaccess.attributes,
  87                                      s->pkt.busaccess.len,
  88                                      s->pkt.busaccess.width,
  89                                      s->pkt.busaccess.stream_width);
  90        assert(enclen == pktlen);
  91
  92        rp_write(s->rp, (void *)rsp.pkt, pktlen);
  93    }
  94}
  95
  96static void rp_stream_write(RemotePortDevice *obj, struct rp_pkt *pkt)
  97{
  98    RemotePortStream *s = REMOTE_PORT_STREAM(obj);
  99
 100    assert(pkt->busaccess.width == 0);
 101    assert(pkt->busaccess.stream_width == pkt->busaccess.len);
 102    assert(pkt->busaccess.addr == 0);
 103
 104    if (pkt->hdr.flags & RP_PKT_FLAGS_response) {
 105        /* FXIME - probably need to do syncs and stuff */
 106        assert(s->rsp_pending);
 107        s->rsp_pending = false;
 108        if (s->notify) {
 109            StreamCanPushNotifyFn notify = s->notify;
 110            s->notify = NULL;
 111            notify(s->notify_opaque);
 112        }
 113    } else {
 114        assert(!s->buf);
 115        s->buf = g_memdup(pkt + 1, 4);
 116        s->pkt = *pkt;
 117        rp_stream_notify(s);
 118    }
 119}
 120
 121static bool rp_stream_stream_can_push(StreamSlave *obj,
 122                                            StreamCanPushNotifyFn notify,
 123                                            void *notify_opaque)
 124{
 125    RemotePortStream *s = REMOTE_PORT_STREAM(obj);
 126
 127    if (s->rsp_pending) {
 128        s->notify = notify;
 129        s->notify = notify_opaque;
 130        return false;
 131    }
 132    return true;
 133}
 134
 135static size_t rp_stream_stream_push(StreamSlave *obj, uint8_t *buf,
 136                                    size_t len, uint32_t attr)
 137{
 138    RemotePortStream *s = REMOTE_PORT_STREAM(obj);
 139    RemotePortDynPkt rsp;
 140    struct rp_pkt_busaccess pkt;
 141    uint64_t rp_attr = stream_attr_has_eop(attr) ? RP_BUS_ATTR_EOP : 0;
 142    int64_t clk;
 143    int enclen;
 144
 145    clk = rp_normalized_vmclk(s->rp);
 146    enclen = rp_encode_write(s->current_id++, s->rp_dev, &pkt, clk,
 147                             0, 0, rp_attr, len, 0, 4);
 148
 149    rp_rsp_mutex_lock(s->rp);
 150    rp_write(s->rp, (void *) &pkt, enclen);
 151    rp_write(s->rp, buf, len);
 152    rsp = rp_wait_resp(s->rp);
 153    assert(rsp.pkt->hdr.id == be32_to_cpu(pkt.hdr.id));
 154    rp_dpkt_invalidate(&rsp);
 155    rp_rsp_mutex_unlock(s->rp);
 156    rp_restart_sync_timer(s->rp);
 157    rp_leave_iothread(s->rp);
 158    return len;
 159}
 160
 161static void rp_stream_init(Object *obj)
 162{
 163    RemotePortStream *s = REMOTE_PORT_STREAM(obj);
 164
 165    object_property_add_link(obj, "axistream-connected",
 166                             TYPE_STREAM_SLAVE, (Object **) &s->tx_dev,
 167                             qdev_prop_allow_set_link_before_realize,
 168                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
 169                             &error_abort);
 170    object_property_add_link(obj, "rp-adaptor0", "remote-port",
 171                             (Object **)&s->rp,
 172                             qdev_prop_allow_set_link_before_realize,
 173                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
 174                             &error_abort);
 175}
 176
 177static Property rp_properties[] = {
 178    DEFINE_PROP_UINT32("rp-chan0", RemotePortStream, rp_dev, 0),
 179    DEFINE_PROP_END_OF_LIST(),
 180};
 181
 182static void rp_stream_class_init(ObjectClass *oc, void *data)
 183{
 184    DeviceClass *dc = DEVICE_CLASS(oc);
 185    StreamSlaveClass *ssc = STREAM_SLAVE_CLASS(oc);
 186    RemotePortDeviceClass *rpdc = REMOTE_PORT_DEVICE_CLASS(oc);
 187
 188    ssc->push = rp_stream_stream_push;
 189    ssc->can_push = rp_stream_stream_can_push;
 190    dc->props = rp_properties;
 191    rpdc->ops[RP_CMD_write] = rp_stream_write;
 192}
 193
 194static const TypeInfo rp_stream_info = {
 195    .name          = TYPE_REMOTE_PORT_STREAM,
 196    .parent        = TYPE_DEVICE,
 197    .instance_size = sizeof(RemotePortStream),
 198    .class_init    = rp_stream_class_init,
 199    .instance_init = rp_stream_init,
 200    .interfaces = (InterfaceInfo[]) {
 201        { TYPE_STREAM_SLAVE },
 202        { TYPE_REMOTE_PORT_DEVICE },
 203        { },
 204    }
 205};
 206
 207static void rp_stream_register_types(void)
 208{
 209    type_register_static(&rp_stream_info);
 210}
 211
 212type_init(rp_stream_register_types)
 213