1
2
3
4
5
6
7
8
9
10#include <linux/kernel.h>
11#include <linux/delay.h>
12#include <linux/dmi.h>
13#include <linux/init.h>
14#include <linux/pci.h>
15#include <linux/pm.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/spinlock.h>
19#include <linux/string.h>
20#include <linux/log2.h>
21#include <linux/pci-aspm.h>
22#include <linux/pm_wakeup.h>
23#include <linux/interrupt.h>
24#include <linux/device.h>
25#include <linux/pm_runtime.h>
26#include <linux/pci_hotplug.h>
27#include <asm/setup.h>
28#include <linux/aer.h>
29#include "pci.h"
30
31const char *pci_power_names[] = {
32 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
33};
34EXPORT_SYMBOL_GPL(pci_power_names);
35
36int isa_dma_bridge_buggy;
37EXPORT_SYMBOL(isa_dma_bridge_buggy);
38
39int pci_pci_problems;
40EXPORT_SYMBOL(pci_pci_problems);
41
42unsigned int pci_pm_d3_delay;
43
44static void pci_pme_list_scan(struct work_struct *work);
45
46static LIST_HEAD(pci_pme_list);
47static DEFINE_MUTEX(pci_pme_list_mutex);
48static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
49
50struct pci_pme_device {
51 struct list_head list;
52 struct pci_dev *dev;
53};
54
55#define PME_TIMEOUT 1000
56
57static void pci_dev_d3_sleep(struct pci_dev *dev)
58{
59 unsigned int delay = dev->d3_delay;
60
61 if (delay < pci_pm_d3_delay)
62 delay = pci_pm_d3_delay;
63
64 msleep(delay);
65}
66
67#ifdef CONFIG_PCI_DOMAINS
68int pci_domains_supported = 1;
69#endif
70
71#define DEFAULT_CARDBUS_IO_SIZE (256)
72#define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
73
74unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
75unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
76
77#define DEFAULT_HOTPLUG_IO_SIZE (256)
78#define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
79
80unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
81unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
82
83#define DEFAULT_HOTPLUG_BUS_SIZE 1
84unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
85
86enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
87
88
89
90
91
92
93
94u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
95u8 pci_cache_line_size;
96
97
98
99
100
101unsigned int pcibios_max_latency = 255;
102
103
104static bool pcie_ari_disabled;
105
106
107static bool pci_bridge_d3_disable;
108
109static bool pci_bridge_d3_force;
110
111static int __init pcie_port_pm_setup(char *str)
112{
113 if (!strcmp(str, "off"))
114 pci_bridge_d3_disable = true;
115 else if (!strcmp(str, "force"))
116 pci_bridge_d3_force = true;
117 return 1;
118}
119__setup("pcie_port_pm=", pcie_port_pm_setup);
120
121
122
123
124
125
126
127
128unsigned char pci_bus_max_busnr(struct pci_bus *bus)
129{
130 struct pci_bus *tmp;
131 unsigned char max, n;
132
133 max = bus->busn_res.end;
134 list_for_each_entry(tmp, &bus->children, node) {
135 n = pci_bus_max_busnr(tmp);
136 if (n > max)
137 max = n;
138 }
139 return max;
140}
141EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
142
143#ifdef CONFIG_HAS_IOMEM
144void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
145{
146 struct resource *res = &pdev->resource[bar];
147
148
149
150
151 if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
152 dev_warn(&pdev->dev, "can't ioremap BAR %d: %pR\n", bar, res);
153 return NULL;
154 }
155 return ioremap_nocache(res->start, resource_size(res));
156}
157EXPORT_SYMBOL_GPL(pci_ioremap_bar);
158#endif
159
160
161static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
162 u8 pos, int cap, int *ttl)
163{
164 u8 id;
165 u16 ent;
166
167 pci_bus_read_config_byte(bus, devfn, pos, &pos);
168
169 while ((*ttl)--) {
170 if (pos < 0x40)
171 break;
172 pos &= ~3;
173 pci_bus_read_config_word(bus, devfn, pos, &ent);
174
175 id = ent & 0xff;
176 if (id == 0xff)
177 break;
178 if (id == cap)
179 return pos;
180 pos = (ent >> 8);
181 }
182 return 0;
183}
184
185static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
186 u8 pos, int cap)
187{
188 int ttl = PCI_FIND_CAP_TTL;
189
190 return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
191}
192
193int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
194{
195 return __pci_find_next_cap(dev->bus, dev->devfn,
196 pos + PCI_CAP_LIST_NEXT, cap);
197}
198EXPORT_SYMBOL_GPL(pci_find_next_capability);
199
200static int __pci_bus_find_cap_start(struct pci_bus *bus,
201 unsigned int devfn, u8 hdr_type)
202{
203 u16 status;
204
205 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
206 if (!(status & PCI_STATUS_CAP_LIST))
207 return 0;
208
209 switch (hdr_type) {
210 case PCI_HEADER_TYPE_NORMAL:
211 case PCI_HEADER_TYPE_BRIDGE:
212 return PCI_CAPABILITY_LIST;
213 case PCI_HEADER_TYPE_CARDBUS:
214 return PCI_CB_CAPABILITY_LIST;
215 }
216
217 return 0;
218}
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239int pci_find_capability(struct pci_dev *dev, int cap)
240{
241 int pos;
242
243 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
244 if (pos)
245 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
246
247 return pos;
248}
249EXPORT_SYMBOL(pci_find_capability);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
265{
266 int pos;
267 u8 hdr_type;
268
269 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
270
271 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
272 if (pos)
273 pos = __pci_find_next_cap(bus, devfn, pos, cap);
274
275 return pos;
276}
277EXPORT_SYMBOL(pci_bus_find_capability);
278
279
280
281
282
283
284
285
286
287
288
289
290int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
291{
292 u32 header;
293 int ttl;
294 int pos = PCI_CFG_SPACE_SIZE;
295
296
297 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
298
299 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
300 return 0;
301
302 if (start)
303 pos = start;
304
305 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
306 return 0;
307
308
309
310
311
312 if (header == 0)
313 return 0;
314
315 while (ttl-- > 0) {
316 if (PCI_EXT_CAP_ID(header) == cap && pos != start)
317 return pos;
318
319 pos = PCI_EXT_CAP_NEXT(header);
320 if (pos < PCI_CFG_SPACE_SIZE)
321 break;
322
323 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
324 break;
325 }
326
327 return 0;
328}
329EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345int pci_find_ext_capability(struct pci_dev *dev, int cap)
346{
347 return pci_find_next_ext_capability(dev, 0, cap);
348}
349EXPORT_SYMBOL_GPL(pci_find_ext_capability);
350
351static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
352{
353 int rc, ttl = PCI_FIND_CAP_TTL;
354 u8 cap, mask;
355
356 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
357 mask = HT_3BIT_CAP_MASK;
358 else
359 mask = HT_5BIT_CAP_MASK;
360
361 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
362 PCI_CAP_ID_HT, &ttl);
363 while (pos) {
364 rc = pci_read_config_byte(dev, pos + 3, &cap);
365 if (rc != PCIBIOS_SUCCESSFUL)
366 return 0;
367
368 if ((cap & mask) == ht_cap)
369 return pos;
370
371 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
372 pos + PCI_CAP_LIST_NEXT,
373 PCI_CAP_ID_HT, &ttl);
374 }
375
376 return 0;
377}
378
379
380
381
382
383
384
385
386
387
388
389
390
391int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
392{
393 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
394}
395EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
396
397
398
399
400
401
402
403
404
405
406
407
408int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
409{
410 int pos;
411
412 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
413 if (pos)
414 pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
415
416 return pos;
417}
418EXPORT_SYMBOL_GPL(pci_find_ht_capability);
419
420
421
422
423
424
425
426
427
428struct resource *pci_find_parent_resource(const struct pci_dev *dev,
429 struct resource *res)
430{
431 const struct pci_bus *bus = dev->bus;
432 struct resource *r;
433 int i;
434
435 pci_bus_for_each_resource(bus, r, i) {
436 if (!r)
437 continue;
438 if (res->start && resource_contains(r, res)) {
439
440
441
442
443
444 if (r->flags & IORESOURCE_PREFETCH &&
445 !(res->flags & IORESOURCE_PREFETCH))
446 return NULL;
447
448
449
450
451
452
453
454
455
456 return r;
457 }
458 }
459 return NULL;
460}
461EXPORT_SYMBOL(pci_find_parent_resource);
462
463
464
465
466
467
468
469
470struct pci_dev *pci_find_pcie_root_port(struct pci_dev *dev)
471{
472 struct pci_dev *bridge, *highest_pcie_bridge = dev;
473
474 bridge = pci_upstream_bridge(dev);
475 while (bridge && pci_is_pcie(bridge)) {
476 highest_pcie_bridge = bridge;
477 bridge = pci_upstream_bridge(bridge);
478 }
479
480 if (pci_pcie_type(highest_pcie_bridge) != PCI_EXP_TYPE_ROOT_PORT)
481 return NULL;
482
483 return highest_pcie_bridge;
484}
485EXPORT_SYMBOL(pci_find_pcie_root_port);
486
487
488
489
490
491
492
493
494
495int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
496{
497 int i;
498
499
500 for (i = 0; i < 4; i++) {
501 u16 status;
502 if (i)
503 msleep((1 << (i - 1)) * 100);
504
505 pci_read_config_word(dev, pos, &status);
506 if (!(status & mask))
507 return 1;
508 }
509
510 return 0;
511}
512
513
514
515
516
517
518
519
520static void pci_restore_bars(struct pci_dev *dev)
521{
522 int i;
523
524
525 if (dev->is_virtfn)
526 return;
527
528 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
529 pci_update_resource(dev, i);
530}
531
532static struct pci_platform_pm_ops *pci_platform_pm;
533
534int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
535{
536 if (!ops->is_manageable || !ops->set_state || !ops->choose_state
537 || !ops->sleep_wake)
538 return -EINVAL;
539 pci_platform_pm = ops;
540 return 0;
541}
542
543static inline bool platform_pci_power_manageable(struct pci_dev *dev)
544{
545 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
546}
547
548static inline int platform_pci_set_power_state(struct pci_dev *dev,
549 pci_power_t t)
550{
551 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
552}
553
554static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
555{
556 return pci_platform_pm ?
557 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
558}
559
560static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
561{
562 return pci_platform_pm ?
563 pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
564}
565
566static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable)
567{
568 return pci_platform_pm ?
569 pci_platform_pm->run_wake(dev, enable) : -ENODEV;
570}
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
586{
587 u16 pmcsr;
588 bool need_restore = false;
589
590
591 if (dev->current_state == state)
592 return 0;
593
594 if (!dev->pm_cap)
595 return -EIO;
596
597 if (state < PCI_D0 || state > PCI_D3hot)
598 return -EINVAL;
599
600
601
602
603
604 if (state != PCI_D0 && dev->current_state <= PCI_D3cold
605 && dev->current_state > state) {
606 dev_err(&dev->dev, "invalid power transition (from state %d to %d)\n",
607 dev->current_state, state);
608 return -EINVAL;
609 }
610
611
612 if ((state == PCI_D1 && !dev->d1_support)
613 || (state == PCI_D2 && !dev->d2_support))
614 return -EIO;
615
616 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
617
618
619
620
621
622 switch (dev->current_state) {
623 case PCI_D0:
624 case PCI_D1:
625 case PCI_D2:
626 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
627 pmcsr |= state;
628 break;
629 case PCI_D3hot:
630 case PCI_D3cold:
631 case PCI_UNKNOWN:
632 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
633 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
634 need_restore = true;
635
636 default:
637 pmcsr = 0;
638 break;
639 }
640
641
642 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
643
644
645
646 if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
647 pci_dev_d3_sleep(dev);
648 else if (state == PCI_D2 || dev->current_state == PCI_D2)
649 udelay(PCI_PM_D2_DELAY);
650
651 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
652 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
653 if (dev->current_state != state && printk_ratelimit())
654 dev_info(&dev->dev, "Refused to change power state, currently in D%d\n",
655 dev->current_state);
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670 if (need_restore)
671 pci_restore_bars(dev);
672
673 if (dev->bus->self)
674 pcie_aspm_pm_state_change(dev->bus->self);
675
676 return 0;
677}
678
679
680
681
682
683
684
685void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
686{
687 if (dev->pm_cap) {
688 u16 pmcsr;
689
690
691
692
693
694 if (dev->current_state == PCI_D3cold)
695 return;
696 if (state == PCI_D3cold) {
697 dev->current_state = PCI_D3cold;
698 return;
699 }
700 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
701 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
702 } else {
703 dev->current_state = state;
704 }
705}
706
707
708
709
710
711void pci_power_up(struct pci_dev *dev)
712{
713 if (platform_pci_power_manageable(dev))
714 platform_pci_set_power_state(dev, PCI_D0);
715
716 pci_raw_set_power_state(dev, PCI_D0);
717 pci_update_current_state(dev, PCI_D0);
718}
719
720
721
722
723
724
725static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
726{
727 int error;
728
729 if (platform_pci_power_manageable(dev)) {
730 error = platform_pci_set_power_state(dev, state);
731 if (!error)
732 pci_update_current_state(dev, state);
733 } else
734 error = -ENODEV;
735
736 if (error && !dev->pm_cap)
737 dev->current_state = PCI_D0;
738
739 return error;
740}
741
742
743
744
745
746
747static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
748{
749 pci_wakeup_event(pci_dev);
750 pm_request_resume(&pci_dev->dev);
751 return 0;
752}
753
754
755
756
757
758static void pci_wakeup_bus(struct pci_bus *bus)
759{
760 if (bus)
761 pci_walk_bus(bus, pci_wakeup, NULL);
762}
763
764
765
766
767
768
769static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
770{
771 if (state == PCI_D0) {
772 pci_platform_power_transition(dev, PCI_D0);
773
774
775
776
777
778
779
780 if (dev->runtime_d3cold) {
781 msleep(dev->d3cold_delay);
782
783
784
785
786
787
788 pci_wakeup_bus(dev->subordinate);
789 }
790 }
791}
792
793
794
795
796
797
798static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
799{
800 pci_power_t state = *(pci_power_t *)data;
801
802 dev->current_state = state;
803 return 0;
804}
805
806
807
808
809
810
811static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
812{
813 if (bus)
814 pci_walk_bus(bus, __pci_dev_set_current_state, &state);
815}
816
817
818
819
820
821
822
823
824int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
825{
826 int ret;
827
828 if (state <= PCI_D0)
829 return -EINVAL;
830 ret = pci_platform_power_transition(dev, state);
831
832 if (!ret && state == PCI_D3cold)
833 __pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
834 return ret;
835}
836EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
854{
855 int error;
856
857
858 if (state > PCI_D3cold)
859 state = PCI_D3cold;
860 else if (state < PCI_D0)
861 state = PCI_D0;
862 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
863
864
865
866
867
868 return 0;
869
870
871 if (dev->current_state == state)
872 return 0;
873
874 __pci_start_power_transition(dev, state);
875
876
877
878 if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
879 return 0;
880
881
882
883
884
885 error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
886 PCI_D3hot : state);
887
888 if (!__pci_complete_power_transition(dev, state))
889 error = 0;
890
891 return error;
892}
893EXPORT_SYMBOL(pci_set_power_state);
894
895
896
897
898
899
900
901
902
903
904
905pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
906{
907 pci_power_t ret;
908
909 if (!dev->pm_cap)
910 return PCI_D0;
911
912 ret = platform_pci_choose_state(dev);
913 if (ret != PCI_POWER_ERROR)
914 return ret;
915
916 switch (state.event) {
917 case PM_EVENT_ON:
918 return PCI_D0;
919 case PM_EVENT_FREEZE:
920 case PM_EVENT_PRETHAW:
921
922 case PM_EVENT_SUSPEND:
923 case PM_EVENT_HIBERNATE:
924 return PCI_D3hot;
925 default:
926 dev_info(&dev->dev, "unrecognized suspend event %d\n",
927 state.event);
928 BUG();
929 }
930 return PCI_D0;
931}
932EXPORT_SYMBOL(pci_choose_state);
933
934#define PCI_EXP_SAVE_REGS 7
935
936static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
937 u16 cap, bool extended)
938{
939 struct pci_cap_saved_state *tmp;
940
941 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
942 if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
943 return tmp;
944 }
945 return NULL;
946}
947
948struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
949{
950 return _pci_find_saved_cap(dev, cap, false);
951}
952
953struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
954{
955 return _pci_find_saved_cap(dev, cap, true);
956}
957
958static int pci_save_pcie_state(struct pci_dev *dev)
959{
960 int i = 0;
961 struct pci_cap_saved_state *save_state;
962 u16 *cap;
963
964 if (!pci_is_pcie(dev))
965 return 0;
966
967 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
968 if (!save_state) {
969 dev_err(&dev->dev, "buffer not found in %s\n", __func__);
970 return -ENOMEM;
971 }
972
973 cap = (u16 *)&save_state->cap.data[0];
974 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
975 pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
976 pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
977 pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
978 pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
979 pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
980 pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
981
982 return 0;
983}
984
985static void pci_restore_pcie_state(struct pci_dev *dev)
986{
987 int i = 0;
988 struct pci_cap_saved_state *save_state;
989 u16 *cap;
990
991 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
992 if (!save_state)
993 return;
994
995 cap = (u16 *)&save_state->cap.data[0];
996 pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
997 pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
998 pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
999 pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
1000 pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
1001 pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
1002 pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
1003}
1004
1005
1006static int pci_save_pcix_state(struct pci_dev *dev)
1007{
1008 int pos;
1009 struct pci_cap_saved_state *save_state;
1010
1011 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1012 if (!pos)
1013 return 0;
1014
1015 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
1016 if (!save_state) {
1017 dev_err(&dev->dev, "buffer not found in %s\n", __func__);
1018 return -ENOMEM;
1019 }
1020
1021 pci_read_config_word(dev, pos + PCI_X_CMD,
1022 (u16 *)save_state->cap.data);
1023
1024 return 0;
1025}
1026
1027static void pci_restore_pcix_state(struct pci_dev *dev)
1028{
1029 int i = 0, pos;
1030 struct pci_cap_saved_state *save_state;
1031 u16 *cap;
1032
1033 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
1034 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1035 if (!save_state || !pos)
1036 return;
1037 cap = (u16 *)&save_state->cap.data[0];
1038
1039 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
1040}
1041
1042
1043
1044
1045
1046
1047int pci_save_state(struct pci_dev *dev)
1048{
1049 int i;
1050
1051 for (i = 0; i < 16; i++)
1052 pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
1053 dev->state_saved = true;
1054
1055 i = pci_save_pcie_state(dev);
1056 if (i != 0)
1057 return i;
1058
1059 i = pci_save_pcix_state(dev);
1060 if (i != 0)
1061 return i;
1062
1063 return pci_save_vc_state(dev);
1064}
1065EXPORT_SYMBOL(pci_save_state);
1066
1067static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
1068 u32 saved_val, int retry)
1069{
1070 u32 val;
1071
1072 pci_read_config_dword(pdev, offset, &val);
1073 if (val == saved_val)
1074 return;
1075
1076 for (;;) {
1077 dev_dbg(&pdev->dev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
1078 offset, val, saved_val);
1079 pci_write_config_dword(pdev, offset, saved_val);
1080 if (retry-- <= 0)
1081 return;
1082
1083 pci_read_config_dword(pdev, offset, &val);
1084 if (val == saved_val)
1085 return;
1086
1087 mdelay(1);
1088 }
1089}
1090
1091static void pci_restore_config_space_range(struct pci_dev *pdev,
1092 int start, int end, int retry)
1093{
1094 int index;
1095
1096 for (index = end; index >= start; index--)
1097 pci_restore_config_dword(pdev, 4 * index,
1098 pdev->saved_config_space[index],
1099 retry);
1100}
1101
1102static void pci_restore_config_space(struct pci_dev *pdev)
1103{
1104 if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
1105 pci_restore_config_space_range(pdev, 10, 15, 0);
1106
1107 pci_restore_config_space_range(pdev, 4, 9, 10);
1108 pci_restore_config_space_range(pdev, 0, 3, 0);
1109 } else {
1110 pci_restore_config_space_range(pdev, 0, 15, 0);
1111 }
1112}
1113
1114
1115
1116
1117
1118void pci_restore_state(struct pci_dev *dev)
1119{
1120 if (!dev->state_saved)
1121 return;
1122
1123
1124 pci_restore_pcie_state(dev);
1125 pci_restore_ats_state(dev);
1126 pci_restore_vc_state(dev);
1127
1128 pci_cleanup_aer_error_status_regs(dev);
1129
1130 pci_restore_config_space(dev);
1131
1132 pci_restore_pcix_state(dev);
1133 pci_restore_msi_state(dev);
1134
1135
1136 pci_enable_acs(dev);
1137 pci_restore_iov_state(dev);
1138
1139 dev->state_saved = false;
1140}
1141EXPORT_SYMBOL(pci_restore_state);
1142
1143struct pci_saved_state {
1144 u32 config_space[16];
1145 struct pci_cap_saved_data cap[0];
1146};
1147
1148
1149
1150
1151
1152
1153
1154
1155struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
1156{
1157 struct pci_saved_state *state;
1158 struct pci_cap_saved_state *tmp;
1159 struct pci_cap_saved_data *cap;
1160 size_t size;
1161
1162 if (!dev->state_saved)
1163 return NULL;
1164
1165 size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
1166
1167 hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
1168 size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1169
1170 state = kzalloc(size, GFP_KERNEL);
1171 if (!state)
1172 return NULL;
1173
1174 memcpy(state->config_space, dev->saved_config_space,
1175 sizeof(state->config_space));
1176
1177 cap = state->cap;
1178 hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
1179 size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
1180 memcpy(cap, &tmp->cap, len);
1181 cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
1182 }
1183
1184
1185 return state;
1186}
1187EXPORT_SYMBOL_GPL(pci_store_saved_state);
1188
1189
1190
1191
1192
1193
1194int pci_load_saved_state(struct pci_dev *dev,
1195 struct pci_saved_state *state)
1196{
1197 struct pci_cap_saved_data *cap;
1198
1199 dev->state_saved = false;
1200
1201 if (!state)
1202 return 0;
1203
1204 memcpy(dev->saved_config_space, state->config_space,
1205 sizeof(state->config_space));
1206
1207 cap = state->cap;
1208 while (cap->size) {
1209 struct pci_cap_saved_state *tmp;
1210
1211 tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
1212 if (!tmp || tmp->cap.size != cap->size)
1213 return -EINVAL;
1214
1215 memcpy(tmp->cap.data, cap->data, tmp->cap.size);
1216 cap = (struct pci_cap_saved_data *)((u8 *)cap +
1217 sizeof(struct pci_cap_saved_data) + cap->size);
1218 }
1219
1220 dev->state_saved = true;
1221 return 0;
1222}
1223EXPORT_SYMBOL_GPL(pci_load_saved_state);
1224
1225
1226
1227
1228
1229
1230
1231int pci_load_and_free_saved_state(struct pci_dev *dev,
1232 struct pci_saved_state **state)
1233{
1234 int ret = pci_load_saved_state(dev, *state);
1235 kfree(*state);
1236 *state = NULL;
1237 return ret;
1238}
1239EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
1240
1241int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
1242{
1243 return pci_enable_resources(dev, bars);
1244}
1245
1246static int do_pci_enable_device(struct pci_dev *dev, int bars)
1247{
1248 int err;
1249 struct pci_dev *bridge;
1250 u16 cmd;
1251 u8 pin;
1252
1253 err = pci_set_power_state(dev, PCI_D0);
1254 if (err < 0 && err != -EIO)
1255 return err;
1256
1257 bridge = pci_upstream_bridge(dev);
1258 if (bridge)
1259 pcie_aspm_powersave_config_link(bridge);
1260
1261 err = pcibios_enable_device(dev, bars);
1262 if (err < 0)
1263 return err;
1264 pci_fixup_device(pci_fixup_enable, dev);
1265
1266 if (dev->msi_enabled || dev->msix_enabled)
1267 return 0;
1268
1269 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1270 if (pin) {
1271 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1272 if (cmd & PCI_COMMAND_INTX_DISABLE)
1273 pci_write_config_word(dev, PCI_COMMAND,
1274 cmd & ~PCI_COMMAND_INTX_DISABLE);
1275 }
1276
1277 return 0;
1278}
1279
1280int pci_is_enabled(struct pci_dev *pdev)
1281{
1282 return (atomic_read(&pdev->enable_cnt) > 0);
1283}
1284EXPORT_SYMBOL(pci_is_enabled);
1285
1286
1287
1288
1289
1290
1291
1292
1293int pci_reenable_device(struct pci_dev *dev)
1294{
1295 if (pci_is_enabled(dev))
1296 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
1297 return 0;
1298}
1299EXPORT_SYMBOL(pci_reenable_device);
1300
1301static void pci_enable_bridge(struct pci_dev *dev)
1302{
1303 struct pci_dev *bridge;
1304 int retval;
1305
1306 bridge = pci_upstream_bridge(dev);
1307 if (bridge)
1308 pci_enable_bridge(bridge);
1309
1310 if (pci_is_enabled(dev)) {
1311 if (!dev->is_busmaster)
1312 pci_set_master(dev);
1313 return;
1314 }
1315
1316 retval = pci_enable_device(dev);
1317 if (retval)
1318 dev_err(&dev->dev, "Error enabling bridge (%d), continuing\n",
1319 retval);
1320 pci_set_master(dev);
1321}
1322
1323static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
1324{
1325 struct pci_dev *bridge;
1326 int err;
1327 int i, bars = 0;
1328
1329
1330
1331
1332
1333
1334
1335 if (dev->pm_cap) {
1336 u16 pmcsr;
1337 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1338 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
1339 }
1340
1341 if (atomic_inc_return(&dev->enable_cnt) > 1)
1342 return 0;
1343
1344 bridge = pci_upstream_bridge(dev);
1345 if (bridge)
1346 pci_enable_bridge(bridge);
1347
1348
1349 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1350 if (dev->resource[i].flags & flags)
1351 bars |= (1 << i);
1352 for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
1353 if (dev->resource[i].flags & flags)
1354 bars |= (1 << i);
1355
1356 err = do_pci_enable_device(dev, bars);
1357 if (err < 0)
1358 atomic_dec(&dev->enable_cnt);
1359 return err;
1360}
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370int pci_enable_device_io(struct pci_dev *dev)
1371{
1372 return pci_enable_device_flags(dev, IORESOURCE_IO);
1373}
1374EXPORT_SYMBOL(pci_enable_device_io);
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384int pci_enable_device_mem(struct pci_dev *dev)
1385{
1386 return pci_enable_device_flags(dev, IORESOURCE_MEM);
1387}
1388EXPORT_SYMBOL(pci_enable_device_mem);
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401int pci_enable_device(struct pci_dev *dev)
1402{
1403 return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
1404}
1405EXPORT_SYMBOL(pci_enable_device);
1406
1407
1408
1409
1410
1411
1412
1413struct pci_devres {
1414 unsigned int enabled:1;
1415 unsigned int pinned:1;
1416 unsigned int orig_intx:1;
1417 unsigned int restore_intx:1;
1418 u32 region_mask;
1419};
1420
1421static void pcim_release(struct device *gendev, void *res)
1422{
1423 struct pci_dev *dev = to_pci_dev(gendev);
1424 struct pci_devres *this = res;
1425 int i;
1426
1427 if (dev->msi_enabled)
1428 pci_disable_msi(dev);
1429 if (dev->msix_enabled)
1430 pci_disable_msix(dev);
1431
1432 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
1433 if (this->region_mask & (1 << i))
1434 pci_release_region(dev, i);
1435
1436 if (this->restore_intx)
1437 pci_intx(dev, this->orig_intx);
1438
1439 if (this->enabled && !this->pinned)
1440 pci_disable_device(dev);
1441}
1442
1443static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
1444{
1445 struct pci_devres *dr, *new_dr;
1446
1447 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
1448 if (dr)
1449 return dr;
1450
1451 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
1452 if (!new_dr)
1453 return NULL;
1454 return devres_get(&pdev->dev, new_dr, NULL, NULL);
1455}
1456
1457static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
1458{
1459 if (pci_is_managed(pdev))
1460 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
1461 return NULL;
1462}
1463
1464
1465
1466
1467
1468
1469
1470int pcim_enable_device(struct pci_dev *pdev)
1471{
1472 struct pci_devres *dr;
1473 int rc;
1474
1475 dr = get_pci_dr(pdev);
1476 if (unlikely(!dr))
1477 return -ENOMEM;
1478 if (dr->enabled)
1479 return 0;
1480
1481 rc = pci_enable_device(pdev);
1482 if (!rc) {
1483 pdev->is_managed = 1;
1484 dr->enabled = 1;
1485 }
1486 return rc;
1487}
1488EXPORT_SYMBOL(pcim_enable_device);
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498void pcim_pin_device(struct pci_dev *pdev)
1499{
1500 struct pci_devres *dr;
1501
1502 dr = find_pci_dr(pdev);
1503 WARN_ON(!dr || !dr->enabled);
1504 if (dr)
1505 dr->pinned = 1;
1506}
1507EXPORT_SYMBOL(pcim_pin_device);
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517int __weak pcibios_add_device(struct pci_dev *dev)
1518{
1519 return 0;
1520}
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530void __weak pcibios_release_device(struct pci_dev *dev) {}
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540void __weak pcibios_disable_device(struct pci_dev *dev) {}
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551void __weak pcibios_penalize_isa_irq(int irq, int active) {}
1552
1553static void do_pci_disable_device(struct pci_dev *dev)
1554{
1555 u16 pci_command;
1556
1557 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
1558 if (pci_command & PCI_COMMAND_MASTER) {
1559 pci_command &= ~PCI_COMMAND_MASTER;
1560 pci_write_config_word(dev, PCI_COMMAND, pci_command);
1561 }
1562
1563 pcibios_disable_device(dev);
1564}
1565
1566
1567
1568
1569
1570
1571
1572
1573void pci_disable_enabled_device(struct pci_dev *dev)
1574{
1575 if (pci_is_enabled(dev))
1576 do_pci_disable_device(dev);
1577}
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589void pci_disable_device(struct pci_dev *dev)
1590{
1591 struct pci_devres *dr;
1592
1593 dr = find_pci_dr(dev);
1594 if (dr)
1595 dr->enabled = 0;
1596
1597 dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
1598 "disabling already-disabled device");
1599
1600 if (atomic_dec_return(&dev->enable_cnt) != 0)
1601 return;
1602
1603 do_pci_disable_device(dev);
1604
1605 dev->is_busmaster = 0;
1606}
1607EXPORT_SYMBOL(pci_disable_device);
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
1619 enum pcie_reset_state state)
1620{
1621 return -EINVAL;
1622}
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
1633{
1634 return pcibios_set_pcie_reset_state(dev, state);
1635}
1636EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646bool pci_check_pme_status(struct pci_dev *dev)
1647{
1648 int pmcsr_pos;
1649 u16 pmcsr;
1650 bool ret = false;
1651
1652 if (!dev->pm_cap)
1653 return false;
1654
1655 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
1656 pci_read_config_word(dev, pmcsr_pos, &pmcsr);
1657 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
1658 return false;
1659
1660
1661 pmcsr |= PCI_PM_CTRL_PME_STATUS;
1662 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
1663
1664 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1665 ret = true;
1666 }
1667
1668 pci_write_config_word(dev, pmcsr_pos, pmcsr);
1669
1670 return ret;
1671}
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
1682{
1683 if (pme_poll_reset && dev->pme_poll)
1684 dev->pme_poll = false;
1685
1686 if (pci_check_pme_status(dev)) {
1687 pci_wakeup_event(dev);
1688 pm_request_resume(&dev->dev);
1689 }
1690 return 0;
1691}
1692
1693
1694
1695
1696
1697void pci_pme_wakeup_bus(struct pci_bus *bus)
1698{
1699 if (bus)
1700 pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
1701}
1702
1703
1704
1705
1706
1707
1708
1709bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
1710{
1711 if (!dev->pm_cap)
1712 return false;
1713
1714 return !!(dev->pme_support & (1 << state));
1715}
1716EXPORT_SYMBOL(pci_pme_capable);
1717
1718static void pci_pme_list_scan(struct work_struct *work)
1719{
1720 struct pci_pme_device *pme_dev, *n;
1721
1722 mutex_lock(&pci_pme_list_mutex);
1723 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
1724 if (pme_dev->dev->pme_poll) {
1725 struct pci_dev *bridge;
1726
1727 bridge = pme_dev->dev->bus->self;
1728
1729
1730
1731
1732
1733 if (bridge && bridge->current_state != PCI_D0)
1734 continue;
1735 pci_pme_wakeup(pme_dev->dev, NULL);
1736 } else {
1737 list_del(&pme_dev->list);
1738 kfree(pme_dev);
1739 }
1740 }
1741 if (!list_empty(&pci_pme_list))
1742 schedule_delayed_work(&pci_pme_work,
1743 msecs_to_jiffies(PME_TIMEOUT));
1744 mutex_unlock(&pci_pme_list_mutex);
1745}
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755void pci_pme_active(struct pci_dev *dev, bool enable)
1756{
1757 u16 pmcsr;
1758
1759 if (!dev->pme_support)
1760 return;
1761
1762 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1763
1764 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1765 if (!enable)
1766 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1767
1768 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790 if (dev->pme_poll) {
1791 struct pci_pme_device *pme_dev;
1792 if (enable) {
1793 pme_dev = kmalloc(sizeof(struct pci_pme_device),
1794 GFP_KERNEL);
1795 if (!pme_dev) {
1796 dev_warn(&dev->dev, "can't enable PME#\n");
1797 return;
1798 }
1799 pme_dev->dev = dev;
1800 mutex_lock(&pci_pme_list_mutex);
1801 list_add(&pme_dev->list, &pci_pme_list);
1802 if (list_is_singular(&pci_pme_list))
1803 schedule_delayed_work(&pci_pme_work,
1804 msecs_to_jiffies(PME_TIMEOUT));
1805 mutex_unlock(&pci_pme_list_mutex);
1806 } else {
1807 mutex_lock(&pci_pme_list_mutex);
1808 list_for_each_entry(pme_dev, &pci_pme_list, list) {
1809 if (pme_dev->dev == dev) {
1810 list_del(&pme_dev->list);
1811 kfree(pme_dev);
1812 break;
1813 }
1814 }
1815 mutex_unlock(&pci_pme_list_mutex);
1816 }
1817 }
1818
1819 dev_dbg(&dev->dev, "PME# %s\n", enable ? "enabled" : "disabled");
1820}
1821EXPORT_SYMBOL(pci_pme_active);
1822
1823int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
1824{
1825 return __pci_enable_wake(dev, state, false, enable);
1826}
1827EXPORT_SYMBOL(pci_enable_wake);
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
1850 bool runtime, bool enable)
1851{
1852 int ret = 0;
1853
1854 if (enable && !runtime && !device_may_wakeup(&dev->dev))
1855 return -EINVAL;
1856
1857
1858 if (!!enable == !!dev->wakeup_prepared)
1859 return 0;
1860
1861
1862
1863
1864
1865
1866
1867 if (enable) {
1868 int error;
1869
1870 if (pci_pme_capable(dev, state))
1871 pci_pme_active(dev, true);
1872 else
1873 ret = 1;
1874 error = runtime ? platform_pci_run_wake(dev, true) :
1875 platform_pci_sleep_wake(dev, true);
1876 if (ret)
1877 ret = error;
1878 if (!ret)
1879 dev->wakeup_prepared = true;
1880 } else {
1881 if (runtime)
1882 platform_pci_run_wake(dev, false);
1883 else
1884 platform_pci_sleep_wake(dev, false);
1885 pci_pme_active(dev, false);
1886 dev->wakeup_prepared = false;
1887 }
1888
1889 return ret;
1890}
1891EXPORT_SYMBOL(__pci_enable_wake);
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907int pci_wake_from_d3(struct pci_dev *dev, bool enable)
1908{
1909 return pci_pme_capable(dev, PCI_D3cold) ?
1910 pci_enable_wake(dev, PCI_D3cold, enable) :
1911 pci_enable_wake(dev, PCI_D3hot, enable);
1912}
1913EXPORT_SYMBOL(pci_wake_from_d3);
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923static pci_power_t pci_target_state(struct pci_dev *dev)
1924{
1925 pci_power_t target_state = PCI_D3hot;
1926
1927 if (platform_pci_power_manageable(dev)) {
1928
1929
1930
1931
1932 pci_power_t state = platform_pci_choose_state(dev);
1933
1934 switch (state) {
1935 case PCI_POWER_ERROR:
1936 case PCI_UNKNOWN:
1937 break;
1938 case PCI_D1:
1939 case PCI_D2:
1940 if (pci_no_d1d2(dev))
1941 break;
1942 default:
1943 target_state = state;
1944 }
1945 } else if (!dev->pm_cap) {
1946 target_state = PCI_D0;
1947 } else if (device_may_wakeup(&dev->dev)) {
1948
1949
1950
1951
1952
1953 if (dev->pme_support) {
1954 while (target_state
1955 && !(dev->pme_support & (1 << target_state)))
1956 target_state--;
1957 }
1958 }
1959
1960 return target_state;
1961}
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971int pci_prepare_to_sleep(struct pci_dev *dev)
1972{
1973 pci_power_t target_state = pci_target_state(dev);
1974 int error;
1975
1976 if (target_state == PCI_POWER_ERROR)
1977 return -EIO;
1978
1979 pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
1980
1981 error = pci_set_power_state(dev, target_state);
1982
1983 if (error)
1984 pci_enable_wake(dev, target_state, false);
1985
1986 return error;
1987}
1988EXPORT_SYMBOL(pci_prepare_to_sleep);
1989
1990
1991
1992
1993
1994
1995
1996int pci_back_from_sleep(struct pci_dev *dev)
1997{
1998 pci_enable_wake(dev, PCI_D0, false);
1999 return pci_set_power_state(dev, PCI_D0);
2000}
2001EXPORT_SYMBOL(pci_back_from_sleep);
2002
2003
2004
2005
2006
2007
2008
2009
2010int pci_finish_runtime_suspend(struct pci_dev *dev)
2011{
2012 pci_power_t target_state = pci_target_state(dev);
2013 int error;
2014
2015 if (target_state == PCI_POWER_ERROR)
2016 return -EIO;
2017
2018 dev->runtime_d3cold = target_state == PCI_D3cold;
2019
2020 __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
2021
2022 error = pci_set_power_state(dev, target_state);
2023
2024 if (error) {
2025 __pci_enable_wake(dev, target_state, true, false);
2026 dev->runtime_d3cold = false;
2027 }
2028
2029 return error;
2030}
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040bool pci_dev_run_wake(struct pci_dev *dev)
2041{
2042 struct pci_bus *bus = dev->bus;
2043
2044 if (device_run_wake(&dev->dev))
2045 return true;
2046
2047 if (!dev->pme_support)
2048 return false;
2049
2050 while (bus->parent) {
2051 struct pci_dev *bridge = bus->self;
2052
2053 if (device_run_wake(&bridge->dev))
2054 return true;
2055
2056 bus = bus->parent;
2057 }
2058
2059
2060 if (bus->bridge)
2061 return device_run_wake(bus->bridge);
2062
2063 return false;
2064}
2065EXPORT_SYMBOL_GPL(pci_dev_run_wake);
2066
2067void pci_config_pm_runtime_get(struct pci_dev *pdev)
2068{
2069 struct device *dev = &pdev->dev;
2070 struct device *parent = dev->parent;
2071
2072 if (parent)
2073 pm_runtime_get_sync(parent);
2074 pm_runtime_get_noresume(dev);
2075
2076
2077
2078
2079 pm_runtime_barrier(dev);
2080
2081
2082
2083
2084
2085 if (pdev->current_state == PCI_D3cold)
2086 pm_runtime_resume(dev);
2087}
2088
2089void pci_config_pm_runtime_put(struct pci_dev *pdev)
2090{
2091 struct device *dev = &pdev->dev;
2092 struct device *parent = dev->parent;
2093
2094 pm_runtime_put(dev);
2095 if (parent)
2096 pm_runtime_put_sync(parent);
2097}
2098
2099
2100
2101
2102
2103
2104
2105
2106bool pci_bridge_d3_possible(struct pci_dev *bridge)
2107{
2108 unsigned int year;
2109
2110 if (!pci_is_pcie(bridge))
2111 return false;
2112
2113 switch (pci_pcie_type(bridge)) {
2114 case PCI_EXP_TYPE_ROOT_PORT:
2115 case PCI_EXP_TYPE_UPSTREAM:
2116 case PCI_EXP_TYPE_DOWNSTREAM:
2117 if (pci_bridge_d3_disable)
2118 return false;
2119
2120
2121
2122
2123
2124
2125
2126
2127 if (bridge->is_hotplug_bridge)
2128 return false;
2129
2130 if (pci_bridge_d3_force)
2131 return true;
2132
2133
2134
2135
2136
2137 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
2138 year >= 2015) {
2139 return true;
2140 }
2141 break;
2142 }
2143
2144 return false;
2145}
2146
2147static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
2148{
2149 bool *d3cold_ok = data;
2150
2151 if (
2152 dev->no_d3cold || !dev->d3cold_allowed ||
2153
2154
2155 (device_may_wakeup(&dev->dev) &&
2156 !pci_pme_capable(dev, PCI_D3cold)) ||
2157
2158
2159 !pci_power_manageable(dev))
2160
2161 *d3cold_ok = false;
2162
2163 return !*d3cold_ok;
2164}
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174void pci_bridge_d3_update(struct pci_dev *dev)
2175{
2176 bool remove = !device_is_registered(&dev->dev);
2177 struct pci_dev *bridge;
2178 bool d3cold_ok = true;
2179
2180 bridge = pci_upstream_bridge(dev);
2181 if (!bridge || !pci_bridge_d3_possible(bridge))
2182 return;
2183
2184
2185
2186
2187
2188 if (remove && bridge->bridge_d3)
2189 return;
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199 if (!remove)
2200 pci_dev_check_d3cold(dev, &d3cold_ok);
2201
2202
2203
2204
2205
2206
2207
2208 if (d3cold_ok && !bridge->bridge_d3)
2209 pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
2210 &d3cold_ok);
2211
2212 if (bridge->bridge_d3 != d3cold_ok) {
2213 bridge->bridge_d3 = d3cold_ok;
2214
2215 pci_bridge_d3_update(bridge);
2216 }
2217}
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227void pci_d3cold_enable(struct pci_dev *dev)
2228{
2229 if (dev->no_d3cold) {
2230 dev->no_d3cold = false;
2231 pci_bridge_d3_update(dev);
2232 }
2233}
2234EXPORT_SYMBOL_GPL(pci_d3cold_enable);
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244void pci_d3cold_disable(struct pci_dev *dev)
2245{
2246 if (!dev->no_d3cold) {
2247 dev->no_d3cold = true;
2248 pci_bridge_d3_update(dev);
2249 }
2250}
2251EXPORT_SYMBOL_GPL(pci_d3cold_disable);
2252
2253
2254
2255
2256
2257void pci_pm_init(struct pci_dev *dev)
2258{
2259 int pm;
2260 u16 pmc;
2261
2262 pm_runtime_forbid(&dev->dev);
2263 pm_runtime_set_active(&dev->dev);
2264 pm_runtime_enable(&dev->dev);
2265 device_enable_async_suspend(&dev->dev);
2266 dev->wakeup_prepared = false;
2267
2268 dev->pm_cap = 0;
2269 dev->pme_support = 0;
2270
2271
2272 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
2273 if (!pm)
2274 return;
2275
2276 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
2277
2278 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
2279 dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
2280 pmc & PCI_PM_CAP_VER_MASK);
2281 return;
2282 }
2283
2284 dev->pm_cap = pm;
2285 dev->d3_delay = PCI_PM_D3_WAIT;
2286 dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
2287 dev->bridge_d3 = pci_bridge_d3_possible(dev);
2288 dev->d3cold_allowed = true;
2289
2290 dev->d1_support = false;
2291 dev->d2_support = false;
2292 if (!pci_no_d1d2(dev)) {
2293 if (pmc & PCI_PM_CAP_D1)
2294 dev->d1_support = true;
2295 if (pmc & PCI_PM_CAP_D2)
2296 dev->d2_support = true;
2297
2298 if (dev->d1_support || dev->d2_support)
2299 dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
2300 dev->d1_support ? " D1" : "",
2301 dev->d2_support ? " D2" : "");
2302 }
2303
2304 pmc &= PCI_PM_CAP_PME_MASK;
2305 if (pmc) {
2306 dev_printk(KERN_DEBUG, &dev->dev,
2307 "PME# supported from%s%s%s%s%s\n",
2308 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
2309 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
2310 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
2311 (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
2312 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
2313 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
2314 dev->pme_poll = true;
2315
2316
2317
2318
2319 device_set_wakeup_capable(&dev->dev, true);
2320
2321 pci_pme_active(dev, false);
2322 }
2323}
2324
2325static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop)
2326{
2327 unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI;
2328
2329 switch (prop) {
2330 case PCI_EA_P_MEM:
2331 case PCI_EA_P_VF_MEM:
2332 flags |= IORESOURCE_MEM;
2333 break;
2334 case PCI_EA_P_MEM_PREFETCH:
2335 case PCI_EA_P_VF_MEM_PREFETCH:
2336 flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
2337 break;
2338 case PCI_EA_P_IO:
2339 flags |= IORESOURCE_IO;
2340 break;
2341 default:
2342 return 0;
2343 }
2344
2345 return flags;
2346}
2347
2348static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei,
2349 u8 prop)
2350{
2351 if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO)
2352 return &dev->resource[bei];
2353#ifdef CONFIG_PCI_IOV
2354 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 &&
2355 (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH))
2356 return &dev->resource[PCI_IOV_RESOURCES +
2357 bei - PCI_EA_BEI_VF_BAR0];
2358#endif
2359 else if (bei == PCI_EA_BEI_ROM)
2360 return &dev->resource[PCI_ROM_RESOURCE];
2361 else
2362 return NULL;
2363}
2364
2365
2366static int pci_ea_read(struct pci_dev *dev, int offset)
2367{
2368 struct resource *res;
2369 int ent_size, ent_offset = offset;
2370 resource_size_t start, end;
2371 unsigned long flags;
2372 u32 dw0, bei, base, max_offset;
2373 u8 prop;
2374 bool support_64 = (sizeof(resource_size_t) >= 8);
2375
2376 pci_read_config_dword(dev, ent_offset, &dw0);
2377 ent_offset += 4;
2378
2379
2380 ent_size = ((dw0 & PCI_EA_ES) + 1) << 2;
2381
2382 if (!(dw0 & PCI_EA_ENABLE))
2383 goto out;
2384
2385 bei = (dw0 & PCI_EA_BEI) >> 4;
2386 prop = (dw0 & PCI_EA_PP) >> 8;
2387
2388
2389
2390
2391
2392 if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED)
2393 prop = (dw0 & PCI_EA_SP) >> 16;
2394 if (prop > PCI_EA_P_BRIDGE_IO)
2395 goto out;
2396
2397 res = pci_ea_get_resource(dev, bei, prop);
2398 if (!res) {
2399 dev_err(&dev->dev, "Unsupported EA entry BEI: %u\n", bei);
2400 goto out;
2401 }
2402
2403 flags = pci_ea_flags(dev, prop);
2404 if (!flags) {
2405 dev_err(&dev->dev, "Unsupported EA properties: %#x\n", prop);
2406 goto out;
2407 }
2408
2409
2410 pci_read_config_dword(dev, ent_offset, &base);
2411 start = (base & PCI_EA_FIELD_MASK);
2412 ent_offset += 4;
2413
2414
2415 pci_read_config_dword(dev, ent_offset, &max_offset);
2416 ent_offset += 4;
2417
2418
2419 if (base & PCI_EA_IS_64) {
2420 u32 base_upper;
2421
2422 pci_read_config_dword(dev, ent_offset, &base_upper);
2423 ent_offset += 4;
2424
2425 flags |= IORESOURCE_MEM_64;
2426
2427
2428 if (!support_64 && base_upper)
2429 goto out;
2430
2431 if (support_64)
2432 start |= ((u64)base_upper << 32);
2433 }
2434
2435 end = start + (max_offset | 0x03);
2436
2437
2438 if (max_offset & PCI_EA_IS_64) {
2439 u32 max_offset_upper;
2440
2441 pci_read_config_dword(dev, ent_offset, &max_offset_upper);
2442 ent_offset += 4;
2443
2444 flags |= IORESOURCE_MEM_64;
2445
2446
2447 if (!support_64 && max_offset_upper)
2448 goto out;
2449
2450 if (support_64)
2451 end += ((u64)max_offset_upper << 32);
2452 }
2453
2454 if (end < start) {
2455 dev_err(&dev->dev, "EA Entry crosses address boundary\n");
2456 goto out;
2457 }
2458
2459 if (ent_size != ent_offset - offset) {
2460 dev_err(&dev->dev,
2461 "EA Entry Size (%d) does not match length read (%d)\n",
2462 ent_size, ent_offset - offset);
2463 goto out;
2464 }
2465
2466 res->name = pci_name(dev);
2467 res->start = start;
2468 res->end = end;
2469 res->flags = flags;
2470
2471 if (bei <= PCI_EA_BEI_BAR5)
2472 dev_printk(KERN_DEBUG, &dev->dev, "BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
2473 bei, res, prop);
2474 else if (bei == PCI_EA_BEI_ROM)
2475 dev_printk(KERN_DEBUG, &dev->dev, "ROM: %pR (from Enhanced Allocation, properties %#02x)\n",
2476 res, prop);
2477 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5)
2478 dev_printk(KERN_DEBUG, &dev->dev, "VF BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
2479 bei - PCI_EA_BEI_VF_BAR0, res, prop);
2480 else
2481 dev_printk(KERN_DEBUG, &dev->dev, "BEI %d res: %pR (from Enhanced Allocation, properties %#02x)\n",
2482 bei, res, prop);
2483
2484out:
2485 return offset + ent_size;
2486}
2487
2488
2489void pci_ea_init(struct pci_dev *dev)
2490{
2491 int ea;
2492 u8 num_ent;
2493 int offset;
2494 int i;
2495
2496
2497 ea = pci_find_capability(dev, PCI_CAP_ID_EA);
2498 if (!ea)
2499 return;
2500
2501
2502 pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT,
2503 &num_ent);
2504 num_ent &= PCI_EA_NUM_ENT_MASK;
2505
2506 offset = ea + PCI_EA_FIRST_ENT;
2507
2508
2509 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
2510 offset += 4;
2511
2512
2513 for (i = 0; i < num_ent; ++i)
2514 offset = pci_ea_read(dev, offset);
2515}
2516
2517static void pci_add_saved_cap(struct pci_dev *pci_dev,
2518 struct pci_cap_saved_state *new_cap)
2519{
2520 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
2521}
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
2532 bool extended, unsigned int size)
2533{
2534 int pos;
2535 struct pci_cap_saved_state *save_state;
2536
2537 if (extended)
2538 pos = pci_find_ext_capability(dev, cap);
2539 else
2540 pos = pci_find_capability(dev, cap);
2541
2542 if (!pos)
2543 return 0;
2544
2545 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
2546 if (!save_state)
2547 return -ENOMEM;
2548
2549 save_state->cap.cap_nr = cap;
2550 save_state->cap.cap_extended = extended;
2551 save_state->cap.size = size;
2552 pci_add_saved_cap(dev, save_state);
2553
2554 return 0;
2555}
2556
2557int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
2558{
2559 return _pci_add_cap_save_buffer(dev, cap, false, size);
2560}
2561
2562int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
2563{
2564 return _pci_add_cap_save_buffer(dev, cap, true, size);
2565}
2566
2567
2568
2569
2570
2571void pci_allocate_cap_save_buffers(struct pci_dev *dev)
2572{
2573 int error;
2574
2575 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
2576 PCI_EXP_SAVE_REGS * sizeof(u16));
2577 if (error)
2578 dev_err(&dev->dev,
2579 "unable to preallocate PCI Express save buffer\n");
2580
2581 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
2582 if (error)
2583 dev_err(&dev->dev,
2584 "unable to preallocate PCI-X save buffer\n");
2585
2586 pci_allocate_vc_save_buffers(dev);
2587}
2588
2589void pci_free_cap_save_buffers(struct pci_dev *dev)
2590{
2591 struct pci_cap_saved_state *tmp;
2592 struct hlist_node *n;
2593
2594 hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
2595 kfree(tmp);
2596}
2597
2598
2599
2600
2601
2602
2603
2604
2605void pci_configure_ari(struct pci_dev *dev)
2606{
2607 u32 cap;
2608 struct pci_dev *bridge;
2609
2610 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
2611 return;
2612
2613 bridge = dev->bus->self;
2614 if (!bridge)
2615 return;
2616
2617 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
2618 if (!(cap & PCI_EXP_DEVCAP2_ARI))
2619 return;
2620
2621 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
2622 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
2623 PCI_EXP_DEVCTL2_ARI);
2624 bridge->ari_enabled = 1;
2625 } else {
2626 pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
2627 PCI_EXP_DEVCTL2_ARI);
2628 bridge->ari_enabled = 0;
2629 }
2630}
2631
2632static int pci_acs_enable;
2633
2634
2635
2636
2637void pci_request_acs(void)
2638{
2639 pci_acs_enable = 1;
2640}
2641
2642
2643
2644
2645
2646static void pci_std_enable_acs(struct pci_dev *dev)
2647{
2648 int pos;
2649 u16 cap;
2650 u16 ctrl;
2651
2652 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
2653 if (!pos)
2654 return;
2655
2656 pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
2657 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
2658
2659
2660 ctrl |= (cap & PCI_ACS_SV);
2661
2662
2663 ctrl |= (cap & PCI_ACS_RR);
2664
2665
2666 ctrl |= (cap & PCI_ACS_CR);
2667
2668
2669 ctrl |= (cap & PCI_ACS_UF);
2670
2671 pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
2672}
2673
2674
2675
2676
2677
2678void pci_enable_acs(struct pci_dev *dev)
2679{
2680 if (!pci_acs_enable)
2681 return;
2682
2683 if (!pci_dev_specific_enable_acs(dev))
2684 return;
2685
2686 pci_std_enable_acs(dev);
2687}
2688
2689static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
2690{
2691 int pos;
2692 u16 cap, ctrl;
2693
2694 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
2695 if (!pos)
2696 return false;
2697
2698
2699
2700
2701
2702
2703 pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
2704 acs_flags &= (cap | PCI_ACS_EC);
2705
2706 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
2707 return (ctrl & acs_flags) == acs_flags;
2708}
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
2727{
2728 int ret;
2729
2730 ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
2731 if (ret >= 0)
2732 return ret > 0;
2733
2734
2735
2736
2737
2738
2739 if (!pci_is_pcie(pdev))
2740 return false;
2741
2742 switch (pci_pcie_type(pdev)) {
2743
2744
2745
2746
2747
2748 case PCI_EXP_TYPE_PCIE_BRIDGE:
2749
2750
2751
2752
2753
2754
2755 case PCI_EXP_TYPE_PCI_BRIDGE:
2756 case PCI_EXP_TYPE_RC_EC:
2757 return false;
2758
2759
2760
2761
2762
2763 case PCI_EXP_TYPE_DOWNSTREAM:
2764 case PCI_EXP_TYPE_ROOT_PORT:
2765 return pci_acs_flags_enabled(pdev, acs_flags);
2766
2767
2768
2769
2770
2771
2772
2773 case PCI_EXP_TYPE_ENDPOINT:
2774 case PCI_EXP_TYPE_UPSTREAM:
2775 case PCI_EXP_TYPE_LEG_END:
2776 case PCI_EXP_TYPE_RC_END:
2777 if (!pdev->multifunction)
2778 break;
2779
2780 return pci_acs_flags_enabled(pdev, acs_flags);
2781 }
2782
2783
2784
2785
2786
2787 return true;
2788}
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799bool pci_acs_path_enabled(struct pci_dev *start,
2800 struct pci_dev *end, u16 acs_flags)
2801{
2802 struct pci_dev *pdev, *parent = start;
2803
2804 do {
2805 pdev = parent;
2806
2807 if (!pci_acs_enabled(pdev, acs_flags))
2808 return false;
2809
2810 if (pci_is_root_bus(pdev->bus))
2811 return (end == NULL);
2812
2813 parent = pdev->bus->self;
2814 } while (pdev != end);
2815
2816 return true;
2817}
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
2831{
2832 int slot;
2833
2834 if (pci_ari_enabled(dev->bus))
2835 slot = 0;
2836 else
2837 slot = PCI_SLOT(dev->devfn);
2838
2839 return (((pin - 1) + slot) % 4) + 1;
2840}
2841
2842int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
2843{
2844 u8 pin;
2845
2846 pin = dev->pin;
2847 if (!pin)
2848 return -1;
2849
2850 while (!pci_is_root_bus(dev->bus)) {
2851 pin = pci_swizzle_interrupt_pin(dev, pin);
2852 dev = dev->bus->self;
2853 }
2854 *bridge = dev;
2855 return pin;
2856}
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
2867{
2868 u8 pin = *pinp;
2869
2870 while (!pci_is_root_bus(dev->bus)) {
2871 pin = pci_swizzle_interrupt_pin(dev, pin);
2872 dev = dev->bus->self;
2873 }
2874 *pinp = pin;
2875 return PCI_SLOT(dev->devfn);
2876}
2877EXPORT_SYMBOL_GPL(pci_common_swizzle);
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888void pci_release_region(struct pci_dev *pdev, int bar)
2889{
2890 struct pci_devres *dr;
2891
2892 if (pci_resource_len(pdev, bar) == 0)
2893 return;
2894 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
2895 release_region(pci_resource_start(pdev, bar),
2896 pci_resource_len(pdev, bar));
2897 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
2898 release_mem_region(pci_resource_start(pdev, bar),
2899 pci_resource_len(pdev, bar));
2900
2901 dr = find_pci_dr(pdev);
2902 if (dr)
2903 dr->region_mask &= ~(1 << bar);
2904}
2905EXPORT_SYMBOL(pci_release_region);
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926static int __pci_request_region(struct pci_dev *pdev, int bar,
2927 const char *res_name, int exclusive)
2928{
2929 struct pci_devres *dr;
2930
2931 if (pci_resource_len(pdev, bar) == 0)
2932 return 0;
2933
2934 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
2935 if (!request_region(pci_resource_start(pdev, bar),
2936 pci_resource_len(pdev, bar), res_name))
2937 goto err_out;
2938 } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
2939 if (!__request_mem_region(pci_resource_start(pdev, bar),
2940 pci_resource_len(pdev, bar), res_name,
2941 exclusive))
2942 goto err_out;
2943 }
2944
2945 dr = find_pci_dr(pdev);
2946 if (dr)
2947 dr->region_mask |= 1 << bar;
2948
2949 return 0;
2950
2951err_out:
2952 dev_warn(&pdev->dev, "BAR %d: can't reserve %pR\n", bar,
2953 &pdev->resource[bar]);
2954 return -EBUSY;
2955}
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
2972{
2973 return __pci_request_region(pdev, bar, res_name, 0);
2974}
2975EXPORT_SYMBOL(pci_request_region);
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995int pci_request_region_exclusive(struct pci_dev *pdev, int bar,
2996 const char *res_name)
2997{
2998 return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
2999}
3000EXPORT_SYMBOL(pci_request_region_exclusive);
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010void pci_release_selected_regions(struct pci_dev *pdev, int bars)
3011{
3012 int i;
3013
3014 for (i = 0; i < 6; i++)
3015 if (bars & (1 << i))
3016 pci_release_region(pdev, i);
3017}
3018EXPORT_SYMBOL(pci_release_selected_regions);
3019
3020static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
3021 const char *res_name, int excl)
3022{
3023 int i;
3024
3025 for (i = 0; i < 6; i++)
3026 if (bars & (1 << i))
3027 if (__pci_request_region(pdev, i, res_name, excl))
3028 goto err_out;
3029 return 0;
3030
3031err_out:
3032 while (--i >= 0)
3033 if (bars & (1 << i))
3034 pci_release_region(pdev, i);
3035
3036 return -EBUSY;
3037}
3038
3039
3040
3041
3042
3043
3044
3045
3046int pci_request_selected_regions(struct pci_dev *pdev, int bars,
3047 const char *res_name)
3048{
3049 return __pci_request_selected_regions(pdev, bars, res_name, 0);
3050}
3051EXPORT_SYMBOL(pci_request_selected_regions);
3052
3053int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
3054 const char *res_name)
3055{
3056 return __pci_request_selected_regions(pdev, bars, res_name,
3057 IORESOURCE_EXCLUSIVE);
3058}
3059EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070void pci_release_regions(struct pci_dev *pdev)
3071{
3072 pci_release_selected_regions(pdev, (1 << 6) - 1);
3073}
3074EXPORT_SYMBOL(pci_release_regions);
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089int pci_request_regions(struct pci_dev *pdev, const char *res_name)
3090{
3091 return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
3092}
3093EXPORT_SYMBOL(pci_request_regions);
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
3112{
3113 return pci_request_selected_regions_exclusive(pdev,
3114 ((1 << 6) - 1), res_name);
3115}
3116EXPORT_SYMBOL(pci_request_regions_exclusive);
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128int __weak pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
3129{
3130#if defined(PCI_IOBASE) && defined(CONFIG_MMU)
3131 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
3132
3133 if (!(res->flags & IORESOURCE_IO))
3134 return -EINVAL;
3135
3136 if (res->end > IO_SPACE_LIMIT)
3137 return -EINVAL;
3138
3139 return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
3140 pgprot_device(PAGE_KERNEL));
3141#else
3142
3143
3144 WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
3145 return -ENODEV;
3146#endif
3147}
3148
3149static void __pci_set_master(struct pci_dev *dev, bool enable)
3150{
3151 u16 old_cmd, cmd;
3152
3153 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
3154 if (enable)
3155 cmd = old_cmd | PCI_COMMAND_MASTER;
3156 else
3157 cmd = old_cmd & ~PCI_COMMAND_MASTER;
3158 if (cmd != old_cmd) {
3159 dev_dbg(&dev->dev, "%s bus mastering\n",
3160 enable ? "enabling" : "disabling");
3161 pci_write_config_word(dev, PCI_COMMAND, cmd);
3162 }
3163 dev->is_busmaster = enable;
3164}
3165
3166
3167
3168
3169
3170
3171
3172
3173char * __weak __init pcibios_setup(char *str)
3174{
3175 return str;
3176}
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186void __weak pcibios_set_master(struct pci_dev *dev)
3187{
3188 u8 lat;
3189
3190
3191 if (pci_is_pcie(dev))
3192 return;
3193
3194 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3195 if (lat < 16)
3196 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
3197 else if (lat > pcibios_max_latency)
3198 lat = pcibios_max_latency;
3199 else
3200 return;
3201
3202 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
3203}
3204
3205
3206
3207
3208
3209
3210
3211
3212void pci_set_master(struct pci_dev *dev)
3213{
3214 __pci_set_master(dev, true);
3215 pcibios_set_master(dev);
3216}
3217EXPORT_SYMBOL(pci_set_master);
3218
3219
3220
3221
3222
3223void pci_clear_master(struct pci_dev *dev)
3224{
3225 __pci_set_master(dev, false);
3226}
3227EXPORT_SYMBOL(pci_clear_master);
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239int pci_set_cacheline_size(struct pci_dev *dev)
3240{
3241 u8 cacheline_size;
3242
3243 if (!pci_cache_line_size)
3244 return -EINVAL;
3245
3246
3247
3248 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
3249 if (cacheline_size >= pci_cache_line_size &&
3250 (cacheline_size % pci_cache_line_size) == 0)
3251 return 0;
3252
3253
3254 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
3255
3256 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
3257 if (cacheline_size == pci_cache_line_size)
3258 return 0;
3259
3260 dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not supported\n",
3261 pci_cache_line_size << 2);
3262
3263 return -EINVAL;
3264}
3265EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275int pci_set_mwi(struct pci_dev *dev)
3276{
3277#ifdef PCI_DISABLE_MWI
3278 return 0;
3279#else
3280 int rc;
3281 u16 cmd;
3282
3283 rc = pci_set_cacheline_size(dev);
3284 if (rc)
3285 return rc;
3286
3287 pci_read_config_word(dev, PCI_COMMAND, &cmd);
3288 if (!(cmd & PCI_COMMAND_INVALIDATE)) {
3289 dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
3290 cmd |= PCI_COMMAND_INVALIDATE;
3291 pci_write_config_word(dev, PCI_COMMAND, cmd);
3292 }
3293 return 0;
3294#endif
3295}
3296EXPORT_SYMBOL(pci_set_mwi);
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307int pci_try_set_mwi(struct pci_dev *dev)
3308{
3309#ifdef PCI_DISABLE_MWI
3310 return 0;
3311#else
3312 return pci_set_mwi(dev);
3313#endif
3314}
3315EXPORT_SYMBOL(pci_try_set_mwi);
3316
3317
3318
3319
3320
3321
3322
3323void pci_clear_mwi(struct pci_dev *dev)
3324{
3325#ifndef PCI_DISABLE_MWI
3326 u16 cmd;
3327
3328 pci_read_config_word(dev, PCI_COMMAND, &cmd);
3329 if (cmd & PCI_COMMAND_INVALIDATE) {
3330 cmd &= ~PCI_COMMAND_INVALIDATE;
3331 pci_write_config_word(dev, PCI_COMMAND, cmd);
3332 }
3333#endif
3334}
3335EXPORT_SYMBOL(pci_clear_mwi);
3336
3337
3338
3339
3340
3341
3342
3343
3344void pci_intx(struct pci_dev *pdev, int enable)
3345{
3346 u16 pci_command, new;
3347
3348 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
3349
3350 if (enable)
3351 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
3352 else
3353 new = pci_command | PCI_COMMAND_INTX_DISABLE;
3354
3355 if (new != pci_command) {
3356 struct pci_devres *dr;
3357
3358 pci_write_config_word(pdev, PCI_COMMAND, new);
3359
3360 dr = find_pci_dr(pdev);
3361 if (dr && !dr->restore_intx) {
3362 dr->restore_intx = 1;
3363 dr->orig_intx = !enable;
3364 }
3365 }
3366}
3367EXPORT_SYMBOL_GPL(pci_intx);
3368
3369
3370
3371
3372
3373
3374
3375
3376bool pci_intx_mask_supported(struct pci_dev *dev)
3377{
3378 bool mask_supported = false;
3379 u16 orig, new;
3380
3381 if (dev->broken_intx_masking)
3382 return false;
3383
3384 pci_cfg_access_lock(dev);
3385
3386 pci_read_config_word(dev, PCI_COMMAND, &orig);
3387 pci_write_config_word(dev, PCI_COMMAND,
3388 orig ^ PCI_COMMAND_INTX_DISABLE);
3389 pci_read_config_word(dev, PCI_COMMAND, &new);
3390
3391
3392
3393
3394
3395
3396 if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
3397 dev_err(&dev->dev, "Command register changed from 0x%x to 0x%x: driver or hardware bug?\n",
3398 orig, new);
3399 } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) {
3400 mask_supported = true;
3401 pci_write_config_word(dev, PCI_COMMAND, orig);
3402 }
3403
3404 pci_cfg_access_unlock(dev);
3405 return mask_supported;
3406}
3407EXPORT_SYMBOL_GPL(pci_intx_mask_supported);
3408
3409static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
3410{
3411 struct pci_bus *bus = dev->bus;
3412 bool mask_updated = true;
3413 u32 cmd_status_dword;
3414 u16 origcmd, newcmd;
3415 unsigned long flags;
3416 bool irq_pending;
3417
3418
3419
3420
3421
3422 BUILD_BUG_ON(PCI_COMMAND % 4);
3423 BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
3424
3425 raw_spin_lock_irqsave(&pci_lock, flags);
3426
3427 bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
3428
3429 irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
3430
3431
3432
3433
3434
3435
3436 if (mask != irq_pending) {
3437 mask_updated = false;
3438 goto done;
3439 }
3440
3441 origcmd = cmd_status_dword;
3442 newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
3443 if (mask)
3444 newcmd |= PCI_COMMAND_INTX_DISABLE;
3445 if (newcmd != origcmd)
3446 bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
3447
3448done:
3449 raw_spin_unlock_irqrestore(&pci_lock, flags);
3450
3451 return mask_updated;
3452}
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462bool pci_check_and_mask_intx(struct pci_dev *dev)
3463{
3464 return pci_check_and_set_intx_mask(dev, true);
3465}
3466EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476bool pci_check_and_unmask_intx(struct pci_dev *dev)
3477{
3478 return pci_check_and_set_intx_mask(dev, false);
3479}
3480EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
3481
3482
3483
3484
3485
3486
3487
3488int pci_wait_for_pending_transaction(struct pci_dev *dev)
3489{
3490 if (!pci_is_pcie(dev))
3491 return 1;
3492
3493 return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
3494 PCI_EXP_DEVSTA_TRPND);
3495}
3496EXPORT_SYMBOL(pci_wait_for_pending_transaction);
3497
3498
3499
3500
3501
3502
3503
3504static void pci_flr_wait(struct pci_dev *dev)
3505{
3506 int i = 0;
3507 u32 id;
3508
3509 do {
3510 msleep(100);
3511 pci_read_config_dword(dev, PCI_COMMAND, &id);
3512 } while (i++ < 10 && id == ~0);
3513
3514 if (id == ~0)
3515 dev_warn(&dev->dev, "Failed to return from FLR\n");
3516 else if (i > 1)
3517 dev_info(&dev->dev, "Required additional %dms to return from FLR\n",
3518 (i - 1) * 100);
3519}
3520
3521
3522
3523
3524
3525
3526
3527
3528static bool pcie_has_flr(struct pci_dev *dev)
3529{
3530 u32 cap;
3531
3532 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
3533 return false;
3534
3535 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
3536 return cap & PCI_EXP_DEVCAP_FLR;
3537}
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547void pcie_flr(struct pci_dev *dev)
3548{
3549 if (!pci_wait_for_pending_transaction(dev))
3550 dev_err(&dev->dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
3551
3552 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
3553 pci_flr_wait(dev);
3554}
3555EXPORT_SYMBOL_GPL(pcie_flr);
3556
3557static int pci_af_flr(struct pci_dev *dev, int probe)
3558{
3559 int pos;
3560 u8 cap;
3561
3562 pos = pci_find_capability(dev, PCI_CAP_ID_AF);
3563 if (!pos)
3564 return -ENOTTY;
3565
3566 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
3567 return -ENOTTY;
3568
3569 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
3570 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
3571 return -ENOTTY;
3572
3573 if (probe)
3574 return 0;
3575
3576
3577
3578
3579
3580
3581 if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
3582 PCI_AF_STATUS_TP << 8))
3583 dev_err(&dev->dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
3584
3585 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
3586 pci_flr_wait(dev);
3587 return 0;
3588}
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605static int pci_pm_reset(struct pci_dev *dev, int probe)
3606{
3607 u16 csr;
3608
3609 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
3610 return -ENOTTY;
3611
3612 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
3613 if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
3614 return -ENOTTY;
3615
3616 if (probe)
3617 return 0;
3618
3619 if (dev->current_state != PCI_D0)
3620 return -EINVAL;
3621
3622 csr &= ~PCI_PM_CTRL_STATE_MASK;
3623 csr |= PCI_D3hot;
3624 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
3625 pci_dev_d3_sleep(dev);
3626
3627 csr &= ~PCI_PM_CTRL_STATE_MASK;
3628 csr |= PCI_D0;
3629 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
3630 pci_dev_d3_sleep(dev);
3631
3632 return 0;
3633}
3634
3635void pci_reset_secondary_bus(struct pci_dev *dev)
3636{
3637 u16 ctrl;
3638
3639 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
3640 ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
3641 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
3642
3643
3644
3645
3646 msleep(2);
3647
3648 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
3649 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
3650
3651
3652
3653
3654
3655
3656
3657
3658 ssleep(1);
3659}
3660
3661void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
3662{
3663 pci_reset_secondary_bus(dev);
3664}
3665
3666
3667
3668
3669
3670
3671
3672
3673void pci_reset_bridge_secondary_bus(struct pci_dev *dev)
3674{
3675 pcibios_reset_secondary_bus(dev);
3676}
3677EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus);
3678
3679static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
3680{
3681 struct pci_dev *pdev;
3682
3683 if (pci_is_root_bus(dev->bus) || dev->subordinate ||
3684 !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
3685 return -ENOTTY;
3686
3687 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
3688 if (pdev != dev)
3689 return -ENOTTY;
3690
3691 if (probe)
3692 return 0;
3693
3694 pci_reset_bridge_secondary_bus(dev->bus->self);
3695
3696 return 0;
3697}
3698
3699static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
3700{
3701 int rc = -ENOTTY;
3702
3703 if (!hotplug || !try_module_get(hotplug->ops->owner))
3704 return rc;
3705
3706 if (hotplug->ops->reset_slot)
3707 rc = hotplug->ops->reset_slot(hotplug, probe);
3708
3709 module_put(hotplug->ops->owner);
3710
3711 return rc;
3712}
3713
3714static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
3715{
3716 struct pci_dev *pdev;
3717
3718 if (dev->subordinate || !dev->slot ||
3719 dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
3720 return -ENOTTY;
3721
3722 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
3723 if (pdev != dev && pdev->slot == dev->slot)
3724 return -ENOTTY;
3725
3726 return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
3727}
3728
3729static int __pci_dev_reset(struct pci_dev *dev, int probe)
3730{
3731 int rc;
3732
3733 might_sleep();
3734
3735 rc = pci_dev_specific_reset(dev, probe);
3736 if (rc != -ENOTTY)
3737 goto done;
3738
3739 if (pcie_has_flr(dev)) {
3740 if (!probe)
3741 pcie_flr(dev);
3742 rc = 0;
3743 goto done;
3744 }
3745
3746 rc = pci_af_flr(dev, probe);
3747 if (rc != -ENOTTY)
3748 goto done;
3749
3750 rc = pci_pm_reset(dev, probe);
3751 if (rc != -ENOTTY)
3752 goto done;
3753
3754 rc = pci_dev_reset_slot_function(dev, probe);
3755 if (rc != -ENOTTY)
3756 goto done;
3757
3758 rc = pci_parent_bus_reset(dev, probe);
3759done:
3760 return rc;
3761}
3762
3763static void pci_dev_lock(struct pci_dev *dev)
3764{
3765 pci_cfg_access_lock(dev);
3766
3767 device_lock(&dev->dev);
3768}
3769
3770
3771static int pci_dev_trylock(struct pci_dev *dev)
3772{
3773 if (pci_cfg_access_trylock(dev)) {
3774 if (device_trylock(&dev->dev))
3775 return 1;
3776 pci_cfg_access_unlock(dev);
3777 }
3778
3779 return 0;
3780}
3781
3782static void pci_dev_unlock(struct pci_dev *dev)
3783{
3784 device_unlock(&dev->dev);
3785 pci_cfg_access_unlock(dev);
3786}
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797static void pci_reset_notify(struct pci_dev *dev, bool prepare)
3798{
3799 const struct pci_error_handlers *err_handler =
3800 dev->driver ? dev->driver->err_handler : NULL;
3801 if (err_handler && dev->driver->pci_driver_rh
3802 && dev->driver->pci_driver_rh->reset_notify)
3803 dev->driver->pci_driver_rh->reset_notify(dev, prepare);
3804}
3805
3806static void pci_dev_save_and_disable(struct pci_dev *dev)
3807{
3808 pci_reset_notify(dev, true);
3809
3810
3811
3812
3813
3814
3815 pci_set_power_state(dev, PCI_D0);
3816
3817 pci_save_state(dev);
3818
3819
3820
3821
3822
3823
3824
3825 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
3826}
3827
3828static void pci_dev_restore(struct pci_dev *dev)
3829{
3830 pci_restore_state(dev);
3831 pci_reset_notify(dev, false);
3832}
3833
3834static int pci_dev_reset(struct pci_dev *dev, int probe)
3835{
3836 int rc;
3837
3838 if (!probe)
3839 pci_dev_lock(dev);
3840
3841 rc = __pci_dev_reset(dev, probe);
3842
3843 if (!probe)
3844 pci_dev_unlock(dev);
3845
3846 return rc;
3847}
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866int __pci_reset_function(struct pci_dev *dev)
3867{
3868 return pci_dev_reset(dev, 0);
3869}
3870EXPORT_SYMBOL_GPL(__pci_reset_function);
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891int __pci_reset_function_locked(struct pci_dev *dev)
3892{
3893 return __pci_dev_reset(dev, 0);
3894}
3895EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908int pci_probe_reset_function(struct pci_dev *dev)
3909{
3910 return pci_dev_reset(dev, 1);
3911}
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929int pci_reset_function(struct pci_dev *dev)
3930{
3931 int rc;
3932
3933 rc = pci_dev_reset(dev, 1);
3934 if (rc)
3935 return rc;
3936
3937 pci_dev_save_and_disable(dev);
3938
3939 rc = pci_dev_reset(dev, 0);
3940
3941 pci_dev_restore(dev);
3942
3943 return rc;
3944}
3945EXPORT_SYMBOL_GPL(pci_reset_function);
3946
3947
3948
3949
3950
3951
3952
3953int pci_try_reset_function(struct pci_dev *dev)
3954{
3955 int rc;
3956
3957 rc = pci_dev_reset(dev, 1);
3958 if (rc)
3959 return rc;
3960
3961 pci_dev_save_and_disable(dev);
3962
3963 if (pci_dev_trylock(dev)) {
3964 rc = __pci_dev_reset(dev, 0);
3965 pci_dev_unlock(dev);
3966 } else
3967 rc = -EAGAIN;
3968
3969 pci_dev_restore(dev);
3970
3971 return rc;
3972}
3973EXPORT_SYMBOL_GPL(pci_try_reset_function);
3974
3975
3976static bool pci_bus_resetable(struct pci_bus *bus)
3977{
3978 struct pci_dev *dev;
3979
3980 list_for_each_entry(dev, &bus->devices, bus_list) {
3981 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
3982 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
3983 return false;
3984 }
3985
3986 return true;
3987}
3988
3989
3990static void pci_bus_lock(struct pci_bus *bus)
3991{
3992 struct pci_dev *dev;
3993
3994 list_for_each_entry(dev, &bus->devices, bus_list) {
3995 pci_dev_lock(dev);
3996 if (dev->subordinate)
3997 pci_bus_lock(dev->subordinate);
3998 }
3999}
4000
4001
4002static void pci_bus_unlock(struct pci_bus *bus)
4003{
4004 struct pci_dev *dev;
4005
4006 list_for_each_entry(dev, &bus->devices, bus_list) {
4007 if (dev->subordinate)
4008 pci_bus_unlock(dev->subordinate);
4009 pci_dev_unlock(dev);
4010 }
4011}
4012
4013
4014static int pci_bus_trylock(struct pci_bus *bus)
4015{
4016 struct pci_dev *dev;
4017
4018 list_for_each_entry(dev, &bus->devices, bus_list) {
4019 if (!pci_dev_trylock(dev))
4020 goto unlock;
4021 if (dev->subordinate) {
4022 if (!pci_bus_trylock(dev->subordinate)) {
4023 pci_dev_unlock(dev);
4024 goto unlock;
4025 }
4026 }
4027 }
4028 return 1;
4029
4030unlock:
4031 list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
4032 if (dev->subordinate)
4033 pci_bus_unlock(dev->subordinate);
4034 pci_dev_unlock(dev);
4035 }
4036 return 0;
4037}
4038
4039
4040static bool pci_slot_resetable(struct pci_slot *slot)
4041{
4042 struct pci_dev *dev;
4043
4044 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4045 if (!dev->slot || dev->slot != slot)
4046 continue;
4047 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
4048 (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
4049 return false;
4050 }
4051
4052 return true;
4053}
4054
4055
4056static void pci_slot_lock(struct pci_slot *slot)
4057{
4058 struct pci_dev *dev;
4059
4060 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4061 if (!dev->slot || dev->slot != slot)
4062 continue;
4063 pci_dev_lock(dev);
4064 if (dev->subordinate)
4065 pci_bus_lock(dev->subordinate);
4066 }
4067}
4068
4069
4070static void pci_slot_unlock(struct pci_slot *slot)
4071{
4072 struct pci_dev *dev;
4073
4074 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4075 if (!dev->slot || dev->slot != slot)
4076 continue;
4077 if (dev->subordinate)
4078 pci_bus_unlock(dev->subordinate);
4079 pci_dev_unlock(dev);
4080 }
4081}
4082
4083
4084static int pci_slot_trylock(struct pci_slot *slot)
4085{
4086 struct pci_dev *dev;
4087
4088 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4089 if (!dev->slot || dev->slot != slot)
4090 continue;
4091 if (!pci_dev_trylock(dev))
4092 goto unlock;
4093 if (dev->subordinate) {
4094 if (!pci_bus_trylock(dev->subordinate)) {
4095 pci_dev_unlock(dev);
4096 goto unlock;
4097 }
4098 }
4099 }
4100 return 1;
4101
4102unlock:
4103 list_for_each_entry_continue_reverse(dev,
4104 &slot->bus->devices, bus_list) {
4105 if (!dev->slot || dev->slot != slot)
4106 continue;
4107 if (dev->subordinate)
4108 pci_bus_unlock(dev->subordinate);
4109 pci_dev_unlock(dev);
4110 }
4111 return 0;
4112}
4113
4114
4115static void pci_bus_save_and_disable(struct pci_bus *bus)
4116{
4117 struct pci_dev *dev;
4118
4119 list_for_each_entry(dev, &bus->devices, bus_list) {
4120 pci_dev_save_and_disable(dev);
4121 if (dev->subordinate)
4122 pci_bus_save_and_disable(dev->subordinate);
4123 }
4124}
4125
4126
4127
4128
4129
4130static void pci_bus_restore(struct pci_bus *bus)
4131{
4132 struct pci_dev *dev;
4133
4134 list_for_each_entry(dev, &bus->devices, bus_list) {
4135 pci_dev_restore(dev);
4136 if (dev->subordinate)
4137 pci_bus_restore(dev->subordinate);
4138 }
4139}
4140
4141
4142static void pci_slot_save_and_disable(struct pci_slot *slot)
4143{
4144 struct pci_dev *dev;
4145
4146 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4147 if (!dev->slot || dev->slot != slot)
4148 continue;
4149 pci_dev_save_and_disable(dev);
4150 if (dev->subordinate)
4151 pci_bus_save_and_disable(dev->subordinate);
4152 }
4153}
4154
4155
4156
4157
4158
4159static void pci_slot_restore(struct pci_slot *slot)
4160{
4161 struct pci_dev *dev;
4162
4163 list_for_each_entry(dev, &slot->bus->devices, bus_list) {
4164 if (!dev->slot || dev->slot != slot)
4165 continue;
4166 pci_dev_restore(dev);
4167 if (dev->subordinate)
4168 pci_bus_restore(dev->subordinate);
4169 }
4170}
4171
4172static int pci_slot_reset(struct pci_slot *slot, int probe)
4173{
4174 int rc;
4175
4176 if (!slot || !pci_slot_resetable(slot))
4177 return -ENOTTY;
4178
4179 if (!probe)
4180 pci_slot_lock(slot);
4181
4182 might_sleep();
4183
4184 rc = pci_reset_hotplug_slot(slot->hotplug, probe);
4185
4186 if (!probe)
4187 pci_slot_unlock(slot);
4188
4189 return rc;
4190}
4191
4192
4193
4194
4195
4196
4197
4198int pci_probe_reset_slot(struct pci_slot *slot)
4199{
4200 return pci_slot_reset(slot, 1);
4201}
4202EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219int pci_reset_slot(struct pci_slot *slot)
4220{
4221 int rc;
4222
4223 rc = pci_slot_reset(slot, 1);
4224 if (rc)
4225 return rc;
4226
4227 pci_slot_save_and_disable(slot);
4228
4229 rc = pci_slot_reset(slot, 0);
4230
4231 pci_slot_restore(slot);
4232
4233 return rc;
4234}
4235EXPORT_SYMBOL_GPL(pci_reset_slot);
4236
4237
4238
4239
4240
4241
4242
4243int pci_try_reset_slot(struct pci_slot *slot)
4244{
4245 int rc;
4246
4247 rc = pci_slot_reset(slot, 1);
4248 if (rc)
4249 return rc;
4250
4251 pci_slot_save_and_disable(slot);
4252
4253 if (pci_slot_trylock(slot)) {
4254 might_sleep();
4255 rc = pci_reset_hotplug_slot(slot->hotplug, 0);
4256 pci_slot_unlock(slot);
4257 } else
4258 rc = -EAGAIN;
4259
4260 pci_slot_restore(slot);
4261
4262 return rc;
4263}
4264EXPORT_SYMBOL_GPL(pci_try_reset_slot);
4265
4266static int pci_bus_reset(struct pci_bus *bus, int probe)
4267{
4268 if (!bus->self || !pci_bus_resetable(bus))
4269 return -ENOTTY;
4270
4271 if (probe)
4272 return 0;
4273
4274 pci_bus_lock(bus);
4275
4276 might_sleep();
4277
4278 pci_reset_bridge_secondary_bus(bus->self);
4279
4280 pci_bus_unlock(bus);
4281
4282 return 0;
4283}
4284
4285
4286
4287
4288
4289
4290
4291int pci_probe_reset_bus(struct pci_bus *bus)
4292{
4293 return pci_bus_reset(bus, 1);
4294}
4295EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306int pci_reset_bus(struct pci_bus *bus)
4307{
4308 int rc;
4309
4310 rc = pci_bus_reset(bus, 1);
4311 if (rc)
4312 return rc;
4313
4314 pci_bus_save_and_disable(bus);
4315
4316 rc = pci_bus_reset(bus, 0);
4317
4318 pci_bus_restore(bus);
4319
4320 return rc;
4321}
4322EXPORT_SYMBOL_GPL(pci_reset_bus);
4323
4324
4325
4326
4327
4328
4329
4330int pci_try_reset_bus(struct pci_bus *bus)
4331{
4332 int rc;
4333
4334 rc = pci_bus_reset(bus, 1);
4335 if (rc)
4336 return rc;
4337
4338 pci_bus_save_and_disable(bus);
4339
4340 if (pci_bus_trylock(bus)) {
4341 might_sleep();
4342 pci_reset_bridge_secondary_bus(bus->self);
4343 pci_bus_unlock(bus);
4344 } else
4345 rc = -EAGAIN;
4346
4347 pci_bus_restore(bus);
4348
4349 return rc;
4350}
4351EXPORT_SYMBOL_GPL(pci_try_reset_bus);
4352
4353
4354
4355
4356
4357
4358
4359
4360int pcix_get_max_mmrbc(struct pci_dev *dev)
4361{
4362 int cap;
4363 u32 stat;
4364
4365 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4366 if (!cap)
4367 return -EINVAL;
4368
4369 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
4370 return -EINVAL;
4371
4372 return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
4373}
4374EXPORT_SYMBOL(pcix_get_max_mmrbc);
4375
4376
4377
4378
4379
4380
4381
4382
4383int pcix_get_mmrbc(struct pci_dev *dev)
4384{
4385 int cap;
4386 u16 cmd;
4387
4388 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4389 if (!cap)
4390 return -EINVAL;
4391
4392 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
4393 return -EINVAL;
4394
4395 return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
4396}
4397EXPORT_SYMBOL(pcix_get_mmrbc);
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
4409{
4410 int cap;
4411 u32 stat, v, o;
4412 u16 cmd;
4413
4414 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
4415 return -EINVAL;
4416
4417 v = ffs(mmrbc) - 10;
4418
4419 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4420 if (!cap)
4421 return -EINVAL;
4422
4423 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
4424 return -EINVAL;
4425
4426 if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
4427 return -E2BIG;
4428
4429 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
4430 return -EINVAL;
4431
4432 o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
4433 if (o != v) {
4434 if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
4435 return -EIO;
4436
4437 cmd &= ~PCI_X_CMD_MAX_READ;
4438 cmd |= v << 2;
4439 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
4440 return -EIO;
4441 }
4442 return 0;
4443}
4444EXPORT_SYMBOL(pcix_set_mmrbc);
4445
4446
4447
4448
4449
4450
4451
4452
4453int pcie_get_readrq(struct pci_dev *dev)
4454{
4455 u16 ctl;
4456
4457 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
4458
4459 return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
4460}
4461EXPORT_SYMBOL(pcie_get_readrq);
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471int pcie_set_readrq(struct pci_dev *dev, int rq)
4472{
4473 u16 v;
4474
4475 if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
4476 return -EINVAL;
4477
4478
4479
4480
4481
4482
4483
4484 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
4485 int mps = pcie_get_mps(dev);
4486
4487 if (mps < rq)
4488 rq = mps;
4489 }
4490
4491 v = (ffs(rq) - 8) << 12;
4492
4493 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
4494 PCI_EXP_DEVCTL_READRQ, v);
4495}
4496EXPORT_SYMBOL(pcie_set_readrq);
4497
4498
4499
4500
4501
4502
4503
4504int pcie_get_mps(struct pci_dev *dev)
4505{
4506 u16 ctl;
4507
4508 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
4509
4510 return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
4511}
4512EXPORT_SYMBOL(pcie_get_mps);
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522int pcie_set_mps(struct pci_dev *dev, int mps)
4523{
4524 u16 v;
4525
4526 if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
4527 return -EINVAL;
4528
4529 v = ffs(mps) - 8;
4530 if (v > dev->pcie_mpss)
4531 return -EINVAL;
4532 v <<= 5;
4533
4534 return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
4535 PCI_EXP_DEVCTL_PAYLOAD, v);
4536}
4537EXPORT_SYMBOL(pcie_set_mps);
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548int pcie_get_minimum_link(struct pci_dev *dev, enum pci_bus_speed *speed,
4549 enum pcie_link_width *width)
4550{
4551 int ret;
4552
4553 *speed = PCI_SPEED_UNKNOWN;
4554 *width = PCIE_LNK_WIDTH_UNKNOWN;
4555
4556 while (dev) {
4557 u16 lnksta;
4558 enum pci_bus_speed next_speed;
4559 enum pcie_link_width next_width;
4560
4561 ret = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
4562 if (ret)
4563 return ret;
4564
4565 next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
4566 next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
4567 PCI_EXP_LNKSTA_NLW_SHIFT;
4568
4569 if (next_speed < *speed)
4570 *speed = next_speed;
4571
4572 if (next_width < *width)
4573 *width = next_width;
4574
4575 dev = dev->bus->self;
4576 }
4577
4578 return 0;
4579}
4580EXPORT_SYMBOL(pcie_get_minimum_link);
4581
4582
4583
4584
4585
4586
4587
4588
4589int pci_select_bars(struct pci_dev *dev, unsigned long flags)
4590{
4591 int i, bars = 0;
4592 for (i = 0; i < PCI_NUM_RESOURCES; i++)
4593 if (pci_resource_flags(dev, i) & flags)
4594 bars |= (1 << i);
4595 return bars;
4596}
4597EXPORT_SYMBOL(pci_select_bars);
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
4608{
4609 int reg;
4610
4611 if (resno < PCI_ROM_RESOURCE) {
4612 *type = pci_bar_unknown;
4613 return PCI_BASE_ADDRESS_0 + 4 * resno;
4614 } else if (resno == PCI_ROM_RESOURCE) {
4615 *type = pci_bar_mem32;
4616 return dev->rom_base_reg;
4617 } else if (resno < PCI_BRIDGE_RESOURCES) {
4618
4619 *type = pci_bar_unknown;
4620 reg = pci_iov_resource_bar(dev, resno);
4621 if (reg)
4622 return reg;
4623 }
4624
4625 dev_err(&dev->dev, "BAR %d: invalid resource\n", resno);
4626 return 0;
4627}
4628
4629
4630static arch_set_vga_state_t arch_set_vga_state;
4631
4632void __init pci_register_set_vga_state(arch_set_vga_state_t func)
4633{
4634 arch_set_vga_state = func;
4635}
4636
4637static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
4638 unsigned int command_bits, u32 flags)
4639{
4640 if (arch_set_vga_state)
4641 return arch_set_vga_state(dev, decode, command_bits,
4642 flags);
4643 return 0;
4644}
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654int pci_set_vga_state(struct pci_dev *dev, bool decode,
4655 unsigned int command_bits, u32 flags)
4656{
4657 struct pci_bus *bus;
4658 struct pci_dev *bridge;
4659 u16 cmd;
4660 int rc;
4661
4662 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
4663
4664
4665 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
4666 if (rc)
4667 return rc;
4668
4669 if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
4670 pci_read_config_word(dev, PCI_COMMAND, &cmd);
4671 if (decode == true)
4672 cmd |= command_bits;
4673 else
4674 cmd &= ~command_bits;
4675 pci_write_config_word(dev, PCI_COMMAND, cmd);
4676 }
4677
4678 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
4679 return 0;
4680
4681 bus = dev->bus;
4682 while (bus) {
4683 bridge = bus->self;
4684 if (bridge) {
4685 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
4686 &cmd);
4687 if (decode == true)
4688 cmd |= PCI_BRIDGE_CTL_VGA;
4689 else
4690 cmd &= ~PCI_BRIDGE_CTL_VGA;
4691 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
4692 cmd);
4693 }
4694 bus = bus->parent;
4695 }
4696 return 0;
4697}
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707void pci_add_dma_alias(struct pci_dev *dev, u8 devfn)
4708{
4709 if (!dev->pci_dev_rh->dma_alias_mask)
4710 dev->pci_dev_rh->dma_alias_mask = kcalloc(BITS_TO_LONGS(U8_MAX),
4711 sizeof(long), GFP_KERNEL);
4712 if (!dev->pci_dev_rh->dma_alias_mask) {
4713 dev_warn(&dev->dev, "Unable to allocate DMA alias mask\n");
4714 return;
4715 }
4716 set_bit(devfn, dev->pci_dev_rh->dma_alias_mask);
4717 dev_info(&dev->dev, "Enabling fixed DMA alias to %02x.%d\n",
4718 PCI_SLOT(devfn), PCI_FUNC(devfn));
4719}
4720
4721bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)
4722{
4723 return (dev1->pci_dev_rh->dma_alias_mask &&
4724 test_bit(dev2->devfn, dev1->pci_dev_rh->dma_alias_mask)) ||
4725 (dev2->pci_dev_rh->dma_alias_mask &&
4726 test_bit(dev1->devfn, dev2->pci_dev_rh->dma_alias_mask));
4727}
4728
4729bool pci_device_is_present(struct pci_dev *pdev)
4730{
4731 u32 v;
4732
4733 return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
4734}
4735EXPORT_SYMBOL_GPL(pci_device_is_present);
4736
4737void pci_ignore_hotplug(struct pci_dev *dev)
4738{
4739 struct pci_dev *bridge = dev->bus->self;
4740
4741 dev->ignore_hotplug = 1;
4742
4743 if (bridge)
4744 bridge->ignore_hotplug = 1;
4745}
4746EXPORT_SYMBOL_GPL(pci_ignore_hotplug);
4747
4748#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
4749static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
4750static DEFINE_SPINLOCK(resource_alignment_lock);
4751
4752
4753
4754
4755
4756
4757
4758
4759static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
4760{
4761 int seg, bus, slot, func, align_order, count;
4762 resource_size_t align = 0;
4763 char *p;
4764
4765 spin_lock(&resource_alignment_lock);
4766 p = resource_alignment_param;
4767 while (*p) {
4768 count = 0;
4769 if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
4770 p[count] == '@') {
4771 p += count + 1;
4772 } else {
4773 align_order = -1;
4774 }
4775 if (sscanf(p, "%x:%x:%x.%x%n",
4776 &seg, &bus, &slot, &func, &count) != 4) {
4777 seg = 0;
4778 if (sscanf(p, "%x:%x.%x%n",
4779 &bus, &slot, &func, &count) != 3) {
4780
4781 printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
4782 p);
4783 break;
4784 }
4785 }
4786 p += count;
4787 if (seg == pci_domain_nr(dev->bus) &&
4788 bus == dev->bus->number &&
4789 slot == PCI_SLOT(dev->devfn) &&
4790 func == PCI_FUNC(dev->devfn)) {
4791 if (align_order == -1)
4792 align = PAGE_SIZE;
4793 else
4794 align = 1 << align_order;
4795
4796 break;
4797 }
4798 if (*p != ';' && *p != ',') {
4799
4800 break;
4801 }
4802 p++;
4803 }
4804 spin_unlock(&resource_alignment_lock);
4805 return align;
4806}
4807
4808
4809
4810
4811
4812
4813
4814
4815void pci_reassigndev_resource_alignment(struct pci_dev *dev)
4816{
4817 int i;
4818 struct resource *r;
4819 resource_size_t align, size;
4820 u16 command;
4821
4822
4823 align = pci_specified_resource_alignment(dev);
4824 if (!align)
4825 return;
4826
4827 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
4828 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
4829 dev_warn(&dev->dev,
4830 "Can't reassign resources to host bridge.\n");
4831 return;
4832 }
4833
4834 dev_info(&dev->dev,
4835 "Disabling memory decoding and releasing memory resources.\n");
4836 pci_read_config_word(dev, PCI_COMMAND, &command);
4837 command &= ~PCI_COMMAND_MEMORY;
4838 pci_write_config_word(dev, PCI_COMMAND, command);
4839
4840 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
4841 r = &dev->resource[i];
4842 if (!(r->flags & IORESOURCE_MEM))
4843 continue;
4844 size = resource_size(r);
4845 if (size < align) {
4846 size = align;
4847 dev_info(&dev->dev,
4848 "Rounding up size of resource #%d to %#llx.\n",
4849 i, (unsigned long long)size);
4850 }
4851 r->flags |= IORESOURCE_UNSET;
4852 r->end = size - 1;
4853 r->start = 0;
4854 }
4855
4856
4857
4858
4859 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
4860 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
4861 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
4862 r = &dev->resource[i];
4863 if (!(r->flags & IORESOURCE_MEM))
4864 continue;
4865 r->flags |= IORESOURCE_UNSET;
4866 r->end = resource_size(r) - 1;
4867 r->start = 0;
4868 }
4869 pci_disable_bridge_window(dev);
4870 }
4871}
4872
4873static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
4874{
4875 if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
4876 count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
4877 spin_lock(&resource_alignment_lock);
4878 strncpy(resource_alignment_param, buf, count);
4879 resource_alignment_param[count] = '\0';
4880 spin_unlock(&resource_alignment_lock);
4881 return count;
4882}
4883
4884static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
4885{
4886 size_t count;
4887 spin_lock(&resource_alignment_lock);
4888 count = snprintf(buf, size, "%s", resource_alignment_param);
4889 spin_unlock(&resource_alignment_lock);
4890 return count;
4891}
4892
4893static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
4894{
4895 return pci_get_resource_alignment_param(buf, PAGE_SIZE);
4896}
4897
4898static ssize_t pci_resource_alignment_store(struct bus_type *bus,
4899 const char *buf, size_t count)
4900{
4901 return pci_set_resource_alignment_param(buf, count);
4902}
4903
4904BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
4905 pci_resource_alignment_store);
4906
4907static int __init pci_resource_alignment_sysfs_init(void)
4908{
4909 return bus_create_file(&pci_bus_type,
4910 &bus_attr_resource_alignment);
4911}
4912late_initcall(pci_resource_alignment_sysfs_init);
4913
4914static void pci_no_domains(void)
4915{
4916#ifdef CONFIG_PCI_DOMAINS
4917 pci_domains_supported = 0;
4918#endif
4919}
4920
4921
4922
4923
4924
4925
4926
4927
4928int __weak pci_ext_cfg_avail(void)
4929{
4930 return 1;
4931}
4932
4933void __weak pci_fixup_cardbus(struct pci_bus *bus)
4934{
4935}
4936EXPORT_SYMBOL(pci_fixup_cardbus);
4937
4938static int __init pci_setup(char *str)
4939{
4940 while (str) {
4941 char *k = strchr(str, ',');
4942 if (k)
4943 *k++ = 0;
4944 if (*str && (str = pcibios_setup(str)) && *str) {
4945 if (!strcmp(str, "nomsi")) {
4946 pci_no_msi();
4947 } else if (!strcmp(str, "noaer")) {
4948 pci_no_aer();
4949 } else if (!strncmp(str, "realloc=", 8)) {
4950 pci_realloc_get_opt(str + 8);
4951 } else if (!strncmp(str, "realloc", 7)) {
4952 pci_realloc_get_opt("on");
4953 } else if (!strcmp(str, "nodomains")) {
4954 pci_no_domains();
4955 } else if (!strncmp(str, "noari", 5)) {
4956 pcie_ari_disabled = true;
4957 } else if (!strncmp(str, "cbiosize=", 9)) {
4958 pci_cardbus_io_size = memparse(str + 9, &str);
4959 } else if (!strncmp(str, "cbmemsize=", 10)) {
4960 pci_cardbus_mem_size = memparse(str + 10, &str);
4961 } else if (!strncmp(str, "resource_alignment=", 19)) {
4962 pci_set_resource_alignment_param(str + 19,
4963 strlen(str + 19));
4964 } else if (!strncmp(str, "ecrc=", 5)) {
4965 pcie_ecrc_get_policy(str + 5);
4966 } else if (!strncmp(str, "hpiosize=", 9)) {
4967 pci_hotplug_io_size = memparse(str + 9, &str);
4968 } else if (!strncmp(str, "hpmemsize=", 10)) {
4969 pci_hotplug_mem_size = memparse(str + 10, &str);
4970 } else if (!strncmp(str, "hpbussize=", 10)) {
4971 pci_hotplug_bus_size =
4972 simple_strtoul(str + 10, &str, 0);
4973 if (pci_hotplug_bus_size > 0xff)
4974 pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
4975 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
4976 pcie_bus_config = PCIE_BUS_TUNE_OFF;
4977 } else if (!strncmp(str, "pcie_bus_safe", 13)) {
4978 pcie_bus_config = PCIE_BUS_SAFE;
4979 } else if (!strncmp(str, "pcie_bus_perf", 13)) {
4980 pcie_bus_config = PCIE_BUS_PERFORMANCE;
4981 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
4982 pcie_bus_config = PCIE_BUS_PEER2PEER;
4983 } else if (!strncmp(str, "pcie_scan_all", 13)) {
4984 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
4985 } else {
4986 printk(KERN_ERR "PCI: Unknown option `%s'\n",
4987 str);
4988 }
4989 }
4990 str = k;
4991 }
4992 return 0;
4993}
4994early_param("pci", pci_setup);
4995