summaryrefslogtreecommitdiff
path: root/src/device/device_const.c
blob: a63a629344422a8132b9af5696f24be609b8e983 (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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* SPDX-License-Identifier: GPL-2.0-only */

#include <assert.h>
#include <console/console.h>
#include <device/device.h>
#include <device/path.h>
#include <device/pci_def.h>
#include <device/pci_type.h>
#include <fw_config.h>
#include <types.h>

/** Linked list of ALL devices */
DEVTREE_CONST struct device *DEVTREE_CONST all_devices = &dev_root;

/**
 * Given a PCI bus and a devfn number, find the device structure.
 *
 * Note that this function can return the incorrect device prior
 * to PCI enumeration because the secondary field of the bus object
 * is 0. The failing scenario is determined by the order of the
 * devices in all_devices singly-linked list as well as the time
 * when this function is called (secondary reflecting topology).
 *
 * @param bus The bus number.
 * @param devfn A device/function number.
 * @return Pointer to the device structure (if found), 0 otherwise.
 */

static DEVTREE_CONST struct device *dev_find_slot(unsigned int bus,
						unsigned int devfn)
{
	DEVTREE_CONST struct device *dev, *result;

	result = 0;
	for (dev = all_devices; dev; dev = dev->next) {
		if ((dev->path.type == DEVICE_PATH_PCI) &&
		    (dev->bus->secondary == bus) &&
		    (dev->path.pci.devfn == devfn)) {
			result = dev;
			break;
		}
	}
	return result;
}

/**
 * Given a Device Path Type, find the device structure.
 *
 * @param prev_match The previously matched device instance.
 * @param path_type The Device Path Type.
 * @return Pointer to the device structure (if found), 0 otherwise.
 */
DEVTREE_CONST struct device *dev_find_path(
		DEVTREE_CONST struct device *prev_match,
		enum device_path_type path_type)
{
	DEVTREE_CONST struct device *dev, *result = NULL;

	if (prev_match == NULL)
		prev_match = all_devices;
	else
		prev_match = prev_match->next;

	for (dev = prev_match; dev; dev = dev->next) {
		if (dev->path.type == path_type) {
			result = dev;
			break;
		}
	}
	return result;
}

/**
 * Given a device pointer, find the next PCI device.
 *
 * @param previous_dev A pointer to a PCI device structure.
 * @return Pointer to the next device structure (if found), 0 otherwise.
 */
DEVTREE_CONST struct device *dev_find_next_pci_device(
		DEVTREE_CONST struct device *previous_dev)
{
	return dev_find_path(previous_dev, DEVICE_PATH_PCI);
}

static int path_eq(const struct device_path *path1,
		const struct device_path *path2)
{
	int equal = 0;

	if (!path1 || !path2) {
		assert(path1);
		assert(path2);
		/* Return 0 in case assert is considered non-fatal. */
		return 0;
	}

	if (path1->type != path2->type)
		return 0;

	switch (path1->type) {
	case DEVICE_PATH_NONE:
		break;
	case DEVICE_PATH_ROOT:
		equal = 1;
		break;
	case DEVICE_PATH_PCI:
		equal = (path1->pci.devfn == path2->pci.devfn);
		break;
	case DEVICE_PATH_PNP:
		equal = (path1->pnp.port == path2->pnp.port) &&
			(path1->pnp.device == path2->pnp.device);
		break;
	case DEVICE_PATH_I2C:
		equal = (path1->i2c.device == path2->i2c.device) &&
			(path1->i2c.mode_10bit == path2->i2c.mode_10bit);
		break;
	case DEVICE_PATH_APIC:
		equal = (path1->apic.apic_id == path2->apic.apic_id);
		break;
	case DEVICE_PATH_DOMAIN:
		equal = (path1->domain.domain == path2->domain.domain);
		break;
	case DEVICE_PATH_CPU_CLUSTER:
		equal = (path1->cpu_cluster.cluster
			 == path2->cpu_cluster.cluster);
		break;
	case DEVICE_PATH_CPU:
		equal = (path1->cpu.id == path2->cpu.id);
		break;
	case DEVICE_PATH_CPU_BUS:
		equal = (path1->cpu_bus.id == path2->cpu_bus.id);
		break;
	case DEVICE_PATH_GENERIC:
		equal = (path1->generic.id == path2->generic.id) &&
			(path1->generic.subid == path2->generic.subid);
		break;
	case DEVICE_PATH_SPI:
		equal = (path1->spi.cs == path2->spi.cs);
		break;
	case DEVICE_PATH_USB:
		equal = (path1->usb.port_type == path2->usb.port_type) &&
			(path1->usb.port_id == path2->usb.port_id);
		break;
	case DEVICE_PATH_MMIO:
		equal = (path1->mmio.addr == path2->mmio.addr);
		break;
	case DEVICE_PATH_GPIO:
		equal = (path1->gpio.id == path2->gpio.id);
		break;
	case DEVICE_PATH_MDIO:
		equal = (path1->mdio.addr == path2->mdio.addr);
		break;

	default:
		printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
		break;
	}

	return equal;
}

/**
 * See if a device structure exists for path.
 *
 * @param parent The bus to find the device on.
 * @param path The relative path from the bus to the appropriate device.
 * @return Pointer to a device structure for the device on bus at path
 *         or 0/NULL if no device is found.
 */
DEVTREE_CONST struct device *find_dev_path(
	const struct bus *parent, const struct device_path *path)
{
	DEVTREE_CONST struct device *child;

	if (!parent) {
		BUG();
		/* Return NULL in case asserts are considered non-fatal. */
		return NULL;
	}

	for (child = parent->children; child; child = child->sibling) {
		if (path_eq(path, &child->path))
			break;
	}
	return child;
}

/**
 * Find the device structure given an array of nested device paths,
 *
 * @param parent The parent bus to start the search on.
 * @param nested_path An array of relative paths from the parent bus to the target device.
 * @param nested_path_length Number of path elements in nested_path array.
 * @return Pointer to a device structure for the device at nested path
 *         or 0/NULL if no device is found.
 */
DEVTREE_CONST struct device *find_dev_nested_path(
	const struct bus *parent, const struct device_path nested_path[],
	size_t nested_path_length)
{
	DEVTREE_CONST struct device *child;

	if (!parent || !nested_path || !nested_path_length)
		return NULL;

	child = find_dev_path(parent, nested_path);

	/* Terminate recursion at end of nested path or child not found */
	if (nested_path_length == 1 || !child)
		return child;

	return find_dev_nested_path(child->link_list, nested_path + 1, nested_path_length - 1);
}

DEVTREE_CONST struct device *pcidev_path_behind(
	const struct bus *parent, pci_devfn_t devfn)
{
	const struct device_path path = {
		.type = DEVICE_PATH_PCI,
		.pci.devfn = devfn,
	};
	return find_dev_path(parent, &path);
}

DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn)
{
	DEVTREE_CONST struct bus *parent = pci_root_bus();
	DEVTREE_CONST struct device *dev = parent->children;

	/* FIXME: Write the loop with topology links. */
	while (dev) {
		if (dev->path.type != DEVICE_PATH_PCI) {
			dev = dev->next;
			continue;
		}
		if (dev->bus->secondary == bus)
			return pcidev_path_behind(dev->bus, devfn);
		dev = dev->next;
	}
	return NULL;
}

