#include <linux/kernel.h>
#include <linux/acpi.h>
#include <linux/completion.h>
#include <linux/idr.h>
#include <linux/io.h>
#include <acpi/pcc.h>
#define PCC_CMD_WAIT_RETRIES_NUM 500ULL
#define PCC_SIGNATURE_SIZE sizeof(u32)
struct pcc_data {
struct pcc_mbox_chan *pcc_chan;
struct completion done;
struct mbox_client cl;
struct acpi_pcc_info ctx;
};
static struct acpi_pcc_info pcc_ctx;
static void pcc_rx_callback(struct mbox_client *cl, void *m)
{
struct pcc_data *data = container_of(cl, struct pcc_data, cl);
complete(&data->done);
}
static acpi_status
acpi_pcc_address_space_setup(acpi_handle region_handle, u32 function,
void *handler_context, void **region_context)
{
struct acpi_pcc_info *ctx = handler_context;
struct pcc_mbox_chan *pcc_chan;
struct pcc_data *data;
acpi_status ret;
u64 usecs_lat;
if (function == ACPI_REGION_DEACTIVATE) {
data = *region_context;
if (data) {
pcc_mbox_free_channel(data->pcc_chan);
kfree(data);
*region_context = NULL;
}
return AE_OK;
}
if (function != ACPI_REGION_ACTIVATE)
return AE_BAD_PARAMETER;
data = kzalloc_obj(*data);
if (!data)
return AE_NO_MEMORY;
data->cl.rx_callback = pcc_rx_callback;
data->cl.knows_txdone = true;
data->ctx.length = ctx->length;
data->ctx.subspace_id = ctx->subspace_id;
data->ctx.internal_buffer = ctx->internal_buffer;
init_completion(&data->done);
data->pcc_chan = pcc_mbox_request_channel(&data->cl, ctx->subspace_id);
if (IS_ERR(data->pcc_chan)) {
pr_err("Failed to find PCC channel for subspace %d\n",
ctx->subspace_id);
ret = AE_NOT_FOUND;
goto err_free_data;
}
pcc_chan = data->pcc_chan;
if (pcc_chan->shmem_size < PCC_SIGNATURE_SIZE ||
ctx->length > pcc_chan->shmem_size - PCC_SIGNATURE_SIZE) {
pr_err("PCC channel-%d shared memory is too small.\n",
ctx->subspace_id);
ret = AE_AML_REGION_LIMIT;
goto err_free_channel;
}
if (!pcc_chan->mchan->mbox->txdone_irq) {
pr_err("This channel-%d does not support interrupt.\n",
ctx->subspace_id);
ret = AE_SUPPORT;
goto err_free_channel;
}
usecs_lat = PCC_CMD_WAIT_RETRIES_NUM * pcc_chan->latency;
data->cl.tx_tout = DIV_ROUND_UP_ULL(usecs_lat, 1000);
if (!data->cl.tx_tout)
data->cl.tx_tout = 1;
*region_context = data;
return AE_OK;
err_free_channel:
pcc_mbox_free_channel(data->pcc_chan);
err_free_data:
kfree(data);
return ret;
}
static acpi_status
acpi_pcc_address_space_handler(u32 function, acpi_physical_address addr,
u32 bits, acpi_integer *value,
void *handler_context, void *region_context)
{
struct pcc_data *data = region_context;
void __iomem *pcc_opregion;
int ret;
pcc_opregion = data->pcc_chan->shmem + PCC_SIGNATURE_SIZE;
reinit_completion(&data->done);
memcpy_toio(pcc_opregion, (void *)value, data->ctx.length);
ret = mbox_send_message(data->pcc_chan->mchan, NULL);
if (ret < 0)
return AE_ERROR;
ret = wait_for_completion_timeout(&data->done,
msecs_to_jiffies(data->cl.tx_tout));
if (ret == 0) {
pr_err("PCC command executed timeout!\n");
return AE_TIME;
}
mbox_chan_txdone(data->pcc_chan->mchan, ret);
memcpy_fromio(value, pcc_opregion, data->ctx.length);
return AE_OK;
}
void __init acpi_init_pcc(void)
{
acpi_status status;
status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
ACPI_ADR_SPACE_PLATFORM_COMM,
&acpi_pcc_address_space_handler,
&acpi_pcc_address_space_setup,
&pcc_ctx);
if (ACPI_FAILURE(status))
pr_alert("OperationRegion handler could not be installed\n");
}