pci.c 163 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2
/*
Bjorn Helgaas's avatar
Bjorn Helgaas committed
3
 * PCI Bus Services, see include/linux/pci.h for further explanation.
Linus Torvalds's avatar
Linus Torvalds committed
4
 *
Bjorn Helgaas's avatar
Bjorn Helgaas committed
5
6
 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
 * David Mosberger-Tang
Linus Torvalds's avatar
Linus Torvalds committed
7
 *
Bjorn Helgaas's avatar
Bjorn Helgaas committed
8
 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
Linus Torvalds's avatar
Linus Torvalds committed
9
10
 */

11
#include <linux/acpi.h>
Linus Torvalds's avatar
Linus Torvalds committed
12
13
#include <linux/kernel.h>
#include <linux/delay.h>
14
#include <linux/dmi.h>
Linus Torvalds's avatar
Linus Torvalds committed
15
#include <linux/init.h>
16
17
#include <linux/of.h>
#include <linux/of_pci.h>
Linus Torvalds's avatar
Linus Torvalds committed
18
#include <linux/pci.h>
19
#include <linux/pm.h>
20
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
21
22
#include <linux/module.h>
#include <linux/spinlock.h>
Tim Schmielau's avatar
Tim Schmielau committed
23
#include <linux/string.h>
24
#include <linux/log2.h>
25
#include <linux/logic_pio.h>
26
#include <linux/pm_wakeup.h>
27
#include <linux/interrupt.h>
28
#include <linux/device.h>
29
#include <linux/pm_runtime.h>
30
#include <linux/pci_hotplug.h>
31
#include <linux/vmalloc.h>
32
#include <linux/pci-ats.h>
33
#include <asm/setup.h>
34
#include <asm/dma.h>
35
#include <linux/aer.h>
36
#include "pci.h"
Linus Torvalds's avatar
Linus Torvalds committed
37

38
39
DEFINE_MUTEX(pci_slot_mutex);

Alan Stern's avatar
Alan Stern committed
40
41
42
43
44
const char *pci_power_names[] = {
	"error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
};
EXPORT_SYMBOL_GPL(pci_power_names);

45
46
47
48
49
50
int isa_dma_bridge_buggy;
EXPORT_SYMBOL(isa_dma_bridge_buggy);

int pci_pci_problems;
EXPORT_SYMBOL(pci_pci_problems);

51
52
unsigned int pci_pm_d3_delay;

53
54
55
56
57
58
59
60
61
62
63
64
65
static void pci_pme_list_scan(struct work_struct *work);

static LIST_HEAD(pci_pme_list);
static DEFINE_MUTEX(pci_pme_list_mutex);
static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);

struct pci_pme_device {
	struct list_head list;
	struct pci_dev *dev;
};

#define PME_TIMEOUT 1000 /* How long between PME checks */

66
67
68
69
70
71
72
static void pci_dev_d3_sleep(struct pci_dev *dev)
{
	unsigned int delay = dev->d3_delay;

	if (delay < pci_pm_d3_delay)
		delay = pci_pm_d3_delay;

73
74
	if (delay)
		msleep(delay);
75
}
Linus Torvalds's avatar
Linus Torvalds committed
76

77
78
79
80
#ifdef CONFIG_PCI_DOMAINS
int pci_domains_supported = 1;
#endif

81
82
83
84
85
86
#define DEFAULT_CARDBUS_IO_SIZE		(256)
#define DEFAULT_CARDBUS_MEM_SIZE	(64*1024*1024)
/* pci=cbmemsize=nnM,cbiosize=nn can override this */
unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;

87
88
89
90
91
92
#define DEFAULT_HOTPLUG_IO_SIZE		(256)
#define DEFAULT_HOTPLUG_MEM_SIZE	(2*1024*1024)
/* pci=hpmemsize=nnM,hpiosize=nn can override this */
unsigned long pci_hotplug_io_size  = DEFAULT_HOTPLUG_IO_SIZE;
unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;

93
94
95
#define DEFAULT_HOTPLUG_BUS_SIZE	1
unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;

96
enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
97

98
99
100
101
102
103
/*
 * The default CLS is used if arch didn't set CLS explicitly and not
 * all pci devices agree on the same value.  Arch can override either
 * the dfl or actual value as it sees fit.  Don't forget this is
 * measured in 32-bit words, not bytes.
 */
Bill Pemberton's avatar
Bill Pemberton committed
104
u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
105
106
u8 pci_cache_line_size;

107
108
109
110
111
112
/*
 * If we set up a device for bus mastering, we need to check the latency
 * timer as certain BIOSes forget to set it properly.
 */
unsigned int pcibios_max_latency = 255;

113
114
115
/* If set, the PCIe ARI capability will not be used. */
static bool pcie_ari_disabled;

116
117
118
/* If set, the PCIe ATS capability will not be used. */
static bool pcie_ats_disabled;

119
120
121
/* If set, the PCI config space of each device is printed during boot. */
bool pci_early_dump;

122
123
124
125
126
bool pci_ats_disabled(void)
{
	return pcie_ats_disabled;
}

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Disable bridge_d3 for all PCIe ports */
static bool pci_bridge_d3_disable;
/* Force bridge_d3 for all PCIe ports */
static bool pci_bridge_d3_force;

static int __init pcie_port_pm_setup(char *str)
{
	if (!strcmp(str, "off"))
		pci_bridge_d3_disable = true;
	else if (!strcmp(str, "force"))
		pci_bridge_d3_force = true;
	return 1;
}
__setup("pcie_port_pm=", pcie_port_pm_setup);

