blob: df3d7ba417dd6550a588356b68cbd08821c69ada [file] [log] [blame]
// SPDX-License-Identifier: GPL-2.0+
/*
* Core driver model support for ACPI table generation
*
* Copyright 2019 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#define LOG_CATEOGRY LOGC_ACPI
#include <common.h>
#include <malloc.h>
#include <dm.h>
#include <log.h>
#include <dm/acpi.h>
#include <dm/device-internal.h>
#include <dm/root.h>
#define MAX_ACPI_ITEMS 100
/* Type of table that we collected */
enum gen_type_t {
TYPE_NONE,
TYPE_SSDT,
};
/* Type of method to call */
enum method_t {
METHOD_WRITE_TABLES,
METHOD_FILL_SSDT,
};
/* Prototype for all methods */
typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
/**
* struct acpi_item - Holds info about ACPI data generated by a driver method
*
* @dev: Device that generated this data
* @type: Table type it refers to
* @buf: Buffer containing the data
* @size: Size of the data in bytes
*/
struct acpi_item {
struct udevice *dev;
enum gen_type_t type;
char *buf;
int size;
};
/* List of ACPI items collected */
static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
static int item_count;
int acpi_copy_name(char *out_name, const char *name)
{
strncpy(out_name, name, ACPI_NAME_LEN);
out_name[ACPI_NAME_LEN] = '\0';
return 0;
}
int acpi_get_name(const struct udevice *dev, char *out_name)
{
struct acpi_ops *aops;
aops = device_get_acpi_ops(dev);
if (aops && aops->get_name)
return aops->get_name(dev, out_name);
return -ENOSYS;
}
/**
* acpi_add_item() - Add a new item to the list of data collected
*
* @ctx: ACPI context
* @dev: Device that generated the data
* @type: Table type it refers to
* @start: The start of the data (the end is obtained from ctx->current)
* @return 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
*/
static int acpi_add_item(struct acpi_ctx *ctx, struct udevice *dev,
enum gen_type_t type, void *start)
{
struct acpi_item *item;
void *end = ctx->current;
if (item_count == MAX_ACPI_ITEMS) {
log_err("Too many items\n");
return log_msg_ret("mem", -ENOSPC);
}
item = &acpi_item[item_count];
item->dev = dev;
item->type = type;
item->size = end - start;
if (!item->size)
return 0;
item->buf = malloc(item->size);
if (!item->buf)
return log_msg_ret("mem", -ENOMEM);
memcpy(item->buf, start, item->size);
item_count++;
log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
item->size);
return 0;
}
acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
{
struct acpi_ops *aops;
aops = device_get_acpi_ops(dev);
if (aops) {
switch (method) {
case METHOD_WRITE_TABLES:
return aops->write_tables;
case METHOD_FILL_SSDT:
return aops->fill_ssdt;
}
}
return NULL;
}
int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
enum method_t method, enum gen_type_t type)
{
struct udevice *dev;
acpi_method func;
int ret;
func = acpi_get_method(parent, method);
if (func) {
void *start = ctx->current;
log_debug("\n");
log_debug("- %s %p\n", parent->name, func);
ret = device_ofdata_to_platdata(parent);
if (ret)
return log_msg_ret("ofdata", ret);
ret = func(parent, ctx);
if (ret)
return log_msg_ret("func", ret);
/* Add the item to the internal list */
if (type != TYPE_NONE) {
ret = acpi_add_item(ctx, parent, type, start);
if (ret)
return log_msg_ret("add", ret);
}
}
device_foreach_child(dev, parent) {
ret = acpi_recurse_method(ctx, dev, method, type);
if (ret)
return log_msg_ret("recurse", ret);
}
return 0;
}
int acpi_fill_ssdt(struct acpi_ctx *ctx)
{
int ret;
log_debug("Writing SSDT tables\n");
ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
log_debug("Writing SSDT finished, err=%d\n", ret);
return ret;
}
int acpi_write_dev_tables(struct acpi_ctx *ctx)
{
int ret;
log_debug("Writing device tables\n");
ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
TYPE_NONE);
log_debug("Writing finished, err=%d\n", ret);
return ret;
}