Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 5 | * Copyright (C) 2015 Intel Corp. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program 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 |
| 14 | * GNU General Public License for more details. |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | |
| 18 | #include <arch/byteorder.h> |
| 19 | #include <boot/coreboot_tables.h> |
| 20 | #include <cbmem.h> |
| 21 | #include <cbfs.h> |
| 22 | #include <console/console.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <vendorcode/google/chromeos/chromeos.h> |
| 26 | |
| 27 | #define CACHELINE_SIZE 64 |
| 28 | #define INTRA_CACHELINE_MASK (CACHELINE_SIZE - 1) |
| 29 | #define CACHELINE_MASK (~INTRA_CACHELINE_MASK) |
| 30 | |
| 31 | static void *find_mirror_buffer(int len) |
| 32 | { |
| 33 | int nentries; |
| 34 | int i; |
| 35 | struct lb_memory *mem; |
| 36 | void *buffer; |
| 37 | |
| 38 | len = ALIGN(len, 4096); |
| 39 | |
| 40 | mem = get_lb_mem(); |
| 41 | nentries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]); |
| 42 | |
| 43 | /* |
| 44 | * Find the highest RAM entry that accommodates the lenth provide |
| 45 | * while falling below 4GiB. |
| 46 | */ |
| 47 | buffer = NULL; |
| 48 | for (i = 0; i < nentries; i++) { |
| 49 | const uint64_t max_addr = 1ULL << 32; |
| 50 | uint64_t start; |
| 51 | uint64_t size; |
| 52 | struct lb_memory_range *r; |
| 53 | |
| 54 | r = &mem->map[i]; |
| 55 | |
| 56 | if (r->type != LB_MEM_RAM) |
| 57 | continue; |
| 58 | |
| 59 | start = unpack_lb64(r->start); |
| 60 | if (start >= max_addr) |
| 61 | continue; |
| 62 | |
| 63 | size = unpack_lb64(r->size); |
| 64 | if (size < len) |
| 65 | continue; |
| 66 | |
| 67 | /* Adjust size of buffer if range exceeds max address. */ |
| 68 | if (start + size > max_addr) |
| 69 | size = max_addr - start; |
| 70 | |
| 71 | if (size < len) |
| 72 | continue; |
| 73 | |
| 74 | buffer = (void *)(uintptr_t)(start + size - len); |
| 75 | } |
| 76 | |
| 77 | return buffer; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Mirror the payload file to the default SMM location if it is small enough. |
| 82 | * The default SMM region can be used since no one is using the memory at this |
| 83 | * location at this stage in the boot. |
| 84 | */ |
| 85 | static void *spi_mirror(void *file_start, int file_len) |
| 86 | { |
| 87 | int alignment_diff; |
| 88 | char *src; |
| 89 | char *dest; |
| 90 | |
| 91 | alignment_diff = (INTRA_CACHELINE_MASK & (long)file_start); |
| 92 | |
| 93 | /* |
| 94 | * Adjust file length so that the start and end points are aligned to a |
| 95 | * cacheline. Coupled with the ROM caching in the CPU the SPI hardware |
| 96 | * will read and cache full length cachelines. It will also prefetch |
| 97 | * data as well. Once things are mirrored in memory all accesses should |
| 98 | * hit the CPUs cache. |
| 99 | */ |
| 100 | file_len += alignment_diff; |
| 101 | file_len = ALIGN(file_len, CACHELINE_SIZE); |
| 102 | |
| 103 | printk(BIOS_DEBUG, "Payload aligned size: 0x%x\n", file_len); |
| 104 | |
| 105 | dest = find_mirror_buffer(file_len); |
| 106 | |
| 107 | /* |
| 108 | * Just pass back the pointer to ROM space if a buffer could not |
| 109 | * be found to mirror into. |
| 110 | */ |
| 111 | if (dest == NULL) |
| 112 | return file_start; |
| 113 | |
| 114 | src = (void *)(CACHELINE_MASK & (long)file_start); |
| 115 | /* |
| 116 | * Note that if mempcy is not using 32-bit moves the performance will |
| 117 | * degrade because the SPI hardware prefetchers look for |
| 118 | * cacheline-aligned 32-bit accesses to kick in. |
| 119 | */ |
| 120 | memcpy(dest, src, file_len); |
| 121 | |
| 122 | /* Provide pointer into mirrored space. */ |
| 123 | return &dest[alignment_diff]; |
| 124 | } |
| 125 | |
| 126 | void *cbfs_load_payload(struct cbfs_media *media, const char *name) |
| 127 | { |
| 128 | int file_len; |
| 129 | void *file_start; |
| 130 | struct cbfs_file *file; |
| 131 | |
| 132 | file_start = vboot_get_payload(&file_len); |
| 133 | |
| 134 | if (file_start != NULL) |
| 135 | return spi_mirror(file_start, file_len); |
| 136 | |
| 137 | file = cbfs_get_file(media, name); |
| 138 | |
| 139 | if (file == NULL) |
| 140 | return NULL; |
| 141 | |
| 142 | if (ntohl(file->type) != CBFS_TYPE_PAYLOAD) |
| 143 | return NULL; |
| 144 | |
| 145 | file_len = ntohl(file->len); |
| 146 | |
| 147 | file_start = CBFS_SUBHEADER(file); |
| 148 | |
| 149 | return spi_mirror(file_start, file_len); |
| 150 | } |