142
143
144
/* Time to wait after a reset for device to become responsive */
#define PCIE_RESET_READY_POLL_MS 60000

Linus Torvalds's avatar
Linus Torvalds committed
145
146
147
148
149
150
151
/**
 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
 * @bus: pointer to PCI bus structure to search
 *
 * Given a PCI bus, returns the highest PCI bus number present in the set
 * including the given PCI bus and its list of child PCI buses.
 */
152
unsigned char pci_bus_max_busnr(struct pci_bus *bus)
Linus Torvalds's avatar
Linus Torvalds committed
153
{
154
	struct pci_bus *tmp;
Linus Torvalds's avatar
Linus Torvalds committed
155
156
	unsigned char max, n;

157
	max = bus->busn_res.end;
158
159
	list_for_each_entry(tmp, &bus->children, node) {
		n = pci_bus_max_busnr(tmp);
Ryan Desfosses's avatar
Ryan Desfosses committed
160
		if (n > max)
Linus Torvalds's avatar
Linus Torvalds committed
161
162
163
164
			max = n;
	}
	return max;
}
165
EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
Linus Torvalds's avatar
Linus Torvalds committed
166

167
168
169
#ifdef CONFIG_HAS_IOMEM
void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
{
170
171
	struct resource *res = &pdev->resource[bar];

172
173
174
	/*
	 * Make sure the BAR is actually a memory resource, not an IO resource
	 */
175
	if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
176
		pci_warn(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
177
178
		return NULL;
	}
179
	return ioremap_nocache(res->start, resource_size(res));
180
181
}
EXPORT_SYMBOL_GPL(pci_ioremap_bar);
182
183
184
185
186
187
188
189
190
191
192
193
194
195

void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar)
{
	/*
	 * Make sure the BAR is actually a memory resource, not an IO resource
	 */
	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
		WARN_ON(1);
		return NULL;
	}
	return ioremap_wc(pci_resource_start(pdev, bar),
			  pci_resource_len(pdev, bar));
}
EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar);
196
197
#endif

198
199
200
/**
 * pci_dev_str_match_path - test if a path string matches a device
 * @dev:    the PCI device to test
201
 * @path:   string to match the device against
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
 * @endptr: pointer to the string after the match
 *
 * Test if a string (typically from a kernel parameter) formatted as a
 * path of device/function addresses matches a PCI device. The string must
 * be of the form:
 *
 *   [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
 *
 * A path for a device can be obtained using 'lspci -t'.  Using a path
 * is more robust against bus renumbering than using only a single bus,
 * device and function address.
 *
 * Returns 1 if the string matches the device, 0 if it does not and
 * a negative error code if it fails to parse the string.
 */
static int pci_dev_str_match_path(struct pci_dev *dev, const char *path,
				  const char **endptr)
{
	int ret;
	int seg, bus, slot, func;
	char *wpath, *p;
	char end;

	*endptr = strchrnul(path, ';');

	wpath = kmemdup_nul(path, *endptr - path, GFP_KERNEL);
	if (!wpath)
		return -ENOMEM;

	while (1) {
		p = strrchr(wpath, '/');
		if (!p)
			break;
		ret = sscanf(p, "/%x.%x%c", &slot, &func, &end);
		if (ret != 2) {
			ret = -EINVAL;
			goto free_and_exit;
		}

		if (dev->devfn != PCI_DEVFN(slot, func)) {
			ret = 0;
			goto free_and_exit;
		}

		/*
		 * Note: we don't need to get a reference to the upstream
		 * bridge because we hold a reference to the top level
		 * device which should hold a reference to the bridge,
		 * and so on.
		 */
		dev = pci_upstream_bridge(dev);
		if (!dev) {
			ret = 0;
			goto free_and_exit;
		}

		*p = 0;
	}

	ret = sscanf(wpath, "%x:%x:%x.%x%c", &seg, &bus, &slot,
		     &func, &end);
	if (ret != 4) {
		seg = 0;
		ret = sscanf(wpath, "%x:%x.%x%c", &bus, &slot, &func, &end);
		if (ret != 3) {
			ret = -EINVAL;
			goto free_and_exit;
		}
	}

	ret = (seg == pci_domain_nr(dev->bus) &&
	       bus == dev->bus->number &&
	       dev->devfn == PCI_DEVFN(slot, func));

free_and_exit:
	kfree(wpath);
	return ret;
}

281
282
283
284
285
286
287
288
289
/**
 * pci_dev_str_match - test if a string matches a device
 * @dev:    the PCI device to test
 * @p:      string to match the device against
 * @endptr: pointer to the string after the match
 *
 * Test if a string (typically from a kernel parameter) matches a specified
 * PCI device. The string may be of one of the following formats:
 *
290
 *   [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
291
292
293
294
295
 *   pci:<vendor>:<device>[:<subvendor>:<subdevice>]
 *
 * The first format specifies a PCI bus/device/function address which
 * may change if new hardware is inserted, if motherboard firmware changes,
 * or due to changes caused in kernel parameters. If the domain is
296
297
298
299
 * left unspecified, it is taken to be 0.  In order to be robust against
 * bus renumbering issues, a path of PCI device/function numbers may be used
 * to address the specific device.  The path for a device can be determined
 * through the use of 'lspci -t'.
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
 *
 * The second format matches devices using IDs in the configuration
 * space which may match multiple devices in the system. A value of 0
 * for any field will match all devices. (Note: this differs from
 * in-kernel code that uses PCI_ANY_ID which is ~0; this is for
 * legacy reasons and convenience so users don't have to specify
 * FFFFFFFFs on the command line.)
 *
 * Returns 1 if the string matches the device, 0 if it does not and
 * a negative error code if the string cannot be parsed.
 */
