-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuncore_imc.c
281 lines (243 loc) · 6.42 KB
/
uncore_imc.c
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
/*
* Copyright (C) 2015-2016 Yizhou Shan <shanyizhou@ict.ac.cn>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* This file describes methods to manipulate Integrated Memory Controller (IMC)
*/
#define pr_fmt(fmt) "UNCORE IMC: " fmt
#include "uncore_pmu.h"
#include <asm/setup.h>
#include <linux/bug.h>
#include <linux/pci.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/kernel.h>
const struct pci_device_id *uncore_imc_device_ids;
const struct uncore_imc_ops *uncore_imc_ops;
LIST_HEAD(uncore_imc_devices);
void uncore_imc_exit(void)
{
struct list_head *head;
struct uncore_imc *imc;
head = &uncore_imc_devices;
while (!list_empty(head)) {
imc = list_first_entry(head, struct uncore_imc, next);
list_del(&imc->next);
/* Since we have get_device manually */
pci_dev_put(imc->pdev);
kfree(imc);
}
}
/**
* uncore_imc_new_device
* @pdev: the pci device instance
* Return: Non-zero on failure
*
* Add a new IMC struct to the list.
*/
static int __must_check uncore_imc_new_device(struct pci_dev *pdev)
{
struct uncore_imc *imc;
int nodeid;
if (!pdev)
return -EINVAL;
imc = kzalloc(sizeof(struct uncore_imc), GFP_KERNEL);
if (!imc)
return -ENOMEM;
nodeid = uncore_pcibus_to_nodeid[pdev->bus->number];
WARN_ONCE((nodeid < 0) || (nodeid > UNCORE_MAX_SOCKET),
"Invalid Node ID: %d, check pci-node mapping", nodeid);
imc->nodeid = nodeid;
imc->pdev = pdev;
imc->ops = uncore_imc_ops;
list_add_tail(&imc->next, &uncore_imc_devices);
return 0;
}
int __must_check uncore_imc_init(void)
{
const struct pci_device_id *ids;
struct pci_dev *pdev;
int ret;
ret = -ENXIO;
switch (boot_cpu_data.x86_model) {
case 45: /* Sandy Bridge-EP*/
break;
case 62: /* Ivy Bridge-EP */
break;
case 63: /* Haswell-EP */
ret = hswep_imc_init();
break;
default:
pr_err("Buy an E5-v3");
};
if (ret)
return ret;
/* IMC part need all low-level CPU-specific methods. */
if (!uncore_imc_ops ||
!uncore_imc_ops->set_threshold ||
!uncore_imc_ops->enable_throttle ||
!uncore_imc_ops->disable_throttle)
return -EINVAL;
/* Now initialize all IMCs on all sockets */
ids = uncore_imc_device_ids;
for (; ids->vendor; ids++) {
pdev = NULL;
while (1) {
pdev = pci_get_device(ids->vendor, ids->device, pdev);
if (!pdev)
break;
/* See uncore_pmu.c for why */
get_device(&pdev->dev);
ret = uncore_imc_new_device(pdev);
if (ret)
goto out;
}
}
return 0;
out:
uncore_imc_exit();
return ret;
}
/**
* uncore_imc_set_threshold
* @nodeid: NUMA node to set threshold
* @threshold: 1/(threshold) to throttle memory bandwidth
* Return: 0 on success
*
* Let us say the original bandwidth is BW, then:
* If @threshold = 1, the bandwidth after throttling is: BW
* If @threshold = 2, the bandwidth after throttling is: BW/2
*
* The biggest @threshold depends on specific CPU.
*/
int uncore_imc_set_threshold(unsigned int nodeid, unsigned int threshold)
{
struct uncore_imc *imc;
int ret = -ENXIO;
if (nodeid > UNCORE_MAX_SOCKET)
return -EINVAL;
list_for_each_entry(imc, &uncore_imc_devices, next) {
if (imc->nodeid == nodeid) {
ret = imc->ops->set_threshold(imc->pdev, threshold);
if (ret)
break;
}
}
return ret;
}
/**
* uncore_imc_disable_throttle
* @nodeid: NUMA node to disable throttling
*
* This method will disable memory bandwidth throttling in node @nodeid.
* It depends on CPU-specific method to disable each IMC device.
*/
void uncore_imc_disable_throttle(unsigned int nodeid)
{
struct uncore_imc *imc;
if (nodeid > UNCORE_MAX_SOCKET)
return;
list_for_each_entry(imc, &uncore_imc_devices, next) {
if (imc->nodeid == nodeid)
imc->ops->disable_throttle(imc->pdev);
}
}
/**
* uncore_imc_enable_throttle
* @nodeid: NUMA node to enable throttling
* Return: 0 on success
*
* This method will enable memory bandwidth throttling in node @nodeid.
* It depends on CPU-specific method to enable each IMC device. You
* should set threshold before enable throttling.
*/
int uncore_imc_enable_throttle(unsigned int nodeid)
{
struct uncore_imc *imc;
int ret = -ENXIO;
if (nodeid > UNCORE_MAX_SOCKET)
return -EINVAL;
list_for_each_entry(imc, &uncore_imc_devices, next) {
if (imc->nodeid == nodeid) {
ret = imc->ops->enable_throttle(imc->pdev);
if (ret) {
uncore_imc_disable_throttle(nodeid);
break;
}
}
}
return ret;
}
/**
* uncore_imc_set_threshold_all
* @threshold: 1/(threshold) to throttle memory bandwidth
* Return: 0 on success
*
* This method will set memory bandwidth for all online nodes.
*/
int uncore_imc_set_threshold_all(unsigned int threshold)
{
int node, ret = -ENXIO;
for_each_online_node(node) {
ret = uncore_imc_set_threshold(node, threshold);
if (ret)
break;
}
return ret;
}
/**
* uncore_imc_enable_throttle_all
* Return: 0 on success
*
* This method enables memory bandwidth throttling on all online nodes.
* It walks through all online nodes to enable throttling.
*/
int uncore_imc_enable_throttle_all(void)
{
int node, ret = -ENXIO;
for_each_online_node(node) {
ret = uncore_imc_enable_throttle(node);
if (ret) {
uncore_imc_disable_throttle_all();
break;
}
}
return ret;
}
void uncore_imc_disable_throttle_all(void)
{
int node;
for_each_online_node(node)
uncore_imc_disable_throttle(node);
}
void uncore_print_imc_devices(void)
{
struct uncore_imc *imc;
pr_info("\033[34m------------------------ IMC Devices ----------------------\033[0m");
list_for_each_entry(imc, &uncore_imc_devices, next) {
pr_info("......Node %d, %x:%x:%x, %d:%d:%d, Kref = %d",
imc->nodeid,
imc->pdev->bus->number,
imc->pdev->vendor,
imc->pdev->device,
imc->pdev->bus->number,
(imc->pdev->devfn >> 3) & 0x1f,
(imc->pdev->devfn) & 0x7,
imc->pdev->dev.kobj.kref.refcount.counter);
}
}