aboutsummaryrefslogtreecommitdiff
path: root/sys/x86/pci/pci_early_quirks.c
blob: 29788cdd19d44039ed52dc5beedacc5ec32cc6a2 (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2018 Johannes Lundberg
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <vm/vm.h>
/* XXX: enable this once the KPI is available */
/* #include <x86/physmem.h> */
#include <machine/pci_cfgreg.h>
#include <machine/md_var.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>

#include <x86/pci/pci_early_quirks.h>

#define	MiB(v) ((unsigned long)(v) << 20)

struct pci_device_id {
	uint32_t	vendor;
	uint32_t	device;
	const struct intel_stolen_ops *data;
};

/*
 * These global variables are read by LinuxKPI.
 * LinuxKPI provide this information to the i915 driver.
 */
vm_paddr_t intel_graphics_stolen_base = 0;
vm_paddr_t intel_graphics_stolen_size = 0;

/*
 * Intel early quirks functions
 */
static vm_paddr_t
intel_stolen_base_gen3(int bus, int slot, int func)
{
	uint32_t ctrl;
	vm_paddr_t val;

	ctrl = pci_cfgregread(bus, slot, func, INTEL_BSM, 4);
	val = ctrl & INTEL_BSM_MASK;
	return (val);
}

static vm_paddr_t
intel_stolen_size_gen3(int bus, int slot, int func)
{
	uint32_t ctrl;
	vm_paddr_t val;

	ctrl = pci_cfgregread(0, 0, 0, I830_GMCH_CTRL, 2);
	val = ctrl & I855_GMCH_GMS_MASK;

	switch (val) {
	case I855_GMCH_GMS_STOLEN_1M:
		return (MiB(1));
	case I855_GMCH_GMS_STOLEN_4M:
		return (MiB(4));
	case I855_GMCH_GMS_STOLEN_8M:
		return (MiB(8));
	case I855_GMCH_GMS_STOLEN_16M:
		return (MiB(16));
	case I855_GMCH_GMS_STOLEN_32M:
		return (MiB(32));
	case I915_GMCH_GMS_STOLEN_48M:
		return (MiB(48));
	case I915_GMCH_GMS_STOLEN_64M:
		return (MiB(64));
	case G33_GMCH_GMS_STOLEN_128M:
		return (MiB(128));
	case G33_GMCH_GMS_STOLEN_256M:
		return (MiB(256));
	case INTEL_GMCH_GMS_STOLEN_96M:
		return (MiB(96));
	case INTEL_GMCH_GMS_STOLEN_160M:
		return (MiB(160));
	case INTEL_GMCH_GMS_STOLEN_224M:
		return (MiB(224));
	case INTEL_GMCH_GMS_STOLEN_352M:
		return (MiB(352));
	}
	return (0);
}

static vm_paddr_t
intel_stolen_size_gen6(int bus, int slot, int func)
{
	uint32_t ctrl;
	vm_paddr_t val;

	ctrl = pci_cfgregread(bus, slot, func, SNB_GMCH_CTRL, 2);
	val = (ctrl >> SNB_GMCH_GMS_SHIFT) & SNB_GMCH_GMS_MASK;
	return (val * MiB(32));
}

static vm_paddr_t
intel_stolen_size_gen8(int bus, int slot, int func)
{
	uint32_t ctrl;
	vm_paddr_t val;

	ctrl = pci_cfgregread(bus, slot, func, SNB_GMCH_CTRL, 2);
	val = (ctrl >> BDW_GMCH_GMS_SHIFT) & BDW_GMCH_GMS_MASK;
	return (val * MiB(32));
}

static vm_paddr_t
intel_stolen_size_chv(int bus, int slot, int func)
{
	uint32_t ctrl;
	vm_paddr_t val;

	ctrl = pci_cfgregread(bus, slot, func, SNB_GMCH_CTRL, 2);
	val = (ctrl >> SNB_GMCH_GMS_SHIFT) & SNB_GMCH_GMS_MASK;

	/*
	 * 0x0  to 0x10: 32MB increments starting at 0MB
	 * 0x11 to 0x16: 4MB increments starting at 8MB
	 * 0x17 to 0x1d: 4MB increments start at 36MB
	 */
	if (val < 0x11)
		return (val * MiB(32));
	else if (val < 0x17)
		return ((val - 0x11) * MiB(4) + MiB(8));
	else
		return ((val - 0x17) * MiB(4) + MiB(36));
}

static vm_paddr_t
intel_stolen_size_gen9(int bus, int slot, int func)
{
	uint32_t ctrl;
	vm_paddr_t val;

	ctrl = pci_cfgregread(bus, slot, func, SNB_GMCH_CTRL, 2);
	val = (ctrl >> BDW_GMCH_GMS_SHIFT) & BDW_GMCH_GMS_MASK;

	/* 0x0  to 0xEF: 32MB increments starting at 0MB */
	/* 0xF0 to 0xFE: 4MB increments starting at 4MB */
	if (val < 0xF0)
		return (val * MiB(32));
	return ((val - 0xF0) * MiB(4) + MiB(4));
}

struct intel_stolen_ops {
	vm_paddr_t (*base)(int bus, int slot, int func);
	vm_paddr_t (*size)(int bus, int slot, int func);
};

static const struct intel_stolen_ops intel_stolen_ops_gen3 = {
	.base = intel_stolen_base_gen3,
	.size = intel_stolen_size_gen3,
};

static const struct intel_stolen_ops intel_stolen_ops_gen6 = {
	.base = intel_stolen_base_gen3,
	.size = intel_stolen_size_gen6,
};

static const struct intel_stolen_ops intel_stolen_ops_gen8 = {
	.base = intel_stolen_base_gen3,
	.size = intel_stolen_size_gen8,
};

static const struct intel_stolen_ops intel_stolen_ops_gen9 = {
	.base = intel_stolen_base_gen3,
	.size = intel_stolen_size_gen9,
};

static const struct intel_stolen_ops intel_stolen_ops_chv = {
	.base = intel_stolen_base_gen3,
	.size = intel_stolen_size_chv,
};

static const struct pci_device_id intel_ids[] = {
	INTEL_I915G_IDS(&intel_stolen_ops_gen3),
	INTEL_I915GM_IDS(&intel_stolen_ops_gen3),
	INTEL_I945G_IDS(&intel_stolen_ops_gen3),
	INTEL_I945GM_IDS(&intel_stolen_ops_gen3),
	INTEL_VLV_IDS(&intel_stolen_ops_gen6),
	INTEL_PINEVIEW_IDS(&intel_stolen_ops_gen3),
	INTEL_I965G_IDS(&intel_stolen_ops_gen3),
	INTEL_G33_IDS(&intel_stolen_ops_gen3),
	INTEL_I965GM_IDS(&intel_stolen_ops_gen3),
	INTEL_GM45_IDS(&intel_stolen_ops_gen3),
	INTEL_G45_IDS(&intel_stolen_ops_gen3),
	INTEL_IRONLAKE_D_IDS(&intel_stolen_ops_gen3),
	INTEL_IRONLAKE_M_IDS(&intel_stolen_ops_gen3),
	INTEL_SNB_D_IDS(&intel_stolen_ops_gen6),
	INTEL_SNB_M_IDS(&intel_stolen_ops_gen6),
	INTEL_IVB_M_IDS(&intel_stolen_ops_gen6),
	INTEL_IVB_D_IDS(&intel_stolen_ops_gen6),
	INTEL_HSW_IDS(&intel_stolen_ops_gen6),
	INTEL_BDW_IDS(&intel_stolen_ops_gen8),
	INTEL_CHV_IDS(&intel_stolen_ops_chv),
	INTEL_SKL_IDS(&intel_stolen_ops_gen9),
	INTEL_BXT_IDS(&intel_stolen_ops_gen9),
	INTEL_KBL_IDS(&intel_stolen_ops_gen9),
	INTEL_CFL_IDS(&intel_stolen_ops_gen9),
	INTEL_GLK_IDS(&intel_stolen_ops_gen9),
	INTEL_CNL_IDS(&intel_stolen_ops_gen9),
};

/*
 * Buggy BIOS don't reserve memory for the GPU properly and the OS
 * can claim it before the GPU driver is loaded. This function will
 * check the registers for base and size of this memory and reserve
 * it for the GPU driver.
 * gen3 (2004) and newer devices are supported. Support for older hw
 * can be ported from Linux if needed.
 */
static void
intel_graphics_stolen(void)
{
	const struct intel_stolen_ops *ops;
	uint32_t vendor, device, class;
	int i;

	/* XXX: Scan bus instead of assuming 0:2:0? */
	const int bus = 0;
	const int slot = 2;
	const int func = 0;

	if (pci_cfgregopen() == 0)
		return;

	vendor = pci_cfgregread(bus, slot, func, PCIR_VENDOR, 2);
	if (vendor != PCI_VENDOR_INTEL)
		return;

	class = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 2);
	if (class != PCI_CLASS_VGA)
		return;

	device = pci_cfgregread(bus, slot, func, PCIR_DEVICE, 2);
	if (device == 0xFFFF)
		return;

	for (i = 0; i < nitems(intel_ids); i++) {
		if (intel_ids[i].device != device)
			continue;
		ops = intel_ids[i].data;
		intel_graphics_stolen_base = ops->base(bus, slot, func);
		intel_graphics_stolen_size = ops->size(bus, slot, func);
		break;
	}

	/* XXX: enable this once the KPI is available */
	/* phys_avail_reserve(intel_graphics_stolen_base, */
	/*     intel_graphics_stolen_base + intel_graphics_stolen_size); */
}

void
pci_early_quirks(void)
{

	intel_graphics_stolen();
}