Coverage Report

Created: 2017-10-25 09:10

/root/src/xen/xen/drivers/acpi/tables/tbutils.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Module Name: tbutils   - table utilities
4
 *
5
 *****************************************************************************/
6
7
/*
8
 * Copyright (C) 2000 - 2008, Intel Corp.
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions, and the following disclaimer,
16
 *    without modification.
17
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18
 *    substantially similar to the "NO WARRANTY" disclaimer below
19
 *    ("Disclaimer") and any redistribution must be conditioned upon
20
 *    including a substantially similar Disclaimer requirement for further
21
 *    binary redistribution.
22
 * 3. Neither the names of the above-listed copyright holders nor the names
23
 *    of any contributors may be used to endorse or promote products derived
24
 *    from this software without specific prior written permission.
25
 *
26
 * Alternatively, this software may be distributed under the terms of the
27
 * GNU General Public License ("GPL") version 2 as published by the Free
28
 * Software Foundation.
29
 *
30
 * NO WARRANTY
31
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41
 * POSSIBILITY OF SUCH DAMAGES.
42
 */
43
44
#include <xen/init.h>
45
#include <acpi/acpi.h>
46
#include <acpi/actables.h>
47
48
#define _COMPONENT          ACPI_TABLES
49
ACPI_MODULE_NAME("tbutils")
50
51
/*******************************************************************************
52
 *
53
 * FUNCTION:    acpi_tb_check_xsdt
54
 *
55
 * PARAMETERS:  address                    - Pointer to the XSDT
56
 *
57
 * RETURN:      status
58
 *    AE_OK - XSDT is okay
59
 *    AE_NO_MEMORY - can't map XSDT
60
 *    AE_INVALID_TABLE_LENGTH - invalid table length
61
 *    AE_NULL_ENTRY - XSDT has NULL entry
62
 *
63
 * DESCRIPTION: validate XSDT
64
******************************************************************************/
65
66
static acpi_status __init
67
acpi_tb_check_xsdt(acpi_physical_address address)
68
1
{
69
1
  struct acpi_table_header *table;
70
1
  u32 length;
71
1
  u64 xsdt_entry_address;
72
1
  u8 *table_entry;
73
1
  u32 table_count;
74
1
  int i;
75
1
76
1
  table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
77
1
  if (!table)
78
0
    return AE_NO_MEMORY;
79
1
80
1
  length = table->length;
81
1
  acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
82
1
  if (length < sizeof(struct acpi_table_header))
83
0
    return AE_INVALID_TABLE_LENGTH;
84
1
85
1
  table = acpi_os_map_memory(address, length);
86
1
  if (!table)
87
0
    return AE_NO_MEMORY;
88
1
89
1
  /* Calculate the number of tables described in XSDT */
90
1
  table_count =
91
1
    (u32) ((table->length -
92
1
    sizeof(struct acpi_table_header)) / sizeof(u64));
93
1
  table_entry =
94
1
    ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
95
15
  for (i = 0; i < table_count; i++) {
96
14
    ACPI_MOVE_64_TO_64(&xsdt_entry_address, table_entry);
97
14
    if (!xsdt_entry_address) {
98
0
      /* XSDT has NULL entry */
99
0
      break;
100
0
    }
101
14
    table_entry += sizeof(u64);
102
14
  }
103
1
  acpi_os_unmap_memory(table, length);
104
1
105
1
  if (i < table_count)
106
0
    return AE_NULL_ENTRY;
107
1
  else
108
1
    return AE_OK;
109
1
}
110
111
/*******************************************************************************
112
 *
113
 * FUNCTION:    acpi_tb_print_table_header
114
 *
115
 * PARAMETERS:  Address             - Table physical address
116
 *              Header              - Table header
117
 *
118
 * RETURN:      None
119
 *
120
 * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
121
 *
122
 ******************************************************************************/
123
124
void __init
125
acpi_tb_print_table_header(acpi_physical_address address,
126
         struct acpi_table_header *header)
127
18
{
128
18
129
18
  if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
130
1
131
1
    /* FACS only has signature and length fields of common table header */
132
1
133
1
    ACPI_INFO((AE_INFO, "%4.4s %08lX, %04X",
134
1
         header->signature, (unsigned long)address,
135
1
         header->length));
136
17
  } else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
137
1
138
1
    /* RSDP has no common fields */