DEVTREE_CONST struct bus *pci_root_bus(void)
{
	DEVTREE_CONST struct device *pci_domain;
	static DEVTREE_CONST struct bus *pci_root;

	if (pci_root)
		return pci_root;

	pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN);
	if (!pci_domain)
		return NULL;

	pci_root = pci_domain->link_list;
	return pci_root;
}

DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
{
	return pcidev_path_behind(pci_root_bus(), devfn);
}

DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
{
	return pcidev_path_on_root(PCI_DEVFN(dev, fn));
}

DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge(
							const struct device *bridge,
							pci_devfn_t devfn)
{
	if (!bridge || (bridge->path.type != DEVICE_PATH_PCI)) {
		BUG();
		/* Return NULL in case asserts are non-fatal. */
		return NULL;
	}

	return pcidev_path_behind(bridge->link_list, devfn);
}

DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
{
	DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
	if (dev)
		return dev;

	devtree_bug(func, devfn);

	/* FIXME: This can return wrong device. */
	return dev_find_slot(0, devfn);
}

void devtree_bug(const char *func, pci_devfn_t devfn)
{
	printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
}

void __noreturn devtree_die(void)
{
	die("DEVTREE: dev or chip_info is NULL\n");
}

/**
 * Given an SMBus bus and a device number, find the device structure.
 *
 * @param bus The bus number.
 * @param addr A device number.
 * @return Pointer to the device structure (if found), 0 otherwise.
 */
DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
							unsigned int addr)
{
	DEVTREE_CONST struct device *dev, *result;

	result = 0;
	for (dev = all_devices; dev; dev = dev->next) {
		if ((dev->path.type == DEVICE_PATH_I2C) &&
		    (dev->bus->secondary == bus) &&
		    (dev->path.i2c.device == addr)) {
			result = dev;
			break;
		}
	}
	return result;
}

/**
 * Given a PnP port and a device number, find the device structure.
 *
 * @param port The I/O port.
 * @param device Logical device number.
 * @return Pointer to the device structure (if found), 0 otherwise.
 */
DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
{
	DEVTREE_CONST struct device *dev;

	for (dev = all_devices; dev; dev = dev->next) {
		if ((dev->path.type == DEVICE_PATH_PNP) &&
		    (dev->path.pnp.port == port) &&
		    (dev->path.pnp.device == device)) {
			return dev;
		}
	}
	return 0;
}

/**
 * Given a device and previous match iterate through all the children.
 *
 * @param bus parent device's bus holding all the children
 * @param prev_child previous child already traversed, if NULL start at
 *        children of parent bus.
 * @return pointer to child or NULL when no more children
 */
DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
					DEVTREE_CONST struct device *prev_child)
{
	DEVTREE_CONST struct device *dev;

	if (parent == NULL)
		return NULL;

	if (prev_child == NULL)
		dev = parent->children;
	else
		dev = prev_child->sibling;

	return dev;
}

bool is_dev_enabled(const struct device *dev)
{
	if (!dev)
		return false;

	/* For stages with immutable device tree, first check if device is disabled because of
	   fw_config probing. In these stages, dev->enabled does not reflect the true state of a
	   device that uses fw_config probing. */
	if (DEVTREE_EARLY && !fw_config_probe_dev(dev, NULL))
		return false;
	return dev->enabled;
}

bool is_devfn_enabled(unsigned int devfn)
{
	const struct device *dev = pcidev_path_on_root(devfn);
	return is_dev_enabled(dev);
}