static int pci_dev_str_match(struct pci_dev *dev, const char *p,
			     const char **endptr)
{
	int ret;
315
	int count;
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
	unsigned short vendor, device, subsystem_vendor, subsystem_device;

	if (strncmp(p, "pci:", 4) == 0) {
		/* PCI vendor/device (subvendor/subdevice) IDs are specified */
		p += 4;
		ret = sscanf(p, "%hx:%hx:%hx:%hx%n", &vendor, &device,
			     &subsystem_vendor, &subsystem_device, &count);
		if (ret != 4) {
			ret = sscanf(p, "%hx:%hx%n", &vendor, &device, &count);
			if (ret != 2)
				return -EINVAL;

			subsystem_vendor = 0;
			subsystem_device = 0;
		}

		p += count;

		if ((!vendor || vendor == dev->vendor) &&
		    (!device || device == dev->device) &&
		    (!subsystem_vendor ||
			    subsystem_vendor == dev->subsystem_vendor) &&
		    (!subsystem_device ||
			    subsystem_device == dev->subsystem_device))
			goto found;
	} else {
342
343
344
345
346
347
348
349
		/*
		 * PCI Bus, Device, Function IDs are specified
		 *  (optionally, may include a path of devfns following it)
		 */
		ret = pci_dev_str_match_path(dev, p, &p);
		if (ret < 0)
			return ret;
		else if (ret)
350
351
352
353
354
355
356
357
358
359
			goto found;
	}

	*endptr = p;
	return 0;

found:
	*endptr = p;
	return 1;
}
360
361
362

static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
				   u8 pos, int cap, int *ttl)
363
364
{
	u8 id;
365
366
367
	u16 ent;

	pci_bus_read_config_byte(bus, devfn, pos, &pos);
368

369
	while ((*ttl)--) {
370
371
372
		if (pos < 0x40)
			break;
		pos &= ~3;
373
374
375
		pci_bus_read_config_word(bus, devfn, pos, &ent);

		id = ent & 0xff;
376
377
378
379
		if (id == 0xff)
			break;
		if (id == cap)
			return pos;
380
		pos = (ent >> 8);
381
382
383
384
	}
	return 0;
}

385
386
387
388
389
390
391
392
static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
			       u8 pos, int cap)
{
	int ttl = PCI_FIND_CAP_TTL;

	return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
}

393
394
395
396
397
398
399
int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
{
	return __pci_find_next_cap(dev->bus, dev->devfn,
				   pos + PCI_CAP_LIST_NEXT, cap);
}
EXPORT_SYMBOL_GPL(pci_find_next_capability);

400
401
static int __pci_bus_find_cap_start(struct pci_bus *bus,
				    unsigned int devfn, u8 hdr_type)
Linus Torvalds's avatar
Linus Torvalds committed
402
403
404
405
406
407
408
409
410
411
{
	u16 status;

	pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
	if (!(status & PCI_STATUS_CAP_LIST))
		return 0;

	switch (hdr_type) {
	case PCI_HEADER_TYPE_NORMAL:
	case PCI_HEADER_TYPE_BRIDGE:
412
		return PCI_CAPABILITY_LIST;
Linus Torvalds's avatar
Linus Torvalds committed
413
	case PCI_HEADER_TYPE_CARDBUS:
414
		return PCI_CB_CAPABILITY_LIST;
Linus Torvalds's avatar
Linus Torvalds committed
415
	}
416
417

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
418
419
420
}

/**
421
 * pci_find_capability - query for devices' capabilities
Linus Torvalds's avatar
Linus Torvalds committed
422
423
424
425
426
427
428
429
 * @dev: PCI device to query
 * @cap: capability code
 *
 * Tell if a device supports a given PCI capability.
 * Returns the address of the requested capability structure within the
 * device's PCI configuration space or 0 in case the device does not
 * support it.  Possible values for @cap:
 *
430
431
432
433
 *  %PCI_CAP_ID_PM           Power Management
 *  %PCI_CAP_ID_AGP          Accelerated Graphics Port
 *  %PCI_CAP_ID_VPD          Vital Product Data
 *  %PCI_CAP_ID_SLOTID       Slot Identification
Linus Torvalds's avatar
Linus Torvalds committed
434
 *  %PCI_CAP_ID_MSI          Message Signalled Interrupts
435
 *  %PCI_CAP_ID_CHSWP        CompactPCI HotSwap
Linus Torvalds's avatar
Linus Torvalds committed
436
437
438
439
440
 *  %PCI_CAP_ID_PCIX         PCI-X
 *  %PCI_CAP_ID_EXP          PCI Express
 */
int pci_find_capability(struct pci_dev *dev, int cap)
{
441
442
443
444
445
446
447
	int pos;

	pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
	if (pos)
		pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);

	return pos;
Linus Torvalds's avatar
Linus Torvalds committed
448
}
449
EXPORT_SYMBOL(pci_find_capability);
Linus Torvalds's avatar
Linus Torvalds committed
450
451

/**
452
 * pci_bus_find_capability - query for devices' capabilities
Linus Torvalds's avatar
Linus Torvalds committed
453
454
455
456
457
 * @bus:   the PCI bus to query
 * @devfn: PCI device to query
 * @cap:   capability code
 *
 * Like pci_find_capability() but works for pci devices that do not have a
458
 * pci_dev structure set up yet.
Linus Torvalds's avatar
Linus Torvalds committed
459
460
461
462
463
464
465
 *
 * Returns the address of the requested capability structure within the
 * device's PCI configuration space or 0 in case the device does not
 * support it.
 */
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
{
466
	int pos;
Linus Torvalds's avatar
Linus Torvalds committed
467
468
469
470
	u8 hdr_type;

	pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);