139
1
140
1
    ACPI_INFO((AE_INFO, "RSDP %08lX, %04X (r%d %6.6s)",
141
1
         (unsigned long)address,
142
1
         (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
143
1
          revision >
144
1
          0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
145
1
                 header)->length : 20,
146
1
         ACPI_CAST_PTR(struct acpi_table_rsdp,
147
1
           header)->revision,
148
1
         ACPI_CAST_PTR(struct acpi_table_rsdp,
149
1
           header)->oem_id));
150
16
  } else {
151
16
    /* Standard ACPI table with full common header */
152
16
153
16
    ACPI_INFO((AE_INFO,
154
16
         "%4.4s %08lX, %04X (r%d %6.6s %8.8s %8X %4.4s %8X)",
155
16
         header->signature, (unsigned long)address,
156
16
         header->length, header->revision, header->oem_id,
157
16
         header->oem_table_id, header->oem_revision,
158
16
         header->asl_compiler_id,
159
16
         header->asl_compiler_revision));
160
16
  }
161
18
}
162
163
/*******************************************************************************
164
 *
165
 * FUNCTION:    acpi_tb_validate_checksum
166
 *
167
 * PARAMETERS:  Table               - ACPI table to verify
168
 *              Length              - Length of entire table
169
 *
170
 * RETURN:      Status
171
 *
172
 * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
173
 *              exception on bad checksum.
174
 *
175
 ******************************************************************************/
176
177
acpi_status __init
178
acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
179
23
{
180
23
  u8 checksum;
181
23
182
23
  /* Compute the checksum on the table */
183
23
184
23
  checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
185
23
186
23
  /* Checksum ok? (should be zero) */
187
23
188
23
  if (checksum) {
189
0
    ACPI_WARNING((AE_INFO,
190
0
            "Incorrect checksum in table [%4.4s] - %2.2X, should be %2.2X",
191
0
            table->signature, table->checksum,
192
0
            (u8) (table->checksum - checksum)));
193
0
194
0
#if (ACPI_CHECKSUM_ABORT)
195
196
    return (AE_BAD_CHECKSUM);
197
#endif
198
0
  }
199
23
200
23
  return (AE_OK);
201
23
}
202
203
/*******************************************************************************
204
 *
205
 * FUNCTION:    acpi_tb_checksum
206
 *
207
 * PARAMETERS:  Buffer          - Pointer to memory region to be checked
208
 *              Length          - Length of this memory region
209
 *
210
 * RETURN:      Checksum (u8)
211
 *
212
 * DESCRIPTION: Calculates circular checksum of memory region.
213
 *
214
 ******************************************************************************/
215
216
u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length)
217
33
{
218
33
  u8 sum = 0;
219
33
  u8 *end = buffer + length;
220
33
221
6.15k
  while (buffer < end) {
222
6.12k
    sum = (u8) (sum + *(buffer++));
223
6.12k
  }
224
33
225
33
  return sum;
226
33
}
227
228
/*******************************************************************************
229
 *
230
 * FUNCTION:    acpi_tb_install_table
231
 *
232
 * PARAMETERS:  Address                 - Physical address of DSDT or FACS
233
 *              Flags                   - Flags
234
 *              Signature               - Table signature, NULL if no need to
235
 *                                        match
236
 *              table_index             - Index into root table array
237
 *
238
 * RETURN:      None
239
 *
240
 * DESCRIPTION: Install an ACPI table into the global data structure.
241
 *
242
 ******************************************************************************/
243
244
void __init
245
acpi_tb_install_table(acpi_physical_address address,
246
          u8 flags, char *signature, acpi_native_uint table_index)
247
16
{
248
16
  struct acpi_table_header *table;
249
16
250
16
  if (!address) {
251
0
    ACPI_ERROR((AE_INFO,
252
0
          "Null physical address for ACPI table [%s]",
253
0
          signature));
254
0
    return;
255
0
  }
256
16
257
16
  /* Map just the table header */
258
16
259
16
  table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
260
16
  if (!table) {
261
0
    return;
262
0
  }
263
16
264
16
  /* If a particular signature is expected, signature must match */
265
16
266
16
  if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
267
0
    ACPI_ERROR((AE_INFO,
268
0
          "Invalid signature 0x%X for ACPI table [%s]",
269
0
          *ACPI_CAST_PTR(u32, table->signature), signature));
270
0
    goto unmap_and_exit;
271
0
  }
272
16
273
16
  /* Initialize the table entry */
274
16
275
16
  acpi_gbl_root_table_list.tables[table_index].address = address;
276
16
  acpi_gbl_root_table_list.tables[table_index].length = table->length;
277
16
  acpi_gbl_root_table_list.tables[table_index].flags = flags;
