xref: /openbmc/linux/drivers/acpi/acpica/hwxface.c (revision 3371c19c294a4cb3649aa4e84606be8a1d999e61)
1 
2 /******************************************************************************
3  *
4  * Module Name: hwxface - Public ACPICA hardware interfaces
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2008, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acnamesp.h"
48 
49 #define _COMPONENT          ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwxface")
51 
52 /******************************************************************************
53  *
54  * FUNCTION:    acpi_reset
55  *
56  * PARAMETERS:  None
57  *
58  * RETURN:      Status
59  *
60  * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
61  *              support reset register in PCI config space, this must be
62  *              handled separately.
63  *
64  ******************************************************************************/
65 acpi_status acpi_reset(void)
66 {
67 	struct acpi_generic_address *reset_reg;
68 	acpi_status status;
69 
70 	ACPI_FUNCTION_TRACE(acpi_reset);
71 
72 	reset_reg = &acpi_gbl_FADT.reset_register;
73 
74 	/* Check if the reset register is supported */
75 
76 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) ||
77 	    !reset_reg->address) {
78 		return_ACPI_STATUS(AE_NOT_EXIST);
79 	}
80 
81 	/* Write the reset value to the reset register */
82 
83 	status = acpi_write(acpi_gbl_FADT.reset_value, reset_reg);
84 	return_ACPI_STATUS(status);
85 }
86 
87 ACPI_EXPORT_SYMBOL(acpi_reset)
88 
89 /******************************************************************************
90  *
91  * FUNCTION:    acpi_read
92  *
93  * PARAMETERS:  Value               - Where the value is returned
94  *              Reg                 - GAS register structure
95  *
96  * RETURN:      Status
97  *
98  * DESCRIPTION: Read from either memory or IO space.
99  *
100  ******************************************************************************/
101 acpi_status acpi_read(u32 *value, struct acpi_generic_address *reg)
102 {
103 	u32 width;
104 	u64 address;
105 	acpi_status status;
106 
107 	ACPI_FUNCTION_NAME(acpi_read);
108 
109 	/*
110 	 * Must have a valid pointer to a GAS structure, and a non-zero address
111 	 * within.
112 	 */
113 	if (!reg) {
114 		return (AE_BAD_PARAMETER);
115 	}
116 
117 	/* Get a local copy of the address. Handles possible alignment issues */
118 
119 	ACPI_MOVE_64_TO_64(&address, &reg->address);
120 	if (!address) {
121 		return (AE_BAD_ADDRESS);
122 	}
123 
124 	/* Supported widths are 8/16/32 */
125 
126 	width = reg->bit_width;
127 	if ((width != 8) && (width != 16) && (width != 32)) {
128 		return (AE_SUPPORT);
129 	}
130 
131 	/* Initialize entire 32-bit return value to zero */
132 
133 	*value = 0;
134 
135 	/*
136 	 * Two address spaces supported: Memory or IO.
137 	 * PCI_Config is not supported here because the GAS struct is insufficient
138 	 */
139 	switch (reg->space_id) {
140 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
141 
142 		status = acpi_os_read_memory((acpi_physical_address) address,
143 					     value, width);
144 		break;
145 
146 	case ACPI_ADR_SPACE_SYSTEM_IO:
147 
148 		status =
149 		    acpi_os_read_port((acpi_io_address) address, value, width);
150 		break;
151 
152 	default:
153 		ACPI_ERROR((AE_INFO,
154 			    "Unsupported address space: %X", reg->space_id));
155 		return (AE_BAD_PARAMETER);
156 	}
157 
158 	ACPI_DEBUG_PRINT((ACPI_DB_IO,
159 			  "Read:  %8.8X width %2d from %8.8X%8.8X (%s)\n",
160 			  *value, width, ACPI_FORMAT_UINT64(address),
161 			  acpi_ut_get_region_name(reg->space_id)));
162 
163 	return (status);
164 }
165 
166 ACPI_EXPORT_SYMBOL(acpi_read)
167 
168 /******************************************************************************
169  *
170  * FUNCTION:    acpi_write
171  *
172  * PARAMETERS:  Value               - To be written
173  *              Reg                 - GAS register structure
174  *
175  * RETURN:      Status
176  *
177  * DESCRIPTION: Write to either memory or IO space.
178  *
179  ******************************************************************************/
180 acpi_status acpi_write(u32 value, struct acpi_generic_address *reg)
181 {
182 	u32 width;
183 	u64 address;
184 	acpi_status status;
185 
186 	ACPI_FUNCTION_NAME(acpi_write);
187 
188 	/*
189 	 * Must have a valid pointer to a GAS structure, and a non-zero address
190 	 * within.
191 	 */
192 	if (!reg) {
193 		return (AE_BAD_PARAMETER);
194 	}
195 
196 	/* Get a local copy of the address. Handles possible alignment issues */
197 
198 	ACPI_MOVE_64_TO_64(&address, &reg->address);
199 	if (!address) {
200 		return (AE_BAD_ADDRESS);
201 	}
202 
203 	/* Supported widths are 8/16/32 */
204 
205 	width = reg->bit_width;
206 	if ((width != 8) && (width != 16) && (width != 32)) {
207 		return (AE_SUPPORT);
208 	}
209 
210 	/*
211 	 * Two address spaces supported: Memory or IO.
212 	 * PCI_Config is not supported here because the GAS struct is insufficient
213 	 */
214 	switch (reg->space_id) {
215 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
216 
217 		status = acpi_os_write_memory((acpi_physical_address) address,
218 					      value, width);
219 		break;
220 
221 	case ACPI_ADR_SPACE_SYSTEM_IO:
222 
223 		status = acpi_os_write_port((acpi_io_address) address, value,
224 					    width);
225 		break;
226 
227 	default:
228 		ACPI_ERROR((AE_INFO,
229 			    "Unsupported address space: %X", reg->space_id));
230 		return (AE_BAD_PARAMETER);
231 	}
232 
233 	ACPI_DEBUG_PRINT((ACPI_DB_IO,
234 			  "Wrote: %8.8X width %2d   to %8.8X%8.8X (%s)\n",
235 			  value, width, ACPI_FORMAT_UINT64(address),
236 			  acpi_ut_get_region_name(reg->space_id)));
237 
238 	return (status);
239 }
240 
241 ACPI_EXPORT_SYMBOL(acpi_write)
242 
243 /*******************************************************************************
244  *
245  * FUNCTION:    acpi_get_register_unlocked
246  *
247  * PARAMETERS:  register_id     - ID of ACPI bit_register to access
248  *              return_value    - Value that was read from the register
249  *
250  * RETURN:      Status and the value read from specified Register. Value
251  *              returned is normalized to bit0 (is shifted all the way right)
252  *
253  * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
254  *
255  ******************************************************************************/
256 acpi_status acpi_get_register_unlocked(u32 register_id, u32 *return_value)
257 {
258 	u32 register_value = 0;
259 	struct acpi_bit_register_info *bit_reg_info;
260 	acpi_status status;
261 
262 	ACPI_FUNCTION_TRACE(acpi_get_register_unlocked);
263 
264 	/* Get the info structure corresponding to the requested ACPI Register */
265 
266 	bit_reg_info = acpi_hw_get_bit_register_info(register_id);
267 	if (!bit_reg_info) {
268 		return_ACPI_STATUS(AE_BAD_PARAMETER);
269 	}
270 
271 	/* Read from the register */
272 
273 	status = acpi_hw_register_read(bit_reg_info->parent_register,
274 				       &register_value);
275 
276 	if (ACPI_SUCCESS(status)) {
277 
278 		/* Normalize the value that was read */
279 
280 		register_value =
281 		    ((register_value & bit_reg_info->access_bit_mask)
282 		     >> bit_reg_info->bit_position);
283 
284 		*return_value = register_value;
285 
286 		ACPI_DEBUG_PRINT((ACPI_DB_IO, "Read value %8.8X register %X\n",
287 				  register_value,
288 				  bit_reg_info->parent_register));
289 	}
290 
291 	return_ACPI_STATUS(status);
292 }
293 
294 ACPI_EXPORT_SYMBOL(acpi_get_register_unlocked)
295 
296 /*******************************************************************************
297  *
298  * FUNCTION:    acpi_get_register
299  *
300  * PARAMETERS:  register_id     - ID of ACPI bit_register to access
301  *              return_value    - Value that was read from the register
302  *
303  * RETURN:      Status and the value read from specified Register. Value
304  *              returned is normalized to bit0 (is shifted all the way right)
305  *
306  * DESCRIPTION: ACPI bit_register read function.
307  *
308  ******************************************************************************/
309 acpi_status acpi_get_register(u32 register_id, u32 *return_value)
310 {
311 	acpi_status status;
312 	acpi_cpu_flags flags;
313 
314 	flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
315 	status = acpi_get_register_unlocked(register_id, return_value);
316 	acpi_os_release_lock(acpi_gbl_hardware_lock, flags);
317 
318 	return (status);
319 }
320 
321 ACPI_EXPORT_SYMBOL(acpi_get_register)
322 
323 /*******************************************************************************
324  *
325  * FUNCTION:    acpi_set_register
326  *
327  * PARAMETERS:  register_id     - ID of ACPI bit_register to access
328  *              Value           - (only used on write) value to write to the
329  *                                Register, NOT pre-normalized to the bit pos
330  *
331  * RETURN:      Status
332  *
333  * DESCRIPTION: ACPI Bit Register write function.
334  *
335  ******************************************************************************/
336 acpi_status acpi_set_register(u32 register_id, u32 value)
337 {
338 	u32 register_value = 0;
339 	struct acpi_bit_register_info *bit_reg_info;
340 	acpi_status status;
341 	acpi_cpu_flags lock_flags;
342 
343 	ACPI_FUNCTION_TRACE_U32(acpi_set_register, register_id);
344 
345 	/* Get the info structure corresponding to the requested ACPI Register */
346 
347 	bit_reg_info = acpi_hw_get_bit_register_info(register_id);
348 	if (!bit_reg_info) {
349 		ACPI_ERROR((AE_INFO, "Bad ACPI HW RegisterId: %X",
350 			    register_id));
351 		return_ACPI_STATUS(AE_BAD_PARAMETER);
352 	}
353 
354 	lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
355 
356 	/* Always do a register read first so we can insert the new bits  */
357 
358 	status = acpi_hw_register_read(bit_reg_info->parent_register,
359 				       &register_value);
360 	if (ACPI_FAILURE(status)) {
361 		goto unlock_and_exit;
362 	}
363 
364 	/*
365 	 * Decode the Register ID
366 	 * Register ID = [Register block ID] | [bit ID]
367 	 *
368 	 * Check bit ID to fine locate Register offset.
369 	 * Check Mask to determine Register offset, and then read-write.
370 	 */
371 	switch (bit_reg_info->parent_register) {
372 	case ACPI_REGISTER_PM1_STATUS:
373 
374 		/*
375 		 * Status Registers are different from the rest. Clear by
376 		 * writing 1, and writing 0 has no effect. So, the only relevant
377 		 * information is the single bit we're interested in, all others should
378 		 * be written as 0 so they will be left unchanged.
379 		 */
380 		value = ACPI_REGISTER_PREPARE_BITS(value,
381 						   bit_reg_info->bit_position,
382 						   bit_reg_info->
383 						   access_bit_mask);
384 		if (value) {
385 			status =
386 			    acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
387 						   (u16) value);
388 			register_value = 0;
389 		}
390 		break;
391 
392 	case ACPI_REGISTER_PM1_ENABLE:
393 
394 		ACPI_REGISTER_INSERT_VALUE(register_value,
395 					   bit_reg_info->bit_position,
396 					   bit_reg_info->access_bit_mask,
397 					   value);
398 
399 		status = acpi_hw_register_write(ACPI_REGISTER_PM1_ENABLE,
400 						(u16) register_value);
401 		break;
402 
403 	case ACPI_REGISTER_PM1_CONTROL:
404 
405 		/*
406 		 * Write the PM1 Control register.
407 		 * Note that at this level, the fact that there are actually TWO
408 		 * registers (A and B - and B may not exist) is abstracted.
409 		 */
410 		ACPI_DEBUG_PRINT((ACPI_DB_IO, "PM1 control: Read %X\n",
411 				  register_value));
412 
413 		ACPI_REGISTER_INSERT_VALUE(register_value,
414 					   bit_reg_info->bit_position,
415 					   bit_reg_info->access_bit_mask,
416 					   value);
417 
418 		status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
419 						(u16) register_value);
420 		break;
421 
422 	case ACPI_REGISTER_PM2_CONTROL:
423 
424 		status = acpi_hw_register_read(ACPI_REGISTER_PM2_CONTROL,
425 					       &register_value);
426 		if (ACPI_FAILURE(status)) {
427 			goto unlock_and_exit;
428 		}
429 
430 		ACPI_DEBUG_PRINT((ACPI_DB_IO,
431 				  "PM2 control: Read %X from %8.8X%8.8X\n",
432 				  register_value,
433 				  ACPI_FORMAT_UINT64(acpi_gbl_FADT.
434 						     xpm2_control_block.
435 						     address)));
436 
437 		ACPI_REGISTER_INSERT_VALUE(register_value,
438 					   bit_reg_info->bit_position,
439 					   bit_reg_info->access_bit_mask,
440 					   value);
441 
442 		ACPI_DEBUG_PRINT((ACPI_DB_IO,
443 				  "About to write %4.4X to %8.8X%8.8X\n",
444 				  register_value,
445 				  ACPI_FORMAT_UINT64(acpi_gbl_FADT.
446 						     xpm2_control_block.
447 						     address)));
448 
449 		status = acpi_hw_register_write(ACPI_REGISTER_PM2_CONTROL,
450 						(u8) (register_value));
451 		break;
452 
453 	default:
454 		break;
455 	}
456 
457       unlock_and_exit:
458 
459 	acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
460 
461 	/* Normalize the value that was read */
462 
463 	ACPI_DEBUG_EXEC(register_value =
464 			((register_value & bit_reg_info->access_bit_mask) >>
465 			 bit_reg_info->bit_position));
466 
467 	ACPI_DEBUG_PRINT((ACPI_DB_IO,
468 			  "Set bits: %8.8X actual %8.8X register %X\n", value,
469 			  register_value, bit_reg_info->parent_register));
470 	return_ACPI_STATUS(status);
471 }
472 
473 ACPI_EXPORT_SYMBOL(acpi_set_register)
474 
475 /*******************************************************************************
476  *
477  * FUNCTION:    acpi_get_sleep_type_data
478  *
479  * PARAMETERS:  sleep_state         - Numeric sleep state
480  *              *sleep_type_a        - Where SLP_TYPa is returned
481  *              *sleep_type_b        - Where SLP_TYPb is returned
482  *
483  * RETURN:      Status - ACPI status
484  *
485  * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested sleep
486  *              state.
487  *
488  ******************************************************************************/
489 acpi_status
490 acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
491 {
492 	acpi_status status = AE_OK;
493 	struct acpi_evaluate_info *info;
494 
495 	ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
496 
497 	/* Validate parameters */
498 
499 	if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
500 		return_ACPI_STATUS(AE_BAD_PARAMETER);
501 	}
502 
503 	/* Allocate the evaluation information block */
504 
505 	info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
506 	if (!info) {
507 		return_ACPI_STATUS(AE_NO_MEMORY);
508 	}
509 
510 	info->pathname =
511 	    ACPI_CAST_PTR(char, acpi_gbl_sleep_state_names[sleep_state]);
512 
513 	/* Evaluate the namespace object containing the values for this state */
514 
515 	status = acpi_ns_evaluate(info);
516 	if (ACPI_FAILURE(status)) {
517 		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
518 				  "%s while evaluating SleepState [%s]\n",
519 				  acpi_format_exception(status),
520 				  info->pathname));
521 
522 		goto cleanup;
523 	}
524 
525 	/* Must have a return object */
526 
527 	if (!info->return_object) {
528 		ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
529 			    info->pathname));
530 		status = AE_NOT_EXIST;
531 	}
532 
533 	/* It must be of type Package */
534 
535 	else if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
536 		ACPI_ERROR((AE_INFO,
537 			    "Sleep State return object is not a Package"));
538 		status = AE_AML_OPERAND_TYPE;
539 	}
540 
541 	/*
542 	 * The package must have at least two elements. NOTE (March 2005): This
543 	 * goes against the current ACPI spec which defines this object as a
544 	 * package with one encoded DWORD element. However, existing practice
545 	 * by BIOS vendors seems to be to have 2 or more elements, at least
546 	 * one per sleep type (A/B).
547 	 */
548 	else if (info->return_object->package.count < 2) {
549 		ACPI_ERROR((AE_INFO,
550 			    "Sleep State return package does not have at least two elements"));
551 		status = AE_AML_NO_OPERAND;
552 	}
553 
554 	/* The first two elements must both be of type Integer */
555 
556 	else if (((info->return_object->package.elements[0])->common.type
557 		  != ACPI_TYPE_INTEGER) ||
558 		 ((info->return_object->package.elements[1])->common.type
559 		  != ACPI_TYPE_INTEGER)) {
560 		ACPI_ERROR((AE_INFO,
561 			    "Sleep State return package elements are not both Integers (%s, %s)",
562 			    acpi_ut_get_object_type_name(info->return_object->
563 							 package.elements[0]),
564 			    acpi_ut_get_object_type_name(info->return_object->
565 							 package.elements[1])));
566 		status = AE_AML_OPERAND_TYPE;
567 	} else {
568 		/* Valid _Sx_ package size, type, and value */
569 
570 		*sleep_type_a = (u8)
571 		    (info->return_object->package.elements[0])->integer.value;
572 		*sleep_type_b = (u8)
573 		    (info->return_object->package.elements[1])->integer.value;
574 	}
575 
576 	if (ACPI_FAILURE(status)) {
577 		ACPI_EXCEPTION((AE_INFO, status,
578 				"While evaluating SleepState [%s], bad Sleep object %p type %s",
579 				info->pathname, info->return_object,
580 				acpi_ut_get_object_type_name(info->
581 							     return_object)));
582 	}
583 
584 	acpi_ut_remove_reference(info->return_object);
585 
586       cleanup:
587 	ACPI_FREE(info);
588 	return_ACPI_STATUS(status);
589 }
590 
591 ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)
592