Feng Tang | 74e654c | 2010-05-27 10:45:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * sfi.c - driver for parsing sfi mmap table and build e820 table |
| 3 | * |
| 4 | * Copyright (c) 2009, Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 20 | #include "types.h" |
| 21 | #include "bootparam.h" |
| 22 | #include "bootstub.h" |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 23 | #include "mb.h" |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 24 | #include "sfi.h" |
| 25 | |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 26 | #define SFI_BASE_ADDR 0x000E0000 |
| 27 | #define SFI_LENGTH 0x00020000 |
| 28 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 29 | static unsigned long sfi_search_mmap(unsigned long start, int len) |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 30 | { |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 31 | unsigned long i = 0; |
| 32 | char *pchar = (char *)start; |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 33 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 34 | for (i = 0; i < len; i++, pchar++) { |
| 35 | if (pchar[0] == 'M' |
| 36 | && pchar[1] == 'M' |
| 37 | && pchar[2] == 'A' |
| 38 | && pchar[3] == 'P') |
| 39 | return start + i; |
| 40 | } |
| 41 | return 0; |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 44 | int sfi_add_e820_entry(struct boot_params *bp, memory_map_t *mb_mmap, u64 start, u64 size, int type) |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 45 | { |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 46 | struct e820entry * e820_entry; |
| 47 | memory_map_t *mb_mmap_entry; |
| 48 | int i; |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 49 | |
Michel Jauffres | 978d751 | 2014-01-08 13:34:32 +0100 | [diff] [blame] | 50 | if (!bp || !mb_mmap) { |
| 51 | bs_printk("Bootstub: sfi_add_e820_entry failed\n"); |
| 52 | return -1; |
| 53 | } |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 54 | |
| 55 | for (i=0; i < bp->e820_entries; i++) { |
| 56 | e820_entry = &(bp->e820_map[i]); |
| 57 | mb_mmap_entry = &(mb_mmap[i]); |
| 58 | if (e820_entry->addr == start) { |
| 59 | /* Override size and type */ |
| 60 | e820_entry->size = size; |
| 61 | e820_entry->type = type; |
| 62 | mb_mmap_entry->length_low = size; |
| 63 | mb_mmap_entry->length_high = 0; |
| 64 | mb_mmap_entry->type = (type == E820_RAM)?1:0; |
| 65 | return 0; |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 66 | } |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 67 | } |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 68 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 69 | /* ASSERT: no duplicate start address found */ |
| 70 | if (bp->e820_entries == E820MAX) |
| 71 | return -1; |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 72 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 73 | e820_entry = &(bp->e820_map[bp->e820_entries]); |
| 74 | mb_mmap_entry = &(mb_mmap[bp->e820_entries]); |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 75 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 76 | e820_entry->addr = start; |
| 77 | e820_entry->size = size; |
| 78 | e820_entry->type = type; |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 79 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 80 | mb_mmap_entry->size = 20; |
| 81 | mb_mmap_entry->base_addr_low = start; |
| 82 | mb_mmap_entry->base_addr_high = 0; |
| 83 | mb_mmap_entry->length_low = size; |
| 84 | mb_mmap_entry->length_high = 0; |
| 85 | mb_mmap_entry->type = (type == E820_RAM)?1:0; |
| 86 | |
| 87 | bp->e820_entries++; |
Mark F. Brown | afcd466 | 2011-09-07 18:09:37 -0400 | [diff] [blame] | 88 | |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 92 | void sfi_setup_mmap(struct boot_params *bp, memory_map_t *mb_mmap) |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 93 | { |
| 94 | struct sfi_table *sb; |
| 95 | struct sfi_mem_entry *mentry; |
| 96 | unsigned long long start, end, size; |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 97 | int i, num, type; |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 98 | |
Michel Jauffres | 978d751 | 2014-01-08 13:34:32 +0100 | [diff] [blame] | 99 | if (!bp || !mb_mmap) { |
| 100 | bs_printk("Bootstub: sfi_setup_mmap failed\n"); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | bp->e820_entries = 0; |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 105 | |
| 106 | /* search for sfi mmap table */ |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 107 | sb = (struct sfi_table *)sfi_search_mmap(SFI_BASE_ADDR, SFI_LENGTH); |
Jacob Pan | 9fc3341 | 2010-10-07 14:12:39 -0700 | [diff] [blame] | 108 | if (!sb) { |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 109 | bs_printk("Bootstub: SFI MMAP table not found\n"); |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 110 | return; |
Jacob Pan | 9fc3341 | 2010-10-07 14:12:39 -0700 | [diff] [blame] | 111 | } |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 112 | bs_printk("Bootstub: map SFI MMAP to e820 table\n"); |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 113 | num = SFI_GET_ENTRY_NUM(sb, sfi_mem_entry); |
| 114 | mentry = (struct sfi_mem_entry *)sb->pentry; |
| 115 | |
| 116 | for (i = 0; i < num; i++) { |
| 117 | start = mentry->phy_start; |
| 118 | size = mentry->pages << 12; |
| 119 | end = start + size; |
| 120 | |
| 121 | if (start > end) |
| 122 | continue; |
| 123 | |
| 124 | /* translate SFI mmap type to E820 map type */ |
| 125 | switch (mentry->type) { |
| 126 | case SFI_MEM_CONV: |
| 127 | type = E820_RAM; |
| 128 | break; |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 129 | case SFI_MEM_UNUSABLE: |
| 130 | case SFI_RUNTIME_SERVICE_DATA: |
| 131 | mentry++; |
| 132 | continue; |
| 133 | default: |
| 134 | type = E820_RESERVED; |
| 135 | } |
| 136 | |
Evgeny Kalugin | 8e8bf00 | 2013-10-24 11:21:07 +0300 | [diff] [blame] | 137 | if (sfi_add_e820_entry(bp, mb_mmap, start, size, type) != 0) |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 138 | break; |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 139 | |
| 140 | mentry++; |
| 141 | } |
| 142 | |
Feng Tang | 7cc52cd | 2009-06-01 11:23:28 +0800 | [diff] [blame] | 143 | } |