summaryrefslogtreecommitdiff
path: root/drivers/peci/device.c
blob: e6b0bffb14f4131676ff12d489f46335204467e1 (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
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2018-2021 Intel Corporation

#include <linux/bitfield.h>
#include <linux/peci.h>
#include <linux/peci-cpu.h>
#include <linux/slab.h>

#include "internal.h"

/*
 * PECI device can be removed using sysfs, but the removal can also happen as
 * a result of controller being removed.
 * Mutex is used to protect PECI device from being double-deleted.
 */
static DEFINE_MUTEX(peci_device_del_lock);

#define REVISION_NUM_MASK GENMASK(15, 8)
static int peci_get_revision(struct peci_device *device, u8 *revision)
{
	struct peci_request *req;
	u64 dib;

	req = peci_xfer_get_dib(device);
	if (IS_ERR(req))
		return PTR_ERR(req);

	/*
	 * PECI device may be in a state where it is unable to return a proper
	 * DIB, in which case it returns 0 as DIB value.
	 * Let's treat this as an error to avoid carrying on with the detection
	 * using invalid revision.
	 */
	dib = peci_request_dib_read(req);
	if (dib == 0) {
		peci_request_free(req);
		return -EIO;
	}

	*revision = FIELD_GET(REVISION_NUM_MASK, dib);

	peci_request_free(req);

	return 0;
}

static int peci_get_cpu_id(struct peci_device *device, u32 *cpu_id)
{
	struct peci_request *req;
	int ret;

	req = peci_xfer_pkg_cfg_readl(device, PECI_PCS_PKG_ID, PECI_PKG_ID_CPU_ID);
	if (IS_ERR(req))
		return PTR_ERR(req);

	ret = peci_request_status(req);
	if (ret)
		goto out_req_free;

	*cpu_id = peci_request_data_readl(req);
out_req_free:
	peci_request_free(req);

	return ret;
}

static unsigned int peci_x86_cpu_family(unsigned int sig)
{
	unsigned int x86;

	x86 = (sig >> 8) & 0xf;

	if (x86 == 0xf)
		x86 += (sig >> 20) & 0xff;

	return x86;
}

static unsigned int peci_x86_cpu_model(unsigned int sig)
{
	unsigned int fam, model;

	fam = peci_x86_cpu_family(sig);

	model = (sig >> 4) & 0xf;

	if (fam >= 0x6)
		model += ((sig >> 16) & 0xf) << 4;

	return model;
}

static int peci_device_info_init(struct peci_device *device)
{
	u8 revision;
	u32 cpu_id;
	int ret;

	ret = peci_get_cpu_id(device, &cpu_id);
	if (ret)
		return ret;

	device->info.family = peci_x86_cpu_family(cpu_id);
	device->info.model = peci_x86_cpu_model(cpu_id);

	ret = peci_get_revision(device, &revision);
	if (ret)
		return ret;
	device->info.peci_revision = revision;

	device->info.socket_id = device->addr - PECI_BASE_ADDR;

	return 0;
}

static int peci_detect(struct peci_controller *controller, u8 addr)
{
	/*
	 * PECI Ping is a command encoded by tx_len = 0, rx_len = 0.
	 * We expect correct Write FCS if the device at the target address
	 * is able to respond.
	 */
	struct peci_request req = { 0 };
	int ret;

	mutex_lock(&controller->bus_lock);
	ret = controller->ops->xfer(controller, addr, &req);
	mutex_unlock(&controller->bus_lock);

	return ret;
}

static bool peci_addr_valid(u8 addr)
{
	return addr >= PECI_BASE_ADDR && addr < PECI_BASE_ADDR + PECI_DEVICE_NUM_MAX;
}

static int peci_dev_exists(struct device *dev, void *data)
{
	struct peci_device *device = to_peci_device(dev);
	u8 *addr = data;

	if (device->addr == *addr)
		return -EBUSY;

	return 0;
}

int peci_device_create(struct peci_controller *controller, u8 addr)
{
	struct peci_device *device;
	int ret;

	if (!peci_addr_valid(addr))
		return -EINVAL;

	/* Check if we have already detected this device before. */
	ret = device_for_each_child(&controller->dev, &addr, peci_dev_exists);
	if (ret)
		return 0;

	ret = peci_detect(controller, addr);
	if (ret) {
		/*
		 * Device not present or host state doesn't allow successful
		 * detection at this time.
		 */
		if (ret == -EIO || ret == -ETIMEDOUT)
			return 0;

		return ret;
	}

	device = kzalloc(sizeof(*device), GFP_KERNEL);
	if (!device)
		return -ENOMEM;

	device_initialize(&device->dev);

	device->addr = addr;
	device->dev.parent = &controller->dev;
	device->dev.bus = &peci_bus_type;
	device->dev.type = &peci_device_type;

	ret = peci_device_info_init(device);
	if (ret)
		goto err_put;

	ret = dev_set_name(&device->dev, "%d-%02x", controller->id, device->addr);
	if (ret)
		goto err_put;

	ret = device_add(&device->dev);
	if (ret)
		goto err_put;

	return 0;

err_put:
	put_device(&device->dev);

	return ret;
}

void peci_device_destroy(struct peci_device *device)
{
	mutex_lock(&peci_device_del_lock);
	if (!device->deleted) {
		device_unregister(&device->dev);
		device->deleted = true;
	}
	mutex_unlock(&peci_device_del_lock);
}

int __peci_driver_register(struct peci_driver *driver, struct module *owner,
			   const char *mod_name)
{
	driver->driver.bus = &peci_bus_type;
	driver->driver.owner = owner;
	driver->driver.mod_name = mod_name;

	if (!driver->probe) {
		pr_err("peci: trying to register driver without probe callback\n");
		return -EINVAL;
	}

	if (!driver->id_table) {
		pr_err("peci: trying to register driver without device id table\n");
		return -EINVAL;
	}

	return driver_register(&driver->driver);
}
EXPORT_SYMBOL_NS_GPL(__peci_driver_register, PECI);

void peci_driver_unregister(struct peci_driver *driver)
{
	driver_unregister(&driver->driver);
}
EXPORT_SYMBOL_NS_GPL(peci_driver_unregister, PECI);

static void peci_device_release(struct device *dev)
{
	struct peci_device *device = to_peci_device(dev);

	kfree(device);
}

struct device_type peci_device_type = {
	.groups		= peci_device_groups,
	.release	= peci_device_release,
};