blob: dfe9020d30b768202f48d0eed2b97bfdffae6d12 [file] [log] [blame]
Isaku Yamahata04762842010-05-14 16:29:00 +09001/*
2 * QEMU PC APM controller Emulation
3 * This is split out from acpi.c
4 *
5 * Copyright (c) 2006 Fabrice Bellard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
Chetan Pant61f3c912020-10-23 12:44:24 +00009 * License version 2.1 as published by the Free Software Foundation.
Isaku Yamahata04762842010-05-14 16:29:00 +090010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010018 *
19 * Contributions after 2012-01-13 are licensed under the terms of the
20 * GNU GPL, version 2 or (at your option) any later version.
Isaku Yamahata04762842010-05-14 16:29:00 +090021 */
22
Peter Maydell04308912016-01-26 18:17:30 +000023#include "qemu/osdep.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010024#include "hw/isa/apm.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/pci/pci.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020026#include "migration/vmstate.h"
Philippe Mathieu-Daudé18cdeb72020-05-24 18:48:06 +020027#include "trace.h"
Isaku Yamahata04762842010-05-14 16:29:00 +090028
Isaku Yamahata019ea972010-05-14 16:29:23 +090029
Isaku Yamahata04762842010-05-14 16:29:00 +090030/* fixed I/O location */
Isaku Yamahata04762842010-05-14 16:29:00 +090031#define APM_STS_IOPORT 0xb3
32
Julien Grall42d8a3c2012-09-19 12:50:03 +010033static void apm_ioport_writeb(void *opaque, hwaddr addr, uint64_t val,
34 unsigned size)
Isaku Yamahata04762842010-05-14 16:29:00 +090035{
36 APMState *apm = opaque;
37 addr &= 1;
Philippe Mathieu-Daudé18cdeb72020-05-24 18:48:06 +020038
39 trace_apm_io_write(addr, val);
Isaku Yamahata04762842010-05-14 16:29:00 +090040 if (addr == 0) {
41 apm->apmc = val;
42
43 if (apm->callback) {
44 (apm->callback)(val, apm->arg);
45 }
46 } else {
47 apm->apms = val;
48 }
49}
50
Julien Grall42d8a3c2012-09-19 12:50:03 +010051static uint64_t apm_ioport_readb(void *opaque, hwaddr addr, unsigned size)
Isaku Yamahata04762842010-05-14 16:29:00 +090052{
53 APMState *apm = opaque;
54 uint32_t val;
55
56 addr &= 1;
57 if (addr == 0) {
58 val = apm->apmc;
59 } else {
60 val = apm->apms;
61 }
Philippe Mathieu-Daudé18cdeb72020-05-24 18:48:06 +020062 trace_apm_io_read(addr, val);
63
Isaku Yamahata04762842010-05-14 16:29:00 +090064 return val;
65}
66
67const VMStateDescription vmstate_apm = {
68 .name = "APM State",
69 .version_id = 1,
70 .minimum_version_id = 1,
Isaku Yamahata04762842010-05-14 16:29:00 +090071 .fields = (VMStateField[]) {
72 VMSTATE_UINT8(apmc, APMState),
73 VMSTATE_UINT8(apms, APMState),
74 VMSTATE_END_OF_LIST()
75 }
76};
77
Julien Grall42d8a3c2012-09-19 12:50:03 +010078static const MemoryRegionOps apm_ops = {
79 .read = apm_ioport_readb,
80 .write = apm_ioport_writeb,
81 .impl = {
82 .min_access_size = 1,
83 .max_access_size = 1,
84 },
85};
86
87void apm_init(PCIDevice *dev, APMState *apm, apm_ctrl_changed_t callback,
88 void *arg)
Isaku Yamahata04762842010-05-14 16:29:00 +090089{
90 apm->callback = callback;
91 apm->arg = arg;
92
93 /* ioport 0xb2, 0xb3 */
Paolo Bonzini1437c942013-06-06 21:25:08 -040094 memory_region_init_io(&apm->io, OBJECT(dev), &apm_ops, apm, "apm-io", 2);
Julien Grall42d8a3c2012-09-19 12:50:03 +010095 memory_region_add_subregion(pci_address_space_io(dev), APM_CNT_IOPORT,
96 &apm->io);
Isaku Yamahata04762842010-05-14 16:29:00 +090097}