summaryrefslogtreecommitdiff
path: root/src/soc/intel/denverton_ns/soc_util.c
blob: 1626927ef20966efb507b261db73d386a3ba55cc (plain)
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
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2014 - 2017 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <stdint.h>
#include <arch/io.h>
#include <device/pci.h>
#include <device/pci_def.h>
#include <device/device.h>
#include <console/console.h>

#include <soc/iomap.h>
#include <soc/soc_util.h>
#include <soc/pmc.h>
#include <soc/smbus.h>
#include <soc/lpc.h>
#include <soc/pci_devs.h>
#include <soc/systemagent.h>

device_t get_hostbridge_dev(void)
{
#if defined(__PRE_RAM__) || defined(__SMM__)
	return PCI_DEV(0, SA_DEV, SA_FUNC);
#else
	return dev_find_slot(0, PCI_DEVFN(SA_DEV, SA_FUNC));
#endif
}

device_t get_lpc_dev(void)
{
#if defined(__PRE_RAM__) || defined(__SMM__)
	return PCI_DEV(0, LPC_DEV, LPC_FUNC);
#else
	return dev_find_slot(0, PCI_DEVFN(LPC_DEV, LPC_FUNC));
#endif
}

device_t get_pmc_dev(void)
{
#if defined(__PRE_RAM__) || defined(__SMM__)
	return PCI_DEV(0, PMC_DEV, PMC_FUNC);
#else
	return dev_find_slot(0, PCI_DEVFN(PMC_DEV, PMC_FUNC));
#endif
}

device_t get_smbus_dev(void)
{
#if defined(__PRE_RAM__) || defined(__SMM__)
	return PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
#else
	return dev_find_slot(0, PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC));
#endif
}

uint32_t get_pciebase(void)
{
	device_t dev;
	u32 pciexbar_reg;

	dev = get_hostbridge_dev();
	if (!dev)
		return 0;

	pciexbar_reg = pci_read_config32(dev, PCIEXBAR);

	if (!(pciexbar_reg & (1 << 0)))
		return 0;

	switch (pciexbar_reg & MASK_PCIEXBAR_LENGTH) {
	case MASK_PCIEXBAR_LENGTH_256M:
		pciexbar_reg &= MASK_PCIEXBAR_256M;
		break;
	case MASK_PCIEXBAR_LENGTH_128M:
		pciexbar_reg &= MASK_PCIEXBAR_128M;
		break;
	case MASK_PCIEXBAR_LENGTH_64M:
		pciexbar_reg &= MASK_PCIEXBAR_64M;
		break;
	default:
		pciexbar_reg &= MASK_PCIEXBAR_256M;
		break;
	}

	return pciexbar_reg;
}

uint32_t get_pcielength(void)
{
	device_t dev;
	u32 pciexbar_reg;

	dev = get_hostbridge_dev();
	if (!dev)
		return 0;

	pciexbar_reg = pci_read_config32(dev, PCIEXBAR);

	if (!(pciexbar_reg & (1 << 0)))
		return 0;

	switch (pciexbar_reg & MASK_PCIEXBAR_LENGTH) {
	case MASK_PCIEXBAR_LENGTH_256M:
		pciexbar_reg = 256;
		break;
	case MASK_PCIEXBAR_LENGTH_128M:
		pciexbar_reg = 128;
		break;
	case MASK_PCIEXBAR_LENGTH_64M:
		pciexbar_reg = 64;
		break;
	default:
		pciexbar_reg = 64;
		break;
	}

	return pciexbar_reg;
}

uint32_t get_tseg_memory(void)
{
	device_t dev = get_hostbridge_dev();

	if (!dev)
		return 0;

	return pci_read_config32(dev, TSEGMB) & MASK_TSEGMB;
}

uint32_t get_top_of_low_memory(void)
{
	device_t dev = get_hostbridge_dev();

	if (!dev)
		return 0;

	return pci_read_config32(dev, TOLUD) & MASK_TOLUD;
}

uint64_t get_top_of_upper_memory(void)
{
	device_t dev = get_hostbridge_dev();

	if (!dev)
		return 0;

	return ((uint64_t)(pci_read_config32(dev, TOUUD_HI) & MASK_TOUUD_HI)
		<< 32) +
	       (uint64_t)(pci_read_config32(dev, TOUUD_LO) & MASK_TOUUD_LO);
}

uint16_t get_pmbase(void)
{
	device_t dev = get_pmc_dev();

	if (!dev)
		return 0;

	return pci_read_config16(dev, PMC_ACPI_BASE) & 0xfff8;
}

uint16_t get_tcobase(void)
{
	device_t dev = get_smbus_dev();

	if (!dev)
		return 0;

	return pci_read_config16(dev, TCOBASE) & MASK_TCOBASE;
}

void mmio_andthenor32(void *addr, uint32_t val2and, uint32_t val2or)
{
	uint32_t reg32;

	reg32 = read32(addr);
	reg32 &= (uint32_t)val2and;
	reg32 |= (uint32_t)val2or;
	write32(addr, reg32);
}

uint8_t silicon_stepping(void)
{
	uint8_t revision_id;
	device_t dev = get_lpc_dev();

	if (!dev)
		return 0;

	revision_id = pci_read_config8(dev, PCI_REVISION_ID);

	return revision_id;
}

void *memcpy_s(void *dest, const void *src, size_t n)
{
	uint8_t *dp;
	const uint8_t *sp;

	dp = (uint8_t *)dest;
	sp = (uint8_t *)src;

	if (!n)
		return dest;

	if (n > UINT32_MAX)
		return dest;

	if (!dp)
		return dest;

	if (!sp)
		return dest;

	/*
	 * overlap is undefined behavior, do not allow
	 */
	if (((dp > sp) && (dp < (sp + n))) || ((sp > dp) && (sp < (dp + n))))
		return dest;

	/*
	 * now perform the copy
	 */

	/* Original memcpy() function */
	unsigned long d0, d1, d2;

	asm volatile(
#ifdef __x86_64__
		"rep ; movsd\n\t"
		"mov %4,%%rcx\n\t"
#else
		"rep ; movsl\n\t"
		"movl %4,%%ecx\n\t"
#endif
		"rep ; movsb\n\t"
		: "=&c"(d0), "=&D"(d1), "=&S"(d2)
		: "0"(n >> 2), "g"(n & 3), "1"(dest), "2"(src)
		: "memory");

	return dest;
}