2 * Copyright (C) ARM Limited 2014. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include "MaliVideoDriver.h"
16 #include "SessionData.h"
18 // From instr/src/mve_instr_comm_protocol.h
19 typedef enum mve_instr_configuration_type {
20 MVE_INSTR_RAW = 1 << 0,
21 MVE_INSTR_COUNTERS = 1 << 1,
22 MVE_INSTR_EVENTS = 1 << 2,
23 MVE_INSTR_ACTIVITIES = 1 << 3,
25 // Raw always pushed regardless
26 MVE_INSTR_PULL = 1 << 12,
27 // Raw always unpacked regardless
28 MVE_INSTR_PACKED_COMM = 1 << 13,
29 // Don’t send ACKt response
30 MVE_INSTR_NO_AUTO_ACK = 1 << 14,
31 } mve_instr_configuration_type_t;
33 static const char COUNTER[] = "ARM_Mali-V500_cnt";
34 static const char EVENT[] = "ARM_Mali-V500_evn";
35 static const char ACTIVITY[] = "ARM_Mali-V500_act";
37 class MaliVideoCounter : public DriverCounter {
39 MaliVideoCounter(DriverCounter *next, const char *name, const MaliVideoCounterType type, const int id) : DriverCounter(next, name), mType(type), mId(id) {
45 MaliVideoCounterType getType() const { return mType; }
46 int getId() const { return mId; }
49 const MaliVideoCounterType mType;
54 MaliVideoDriver::MaliVideoDriver() {
57 MaliVideoDriver::~MaliVideoDriver() {
60 void MaliVideoDriver::readEvents(mxml_node_t *const xml) {
61 mxml_node_t *node = xml;
63 node = mxmlFindElement(node, xml, "event", NULL, NULL, MXML_DESCEND);
67 const char *counter = mxmlElementGetAttr(node, "counter");
68 if (counter == NULL) {
70 } else if (strncmp(counter, COUNTER, sizeof(COUNTER) - 1) == 0) {
71 const int i = strtol(counter + sizeof(COUNTER) - 1, NULL, 10);
72 setCounters(new MaliVideoCounter(getCounters(), strdup(counter), MVCT_COUNTER, i));
73 } else if (strncmp(counter, EVENT, sizeof(EVENT) - 1) == 0) {
74 const int i = strtol(counter + sizeof(EVENT) - 1, NULL, 10);
75 setCounters(new MaliVideoCounter(getCounters(), strdup(counter), MVCT_EVENT, i));
76 } else if (strncmp(counter, ACTIVITY, sizeof(ACTIVITY) - 1) == 0) {
77 const int i = strtol(counter + sizeof(ACTIVITY) - 1, NULL, 10);
78 setCounters(new MaliVideoCounter(getCounters(), strdup(counter), MVCT_ACTIVITY, i));
83 int MaliVideoDriver::writeCounters(mxml_node_t *root) const {
84 if (access("/dev/mv500", F_OK) != 0) {
88 return super::writeCounters(root);
91 void MaliVideoDriver::marshalEnable(const MaliVideoCounterType type, char *const buf, const size_t bufsize, int &pos) {
94 for (MaliVideoCounter *counter = static_cast<MaliVideoCounter *>(getCounters()); counter != NULL; counter = static_cast<MaliVideoCounter *>(counter->getNext())) {
95 if (counter->isEnabled() && (counter->getType() == type)) {
99 Buffer::packInt(buf, bufsize, pos, numEnabled*sizeof(uint32_t));
100 for (MaliVideoCounter *counter = static_cast<MaliVideoCounter *>(getCounters()); counter != NULL; counter = static_cast<MaliVideoCounter *>(counter->getNext())) {
101 if (counter->isEnabled() && (counter->getType() == type)) {
102 Buffer::packInt(buf, bufsize, pos, counter->getId());
107 static bool writeAll(const int mveUds, const char *const buf, const int pos) {
109 while (written < pos) {
110 size_t bytes = ::write(mveUds, buf + written, pos - written);
112 logg->logMessage("%s(%s:%i): write failed", __FUNCTION__, __FILE__, __LINE__);
121 bool MaliVideoDriver::start(const int mveUds) {
125 // code - MVE_INSTR_STARTUP
131 Buffer::packInt(buf, sizeof(buf), pos, sizeof(uint32_t));
132 // client_version_number
133 Buffer::packInt(buf, sizeof(buf), pos, 1);
135 // code - MVE_INSTR_CONFIGURE
141 Buffer::packInt(buf, sizeof(buf), pos, 5*sizeof(uint32_t));
143 Buffer::packInt(buf, sizeof(buf), pos, MVE_INSTR_COUNTERS | MVE_INSTR_EVENTS | MVE_INSTR_ACTIVITIES | MVE_INSTR_PACKED_COMM);
144 // communication_protocol_version
145 Buffer::packInt(buf, sizeof(buf), pos, 1);
146 // data_protocol_version
147 Buffer::packInt(buf, sizeof(buf), pos, 1);
148 // sample_rate - convert samples/second to ms/sample
149 Buffer::packInt(buf, sizeof(buf), pos, 1000/gSessionData->mSampleRate);
150 // live_rate - convert ns/flush to ms/flush
151 Buffer::packInt(buf, sizeof(buf), pos, gSessionData->mLiveRate/1000000);
153 // code - MVE_INSTR_ENABLE_COUNTERS
158 marshalEnable(MVCT_COUNTER, buf, sizeof(buf), pos);
160 // code - MVE_INSTR_ENABLE_EVENTS
165 marshalEnable(MVCT_EVENT, buf, sizeof(buf), pos);
167 // code - MVE_INSTR_ENABLE_ACTIVITIES
172 marshalEnable(MVCT_ACTIVITY, buf, sizeof(buf), pos);
174 return writeAll(mveUds, buf, pos);
177 void MaliVideoDriver::stop(const int mveUds) {
181 // code - MVE_INSTR_STOP
186 marshalEnable(MVCT_COUNTER, buf, sizeof(buf), pos);
188 writeAll(mveUds, buf, pos);