aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/acpi/tables/tbutils.c
blob: 3620ac5f86811c8cbe69d7a1e6021f7ff4f2f9cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/******************************************************************************
 *
 * Module Name: tbutils   - table utilities
 *
 *****************************************************************************/

/*
 * Copyright (C) 2000 - 2006, R. Byron Moore
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
#include <acpi/actables.h>

#define _COMPONENT          ACPI_TABLES
ACPI_MODULE_NAME("tbutils")

/* Local prototypes */
static void acpi_tb_parse_fadt(struct acpi_table_fadt *fadt, u8 flags);

static void inline
acpi_tb_init_generic_address(struct acpi_generic_address *new_gas_struct,
			     u8 bit_width, acpi_physical_address address);

/*******************************************************************************
 *
 * FUNCTION:    acpi_tb_print_table_header
 *
 * PARAMETERS:  Address             - Table physical address
 *              Header              - Table header
 *
 * RETURN:      None
 *
 * DESCRIPTION: Print an ACPI table header
 *
 ******************************************************************************/

void
acpi_tb_print_table_header(acpi_physical_address address,
			   struct acpi_table_header *header)
{

	ACPI_INFO((AE_INFO,
		   "%4.4s @ 0x%p Length 0x%04X (v%3.3d %6.6s %8.8s 0x%08X %4.4s 0x%08X)",
		   header->signature, ACPI_CAST_PTR(void, address),
		   header->length, header->revision, header->oem_id,
		   header->oem_table_id, header->oem_revision,
		   header->asl_compiler_id, header->asl_compiler_revision));
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_tb_init_generic_address
 *
 * PARAMETERS:  new_gas_struct      - GAS struct to be initialized
 *              bit_width           - Width of this register
 *              Address             - Address of the register
 *
 * RETURN:      None
 *
 * DESCRIPTION: Initialize a GAS structure.
 *
 ******************************************************************************/

static void inline
acpi_tb_init_generic_address(struct acpi_generic_address *new_gas_struct,
			     u8 bit_width, acpi_physical_address address)
{

	ACPI_STORE_ADDRESS(new_gas_struct->address, address);
	new_gas_struct->space_id = ACPI_ADR_SPACE_SYSTEM_IO;
	new_gas_struct->bit_width = bit_width;
	new_gas_struct->bit_offset = 0;
	new_gas_struct->access_width = 0;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_tb_checksum
 *
 * PARAMETERS:  Buffer          - Pointer to memory region to be checked
 *              Length          - Length of this memory region
 *
 * RETURN:      Checksum (u8)
 *
 * DESCRIPTION: Calculates circular checksum of memory region.
 *
 ******************************************************************************/

u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length)
{
	u8 sum = 0;
	u8 *end = buffer + length;

	while (buffer < end) {
		sum = (u8) (sum + *(buffer++));
	}

	return sum;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_tb_convert_fadt
 *
 * PARAMETERS:  Fadt                - FADT table to be converted
 *
 * RETURN:      None
 *
 * DESCRIPTION: Converts a BIOS supplied ACPI 1.0 FADT to a local
 *              ACPI 2.0 FADT. If the BIOS supplied a 2.0 FADT then it is simply
 *              copied to the local FADT.  The ACPI CA software uses this
 *              local FADT. Thus a significant amount of special #ifdef
 *              type codeing is saved.
 *
 ******************************************************************************/

void acpi_tb_convert_fadt(struct acpi_table_fadt *fadt)
{

	/*
	 * Convert table pointers to 64-bit fields
	 */
	if (!acpi_gbl_FADT.Xfacs) {
		acpi_gbl_FADT.Xfacs = (u64) acpi_gbl_FADT.facs;
	}

	if (!acpi_gbl_FADT.Xdsdt) {
		acpi_gbl_FADT.Xdsdt = (u64) acpi_gbl_FADT.dsdt;
	}

	/*
	 * Convert the V1.0 block addresses to V2.0 GAS structures
	 */
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xpm1a_event_block,
				     acpi_gbl_FADT.pm1_event_length,
				     (acpi_physical_address) acpi_gbl_FADT.
				     pm1a_event_block);
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xpm1b_event_block,
				     acpi_gbl_FADT.pm1_event_length,
				     (acpi_physical_address) acpi_gbl_FADT.
				     pm1b_event_block);
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xpm1a_control_block,
				     acpi_gbl_FADT.pm1_control_length,
				     (acpi_physical_address) acpi_gbl_FADT.
				     pm1a_control_block);
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xpm1b_control_block,
				     acpi_gbl_FADT.pm1_control_length,
				     (acpi_physical_address) acpi_gbl_FADT.
				     pm1b_control_block);
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xpm2_control_block,
				     acpi_gbl_FADT.pm2_control_length,
				     (acpi_physical_address) acpi_gbl_FADT.
				     pm2_control_block);
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xpm_timer_block,
				     acpi_gbl_FADT.pm_timer_length,
				     (acpi_physical_address) acpi_gbl_FADT.
				     pm_timer_block);
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xgpe0_block, 0,
				     (acpi_physical_address) acpi_gbl_FADT.
				     gpe0_block);
	acpi_tb_init_generic_address(&acpi_gbl_FADT.xgpe1_block, 0,
				     (acpi_physical_address) acpi_gbl_FADT.
				     gpe1_block);

	/*
	 * Create separate GAS structs for the PM1 Enable registers
	 */
	acpi_tb_init_generic_address(&acpi_gbl_xpm1a_enable,
				     (u8) ACPI_DIV_2(acpi_gbl_FADT.
						     pm1_event_length),
				     (acpi_physical_address)
				     (acpi_gbl_FADT.xpm1a_event_block.address +
				      ACPI_DIV_2(acpi_gbl_FADT.
						 pm1_event_length)));

	/*
	 * PM1B is optional; leave null if not present
	 */
	if (acpi_gbl_FADT.xpm1b_event_block.address) {
		acpi_tb_init_generic_address(&acpi_gbl_xpm1b_enable,
					     (u8) ACPI_DIV_2(acpi_gbl_FADT.
							     pm1_event_length),
					     (acpi_physical_address)
					     (acpi_gbl_FADT.xpm1b_event_block.
					      address +
					      ACPI_DIV_2(acpi_gbl_FADT.
							 pm1_event_length)));
	}

	/* Global FADT is the new common V2.0 FADT  */

	acpi_gbl_FADT.header.length = sizeof(struct acpi_table_fadt);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_tb_parse_fadt
 *
 * PARAMETERS:  Fadt                - Pointer to FADT table
 *              Flags               - Flags
 *
 * RETURN:      none
 *
 * DESCRIPTION: This function is called to initialise the FADT, DSDT and FACS
 *              tables (FADT contains the addresses of the DSDT and FACS)
 *
 ******************************************************************************/

