1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: exserial - field_unit support for serial address spaces
6 * Copyright (C) 2000 - 2021, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
16 #define _COMPONENT ACPI_EXECUTER
17 ACPI_MODULE_NAME("exserial")
19 /*******************************************************************************
21 * FUNCTION: acpi_ex_read_gpio
23 * PARAMETERS: obj_desc - The named field to read
24 * buffer - Where the return data is returned
28 * DESCRIPTION: Read from a named field that references a Generic Serial Bus
31 ******************************************************************************/
32 acpi_status acpi_ex_read_gpio(union acpi_operand_object *obj_desc, void *buffer)
36 ACPI_FUNCTION_TRACE_PTR(ex_read_gpio, obj_desc);
39 * For GPIO (general_purpose_io), the Address will be the bit offset
40 * from the previous Connection() operator, making it effectively a
41 * pin number index. The bit_length is the length of the field, which
42 * is thus the number of pins.
44 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
45 "GPIO FieldRead [FROM]: Pin %u Bits %u\n",
46 obj_desc->field.pin_number_index,
47 obj_desc->field.bit_length));
49 /* Lock entire transaction if requested */
51 acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
53 /* Perform the read */
55 status = acpi_ex_access_region(obj_desc, 0, (u64 *)buffer, ACPI_READ);
57 acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
58 return_ACPI_STATUS(status);
61 /*******************************************************************************
63 * FUNCTION: acpi_ex_write_gpio
65 * PARAMETERS: source_desc - Contains data to write. Expect to be
67 * obj_desc - The named field
68 * result_desc - Where the return value is returned, if any
72 * DESCRIPTION: Write to a named field that references a General Purpose I/O
75 ******************************************************************************/
78 acpi_ex_write_gpio(union acpi_operand_object *source_desc,
79 union acpi_operand_object *obj_desc,
80 union acpi_operand_object **return_buffer)
85 ACPI_FUNCTION_TRACE_PTR(ex_write_gpio, obj_desc);
88 * For GPIO (general_purpose_io), we will bypass the entire field
89 * mechanism and handoff the bit address and bit width directly to
90 * the handler. The Address will be the bit offset
91 * from the previous Connection() operator, making it effectively a
92 * pin number index. The bit_length is the length of the field, which
93 * is thus the number of pins.
95 if (source_desc->common.type != ACPI_TYPE_INTEGER) {
96 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
99 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
100 "GPIO FieldWrite [FROM]: (%s:%X), Value %.8X [TO]: Pin %u Bits %u\n",
101 acpi_ut_get_type_name(source_desc->common.type),
102 source_desc->common.type,
103 (u32)source_desc->integer.value,
104 obj_desc->field.pin_number_index,
105 obj_desc->field.bit_length));
107 buffer = &source_desc->integer.value;
109 /* Lock entire transaction if requested */
111 acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
113 /* Perform the write */
115 status = acpi_ex_access_region(obj_desc, 0, (u64 *)buffer, ACPI_WRITE);
116 acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
117 return_ACPI_STATUS(status);
120 /*******************************************************************************
122 * FUNCTION: acpi_ex_read_serial_bus
124 * PARAMETERS: obj_desc - The named field to read
125 * return_buffer - Where the return value is returned, if any
129 * DESCRIPTION: Read from a named field that references a serial bus
130 * (SMBus, IPMI, or GSBus).
132 ******************************************************************************/
135 acpi_ex_read_serial_bus(union acpi_operand_object *obj_desc,
136 union acpi_operand_object **return_buffer)
140 union acpi_operand_object *buffer_desc;
144 ACPI_FUNCTION_TRACE_PTR(ex_read_serial_bus, obj_desc);
147 * This is an SMBus, GSBus or IPMI read. We must create a buffer to
148 * hold the data and then directly access the region handler.
150 * Note: SMBus and GSBus protocol value is passed in upper 16-bits
153 * Common buffer format:
154 * Status; (Byte 0 of the data buffer)
155 * Length; (Byte 1 of the data buffer)
156 * Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
158 switch (obj_desc->field.region_obj->region.space_id) {
159 case ACPI_ADR_SPACE_SMBUS:
161 buffer_length = ACPI_SMBUS_BUFFER_SIZE;
162 function = ACPI_READ | (obj_desc->field.attribute << 16);
165 case ACPI_ADR_SPACE_IPMI:
167 buffer_length = ACPI_IPMI_BUFFER_SIZE;
168 function = ACPI_READ;
171 case ACPI_ADR_SPACE_GSBUS:
173 accessor_type = obj_desc->field.attribute;
174 if (accessor_type == AML_FIELD_ATTRIB_RAW_PROCESS_BYTES) {
176 "Invalid direct read using bidirectional write-then-read protocol"));
178 return_ACPI_STATUS(AE_AML_PROTOCOL);
182 acpi_ex_get_protocol_buffer_length(accessor_type,
184 if (ACPI_FAILURE(status)) {
186 "Invalid protocol ID for GSBus: 0x%4.4X",
189 return_ACPI_STATUS(status);
192 /* Add header length to get the full size of the buffer */
194 buffer_length += ACPI_SERIAL_HEADER_SIZE;
195 function = ACPI_READ | (accessor_type << 16);
198 case ACPI_ADR_SPACE_PLATFORM_RT:
200 buffer_length = ACPI_PRM_INPUT_BUFFER_SIZE;
201 function = ACPI_READ;
205 return_ACPI_STATUS(AE_AML_INVALID_SPACE_ID);
208 /* Create the local transfer buffer that is returned to the caller */
210 buffer_desc = acpi_ut_create_buffer_object(buffer_length);
212 return_ACPI_STATUS(AE_NO_MEMORY);
215 /* Lock entire transaction if requested */
217 acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
219 /* Call the region handler for the write-then-read */
221 status = acpi_ex_access_region(obj_desc, 0,
225 acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
227 *return_buffer = buffer_desc;
228 return_ACPI_STATUS(status);
231 /*******************************************************************************
233 * FUNCTION: acpi_ex_write_serial_bus
235 * PARAMETERS: source_desc - Contains data to write
236 * obj_desc - The named field
237 * return_buffer - Where the return value is returned, if any
241 * DESCRIPTION: Write to a named field that references a serial bus
242 * (SMBus, IPMI, GSBus).
244 ******************************************************************************/
247 acpi_ex_write_serial_bus(union acpi_operand_object *source_desc,
248 union acpi_operand_object *obj_desc,
249 union acpi_operand_object **return_buffer)
255 union acpi_operand_object *buffer_desc;
259 ACPI_FUNCTION_TRACE_PTR(ex_write_serial_bus, obj_desc);
262 * This is an SMBus, GSBus or IPMI write. We will bypass the entire
263 * field mechanism and handoff the buffer directly to the handler.
264 * For these address spaces, the buffer is bidirectional; on a
265 * write, return data is returned in the same buffer.
267 * Source must be a buffer of sufficient size, these are fixed size:
268 * ACPI_SMBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
270 * Note: SMBus and GSBus protocol type is passed in upper 16-bits
273 * Common buffer format:
274 * Status; (Byte 0 of the data buffer)
275 * Length; (Byte 1 of the data buffer)
276 * Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
278 if (source_desc->common.type != ACPI_TYPE_BUFFER) {
280 "SMBus/IPMI/GenericSerialBus write requires "
281 "Buffer, found type %s",
282 acpi_ut_get_object_type_name(source_desc)));
284 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
287 switch (obj_desc->field.region_obj->region.space_id) {
288 case ACPI_ADR_SPACE_SMBUS:
290 buffer_length = ACPI_SMBUS_BUFFER_SIZE;
291 function = ACPI_WRITE | (obj_desc->field.attribute << 16);
294 case ACPI_ADR_SPACE_IPMI:
296 buffer_length = ACPI_IPMI_BUFFER_SIZE;
297 function = ACPI_WRITE;
300 case ACPI_ADR_SPACE_GSBUS:
302 accessor_type = obj_desc->field.attribute;
304 acpi_ex_get_protocol_buffer_length(accessor_type,
306 if (ACPI_FAILURE(status)) {
308 "Invalid protocol ID for GSBus: 0x%4.4X",
311 return_ACPI_STATUS(status);
314 /* Add header length to get the full size of the buffer */
316 buffer_length += ACPI_SERIAL_HEADER_SIZE;
317 function = ACPI_WRITE | (accessor_type << 16);
320 case ACPI_ADR_SPACE_PLATFORM_RT:
322 buffer_length = ACPI_PRM_INPUT_BUFFER_SIZE;
323 function = ACPI_WRITE;
327 return_ACPI_STATUS(AE_AML_INVALID_SPACE_ID);
330 /* Create the transfer/bidirectional/return buffer */
332 buffer_desc = acpi_ut_create_buffer_object(buffer_length);
334 return_ACPI_STATUS(AE_NO_MEMORY);
337 /* Copy the input buffer data to the transfer buffer */
339 buffer = buffer_desc->buffer.pointer;
340 data_length = (buffer_length < source_desc->buffer.length ?
341 buffer_length : source_desc->buffer.length);
342 memcpy(buffer, source_desc->buffer.pointer, data_length);
344 /* Lock entire transaction if requested */
346 acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
349 * Perform the write (returns status and perhaps data in the
352 status = acpi_ex_access_region(obj_desc, 0, (u64 *)buffer, function);
353 acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
355 *return_buffer = buffer_desc;
356 return_ACPI_STATUS(status);