xref: /qemu/hw/pci-host/pam.c (revision 30dc600bbfe2c927440fd611d585d7a2acb42fbf)
1410edd92SIsaku Yamahata /*
2*30dc600bSGonglei  * QEMU Smram/pam logic implementation
3410edd92SIsaku Yamahata  *
4410edd92SIsaku Yamahata  * Copyright (c) 2006 Fabrice Bellard
5410edd92SIsaku Yamahata  * Copyright (c) 2011 Isaku Yamahata <yamahata at valinux co jp>
6410edd92SIsaku Yamahata  *                    VA Linux Systems Japan K.K.
7410edd92SIsaku Yamahata  * Copyright (c) 2012 Jason Baron <jbaron@redhat.com>
8410edd92SIsaku Yamahata  *
9ef9f7b58SGonglei  * Split out from piix.c
10410edd92SIsaku Yamahata  *
11410edd92SIsaku Yamahata  * Permission is hereby granted, free of charge, to any person obtaining a copy
12410edd92SIsaku Yamahata  * of this software and associated documentation files (the "Software"), to deal
13410edd92SIsaku Yamahata  * in the Software without restriction, including without limitation the rights
14410edd92SIsaku Yamahata  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15410edd92SIsaku Yamahata  * copies of the Software, and to permit persons to whom the Software is
16410edd92SIsaku Yamahata  * furnished to do so, subject to the following conditions:
17410edd92SIsaku Yamahata  *
18410edd92SIsaku Yamahata  * The above copyright notice and this permission notice shall be included in
19410edd92SIsaku Yamahata  * all copies or substantial portions of the Software.
20410edd92SIsaku Yamahata  *
21410edd92SIsaku Yamahata  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22410edd92SIsaku Yamahata  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23410edd92SIsaku Yamahata  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24410edd92SIsaku Yamahata  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25410edd92SIsaku Yamahata  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26410edd92SIsaku Yamahata  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27410edd92SIsaku Yamahata  * THE SOFTWARE.
28410edd92SIsaku Yamahata  */
293cd2cf43SPaolo Bonzini 
303cd2cf43SPaolo Bonzini #include "qom/object.h"
319c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
320d09e41aSPaolo Bonzini #include "hw/pci-host/pam.h"
33410edd92SIsaku Yamahata 
34410edd92SIsaku Yamahata void smram_update(MemoryRegion *smram_region, uint8_t smram,
35410edd92SIsaku Yamahata                   uint8_t smm_enabled)
36410edd92SIsaku Yamahata {
37410edd92SIsaku Yamahata     bool smram_enabled;
38410edd92SIsaku Yamahata 
39410edd92SIsaku Yamahata     smram_enabled = ((smm_enabled && (smram & SMRAM_G_SMRAME)) ||
40410edd92SIsaku Yamahata                         (smram & SMRAM_D_OPEN));
41410edd92SIsaku Yamahata     memory_region_set_enabled(smram_region, !smram_enabled);
42410edd92SIsaku Yamahata }
43410edd92SIsaku Yamahata 
44410edd92SIsaku Yamahata void smram_set_smm(uint8_t *host_smm_enabled, int smm, uint8_t smram,
45410edd92SIsaku Yamahata                    MemoryRegion *smram_region)
46410edd92SIsaku Yamahata {
47410edd92SIsaku Yamahata     uint8_t smm_enabled = (smm != 0);
48410edd92SIsaku Yamahata     if (*host_smm_enabled != smm_enabled) {
49410edd92SIsaku Yamahata         *host_smm_enabled = smm_enabled;
50410edd92SIsaku Yamahata         smram_update(smram_region, smram, *host_smm_enabled);
51410edd92SIsaku Yamahata     }
52410edd92SIsaku Yamahata }
53410edd92SIsaku Yamahata 
543cd2cf43SPaolo Bonzini void init_pam(DeviceState *dev, MemoryRegion *ram_memory,
553cd2cf43SPaolo Bonzini               MemoryRegion *system_memory, MemoryRegion *pci_address_space,
563cd2cf43SPaolo Bonzini               PAMMemoryRegion *mem, uint32_t start, uint32_t size)
57410edd92SIsaku Yamahata {
58410edd92SIsaku Yamahata     int i;
59410edd92SIsaku Yamahata 
60410edd92SIsaku Yamahata     /* RAM */
613cd2cf43SPaolo Bonzini     memory_region_init_alias(&mem->alias[3], OBJECT(dev), "pam-ram", ram_memory,
62410edd92SIsaku Yamahata                              start, size);
63410edd92SIsaku Yamahata     /* ROM (XXX: not quite correct) */
643cd2cf43SPaolo Bonzini     memory_region_init_alias(&mem->alias[1], OBJECT(dev), "pam-rom", ram_memory,
65410edd92SIsaku Yamahata                              start, size);
66410edd92SIsaku Yamahata     memory_region_set_readonly(&mem->alias[1], true);
67410edd92SIsaku Yamahata 
68410edd92SIsaku Yamahata     /* XXX: should distinguish read/write cases */
693cd2cf43SPaolo Bonzini     memory_region_init_alias(&mem->alias[0], OBJECT(dev), "pam-pci", pci_address_space,
70410edd92SIsaku Yamahata                              start, size);
71175f099bSHervé Poussineau     memory_region_init_alias(&mem->alias[2], OBJECT(dev), "pam-pci", ram_memory,
72410edd92SIsaku Yamahata                              start, size);
73410edd92SIsaku Yamahata 
74410edd92SIsaku Yamahata     for (i = 0; i < 4; ++i) {
75410edd92SIsaku Yamahata         memory_region_set_enabled(&mem->alias[i], false);
76410edd92SIsaku Yamahata         memory_region_add_subregion_overlap(system_memory, start,
77410edd92SIsaku Yamahata                                             &mem->alias[i], 1);
78410edd92SIsaku Yamahata     }
79410edd92SIsaku Yamahata     mem->current = 0;
80410edd92SIsaku Yamahata }
81410edd92SIsaku Yamahata 
82410edd92SIsaku Yamahata void pam_update(PAMMemoryRegion *pam, int idx, uint8_t val)
83410edd92SIsaku Yamahata {
84410edd92SIsaku Yamahata     assert(0 <= idx && idx <= 12);
85410edd92SIsaku Yamahata 
86410edd92SIsaku Yamahata     memory_region_set_enabled(&pam->alias[pam->current], false);
87410edd92SIsaku Yamahata     pam->current = (val >> ((!(idx & 1)) * 4)) & PAM_ATTR_MASK;
88410edd92SIsaku Yamahata     memory_region_set_enabled(&pam->alias[pam->current], true);
89410edd92SIsaku Yamahata }
90