:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / bus / acpi / tables / tbconvrt.c
1 /******************************************************************************
2  *
3  * Module Name: tbconvrt - ACPI Table conversion utilities
4  *              $Revision$
5  *
6  *****************************************************************************/
7
8 /*
9  *  Copyright (C) 2000, 2001 R. Byron Moore
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26
27 #include "acpi.h"
28 #include "achware.h"
29 #include "actables.h"
30 #include "actbl.h"
31
32
33 #define _COMPONENT          ACPI_TABLES
34          MODULE_NAME         ("tbconvrt")
35
36
37 /*
38  * Build a GAS structure from earlier ACPI table entries (V1.0 and 0.71 extensions)
39  *
40  * 1) Address space
41  * 2) Length in bytes -- convert to length in bits
42  * 3) Bit offset is zero
43  * 4) Reserved field is zero
44  * 5) Expand address to 64 bits
45  */
46 #define ASL_BUILD_GAS_FROM_ENTRY(a,b,c,d)   {a.address_space_id = (u8) d;\
47                            a.register_bit_width = (u8) MUL_8 (b);\
48                            a.register_bit_offset = 0;\
49                            a.reserved = 0;\
50                            ACPI_STORE_ADDRESS (a.address,c);}
51
52
53 /* ACPI V1.0 entries -- address space is always I/O */
54
55 #define ASL_BUILD_GAS_FROM_V1_ENTRY(a,b,c)  ASL_BUILD_GAS_FROM_ENTRY(a,b,c,ADDRESS_SPACE_SYSTEM_IO)
56
57
58 /*******************************************************************************
59  *
60  * FUNCTION:    Acpi_tb_convert_to_xsdt
61  *
62  * PARAMETERS:
63  *
64  * RETURN:
65  *
66  * DESCRIPTION:
67  *
68  ******************************************************************************/
69
70 ACPI_STATUS
71 acpi_tb_convert_to_xsdt (
72         ACPI_TABLE_DESC         *table_info,
73         u32                     *number_of_tables) {
74         u32                     table_size;
75         u32                     pointer_size;
76         u32                     i;
77         XSDT_DESCRIPTOR         *new_table;
78
79
80 #ifndef _IA64
81
82         if (acpi_gbl_RSDP->revision < 2) {
83                 pointer_size = sizeof (u32);
84         }
85
86         else
87 #endif
88         {
89                 pointer_size = sizeof (UINT64);
90         }
91
92         /*
93          * Determine the number of tables pointed to by the RSDT/XSDT.
94          * This is defined by the ACPI Specification to be the number of
95          * pointers contained within the RSDT/XSDT.  The size of the pointers
96          * is architecture-dependent.
97          */
98
99         table_size = table_info->pointer->length;
100         *number_of_tables = (table_size -
101                            sizeof (ACPI_TABLE_HEADER)) / pointer_size;
102
103         /* Compute size of the converted XSDT */
104
105         table_size = (*number_of_tables * sizeof (UINT64)) + sizeof (ACPI_TABLE_HEADER);
106
107
108         /* Allocate an XSDT */
109
110         new_table = acpi_cm_callocate (table_size);
111         if (!new_table) {
112                 return (AE_NO_MEMORY);
113         }
114
115         /* Copy the header and set the length */
116
117         MEMCPY (new_table, table_info->pointer, sizeof (ACPI_TABLE_HEADER));
118         new_table->header.length = table_size;
119
120         /* Copy the table pointers */
121
122         for (i = 0; i < *number_of_tables; i++) {
123                 if (acpi_gbl_RSDP->revision < 2) {
124 #ifdef _IA64
125                         new_table->table_offset_entry[i] =
126                                 ((RSDT_DESCRIPTOR_REV071 *) table_info->pointer)->table_offset_entry[i];
127 #else
128                         ACPI_STORE_ADDRESS (new_table->table_offset_entry[i],
129                                 ((RSDT_DESCRIPTOR_REV1 *) table_info->pointer)->table_offset_entry[i]);
130 #endif
131                 }
132                 else {
133                         new_table->table_offset_entry[i] =
134                                 ((XSDT_DESCRIPTOR *) table_info->pointer)->table_offset_entry[i];
135                 }
136         }
137
138
139         /* Delete the original table (either mapped or in a buffer) */
140
141         acpi_tb_delete_single_table (table_info);
142
143
144         /* Point the table descriptor to the new table */
145
146         table_info->pointer     = (ACPI_TABLE_HEADER *) new_table;
147         table_info->base_pointer = (ACPI_TABLE_HEADER *) new_table;
148         table_info->length      = table_size;
149         table_info->allocation  = ACPI_MEM_ALLOCATED;
150
151         return (AE_OK);
152 }
153
154
155 /*******************************************************************************
156  *
157  * FUNCTION:    Acpi_tb_convert_table_fadt
158  *
159  * PARAMETERS:
160  *
161  * RETURN:
162  *
163  * DESCRIPTION:
164  *    Converts BIOS supplied 1.0 and 0.71 ACPI FADT to an intermediate
165  *    ACPI 2.0 FADT. If the BIOS supplied a 2.0 FADT then it is simply
166  *    copied to the intermediate FADT.  The ACPI CA software uses this
167  *    intermediate FADT. Thus a significant amount of special #ifdef
168  *    type codeing is saved. This intermediate FADT will need to be
169  *    freed at some point.
170  *
171  ******************************************************************************/
172
173 ACPI_STATUS
174 acpi_tb_convert_table_fadt (void)
175 {
176
177 #ifdef _IA64
178         FADT_DESCRIPTOR_REV071 *FADT71;
179         u8                      pm1_address_space;
180         u8                      pm2_address_space;
181         u8                      pm_timer_address_space;
182         u8                      gpe0address_space;
183         u8                      gpe1_address_space;
184 #else
185         FADT_DESCRIPTOR_REV1   *FADT1;
186 #endif
187
188         FADT_DESCRIPTOR_REV2   *FADT2;
189         ACPI_TABLE_DESC        *table_desc;
190
191
192         /* Acpi_gbl_FADT is valid */
193         /* Allocate and zero the 2.0 buffer */
194
195         FADT2 = acpi_cm_callocate (sizeof (FADT_DESCRIPTOR_REV2));
196         if (FADT2 == NULL) {
197                 return (AE_NO_MEMORY);
198         }
199
200
201         /* The ACPI FADT revision number is FADT2_REVISION_ID=3 */
202         /* So, if the current table revision is less than 3 it is type 1.0 or 0.71 */
203
204         if (acpi_gbl_FADT->header.revision >= FADT2_REVISION_ID) {
205                 /* We have an ACPI 2.0 FADT but we must copy it to our local buffer */
206
207                 *FADT2 = *((FADT_DESCRIPTOR_REV2*) acpi_gbl_FADT);
208
209         }
210
211         else {
212
213 #ifdef _IA64
214                 /*
215                  * For the 64-bit case only, a revision ID less than V2.0 means the
216                  * tables are the 0.71 extensions
217                  */
218
219                 /* The BIOS stored FADT should agree with Revision 0.71 */
220
221                 FADT71 = (FADT_DESCRIPTOR_REV071 *) acpi_gbl_FADT;
222
223                 /* Copy the table header*/
224
225                 FADT2->header       = FADT71->header;
226
227                 /* Copy the common fields */
228
229                 FADT2->sci_int      = FADT71->sci_int;
230                 FADT2->acpi_enable  = FADT71->acpi_enable;
231                 FADT2->acpi_disable = FADT71->acpi_disable;
232                 FADT2->S4_bios_req  = FADT71->S4_bios_req;
233                 FADT2->plvl2_lat    = FADT71->plvl2_lat;
234                 FADT2->plvl3_lat    = FADT71->plvl3_lat;
235                 FADT2->day_alrm     = FADT71->day_alrm;
236                 FADT2->mon_alrm     = FADT71->mon_alrm;
237                 FADT2->century      = FADT71->century;
238                 FADT2->gpe1_base    = FADT71->gpe1_base;
239
240                 /*
241                  * We still use the block length registers even though
242                  * the GAS structure should obsolete them.  This is because
243                  * these registers are byte lengths versus the GAS which
244                  * contains a bit width
245                  */
246                 FADT2->pm1_evt_len  = FADT71->pm1_evt_len;
247                 FADT2->pm1_cnt_len  = FADT71->pm1_cnt_len;
248                 FADT2->pm2_cnt_len  = FADT71->pm2_cnt_len;
249                 FADT2->pm_tm_len    = FADT71->pm_tm_len;
250                 FADT2->gpe0blk_len  = FADT71->gpe0blk_len;
251                 FADT2->gpe1_blk_len = FADT71->gpe1_blk_len;
252                 FADT2->gpe1_base    = FADT71->gpe1_base;
253
254                 /* Copy the existing 0.71 flags to 2.0. The other bits are zero.*/
255
256                 FADT2->wb_invd      = FADT71->flush_cash;
257                 FADT2->proc_c1      = FADT71->proc_c1;
258                 FADT2->plvl2_up     = FADT71->plvl2_up;
259                 FADT2->pwr_button   = FADT71->pwr_button;
260                 FADT2->sleep_button = FADT71->sleep_button;
261                 FADT2->fixed_rTC    = FADT71->fixed_rTC;
262                 FADT2->rtcs4        = FADT71->rtcs4;
263                 FADT2->tmr_val_ext  = FADT71->tmr_val_ext;
264                 FADT2->dock_cap     = FADT71->dock_cap;
265
266
267                 /* We should not use these next two addresses */
268                 /* Since our buffer is pre-zeroed nothing to do for */
269                 /* the next three data items in the structure */
270                 /* FADT2->Firmware_ctrl = 0; */
271                 /* FADT2->Dsdt = 0; */
272
273                 /* System Interrupt Model isn't used in ACPI 2.0*/
274                 /* FADT2->Reserved1 = 0; */
275
276                 /* This field is set by the OEM to convey the preferred */
277                 /* power management profile to OSPM. It doesn't have any*/
278                 /* 0.71 equivalence.  Since we don't know what kind of  */
279                 /* 64-bit system this is, we will pick unspecified.     */
280
281                 FADT2->prefer_PM_profile = PM_UNSPECIFIED;
282
283
284                 /* Port address of SMI command port */
285                 /* We shouldn't use this port because IA64 doesn't */
286                 /* have or use SMI.  It has PMI. */
287
288                 FADT2->smi_cmd      = (u32)(FADT71->smi_cmd & 0xFFFFFFFF);
289
290
291                 /* processor performance state control*/
292                 /* The value OSPM writes to the SMI_CMD register to assume */
293                 /* processor performance state control responsibility. */
294                 /* There isn't any equivalence in 0.71 */
295                 /* Again this should be meaningless for IA64 */
296                 /* FADT2->Pstate_cnt = 0; */
297
298                 /* The 32-bit Power management and GPE registers are */
299                 /* not valid in IA-64 and we are not going to use them */
300                 /* so leaving them pre-zeroed. */
301
302                 /* Support for the _CST object and C States change notification.*/
303                 /* This data item hasn't any 0.71 equivalence so leaving it zero.*/
304                 /* FADT2->Cst_cnt = 0; */
305
306                 /* number of flush strides that need to be read */
307                 /* No 0.71 equivalence. Leave pre-zeroed. */
308                 /* FADT2->Flush_size = 0; */
309
310                 /* Processor's memory cache line width, in bytes */
311                 /* No 0.71 equivalence. Leave pre-zeroed. */
312                 /* FADT2->Flush_stride = 0; */
313
314                 /* Processor's duty cycle index in processor's P_CNT reg*/
315                 /* No 0.71 equivalence. Leave pre-zeroed. */
316                 /* FADT2->Duty_offset = 0; */
317
318                 /* Processor's duty cycle value bit width in P_CNT register.*/
319                 /* No 0.71 equivalence. Leave pre-zeroed. */
320                 /* FADT2->Duty_width = 0; */
321
322
323                 /* Since there isn't any equivalence in 0.71 */
324                 /* and since Big_sur had to support legacy */
325
326                 FADT2->iapc_boot_arch = BAF_LEGACY_DEVICES;
327
328                 /* Copy to ACPI 2.0 64-BIT Extended Addresses */
329
330                 FADT2->Xfirmware_ctrl = FADT71->firmware_ctrl;
331                 FADT2->Xdsdt         = FADT71->dsdt;
332
333
334                 /* Extract the address space IDs */
335
336                 pm1_address_space   = (u8)((FADT71->address_space & PM1_BLK_ADDRESS_SPACE)    >> 1);
337                 pm2_address_space   = (u8)((FADT71->address_space & PM2_CNT_BLK_ADDRESS_SPACE) >> 2);
338                 pm_timer_address_space = (u8)((FADT71->address_space & PM_TMR_BLK_ADDRESS_SPACE) >> 3);
339                 gpe0address_space   = (u8)((FADT71->address_space & GPE0_BLK_ADDRESS_SPACE)   >> 4);
340                 gpe1_address_space  = (u8)((FADT71->address_space & GPE1_BLK_ADDRESS_SPACE)   >> 5);
341
342                 /*
343                  * Convert the 0.71 (non-GAS style) Block addresses to V2.0 GAS structures,
344                  * in this order:
345                  *
346                  * PM 1_a Events
347                  * PM 1_b Events
348                  * PM 1_a Control
349                  * PM 1_b Control
350                  * PM 2 Control
351                  * PM Timer Control
352                  * GPE Block 0
353                  * GPE Block 1
354                  */
355
356                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xpm1a_evt_blk, FADT71->pm1_evt_len, FADT71->pm1a_evt_blk, pm1_address_space);
357                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xpm1b_evt_blk, FADT71->pm1_evt_len, FADT71->pm1b_evt_blk, pm1_address_space);
358                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xpm1a_cnt_blk, FADT71->pm1_cnt_len, FADT71->pm1a_cnt_blk, pm1_address_space);
359                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xpm1b_cnt_blk, FADT71->pm1_cnt_len, FADT71->pm1b_cnt_blk, pm1_address_space);
360                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xpm2_cnt_blk, FADT71->pm2_cnt_len, FADT71->pm2_cnt_blk, pm2_address_space);
361                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xpm_tmr_blk, FADT71->pm_tm_len,  FADT71->pm_tmr_blk, pm_timer_address_space);
362                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xgpe0blk,    FADT71->gpe0blk_len, FADT71->gpe0blk,   gpe0address_space);
363                 ASL_BUILD_GAS_FROM_ENTRY (FADT2->Xgpe1_blk,   FADT71->gpe1_blk_len, FADT71->gpe1_blk, gpe1_address_space);
364
365 #else
366
367                 /* ACPI 1.0 FACS */
368
369
370                 /* The BIOS stored FADT should agree with Revision 1.0 */
371
372                 FADT1 = (FADT_DESCRIPTOR_REV1*) acpi_gbl_FADT;
373
374                 /*
375                  * Copy the table header and the common part of the tables
376                  * The 2.0 table is an extension of the 1.0 table, so the
377                  * entire 1.0 table can be copied first, then expand some
378                  * fields to 64 bits.
379                  */
380
381                 MEMCPY (FADT2, FADT1, sizeof (FADT_DESCRIPTOR_REV1));
382
383
384                 /* Convert table pointers to 64-bit fields */
385
386                 ACPI_STORE_ADDRESS (FADT2->Xfirmware_ctrl, FADT1->firmware_ctrl);
387                 ACPI_STORE_ADDRESS (FADT2->Xdsdt, FADT1->dsdt);
388
389                 /* System Interrupt Model isn't used in ACPI 2.0*/
390                 /* FADT2->Reserved1 = 0; */
391
392                 /* This field is set by the OEM to convey the preferred */
393                 /* power management profile to OSPM. It doesn't have any*/
394                 /* 1.0 equivalence.  Since we don't know what kind of   */
395                 /* 32-bit system this is, we will pick unspecified.     */
396
397                 FADT2->prefer_PM_profile = PM_UNSPECIFIED;
398
399
400                 /* Processor Performance State Control. This is the value  */
401                 /* OSPM writes to the SMI_CMD register to assume processor */
402                 /* performance state control responsibility. There isn't   */
403                 /* any equivalence in 1.0.  So leave it zeroed.            */
404
405                 FADT2->pstate_cnt = 0;
406
407
408                 /* Support for the _CST object and C States change notification.*/
409                 /* This data item hasn't any 1.0 equivalence so leaving it zero.*/
410
411                 FADT2->cst_cnt = 0;
412
413
414                 /* Since there isn't any equivalence in 1.0 and since it   */
415                 /* is highly likely that a 1.0 system has legacy  support. */
416
417                 FADT2->iapc_boot_arch = BAF_LEGACY_DEVICES;
418
419
420                 /*
421                  * Convert the V1.0 Block addresses to V2.0 GAS structures
422                  * in this order:
423                  *
424                  * PM 1_a Events
425                  * PM 1_b Events
426                  * PM 1_a Control
427                  * PM 1_b Control
428                  * PM 2 Control
429                  * PM Timer Control
430                  * GPE Block 0
431                  * GPE Block 1
432                  */
433
434                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xpm1a_evt_blk, FADT1->pm1_evt_len, FADT1->pm1a_evt_blk);
435                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xpm1b_evt_blk, FADT1->pm1_evt_len, FADT1->pm1b_evt_blk);
436                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xpm1a_cnt_blk, FADT1->pm1_cnt_len, FADT1->pm1a_cnt_blk);
437                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xpm1b_cnt_blk, FADT1->pm1_cnt_len, FADT1->pm1b_cnt_blk);
438                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xpm2_cnt_blk, FADT1->pm2_cnt_len, FADT1->pm2_cnt_blk);
439                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xpm_tmr_blk, FADT1->pm_tm_len,  FADT1->pm_tmr_blk);
440                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xgpe0blk,    FADT1->gpe0blk_len, FADT1->gpe0blk);
441                 ASL_BUILD_GAS_FROM_V1_ENTRY (FADT2->Xgpe1_blk,   FADT1->gpe1_blk_len, FADT1->gpe1_blk);
442 #endif
443         }
444
445
446         /*
447          * Global FADT pointer will point to the common V2.0 FADT
448          */
449         acpi_gbl_FADT = FADT2;
450         acpi_gbl_FADT->header.length = sizeof (FADT_DESCRIPTOR);
451
452
453         /* Free the original table */
454
455         table_desc = &acpi_gbl_acpi_tables[ACPI_TABLE_FADT];
456         acpi_tb_delete_single_table (table_desc);
457
458
459         /* Install the new table */
460
461         table_desc->pointer = (ACPI_TABLE_HEADER *) acpi_gbl_FADT;
462         table_desc->base_pointer = acpi_gbl_FADT;
463         table_desc->allocation = ACPI_MEM_ALLOCATED;
464         table_desc->length = sizeof (FADT_DESCRIPTOR_REV2);
465
466
467         /* Dump the entire FADT */
468
469
470         return (AE_OK);
471 }
472
473
474 /*******************************************************************************
475  *
476  * FUNCTION:    Acpi_tb_convert_table_facs
477  *
478  * PARAMETERS:
479  *
480  * RETURN:
481  *
482  * DESCRIPTION:
483  *
484  ******************************************************************************/
485
486 ACPI_STATUS
487 acpi_tb_build_common_facs (
488         ACPI_TABLE_DESC         *table_info)
489 {
490         ACPI_COMMON_FACS        *common_facs;
491
492 #ifdef _IA64
493         FACS_DESCRIPTOR_REV071  *FACS71;
494 #else
495         FACS_DESCRIPTOR_REV1    *FACS1;
496 #endif
497
498         FACS_DESCRIPTOR_REV2    *FACS2;
499
500
501         /* Allocate a common FACS */
502
503         common_facs = acpi_cm_callocate (sizeof (ACPI_COMMON_FACS));
504         if (!common_facs) {
505                 return (AE_NO_MEMORY);
506         }
507
508
509         /* Copy fields to the new FACS */
510
511         if (acpi_gbl_RSDP->revision < 2) {
512 #ifdef _IA64
513                 /* 0.71 FACS */
514
515                 FACS71 = (FACS_DESCRIPTOR_REV071 *) acpi_gbl_FACS;
516
517                 common_facs->global_lock = (u32 *) &(FACS71->global_lock);
518                 common_facs->firmware_waking_vector = &FACS71->firmware_waking_vector;
519                 common_facs->vector_width = 64;
520 #else
521                 /* ACPI 1.0 FACS */
522
523                 FACS1 = (FACS_DESCRIPTOR_REV1 *) acpi_gbl_FACS;
524
525                 common_facs->global_lock = &(FACS1->global_lock);
526                 common_facs->firmware_waking_vector = (UINT64 *) &FACS1->firmware_waking_vector;
527                 common_facs->vector_width = 32;
528
529 #endif
530         }
531
532         else {
533                 /* ACPI 2.0 FACS */
534
535                 FACS2 = (FACS_DESCRIPTOR_REV2 *) acpi_gbl_FACS;
536
537                 common_facs->global_lock = &(FACS2->global_lock);
538                 common_facs->firmware_waking_vector = &FACS2->Xfirmware_waking_vector;
539                 common_facs->vector_width = 64;
540         }
541
542
543         /* Set the global FACS pointer to point to the common FACS */
544
545
546         acpi_gbl_FACS = common_facs;
547
548         return  (AE_OK);
549 }
550
551