2004-06-03 18:46:20 +00:00
|
|
|
/*
|
2007-10-28 23:42:18 +00:00
|
|
|
* QEMU PowerMac CUDA device support
|
2007-09-16 21:08:06 +00:00
|
|
|
*
|
2007-10-28 23:42:18 +00:00
|
|
|
* Copyright (c) 2004-2007 Fabrice Bellard
|
|
|
|
* Copyright (c) 2007 Jocelyn Mayer
|
2007-09-16 21:08:06 +00:00
|
|
|
*
|
2004-06-03 18:46:20 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 14:35:07 +00:00
|
|
|
|
2016-01-26 18:16:58 +00:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 14:35:08 +00:00
|
|
|
#include "qemu-common.h"
|
2013-02-04 14:40:22 +00:00
|
|
|
#include "hw/ppc/mac.h"
|
2019-08-12 05:23:51 +00:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 05:23:45 +00:00
|
|
|
#include "migration/vmstate.h"
|
2013-02-05 16:06:20 +00:00
|
|
|
#include "hw/input/adb.h"
|
2018-02-09 18:51:40 +00:00
|
|
|
#include "hw/misc/mos6522.h"
|
2018-02-09 18:51:41 +00:00
|
|
|
#include "hw/misc/macio/cuda.h"
|
2012-12-17 17:20:00 +00:00
|
|
|
#include "qemu/timer.h"
|
2019-08-12 05:23:59 +00:00
|
|
|
#include "sysemu/runstate.h"
|
2016-03-20 17:16:19 +00:00
|
|
|
#include "qemu/cutils.h"
|
2015-12-15 12:16:16 +00:00
|
|
|
#include "qemu/log.h"
|
2019-05-23 14:35:07 +00:00
|
|
|
#include "qemu/module.h"
|
2018-02-09 18:51:42 +00:00
|
|
|
#include "trace.h"
|
2008-12-24 09:38:16 +00:00
|
|
|
|
2004-06-03 18:46:20 +00:00
|
|
|
/* Bits in B data register: all active low */
|
2018-02-09 18:51:40 +00:00
|
|
|
#define TREQ 0x08 /* Transfer request (input) */
|
|
|
|
#define TACK 0x10 /* Transfer acknowledge (output) */
|
|
|
|
#define TIP 0x20 /* Transfer in progress (output) */
|
2004-06-03 18:46:20 +00:00
|
|
|
|
|
|
|
/* commands (1st byte) */
|
2018-02-09 18:51:40 +00:00
|
|
|
#define ADB_PACKET 0
|
|
|
|
#define CUDA_PACKET 1
|
|
|
|
#define ERROR_PACKET 2
|
|
|
|
#define TIMER_PACKET 3
|
|
|
|
#define POWER_PACKET 4
|
|
|
|
#define MACIIC_PACKET 5
|
|
|
|
#define PMU_PACKET 6
|
2004-06-03 18:46:20 +00:00
|
|
|
|
|
|
|
#define CUDA_TIMER_FREQ (4700000 / 6)
|
|
|
|
|
2005-02-09 00:07:08 +00:00
|
|
|
/* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
|
|
|
|
#define RTC_OFFSET 2082844800
|
|
|
|
|
2007-09-16 21:08:06 +00:00
|
|
|
static void cuda_receive_packet_from_host(CUDAState *s,
|
2004-06-03 18:46:20 +00:00
|
|
|
const uint8_t *data, int len);
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
/* MacOS uses timer 1 for calibration on startup, so we use
|
|
|
|
* the timebase frequency and cuda_get_counter_value() with
|
|
|
|
* cuda_get_load_time() to steer MacOS to calculate calibrate its timers
|
|
|
|
* correctly for both TCG and KVM (see commit b981289c49 "PPC: Cuda: Use cuda
|
|
|
|
* timer to expose tbfreq to guest" for more information) */
|
2004-06-03 18:46:20 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
static uint64_t cuda_get_counter_value(MOS6522State *s, MOS6522Timer *ti)
|
2014-07-13 20:31:53 +00:00
|
|
|
{
|
2018-02-09 18:51:40 +00:00
|
|
|
MOS6522CUDAState *mcs = container_of(s, MOS6522CUDAState, parent_obj);
|
2018-06-07 17:17:49 +00:00
|
|
|
CUDAState *cs = container_of(mcs, CUDAState, mos6522_cuda);
|
2018-02-09 18:51:40 +00:00
|
|
|
|
2018-02-09 18:51:38 +00:00
|
|
|
/* Reverse of the tb calculation algorithm that Mac OS X uses on bootup */
|
|
|
|
uint64_t tb_diff = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
|
2018-02-09 18:51:40 +00:00
|
|
|
cs->tb_frequency, NANOSECONDS_PER_SECOND) -
|
2018-02-09 18:51:38 +00:00
|
|
|
ti->load_time;
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
return (tb_diff * 0xBF401675E5DULL) / (cs->tb_frequency << 24);
|
2018-02-09 18:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
static uint64_t cuda_get_load_time(MOS6522State *s, MOS6522Timer *ti)
|
2018-02-09 18:51:38 +00:00
|
|
|
{
|
2018-02-09 18:51:40 +00:00
|
|
|
MOS6522CUDAState *mcs = container_of(s, MOS6522CUDAState, parent_obj);
|
2018-06-07 17:17:49 +00:00
|
|
|
CUDAState *cs = container_of(mcs, CUDAState, mos6522_cuda);
|
2018-02-09 18:51:40 +00:00
|
|
|
|
2018-02-09 18:51:38 +00:00
|
|
|
uint64_t load_time = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
|
2018-02-09 18:51:40 +00:00
|
|
|
cs->tb_frequency, NANOSECONDS_PER_SECOND);
|
2018-02-09 18:51:38 +00:00
|
|
|
return load_time;
|
2014-07-13 20:31:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 22:49:51 +00:00
|
|
|
static void cuda_set_sr_int(void *opaque)
|
|
|
|
{
|
|
|
|
CUDAState *s = opaque;
|
2018-06-07 17:17:49 +00:00
|
|
|
MOS6522CUDAState *mcs = &s->mos6522_cuda;
|
2018-02-09 18:51:40 +00:00
|
|
|
MOS6522State *ms = MOS6522(mcs);
|
|
|
|
MOS6522DeviceClass *mdc = MOS6522_DEVICE_GET_CLASS(ms);
|
2015-11-11 22:49:51 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
mdc->set_sr_int(ms);
|
2015-11-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cuda_delay_set_sr_int(CUDAState *s)
|
|
|
|
{
|
|
|
|
int64_t expire;
|
|
|
|
|
2018-02-09 18:51:42 +00:00
|
|
|
trace_cuda_delay_set_sr_int();
|
2015-11-11 22:49:51 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
expire = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->sr_delay_ns;
|
2015-11-11 22:49:51 +00:00
|
|
|
timer_mod(s->sr_delay_timer, expire);
|
|
|
|
}
|
|
|
|
|
2004-06-03 18:46:20 +00:00
|
|
|
/* NOTE: TIP and TREQ are negated */
|
|
|
|
static void cuda_update(CUDAState *s)
|
|
|
|
{
|
2018-06-07 17:17:49 +00:00
|
|
|
MOS6522CUDAState *mcs = &s->mos6522_cuda;
|
2018-02-09 18:51:40 +00:00
|
|
|
MOS6522State *ms = MOS6522(mcs);
|
2004-06-21 16:47:13 +00:00
|
|
|
int packet_received, len;
|
|
|
|
|
|
|
|
packet_received = 0;
|
2018-02-09 18:51:40 +00:00
|
|
|
if (!(ms->b & TIP)) {
|
2004-06-21 16:47:13 +00:00
|
|
|
/* transfer requested from host */
|
2004-06-03 18:46:20 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
if (ms->acr & SR_OUT) {
|
2004-06-21 16:47:13 +00:00
|
|
|
/* data output */
|
2018-02-09 18:51:40 +00:00
|
|
|
if ((ms->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
|
2004-06-21 16:47:13 +00:00
|
|
|
if (s->data_out_index < sizeof(s->data_out)) {
|
2018-02-09 18:51:42 +00:00
|
|
|
trace_cuda_data_send(ms->sr);
|
2018-02-09 18:51:40 +00:00
|
|
|
s->data_out[s->data_out_index++] = ms->sr;
|
2015-11-11 22:49:51 +00:00
|
|
|
cuda_delay_set_sr_int(s);
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (s->data_in_index < s->data_in_size) {
|
|
|
|
/* data input */
|
2018-02-09 18:51:40 +00:00
|
|
|
if ((ms->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
|
|
|
|
ms->sr = s->data_in[s->data_in_index++];
|
2018-02-09 18:51:42 +00:00
|
|
|
trace_cuda_data_recv(ms->sr);
|
2004-06-21 16:47:13 +00:00
|
|
|
/* indicate end of transfer */
|
|
|
|
if (s->data_in_index >= s->data_in_size) {
|
2018-02-09 18:51:40 +00:00
|
|
|
ms->b = (ms->b | TREQ);
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
2015-11-11 22:49:51 +00:00
|
|
|
cuda_delay_set_sr_int(s);
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
2004-06-03 18:46:20 +00:00
|
|
|
}
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* no transfer requested: handle sync case */
|
2018-02-09 18:51:40 +00:00
|
|
|
if ((s->last_b & TIP) && (ms->b & TACK) != (s->last_b & TACK)) {
|
2004-06-21 16:47:13 +00:00
|
|
|
/* update TREQ state each time TACK change state */
|
2018-02-09 18:51:40 +00:00
|
|
|
if (ms->b & TACK) {
|
|
|
|
ms->b = (ms->b | TREQ);
|
|
|
|
} else {
|
|
|
|
ms->b = (ms->b & ~TREQ);
|
|
|
|
}
|
2015-11-11 22:49:51 +00:00
|
|
|
cuda_delay_set_sr_int(s);
|
2004-06-21 16:47:13 +00:00
|
|
|
} else {
|
|
|
|
if (!(s->last_b & TIP)) {
|
2007-06-03 13:35:16 +00:00
|
|
|
/* handle end of host to cuda transfer */
|
2004-06-21 16:47:13 +00:00
|
|
|
packet_received = (s->data_out_index > 0);
|
2007-06-03 13:35:16 +00:00
|
|
|
/* always an IRQ at the end of transfer */
|
2015-11-11 22:49:51 +00:00
|
|
|
cuda_delay_set_sr_int(s);
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
|
|
|
/* signal if there is data to read */
|
|
|
|
if (s->data_in_index < s->data_in_size) {
|
2018-02-09 18:51:40 +00:00
|
|
|
ms->b = (ms->b & ~TREQ);
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
2004-06-03 18:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
s->last_acr = ms->acr;
|
|
|
|
s->last_b = ms->b;
|
2004-06-21 16:47:13 +00:00
|
|
|
|
|
|
|
/* NOTE: cuda_receive_packet_from_host() can call cuda_update()
|
|
|
|
recursively */
|
|
|
|
if (packet_received) {
|
|
|
|
len = s->data_out_index;
|
|
|
|
s->data_out_index = 0;
|
|
|
|
cuda_receive_packet_from_host(s, s->data_out, len);
|
|
|
|
}
|
2004-06-03 18:46:20 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 21:08:06 +00:00
|
|
|
static void cuda_send_packet_to_host(CUDAState *s,
|
2004-06-03 18:46:20 +00:00
|
|
|
const uint8_t *data, int len)
|
|
|
|
{
|
2018-02-09 18:51:42 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
trace_cuda_packet_send(len);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
trace_cuda_packet_send_data(i, data[i]);
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
2018-02-09 18:51:42 +00:00
|
|
|
|
2004-06-03 18:46:20 +00:00
|
|
|
memcpy(s->data_in, data, len);
|
|
|
|
s->data_in_size = len;
|
|
|
|
s->data_in_index = 0;
|
|
|
|
cuda_update(s);
|
2015-11-11 22:49:51 +00:00
|
|
|
cuda_delay_set_sr_int(s);
|
2004-06-03 18:46:20 +00:00
|
|
|
}
|
|
|
|
|
2004-07-06 20:57:47 +00:00
|
|
|
static void cuda_adb_poll(void *opaque)
|
2004-06-21 22:46:10 +00:00
|
|
|
{
|
|
|
|
CUDAState *s = opaque;
|
|
|
|
uint8_t obuf[ADB_MAX_OUT_LEN + 2];
|
|
|
|
int olen;
|
|
|
|
|
2016-02-07 20:34:08 +00:00
|
|
|
olen = adb_poll(&s->adb_bus, obuf + 2, s->adb_poll_mask);
|
2004-06-21 22:46:10 +00:00
|
|
|
if (olen > 0) {
|
|
|
|
obuf[0] = ADB_PACKET;
|
|
|
|
obuf[1] = 0x40; /* polled data */
|
|
|
|
cuda_send_packet_to_host(s, obuf, olen + 2);
|
|
|
|
}
|
2018-02-09 18:51:40 +00:00
|
|
|
timer_mod(s->adb_poll_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
|
|
|
|
(NANOSECONDS_PER_SECOND / (1000 / s->autopoll_rate_ms)));
|
2004-06-21 22:46:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:04 +00:00
|
|
|
/* description of commands */
|
|
|
|
typedef struct CudaCommand {
|
|
|
|
uint8_t command;
|
|
|
|
const char *name;
|
|
|
|
bool (*handler)(CUDAState *s,
|
|
|
|
const uint8_t *in_args, int in_len,
|
|
|
|
uint8_t *out_args, int *out_len);
|
|
|
|
} CudaCommand;
|
|
|
|
|
2016-02-07 20:34:06 +00:00
|
|
|
static bool cuda_cmd_autopoll(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
int autopoll;
|
|
|
|
|
|
|
|
if (in_len != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
autopoll = (in_data[0] != 0);
|
|
|
|
if (autopoll != s->autopoll) {
|
|
|
|
s->autopoll = autopoll;
|
|
|
|
if (autopoll) {
|
|
|
|
timer_mod(s->adb_poll_timer,
|
|
|
|
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
|
2016-03-21 16:02:30 +00:00
|
|
|
(NANOSECONDS_PER_SECOND / (1000 / s->autopoll_rate_ms)));
|
2016-02-07 20:34:06 +00:00
|
|
|
} else {
|
|
|
|
timer_del(s->adb_poll_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:07 +00:00
|
|
|
static bool cuda_cmd_set_autorate(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
if (in_len != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we don't want a period of 0 ms */
|
|
|
|
/* FIXME: check what real hardware does */
|
|
|
|
if (in_data[0] == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->autopoll_rate_ms = in_data[0];
|
|
|
|
if (s->autopoll) {
|
|
|
|
timer_mod(s->adb_poll_timer,
|
|
|
|
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
|
2016-03-21 16:02:30 +00:00
|
|
|
(NANOSECONDS_PER_SECOND / (1000 / s->autopoll_rate_ms)));
|
2016-02-07 20:34:07 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:08 +00:00
|
|
|
static bool cuda_cmd_set_device_list(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
if (in_len != 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->adb_poll_mask = (((uint16_t)in_data[0]) << 8) | in_data[1];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:09 +00:00
|
|
|
static bool cuda_cmd_powerdown(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
if (in_len != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:41:13 +00:00
|
|
|
qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
|
2016-02-07 20:34:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:10 +00:00
|
|
|
static bool cuda_cmd_reset_system(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
if (in_len != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:41:13 +00:00
|
|
|
qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
|
2016-02-07 20:34:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:11 +00:00
|
|
|
static bool cuda_cmd_set_file_server_flag(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
if (in_len != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"CUDA: unimplemented command FILE_SERVER_FLAG %d\n",
|
|
|
|
in_data[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:12 +00:00
|
|
|
static bool cuda_cmd_set_power_message(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
if (in_len != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"CUDA: unimplemented command SET_POWER_MESSAGE %d\n",
|
|
|
|
in_data[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:13 +00:00
|
|
|
static bool cuda_cmd_get_time(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
uint32_t ti;
|
|
|
|
|
|
|
|
if (in_len != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ti = s->tick_offset + (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)
|
2016-03-21 16:02:30 +00:00
|
|
|
/ NANOSECONDS_PER_SECOND);
|
2016-02-07 20:34:13 +00:00
|
|
|
out_data[0] = ti >> 24;
|
|
|
|
out_data[1] = ti >> 16;
|
|
|
|
out_data[2] = ti >> 8;
|
|
|
|
out_data[3] = ti;
|
|
|
|
*out_len = 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:14 +00:00
|
|
|
static bool cuda_cmd_set_time(CUDAState *s,
|
|
|
|
const uint8_t *in_data, int in_len,
|
|
|
|
uint8_t *out_data, int *out_len)
|
|
|
|
{
|
|
|
|
uint32_t ti;
|
|
|
|
|
|
|
|
if (in_len != 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-18 08:07:45 +00:00
|
|
|
ti = (((uint32_t)in_data[0]) << 24) + (((uint32_t)in_data[1]) << 16)
|
|
|
|
+ (((uint32_t)in_data[2]) << 8) + in_data[3];
|
2016-02-07 20:34:14 +00:00
|
|
|
s->tick_offset = ti - (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)
|
2016-03-21 16:02:30 +00:00
|
|
|
/ NANOSECONDS_PER_SECOND);
|
2016-02-07 20:34:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:04 +00:00
|
|
|
static const CudaCommand handlers[] = {
|
2016-02-07 20:34:06 +00:00
|
|
|
{ CUDA_AUTOPOLL, "AUTOPOLL", cuda_cmd_autopoll },
|
2016-02-07 20:34:07 +00:00
|
|
|
{ CUDA_SET_AUTO_RATE, "SET_AUTO_RATE", cuda_cmd_set_autorate },
|
2016-02-07 20:34:08 +00:00
|
|
|
{ CUDA_SET_DEVICE_LIST, "SET_DEVICE_LIST", cuda_cmd_set_device_list },
|
2016-02-07 20:34:09 +00:00
|
|
|
{ CUDA_POWERDOWN, "POWERDOWN", cuda_cmd_powerdown },
|
2016-02-07 20:34:10 +00:00
|
|
|
{ CUDA_RESET_SYSTEM, "RESET_SYSTEM", cuda_cmd_reset_system },
|
2016-02-07 20:34:11 +00:00
|
|
|
{ CUDA_FILE_SERVER_FLAG, "FILE_SERVER_FLAG",
|
|
|
|
cuda_cmd_set_file_server_flag },
|
2016-02-07 20:34:12 +00:00
|
|
|
{ CUDA_SET_POWER_MESSAGES, "SET_POWER_MESSAGES",
|
|
|
|
cuda_cmd_set_power_message },
|
2016-02-07 20:34:13 +00:00
|
|
|
{ CUDA_GET_TIME, "GET_TIME", cuda_cmd_get_time },
|
2016-02-07 20:34:14 +00:00
|
|
|
{ CUDA_SET_TIME, "SET_TIME", cuda_cmd_set_time },
|
2016-02-07 20:34:04 +00:00
|
|
|
};
|
|
|
|
|
2007-09-16 21:08:06 +00:00
|
|
|
static void cuda_receive_packet(CUDAState *s,
|
2004-06-03 18:46:20 +00:00
|
|
|
const uint8_t *data, int len)
|
|
|
|
{
|
2015-11-11 22:49:43 +00:00
|
|
|
uint8_t obuf[16] = { CUDA_PACKET, 0, data[0] };
|
2016-02-07 20:34:04 +00:00
|
|
|
int i, out_len = 0;
|
2004-06-03 18:46:20 +00:00
|
|
|
|
2016-02-07 20:34:04 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(handlers); i++) {
|
|
|
|
const CudaCommand *desc = &handlers[i];
|
|
|
|
if (desc->command == data[0]) {
|
2018-02-09 18:51:42 +00:00
|
|
|
trace_cuda_receive_packet_cmd(desc->name);
|
2016-02-07 20:34:04 +00:00
|
|
|
out_len = 0;
|
|
|
|
if (desc->handler(s, data + 1, len - 1, obuf + 3, &out_len)) {
|
|
|
|
cuda_send_packet_to_host(s, obuf, 3 + out_len);
|
|
|
|
} else {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"CUDA: %s: wrong parameters %d\n",
|
|
|
|
desc->name, len);
|
|
|
|
obuf[0] = ERROR_PACKET;
|
|
|
|
obuf[1] = 0x5; /* bad parameters */
|
|
|
|
obuf[2] = CUDA_PACKET;
|
|
|
|
obuf[3] = data[0];
|
|
|
|
cuda_send_packet_to_host(s, obuf, 4);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:34:05 +00:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "CUDA: unknown command 0x%02x\n", data[0]);
|
|
|
|
obuf[0] = ERROR_PACKET;
|
|
|
|
obuf[1] = 0x2; /* unknown command */
|
|
|
|
obuf[2] = CUDA_PACKET;
|
|
|
|
obuf[3] = data[0];
|
|
|
|
cuda_send_packet_to_host(s, obuf, 4);
|
2004-06-03 18:46:20 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 21:08:06 +00:00
|
|
|
static void cuda_receive_packet_from_host(CUDAState *s,
|
2004-06-03 18:46:20 +00:00
|
|
|
const uint8_t *data, int len)
|
|
|
|
{
|
2018-02-09 18:51:42 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
trace_cuda_packet_receive(len);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
trace_cuda_packet_receive_data(i, data[i]);
|
2004-06-21 16:47:13 +00:00
|
|
|
}
|
2018-02-09 18:51:42 +00:00
|
|
|
|
2004-06-03 18:46:20 +00:00
|
|
|
switch(data[0]) {
|
|
|
|
case ADB_PACKET:
|
2004-06-21 22:46:10 +00:00
|
|
|
{
|
2015-11-11 22:49:42 +00:00
|
|
|
uint8_t obuf[ADB_MAX_OUT_LEN + 3];
|
2004-06-21 22:46:10 +00:00
|
|
|
int olen;
|
2013-01-23 23:04:05 +00:00
|
|
|
olen = adb_request(&s->adb_bus, obuf + 2, data + 1, len - 1);
|
2004-07-12 20:16:00 +00:00
|
|
|
if (olen > 0) {
|
2004-06-21 22:46:10 +00:00
|
|
|
obuf[0] = ADB_PACKET;
|
|
|
|
obuf[1] = 0x00;
|
2015-11-11 22:49:42 +00:00
|
|
|
cuda_send_packet_to_host(s, obuf, olen + 2);
|
2004-06-21 22:46:10 +00:00
|
|
|
} else {
|
2004-07-12 20:16:00 +00:00
|
|
|
/* error */
|
2004-06-21 22:46:10 +00:00
|
|
|
obuf[0] = ADB_PACKET;
|
2004-07-12 20:16:00 +00:00
|
|
|
obuf[1] = -olen;
|
2015-11-11 22:49:42 +00:00
|
|
|
obuf[2] = data[1];
|
2004-07-12 20:16:00 +00:00
|
|
|
olen = 0;
|
2015-11-11 22:49:42 +00:00
|
|
|
cuda_send_packet_to_host(s, obuf, olen + 3);
|
2004-06-21 22:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
2004-06-03 18:46:20 +00:00
|
|
|
break;
|
|
|
|
case CUDA_PACKET:
|
|
|
|
cuda_receive_packet(s, data + 1, len - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
static uint64_t mos6522_cuda_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
CUDAState *s = opaque;
|
2018-06-07 17:17:49 +00:00
|
|
|
MOS6522CUDAState *mcs = &s->mos6522_cuda;
|
2018-02-09 18:51:40 +00:00
|
|
|
MOS6522State *ms = MOS6522(mcs);
|
2004-06-03 18:46:20 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
addr = (addr >> 9) & 0xf;
|
|
|
|
return mos6522_read(ms, addr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mos6522_cuda_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned size)
|
2008-12-30 19:01:19 +00:00
|
|
|
{
|
2018-02-09 18:51:40 +00:00
|
|
|
CUDAState *s = opaque;
|
2018-06-07 17:17:49 +00:00
|
|
|
MOS6522CUDAState *mcs = &s->mos6522_cuda;
|
2018-02-09 18:51:40 +00:00
|
|
|
MOS6522State *ms = MOS6522(mcs);
|
2008-12-30 19:01:19 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
addr = (addr >> 9) & 0xf;
|
|
|
|
mos6522_write(ms, addr, val, size);
|
2008-12-30 19:01:19 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
static const MemoryRegionOps mos6522_cuda_ops = {
|
|
|
|
.read = mos6522_cuda_read,
|
|
|
|
.write = mos6522_cuda_write,
|
|
|
|
.endianness = DEVICE_BIG_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
2010-12-02 12:53:24 +00:00
|
|
|
};
|
2008-12-30 19:01:19 +00:00
|
|
|
|
2010-12-02 12:53:24 +00:00
|
|
|
static const VMStateDescription vmstate_cuda = {
|
|
|
|
.name = "cuda",
|
2018-06-07 17:17:49 +00:00
|
|
|
.version_id = 5,
|
|
|
|
.minimum_version_id = 5,
|
2014-04-16 14:01:33 +00:00
|
|
|
.fields = (VMStateField[]) {
|
2018-06-07 17:17:49 +00:00
|
|
|
VMSTATE_STRUCT(mos6522_cuda.parent_obj, CUDAState, 0, vmstate_mos6522,
|
|
|
|
MOS6522State),
|
2016-01-06 20:37:26 +00:00
|
|
|
VMSTATE_UINT8(last_b, CUDAState),
|
|
|
|
VMSTATE_UINT8(last_acr, CUDAState),
|
2010-12-02 12:53:24 +00:00
|
|
|
VMSTATE_INT32(data_in_size, CUDAState),
|
|
|
|
VMSTATE_INT32(data_in_index, CUDAState),
|
|
|
|
VMSTATE_INT32(data_out_index, CUDAState),
|
|
|
|
VMSTATE_UINT8(autopoll, CUDAState),
|
2016-02-07 20:34:07 +00:00
|
|
|
VMSTATE_UINT8(autopoll_rate_ms, CUDAState),
|
2016-02-07 20:34:08 +00:00
|
|
|
VMSTATE_UINT16(adb_poll_mask, CUDAState),
|
2010-12-02 12:53:24 +00:00
|
|
|
VMSTATE_BUFFER(data_in, CUDAState),
|
|
|
|
VMSTATE_BUFFER(data_out, CUDAState),
|
|
|
|
VMSTATE_UINT32(tick_offset, CUDAState),
|
2015-02-09 22:40:46 +00:00
|
|
|
VMSTATE_TIMER_PTR(adb_poll_timer, CUDAState),
|
2016-01-06 20:37:26 +00:00
|
|
|
VMSTATE_TIMER_PTR(sr_delay_timer, CUDAState),
|
2010-12-02 12:53:24 +00:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2008-12-30 19:01:19 +00:00
|
|
|
|
2013-01-23 23:04:02 +00:00
|
|
|
static void cuda_reset(DeviceState *dev)
|
2008-12-28 18:27:10 +00:00
|
|
|
{
|
2013-01-23 23:04:02 +00:00
|
|
|
CUDAState *s = CUDA(dev);
|
2008-12-28 18:27:10 +00:00
|
|
|
|
|
|
|
s->data_in_size = 0;
|
|
|
|
s->data_in_index = 0;
|
|
|
|
s->data_out_index = 0;
|
|
|
|
s->autopoll = 0;
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
static void cuda_realize(DeviceState *dev, Error **errp)
|
2004-06-03 18:46:20 +00:00
|
|
|
{
|
2013-01-23 23:04:02 +00:00
|
|
|
CUDAState *s = CUDA(dev);
|
2018-02-09 18:51:40 +00:00
|
|
|
SysBusDevice *sbd;
|
|
|
|
MOS6522State *ms;
|
|
|
|
DeviceState *d;
|
2009-01-15 18:57:15 +00:00
|
|
|
struct tm tm;
|
2004-06-21 16:47:13 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
/* Pass IRQ from 6522 */
|
2018-06-07 17:17:49 +00:00
|
|
|
d = DEVICE(&s->mos6522_cuda);
|
2018-02-09 18:51:40 +00:00
|
|
|
ms = MOS6522(d);
|
|
|
|
sbd = SYS_BUS_DEVICE(s);
|
|
|
|
sysbus_pass_irq(sbd, SYS_BUS_DEVICE(ms));
|
2005-07-07 21:45:18 +00:00
|
|
|
|
2009-01-18 12:16:26 +00:00
|
|
|
qemu_get_timedate(&tm, 0);
|
|
|
|
s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
|
2009-01-15 18:57:15 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
s->sr_delay_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, cuda_set_sr_int, s);
|
2019-02-10 17:44:21 +00:00
|
|
|
s->sr_delay_ns = 20 * SCALE_US;
|
2018-02-09 18:51:40 +00:00
|
|
|
|
2013-08-21 15:03:08 +00:00
|
|
|
s->adb_poll_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, cuda_adb_poll, s);
|
2016-02-07 20:34:08 +00:00
|
|
|
s->adb_poll_mask = 0xffff;
|
2018-02-09 18:51:40 +00:00
|
|
|
s->autopoll_rate_ms = 20;
|
2013-01-23 23:04:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
static void cuda_init(Object *obj)
|
2013-01-23 23:04:02 +00:00
|
|
|
{
|
|
|
|
CUDAState *s = CUDA(obj);
|
2018-02-09 18:51:40 +00:00
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
|
2013-01-23 23:04:02 +00:00
|
|
|
|
2018-07-17 14:51:54 +00:00
|
|
|
sysbus_init_child_obj(obj, "mos6522-cuda", &s->mos6522_cuda,
|
|
|
|
sizeof(s->mos6522_cuda), TYPE_MOS6522_CUDA);
|
2018-06-07 17:17:49 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
memory_region_init_io(&s->mem, obj, &mos6522_cuda_ops, s, "cuda", 0x2000);
|
|
|
|
sysbus_init_mmio(sbd, &s->mem);
|
2013-01-23 23:04:03 +00:00
|
|
|
|
2013-08-23 22:02:27 +00:00
|
|
|
qbus_create_inplace(&s->adb_bus, sizeof(s->adb_bus), TYPE_ADB_BUS,
|
|
|
|
DEVICE(obj), "adb.0");
|
2013-01-23 23:04:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 20:31:53 +00:00
|
|
|
static Property cuda_properties[] = {
|
2018-02-09 18:51:35 +00:00
|
|
|
DEFINE_PROP_UINT64("timebase-frequency", CUDAState, tb_frequency, 0),
|
2014-07-13 20:31:53 +00:00
|
|
|
DEFINE_PROP_END_OF_LIST()
|
|
|
|
};
|
|
|
|
|
2013-01-23 23:04:02 +00:00
|
|
|
static void cuda_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
2011-09-08 16:51:17 +00:00
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
dc->realize = cuda_realize;
|
2013-01-23 23:04:02 +00:00
|
|
|
dc->reset = cuda_reset;
|
|
|
|
dc->vmsd = &vmstate_cuda;
|
2020-01-10 15:30:32 +00:00
|
|
|
device_class_set_props(dc, cuda_properties);
|
2015-09-26 16:22:07 +00:00
|
|
|
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
|
2004-06-03 18:46:20 +00:00
|
|
|
}
|
2013-01-23 23:04:02 +00:00
|
|
|
|
|
|
|
static const TypeInfo cuda_type_info = {
|
|
|
|
.name = TYPE_CUDA,
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(CUDAState),
|
2018-02-09 18:51:40 +00:00
|
|
|
.instance_init = cuda_init,
|
2013-01-23 23:04:02 +00:00
|
|
|
.class_init = cuda_class_init,
|
|
|
|
};
|
|
|
|
|
2018-02-09 18:51:40 +00:00
|
|
|
static void mos6522_cuda_portB_write(MOS6522State *s)
|
|
|
|
{
|
|
|
|
MOS6522CUDAState *mcs = container_of(s, MOS6522CUDAState, parent_obj);
|
2018-06-07 17:17:49 +00:00
|
|
|
CUDAState *cs = container_of(mcs, CUDAState, mos6522_cuda);
|
2018-02-09 18:51:40 +00:00
|
|
|
|
2018-06-07 17:17:49 +00:00
|
|
|
cuda_update(cs);
|
2018-02-09 18:51:40 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 17:17:50 +00:00
|
|
|
static void mos6522_cuda_reset(DeviceState *dev)
|
2018-02-09 18:51:40 +00:00
|
|
|
{
|
|
|
|
MOS6522State *ms = MOS6522(dev);
|
|
|
|
MOS6522DeviceClass *mdc = MOS6522_DEVICE_GET_CLASS(ms);
|
|
|
|
|
2018-06-07 17:17:50 +00:00
|
|
|
mdc->parent_reset(dev);
|
2018-02-09 18:51:40 +00:00
|
|
|
|
|
|
|
ms->timers[0].frequency = CUDA_TIMER_FREQ;
|
|
|
|
ms->timers[1].frequency = (SCALE_US * 6000) / 4700;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mos6522_cuda_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
MOS6522DeviceClass *mdc = MOS6522_DEVICE_CLASS(oc);
|
|
|
|
|
2018-06-07 17:17:50 +00:00
|
|
|
dc->reset = mos6522_cuda_reset;
|
2018-02-09 18:51:40 +00:00
|
|
|
mdc->portB_write = mos6522_cuda_portB_write;
|
|
|
|
mdc->get_timer1_counter_value = cuda_get_counter_value;
|
|
|
|
mdc->get_timer2_counter_value = cuda_get_counter_value;
|
|
|
|
mdc->get_timer1_load_time = cuda_get_load_time;
|
|
|
|
mdc->get_timer2_load_time = cuda_get_load_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo mos6522_cuda_type_info = {
|
|
|
|
.name = TYPE_MOS6522_CUDA,
|
|
|
|
.parent = TYPE_MOS6522,
|
|
|
|
.instance_size = sizeof(MOS6522CUDAState),
|
|
|
|
.class_init = mos6522_cuda_class_init,
|
|
|
|
};
|
|
|
|
|
2013-01-23 23:04:02 +00:00
|
|
|
static void cuda_register_types(void)
|
|
|
|
{
|
2018-02-09 18:51:40 +00:00
|
|
|
type_register_static(&mos6522_cuda_type_info);
|
2013-01-23 23:04:02 +00:00
|
|
|
type_register_static(&cuda_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(cuda_register_types)
|