ppc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /*
  2. * QEMU generic PowerPC hardware System Emulator
  3. *
  4. * Copyright (c) 2003-2007 Jocelyn Mayer
  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. #include "hw.h"
  25. #include "ppc.h"
  26. #include "qemu-timer.h"
  27. #include "sysemu.h"
  28. #include "nvram.h"
  29. #include "qemu-log.h"
  30. #include "loader.h"
  31. #include "kvm.h"
  32. #include "kvm_ppc.h"
  33. //#define PPC_DEBUG_IRQ
  34. //#define PPC_DEBUG_TB
  35. #ifdef PPC_DEBUG_IRQ
  36. # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
  37. #else
  38. # define LOG_IRQ(...) do { } while (0)
  39. #endif
  40. #ifdef PPC_DEBUG_TB
  41. # define LOG_TB(...) qemu_log(__VA_ARGS__)
  42. #else
  43. # define LOG_TB(...) do { } while (0)
  44. #endif
  45. static void cpu_ppc_tb_stop (CPUState *env);
  46. static void cpu_ppc_tb_start (CPUState *env);
  47. void ppc_set_irq(CPUState *env, int n_IRQ, int level)
  48. {
  49. unsigned int old_pending = env->pending_interrupts;
  50. if (level) {
  51. env->pending_interrupts |= 1 << n_IRQ;
  52. cpu_interrupt(env, CPU_INTERRUPT_HARD);
  53. } else {
  54. env->pending_interrupts &= ~(1 << n_IRQ);
  55. if (env->pending_interrupts == 0)
  56. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  57. }
  58. if (old_pending != env->pending_interrupts) {
  59. #ifdef CONFIG_KVM
  60. kvmppc_set_interrupt(env, n_IRQ, level);
  61. #endif
  62. }
  63. LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
  64. "req %08x\n", __func__, env, n_IRQ, level,
  65. env->pending_interrupts, env->interrupt_request);
  66. }
  67. /* PowerPC 6xx / 7xx internal IRQ controller */
  68. static void ppc6xx_set_irq (void *opaque, int pin, int level)
  69. {
  70. CPUState *env = opaque;
  71. int cur_level;
  72. LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
  73. env, pin, level);
  74. cur_level = (env->irq_input_state >> pin) & 1;
  75. /* Don't generate spurious events */
  76. if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
  77. switch (pin) {
  78. case PPC6xx_INPUT_TBEN:
  79. /* Level sensitive - active high */
  80. LOG_IRQ("%s: %s the time base\n",
  81. __func__, level ? "start" : "stop");
  82. if (level) {
  83. cpu_ppc_tb_start(env);
  84. } else {
  85. cpu_ppc_tb_stop(env);
  86. }
  87. case PPC6xx_INPUT_INT:
  88. /* Level sensitive - active high */
  89. LOG_IRQ("%s: set the external IRQ state to %d\n",
  90. __func__, level);
  91. ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
  92. break;
  93. case PPC6xx_INPUT_SMI:
  94. /* Level sensitive - active high */
  95. LOG_IRQ("%s: set the SMI IRQ state to %d\n",
  96. __func__, level);
  97. ppc_set_irq(env, PPC_INTERRUPT_SMI, level);
  98. break;
  99. case PPC6xx_INPUT_MCP:
  100. /* Negative edge sensitive */
  101. /* XXX: TODO: actual reaction may depends on HID0 status
  102. * 603/604/740/750: check HID0[EMCP]
  103. */
  104. if (cur_level == 1 && level == 0) {
  105. LOG_IRQ("%s: raise machine check state\n",
  106. __func__);
  107. ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
  108. }
  109. break;
  110. case PPC6xx_INPUT_CKSTP_IN:
  111. /* Level sensitive - active low */
  112. /* XXX: TODO: relay the signal to CKSTP_OUT pin */
  113. /* XXX: Note that the only way to restart the CPU is to reset it */
  114. if (level) {
  115. LOG_IRQ("%s: stop the CPU\n", __func__);
  116. env->halted = 1;
  117. }
  118. break;
  119. case PPC6xx_INPUT_HRESET:
  120. /* Level sensitive - active low */
  121. if (level) {
  122. LOG_IRQ("%s: reset the CPU\n", __func__);
  123. env->interrupt_request |= CPU_INTERRUPT_EXITTB;
  124. /* XXX: TOFIX */
  125. #if 0
  126. cpu_reset(env);
  127. #else
  128. qemu_system_reset_request();
  129. #endif
  130. }
  131. break;
  132. case PPC6xx_INPUT_SRESET:
  133. LOG_IRQ("%s: set the RESET IRQ state to %d\n",
  134. __func__, level);
  135. ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
  136. break;
  137. default:
  138. /* Unknown pin - do nothing */
  139. LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
  140. return;
  141. }
  142. if (level)
  143. env->irq_input_state |= 1 << pin;
  144. else
  145. env->irq_input_state &= ~(1 << pin);
  146. }
  147. }
  148. void ppc6xx_irq_init (CPUState *env)
  149. {
  150. env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, env,
  151. PPC6xx_INPUT_NB);
  152. }
  153. #if defined(TARGET_PPC64)
  154. /* PowerPC 970 internal IRQ controller */
  155. static void ppc970_set_irq (void *opaque, int pin, int level)
  156. {
  157. CPUState *env = opaque;
  158. int cur_level;
  159. LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
  160. env, pin, level);
  161. cur_level = (env->irq_input_state >> pin) & 1;
  162. /* Don't generate spurious events */
  163. if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
  164. switch (pin) {
  165. case PPC970_INPUT_INT:
  166. /* Level sensitive - active high */
  167. LOG_IRQ("%s: set the external IRQ state to %d\n",
  168. __func__, level);
  169. ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
  170. break;
  171. case PPC970_INPUT_THINT:
  172. /* Level sensitive - active high */
  173. LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__,
  174. level);
  175. ppc_set_irq(env, PPC_INTERRUPT_THERM, level);
  176. break;
  177. case PPC970_INPUT_MCP:
  178. /* Negative edge sensitive */
  179. /* XXX: TODO: actual reaction may depends on HID0 status
  180. * 603/604/740/750: check HID0[EMCP]
  181. */
  182. if (cur_level == 1 && level == 0) {
  183. LOG_IRQ("%s: raise machine check state\n",
  184. __func__);
  185. ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
  186. }
  187. break;
  188. case PPC970_INPUT_CKSTP:
  189. /* Level sensitive - active low */
  190. /* XXX: TODO: relay the signal to CKSTP_OUT pin */
  191. if (level) {
  192. LOG_IRQ("%s: stop the CPU\n", __func__);
  193. env->halted = 1;
  194. } else {
  195. LOG_IRQ("%s: restart the CPU\n", __func__);
  196. env->halted = 0;
  197. qemu_cpu_kick(env);
  198. }
  199. break;
  200. case PPC970_INPUT_HRESET:
  201. /* Level sensitive - active low */
  202. if (level) {
  203. #if 0 // XXX: TOFIX
  204. LOG_IRQ("%s: reset the CPU\n", __func__);
  205. cpu_reset(env);
  206. #endif
  207. }
  208. break;
  209. case PPC970_INPUT_SRESET:
  210. LOG_IRQ("%s: set the RESET IRQ state to %d\n",
  211. __func__, level);
  212. ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
  213. break;
  214. case PPC970_INPUT_TBEN:
  215. LOG_IRQ("%s: set the TBEN state to %d\n", __func__,
  216. level);
  217. /* XXX: TODO */
  218. break;
  219. default:
  220. /* Unknown pin - do nothing */
  221. LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
  222. return;
  223. }
  224. if (level)
  225. env->irq_input_state |= 1 << pin;
  226. else
  227. env->irq_input_state &= ~(1 << pin);
  228. }
  229. }
  230. void ppc970_irq_init (CPUState *env)
  231. {
  232. env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, env,
  233. PPC970_INPUT_NB);
  234. }
  235. /* POWER7 internal IRQ controller */
  236. static void power7_set_irq (void *opaque, int pin, int level)
  237. {
  238. CPUState *env = opaque;
  239. LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
  240. env, pin, level);
  241. switch (pin) {
  242. case POWER7_INPUT_INT:
  243. /* Level sensitive - active high */
  244. LOG_IRQ("%s: set the external IRQ state to %d\n",
  245. __func__, level);
  246. ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
  247. break;
  248. default:
  249. /* Unknown pin - do nothing */
  250. LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
  251. return;
  252. }
  253. if (level) {
  254. env->irq_input_state |= 1 << pin;
  255. } else {
  256. env->irq_input_state &= ~(1 << pin);
  257. }
  258. }
  259. void ppcPOWER7_irq_init (CPUState *env)
  260. {
  261. env->irq_inputs = (void **)qemu_allocate_irqs(&power7_set_irq, env,
  262. POWER7_INPUT_NB);
  263. }
  264. #endif /* defined(TARGET_PPC64) */
  265. /* PowerPC 40x internal IRQ controller */
  266. static void ppc40x_set_irq (void *opaque, int pin, int level)
  267. {
  268. CPUState *env = opaque;
  269. int cur_level;
  270. LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
  271. env, pin, level);
  272. cur_level = (env->irq_input_state >> pin) & 1;
  273. /* Don't generate spurious events */
  274. if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
  275. switch (pin) {
  276. case PPC40x_INPUT_RESET_SYS:
  277. if (level) {
  278. LOG_IRQ("%s: reset the PowerPC system\n",
  279. __func__);
  280. ppc40x_system_reset(env);
  281. }
  282. break;
  283. case PPC40x_INPUT_RESET_CHIP:
  284. if (level) {
  285. LOG_IRQ("%s: reset the PowerPC chip\n", __func__);
  286. ppc40x_chip_reset(env);
  287. }
  288. break;
  289. case PPC40x_INPUT_RESET_CORE:
  290. /* XXX: TODO: update DBSR[MRR] */
  291. if (level) {
  292. LOG_IRQ("%s: reset the PowerPC core\n", __func__);
  293. ppc40x_core_reset(env);
  294. }
  295. break;
  296. case PPC40x_INPUT_CINT:
  297. /* Level sensitive - active high */
  298. LOG_IRQ("%s: set the critical IRQ state to %d\n",
  299. __func__, level);
  300. ppc_set_irq(env, PPC_INTERRUPT_CEXT, level);
  301. break;
  302. case PPC40x_INPUT_INT:
  303. /* Level sensitive - active high */
  304. LOG_IRQ("%s: set the external IRQ state to %d\n",
  305. __func__, level);
  306. ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
  307. break;
  308. case PPC40x_INPUT_HALT:
  309. /* Level sensitive - active low */
  310. if (level) {
  311. LOG_IRQ("%s: stop the CPU\n", __func__);
  312. env->halted = 1;
  313. } else {
  314. LOG_IRQ("%s: restart the CPU\n", __func__);
  315. env->halted = 0;
  316. qemu_cpu_kick(env);
  317. }
  318. break;
  319. case PPC40x_INPUT_DEBUG:
  320. /* Level sensitive - active high */
  321. LOG_IRQ("%s: set the debug pin state to %d\n",
  322. __func__, level);
  323. ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level);
  324. break;
  325. default:
  326. /* Unknown pin - do nothing */
  327. LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
  328. return;
  329. }
  330. if (level)
  331. env->irq_input_state |= 1 << pin;
  332. else
  333. env->irq_input_state &= ~(1 << pin);
  334. }
  335. }
  336. void ppc40x_irq_init (CPUState *env)
  337. {
  338. env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
  339. env, PPC40x_INPUT_NB);
  340. }
  341. /* PowerPC E500 internal IRQ controller */
  342. static void ppce500_set_irq (void *opaque, int pin, int level)
  343. {
  344. CPUState *env = opaque;
  345. int cur_level;
  346. LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
  347. env, pin, level);
  348. cur_level = (env->irq_input_state >> pin) & 1;
  349. /* Don't generate spurious events */
  350. if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
  351. switch (pin) {
  352. case PPCE500_INPUT_MCK:
  353. if (level) {
  354. LOG_IRQ("%s: reset the PowerPC system\n",
  355. __func__);
  356. qemu_system_reset_request();
  357. }
  358. break;
  359. case PPCE500_INPUT_RESET_CORE:
  360. if (level) {
  361. LOG_IRQ("%s: reset the PowerPC core\n", __func__);
  362. ppc_set_irq(env, PPC_INTERRUPT_MCK, level);
  363. }
  364. break;
  365. case PPCE500_INPUT_CINT:
  366. /* Level sensitive - active high */
  367. LOG_IRQ("%s: set the critical IRQ state to %d\n",
  368. __func__, level);
  369. ppc_set_irq(env, PPC_INTERRUPT_CEXT, level);
  370. break;
  371. case PPCE500_INPUT_INT:
  372. /* Level sensitive - active high */
  373. LOG_IRQ("%s: set the core IRQ state to %d\n",
  374. __func__, level);
  375. ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
  376. break;
  377. case PPCE500_INPUT_DEBUG:
  378. /* Level sensitive - active high */
  379. LOG_IRQ("%s: set the debug pin state to %d\n",
  380. __func__, level);
  381. ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level);
  382. break;
  383. default:
  384. /* Unknown pin - do nothing */
  385. LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
  386. return;
  387. }
  388. if (level)
  389. env->irq_input_state |= 1 << pin;
  390. else
  391. env->irq_input_state &= ~(1 << pin);
  392. }
  393. }
  394. void ppce500_irq_init (CPUState *env)
  395. {
  396. env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq,
  397. env, PPCE500_INPUT_NB);
  398. }
  399. /*****************************************************************************/
  400. /* PowerPC time base and decrementer emulation */
  401. uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset)
  402. {
  403. /* TB time in tb periods */
  404. return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset;
  405. }
  406. uint64_t cpu_ppc_load_tbl (CPUState *env)
  407. {
  408. ppc_tb_t *tb_env = env->tb_env;
  409. uint64_t tb;
  410. if (kvm_enabled()) {
  411. return env->spr[SPR_TBL];
  412. }
  413. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
  414. LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
  415. return tb;
  416. }
  417. static inline uint32_t _cpu_ppc_load_tbu(CPUState *env)
  418. {
  419. ppc_tb_t *tb_env = env->tb_env;
  420. uint64_t tb;
  421. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
  422. LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
  423. return tb >> 32;
  424. }
  425. uint32_t cpu_ppc_load_tbu (CPUState *env)
  426. {
  427. if (kvm_enabled()) {
  428. return env->spr[SPR_TBU];
  429. }
  430. return _cpu_ppc_load_tbu(env);
  431. }
  432. static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk,
  433. int64_t *tb_offsetp, uint64_t value)
  434. {
  435. *tb_offsetp = value - muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec());
  436. LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n",
  437. __func__, value, *tb_offsetp);
  438. }
  439. void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
  440. {
  441. ppc_tb_t *tb_env = env->tb_env;
  442. uint64_t tb;
  443. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
  444. tb &= 0xFFFFFFFF00000000ULL;
  445. cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
  446. &tb_env->tb_offset, tb | (uint64_t)value);
  447. }
  448. static inline void _cpu_ppc_store_tbu(CPUState *env, uint32_t value)
  449. {
  450. ppc_tb_t *tb_env = env->tb_env;
  451. uint64_t tb;
  452. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
  453. tb &= 0x00000000FFFFFFFFULL;
  454. cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
  455. &tb_env->tb_offset, ((uint64_t)value << 32) | tb);
  456. }
  457. void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
  458. {
  459. _cpu_ppc_store_tbu(env, value);
  460. }
  461. uint64_t cpu_ppc_load_atbl (CPUState *env)
  462. {
  463. ppc_tb_t *tb_env = env->tb_env;
  464. uint64_t tb;
  465. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
  466. LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
  467. return tb;
  468. }
  469. uint32_t cpu_ppc_load_atbu (CPUState *env)
  470. {
  471. ppc_tb_t *tb_env = env->tb_env;
  472. uint64_t tb;
  473. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
  474. LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
  475. return tb >> 32;
  476. }
  477. void cpu_ppc_store_atbl (CPUState *env, uint32_t value)
  478. {
  479. ppc_tb_t *tb_env = env->tb_env;
  480. uint64_t tb;
  481. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
  482. tb &= 0xFFFFFFFF00000000ULL;
  483. cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
  484. &tb_env->atb_offset, tb | (uint64_t)value);
  485. }
  486. void cpu_ppc_store_atbu (CPUState *env, uint32_t value)
  487. {
  488. ppc_tb_t *tb_env = env->tb_env;
  489. uint64_t tb;
  490. tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
  491. tb &= 0x00000000FFFFFFFFULL;
  492. cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
  493. &tb_env->atb_offset, ((uint64_t)value << 32) | tb);
  494. }
  495. static void cpu_ppc_tb_stop (CPUState *env)
  496. {
  497. ppc_tb_t *tb_env = env->tb_env;
  498. uint64_t tb, atb, vmclk;
  499. /* If the time base is already frozen, do nothing */
  500. if (tb_env->tb_freq != 0) {
  501. vmclk = qemu_get_clock_ns(vm_clock);
  502. /* Get the time base */
  503. tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset);
  504. /* Get the alternate time base */
  505. atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset);
  506. /* Store the time base value (ie compute the current offset) */
  507. cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
  508. /* Store the alternate time base value (compute the current offset) */
  509. cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
  510. /* Set the time base frequency to zero */
  511. tb_env->tb_freq = 0;
  512. /* Now, the time bases are frozen to tb_offset / atb_offset value */
  513. }
  514. }
  515. static void cpu_ppc_tb_start (CPUState *env)
  516. {
  517. ppc_tb_t *tb_env = env->tb_env;
  518. uint64_t tb, atb, vmclk;
  519. /* If the time base is not frozen, do nothing */
  520. if (tb_env->tb_freq == 0) {
  521. vmclk = qemu_get_clock_ns(vm_clock);
  522. /* Get the time base from tb_offset */
  523. tb = tb_env->tb_offset;
  524. /* Get the alternate time base from atb_offset */
  525. atb = tb_env->atb_offset;
  526. /* Restore the tb frequency from the decrementer frequency */
  527. tb_env->tb_freq = tb_env->decr_freq;
  528. /* Store the time base value */
  529. cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
  530. /* Store the alternate time base value */
  531. cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
  532. }
  533. }
  534. static inline uint32_t _cpu_ppc_load_decr(CPUState *env, uint64_t next)
  535. {
  536. ppc_tb_t *tb_env = env->tb_env;
  537. uint32_t decr;
  538. int64_t diff;
  539. diff = next - qemu_get_clock_ns(vm_clock);
  540. if (diff >= 0) {
  541. decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec());
  542. } else if (tb_env->flags & PPC_TIMER_BOOKE) {
  543. decr = 0;
  544. } else {
  545. decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec());
  546. }
  547. LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
  548. return decr;
  549. }
  550. uint32_t cpu_ppc_load_decr (CPUState *env)
  551. {
  552. ppc_tb_t *tb_env = env->tb_env;
  553. if (kvm_enabled()) {
  554. return env->spr[SPR_DECR];
  555. }
  556. return _cpu_ppc_load_decr(env, tb_env->decr_next);
  557. }
  558. uint32_t cpu_ppc_load_hdecr (CPUState *env)
  559. {
  560. ppc_tb_t *tb_env = env->tb_env;
  561. return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
  562. }
  563. uint64_t cpu_ppc_load_purr (CPUState *env)
  564. {
  565. ppc_tb_t *tb_env = env->tb_env;
  566. uint64_t diff;
  567. diff = qemu_get_clock_ns(vm_clock) - tb_env->purr_start;
  568. return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, get_ticks_per_sec());
  569. }
  570. /* When decrementer expires,
  571. * all we need to do is generate or queue a CPU exception
  572. */
  573. static inline void cpu_ppc_decr_excp(CPUState *env)
  574. {
  575. /* Raise it */
  576. LOG_TB("raise decrementer exception\n");
  577. ppc_set_irq(env, PPC_INTERRUPT_DECR, 1);
  578. }
  579. static inline void cpu_ppc_hdecr_excp(CPUState *env)
  580. {
  581. /* Raise it */
  582. LOG_TB("raise decrementer exception\n");
  583. ppc_set_irq(env, PPC_INTERRUPT_HDECR, 1);
  584. }
  585. static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,
  586. struct QEMUTimer *timer,
  587. void (*raise_excp)(CPUState *),
  588. uint32_t decr, uint32_t value,
  589. int is_excp)
  590. {
  591. ppc_tb_t *tb_env = env->tb_env;
  592. uint64_t now, next;
  593. LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
  594. decr, value);
  595. if (kvm_enabled()) {
  596. /* KVM handles decrementer exceptions, we don't need our own timer */
  597. return;
  598. }
  599. now = qemu_get_clock_ns(vm_clock);
  600. next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
  601. if (is_excp) {
  602. next += *nextp - now;
  603. }
  604. if (next == now) {
  605. next++;
  606. }
  607. *nextp = next;
  608. /* Adjust timer */
  609. qemu_mod_timer(timer, next);
  610. /* If we set a negative value and the decrementer was positive, raise an
  611. * exception.
  612. */
  613. if ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED)
  614. && (value & 0x80000000)
  615. && !(decr & 0x80000000)) {
  616. (*raise_excp)(env);
  617. }
  618. }
  619. static inline void _cpu_ppc_store_decr(CPUState *env, uint32_t decr,
  620. uint32_t value, int is_excp)
  621. {
  622. ppc_tb_t *tb_env = env->tb_env;
  623. __cpu_ppc_store_decr(env, &tb_env->decr_next, tb_env->decr_timer,
  624. &cpu_ppc_decr_excp, decr, value, is_excp);
  625. }
  626. void cpu_ppc_store_decr (CPUState *env, uint32_t value)
  627. {
  628. _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0);
  629. }
  630. static void cpu_ppc_decr_cb (void *opaque)
  631. {
  632. _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1);
  633. }
  634. static inline void _cpu_ppc_store_hdecr(CPUState *env, uint32_t hdecr,
  635. uint32_t value, int is_excp)
  636. {
  637. ppc_tb_t *tb_env = env->tb_env;
  638. if (tb_env->hdecr_timer != NULL) {
  639. __cpu_ppc_store_decr(env, &tb_env->hdecr_next, tb_env->hdecr_timer,
  640. &cpu_ppc_hdecr_excp, hdecr, value, is_excp);
  641. }
  642. }
  643. void cpu_ppc_store_hdecr (CPUState *env, uint32_t value)
  644. {
  645. _cpu_ppc_store_hdecr(env, cpu_ppc_load_hdecr(env), value, 0);
  646. }
  647. static void cpu_ppc_hdecr_cb (void *opaque)
  648. {
  649. _cpu_ppc_store_hdecr(opaque, 0x00000000, 0xFFFFFFFF, 1);
  650. }
  651. void cpu_ppc_store_purr (CPUState *env, uint64_t value)
  652. {
  653. ppc_tb_t *tb_env = env->tb_env;
  654. tb_env->purr_load = value;
  655. tb_env->purr_start = qemu_get_clock_ns(vm_clock);
  656. }
  657. static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
  658. {
  659. CPUState *env = opaque;
  660. ppc_tb_t *tb_env = env->tb_env;
  661. tb_env->tb_freq = freq;
  662. tb_env->decr_freq = freq;
  663. /* There is a bug in Linux 2.4 kernels:
  664. * if a decrementer exception is pending when it enables msr_ee at startup,
  665. * it's not ready to handle it...
  666. */
  667. _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
  668. _cpu_ppc_store_hdecr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
  669. cpu_ppc_store_purr(env, 0x0000000000000000ULL);
  670. }
  671. /* Set up (once) timebase frequency (in Hz) */
  672. clk_setup_cb cpu_ppc_tb_init (CPUState *env, uint32_t freq)
  673. {
  674. ppc_tb_t *tb_env;
  675. tb_env = g_malloc0(sizeof(ppc_tb_t));
  676. env->tb_env = tb_env;
  677. tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
  678. /* Create new timer */
  679. tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_decr_cb, env);
  680. if (0) {
  681. /* XXX: find a suitable condition to enable the hypervisor decrementer
  682. */
  683. tb_env->hdecr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_hdecr_cb, env);
  684. } else {
  685. tb_env->hdecr_timer = NULL;
  686. }
  687. cpu_ppc_set_tb_clk(env, freq);
  688. return &cpu_ppc_set_tb_clk;
  689. }
  690. /* Specific helpers for POWER & PowerPC 601 RTC */
  691. #if 0
  692. static clk_setup_cb cpu_ppc601_rtc_init (CPUState *env)
  693. {
  694. return cpu_ppc_tb_init(env, 7812500);
  695. }
  696. #endif
  697. void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value)
  698. {
  699. _cpu_ppc_store_tbu(env, value);
  700. }
  701. uint32_t cpu_ppc601_load_rtcu (CPUState *env)
  702. {
  703. return _cpu_ppc_load_tbu(env);
  704. }
  705. void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value)
  706. {
  707. cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
  708. }
  709. uint32_t cpu_ppc601_load_rtcl (CPUState *env)
  710. {
  711. return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
  712. }
  713. /*****************************************************************************/
  714. /* PowerPC 40x timers */
  715. /* PIT, FIT & WDT */
  716. typedef struct ppc40x_timer_t ppc40x_timer_t;
  717. struct ppc40x_timer_t {
  718. uint64_t pit_reload; /* PIT auto-reload value */
  719. uint64_t fit_next; /* Tick for next FIT interrupt */
  720. struct QEMUTimer *fit_timer;
  721. uint64_t wdt_next; /* Tick for next WDT interrupt */
  722. struct QEMUTimer *wdt_timer;
  723. /* 405 have the PIT, 440 have a DECR. */
  724. unsigned int decr_excp;
  725. };
  726. /* Fixed interval timer */
  727. static void cpu_4xx_fit_cb (void *opaque)
  728. {
  729. CPUState *env;
  730. ppc_tb_t *tb_env;
  731. ppc40x_timer_t *ppc40x_timer;
  732. uint64_t now, next;
  733. env = opaque;
  734. tb_env = env->tb_env;
  735. ppc40x_timer = tb_env->opaque;
  736. now = qemu_get_clock_ns(vm_clock);
  737. switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
  738. case 0:
  739. next = 1 << 9;
  740. break;
  741. case 1:
  742. next = 1 << 13;
  743. break;
  744. case 2:
  745. next = 1 << 17;
  746. break;
  747. case 3:
  748. next = 1 << 21;
  749. break;
  750. default:
  751. /* Cannot occur, but makes gcc happy */
  752. return;
  753. }
  754. next = now + muldiv64(next, get_ticks_per_sec(), tb_env->tb_freq);
  755. if (next == now)
  756. next++;
  757. qemu_mod_timer(ppc40x_timer->fit_timer, next);
  758. env->spr[SPR_40x_TSR] |= 1 << 26;
  759. if ((env->spr[SPR_40x_TCR] >> 23) & 0x1)
  760. ppc_set_irq(env, PPC_INTERRUPT_FIT, 1);
  761. LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
  762. (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
  763. env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
  764. }
  765. /* Programmable interval timer */
  766. static void start_stop_pit (CPUState *env, ppc_tb_t *tb_env, int is_excp)
  767. {
  768. ppc40x_timer_t *ppc40x_timer;
  769. uint64_t now, next;
  770. ppc40x_timer = tb_env->opaque;
  771. if (ppc40x_timer->pit_reload <= 1 ||
  772. !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
  773. (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
  774. /* Stop PIT */
  775. LOG_TB("%s: stop PIT\n", __func__);
  776. qemu_del_timer(tb_env->decr_timer);
  777. } else {
  778. LOG_TB("%s: start PIT %016" PRIx64 "\n",
  779. __func__, ppc40x_timer->pit_reload);
  780. now = qemu_get_clock_ns(vm_clock);
  781. next = now + muldiv64(ppc40x_timer->pit_reload,
  782. get_ticks_per_sec(), tb_env->decr_freq);
  783. if (is_excp)
  784. next += tb_env->decr_next - now;
  785. if (next == now)
  786. next++;
  787. qemu_mod_timer(tb_env->decr_timer, next);
  788. tb_env->decr_next = next;
  789. }
  790. }
  791. static void cpu_4xx_pit_cb (void *opaque)
  792. {
  793. CPUState *env;
  794. ppc_tb_t *tb_env;
  795. ppc40x_timer_t *ppc40x_timer;
  796. env = opaque;
  797. tb_env = env->tb_env;
  798. ppc40x_timer = tb_env->opaque;
  799. env->spr[SPR_40x_TSR] |= 1 << 27;
  800. if ((env->spr[SPR_40x_TCR] >> 26) & 0x1)
  801. ppc_set_irq(env, ppc40x_timer->decr_excp, 1);
  802. start_stop_pit(env, tb_env, 1);
  803. LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " "
  804. "%016" PRIx64 "\n", __func__,
  805. (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
  806. (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
  807. env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
  808. ppc40x_timer->pit_reload);
  809. }
  810. /* Watchdog timer */
  811. static void cpu_4xx_wdt_cb (void *opaque)
  812. {
  813. CPUState *env;
  814. ppc_tb_t *tb_env;
  815. ppc40x_timer_t *ppc40x_timer;
  816. uint64_t now, next;
  817. env = opaque;
  818. tb_env = env->tb_env;
  819. ppc40x_timer = tb_env->opaque;
  820. now = qemu_get_clock_ns(vm_clock);
  821. switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
  822. case 0:
  823. next = 1 << 17;
  824. break;
  825. case 1:
  826. next = 1 << 21;
  827. break;
  828. case 2:
  829. next = 1 << 25;
  830. break;
  831. case 3:
  832. next = 1 << 29;
  833. break;
  834. default:
  835. /* Cannot occur, but makes gcc happy */
  836. return;
  837. }
  838. next = now + muldiv64(next, get_ticks_per_sec(), tb_env->decr_freq);
  839. if (next == now)
  840. next++;
  841. LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
  842. env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
  843. switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
  844. case 0x0:
  845. case 0x1:
  846. qemu_mod_timer(ppc40x_timer->wdt_timer, next);
  847. ppc40x_timer->wdt_next = next;
  848. env->spr[SPR_40x_TSR] |= 1 << 31;
  849. break;
  850. case 0x2:
  851. qemu_mod_timer(ppc40x_timer->wdt_timer, next);
  852. ppc40x_timer->wdt_next = next;
  853. env->spr[SPR_40x_TSR] |= 1 << 30;
  854. if ((env->spr[SPR_40x_TCR] >> 27) & 0x1)
  855. ppc_set_irq(env, PPC_INTERRUPT_WDT, 1);
  856. break;
  857. case 0x3:
  858. env->spr[SPR_40x_TSR] &= ~0x30000000;
  859. env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
  860. switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
  861. case 0x0:
  862. /* No reset */
  863. break;
  864. case 0x1: /* Core reset */
  865. ppc40x_core_reset(env);
  866. break;
  867. case 0x2: /* Chip reset */
  868. ppc40x_chip_reset(env);
  869. break;
  870. case 0x3: /* System reset */
  871. ppc40x_system_reset(env);
  872. break;
  873. }
  874. }
  875. }
  876. void store_40x_pit (CPUState *env, target_ulong val)
  877. {
  878. ppc_tb_t *tb_env;
  879. ppc40x_timer_t *ppc40x_timer;
  880. tb_env = env->tb_env;
  881. ppc40x_timer = tb_env->opaque;
  882. LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val);
  883. ppc40x_timer->pit_reload = val;
  884. start_stop_pit(env, tb_env, 0);
  885. }
  886. target_ulong load_40x_pit (CPUState *env)
  887. {
  888. return cpu_ppc_load_decr(env);
  889. }
  890. static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq)
  891. {
  892. CPUState *env = opaque;
  893. ppc_tb_t *tb_env = env->tb_env;
  894. LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__,
  895. freq);
  896. tb_env->tb_freq = freq;
  897. tb_env->decr_freq = freq;
  898. /* XXX: we should also update all timers */
  899. }
  900. clk_setup_cb ppc_40x_timers_init (CPUState *env, uint32_t freq,
  901. unsigned int decr_excp)
  902. {
  903. ppc_tb_t *tb_env;
  904. ppc40x_timer_t *ppc40x_timer;
  905. tb_env = g_malloc0(sizeof(ppc_tb_t));
  906. env->tb_env = tb_env;
  907. tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
  908. ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t));
  909. tb_env->tb_freq = freq;
  910. tb_env->decr_freq = freq;
  911. tb_env->opaque = ppc40x_timer;
  912. LOG_TB("%s freq %" PRIu32 "\n", __func__, freq);
  913. if (ppc40x_timer != NULL) {
  914. /* We use decr timer for PIT */
  915. tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_4xx_pit_cb, env);
  916. ppc40x_timer->fit_timer =
  917. qemu_new_timer_ns(vm_clock, &cpu_4xx_fit_cb, env);
  918. ppc40x_timer->wdt_timer =
  919. qemu_new_timer_ns(vm_clock, &cpu_4xx_wdt_cb, env);
  920. ppc40x_timer->decr_excp = decr_excp;
  921. }
  922. return &ppc_40x_set_tb_clk;
  923. }
  924. /*****************************************************************************/
  925. /* Embedded PowerPC Device Control Registers */
  926. typedef struct ppc_dcrn_t ppc_dcrn_t;
  927. struct ppc_dcrn_t {
  928. dcr_read_cb dcr_read;
  929. dcr_write_cb dcr_write;
  930. void *opaque;
  931. };
  932. /* XXX: on 460, DCR addresses are 32 bits wide,
  933. * using DCRIPR to get the 22 upper bits of the DCR address
  934. */
  935. #define DCRN_NB 1024
  936. struct ppc_dcr_t {
  937. ppc_dcrn_t dcrn[DCRN_NB];
  938. int (*read_error)(int dcrn);
  939. int (*write_error)(int dcrn);
  940. };
  941. int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
  942. {
  943. ppc_dcrn_t *dcr;
  944. if (dcrn < 0 || dcrn >= DCRN_NB)
  945. goto error;
  946. dcr = &dcr_env->dcrn[dcrn];
  947. if (dcr->dcr_read == NULL)
  948. goto error;
  949. *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
  950. return 0;
  951. error:
  952. if (dcr_env->read_error != NULL)
  953. return (*dcr_env->read_error)(dcrn);
  954. return -1;
  955. }
  956. int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
  957. {
  958. ppc_dcrn_t *dcr;
  959. if (dcrn < 0 || dcrn >= DCRN_NB)
  960. goto error;
  961. dcr = &dcr_env->dcrn[dcrn];
  962. if (dcr->dcr_write == NULL)
  963. goto error;
  964. (*dcr->dcr_write)(dcr->opaque, dcrn, val);
  965. return 0;
  966. error:
  967. if (dcr_env->write_error != NULL)
  968. return (*dcr_env->write_error)(dcrn);
  969. return -1;
  970. }
  971. int ppc_dcr_register (CPUState *env, int dcrn, void *opaque,
  972. dcr_read_cb dcr_read, dcr_write_cb dcr_write)
  973. {
  974. ppc_dcr_t *dcr_env;
  975. ppc_dcrn_t *dcr;
  976. dcr_env = env->dcr_env;
  977. if (dcr_env == NULL)
  978. return -1;
  979. if (dcrn < 0 || dcrn >= DCRN_NB)
  980. return -1;
  981. dcr = &dcr_env->dcrn[dcrn];
  982. if (dcr->opaque != NULL ||
  983. dcr->dcr_read != NULL ||
  984. dcr->dcr_write != NULL)
  985. return -1;
  986. dcr->opaque = opaque;
  987. dcr->dcr_read = dcr_read;
  988. dcr->dcr_write = dcr_write;
  989. return 0;
  990. }
  991. int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn),
  992. int (*write_error)(int dcrn))
  993. {
  994. ppc_dcr_t *dcr_env;
  995. dcr_env = g_malloc0(sizeof(ppc_dcr_t));
  996. dcr_env->read_error = read_error;
  997. dcr_env->write_error = write_error;
  998. env->dcr_env = dcr_env;
  999. return 0;
  1000. }
  1001. /*****************************************************************************/
  1002. /* Debug port */
  1003. void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
  1004. {
  1005. addr &= 0xF;
  1006. switch (addr) {
  1007. case 0:
  1008. printf("%c", val);
  1009. break;
  1010. case 1:
  1011. printf("\n");
  1012. fflush(stdout);
  1013. break;
  1014. case 2:
  1015. printf("Set loglevel to %04" PRIx32 "\n", val);
  1016. cpu_set_log(val | 0x100);
  1017. break;
  1018. }
  1019. }
  1020. /*****************************************************************************/
  1021. /* NVRAM helpers */
  1022. static inline uint32_t nvram_read (nvram_t *nvram, uint32_t addr)
  1023. {
  1024. return (*nvram->read_fn)(nvram->opaque, addr);;
  1025. }
  1026. static inline void nvram_write (nvram_t *nvram, uint32_t addr, uint32_t val)
  1027. {
  1028. (*nvram->write_fn)(nvram->opaque, addr, val);
  1029. }
  1030. void NVRAM_set_byte (nvram_t *nvram, uint32_t addr, uint8_t value)
  1031. {
  1032. nvram_write(nvram, addr, value);
  1033. }
  1034. uint8_t NVRAM_get_byte (nvram_t *nvram, uint32_t addr)
  1035. {
  1036. return nvram_read(nvram, addr);
  1037. }
  1038. void NVRAM_set_word (nvram_t *nvram, uint32_t addr, uint16_t value)
  1039. {
  1040. nvram_write(nvram, addr, value >> 8);
  1041. nvram_write(nvram, addr + 1, value & 0xFF);
  1042. }
  1043. uint16_t NVRAM_get_word (nvram_t *nvram, uint32_t addr)
  1044. {
  1045. uint16_t tmp;
  1046. tmp = nvram_read(nvram, addr) << 8;
  1047. tmp |= nvram_read(nvram, addr + 1);
  1048. return tmp;
  1049. }
  1050. void NVRAM_set_lword (nvram_t *nvram, uint32_t addr, uint32_t value)
  1051. {
  1052. nvram_write(nvram, addr, value >> 24);
  1053. nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
  1054. nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
  1055. nvram_write(nvram, addr + 3, value & 0xFF);
  1056. }
  1057. uint32_t NVRAM_get_lword (nvram_t *nvram, uint32_t addr)
  1058. {
  1059. uint32_t tmp;
  1060. tmp = nvram_read(nvram, addr) << 24;
  1061. tmp |= nvram_read(nvram, addr + 1) << 16;
  1062. tmp |= nvram_read(nvram, addr + 2) << 8;
  1063. tmp |= nvram_read(nvram, addr + 3);
  1064. return tmp;
  1065. }
  1066. void NVRAM_set_string (nvram_t *nvram, uint32_t addr,
  1067. const char *str, uint32_t max)
  1068. {
  1069. int i;
  1070. for (i = 0; i < max && str[i] != '\0'; i++) {
  1071. nvram_write(nvram, addr + i, str[i]);
  1072. }
  1073. nvram_write(nvram, addr + i, str[i]);
  1074. nvram_write(nvram, addr + max - 1, '\0');
  1075. }
  1076. int NVRAM_get_string (nvram_t *nvram, uint8_t *dst, uint16_t addr, int max)
  1077. {
  1078. int i;
  1079. memset(dst, 0, max);
  1080. for (i = 0; i < max; i++) {
  1081. dst[i] = NVRAM_get_byte(nvram, addr + i);
  1082. if (dst[i] == '\0')
  1083. break;
  1084. }
  1085. return i;
  1086. }
  1087. static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
  1088. {
  1089. uint16_t tmp;
  1090. uint16_t pd, pd1, pd2;
  1091. tmp = prev >> 8;
  1092. pd = prev ^ value;
  1093. pd1 = pd & 0x000F;
  1094. pd2 = ((pd >> 4) & 0x000F) ^ pd1;
  1095. tmp ^= (pd1 << 3) | (pd1 << 8);
  1096. tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
  1097. return tmp;
  1098. }
  1099. static uint16_t NVRAM_compute_crc (nvram_t *nvram, uint32_t start, uint32_t count)
  1100. {
  1101. uint32_t i;
  1102. uint16_t crc = 0xFFFF;
  1103. int odd;
  1104. odd = count & 1;
  1105. count &= ~1;
  1106. for (i = 0; i != count; i++) {
  1107. crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
  1108. }
  1109. if (odd) {
  1110. crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
  1111. }
  1112. return crc;
  1113. }
  1114. #define CMDLINE_ADDR 0x017ff000
  1115. int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
  1116. const char *arch,
  1117. uint32_t RAM_size, int boot_device,
  1118. uint32_t kernel_image, uint32_t kernel_size,
  1119. const char *cmdline,
  1120. uint32_t initrd_image, uint32_t initrd_size,
  1121. uint32_t NVRAM_image,
  1122. int width, int height, int depth)
  1123. {
  1124. uint16_t crc;
  1125. /* Set parameters for Open Hack'Ware BIOS */
  1126. NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
  1127. NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
  1128. NVRAM_set_word(nvram, 0x14, NVRAM_size);
  1129. NVRAM_set_string(nvram, 0x20, arch, 16);
  1130. NVRAM_set_lword(nvram, 0x30, RAM_size);
  1131. NVRAM_set_byte(nvram, 0x34, boot_device);
  1132. NVRAM_set_lword(nvram, 0x38, kernel_image);
  1133. NVRAM_set_lword(nvram, 0x3C, kernel_size);
  1134. if (cmdline) {
  1135. /* XXX: put the cmdline in NVRAM too ? */
  1136. pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR, cmdline);
  1137. NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
  1138. NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
  1139. } else {
  1140. NVRAM_set_lword(nvram, 0x40, 0);
  1141. NVRAM_set_lword(nvram, 0x44, 0);
  1142. }
  1143. NVRAM_set_lword(nvram, 0x48, initrd_image);
  1144. NVRAM_set_lword(nvram, 0x4C, initrd_size);
  1145. NVRAM_set_lword(nvram, 0x50, NVRAM_image);
  1146. NVRAM_set_word(nvram, 0x54, width);
  1147. NVRAM_set_word(nvram, 0x56, height);
  1148. NVRAM_set_word(nvram, 0x58, depth);
  1149. crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
  1150. NVRAM_set_word(nvram, 0xFC, crc);
  1151. return 0;
  1152. }