1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Address translation interface via ACPI DSM.
4*4882a593Smuzhiyun * Copyright (C) 2018 Intel Corporation
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Specification for this interface is available at:
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * https://cdrdv2.intel.com/v1/dl/getContent/603354
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/acpi.h>
12*4882a593Smuzhiyun #include <linux/adxl.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define ADXL_REVISION 0x1
15*4882a593Smuzhiyun #define ADXL_IDX_GET_ADDR_PARAMS 0x1
16*4882a593Smuzhiyun #define ADXL_IDX_FORWARD_TRANSLATE 0x2
17*4882a593Smuzhiyun #define ACPI_ADXL_PATH "\\_SB.ADXL"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun * The specification doesn't provide a limit on how many
21*4882a593Smuzhiyun * components are in a memory address. But since we allocate
22*4882a593Smuzhiyun * memory based on the number the BIOS tells us, we should
23*4882a593Smuzhiyun * defend against insane values.
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun #define ADXL_MAX_COMPONENTS 500
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #undef pr_fmt
28*4882a593Smuzhiyun #define pr_fmt(fmt) "ADXL: " fmt
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun static acpi_handle handle;
31*4882a593Smuzhiyun static union acpi_object *params;
32*4882a593Smuzhiyun static const guid_t adxl_guid =
33*4882a593Smuzhiyun GUID_INIT(0xAA3C050A, 0x7EA4, 0x4C1F,
34*4882a593Smuzhiyun 0xAF, 0xDA, 0x12, 0x67, 0xDF, 0xD3, 0xD4, 0x8D);
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun static int adxl_count;
37*4882a593Smuzhiyun static char **adxl_component_names;
38*4882a593Smuzhiyun
adxl_dsm(int cmd,union acpi_object argv[])39*4882a593Smuzhiyun static union acpi_object *adxl_dsm(int cmd, union acpi_object argv[])
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun union acpi_object *obj, *o;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun obj = acpi_evaluate_dsm_typed(handle, &adxl_guid, ADXL_REVISION,
44*4882a593Smuzhiyun cmd, argv, ACPI_TYPE_PACKAGE);
45*4882a593Smuzhiyun if (!obj) {
46*4882a593Smuzhiyun pr_info("DSM call failed for cmd=%d\n", cmd);
47*4882a593Smuzhiyun return NULL;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun if (obj->package.count != 2) {
51*4882a593Smuzhiyun pr_info("Bad pkg count %d\n", obj->package.count);
52*4882a593Smuzhiyun goto err;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun o = obj->package.elements;
56*4882a593Smuzhiyun if (o->type != ACPI_TYPE_INTEGER) {
57*4882a593Smuzhiyun pr_info("Bad 1st element type %d\n", o->type);
58*4882a593Smuzhiyun goto err;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun if (o->integer.value) {
61*4882a593Smuzhiyun pr_info("Bad ret val %llu\n", o->integer.value);
62*4882a593Smuzhiyun goto err;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun o = obj->package.elements + 1;
66*4882a593Smuzhiyun if (o->type != ACPI_TYPE_PACKAGE) {
67*4882a593Smuzhiyun pr_info("Bad 2nd element type %d\n", o->type);
68*4882a593Smuzhiyun goto err;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun return obj;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun err:
73*4882a593Smuzhiyun ACPI_FREE(obj);
74*4882a593Smuzhiyun return NULL;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun * adxl_get_component_names - get list of memory component names
79*4882a593Smuzhiyun * Returns NULL terminated list of string names
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * Give the caller a pointer to the list of memory component names
82*4882a593Smuzhiyun * e.g. { "SystemAddress", "ProcessorSocketId", "ChannelId", ... NULL }
83*4882a593Smuzhiyun * Caller should count how many strings in order to allocate a buffer
84*4882a593Smuzhiyun * for the return from adxl_decode().
85*4882a593Smuzhiyun */
adxl_get_component_names(void)86*4882a593Smuzhiyun const char * const *adxl_get_component_names(void)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun return (const char * const *)adxl_component_names;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(adxl_get_component_names);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun * adxl_decode - ask BIOS to decode a system address to memory address
94*4882a593Smuzhiyun * @addr: the address to decode
95*4882a593Smuzhiyun * @component_values: pointer to array of values for each component
96*4882a593Smuzhiyun * Returns 0 on success, negative error code otherwise
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * The index of each value returned in the array matches the index of
99*4882a593Smuzhiyun * each component name returned by adxl_get_component_names().
100*4882a593Smuzhiyun * Components that are not defined for this address translation (e.g.
101*4882a593Smuzhiyun * mirror channel number for a non-mirrored address) are set to ~0ull.
102*4882a593Smuzhiyun */
adxl_decode(u64 addr,u64 component_values[])103*4882a593Smuzhiyun int adxl_decode(u64 addr, u64 component_values[])
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun union acpi_object argv4[2], *results, *r;
106*4882a593Smuzhiyun int i, cnt;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun if (!adxl_component_names)
109*4882a593Smuzhiyun return -EOPNOTSUPP;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun argv4[0].type = ACPI_TYPE_PACKAGE;
112*4882a593Smuzhiyun argv4[0].package.count = 1;
113*4882a593Smuzhiyun argv4[0].package.elements = &argv4[1];
114*4882a593Smuzhiyun argv4[1].integer.type = ACPI_TYPE_INTEGER;
115*4882a593Smuzhiyun argv4[1].integer.value = addr;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun results = adxl_dsm(ADXL_IDX_FORWARD_TRANSLATE, argv4);
118*4882a593Smuzhiyun if (!results)
119*4882a593Smuzhiyun return -EINVAL;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun r = results->package.elements + 1;
122*4882a593Smuzhiyun cnt = r->package.count;
123*4882a593Smuzhiyun if (cnt != adxl_count) {
124*4882a593Smuzhiyun ACPI_FREE(results);
125*4882a593Smuzhiyun return -EINVAL;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun r = r->package.elements;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun for (i = 0; i < cnt; i++)
130*4882a593Smuzhiyun component_values[i] = r[i].integer.value;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun ACPI_FREE(results);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun return 0;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(adxl_decode);
137*4882a593Smuzhiyun
adxl_init(void)138*4882a593Smuzhiyun static int __init adxl_init(void)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun char *path = ACPI_ADXL_PATH;
141*4882a593Smuzhiyun union acpi_object *p;
142*4882a593Smuzhiyun acpi_status status;
143*4882a593Smuzhiyun int i;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun status = acpi_get_handle(NULL, path, &handle);
146*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
147*4882a593Smuzhiyun pr_debug("No ACPI handle for path %s\n", path);
148*4882a593Smuzhiyun return -ENODEV;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if (!acpi_has_method(handle, "_DSM")) {
152*4882a593Smuzhiyun pr_info("No DSM method\n");
153*4882a593Smuzhiyun return -ENODEV;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (!acpi_check_dsm(handle, &adxl_guid, ADXL_REVISION,
157*4882a593Smuzhiyun ADXL_IDX_GET_ADDR_PARAMS |
158*4882a593Smuzhiyun ADXL_IDX_FORWARD_TRANSLATE)) {
159*4882a593Smuzhiyun pr_info("DSM method does not support forward translate\n");
160*4882a593Smuzhiyun return -ENODEV;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun params = adxl_dsm(ADXL_IDX_GET_ADDR_PARAMS, NULL);
164*4882a593Smuzhiyun if (!params) {
165*4882a593Smuzhiyun pr_info("Failed to get component names\n");
166*4882a593Smuzhiyun return -ENODEV;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun p = params->package.elements + 1;
170*4882a593Smuzhiyun adxl_count = p->package.count;
171*4882a593Smuzhiyun if (adxl_count > ADXL_MAX_COMPONENTS) {
172*4882a593Smuzhiyun pr_info("Insane number of address component names %d\n", adxl_count);
173*4882a593Smuzhiyun ACPI_FREE(params);
174*4882a593Smuzhiyun return -ENODEV;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun p = p->package.elements;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun * Allocate one extra for NULL termination.
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun adxl_component_names = kcalloc(adxl_count + 1, sizeof(char *), GFP_KERNEL);
182*4882a593Smuzhiyun if (!adxl_component_names) {
183*4882a593Smuzhiyun ACPI_FREE(params);
184*4882a593Smuzhiyun return -ENOMEM;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun for (i = 0; i < adxl_count; i++)
188*4882a593Smuzhiyun adxl_component_names[i] = p[i].string.pointer;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun return 0;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun subsys_initcall(adxl_init);
193