2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2013 Google Inc.
6 * Copyright (C) 2015 Intel Corp.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <arch/acpi.h>
19 #include <arch/acpigen.h>
22 #include <arch/smp/mpspec.h>
25 #include <console/console.h>
27 #include <cpu/intel/turbo.h>
28 #include <cpu/x86/msr.h>
29 #include <cpu/x86/smm.h>
30 #include <cpu/x86/tsc.h>
31 #include <device/pci.h>
32 #include <device/pci_ids.h>
33 #include <ec/google/chromeec/ec.h>
38 #include <soc/iomap.h>
41 #include <soc/pattrs.h>
42 #include <soc/pci_devs.h>
46 #include <vendorcode/google/chromeos/gnvs.h>
48 #define MWAIT_RES(state, sub_state) \
50 .addrl = (((state) << 4) | (sub_state)), \
51 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
52 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
53 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
54 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
57 /* C-state map without S0ix */
58 static acpi_cstate_t cstate_map
[] = {
61 .ctype
= 1, /* ACPI C1 */
64 .resource
= MWAIT_RES(0, 0),
67 /* C6NS with no L2 shrink */
68 /* NOTE: this substate is above CPUID limit */
69 .ctype
= 2, /* ACPI C2 */
72 .resource
= MWAIT_RES(5, 1),
75 /* C6FS with full L2 shrink */
76 .ctype
= 3, /* ACPI C3 */
77 .latency
= 1500, /* 1.5ms worst case */
79 .resource
= MWAIT_RES(5, 2),
83 void acpi_init_gnvs(global_nvs_t
*gnvs
)
85 /* Set unknown wake source */
89 gnvs
->pcnt
= dev_count_cpu();
91 /* Top of Low Memory (start of resource allocation) */
92 gnvs
->tolm
= nc_read_top_of_low_memory();
94 #if IS_ENABLED(CONFIG_CONSOLE_CBMEM)
95 /* Update the mem console pointer. */
96 gnvs
->cbmc
= (u32
)cbmem_find(CBMEM_ID_CONSOLE
);
99 #if IS_ENABLED(CONFIG_CHROMEOS)
100 /* Initialize Verified Boot data */
101 chromeos_init_vboot(&(gnvs
->chromeos
));
102 #if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC)
103 gnvs
->chromeos
.vbt2
= google_ec_running_ro() ?
104 ACTIVE_ECFW_RO
: ACTIVE_ECFW_RW
;
109 static int acpi_sci_irq(void)
111 u32
*actl
= (u32
*)(ILB_BASE_ADDRESS
+ ACTL
);
118 /* Determine how SCI is routed. */
119 scis
= read32(actl
) & SCIS_MASK
;
124 sci_irq
= scis
- SCIS_IRQ9
+ 9;
130 sci_irq
= scis
- SCIS_IRQ20
+ 20;
133 printk(BIOS_DEBUG
, "Invalid SCI route! Defaulting to IRQ9.\n");
138 printk(BIOS_DEBUG
, "SCI is IRQ%d\n", sci_irq
);
142 unsigned long acpi_fill_mcfg(unsigned long current
)
144 current
+= acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t
*)current
,
145 MCFG_BASE_ADDRESS
, 0, 0, 255);
149 void acpi_fill_in_fadt(acpi_fadt_t
*fadt
)
151 const uint16_t pmbase
= ACPI_BASE_ADDRESS
;
153 fadt
->sci_int
= acpi_sci_irq();
154 fadt
->smi_cmd
= APM_CNT
;
155 fadt
->acpi_enable
= APM_CNT_ACPI_ENABLE
;
156 fadt
->acpi_disable
= APM_CNT_ACPI_DISABLE
;
157 fadt
->s4bios_req
= 0x0;
158 fadt
->pstate_cnt
= 0;
160 fadt
->pm1a_evt_blk
= pmbase
+ PM1_STS
;
161 fadt
->pm1b_evt_blk
= 0x0;
162 fadt
->pm1a_cnt_blk
= pmbase
+ PM1_CNT
;
163 fadt
->pm1b_cnt_blk
= 0x0;
164 fadt
->pm2_cnt_blk
= pmbase
+ PM2A_CNT_BLK
;
165 fadt
->pm_tmr_blk
= pmbase
+ PM1_TMR
;
166 fadt
->gpe0_blk
= pmbase
+ GPE0_STS
;
169 fadt
->pm1_evt_len
= 4;
170 fadt
->pm1_cnt_len
= 2;
171 fadt
->pm2_cnt_len
= 1;
172 fadt
->pm_tmr_len
= 4;
173 fadt
->gpe0_blk_len
= 2 * (GPE0_EN
- GPE0_STS
);
174 fadt
->gpe1_blk_len
= 0;
177 fadt
->p_lvl2_lat
= 1;
178 fadt
->p_lvl3_lat
= 87;
179 fadt
->flush_size
= 1024;
180 fadt
->flush_stride
= 16;
181 fadt
->duty_offset
= 1;
182 fadt
->duty_width
= 0;
183 fadt
->day_alrm
= 0xd;
184 fadt
->mon_alrm
= 0x00;
185 fadt
->century
= 0x00;
186 fadt
->iapc_boot_arch
= ACPI_FADT_LEGACY_DEVICES
| ACPI_FADT_8042
;
188 fadt
->flags
= ACPI_FADT_WBINVD
| ACPI_FADT_C1_SUPPORTED
|
189 ACPI_FADT_C2_MP_SUPPORTED
| ACPI_FADT_SLEEP_BUTTON
|
190 ACPI_FADT_RESET_REGISTER
| ACPI_FADT_SEALED_CASE
|
191 ACPI_FADT_S4_RTC_WAKE
| ACPI_FADT_PLATFORM_CLOCK
;
193 fadt
->reset_reg
.space_id
= 1;
194 fadt
->reset_reg
.bit_width
= 8;
195 fadt
->reset_reg
.bit_offset
= 0;
196 fadt
->reset_reg
.resv
= 0;
197 fadt
->reset_reg
.addrl
= 0xcf9;
198 fadt
->reset_reg
.addrh
= 0;
199 fadt
->reset_value
= 6;
201 fadt
->x_pm1a_evt_blk
.space_id
= 1;
202 fadt
->x_pm1a_evt_blk
.bit_width
= fadt
->pm1_evt_len
* 8;
203 fadt
->x_pm1a_evt_blk
.bit_offset
= 0;
204 fadt
->x_pm1a_evt_blk
.resv
= 0;
205 fadt
->x_pm1a_evt_blk
.addrl
= pmbase
+ PM1_STS
;
206 fadt
->x_pm1a_evt_blk
.addrh
= 0x0;
208 fadt
->x_pm1b_evt_blk
.space_id
= 1;
209 fadt
->x_pm1b_evt_blk
.bit_width
= 0;
210 fadt
->x_pm1b_evt_blk
.bit_offset
= 0;
211 fadt
->x_pm1b_evt_blk
.resv
= 0;
212 fadt
->x_pm1b_evt_blk
.addrl
= 0x0;
213 fadt
->x_pm1b_evt_blk
.addrh
= 0x0;
215 fadt
->x_pm1a_cnt_blk
.space_id
= 1;
216 fadt
->x_pm1a_cnt_blk
.bit_width
= fadt
->pm1_cnt_len
* 8;
217 fadt
->x_pm1a_cnt_blk
.bit_offset
= 0;
218 fadt
->x_pm1a_cnt_blk
.resv
= 0;
219 fadt
->x_pm1a_cnt_blk
.addrl
= pmbase
+ PM1_CNT
;
220 fadt
->x_pm1a_cnt_blk
.addrh
= 0x0;
222 fadt
->x_pm1b_cnt_blk
.space_id
= 1;
223 fadt
->x_pm1b_cnt_blk
.bit_width
= 0;
224 fadt
->x_pm1b_cnt_blk
.bit_offset
= 0;
225 fadt
->x_pm1b_cnt_blk
.resv
= 0;
226 fadt
->x_pm1b_cnt_blk
.addrl
= 0x0;
227 fadt
->x_pm1b_cnt_blk
.addrh
= 0x0;
229 fadt
->x_pm2_cnt_blk
.space_id
= 1;
230 fadt
->x_pm2_cnt_blk
.bit_width
= fadt
->pm2_cnt_len
* 8;
231 fadt
->x_pm2_cnt_blk
.bit_offset
= 0;
232 fadt
->x_pm2_cnt_blk
.resv
= 0;
233 fadt
->x_pm2_cnt_blk
.addrl
= pmbase
+ PM2A_CNT_BLK
;
234 fadt
->x_pm2_cnt_blk
.addrh
= 0x0;
236 fadt
->x_pm_tmr_blk
.space_id
= 1;
237 fadt
->x_pm_tmr_blk
.bit_width
= fadt
->pm_tmr_len
* 8;
238 fadt
->x_pm_tmr_blk
.bit_offset
= 0;
239 fadt
->x_pm_tmr_blk
.resv
= 0;
240 fadt
->x_pm_tmr_blk
.addrl
= pmbase
+ PM1_TMR
;
241 fadt
->x_pm_tmr_blk
.addrh
= 0x0;
243 fadt
->x_gpe0_blk
.space_id
= 1;
244 fadt
->x_gpe0_blk
.bit_width
= fadt
->gpe0_blk_len
* 8;
245 fadt
->x_gpe0_blk
.bit_offset
= 0;
246 fadt
->x_gpe0_blk
.resv
= 0;
247 fadt
->x_gpe0_blk
.addrl
= pmbase
+ GPE0_STS
;
248 fadt
->x_gpe0_blk
.addrh
= 0x0;
250 fadt
->x_gpe1_blk
.space_id
= 1;
251 fadt
->x_gpe1_blk
.bit_width
= 0;
252 fadt
->x_gpe1_blk
.bit_offset
= 0;
253 fadt
->x_gpe1_blk
.resv
= 0;
254 fadt
->x_gpe1_blk
.addrl
= 0x0;
255 fadt
->x_gpe1_blk
.addrh
= 0x0;
258 static acpi_tstate_t soc_tss_table
[] = {
259 { 100, 1000, 0, 0x00, 0 },
260 { 88, 875, 0, 0x1e, 0 },
261 { 75, 750, 0, 0x1c, 0 },
262 { 63, 625, 0, 0x1a, 0 },
263 { 50, 500, 0, 0x18, 0 },
264 { 38, 375, 0, 0x16, 0 },
265 { 25, 250, 0, 0x14, 0 },
266 { 13, 125, 0, 0x12, 0 },
269 static void generate_t_state_entries(int core
, int cores_per_package
)
271 /* Indicate SW_ALL coordination for T-states */
272 acpigen_write_TSD_package(core
, cores_per_package
, SW_ALL
);
274 /* Indicate FFixedHW so OS will use MSR */
275 acpigen_write_empty_PTC();
277 /* Set NVS controlled T-state limit */
278 acpigen_write_TPC("\\TLVL");
280 /* Write TSS table for MSR access */
281 acpigen_write_TSS_package(
282 ARRAY_SIZE(soc_tss_table
), soc_tss_table
);
285 static int calculate_power(int tdp
, int p1_ratio
, int ratio
)
291 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
293 * Power = (ratio / p1_ratio) * m * tdp
296 m
= (110000 - ((p1_ratio
- ratio
) * 625)) / 11;
299 power
= ((ratio
* 100000 / p1_ratio
) / 100);
300 power
*= (m
/ 100) * (tdp
/ 1000);
306 static void generate_p_state_entries(int core
, int cores_per_package
)
308 int ratio_min
, ratio_max
, ratio_turbo
, ratio_step
, ratio_range_2
;
309 int coord_type
, power_max
, power_unit
, num_entries
;
310 int ratio
, power
, clock
, clock_max
;
311 int vid
, vid_turbo
, vid_min
, vid_max
, vid_range_2
;
313 const struct pattrs
*pattrs
= pattrs_get();
316 /* Inputs from CPU attributes */
317 ratio_max
= pattrs
->iacore_ratios
[IACORE_MAX
];
318 ratio_min
= pattrs
->iacore_ratios
[IACORE_LFM
];
319 vid_max
= pattrs
->iacore_vids
[IACORE_MAX
];
320 vid_min
= pattrs
->iacore_vids
[IACORE_LFM
];
322 /* Set P-states coordination type based on MSR disable bit */
323 coord_type
= (pattrs
->num_cpus
> 2) ? SW_ALL
: HW_ALL
;
325 /* Max Non-Turbo Frequency */
326 clock_max
= (ratio_max
* pattrs
->bclk_khz
) / 1000;
328 /* Calculate CPU TDP in mW */
329 msr
= rdmsr(MSR_PKG_POWER_SKU_UNIT
);
330 power_unit
= 1 << (msr
.lo
& 0xf);
331 msr
= rdmsr(MSR_PKG_POWER_LIMIT
);
332 power_max
= ((msr
.lo
& 0x7fff) / power_unit
) * 1000;
334 /* Write _PCT indicating use of FFixedHW */
335 acpigen_write_empty_PCT();
337 /* Write _PPC with NVS specified limit on supported P-state */
338 acpigen_write_PPC_NVS();
340 /* Write PSD indicating configured coordination type */
341 acpigen_write_PSD_package(core
, 1, coord_type
);
343 /* Add P-state entries in _PSS table */
344 acpigen_write_name("_PSS");
346 /* Determine ratio points */
348 num_entries
= (ratio_max
- ratio_min
) / ratio_step
;
349 while (num_entries
> 15) { /* ACPI max is 15 ratios */
354 /* P[T] is Turbo state if enabled */
355 if (get_turbo_state() == TURBO_ENABLED
) {
356 /* _PSS package count including Turbo */
357 acpigen_write_package(num_entries
+ 2);
359 ratio_turbo
= pattrs
->iacore_ratios
[IACORE_TURBO
];
360 vid_turbo
= pattrs
->iacore_vids
[IACORE_TURBO
];
361 control_status
= (ratio_turbo
<< 8) | vid_turbo
;
363 /* Add entry for Turbo ratio */
364 acpigen_write_PSS_package(
365 clock_max
+ 1, /* MHz */
369 control_status
, /* control */
370 control_status
); /* status */
372 /* _PSS package count without Turbo */
373 acpigen_write_package(num_entries
+ 1);
374 ratio_turbo
= ratio_max
;
378 /* First regular entry is max non-turbo ratio */
379 control_status
= (ratio_max
<< 8) | vid_max
;
380 acpigen_write_PSS_package(
385 control_status
, /* control */
386 control_status
); /* status */
388 /* Set up ratio and vid ranges for VID calculation */
389 ratio_range_2
= (ratio_turbo
- ratio_min
) * 2;
390 vid_range_2
= (vid_turbo
- vid_min
) * 2;
392 /* Generate the remaining entries */
393 for (ratio
= ratio_min
+ ((num_entries
- 1) * ratio_step
);
394 ratio
>= ratio_min
; ratio
-= ratio_step
) {
396 /* Calculate VID for this ratio */
397 vid
= ((ratio
- ratio_min
) * vid_range_2
) /
398 ratio_range_2
+ vid_min
;
399 /* Round up if remainder */
400 if (((ratio
- ratio_min
) * vid_range_2
) % ratio_range_2
)
403 /* Calculate power at this ratio */
404 power
= calculate_power(power_max
, ratio_max
, ratio
);
405 clock
= (ratio
* pattrs
->bclk_khz
) / 1000;
406 control_status
= (ratio
<< 8) | (vid
& 0xff);
408 acpigen_write_PSS_package(
413 control_status
, /* control */
414 control_status
); /* status */
417 /* Fix package length */
421 void generate_cpu_entries(device_t device
)
424 int pcontrol_blk
= get_pmbase(), plen
= 6;
425 const struct pattrs
*pattrs
= pattrs_get();
427 for (core
= 0; core
< pattrs
->num_cpus
; core
++) {
433 /* Generate processor \_PR.CPUx */
434 acpigen_write_processor(
435 core
, pcontrol_blk
, plen
);
437 /* Generate P-state tables */
438 generate_p_state_entries(
439 core
, pattrs
->num_cpus
);
441 /* Generate C-state tables */
442 acpigen_write_CST_package(
443 cstate_map
, ARRAY_SIZE(cstate_map
));
445 /* Generate T-state tables */
446 generate_t_state_entries(
447 core
, pattrs
->num_cpus
);
453 unsigned long acpi_madt_irq_overrides(unsigned long current
)
455 int sci_irq
= acpi_sci_irq();
456 acpi_madt_irqoverride_t
*irqovr
;
457 uint16_t sci_flags
= MP_IRQ_TRIGGER_LEVEL
;
460 irqovr
= (void *)current
;
461 current
+= acpi_create_madt_irqoverride(irqovr
, 0, 0, 2, 0);
464 sci_flags
|= MP_IRQ_POLARITY_LOW
;
466 sci_flags
|= MP_IRQ_POLARITY_HIGH
;
468 irqovr
= (void *)current
;
469 current
+= acpi_create_madt_irqoverride(irqovr
, 0, sci_irq
, sci_irq
,
475 #define ALIGN_CURRENT current = (ALIGN(current, 16))
477 unsigned long southcluster_write_acpi_tables(device_t device
,
478 unsigned long current
,
479 struct acpi_rsdp
*rsdp
)
481 acpi_header_t
*ssdt2
;
483 current
= acpi_write_hpet(device
, current
, rsdp
);
486 #if CONFIG_GOP_SUPPORT
487 igd_opregion_t
*opregion
;
489 printk(BIOS_DEBUG
, "ACPI: * IGD OpRegion\n");
490 opregion
= (igd_opregion_t
*)current
;
491 init_igd_opregion(opregion
);
492 current
+= sizeof(igd_opregion_t
);
496 ssdt2
= (acpi_header_t
*)current
;
497 memset(ssdt2
, 0, sizeof(acpi_header_t
));
498 acpi_create_serialio_ssdt(ssdt2
);
500 current
+= ssdt2
->length
;
501 acpi_add_table(rsdp
, ssdt2
);
502 printk(BIOS_DEBUG
, "ACPI: * SSDT2 @ %p Length %x\n",ssdt2
,
507 printk(BIOS_DEBUG
, "ACPI: * SSDT2 not generated.\n");
510 printk(BIOS_DEBUG
, "current = %lx\n", current
);
515 void southcluster_inject_dsdt(device_t device
)
519 gnvs
= cbmem_find(CBMEM_ID_ACPI_GNVS
);
521 gnvs
= cbmem_add(CBMEM_ID_ACPI_GNVS
, sizeof (*gnvs
));
523 memset(gnvs
, 0, sizeof(*gnvs
));
527 acpi_create_gnvs(gnvs
);
528 acpi_save_gnvs((unsigned long)gnvs
);
529 /* And tell SMI about it */
530 smm_setup_structures(gnvs
, NULL
, NULL
);
532 /* Add it to DSDT. */
533 acpigen_write_scope("\\");
534 acpigen_write_name_dword("NVSA", (u32
) gnvs
);
539 __attribute__((weak
)) void acpi_create_serialio_ssdt(acpi_header_t
*ssdt
)
543 #if CONFIG_GOP_SUPPORT
544 /* Reading VBT table from flash */
545 static void get_fsp_vbt(igd_opregion_t
*opregion
)
547 const optionrom_vbt_t
*vbt
;
550 vbt
= fsp_get_vbt(&vbt_len
);
552 die("vbt data not found");
553 memcpy(opregion
->header
.vbios_version
, vbt
->coreblock_biosbuild
, 4);
554 memcpy(opregion
->vbt
.gvd1
, vbt
, vbt
->hdr_vbt_size
<
555 sizeof(opregion
->vbt
.gvd1
) ? vbt
->hdr_vbt_size
:
556 sizeof(opregion
->vbt
.gvd1
));
559 /* Initialize IGD OpRegion, called from ACPI code */
560 int init_igd_opregion(igd_opregion_t
*opregion
)
565 memset(opregion
, 0, sizeof(igd_opregion_t
));
567 /* FIXME if IGD is disabled, we should exit here. */
569 memcpy(&opregion
->header
.signature
, IGD_OPREGION_SIGNATURE
,
570 sizeof(IGD_OPREGION_SIGNATURE
));
573 opregion
->header
.size
= sizeof(igd_opregion_t
) / 1024;
574 opregion
->header
.version
= IGD_OPREGION_VERSION
;
576 /* FIXME We just assume we're mobile for now */
577 opregion
->header
.mailboxes
= MAILBOXES_MOBILE
;
579 /* TODO Initialize Mailbox 1 */
581 /* TODO Initialize Mailbox 3 */
582 opregion
->mailbox3
.bclp
= IGD_BACKLIGHT_BRIGHTNESS
;
583 opregion
->mailbox3
.pfit
= IGD_FIELD_VALID
| IGD_PFIT_STRETCH
;
584 opregion
->mailbox3
.pcft
= 0; /* should be (IMON << 1) & 0x3e */
585 opregion
->mailbox3
.cblv
= IGD_FIELD_VALID
| IGD_INITIAL_BRIGHTNESS
;
586 opregion
->mailbox3
.bclm
[0] = IGD_WORD_FIELD_VALID
+ 0x0000;
587 opregion
->mailbox3
.bclm
[1] = IGD_WORD_FIELD_VALID
+ 0x0a19;
588 opregion
->mailbox3
.bclm
[2] = IGD_WORD_FIELD_VALID
+ 0x1433;
589 opregion
->mailbox3
.bclm
[3] = IGD_WORD_FIELD_VALID
+ 0x1e4c;
590 opregion
->mailbox3
.bclm
[4] = IGD_WORD_FIELD_VALID
+ 0x2866;
591 opregion
->mailbox3
.bclm
[5] = IGD_WORD_FIELD_VALID
+ 0x327f;
592 opregion
->mailbox3
.bclm
[6] = IGD_WORD_FIELD_VALID
+ 0x3c99;
593 opregion
->mailbox3
.bclm
[7] = IGD_WORD_FIELD_VALID
+ 0x46b2;
594 opregion
->mailbox3
.bclm
[8] = IGD_WORD_FIELD_VALID
+ 0x50cc;
595 opregion
->mailbox3
.bclm
[9] = IGD_WORD_FIELD_VALID
+ 0x5ae5;
596 opregion
->mailbox3
.bclm
[10] = IGD_WORD_FIELD_VALID
+ 0x64ff;
598 get_fsp_vbt(opregion
);
601 * TODO This needs to happen in S3 resume, too.
602 * Maybe it should move to the finalize handler
604 igd
= dev_find_slot(0, PCI_DEVFN(GFX_DEV
, GFX_FUNC
));
606 pci_write_config32(igd
, ASLS
, (u32
)opregion
);
607 reg16
= pci_read_config16(igd
, SWSCI
);
610 pci_write_config16(igd
, SWSCI
, reg16
);