static void acpi_tb_parse_fadt(struct acpi_table_fadt *fadt, u8 flags)
{
	acpi_physical_address dsdt_address =
	    (acpi_physical_address) fadt->Xdsdt;
	acpi_physical_address facs_address =
	    (acpi_physical_address) fadt->Xfacs;
	struct acpi_table_header *table;

	if (!dsdt_address) {
		goto no_dsdt;
	}

	table =
	    acpi_os_map_memory(dsdt_address, sizeof(struct acpi_table_header));
	if (!table) {
		goto no_dsdt;
	}

	/* Initialize the DSDT table */

	ACPI_MOVE_32_TO_32(&
			   (acpi_gbl_root_table_list.
			    tables[ACPI_TABLE_INDEX_DSDT].signature),
			   ACPI_SIG_DSDT);

	acpi_gbl_root_table_list.tables[ACPI_TABLE_INDEX_DSDT].address =
	    dsdt_address;
	acpi_gbl_root_table_list.tables[ACPI_TABLE_INDEX_DSDT].length =
	    table->length;
	acpi_gbl_root_table_list.tables[ACPI_TABLE_INDEX_DSDT].flags = flags;

	acpi_tb_print_table_header(dsdt_address, table);

	/* Global integer width is based upon revision of the DSDT */

	acpi_ut_set_integer_width(table->revision);
	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));

      no_dsdt:
	if (!facs_address) {
		return;
	}

	table =
	    acpi_os_map_memory(facs_address, sizeof(struct acpi_table_header));
	if (!table) {
		return;
	}

	/* Initialize the FACS table */

	ACPI_MOVE_32_TO_32(&
			   (acpi_gbl_root_table_list.
			    tables[ACPI_TABLE_INDEX_FACS].signature),
			   ACPI_SIG_FACS);

	acpi_gbl_root_table_list.tables[ACPI_TABLE_INDEX_FACS].address =
	    facs_address;
	acpi_gbl_root_table_list.tables[ACPI_TABLE_INDEX_FACS].length =
	    table->length;
	acpi_gbl_root_table_list.tables[ACPI_TABLE_INDEX_FACS].flags = flags;

	ACPI_INFO((AE_INFO, "%4.4s @ 0x%p",
		   table->signature, ACPI_CAST_PTR(void, facs_address)));

	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_tb_parse_root_table
 *
 * PARAMETERS:  Rsdp                    - Pointer to the RSDP
 *              Flags                   - Flags
 *
 * RETURN:      Status
 *
 * DESCRIPTION: This function is called to parse the Root System Description
 *              Table (RSDT or XSDT)
 *
 * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
 *              be mapped and cannot be copied because it contains the actual
 *              memory location of the ACPI Global Lock.
 *
 ******************************************************************************/

