pflash_cfi01: Drop unused 'bypass' field
[qemu/ar7.git] / page_cache.c
blob938a79c9ea7999b4a46a639c35ff27bc639e6892
1 /*
2 * Page cache for QEMU
3 * The cache is base on a hash of the page address
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
7 * Authors:
8 * Orit Wasserman <owasserm@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <strings.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <stdbool.h>
23 #include <glib.h>
24 #include <strings.h>
26 #include "qemu-common.h"
27 #include "migration/page_cache.h"
29 #ifdef DEBUG_CACHE
30 #define DPRINTF(fmt, ...) \
31 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
32 #else
33 #define DPRINTF(fmt, ...) \
34 do { } while (0)
35 #endif
37 typedef struct CacheItem CacheItem;
39 struct CacheItem {
40 uint64_t it_addr;
41 uint64_t it_age;
42 uint8_t *it_data;
45 struct PageCache {
46 CacheItem *page_cache;
47 unsigned int page_size;
48 int64_t max_num_items;
49 uint64_t max_item_age;
50 int64_t num_items;
53 PageCache *cache_init(int64_t num_pages, unsigned int page_size)
55 int64_t i;
57 PageCache *cache;
59 if (num_pages <= 0) {
60 DPRINTF("invalid number of pages\n");
61 return NULL;
64 cache = g_malloc(sizeof(*cache));
66 /* round down to the nearest power of 2 */
67 if (!is_power_of_2(num_pages)) {
68 num_pages = pow2floor(num_pages);
69 DPRINTF("rounding down to %" PRId64 "\n", num_pages);
71 cache->page_size = page_size;
72 cache->num_items = 0;
73 cache->max_item_age = 0;
74 cache->max_num_items = num_pages;
76 DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
78 cache->page_cache = g_malloc((cache->max_num_items) *
79 sizeof(*cache->page_cache));
81 for (i = 0; i < cache->max_num_items; i++) {
82 cache->page_cache[i].it_data = NULL;
83 cache->page_cache[i].it_age = 0;
84 cache->page_cache[i].it_addr = -1;
87 return cache;
90 void cache_fini(PageCache *cache)
92 int64_t i;
94 g_assert(cache);
95 g_assert(cache->page_cache);
97 for (i = 0; i < cache->max_num_items; i++) {
98 g_free(cache->page_cache[i].it_data);
101 g_free(cache->page_cache);
102 cache->page_cache = NULL;
105 static size_t cache_get_cache_pos(const PageCache *cache,
106 uint64_t address)
108 size_t pos;
110 g_assert(cache->max_num_items);
111 pos = (address / cache->page_size) & (cache->max_num_items - 1);
112 return pos;
115 bool cache_is_cached(const PageCache *cache, uint64_t addr)
117 size_t pos;
119 g_assert(cache);
120 g_assert(cache->page_cache);
122 pos = cache_get_cache_pos(cache, addr);
124 return (cache->page_cache[pos].it_addr == addr);
127 static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
129 size_t pos;
131 g_assert(cache);
132 g_assert(cache->page_cache);
134 pos = cache_get_cache_pos(cache, addr);
136 return &cache->page_cache[pos];
139 uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
141 return cache_get_by_addr(cache, addr)->it_data;
144 void cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata)
147 CacheItem *it = NULL;
149 g_assert(cache);
150 g_assert(cache->page_cache);
152 /* actual update of entry */
153 it = cache_get_by_addr(cache, addr);
155 /* free old cached data if any */
156 g_free(it->it_data);
158 if (!it->it_data) {
159 cache->num_items++;
162 it->it_data = g_memdup(pdata, cache->page_size);
163 it->it_age = ++cache->max_item_age;
164 it->it_addr = addr;
167 int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
169 PageCache *new_cache;
170 int64_t i;
172 CacheItem *old_it, *new_it;
174 g_assert(cache);
176 /* cache was not inited */
177 if (cache->page_cache == NULL) {
178 return -1;
181 /* same size */
182 if (pow2floor(new_num_pages) == cache->max_num_items) {
183 return cache->max_num_items;
186 new_cache = cache_init(new_num_pages, cache->page_size);
187 if (!(new_cache)) {
188 DPRINTF("Error creating new cache\n");
189 return -1;
192 /* move all data from old cache */
193 for (i = 0; i < cache->max_num_items; i++) {
194 old_it = &cache->page_cache[i];
195 if (old_it->it_addr != -1) {
196 /* check for collision, if there is, keep MRU page */
197 new_it = cache_get_by_addr(new_cache, old_it->it_addr);
198 if (new_it->it_data && new_it->it_age >= old_it->it_age) {
199 /* keep the MRU page */
200 g_free(old_it->it_data);
201 } else {
202 if (!new_it->it_data) {
203 new_cache->num_items++;
205 g_free(new_it->it_data);
206 new_it->it_data = old_it->it_data;
207 new_it->it_age = old_it->it_age;
208 new_it->it_addr = old_it->it_addr;
213 g_free(cache->page_cache);
214 cache->page_cache = new_cache->page_cache;
215 cache->max_num_items = new_cache->max_num_items;
216 cache->num_items = new_cache->num_items;
218 g_free(new_cache);
220 return cache->max_num_items;