1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2005 Intel Corporation
4  * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5  *
6  *      Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
7  *      - Added _PDC for platforms with Intel CPUs
8  */
9 
10 #define pr_fmt(fmt) "ACPI: " fmt
11 
12 #include <linux/dmi.h>
13 #include <linux/slab.h>
14 #include <linux/acpi.h>
15 #include <acpi/processor.h>
16 
17 #include <xen/xen.h>
18 
19 #include "internal.h"
20 
21 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
22 ACPI_MODULE_NAME("processor_pdc");
23 
processor_physically_present(acpi_handle handle)24 static bool __init processor_physically_present(acpi_handle handle)
25 {
26 	int cpuid, type;
27 	u32 acpi_id;
28 	acpi_status status;
29 	acpi_object_type acpi_type;
30 	unsigned long long tmp;
31 	union acpi_object object = { 0 };
32 	struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
33 
34 	status = acpi_get_type(handle, &acpi_type);
35 	if (ACPI_FAILURE(status))
36 		return false;
37 
38 	switch (acpi_type) {
39 	case ACPI_TYPE_PROCESSOR:
40 		status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
41 		if (ACPI_FAILURE(status))
42 			return false;
43 		acpi_id = object.processor.proc_id;
44 		break;
45 	case ACPI_TYPE_DEVICE:
46 		status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
47 		if (ACPI_FAILURE(status))
48 			return false;
49 		acpi_id = tmp;
50 		break;
51 	default:
52 		return false;
53 	}
54 
55 	if (xen_initial_domain())
56 		/*
57 		 * When running as a Xen dom0 the number of processors Linux
58 		 * sees can be different from the real number of processors on
59 		 * the system, and we still need to execute _PDC for all of
60 		 * them.
61 		 */
62 		return xen_processor_present(acpi_id);
63 
64 	type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
65 	cpuid = acpi_get_cpuid(handle, type, acpi_id);
66 
67 	return !invalid_logical_cpuid(cpuid);
68 }
69 
acpi_set_pdc_bits(u32 *buf)70 static void acpi_set_pdc_bits(u32 *buf)
71 {
72 	buf[0] = ACPI_PDC_REVISION_ID;
73 	buf[1] = 1;
74 
75 	/* Enable coordination with firmware's _TSD info */
76 	buf[2] = ACPI_PDC_SMP_T_SWCOORD;
77 
78 	/* Twiddle arch-specific bits needed for _PDC */
79 	arch_acpi_set_pdc_bits(buf);
80 }
81 
acpi_processor_alloc_pdc(void)82 static struct acpi_object_list *acpi_processor_alloc_pdc(void)
83 {
84 	struct acpi_object_list *obj_list;
85 	union acpi_object *obj;
86 	u32 *buf;
87 
88 	/* allocate and initialize pdc. It will be used later. */
89 	obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);
90 	if (!obj_list)
91 		goto out;
92 
93 	obj = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
94 	if (!obj) {
95 		kfree(obj_list);
96 		goto out;
97 	}
98 
99 	buf = kmalloc(12, GFP_KERNEL);
100 	if (!buf) {
101 		kfree(obj);
102 		kfree(obj_list);
103 		goto out;
104 	}
105 
106 	acpi_set_pdc_bits(buf);
107 
108 	obj->type = ACPI_TYPE_BUFFER;
109 	obj->buffer.length = 12;
110 	obj->buffer.pointer = (u8 *) buf;
111 	obj_list->count = 1;
112 	obj_list->pointer = obj;
113 
114 	return obj_list;
115 out:
116 	pr_err("Memory allocation error\n");
117 	return NULL;
118 }
119 
120 /*
121  * _PDC is required for a BIOS-OS handshake for most of the newer
122  * ACPI processor features.
123  */
124 static acpi_status
acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)125 acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
126 {
127 	acpi_status status = AE_OK;
128 
129 	if (boot_option_idle_override == IDLE_NOMWAIT) {
130 		/*
131 		 * If mwait is disabled for CPU C-states, the C2C3_FFH access
132 		 * mode will be disabled in the parameter of _PDC object.
133 		 * Of course C1_FFH access mode will also be disabled.
134 		 */
135 		union acpi_object *obj;
136 		u32 *buffer = NULL;
137 
138 		obj = pdc_in->pointer;
139 		buffer = (u32 *)(obj->buffer.pointer);
140 		buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
141 
142 	}
143 	status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
144 
145 	if (ACPI_FAILURE(status))
146 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
147 		    "Could not evaluate _PDC, using legacy perf. control.\n"));
148 
149 	return status;
150 }
151 
acpi_processor_set_pdc(acpi_handle handle)152 void acpi_processor_set_pdc(acpi_handle handle)
153 {
154 	struct acpi_object_list *obj_list;
155 
156 	if (arch_has_acpi_pdc() == false)
157 		return;
158 
159 	obj_list = acpi_processor_alloc_pdc();
160 	if (!obj_list)
161 		return;
162 
163 	acpi_processor_eval_pdc(handle, obj_list);
164 
165 	kfree(obj_list->pointer->buffer.pointer);
166 	kfree(obj_list->pointer);
167 	kfree(obj_list);
168 }
169 
170 static acpi_status __init
early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)171 early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
172 {
173 	if (processor_physically_present(handle) == false)
174 		return AE_OK;
175 
176 	acpi_processor_set_pdc(handle);
177 	return AE_OK;
178 }
179 
set_no_mwait(const struct dmi_system_id *id)180 static int __init set_no_mwait(const struct dmi_system_id *id)
181 {
182 	pr_notice("%s detected - disabling mwait for CPU C-states\n",
183 		  id->ident);
184 	boot_option_idle_override = IDLE_NOMWAIT;
185 	return 0;
186 }
187 
188 static const struct dmi_system_id processor_idle_dmi_table[] __initconst = {
189 	{
190 	set_no_mwait, "Extensa 5220", {
191 	DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
192 	DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
193 	DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
194 	DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
195 	{},
196 };
197 
processor_dmi_check(void)198 static void __init processor_dmi_check(void)
199 {
200 	/*
201 	 * Check whether the system is DMI table. If yes, OSPM
202 	 * should not use mwait for CPU-states.
203 	 */
204 	dmi_check_system(processor_idle_dmi_table);
205 }
206 
acpi_early_processor_set_pdc(void)207 void __init acpi_early_processor_set_pdc(void)
208 {
209 	processor_dmi_check();
210 
211 	acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
212 			    ACPI_UINT32_MAX,
213 			    early_init_pdc, NULL, NULL, NULL);
214 	acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, early_init_pdc, NULL, NULL);
215 }
216