Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 3 | |
| 4 | #define _GNU_SOURCE |
| 5 | #include <assert.h> |
| 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <getopt.h> |
| 9 | #include <limits.h> |
| 10 | #include <poll.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <syslog.h> |
| 17 | #include <signal.h> |
| 18 | #include <sys/ioctl.h> |
| 19 | #include <sys/mman.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/timerfd.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <time.h> |
| 24 | #include <unistd.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <mtd/mtd-abi.h> |
| 27 | |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 28 | #include "mboxd.h" |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 29 | #include "common.h" |
Andrew Jeffery | 457a6e5 | 2018-08-08 11:21:08 +0930 | [diff] [blame] | 30 | #include "transport_mbox.h" |
Andrew Jeffery | f593b1b | 2018-08-08 11:01:04 +0930 | [diff] [blame] | 31 | #include "windows.h" |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 32 | #include "backend.h" |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 33 | |
| 34 | /* Initialisation Functions */ |
| 35 | |
| 36 | /* |
| 37 | * init_window_state() - Initialise a new window to a known state |
| 38 | * @window: The window to initialise |
| 39 | * @size: The size of the window |
| 40 | */ |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 41 | static void init_window_state(struct window_context *window, uint32_t size) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 42 | { |
| 43 | window->mem = NULL; |
| 44 | window->flash_offset = FLASH_OFFSET_UNINIT; |
| 45 | window->size = size; |
| 46 | window->dirty_bmap = NULL; |
| 47 | window->age = 0; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * init_window_mem() - Divide the reserved memory region among the windows |
| 52 | * @context: The mbox context pointer |
| 53 | * |
| 54 | * Return: 0 on success otherwise negative error code |
| 55 | */ |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 56 | static int init_window_mem(struct mbox_context *context) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 57 | { |
| 58 | void *mem_location = context->mem; |
| 59 | int i; |
| 60 | |
| 61 | /* |
| 62 | * Carve up the reserved memory region and allocate it to each of the |
| 63 | * windows. The windows are placed one after the other in ascending |
| 64 | * order, so the first window will be first in memory and so on. We |
| 65 | * shouldn't have allocated more windows than we have memory, but if we |
| 66 | * did we will error out here |
| 67 | */ |
| 68 | for (i = 0; i < context->windows.num; i++) { |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 69 | uint32_t size = context->windows.window[i].size; |
| 70 | MSG_DBG("Window %d @ %p for size 0x%.8x\n", i, |
| 71 | mem_location, size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 72 | context->windows.window[i].mem = mem_location; |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 73 | mem_location += size; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 74 | if (mem_location > (context->mem + context->mem_size)) { |
| 75 | /* Tried to allocate window past the end of memory */ |
| 76 | MSG_ERR("Total size of windows exceeds reserved mem\n"); |
| 77 | MSG_ERR("Try smaller or fewer windows\n"); |
| 78 | MSG_ERR("Mem size: 0x%.8x\n", context->mem_size); |
| 79 | return -1; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 85 | /* |
Andrew Jeffery | c1a67fa | 2018-08-08 17:07:38 +0930 | [diff] [blame] | 86 | * windows_init() - Initalise the window cache |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 87 | * @context: The mbox context pointer |
| 88 | * |
| 89 | * Return: 0 on success otherwise negative |
| 90 | */ |
Andrew Jeffery | c1a67fa | 2018-08-08 17:07:38 +0930 | [diff] [blame] | 91 | int windows_init(struct mbox_context *context) |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 92 | { |
| 93 | int i; |
| 94 | |
| 95 | /* Check if window size and number set - otherwise set to default */ |
| 96 | if (!context->windows.default_size) { |
| 97 | /* Default to 1MB windows */ |
| 98 | context->windows.default_size = 1 << 20; |
| 99 | } |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 100 | MSG_INFO("Window size: 0x%.8x\n", context->windows.default_size); |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 101 | if (!context->windows.num) { |
| 102 | /* Use the entire reserved memory region by default */ |
| 103 | context->windows.num = context->mem_size / |
| 104 | context->windows.default_size; |
| 105 | } |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 106 | MSG_INFO("Number of windows: %d\n", context->windows.num); |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 107 | |
| 108 | context->windows.window = calloc(context->windows.num, |
| 109 | sizeof(*context->windows.window)); |
| 110 | if (!context->windows.window) { |
| 111 | MSG_ERR("Memory allocation failed\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | for (i = 0; i < context->windows.num; i++) { |
| 116 | init_window_state(&context->windows.window[i], |
| 117 | context->windows.default_size); |
| 118 | } |
| 119 | |
| 120 | return init_window_mem(context); |
| 121 | } |
| 122 | |
| 123 | /* |
Andrew Jeffery | f5f5142 | 2018-08-08 17:08:33 +0930 | [diff] [blame] | 124 | * windows_free() - Free the window cache |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 125 | * @context: The mbox context pointer |
| 126 | */ |
Andrew Jeffery | f5f5142 | 2018-08-08 17:08:33 +0930 | [diff] [blame] | 127 | void windows_free(struct mbox_context *context) |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 128 | { |
| 129 | int i; |
| 130 | |
| 131 | /* Check window cache has actually been allocated */ |
| 132 | if (context->windows.window) { |
| 133 | for (i = 0; i < context->windows.num; i++) { |
| 134 | free(context->windows.window[i].dirty_bmap); |
| 135 | } |
| 136 | free(context->windows.window); |
| 137 | } |
| 138 | } |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 139 | |
| 140 | /* Write from Window Functions */ |
| 141 | |
| 142 | /* |
Andrew Jeffery | 3200c07 | 2018-08-08 17:12:03 +0930 | [diff] [blame] | 143 | * window_flush_v1() - Handle writing when erase and block size differ |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 144 | * @context: The mbox context pointer |
| 145 | * @offset_bytes: The offset in the current window to write from (bytes) |
| 146 | * @count_bytes: Number of bytes to write |
| 147 | * |
Andrew Jeffery | 3200c07 | 2018-08-08 17:12:03 +0930 | [diff] [blame] | 148 | * Handle a window_flush for dirty memory when block_size is less than the |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 149 | * flash erase size |
| 150 | * This requires us to be a bit careful because we might have to erase more |
| 151 | * than we want to write which could result in data loss if we don't have the |
| 152 | * entire portion of flash to be erased already saved in memory (for us to |
| 153 | * write back after the erase) |
| 154 | * |
| 155 | * Return: 0 on success otherwise negative error code |
| 156 | */ |
Andrew Jeffery | 3200c07 | 2018-08-08 17:12:03 +0930 | [diff] [blame] | 157 | int window_flush_v1(struct mbox_context *context, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 158 | uint32_t offset_bytes, uint32_t count_bytes) |
| 159 | { |
| 160 | int rc; |
| 161 | uint32_t flash_offset; |
| 162 | struct window_context low_mem = { 0 }, high_mem = { 0 }; |
| 163 | |
| 164 | /* Find where in phys flash this is based on the window.flash_offset */ |
| 165 | flash_offset = context->current->flash_offset + offset_bytes; |
| 166 | |
| 167 | /* |
| 168 | * low_mem.flash_offset = erase boundary below where we're writing |
| 169 | * low_mem.size = size from low_mem.flash_offset to where we're writing |
| 170 | * |
| 171 | * high_mem.flash_offset = end of where we're writing |
| 172 | * high_mem.size = size from end of where we're writing to next erase |
| 173 | * boundary |
| 174 | */ |
| 175 | low_mem.flash_offset = align_down(flash_offset, |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 176 | 1 << context->backend.erase_size_shift); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 177 | low_mem.size = flash_offset - low_mem.flash_offset; |
| 178 | high_mem.flash_offset = flash_offset + count_bytes; |
| 179 | high_mem.size = align_up(high_mem.flash_offset, |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 180 | 1 << context->backend.erase_size_shift) - |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 181 | high_mem.flash_offset; |
| 182 | |
| 183 | /* |
| 184 | * Check if we already have a copy of the required flash areas in |
| 185 | * memory as part of the existing window |
| 186 | */ |
| 187 | if (low_mem.flash_offset < context->current->flash_offset) { |
| 188 | /* Before the start of our current window */ |
| 189 | low_mem.mem = malloc(low_mem.size); |
| 190 | if (!low_mem.mem) { |
| 191 | MSG_ERR("Unable to allocate memory\n"); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 192 | return -ENOMEM; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 193 | } |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 194 | rc = backend_copy(&context->backend, low_mem.flash_offset, |
| 195 | low_mem.mem, low_mem.size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 196 | if (rc < 0) { |
| 197 | goto out; |
| 198 | } |
| 199 | } |
| 200 | if ((high_mem.flash_offset + high_mem.size) > |
| 201 | (context->current->flash_offset + context->current->size)) { |
| 202 | /* After the end of our current window */ |
| 203 | high_mem.mem = malloc(high_mem.size); |
| 204 | if (!high_mem.mem) { |
| 205 | MSG_ERR("Unable to allocate memory\n"); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 206 | rc = -ENOMEM; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 207 | goto out; |
| 208 | } |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 209 | rc = backend_copy(&context->backend, high_mem.flash_offset, |
| 210 | high_mem.mem, high_mem.size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 211 | if (rc < 0) { |
| 212 | goto out; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * We need to erase the flash from low_mem.flash_offset-> |
| 218 | * high_mem.flash_offset + high_mem.size |
| 219 | */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 220 | rc = backend_erase(&context->backend, low_mem.flash_offset, |
| 221 | (high_mem.flash_offset - low_mem.flash_offset) + |
| 222 | high_mem.size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 223 | if (rc < 0) { |
| 224 | MSG_ERR("Couldn't erase flash\n"); |
| 225 | goto out; |
| 226 | } |
| 227 | |
| 228 | /* Write back over the erased area */ |
| 229 | if (low_mem.mem) { |
| 230 | /* Exceed window at the start */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 231 | rc = backend_write(&context->backend, low_mem.flash_offset, |
| 232 | low_mem.mem, low_mem.size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 233 | if (rc < 0) { |
| 234 | goto out; |
| 235 | } |
| 236 | } |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 237 | rc = backend_write(&context->backend, flash_offset, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 238 | context->current->mem + offset_bytes, count_bytes); |
| 239 | if (rc < 0) { |
| 240 | goto out; |
| 241 | } |
| 242 | /* |
| 243 | * We still need to write the last little bit that we erased - it's |
| 244 | * either in the current window or the high_mem window. |
| 245 | */ |
| 246 | if (high_mem.mem) { |
| 247 | /* Exceed window at the end */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 248 | rc = backend_write(&context->backend, high_mem.flash_offset, |
| 249 | high_mem.mem, high_mem.size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 250 | if (rc < 0) { |
| 251 | goto out; |
| 252 | } |
| 253 | } else { |
| 254 | /* Write from the current window - it's atleast that big */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 255 | rc = backend_write(&context->backend, high_mem.flash_offset, |
| 256 | context->current->mem + offset_bytes + |
| 257 | count_bytes, high_mem.size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 258 | if (rc < 0) { |
| 259 | goto out; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | out: |
| 264 | free(low_mem.mem); |
| 265 | free(high_mem.mem); |
| 266 | return rc; |
| 267 | } |
| 268 | |
| 269 | /* |
Andrew Jeffery | 3200c07 | 2018-08-08 17:12:03 +0930 | [diff] [blame] | 270 | * window_flush() - Write back to the flash from the current window |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 271 | * @context: The mbox context pointer |
| 272 | * @offset_bytes: The offset in the current window to write from (blocks) |
| 273 | * @count_bytes: Number of blocks to write |
| 274 | * @type: Whether this is an erase & write or just an erase |
| 275 | * |
| 276 | * Return: 0 on success otherwise negative error code |
| 277 | */ |
Andrew Jeffery | 3200c07 | 2018-08-08 17:12:03 +0930 | [diff] [blame] | 278 | int window_flush(struct mbox_context *context, uint32_t offset, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 279 | uint32_t count, uint8_t type) |
| 280 | { |
| 281 | int rc; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 282 | uint32_t flash_offset, count_bytes = count << context->backend.block_size_shift; |
| 283 | uint32_t offset_bytes = offset << context->backend.block_size_shift; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 284 | |
| 285 | switch (type) { |
| 286 | case WINDOW_ERASED: /* >= V2 ONLY -> block_size == erasesize */ |
| 287 | flash_offset = context->current->flash_offset + offset_bytes; |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 288 | rc = backend_erase(&context->backend, flash_offset, |
| 289 | count_bytes); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 290 | if (rc < 0) { |
| 291 | MSG_ERR("Couldn't erase flash\n"); |
| 292 | return rc; |
| 293 | } |
| 294 | break; |
| 295 | case WINDOW_DIRTY: |
| 296 | /* |
| 297 | * For protocol V1, block_size may be smaller than erase size |
| 298 | * so we have a special function to make sure that we do this |
| 299 | * correctly without losing data. |
| 300 | */ |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 301 | if (context->backend.erase_size_shift != |
| 302 | context->backend.block_size_shift) { |
Andrew Jeffery | 3200c07 | 2018-08-08 17:12:03 +0930 | [diff] [blame] | 303 | return window_flush_v1(context, offset_bytes, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 304 | count_bytes); |
| 305 | } |
| 306 | flash_offset = context->current->flash_offset + offset_bytes; |
| 307 | |
| 308 | /* Erase the flash */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 309 | rc = backend_erase(&context->backend, flash_offset, |
| 310 | count_bytes); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 311 | if (rc < 0) { |
| 312 | return rc; |
| 313 | } |
| 314 | |
| 315 | /* Write to the erased flash */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 316 | rc = backend_write(&context->backend, flash_offset, |
| 317 | context->current->mem + offset_bytes, |
| 318 | count_bytes); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 319 | if (rc < 0) { |
| 320 | return rc; |
| 321 | } |
| 322 | |
| 323 | break; |
| 324 | default: |
| 325 | /* We shouldn't be able to get here */ |
| 326 | MSG_ERR("Write from window with invalid type: %d\n", type); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 327 | return -EPERM; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /* Window Management Functions */ |
| 334 | |
| 335 | /* |
Andrew Jeffery | 348ea79 | 2018-08-08 17:13:53 +0930 | [diff] [blame] | 336 | * windows_alloc_dirty_bytemap() - (re)allocate all the window dirty bytemaps |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 337 | * @context: The mbox context pointer |
| 338 | */ |
Andrew Jeffery | 348ea79 | 2018-08-08 17:13:53 +0930 | [diff] [blame] | 339 | void windows_alloc_dirty_bytemap(struct mbox_context *context) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 340 | { |
| 341 | struct window_context *cur; |
| 342 | int i; |
| 343 | |
| 344 | for (i = 0; i < context->windows.num; i++) { |
| 345 | cur = &context->windows.window[i]; |
| 346 | /* There may already be one allocated */ |
| 347 | free(cur->dirty_bmap); |
| 348 | /* Allocate the new one */ |
Andrew Jeffery | 41c337e | 2018-08-13 23:30:16 +0930 | [diff] [blame] | 349 | cur->dirty_bmap = calloc((context->windows.default_size >> |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 350 | context->backend.block_size_shift), |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 351 | sizeof(*cur->dirty_bmap)); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /* |
Andrew Jeffery | 7d5ada6 | 2018-08-08 17:16:16 +0930 | [diff] [blame] | 356 | * window_set_bytemap() - Set the window bytemap |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 357 | * @context: The mbox context pointer |
| 358 | * @cur: The window to set the bytemap of |
| 359 | * @offset: Where in the window to set the bytemap (blocks) |
| 360 | * @size: The number of blocks to set |
| 361 | * @val: The value to set the bytemap to |
| 362 | * |
| 363 | * Return: 0 on success otherwise negative error code |
| 364 | */ |
Andrew Jeffery | 7d5ada6 | 2018-08-08 17:16:16 +0930 | [diff] [blame] | 365 | int window_set_bytemap(struct mbox_context *context, struct window_context *cur, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 366 | uint32_t offset, uint32_t size, uint8_t val) |
| 367 | { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 368 | if (offset + size > (cur->size >> context->backend.block_size_shift)) { |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 369 | MSG_ERR("Tried to set window bytemap past end of window\n"); |
| 370 | MSG_ERR("Requested offset: 0x%x size: 0x%x window size: 0x%x\n", |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 371 | offset << context->backend.block_size_shift, |
| 372 | size << context->backend.block_size_shift, |
| 373 | cur->size << context->backend.block_size_shift); |
Andrew Jeffery | 6a0e2de | 2018-08-07 22:31:00 +0930 | [diff] [blame] | 374 | return -EACCES; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | memset(cur->dirty_bmap + offset, val, size); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /* |
Andrew Jeffery | b65bb4c | 2018-08-08 17:18:24 +0930 | [diff] [blame] | 382 | * windows_close_current() - Close the current (active) window |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 383 | * @context: The mbox context pointer |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 384 | * @flags: Flags as defined for a close command in the protocol |
| 385 | * |
| 386 | * This closes the current window. If the host has requested the current window |
| 387 | * be closed then we don't need to set the bmc event bit |
| 388 | * (set_bmc_event == false), otherwise if the current window has been closed |
| 389 | * without the host requesting it the bmc event bit must be set to indicate this |
| 390 | * to the host (set_bmc_event == true). |
| 391 | */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 392 | void windows_close_current(struct mbox_context *context, uint8_t flags) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 393 | { |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 394 | MSG_DBG("Close current window, flags: 0x%.2x\n", flags); |
| 395 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 396 | if (flags & FLAGS_SHORT_LIFETIME) { |
| 397 | context->current->age = 0; |
| 398 | } |
| 399 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 400 | context->current = NULL; |
| 401 | context->current_is_write = false; |
| 402 | } |
| 403 | |
| 404 | /* |
Andrew Jeffery | 5dc9f95 | 2018-08-08 17:21:34 +0930 | [diff] [blame] | 405 | * window_reset() - Reset a window context to a well defined default state |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 406 | * @context: The mbox context pointer |
| 407 | * @window: The window to reset |
| 408 | */ |
Andrew Jeffery | 5dc9f95 | 2018-08-08 17:21:34 +0930 | [diff] [blame] | 409 | void window_reset(struct mbox_context *context, struct window_context *window) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 410 | { |
| 411 | window->flash_offset = FLASH_OFFSET_UNINIT; |
| 412 | window->size = context->windows.default_size; |
| 413 | if (window->dirty_bmap) { /* Might not have been allocated */ |
Andrew Jeffery | 7d5ada6 | 2018-08-08 17:16:16 +0930 | [diff] [blame] | 414 | window_set_bytemap(context, window, 0, |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 415 | window->size >> context->backend.block_size_shift, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 416 | WINDOW_CLEAN); |
| 417 | } |
| 418 | window->age = 0; |
| 419 | } |
| 420 | |
| 421 | /* |
Andrew Jeffery | d6a7426 | 2018-08-08 17:25:01 +0930 | [diff] [blame] | 422 | * windows_reset_all() - Reset all windows to a well defined default state |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 423 | * @context: The mbox context pointer |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 424 | * |
| 425 | * @return True if there was a window open that was closed, false otherwise |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 426 | */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 427 | bool windows_reset_all(struct mbox_context *context) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 428 | { |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 429 | bool closed = context->current; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 430 | int i; |
| 431 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 432 | MSG_DBG("Resetting all windows\n"); |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 433 | |
| 434 | context->windows.max_age = 0; |
| 435 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 436 | /* We might have an open window which needs closing */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 437 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 438 | if (context->current) { |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 439 | windows_close_current(context, FLAGS_NONE); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 440 | } |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 441 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 442 | for (i = 0; i < context->windows.num; i++) { |
Andrew Jeffery | 5dc9f95 | 2018-08-08 17:21:34 +0930 | [diff] [blame] | 443 | window_reset(context, &context->windows.window[i]); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 444 | } |
| 445 | |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 446 | return closed; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* |
Andrew Jeffery | 9412f05 | 2018-08-08 17:25:47 +0930 | [diff] [blame] | 450 | * windows_find_oldest() - Find the oldest (Least Recently Used) window |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 451 | * @context: The mbox context pointer |
| 452 | * |
| 453 | * Return: Pointer to the least recently used window |
| 454 | */ |
Andrew Jeffery | 9412f05 | 2018-08-08 17:25:47 +0930 | [diff] [blame] | 455 | struct window_context *windows_find_oldest(struct mbox_context *context) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 456 | { |
| 457 | struct window_context *oldest = NULL, *cur; |
| 458 | uint32_t min_age = context->windows.max_age + 1; |
| 459 | int i; |
| 460 | |
| 461 | for (i = 0; i < context->windows.num; i++) { |
| 462 | cur = &context->windows.window[i]; |
| 463 | |
| 464 | if (cur->age < min_age) { |
| 465 | min_age = cur->age; |
| 466 | oldest = cur; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return oldest; |
| 471 | } |
| 472 | |
| 473 | /* |
Andrew Jeffery | d8c12e1 | 2018-08-08 17:26:31 +0930 | [diff] [blame] | 474 | * windows_find_largest() - Find the largest window in the window cache |
Suraj Jitindar Singh | 5a3a066 | 2017-04-27 11:55:26 +1000 | [diff] [blame] | 475 | * @context: The mbox context pointer |
| 476 | * |
| 477 | * Return: The largest window |
| 478 | */ |
Andrew Jeffery | d8c12e1 | 2018-08-08 17:26:31 +0930 | [diff] [blame] | 479 | struct window_context *windows_find_largest(struct mbox_context *context) |
Suraj Jitindar Singh | 5a3a066 | 2017-04-27 11:55:26 +1000 | [diff] [blame] | 480 | { |
| 481 | struct window_context *largest = NULL, *cur; |
| 482 | uint32_t max_size = 0; |
| 483 | int i; |
| 484 | |
| 485 | for (i = 0; i < context->windows.num; i++) { |
| 486 | cur = &context->windows.window[i]; |
| 487 | |
| 488 | if (cur->size > max_size) { |
| 489 | max_size = cur->size; |
| 490 | largest = cur; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return largest; |
| 495 | } |
| 496 | |
| 497 | /* |
Andrew Jeffery | 17c477a | 2018-08-08 17:27:19 +0930 | [diff] [blame] | 498 | * windows_search() - Search the window cache for a window containing offset |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 499 | * @context: The mbox context pointer |
| 500 | * @offset: Absolute flash offset to search for (bytes) |
| 501 | * @exact: If the window must exactly map the requested offset |
| 502 | * |
| 503 | * This will search the cache of windows for one containing the requested |
| 504 | * offset. For V1 of the protocol windows must exactly map the offset since we |
| 505 | * can't tell the host how much of its request we actually mapped and it will |
| 506 | * thus assume it can access window->size from the offset we give it. |
| 507 | * |
| 508 | * Return: Pointer to a window containing the requested offset otherwise |
| 509 | * NULL |
| 510 | */ |
Andrew Jeffery | 17c477a | 2018-08-08 17:27:19 +0930 | [diff] [blame] | 511 | struct window_context *windows_search(struct mbox_context *context, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 512 | uint32_t offset, bool exact) |
| 513 | { |
| 514 | struct window_context *cur; |
| 515 | int i; |
| 516 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 517 | MSG_DBG("Searching for window which contains 0x%.8x %s\n", |
| 518 | offset, exact ? "exactly" : ""); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 519 | for (i = 0; i < context->windows.num; i++) { |
| 520 | cur = &context->windows.window[i]; |
| 521 | if (cur->flash_offset == FLASH_OFFSET_UNINIT) { |
| 522 | /* Uninitialised Window */ |
| 523 | if (offset == FLASH_OFFSET_UNINIT) { |
| 524 | return cur; |
| 525 | } |
| 526 | continue; |
| 527 | } |
| 528 | if ((offset >= cur->flash_offset) && |
| 529 | (offset < (cur->flash_offset + cur->size))) { |
| 530 | if (exact && (cur->flash_offset != offset)) { |
| 531 | continue; |
| 532 | } |
| 533 | /* This window contains the requested offset */ |
| 534 | cur->age = ++(context->windows.max_age); |
| 535 | return cur; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | /* |
Andrew Jeffery | ebbfce5 | 2018-08-08 17:29:45 +0930 | [diff] [blame] | 543 | * windows_create_map() - Create a window mapping which maps the requested offset |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 544 | * @context: The mbox context pointer |
| 545 | * @this_window: A pointer to update to the "new" window |
| 546 | * @offset: Absolute flash offset to create a mapping for (bytes) |
| 547 | * @exact: If the window must exactly map the requested offset |
| 548 | * |
| 549 | * This is used to create a window mapping for the requested offset when there |
| 550 | * is no existing window in the cache which satisfies the offset. This involves |
| 551 | * choosing an existing window from the window cache to evict so we can use it |
| 552 | * to store the flash contents from the requested offset, we then point the |
| 553 | * caller to that window since it now maps their request. |
| 554 | * |
| 555 | * Return: 0 on success otherwise negative error code |
| 556 | */ |
Andrew Jeffery | ebbfce5 | 2018-08-08 17:29:45 +0930 | [diff] [blame] | 557 | int windows_create_map(struct mbox_context *context, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 558 | struct window_context **this_window, uint32_t offset, |
| 559 | bool exact) |
| 560 | { |
| 561 | struct window_context *cur = NULL; |
| 562 | int rc; |
| 563 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 564 | MSG_DBG("Creating window which maps 0x%.8x %s\n", offset, |
| 565 | exact ? "exactly" : ""); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 566 | |
| 567 | /* Search for an uninitialised window, use this before evicting */ |
Andrew Jeffery | 17c477a | 2018-08-08 17:27:19 +0930 | [diff] [blame] | 568 | cur = windows_search(context, FLASH_OFFSET_UNINIT, true); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 569 | |
| 570 | /* No uninitialised window found, we need to choose one to "evict" */ |
| 571 | if (!cur) { |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 572 | MSG_DBG("No uninitialised window, evicting one\n"); |
Andrew Jeffery | 9412f05 | 2018-08-08 17:25:47 +0930 | [diff] [blame] | 573 | cur = windows_find_oldest(context); |
Andrew Jeffery | 5dc9f95 | 2018-08-08 17:21:34 +0930 | [diff] [blame] | 574 | window_reset(context, cur); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 575 | } |
| 576 | |
Alvin Wang | 8cef63e | 2019-10-15 23:23:38 +0800 | [diff] [blame^] | 577 | /* Adjust the offset for alignment by the backend. It will help prevent the |
| 578 | * overlap. |
| 579 | */ |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 580 | if (!exact) { |
Alvin Wang | 8cef63e | 2019-10-15 23:23:38 +0800 | [diff] [blame^] | 581 | if (backend_align_offset(&(context->backend), &offset, cur->size)) { |
| 582 | MSG_ERR("Can't adjust the offset by backend\n"); |
| 583 | } |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 584 | } |
| 585 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 586 | if (offset > context->backend.flash_size) { |
Andrew Jeffery | 3da0de6 | 2018-03-23 12:27:15 +1030 | [diff] [blame] | 587 | MSG_ERR("Tried to open read window past flash limit\n"); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 588 | return -EINVAL; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 589 | } else if ((offset + cur->size) > context->backend.flash_size) { |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 590 | /* |
| 591 | * There is V1 skiboot implementations out there which don't |
| 592 | * mask offset with window size, meaning when we have |
| 593 | * window size == flash size we will never allow the host to |
| 594 | * open a window except at 0x0, which isn't always where the |
| 595 | * host requests it. Thus we have to ignore this check and just |
| 596 | * hope the host doesn't access past the end of the window |
| 597 | * (which it shouldn't) for V1 implementations to get around |
| 598 | * this. |
| 599 | */ |
| 600 | if (context->version == API_VERSION_1) { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 601 | cur->size = align_down(context->backend.flash_size - offset, |
| 602 | 1 << context->backend.block_size_shift); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 603 | } else { |
Andrew Jeffery | 3da0de6 | 2018-03-23 12:27:15 +1030 | [diff] [blame] | 604 | /* |
| 605 | * Allow requests to exceed the flash size, but limit |
| 606 | * the response to the size of the flash. |
| 607 | */ |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 608 | cur->size = context->backend.flash_size - offset; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | /* Copy from flash into the window buffer */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 613 | rc = backend_copy(&context->backend, offset, cur->mem, cur->size); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 614 | if (rc < 0) { |
| 615 | /* We don't know how much we've copied -> better reset window */ |
Andrew Jeffery | 5dc9f95 | 2018-08-08 17:21:34 +0930 | [diff] [blame] | 616 | window_reset(context, cur); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 617 | return rc; |
| 618 | } |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 619 | /* |
| 620 | * rc isn't guaranteed to be aligned, so align up |
| 621 | * |
| 622 | * FIXME: This should only be the case for the vpnor ToC now, so handle |
| 623 | * it there |
| 624 | */ |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 625 | cur->size = align_up(rc, (1ULL << context->backend.block_size_shift)); |
Suraj Jitindar Singh | 8493c33 | 2017-10-06 14:29:45 +1100 | [diff] [blame] | 626 | /* Would like a known value, pick 0xFF to it looks like erased flash */ |
| 627 | memset(cur->mem + rc, 0xFF, cur->size - rc); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * Since for V1 windows aren't constrained to start at multiples of |
| 631 | * window size it's possible that something already maps this offset. |
| 632 | * Reset any windows which map this offset to avoid coherency problems. |
| 633 | * We just have to check for anything which maps the start or the end |
| 634 | * of the window since all windows are the same size so another window |
| 635 | * cannot map just the middle of this window. |
| 636 | */ |
| 637 | if (context->version == API_VERSION_1) { |
| 638 | uint32_t i; |
| 639 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 640 | MSG_DBG("Checking for window overlap\n"); |
| 641 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 642 | for (i = offset; i < (offset + cur->size); i += (cur->size - 1)) { |
| 643 | struct window_context *tmp = NULL; |
| 644 | do { |
Andrew Jeffery | 17c477a | 2018-08-08 17:27:19 +0930 | [diff] [blame] | 645 | tmp = windows_search(context, i, false); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 646 | if (tmp) { |
Andrew Jeffery | 5dc9f95 | 2018-08-08 17:21:34 +0930 | [diff] [blame] | 647 | window_reset(context, tmp); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 648 | } |
| 649 | } while (tmp); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* Clear the bytemap of the window just loaded -> we know it's clean */ |
Andrew Jeffery | 7d5ada6 | 2018-08-08 17:16:16 +0930 | [diff] [blame] | 654 | window_set_bytemap(context, cur, 0, |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 655 | cur->size >> context->backend.block_size_shift, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 656 | WINDOW_CLEAN); |
| 657 | |
| 658 | /* Update so we know what's in the window */ |
| 659 | cur->flash_offset = offset; |
| 660 | cur->age = ++(context->windows.max_age); |
| 661 | *this_window = cur; |
| 662 | |
| 663 | return 0; |
| 664 | } |