blob: 6d1d36b8269678c5d5af7737f2bcd944b4290114 [file] [log] [blame]
Feng Tang74e654c2010-05-27 10:45:00 +08001/*
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 Tang7cc52cd2009-06-01 11:23:28 +080020#include "types.h"
21#include "bootparam.h"
22#include "bootstub.h"
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030023#include "mb.h"
Feng Tang7cc52cd2009-06-01 11:23:28 +080024#include "sfi.h"
25
Feng Tang7cc52cd2009-06-01 11:23:28 +080026#define SFI_BASE_ADDR 0x000E0000
27#define SFI_LENGTH 0x00020000
28
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030029static unsigned long sfi_search_mmap(unsigned long start, int len)
Feng Tang7cc52cd2009-06-01 11:23:28 +080030{
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030031 unsigned long i = 0;
32 char *pchar = (char *)start;
Feng Tang7cc52cd2009-06-01 11:23:28 +080033
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030034 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. Brownafcd4662011-09-07 18:09:37 -040042}
43
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030044int sfi_add_e820_entry(struct boot_params *bp, memory_map_t *mb_mmap, u64 start, u64 size, int type)
Mark F. Brownafcd4662011-09-07 18:09:37 -040045{
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030046 struct e820entry * e820_entry;
47 memory_map_t *mb_mmap_entry;
48 int i;
Mark F. Brownafcd4662011-09-07 18:09:37 -040049
Michel Jauffres978d7512014-01-08 13:34:32 +010050 if (!bp || !mb_mmap) {
51 bs_printk("Bootstub: sfi_add_e820_entry failed\n");
52 return -1;
53 }
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030054
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. Brownafcd4662011-09-07 18:09:37 -040066 }
Feng Tang7cc52cd2009-06-01 11:23:28 +080067 }
Mark F. Brownafcd4662011-09-07 18:09:37 -040068
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030069 /* ASSERT: no duplicate start address found */
70 if (bp->e820_entries == E820MAX)
71 return -1;
Mark F. Brownafcd4662011-09-07 18:09:37 -040072
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030073 e820_entry = &(bp->e820_map[bp->e820_entries]);
74 mb_mmap_entry = &(mb_mmap[bp->e820_entries]);
Mark F. Brownafcd4662011-09-07 18:09:37 -040075
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030076 e820_entry->addr = start;
77 e820_entry->size = size;
78 e820_entry->type = type;
Mark F. Brownafcd4662011-09-07 18:09:37 -040079
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030080 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. Brownafcd4662011-09-07 18:09:37 -040088
Feng Tang7cc52cd2009-06-01 11:23:28 +080089 return 0;
90}
91
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030092void sfi_setup_mmap(struct boot_params *bp, memory_map_t *mb_mmap)
Feng Tang7cc52cd2009-06-01 11:23:28 +080093{
94 struct sfi_table *sb;
95 struct sfi_mem_entry *mentry;
96 unsigned long long start, end, size;
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +030097 int i, num, type;
Feng Tang7cc52cd2009-06-01 11:23:28 +080098
Michel Jauffres978d7512014-01-08 13:34:32 +010099 if (!bp || !mb_mmap) {
100 bs_printk("Bootstub: sfi_setup_mmap failed\n");
101 return;
102 }
103
104 bp->e820_entries = 0;
Feng Tang7cc52cd2009-06-01 11:23:28 +0800105
106 /* search for sfi mmap table */
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +0300107 sb = (struct sfi_table *)sfi_search_mmap(SFI_BASE_ADDR, SFI_LENGTH);
Jacob Pan9fc33412010-10-07 14:12:39 -0700108 if (!sb) {
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +0300109 bs_printk("Bootstub: SFI MMAP table not found\n");
Feng Tang7cc52cd2009-06-01 11:23:28 +0800110 return;
Jacob Pan9fc33412010-10-07 14:12:39 -0700111 }
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +0300112 bs_printk("Bootstub: map SFI MMAP to e820 table\n");
Feng Tang7cc52cd2009-06-01 11:23:28 +0800113 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 Tang7cc52cd2009-06-01 11:23:28 +0800129 case SFI_MEM_UNUSABLE:
130 case SFI_RUNTIME_SERVICE_DATA:
131 mentry++;
132 continue;
133 default:
134 type = E820_RESERVED;
135 }
136
Evgeny Kalugin8e8bf002013-10-24 11:21:07 +0300137 if (sfi_add_e820_entry(bp, mb_mmap, start, size, type) != 0)
Feng Tang7cc52cd2009-06-01 11:23:28 +0800138 break;
Feng Tang7cc52cd2009-06-01 11:23:28 +0800139
140 mentry++;
141 }
142
Feng Tang7cc52cd2009-06-01 11:23:28 +0800143}