|
| 1 | +/* |
| 2 | + * ESP Display RGB emulation |
| 3 | + * |
| 4 | + * Copyright (c) 2023 Espressif Systems (Shanghai) Co. Ltd. |
| 5 | + * |
| 6 | + * This implementation overrides the ESP32 UARt one, check it out first. |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License version 2 or |
| 10 | + * (at your option) any later version. |
| 11 | + */ |
| 12 | + |
| 13 | +#include "qemu/osdep.h" |
| 14 | +#include "qemu/module.h" |
| 15 | +#include "qapi/error.h" |
| 16 | +#include "sysemu/sysemu.h" |
| 17 | +#include "hw/sysbus.h" |
| 18 | +#include "hw/irq.h" |
| 19 | +#include "hw/display/esp_rgb.h" |
| 20 | +#include "ui/console.h" |
| 21 | +#include "qemu/error-report.h" |
| 22 | +#include "sysemu/dma.h" |
| 23 | + |
| 24 | +#define RGB_WARNING 1 |
| 25 | +#define RGB_DEBUG 0 |
| 26 | + |
| 27 | +#define RGB_VERSION_MAJOR 0 |
| 28 | +#define RGB_VERSION_MINOR 1 |
| 29 | + |
| 30 | +static uint64_t esp_rgb_read(void *opaque, hwaddr addr, unsigned int size) |
| 31 | +{ |
| 32 | + ESPRgbState *s = ESP_RGB(opaque); |
| 33 | + uint32_t r = 0; |
| 34 | + |
| 35 | + /* Only accept 32-bit transactions */ |
| 36 | + if (size != sizeof(uint32_t)) { |
| 37 | + return r; |
| 38 | + } |
| 39 | + |
| 40 | + switch(addr) { |
| 41 | + case A_RGB_VERSION: |
| 42 | + r = FIELD_DP32(r, RGB_VERSION, MAJOR, RGB_VERSION_MAJOR); |
| 43 | + r = FIELD_DP32(r, RGB_VERSION, MINOR, RGB_VERSION_MINOR); |
| 44 | + break; |
| 45 | + |
| 46 | + case A_RGB_WIN_SIZE: |
| 47 | + r = FIELD_DP32(r, RGB_WIN_SIZE, WIDTH, s->width); |
| 48 | + r = FIELD_DP32(r, RGB_WIN_SIZE, HEIGHT, s->height); |
| 49 | + break; |
| 50 | + |
| 51 | + case A_RGB_UPDATE_FROM: |
| 52 | + r = FIELD_DP32(r, RGB_UPDATE_FROM, X, s->from_x); |
| 53 | + r = FIELD_DP32(r, RGB_UPDATE_FROM, Y, s->from_y); |
| 54 | + break; |
| 55 | + |
| 56 | + case A_RGB_UPDATE_TO: |
| 57 | + r = FIELD_DP32(r, RGB_UPDATE_TO, X, s->to_x); |
| 58 | + r = FIELD_DP32(r, RGB_UPDATE_TO, Y, s->to_y); |
| 59 | + break; |
| 60 | + |
| 61 | + case A_RGB_UPDATE_CONTENT: |
| 62 | + r = s->color_content; |
| 63 | + break; |
| 64 | + |
| 65 | + case A_RGB_UPDATE_STATUS: |
| 66 | + r = s->update_area; |
| 67 | + break; |
| 68 | + |
| 69 | + default: |
| 70 | +#if RGB_WARNING |
| 71 | + warn_report("[ESP RGB] Unsupported read to 0x%lx", addr); |
| 72 | +#endif |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | +#if RGB_DEBUG |
| 77 | + info_report("[ESP RGB] Reading 0x%lx (0x%x)", addr, r); |
| 78 | +#endif |
| 79 | + |
| 80 | + return r; |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +static void esp_rgb_write(void *opaque, hwaddr addr, |
| 85 | + uint64_t value, unsigned int size) |
| 86 | +{ |
| 87 | + ESPRgbState *s = ESP_RGB(opaque); |
| 88 | + uint32_t width = 0; |
| 89 | + uint32_t height = 0; |
| 90 | + |
| 91 | + /* Only accept 32-bit transactions */ |
| 92 | + if (size != sizeof(uint32_t)) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | +#if RGB_DEBUG |
| 97 | + info_report("[ESP RGB] Writing 0x%lx = %08lx", addr, value); |
| 98 | +#endif |
| 99 | + |
| 100 | + switch(addr) { |
| 101 | + case A_RGB_WIN_SIZE: |
| 102 | + width = FIELD_EX32(value, RGB_WIN_SIZE, WIDTH); |
| 103 | + height = FIELD_EX32(value, RGB_WIN_SIZE, HEIGHT); |
| 104 | + |
| 105 | + /* Check the bounds of the new window size */ |
| 106 | + s->width = MIN(width, ESP_RGB_MAX_WIDTH); |
| 107 | + s->height = MIN(height, ESP_RGB_MAX_HEIGHT); |
| 108 | + /* Make sure it's bigger than 10x10 */ |
| 109 | + s->width = MAX(s->width, 10); |
| 110 | + s->height = MAX(s->height, 10); |
| 111 | + |
| 112 | + /* Update the window size */ |
| 113 | + if (s->con) { |
| 114 | + qemu_console_resize(s->con, s->width, s->height); |
| 115 | + } |
| 116 | + break; |
| 117 | + |
| 118 | + case A_RGB_UPDATE_STATUS: |
| 119 | + s->update_area = FIELD_EX32(value, RGB_UPDATE_STATUS, ENA) != 0; |
| 120 | + break; |
| 121 | + |
| 122 | + case A_RGB_UPDATE_FROM: |
| 123 | + s->from_x = FIELD_EX32(value, RGB_UPDATE_FROM, X); |
| 124 | + s->from_y = FIELD_EX32(value, RGB_UPDATE_FROM, Y); |
| 125 | + break; |
| 126 | + |
| 127 | + case A_RGB_UPDATE_TO: |
| 128 | + s->to_x = FIELD_EX32(value, RGB_UPDATE_TO, X); |
| 129 | + s->to_y = FIELD_EX32(value, RGB_UPDATE_TO, Y); |
| 130 | + break; |
| 131 | + |
| 132 | + case A_RGB_UPDATE_CONTENT: |
| 133 | + s->color_content = (uint32_t) value; |
| 134 | + break; |
| 135 | + |
| 136 | + default: |
| 137 | +#if RGB_WARNING |
| 138 | + warn_report("[ESP RGB] Unsupported write to 0x%lx (%08lx)", addr, value); |
| 139 | +#endif |
| 140 | + break; |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +static void rgb_update(void* opaque) |
| 147 | +{ |
| 148 | + ESPRgbState* s = (ESPRgbState*) opaque; |
| 149 | + |
| 150 | + if (s->con && s->update_area) { |
| 151 | + uint32_t src = s->color_content; |
| 152 | + AddressSpace* src_as = NULL; |
| 153 | + |
| 154 | + /* Since we are in a 32bpp configuration, it's enough to cast the framebuffer |
| 155 | + * as a uint32_t pointer */ |
| 156 | + uint32_t* data = surface_data(qemu_console_surface(s->con)); |
| 157 | + |
| 158 | + /* Width and height of the area to update */ |
| 159 | + const int width = s->to_x - s->from_x; |
| 160 | + const int height = s->to_y - s->from_y; |
| 161 | + const int bytes_per_pixel = sizeof(uint32_t); |
| 162 | + const int total_bytes = width * height * bytes_per_pixel; |
| 163 | + |
| 164 | + if (address_space_access_valid(&s->vram_as, src, total_bytes, false, MEMTXATTRS_UNSPECIFIED)) { |
| 165 | + src_as = &s->vram_as; |
| 166 | + } else if (address_space_access_valid(&s->intram_as, src, total_bytes, false, MEMTXATTRS_UNSPECIFIED)) { |
| 167 | + src_as = &s->intram_as; |
| 168 | + } else { |
| 169 | + s->update_area = false; |
| 170 | +#if RGB_WARNING |
| 171 | + warn_report("[ESP RGB] Invalid color content address or length"); |
| 172 | +#endif |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + /* Only perform the copy if the area is valid */ |
| 177 | + if (width > 0 && height > 0 && |
| 178 | + (s->from_x + width) <= s->width && (s->from_y + height) <= s->height) { |
| 179 | + |
| 180 | + uint32_t* dest = &data[s->from_y * s->width + s->from_x]; |
| 181 | + |
| 182 | + /* Copy the pixels to the framebuffer */ |
| 183 | + for (int i = 0; i < height; i++) { |
| 184 | + dma_memory_read(src_as, src, dest, width * bytes_per_pixel, MEMTXATTRS_UNSPECIFIED); |
| 185 | + /* Go to the next line in the destination */ |
| 186 | + dest += s->width; |
| 187 | + /* Same goes for the source */ |
| 188 | + src += width * bytes_per_pixel; |
| 189 | + } |
| 190 | + |
| 191 | + dpy_gfx_update(s->con, s->from_x, s->from_y, width, height); |
| 192 | + } |
| 193 | +#if RGB_WARNING |
| 194 | + else { |
| 195 | + warn_report("[ESP RGB] Invalid drawing area"); |
| 196 | + } |
| 197 | +#endif |
| 198 | + |
| 199 | + /* Automatically clear the update flag, the guest can re-use the given color_content buffer. |
| 200 | + * It must set it again to trigger another update. */ |
| 201 | + s->update_area = false; |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | + |
| 206 | +static void rgb_invalidate(void *opaque) |
| 207 | +{ |
| 208 | + ESPRgbState* s = (ESPRgbState*) opaque; |
| 209 | + |
| 210 | + if (s->con) { |
| 211 | + uint32_t* data = surface_data(qemu_console_surface(s->con)); |
| 212 | + |
| 213 | + /* On invalidate, reset the display */ |
| 214 | + memset(data, 0, s->width * s->height * 4); |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | + |
| 219 | +static const GraphicHwOps fb_ops = { |
| 220 | + .invalidate = rgb_invalidate, |
| 221 | + .gfx_update = rgb_update |
| 222 | +}; |
| 223 | + |
| 224 | + |
| 225 | +static void esp_rgb_realize(DeviceState *dev, Error **errp) |
| 226 | +{ |
| 227 | + ESPRgbState* s = ESP_RGB(dev); |
| 228 | + |
| 229 | + assert(s->intram != NULL); |
| 230 | + /* Create an address space for internal RAM so that we can read data from it on GUI update */ |
| 231 | + address_space_init(&s->intram_as, s->intram, "esp32c3.rgb.intram_as"); |
| 232 | +} |
| 233 | + |
| 234 | + |
| 235 | +static const MemoryRegionOps esp_rgb_ops = { |
| 236 | + .read = esp_rgb_read, |
| 237 | + .write = esp_rgb_write, |
| 238 | + .endianness = DEVICE_LITTLE_ENDIAN, |
| 239 | +}; |
| 240 | + |
| 241 | + |
| 242 | +static void esp_rgb_init(Object *obj) |
| 243 | +{ |
| 244 | + ESPRgbState* s = ESP_RGB(obj); |
| 245 | + SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
| 246 | + |
| 247 | + memory_region_init_io(&s->iomem, obj, &esp_rgb_ops, s, |
| 248 | + TYPE_ESP_RGB, ESP_RGB_IO_SIZE); |
| 249 | + sysbus_init_mmio(sbd, &s->iomem); |
| 250 | + |
| 251 | + /* Default window size */ |
| 252 | + s->width = ESP_RGB_MAX_WIDTH; |
| 253 | + s->height = ESP_RGB_MAX_HEIGHT; |
| 254 | + s->update_area = false; |
| 255 | + |
| 256 | + if (s->con == NULL) { |
| 257 | + s->con = graphic_console_init(DEVICE(s), 0, &fb_ops, s); |
| 258 | + qemu_console_resize(s->con, s->width, s->height); |
| 259 | + uint32_t* data = surface_data(qemu_console_surface(s->con)); |
| 260 | + /* Initialize the window to black */ |
| 261 | + memset(data, 0, ESP_RGB_MAX_VRAM_SIZE); |
| 262 | + } |
| 263 | + |
| 264 | + /* Create a memory region that can be used as a framebuffer by the guest */ |
| 265 | + memory_region_init_ram(&s->vram, OBJECT(s), "esp32c3-rgb-vram", ESP_RGB_MAX_VRAM_SIZE, &error_abort); |
| 266 | + |
| 267 | + /* Create an AddressSpace out of the MemoryRegion to be able to perform DMA */ |
| 268 | + address_space_init(&s->vram_as, &s->vram, "esp32c3.rgb.vram_as"); |
| 269 | +} |
| 270 | + |
| 271 | + |
| 272 | +static void esp_rgb_reset(DeviceState *dev) |
| 273 | +{ |
| 274 | + ESPRgbState* s = ESP_RGB(dev); |
| 275 | + |
| 276 | + /* Default window size */ |
| 277 | + s->width = ESP_RGB_MAX_WIDTH; |
| 278 | + s->height = ESP_RGB_MAX_HEIGHT; |
| 279 | + s->update_area = false; |
| 280 | + s->from_x = 0; |
| 281 | + s->from_y = 0; |
| 282 | + s->to_x = 0; |
| 283 | + s->to_y = 0; |
| 284 | +} |
| 285 | + |
| 286 | + |
| 287 | +static void esp_rgb_class_init(ObjectClass *klass, void *data) |
| 288 | +{ |
| 289 | + DeviceClass *dc = DEVICE_CLASS(klass); |
| 290 | + |
| 291 | + dc->reset = esp_rgb_reset; |
| 292 | + dc->realize = esp_rgb_realize; |
| 293 | +} |
| 294 | + |
| 295 | +static const TypeInfo esp_rgb_info = { |
| 296 | + .name = TYPE_ESP_RGB, |
| 297 | + .parent = TYPE_SYS_BUS_DEVICE, |
| 298 | + .instance_size = sizeof(ESPRgbState), |
| 299 | + .instance_init = esp_rgb_init, |
| 300 | + .class_init = esp_rgb_class_init, |
| 301 | +}; |
| 302 | + |
| 303 | +static void esp_rgb_register_types(void) |
| 304 | +{ |
| 305 | + type_register_static(&esp_rgb_info); |
| 306 | +} |
| 307 | + |
| 308 | +type_init(esp_rgb_register_types) |
0 commit comments