471
472
473
474
475
	pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
	if (pos)
		pos = __pci_find_next_cap(bus, devfn, pos, cap);

	return pos;
Linus Torvalds's avatar
Linus Torvalds committed
476
}
477
EXPORT_SYMBOL(pci_bus_find_capability);
Linus Torvalds's avatar
Linus Torvalds committed
478
479

/**
480
 * pci_find_next_ext_capability - Find an extended capability
Linus Torvalds's avatar
Linus Torvalds committed
481
 * @dev: PCI device to query
482
 * @start: address at which to start looking (0 to start at beginning of list)
Linus Torvalds's avatar
Linus Torvalds committed
483
484
 * @cap: capability code
 *
485
 * Returns the address of the next matching extended capability structure
Linus Torvalds's avatar
Linus Torvalds committed
486
 * within the device's PCI configuration space or 0 if the device does
487
488
 * not support it.  Some capabilities can occur several times, e.g., the
 * vendor-specific capability, and this provides a way to find them all.
Linus Torvalds's avatar
Linus Torvalds committed
489
 */
490
int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
Linus Torvalds's avatar
Linus Torvalds committed
491
492
{
	u32 header;
493
494
	int ttl;
	int pos = PCI_CFG_SPACE_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
495

496
497
498
499
	/* minimum 8 bytes per capability */
	ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;

	if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
Linus Torvalds's avatar
Linus Torvalds committed
500
501
		return 0;

502
503
504
	if (start)
		pos = start;

Linus Torvalds's avatar
Linus Torvalds committed
505
506
507
508
509
510
511
512
513
514
515
	if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
		return 0;

	/*
	 * If we have no capabilities, this is indicated by cap ID,
	 * cap version and next pointer all being 0.
	 */
	if (header == 0)
		return 0;

	while (ttl-- > 0) {
516
		if (PCI_EXT_CAP_ID(header) == cap && pos != start)
Linus Torvalds's avatar
Linus Torvalds committed
517
518
519
			return pos;

		pos = PCI_EXT_CAP_NEXT(header);
520
		if (pos < PCI_CFG_SPACE_SIZE)
Linus Torvalds's avatar
Linus Torvalds committed
521
522
523
524
525
526
527
528
			break;

		if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
			break;
	}

	return 0;
}
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);

/**
 * pci_find_ext_capability - Find an extended capability
 * @dev: PCI device to query
 * @cap: capability code
 *
 * Returns the address of the requested extended capability structure
 * within the device's PCI configuration space or 0 if the device does
 * not support it.  Possible values for @cap:
 *
 *  %PCI_EXT_CAP_ID_ERR		Advanced Error Reporting
 *  %PCI_EXT_CAP_ID_VC		Virtual Channel
 *  %PCI_EXT_CAP_ID_DSN		Device Serial Number
 *  %PCI_EXT_CAP_ID_PWR		Power Budgeting
 */
int pci_find_ext_capability(struct pci_dev *dev, int cap)
{
	return pci_find_next_ext_capability(dev, 0, cap);
}
549
EXPORT_SYMBOL_GPL(pci_find_ext_capability);
Linus Torvalds's avatar
Linus Torvalds committed
550

551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
{
	int rc, ttl = PCI_FIND_CAP_TTL;
	u8 cap, mask;

	if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
		mask = HT_3BIT_CAP_MASK;
	else
		mask = HT_5BIT_CAP_MASK;

	pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
				      PCI_CAP_ID_HT, &ttl);
	while (pos) {
		rc = pci_read_config_byte(dev, pos + 3, &cap);
		if (rc != PCIBIOS_SUCCESSFUL)
			return 0;

		if ((cap & mask) == ht_cap)
			return pos;

571
572
		pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
					      pos + PCI_CAP_LIST_NEXT,
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
					      PCI_CAP_ID_HT, &ttl);
	}

	return 0;
}
/**
 * pci_find_next_ht_capability - query a device's Hypertransport capabilities
 * @dev: PCI device to query
 * @pos: Position from which to continue searching
 * @ht_cap: Hypertransport capability code
 *
 * To be used in conjunction with pci_find_ht_capability() to search for
 * all capabilities matching @ht_cap. @pos should always be a value returned
 * from pci_find_ht_capability().
 *
 * NB. To be 100% safe against broken PCI devices, the caller should take
 * steps to avoid an infinite loop.
 */
int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
{
	return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
}
EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);

/**
 * pci_find_ht_capability - query a device's Hypertransport capabilities
 * @dev: PCI device to query
 * @ht_cap: Hypertransport capability code
 *
 * Tell if a device supports a given Hypertransport capability.
 * Returns an address within the device's PCI configuration space
 * or 0 in case the device does not support the request capability.
 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
 * which has a Hypertransport capability matching @ht_cap.
 */
int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
{
	int pos;

	pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
	if (pos)
		pos = __pci_find_next_ht_cap(dev, pos, ht_cap);

	return pos;
}
EXPORT_SYMBOL_GPL(pci_find_ht_capability);

