| 1 | /* |
| 2 | * K230 Decompress Engine |
| 3 | * |
| 4 | * Copyright (c) 2026 Tao Ding <dingtao0430@163.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef HW_MISC_K230_DECOMP_GZIP_H |
| 10 | #define HW_MISC_K230_DECOMP_GZIP_H |
| 11 | |
| 12 | #include <zlib.h> |
| 13 | #include "hw/core/sysbus.h" |
| 14 | |
| 15 | #define TYPE_K230_DECOMP_GZIP "riscv.k230.decomp-gzip" |
| 16 | OBJECT_DECLARE_SIMPLE_TYPE(K230DecompGzipState, K230_DECOMP_GZIP) |
| 17 | |
| 18 | /* K230 DECOMP GZIP ACK input */ |
| 19 | enum { |
| 20 | K230_DECOMP_GZIP_GPIO_DMA_WRITE_ACK, |
| 21 | K230_DECOMP_GZIP_GPIO_DMA_READ_ACK, |
| 22 | K230_DECOMP_GZIP_NUM_GPIOS_IN, |
| 23 | }; |
| 24 | |
| 25 | /* K230 DECOMP GZIP REQ output */ |
| 26 | enum { |
| 27 | K230_DECOMP_GZIP_GPIO_DECOMP_CTRL_EN, |
| 28 | K230_DECOMP_GZIP_GPIO_DMA_WRITE_REQ, |
| 29 | K230_DECOMP_GZIP_GPIO_DMA_READ_REQ, |
| 30 | K230_DECOMP_GZIP_NUM_GPIOS_OUT, |
| 31 | }; |
| 32 | |
| 33 | #define K230_DECOMP_GZIP_MMIO_SIZE 0x4000 |
| 34 | |
| 35 | /* K230 DECOMP GZIP Registers map */ |
| 36 | /* Start decompression controller */ |
| 37 | #define K230_DECOMP_GZIP_DECOMP_START 0x00 |
| 38 | /* Source data length register */ |
| 39 | #define K230_DECOMP_GZIP_GZIP_SRC_SIZE 0x04 |
| 40 | /* Output data length register */ |
| 41 | #define K230_DECOMP_GZIP_GZIP_OUT_SIZE 0x08 |
| 42 | /* Decompress status register */ |
| 43 | #define K230_DECOMP_GZIP_DECOMP_STAT 0x0c |
| 44 | |
| 45 | /* Start decompression controller */ |
| 46 | #define K230_DECOMP_GZIP_START (1U << 0) |
| 47 | |
| 48 | #define K230_DECOMP_GZIP_CTRL_EN (1U << 31) |
| 49 | #define K230_DECOMP_GZIP_DMA_IN_MASK 0x7fffffffU |
| 50 | |
| 51 | #define K230_DECOMP_GZIP_STAT_CRC_OK (1U << 10) |
| 52 | #define K230_DECOMP_GZIP_STAT_STATE_MASK 0xfU |
| 53 | |
| 54 | #define K230_DECOMP_GZIP_BLOCK_SIZE 0x00020000 |
| 55 | #define K230_DECOMP_GZIP_SRAM_IN_BASE 0x80000 |
| 56 | #define K230_DECOMP_GZIP_SRAM_OUT_BASE 0 |
| 57 | |
| 58 | typedef struct K230DecompGzipSlotState { |
| 59 | uint32_t slot; /* Slot index */ |
| 60 | /* |
| 61 | * Data offset in current slot. |
| 62 | * First valid data in input buffer. Last valid data in output buffer. |
| 63 | */ |
| 64 | uint32_t current_offset; |
| 65 | } K230DecompGzipSlotState; |
| 66 | |
| 67 | struct K230DecompGzipState { |
| 68 | SysBusDevice parent_obj; |
| 69 | |
| 70 | MemoryRegion iomem; |
| 71 | hwaddr sram_base; |
| 72 | qemu_irq signal_out[K230_DECOMP_GZIP_NUM_GPIOS_OUT]; |
| 73 | uint32_t decomp_start; |
| 74 | uint32_t gzip_src_size; |
| 75 | uint32_t gzip_out_size; |
| 76 | uint32_t decomp_stat; |
| 77 | K230DecompGzipSlotState input; /* decomp gzip ring input */ |
| 78 | K230DecompGzipSlotState output; /* decomp gzip ring output */ |
| 79 | uint32_t total_requested; |
| 80 | uint32_t total_produced; |
| 81 | bool active; |
| 82 | bool in_kick; |
| 83 | bool zstream_inited; |
| 84 | bool stream_end; |
| 85 | /* Read from ring input. */ |
| 86 | uint8_t input_buf[K230_DECOMP_GZIP_BLOCK_SIZE]; |
| 87 | /* Write to ring output. */ |
| 88 | uint8_t output_buf[K230_DECOMP_GZIP_BLOCK_SIZE]; |
| 89 | z_stream zs; |
| 90 | }; |
| 91 | |
| 92 | #endif |