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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Advanced Micro Devices, Inc.
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2022 Oxide Computer Company
*/
/*
* PCI Mechanism 1 low-level routines with ECS support for AMD family >= 0x10
*/
#include <sys/controlregs.h>
#include <sys/cpuvar.h>
#include <sys/types.h>
#include <sys/pci.h>
#include <sys/pci_impl.h>
#include <sys/sunddi.h>
#include <sys/pci_cfgspace_impl.h>
#include <sys/x86_archext.h>
boolean_t
pci_check_amd_ioecs(void)
{
struct cpuid_regs cp;
int family;
ASSERT(is_x86_feature(x86_featureset, X86FSET_CPUID));
/*
* Get the CPU vendor string from CPUID.
* This PCI mechanism only applies to AMD CPUs.
*/
cp.cp_eax = 0;
(void) __cpuid_insn(&cp);
if ((cp.cp_ebx != 0x68747541) || /* Auth */
(cp.cp_edx != 0x69746e65) || /* enti */
(cp.cp_ecx != 0x444d4163)) /* cAMD */
return (B_FALSE);
/*
* Get the CPU family from CPUID.
* This PCI mechanism is only available on family 0x10 or higher.
*/
cp.cp_eax = 1;
(void) __cpuid_insn(&cp);
family = ((cp.cp_eax >> 8) & 0xf) + ((cp.cp_eax >> 20) & 0xff);
if (family < 0x10)
return (B_FALSE);
/*
* Set the EnableCf8ExtCfg bit in the Northbridge Configuration Register
* to enable accessing PCI ECS using in/out instructions.
*/
wrmsr(MSR_AMD_NB_CFG, rdmsr(MSR_AMD_NB_CFG) | AMD_GH_NB_CFG_EN_ECS);
return (B_TRUE);
}
/*
* Macro to setup PCI Extended Configuration Space (ECS) address to give to
* "in/out" instructions
*/
#define PCI_CADDR1_ECS(b, d, f, r) \
(PCI_CADDR1((b), (d), (f), (r)) | ((((r) >> 8) & 0xf) << 24))
/*
* Per PCI 2.1 section 3.7.4.1 and PCI-PCI Bridge Architecture 1.0 section
* 5.3.1.2: dev=31 func=7 reg=0 means a special cycle. We don't want to
* trigger that by accident, so we pretend that dev 31, func 7 doesn't
* exist. If we ever want special cycle support, we'll add explicit
* special cycle support.
*/
uint8_t
pci_mech1_amd_getb(int bus, int device, int function, int reg)
{
uint8_t val;
if (device == PCI_MECH1_SPEC_CYCLE_DEV &&
function == PCI_MECH1_SPEC_CYCLE_FUNC) {
return (PCI_EINVAL8);
}
if (reg > pci_iocfg_max_offset) {
return (PCI_EINVAL8);
}
mutex_enter(&pcicfg_mutex);
outl(PCI_CONFADD, PCI_CADDR1_ECS(bus, device, function, reg));
val = inb(PCI_CONFDATA | (reg & 0x3));
mutex_exit(&pcicfg_mutex);
return (val);
}
uint16_t
pci_mech1_amd_getw(int bus, int device, int function, int reg)
{
uint16_t val;
if (device == PCI_MECH1_SPEC_CYCLE_DEV &&
function == PCI_MECH1_SPEC_CYCLE_FUNC) {
return (PCI_EINVAL16);
}
if (reg > pci_iocfg_max_offset) {
return (PCI_EINVAL16);
}
mutex_enter(&pcicfg_mutex);
outl(PCI_CONFADD, PCI_CADDR1_ECS(bus, device, function, reg));
val = inw(PCI_CONFDATA | (reg & 0x2));
mutex_exit(&pcicfg_mutex);
return (val);
}
uint32_t
pci_mech1_amd_getl(int bus, int device, int function, int reg)
{
uint32_t val;
if (device == PCI_MECH1_SPEC_CYCLE_DEV &&
function == PCI_MECH1_SPEC_CYCLE_FUNC) {
return (PCI_EINVAL32);
}
if (reg > pci_iocfg_max_offset) {
return (PCI_EINVAL32);
}
mutex_enter(&pcicfg_mutex);
outl(PCI_CONFADD, PCI_CADDR1_ECS(bus, device, function, reg));
val = inl(PCI_CONFDATA);
mutex_exit(&pcicfg_mutex);
return (val);
}
void
pci_mech1_amd_putb(int bus, int device, int function, int reg, uint8_t val)
{
if (device == PCI_MECH1_SPEC_CYCLE_DEV &&
function == PCI_MECH1_SPEC_CYCLE_FUNC) {
return;
}
mutex_enter(&pcicfg_mutex);
outl(PCI_CONFADD, PCI_CADDR1_ECS(bus, device, function, reg));
outb(PCI_CONFDATA | (reg & 0x3), val);
mutex_exit(&pcicfg_mutex);
}
void
pci_mech1_amd_putw(int bus, int device, int function, int reg, uint16_t val)
{
if (device == PCI_MECH1_SPEC_CYCLE_DEV &&
function == PCI_MECH1_SPEC_CYCLE_FUNC) {
return;
}
mutex_enter(&pcicfg_mutex);
outl(PCI_CONFADD, PCI_CADDR1_ECS(bus, device, function, reg));
outw(PCI_CONFDATA | (reg & 0x2), val);
mutex_exit(&pcicfg_mutex);
}
void
pci_mech1_amd_putl(int bus, int device, int function, int reg, uint32_t val)
{
if (device == PCI_MECH1_SPEC_CYCLE_DEV &&
function == PCI_MECH1_SPEC_CYCLE_FUNC) {
return;
}
mutex_enter(&pcicfg_mutex);
outl(PCI_CONFADD, PCI_CADDR1_ECS(bus, device, function, reg));
outl(PCI_CONFDATA, val);
mutex_exit(&pcicfg_mutex);
}
|