2 * Copyright (c) 2004, 2006 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
37 #define EFI_INTEL_FPSWA \
38 {0xc41b6531,0x97b9,0x11d3,{0x9a,0x29,0x00,0x90,0x27,0x3f,0xc1,0x4d}}
40 static EFI_GUID fpswa_guid = EFI_INTEL_FPSWA;
42 /* DIG64 Headless Console & Debug Port Table. */
43 #define HCDP_TABLE_GUID \
44 {0xf951938d,0x620b,0x42ef,{0x82,0x79,0xa8,0x4b,0x79,0x61,0x78,0x98}}
46 static EFI_GUID hcdp_guid = HCDP_TABLE_GUID;
48 static EFI_MEMORY_DESCRIPTOR *memmap;
49 static UINTN memmapsz;
52 static UINT32 descver;
54 #define IA64_EFI_CHUNK_SIZE (32 * 1048576)
55 static vm_paddr_t ia64_efi_chunk;
57 #define IA64_EFI_PGTBLSZ_MAX 1048576
58 static vm_paddr_t ia64_efi_pgtbl;
59 static vm_size_t ia64_efi_pgtblsz;
61 /* Don't allocate memory below the boundary */
62 #define IA64_EFI_ALLOC_BOUNDARY 1048576
65 ia64_efi_memmap_update(void)
75 BS->GetMemoryMap(&memmapsz, NULL, &mapkey, &descsz, &descver);
78 memmap = malloc(memmapsz);
82 status = BS->GetMemoryMap(&memmapsz, memmap, &mapkey, &descsz,
84 if (EFI_ERROR(status)) {
94 * Returns 0 on failure. Successful allocations return an address
95 * larger or equal to IA64_EFI_ALLOC_BOUNDARY.
98 ia64_efi_alloc(vm_size_t sz)
100 EFI_PHYSICAL_ADDRESS pa;
101 EFI_MEMORY_DESCRIPTOR *mm;
102 uint8_t *mmiter, *mmiterend;
107 /* We can't allocate less than a page */
108 if (sz < EFI_PAGE_SIZE)
111 /* The size must be a power of 2. */
115 if (!ia64_efi_memmap_update())
118 mmiter = (void *)memmap;
119 mmiterend = mmiter + memmapsz;
120 for (; mmiter < mmiterend; mmiter += descsz) {
122 if (mm->Type != EfiConventionalMemory)
124 memsz = mm->NumberOfPages * EFI_PAGE_SIZE;
125 if (mm->PhysicalStart + memsz <= IA64_EFI_ALLOC_BOUNDARY)
128 * XXX We really should make sure the memory is local to the
131 pa = (mm->PhysicalStart < IA64_EFI_ALLOC_BOUNDARY) ?
132 IA64_EFI_ALLOC_BOUNDARY : mm->PhysicalStart;
133 pa = (pa + sz - 1) & ~(sz - 1);
134 if (pa + sz > mm->PhysicalStart + memsz)
137 npgs = EFI_SIZE_TO_PAGES(sz);
138 status = BS->AllocatePages(AllocateAddress, EfiLoaderData,
140 if (!EFI_ERROR(status))
144 printf("%s: unable to allocate %lx bytes\n", __func__, sz);
149 ia64_platform_alloc(vm_offset_t va, vm_size_t sz)
154 /* Page table itself. */
155 if (sz > IA64_EFI_PGTBLSZ_MAX)
157 if (ia64_efi_pgtbl == 0)
158 ia64_efi_pgtbl = ia64_efi_alloc(IA64_EFI_PGTBLSZ_MAX);
159 if (ia64_efi_pgtbl != 0)
160 ia64_efi_pgtblsz = sz;
161 return (ia64_efi_pgtbl);
162 } else if (va < IA64_PBVM_BASE) {
163 /* Should not happen. */
167 /* Loader virtual memory page. */
168 va -= IA64_PBVM_BASE;
170 /* Allocate a big chunk that can be wired with a single PTE. */
171 if (ia64_efi_chunk == 0)
172 ia64_efi_chunk = ia64_efi_alloc(IA64_EFI_CHUNK_SIZE);
173 if (va < IA64_EFI_CHUNK_SIZE)
174 return (ia64_efi_chunk + va);
176 /* Allocate a page at a time when we go beyond the chunk. */
177 pa = ia64_efi_alloc(sz);
178 return ((pa == 0) ? ~0UL : pa);
182 ia64_platform_free(vm_offset_t va, vm_paddr_t pa, vm_size_t sz)
185 BS->FreePages(pa, sz >> EFI_PAGE_SHIFT);
189 ia64_platform_bootinfo(struct bootinfo *bi, struct bootinfo **res)
196 bi->bi_systab = (uint64_t)ST;
197 bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp_guid);
199 sz = sizeof(EFI_HANDLE);
200 status = BS->LocateHandle(ByProtocol, &fpswa_guid, 0, &sz, &handle);
202 status = BS->HandleProtocol(handle, &fpswa_guid, &fpswa);
203 bi->bi_fpswa = (status == 0) ? (uint64_t)fpswa : 0;
205 if (!ia64_efi_memmap_update())
208 bi->bi_memmap = (uint64_t)memmap;
209 bi->bi_memmap_size = memmapsz;
210 bi->bi_memdesc_size = descsz;
211 bi->bi_memdesc_version = descver;
213 if (IS_LEGACY_KERNEL())
214 *res = malloc(sizeof(**res));
220 ia64_platform_enter(const char *kernel)
224 status = BS->ExitBootServices(IH, mapkey);
225 if (EFI_ERROR(status)) {
226 printf("%s: ExitBootServices() returned 0x%lx\n", __func__,
234 COMMAND_SET(pbvm, "pbvm", "show PBVM details", command_pbvm);
237 command_pbvm(int argc, char *argv[])
239 uint64_t limit, pg, start;
242 printf("Page table @ %p, size %x\n", ia64_pgtbl, ia64_pgtblsz);
244 if (ia64_pgtbl == NULL)
250 while (ia64_pgtbl[idx] != 0) {
251 pg = ia64_pgtbl[idx];
254 printf("%#lx-%#lx\n", start, limit);
257 limit = pg + IA64_PBVM_PAGE_SIZE;
261 printf("%#lx-%#lx\n", start, limit);