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 
20 #include "types.h"
21 #include "bootparam.h"
22 #include "bootstub.h"
23 #include "mb.h"
24 #include "sfi.h"
25 
26 #define SFI_BASE_ADDR		0x000E0000
27 #define SFI_LENGTH		0x00020000
28 
sfi_search_mmap(unsigned long start,int len)29 static unsigned long sfi_search_mmap(unsigned long start, int len)
30 {
31 	unsigned long i = 0;
32 	char *pchar = (char *)start;
33 
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;
42 }
43 
sfi_add_e820_entry(struct boot_params * bp,memory_map_t * mb_mmap,u64 start,u64 size,int type)44 int sfi_add_e820_entry(struct boot_params *bp, memory_map_t *mb_mmap, u64 start, u64 size, int type)
45 {
46         struct e820entry * e820_entry;
47 	memory_map_t	*mb_mmap_entry;
48 	int	i;
49 
50 	if (!bp || !mb_mmap) {
51 		bs_printk("Bootstub: sfi_add_e820_entry failed\n");
52 		return -1;
53 	}
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;
66 		}
67 	}
68 
69 	/* ASSERT: no duplicate start address found */
70 	if (bp->e820_entries == E820MAX)
71 		return -1;
72 
73 	e820_entry = &(bp->e820_map[bp->e820_entries]);
74 	mb_mmap_entry = &(mb_mmap[bp->e820_entries]);
75 
76 	e820_entry->addr = start;
77 	e820_entry->size = size;
78 	e820_entry->type = type;
79 
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++;
88 
89 	return 0;
90 }
91 
sfi_setup_mmap(struct boot_params * bp,memory_map_t * mb_mmap)92 void sfi_setup_mmap(struct boot_params *bp, memory_map_t *mb_mmap)
93 {
94 	struct sfi_table *sb;
95 	struct sfi_mem_entry *mentry;
96 	unsigned long long start, end, size;
97 	int i, num, type;
98 
99 	if (!bp || !mb_mmap) {
100 		bs_printk("Bootstub: sfi_setup_mmap failed\n");
101 		return;
102 	}
103 
104 	bp->e820_entries = 0;
105 
106 	/* search for sfi mmap table */
107 	sb = (struct sfi_table *)sfi_search_mmap(SFI_BASE_ADDR, SFI_LENGTH);
108 	if (!sb) {
109 		bs_printk("Bootstub: SFI MMAP table not found\n");
110 		return;
111 	}
112 	bs_printk("Bootstub: map SFI MMAP to e820 table\n");
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;
129 		case SFI_MEM_UNUSABLE:
130 		case SFI_RUNTIME_SERVICE_DATA:
131 			mentry++;
132 			continue;
133 		default:
134 			type = E820_RESERVED;
135 		}
136 
137 		if (sfi_add_e820_entry(bp, mb_mmap, start, size, type) != 0)
138 			break;
139 
140 		mentry++;
141 	}
142 
143 }
144