j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * PowerMac NVRAM emulation |
| 3 | * |
| 4 | * Copyright (c) 2005-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 | */ |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 25 | #include "hw.h" |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 26 | #include "firmware_abi.h" |
| 27 | #include "sysemu.h" |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 28 | #include "ppc_mac.h" |
| 29 | |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 30 | /* debug NVR */ |
| 31 | //#define DEBUG_NVR |
| 32 | |
| 33 | #ifdef DEBUG_NVR |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 34 | #define NVR_DPRINTF(fmt, ...) \ |
| 35 | do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0) |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 36 | #else |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 37 | #define NVR_DPRINTF(fmt, ...) |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 40 | struct MacIONVRAMState { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 41 | target_phys_addr_t size; |
j_mayer | 74e9115 | 2007-11-04 01:16:04 +0000 | [diff] [blame] | 42 | int mem_index; |
blueswir1 | 68af3f2 | 2009-02-07 10:48:26 +0000 | [diff] [blame] | 43 | unsigned int it_shift; |
aurel32 | 3f7cbbb | 2008-12-26 23:05:15 +0000 | [diff] [blame] | 44 | uint8_t *data; |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
aurel32 | bd89f43 | 2008-12-26 23:05:23 +0000 | [diff] [blame] | 47 | #define DEF_SYSTEM_SIZE 0xc10 |
| 48 | |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 49 | /* Direct access to NVRAM */ |
| 50 | uint32_t macio_nvram_read (void *opaque, uint32_t addr) |
| 51 | { |
| 52 | MacIONVRAMState *s = opaque; |
| 53 | uint32_t ret; |
| 54 | |
aurel32 | 3f7cbbb | 2008-12-26 23:05:15 +0000 | [diff] [blame] | 55 | if (addr < s->size) |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 56 | ret = s->data[addr]; |
| 57 | else |
| 58 | ret = -1; |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 59 | NVR_DPRINTF("read addr %04x val %x\n", addr, ret); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val) |
| 65 | { |
| 66 | MacIONVRAMState *s = opaque; |
| 67 | |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 68 | NVR_DPRINTF("write addr %04x val %x\n", addr, val); |
aurel32 | 3f7cbbb | 2008-12-26 23:05:15 +0000 | [diff] [blame] | 69 | if (addr < s->size) |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 70 | s->data[addr] = val; |
| 71 | } |
| 72 | |
| 73 | /* macio style NVRAM device */ |
| 74 | static void macio_nvram_writeb (void *opaque, |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 75 | target_phys_addr_t addr, uint32_t value) |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 76 | { |
| 77 | MacIONVRAMState *s = opaque; |
j_mayer | 74e9115 | 2007-11-04 01:16:04 +0000 | [diff] [blame] | 78 | |
blueswir1 | 68af3f2 | 2009-02-07 10:48:26 +0000 | [diff] [blame] | 79 | addr = (addr >> s->it_shift) & (s->size - 1); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 80 | s->data[addr] = value; |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 81 | NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 84 | static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr) |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 85 | { |
| 86 | MacIONVRAMState *s = opaque; |
| 87 | uint32_t value; |
| 88 | |
blueswir1 | 68af3f2 | 2009-02-07 10:48:26 +0000 | [diff] [blame] | 89 | addr = (addr >> s->it_shift) & (s->size - 1); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 90 | value = s->data[addr]; |
blueswir1 | ea026b2 | 2008-12-24 09:38:16 +0000 | [diff] [blame] | 91 | NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 92 | |
| 93 | return value; |
| 94 | } |
| 95 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 96 | static CPUWriteMemoryFunc * const nvram_write[] = { |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 97 | &macio_nvram_writeb, |
| 98 | &macio_nvram_writeb, |
| 99 | &macio_nvram_writeb, |
| 100 | }; |
| 101 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 102 | static CPUReadMemoryFunc * const nvram_read[] = { |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 103 | &macio_nvram_readb, |
| 104 | &macio_nvram_readb, |
| 105 | &macio_nvram_readb, |
| 106 | }; |
| 107 | |
blueswir1 | 9b64997 | 2008-12-30 19:01:19 +0000 | [diff] [blame] | 108 | static void macio_nvram_save(QEMUFile *f, void *opaque) |
| 109 | { |
| 110 | MacIONVRAMState *s = (MacIONVRAMState *)opaque; |
| 111 | |
| 112 | qemu_put_buffer(f, s->data, s->size); |
| 113 | } |
| 114 | |
| 115 | static int macio_nvram_load(QEMUFile *f, void *opaque, int version_id) |
| 116 | { |
| 117 | MacIONVRAMState *s = (MacIONVRAMState *)opaque; |
| 118 | |
| 119 | if (version_id != 1) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | qemu_get_buffer(f, s->data, s->size); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
blueswir1 | 6e6b736 | 2008-12-28 18:27:10 +0000 | [diff] [blame] | 127 | static void macio_nvram_reset(void *opaque) |
| 128 | { |
| 129 | } |
| 130 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 131 | MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size, |
blueswir1 | 68af3f2 | 2009-02-07 10:48:26 +0000 | [diff] [blame] | 132 | unsigned int it_shift) |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 133 | { |
| 134 | MacIONVRAMState *s; |
j_mayer | 74e9115 | 2007-11-04 01:16:04 +0000 | [diff] [blame] | 135 | |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 136 | s = qemu_mallocz(sizeof(MacIONVRAMState)); |
aurel32 | 3f7cbbb | 2008-12-26 23:05:15 +0000 | [diff] [blame] | 137 | s->data = qemu_mallocz(size); |
j_mayer | 74e9115 | 2007-11-04 01:16:04 +0000 | [diff] [blame] | 138 | s->size = size; |
blueswir1 | 68af3f2 | 2009-02-07 10:48:26 +0000 | [diff] [blame] | 139 | s->it_shift = it_shift; |
aurel32 | 3f7cbbb | 2008-12-26 23:05:15 +0000 | [diff] [blame] | 140 | |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 141 | s->mem_index = cpu_register_io_memory(nvram_read, nvram_write, s); |
j_mayer | 74e9115 | 2007-11-04 01:16:04 +0000 | [diff] [blame] | 142 | *mem_index = s->mem_index; |
blueswir1 | 9b64997 | 2008-12-30 19:01:19 +0000 | [diff] [blame] | 143 | register_savevm("macio_nvram", -1, 1, macio_nvram_save, macio_nvram_load, |
| 144 | s); |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 145 | qemu_register_reset(macio_nvram_reset, s); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 146 | |
| 147 | return s; |
| 148 | } |
| 149 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 150 | void macio_nvram_map (void *opaque, target_phys_addr_t mem_base) |
j_mayer | 74e9115 | 2007-11-04 01:16:04 +0000 | [diff] [blame] | 151 | { |
| 152 | MacIONVRAMState *s; |
| 153 | |
| 154 | s = opaque; |
blueswir1 | 68af3f2 | 2009-02-07 10:48:26 +0000 | [diff] [blame] | 155 | cpu_register_physical_memory(mem_base, s->size << s->it_shift, |
| 156 | s->mem_index); |
j_mayer | 74e9115 | 2007-11-04 01:16:04 +0000 | [diff] [blame] | 157 | } |
| 158 | |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 159 | /* Set up a system OpenBIOS NVRAM partition */ |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 160 | void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len) |
| 161 | { |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 162 | unsigned int i; |
| 163 | uint32_t start = 0, end; |
| 164 | struct OpenBIOS_nvpart_v1 *part_header; |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 165 | |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 166 | // OpenBIOS nvram variables |
| 167 | // Variable partition |
| 168 | part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data; |
| 169 | part_header->signature = OPENBIOS_PART_SYSTEM; |
| 170 | pstrcpy(part_header->name, sizeof(part_header->name), "system"); |
| 171 | |
| 172 | end = start + sizeof(struct OpenBIOS_nvpart_v1); |
| 173 | for (i = 0; i < nb_prom_envs; i++) |
| 174 | end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]); |
| 175 | |
| 176 | // End marker |
| 177 | nvr->data[end++] = '\0'; |
| 178 | |
| 179 | end = start + ((end - start + 15) & ~15); |
aurel32 | bd89f43 | 2008-12-26 23:05:23 +0000 | [diff] [blame] | 180 | /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for |
| 181 | new variables. */ |
| 182 | if (end < DEF_SYSTEM_SIZE) |
| 183 | end = DEF_SYSTEM_SIZE; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 184 | OpenBIOS_finish_partition(part_header, end - start); |
| 185 | |
| 186 | // free partition |
| 187 | start = end; |
| 188 | part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start]; |
| 189 | part_header->signature = OPENBIOS_PART_FREE; |
| 190 | pstrcpy(part_header->name, sizeof(part_header->name), "free"); |
| 191 | |
| 192 | end = len; |
| 193 | OpenBIOS_finish_partition(part_header, end - start); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame] | 194 | } |