Linus Torvalds's avatar
Linus Torvalds committed
620
621
622
623
624
625
/**
 * pci_find_parent_resource - return resource region of parent bus of given region
 * @dev: PCI device structure contains resources to be searched
 * @res: child resource record for which parent is sought
 *
 *  For given resource region of given device, return the resource
626
 *  region of parent bus the given region is contained in.
Linus Torvalds's avatar
Linus Torvalds committed
627
 */
Ryan Desfosses's avatar
Ryan Desfosses committed
628
629
struct resource *pci_find_parent_resource(const struct pci_dev *dev,
					  struct resource *res)
Linus Torvalds's avatar
Linus Torvalds committed
630
631
{
	const struct pci_bus *bus = dev->bus;
632
	struct resource *r;
Linus Torvalds's avatar
Linus Torvalds committed
633
634
	int i;

635
	pci_bus_for_each_resource(bus, r, i) {
Linus Torvalds's avatar
Linus Torvalds committed
636
637
		if (!r)
			continue;
638
		if (resource_contains(r, res)) {
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657

			/*
			 * If the window is prefetchable but the BAR is
			 * not, the allocator made a mistake.
			 */
			if (r->flags & IORESOURCE_PREFETCH &&
			    !(res->flags & IORESOURCE_PREFETCH))
				return NULL;

			/*
			 * If we're below a transparent bridge, there may
			 * be both a positively-decoded aperture and a
			 * subtractively-decoded region that contain the BAR.
			 * We want the positively-decoded one, so this depends
			 * on pci_bus_for_each_resource() giving us those
			 * first.
			 */
			return r;
		}
Linus Torvalds's avatar
Linus Torvalds committed
658
	}
659
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
660
}
661
EXPORT_SYMBOL(pci_find_parent_resource);
Linus Torvalds's avatar
Linus Torvalds committed
662

Mika Westerberg's avatar
Mika Westerberg committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/**
 * pci_find_resource - Return matching PCI device resource
 * @dev: PCI device to query
 * @res: Resource to look for
 *
 * Goes over standard PCI resources (BARs) and checks if the given resource
 * is partially or fully contained in any of them. In that case the
 * matching resource is returned, %NULL otherwise.
 */
struct resource *pci_find_resource(struct pci_dev *dev, struct resource *res)
{
	int i;

	for (i = 0; i < PCI_ROM_RESOURCE; i++) {
		struct resource *r = &dev->resource[i];

		if (r->start && resource_contains(r, res))
			return r;
	}

	return NULL;
}
EXPORT_SYMBOL(pci_find_resource);

687
688
689
690
691
692
693
694
695
/**
 * pci_find_pcie_root_port - return PCIe Root Port
 * @dev: PCI device to query
 *
 * Traverse up the parent chain and return the PCIe Root Port PCI Device
 * for a given PCI Device.
 */
struct pci_dev *pci_find_pcie_root_port(struct pci_dev *dev)
{
696
	struct pci_dev *bridge, *highest_pcie_bridge = dev;
697
698
699
700
701
702
703

	bridge = pci_upstream_bridge(dev);
	while (bridge && pci_is_pcie(bridge)) {
		highest_pcie_bridge = bridge;
		bridge = pci_upstream_bridge(bridge);
	}

704
705
	if (pci_pcie_type(highest_pcie_bridge) != PCI_EXP_TYPE_ROOT_PORT)
		return NULL;
706

707
	return highest_pcie_bridge;
708
709
710
}
EXPORT_SYMBOL(pci_find_pcie_root_port);

711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/**
 * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
 * @dev: the PCI device to operate on
 * @pos: config space offset of status word
 * @mask: mask of bit(s) to care about in status word
 *
 * Return 1 when mask bit(s) in status word clear, 0 otherwise.
 */
int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
{
	int i;

	/* Wait for Transaction Pending bit clean */
	for (i = 0; i < 4; i++) {
		u16 status;
		if (i)
			msleep((1 << (i - 1)) * 100);

		pci_read_config_word(dev, pos, &status);
		if (!(status & mask))
			return 1;
	}

	return 0;
}

737
/**
Wei Yang's avatar
Wei Yang committed
738
 * pci_restore_bars - restore a device's BAR values (e.g. after wake-up)
739
740
741
742
743
 * @dev: PCI device to have its BARs restored
 *
 * Restore the BAR values for a given device, so as to make it
 * accessible by its driver.
 */
Ryan Desfosses's avatar
Ryan Desfosses committed
744
static void pci_restore_bars(struct pci_dev *dev)
745
{
746
	int i;
747

748
	for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
749
		pci_update_resource(dev, i);
750
751
}

752
static const struct pci_platform_pm_ops *pci_platform_pm;
753

754
int pci_set_platform_pm(const struct pci_platform_pm_ops *ops)
755
{
756
	if (!ops->is_manageable || !ops->set_state  || !ops->get_state ||
757
	    !ops->choose_state  || !ops->set_wakeup || !ops->need_resume)
758
759
760
761
762
763
764
765
766
767
768
		return -EINVAL;
	pci_platform_pm = ops;
	return 0;
}

static inline bool platform_pci_power_manageable(struct pci_dev *dev)
{
	return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
}

static inline int platform_pci_set_power_state(struct pci_dev *dev,
Ryan Desfosses's avatar
Ryan Desfosses committed
769
					       pci_power_t t)
770
771
772
773
{
	return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
}

774
775
776
777
778
static inline pci_power_t platform_pci_get_power_state(struct pci_dev *dev)
{
	return pci_platform_pm ? pci_platform_pm->get_state(dev) : PCI_UNKNOWN;
}

779
780
781
782
783
static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
{
	return pci_platform_pm ?
			pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
}
Randy Dunlap's avatar
Randy Dunlap committed
784

