|
| 1 | +/* Copyright (c) 2025 Intel Corporation. All rights reserved. |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | + |
| 5 | +#include <adsp_memory.h> |
| 6 | +#include <adsp_debug_window.h> |
| 7 | +#include <zephyr/cache.h> |
| 8 | + |
| 9 | +#include <zephyr/logging/log.h> |
| 10 | +LOG_MODULE_REGISTER(debug_window, CONFIG_SOC_LOG_LEVEL); |
| 11 | + |
| 12 | +struct adsp_debug_window { |
| 13 | + struct adsp_dw_desc descs[ADSP_DW_DESC_COUNT]; |
| 14 | + uint8_t reserved[ADSP_DW_PAGE0_SLOT_OFFSET - |
| 15 | + ADSP_DW_DESC_COUNT * sizeof(struct adsp_dw_desc)]; |
| 16 | + uint8_t partial_page0[ADSP_DW_SLOT_SIZE - ADSP_DW_PAGE0_SLOT_OFFSET]; |
| 17 | + uint8_t slots[ADSP_DW_SLOT_COUNT][ADSP_DW_SLOT_SIZE]; |
| 18 | +} __packed; |
| 19 | + |
| 20 | +#define WIN2_MBASE DT_REG_ADDR(DT_PHANDLE(DT_NODELABEL(mem_window2), memory)) |
| 21 | +#define WIN2_SLOTS ((HP_SRAM_WIN2_SIZE / ADSP_DW_PAGE_SIZE) - 1) |
| 22 | +#define ADSP_DW_FULL_SLOTS MIN(WIN2_SLOTS, ADSP_DW_SLOT_COUNT - 1) |
| 23 | + |
| 24 | +#define ADSP_DW ((volatile struct adsp_debug_window *) \ |
| 25 | + (sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *) \ |
| 26 | + (WIN2_MBASE + WIN2_OFFSET)))) |
| 27 | + |
| 28 | +static int adsp_dw_find_slot(uint32_t type, bool prefer_partial) |
| 29 | +{ |
| 30 | + int i; |
| 31 | + |
| 32 | + if (prefer_partial) { |
| 33 | + if (ADSP_DW->descs[ADSP_DW_SLOT_COUNT].type == type) { |
| 34 | + return ADSP_DW_SLOT_COUNT; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + for (i = 0; i < ADSP_DW_FULL_SLOTS; i++) { |
| 39 | + if (ADSP_DW->descs[i].type == type) { |
| 40 | + return i; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (!prefer_partial) { |
| 45 | + if (ADSP_DW->descs[ADSP_DW_SLOT_COUNT].type == type) { |
| 46 | + return ADSP_DW_SLOT_COUNT; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return -ENOENT; |
| 51 | +} |
| 52 | + |
| 53 | +volatile void *adsp_dw_request_slot(struct adsp_dw_desc *dw_desc, size_t *slot_size) |
| 54 | +{ |
| 55 | + int slot_idx; |
| 56 | + |
| 57 | + if (!dw_desc->type) { |
| 58 | + return NULL; |
| 59 | + } |
| 60 | + |
| 61 | + /* Check if a slot have been allocated for this type */ |
| 62 | + slot_idx = adsp_dw_find_slot(dw_desc->type, !!slot_size); |
| 63 | + if (slot_idx < 0) { |
| 64 | + /* Look for an empty slot */ |
| 65 | + slot_idx = adsp_dw_find_slot(ADSP_DW_SLOT_UNUSED, !!slot_size); |
| 66 | + if (slot_idx < 0) { |
| 67 | + LOG_INF("No available slot for %#x", dw_desc->type); |
| 68 | + return NULL; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + ADSP_DW->descs[slot_idx].resource_id = dw_desc->resource_id; |
| 73 | + ADSP_DW->descs[slot_idx].type = dw_desc->type; |
| 74 | + ADSP_DW->descs[slot_idx].vma = dw_desc->vma; |
| 75 | + |
| 76 | + if (slot_size) { |
| 77 | + *slot_size = ADSP_DW_SLOT_SIZE; |
| 78 | + } |
| 79 | + |
| 80 | + if (slot_idx == ADSP_DW_SLOT_COUNT) { |
| 81 | + if (slot_size) { |
| 82 | + *slot_size -= ADSP_DW_PAGE0_SLOT_OFFSET; |
| 83 | + } |
| 84 | + |
| 85 | + LOG_DBG("Allocating partial page0 to be used for %#x", dw_desc->type); |
| 86 | + |
| 87 | + return ADSP_DW->partial_page0; |
| 88 | + } |
| 89 | + |
| 90 | + LOG_DBG("Allocating debug slot %d to be used for %#x", slot_idx, dw_desc->type); |
| 91 | + |
| 92 | + return ADSP_DW->slots[slot_idx]; |
| 93 | +} |
| 94 | + |
| 95 | +volatile void *adsp_dw_seize_slot(uint32_t slot_index, struct adsp_dw_desc *dw_desc, |
| 96 | + size_t *slot_size) |
| 97 | +{ |
| 98 | + if ((slot_index >= ADSP_DW_FULL_SLOTS && slot_index != ADSP_DW_SLOT_COUNT) || |
| 99 | + !dw_desc->type) { |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + |
| 103 | + if (slot_index < ADSP_DW_FULL_SLOTS) { |
| 104 | + if (ADSP_DW->descs[slot_index].type != ADSP_DW_SLOT_UNUSED) { |
| 105 | + LOG_INF("Overtaking debug slot %d, used by %#x for %#x", slot_index, |
| 106 | + ADSP_DW->descs[slot_index].type, dw_desc->type); |
| 107 | + } |
| 108 | + |
| 109 | + if (slot_size) { |
| 110 | + *slot_size = ADSP_DW_SLOT_SIZE; |
| 111 | + } |
| 112 | + |
| 113 | + ADSP_DW->descs[slot_index].resource_id = dw_desc->resource_id; |
| 114 | + ADSP_DW->descs[slot_index].type = dw_desc->type; |
| 115 | + ADSP_DW->descs[slot_index].vma = dw_desc->vma; |
| 116 | + |
| 117 | + return ADSP_DW->slots[slot_index]; |
| 118 | + } |
| 119 | + |
| 120 | + /* The caller is not prepared to handle partial debug slot */ |
| 121 | + if (!slot_size) { |
| 122 | + return NULL; |
| 123 | + } |
| 124 | + |
| 125 | + if (ADSP_DW->descs[slot_index].type != ADSP_DW_SLOT_UNUSED) { |
| 126 | + LOG_INF("Overtaking partial page0, used by %#x for %#x", |
| 127 | + ADSP_DW->descs[slot_index].type, dw_desc->type); |
| 128 | + } |
| 129 | + |
| 130 | + *slot_size = ADSP_DW_SLOT_SIZE - ADSP_DW_PAGE0_SLOT_OFFSET; |
| 131 | + |
| 132 | + ADSP_DW->descs[slot_index].resource_id = dw_desc->resource_id; |
| 133 | + ADSP_DW->descs[slot_index].type = dw_desc->type; |
| 134 | + ADSP_DW->descs[slot_index].vma = dw_desc->vma; |
| 135 | + |
| 136 | + return ADSP_DW->partial_page0; |
| 137 | +} |
| 138 | + |
| 139 | +void adsp_dw_release_slot(uint32_t type) |
| 140 | +{ |
| 141 | + int slot_idx; |
| 142 | + |
| 143 | + slot_idx = adsp_dw_find_slot(type, true); |
| 144 | + |
| 145 | + if (slot_idx < 0) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + if (slot_idx == ADSP_DW_SLOT_COUNT) { |
| 150 | + LOG_DBG("Releasing partial page0 used by %#x", type); |
| 151 | + } else { |
| 152 | + LOG_DBG("Releasing debug slot %d used by %#x", slot_idx, type); |
| 153 | + } |
| 154 | + |
| 155 | + ADSP_DW->descs[slot_idx].resource_id = 0; |
| 156 | + ADSP_DW->descs[slot_idx].type = ADSP_DW_SLOT_UNUSED; |
| 157 | + ADSP_DW->descs[slot_idx].vma = 0; |
| 158 | +} |
0 commit comments