blob: 8be63ef80eccb736ecafa24c979a28adde52197f [file] [log] [blame]
Damien Zammit62477932015-05-03 21:34:38 +10001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2015 Damien Zammit <damien@zamaudio.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
Damien Zammit62477932015-05-03 21:34:38 +100017#define __SIMPLE_DEVICE__
18
Kyösti Mälkki6e2d0c12019-06-28 10:08:51 +030019#include <arch/cpu.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Damien Zammitf7060f12015-11-14 00:59:21 +110021#include <device/device.h>
22#include <device/pci_def.h>
23#include <console/console.h>
Damien Zammit62477932015-05-03 21:34:38 +100024#include <cbmem.h>
25#include <northbridge/intel/pineview/pineview.h>
Arthur Heymans62e784b2017-04-21 15:54:44 +020026#include <cpu/x86/mtrr.h>
27#include <cpu/intel/romstage.h>
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030028#include <cpu/intel/smm_reloc.h>
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030029#include <stdint.h>
30#include <stage_cache.h>
Damien Zammit62477932015-05-03 21:34:38 +100031
Damien Zammitf7060f12015-11-14 00:59:21 +110032u8 decode_pciebar(u32 *const base, u32 *const len)
Damien Zammit62477932015-05-03 21:34:38 +100033{
Damien Zammitf7060f12015-11-14 00:59:21 +110034 *base = 0;
35 *len = 0;
36 const pci_devfn_t dev = PCI_DEV(0,0,0);
37 u32 pciexbar = 0;
38 u32 pciexbar_reg;
39 u32 reg32;
40 int max_buses;
41 const struct {
42 u16 num_buses;
43 u32 addr_mask;
44 } busmask[] = {
45 {256, 0xf0000000},
46 {128, 0xf8000000},
47 {64, 0xfc000000},
48 {0, 0},
49 };
Damien Zammit62477932015-05-03 21:34:38 +100050
Damien Zammitf7060f12015-11-14 00:59:21 +110051 pciexbar_reg = pci_read_config32(dev, PCIEXBAR);
52
53 // MMCFG not supported or not enabled.
54 if (!(pciexbar_reg & (1 << 0))) {
55 printk(BIOS_WARNING, "WARNING: MMCONF not set\n");
56 return 0;
Damien Zammit62477932015-05-03 21:34:38 +100057 }
Damien Zammitf7060f12015-11-14 00:59:21 +110058
59 reg32 = (pciexbar_reg >> 1) & 3;
60 pciexbar = pciexbar_reg & busmask[reg32].addr_mask;
61 max_buses = busmask[reg32].num_buses;
62
63 if (!pciexbar) {
64 printk(BIOS_WARNING, "WARNING: pciexbar invalid\n");
65 return 0;
66 }
67
68 *base = pciexbar;
69 *len = max_buses << 20;
70 return 1;
Damien Zammit62477932015-05-03 21:34:38 +100071}
72
Damien Zammitf7060f12015-11-14 00:59:21 +110073/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
74u32 decode_igd_memory_size(const u32 gms)
Damien Zammit62477932015-05-03 21:34:38 +100075{
Damien Zammitf7060f12015-11-14 00:59:21 +110076 const u32 gmssize[] = {
77 0, 1, 4, 8, 16, 32, 48, 64, 128, 256
78 };
79
80 if (gms > 9) {
81 printk(BIOS_DEBUG, "Bad Graphics Mode Select (GMS) value.\n");
82 return 0;
83 }
84 return gmssize[gms] << 10;
85}
86
87/** Decodes used Graphics Stolen Memory (GSM) to kilobytes. */
88u32 decode_igd_gtt_size(const u32 gsm)
89{
90 const u8 gsmsize[] = {
91 0, 1, 0, 0,
92 };
93
94 if (gsm > 3) {
95 printk(BIOS_DEBUG, "Bad Graphics Stolen Memory (GSM) value.\n");
96 return 0;
97 }
98 return (u32)(gsmsize[gsm] << 10);
Damien Zammit62477932015-05-03 21:34:38 +100099}
Arthur Heymans62e784b2017-04-21 15:54:44 +0200100
Arthur Heymansde6bda62018-04-10 13:40:39 +0200101/** Decodes used TSEG size to bytes. */
102static u32 decode_tseg_size(const u32 esmramc)
103{
104 if (!(esmramc & 1))
105 return 0;
106
107 switch ((esmramc >> 1) & 3) {
108 case 0:
109 return 1 << 20;
110 case 1:
111 return 2 << 20;
112 case 2:
113 return 8 << 20;
114 case 3:
115 default:
116 die("Bad TSEG setting.\n");
117 }
118}
119
120u32 northbridge_get_tseg_size(void)
121{
122 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC);
123 return decode_tseg_size(esmramc);
124}
125
126u32 northbridge_get_tseg_base(void)
127{
128 return pci_read_config32(PCI_DEV(0, 0, 0), TSEG);
129}
130
131
Arthur Heymans62e784b2017-04-21 15:54:44 +0200132/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +0200133 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Arthur Heymans62e784b2017-04-21 15:54:44 +0200134 * CBMEM top downwards to 4 MiB boundary.
135 */
136void *cbmem_top(void)
137{
Arthur Heymansde6bda62018-04-10 13:40:39 +0200138 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Arthur Heymans62e784b2017-04-21 15:54:44 +0200139 return (void *) top_of_ram;
Arthur Heymansde6bda62018-04-10 13:40:39 +0200140
Arthur Heymans62e784b2017-04-21 15:54:44 +0200141}
142
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300143void stage_cache_external_region(void **base, size_t *size)
144{
Kyösti Mälkkibccd2b62019-08-02 06:12:03 +0300145 /* The stage cache lives at the end of the TSEG region.
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300146 * The top of RAM is defined to be the TSEG base address.
147 */
148 *size = CONFIG_SMM_RESERVED_SIZE;
Kyösti Mälkkibccd2b62019-08-02 06:12:03 +0300149 *base = (void *)((uintptr_t)northbridge_get_tseg_base()
150 + northbridge_get_tseg_size() - CONFIG_SMM_RESERVED_SIZE);
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300151}
152
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300153void fill_postcar_frame(struct postcar_frame *pcf)
Arthur Heymans62e784b2017-04-21 15:54:44 +0200154{
Arthur Heymans62e784b2017-04-21 15:54:44 +0200155 uintptr_t top_of_ram;
156
Arthur Heymansde6bda62018-04-10 13:40:39 +0200157 /* Cache 8 MiB region below the top of ram and 2 MiB above top of
158 * ram to cover both cbmem as the TSEG region.
Arthur Heymans62e784b2017-04-21 15:54:44 +0200159 */
160 top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300161 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
Arthur Heymansde6bda62018-04-10 13:40:39 +0200162 MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300163 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymansde6bda62018-04-10 13:40:39 +0200164 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Arthur Heymans62e784b2017-04-21 15:54:44 +0200165}