cuda.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * QEMU PowerMac CUDA device support
  3. *
  4. * Copyright (c) 2004-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "hw.h"
  26. #include "ppc_mac.h"
  27. #include "adb.h"
  28. #include "qemu-timer.h"
  29. #include "sysemu.h"
  30. /* XXX: implement all timer modes */
  31. /* debug CUDA */
  32. //#define DEBUG_CUDA
  33. /* debug CUDA packets */
  34. //#define DEBUG_CUDA_PACKET
  35. #ifdef DEBUG_CUDA
  36. #define CUDA_DPRINTF(fmt, ...) \
  37. do { printf("CUDA: " fmt , ## __VA_ARGS__); } while (0)
  38. #else
  39. #define CUDA_DPRINTF(fmt, ...)
  40. #endif
  41. /* Bits in B data register: all active low */
  42. #define TREQ 0x08 /* Transfer request (input) */
  43. #define TACK 0x10 /* Transfer acknowledge (output) */
  44. #define TIP 0x20 /* Transfer in progress (output) */
  45. /* Bits in ACR */
  46. #define SR_CTRL 0x1c /* Shift register control bits */
  47. #define SR_EXT 0x0c /* Shift on external clock */
  48. #define SR_OUT 0x10 /* Shift out if 1 */
  49. /* Bits in IFR and IER */
  50. #define IER_SET 0x80 /* set bits in IER */
  51. #define IER_CLR 0 /* clear bits in IER */
  52. #define SR_INT 0x04 /* Shift register full/empty */
  53. #define T1_INT 0x40 /* Timer 1 interrupt */
  54. #define T2_INT 0x20 /* Timer 2 interrupt */
  55. /* Bits in ACR */
  56. #define T1MODE 0xc0 /* Timer 1 mode */
  57. #define T1MODE_CONT 0x40 /* continuous interrupts */
  58. /* commands (1st byte) */
  59. #define ADB_PACKET 0
  60. #define CUDA_PACKET 1
  61. #define ERROR_PACKET 2
  62. #define TIMER_PACKET 3
  63. #define POWER_PACKET 4
  64. #define MACIIC_PACKET 5
  65. #define PMU_PACKET 6
  66. /* CUDA commands (2nd byte) */
  67. #define CUDA_WARM_START 0x0
  68. #define CUDA_AUTOPOLL 0x1
  69. #define CUDA_GET_6805_ADDR 0x2
  70. #define CUDA_GET_TIME 0x3
  71. #define CUDA_GET_PRAM 0x7
  72. #define CUDA_SET_6805_ADDR 0x8
  73. #define CUDA_SET_TIME 0x9
  74. #define CUDA_POWERDOWN 0xa
  75. #define CUDA_POWERUP_TIME 0xb
  76. #define CUDA_SET_PRAM 0xc
  77. #define CUDA_MS_RESET 0xd
  78. #define CUDA_SEND_DFAC 0xe
  79. #define CUDA_BATTERY_SWAP_SENSE 0x10
  80. #define CUDA_RESET_SYSTEM 0x11
  81. #define CUDA_SET_IPL 0x12
  82. #define CUDA_FILE_SERVER_FLAG 0x13
  83. #define CUDA_SET_AUTO_RATE 0x14
  84. #define CUDA_GET_AUTO_RATE 0x16
  85. #define CUDA_SET_DEVICE_LIST 0x19
  86. #define CUDA_GET_DEVICE_LIST 0x1a
  87. #define CUDA_SET_ONE_SECOND_MODE 0x1b
  88. #define CUDA_SET_POWER_MESSAGES 0x21
  89. #define CUDA_GET_SET_IIC 0x22
  90. #define CUDA_WAKEUP 0x23
  91. #define CUDA_TIMER_TICKLE 0x24
  92. #define CUDA_COMBINED_FORMAT_IIC 0x25
  93. #define CUDA_TIMER_FREQ (4700000 / 6)
  94. #define CUDA_ADB_POLL_FREQ 50
  95. /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
  96. #define RTC_OFFSET 2082844800
  97. typedef struct CUDATimer {
  98. int index;
  99. uint16_t latch;
  100. uint16_t counter_value; /* counter value at load time */
  101. int64_t load_time;
  102. int64_t next_irq_time;
  103. QEMUTimer *timer;
  104. } CUDATimer;
  105. typedef struct CUDAState {
  106. MemoryRegion mem;
  107. /* cuda registers */
  108. uint8_t b; /* B-side data */
  109. uint8_t a; /* A-side data */
  110. uint8_t dirb; /* B-side direction (1=output) */
  111. uint8_t dira; /* A-side direction (1=output) */
  112. uint8_t sr; /* Shift register */
  113. uint8_t acr; /* Auxiliary control register */
  114. uint8_t pcr; /* Peripheral control register */
  115. uint8_t ifr; /* Interrupt flag register */
  116. uint8_t ier; /* Interrupt enable register */
  117. uint8_t anh; /* A-side data, no handshake */
  118. CUDATimer timers[2];
  119. uint32_t tick_offset;
  120. uint8_t last_b; /* last value of B register */
  121. uint8_t last_acr; /* last value of B register */
  122. int data_in_size;
  123. int data_in_index;
  124. int data_out_index;
  125. qemu_irq irq;
  126. uint8_t autopoll;
  127. uint8_t data_in[128];
  128. uint8_t data_out[16];
  129. QEMUTimer *adb_poll_timer;
  130. } CUDAState;
  131. static CUDAState cuda_state;
  132. ADBBusState adb_bus;
  133. static void cuda_update(CUDAState *s);
  134. static void cuda_receive_packet_from_host(CUDAState *s,
  135. const uint8_t *data, int len);
  136. static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
  137. int64_t current_time);
  138. static void cuda_update_irq(CUDAState *s)
  139. {
  140. if (s->ifr & s->ier & (SR_INT | T1_INT)) {
  141. qemu_irq_raise(s->irq);
  142. } else {
  143. qemu_irq_lower(s->irq);
  144. }
  145. }
  146. static unsigned int get_counter(CUDATimer *s)
  147. {
  148. int64_t d;
  149. unsigned int counter;
  150. d = muldiv64(qemu_get_clock_ns(vm_clock) - s->load_time,
  151. CUDA_TIMER_FREQ, get_ticks_per_sec());
  152. if (s->index == 0) {
  153. /* the timer goes down from latch to -1 (period of latch + 2) */
  154. if (d <= (s->counter_value + 1)) {
  155. counter = (s->counter_value - d) & 0xffff;
  156. } else {
  157. counter = (d - (s->counter_value + 1)) % (s->latch + 2);
  158. counter = (s->latch - counter) & 0xffff;
  159. }
  160. } else {
  161. counter = (s->counter_value - d) & 0xffff;
  162. }
  163. return counter;
  164. }
  165. static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
  166. {
  167. CUDA_DPRINTF("T%d.counter=%d\n", 1 + (ti->timer == NULL), val);
  168. ti->load_time = qemu_get_clock_ns(vm_clock);
  169. ti->counter_value = val;
  170. cuda_timer_update(s, ti, ti->load_time);
  171. }
  172. static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
  173. {
  174. int64_t d, next_time;
  175. unsigned int counter;
  176. /* current counter value */
  177. d = muldiv64(current_time - s->load_time,
  178. CUDA_TIMER_FREQ, get_ticks_per_sec());
  179. /* the timer goes down from latch to -1 (period of latch + 2) */
  180. if (d <= (s->counter_value + 1)) {
  181. counter = (s->counter_value - d) & 0xffff;
  182. } else {
  183. counter = (d - (s->counter_value + 1)) % (s->latch + 2);
  184. counter = (s->latch - counter) & 0xffff;
  185. }
  186. /* Note: we consider the irq is raised on 0 */
  187. if (counter == 0xffff) {
  188. next_time = d + s->latch + 1;
  189. } else if (counter == 0) {
  190. next_time = d + s->latch + 2;
  191. } else {
  192. next_time = d + counter;
  193. }
  194. CUDA_DPRINTF("latch=%d counter=%" PRId64 " delta_next=%" PRId64 "\n",
  195. s->latch, d, next_time - d);
  196. next_time = muldiv64(next_time, get_ticks_per_sec(), CUDA_TIMER_FREQ) +
  197. s->load_time;
  198. if (next_time <= current_time)
  199. next_time = current_time + 1;
  200. return next_time;
  201. }
  202. static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
  203. int64_t current_time)
  204. {
  205. if (!ti->timer)
  206. return;
  207. if ((s->acr & T1MODE) != T1MODE_CONT) {
  208. qemu_del_timer(ti->timer);
  209. } else {
  210. ti->next_irq_time = get_next_irq_time(ti, current_time);
  211. qemu_mod_timer(ti->timer, ti->next_irq_time);
  212. }
  213. }
  214. static void cuda_timer1(void *opaque)
  215. {
  216. CUDAState *s = opaque;
  217. CUDATimer *ti = &s->timers[0];
  218. cuda_timer_update(s, ti, ti->next_irq_time);
  219. s->ifr |= T1_INT;
  220. cuda_update_irq(s);
  221. }
  222. static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr)
  223. {
  224. CUDAState *s = opaque;
  225. uint32_t val;
  226. addr = (addr >> 9) & 0xf;
  227. switch(addr) {
  228. case 0:
  229. val = s->b;
  230. break;
  231. case 1:
  232. val = s->a;
  233. break;
  234. case 2:
  235. val = s->dirb;
  236. break;
  237. case 3:
  238. val = s->dira;
  239. break;
  240. case 4:
  241. val = get_counter(&s->timers[0]) & 0xff;
  242. s->ifr &= ~T1_INT;
  243. cuda_update_irq(s);
  244. break;
  245. case 5:
  246. val = get_counter(&s->timers[0]) >> 8;
  247. cuda_update_irq(s);
  248. break;
  249. case 6:
  250. val = s->timers[0].latch & 0xff;
  251. break;
  252. case 7:
  253. /* XXX: check this */
  254. val = (s->timers[0].latch >> 8) & 0xff;
  255. break;
  256. case 8:
  257. val = get_counter(&s->timers[1]) & 0xff;
  258. s->ifr &= ~T2_INT;
  259. break;
  260. case 9:
  261. val = get_counter(&s->timers[1]) >> 8;
  262. break;
  263. case 10:
  264. val = s->sr;
  265. s->ifr &= ~SR_INT;
  266. cuda_update_irq(s);
  267. break;
  268. case 11:
  269. val = s->acr;
  270. break;
  271. case 12:
  272. val = s->pcr;
  273. break;
  274. case 13:
  275. val = s->ifr;
  276. if (s->ifr & s->ier)
  277. val |= 0x80;
  278. break;
  279. case 14:
  280. val = s->ier | 0x80;
  281. break;
  282. default:
  283. case 15:
  284. val = s->anh;
  285. break;
  286. }
  287. if (addr != 13 || val != 0) {
  288. CUDA_DPRINTF("read: reg=0x%x val=%02x\n", (int)addr, val);
  289. }
  290. return val;
  291. }
  292. static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  293. {
  294. CUDAState *s = opaque;
  295. addr = (addr >> 9) & 0xf;
  296. CUDA_DPRINTF("write: reg=0x%x val=%02x\n", (int)addr, val);
  297. switch(addr) {
  298. case 0:
  299. s->b = val;
  300. cuda_update(s);
  301. break;
  302. case 1:
  303. s->a = val;
  304. break;
  305. case 2:
  306. s->dirb = val;
  307. break;
  308. case 3:
  309. s->dira = val;
  310. break;
  311. case 4:
  312. s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
  313. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  314. break;
  315. case 5:
  316. s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
  317. s->ifr &= ~T1_INT;
  318. set_counter(s, &s->timers[0], s->timers[0].latch);
  319. break;
  320. case 6:
  321. s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
  322. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  323. break;
  324. case 7:
  325. s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
  326. s->ifr &= ~T1_INT;
  327. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  328. break;
  329. case 8:
  330. s->timers[1].latch = val;
  331. set_counter(s, &s->timers[1], val);
  332. break;
  333. case 9:
  334. set_counter(s, &s->timers[1], (val << 8) | s->timers[1].latch);
  335. break;
  336. case 10:
  337. s->sr = val;
  338. break;
  339. case 11:
  340. s->acr = val;
  341. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  342. cuda_update(s);
  343. break;
  344. case 12:
  345. s->pcr = val;
  346. break;
  347. case 13:
  348. /* reset bits */
  349. s->ifr &= ~val;
  350. cuda_update_irq(s);
  351. break;
  352. case 14:
  353. if (val & IER_SET) {
  354. /* set bits */
  355. s->ier |= val & 0x7f;
  356. } else {
  357. /* reset bits */
  358. s->ier &= ~val;
  359. }
  360. cuda_update_irq(s);
  361. break;
  362. default:
  363. case 15:
  364. s->anh = val;
  365. break;
  366. }
  367. }
  368. /* NOTE: TIP and TREQ are negated */
  369. static void cuda_update(CUDAState *s)
  370. {
  371. int packet_received, len;
  372. packet_received = 0;
  373. if (!(s->b & TIP)) {
  374. /* transfer requested from host */
  375. if (s->acr & SR_OUT) {
  376. /* data output */
  377. if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
  378. if (s->data_out_index < sizeof(s->data_out)) {
  379. CUDA_DPRINTF("send: %02x\n", s->sr);
  380. s->data_out[s->data_out_index++] = s->sr;
  381. s->ifr |= SR_INT;
  382. cuda_update_irq(s);
  383. }
  384. }
  385. } else {
  386. if (s->data_in_index < s->data_in_size) {
  387. /* data input */
  388. if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
  389. s->sr = s->data_in[s->data_in_index++];
  390. CUDA_DPRINTF("recv: %02x\n", s->sr);
  391. /* indicate end of transfer */
  392. if (s->data_in_index >= s->data_in_size) {
  393. s->b = (s->b | TREQ);
  394. }
  395. s->ifr |= SR_INT;
  396. cuda_update_irq(s);
  397. }
  398. }
  399. }
  400. } else {
  401. /* no transfer requested: handle sync case */
  402. if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
  403. /* update TREQ state each time TACK change state */
  404. if (s->b & TACK)
  405. s->b = (s->b | TREQ);
  406. else
  407. s->b = (s->b & ~TREQ);
  408. s->ifr |= SR_INT;
  409. cuda_update_irq(s);
  410. } else {
  411. if (!(s->last_b & TIP)) {
  412. /* handle end of host to cuda transfer */
  413. packet_received = (s->data_out_index > 0);
  414. /* always an IRQ at the end of transfer */
  415. s->ifr |= SR_INT;
  416. cuda_update_irq(s);
  417. }
  418. /* signal if there is data to read */
  419. if (s->data_in_index < s->data_in_size) {
  420. s->b = (s->b & ~TREQ);
  421. }
  422. }
  423. }
  424. s->last_acr = s->acr;
  425. s->last_b = s->b;
  426. /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
  427. recursively */
  428. if (packet_received) {
  429. len = s->data_out_index;
  430. s->data_out_index = 0;
  431. cuda_receive_packet_from_host(s, s->data_out, len);
  432. }
  433. }
  434. static void cuda_send_packet_to_host(CUDAState *s,
  435. const uint8_t *data, int len)
  436. {
  437. #ifdef DEBUG_CUDA_PACKET
  438. {
  439. int i;
  440. printf("cuda_send_packet_to_host:\n");
  441. for(i = 0; i < len; i++)
  442. printf(" %02x", data[i]);
  443. printf("\n");
  444. }
  445. #endif
  446. memcpy(s->data_in, data, len);
  447. s->data_in_size = len;
  448. s->data_in_index = 0;
  449. cuda_update(s);
  450. s->ifr |= SR_INT;
  451. cuda_update_irq(s);
  452. }
  453. static void cuda_adb_poll(void *opaque)
  454. {
  455. CUDAState *s = opaque;
  456. uint8_t obuf[ADB_MAX_OUT_LEN + 2];
  457. int olen;
  458. olen = adb_poll(&adb_bus, obuf + 2);
  459. if (olen > 0) {
  460. obuf[0] = ADB_PACKET;
  461. obuf[1] = 0x40; /* polled data */
  462. cuda_send_packet_to_host(s, obuf, olen + 2);
  463. }
  464. qemu_mod_timer(s->adb_poll_timer,
  465. qemu_get_clock_ns(vm_clock) +
  466. (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ));
  467. }
  468. static void cuda_receive_packet(CUDAState *s,
  469. const uint8_t *data, int len)
  470. {
  471. uint8_t obuf[16];
  472. int autopoll;
  473. uint32_t ti;
  474. switch(data[0]) {
  475. case CUDA_AUTOPOLL:
  476. autopoll = (data[1] != 0);
  477. if (autopoll != s->autopoll) {
  478. s->autopoll = autopoll;
  479. if (autopoll) {
  480. qemu_mod_timer(s->adb_poll_timer,
  481. qemu_get_clock_ns(vm_clock) +
  482. (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ));
  483. } else {
  484. qemu_del_timer(s->adb_poll_timer);
  485. }
  486. }
  487. obuf[0] = CUDA_PACKET;
  488. obuf[1] = data[1];
  489. cuda_send_packet_to_host(s, obuf, 2);
  490. break;
  491. case CUDA_SET_TIME:
  492. ti = (((uint32_t)data[1]) << 24) + (((uint32_t)data[2]) << 16) + (((uint32_t)data[3]) << 8) + data[4];
  493. s->tick_offset = ti - (qemu_get_clock_ns(vm_clock) / get_ticks_per_sec());
  494. obuf[0] = CUDA_PACKET;
  495. obuf[1] = 0;
  496. obuf[2] = 0;
  497. cuda_send_packet_to_host(s, obuf, 3);
  498. break;
  499. case CUDA_GET_TIME:
  500. ti = s->tick_offset + (qemu_get_clock_ns(vm_clock) / get_ticks_per_sec());
  501. obuf[0] = CUDA_PACKET;
  502. obuf[1] = 0;
  503. obuf[2] = 0;
  504. obuf[3] = ti >> 24;
  505. obuf[4] = ti >> 16;
  506. obuf[5] = ti >> 8;
  507. obuf[6] = ti;
  508. cuda_send_packet_to_host(s, obuf, 7);
  509. break;
  510. case CUDA_FILE_SERVER_FLAG:
  511. case CUDA_SET_DEVICE_LIST:
  512. case CUDA_SET_AUTO_RATE:
  513. case CUDA_SET_POWER_MESSAGES:
  514. obuf[0] = CUDA_PACKET;
  515. obuf[1] = 0;
  516. cuda_send_packet_to_host(s, obuf, 2);
  517. break;
  518. case CUDA_POWERDOWN:
  519. obuf[0] = CUDA_PACKET;
  520. obuf[1] = 0;
  521. cuda_send_packet_to_host(s, obuf, 2);
  522. qemu_system_shutdown_request();
  523. break;
  524. case CUDA_RESET_SYSTEM:
  525. obuf[0] = CUDA_PACKET;
  526. obuf[1] = 0;
  527. cuda_send_packet_to_host(s, obuf, 2);
  528. qemu_system_reset_request();
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. static void cuda_receive_packet_from_host(CUDAState *s,
  535. const uint8_t *data, int len)
  536. {
  537. #ifdef DEBUG_CUDA_PACKET
  538. {
  539. int i;
  540. printf("cuda_receive_packet_from_host:\n");
  541. for(i = 0; i < len; i++)
  542. printf(" %02x", data[i]);
  543. printf("\n");
  544. }
  545. #endif
  546. switch(data[0]) {
  547. case ADB_PACKET:
  548. {
  549. uint8_t obuf[ADB_MAX_OUT_LEN + 2];
  550. int olen;
  551. olen = adb_request(&adb_bus, obuf + 2, data + 1, len - 1);
  552. if (olen > 0) {
  553. obuf[0] = ADB_PACKET;
  554. obuf[1] = 0x00;
  555. } else {
  556. /* error */
  557. obuf[0] = ADB_PACKET;
  558. obuf[1] = -olen;
  559. olen = 0;
  560. }
  561. cuda_send_packet_to_host(s, obuf, olen + 2);
  562. }
  563. break;
  564. case CUDA_PACKET:
  565. cuda_receive_packet(s, data + 1, len - 1);
  566. break;
  567. }
  568. }
  569. static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
  570. {
  571. }
  572. static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  573. {
  574. }
  575. static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr)
  576. {
  577. return 0;
  578. }
  579. static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)
  580. {
  581. return 0;
  582. }
  583. static MemoryRegionOps cuda_ops = {
  584. .old_mmio = {
  585. .write = {
  586. cuda_writeb,
  587. cuda_writew,
  588. cuda_writel,
  589. },
  590. .read = {
  591. cuda_readb,
  592. cuda_readw,
  593. cuda_readl,
  594. },
  595. },
  596. .endianness = DEVICE_NATIVE_ENDIAN,
  597. };
  598. static bool cuda_timer_exist(void *opaque, int version_id)
  599. {
  600. CUDATimer *s = opaque;
  601. return s->timer != NULL;
  602. }
  603. static const VMStateDescription vmstate_cuda_timer = {
  604. .name = "cuda_timer",
  605. .version_id = 0,
  606. .minimum_version_id = 0,
  607. .minimum_version_id_old = 0,
  608. .fields = (VMStateField[]) {
  609. VMSTATE_UINT16(latch, CUDATimer),
  610. VMSTATE_UINT16(counter_value, CUDATimer),
  611. VMSTATE_INT64(load_time, CUDATimer),
  612. VMSTATE_INT64(next_irq_time, CUDATimer),
  613. VMSTATE_TIMER_TEST(timer, CUDATimer, cuda_timer_exist),
  614. VMSTATE_END_OF_LIST()
  615. }
  616. };
  617. static const VMStateDescription vmstate_cuda = {
  618. .name = "cuda",
  619. .version_id = 1,
  620. .minimum_version_id = 1,
  621. .minimum_version_id_old = 1,
  622. .fields = (VMStateField[]) {
  623. VMSTATE_UINT8(a, CUDAState),
  624. VMSTATE_UINT8(b, CUDAState),
  625. VMSTATE_UINT8(dira, CUDAState),
  626. VMSTATE_UINT8(dirb, CUDAState),
  627. VMSTATE_UINT8(sr, CUDAState),
  628. VMSTATE_UINT8(acr, CUDAState),
  629. VMSTATE_UINT8(pcr, CUDAState),
  630. VMSTATE_UINT8(ifr, CUDAState),
  631. VMSTATE_UINT8(ier, CUDAState),
  632. VMSTATE_UINT8(anh, CUDAState),
  633. VMSTATE_INT32(data_in_size, CUDAState),
  634. VMSTATE_INT32(data_in_index, CUDAState),
  635. VMSTATE_INT32(data_out_index, CUDAState),
  636. VMSTATE_UINT8(autopoll, CUDAState),
  637. VMSTATE_BUFFER(data_in, CUDAState),
  638. VMSTATE_BUFFER(data_out, CUDAState),
  639. VMSTATE_UINT32(tick_offset, CUDAState),
  640. VMSTATE_STRUCT_ARRAY(timers, CUDAState, 2, 1,
  641. vmstate_cuda_timer, CUDATimer),
  642. VMSTATE_END_OF_LIST()
  643. }
  644. };
  645. static void cuda_reset(void *opaque)
  646. {
  647. CUDAState *s = opaque;
  648. s->b = 0;
  649. s->a = 0;
  650. s->dirb = 0;
  651. s->dira = 0;
  652. s->sr = 0;
  653. s->acr = 0;
  654. s->pcr = 0;
  655. s->ifr = 0;
  656. s->ier = 0;
  657. // s->ier = T1_INT | SR_INT;
  658. s->anh = 0;
  659. s->data_in_size = 0;
  660. s->data_in_index = 0;
  661. s->data_out_index = 0;
  662. s->autopoll = 0;
  663. s->timers[0].latch = 0xffff;
  664. set_counter(s, &s->timers[0], 0xffff);
  665. s->timers[1].latch = 0;
  666. set_counter(s, &s->timers[1], 0xffff);
  667. }
  668. void cuda_init (MemoryRegion **cuda_mem, qemu_irq irq)
  669. {
  670. struct tm tm;
  671. CUDAState *s = &cuda_state;
  672. s->irq = irq;
  673. s->timers[0].index = 0;
  674. s->timers[0].timer = qemu_new_timer_ns(vm_clock, cuda_timer1, s);
  675. s->timers[1].index = 1;
  676. qemu_get_timedate(&tm, 0);
  677. s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
  678. s->adb_poll_timer = qemu_new_timer_ns(vm_clock, cuda_adb_poll, s);
  679. memory_region_init_io(&s->mem, &cuda_ops, s, "cuda", 0x2000);
  680. *cuda_mem = &s->mem;
  681. vmstate_register(NULL, -1, &vmstate_cuda, s);
  682. qemu_register_reset(cuda_reset, s);
  683. }