acpi_status acpi_tb_parse_root_table(struct acpi_table_rsdp *rsdp, u8 flags)
{
	struct acpi_table_header *table;
	acpi_physical_address address;
	u32 length;
	u8 *table_entry;
	acpi_native_uint i;
	acpi_native_uint pointer_size;
	u32 table_count;
	u8 checksum;
	acpi_status status;

	ACPI_FUNCTION_TRACE(tb_parse_root_table);

	/* Differentiate between RSDT and XSDT root tables */

	if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
		/*
		 * Root table is an XSDT (64-bit physical addresses). We must use the
		 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
		 * the ACPI specification.
		 */
		address = (acpi_native_uint) rsdp->xsdt_physical_address;
		pointer_size = sizeof(u64);
	} else {
		/* Root table is an RSDT (32-bit physical addresses) */

		address = (acpi_native_uint) rsdp->rsdt_physical_address;
		pointer_size = sizeof(u32);
	}

	/* Map the table header to get the full table length */

	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
	if (!table) {
		return (AE_NO_MEMORY);
	}

	/* Get the length of the full table, verify length and map entire table */

	length = table->length;
	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));

	if (length < sizeof(struct acpi_table_header)) {
		ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
			    length));
		return (AE_INVALID_TABLE_LENGTH);
	}

	table = acpi_os_map_memory(address, length);
	if (!table) {
		return (AE_NO_MEMORY);
	}

	/* Validate the root table checksum */

	checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
#if (ACPI_CHECKSUM_ABORT)

	if (checksum) {
		acpi_os_unmap_memory(table, length);
		return (AE_BAD_CHECKSUM);
	}
