18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Address translation interface via ACPI DSM.
48c2ecf20Sopenharmony_ci * Copyright (C) 2018 Intel Corporation
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Specification for this interface is available at:
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *	https://cdrdv2.intel.com/v1/dl/getContent/603354
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/acpi.h>
128c2ecf20Sopenharmony_ci#include <linux/adxl.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define ADXL_REVISION			0x1
158c2ecf20Sopenharmony_ci#define ADXL_IDX_GET_ADDR_PARAMS	0x1
168c2ecf20Sopenharmony_ci#define ADXL_IDX_FORWARD_TRANSLATE	0x2
178c2ecf20Sopenharmony_ci#define ACPI_ADXL_PATH			"\\_SB.ADXL"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * The specification doesn't provide a limit on how many
218c2ecf20Sopenharmony_ci * components are in a memory address. But since we allocate
228c2ecf20Sopenharmony_ci * memory based on the number the BIOS tells us, we should
238c2ecf20Sopenharmony_ci * defend against insane values.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci#define ADXL_MAX_COMPONENTS		500
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#undef pr_fmt
288c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "ADXL: " fmt
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic acpi_handle handle;
318c2ecf20Sopenharmony_cistatic union acpi_object *params;
328c2ecf20Sopenharmony_cistatic const guid_t adxl_guid =
338c2ecf20Sopenharmony_ci	GUID_INIT(0xAA3C050A, 0x7EA4, 0x4C1F,
348c2ecf20Sopenharmony_ci		  0xAF, 0xDA, 0x12, 0x67, 0xDF, 0xD3, 0xD4, 0x8D);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic int adxl_count;
378c2ecf20Sopenharmony_cistatic char **adxl_component_names;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic union acpi_object *adxl_dsm(int cmd, union acpi_object argv[])
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	union acpi_object *obj, *o;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	obj = acpi_evaluate_dsm_typed(handle, &adxl_guid, ADXL_REVISION,
448c2ecf20Sopenharmony_ci				      cmd, argv, ACPI_TYPE_PACKAGE);
458c2ecf20Sopenharmony_ci	if (!obj) {
468c2ecf20Sopenharmony_ci		pr_info("DSM call failed for cmd=%d\n", cmd);
478c2ecf20Sopenharmony_ci		return NULL;
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (obj->package.count != 2) {
518c2ecf20Sopenharmony_ci		pr_info("Bad pkg count %d\n", obj->package.count);
528c2ecf20Sopenharmony_ci		goto err;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	o = obj->package.elements;
568c2ecf20Sopenharmony_ci	if (o->type != ACPI_TYPE_INTEGER) {
578c2ecf20Sopenharmony_ci		pr_info("Bad 1st element type %d\n", o->type);
588c2ecf20Sopenharmony_ci		goto err;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	if (o->integer.value) {
618c2ecf20Sopenharmony_ci		pr_info("Bad ret val %llu\n", o->integer.value);
628c2ecf20Sopenharmony_ci		goto err;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	o = obj->package.elements + 1;
668c2ecf20Sopenharmony_ci	if (o->type != ACPI_TYPE_PACKAGE) {
678c2ecf20Sopenharmony_ci		pr_info("Bad 2nd element type %d\n", o->type);
688c2ecf20Sopenharmony_ci		goto err;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci	return obj;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cierr:
738c2ecf20Sopenharmony_ci	ACPI_FREE(obj);
748c2ecf20Sopenharmony_ci	return NULL;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/**
788c2ecf20Sopenharmony_ci * adxl_get_component_names - get list of memory component names
798c2ecf20Sopenharmony_ci * Returns NULL terminated list of string names
808c2ecf20Sopenharmony_ci *
818c2ecf20Sopenharmony_ci * Give the caller a pointer to the list of memory component names
828c2ecf20Sopenharmony_ci * e.g. { "SystemAddress", "ProcessorSocketId", "ChannelId", ... NULL }
838c2ecf20Sopenharmony_ci * Caller should count how many strings in order to allocate a buffer
848c2ecf20Sopenharmony_ci * for the return from adxl_decode().
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_ciconst char * const *adxl_get_component_names(void)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return (const char * const *)adxl_component_names;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(adxl_get_component_names);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/**
938c2ecf20Sopenharmony_ci * adxl_decode - ask BIOS to decode a system address to memory address
948c2ecf20Sopenharmony_ci * @addr: the address to decode
958c2ecf20Sopenharmony_ci * @component_values: pointer to array of values for each component
968c2ecf20Sopenharmony_ci * Returns 0 on success, negative error code otherwise
978c2ecf20Sopenharmony_ci *
988c2ecf20Sopenharmony_ci * The index of each value returned in the array matches the index of
998c2ecf20Sopenharmony_ci * each component name returned by adxl_get_component_names().
1008c2ecf20Sopenharmony_ci * Components that are not defined for this address translation (e.g.
1018c2ecf20Sopenharmony_ci * mirror channel number for a non-mirrored address) are set to ~0ull.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ciint adxl_decode(u64 addr, u64 component_values[])
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	union acpi_object argv4[2], *results, *r;
1068c2ecf20Sopenharmony_ci	int i, cnt;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	if (!adxl_component_names)
1098c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	argv4[0].type = ACPI_TYPE_PACKAGE;
1128c2ecf20Sopenharmony_ci	argv4[0].package.count = 1;
1138c2ecf20Sopenharmony_ci	argv4[0].package.elements = &argv4[1];
1148c2ecf20Sopenharmony_ci	argv4[1].integer.type = ACPI_TYPE_INTEGER;
1158c2ecf20Sopenharmony_ci	argv4[1].integer.value = addr;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	results = adxl_dsm(ADXL_IDX_FORWARD_TRANSLATE, argv4);
1188c2ecf20Sopenharmony_ci	if (!results)
1198c2ecf20Sopenharmony_ci		return -EINVAL;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	r = results->package.elements + 1;
1228c2ecf20Sopenharmony_ci	cnt = r->package.count;
1238c2ecf20Sopenharmony_ci	if (cnt != adxl_count) {
1248c2ecf20Sopenharmony_ci		ACPI_FREE(results);
1258c2ecf20Sopenharmony_ci		return -EINVAL;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	r = r->package.elements;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	for (i = 0; i < cnt; i++)
1308c2ecf20Sopenharmony_ci		component_values[i] = r[i].integer.value;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	ACPI_FREE(results);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(adxl_decode);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int __init adxl_init(void)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	char *path = ACPI_ADXL_PATH;
1418c2ecf20Sopenharmony_ci	union acpi_object *p;
1428c2ecf20Sopenharmony_ci	acpi_status status;
1438c2ecf20Sopenharmony_ci	int i;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	status = acpi_get_handle(NULL, path, &handle);
1468c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(status)) {
1478c2ecf20Sopenharmony_ci		pr_debug("No ACPI handle for path %s\n", path);
1488c2ecf20Sopenharmony_ci		return -ENODEV;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	if (!acpi_has_method(handle, "_DSM")) {
1528c2ecf20Sopenharmony_ci		pr_info("No DSM method\n");
1538c2ecf20Sopenharmony_ci		return -ENODEV;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (!acpi_check_dsm(handle, &adxl_guid, ADXL_REVISION,
1578c2ecf20Sopenharmony_ci			    ADXL_IDX_GET_ADDR_PARAMS |
1588c2ecf20Sopenharmony_ci			    ADXL_IDX_FORWARD_TRANSLATE)) {
1598c2ecf20Sopenharmony_ci		pr_info("DSM method does not support forward translate\n");
1608c2ecf20Sopenharmony_ci		return -ENODEV;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	params = adxl_dsm(ADXL_IDX_GET_ADDR_PARAMS, NULL);
1648c2ecf20Sopenharmony_ci	if (!params) {
1658c2ecf20Sopenharmony_ci		pr_info("Failed to get component names\n");
1668c2ecf20Sopenharmony_ci		return -ENODEV;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	p = params->package.elements + 1;
1708c2ecf20Sopenharmony_ci	adxl_count = p->package.count;
1718c2ecf20Sopenharmony_ci	if (adxl_count > ADXL_MAX_COMPONENTS) {
1728c2ecf20Sopenharmony_ci		pr_info("Insane number of address component names %d\n", adxl_count);
1738c2ecf20Sopenharmony_ci		ACPI_FREE(params);
1748c2ecf20Sopenharmony_ci		return -ENODEV;
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci	p = p->package.elements;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/*
1798c2ecf20Sopenharmony_ci	 * Allocate one extra for NULL termination.
1808c2ecf20Sopenharmony_ci	 */
1818c2ecf20Sopenharmony_ci	adxl_component_names = kcalloc(adxl_count + 1, sizeof(char *), GFP_KERNEL);
1828c2ecf20Sopenharmony_ci	if (!adxl_component_names) {
1838c2ecf20Sopenharmony_ci		ACPI_FREE(params);
1848c2ecf20Sopenharmony_ci		return -ENOMEM;
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	for (i = 0; i < adxl_count; i++)
1888c2ecf20Sopenharmony_ci		adxl_component_names[i] = p[i].string.pointer;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return 0;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_cisubsys_initcall(adxl_init);
193