| 1 | /* |
| 2 | * Memory access templates for MemoryRegionCache |
| 3 | * |
| 4 | * Copyright (c) 2018 Red Hat, Inc. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #define ADDRESS_SPACE_LD_CACHED(size) \ |
| 21 | glue(glue(address_space_ld, size), glue(ENDIANNESS, _cached)) |
| 22 | #define ADDRESS_SPACE_LD_CACHED_SLOW(size) \ |
| 23 | glue(glue(address_space_ld, size), glue(ENDIANNESS, _cached_slow)) |
| 24 | #define LD_P(size) \ |
| 25 | glue(glue(ld, size), glue(ENDIANNESS, _p)) |
| 26 | |
| 27 | static inline |
| 28 | uint16_t ADDRESS_SPACE_LD_CACHED(uw)(const MemoryRegionCache *cache, |
| 29 | hwaddr addr, MemTxAttrs attrs, MemTxResult *result) |
| 30 | { |
| 31 | assert(addr < cache->len && 2 <= cache->len - addr); |
| 32 | fuzz_dma_read_cb(cache->xlat + addr, 2, cache->mrs.mr); |
| 33 | if (likely(cache->ptr)) { |
| 34 | return LD_P(uw)(cache->ptr + addr); |
| 35 | } else { |
| 36 | return ADDRESS_SPACE_LD_CACHED_SLOW(uw)(cache, addr, attrs, result); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static inline |
| 41 | uint32_t ADDRESS_SPACE_LD_CACHED(l)(const MemoryRegionCache *cache, |
| 42 | hwaddr addr, MemTxAttrs attrs, MemTxResult *result) |
| 43 | { |
| 44 | assert(addr < cache->len && 4 <= cache->len - addr); |
| 45 | fuzz_dma_read_cb(cache->xlat + addr, 4, cache->mrs.mr); |
| 46 | if (likely(cache->ptr)) { |
| 47 | return LD_P(l)(cache->ptr + addr); |
| 48 | } else { |
| 49 | return ADDRESS_SPACE_LD_CACHED_SLOW(l)(cache, addr, attrs, result); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static inline |
| 54 | uint64_t ADDRESS_SPACE_LD_CACHED(q)(const MemoryRegionCache *cache, |
| 55 | hwaddr addr, MemTxAttrs attrs, MemTxResult *result) |
| 56 | { |
| 57 | assert(addr < cache->len && 8 <= cache->len - addr); |
| 58 | fuzz_dma_read_cb(cache->xlat + addr, 8, cache->mrs.mr); |
| 59 | if (likely(cache->ptr)) { |
| 60 | return LD_P(q)(cache->ptr + addr); |
| 61 | } else { |
| 62 | return ADDRESS_SPACE_LD_CACHED_SLOW(q)(cache, addr, attrs, result); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #undef ADDRESS_SPACE_LD_CACHED |
| 67 | #undef ADDRESS_SPACE_LD_CACHED_SLOW |
| 68 | #undef LD_P |
| 69 | |
| 70 | #define ADDRESS_SPACE_ST_CACHED(size) \ |
| 71 | glue(glue(address_space_st, size), glue(ENDIANNESS, _cached)) |
| 72 | #define ADDRESS_SPACE_ST_CACHED_SLOW(size) \ |
| 73 | glue(glue(address_space_st, size), glue(ENDIANNESS, _cached_slow)) |
| 74 | #define ST_P(size) \ |
| 75 | glue(glue(st, size), glue(ENDIANNESS, _p)) |
| 76 | |
| 77 | static inline void ADDRESS_SPACE_ST_CACHED(w)(const MemoryRegionCache *cache, |
| 78 | hwaddr addr, uint16_t val, MemTxAttrs attrs, MemTxResult *result) |
| 79 | { |
| 80 | assert(addr < cache->len && 2 <= cache->len - addr); |
| 81 | if (likely(cache->ptr)) { |
| 82 | ST_P(w)(cache->ptr + addr, val); |
| 83 | } else { |
| 84 | ADDRESS_SPACE_ST_CACHED_SLOW(w)(cache, addr, val, attrs, result); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static inline void ADDRESS_SPACE_ST_CACHED(l)(const MemoryRegionCache *cache, |
| 89 | hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result) |
| 90 | { |
| 91 | assert(addr < cache->len && 4 <= cache->len - addr); |
| 92 | if (likely(cache->ptr)) { |
| 93 | ST_P(l)(cache->ptr + addr, val); |
| 94 | } else { |
| 95 | ADDRESS_SPACE_ST_CACHED_SLOW(l)(cache, addr, val, attrs, result); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static inline void ADDRESS_SPACE_ST_CACHED(q)(const MemoryRegionCache *cache, |
| 100 | hwaddr addr, uint64_t val, MemTxAttrs attrs, MemTxResult *result) |
| 101 | { |
| 102 | assert(addr < cache->len && 8 <= cache->len - addr); |
| 103 | if (likely(cache->ptr)) { |
| 104 | ST_P(q)(cache->ptr + addr, val); |
| 105 | } else { |
| 106 | ADDRESS_SPACE_ST_CACHED_SLOW(q)(cache, addr, val, attrs, result); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #undef ADDRESS_SPACE_ST_CACHED |
| 111 | #undef ADDRESS_SPACE_ST_CACHED_SLOW |
| 112 | #undef ST_P |
| 113 | |
| 114 | #undef ENDIANNESS |