2009-10-26 18:04:28 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 coresystems GmbH
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <types.h>
|
|
|
|
#include <string.h>
|
2013-04-24 23:39:08 +02:00
|
|
|
#include <bootstate.h>
|
2009-10-26 18:04:28 +01:00
|
|
|
#include <cbmem.h>
|
2013-09-04 13:11:08 +02:00
|
|
|
#include <boot/coreboot_tables.h>
|
2009-10-26 18:04:28 +01:00
|
|
|
#include <console/console.h>
|
2013-06-19 21:25:44 +02:00
|
|
|
#include <arch/early_variables.h>
|
2011-09-23 19:24:49 +02:00
|
|
|
#if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
|
|
|
|
#include <arch/acpi.h>
|
|
|
|
#endif
|
2009-10-26 18:04:28 +01:00
|
|
|
|
|
|
|
// The CBMEM TOC reserves 512 bytes to keep
|
|
|
|
// the other entries somewhat aligned.
|
|
|
|
// Increase if MAX_CBMEM_ENTRIES exceeds 21
|
|
|
|
#define CBMEM_TOC_RESERVED 512
|
|
|
|
#define MAX_CBMEM_ENTRIES 16
|
|
|
|
#define CBMEM_MAGIC 0x434f5245
|
|
|
|
|
|
|
|
struct cbmem_entry {
|
|
|
|
u32 magic;
|
|
|
|
u32 id;
|
|
|
|
u64 base;
|
|
|
|
u64 size;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2010-12-13 20:59:13 +01:00
|
|
|
#ifndef __PRE_RAM__
|
2013-06-23 19:03:50 +02:00
|
|
|
static uint64_t cbmem_base = 0;
|
|
|
|
static uint64_t cbmem_size = 0;
|
2013-09-04 12:26:11 +02:00
|
|
|
#endif
|
2010-11-22 23:00:52 +01:00
|
|
|
|
2013-09-04 12:31:39 +02:00
|
|
|
static void cbmem_trace_location(uint64_t base, uint64_t size, const char *s)
|
2010-11-22 23:00:52 +01:00
|
|
|
{
|
2013-09-04 12:31:39 +02:00
|
|
|
if (base && size && s) {
|
|
|
|
printk(BIOS_DEBUG, "CBMEM region %llx-%llx (%s)\n",
|
|
|
|
base, base + size - 1, s);
|
|
|
|
}
|
2010-11-22 23:00:52 +01:00
|
|
|
}
|
2013-09-04 12:31:39 +02:00
|
|
|
|
|
|
|
static void cbmem_locate_table(uint64_t *base, uint64_t *size)
|
|
|
|
{
|
|
|
|
#ifdef __PRE_RAM__
|
|
|
|
get_cbmem_table(base, size);
|
2010-12-13 21:02:23 +01:00
|
|
|
#else
|
2013-06-23 19:03:50 +02:00
|
|
|
if (!(cbmem_base && cbmem_size)) {
|
|
|
|
get_cbmem_table(&cbmem_base, &cbmem_size);
|
|
|
|
cbmem_trace_location(cbmem_base, cbmem_size, __FUNCTION__);
|
2013-09-04 12:31:39 +02:00
|
|
|
}
|
2013-06-23 19:03:50 +02:00
|
|
|
*base = cbmem_base;
|
|
|
|
*size = cbmem_size;
|
2013-09-04 12:31:39 +02:00
|
|
|
#endif
|
|
|
|
}
|
2010-12-13 21:02:23 +01:00
|
|
|
|
2013-09-06 09:46:22 +02:00
|
|
|
struct cbmem_entry *get_cbmem_toc(void)
|
2010-12-13 21:02:23 +01:00
|
|
|
{
|
2013-09-04 12:31:39 +02:00
|
|
|
uint64_t base, size;
|
|
|
|
cbmem_locate_table(&base, &size);
|
|
|
|
return (struct cbmem_entry *)(unsigned long)base;
|
2010-12-13 21:02:23 +01:00
|
|
|
}
|
2010-11-22 23:00:52 +01:00
|
|
|
|
2013-09-04 12:05:01 +02:00
|
|
|
#if !defined(__PRE_RAM__)
|
|
|
|
void cbmem_late_set_table(uint64_t base, uint64_t size)
|
|
|
|
{
|
2013-09-04 12:31:39 +02:00
|
|
|
cbmem_trace_location(base, size, __FUNCTION__);
|
2013-06-23 19:03:50 +02:00
|
|
|
cbmem_base = base;
|
|
|
|
cbmem_size = size;
|
2013-09-04 12:05:01 +02:00
|
|
|
}
|
2009-10-26 18:04:28 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cbmem is a simple mechanism to do some kind of book keeping of the coreboot
|
|
|
|
* high tables memory. This is a small amount of memory which is "stolen" from
|
|
|
|
* the system memory for coreboot purposes. Usually this memory is used for
|
|
|
|
* - the coreboot table
|
|
|
|
* - legacy tables (PIRQ, MP table)
|
|
|
|
* - ACPI tables
|
|
|
|
* - suspend/resume backup memory
|
|
|
|
*/
|
|
|
|
|
2013-10-11 21:08:02 +02:00
|
|
|
#if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
|
2013-09-04 13:36:31 +02:00
|
|
|
static void cbmem_init(void)
|
2009-10-26 18:04:28 +01:00
|
|
|
{
|
2013-09-04 13:36:31 +02:00
|
|
|
uint64_t baseaddr, size;
|
2009-10-26 18:04:28 +01:00
|
|
|
struct cbmem_entry *cbmem_toc;
|
|
|
|
|
2013-09-04 13:36:31 +02:00
|
|
|
cbmem_locate_table(&baseaddr, &size);
|
|
|
|
cbmem_trace_location(baseaddr, size, __FUNCTION__);
|
|
|
|
|
|
|
|
cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
|
2009-10-26 18:04:28 +01:00
|
|
|
|
|
|
|
if (size < (64 * 1024)) {
|
2010-11-10 01:14:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Increase CBMEM size!\n");
|
2009-10-26 18:04:28 +01:00
|
|
|
for (;;) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
|
|
|
|
|
|
|
|
cbmem_toc[0] = (struct cbmem_entry) {
|
|
|
|
.magic = CBMEM_MAGIC,
|
|
|
|
.id = CBMEM_ID_FREESPACE,
|
|
|
|
.base = baseaddr + CBMEM_TOC_RESERVED,
|
|
|
|
.size = size - CBMEM_TOC_RESERVED
|
|
|
|
};
|
|
|
|
}
|
2013-10-11 21:08:02 +02:00
|
|
|
#endif
|
2009-10-26 18:04:28 +01:00
|
|
|
|
2013-06-23 16:01:29 +02:00
|
|
|
int cbmem_reinit(void)
|
2009-10-26 18:04:28 +01:00
|
|
|
{
|
2013-06-23 16:01:29 +02:00
|
|
|
uint64_t baseaddr, size;
|
2009-10-26 18:04:28 +01:00
|
|
|
struct cbmem_entry *cbmem_toc;
|
|
|
|
|
2013-06-23 16:01:29 +02:00
|
|
|
cbmem_locate_table(&baseaddr, &size);
|
|
|
|
cbmem_trace_location(baseaddr, size, __FUNCTION__);
|
|
|
|
|
|
|
|
cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
|
2010-11-10 01:14:32 +01:00
|
|
|
|
2009-10-26 18:04:28 +01:00
|
|
|
return (cbmem_toc[0].magic == CBMEM_MAGIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *cbmem_add(u32 id, u64 size)
|
|
|
|
{
|
|
|
|
struct cbmem_entry *cbmem_toc;
|
|
|
|
int i;
|
2011-09-22 01:12:39 +02:00
|
|
|
void *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This could be a restart, check if the section is there already. It
|
|
|
|
* is remotely possible that the dram contents persisted over the
|
|
|
|
* bootloader upgrade AND the same section now needs more room, but
|
|
|
|
* this is quite a remote possibility and it is ignored here.
|
|
|
|
*/
|
|
|
|
p = cbmem_find(id);
|
|
|
|
if (p) {
|
|
|
|
printk(BIOS_NOTICE,
|
|
|
|
"CBMEM section %x: using existing location at %p.\n",
|
|
|
|
id, p);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-03-31 16:57:55 +02:00
|
|
|
cbmem_toc = get_cbmem_toc();
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-10-26 18:04:28 +01:00
|
|
|
if (cbmem_toc == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cbmem_toc[0].magic != CBMEM_MAGIC) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
|
2009-10-26 18:04:28 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Will the entry fit at all? */
|
|
|
|
if (size > cbmem_toc[0].size) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_ERR, "ERROR: Not enough memory for table %x\n", id);
|
2009-10-26 18:04:28 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Align size to 512 byte blocks */
|
|
|
|
|
2010-04-27 08:56:47 +02:00
|
|
|
size = ALIGN(size, 512) < cbmem_toc[0].size ?
|
2009-10-26 18:04:28 +01:00
|
|
|
ALIGN(size, 512) : cbmem_toc[0].size;
|
|
|
|
|
|
|
|
/* Now look for the first free/usable TOC entry */
|
|
|
|
for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
|
|
|
|
if (cbmem_toc[i].id == CBMEM_ID_NONE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= MAX_CBMEM_ENTRIES) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
|
2009-10-26 18:04:28 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-10 01:14:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
|
2009-10-26 18:04:28 +01:00
|
|
|
|
|
|
|
cbmem_toc[i] = (struct cbmem_entry) {
|
|
|
|
.magic = CBMEM_MAGIC,
|
|
|
|
.id = id,
|
|
|
|
.base = cbmem_toc[0].base,
|
|
|
|
.size = size
|
|
|
|
};
|
|
|
|
|
|
|
|
cbmem_toc[0].base += size;
|
|
|
|
cbmem_toc[0].size -= size;
|
|
|
|
|
2013-11-11 19:36:28 +01:00
|
|
|
return (void *)(uintptr_t)cbmem_toc[i].base;
|
2009-10-26 18:04:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *cbmem_find(u32 id)
|
|
|
|
{
|
|
|
|
struct cbmem_entry *cbmem_toc;
|
|
|
|
int i;
|
2010-03-31 16:57:55 +02:00
|
|
|
cbmem_toc = get_cbmem_toc();
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-10-26 18:04:28 +01:00
|
|
|
if (cbmem_toc == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
|
|
|
|
if (cbmem_toc[i].id == id)
|
|
|
|
return (void *)(unsigned long)cbmem_toc[i].base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void *)NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:08:02 +02:00
|
|
|
#if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
|
2013-07-10 05:51:14 +02:00
|
|
|
/* Returns True if it was not initialized before. */
|
2011-09-21 02:07:14 +02:00
|
|
|
int cbmem_initialize(void)
|
2009-10-26 18:04:28 +01:00
|
|
|
{
|
2011-09-21 02:07:14 +02:00
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
/* We expect the romstage to always initialize it. */
|
2013-06-23 16:01:29 +02:00
|
|
|
if (!cbmem_reinit()) {
|
2011-09-21 02:07:14 +02:00
|
|
|
#if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
|
|
|
|
/* Something went wrong, our high memory area got wiped */
|
|
|
|
if (acpi_slp_type == 3 || acpi_slp_type == 2)
|
2010-02-22 07:09:43 +01:00
|
|
|
acpi_slp_type = 0;
|
2011-09-21 02:07:14 +02:00
|
|
|
#endif
|
2013-09-04 13:36:31 +02:00
|
|
|
cbmem_init();
|
2011-09-21 02:07:14 +02:00
|
|
|
rv = 1;
|
2009-10-26 18:04:28 +01:00
|
|
|
}
|
2011-09-21 02:07:14 +02:00
|
|
|
#ifndef __PRE_RAM__
|
2009-10-26 18:04:28 +01:00
|
|
|
cbmem_arch_init();
|
2011-09-21 02:07:14 +02:00
|
|
|
#endif
|
2013-05-10 07:33:32 +02:00
|
|
|
/* Migrate cache-as-ram variables. */
|
|
|
|
car_migrate_variables();
|
|
|
|
|
2011-09-21 02:07:14 +02:00
|
|
|
return rv;
|
2009-10-26 18:04:28 +01:00
|
|
|
}
|
2013-10-11 21:08:02 +02:00
|
|
|
#endif
|
2009-10-26 18:04:28 +01:00
|
|
|
|
2009-11-06 18:02:51 +01:00
|
|
|
#ifndef __PRE_RAM__
|
2013-04-24 23:39:08 +02:00
|
|
|
static void init_cbmem_post_device(void *unused)
|
2013-03-13 18:41:44 +01:00
|
|
|
{
|
|
|
|
cbmem_initialize();
|
|
|
|
#if CONFIG_CONSOLE_CBMEM
|
|
|
|
cbmemc_reinit();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-24 23:39:08 +02:00
|
|
|
BOOT_STATE_INIT_ENTRIES(cbmem_bscb) = {
|
|
|
|
BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_ENTRY,
|
|
|
|
init_cbmem_post_device, NULL),
|
|
|
|
};
|
|
|
|
|
2013-09-04 13:11:08 +02:00
|
|
|
int cbmem_base_check(void)
|
|
|
|
{
|
2013-06-23 19:03:50 +02:00
|
|
|
if (!cbmem_base) {
|
2013-09-04 13:11:08 +02:00
|
|
|
printk(BIOS_ERR, "ERROR: CBMEM Base is not set.\n");
|
|
|
|
// Are there any boards without?
|
|
|
|
// Stepan thinks we should die() here!
|
|
|
|
}
|
2013-06-23 19:03:50 +02:00
|
|
|
printk(BIOS_DEBUG, "CBMEM Base is %llx.\n", cbmem_base);
|
|
|
|
return !!cbmem_base;
|
2013-09-04 13:11:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cbmem_add_lb_mem(struct lb_memory *mem)
|
|
|
|
{
|
2013-06-23 19:03:50 +02:00
|
|
|
lb_add_memory_range(mem, LB_MEM_TABLE, cbmem_base, cbmem_size);
|
2013-09-04 13:11:08 +02:00
|
|
|
}
|
|
|
|
|
2009-10-26 18:04:28 +01:00
|
|
|
void cbmem_list(void)
|
|
|
|
{
|
|
|
|
struct cbmem_entry *cbmem_toc;
|
|
|
|
int i;
|
2010-03-31 16:57:55 +02:00
|
|
|
cbmem_toc = get_cbmem_toc();
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-10-26 18:04:28 +01:00
|
|
|
if (cbmem_toc == NULL)
|
2009-10-27 15:29:29 +01:00
|
|
|
return;
|
2009-10-26 18:04:28 +01:00
|
|
|
|
|
|
|
for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
|
|
|
|
|
|
|
|
if (cbmem_toc[i].magic != CBMEM_MAGIC)
|
|
|
|
continue;
|
2013-03-13 18:41:44 +01:00
|
|
|
cbmem_print_entry(i, cbmem_toc[i].id, cbmem_toc[i].base,
|
|
|
|
cbmem_toc[i].size);
|
2009-10-26 18:04:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|