785
static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable)
786
787
{
	return pci_platform_pm ?
788
			pci_platform_pm->set_wakeup(dev, enable) : -ENODEV;
789
790
}

791
792
793
794
795
static inline bool platform_pci_need_resume(struct pci_dev *dev)
{
	return pci_platform_pm ? pci_platform_pm->need_resume(dev) : false;
}

796
797
798
799
800
static inline bool platform_pci_bridge_d3(struct pci_dev *dev)
{
	return pci_platform_pm ? pci_platform_pm->bridge_d3(dev) : false;
}

Linus Torvalds's avatar
Linus Torvalds committed
801
/**
802
803
804
805
 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
 *                           given PCI device
 * @dev: PCI device to handle.
 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
Linus Torvalds's avatar
Linus Torvalds committed
806
 *
807
808
809
810
811
812
 * RETURN VALUE:
 * -EINVAL if the requested state is invalid.
 * -EIO if device does not support PCI PM or its PM capabilities register has a
 * wrong version, or device doesn't support the requested state.
 * 0 if device already is in the requested state.
 * 0 if device's power state has been successfully changed.
Linus Torvalds's avatar
Linus Torvalds committed
813
 */
814
static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
Linus Torvalds's avatar
Linus Torvalds committed
815
{
816
	u16 pmcsr;
817
	bool need_restore = false;
Linus Torvalds's avatar
Linus Torvalds committed
818

819
820
821
822
	/* Check if we're already there */
	if (dev->current_state == state)
		return 0;

823
	if (!dev->pm_cap)
824
825
		return -EIO;

826
827
828
	if (state < PCI_D0 || state > PCI_D3hot)
		return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
829
	/* Validate current state:
830
	 * Can enter D0 from any state, but if we can only go deeper
Linus Torvalds's avatar
Linus Torvalds committed
831
832
	 * to sleep if we're already in a low power state
	 */
833
	if (state != PCI_D0 && dev->current_state <= PCI_D3cold
834
	    && dev->current_state > state) {
835
		pci_err(dev, "invalid power transition (from state %d to %d)\n",
836
			dev->current_state, state);
Linus Torvalds's avatar
Linus Torvalds committed
837
		return -EINVAL;
838
	}
Linus Torvalds's avatar
Linus Torvalds committed
839
840

	/* check if this device supports the desired state */
841
842
	if ((state == PCI_D1 && !dev->d1_support)
	   || (state == PCI_D2 && !dev->d2_support))
843
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
844

845
	pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
846

847
	/* If we're (effectively) in D3, force entire word to 0.
Linus Torvalds's avatar
Linus Torvalds committed
848
849
850
	 * This doesn't affect PME_Status, disables PME_En, and
	 * sets PowerState to 0.
	 */
851
	switch (dev->current_state) {
852
853
854
855
856
857
	case PCI_D0:
	case PCI_D1:
	case PCI_D2:
		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
		pmcsr |= state;
		break;
858
859
	case PCI_D3hot:
	case PCI_D3cold:
860
861
	case PCI_UNKNOWN: /* Boot-up */
		if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
862
		 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
863
			need_restore = true;
864
865
		/* Fall-through: force to D0 */
	default:
866
		pmcsr = 0;
867
		break;
Linus Torvalds's avatar
Linus Torvalds committed
868
869
870
	}

	/* enter specified state */
871
	pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
Linus Torvalds's avatar
Linus Torvalds committed
872
873
874
875

	/* Mandatory power management transition delays */
	/* see PCI PM 1.1 5.6.1 table 18 */
	if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
876
		pci_dev_d3_sleep(dev);
Linus Torvalds's avatar
Linus Torvalds committed
877
	else if (state == PCI_D2 || dev->current_state == PCI_D2)
878
		udelay(PCI_PM_D2_DELAY);
Linus Torvalds's avatar
Linus Torvalds committed
879

880
881
882
	pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
	dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
	if (dev->current_state != state && printk_ratelimit())
883
		pci_info(dev, "Refused to change power state, currently in D%d\n",
884
			 dev->current_state);
885

886
887
	/*
	 * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
888
889
890
891
892
893
894
895
896
897
898
899
900
901
	 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
	 * from D3hot to D0 _may_ perform an internal reset, thereby
	 * going to "D0 Uninitialized" rather than "D0 Initialized".
	 * For example, at least some versions of the 3c905B and the
	 * 3c556B exhibit this behaviour.
	 *
	 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
	 * devices in a D3hot state at boot.  Consequently, we need to
	 * restore at least the BARs so that the device will be
	 * accessible to its driver.
	 */
	if (need_restore)
		pci_restore_bars(dev);

902
	if (dev->bus->self)
Shaohua Li's avatar
Shaohua Li committed
903
904
		pcie_aspm_pm_state_change(dev->bus->self);

Linus Torvalds's avatar
Linus Torvalds committed
905
906
907
	return 0;
}

908
/**
909
 * pci_update_current_state - Read power state of given device and cache it
910
 * @dev: PCI device to handle.
911
 * @state: State to cache in case the device doesn't have the PM capability
912
913
914
915
916
917
918
 *
 * The power state is read from the PMCSR register, which however is
 * inaccessible in D3cold.  The platform firmware is therefore queried first
 * to detect accessibility of the register.  In case the platform firmware
 * reports an incorrect state or the device isn't power manageable by the
 * platform at all, we try to detect D3cold by testing accessibility of the
 * vendor ID in config space.
919
 */
