|
| 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 | + |
| 10 | +#include <zephyr/logging/log.h> |
| 11 | +LOG_MODULE_REGISTER(debug_window, CONFIG_SOC_LOG_LEVEL); |
| 12 | + |
| 13 | +struct adsp_debug_window { |
| 14 | + struct adsp_dw_desc descs[ADSP_DW_DESC_COUNT]; |
| 15 | + uint8_t reserved[ADSP_DW_PAGE0_SLOT_OFFSET- 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 | + |
| 23 | +#define ADSP_DW ((volatile struct adsp_debug_window *) \ |
| 24 | + (sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *) \ |
| 25 | + (WIN2_MBASE + WIN2_OFFSET)))) |
| 26 | + |
| 27 | +volatile void *adsp_dw_request_slot(struct adsp_dw_desc *dw_desc, size_t *slot_size) |
| 28 | +{ |
| 29 | + int i; |
| 30 | + |
| 31 | + if (!dw_desc->type) { |
| 32 | + return NULL; |
| 33 | + } |
| 34 | + |
| 35 | + /* Try to use partial slot if caller is prepared to handle it - slot_size pointer is provided */ |
| 36 | + if (slot_size) { |
| 37 | + if (ADSP_DW->descs[ADSP_DW_SLOT_COUNT].type == ADSP_DW_SLOT_UNUSED || |
| 38 | + ADSP_DW->descs[ADSP_DW_SLOT_COUNT].type == dw_desc->type) { |
| 39 | + i = ADSP_DW_SLOT_COUNT; |
| 40 | + goto found; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /* Look for an already allocated slot with the same type */ |
| 45 | + for (i = 0; i < WIN2_SLOTS; i++) { |
| 46 | + if (ADSP_DW->descs[i].type == dw_desc->type) { |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + if (i == WIN2_SLOTS) { |
| 51 | + /* Look for an empty full slot */ |
| 52 | + for (i = 0; i < WIN2_SLOTS; i++) { |
| 53 | + if (ADSP_DW->descs[i].type == ADSP_DW_SLOT_UNUSED) { |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (i == WIN2_SLOTS) { |
| 60 | + LOG_INF("No available slot for %#x", dw_desc->type); |
| 61 | + return NULL; |
| 62 | + } |
| 63 | + |
| 64 | +found: |
| 65 | + ADSP_DW->descs[i].resource_id = dw_desc->resource_id; |
| 66 | + ADSP_DW->descs[i].type = dw_desc->type; |
| 67 | + ADSP_DW->descs[i].vma = dw_desc->vma; |
| 68 | + |
| 69 | + if (slot_size) { |
| 70 | + *slot_size = ADSP_DW_SLOT_SIZE; |
| 71 | + } |
| 72 | + |
| 73 | + if (i == ADSP_DW_SLOT_COUNT) { |
| 74 | + if (slot_size) { |
| 75 | + *slot_size -= ADSP_DW_PAGE0_SLOT_OFFSET; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + LOG_DBG("Allocating partial page0 to be used for %#x", dw_desc->type); |
| 80 | + |
| 81 | + return ADSP_DW->partial_page0; |
| 82 | + } |
| 83 | + |
| 84 | + LOG_DBG("Allocating debug slot %d to be used for %#x", i, dw_desc->type); |
| 85 | + |
| 86 | + return ADSP_DW->slots[i]; |
| 87 | +} |
| 88 | + |
| 89 | +volatile void *adsp_dw_seize_slot(uint32_t slot_index, struct adsp_dw_desc *dw_desc, size_t *slot_size) |
| 90 | +{ |
| 91 | + if ((slot_index >= WIN2_SLOTS && slot_index != ADSP_DW_SLOT_COUNT) || !dw_desc->type) { |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + |
| 95 | + if (slot_index < WIN2_SLOTS) { |
| 96 | + if (ADSP_DW->descs[slot_index].type != ADSP_DW_SLOT_UNUSED) { |
| 97 | + LOG_INF("Overtaking debug slot %d, used by %#x for %#x", slot_index, |
| 98 | + ADSP_DW->descs[slot_index].type, dw_desc->type); |
| 99 | + } |
| 100 | + |
| 101 | + if (slot_size) { |
| 102 | + *slot_size = ADSP_DW_SLOT_SIZE; |
| 103 | + } |
| 104 | + |
| 105 | + ADSP_DW->descs[slot_index].resource_id = dw_desc->resource_id; |
| 106 | + ADSP_DW->descs[slot_index].type = dw_desc->type; |
| 107 | + ADSP_DW->descs[slot_index].vma = dw_desc->vma; |
| 108 | + |
| 109 | + return ADSP_DW->slots[slot_index]; |
| 110 | + } |
| 111 | + |
| 112 | + /* The caller is not prepared to handle partial debug slot */ |
| 113 | + if (!slot_size) { |
| 114 | + return NULL; |
| 115 | + } |
| 116 | + |
| 117 | + if (ADSP_DW->descs[slot_index].type != ADSP_DW_SLOT_UNUSED) { |
| 118 | + LOG_INF("Overtaking partial page0, used by %#x for %#x", |
| 119 | + ADSP_DW->descs[slot_index].type, dw_desc->type); |
| 120 | + } |
| 121 | + |
| 122 | + *slot_size = ADSP_DW_SLOT_SIZE - ADSP_DW_PAGE0_SLOT_OFFSET; |
| 123 | + |
| 124 | + ADSP_DW->descs[slot_index].resource_id = dw_desc->resource_id; |
| 125 | + ADSP_DW->descs[slot_index].type = dw_desc->type; |
| 126 | + ADSP_DW->descs[slot_index].vma = dw_desc->vma; |
| 127 | + |
| 128 | + return ADSP_DW->partial_page0; |
| 129 | +} |
| 130 | + |
| 131 | +void adsp_dw_release_slot(void *slot_address) |
| 132 | +{ |
| 133 | + int i; |
| 134 | + |
| 135 | + for (i = 0; i < WIN2_SLOTS; i++) { |
| 136 | + if (ADSP_DW->slots[i] == slot_address) { |
| 137 | + LOG_DBG("Releasing debug slot %d used by %#x", i, ADSP_DW->descs[i].type); |
| 138 | + goto found; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (ADSP_DW->partial_page0 == slot_address) { |
| 143 | + i = ADSP_DW_SLOT_COUNT; |
| 144 | + LOG_DBG("Releasing partial page0 used by %#x", ADSP_DW->descs[i].type); |
| 145 | + goto found; |
| 146 | + } |
| 147 | + |
| 148 | + return; |
| 149 | + |
| 150 | +found: |
| 151 | + ADSP_DW->descs[i].resource_id = 0; |
| 152 | + ADSP_DW->descs[i].type = ADSP_DW_SLOT_UNUSED; |
| 153 | + ADSP_DW->descs[i].vma = 0; |
| 154 | +} |
0 commit comments