1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2015-2021 Paul Cercueil <paul@crapouillou.net>
6 #include "blockcache.h"
8 #include "lightrec-private.h"
9 #include "memmanager.h"
15 /* Must be power of two */
16 #define LUT_SIZE 0x4000
19 struct lightrec_state *state;
20 struct block * lut[LUT_SIZE];
23 u16 lightrec_get_lut_entry(const struct block *block)
25 return (kunseg(block->pc) >> 2) & (LUT_SIZE - 1);
28 struct block * lightrec_find_block(struct blockcache *cache, u32 pc)
34 for (block = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
35 block; block = block->next)
36 if (kunseg(block->pc) == pc)
42 struct block * lightrec_find_block_from_lut(struct blockcache *cache,
43 u16 lut_entry, u32 addr_in_block)
48 addr_in_block = kunseg(addr_in_block);
50 for (block = cache->lut[lut_entry]; block; block = block->next) {
51 pc = kunseg(block->pc);
52 if (addr_in_block >= pc &&
53 addr_in_block < pc + (block->nb_ops << 2))
60 void remove_from_code_lut(struct blockcache *cache, struct block *block)
62 struct lightrec_state *state = cache->state;
63 u32 offset = lut_offset(block->pc);
65 if (block->function) {
66 memset(lut_address(state, offset), 0,
67 block->nb_ops * lut_elm_size(state));
71 void lightrec_register_block(struct blockcache *cache, struct block *block)
73 u32 pc = kunseg(block->pc);
76 old = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
80 cache->lut[(pc >> 2) & (LUT_SIZE - 1)] = block;
82 remove_from_code_lut(cache, block);
85 void lightrec_unregister_block(struct blockcache *cache, struct block *block)
87 u32 pc = kunseg(block->pc);
88 struct block *old = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
91 cache->lut[(pc >> 2) & (LUT_SIZE - 1)] = old->next;
95 for (; old; old = old->next) {
96 if (old->next == block) {
97 old->next = block->next;
102 pr_err("Block at PC 0x%x is not in cache\n", block->pc);
105 static bool lightrec_block_is_old(const struct lightrec_state *state,
106 const struct block *block)
108 u32 diff = state->current_cycle - block->precompile_date;
110 return diff > (1 << 27); /* About 4 seconds */
113 static void lightrec_free_blocks(struct blockcache *cache,
114 const struct block *except, bool all)
116 struct lightrec_state *state = cache->state;
117 struct block *block, *next;
121 for (i = 0; i < LUT_SIZE; i++) {
122 for (block = cache->lut[i]; block; block = next) {
125 if (except && block == except)
129 outdated = lightrec_block_is_old(state, block) ||
130 lightrec_block_is_outdated(state, block);
134 pr_debug("Freeing outdated block at PC 0x%08x\n", block->pc);
135 remove_from_code_lut(cache, block);
136 lightrec_unregister_block(cache, block);
137 lightrec_free_block(state, block);
143 void lightrec_remove_outdated_blocks(struct blockcache *cache,
144 const struct block *except)
146 pr_info("Running out of code space. Cleaning block cache...\n");
148 lightrec_free_blocks(cache, except, false);
151 void lightrec_free_block_cache(struct blockcache *cache)
153 lightrec_free_blocks(cache, NULL, true);
154 lightrec_free(cache->state, MEM_FOR_LIGHTREC, sizeof(*cache), cache);
157 struct blockcache * lightrec_blockcache_init(struct lightrec_state *state)
159 struct blockcache *cache;
161 cache = lightrec_calloc(state, MEM_FOR_LIGHTREC, sizeof(*cache));
165 cache->state = state;
170 u32 lightrec_calculate_block_hash(const struct block *block)
172 const u32 *code = block->code;
173 u32 hash = 0xffffffff;
176 /* Jenkins one-at-a-time hash algorithm */
177 for (i = 0; i < block->nb_ops; i++) {
179 hash += (hash << 10);
184 hash ^= (hash >> 11);
185 hash += (hash << 15);
190 bool lightrec_block_is_outdated(struct lightrec_state *state, struct block *block)
192 u32 offset = lut_offset(block->pc);
196 if (lut_read(state, offset))
199 outdated = block->hash != lightrec_calculate_block_hash(block);
200 if (likely(!outdated)) {
201 /* The block was marked as outdated, but the content is still
204 addr = block->function;
206 addr = state->get_next_block;
208 lut_write(state, offset, addr);