920
void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
921
{
922
923
924
925
	if (platform_pci_get_power_state(dev) == PCI_D3cold ||
	    !pci_device_is_present(dev)) {
		dev->current_state = PCI_D3cold;
	} else if (dev->pm_cap) {
926
927
		u16 pmcsr;

928
		pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
929
		dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
930
931
	} else {
		dev->current_state = state;
932
933
934
	}
}

935
936
937
938
939
940
941
942
943
944
945
946
947
/**
 * pci_power_up - Put the given device into D0 forcibly
 * @dev: PCI device to power up
 */
void pci_power_up(struct pci_dev *dev)
{
	if (platform_pci_power_manageable(dev))
		platform_pci_set_power_state(dev, PCI_D0);

	pci_raw_set_power_state(dev, PCI_D0);
	pci_update_current_state(dev, PCI_D0);
}

948
949
950
951
952
953
954
955
956
957
958
959
960
/**
 * pci_platform_power_transition - Use platform to change device power state
 * @dev: PCI device to handle.
 * @state: State to put the device into.
 */
static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
{
	int error;

	if (platform_pci_power_manageable(dev)) {
		error = platform_pci_set_power_state(dev, state);
		if (!error)
			pci_update_current_state(dev, state);
961
	} else
962
		error = -ENODEV;
963
964
965

	if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
		dev->current_state = PCI_D0;
966
967
968
969

	return error;
}

970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/**
 * pci_wakeup - Wake up a PCI device
 * @pci_dev: Device to handle.
 * @ign: ignored parameter
 */
static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
{
	pci_wakeup_event(pci_dev);
	pm_request_resume(&pci_dev->dev);
	return 0;
}

/**
 * pci_wakeup_bus - Walk given bus and wake up devices on it
 * @bus: Top bus of the subtree to walk.
 */
986
void pci_wakeup_bus(struct pci_bus *bus)
987
988
989
990
991
{
	if (bus)
		pci_walk_bus(bus, pci_wakeup, NULL);
}

992
993
994
995
996
997
998
/**
 * __pci_start_power_transition - Start power transition of a PCI device
 * @dev: PCI device to handle.
 * @state: State to put the device into.
 */
static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
{
999
	if (state == PCI_D0) {
1000
		pci_platform_power_transition(dev, PCI_D0);
1001
1002
1003
1004
1005
1006
1007
1008
		/*
		 * Mandatory power management transition delays, see
		 * PCI Express Base Specification Revision 2.0 Section
		 * 6.6.1: Conventional Reset.  Do not delay for
		 * devices powered on/off by corresponding bridge,
		 * because have already delayed for the bridge.
		 */
		if (dev->runtime_d3cold) {
1009
			if (dev->d3cold_delay && !dev->imm_ready)
1010
				msleep(dev->d3cold_delay);
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
			/*
			 * When powering on a bridge from D3cold, the
			 * whole hierarchy may be powered on into
			 * D0uninitialized state, resume them to give
			 * them a chance to suspend again
			 */
			pci_wakeup_bus(dev->subordinate);
		}
	}
}

/**
 * __pci_dev_set_current_state - Set current state of a PCI device
 * @dev: Device to handle
 * @data: pointer to state to be set
 */
static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
{
	pci_power_t state = *(pci_power_t *)data;

	dev->current_state = state;
	return 0;
}

/**
1036
 * pci_bus_set_current_state - Walk given bus and set current state of devices
1037
1038
1039
 * @bus: Top bus of the subtree to walk.
 * @state: state to be set
 */
1040
void pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
1041
1042
1043
{
	if (bus)
		pci_walk_bus(bus, __pci_dev_set_current_state, &state);
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
}

/**
 * __pci_complete_power_transition - Complete power transition of a PCI device
 * @dev: PCI device to handle.
 * @state: State to put the device into.
 *
 * This function should not be called directly by device drivers.
 */
int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
{
1055
1056
	int ret;

1057
	if (state <= PCI_D0)
1058
1059
1060
1061
		return -EINVAL;
	ret = pci_platform_power_transition(dev, state);
	/* Power off the bridge may power off the whole hierarchy */
	if (!ret && state == PCI_D3cold)
1062
		pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
1063
	return ret;
1064
1065
1066
}
EXPORT_SYMBOL_GPL(__pci_complete_power_transition);

1067
1068
1069
1070
1071
/**
 * pci_set_power_state - Set the power state of a PCI device
 * @dev: PCI device to handle.
 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
 *
1072
 * Transition a device to a new power state, using the platform firmware and/or
1073
1074
1075
1076
1077
1078
 * the device's PCI PM registers.
 *
 * RETURN VALUE:
 * -EINVAL if the requested state is invalid.
 * -EIO if device does not support PCI PM or its PM capabilities register has a
 * wrong version, or device doesn't support the requested state.
1079
 * 0 if the transition is to D1 or D2 but D1 and D2 are not supported.
1080
 * 0 if device already is in the requested state.
1081
 * 0 if the transition is to D3 but D3 is not supported.
1082
1083
1084
1085
 * 0 if device's power state has been successfully changed.
 */
int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
{
1086
	int error;
1087
1088

	/* bound the state we're entering */
1089
1090
	if (state > PCI_D3cold)
		state = PCI_D3cold;
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
	else if (state < PCI_D0)
		state = PCI_D0;
	else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
		/*
		 * If the device or the parent bridge do not support PCI PM,
		 * ignore the request if we're doing anything other than putting
		 * it into D0 (which would only happen on boot).
		 */
		return 0;

1101
1102
1103
1104
	/* Check if we're already there */
	if (dev->current_state == state)
		return 0;

1105
1106
	__pci_start_power_transition(dev, state);

1107
1108
	/* This device is quirked not to be put into D3, so
	   don't put it in D3 */
1109
	if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
1110
		return 0;
1111

1112
1113
1114
1115
1116
1117
	/*
	 * To put device in D3cold, we put device into D3hot in native
	 * way, then put device into D3cold with platform ops
	 */
	error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
					PCI_D3hot : state);