278
16
279
16
  ACPI_MOVE_32_TO_32(&
280
16
         (acpi_gbl_root_table_list.tables[table_index].
281
16
          signature), table->signature);
282
16
283
16
  acpi_tb_print_table_header(address, table);
284
16
285
16
      unmap_and_exit:
286
16
  acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
287
16
}
288
289
/*******************************************************************************
290
 *
291
 * FUNCTION:    acpi_tb_get_root_table_entry
292
 *
293
 * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
294
 *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
295
 *
296
 * RETURN:      Physical address extracted from the root table
297
 *
298
 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
299
 *              both 32-bit and 64-bit platforms
300
 *
301
 * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
302
 *              64-bit platforms.
303
 *
304
 ******************************************************************************/
305
306
static acpi_physical_address __init
307
acpi_tb_get_root_table_entry(u8 * table_entry,
308
           acpi_native_uint table_entry_size)
309
14
{
310
14
  u64 address64;
311
14
312
14
  /*
313
14
   * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
314
14
   * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
315
14
   */
316
14
  if (table_entry_size == sizeof(u32)) {
317
0
    /*
318
0
     * 32-bit platform, RSDT: Return 32-bit table entry
319
0
     * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
320
0
     */
321
0
    return ((acpi_physical_address)
322
0
      (*ACPI_CAST_PTR(u32, table_entry)));
323
14
  } else {
324
14
    /*
325
14
     * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
326
14
     * 64-bit platform, XSDT: Move (unaligned) 64-bit to local, return 64-bit
327
14
     */
328
14
    ACPI_MOVE_64_TO_64(&address64, table_entry);
329
14
330
14
#if ACPI_MACHINE_WIDTH == 32
331
    if (address64 > ACPI_UINT32_MAX) {
332
333
      /* Will truncate 64-bit address to 32 bits, issue warning */
334
335
      ACPI_WARNING((AE_INFO,
336
              "64-bit Physical Address in XSDT is too large (%8.8X%8.8X), truncating",
337
              ACPI_FORMAT_UINT64(address64)));
338
    }
339
#endif
340
14
    return ((acpi_physical_address) (address64));
341
14
  }
342
14
}
343
344
/*******************************************************************************
345
 *
346
 * FUNCTION:    acpi_tb_parse_root_table
347
 *
348
 * PARAMETERS:  Rsdp                    - Pointer to the RSDP
349
 *              Flags                   - Flags
350
 *
351
 * RETURN:      Status
352
 *
353
 * DESCRIPTION: This function is called to parse the Root System Description
354
 *              Table (RSDT or XSDT)
355
 *
356
 * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
357
 *              be mapped and cannot be copied because it contains the actual
358
 *              memory location of the ACPI Global Lock.
359
 *
360
 ******************************************************************************/
361
362
acpi_status __init
363
acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags)
364
1
{
365
1
  struct acpi_table_rsdp *rsdp;
366
1
  acpi_native_uint table_entry_size;
367
1
  acpi_native_uint i;
368
1
  u32 table_count;
369
1
  struct acpi_table_header *table;
370
1
  acpi_physical_address address;
371
1
  acpi_physical_address rsdt_address = 0;
372
1
  u32 length;
373
1
  u8 *table_entry;
374
1
  acpi_status status;
375
1
376
1
  ACPI_FUNCTION_TRACE(tb_parse_root_table);
377
1
378
1
  /*
379
1
   * Map the entire RSDP and extract the address of the RSDT or XSDT
380
1
   */
381
1
  rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
382
1
  if (!rsdp) {
383
0
    return_ACPI_STATUS(AE_NO_MEMORY);
384
0
  }
385
1
386
1
  acpi_tb_print_table_header(rsdp_address,
387
1
           ACPI_CAST_PTR(struct acpi_table_header,
388
1
             rsdp));
389
1
390
1
  /* Differentiate between RSDT and XSDT root tables */
391
1
392
1
  if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
393
1
    /*
394
1
     * Root table is an XSDT (64-bit physical addresses). We must use the
395
1
     * XSDT if the revision is > 1 and the XSDT pointer is present, as per
396
1
     * the ACPI specification.
397
1
     */
398
1
    address = (acpi_physical_address) rsdp->xsdt_physical_address;
399
1
    table_entry_size = sizeof(u64);
400
1
    rsdt_address = (acpi_physical_address)
401
1
          rsdp->rsdt_physical_address;
402
0
  } else {
403
0
    /* Root table is an RSDT (32-bit physical addresses) */
404
0
405
0
    address = (acpi_physical_address) rsdp->rsdt_physical_address;
406
0
    table_entry_size = sizeof(u32);
407
0
  }
