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