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

#include <linux/bug.h>
#include <linux/device.h>
#include <linux/export.h>
#include <linux/idr.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/peci.h>
#include <linux/pm_runtime.h>
#include <linux/property.h>
#include <linux/slab.h>

#include "internal.h"

static DEFINE_IDA(peci_controller_ida);

static void peci_controller_dev_release(struct device *dev)
{
	struct peci_controller *controller = to_peci_controller(dev);

	mutex_destroy(&controller->bus_lock);
	ida_free(&peci_controller_ida, controller->id);
	kfree(controller);
}

struct device_type peci_controller_type = {
	.release	= peci_controller_dev_release,
};

int peci_controller_scan_devices(struct peci_controller *controller)
{
	int ret;
	u8 addr;

	for (addr = PECI_BASE_ADDR; addr < PECI_BASE_ADDR + PECI_DEVICE_NUM_MAX; addr++) {
		ret = peci_device_create(controller, addr);
		if (ret)
			return ret;
	}

	return 0;
}

static struct peci_controller *peci_controller_alloc(struct device *dev,
						     const struct peci_controller_ops *ops)
{
	struct peci_controller *controller;
	int ret;

	if (!ops->xfer)
		return ERR_PTR(-EINVAL);

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

	ret = ida_alloc_max(&peci_controller_ida, U8_MAX, GFP_KERNEL);
	if (ret < 0)
		goto err;
	controller->id = ret;

	controller->ops = ops;

	controller->dev.parent = dev;
	controller->dev.bus = &peci_bus_type;
	controller->dev.type = &peci_controller_type;

	device_initialize(&controller->dev);

	mutex_init(&controller->bus_lock);

	return controller;

err:
	kfree(controller);
	return ERR_PTR(ret);
}

static int unregister_child(struct device *dev, void *dummy)
{
	peci_device_destroy(to_peci_device(dev));

	return 0;
}

static void unregister_controller(void *_controller)
{
	struct peci_controller *controller = _controller;

	/*
	 * Detach any active PECI devices. This can't fail, thus we do not
	 * check the returned value.
	 */
	device_for_each_child_reverse(&controller->dev, NULL, unregister_child);

	device_unregister(&controller->dev);

	fwnode_handle_put(controller->dev.fwnode);

	pm_runtime_disable(&controller->dev);
}

/**
 * devm_peci_controller_add() - add PECI controller
 * @dev: device for devm operations
 * @ops: pointer to controller specific methods
 *
 * In final stage of its probe(), peci_controller driver calls
 * devm_peci_controller_add() to register itself with the PECI bus.
 *
 * Return: Pointer to the newly allocated controller or ERR_PTR() in case of failure.
 */
struct peci_controller *devm_peci_controller_add(struct device *dev,
						 const struct peci_controller_ops *ops)
{
	struct peci_controller *controller;
	int ret;

	controller = peci_controller_alloc(dev, ops);
	if (IS_ERR(controller))
		return controller;

	ret = dev_set_name(&controller->dev, "peci-%d", controller->id);
	if (ret)
		goto err_put;

	pm_runtime_no_callbacks(&controller->dev);
	pm_suspend_ignore_children(&controller->dev, true);
	pm_runtime_enable(&controller->dev);

	device_set_node(&controller->dev, fwnode_handle_get(dev_fwnode(dev)));

	ret = device_add(&controller->dev);
	if (ret)
		goto err_fwnode;

	ret = devm_add_action_or_reset(dev, unregister_controller, controller);
	if (ret)
		return ERR_PTR(ret);

	/*
	 * Ignoring retval since failures during scan are non-critical for
	 * controller itself.
	 */
	peci_controller_scan_devices(controller);

	return controller;

err_fwnode:
	fwnode_handle_put(controller->dev.fwnode);

	pm_runtime_disable(&controller->dev);

err_put:
	put_device(&controller->dev);

	return ERR_PTR(ret);
}
EXPORT_SYMBOL_NS_GPL(devm_peci_controller_add, PECI);

static const struct peci_device_id *
peci_bus_match_device_id(const struct peci_device_id *id, struct peci_device *device)
{
	while (id->family != 0) {
		if (id->family == device->info.family &&
		    id->model == device->info.model)
			return id;
		id++;
	}

	return NULL;
}

static int peci_bus_device_match(struct device *dev, struct device_driver *drv)
{
	struct peci_device *device = to_peci_device(dev);
	struct peci_driver *peci_drv = to_peci_driver(drv);

	if (dev->type != &peci_device_type)
		return 0;

	return !!peci_bus_match_device_id(peci_drv->id_table, device);
}

static int peci_bus_device_probe(struct device *dev)
{
	struct peci_device *device = to_peci_device(dev);
	struct peci_driver *driver = to_peci_driver(dev->driver);

	return driver->probe(device, peci_bus_match_device_id(driver->id_table, device));
}

static void peci_bus_device_remove(struct device *dev)
{
	struct peci_device *device = to_peci_device(dev);
	struct peci_driver *driver = to_peci_driver(dev->driver);

	if (driver->remove)
		driver->remove(device);
}

struct bus_type peci_bus_type = {
	.name		= "peci",
	.match		= peci_bus_device_match,
	.probe		= peci_bus_device_probe,
	.remove		= peci_bus_device_remove,
	.bus_groups	= peci_bus_groups,
};

static int __init peci_init(void)
{
	int ret;

	ret = bus_register(&peci_bus_type);
	if (ret < 0) {
		pr_err("peci: failed to register PECI bus type!\n");
		return ret;
	}

	return 0;
}
module_init(peci_init);

static void __exit peci_exit(void)
{
	bus_unregister(&peci_bus_type);
}
module_exit(peci_exit);

MODULE_AUTHOR("Jason M Bills <jason.m.bills@linux.intel.com>");
MODULE_AUTHOR("Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>");
MODULE_AUTHOR("Iwona Winiarska <iwona.winiarska@intel.com>");
MODULE_DESCRIPTION("PECI bus core module");
MODULE_LICENSE("GPL");