acpi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * ACPI implementation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License version 2 as published by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  18. */
  19. #include "hw.h"
  20. #include "pc.h"
  21. #include "pci.h"
  22. #include "qemu-timer.h"
  23. #include "sysemu.h"
  24. #include "i2c.h"
  25. #include "smbus.h"
  26. #include "kvm.h"
  27. //#define DEBUG
  28. /* i82731AB (PIIX4) compatible power management function */
  29. #define PM_FREQ 3579545
  30. #define ACPI_DBG_IO_ADDR 0xb044
  31. typedef struct PIIX4PMState {
  32. PCIDevice dev;
  33. uint16_t pmsts;
  34. uint16_t pmen;
  35. uint16_t pmcntrl;
  36. uint8_t apmc;
  37. uint8_t apms;
  38. QEMUTimer *tmr_timer;
  39. int64_t tmr_overflow_time;
  40. i2c_bus *smbus;
  41. uint8_t smb_stat;
  42. uint8_t smb_ctl;
  43. uint8_t smb_cmd;
  44. uint8_t smb_addr;
  45. uint8_t smb_data0;
  46. uint8_t smb_data1;
  47. uint8_t smb_data[32];
  48. uint8_t smb_index;
  49. qemu_irq irq;
  50. } PIIX4PMState;
  51. #define RSM_STS (1 << 15)
  52. #define PWRBTN_STS (1 << 8)
  53. #define RTC_EN (1 << 10)
  54. #define PWRBTN_EN (1 << 8)
  55. #define GBL_EN (1 << 5)
  56. #define TMROF_EN (1 << 0)
  57. #define SCI_EN (1 << 0)
  58. #define SUS_EN (1 << 13)
  59. #define ACPI_ENABLE 0xf1
  60. #define ACPI_DISABLE 0xf0
  61. #define SMBHSTSTS 0x00
  62. #define SMBHSTCNT 0x02
  63. #define SMBHSTCMD 0x03
  64. #define SMBHSTADD 0x04
  65. #define SMBHSTDAT0 0x05
  66. #define SMBHSTDAT1 0x06
  67. #define SMBBLKDAT 0x07
  68. static PIIX4PMState *pm_state;
  69. static uint32_t get_pmtmr(PIIX4PMState *s)
  70. {
  71. uint32_t d;
  72. d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
  73. return d & 0xffffff;
  74. }
  75. static int get_pmsts(PIIX4PMState *s)
  76. {
  77. int64_t d;
  78. int pmsts;
  79. pmsts = s->pmsts;
  80. d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
  81. if (d >= s->tmr_overflow_time)
  82. s->pmsts |= TMROF_EN;
  83. return s->pmsts;
  84. }
  85. static void pm_update_sci(PIIX4PMState *s)
  86. {
  87. int sci_level, pmsts;
  88. int64_t expire_time;
  89. pmsts = get_pmsts(s);
  90. sci_level = (((pmsts & s->pmen) &
  91. (RTC_EN | PWRBTN_EN | GBL_EN | TMROF_EN)) != 0);
  92. qemu_set_irq(s->irq, sci_level);
  93. /* schedule a timer interruption if needed */
  94. if ((s->pmen & TMROF_EN) && !(pmsts & TMROF_EN)) {
  95. expire_time = muldiv64(s->tmr_overflow_time, ticks_per_sec, PM_FREQ);
  96. qemu_mod_timer(s->tmr_timer, expire_time);
  97. } else {
  98. qemu_del_timer(s->tmr_timer);
  99. }
  100. }
  101. static void pm_tmr_timer(void *opaque)
  102. {
  103. PIIX4PMState *s = opaque;
  104. pm_update_sci(s);
  105. }
  106. static void pm_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  107. {
  108. PIIX4PMState *s = opaque;
  109. addr &= 0x3f;
  110. switch(addr) {
  111. case 0x00:
  112. {
  113. int64_t d;
  114. int pmsts;
  115. pmsts = get_pmsts(s);
  116. if (pmsts & val & TMROF_EN) {
  117. /* if TMRSTS is reset, then compute the new overflow time */
  118. d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
  119. s->tmr_overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
  120. }
  121. s->pmsts &= ~val;
  122. pm_update_sci(s);
  123. }
  124. break;
  125. case 0x02:
  126. s->pmen = val;
  127. pm_update_sci(s);
  128. break;
  129. case 0x04:
  130. {
  131. int sus_typ;
  132. s->pmcntrl = val & ~(SUS_EN);
  133. if (val & SUS_EN) {
  134. /* change suspend type */
  135. sus_typ = (val >> 10) & 7;
  136. switch(sus_typ) {
  137. case 0: /* soft power off */
  138. qemu_system_shutdown_request();
  139. break;
  140. case 1:
  141. /* RSM_STS should be set on resume. Pretend that resume
  142. was caused by power button */
  143. s->pmsts |= (RSM_STS | PWRBTN_STS);
  144. qemu_system_reset_request();
  145. #if defined(TARGET_I386)
  146. cmos_set_s3_resume();
  147. #endif
  148. default:
  149. break;
  150. }
  151. }
  152. }
  153. break;
  154. default:
  155. break;
  156. }
  157. #ifdef DEBUG
  158. printf("PM writew port=0x%04x val=0x%04x\n", addr, val);
  159. #endif
  160. }
  161. static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
  162. {
  163. PIIX4PMState *s = opaque;
  164. uint32_t val;
  165. addr &= 0x3f;
  166. switch(addr) {
  167. case 0x00:
  168. val = get_pmsts(s);
  169. break;
  170. case 0x02:
  171. val = s->pmen;
  172. break;
  173. case 0x04:
  174. val = s->pmcntrl;
  175. break;
  176. default:
  177. val = 0;
  178. break;
  179. }
  180. #ifdef DEBUG
  181. printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
  182. #endif
  183. return val;
  184. }
  185. static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
  186. {
  187. // PIIX4PMState *s = opaque;
  188. addr &= 0x3f;
  189. #ifdef DEBUG
  190. printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
  191. #endif
  192. }
  193. static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
  194. {
  195. PIIX4PMState *s = opaque;
  196. uint32_t val;
  197. addr &= 0x3f;
  198. switch(addr) {
  199. case 0x08:
  200. val = get_pmtmr(s);
  201. break;
  202. default:
  203. val = 0;
  204. break;
  205. }
  206. #ifdef DEBUG
  207. printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
  208. #endif
  209. return val;
  210. }
  211. static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val)
  212. {
  213. PIIX4PMState *s = opaque;
  214. addr &= 1;
  215. #ifdef DEBUG
  216. printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
  217. #endif
  218. if (addr == 0) {
  219. s->apmc = val;
  220. /* ACPI specs 3.0, 4.7.2.5 */
  221. if (val == ACPI_ENABLE) {
  222. s->pmcntrl |= SCI_EN;
  223. } else if (val == ACPI_DISABLE) {
  224. s->pmcntrl &= ~SCI_EN;
  225. }
  226. if (s->dev.config[0x5b] & (1 << 1)) {
  227. cpu_interrupt(first_cpu, CPU_INTERRUPT_SMI);
  228. }
  229. } else {
  230. s->apms = val;
  231. }
  232. }
  233. static uint32_t pm_smi_readb(void *opaque, uint32_t addr)
  234. {
  235. PIIX4PMState *s = opaque;
  236. uint32_t val;
  237. addr &= 1;
  238. if (addr == 0) {
  239. val = s->apmc;
  240. } else {
  241. val = s->apms;
  242. }
  243. #ifdef DEBUG
  244. printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
  245. #endif
  246. return val;
  247. }
  248. static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
  249. {
  250. #if defined(DEBUG)
  251. printf("ACPI: DBG: 0x%08x\n", val);
  252. #endif
  253. }
  254. static void smb_transaction(PIIX4PMState *s)
  255. {
  256. uint8_t prot = (s->smb_ctl >> 2) & 0x07;
  257. uint8_t read = s->smb_addr & 0x01;
  258. uint8_t cmd = s->smb_cmd;
  259. uint8_t addr = s->smb_addr >> 1;
  260. i2c_bus *bus = s->smbus;
  261. #ifdef DEBUG
  262. printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
  263. #endif
  264. switch(prot) {
  265. case 0x0:
  266. smbus_quick_command(bus, addr, read);
  267. break;
  268. case 0x1:
  269. if (read) {
  270. s->smb_data0 = smbus_receive_byte(bus, addr);
  271. } else {
  272. smbus_send_byte(bus, addr, cmd);
  273. }
  274. break;
  275. case 0x2:
  276. if (read) {
  277. s->smb_data0 = smbus_read_byte(bus, addr, cmd);
  278. } else {
  279. smbus_write_byte(bus, addr, cmd, s->smb_data0);
  280. }
  281. break;
  282. case 0x3:
  283. if (read) {
  284. uint16_t val;
  285. val = smbus_read_word(bus, addr, cmd);
  286. s->smb_data0 = val;
  287. s->smb_data1 = val >> 8;
  288. } else {
  289. smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
  290. }
  291. break;
  292. case 0x5:
  293. if (read) {
  294. s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
  295. } else {
  296. smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
  297. }
  298. break;
  299. default:
  300. goto error;
  301. }
  302. return;
  303. error:
  304. s->smb_stat |= 0x04;
  305. }
  306. static void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  307. {
  308. PIIX4PMState *s = opaque;
  309. addr &= 0x3f;
  310. #ifdef DEBUG
  311. printf("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
  312. #endif
  313. switch(addr) {
  314. case SMBHSTSTS:
  315. s->smb_stat = 0;
  316. s->smb_index = 0;
  317. break;
  318. case SMBHSTCNT:
  319. s->smb_ctl = val;
  320. if (val & 0x40)
  321. smb_transaction(s);
  322. break;
  323. case SMBHSTCMD:
  324. s->smb_cmd = val;
  325. break;
  326. case SMBHSTADD:
  327. s->smb_addr = val;
  328. break;
  329. case SMBHSTDAT0:
  330. s->smb_data0 = val;
  331. break;
  332. case SMBHSTDAT1:
  333. s->smb_data1 = val;
  334. break;
  335. case SMBBLKDAT:
  336. s->smb_data[s->smb_index++] = val;
  337. if (s->smb_index > 31)
  338. s->smb_index = 0;
  339. break;
  340. default:
  341. break;
  342. }
  343. }
  344. static uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
  345. {
  346. PIIX4PMState *s = opaque;
  347. uint32_t val;
  348. addr &= 0x3f;
  349. switch(addr) {
  350. case SMBHSTSTS:
  351. val = s->smb_stat;
  352. break;
  353. case SMBHSTCNT:
  354. s->smb_index = 0;
  355. val = s->smb_ctl & 0x1f;
  356. break;
  357. case SMBHSTCMD:
  358. val = s->smb_cmd;
  359. break;
  360. case SMBHSTADD:
  361. val = s->smb_addr;
  362. break;
  363. case SMBHSTDAT0:
  364. val = s->smb_data0;
  365. break;
  366. case SMBHSTDAT1:
  367. val = s->smb_data1;
  368. break;
  369. case SMBBLKDAT:
  370. val = s->smb_data[s->smb_index++];
  371. if (s->smb_index > 31)
  372. s->smb_index = 0;
  373. break;
  374. default:
  375. val = 0;
  376. break;
  377. }
  378. #ifdef DEBUG
  379. printf("SMB readb port=0x%04x val=0x%02x\n", addr, val);
  380. #endif
  381. return val;
  382. }
  383. static void pm_io_space_update(PIIX4PMState *s)
  384. {
  385. uint32_t pm_io_base;
  386. if (s->dev.config[0x80] & 1) {
  387. pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
  388. pm_io_base &= 0xffc0;
  389. /* XXX: need to improve memory and ioport allocation */
  390. #if defined(DEBUG)
  391. printf("PM: mapping to 0x%x\n", pm_io_base);
  392. #endif
  393. register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
  394. register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
  395. register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
  396. register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
  397. }
  398. }
  399. static void pm_write_config(PCIDevice *d,
  400. uint32_t address, uint32_t val, int len)
  401. {
  402. pci_default_write_config(d, address, val, len);
  403. if (address == 0x80)
  404. pm_io_space_update((PIIX4PMState *)d);
  405. }
  406. static void pm_save(QEMUFile* f,void *opaque)
  407. {
  408. PIIX4PMState *s = opaque;
  409. pci_device_save(&s->dev, f);
  410. qemu_put_be16s(f, &s->pmsts);
  411. qemu_put_be16s(f, &s->pmen);
  412. qemu_put_be16s(f, &s->pmcntrl);
  413. qemu_put_8s(f, &s->apmc);
  414. qemu_put_8s(f, &s->apms);
  415. qemu_put_timer(f, s->tmr_timer);
  416. qemu_put_be64(f, s->tmr_overflow_time);
  417. }
  418. static int pm_load(QEMUFile* f,void* opaque,int version_id)
  419. {
  420. PIIX4PMState *s = opaque;
  421. int ret;
  422. if (version_id > 1)
  423. return -EINVAL;
  424. ret = pci_device_load(&s->dev, f);
  425. if (ret < 0)
  426. return ret;
  427. qemu_get_be16s(f, &s->pmsts);
  428. qemu_get_be16s(f, &s->pmen);
  429. qemu_get_be16s(f, &s->pmcntrl);
  430. qemu_get_8s(f, &s->apmc);
  431. qemu_get_8s(f, &s->apms);
  432. qemu_get_timer(f, s->tmr_timer);
  433. s->tmr_overflow_time=qemu_get_be64(f);
  434. pm_io_space_update(s);
  435. return 0;
  436. }
  437. static void piix4_reset(void *opaque)
  438. {
  439. PIIX4PMState *s = opaque;
  440. uint8_t *pci_conf = s->dev.config;
  441. pci_conf[0x58] = 0;
  442. pci_conf[0x59] = 0;
  443. pci_conf[0x5a] = 0;
  444. pci_conf[0x5b] = 0;
  445. }
  446. i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
  447. qemu_irq sci_irq)
  448. {
  449. PIIX4PMState *s;
  450. uint8_t *pci_conf;
  451. s = (PIIX4PMState *)pci_register_device(bus,
  452. "PM", sizeof(PIIX4PMState),
  453. devfn, NULL, pm_write_config);
  454. pm_state = s;
  455. pci_conf = s->dev.config;
  456. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  457. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_3);
  458. pci_conf[0x06] = 0x80;
  459. pci_conf[0x07] = 0x02;
  460. pci_conf[0x08] = 0x03; // revision number
  461. pci_conf[0x09] = 0x00;
  462. pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
  463. pci_conf[0x0e] = 0x00; // header_type
  464. pci_conf[0x3d] = 0x01; // interrupt pin 1
  465. pci_conf[0x40] = 0x01; /* PM io base read only bit */
  466. register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s);
  467. register_ioport_read(0xb2, 2, 1, pm_smi_readb, s);
  468. register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
  469. if (kvm_enabled()) {
  470. /* Mark SMM as already inited to prevent SMM from running. KVM does not
  471. * support SMM mode. */
  472. pci_conf[0x5B] = 0x02;
  473. }
  474. /* XXX: which specification is used ? The i82731AB has different
  475. mappings */
  476. pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
  477. pci_conf[0x63] = 0x60;
  478. pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
  479. (serial_hds[1] != NULL ? 0x90 : 0);
  480. pci_conf[0x90] = smb_io_base | 1;
  481. pci_conf[0x91] = smb_io_base >> 8;
  482. pci_conf[0xd2] = 0x09;
  483. register_ioport_write(smb_io_base, 64, 1, smb_ioport_writeb, s);
  484. register_ioport_read(smb_io_base, 64, 1, smb_ioport_readb, s);
  485. s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
  486. register_savevm("piix4_pm", 0, 1, pm_save, pm_load, s);
  487. s->smbus = i2c_init_bus();
  488. s->irq = sci_irq;
  489. qemu_register_reset(piix4_reset, s);
  490. return s->smbus;
  491. }
  492. #if defined(TARGET_I386)
  493. void qemu_system_powerdown(void)
  494. {
  495. if (!pm_state) {
  496. qemu_system_shutdown_request();
  497. } else if (pm_state->pmen & PWRBTN_EN) {
  498. pm_state->pmsts |= PWRBTN_EN;
  499. pm_update_sci(pm_state);
  500. }
  501. }
  502. #endif
  503. #define GPE_BASE 0xafe0
  504. #define PCI_BASE 0xae00
  505. #define PCI_EJ_BASE 0xae08
  506. struct gpe_regs {
  507. uint16_t sts; /* status */
  508. uint16_t en; /* enabled */
  509. };
  510. struct pci_status {
  511. uint32_t up;
  512. uint32_t down;
  513. };
  514. static struct gpe_regs gpe;
  515. static struct pci_status pci0_status;
  516. static uint32_t gpe_read_val(uint16_t val, uint32_t addr)
  517. {
  518. if (addr & 1)
  519. return (val >> 8) & 0xff;
  520. return val & 0xff;
  521. }
  522. static uint32_t gpe_readb(void *opaque, uint32_t addr)
  523. {
  524. uint32_t val = 0;
  525. struct gpe_regs *g = opaque;
  526. switch (addr) {
  527. case GPE_BASE:
  528. case GPE_BASE + 1:
  529. val = gpe_read_val(g->sts, addr);
  530. break;
  531. case GPE_BASE + 2:
  532. case GPE_BASE + 3:
  533. val = gpe_read_val(g->en, addr);
  534. break;
  535. default:
  536. break;
  537. }
  538. #if defined(DEBUG)
  539. printf("gpe read %x == %x\n", addr, val);
  540. #endif
  541. return val;
  542. }
  543. static void gpe_write_val(uint16_t *cur, int addr, uint32_t val)
  544. {
  545. if (addr & 1)
  546. *cur = (*cur & 0xff) | (val << 8);
  547. else
  548. *cur = (*cur & 0xff00) | (val & 0xff);
  549. }
  550. static void gpe_reset_val(uint16_t *cur, int addr, uint32_t val)
  551. {
  552. uint16_t x1, x0 = val & 0xff;
  553. int shift = (addr & 1) ? 8 : 0;
  554. x1 = (*cur >> shift) & 0xff;
  555. x1 = x1 & ~x0;
  556. *cur = (*cur & (0xff << (8 - shift))) | (x1 << shift);
  557. }
  558. static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
  559. {
  560. struct gpe_regs *g = opaque;
  561. switch (addr) {
  562. case GPE_BASE:
  563. case GPE_BASE + 1:
  564. gpe_reset_val(&g->sts, addr, val);
  565. break;
  566. case GPE_BASE + 2:
  567. case GPE_BASE + 3:
  568. gpe_write_val(&g->en, addr, val);
  569. break;
  570. default:
  571. break;
  572. }
  573. #if defined(DEBUG)
  574. printf("gpe write %x <== %d\n", addr, val);
  575. #endif
  576. }
  577. static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
  578. {
  579. uint32_t val = 0;
  580. struct pci_status *g = opaque;
  581. switch (addr) {
  582. case PCI_BASE:
  583. val = g->up;
  584. break;
  585. case PCI_BASE + 4:
  586. val = g->down;
  587. break;
  588. default:
  589. break;
  590. }
  591. #if defined(DEBUG)
  592. printf("pcihotplug read %x == %x\n", addr, val);
  593. #endif
  594. return val;
  595. }
  596. static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
  597. {
  598. struct pci_status *g = opaque;
  599. switch (addr) {
  600. case PCI_BASE:
  601. g->up = val;
  602. break;
  603. case PCI_BASE + 4:
  604. g->down = val;
  605. break;
  606. }
  607. #if defined(DEBUG)
  608. printf("pcihotplug write %x <== %d\n", addr, val);
  609. #endif
  610. }
  611. static uint32_t pciej_read(void *opaque, uint32_t addr)
  612. {
  613. #if defined(DEBUG)
  614. printf("pciej read %x\n", addr);
  615. #endif
  616. return 0;
  617. }
  618. static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
  619. {
  620. #if defined (TARGET_I386)
  621. int slot = ffs(val) - 1;
  622. pci_device_hot_remove_success(0, slot);
  623. #endif
  624. #if defined(DEBUG)
  625. printf("pciej write %x <== %d\n", addr, val);
  626. #endif
  627. }
  628. void qemu_system_hot_add_init(void)
  629. {
  630. register_ioport_write(GPE_BASE, 4, 1, gpe_writeb, &gpe);
  631. register_ioport_read(GPE_BASE, 4, 1, gpe_readb, &gpe);
  632. register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, &pci0_status);
  633. register_ioport_read(PCI_BASE, 8, 4, pcihotplug_read, &pci0_status);
  634. register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, NULL);
  635. register_ioport_read(PCI_EJ_BASE, 4, 4, pciej_read, NULL);
  636. }
  637. static void enable_device(struct pci_status *p, struct gpe_regs *g, int slot)
  638. {
  639. g->sts |= 2;
  640. p->up |= (1 << slot);
  641. }
  642. static void disable_device(struct pci_status *p, struct gpe_regs *g, int slot)
  643. {
  644. g->sts |= 2;
  645. p->down |= (1 << slot);
  646. }
  647. void qemu_system_device_hot_add(int bus, int slot, int state)
  648. {
  649. pci0_status.up = 0;
  650. pci0_status.down = 0;
  651. if (state)
  652. enable_device(&pci0_status, &gpe, slot);
  653. else
  654. disable_device(&pci0_status, &gpe, slot);
  655. if (gpe.en & 2) {
  656. qemu_set_irq(pm_state->irq, 1);
  657. qemu_set_irq(pm_state->irq, 0);
  658. }
  659. }
  660. struct acpi_table_header
  661. {
  662. char signature [4]; /* ACPI signature (4 ASCII characters) */
  663. uint32_t length; /* Length of table, in bytes, including header */
  664. uint8_t revision; /* ACPI Specification minor version # */
  665. uint8_t checksum; /* To make sum of entire table == 0 */
  666. char oem_id [6]; /* OEM identification */
  667. char oem_table_id [8]; /* OEM table identification */
  668. uint32_t oem_revision; /* OEM revision number */
  669. char asl_compiler_id [4]; /* ASL compiler vendor ID */
  670. uint32_t asl_compiler_revision; /* ASL compiler revision number */
  671. } __attribute__((packed));
  672. char *acpi_tables;
  673. size_t acpi_tables_len;
  674. static int acpi_checksum(const uint8_t *data, int len)
  675. {
  676. int sum, i;
  677. sum = 0;
  678. for(i = 0; i < len; i++)
  679. sum += data[i];
  680. return (-sum) & 0xff;
  681. }
  682. int acpi_table_add(const char *t)
  683. {
  684. static const char *dfl_id = "QEMUQEMU";
  685. char buf[1024], *p, *f;
  686. struct acpi_table_header acpi_hdr;
  687. unsigned long val;
  688. size_t off;
  689. memset(&acpi_hdr, 0, sizeof(acpi_hdr));
  690. if (get_param_value(buf, sizeof(buf), "sig", t)) {
  691. strncpy(acpi_hdr.signature, buf, 4);
  692. } else {
  693. strncpy(acpi_hdr.signature, dfl_id, 4);
  694. }
  695. if (get_param_value(buf, sizeof(buf), "rev", t)) {
  696. val = strtoul(buf, &p, 10);
  697. if (val > 255 || *p != '\0')
  698. goto out;
  699. } else {
  700. val = 1;
  701. }
  702. acpi_hdr.revision = (int8_t)val;
  703. if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
  704. strncpy(acpi_hdr.oem_id, buf, 6);
  705. } else {
  706. strncpy(acpi_hdr.oem_id, dfl_id, 6);
  707. }
  708. if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
  709. strncpy(acpi_hdr.oem_table_id, buf, 8);
  710. } else {
  711. strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
  712. }
  713. if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
  714. val = strtol(buf, &p, 10);
  715. if(*p != '\0')
  716. goto out;
  717. } else {
  718. val = 1;
  719. }
  720. acpi_hdr.oem_revision = cpu_to_le32(val);
  721. if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
  722. strncpy(acpi_hdr.asl_compiler_id, buf, 4);
  723. } else {
  724. strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
  725. }
  726. if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
  727. val = strtol(buf, &p, 10);
  728. if(*p != '\0')
  729. goto out;
  730. } else {
  731. val = 1;
  732. }
  733. acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
  734. if (!get_param_value(buf, sizeof(buf), "data", t)) {
  735. buf[0] = '\0';
  736. }
  737. acpi_hdr.length = sizeof(acpi_hdr);
  738. f = buf;
  739. while (buf[0]) {
  740. struct stat s;
  741. char *n = strchr(f, ':');
  742. if (n)
  743. *n = '\0';
  744. if(stat(f, &s) < 0) {
  745. fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
  746. goto out;
  747. }
  748. acpi_hdr.length += s.st_size;
  749. if (!n)
  750. break;
  751. *n = ':';
  752. f = n + 1;
  753. }
  754. if (!acpi_tables) {
  755. acpi_tables_len = sizeof(uint16_t);
  756. acpi_tables = qemu_mallocz(acpi_tables_len);
  757. }
  758. p = acpi_tables + acpi_tables_len;
  759. acpi_tables_len += sizeof(uint16_t) + acpi_hdr.length;
  760. acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
  761. acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
  762. *(uint16_t*)p = acpi_hdr.length;
  763. p += sizeof(uint16_t);
  764. memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
  765. off = sizeof(acpi_hdr);
  766. f = buf;
  767. while (buf[0]) {
  768. struct stat s;
  769. int fd;
  770. char *n = strchr(f, ':');
  771. if (n)
  772. *n = '\0';
  773. fd = open(f, O_RDONLY);
  774. if(fd < 0)
  775. goto out;
  776. if(fstat(fd, &s) < 0) {
  777. close(fd);
  778. goto out;
  779. }
  780. do {
  781. int r;
  782. r = read(fd, p + off, s.st_size);
  783. if (r > 0) {
  784. off += r;
  785. s.st_size -= r;
  786. } else if ((r < 0 && errno != EINTR) || r == 0) {
  787. close(fd);
  788. goto out;
  789. }
  790. } while(s.st_size);
  791. close(fd);
  792. if (!n)
  793. break;
  794. f = n + 1;
  795. }
  796. ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
  797. /* increase number of tables */
  798. (*(uint16_t*)acpi_tables) =
  799. cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
  800. return 0;
  801. out:
  802. if (acpi_tables) {
  803. free(acpi_tables);
  804. acpi_tables = NULL;
  805. }
  806. return -1;
  807. }