#endif

	acpi_tb_print_table_header(address, table);

	/* Calculate the number of tables described in the root table */

	table_count =
	    (table->length - sizeof(struct acpi_table_header)) / pointer_size;

	/* Setup loop */

	table_entry =
	    ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
	acpi_gbl_root_table_list.count = 2;

	/*
	 * Initialize the ACPI table entries
	 * First two entries in the table array are reserved for the DSDT and FACS
	 */
	for (i = 0; i < table_count; ++i, table_entry += pointer_size) {

		/* Ensure there is room for another table entry */

		if (acpi_gbl_root_table_list.count >=
		    acpi_gbl_root_table_list.size) {
			status = acpi_tb_resize_root_table_list();
			if (ACPI_FAILURE(status)) {
				ACPI_WARNING((AE_INFO,
					      "Truncating %u table entries!",
					      (unsigned)
					      (acpi_gbl_root_table_list.size -
					       acpi_gbl_root_table_list.
					       count)));
				break;
			}
		}

		/* Get the physical address (32-bit for RSDT, 64-bit for XSDT) */

		if (pointer_size == sizeof(u32)) {
			acpi_gbl_root_table_list.
			    tables[acpi_gbl_root_table_list.count].address =
			    (acpi_physical_address) (*ACPI_CAST_PTR
						     (u32, table_entry));
		} else {
			acpi_gbl_root_table_list.
			    tables[acpi_gbl_root_table_list.count].address =
			    (acpi_physical_address) (*ACPI_CAST_PTR
						     (u64, table_entry));
		}

		acpi_gbl_root_table_list.count++;
	}

	/*
	 * It is not possible to map more than one entry in some environments,
	 * so unmap the root table here before mapping other tables
	 */
	acpi_os_unmap_memory(table, length);

	/* Initialize all tables other than the DSDT and FACS */

	for (i = 2; i < acpi_gbl_root_table_list.count; i++) {
		address = acpi_gbl_root_table_list.tables[i].address;
		length = sizeof(struct acpi_table_header);

		table = acpi_os_map_memory(address, length);
		if (!table) {
			continue;
		}

		acpi_gbl_root_table_list.tables[i].length = table->length;
		acpi_gbl_root_table_list.tables[i].flags = flags;

		ACPI_MOVE_32_TO_32(&
				   (acpi_gbl_root_table_list.tables[i].
				    signature), table->signature);

		acpi_tb_print_table_header(address, table);

		/*
		 * Special case for the FADT because of multiple versions -
		 * get a local copy and convert to common format
		 */
		if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_FADT)) {
			acpi_os_unmap_memory(table, length);
			length = table->length;

			table = acpi_os_map_memory(address, length);
			if (!table) {
				continue;
			}

			/* Copy the entire FADT locally */

			ACPI_MEMCPY(&acpi_gbl_FADT, table,
				    ACPI_MIN(table->length,
					     sizeof(struct acpi_table_fadt)));

			/* Small table means old revision, convert to new */

			if (table->length < sizeof(struct acpi_table_fadt)) {
				acpi_tb_convert_fadt(ACPI_CAST_PTR
						     (struct acpi_table_fadt,
						      table));
			}

			/* Unmap original FADT */

			acpi_os_unmap_memory(table, length);
			acpi_tb_parse_fadt(&acpi_gbl_FADT, flags);
		} else {
			acpi_os_unmap_memory(table, length);
		}
	}

	return_ACPI_STATUS(AE_OK);
}

/******************************************************************************
 *
 * FUNCTION:    acpi_tb_map
 *
 * PARAMETERS:  Address             - Address to be mapped
 *              Length              - Length to be mapped
 *              Flags               - Logical or physical addressing mode
 *
 * RETURN:      Pointer to mapped region
 *
 * DESCRIPTION: Maps memory according to flag
 *
 *****************************************************************************/

void *acpi_tb_map(acpi_physical_address address, u32 length, u32 flags)
{

	if (flags == ACPI_TABLE_ORIGIN_MAPPED) {
		return (acpi_os_map_memory(address, length));
	} else {
		return (ACPI_CAST_PTR(void, address));
	}
}

/******************************************************************************
 *
 * FUNCTION:    acpi_tb_unmap
 *
 * PARAMETERS:  Pointer             - To mapped region
 *              Length              - Length to be unmapped
 *              Flags               - Logical or physical addressing mode
 *
 * RETURN:      None
 *
 * DESCRIPTION: Unmaps memory according to flag
 *
 *****************************************************************************/

void acpi_tb_unmap(void *pointer, u32 length, u32 flags)
{

	if (flags == ACPI_TABLE_ORIGIN_MAPPED) {
		acpi_os_unmap_memory(pointer, length);
	}
}