408
1
409
1
  /*
410
1
   * It is not possible to map more than one entry in some environments,
411
1
   * so unmap the RSDP here before mapping other tables
412
1
   */
413
1
  acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
414
1
415
1
  if (table_entry_size == sizeof(u64)) {
416
1
    if (acpi_tb_check_xsdt(address) == AE_NULL_ENTRY) {
417
0
      /* XSDT has NULL entry, RSDT is used */
418
0
      address = rsdt_address;
419
0
      table_entry_size = sizeof(u32);
420
0
      ACPI_WARNING((AE_INFO, "BIOS XSDT has NULL entry, "
421
0
          "using RSDT"));
422
0
    }
423
1
  }
424
1
  /* Map the RSDT/XSDT table header to get the full table length */
425
1
426
1
  table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
427
1
  if (!table) {
428
0
    return_ACPI_STATUS(AE_NO_MEMORY);
429
0
  }
430
1
431
1
  acpi_tb_print_table_header(address, table);
432
1
433
1
  /* Get the length of the full table, verify length and map entire table */
434
1
435
1
  length = table->length;
436
1
  acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
437
1
438
1
  if (length < sizeof(struct acpi_table_header)) {
439
0
    ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
440
0
          length));
441
0
    return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
442
0
  }
443
1
444
1
  table = acpi_os_map_memory(address, length);
445
1
  if (!table) {
446
0
    return_ACPI_STATUS(AE_NO_MEMORY);
447
0
  }
448
1
449
1
  /* Validate the root table checksum */
450
1
451
1
  status = acpi_tb_verify_checksum(table, length);
452
1
  if (ACPI_FAILURE(status)) {
453
0
    acpi_os_unmap_memory(table, length);
454
0
    return_ACPI_STATUS(status);
455
0
  }
456
1
457
1
  /* Calculate the number of tables described in the root table */
458
1
459
1
  table_count =
460
1
      (u32) ((table->length -
461
1
        sizeof(struct acpi_table_header)) / table_entry_size);
462
1
463
1
  /*
464
1
   * First two entries in the table array are reserved for the DSDT and FACS,
465
1
   * which are not actually present in the RSDT/XSDT - they come from the FADT
466
1
   */
467
1
  table_entry =
468
1
      ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
469
1
  acpi_gbl_root_table_list.count = 2;
470
1
471
1
  /*
472
1
   * Initialize the root table array from the RSDT/XSDT
473
1
   */
474
15
  for (i = 0; i < table_count; i++) {
475
14
    if (acpi_gbl_root_table_list.count >=
476
0
        acpi_gbl_root_table_list.size) {
477
0
478
0
      /* There is no more room in the root table array, attempt resize */
479
0
480
0
      status = acpi_tb_resize_root_table_list();
481
0
      if (ACPI_FAILURE(status)) {
482
0
        ACPI_WARNING((AE_INFO,
483
0
                "Truncating %u table entries!",
484
0
                (unsigned)
485
0
                (acpi_gbl_root_table_list.size -
486
0
                 acpi_gbl_root_table_list.
487
0
                 count)));
488
0
        break;
489
0
      }
490
0
    }
491
14
492
14
    /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
493
14
494
14
    acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
495
14
        address =
496
14
        acpi_tb_get_root_table_entry(table_entry, table_entry_size);
497
14
498
14
    table_entry += table_entry_size;
499
14
    acpi_gbl_root_table_list.count++;
500
14
  }
501
1
502
1
  /*
503
1
   * It is not possible to map more than one entry in some environments,
504
1
   * so unmap the root table here before mapping other tables
505
1
   */
506
1
  acpi_os_unmap_memory(table, length);
507
1
508
1
  /*
509
1
   * Complete the initialization of the root table array by examining
510
1
   * the header of each table
511
1
   */
512
15
  for (i = 2; i < acpi_gbl_root_table_list.count; i++) {
513
14
    acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
514
14
              address, flags, NULL, i);
515
14
516
14
    /* Special case for FADT - get the DSDT and FACS */
517
14
518
14
    if (ACPI_COMPARE_NAME
519
14
        (&acpi_gbl_root_table_list.tables[i].signature,
520
1
         ACPI_SIG_FADT)) {
521
1
      acpi_tb_parse_fadt(i, flags);
522
1
    }
523
14
  }
524
1
525
1
  return_ACPI_STATUS(AE_OK);
526
1
}