1118

1119
1120
	if (!__pci_complete_power_transition(dev, state))
		error = 0;
1121
1122
1123

	return error;
}
1124
EXPORT_SYMBOL(pci_set_power_state);
1125

Linus Torvalds's avatar
Linus Torvalds committed
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
/**
 * pci_choose_state - Choose the power state of a PCI device
 * @dev: PCI device to be suspended
 * @state: target sleep state for the whole system. This is the value
 *	that is passed to suspend() function.
 *
 * Returns PCI power state suitable for given device and given system
 * message.
 */

pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
{
1138
	pci_power_t ret;
1139

1140
	if (!dev->pm_cap)
Linus Torvalds's avatar
Linus Torvalds committed
1141
1142
		return PCI_D0;

1143
1144
1145
	ret = platform_pci_choose_state(dev);
	if (ret != PCI_POWER_ERROR)
		return ret;
1146
1147
1148
1149
1150

	switch (state.event) {
	case PM_EVENT_ON:
		return PCI_D0;
	case PM_EVENT_FREEZE:
1151
1152
	case PM_EVENT_PRETHAW:
		/* REVISIT both freeze and pre-thaw "should" use D0 */
1153
	case PM_EVENT_SUSPEND:
1154
	case PM_EVENT_HIBERNATE:
1155
		return PCI_D3hot;
Linus Torvalds's avatar
Linus Torvalds committed
1156
	default:
1157
		pci_info(dev, "unrecognized suspend event %d\n",
1158
			 state.event);
Linus Torvalds's avatar
Linus Torvalds committed
1159
1160
1161
1162
1163
1164
		BUG();
	}
	return PCI_D0;
}
EXPORT_SYMBOL(pci_choose_state);

1165
1166
#define PCI_EXP_SAVE_REGS	7

1167
1168
static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
						       u16 cap, bool extended)
1169
1170
1171
{
	struct pci_cap_saved_state *tmp;

1172
	hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
1173
		if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
1174
1175
1176
1177
1178
			return tmp;
	}
	return NULL;
}

1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
{
	return _pci_find_saved_cap(dev, cap, false);
}

struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
{
	return _pci_find_saved_cap(dev, cap, true);
}

1189
1190
static int pci_save_pcie_state(struct pci_dev *dev)
{
1191
	int i = 0;
1192
1193
1194
	struct pci_cap_saved_state *save_state;
	u16 *cap;

1195
	if (!pci_is_pcie(dev))
1196
1197
		return 0;

1198
	save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
1199
	if (!save_state) {
1200
		pci_err(dev, "buffer not found in %s\n", __func__);
1201
1202
		return -ENOMEM;
	}
1203

1204
1205
1206
1207
1208
1209
1210
1211
	cap = (u16 *)&save_state->cap.data[0];
	pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
	pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
	pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
	pcie_capability_read_word(dev, PCI_EXP_RTCTL,  &cap[i++]);
	pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
	pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
	pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
1212

1213
1214
1215
1216
1217
	return 0;
}

static void pci_restore_pcie_state(struct pci_dev *dev)
{
1218
	int i = 0;
1219
1220
1221
1222
	struct pci_cap_saved_state *save_state;
	u16 *cap;

	save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
1223
	if (!save_state)
1224
1225
		return;

1226
1227
1228
1229
1230
1231
1232
1233
	cap = (u16 *)&save_state->cap.data[0];
	pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
	pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
	pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
	pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
	pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
	pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
	pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
1234
1235
}

1236
1237
1238

static int pci_save_pcix_state(struct pci_dev *dev)
{
1239
	int pos;
1240
1241
1242
	struct pci_cap_saved_state *save_state;

	pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1243
	if (!pos)
1244
1245
		return 0;

1246
	save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
1247
	if (!save_state) {
1248
		pci_err(dev, "buffer not found in %s\n", __func__);
1249
1250
1251
		return -ENOMEM;
	}

1252
1253
	pci_read_config_word(dev, pos + PCI_X_CMD,
			     (u16 *)save_state->cap.data);
1254

1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
	return 0;
}

static void pci_restore_pcix_state(struct pci_dev *dev)
{
	int i = 0, pos;
	struct pci_cap_saved_state *save_state;
	u16 *cap;

	save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
	pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1266
	if (!save_state || !pos)
1267
		return;
1268
	cap = (u16 *)&save_state->cap.data[0];
1269
1270
1271
1272
1273

	pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
}


Linus Torvalds's avatar
Linus Torvalds committed
1274
1275
1276
1277
/**
 * pci_save_state - save the PCI configuration space of a device before suspending
 * @dev: - PCI device that we're dealing with
 */
Ryan Desfosses's avatar
Ryan Desfosses committed
1278
int pci_save_state(struct pci_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1279
1280
1281
1282
{
	int i;
	/* XXX: 100% dword access ok here? */
	for (i = 0; i < 16; i++)
1283
		pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
1284
	dev->state_saved = true;
1285
1286
1287

	i = pci_save_pcie_state(dev);
	if (i != 0)
1288
		return i;
1289
1290
1291

	i = pci_save_pcix_state(dev);
	if (i != 0)
1292
		return i;
1293